netdev-vport: Report carrier state of tunnel egress interfaces.
[sliver-openvswitch.git] / lib / netdev-vport.c
1 /*
2  * Copyright (c) 2010, 2011 Nicira Networks.
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/rtnetlink.h>
25 #include <net/if.h>
26 #include <sys/ioctl.h>
27
28 #include "byte-order.h"
29 #include "hash.h"
30 #include "hmap.h"
31 #include "list.h"
32 #include "netdev-provider.h"
33 #include "netlink.h"
34 #include "netlink-socket.h"
35 #include "ofpbuf.h"
36 #include "openvswitch/datapath-protocol.h"
37 #include "openvswitch/tunnel.h"
38 #include "packets.h"
39 #include "rtnetlink.h"
40 #include "route-table.h"
41 #include "rtnetlink-link.h"
42 #include "shash.h"
43 #include "socket-util.h"
44 #include "vlog.h"
45
46 VLOG_DEFINE_THIS_MODULE(netdev_vport);
47
48 static struct hmap name_map;
49 static struct rtnetlink_notifier netdev_vport_link_notifier;
50
51 struct name_node {
52     struct hmap_node node; /* Node in name_map. */
53     uint32_t ifi_index;    /* Kernel interface index. */
54
55     char ifname[IFNAMSIZ]; /* Interface name. */
56 };
57
58 struct netdev_vport_notifier {
59     struct netdev_notifier notifier;
60     struct list list_node;
61     struct shash_node *shash_node;
62 };
63
64 struct netdev_dev_vport {
65     struct netdev_dev netdev_dev;
66     uint64_t config[VPORT_CONFIG_SIZE / 8];
67 };
68
69 struct netdev_vport {
70     struct netdev netdev;
71 };
72
73 struct vport_class {
74     struct netdev_class netdev_class;
75     int (*parse_config)(const struct netdev_dev *, const struct shash *args,
76                         void *config);
77 };
78
79 static struct shash netdev_vport_notifiers =
80                                     SHASH_INITIALIZER(&netdev_vport_notifiers);
81
82 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
83
84 static int netdev_vport_do_ioctl(int cmd, void *arg);
85 static int netdev_vport_create(const struct netdev_class *, const char *,
86                                const struct shash *, struct netdev_dev **);
87 static void netdev_vport_poll_notify(const struct netdev *);
88
89 static void netdev_vport_tnl_iface_init(void);
90 static void netdev_vport_link_change(const struct rtnetlink_link_change *,
91                                      void *);
92 static const char *netdev_vport_get_tnl_iface(const struct netdev *netdev);
93
94 static bool
95 is_vport_class(const struct netdev_class *class)
96 {
97     return class->create == netdev_vport_create;
98 }
99
100 static const struct vport_class *
101 vport_class_cast(const struct netdev_class *class)
102 {
103     assert(is_vport_class(class));
104     return CONTAINER_OF(class, struct vport_class, netdev_class);
105 }
106
107 static struct netdev_dev_vport *
108 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
109 {
110     assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
111     return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
112 }
113
114 static struct netdev_vport *
115 netdev_vport_cast(const struct netdev *netdev)
116 {
117     struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
118     assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
119     return CONTAINER_OF(netdev, struct netdev_vport, netdev);
120 }
121
122 /* If 'netdev' is a vport netdev, copies its kernel configuration into
123  * 'config'.  Otherwise leaves 'config' untouched. */
124 void
125 netdev_vport_get_config(const struct netdev *netdev, void *config)
126 {
127     const struct netdev_dev *dev = netdev_get_dev(netdev);
128
129     if (is_vport_class(netdev_dev_get_class(dev))) {
130         const struct netdev_dev_vport *vport = netdev_dev_vport_cast(dev);
131         memcpy(config, vport->config, VPORT_CONFIG_SIZE);
132     }
133 }
134
135 static int
136 netdev_vport_init(void)
137 {
138     netdev_vport_tnl_iface_init();
139     route_table_register();
140     return 0;
141 }
142
143 static int
144 netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
145                     const struct shash *args,
146                     struct netdev_dev **netdev_devp)
147 {
148     const struct vport_class *vport_class = vport_class_cast(netdev_class);
149     struct netdev_dev_vport *dev;
150     int error;
151
152     dev = xmalloc(sizeof *dev);
153     *netdev_devp = &dev->netdev_dev;
154     netdev_dev_init(&dev->netdev_dev, name, netdev_class);
155
156     memset(dev->config, 0, sizeof dev->config);
157     error = vport_class->parse_config(&dev->netdev_dev, args, dev->config);
158
159     if (error) {
160         netdev_dev_uninit(&dev->netdev_dev, true);
161     }
162     return error;
163 }
164
165 static void
166 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
167 {
168     struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
169
170     route_table_unregister();
171     free(netdev_dev);
172 }
173
174 static int
175 netdev_vport_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
176                 struct netdev **netdevp)
177 {
178     struct netdev_vport *netdev;
179
180     netdev = xmalloc(sizeof *netdev);
181     netdev_init(&netdev->netdev, netdev_dev_);
182
183     *netdevp = &netdev->netdev;
184     return 0;
185 }
186
187 static void
188 netdev_vport_close(struct netdev *netdev_)
189 {
190     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
191     free(netdev);
192 }
193
194 static int
195 netdev_vport_reconfigure(struct netdev_dev *dev_,
196                          const struct shash *args)
197 {
198     const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
199     const struct vport_class *vport_class = vport_class_cast(netdev_class);
200     struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
201     struct odp_port port;
202     int error;
203
204     memset(&port, 0, sizeof port);
205     strncpy(port.devname, netdev_dev_get_name(dev_), sizeof port.devname);
206     strncpy(port.type, netdev_dev_get_type(dev_), sizeof port.type);
207     error = vport_class->parse_config(dev_, args, port.config);
208     if (!error && memcmp(port.config, dev->config, sizeof dev->config)) {
209         error = netdev_vport_do_ioctl(ODP_VPORT_MOD, &port);
210         if (!error || error == ENODEV) {
211             /* Either reconfiguration succeeded or this vport is not installed
212              * in the kernel (e.g. it hasn't been added to a dpif yet with
213              * dpif_port_add()). */
214             memcpy(dev->config, port.config, sizeof dev->config);
215         }
216     }
217     return error;
218 }
219
220 static int
221 netdev_vport_set_etheraddr(struct netdev *netdev,
222                            const uint8_t mac[ETH_ADDR_LEN])
223 {
224     struct odp_vport_ether vport_ether;
225     int err;
226
227     ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
228                 sizeof vport_ether.devname);
229
230     memcpy(vport_ether.ether_addr, mac, ETH_ADDR_LEN);
231
232     err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_SET, &vport_ether);
233     if (err) {
234         return err;
235     }
236
237     netdev_vport_poll_notify(netdev);
238     return 0;
239 }
240
241 static int
242 netdev_vport_get_etheraddr(const struct netdev *netdev,
243                            uint8_t mac[ETH_ADDR_LEN])
244 {
245     struct odp_vport_ether vport_ether;
246     int err;
247
248     ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
249                 sizeof vport_ether.devname);
250
251     err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_GET, &vport_ether);
252     if (err) {
253         return err;
254     }
255
256     memcpy(mac, vport_ether.ether_addr, ETH_ADDR_LEN);
257     return 0;
258 }
259
260 static int
261 netdev_vport_get_mtu(const struct netdev *netdev, int *mtup)
262 {
263     struct odp_vport_mtu vport_mtu;
264     int err;
265
266     ovs_strlcpy(vport_mtu.devname, netdev_get_name(netdev),
267                 sizeof vport_mtu.devname);
268
269     err = netdev_vport_do_ioctl(ODP_VPORT_MTU_GET, &vport_mtu);
270     if (err) {
271         return err;
272     }
273
274     *mtup = vport_mtu.mtu;
275     return 0;
276 }
277
278 int
279 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
280 {
281     const char *name = netdev_get_name(netdev);
282     struct odp_vport_stats_req ovsr;
283     int err;
284
285     ovs_strlcpy(ovsr.devname, name, sizeof ovsr.devname);
286     err = netdev_vport_do_ioctl(ODP_VPORT_STATS_GET, &ovsr);
287     if (err) {
288         return err;
289     }
290
291     stats->rx_packets = ovsr.stats.rx_packets;
292     stats->tx_packets = ovsr.stats.tx_packets;
293     stats->rx_bytes = ovsr.stats.rx_bytes;
294     stats->tx_bytes = ovsr.stats.tx_bytes;
295     stats->rx_errors = ovsr.stats.rx_errors;
296     stats->tx_errors = ovsr.stats.tx_errors;
297     stats->rx_dropped = ovsr.stats.rx_dropped;
298     stats->tx_dropped = ovsr.stats.tx_dropped;
299     stats->multicast = ovsr.stats.multicast;
300     stats->collisions = ovsr.stats.collisions;
301     stats->rx_length_errors = ovsr.stats.rx_length_errors;
302     stats->rx_over_errors = ovsr.stats.rx_over_errors;
303     stats->rx_crc_errors = ovsr.stats.rx_crc_errors;
304     stats->rx_frame_errors = ovsr.stats.rx_frame_errors;
305     stats->rx_fifo_errors = ovsr.stats.rx_fifo_errors;
306     stats->rx_missed_errors = ovsr.stats.rx_missed_errors;
307     stats->tx_aborted_errors = ovsr.stats.tx_aborted_errors;
308     stats->tx_carrier_errors = ovsr.stats.tx_carrier_errors;
309     stats->tx_fifo_errors = ovsr.stats.tx_fifo_errors;
310     stats->tx_heartbeat_errors = ovsr.stats.tx_heartbeat_errors;
311     stats->tx_window_errors = ovsr.stats.tx_window_errors;
312
313     return 0;
314 }
315
316 int
317 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
318 {
319     struct odp_vport_stats_req ovsr;
320     int err;
321
322     ovs_strlcpy(ovsr.devname, netdev_get_name(netdev), sizeof ovsr.devname);
323
324     ovsr.stats.rx_packets = stats->rx_packets;
325     ovsr.stats.tx_packets = stats->tx_packets;
326     ovsr.stats.rx_bytes = stats->rx_bytes;
327     ovsr.stats.tx_bytes = stats->tx_bytes;
328     ovsr.stats.rx_errors = stats->rx_errors;
329     ovsr.stats.tx_errors = stats->tx_errors;
330     ovsr.stats.rx_dropped = stats->rx_dropped;
331     ovsr.stats.tx_dropped = stats->tx_dropped;
332     ovsr.stats.multicast = stats->multicast;
333     ovsr.stats.collisions = stats->collisions;
334     ovsr.stats.rx_length_errors = stats->rx_length_errors;
335     ovsr.stats.rx_over_errors = stats->rx_over_errors;
336     ovsr.stats.rx_crc_errors = stats->rx_crc_errors;
337     ovsr.stats.rx_frame_errors = stats->rx_frame_errors;
338     ovsr.stats.rx_fifo_errors = stats->rx_fifo_errors;
339     ovsr.stats.rx_missed_errors = stats->rx_missed_errors;
340     ovsr.stats.tx_aborted_errors = stats->tx_aborted_errors;
341     ovsr.stats.tx_carrier_errors = stats->tx_carrier_errors;
342     ovsr.stats.tx_fifo_errors = stats->tx_fifo_errors;
343     ovsr.stats.tx_heartbeat_errors = stats->tx_heartbeat_errors;
344     ovsr.stats.tx_window_errors = stats->tx_window_errors;
345
346     err = netdev_vport_do_ioctl(ODP_VPORT_STATS_SET, &ovsr);
347
348     /* If the vport layer doesn't know about the device, that doesn't mean it
349      * doesn't exist (after all were able to open it when netdev_open() was
350      * called), it just means that it isn't attached and we'll be getting
351      * stats a different way. */
352     if (err == ENODEV) {
353         err = EOPNOTSUPP;
354     }
355
356     return err;
357 }
358
359 static int
360 netdev_vport_get_status(const struct netdev *netdev, struct shash *sh)
361 {
362     const char *iface = netdev_vport_get_tnl_iface(netdev);
363
364     if (iface) {
365         struct netdev *egress_netdev;
366
367         shash_add(sh, "tunnel_egress_iface", xstrdup(iface));
368
369         if (!netdev_open_default(iface, &egress_netdev)) {
370             shash_add(sh, "tunnel_egress_iface_carrier",
371                       xstrdup(netdev_get_carrier(egress_netdev)
372                               ? "up" : "down"));
373             netdev_close(egress_netdev);
374         }
375     }
376
377     return 0;
378 }
379
380 static int
381 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
382                         enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
383                         enum netdev_flags *old_flagsp)
384 {
385     if (off & (NETDEV_UP | NETDEV_PROMISC)) {
386         return EOPNOTSUPP;
387     }
388
389     *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
390     return 0;
391 }
392
393 static char *
394 make_poll_name(const struct netdev *netdev)
395 {
396     return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
397 }
398
399 static int
400 netdev_vport_poll_add(struct netdev *netdev,
401                       void (*cb)(struct netdev_notifier *), void *aux,
402                       struct netdev_notifier **notifierp)
403 {
404     char *poll_name = make_poll_name(netdev);
405     struct netdev_vport_notifier *notifier;
406     struct list *list;
407     struct shash_node *shash_node;
408
409     shash_node = shash_find_data(&netdev_vport_notifiers, poll_name);
410     if (!shash_node) {
411         list = xmalloc(sizeof *list);
412         list_init(list);
413         shash_node = shash_add(&netdev_vport_notifiers, poll_name, list);
414     } else {
415         list = shash_node->data;
416     }
417
418     notifier = xmalloc(sizeof *notifier);
419     netdev_notifier_init(&notifier->notifier, netdev, cb, aux);
420     list_push_back(list, &notifier->list_node);
421     notifier->shash_node = shash_node;
422
423     *notifierp = &notifier->notifier;
424     free(poll_name);
425
426     return 0;
427 }
428
429 static void
430 netdev_vport_poll_remove(struct netdev_notifier *notifier_)
431 {
432     struct netdev_vport_notifier *notifier =
433                 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
434
435     struct list *list;
436
437     list = list_remove(&notifier->list_node);
438     if (list_is_empty(list)) {
439         shash_delete(&netdev_vport_notifiers, notifier->shash_node);
440         free(list);
441     }
442
443     free(notifier);
444 }
445
446 static void
447 netdev_vport_run(void)
448 {
449     rtnetlink_link_notifier_run();
450     route_table_run();
451 }
452
453 static void
454 netdev_vport_wait(void)
455 {
456     rtnetlink_link_notifier_wait();
457     route_table_wait();
458 }
459 \f
460 /* get_tnl_iface() implementation. */
461
462 static struct name_node *
463 name_node_lookup(int ifi_index)
464 {
465     struct name_node *nn;
466
467     HMAP_FOR_EACH_WITH_HASH(nn, node, hash_int(ifi_index, 0), &name_map) {
468         if (nn->ifi_index == ifi_index) {
469             return nn;
470         }
471     }
472
473     return NULL;
474 }
475
476 /* Queries the kernel for fresh data to populate the name map with. */
477 static int
478 netdev_vport_reset_names(void)
479 {
480     int error;
481     struct nl_dump dump;
482     struct rtgenmsg *rtmsg;
483     struct ofpbuf request, reply;
484     static struct nl_sock *rtnl_sock;
485     struct name_node *nn, *nn_next;
486
487     HMAP_FOR_EACH_SAFE(nn, nn_next, node, &name_map) {
488         hmap_remove(&name_map, &nn->node);
489         free(nn);
490     }
491
492     error = nl_sock_create(NETLINK_ROUTE, 0, 0, 0, &rtnl_sock);
493     if (error) {
494         VLOG_WARN_RL(&rl, "Failed to create NETLINK_ROUTE socket");
495         return error;
496     }
497
498     ofpbuf_init(&request, 0);
499
500     nl_msg_put_nlmsghdr(&request, sizeof *rtmsg, RTM_GETLINK, NLM_F_REQUEST);
501
502     rtmsg = ofpbuf_put_zeros(&request, sizeof *rtmsg);
503     rtmsg->rtgen_family = AF_INET;
504
505     nl_dump_start(&dump, rtnl_sock, &request);
506
507     while (nl_dump_next(&dump, &reply)) {
508         struct rtnetlink_link_change change;
509
510         if (rtnetlink_link_parse(&reply, &change)) {
511             netdev_vport_link_change(&change, NULL);
512         }
513     }
514
515     error = nl_dump_done(&dump);
516     nl_sock_destroy(rtnl_sock);
517
518     return error;
519 }
520
521 static void
522 netdev_vport_link_change(const struct rtnetlink_link_change *change,
523                          void *aux OVS_UNUSED)
524 {
525
526     if (!change) {
527         netdev_vport_reset_names();
528     } else if (change->nlmsg_type == RTM_NEWLINK) {
529         struct name_node *nn;
530
531         if (name_node_lookup(change->ifi_index)) {
532             return;
533         }
534
535         nn            = xzalloc(sizeof *nn);
536         nn->ifi_index = change->ifi_index;
537
538         strncpy(nn->ifname, change->ifname, IFNAMSIZ);
539         nn->ifname[IFNAMSIZ - 1] = '\0';
540
541         hmap_insert(&name_map, &nn->node, hash_int(nn->ifi_index, 0));
542     } else if (change->nlmsg_type == RTM_DELLINK) {
543         struct name_node *nn;
544
545         nn = name_node_lookup(change->ifi_index);
546
547         if (nn) {
548             hmap_remove(&name_map, &nn->node);
549             free(nn);
550         }
551
552     } else {
553         VLOG_WARN_RL(&rl, "Received unexpected rtnetlink message type %d",
554                      change->nlmsg_type);
555     }
556 }
557
558 static void
559 netdev_vport_tnl_iface_init(void)
560 {
561     static bool tnl_iface_is_init = false;
562
563     if (!tnl_iface_is_init) {
564         hmap_init(&name_map);
565
566         rtnetlink_link_notifier_register(&netdev_vport_link_notifier,
567                                          netdev_vport_link_change, NULL);
568
569         netdev_vport_reset_names();
570         tnl_iface_is_init = true;
571     }
572 }
573
574 static const char *
575 netdev_vport_get_tnl_iface(const struct netdev *netdev)
576 {
577     int ifindex;
578     uint32_t route;
579     struct netdev_dev_vport *ndv;
580     struct tnl_port_config *config;
581
582     ndv = netdev_dev_vport_cast(netdev_get_dev(netdev));
583     config = (struct tnl_port_config *) ndv->config;
584     route = config->daddr;
585
586     if (route_table_get_ifindex(route, &ifindex)) {
587         struct name_node *nn;
588         HMAP_FOR_EACH_WITH_HASH(nn, node, hash_int(ifindex, 0), &name_map) {
589             if (nn->ifi_index == ifindex) {
590                 return nn->ifname;
591             }
592         }
593     }
594
595     return NULL;
596 }
597 \f
598 /* Helper functions. */
599
600 static int
601 netdev_vport_do_ioctl(int cmd, void *arg)
602 {
603     static int ioctl_fd = -1;
604
605     if (ioctl_fd < 0) {
606         ioctl_fd = open("/dev/net/dp0", O_RDONLY | O_NONBLOCK);
607         if (ioctl_fd < 0) {
608             VLOG_ERR_RL(&rl, "failed to open ioctl fd: %s", strerror(errno));
609             return errno;
610         }
611     }
612
613     return ioctl(ioctl_fd, cmd, arg) ? errno : 0;
614 }
615
616 static void
617 netdev_vport_poll_notify(const struct netdev *netdev)
618 {
619     char *poll_name = make_poll_name(netdev);
620     struct list *list = shash_find_data(&netdev_vport_notifiers,
621                                         poll_name);
622
623     if (list) {
624         struct netdev_vport_notifier *notifier;
625
626         LIST_FOR_EACH (notifier, list_node, list) {
627             struct netdev_notifier *n = &notifier->notifier;
628             n->cb(n);
629         }
630     }
631
632     free(poll_name);
633 }
634 \f
635 /* Code specific to individual vport types. */
636
637 static int
638 parse_tunnel_config(const struct netdev_dev *dev, const struct shash *args,
639                     void *configp)
640 {
641     const char *name = netdev_dev_get_name(dev);
642     const char *type = netdev_dev_get_type(dev);
643     bool is_gre = false;
644     bool is_ipsec = false;
645     struct tnl_port_config config;
646     struct shash_node *node;
647     bool ipsec_mech_set = false;
648
649     memset(&config, 0, sizeof config);
650     config.flags |= TNL_F_PMTUD;
651     config.flags |= TNL_F_HDR_CACHE;
652
653     if (!strcmp(type, "gre")) {
654         is_gre = true;
655     } else if (!strcmp(type, "ipsec_gre")) {
656         is_gre = true;
657         is_ipsec = true;
658
659         config.flags |= TNL_F_IPSEC;
660
661         /* IPsec doesn't work when header caching is enabled. */
662         config.flags &= ~TNL_F_HDR_CACHE;
663     }
664
665     SHASH_FOR_EACH (node, args) {
666         if (!strcmp(node->name, "remote_ip")) {
667             struct in_addr in_addr;
668             if (lookup_ip(node->data, &in_addr)) {
669                 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
670             } else {
671                 config.daddr = in_addr.s_addr;
672             }
673         } else if (!strcmp(node->name, "local_ip")) {
674             struct in_addr in_addr;
675             if (lookup_ip(node->data, &in_addr)) {
676                 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
677             } else {
678                 config.saddr = in_addr.s_addr;
679             }
680         } else if (!strcmp(node->name, "key") && is_gre) {
681             if (!strcmp(node->data, "flow")) {
682                 config.flags |= TNL_F_IN_KEY_MATCH;
683                 config.flags |= TNL_F_OUT_KEY_ACTION;
684             } else {
685                 uint64_t key = strtoull(node->data, NULL, 0);
686                 config.out_key = config.in_key = htonll(key);
687             }
688         } else if (!strcmp(node->name, "in_key") && is_gre) {
689             if (!strcmp(node->data, "flow")) {
690                 config.flags |= TNL_F_IN_KEY_MATCH;
691             } else {
692                 config.in_key = htonll(strtoull(node->data, NULL, 0));
693             }
694         } else if (!strcmp(node->name, "out_key") && is_gre) {
695             if (!strcmp(node->data, "flow")) {
696                 config.flags |= TNL_F_OUT_KEY_ACTION;
697             } else {
698                 config.out_key = htonll(strtoull(node->data, NULL, 0));
699             }
700         } else if (!strcmp(node->name, "tos")) {
701             if (!strcmp(node->data, "inherit")) {
702                 config.flags |= TNL_F_TOS_INHERIT;
703             } else {
704                 config.tos = atoi(node->data);
705             }
706         } else if (!strcmp(node->name, "ttl")) {
707             if (!strcmp(node->data, "inherit")) {
708                 config.flags |= TNL_F_TTL_INHERIT;
709             } else {
710                 config.ttl = atoi(node->data);
711             }
712         } else if (!strcmp(node->name, "csum") && is_gre) {
713             if (!strcmp(node->data, "true")) {
714                 config.flags |= TNL_F_CSUM;
715             }
716         } else if (!strcmp(node->name, "pmtud")) {
717             if (!strcmp(node->data, "false")) {
718                 config.flags &= ~TNL_F_PMTUD;
719             }
720         } else if (!strcmp(node->name, "header_cache")) {
721             if (!strcmp(node->data, "false")) {
722                 config.flags &= ~TNL_F_HDR_CACHE;
723             }
724         } else if (!strcmp(node->name, "peer_cert") && is_ipsec) {
725             if (shash_find(args, "certificate")) {
726                 ipsec_mech_set = true;
727             } else {
728                 const char *use_ssl_cert;
729
730                 /* If the "use_ssl_cert" is true, then "certificate" and
731                  * "private_key" will be pulled from the SSL table.  The
732                  * use of this option is strongly discouraged, since it
733                  * will like be removed when multiple SSL configurations
734                  * are supported by OVS.
735                  */
736                 use_ssl_cert = shash_find_data(args, "use_ssl_cert");
737                 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
738                     VLOG_WARN("%s: 'peer_cert' requires 'certificate' argument",
739                               name);
740                     return EINVAL;
741                 }
742                 ipsec_mech_set = true;
743             }
744         } else if (!strcmp(node->name, "psk") && is_ipsec) {
745             ipsec_mech_set = true;
746         } else if (is_ipsec
747                 && (!strcmp(node->name, "certificate")
748                     || !strcmp(node->name, "private_key")
749                     || !strcmp(node->name, "use_ssl_cert"))) {
750             /* Ignore options not used by the netdev. */
751         } else {
752             VLOG_WARN("%s: unknown %s argument '%s'",
753                       name, type, node->name);
754         }
755     }
756
757     if (is_ipsec) {
758         if (shash_find(args, "peer_cert") && shash_find(args, "psk")) {
759             VLOG_WARN("%s: cannot define both 'peer_cert' and 'psk'", name);
760             return EINVAL;
761         }
762
763         if (!ipsec_mech_set) {
764             VLOG_WARN("%s: IPsec requires an 'peer_cert' or psk' argument",
765                       name);
766             return EINVAL;
767         }
768     }
769
770     if (!config.daddr) {
771         VLOG_WARN("%s: %s type requires valid 'remote_ip' argument",
772                   name, type);
773         return EINVAL;
774     }
775
776     BUILD_ASSERT(sizeof config <= VPORT_CONFIG_SIZE);
777     memcpy(configp, &config, sizeof config);
778     return 0;
779 }
780
781 static int
782 parse_patch_config(const struct netdev_dev *dev, const struct shash *args,
783                    void *configp)
784 {
785     const char *name = netdev_dev_get_name(dev);
786     const char *peer;
787
788     peer = shash_find_data(args, "peer");
789     if (!peer) {
790         VLOG_WARN("%s: patch type requires valid 'peer' argument", name);
791         return EINVAL;
792     }
793
794     if (shash_count(args) > 1) {
795         VLOG_WARN("%s: patch type takes only a 'peer' argument", name);
796         return EINVAL;
797     }
798
799     if (strlen(peer) >= MIN(IFNAMSIZ, VPORT_CONFIG_SIZE)) {
800         VLOG_WARN("%s: patch 'peer' arg too long", name);
801         return EINVAL;
802     }
803
804     if (!strcmp(name, peer)) {
805         VLOG_WARN("%s: patch peer must not be self", name);
806         return EINVAL;
807     }
808
809     strncpy(configp, peer, VPORT_CONFIG_SIZE);
810
811     return 0;
812 }
813 \f
814 #define VPORT_FUNCTIONS(GET_STATUS)                         \
815     netdev_vport_init,                                      \
816     netdev_vport_run,                                       \
817     netdev_vport_wait,                                      \
818                                                             \
819     netdev_vport_create,                                    \
820     netdev_vport_destroy,                                   \
821     netdev_vport_reconfigure,                               \
822                                                             \
823     netdev_vport_open,                                      \
824     netdev_vport_close,                                     \
825                                                             \
826     NULL,                       /* enumerate */             \
827                                                             \
828     NULL,                       /* recv */                  \
829     NULL,                       /* recv_wait */             \
830     NULL,                       /* drain */                 \
831                                                             \
832     NULL,                       /* send */                  \
833     NULL,                       /* send_wait */             \
834                                                             \
835     netdev_vport_set_etheraddr,                             \
836     netdev_vport_get_etheraddr,                             \
837     netdev_vport_get_mtu,                                   \
838     NULL,                       /* get_ifindex */           \
839     NULL,                       /* get_carrier */           \
840     NULL,                       /* get_miimon */            \
841     netdev_vport_get_stats,                                 \
842     netdev_vport_set_stats,                                 \
843                                                             \
844     NULL,                       /* get_features */          \
845     NULL,                       /* set_advertisements */    \
846     NULL,                       /* get_vlan_vid */          \
847                                                             \
848     NULL,                       /* set_policing */          \
849     NULL,                       /* get_qos_types */         \
850     NULL,                       /* get_qos_capabilities */  \
851     NULL,                       /* get_qos */               \
852     NULL,                       /* set_qos */               \
853     NULL,                       /* get_queue */             \
854     NULL,                       /* set_queue */             \
855     NULL,                       /* delete_queue */          \
856     NULL,                       /* get_queue_stats */       \
857     NULL,                       /* dump_queues */           \
858     NULL,                       /* dump_queue_stats */      \
859                                                             \
860     NULL,                       /* get_in4 */               \
861     NULL,                       /* set_in4 */               \
862     NULL,                       /* get_in6 */               \
863     NULL,                       /* add_router */            \
864     NULL,                       /* get_next_hop */          \
865     GET_STATUS,                                             \
866     NULL,                       /* arp_lookup */            \
867                                                             \
868     netdev_vport_update_flags,                              \
869                                                             \
870     netdev_vport_poll_add,                                  \
871     netdev_vport_poll_remove,
872
873 void
874 netdev_vport_register(void)
875 {
876     static const struct vport_class vport_classes[] = {
877         { { "gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
878             parse_tunnel_config },
879         { { "ipsec_gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
880             parse_tunnel_config },
881         { { "capwap", VPORT_FUNCTIONS(netdev_vport_get_status) },
882             parse_tunnel_config },
883         { { "patch", VPORT_FUNCTIONS(NULL) }, parse_patch_config }
884     };
885
886     int i;
887
888     for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
889         netdev_register_provider(&vport_classes[i].netdev_class);
890     }
891 }