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