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