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