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