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