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