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