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