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