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