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