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