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