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