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