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 struct netdev_class netdev_class;
75 int (*parse_config)(const char *name, const char *type,
76 const struct shash *args, void *config);
77 int (*unparse_config)(const char *name, const char *type,
78 const void *config, struct shash *args);
81 static struct shash netdev_vport_notifiers =
82 SHASH_INITIALIZER(&netdev_vport_notifiers);
84 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
86 static int netdev_vport_do_ioctl(int cmd, void *arg);
87 static int netdev_vport_create(const struct netdev_class *, const char *,
88 const struct shash *, struct netdev_dev **);
89 static void netdev_vport_poll_notify(const struct netdev *);
91 static void netdev_vport_tnl_iface_init(void);
92 static void netdev_vport_link_change(const struct rtnetlink_link_change *,
94 static const char *netdev_vport_get_tnl_iface(const struct netdev *netdev);
97 is_vport_class(const struct netdev_class *class)
99 return class->create == netdev_vport_create;
102 static const struct vport_class *
103 vport_class_cast(const struct netdev_class *class)
105 assert(is_vport_class(class));
106 return CONTAINER_OF(class, struct vport_class, netdev_class);
109 static struct netdev_dev_vport *
110 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
112 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
113 return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
116 static struct netdev_vport *
117 netdev_vport_cast(const struct netdev *netdev)
119 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
120 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
121 return CONTAINER_OF(netdev, struct netdev_vport, netdev);
124 /* If 'netdev' is a vport netdev, copies its kernel configuration into
125 * 'config'. Otherwise leaves 'config' untouched. */
127 netdev_vport_get_config(const struct netdev *netdev, void *config)
129 const struct netdev_dev *dev = netdev_get_dev(netdev);
131 if (is_vport_class(netdev_dev_get_class(dev))) {
132 const struct netdev_dev_vport *vport = netdev_dev_vport_cast(dev);
133 memcpy(config, vport->config, VPORT_CONFIG_SIZE);
138 netdev_vport_init(void)
140 netdev_vport_tnl_iface_init();
141 route_table_register();
146 netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
147 const struct shash *args,
148 struct netdev_dev **netdev_devp)
150 const struct vport_class *vport_class = vport_class_cast(netdev_class);
151 uint64_t config[VPORT_CONFIG_SIZE / 8];
152 struct shash fetched_args;
155 memset(config, 0, sizeof config);
156 shash_init(&fetched_args);
158 if (!shash_is_empty(args)) {
159 /* Parse the provided configuration. */
160 error = vport_class->parse_config(name, netdev_class->type,
163 /* Fetch an existing configuration from the kernel.
165 * This case could be ambiguous with initializing a new vport with an
166 * empty configuration, but none of the existing vport classes accept
167 * an empty configuration. */
168 struct odp_port odp_port;
170 memset(&odp_port, 0, sizeof odp_port);
171 strncpy(odp_port.devname, name, sizeof odp_port.devname);
172 error = netdev_vport_do_ioctl(ODP_VPORT_QUERY, &odp_port);
174 /* XXX verify correct type */
175 memcpy(config, odp_port.config, sizeof config);
176 error = vport_class->unparse_config(name, netdev_class->type,
180 VLOG_ERR_RL(&rl, "%s: failed to parse kernel config (%s)",
181 name, strerror(error));
184 VLOG_ERR_RL(&rl, "%s: vport query failed (%s)",
185 name, strerror(error));
190 struct netdev_dev_vport *dev;
192 dev = xmalloc(sizeof *dev);
193 netdev_dev_init(&dev->netdev_dev, name,
194 shash_is_empty(&fetched_args) ? args : &fetched_args,
196 memcpy(dev->config, config, sizeof dev->config);
198 *netdev_devp = &dev->netdev_dev;
201 shash_destroy(&fetched_args);
207 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
209 struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
211 route_table_unregister();
216 netdev_vport_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
217 struct netdev **netdevp)
219 struct netdev_vport *netdev;
221 netdev = xmalloc(sizeof *netdev);
222 netdev_init(&netdev->netdev, netdev_dev_);
224 *netdevp = &netdev->netdev;
229 netdev_vport_close(struct netdev *netdev_)
231 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
236 netdev_vport_set_config(struct netdev_dev *dev_, const struct shash *args)
238 const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
239 const struct vport_class *vport_class = vport_class_cast(netdev_class);
240 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
241 struct odp_port port;
244 memset(&port, 0, sizeof port);
245 strncpy(port.devname, netdev_dev_get_name(dev_), sizeof port.devname);
246 strncpy(port.type, netdev_dev_get_type(dev_), sizeof port.type);
247 error = vport_class->parse_config(netdev_dev_get_name(dev_),
248 netdev_dev_get_type(dev_),
250 if (!error && memcmp(port.config, dev->config, sizeof dev->config)) {
251 error = netdev_vport_do_ioctl(ODP_VPORT_MOD, &port);
252 if (!error || error == ENODEV) {
253 /* Either reconfiguration succeeded or this vport is not installed
254 * in the kernel (e.g. it hasn't been added to a dpif yet with
255 * dpif_port_add()). */
256 memcpy(dev->config, port.config, sizeof dev->config);
263 netdev_vport_set_etheraddr(struct netdev *netdev,
264 const uint8_t mac[ETH_ADDR_LEN])
266 struct odp_vport_ether vport_ether;
269 ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
270 sizeof vport_ether.devname);
272 memcpy(vport_ether.ether_addr, mac, ETH_ADDR_LEN);
274 err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_SET, &vport_ether);
279 netdev_vport_poll_notify(netdev);
284 netdev_vport_get_etheraddr(const struct netdev *netdev,
285 uint8_t mac[ETH_ADDR_LEN])
287 struct odp_vport_ether vport_ether;
290 ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
291 sizeof vport_ether.devname);
293 err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_GET, &vport_ether);
298 memcpy(mac, vport_ether.ether_addr, ETH_ADDR_LEN);
303 netdev_vport_get_mtu(const struct netdev *netdev, int *mtup)
305 struct odp_vport_mtu vport_mtu;
308 ovs_strlcpy(vport_mtu.devname, netdev_get_name(netdev),
309 sizeof vport_mtu.devname);
311 err = netdev_vport_do_ioctl(ODP_VPORT_MTU_GET, &vport_mtu);
316 *mtup = vport_mtu.mtu;
321 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
323 const char *name = netdev_get_name(netdev);
324 struct odp_vport_stats_req ovsr;
327 ovs_strlcpy(ovsr.devname, name, sizeof ovsr.devname);
328 err = netdev_vport_do_ioctl(ODP_VPORT_STATS_GET, &ovsr);
333 stats->rx_packets = ovsr.stats.rx_packets;
334 stats->tx_packets = ovsr.stats.tx_packets;
335 stats->rx_bytes = ovsr.stats.rx_bytes;
336 stats->tx_bytes = ovsr.stats.tx_bytes;
337 stats->rx_errors = ovsr.stats.rx_errors;
338 stats->tx_errors = ovsr.stats.tx_errors;
339 stats->rx_dropped = ovsr.stats.rx_dropped;
340 stats->tx_dropped = ovsr.stats.tx_dropped;
341 stats->multicast = ovsr.stats.multicast;
342 stats->collisions = ovsr.stats.collisions;
343 stats->rx_length_errors = ovsr.stats.rx_length_errors;
344 stats->rx_over_errors = ovsr.stats.rx_over_errors;
345 stats->rx_crc_errors = ovsr.stats.rx_crc_errors;
346 stats->rx_frame_errors = ovsr.stats.rx_frame_errors;
347 stats->rx_fifo_errors = ovsr.stats.rx_fifo_errors;
348 stats->rx_missed_errors = ovsr.stats.rx_missed_errors;
349 stats->tx_aborted_errors = ovsr.stats.tx_aborted_errors;
350 stats->tx_carrier_errors = ovsr.stats.tx_carrier_errors;
351 stats->tx_fifo_errors = ovsr.stats.tx_fifo_errors;
352 stats->tx_heartbeat_errors = ovsr.stats.tx_heartbeat_errors;
353 stats->tx_window_errors = ovsr.stats.tx_window_errors;
359 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
361 struct odp_vport_stats_req ovsr;
364 ovs_strlcpy(ovsr.devname, netdev_get_name(netdev), sizeof ovsr.devname);
366 ovsr.stats.rx_packets = stats->rx_packets;
367 ovsr.stats.tx_packets = stats->tx_packets;
368 ovsr.stats.rx_bytes = stats->rx_bytes;
369 ovsr.stats.tx_bytes = stats->tx_bytes;
370 ovsr.stats.rx_errors = stats->rx_errors;
371 ovsr.stats.tx_errors = stats->tx_errors;
372 ovsr.stats.rx_dropped = stats->rx_dropped;
373 ovsr.stats.tx_dropped = stats->tx_dropped;
374 ovsr.stats.multicast = stats->multicast;
375 ovsr.stats.collisions = stats->collisions;
376 ovsr.stats.rx_length_errors = stats->rx_length_errors;
377 ovsr.stats.rx_over_errors = stats->rx_over_errors;
378 ovsr.stats.rx_crc_errors = stats->rx_crc_errors;
379 ovsr.stats.rx_frame_errors = stats->rx_frame_errors;
380 ovsr.stats.rx_fifo_errors = stats->rx_fifo_errors;
381 ovsr.stats.rx_missed_errors = stats->rx_missed_errors;
382 ovsr.stats.tx_aborted_errors = stats->tx_aborted_errors;
383 ovsr.stats.tx_carrier_errors = stats->tx_carrier_errors;
384 ovsr.stats.tx_fifo_errors = stats->tx_fifo_errors;
385 ovsr.stats.tx_heartbeat_errors = stats->tx_heartbeat_errors;
386 ovsr.stats.tx_window_errors = stats->tx_window_errors;
388 err = netdev_vport_do_ioctl(ODP_VPORT_STATS_SET, &ovsr);
390 /* If the vport layer doesn't know about the device, that doesn't mean it
391 * doesn't exist (after all were able to open it when netdev_open() was
392 * called), it just means that it isn't attached and we'll be getting
393 * stats a different way. */
402 netdev_vport_get_status(const struct netdev *netdev, struct shash *sh)
404 const char *iface = netdev_vport_get_tnl_iface(netdev);
407 struct netdev *egress_netdev;
409 shash_add(sh, "tunnel_egress_iface", xstrdup(iface));
411 if (!netdev_open_default(iface, &egress_netdev)) {
412 shash_add(sh, "tunnel_egress_iface_carrier",
413 xstrdup(netdev_get_carrier(egress_netdev)
415 netdev_close(egress_netdev);
423 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
424 enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
425 enum netdev_flags *old_flagsp)
427 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
431 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
436 make_poll_name(const struct netdev *netdev)
438 return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
442 netdev_vport_poll_add(struct netdev *netdev,
443 void (*cb)(struct netdev_notifier *), void *aux,
444 struct netdev_notifier **notifierp)
446 char *poll_name = make_poll_name(netdev);
447 struct netdev_vport_notifier *notifier;
449 struct shash_node *shash_node;
451 shash_node = shash_find_data(&netdev_vport_notifiers, poll_name);
453 list = xmalloc(sizeof *list);
455 shash_node = shash_add(&netdev_vport_notifiers, poll_name, list);
457 list = shash_node->data;
460 notifier = xmalloc(sizeof *notifier);
461 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
462 list_push_back(list, ¬ifier->list_node);
463 notifier->shash_node = shash_node;
465 *notifierp = ¬ifier->notifier;
472 netdev_vport_poll_remove(struct netdev_notifier *notifier_)
474 struct netdev_vport_notifier *notifier =
475 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
479 list = list_remove(¬ifier->list_node);
480 if (list_is_empty(list)) {
481 shash_delete(&netdev_vport_notifiers, notifier->shash_node);
489 netdev_vport_run(void)
491 rtnetlink_link_notifier_run();
496 netdev_vport_wait(void)
498 rtnetlink_link_notifier_wait();
502 /* get_tnl_iface() implementation. */
504 static struct name_node *
505 name_node_lookup(int ifi_index)
507 struct name_node *nn;
509 HMAP_FOR_EACH_WITH_HASH(nn, node, hash_int(ifi_index, 0), &name_map) {
510 if (nn->ifi_index == ifi_index) {
518 /* Queries the kernel for fresh data to populate the name map with. */
520 netdev_vport_reset_names(void)
524 struct rtgenmsg *rtmsg;
525 struct ofpbuf request, reply;
526 static struct nl_sock *rtnl_sock;
527 struct name_node *nn, *nn_next;
529 HMAP_FOR_EACH_SAFE(nn, nn_next, node, &name_map) {
530 hmap_remove(&name_map, &nn->node);
534 error = nl_sock_create(NETLINK_ROUTE, &rtnl_sock);
536 VLOG_WARN_RL(&rl, "Failed to create NETLINK_ROUTE socket");
540 ofpbuf_init(&request, 0);
542 nl_msg_put_nlmsghdr(&request, sizeof *rtmsg, RTM_GETLINK, NLM_F_REQUEST);
544 rtmsg = ofpbuf_put_zeros(&request, sizeof *rtmsg);
545 rtmsg->rtgen_family = AF_INET;
547 nl_dump_start(&dump, rtnl_sock, &request);
549 while (nl_dump_next(&dump, &reply)) {
550 struct rtnetlink_link_change change;
552 if (rtnetlink_link_parse(&reply, &change)) {
553 netdev_vport_link_change(&change, NULL);
556 nl_sock_destroy(rtnl_sock);
558 return nl_dump_done(&dump);
562 netdev_vport_link_change(const struct rtnetlink_link_change *change,
563 void *aux OVS_UNUSED)
567 netdev_vport_reset_names();
568 } else if (change->nlmsg_type == RTM_NEWLINK) {
569 struct name_node *nn;
571 if (name_node_lookup(change->ifi_index)) {
575 nn = xzalloc(sizeof *nn);
576 nn->ifi_index = change->ifi_index;
578 strncpy(nn->ifname, change->ifname, IFNAMSIZ);
579 nn->ifname[IFNAMSIZ - 1] = '\0';
581 hmap_insert(&name_map, &nn->node, hash_int(nn->ifi_index, 0));
582 } else if (change->nlmsg_type == RTM_DELLINK) {
583 struct name_node *nn;
585 nn = name_node_lookup(change->ifi_index);
588 hmap_remove(&name_map, &nn->node);
593 VLOG_WARN_RL(&rl, "Received unexpected rtnetlink message type %d",
599 netdev_vport_tnl_iface_init(void)
601 static bool tnl_iface_is_init = false;
603 if (!tnl_iface_is_init) {
604 hmap_init(&name_map);
606 rtnetlink_link_notifier_register(&netdev_vport_link_notifier,
607 netdev_vport_link_change, NULL);
609 netdev_vport_reset_names();
610 tnl_iface_is_init = true;
615 netdev_vport_get_tnl_iface(const struct netdev *netdev)
619 struct netdev_dev_vport *ndv;
620 struct tnl_port_config *config;
622 ndv = netdev_dev_vport_cast(netdev_get_dev(netdev));
623 config = (struct tnl_port_config *) ndv->config;
624 route = config->daddr;
626 if (route_table_get_ifindex(route, &ifindex)) {
627 struct name_node *nn;
628 HMAP_FOR_EACH_WITH_HASH(nn, node, hash_int(ifindex, 0), &name_map) {
629 if (nn->ifi_index == ifindex) {
638 /* Helper functions. */
641 netdev_vport_do_ioctl(int cmd, void *arg)
643 static int ioctl_fd = -1;
646 ioctl_fd = open("/dev/net/dp0", O_RDONLY | O_NONBLOCK);
648 VLOG_ERR_RL(&rl, "failed to open ioctl fd: %s", strerror(errno));
653 return ioctl(ioctl_fd, cmd, arg) ? errno : 0;
657 netdev_vport_poll_notify(const struct netdev *netdev)
659 char *poll_name = make_poll_name(netdev);
660 struct list *list = shash_find_data(&netdev_vport_notifiers,
664 struct netdev_vport_notifier *notifier;
666 LIST_FOR_EACH (notifier, list_node, list) {
667 struct netdev_notifier *n = ¬ifier->notifier;
675 /* Code specific to individual vport types. */
678 parse_tunnel_config(const char *name, const char *type,
679 const struct shash *args, void *configp)
682 bool is_ipsec = false;
683 struct tnl_port_config config;
684 struct shash_node *node;
685 bool ipsec_mech_set = false;
687 memset(&config, 0, sizeof config);
688 config.flags |= TNL_F_PMTUD;
689 config.flags |= TNL_F_HDR_CACHE;
691 if (!strcmp(type, "gre")) {
693 } else if (!strcmp(type, "ipsec_gre")) {
697 config.flags |= TNL_F_IPSEC;
699 /* IPsec doesn't work when header caching is enabled. */
700 config.flags &= ~TNL_F_HDR_CACHE;
703 SHASH_FOR_EACH (node, args) {
704 if (!strcmp(node->name, "remote_ip")) {
705 struct in_addr in_addr;
706 if (lookup_ip(node->data, &in_addr)) {
707 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
709 config.daddr = in_addr.s_addr;
711 } else if (!strcmp(node->name, "local_ip")) {
712 struct in_addr in_addr;
713 if (lookup_ip(node->data, &in_addr)) {
714 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
716 config.saddr = in_addr.s_addr;
718 } else if (!strcmp(node->name, "key") && is_gre) {
719 if (!strcmp(node->data, "flow")) {
720 config.flags |= TNL_F_IN_KEY_MATCH;
721 config.flags |= TNL_F_OUT_KEY_ACTION;
723 uint64_t key = strtoull(node->data, NULL, 0);
724 config.out_key = config.in_key = htonll(key);
726 } else if (!strcmp(node->name, "in_key") && is_gre) {
727 if (!strcmp(node->data, "flow")) {
728 config.flags |= TNL_F_IN_KEY_MATCH;
730 config.in_key = htonll(strtoull(node->data, NULL, 0));
732 } else if (!strcmp(node->name, "out_key") && is_gre) {
733 if (!strcmp(node->data, "flow")) {
734 config.flags |= TNL_F_OUT_KEY_ACTION;
736 config.out_key = htonll(strtoull(node->data, NULL, 0));
738 } else if (!strcmp(node->name, "tos")) {
739 if (!strcmp(node->data, "inherit")) {
740 config.flags |= TNL_F_TOS_INHERIT;
742 config.tos = atoi(node->data);
744 } else if (!strcmp(node->name, "ttl")) {
745 if (!strcmp(node->data, "inherit")) {
746 config.flags |= TNL_F_TTL_INHERIT;
748 config.ttl = atoi(node->data);
750 } else if (!strcmp(node->name, "csum") && is_gre) {
751 if (!strcmp(node->data, "true")) {
752 config.flags |= TNL_F_CSUM;
754 } else if (!strcmp(node->name, "pmtud")) {
755 if (!strcmp(node->data, "false")) {
756 config.flags &= ~TNL_F_PMTUD;
758 } else if (!strcmp(node->name, "header_cache")) {
759 if (!strcmp(node->data, "false")) {
760 config.flags &= ~TNL_F_HDR_CACHE;
762 } else if (!strcmp(node->name, "peer_cert") && is_ipsec) {
763 if (shash_find(args, "certificate")) {
764 ipsec_mech_set = true;
766 const char *use_ssl_cert;
768 /* If the "use_ssl_cert" is true, then "certificate" and
769 * "private_key" will be pulled from the SSL table. The
770 * use of this option is strongly discouraged, since it
771 * will like be removed when multiple SSL configurations
772 * are supported by OVS.
774 use_ssl_cert = shash_find_data(args, "use_ssl_cert");
775 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
776 VLOG_WARN("%s: 'peer_cert' requires 'certificate' argument",
780 ipsec_mech_set = true;
782 } else if (!strcmp(node->name, "psk") && is_ipsec) {
783 ipsec_mech_set = true;
785 && (!strcmp(node->name, "certificate")
786 || !strcmp(node->name, "private_key")
787 || !strcmp(node->name, "use_ssl_cert"))) {
788 /* Ignore options not used by the netdev. */
790 VLOG_WARN("%s: unknown %s argument '%s'",
791 name, type, node->name);
796 if (shash_find(args, "peer_cert") && shash_find(args, "psk")) {
797 VLOG_WARN("%s: cannot define both 'peer_cert' and 'psk'", name);
801 if (!ipsec_mech_set) {
802 VLOG_WARN("%s: IPsec requires an 'peer_cert' or psk' argument",
809 VLOG_WARN("%s: %s type requires valid 'remote_ip' argument",
814 BUILD_ASSERT(sizeof config <= VPORT_CONFIG_SIZE);
815 memcpy(configp, &config, sizeof config);
820 unparse_tunnel_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
821 const void *config_, struct shash *args)
823 const struct tnl_port_config *config = config_;
825 if (!(config->flags & TNL_F_HDR_CACHE) == !(config->flags & TNL_F_IPSEC)) {
826 smap_add(args, "header_cache",
827 config->flags & TNL_F_HDR_CACHE ? "true" : "false");
829 shash_add(args, "remote_ip", xasprintf(IP_FMT, IP_ARGS(&config->daddr)));
831 shash_add(args, "local_ip",
832 xasprintf(IP_FMT, IP_ARGS(&config->saddr)));
835 if ((config->flags & (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION))
836 == (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION)) {
837 smap_add(args, "key", "flow");
838 } else if (config->in_key && config->in_key == config->out_key) {
839 shash_add(args, "key",
840 xasprintf("%"PRIu64, ntohll(config->in_key)));
842 if (config->flags & TNL_F_IN_KEY_MATCH) {
843 smap_add(args, "in_key", "flow");
844 } else if (config->in_key) {
845 shash_add(args, "in_key",
846 xasprintf("%"PRIu64, ntohll(config->in_key)));
849 if (config->flags & TNL_F_OUT_KEY_ACTION) {
850 smap_add(args, "out_key", "flow");
851 } else if (config->out_key) {
852 shash_add(args, "out_key",
853 xasprintf("%"PRIu64, ntohll(config->out_key)));
857 if (config->flags & TNL_F_TTL_INHERIT) {
858 smap_add(args, "tos", "inherit");
859 } else if (config->ttl) {
860 shash_add(args, "tos", xasprintf("%"PRIu8, config->ttl));
863 if (config->flags & TNL_F_CSUM) {
864 smap_add(args, "csum", "true");
866 if (!(config->flags & TNL_F_PMTUD)) {
867 smap_add(args, "pmtud", "false");
874 parse_patch_config(const char *name, const char *type OVS_UNUSED,
875 const struct shash *args, void *configp)
879 peer = shash_find_data(args, "peer");
881 VLOG_WARN("%s: patch type requires valid 'peer' argument", name);
885 if (shash_count(args) > 1) {
886 VLOG_WARN("%s: patch type takes only a 'peer' argument", name);
890 if (strlen(peer) >= MIN(IFNAMSIZ, VPORT_CONFIG_SIZE)) {
891 VLOG_WARN("%s: patch 'peer' arg too long", name);
895 if (!strcmp(name, peer)) {
896 VLOG_WARN("%s: patch peer must not be self", name);
900 strncpy(configp, peer, VPORT_CONFIG_SIZE);
906 unparse_patch_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
907 const void *config_, struct shash *args)
911 ovs_strlcpy(peer, config_, MIN(sizeof peer, VPORT_CONFIG_SIZE));
913 smap_add(args, "peer", peer);
919 #define VPORT_FUNCTIONS(GET_STATUS) \
924 netdev_vport_create, \
925 netdev_vport_destroy, \
926 netdev_vport_set_config, \
929 netdev_vport_close, \
931 NULL, /* enumerate */ \
934 NULL, /* recv_wait */ \
938 NULL, /* send_wait */ \
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, \
949 NULL, /* get_features */ \
950 NULL, /* set_advertisements */ \
951 NULL, /* get_vlan_vid */ \
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 */ \
965 NULL, /* get_in4 */ \
966 NULL, /* set_in4 */ \
967 NULL, /* get_in6 */ \
968 NULL, /* add_router */ \
969 NULL, /* get_next_hop */ \
971 NULL, /* arp_lookup */ \
973 netdev_vport_update_flags, \
975 netdev_vport_poll_add, \
976 netdev_vport_poll_remove,
979 netdev_vport_register(void)
981 static const struct vport_class vport_classes[] = {
982 { { "gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
983 parse_tunnel_config, unparse_tunnel_config },
984 { { "ipsec_gre", VPORT_FUNCTIONS(netdev_vport_get_status) },
985 parse_tunnel_config, unparse_tunnel_config },
986 { { "capwap", VPORT_FUNCTIONS(netdev_vport_get_status) },
987 parse_tunnel_config, unparse_tunnel_config },
988 { { "patch", VPORT_FUNCTIONS(NULL) },
989 parse_patch_config, unparse_patch_config }
994 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
995 netdev_register_provider(&vport_classes[i].netdev_class);