2 * Copyright (c) 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "netdev-vport.h"
23 #include <sys/socket.h>
24 #include <linux/rtnetlink.h>
26 #include <sys/ioctl.h>
28 #include "byte-order.h"
31 #include "dpif-linux.h"
35 #include "netdev-provider.h"
37 #include "netlink-socket.h"
39 #include "openvswitch/datapath-protocol.h"
40 #include "openvswitch/tunnel.h"
42 #include "route-table.h"
43 #include "rtnetlink.h"
45 #include "socket-util.h"
48 VLOG_DEFINE_THIS_MODULE(netdev_vport);
50 struct netdev_vport_notifier {
51 struct netdev_notifier notifier;
52 struct list list_node;
53 struct shash_node *shash_node;
56 struct netdev_dev_vport {
57 struct netdev_dev netdev_dev;
58 struct ofpbuf *options;
59 int dp_ifindex; /* -1 if unknown. */
60 uint32_t port_no; /* UINT32_MAX if unknown. */
68 enum odp_vport_type type;
69 struct netdev_class netdev_class;
70 int (*parse_config)(const char *name, const char *type,
71 const struct shash *args, struct ofpbuf *options);
72 int (*unparse_config)(const char *name, const char *type,
73 const struct nlattr *options, size_t options_len,
77 static struct shash netdev_vport_notifiers =
78 SHASH_INITIALIZER(&netdev_vport_notifiers);
80 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
82 static int netdev_vport_create(const struct netdev_class *, const char *,
83 const struct shash *, struct netdev_dev **);
84 static void netdev_vport_poll_notify(const struct netdev *);
85 static int tnl_port_config_from_nlattr(const struct nlattr *options,
87 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1]);
89 static const char *netdev_vport_get_tnl_iface(const struct netdev *netdev);
92 is_vport_class(const struct netdev_class *class)
94 return class->create == netdev_vport_create;
97 static const struct vport_class *
98 vport_class_cast(const struct netdev_class *class)
100 assert(is_vport_class(class));
101 return CONTAINER_OF(class, struct vport_class, netdev_class);
104 static struct netdev_dev_vport *
105 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
107 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
108 return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
111 static struct netdev_vport *
112 netdev_vport_cast(const struct netdev *netdev)
114 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
115 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
116 return CONTAINER_OF(netdev, struct netdev_vport, netdev);
119 /* If 'netdev' is a vport netdev, returns an ofpbuf that contains Netlink
120 * options to include in ODP_VPORT_ATTR_OPTIONS for configuring that vport.
121 * Otherwise returns NULL. */
122 const struct ofpbuf *
123 netdev_vport_get_options(const struct netdev *netdev)
125 const struct netdev_dev *dev = netdev_get_dev(netdev);
127 return (is_vport_class(netdev_dev_get_class(dev))
128 ? netdev_dev_vport_cast(dev)->options
133 netdev_vport_get_vport_type(const struct netdev *netdev)
135 const struct netdev_dev *dev = netdev_get_dev(netdev);
136 const struct netdev_class *class = netdev_dev_get_class(dev);
138 return (is_vport_class(class) ? vport_class_cast(class)->type
139 : class == &netdev_internal_class ? ODP_VPORT_TYPE_INTERNAL
140 : class == &netdev_linux_class ? ODP_VPORT_TYPE_NETDEV
141 : ODP_VPORT_TYPE_UNSPEC);
145 netdev_vport_get_netdev_type(const struct dpif_linux_vport *vport)
147 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1];
149 switch (vport->type) {
150 case ODP_VPORT_TYPE_UNSPEC:
153 case ODP_VPORT_TYPE_NETDEV:
156 case ODP_VPORT_TYPE_INTERNAL:
159 case ODP_VPORT_TYPE_PATCH:
162 case ODP_VPORT_TYPE_GRE:
163 if (tnl_port_config_from_nlattr(vport->options, vport->options_len,
167 return (nl_attr_get_u32(a[ODP_TUNNEL_ATTR_FLAGS]) & TNL_F_IPSEC
168 ? "ipsec_gre" : "gre");
170 case ODP_VPORT_TYPE_CAPWAP:
173 case __ODP_VPORT_TYPE_MAX:
177 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
178 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
183 netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
184 const struct shash *args,
185 struct netdev_dev **netdev_devp)
187 const struct vport_class *vport_class = vport_class_cast(netdev_class);
188 struct ofpbuf *options = NULL;
189 struct shash fetched_args;
194 shash_init(&fetched_args);
197 port_no = UINT32_MAX;
198 if (!shash_is_empty(args)) {
199 /* Parse the provided configuration. */
200 options = ofpbuf_new(64);
201 error = vport_class->parse_config(name, netdev_class->type,
204 /* Fetch an existing configuration from the kernel.
206 * This case could be ambiguous with initializing a new vport with an
207 * empty configuration, but none of the existing vport classes accept
208 * an empty configuration. */
209 struct dpif_linux_vport reply;
212 error = dpif_linux_vport_get(name, &reply, &buf);
214 /* XXX verify correct type */
215 error = vport_class->unparse_config(name, netdev_class->type,
220 VLOG_ERR_RL(&rl, "%s: failed to parse kernel config (%s)",
221 name, strerror(error));
223 options = ofpbuf_clone_data(reply.options, reply.options_len);
224 dp_ifindex = reply.dp_ifindex;
225 port_no = reply.port_no;
229 VLOG_ERR_RL(&rl, "%s: vport query failed (%s)",
230 name, strerror(error));
235 struct netdev_dev_vport *dev;
237 dev = xmalloc(sizeof *dev);
238 netdev_dev_init(&dev->netdev_dev, name,
239 shash_is_empty(&fetched_args) ? args : &fetched_args,
241 dev->options = options;
242 dev->dp_ifindex = dp_ifindex;
243 dev->port_no = port_no;
245 *netdev_devp = &dev->netdev_dev;
246 route_table_register();
248 ofpbuf_delete(options);
251 shash_destroy(&fetched_args);
257 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
259 struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
261 route_table_unregister();
266 netdev_vport_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
267 struct netdev **netdevp)
269 struct netdev_vport *netdev;
271 netdev = xmalloc(sizeof *netdev);
272 netdev_init(&netdev->netdev, netdev_dev_);
274 *netdevp = &netdev->netdev;
279 netdev_vport_close(struct netdev *netdev_)
281 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
286 netdev_vport_set_config(struct netdev_dev *dev_, const struct shash *args)
288 const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
289 const struct vport_class *vport_class = vport_class_cast(netdev_class);
290 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
291 const char *name = netdev_dev_get_name(dev_);
292 struct ofpbuf *options;
295 options = ofpbuf_new(64);
296 error = vport_class->parse_config(name, netdev_dev_get_type(dev_),
299 && (options->size != dev->options->size
300 || memcmp(options->data, dev->options->data, options->size))) {
301 struct dpif_linux_vport vport;
303 dpif_linux_vport_init(&vport);
304 vport.cmd = ODP_VPORT_CMD_SET;
306 vport.options = options->data;
307 vport.options_len = options->size;
308 error = dpif_linux_vport_transact(&vport, NULL, NULL);
309 if (!error || error == ENODEV) {
310 /* Either reconfiguration succeeded or this vport is not installed
311 * in the kernel (e.g. it hasn't been added to a dpif yet with
312 * dpif_port_add()). */
313 ofpbuf_delete(dev->options);
314 dev->options = options;
319 ofpbuf_delete(options);
325 netdev_vport_send(struct netdev *netdev, const void *data, size_t size)
327 struct netdev_dev *dev_ = netdev_get_dev(netdev);
328 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
330 if (dev->dp_ifindex == -1) {
331 const char *name = netdev_get_name(netdev);
332 struct dpif_linux_vport reply;
336 error = dpif_linux_vport_get(name, &reply, &buf);
338 VLOG_ERR_RL(&rl, "%s: failed to query vport for send (%s)",
339 name, strerror(error));
342 dev->dp_ifindex = reply.dp_ifindex;
343 dev->port_no = reply.port_no;
347 return dpif_linux_vport_send(dev->dp_ifindex, dev->port_no, data, size);
351 netdev_vport_set_etheraddr(struct netdev *netdev,
352 const uint8_t mac[ETH_ADDR_LEN])
354 struct dpif_linux_vport vport;
357 dpif_linux_vport_init(&vport);
358 vport.cmd = ODP_VPORT_CMD_SET;
359 vport.name = netdev_get_name(netdev);
362 error = dpif_linux_vport_transact(&vport, NULL, NULL);
364 netdev_vport_poll_notify(netdev);
370 netdev_vport_get_etheraddr(const struct netdev *netdev,
371 uint8_t mac[ETH_ADDR_LEN])
373 struct dpif_linux_vport reply;
377 error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
380 memcpy(mac, reply.address, ETH_ADDR_LEN);
390 netdev_vport_get_mtu(const struct netdev *netdev, int *mtup)
392 struct dpif_linux_vport reply;
396 error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
405 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
407 struct dpif_linux_vport reply;
411 error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
414 } else if (!reply.stats) {
419 stats->rx_packets = reply.stats->rx_packets;
420 stats->tx_packets = reply.stats->tx_packets;
421 stats->rx_bytes = reply.stats->rx_bytes;
422 stats->tx_bytes = reply.stats->tx_bytes;
423 stats->rx_errors = reply.stats->rx_errors;
424 stats->tx_errors = reply.stats->tx_errors;
425 stats->rx_dropped = reply.stats->rx_dropped;
426 stats->tx_dropped = reply.stats->tx_dropped;
427 stats->multicast = reply.stats->multicast;
428 stats->collisions = reply.stats->collisions;
429 stats->rx_length_errors = reply.stats->rx_length_errors;
430 stats->rx_over_errors = reply.stats->rx_over_errors;
431 stats->rx_crc_errors = reply.stats->rx_crc_errors;
432 stats->rx_frame_errors = reply.stats->rx_frame_errors;
433 stats->rx_fifo_errors = reply.stats->rx_fifo_errors;
434 stats->rx_missed_errors = reply.stats->rx_missed_errors;
435 stats->tx_aborted_errors = reply.stats->tx_aborted_errors;
436 stats->tx_carrier_errors = reply.stats->tx_carrier_errors;
437 stats->tx_fifo_errors = reply.stats->tx_fifo_errors;
438 stats->tx_heartbeat_errors = reply.stats->tx_heartbeat_errors;
439 stats->tx_window_errors = reply.stats->tx_window_errors;
447 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
449 struct rtnl_link_stats64 rtnl_stats;
450 struct dpif_linux_vport vport;
453 rtnl_stats.rx_packets = stats->rx_packets;
454 rtnl_stats.tx_packets = stats->tx_packets;
455 rtnl_stats.rx_bytes = stats->rx_bytes;
456 rtnl_stats.tx_bytes = stats->tx_bytes;
457 rtnl_stats.rx_errors = stats->rx_errors;
458 rtnl_stats.tx_errors = stats->tx_errors;
459 rtnl_stats.rx_dropped = stats->rx_dropped;
460 rtnl_stats.tx_dropped = stats->tx_dropped;
461 rtnl_stats.multicast = stats->multicast;
462 rtnl_stats.collisions = stats->collisions;
463 rtnl_stats.rx_length_errors = stats->rx_length_errors;
464 rtnl_stats.rx_over_errors = stats->rx_over_errors;
465 rtnl_stats.rx_crc_errors = stats->rx_crc_errors;
466 rtnl_stats.rx_frame_errors = stats->rx_frame_errors;
467 rtnl_stats.rx_fifo_errors = stats->rx_fifo_errors;
468 rtnl_stats.rx_missed_errors = stats->rx_missed_errors;
469 rtnl_stats.tx_aborted_errors = stats->tx_aborted_errors;
470 rtnl_stats.tx_carrier_errors = stats->tx_carrier_errors;
471 rtnl_stats.tx_fifo_errors = stats->tx_fifo_errors;
472 rtnl_stats.tx_heartbeat_errors = stats->tx_heartbeat_errors;
473 rtnl_stats.tx_window_errors = stats->tx_window_errors;
475 dpif_linux_vport_init(&vport);
476 vport.cmd = ODP_VPORT_CMD_SET;
477 vport.name = netdev_get_name(netdev);
478 vport.stats = &rtnl_stats;
480 err = dpif_linux_vport_transact(&vport, NULL, NULL);
482 /* If the vport layer doesn't know about the device, that doesn't mean it
483 * doesn't exist (after all were able to open it when netdev_open() was
484 * called), it just means that it isn't attached and we'll be getting
485 * stats a different way. */
494 netdev_vport_get_status(const struct netdev *netdev, struct shash *sh)
496 const char *iface = netdev_vport_get_tnl_iface(netdev);
499 struct netdev *egress_netdev;
501 shash_add(sh, "tunnel_egress_iface", xstrdup(iface));
503 if (!netdev_open_default(iface, &egress_netdev)) {
504 shash_add(sh, "tunnel_egress_iface_carrier",
505 xstrdup(netdev_get_carrier(egress_netdev)
507 netdev_close(egress_netdev);
515 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
516 enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
517 enum netdev_flags *old_flagsp)
519 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
523 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
528 make_poll_name(const struct netdev *netdev)
530 return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
534 netdev_vport_poll_add(struct netdev *netdev,
535 void (*cb)(struct netdev_notifier *), void *aux,
536 struct netdev_notifier **notifierp)
538 char *poll_name = make_poll_name(netdev);
539 struct netdev_vport_notifier *notifier;
541 struct shash_node *shash_node;
543 shash_node = shash_find(&netdev_vport_notifiers, poll_name);
545 list = xmalloc(sizeof *list);
547 shash_node = shash_add(&netdev_vport_notifiers, poll_name, list);
549 list = shash_node->data;
552 notifier = xmalloc(sizeof *notifier);
553 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
554 list_push_back(list, ¬ifier->list_node);
555 notifier->shash_node = shash_node;
557 *notifierp = ¬ifier->notifier;
564 netdev_vport_poll_remove(struct netdev_notifier *notifier_)
566 struct netdev_vport_notifier *notifier =
567 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
571 list = list_remove(¬ifier->list_node);
572 if (list_is_empty(list)) {
573 shash_delete(&netdev_vport_notifiers, notifier->shash_node);
581 netdev_vport_run(void)
587 netdev_vport_wait(void)
592 /* get_tnl_iface() implementation. */
594 netdev_vport_get_tnl_iface(const struct netdev *netdev)
596 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1];
598 struct netdev_dev_vport *ndv;
599 static char name[IFNAMSIZ];
601 ndv = netdev_dev_vport_cast(netdev_get_dev(netdev));
602 if (tnl_port_config_from_nlattr(ndv->options->data, ndv->options->size,
606 route = nl_attr_get_be32(a[ODP_TUNNEL_ATTR_DST_IPV4]);
608 if (route_table_get_name(route, name)) {
615 /* Helper functions. */
618 netdev_vport_poll_notify(const struct netdev *netdev)
620 char *poll_name = make_poll_name(netdev);
621 struct list *list = shash_find_data(&netdev_vport_notifiers,
625 struct netdev_vport_notifier *notifier;
627 LIST_FOR_EACH (notifier, list_node, list) {
628 struct netdev_notifier *n = ¬ifier->notifier;
636 /* Code specific to individual vport types. */
639 set_key(const struct shash *args, const char *name, uint16_t type,
640 struct ofpbuf *options)
644 s = shash_find_data(args, name);
646 s = shash_find_data(args, "key");
652 if (!strcmp(s, "flow")) {
653 /* This is the default if no attribute is present. */
655 nl_msg_put_be64(options, type, htonll(strtoull(s, NULL, 0)));
660 parse_tunnel_config(const char *name, const char *type,
661 const struct shash *args, struct ofpbuf *options)
664 bool is_ipsec = false;
665 struct shash_node *node;
666 bool ipsec_mech_set = false;
667 ovs_be32 daddr = htonl(0);
670 flags = TNL_F_DF_DEFAULT | TNL_F_PMTUD | TNL_F_HDR_CACHE;
671 if (!strcmp(type, "gre")) {
673 } else if (!strcmp(type, "ipsec_gre")) {
676 flags |= TNL_F_IPSEC;
677 flags &= ~TNL_F_HDR_CACHE;
680 SHASH_FOR_EACH (node, args) {
681 if (!strcmp(node->name, "remote_ip")) {
682 struct in_addr in_addr;
683 if (lookup_ip(node->data, &in_addr)) {
684 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
686 daddr = in_addr.s_addr;
688 } else if (!strcmp(node->name, "local_ip")) {
689 struct in_addr in_addr;
690 if (lookup_ip(node->data, &in_addr)) {
691 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
693 nl_msg_put_be32(options, ODP_TUNNEL_ATTR_SRC_IPV4,
696 } else if (!strcmp(node->name, "tos")) {
697 if (!strcmp(node->data, "inherit")) {
698 flags |= TNL_F_TOS_INHERIT;
700 nl_msg_put_u8(options, ODP_TUNNEL_ATTR_TOS, atoi(node->data));
702 } else if (!strcmp(node->name, "ttl")) {
703 if (!strcmp(node->data, "inherit")) {
704 flags |= TNL_F_TTL_INHERIT;
706 nl_msg_put_u8(options, ODP_TUNNEL_ATTR_TTL, atoi(node->data));
708 } else if (!strcmp(node->name, "csum") && is_gre) {
709 if (!strcmp(node->data, "true")) {
712 } else if (!strcmp(node->name, "df_inherit")) {
713 if (!strcmp(node->data, "true")) {
714 flags |= TNL_F_DF_INHERIT;
716 } else if (!strcmp(node->name, "df_default")) {
717 if (!strcmp(node->data, "false")) {
718 flags &= ~TNL_F_DF_DEFAULT;
720 } else if (!strcmp(node->name, "pmtud")) {
721 if (!strcmp(node->data, "false")) {
722 flags &= ~TNL_F_PMTUD;
724 } else if (!strcmp(node->name, "header_cache")) {
725 if (!strcmp(node->data, "false")) {
726 flags &= ~TNL_F_HDR_CACHE;
728 } else if (!strcmp(node->name, "peer_cert") && is_ipsec) {
729 if (shash_find(args, "certificate")) {
730 ipsec_mech_set = true;
732 const char *use_ssl_cert;
734 /* If the "use_ssl_cert" is true, then "certificate" and
735 * "private_key" will be pulled from the SSL table. The
736 * use of this option is strongly discouraged, since it
737 * will like be removed when multiple SSL configurations
738 * are supported by OVS.
740 use_ssl_cert = shash_find_data(args, "use_ssl_cert");
741 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
742 VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
746 ipsec_mech_set = true;
748 } else if (!strcmp(node->name, "psk") && is_ipsec) {
749 ipsec_mech_set = true;
751 && (!strcmp(node->name, "certificate")
752 || !strcmp(node->name, "private_key")
753 || !strcmp(node->name, "use_ssl_cert"))) {
754 /* Ignore options not used by the netdev. */
755 } else if (is_gre && (!strcmp(node->name, "key") ||
756 !strcmp(node->name, "in_key") ||
757 !strcmp(node->name, "out_key"))) {
758 /* Handled separately below. */
760 VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->name);
765 char *file_name = xasprintf("%s/%s", ovs_rundir(),
766 "ovs-monitor-ipsec.pid");
767 pid_t pid = read_pidfile(file_name);
770 VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
775 if (shash_find(args, "peer_cert") && shash_find(args, "psk")) {
776 VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
780 if (!ipsec_mech_set) {
781 VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
788 set_key(args, "in_key", ODP_TUNNEL_ATTR_IN_KEY, options);
789 set_key(args, "out_key", ODP_TUNNEL_ATTR_OUT_KEY, options);
793 VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
797 nl_msg_put_be32(options, ODP_TUNNEL_ATTR_DST_IPV4, daddr);
799 nl_msg_put_u32(options, ODP_TUNNEL_ATTR_FLAGS, flags);
805 tnl_port_config_from_nlattr(const struct nlattr *options, size_t options_len,
806 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1])
808 static const struct nl_policy odp_tunnel_policy[] = {
809 [ODP_TUNNEL_ATTR_FLAGS] = { .type = NL_A_U32 },
810 [ODP_TUNNEL_ATTR_DST_IPV4] = { .type = NL_A_BE32 },
811 [ODP_TUNNEL_ATTR_SRC_IPV4] = { .type = NL_A_BE32, .optional = true },
812 [ODP_TUNNEL_ATTR_IN_KEY] = { .type = NL_A_BE64, .optional = true },
813 [ODP_TUNNEL_ATTR_OUT_KEY] = { .type = NL_A_BE64, .optional = true },
814 [ODP_TUNNEL_ATTR_TOS] = { .type = NL_A_U8, .optional = true },
815 [ODP_TUNNEL_ATTR_TTL] = { .type = NL_A_U8, .optional = true },
819 ofpbuf_use_const(&buf, options, options_len);
820 if (!nl_policy_parse(&buf, 0, odp_tunnel_policy,
821 a, ARRAY_SIZE(odp_tunnel_policy))) {
828 get_be64_or_zero(const struct nlattr *a)
830 return a ? ntohll(nl_attr_get_be64(a)) : 0;
834 unparse_tunnel_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
835 const struct nlattr *options, size_t options_len,
838 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1];
843 error = tnl_port_config_from_nlattr(options, options_len, a);
848 flags = nl_attr_get_u32(a[ODP_TUNNEL_ATTR_FLAGS]);
849 if (!(flags & TNL_F_HDR_CACHE) == !(flags & TNL_F_IPSEC)) {
850 smap_add(args, "header_cache",
851 flags & TNL_F_HDR_CACHE ? "true" : "false");
854 daddr = nl_attr_get_be32(a[ODP_TUNNEL_ATTR_DST_IPV4]);
855 shash_add(args, "remote_ip", xasprintf(IP_FMT, IP_ARGS(&daddr)));
857 if (a[ODP_TUNNEL_ATTR_SRC_IPV4]) {
858 ovs_be32 saddr = nl_attr_get_be32(a[ODP_TUNNEL_ATTR_SRC_IPV4]);
859 shash_add(args, "local_ip", xasprintf(IP_FMT, IP_ARGS(&saddr)));
862 if (!a[ODP_TUNNEL_ATTR_IN_KEY] && !a[ODP_TUNNEL_ATTR_OUT_KEY]) {
863 smap_add(args, "key", "flow");
865 uint64_t in_key = get_be64_or_zero(a[ODP_TUNNEL_ATTR_IN_KEY]);
866 uint64_t out_key = get_be64_or_zero(a[ODP_TUNNEL_ATTR_OUT_KEY]);
868 if (in_key && in_key == out_key) {
869 shash_add(args, "key", xasprintf("%"PRIu64, in_key));
871 if (!a[ODP_TUNNEL_ATTR_IN_KEY]) {
872 smap_add(args, "in_key", "flow");
874 shash_add(args, "in_key", xasprintf("%"PRIu64, in_key));
877 if (!a[ODP_TUNNEL_ATTR_OUT_KEY]) {
878 smap_add(args, "out_key", "flow");
879 } else if (out_key) {
880 shash_add(args, "out_key", xasprintf("%"PRIu64, out_key));
885 if (flags & TNL_F_TTL_INHERIT) {
886 smap_add(args, "tos", "inherit");
887 } else if (a[ODP_TUNNEL_ATTR_TTL]) {
888 int ttl = nl_attr_get_u8(a[ODP_TUNNEL_ATTR_TTL]);
889 shash_add(args, "tos", xasprintf("%d", ttl));
892 if (flags & TNL_F_TOS_INHERIT) {
893 smap_add(args, "tos", "inherit");
894 } else if (a[ODP_TUNNEL_ATTR_TOS]) {
895 int tos = nl_attr_get_u8(a[ODP_TUNNEL_ATTR_TOS]);
896 shash_add(args, "tos", xasprintf("%d", tos));
899 if (flags & TNL_F_CSUM) {
900 smap_add(args, "csum", "true");
902 if (flags & TNL_F_DF_INHERIT) {
903 smap_add(args, "df_inherit", "true");
905 if (!(flags & TNL_F_DF_DEFAULT)) {
906 smap_add(args, "df_default", "false");
908 if (!(flags & TNL_F_PMTUD)) {
909 smap_add(args, "pmtud", "false");
916 parse_patch_config(const char *name, const char *type OVS_UNUSED,
917 const struct shash *args, struct ofpbuf *options)
921 peer = shash_find_data(args, "peer");
923 VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
927 if (shash_count(args) > 1) {
928 VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
932 if (strlen(peer) >= IFNAMSIZ) {
933 VLOG_ERR("%s: patch 'peer' arg too long", name);
937 if (!strcmp(name, peer)) {
938 VLOG_ERR("%s: patch peer must not be self", name);
942 nl_msg_put_string(options, ODP_PATCH_ATTR_PEER, peer);
948 unparse_patch_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
949 const struct nlattr *options, size_t options_len,
952 static const struct nl_policy odp_patch_policy[] = {
953 [ODP_PATCH_ATTR_PEER] = { .type = NL_A_STRING,
958 struct nlattr *a[ARRAY_SIZE(odp_patch_policy)];
961 ofpbuf_use_const(&buf, options, options_len);
962 if (!nl_policy_parse(&buf, 0, odp_patch_policy,
963 a, ARRAY_SIZE(odp_patch_policy))) {
967 smap_add(args, "peer", nl_attr_get_string(a[ODP_PATCH_ATTR_PEER]));
971 #define VPORT_FUNCTIONS(GET_STATUS) \
976 netdev_vport_create, \
977 netdev_vport_destroy, \
978 netdev_vport_set_config, \
981 netdev_vport_close, \
983 NULL, /* enumerate */ \
986 NULL, /* recv_wait */ \
989 netdev_vport_send, /* send */ \
990 NULL, /* send_wait */ \
992 netdev_vport_set_etheraddr, \
993 netdev_vport_get_etheraddr, \
994 netdev_vport_get_mtu, \
995 NULL, /* get_ifindex */ \
996 NULL, /* get_carrier */ \
997 NULL, /* get_miimon */ \
998 netdev_vport_get_stats, \
999 netdev_vport_set_stats, \
1001 NULL, /* get_features */ \
1002 NULL, /* set_advertisements */ \
1003 NULL, /* get_vlan_vid */ \
1005 NULL, /* set_policing */ \
1006 NULL, /* get_qos_types */ \
1007 NULL, /* get_qos_capabilities */ \
1008 NULL, /* get_qos */ \
1009 NULL, /* set_qos */ \
1010 NULL, /* get_queue */ \
1011 NULL, /* set_queue */ \
1012 NULL, /* delete_queue */ \
1013 NULL, /* get_queue_stats */ \
1014 NULL, /* dump_queues */ \
1015 NULL, /* dump_queue_stats */ \
1017 NULL, /* get_in4 */ \
1018 NULL, /* set_in4 */ \
1019 NULL, /* get_in6 */ \
1020 NULL, /* add_router */ \
1021 NULL, /* get_next_hop */ \
1023 NULL, /* arp_lookup */ \
1025 netdev_vport_update_flags, \
1027 netdev_vport_poll_add, \
1028 netdev_vport_poll_remove,
1031 netdev_vport_register(void)
1033 static const struct vport_class vport_classes[] = {
1034 { ODP_VPORT_TYPE_GRE,
1035 { "gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
1036 parse_tunnel_config, unparse_tunnel_config },
1038 { ODP_VPORT_TYPE_GRE,
1039 { "ipsec_gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
1040 parse_tunnel_config, unparse_tunnel_config },
1042 { ODP_VPORT_TYPE_CAPWAP,
1043 { "capwap", VPORT_FUNCTIONS(netdev_vport_get_status) },
1044 parse_tunnel_config, unparse_tunnel_config },
1046 { ODP_VPORT_TYPE_PATCH,
1047 { "patch", VPORT_FUNCTIONS(NULL) },
1048 parse_patch_config, unparse_patch_config }
1053 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
1054 netdev_register_provider(&vport_classes[i].netdev_class);