netdev-vport: Fix theoretical null pointer dereference.
[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 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 (!ndv->options) {
469         /* Race condition when 'ndv' was created, but did not have its
470          * configuration set yet. */
471         return NULL;
472     }
473
474     if (tnl_port_config_from_nlattr(ndv->options->data, ndv->options->size,
475                                     a)) {
476         return NULL;
477     }
478     if (a[OVS_TUNNEL_ATTR_DST_IPV4]) {
479         ovs_be32 route = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
480
481         if (route_table_get_name(route, name)) {
482             return name;
483         }
484     }
485     return NULL;
486 }
487 \f
488 /* Helper functions. */
489
490 static void
491 netdev_vport_poll_notify(const struct netdev *netdev)
492 {
493     struct netdev_dev_vport *ndv;
494
495     ndv = netdev_dev_vport_cast(netdev_get_dev(netdev));
496
497     ndv->change_seq++;
498     if (!ndv->change_seq) {
499         ndv->change_seq++;
500     }
501 }
502 \f
503 /* Code specific to individual vport types. */
504
505 static void
506 set_key(const struct smap *args, const char *name, uint16_t type,
507         struct ofpbuf *options)
508 {
509     const char *s;
510
511     s = smap_get(args, name);
512     if (!s) {
513         s = smap_get(args, "key");
514         if (!s) {
515             s = "0";
516         }
517     }
518
519     if (!strcmp(s, "flow")) {
520         /* This is the default if no attribute is present. */
521     } else {
522         nl_msg_put_be64(options, type, htonll(strtoull(s, NULL, 0)));
523     }
524 }
525
526 static int
527 parse_tunnel_config(const char *name, const char *type,
528                     const struct smap *args, struct ofpbuf *options)
529 {
530     bool is_gre = false;
531     bool is_ipsec = false;
532     bool needs_dst_port = false;
533     bool found_dst_port = false;
534     struct smap_node *node;
535     bool ipsec_mech_set = false;
536     ovs_be32 daddr = htonl(0);
537     ovs_be32 saddr = htonl(0);
538     uint32_t flags;
539
540     if (!strcmp(type, "capwap")) {
541         VLOG_WARN_ONCE("CAPWAP tunnel support is deprecated.");
542     }
543
544     flags = TNL_F_DF_DEFAULT;
545     if (!strcmp(type, "gre") || !strcmp(type, "gre64")) {
546         is_gre = true;
547     } else if (!strcmp(type, "ipsec_gre") || !strcmp(type, "ipsec_gre64")) {
548         is_gre = true;
549         is_ipsec = true;
550         flags |= TNL_F_IPSEC;
551     } else if (!strcmp(type, "vxlan")) {
552         needs_dst_port = true;
553     }
554
555     SMAP_FOR_EACH (node, args) {
556         if (!strcmp(node->key, "remote_ip")) {
557             struct in_addr in_addr;
558             if (lookup_ip(node->value, &in_addr)) {
559                 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
560             } else {
561                 daddr = in_addr.s_addr;
562             }
563         } else if (!strcmp(node->key, "local_ip")) {
564             struct in_addr in_addr;
565             if (lookup_ip(node->value, &in_addr)) {
566                 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
567             } else {
568                 saddr = in_addr.s_addr;
569             }
570         } else if (!strcmp(node->key, "tos")) {
571             if (!strcmp(node->value, "inherit")) {
572                 flags |= TNL_F_TOS_INHERIT;
573             } else {
574                 char *endptr;
575                 int tos;
576                 tos = strtol(node->value, &endptr, 0);
577                 if (*endptr == '\0' && tos == (tos & IP_DSCP_MASK)) {
578                     nl_msg_put_u8(options, OVS_TUNNEL_ATTR_TOS, tos);
579                 } else {
580                     VLOG_WARN("%s: invalid TOS %s", name, node->value);
581                 }
582             }
583         } else if (!strcmp(node->key, "ttl")) {
584             if (!strcmp(node->value, "inherit")) {
585                 flags |= TNL_F_TTL_INHERIT;
586             } else {
587                 nl_msg_put_u8(options, OVS_TUNNEL_ATTR_TTL, atoi(node->value));
588             }
589         } else if (!strcmp(node->key, "dst_port") && needs_dst_port) {
590             nl_msg_put_u16(options, OVS_TUNNEL_ATTR_DST_PORT,
591                            atoi(node->value));
592             found_dst_port = true;
593         } else if (!strcmp(node->key, "csum") && is_gre) {
594             if (!strcmp(node->value, "true")) {
595                 flags |= TNL_F_CSUM;
596             }
597         } else if (!strcmp(node->key, "df_inherit")) {
598             if (!strcmp(node->value, "true")) {
599                 flags |= TNL_F_DF_INHERIT;
600             }
601         } else if (!strcmp(node->key, "df_default")) {
602             if (!strcmp(node->value, "false")) {
603                 flags &= ~TNL_F_DF_DEFAULT;
604             }
605         } else if (!strcmp(node->key, "pmtud")) {
606             if (!strcmp(node->value, "true")) {
607                 VLOG_WARN_ONCE("%s: The tunnel Path MTU discovery is "
608                                "deprecated and may be removed in February "
609                                "2013. Please email dev@openvswitch.org with "
610                                "concerns.", name);
611                 flags |= TNL_F_PMTUD;
612             }
613         } else if (!strcmp(node->key, "peer_cert") && is_ipsec) {
614             if (smap_get(args, "certificate")) {
615                 ipsec_mech_set = true;
616             } else {
617                 const char *use_ssl_cert;
618
619                 /* If the "use_ssl_cert" is true, then "certificate" and
620                  * "private_key" will be pulled from the SSL table.  The
621                  * use of this option is strongly discouraged, since it
622                  * will like be removed when multiple SSL configurations
623                  * are supported by OVS.
624                  */
625                 use_ssl_cert = smap_get(args, "use_ssl_cert");
626                 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
627                     VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
628                              name);
629                     return EINVAL;
630                 }
631                 ipsec_mech_set = true;
632             }
633         } else if (!strcmp(node->key, "psk") && is_ipsec) {
634             ipsec_mech_set = true;
635         } else if (is_ipsec
636                 && (!strcmp(node->key, "certificate")
637                     || !strcmp(node->key, "private_key")
638                     || !strcmp(node->key, "use_ssl_cert"))) {
639             /* Ignore options not used by the netdev. */
640         } else if (!strcmp(node->key, "key") ||
641                    !strcmp(node->key, "in_key") ||
642                    !strcmp(node->key, "out_key")) {
643             /* Handled separately below. */
644         } else {
645             VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->key);
646         }
647     }
648
649     /* Add a default destination port for VXLAN if none specified. */
650     if (needs_dst_port && !found_dst_port) {
651         nl_msg_put_u16(options, OVS_TUNNEL_ATTR_DST_PORT, VXLAN_DST_PORT);
652     }
653
654     if (is_ipsec) {
655         static pid_t pid = 0;
656         if (pid <= 0) {
657             char *file_name = xasprintf("%s/%s", ovs_rundir(),
658                                         "ovs-monitor-ipsec.pid");
659             pid = read_pidfile(file_name);
660             free(file_name);
661         }
662
663         if (pid < 0) {
664             VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
665                      name);
666             return EINVAL;
667         }
668
669         if (smap_get(args, "peer_cert") && smap_get(args, "psk")) {
670             VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
671             return EINVAL;
672         }
673
674         if (!ipsec_mech_set) {
675             VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
676                      name);
677             return EINVAL;
678         }
679     }
680
681     set_key(args, "in_key", OVS_TUNNEL_ATTR_IN_KEY, options);
682     set_key(args, "out_key", OVS_TUNNEL_ATTR_OUT_KEY, options);
683
684     if (!daddr) {
685         VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
686                  name, type);
687         return EINVAL;
688     }
689     nl_msg_put_be32(options, OVS_TUNNEL_ATTR_DST_IPV4, daddr);
690
691     if (saddr) {
692         if (ip_is_multicast(daddr)) {
693             VLOG_WARN("%s: remote_ip is multicast, ignoring local_ip", name);
694         } else {
695             nl_msg_put_be32(options, OVS_TUNNEL_ATTR_SRC_IPV4, saddr);
696         }
697     }
698
699     nl_msg_put_u32(options, OVS_TUNNEL_ATTR_FLAGS, flags);
700
701     return 0;
702 }
703
704 static int
705 tnl_port_config_from_nlattr(const struct nlattr *options, size_t options_len,
706                             struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1])
707 {
708     static const struct nl_policy ovs_tunnel_policy[] = {
709         [OVS_TUNNEL_ATTR_FLAGS] = { .type = NL_A_U32, .optional = true },
710         [OVS_TUNNEL_ATTR_DST_IPV4] = { .type = NL_A_BE32, .optional = true },
711         [OVS_TUNNEL_ATTR_SRC_IPV4] = { .type = NL_A_BE32, .optional = true },
712         [OVS_TUNNEL_ATTR_IN_KEY] = { .type = NL_A_BE64, .optional = true },
713         [OVS_TUNNEL_ATTR_OUT_KEY] = { .type = NL_A_BE64, .optional = true },
714         [OVS_TUNNEL_ATTR_TOS] = { .type = NL_A_U8, .optional = true },
715         [OVS_TUNNEL_ATTR_TTL] = { .type = NL_A_U8, .optional = true },
716         [OVS_TUNNEL_ATTR_DST_PORT] = { .type = NL_A_U16, .optional = true },
717     };
718     struct ofpbuf buf;
719
720     ofpbuf_use_const(&buf, options, options_len);
721     if (!nl_policy_parse(&buf, 0, ovs_tunnel_policy,
722                          a, ARRAY_SIZE(ovs_tunnel_policy))) {
723         return EINVAL;
724     }
725     return 0;
726 }
727
728 static uint64_t
729 get_be64_or_zero(const struct nlattr *a)
730 {
731     return a ? ntohll(nl_attr_get_be64(a)) : 0;
732 }
733
734 static int
735 unparse_tunnel_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
736                       const struct nlattr *options, size_t options_len,
737                       struct smap *args)
738 {
739     struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
740     uint32_t flags;
741     int error;
742
743     error = tnl_port_config_from_nlattr(options, options_len, a);
744     if (error) {
745         return error;
746     }
747
748     if (a[OVS_TUNNEL_ATTR_DST_IPV4]) {
749         ovs_be32 daddr = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
750         smap_add_format(args, "remote_ip", IP_FMT, IP_ARGS(daddr));
751     }
752
753     if (a[OVS_TUNNEL_ATTR_SRC_IPV4]) {
754         ovs_be32 saddr = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_SRC_IPV4]);
755         smap_add_format(args, "local_ip", IP_FMT, IP_ARGS(saddr));
756     }
757
758     if (!a[OVS_TUNNEL_ATTR_IN_KEY] && !a[OVS_TUNNEL_ATTR_OUT_KEY]) {
759         smap_add(args, "key", "flow");
760     } else {
761         uint64_t in_key = get_be64_or_zero(a[OVS_TUNNEL_ATTR_IN_KEY]);
762         uint64_t out_key = get_be64_or_zero(a[OVS_TUNNEL_ATTR_OUT_KEY]);
763
764         if (in_key && in_key == out_key) {
765             smap_add_format(args, "key", "%"PRIu64, in_key);
766         } else {
767             if (!a[OVS_TUNNEL_ATTR_IN_KEY]) {
768                 smap_add(args, "in_key", "flow");
769             } else if (in_key) {
770                 smap_add_format(args, "in_key", "%"PRIu64, in_key);
771             }
772
773             if (!a[OVS_TUNNEL_ATTR_OUT_KEY]) {
774                 smap_add(args, "out_key", "flow");
775             } else if (out_key) {
776                 smap_add_format(args, "out_key", "%"PRIu64, out_key);
777             }
778         }
779     }
780
781     flags = get_u32_or_zero(a[OVS_TUNNEL_ATTR_FLAGS]);
782
783     if (flags & TNL_F_TTL_INHERIT) {
784         smap_add(args, "ttl", "inherit");
785     } else if (a[OVS_TUNNEL_ATTR_TTL]) {
786         int ttl = nl_attr_get_u8(a[OVS_TUNNEL_ATTR_TTL]);
787         smap_add_format(args, "ttl", "%d", ttl);
788     }
789
790     if (flags & TNL_F_TOS_INHERIT) {
791         smap_add(args, "tos", "inherit");
792     } else if (a[OVS_TUNNEL_ATTR_TOS]) {
793         int tos = nl_attr_get_u8(a[OVS_TUNNEL_ATTR_TOS]);
794         smap_add_format(args, "tos", "0x%x", tos);
795     }
796
797     if (a[OVS_TUNNEL_ATTR_DST_PORT]) {
798         uint16_t dst_port = nl_attr_get_u16(a[OVS_TUNNEL_ATTR_DST_PORT]);
799         if (dst_port != VXLAN_DST_PORT) {
800             smap_add_format(args, "dst_port", "%d", dst_port);
801         }
802     }
803
804     if (flags & TNL_F_CSUM) {
805         smap_add(args, "csum", "true");
806     }
807     if (flags & TNL_F_DF_INHERIT) {
808         smap_add(args, "df_inherit", "true");
809     }
810     if (!(flags & TNL_F_DF_DEFAULT)) {
811         smap_add(args, "df_default", "false");
812     }
813     if (flags & TNL_F_PMTUD) {
814         smap_add(args, "pmtud", "true");
815     }
816
817     return 0;
818 }
819
820 static int
821 parse_patch_config(const char *name, const char *type OVS_UNUSED,
822                    const struct smap *args, struct ofpbuf *options)
823 {
824     const char *peer;
825
826     peer = smap_get(args, "peer");
827     if (!peer) {
828         VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
829         return EINVAL;
830     }
831
832     if (smap_count(args) > 1) {
833         VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
834         return EINVAL;
835     }
836
837     if (strlen(peer) >= IFNAMSIZ) {
838         VLOG_ERR("%s: patch 'peer' arg too long", name);
839         return EINVAL;
840     }
841
842     if (!strcmp(name, peer)) {
843         VLOG_ERR("%s: patch peer must not be self", name);
844         return EINVAL;
845     }
846
847     nl_msg_put_string(options, OVS_PATCH_ATTR_PEER, peer);
848
849     return 0;
850 }
851
852 static int
853 unparse_patch_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
854                      const struct nlattr *options, size_t options_len,
855                      struct smap *args)
856 {
857     static const struct nl_policy ovs_patch_policy[] = {
858         [OVS_PATCH_ATTR_PEER] = { .type = NL_A_STRING,
859                                .max_len = IFNAMSIZ,
860                                .optional = false }
861     };
862
863     struct nlattr *a[ARRAY_SIZE(ovs_patch_policy)];
864     struct ofpbuf buf;
865
866     ofpbuf_use_const(&buf, options, options_len);
867     if (!nl_policy_parse(&buf, 0, ovs_patch_policy,
868                          a, ARRAY_SIZE(ovs_patch_policy))) {
869         return EINVAL;
870     }
871
872     smap_add(args, "peer", nl_attr_get_string(a[OVS_PATCH_ATTR_PEER]));
873     return 0;
874 }
875 \f
876 #define VPORT_FUNCTIONS(GET_STATUS)                         \
877     NULL,                                                   \
878     netdev_vport_run,                                       \
879     netdev_vport_wait,                                      \
880                                                             \
881     netdev_vport_create,                                    \
882     netdev_vport_destroy,                                   \
883     netdev_vport_get_config,                                \
884     netdev_vport_set_config,                                \
885                                                             \
886     netdev_vport_open,                                      \
887     netdev_vport_close,                                     \
888                                                             \
889     NULL,                       /* listen */                \
890     NULL,                       /* recv */                  \
891     NULL,                       /* recv_wait */             \
892     NULL,                       /* drain */                 \
893                                                             \
894     NULL,                       /* send */                  \
895     NULL,                       /* send_wait */             \
896                                                             \
897     netdev_vport_set_etheraddr,                             \
898     netdev_vport_get_etheraddr,                             \
899     NULL,                       /* get_mtu */               \
900     NULL,                       /* set_mtu */               \
901     NULL,                       /* get_ifindex */           \
902     NULL,                       /* get_carrier */           \
903     NULL,                       /* get_carrier_resets */    \
904     NULL,                       /* get_miimon */            \
905     netdev_vport_get_stats,                                 \
906     NULL,                       /* set_stats */             \
907                                                             \
908     NULL,                       /* get_features */          \
909     NULL,                       /* set_advertisements */    \
910                                                             \
911     NULL,                       /* set_policing */          \
912     NULL,                       /* get_qos_types */         \
913     NULL,                       /* get_qos_capabilities */  \
914     NULL,                       /* get_qos */               \
915     NULL,                       /* set_qos */               \
916     NULL,                       /* get_queue */             \
917     NULL,                       /* set_queue */             \
918     NULL,                       /* delete_queue */          \
919     NULL,                       /* get_queue_stats */       \
920     NULL,                       /* dump_queues */           \
921     NULL,                       /* dump_queue_stats */      \
922                                                             \
923     NULL,                       /* get_in4 */               \
924     NULL,                       /* set_in4 */               \
925     NULL,                       /* get_in6 */               \
926     NULL,                       /* add_router */            \
927     NULL,                       /* get_next_hop */          \
928     GET_STATUS,                                             \
929     NULL,                       /* arp_lookup */            \
930                                                             \
931     netdev_vport_update_flags,                              \
932                                                             \
933     netdev_vport_change_seq
934
935 void
936 netdev_vport_register(void)
937 {
938     static const struct vport_class vport_classes[] = {
939         { OVS_VPORT_TYPE_GRE,
940           { "gre", VPORT_FUNCTIONS(netdev_vport_get_drv_info) },
941           parse_tunnel_config, unparse_tunnel_config },
942
943         { OVS_VPORT_TYPE_GRE,
944           { "ipsec_gre", VPORT_FUNCTIONS(netdev_vport_get_drv_info) },
945           parse_tunnel_config, unparse_tunnel_config },
946
947         { OVS_VPORT_TYPE_GRE64,
948           { "gre64", VPORT_FUNCTIONS(netdev_vport_get_drv_info) },
949           parse_tunnel_config, unparse_tunnel_config },
950
951         { OVS_VPORT_TYPE_GRE64,
952           { "ipsec_gre64", VPORT_FUNCTIONS(netdev_vport_get_drv_info) },
953           parse_tunnel_config, unparse_tunnel_config },
954
955         { OVS_VPORT_TYPE_CAPWAP,
956           { "capwap", VPORT_FUNCTIONS(netdev_vport_get_drv_info) },
957           parse_tunnel_config, unparse_tunnel_config },
958
959         { OVS_VPORT_TYPE_VXLAN,
960           { "vxlan", VPORT_FUNCTIONS(netdev_vport_get_drv_info) },
961           parse_tunnel_config, unparse_tunnel_config },
962
963         { OVS_VPORT_TYPE_PATCH,
964           { "patch", VPORT_FUNCTIONS(NULL) },
965           parse_patch_config, unparse_patch_config }
966     };
967
968     int i;
969
970     for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
971         netdev_register_provider(&vport_classes[i].netdev_class);
972     }
973 }