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