netdev: New function netdev_get_dpif_port().
[sliver-openvswitch.git] / lib / netdev-vport.c
1 /*
2  * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "netdev-vport.h"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <sys/socket.h>
24 #include <linux/openvswitch.h>
25 #include <linux/rtnetlink.h>
26 #include <net/if.h>
27 #include <sys/ioctl.h>
28
29 #include "byte-order.h"
30 #include "daemon.h"
31 #include "dirs.h"
32 #include "dpif.h"
33 #include "dpif-linux.h"
34 #include "hash.h"
35 #include "hmap.h"
36 #include "list.h"
37 #include "netdev-linux.h"
38 #include "netdev-provider.h"
39 #include "netlink.h"
40 #include "netlink-notifier.h"
41 #include "netlink-socket.h"
42 #include "ofpbuf.h"
43 #include "openvswitch/tunnel.h"
44 #include "packets.h"
45 #include "route-table.h"
46 #include "shash.h"
47 #include "socket-util.h"
48 #include "unaligned.h"
49 #include "vlog.h"
50
51 VLOG_DEFINE_THIS_MODULE(netdev_vport);
52
53 /* Default to the OTV port, per the VXLAN IETF draft. */
54 #define VXLAN_DST_PORT 8472
55
56 #define DEFAULT_TTL 64
57
58 struct netdev_dev_vport {
59     struct netdev_dev netdev_dev;
60     unsigned int change_seq;
61     uint8_t etheraddr[ETH_ADDR_LEN];
62
63     /* Tunnels. */
64     struct ofpbuf *options;
65     struct netdev_tunnel_config tnl_cfg;
66
67     /* Patch Ports. */
68     struct netdev_stats stats;
69     char *peer;
70 };
71
72 struct vport_class {
73     enum ovs_vport_type type;
74     struct netdev_class netdev_class;
75 };
76
77 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
78
79 static int netdev_vport_create(const struct netdev_class *, const char *,
80                                struct netdev_dev **);
81 static int get_patch_config(struct netdev_dev *, struct smap *args);
82 static void netdev_vport_poll_notify(const struct netdev *);
83 static int tnl_port_config_from_nlattr(const struct nlattr *options,
84                                        size_t options_len,
85                                        struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1]);
86
87 static bool
88 is_vport_class(const struct netdev_class *class)
89 {
90     return class->create == netdev_vport_create;
91 }
92
93 static const struct vport_class *
94 vport_class_cast(const struct netdev_class *class)
95 {
96     ovs_assert(is_vport_class(class));
97     return CONTAINER_OF(class, struct vport_class, netdev_class);
98 }
99
100 static struct netdev_dev_vport *
101 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
102 {
103     ovs_assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
104     return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
105 }
106
107 static struct netdev_dev_vport *
108 netdev_vport_get_dev(const struct netdev *netdev)
109 {
110     return netdev_dev_vport_cast(netdev_get_dev(netdev));
111 }
112
113 static const struct netdev_tunnel_config *
114 get_netdev_tunnel_config(const struct netdev_dev *netdev_dev)
115 {
116     return &netdev_dev_vport_cast(netdev_dev)->tnl_cfg;
117 }
118
119 /* If 'netdev' is a vport netdev, returns an ofpbuf that contains Netlink
120  * options to include in OVS_VPORT_ATTR_OPTIONS for configuring that vport.
121  * Otherwise returns NULL. */
122 const struct ofpbuf *
123 netdev_vport_get_options(const struct netdev *netdev)
124 {
125     const struct netdev_dev *dev = netdev_get_dev(netdev);
126
127     return (is_vport_class(netdev_dev_get_class(dev))
128             ? netdev_dev_vport_cast(dev)->options
129             : NULL);
130 }
131
132 enum ovs_vport_type
133 netdev_vport_get_vport_type(const struct netdev *netdev)
134 {
135     const struct netdev_dev *dev = netdev_get_dev(netdev);
136     const struct netdev_class *class = netdev_dev_get_class(dev);
137
138     return (is_vport_class(class) ? vport_class_cast(class)->type
139             : class == &netdev_internal_class ? OVS_VPORT_TYPE_INTERNAL
140             : (class == &netdev_linux_class ||
141                class == &netdev_tap_class) ? OVS_VPORT_TYPE_NETDEV
142             : OVS_VPORT_TYPE_UNSPEC);
143 }
144
145 bool
146 netdev_vport_is_patch(const struct netdev *netdev)
147 {
148     const struct netdev_dev *dev = netdev_get_dev(netdev);
149     const struct netdev_class *class = netdev_dev_get_class(dev);
150
151     return class->get_config == get_patch_config; 
152 }
153
154 const char *
155 netdev_vport_get_dpif_port(const struct netdev *netdev)
156 {
157     return netdev_get_name(netdev);
158 }
159
160 static uint32_t
161 get_u32_or_zero(const struct nlattr *a)
162 {
163     return a ? nl_attr_get_u32(a) : 0;
164 }
165
166 const char *
167 netdev_vport_get_netdev_type(const struct dpif_linux_vport *vport)
168 {
169     struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
170
171     switch (vport->type) {
172     case OVS_VPORT_TYPE_UNSPEC:
173         break;
174
175     case OVS_VPORT_TYPE_NETDEV:
176         return "system";
177
178     case OVS_VPORT_TYPE_INTERNAL:
179         return "internal";
180
181     case OVS_VPORT_TYPE_GRE:
182         if (tnl_port_config_from_nlattr(vport->options, vport->options_len,
183                                         a)) {
184             break;
185         }
186         return (get_u32_or_zero(a[OVS_TUNNEL_ATTR_FLAGS]) & TNL_F_IPSEC
187                 ? "ipsec_gre" : "gre");
188
189     case OVS_VPORT_TYPE_GRE64:
190         if (tnl_port_config_from_nlattr(vport->options, vport->options_len,
191                                         a)) {
192             break;
193         }
194         return (get_u32_or_zero(a[OVS_TUNNEL_ATTR_FLAGS]) & TNL_F_IPSEC
195                 ? "ipsec_gre64" : "gre64");
196
197     case OVS_VPORT_TYPE_CAPWAP:
198         return "capwap";
199
200     case OVS_VPORT_TYPE_VXLAN:
201         return "vxlan";
202
203     case __OVS_VPORT_TYPE_MAX:
204         break;
205     }
206
207     VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
208                  vport->dp_ifindex, vport->name, (unsigned int) vport->type);
209     return "unknown";
210 }
211
212 static int
213 netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
214                     struct netdev_dev **netdev_devp)
215 {
216     struct netdev_dev_vport *dev;
217
218     dev = xzalloc(sizeof *dev);
219     netdev_dev_init(&dev->netdev_dev, name, netdev_class);
220     dev->change_seq = 1;
221     eth_addr_random(dev->etheraddr);
222
223     *netdev_devp = &dev->netdev_dev;
224     route_table_register();
225
226     return 0;
227 }
228
229 static void
230 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
231 {
232     struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
233
234     ofpbuf_delete(netdev_dev->options);
235     route_table_unregister();
236     free(netdev_dev->peer);
237     free(netdev_dev);
238 }
239
240 static int
241 netdev_vport_open(struct netdev_dev *netdev_dev, struct netdev **netdevp)
242 {
243     *netdevp = xmalloc(sizeof **netdevp);
244     netdev_init(*netdevp, netdev_dev);
245     return 0;
246 }
247
248 static void
249 netdev_vport_close(struct netdev *netdev)
250 {
251     free(netdev);
252 }
253
254 static int
255 netdev_vport_set_etheraddr(struct netdev *netdev,
256                            const uint8_t mac[ETH_ADDR_LEN])
257 {
258     memcpy(netdev_vport_get_dev(netdev)->etheraddr, mac, ETH_ADDR_LEN);
259     netdev_vport_poll_notify(netdev);
260     return 0;
261 }
262
263 static int
264 netdev_vport_get_etheraddr(const struct netdev *netdev,
265                            uint8_t mac[ETH_ADDR_LEN])
266 {
267     memcpy(mac, netdev_vport_get_dev(netdev)->etheraddr, ETH_ADDR_LEN);
268     return 0;
269 }
270
271 /* Copies 'src' into 'dst', performing format conversion in the process.
272  *
273  * 'src' is allowed to be misaligned. */
274 static void
275 netdev_stats_from_ovs_vport_stats(struct netdev_stats *dst,
276                                   const struct ovs_vport_stats *src)
277 {
278     dst->rx_packets = get_unaligned_u64(&src->rx_packets);
279     dst->tx_packets = get_unaligned_u64(&src->tx_packets);
280     dst->rx_bytes = get_unaligned_u64(&src->rx_bytes);
281     dst->tx_bytes = get_unaligned_u64(&src->tx_bytes);
282     dst->rx_errors = get_unaligned_u64(&src->rx_errors);
283     dst->tx_errors = get_unaligned_u64(&src->tx_errors);
284     dst->rx_dropped = get_unaligned_u64(&src->rx_dropped);
285     dst->tx_dropped = get_unaligned_u64(&src->tx_dropped);
286     dst->multicast = 0;
287     dst->collisions = 0;
288     dst->rx_length_errors = 0;
289     dst->rx_over_errors = 0;
290     dst->rx_crc_errors = 0;
291     dst->rx_frame_errors = 0;
292     dst->rx_fifo_errors = 0;
293     dst->rx_missed_errors = 0;
294     dst->tx_aborted_errors = 0;
295     dst->tx_carrier_errors = 0;
296     dst->tx_fifo_errors = 0;
297     dst->tx_heartbeat_errors = 0;
298     dst->tx_window_errors = 0;
299 }
300
301 int
302 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
303 {
304     struct dpif_linux_vport reply;
305     struct ofpbuf *buf;
306     int error;
307
308     error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
309     if (error) {
310         return error;
311     } else if (!reply.stats) {
312         ofpbuf_delete(buf);
313         return EOPNOTSUPP;
314     }
315
316     netdev_stats_from_ovs_vport_stats(stats, reply.stats);
317
318     ofpbuf_delete(buf);
319
320     return 0;
321 }
322
323 static int
324 tunnel_get_status(const struct netdev *netdev, struct smap *smap)
325 {
326     static char iface[IFNAMSIZ];
327     ovs_be32 route;
328
329     route = netdev_vport_get_dev(netdev)->tnl_cfg.ip_dst;
330     if (route_table_get_name(route, iface)) {
331         struct netdev *egress_netdev;
332
333         smap_add(smap, "tunnel_egress_iface", iface);
334
335         if (!netdev_open(iface, "system", &egress_netdev)) {
336             smap_add(smap, "tunnel_egress_iface_carrier",
337                      netdev_get_carrier(egress_netdev) ? "up" : "down");
338             netdev_close(egress_netdev);
339         }
340     }
341
342     return 0;
343 }
344
345 static int
346 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
347                         enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
348                         enum netdev_flags *old_flagsp)
349 {
350     if (off & (NETDEV_UP | NETDEV_PROMISC)) {
351         return EOPNOTSUPP;
352     }
353
354     *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
355     return 0;
356 }
357
358 static unsigned int
359 netdev_vport_change_seq(const struct netdev *netdev)
360 {
361     return netdev_vport_get_dev(netdev)->change_seq;
362 }
363
364 static void
365 netdev_vport_run(void)
366 {
367     route_table_run();
368 }
369
370 static void
371 netdev_vport_wait(void)
372 {
373     route_table_wait();
374 }
375 \f
376 /* Helper functions. */
377
378 static void
379 netdev_vport_poll_notify(const struct netdev *netdev)
380 {
381     struct netdev_dev_vport *ndv = netdev_vport_get_dev(netdev);
382
383     ndv->change_seq++;
384     if (!ndv->change_seq) {
385         ndv->change_seq++;
386     }
387 }
388 \f
389 /* Code specific to tunnel types. */
390
391 static ovs_be64
392 parse_key(const struct smap *args, const char *name,
393           bool *present, bool *flow)
394 {
395     const char *s;
396
397     *present = false;
398     *flow = false;
399
400     s = smap_get(args, name);
401     if (!s) {
402         s = smap_get(args, "key");
403         if (!s) {
404             return 0;
405         }
406     }
407
408     *present = true;
409
410     if (!strcmp(s, "flow")) {
411         *flow = true;
412         return 0;
413     } else {
414         return htonll(strtoull(s, NULL, 0));
415     }
416 }
417
418 static int
419 set_tunnel_config(struct netdev_dev *dev_, const struct smap *args)
420 {
421     struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
422     const char *name = netdev_dev_get_name(dev_);
423     const char *type = netdev_dev_get_type(dev_);
424     bool ipsec_mech_set, needs_dst_port, has_csum;
425     struct netdev_tunnel_config tnl_cfg;
426     struct smap_node *node;
427     struct ofpbuf *options;
428     int error = EINVAL;
429     uint8_t flags;
430
431     flags = TNL_F_DF_DEFAULT;
432     has_csum = strstr(type, "gre");
433     ipsec_mech_set = false;
434     memset(&tnl_cfg, 0, sizeof tnl_cfg);
435
436     options = ofpbuf_new(64);
437
438     if (!strcmp(type, "capwap")) {
439         VLOG_WARN_ONCE("CAPWAP tunnel support is deprecated.");
440     }
441
442     needs_dst_port = !strcmp(type, "vxlan");
443     tnl_cfg.ipsec = strstr(type, "ipsec");
444     if (tnl_cfg.ipsec) {
445         flags |= TNL_F_IPSEC;
446     }
447     tnl_cfg.dont_fragment = true;
448
449     SMAP_FOR_EACH (node, args) {
450         if (!strcmp(node->key, "remote_ip")) {
451             struct in_addr in_addr;
452             if (lookup_ip(node->value, &in_addr)) {
453                 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
454             } else {
455                 tnl_cfg.ip_dst = in_addr.s_addr;
456             }
457         } else if (!strcmp(node->key, "local_ip")) {
458             struct in_addr in_addr;
459             if (lookup_ip(node->value, &in_addr)) {
460                 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
461             } else {
462                 tnl_cfg.ip_src = in_addr.s_addr;
463             }
464         } else if (!strcmp(node->key, "tos")) {
465             if (!strcmp(node->value, "inherit")) {
466                 flags |= TNL_F_TOS_INHERIT;
467                 tnl_cfg.tos_inherit = true;
468             } else {
469                 char *endptr;
470                 int tos;
471                 tos = strtol(node->value, &endptr, 0);
472                 if (*endptr == '\0' && tos == (tos & IP_DSCP_MASK)) {
473                     nl_msg_put_u8(options, OVS_TUNNEL_ATTR_TOS, tos);
474                     tnl_cfg.tos = tos;
475                 } else {
476                     VLOG_WARN("%s: invalid TOS %s", name, node->value);
477                 }
478             }
479         } else if (!strcmp(node->key, "ttl")) {
480             if (!strcmp(node->value, "inherit")) {
481                 flags |= TNL_F_TTL_INHERIT;
482                 tnl_cfg.ttl_inherit = true;
483             } else {
484                 nl_msg_put_u8(options, OVS_TUNNEL_ATTR_TTL, atoi(node->value));
485                 tnl_cfg.ttl = atoi(node->value);
486             }
487         } else if (!strcmp(node->key, "dst_port") && needs_dst_port) {
488             tnl_cfg.dst_port = htons(atoi(node->value));
489             nl_msg_put_u16(options, OVS_TUNNEL_ATTR_DST_PORT,
490                            atoi(node->value));
491         } else if (!strcmp(node->key, "csum") && has_csum) {
492             if (!strcmp(node->value, "true")) {
493                 flags |= TNL_F_CSUM;
494                 tnl_cfg.csum = true;
495             }
496         } else if (!strcmp(node->key, "df_default")) {
497             if (!strcmp(node->value, "false")) {
498                 flags &= ~TNL_F_DF_DEFAULT;
499                 tnl_cfg.dont_fragment = false;
500             }
501         } else if (!strcmp(node->key, "peer_cert") && tnl_cfg.ipsec) {
502             if (smap_get(args, "certificate")) {
503                 ipsec_mech_set = true;
504             } else {
505                 const char *use_ssl_cert;
506
507                 /* If the "use_ssl_cert" is true, then "certificate" and
508                  * "private_key" will be pulled from the SSL table.  The
509                  * use of this option is strongly discouraged, since it
510                  * will like be removed when multiple SSL configurations
511                  * are supported by OVS.
512                  */
513                 use_ssl_cert = smap_get(args, "use_ssl_cert");
514                 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
515                     VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
516                              name);
517                     goto exit;
518                 }
519                 ipsec_mech_set = true;
520             }
521         } else if (!strcmp(node->key, "psk") && tnl_cfg.ipsec) {
522             ipsec_mech_set = true;
523         } else if (tnl_cfg.ipsec
524                 && (!strcmp(node->key, "certificate")
525                     || !strcmp(node->key, "private_key")
526                     || !strcmp(node->key, "use_ssl_cert"))) {
527             /* Ignore options not used by the netdev. */
528         } else if (!strcmp(node->key, "key") ||
529                    !strcmp(node->key, "in_key") ||
530                    !strcmp(node->key, "out_key")) {
531             /* Handled separately below. */
532         } else {
533             VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->key);
534         }
535     }
536
537     /* Add a default destination port for VXLAN if none specified. */
538     if (needs_dst_port && !tnl_cfg.dst_port) {
539         nl_msg_put_u16(options, OVS_TUNNEL_ATTR_DST_PORT, VXLAN_DST_PORT);
540         tnl_cfg.dst_port = htons(VXLAN_DST_PORT);
541     }
542
543     if (tnl_cfg.ipsec) {
544         static pid_t pid = 0;
545         if (pid <= 0) {
546             char *file_name = xasprintf("%s/%s", ovs_rundir(),
547                                         "ovs-monitor-ipsec.pid");
548             pid = read_pidfile(file_name);
549             free(file_name);
550         }
551
552         if (pid < 0) {
553             VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
554                      name);
555             goto exit;
556         }
557
558         if (smap_get(args, "peer_cert") && smap_get(args, "psk")) {
559             VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
560             goto exit;
561         }
562
563         if (!ipsec_mech_set) {
564             VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
565                      name);
566             goto exit;
567         }
568     }
569
570     if (!tnl_cfg.ip_dst) {
571         VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
572                  name, type);
573         goto exit;
574     }
575     nl_msg_put_be32(options, OVS_TUNNEL_ATTR_DST_IPV4, tnl_cfg.ip_dst);
576
577     if (tnl_cfg.ip_src) {
578         if (ip_is_multicast(tnl_cfg.ip_dst)) {
579             VLOG_WARN("%s: remote_ip is multicast, ignoring local_ip", name);
580             tnl_cfg.ip_src = 0;
581         } else {
582             nl_msg_put_be32(options, OVS_TUNNEL_ATTR_SRC_IPV4, tnl_cfg.ip_src);
583         }
584     }
585
586     if (!tnl_cfg.ttl) {
587         tnl_cfg.ttl = DEFAULT_TTL;
588     }
589
590     tnl_cfg.in_key = parse_key(args, "in_key",
591                                &tnl_cfg.in_key_present,
592                                &tnl_cfg.in_key_flow);
593     if (tnl_cfg.in_key_present && !tnl_cfg.in_key_flow) {
594         nl_msg_put_be64(options, OVS_TUNNEL_ATTR_IN_KEY, tnl_cfg.in_key);
595     }
596
597     tnl_cfg.out_key = parse_key(args, "out_key",
598                                &tnl_cfg.out_key_present,
599                                &tnl_cfg.out_key_flow);
600     if (tnl_cfg.out_key_present && !tnl_cfg.out_key_flow) {
601         nl_msg_put_be64(options, OVS_TUNNEL_ATTR_OUT_KEY, tnl_cfg.out_key);
602     }
603     nl_msg_put_u32(options, OVS_TUNNEL_ATTR_FLAGS, flags);
604
605     dev->tnl_cfg = tnl_cfg;
606
607     error = 0;
608     if (!dev->options
609         || options->size != dev->options->size
610         || memcmp(options->data, dev->options->data, options->size)) {
611         struct dpif_linux_vport vport;
612
613         dpif_linux_vport_init(&vport);
614         vport.cmd = OVS_VPORT_CMD_SET;
615         vport.name = name;
616         vport.options = options->data;
617         vport.options_len = options->size;
618         error = dpif_linux_vport_transact(&vport, NULL, NULL);
619         if (!error || error == ENODEV) {
620             /* Either reconfiguration succeeded or this vport is not installed
621              * in the kernel (e.g. it hasn't been added to a dpif yet with
622              * dpif_port_add()). */
623             ofpbuf_delete(dev->options);
624             dev->options = options;
625             options = NULL;
626             error = 0;
627         }
628     }
629
630 exit:
631     ofpbuf_delete(options);
632     return error;
633 }
634
635 static int
636 tnl_port_config_from_nlattr(const struct nlattr *options, size_t options_len,
637                             struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1])
638 {
639     static const struct nl_policy ovs_tunnel_policy[] = {
640         [OVS_TUNNEL_ATTR_FLAGS] = { .type = NL_A_U32, .optional = true },
641         [OVS_TUNNEL_ATTR_DST_IPV4] = { .type = NL_A_BE32, .optional = true },
642         [OVS_TUNNEL_ATTR_SRC_IPV4] = { .type = NL_A_BE32, .optional = true },
643         [OVS_TUNNEL_ATTR_IN_KEY] = { .type = NL_A_BE64, .optional = true },
644         [OVS_TUNNEL_ATTR_OUT_KEY] = { .type = NL_A_BE64, .optional = true },
645         [OVS_TUNNEL_ATTR_TOS] = { .type = NL_A_U8, .optional = true },
646         [OVS_TUNNEL_ATTR_TTL] = { .type = NL_A_U8, .optional = true },
647         [OVS_TUNNEL_ATTR_DST_PORT] = { .type = NL_A_U16, .optional = true },
648     };
649     struct ofpbuf buf;
650
651     ofpbuf_use_const(&buf, options, options_len);
652     if (!nl_policy_parse(&buf, 0, ovs_tunnel_policy,
653                          a, ARRAY_SIZE(ovs_tunnel_policy))) {
654         return EINVAL;
655     }
656     return 0;
657 }
658
659 static uint64_t
660 get_be64_or_zero(const struct nlattr *a)
661 {
662     return a ? ntohll(nl_attr_get_be64(a)) : 0;
663 }
664
665 static int
666 get_tunnel_config(struct netdev_dev *dev_, struct smap *args)
667 {
668     struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
669     const char *name = netdev_dev_get_name(dev_);
670     struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
671     uint32_t flags;
672     int error;
673
674     if (!dev->options) {
675         struct dpif_linux_vport reply;
676         struct ofpbuf *buf;
677
678         error = dpif_linux_vport_get(name, &reply, &buf);
679         if (error) {
680             VLOG_ERR_RL(&rl, "%s: vport query failed (%s)", name,
681                         strerror(error));
682             return error;
683         }
684
685         dev->options = ofpbuf_clone_data(reply.options, reply.options_len);
686         ofpbuf_delete(buf);
687     }
688
689     error = tnl_port_config_from_nlattr(dev->options->data, dev->options->size,
690                                         a);
691     if (error) {
692         VLOG_ERR_RL(&rl, "%s: failed to parse kernel config (%s)",
693                     name, strerror(error));
694         return error;
695     }
696
697     if (a[OVS_TUNNEL_ATTR_DST_IPV4]) {
698         ovs_be32 daddr = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
699         smap_add_format(args, "remote_ip", IP_FMT, IP_ARGS(daddr));
700     }
701
702     if (a[OVS_TUNNEL_ATTR_SRC_IPV4]) {
703         ovs_be32 saddr = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_SRC_IPV4]);
704         smap_add_format(args, "local_ip", IP_FMT, IP_ARGS(saddr));
705     }
706
707     if (!a[OVS_TUNNEL_ATTR_IN_KEY] && !a[OVS_TUNNEL_ATTR_OUT_KEY]) {
708         smap_add(args, "key", "flow");
709     } else {
710         uint64_t in_key = get_be64_or_zero(a[OVS_TUNNEL_ATTR_IN_KEY]);
711         uint64_t out_key = get_be64_or_zero(a[OVS_TUNNEL_ATTR_OUT_KEY]);
712
713         if (in_key && in_key == out_key) {
714             smap_add_format(args, "key", "%"PRIu64, in_key);
715         } else {
716             if (!a[OVS_TUNNEL_ATTR_IN_KEY]) {
717                 smap_add(args, "in_key", "flow");
718             } else if (in_key) {
719                 smap_add_format(args, "in_key", "%"PRIu64, in_key);
720             }
721
722             if (!a[OVS_TUNNEL_ATTR_OUT_KEY]) {
723                 smap_add(args, "out_key", "flow");
724             } else if (out_key) {
725                 smap_add_format(args, "out_key", "%"PRIu64, out_key);
726             }
727         }
728     }
729
730     flags = get_u32_or_zero(a[OVS_TUNNEL_ATTR_FLAGS]);
731
732     if (flags & TNL_F_TTL_INHERIT) {
733         smap_add(args, "ttl", "inherit");
734     } else if (a[OVS_TUNNEL_ATTR_TTL]) {
735         int ttl = nl_attr_get_u8(a[OVS_TUNNEL_ATTR_TTL]);
736         smap_add_format(args, "ttl", "%d", ttl);
737     }
738
739     if (flags & TNL_F_TOS_INHERIT) {
740         smap_add(args, "tos", "inherit");
741     } else if (a[OVS_TUNNEL_ATTR_TOS]) {
742         int tos = nl_attr_get_u8(a[OVS_TUNNEL_ATTR_TOS]);
743         smap_add_format(args, "tos", "0x%x", tos);
744     }
745
746     if (a[OVS_TUNNEL_ATTR_DST_PORT]) {
747         uint16_t dst_port = nl_attr_get_u16(a[OVS_TUNNEL_ATTR_DST_PORT]);
748         if (dst_port != VXLAN_DST_PORT) {
749             smap_add_format(args, "dst_port", "%d", dst_port);
750         }
751     }
752
753     if (flags & TNL_F_CSUM) {
754         smap_add(args, "csum", "true");
755     }
756
757     if (!(flags & TNL_F_DF_DEFAULT)) {
758         smap_add(args, "df_default", "false");
759     }
760
761     return 0;
762 }
763 \f
764 /* Code specific to patch ports. */
765
766 const char *
767 netdev_vport_patch_peer(const struct netdev *netdev)
768 {
769     return netdev_vport_is_patch(netdev)
770         ? netdev_vport_get_dev(netdev)->peer
771         : NULL;
772 }
773
774 void
775 netdev_vport_patch_inc_rx(const struct netdev *netdev,
776                           const struct dpif_flow_stats *stats)
777 {
778     if (netdev_vport_is_patch(netdev)) {
779         struct netdev_dev_vport *dev = netdev_vport_get_dev(netdev);
780         dev->stats.rx_packets += stats->n_packets;
781         dev->stats.rx_bytes += stats->n_bytes;
782     }
783 }
784
785 void
786 netdev_vport_patch_inc_tx(const struct netdev *netdev,
787                           const struct dpif_flow_stats *stats)
788 {
789     if (netdev_vport_is_patch(netdev)) {
790         struct netdev_dev_vport *dev = netdev_vport_get_dev(netdev);
791         dev->stats.tx_packets += stats->n_packets;
792         dev->stats.tx_bytes += stats->n_bytes;
793     }
794 }
795
796 static int
797 get_patch_config(struct netdev_dev *dev_, struct smap *args)
798 {
799     struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
800
801     if (dev->peer) {
802         smap_add(args, "peer", dev->peer);
803     }
804     return 0;
805 }
806
807 static int
808 set_patch_config(struct netdev_dev *dev_, const struct smap *args)
809 {
810     struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
811     const char *name = netdev_dev_get_name(dev_);
812     const char *peer;
813
814     peer = smap_get(args, "peer");
815     if (!peer) {
816         VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
817         return EINVAL;
818     }
819
820     if (smap_count(args) > 1) {
821         VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
822         return EINVAL;
823     }
824
825     if (!strcmp(name, peer)) {
826         VLOG_ERR("%s: patch peer must not be self", name);
827         return EINVAL;
828     }
829
830     free(dev->peer);
831     dev->peer = xstrdup(peer);
832
833     return 0;
834 }
835
836 static int
837 patch_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
838 {
839     struct netdev_dev_vport *dev = netdev_vport_get_dev(netdev);
840     memcpy(stats, &dev->stats, sizeof *stats);
841     return 0;
842 }
843 \f
844 #define VPORT_FUNCTIONS(GET_CONFIG, SET_CONFIG,             \
845                         GET_TUNNEL_CONFIG, GET_STATS,       \
846                         GET_STATUS)                         \
847     NULL,                                                   \
848     netdev_vport_run,                                       \
849     netdev_vport_wait,                                      \
850                                                             \
851     netdev_vport_create,                                    \
852     netdev_vport_destroy,                                   \
853     GET_CONFIG,                                             \
854     SET_CONFIG,                                             \
855     GET_TUNNEL_CONFIG,                                      \
856                                                             \
857     netdev_vport_open,                                      \
858     netdev_vport_close,                                     \
859                                                             \
860     NULL,                       /* listen */                \
861     NULL,                       /* recv */                  \
862     NULL,                       /* recv_wait */             \
863     NULL,                       /* drain */                 \
864                                                             \
865     NULL,                       /* send */                  \
866     NULL,                       /* send_wait */             \
867                                                             \
868     netdev_vport_set_etheraddr,                             \
869     netdev_vport_get_etheraddr,                             \
870     NULL,                       /* get_mtu */               \
871     NULL,                       /* set_mtu */               \
872     NULL,                       /* get_ifindex */           \
873     NULL,                       /* get_carrier */           \
874     NULL,                       /* get_carrier_resets */    \
875     NULL,                       /* get_miimon */            \
876     GET_STATS,                                              \
877     NULL,                       /* set_stats */             \
878                                                             \
879     NULL,                       /* get_features */          \
880     NULL,                       /* set_advertisements */    \
881                                                             \
882     NULL,                       /* set_policing */          \
883     NULL,                       /* get_qos_types */         \
884     NULL,                       /* get_qos_capabilities */  \
885     NULL,                       /* get_qos */               \
886     NULL,                       /* set_qos */               \
887     NULL,                       /* get_queue */             \
888     NULL,                       /* set_queue */             \
889     NULL,                       /* delete_queue */          \
890     NULL,                       /* get_queue_stats */       \
891     NULL,                       /* dump_queues */           \
892     NULL,                       /* dump_queue_stats */      \
893                                                             \
894     NULL,                       /* get_in4 */               \
895     NULL,                       /* set_in4 */               \
896     NULL,                       /* get_in6 */               \
897     NULL,                       /* add_router */            \
898     NULL,                       /* get_next_hop */          \
899     GET_STATUS,                                             \
900     NULL,                       /* arp_lookup */            \
901                                                             \
902     netdev_vport_update_flags,                              \
903                                                             \
904     netdev_vport_change_seq
905
906 #define TUNNEL_CLASS(NAME, VPORT_TYPE)                      \
907     { VPORT_TYPE,                                           \
908         { NAME, VPORT_FUNCTIONS(get_tunnel_config,          \
909                                 set_tunnel_config,          \
910                                 get_netdev_tunnel_config,   \
911                                 netdev_vport_get_stats,     \
912                                 tunnel_get_status) }}
913
914 void
915 netdev_vport_register(void)
916 {
917     static const struct vport_class vport_classes[] = {
918         TUNNEL_CLASS("gre", OVS_VPORT_TYPE_GRE),
919         TUNNEL_CLASS("ipsec_gre", OVS_VPORT_TYPE_GRE),
920         TUNNEL_CLASS("gre64", OVS_VPORT_TYPE_GRE64),
921         TUNNEL_CLASS("ipsec_gre64", OVS_VPORT_TYPE_GRE64),
922         TUNNEL_CLASS("capwap", OVS_VPORT_TYPE_CAPWAP),
923         TUNNEL_CLASS("vxlan", OVS_VPORT_TYPE_VXLAN),
924
925         { OVS_VPORT_TYPE_UNSPEC,
926           { "patch", VPORT_FUNCTIONS(get_patch_config,
927                                      set_patch_config,
928                                      NULL,
929                                      patch_get_stats,
930                                      NULL) }},
931     };
932
933     int i;
934
935     for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
936         netdev_register_provider(&vport_classes[i].netdev_class);
937     }
938 }