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"
32 #include "netdev-provider.h"
34 #include "netlink-socket.h"
36 #include "openvswitch/datapath-protocol.h"
37 #include "openvswitch/tunnel.h"
39 #include "rtnetlink.h"
40 #include "route-table.h"
41 #include "rtnetlink-link.h"
43 #include "socket-util.h"
46 VLOG_DEFINE_THIS_MODULE(netdev_vport);
48 static struct hmap name_map;
49 static struct rtnetlink_notifier netdev_vport_link_notifier;
52 struct hmap_node node; /* Node in name_map. */
53 uint32_t ifi_index; /* Kernel interface index. */
55 char ifname[IFNAMSIZ]; /* Interface name. */
58 struct netdev_vport_notifier {
59 struct netdev_notifier notifier;
60 struct list list_node;
61 struct shash_node *shash_node;
64 struct netdev_dev_vport {
65 struct netdev_dev netdev_dev;
66 uint64_t config[VPORT_CONFIG_SIZE / 8];
74 enum odp_vport_type type;
75 struct netdev_class netdev_class;
76 int (*parse_config)(const char *name, const char *type,
77 const struct shash *args, void *config);
78 int (*unparse_config)(const char *name, const char *type,
79 const void *config, struct shash *args);
82 static struct shash netdev_vport_notifiers =
83 SHASH_INITIALIZER(&netdev_vport_notifiers);
85 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
87 static int netdev_vport_do_ioctl(int cmd, void *arg);
88 static int netdev_vport_create(const struct netdev_class *, const char *,
89 const struct shash *, struct netdev_dev **);
90 static void netdev_vport_poll_notify(const struct netdev *);
92 static void netdev_vport_tnl_iface_init(void);
93 static void netdev_vport_link_change(const struct rtnetlink_link_change *,
95 static const char *netdev_vport_get_tnl_iface(const struct netdev *netdev);
98 is_vport_class(const struct netdev_class *class)
100 return class->create == netdev_vport_create;
103 static const struct vport_class *
104 vport_class_cast(const struct netdev_class *class)
106 assert(is_vport_class(class));
107 return CONTAINER_OF(class, struct vport_class, netdev_class);
110 static struct netdev_dev_vport *
111 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
113 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
114 return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
117 static struct netdev_vport *
118 netdev_vport_cast(const struct netdev *netdev)
120 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
121 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
122 return CONTAINER_OF(netdev, struct netdev_vport, netdev);
125 /* If 'netdev' is a vport netdev, copies its kernel configuration into
126 * 'config'. Otherwise leaves 'config' untouched. */
128 netdev_vport_get_config(const struct netdev *netdev, void *config)
130 const struct netdev_dev *dev = netdev_get_dev(netdev);
132 if (is_vport_class(netdev_dev_get_class(dev))) {
133 const struct netdev_dev_vport *vport = netdev_dev_vport_cast(dev);
134 memcpy(config, vport->config, VPORT_CONFIG_SIZE);
139 netdev_vport_init(void)
141 netdev_vport_tnl_iface_init();
142 route_table_register();
147 netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
148 const struct shash *args,
149 struct netdev_dev **netdev_devp)
151 const struct vport_class *vport_class = vport_class_cast(netdev_class);
152 uint64_t config[VPORT_CONFIG_SIZE / 8];
153 struct shash fetched_args;
156 memset(config, 0, sizeof config);
157 shash_init(&fetched_args);
159 if (!shash_is_empty(args)) {
160 /* Parse the provided configuration. */
161 error = vport_class->parse_config(name, netdev_class->type,
164 /* Fetch an existing configuration from the kernel.
166 * This case could be ambiguous with initializing a new vport with an
167 * empty configuration, but none of the existing vport classes accept
168 * an empty configuration. */
169 struct odp_port odp_port;
171 memset(&odp_port, 0, sizeof odp_port);
172 strncpy(odp_port.devname, name, sizeof odp_port.devname);
173 error = netdev_vport_do_ioctl(ODP_VPORT_QUERY, &odp_port);
175 /* XXX verify correct type */
176 memcpy(config, odp_port.config, sizeof config);
177 error = vport_class->unparse_config(name, netdev_class->type,
181 VLOG_ERR_RL(&rl, "%s: failed to parse kernel config (%s)",
182 name, strerror(error));
185 VLOG_ERR_RL(&rl, "%s: vport query failed (%s)",
186 name, strerror(error));
191 struct netdev_dev_vport *dev;
193 dev = xmalloc(sizeof *dev);
194 netdev_dev_init(&dev->netdev_dev, name,
195 shash_is_empty(&fetched_args) ? args : &fetched_args,
197 memcpy(dev->config, config, sizeof dev->config);
199 *netdev_devp = &dev->netdev_dev;
202 shash_destroy(&fetched_args);
208 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
210 struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
212 route_table_unregister();
217 netdev_vport_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
218 struct netdev **netdevp)
220 struct netdev_vport *netdev;
222 netdev = xmalloc(sizeof *netdev);
223 netdev_init(&netdev->netdev, netdev_dev_);
225 *netdevp = &netdev->netdev;
230 netdev_vport_close(struct netdev *netdev_)
232 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
237 netdev_vport_set_config(struct netdev_dev *dev_, const struct shash *args)
239 const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
240 const struct vport_class *vport_class = vport_class_cast(netdev_class);
241 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
242 struct odp_port port;
245 memset(&port, 0, sizeof port);
246 strncpy(port.devname, netdev_dev_get_name(dev_), sizeof port.devname);
247 port.type = vport_class->type;
248 error = vport_class->parse_config(netdev_dev_get_name(dev_),
249 netdev_dev_get_type(dev_),
251 if (!error && memcmp(port.config, dev->config, sizeof dev->config)) {
252 error = netdev_vport_do_ioctl(ODP_VPORT_MOD, &port);
253 if (!error || error == ENODEV) {
254 /* Either reconfiguration succeeded or this vport is not installed
255 * in the kernel (e.g. it hasn't been added to a dpif yet with
256 * dpif_port_add()). */
257 memcpy(dev->config, port.config, sizeof dev->config);
264 netdev_vport_set_etheraddr(struct netdev *netdev,
265 const uint8_t mac[ETH_ADDR_LEN])
267 struct odp_vport_ether vport_ether;
270 ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
271 sizeof vport_ether.devname);
273 memcpy(vport_ether.ether_addr, mac, ETH_ADDR_LEN);
275 err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_SET, &vport_ether);
280 netdev_vport_poll_notify(netdev);
285 netdev_vport_get_etheraddr(const struct netdev *netdev,
286 uint8_t mac[ETH_ADDR_LEN])
288 struct odp_vport_ether vport_ether;
291 ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
292 sizeof vport_ether.devname);
294 err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_GET, &vport_ether);
299 memcpy(mac, vport_ether.ether_addr, ETH_ADDR_LEN);
304 netdev_vport_get_mtu(const struct netdev *netdev, int *mtup)
306 struct odp_vport_mtu vport_mtu;
309 ovs_strlcpy(vport_mtu.devname, netdev_get_name(netdev),
310 sizeof vport_mtu.devname);
312 err = netdev_vport_do_ioctl(ODP_VPORT_MTU_GET, &vport_mtu);
317 *mtup = vport_mtu.mtu;
322 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
324 const char *name = netdev_get_name(netdev);
325 struct odp_vport_stats_req ovsr;
328 ovs_strlcpy(ovsr.devname, name, sizeof ovsr.devname);
329 err = netdev_vport_do_ioctl(ODP_VPORT_STATS_GET, &ovsr);
334 stats->rx_packets = ovsr.stats.rx_packets;
335 stats->tx_packets = ovsr.stats.tx_packets;
336 stats->rx_bytes = ovsr.stats.rx_bytes;
337 stats->tx_bytes = ovsr.stats.tx_bytes;
338 stats->rx_errors = ovsr.stats.rx_errors;
339 stats->tx_errors = ovsr.stats.tx_errors;
340 stats->rx_dropped = ovsr.stats.rx_dropped;
341 stats->tx_dropped = ovsr.stats.tx_dropped;
342 stats->multicast = ovsr.stats.multicast;
343 stats->collisions = ovsr.stats.collisions;
344 stats->rx_length_errors = ovsr.stats.rx_length_errors;
345 stats->rx_over_errors = ovsr.stats.rx_over_errors;
346 stats->rx_crc_errors = ovsr.stats.rx_crc_errors;
347 stats->rx_frame_errors = ovsr.stats.rx_frame_errors;
348 stats->rx_fifo_errors = ovsr.stats.rx_fifo_errors;
349 stats->rx_missed_errors = ovsr.stats.rx_missed_errors;
350 stats->tx_aborted_errors = ovsr.stats.tx_aborted_errors;
351 stats->tx_carrier_errors = ovsr.stats.tx_carrier_errors;
352 stats->tx_fifo_errors = ovsr.stats.tx_fifo_errors;
353 stats->tx_heartbeat_errors = ovsr.stats.tx_heartbeat_errors;
354 stats->tx_window_errors = ovsr.stats.tx_window_errors;
360 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
362 struct odp_vport_stats_req ovsr;
365 ovs_strlcpy(ovsr.devname, netdev_get_name(netdev), sizeof ovsr.devname);
367 ovsr.stats.rx_packets = stats->rx_packets;
368 ovsr.stats.tx_packets = stats->tx_packets;
369 ovsr.stats.rx_bytes = stats->rx_bytes;
370 ovsr.stats.tx_bytes = stats->tx_bytes;
371 ovsr.stats.rx_errors = stats->rx_errors;
372 ovsr.stats.tx_errors = stats->tx_errors;
373 ovsr.stats.rx_dropped = stats->rx_dropped;
374 ovsr.stats.tx_dropped = stats->tx_dropped;
375 ovsr.stats.multicast = stats->multicast;
376 ovsr.stats.collisions = stats->collisions;
377 ovsr.stats.rx_length_errors = stats->rx_length_errors;
378 ovsr.stats.rx_over_errors = stats->rx_over_errors;
379 ovsr.stats.rx_crc_errors = stats->rx_crc_errors;
380 ovsr.stats.rx_frame_errors = stats->rx_frame_errors;
381 ovsr.stats.rx_fifo_errors = stats->rx_fifo_errors;
382 ovsr.stats.rx_missed_errors = stats->rx_missed_errors;
383 ovsr.stats.tx_aborted_errors = stats->tx_aborted_errors;
384 ovsr.stats.tx_carrier_errors = stats->tx_carrier_errors;
385 ovsr.stats.tx_fifo_errors = stats->tx_fifo_errors;
386 ovsr.stats.tx_heartbeat_errors = stats->tx_heartbeat_errors;
387 ovsr.stats.tx_window_errors = stats->tx_window_errors;
389 err = netdev_vport_do_ioctl(ODP_VPORT_STATS_SET, &ovsr);
391 /* If the vport layer doesn't know about the device, that doesn't mean it
392 * doesn't exist (after all were able to open it when netdev_open() was
393 * called), it just means that it isn't attached and we'll be getting
394 * stats a different way. */
403 netdev_vport_get_status(const struct netdev *netdev, struct shash *sh)
405 const char *iface = netdev_vport_get_tnl_iface(netdev);
408 struct netdev *egress_netdev;
410 shash_add(sh, "tunnel_egress_iface", xstrdup(iface));
412 if (!netdev_open_default(iface, &egress_netdev)) {
413 shash_add(sh, "tunnel_egress_iface_carrier",
414 xstrdup(netdev_get_carrier(egress_netdev)
416 netdev_close(egress_netdev);
424 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
425 enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
426 enum netdev_flags *old_flagsp)
428 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
432 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
437 make_poll_name(const struct netdev *netdev)
439 return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
443 netdev_vport_poll_add(struct netdev *netdev,
444 void (*cb)(struct netdev_notifier *), void *aux,
445 struct netdev_notifier **notifierp)
447 char *poll_name = make_poll_name(netdev);
448 struct netdev_vport_notifier *notifier;
450 struct shash_node *shash_node;
452 shash_node = shash_find_data(&netdev_vport_notifiers, poll_name);
454 list = xmalloc(sizeof *list);
456 shash_node = shash_add(&netdev_vport_notifiers, poll_name, list);
458 list = shash_node->data;
461 notifier = xmalloc(sizeof *notifier);
462 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
463 list_push_back(list, ¬ifier->list_node);
464 notifier->shash_node = shash_node;
466 *notifierp = ¬ifier->notifier;
473 netdev_vport_poll_remove(struct netdev_notifier *notifier_)
475 struct netdev_vport_notifier *notifier =
476 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
480 list = list_remove(¬ifier->list_node);
481 if (list_is_empty(list)) {
482 shash_delete(&netdev_vport_notifiers, notifier->shash_node);
490 netdev_vport_run(void)
492 rtnetlink_link_notifier_run();
497 netdev_vport_wait(void)
499 rtnetlink_link_notifier_wait();
503 /* get_tnl_iface() implementation. */
505 static struct name_node *
506 name_node_lookup(int ifi_index)
508 struct name_node *nn;
510 HMAP_FOR_EACH_WITH_HASH(nn, node, hash_int(ifi_index, 0), &name_map) {
511 if (nn->ifi_index == ifi_index) {
519 /* Queries the kernel for fresh data to populate the name map with. */
521 netdev_vport_reset_names(void)
525 struct rtgenmsg *rtmsg;
526 struct ofpbuf request, reply;
527 static struct nl_sock *rtnl_sock;
528 struct name_node *nn, *nn_next;
530 HMAP_FOR_EACH_SAFE(nn, nn_next, node, &name_map) {
531 hmap_remove(&name_map, &nn->node);
535 error = nl_sock_create(NETLINK_ROUTE, &rtnl_sock);
537 VLOG_WARN_RL(&rl, "Failed to create NETLINK_ROUTE socket");
541 ofpbuf_init(&request, 0);
543 nl_msg_put_nlmsghdr(&request, sizeof *rtmsg, RTM_GETLINK, NLM_F_REQUEST);
545 rtmsg = ofpbuf_put_zeros(&request, sizeof *rtmsg);
546 rtmsg->rtgen_family = AF_INET;
548 nl_dump_start(&dump, rtnl_sock, &request);
550 while (nl_dump_next(&dump, &reply)) {
551 struct rtnetlink_link_change change;
553 if (rtnetlink_link_parse(&reply, &change)) {
554 netdev_vport_link_change(&change, NULL);
557 nl_sock_destroy(rtnl_sock);
559 return nl_dump_done(&dump);
563 netdev_vport_link_change(const struct rtnetlink_link_change *change,
564 void *aux OVS_UNUSED)
568 netdev_vport_reset_names();
569 } else if (change->nlmsg_type == RTM_NEWLINK) {
570 struct name_node *nn;
572 if (name_node_lookup(change->ifi_index)) {
576 nn = xzalloc(sizeof *nn);
577 nn->ifi_index = change->ifi_index;
579 strncpy(nn->ifname, change->ifname, IFNAMSIZ);
580 nn->ifname[IFNAMSIZ - 1] = '\0';
582 hmap_insert(&name_map, &nn->node, hash_int(nn->ifi_index, 0));
583 } else if (change->nlmsg_type == RTM_DELLINK) {
584 struct name_node *nn;
586 nn = name_node_lookup(change->ifi_index);
589 hmap_remove(&name_map, &nn->node);
594 VLOG_WARN_RL(&rl, "Received unexpected rtnetlink message type %d",
600 netdev_vport_tnl_iface_init(void)
602 static bool tnl_iface_is_init = false;
604 if (!tnl_iface_is_init) {
605 hmap_init(&name_map);
607 rtnetlink_link_notifier_register(&netdev_vport_link_notifier,
608 netdev_vport_link_change, NULL);
610 netdev_vport_reset_names();
611 tnl_iface_is_init = true;
616 netdev_vport_get_tnl_iface(const struct netdev *netdev)
620 struct netdev_dev_vport *ndv;
621 struct tnl_port_config *config;
623 ndv = netdev_dev_vport_cast(netdev_get_dev(netdev));
624 config = (struct tnl_port_config *) ndv->config;
625 route = config->daddr;
627 if (route_table_get_ifindex(route, &ifindex)) {
628 struct name_node *nn;
629 HMAP_FOR_EACH_WITH_HASH(nn, node, hash_int(ifindex, 0), &name_map) {
630 if (nn->ifi_index == ifindex) {
639 /* Helper functions. */
642 netdev_vport_do_ioctl(int cmd, void *arg)
644 static int ioctl_fd = -1;
647 ioctl_fd = open("/dev/net/dp0", O_RDONLY | O_NONBLOCK);
649 VLOG_ERR_RL(&rl, "failed to open ioctl fd: %s", strerror(errno));
654 return ioctl(ioctl_fd, cmd, arg) ? errno : 0;
658 netdev_vport_poll_notify(const struct netdev *netdev)
660 char *poll_name = make_poll_name(netdev);
661 struct list *list = shash_find_data(&netdev_vport_notifiers,
665 struct netdev_vport_notifier *notifier;
667 LIST_FOR_EACH (notifier, list_node, list) {
668 struct netdev_notifier *n = ¬ifier->notifier;
676 /* Code specific to individual vport types. */
679 parse_tunnel_config(const char *name, const char *type,
680 const struct shash *args, void *configp)
683 bool is_ipsec = false;
684 struct tnl_port_config config;
685 struct shash_node *node;
686 bool ipsec_mech_set = false;
688 memset(&config, 0, sizeof config);
689 config.flags |= TNL_F_PMTUD;
690 config.flags |= TNL_F_HDR_CACHE;
692 if (!strcmp(type, "gre")) {
694 } else if (!strcmp(type, "ipsec_gre")) {
698 config.flags |= TNL_F_IPSEC;
700 /* IPsec doesn't work when header caching is enabled. */
701 config.flags &= ~TNL_F_HDR_CACHE;
704 SHASH_FOR_EACH (node, args) {
705 if (!strcmp(node->name, "remote_ip")) {
706 struct in_addr in_addr;
707 if (lookup_ip(node->data, &in_addr)) {
708 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
710 config.daddr = in_addr.s_addr;
712 } else if (!strcmp(node->name, "local_ip")) {
713 struct in_addr in_addr;
714 if (lookup_ip(node->data, &in_addr)) {
715 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
717 config.saddr = in_addr.s_addr;
719 } else if (!strcmp(node->name, "key") && is_gre) {
720 if (!strcmp(node->data, "flow")) {
721 config.flags |= TNL_F_IN_KEY_MATCH;
722 config.flags |= TNL_F_OUT_KEY_ACTION;
724 uint64_t key = strtoull(node->data, NULL, 0);
725 config.out_key = config.in_key = htonll(key);
727 } else if (!strcmp(node->name, "in_key") && is_gre) {
728 if (!strcmp(node->data, "flow")) {
729 config.flags |= TNL_F_IN_KEY_MATCH;
731 config.in_key = htonll(strtoull(node->data, NULL, 0));
733 } else if (!strcmp(node->name, "out_key") && is_gre) {
734 if (!strcmp(node->data, "flow")) {
735 config.flags |= TNL_F_OUT_KEY_ACTION;
737 config.out_key = htonll(strtoull(node->data, NULL, 0));
739 } else if (!strcmp(node->name, "tos")) {
740 if (!strcmp(node->data, "inherit")) {
741 config.flags |= TNL_F_TOS_INHERIT;
743 config.tos = atoi(node->data);
745 } else if (!strcmp(node->name, "ttl")) {
746 if (!strcmp(node->data, "inherit")) {
747 config.flags |= TNL_F_TTL_INHERIT;
749 config.ttl = atoi(node->data);
751 } else if (!strcmp(node->name, "csum") && is_gre) {
752 if (!strcmp(node->data, "true")) {
753 config.flags |= TNL_F_CSUM;
755 } else if (!strcmp(node->name, "pmtud")) {
756 if (!strcmp(node->data, "false")) {
757 config.flags &= ~TNL_F_PMTUD;
759 } else if (!strcmp(node->name, "header_cache")) {
760 if (!strcmp(node->data, "false")) {
761 config.flags &= ~TNL_F_HDR_CACHE;
763 } else if (!strcmp(node->name, "peer_cert") && is_ipsec) {
764 if (shash_find(args, "certificate")) {
765 ipsec_mech_set = true;
767 const char *use_ssl_cert;
769 /* If the "use_ssl_cert" is true, then "certificate" and
770 * "private_key" will be pulled from the SSL table. The
771 * use of this option is strongly discouraged, since it
772 * will like be removed when multiple SSL configurations
773 * are supported by OVS.
775 use_ssl_cert = shash_find_data(args, "use_ssl_cert");
776 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
777 VLOG_WARN("%s: 'peer_cert' requires 'certificate' argument",
781 ipsec_mech_set = true;
783 } else if (!strcmp(node->name, "psk") && is_ipsec) {
784 ipsec_mech_set = true;
786 && (!strcmp(node->name, "certificate")
787 || !strcmp(node->name, "private_key")
788 || !strcmp(node->name, "use_ssl_cert"))) {
789 /* Ignore options not used by the netdev. */
791 VLOG_WARN("%s: unknown %s argument '%s'",
792 name, type, node->name);
797 if (shash_find(args, "peer_cert") && shash_find(args, "psk")) {
798 VLOG_WARN("%s: cannot define both 'peer_cert' and 'psk'", name);
802 if (!ipsec_mech_set) {
803 VLOG_WARN("%s: IPsec requires an 'peer_cert' or psk' argument",
810 VLOG_WARN("%s: %s type requires valid 'remote_ip' argument",
815 BUILD_ASSERT(sizeof config <= VPORT_CONFIG_SIZE);
816 memcpy(configp, &config, sizeof config);
821 unparse_tunnel_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
822 const void *config_, struct shash *args)
824 const struct tnl_port_config *config = config_;
826 if (!(config->flags & TNL_F_HDR_CACHE) == !(config->flags & TNL_F_IPSEC)) {
827 smap_add(args, "header_cache",
828 config->flags & TNL_F_HDR_CACHE ? "true" : "false");
830 shash_add(args, "remote_ip", xasprintf(IP_FMT, IP_ARGS(&config->daddr)));
832 shash_add(args, "local_ip",
833 xasprintf(IP_FMT, IP_ARGS(&config->saddr)));
836 if ((config->flags & (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION))
837 == (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION)) {
838 smap_add(args, "key", "flow");
839 } else if (config->in_key && config->in_key == config->out_key) {
840 shash_add(args, "key",
841 xasprintf("%"PRIu64, ntohll(config->in_key)));
843 if (config->flags & TNL_F_IN_KEY_MATCH) {
844 smap_add(args, "in_key", "flow");
845 } else if (config->in_key) {
846 shash_add(args, "in_key",
847 xasprintf("%"PRIu64, ntohll(config->in_key)));
850 if (config->flags & TNL_F_OUT_KEY_ACTION) {
851 smap_add(args, "out_key", "flow");
852 } else if (config->out_key) {
853 shash_add(args, "out_key",
854 xasprintf("%"PRIu64, ntohll(config->out_key)));
858 if (config->flags & TNL_F_TTL_INHERIT) {
859 smap_add(args, "tos", "inherit");
860 } else if (config->ttl) {
861 shash_add(args, "tos", xasprintf("%"PRIu8, config->ttl));
864 if (config->flags & TNL_F_CSUM) {
865 smap_add(args, "csum", "true");
867 if (!(config->flags & TNL_F_PMTUD)) {
868 smap_add(args, "pmtud", "false");
875 parse_patch_config(const char *name, const char *type OVS_UNUSED,
876 const struct shash *args, void *configp)
880 peer = shash_find_data(args, "peer");
882 VLOG_WARN("%s: patch type requires valid 'peer' argument", name);
886 if (shash_count(args) > 1) {
887 VLOG_WARN("%s: patch type takes only a 'peer' argument", name);
891 if (strlen(peer) >= MIN(IFNAMSIZ, VPORT_CONFIG_SIZE)) {
892 VLOG_WARN("%s: patch 'peer' arg too long", name);
896 if (!strcmp(name, peer)) {
897 VLOG_WARN("%s: patch peer must not be self", name);
901 strncpy(configp, peer, VPORT_CONFIG_SIZE);
907 unparse_patch_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
908 const void *config_, struct shash *args)
912 ovs_strlcpy(peer, config_, MIN(sizeof peer, VPORT_CONFIG_SIZE));
914 smap_add(args, "peer", peer);
920 #define VPORT_FUNCTIONS(GET_STATUS) \
925 netdev_vport_create, \
926 netdev_vport_destroy, \
927 netdev_vport_set_config, \
930 netdev_vport_close, \
932 NULL, /* enumerate */ \
935 NULL, /* recv_wait */ \
939 NULL, /* send_wait */ \
941 netdev_vport_set_etheraddr, \
942 netdev_vport_get_etheraddr, \
943 netdev_vport_get_mtu, \
944 NULL, /* get_ifindex */ \
945 NULL, /* get_carrier */ \
946 NULL, /* get_miimon */ \
947 netdev_vport_get_stats, \
948 netdev_vport_set_stats, \
950 NULL, /* get_features */ \
951 NULL, /* set_advertisements */ \
952 NULL, /* get_vlan_vid */ \
954 NULL, /* set_policing */ \
955 NULL, /* get_qos_types */ \
956 NULL, /* get_qos_capabilities */ \
957 NULL, /* get_qos */ \
958 NULL, /* set_qos */ \
959 NULL, /* get_queue */ \
960 NULL, /* set_queue */ \
961 NULL, /* delete_queue */ \
962 NULL, /* get_queue_stats */ \
963 NULL, /* dump_queues */ \
964 NULL, /* dump_queue_stats */ \
966 NULL, /* get_in4 */ \
967 NULL, /* set_in4 */ \
968 NULL, /* get_in6 */ \
969 NULL, /* add_router */ \
970 NULL, /* get_next_hop */ \
972 NULL, /* arp_lookup */ \
974 netdev_vport_update_flags, \
976 netdev_vport_poll_add, \
977 netdev_vport_poll_remove,
980 netdev_vport_register(void)
982 static const struct vport_class vport_classes[] = {
983 { ODP_VPORT_TYPE_GRE,
984 { "gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
985 parse_tunnel_config, unparse_tunnel_config },
987 { ODP_VPORT_TYPE_GRE,
988 { "ipsec_gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
989 parse_tunnel_config, unparse_tunnel_config },
991 { ODP_VPORT_TYPE_CAPWAP,
992 { "capwap", VPORT_FUNCTIONS(netdev_vport_get_status) },
993 parse_tunnel_config, unparse_tunnel_config },
995 { ODP_VPORT_TYPE_PATCH,
996 { "patch", VPORT_FUNCTIONS(NULL) },
997 parse_patch_config, unparse_patch_config }
1002 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
1003 netdev_register_provider(&vport_classes[i].netdev_class);