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