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"
29 #include "dpif-linux.h"
33 #include "netdev-provider.h"
35 #include "netlink-socket.h"
37 #include "openvswitch/datapath-protocol.h"
38 #include "openvswitch/tunnel.h"
40 #include "route-table.h"
41 #include "rtnetlink.h"
42 #include "rtnetlink-link.h"
44 #include "socket-util.h"
47 VLOG_DEFINE_THIS_MODULE(netdev_vport);
49 static struct hmap name_map;
50 static struct rtnetlink_notifier netdev_vport_link_notifier;
53 struct hmap_node node; /* Node in name_map. */
54 uint32_t ifi_index; /* Kernel interface index. */
56 char ifname[IFNAMSIZ]; /* Interface name. */
59 struct netdev_vport_notifier {
60 struct netdev_notifier notifier;
61 struct list list_node;
62 struct shash_node *shash_node;
65 struct netdev_dev_vport {
66 struct netdev_dev netdev_dev;
67 struct ofpbuf *options;
75 enum odp_vport_type type;
76 struct netdev_class netdev_class;
77 int (*parse_config)(const char *name, const char *type,
78 const struct shash *args, struct ofpbuf *options);
79 int (*unparse_config)(const char *name, const char *type,
80 const struct nlattr *options, size_t options_len,
84 static struct shash netdev_vport_notifiers =
85 SHASH_INITIALIZER(&netdev_vport_notifiers);
87 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
89 static int netdev_vport_create(const struct netdev_class *, const char *,
90 const struct shash *, struct netdev_dev **);
91 static void netdev_vport_poll_notify(const struct netdev *);
92 static int tnl_port_config_from_nlattr(const struct nlattr *options,
94 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1]);
96 static void netdev_vport_tnl_iface_init(void);
97 static void netdev_vport_link_change(const struct rtnetlink_link_change *,
99 static const char *netdev_vport_get_tnl_iface(const struct netdev *netdev);
102 is_vport_class(const struct netdev_class *class)
104 return class->create == netdev_vport_create;
107 static const struct vport_class *
108 vport_class_cast(const struct netdev_class *class)
110 assert(is_vport_class(class));
111 return CONTAINER_OF(class, struct vport_class, netdev_class);
114 static struct netdev_dev_vport *
115 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
117 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
118 return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
121 static struct netdev_vport *
122 netdev_vport_cast(const struct netdev *netdev)
124 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
125 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
126 return CONTAINER_OF(netdev, struct netdev_vport, netdev);
129 /* If 'netdev' is a vport netdev, returns an ofpbuf that contains Netlink
130 * options to include in ODP_VPORT_ATTR_OPTIONS for configuring that vport.
131 * Otherwise returns NULL. */
132 const struct ofpbuf *
133 netdev_vport_get_options(const struct netdev *netdev)
135 const struct netdev_dev *dev = netdev_get_dev(netdev);
137 return (is_vport_class(netdev_dev_get_class(dev))
138 ? netdev_dev_vport_cast(dev)->options
143 netdev_vport_get_vport_type(const struct netdev *netdev)
145 const struct netdev_dev *dev = netdev_get_dev(netdev);
146 const struct netdev_class *class = netdev_dev_get_class(dev);
148 return (is_vport_class(class) ? vport_class_cast(class)->type
149 : class == &netdev_internal_class ? ODP_VPORT_TYPE_INTERNAL
150 : class == &netdev_linux_class ? ODP_VPORT_TYPE_NETDEV
151 : ODP_VPORT_TYPE_UNSPEC);
155 netdev_vport_get_netdev_type(const struct dpif_linux_vport *vport)
157 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1];
159 switch (vport->type) {
160 case ODP_VPORT_TYPE_UNSPEC:
163 case ODP_VPORT_TYPE_NETDEV:
166 case ODP_VPORT_TYPE_INTERNAL:
169 case ODP_VPORT_TYPE_PATCH:
172 case ODP_VPORT_TYPE_GRE:
173 if (tnl_port_config_from_nlattr(vport->options, vport->options_len,
177 return (nl_attr_get_u32(a[ODP_TUNNEL_ATTR_FLAGS]) & TNL_F_IPSEC
178 ? "ipsec_gre" : "gre");
180 case ODP_VPORT_TYPE_CAPWAP:
183 case __ODP_VPORT_TYPE_MAX:
187 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
188 vport->dp_idx, vport->name, (unsigned int) vport->type);
193 netdev_vport_init(void)
195 netdev_vport_tnl_iface_init();
196 route_table_register();
201 netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
202 const struct shash *args,
203 struct netdev_dev **netdev_devp)
205 const struct vport_class *vport_class = vport_class_cast(netdev_class);
206 struct ofpbuf *options = NULL;
207 struct shash fetched_args;
210 shash_init(&fetched_args);
212 if (!shash_is_empty(args)) {
213 /* Parse the provided configuration. */
214 options = ofpbuf_new(64);
215 error = vport_class->parse_config(name, netdev_class->type,
218 /* Fetch an existing configuration from the kernel.
220 * This case could be ambiguous with initializing a new vport with an
221 * empty configuration, but none of the existing vport classes accept
222 * an empty configuration. */
223 struct dpif_linux_vport reply;
226 error = dpif_linux_vport_get(name, &reply, &buf);
228 /* XXX verify correct type */
229 error = vport_class->unparse_config(name, netdev_class->type,
234 VLOG_ERR_RL(&rl, "%s: failed to parse kernel config (%s)",
235 name, strerror(error));
237 options = ofpbuf_clone_data(reply.options, reply.options_len);
241 VLOG_ERR_RL(&rl, "%s: vport query failed (%s)",
242 name, strerror(error));
247 struct netdev_dev_vport *dev;
249 dev = xmalloc(sizeof *dev);
250 netdev_dev_init(&dev->netdev_dev, name,
251 shash_is_empty(&fetched_args) ? args : &fetched_args,
253 dev->options = options;
255 *netdev_devp = &dev->netdev_dev;
257 ofpbuf_delete(options);
260 shash_destroy(&fetched_args);
266 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
268 struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
270 route_table_unregister();
275 netdev_vport_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
276 struct netdev **netdevp)
278 struct netdev_vport *netdev;
280 netdev = xmalloc(sizeof *netdev);
281 netdev_init(&netdev->netdev, netdev_dev_);
283 *netdevp = &netdev->netdev;
288 netdev_vport_close(struct netdev *netdev_)
290 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
295 netdev_vport_set_config(struct netdev_dev *dev_, const struct shash *args)
297 const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
298 const struct vport_class *vport_class = vport_class_cast(netdev_class);
299 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
300 const char *name = netdev_dev_get_name(dev_);
301 struct ofpbuf *options;
304 options = ofpbuf_new(64);
305 error = vport_class->parse_config(name, netdev_dev_get_type(dev_),
308 && (options->size != dev->options->size
309 || memcmp(options->data, dev->options->data, options->size))) {
310 struct dpif_linux_vport vport;
312 dpif_linux_vport_init(&vport);
313 vport.cmd = ODP_VPORT_SET;
315 vport.options = options->data;
316 vport.options_len = options->size;
317 error = dpif_linux_vport_transact(&vport, NULL, NULL);
318 if (!error || error == ENODEV) {
319 /* Either reconfiguration succeeded or this vport is not installed
320 * in the kernel (e.g. it hasn't been added to a dpif yet with
321 * dpif_port_add()). */
322 ofpbuf_delete(dev->options);
323 dev->options = options;
328 ofpbuf_delete(options);
334 netdev_vport_set_etheraddr(struct netdev *netdev,
335 const uint8_t mac[ETH_ADDR_LEN])
337 struct dpif_linux_vport vport;
340 dpif_linux_vport_init(&vport);
341 vport.cmd = ODP_VPORT_SET;
342 vport.name = netdev_get_name(netdev);
345 error = dpif_linux_vport_transact(&vport, NULL, NULL);
347 netdev_vport_poll_notify(netdev);
353 netdev_vport_get_etheraddr(const struct netdev *netdev,
354 uint8_t mac[ETH_ADDR_LEN])
356 struct dpif_linux_vport reply;
360 error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
363 memcpy(mac, reply.address, ETH_ADDR_LEN);
373 netdev_vport_get_mtu(const struct netdev *netdev, int *mtup)
375 struct dpif_linux_vport reply;
379 error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
388 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
390 struct dpif_linux_vport reply;
394 error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
397 } else if (!reply.stats) {
402 stats->rx_packets = reply.stats->rx_packets;
403 stats->tx_packets = reply.stats->tx_packets;
404 stats->rx_bytes = reply.stats->rx_bytes;
405 stats->tx_bytes = reply.stats->tx_bytes;
406 stats->rx_errors = reply.stats->rx_errors;
407 stats->tx_errors = reply.stats->tx_errors;
408 stats->rx_dropped = reply.stats->rx_dropped;
409 stats->tx_dropped = reply.stats->tx_dropped;
410 stats->multicast = reply.stats->multicast;
411 stats->collisions = reply.stats->collisions;
412 stats->rx_length_errors = reply.stats->rx_length_errors;
413 stats->rx_over_errors = reply.stats->rx_over_errors;
414 stats->rx_crc_errors = reply.stats->rx_crc_errors;
415 stats->rx_frame_errors = reply.stats->rx_frame_errors;
416 stats->rx_fifo_errors = reply.stats->rx_fifo_errors;
417 stats->rx_missed_errors = reply.stats->rx_missed_errors;
418 stats->tx_aborted_errors = reply.stats->tx_aborted_errors;
419 stats->tx_carrier_errors = reply.stats->tx_carrier_errors;
420 stats->tx_fifo_errors = reply.stats->tx_fifo_errors;
421 stats->tx_heartbeat_errors = reply.stats->tx_heartbeat_errors;
422 stats->tx_window_errors = reply.stats->tx_window_errors;
430 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
432 struct rtnl_link_stats64 rtnl_stats;
433 struct dpif_linux_vport vport;
436 rtnl_stats.rx_packets = stats->rx_packets;
437 rtnl_stats.tx_packets = stats->tx_packets;
438 rtnl_stats.rx_bytes = stats->rx_bytes;
439 rtnl_stats.tx_bytes = stats->tx_bytes;
440 rtnl_stats.rx_errors = stats->rx_errors;
441 rtnl_stats.tx_errors = stats->tx_errors;
442 rtnl_stats.rx_dropped = stats->rx_dropped;
443 rtnl_stats.tx_dropped = stats->tx_dropped;
444 rtnl_stats.multicast = stats->multicast;
445 rtnl_stats.collisions = stats->collisions;
446 rtnl_stats.rx_length_errors = stats->rx_length_errors;
447 rtnl_stats.rx_over_errors = stats->rx_over_errors;
448 rtnl_stats.rx_crc_errors = stats->rx_crc_errors;
449 rtnl_stats.rx_frame_errors = stats->rx_frame_errors;
450 rtnl_stats.rx_fifo_errors = stats->rx_fifo_errors;
451 rtnl_stats.rx_missed_errors = stats->rx_missed_errors;
452 rtnl_stats.tx_aborted_errors = stats->tx_aborted_errors;
453 rtnl_stats.tx_carrier_errors = stats->tx_carrier_errors;
454 rtnl_stats.tx_fifo_errors = stats->tx_fifo_errors;
455 rtnl_stats.tx_heartbeat_errors = stats->tx_heartbeat_errors;
456 rtnl_stats.tx_window_errors = stats->tx_window_errors;
458 dpif_linux_vport_init(&vport);
459 vport.cmd = ODP_VPORT_SET;
460 vport.name = netdev_get_name(netdev);
461 vport.stats = &rtnl_stats;
463 err = dpif_linux_vport_transact(&vport, NULL, NULL);
465 /* If the vport layer doesn't know about the device, that doesn't mean it
466 * doesn't exist (after all were able to open it when netdev_open() was
467 * called), it just means that it isn't attached and we'll be getting
468 * stats a different way. */
477 netdev_vport_get_status(const struct netdev *netdev, struct shash *sh)
479 const char *iface = netdev_vport_get_tnl_iface(netdev);
482 struct netdev *egress_netdev;
484 shash_add(sh, "tunnel_egress_iface", xstrdup(iface));
486 if (!netdev_open_default(iface, &egress_netdev)) {
487 shash_add(sh, "tunnel_egress_iface_carrier",
488 xstrdup(netdev_get_carrier(egress_netdev)
490 netdev_close(egress_netdev);
498 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
499 enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
500 enum netdev_flags *old_flagsp)
502 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
506 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
511 make_poll_name(const struct netdev *netdev)
513 return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
517 netdev_vport_poll_add(struct netdev *netdev,
518 void (*cb)(struct netdev_notifier *), void *aux,
519 struct netdev_notifier **notifierp)
521 char *poll_name = make_poll_name(netdev);
522 struct netdev_vport_notifier *notifier;
524 struct shash_node *shash_node;
526 shash_node = shash_find_data(&netdev_vport_notifiers, poll_name);
528 list = xmalloc(sizeof *list);
530 shash_node = shash_add(&netdev_vport_notifiers, poll_name, list);
532 list = shash_node->data;
535 notifier = xmalloc(sizeof *notifier);
536 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
537 list_push_back(list, ¬ifier->list_node);
538 notifier->shash_node = shash_node;
540 *notifierp = ¬ifier->notifier;
547 netdev_vport_poll_remove(struct netdev_notifier *notifier_)
549 struct netdev_vport_notifier *notifier =
550 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
554 list = list_remove(¬ifier->list_node);
555 if (list_is_empty(list)) {
556 shash_delete(&netdev_vport_notifiers, notifier->shash_node);
564 netdev_vport_run(void)
566 rtnetlink_link_notifier_run();
571 netdev_vport_wait(void)
573 rtnetlink_link_notifier_wait();
577 /* get_tnl_iface() implementation. */
579 static struct name_node *
580 name_node_lookup(int ifi_index)
582 struct name_node *nn;
584 HMAP_FOR_EACH_WITH_HASH(nn, node, hash_int(ifi_index, 0), &name_map) {
585 if (nn->ifi_index == ifi_index) {
593 /* Queries the kernel for fresh data to populate the name map with. */
595 netdev_vport_reset_names(void)
599 struct rtgenmsg *rtmsg;
600 struct ofpbuf request, reply;
601 static struct nl_sock *rtnl_sock;
602 struct name_node *nn, *nn_next;
604 HMAP_FOR_EACH_SAFE(nn, nn_next, node, &name_map) {
605 hmap_remove(&name_map, &nn->node);
609 error = nl_sock_create(NETLINK_ROUTE, &rtnl_sock);
611 VLOG_WARN_RL(&rl, "Failed to create NETLINK_ROUTE socket");
615 ofpbuf_init(&request, 0);
617 nl_msg_put_nlmsghdr(&request, sizeof *rtmsg, RTM_GETLINK, NLM_F_REQUEST);
619 rtmsg = ofpbuf_put_zeros(&request, sizeof *rtmsg);
620 rtmsg->rtgen_family = AF_INET;
622 nl_dump_start(&dump, rtnl_sock, &request);
624 while (nl_dump_next(&dump, &reply)) {
625 struct rtnetlink_link_change change;
627 if (rtnetlink_link_parse(&reply, &change)) {
628 netdev_vport_link_change(&change, NULL);
631 nl_sock_destroy(rtnl_sock);
633 return nl_dump_done(&dump);
637 netdev_vport_link_change(const struct rtnetlink_link_change *change,
638 void *aux OVS_UNUSED)
642 netdev_vport_reset_names();
643 } else if (change->nlmsg_type == RTM_NEWLINK) {
644 struct name_node *nn;
646 if (name_node_lookup(change->ifi_index)) {
650 nn = xzalloc(sizeof *nn);
651 nn->ifi_index = change->ifi_index;
653 strncpy(nn->ifname, change->ifname, IFNAMSIZ);
654 nn->ifname[IFNAMSIZ - 1] = '\0';
656 hmap_insert(&name_map, &nn->node, hash_int(nn->ifi_index, 0));
657 } else if (change->nlmsg_type == RTM_DELLINK) {
658 struct name_node *nn;
660 nn = name_node_lookup(change->ifi_index);
663 hmap_remove(&name_map, &nn->node);
668 VLOG_WARN_RL(&rl, "Received unexpected rtnetlink message type %d",
674 netdev_vport_tnl_iface_init(void)
676 static bool tnl_iface_is_init = false;
678 if (!tnl_iface_is_init) {
679 hmap_init(&name_map);
681 rtnetlink_link_notifier_register(&netdev_vport_link_notifier,
682 netdev_vport_link_change, NULL);
684 netdev_vport_reset_names();
685 tnl_iface_is_init = true;
690 netdev_vport_get_tnl_iface(const struct netdev *netdev)
692 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1];
695 struct netdev_dev_vport *ndv;
697 ndv = netdev_dev_vport_cast(netdev_get_dev(netdev));
698 if (tnl_port_config_from_nlattr(ndv->options->data, ndv->options->size,
702 route = nl_attr_get_be32(a[ODP_TUNNEL_ATTR_DST_IPV4]);
704 if (route_table_get_ifindex(route, &ifindex)) {
705 struct name_node *nn;
706 HMAP_FOR_EACH_WITH_HASH(nn, node, hash_int(ifindex, 0), &name_map) {
707 if (nn->ifi_index == ifindex) {
716 /* Helper functions. */
719 netdev_vport_poll_notify(const struct netdev *netdev)
721 char *poll_name = make_poll_name(netdev);
722 struct list *list = shash_find_data(&netdev_vport_notifiers,
726 struct netdev_vport_notifier *notifier;
728 LIST_FOR_EACH (notifier, list_node, list) {
729 struct netdev_notifier *n = ¬ifier->notifier;
737 /* Code specific to individual vport types. */
740 set_key(const struct shash *args, const char *name, uint16_t type,
741 struct ofpbuf *options)
745 s = shash_find_data(args, name);
747 s = shash_find_data(args, "key");
753 if (!strcmp(s, "flow")) {
754 /* This is the default if no attribute is present. */
756 nl_msg_put_be64(options, type, htonll(strtoull(s, NULL, 0)));
761 parse_tunnel_config(const char *name, const char *type,
762 const struct shash *args, struct ofpbuf *options)
765 bool is_ipsec = false;
766 struct shash_node *node;
767 bool ipsec_mech_set = false;
768 ovs_be32 daddr = htonl(0);
771 flags = TNL_F_PMTUD | TNL_F_HDR_CACHE;
772 if (!strcmp(type, "gre")) {
774 } else if (!strcmp(type, "ipsec_gre")) {
777 flags |= TNL_F_IPSEC;
778 flags &= ~TNL_F_HDR_CACHE;
781 SHASH_FOR_EACH (node, args) {
782 if (!strcmp(node->name, "remote_ip")) {
783 struct in_addr in_addr;
784 if (lookup_ip(node->data, &in_addr)) {
785 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
787 daddr = in_addr.s_addr;
789 } else if (!strcmp(node->name, "local_ip")) {
790 struct in_addr in_addr;
791 if (lookup_ip(node->data, &in_addr)) {
792 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
794 nl_msg_put_be32(options, ODP_TUNNEL_ATTR_SRC_IPV4,
797 } else if (!strcmp(node->name, "tos")) {
798 if (!strcmp(node->data, "inherit")) {
799 flags |= TNL_F_TOS_INHERIT;
801 nl_msg_put_u8(options, ODP_TUNNEL_ATTR_TOS, atoi(node->data));
803 } else if (!strcmp(node->name, "ttl")) {
804 if (!strcmp(node->data, "inherit")) {
805 flags |= TNL_F_TTL_INHERIT;
807 nl_msg_put_u8(options, ODP_TUNNEL_ATTR_TTL, atoi(node->data));
809 } else if (!strcmp(node->name, "csum") && is_gre) {
810 if (!strcmp(node->data, "true")) {
813 } else if (!strcmp(node->name, "pmtud")) {
814 if (!strcmp(node->data, "false")) {
815 flags &= ~TNL_F_PMTUD;
817 } else if (!strcmp(node->name, "header_cache")) {
818 if (!strcmp(node->data, "false")) {
819 flags &= ~TNL_F_HDR_CACHE;
821 } else if (!strcmp(node->name, "peer_cert") && is_ipsec) {
822 if (shash_find(args, "certificate")) {
823 ipsec_mech_set = true;
825 const char *use_ssl_cert;
827 /* If the "use_ssl_cert" is true, then "certificate" and
828 * "private_key" will be pulled from the SSL table. The
829 * use of this option is strongly discouraged, since it
830 * will like be removed when multiple SSL configurations
831 * are supported by OVS.
833 use_ssl_cert = shash_find_data(args, "use_ssl_cert");
834 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
835 VLOG_WARN("%s: 'peer_cert' requires 'certificate' argument",
839 ipsec_mech_set = true;
841 } else if (!strcmp(node->name, "psk") && is_ipsec) {
842 ipsec_mech_set = true;
844 && (!strcmp(node->name, "certificate")
845 || !strcmp(node->name, "private_key")
846 || !strcmp(node->name, "use_ssl_cert"))) {
847 /* Ignore options not used by the netdev. */
848 } else if (is_gre && (!strcmp(node->name, "key") &&
849 !strcmp(node->name, "in_key") &&
850 !strcmp(node->name, "out_key"))) {
851 /* Handled separately below. */
853 VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->name);
858 if (shash_find(args, "peer_cert") && shash_find(args, "psk")) {
859 VLOG_WARN("%s: cannot define both 'peer_cert' and 'psk'", name);
863 if (!ipsec_mech_set) {
864 VLOG_WARN("%s: IPsec requires an 'peer_cert' or psk' argument",
871 set_key(args, "in_key", ODP_TUNNEL_ATTR_IN_KEY, options);
872 set_key(args, "out_key", ODP_TUNNEL_ATTR_OUT_KEY, options);
876 VLOG_WARN("%s: %s type requires valid 'remote_ip' argument",
880 nl_msg_put_be32(options, ODP_TUNNEL_ATTR_DST_IPV4, daddr);
882 nl_msg_put_u32(options, ODP_TUNNEL_ATTR_FLAGS, flags);
888 tnl_port_config_from_nlattr(const struct nlattr *options, size_t options_len,
889 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1])
891 static const struct nl_policy odp_tunnel_policy[] = {
892 [ODP_TUNNEL_ATTR_FLAGS] = { .type = NL_A_U32 },
893 [ODP_TUNNEL_ATTR_DST_IPV4] = { .type = NL_A_BE32 },
894 [ODP_TUNNEL_ATTR_SRC_IPV4] = { .type = NL_A_BE32, .optional = true },
895 [ODP_TUNNEL_ATTR_IN_KEY] = { .type = NL_A_BE64, .optional = true },
896 [ODP_TUNNEL_ATTR_OUT_KEY] = { .type = NL_A_BE64, .optional = true },
897 [ODP_TUNNEL_ATTR_TOS] = { .type = NL_A_U8, .optional = true },
898 [ODP_TUNNEL_ATTR_TTL] = { .type = NL_A_U8, .optional = true },
902 ofpbuf_use_const(&buf, options, options_len);
903 if (!nl_policy_parse(&buf, 0, odp_tunnel_policy,
904 a, ARRAY_SIZE(odp_tunnel_policy))) {
911 get_be64_or_zero(const struct nlattr *a)
913 return a ? ntohll(nl_attr_get_be64(a)) : 0;
917 unparse_tunnel_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
918 const struct nlattr *options, size_t options_len,
921 struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1];
926 error = tnl_port_config_from_nlattr(options, options_len, a);
931 flags = nl_attr_get_u32(a[ODP_TUNNEL_ATTR_FLAGS]);
932 if (!(flags & TNL_F_HDR_CACHE) == !(flags & TNL_F_IPSEC)) {
933 smap_add(args, "header_cache",
934 flags & TNL_F_HDR_CACHE ? "true" : "false");
937 daddr = nl_attr_get_be32(a[ODP_TUNNEL_ATTR_DST_IPV4]);
938 shash_add(args, "remote_ip", xasprintf(IP_FMT, IP_ARGS(&daddr)));
940 if (a[ODP_TUNNEL_ATTR_SRC_IPV4]) {
941 ovs_be32 saddr = nl_attr_get_be32(a[ODP_TUNNEL_ATTR_SRC_IPV4]);
942 shash_add(args, "local_ip", xasprintf(IP_FMT, IP_ARGS(&saddr)));
945 if (!a[ODP_TUNNEL_ATTR_IN_KEY] && !a[ODP_TUNNEL_ATTR_OUT_KEY]) {
946 smap_add(args, "key", "flow");
948 uint64_t in_key = get_be64_or_zero(a[ODP_TUNNEL_ATTR_IN_KEY]);
949 uint64_t out_key = get_be64_or_zero(a[ODP_TUNNEL_ATTR_OUT_KEY]);
951 if (in_key && in_key == out_key) {
952 shash_add(args, "key", xasprintf("%"PRIu64, in_key));
954 if (!a[ODP_TUNNEL_ATTR_IN_KEY]) {
955 smap_add(args, "in_key", "flow");
957 shash_add(args, "in_key", xasprintf("%"PRIu64, in_key));
960 if (!a[ODP_TUNNEL_ATTR_OUT_KEY]) {
961 smap_add(args, "out_key", "flow");
962 } else if (out_key) {
963 shash_add(args, "out_key", xasprintf("%"PRIu64, out_key));
968 if (flags & TNL_F_TTL_INHERIT) {
969 smap_add(args, "tos", "inherit");
970 } else if (a[ODP_TUNNEL_ATTR_TTL]) {
971 int ttl = nl_attr_get_u8(a[ODP_TUNNEL_ATTR_TTL]);
972 shash_add(args, "tos", xasprintf("%d", ttl));
975 if (flags & TNL_F_TOS_INHERIT) {
976 smap_add(args, "tos", "inherit");
977 } else if (a[ODP_TUNNEL_ATTR_TOS]) {
978 int tos = nl_attr_get_u8(a[ODP_TUNNEL_ATTR_TOS]);
979 shash_add(args, "tos", xasprintf("%d", tos));
982 if (flags & TNL_F_CSUM) {
983 smap_add(args, "csum", "true");
985 if (!(flags & TNL_F_PMTUD)) {
986 smap_add(args, "pmtud", "false");
993 parse_patch_config(const char *name, const char *type OVS_UNUSED,
994 const struct shash *args, struct ofpbuf *options)
998 peer = shash_find_data(args, "peer");
1000 VLOG_WARN("%s: patch type requires valid 'peer' argument", name);
1004 if (shash_count(args) > 1) {
1005 VLOG_WARN("%s: patch type takes only a 'peer' argument", name);
1009 if (strlen(peer) >= IFNAMSIZ) {
1010 VLOG_WARN("%s: patch 'peer' arg too long", name);
1014 if (!strcmp(name, peer)) {
1015 VLOG_WARN("%s: patch peer must not be self", name);
1019 nl_msg_put_string(options, ODP_PATCH_ATTR_PEER, peer);
1025 unparse_patch_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
1026 const struct nlattr *options, size_t options_len,
1029 static const struct nl_policy odp_patch_policy[] = {
1030 [ODP_PATCH_ATTR_PEER] = { .type = NL_A_STRING,
1031 .max_len = IFNAMSIZ,
1035 struct nlattr *a[ARRAY_SIZE(odp_patch_policy)];
1038 ofpbuf_use_const(&buf, options, options_len);
1039 if (!nl_policy_parse(&buf, 0, odp_patch_policy,
1040 a, ARRAY_SIZE(odp_patch_policy))) {
1044 smap_add(args, "peer", nl_attr_get_string(a[ODP_PATCH_ATTR_PEER]));
1048 #define VPORT_FUNCTIONS(GET_STATUS) \
1049 netdev_vport_init, \
1051 netdev_vport_wait, \
1053 netdev_vport_create, \
1054 netdev_vport_destroy, \
1055 netdev_vport_set_config, \
1057 netdev_vport_open, \
1058 netdev_vport_close, \
1060 NULL, /* enumerate */ \
1063 NULL, /* recv_wait */ \
1067 NULL, /* send_wait */ \
1069 netdev_vport_set_etheraddr, \
1070 netdev_vport_get_etheraddr, \
1071 netdev_vport_get_mtu, \
1072 NULL, /* get_ifindex */ \
1073 NULL, /* get_carrier */ \
1074 NULL, /* get_miimon */ \
1075 netdev_vport_get_stats, \
1076 netdev_vport_set_stats, \
1078 NULL, /* get_features */ \
1079 NULL, /* set_advertisements */ \
1080 NULL, /* get_vlan_vid */ \
1082 NULL, /* set_policing */ \
1083 NULL, /* get_qos_types */ \
1084 NULL, /* get_qos_capabilities */ \
1085 NULL, /* get_qos */ \
1086 NULL, /* set_qos */ \
1087 NULL, /* get_queue */ \
1088 NULL, /* set_queue */ \
1089 NULL, /* delete_queue */ \
1090 NULL, /* get_queue_stats */ \
1091 NULL, /* dump_queues */ \
1092 NULL, /* dump_queue_stats */ \
1094 NULL, /* get_in4 */ \
1095 NULL, /* set_in4 */ \
1096 NULL, /* get_in6 */ \
1097 NULL, /* add_router */ \
1098 NULL, /* get_next_hop */ \
1100 NULL, /* arp_lookup */ \
1102 netdev_vport_update_flags, \
1104 netdev_vport_poll_add, \
1105 netdev_vport_poll_remove,
1108 netdev_vport_register(void)
1110 static const struct vport_class vport_classes[] = {
1111 { ODP_VPORT_TYPE_GRE,
1112 { "gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
1113 parse_tunnel_config, unparse_tunnel_config },
1115 { ODP_VPORT_TYPE_GRE,
1116 { "ipsec_gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
1117 parse_tunnel_config, unparse_tunnel_config },
1119 { ODP_VPORT_TYPE_CAPWAP,
1120 { "capwap", VPORT_FUNCTIONS(netdev_vport_get_status) },
1121 parse_tunnel_config, unparse_tunnel_config },
1123 { ODP_VPORT_TYPE_PATCH,
1124 { "patch", VPORT_FUNCTIONS(NULL) },
1125 parse_patch_config, unparse_patch_config }
1130 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
1131 netdev_register_provider(&vport_classes[i].netdev_class);