2 * Copyright (c) 2010, 2011, 2012 Nicira, Inc.
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/openvswitch.h>
25 #include <linux/rtnetlink.h>
27 #include <sys/ioctl.h>
29 #include "byte-order.h"
32 #include "dpif-linux.h"
36 #include "netdev-linux.h"
37 #include "netdev-provider.h"
39 #include "netlink-notifier.h"
40 #include "netlink-socket.h"
42 #include "openvswitch/tunnel.h"
44 #include "route-table.h"
46 #include "socket-util.h"
47 #include "unaligned.h"
50 VLOG_DEFINE_THIS_MODULE(netdev_vport);
52 /* Default to the OTV port, per the VXLAN IETF draft. */
53 #define VXLAN_DST_PORT 8472
55 struct netdev_dev_vport {
56 struct netdev_dev netdev_dev;
57 struct ofpbuf *options;
58 int dp_ifindex; /* -1 if unknown. */
59 uint32_t port_no; /* UINT32_MAX if unknown. */
60 unsigned int change_seq;
68 enum ovs_vport_type type;
69 struct netdev_class netdev_class;
70 int (*parse_config)(const char *name, const char *type,
71 const struct smap *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 vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
79 static int netdev_vport_create(const struct netdev_class *, const char *,
80 struct netdev_dev **);
81 static void netdev_vport_poll_notify(const struct netdev *);
82 static int tnl_port_config_from_nlattr(const struct nlattr *options,
84 struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1]);
86 static const char *netdev_vport_get_tnl_iface(const struct netdev *netdev);
89 is_vport_class(const struct netdev_class *class)
91 return class->create == netdev_vport_create;
94 static const struct vport_class *
95 vport_class_cast(const struct netdev_class *class)
97 assert(is_vport_class(class));
98 return CONTAINER_OF(class, struct vport_class, netdev_class);
101 static struct netdev_dev_vport *
102 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
104 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
105 return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
108 static struct netdev_vport *
109 netdev_vport_cast(const struct netdev *netdev)
111 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
112 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
113 return CONTAINER_OF(netdev, struct netdev_vport, netdev);
116 /* If 'netdev' is a vport netdev, returns an ofpbuf that contains Netlink
117 * options to include in OVS_VPORT_ATTR_OPTIONS for configuring that vport.
118 * Otherwise returns NULL. */
119 const struct ofpbuf *
120 netdev_vport_get_options(const struct netdev *netdev)
122 const struct netdev_dev *dev = netdev_get_dev(netdev);
124 return (is_vport_class(netdev_dev_get_class(dev))
125 ? netdev_dev_vport_cast(dev)->options
130 netdev_vport_get_vport_type(const struct netdev *netdev)
132 const struct netdev_dev *dev = netdev_get_dev(netdev);
133 const struct netdev_class *class = netdev_dev_get_class(dev);
135 return (is_vport_class(class) ? vport_class_cast(class)->type
136 : class == &netdev_internal_class ? OVS_VPORT_TYPE_INTERNAL
137 : (class == &netdev_linux_class ||
138 class == &netdev_tap_class) ? OVS_VPORT_TYPE_NETDEV
139 : OVS_VPORT_TYPE_UNSPEC);
143 netdev_vport_get_netdev_type(const struct dpif_linux_vport *vport)
145 struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
147 switch (vport->type) {
148 case OVS_VPORT_TYPE_UNSPEC:
151 case OVS_VPORT_TYPE_NETDEV:
154 case OVS_VPORT_TYPE_INTERNAL:
157 case OVS_VPORT_TYPE_PATCH:
160 case OVS_VPORT_TYPE_GRE:
161 if (tnl_port_config_from_nlattr(vport->options, vport->options_len,
165 return (nl_attr_get_u32(a[OVS_TUNNEL_ATTR_FLAGS]) & TNL_F_IPSEC
166 ? "ipsec_gre" : "gre");
168 case OVS_VPORT_TYPE_GRE64:
169 if (tnl_port_config_from_nlattr(vport->options, vport->options_len,
173 return (nl_attr_get_u32(a[OVS_TUNNEL_ATTR_FLAGS]) & TNL_F_IPSEC
174 ? "ipsec_gre64" : "gre64");
176 case OVS_VPORT_TYPE_CAPWAP:
179 case OVS_VPORT_TYPE_VXLAN:
182 case OVS_VPORT_TYPE_FT_GRE:
183 case __OVS_VPORT_TYPE_MAX:
187 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
188 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
193 netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
194 struct netdev_dev **netdev_devp)
196 struct netdev_dev_vport *dev;
198 dev = xmalloc(sizeof *dev);
199 netdev_dev_init(&dev->netdev_dev, name, netdev_class);
201 dev->dp_ifindex = -1;
202 dev->port_no = UINT32_MAX;
205 *netdev_devp = &dev->netdev_dev;
206 route_table_register();
212 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
214 struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
216 ofpbuf_delete(netdev_dev->options);
217 route_table_unregister();
222 netdev_vport_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
224 struct netdev_vport *netdev;
226 netdev = xmalloc(sizeof *netdev);
227 netdev_init(&netdev->netdev, netdev_dev_);
229 *netdevp = &netdev->netdev;
234 netdev_vport_close(struct netdev *netdev_)
236 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
241 netdev_vport_get_config(struct netdev_dev *dev_, struct smap *args)
243 const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
244 const struct vport_class *vport_class = vport_class_cast(netdev_class);
245 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
246 const char *name = netdev_dev_get_name(dev_);
250 struct dpif_linux_vport reply;
253 error = dpif_linux_vport_get(name, &reply, &buf);
255 VLOG_ERR_RL(&rl, "%s: vport query failed (%s)",
256 name, strerror(error));
260 dev->options = ofpbuf_clone_data(reply.options, reply.options_len);
261 dev->dp_ifindex = reply.dp_ifindex;
262 dev->port_no = reply.port_no;
266 error = vport_class->unparse_config(name, netdev_class->type,
271 VLOG_ERR_RL(&rl, "%s: failed to parse kernel config (%s)",
272 name, strerror(error));
278 netdev_vport_set_config(struct netdev_dev *dev_, const struct smap *args)
280 const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
281 const struct vport_class *vport_class = vport_class_cast(netdev_class);
282 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
283 const char *name = netdev_dev_get_name(dev_);
284 struct ofpbuf *options;
287 options = ofpbuf_new(64);
288 error = vport_class->parse_config(name, netdev_dev_get_type(dev_),
292 || options->size != dev->options->size
293 || memcmp(options->data, dev->options->data, options->size))) {
294 struct dpif_linux_vport vport;
296 dpif_linux_vport_init(&vport);
297 vport.cmd = OVS_VPORT_CMD_SET;
299 vport.options = options->data;
300 vport.options_len = options->size;
301 error = dpif_linux_vport_transact(&vport, NULL, NULL);
302 if (!error || error == ENODEV) {
303 /* Either reconfiguration succeeded or this vport is not installed
304 * in the kernel (e.g. it hasn't been added to a dpif yet with
305 * dpif_port_add()). */
306 ofpbuf_delete(dev->options);
307 dev->options = options;
312 ofpbuf_delete(options);
318 netdev_vport_send(struct netdev *netdev, const void *data, size_t size)
320 struct netdev_dev *dev_ = netdev_get_dev(netdev);
321 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
323 if (dev->dp_ifindex == -1) {
324 const char *name = netdev_get_name(netdev);
325 struct dpif_linux_vport reply;
329 error = dpif_linux_vport_get(name, &reply, &buf);
331 VLOG_ERR_RL(&rl, "%s: failed to query vport for send (%s)",
332 name, strerror(error));
335 dev->dp_ifindex = reply.dp_ifindex;
336 dev->port_no = reply.port_no;
340 return dpif_linux_vport_send(dev->dp_ifindex, dev->port_no, data, size);
344 netdev_vport_set_etheraddr(struct netdev *netdev,
345 const uint8_t mac[ETH_ADDR_LEN])
347 struct dpif_linux_vport vport;
350 dpif_linux_vport_init(&vport);
351 vport.cmd = OVS_VPORT_CMD_SET;
352 vport.name = netdev_get_name(netdev);
355 error = dpif_linux_vport_transact(&vport, NULL, NULL);
357 netdev_vport_poll_notify(netdev);
363 netdev_vport_get_etheraddr(const struct netdev *netdev,
364 uint8_t mac[ETH_ADDR_LEN])
366 struct dpif_linux_vport reply;
370 error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
373 memcpy(mac, reply.address, ETH_ADDR_LEN);
382 /* Copies 'src' into 'dst', performing format conversion in the process.
384 * 'src' is allowed to be misaligned. */
386 netdev_stats_from_ovs_vport_stats(struct netdev_stats *dst,
387 const struct ovs_vport_stats *src)
389 dst->rx_packets = get_unaligned_u64(&src->rx_packets);
390 dst->tx_packets = get_unaligned_u64(&src->tx_packets);
391 dst->rx_bytes = get_unaligned_u64(&src->rx_bytes);
392 dst->tx_bytes = get_unaligned_u64(&src->tx_bytes);
393 dst->rx_errors = get_unaligned_u64(&src->rx_errors);
394 dst->tx_errors = get_unaligned_u64(&src->tx_errors);
395 dst->rx_dropped = get_unaligned_u64(&src->rx_dropped);
396 dst->tx_dropped = get_unaligned_u64(&src->tx_dropped);
399 dst->rx_length_errors = 0;
400 dst->rx_over_errors = 0;
401 dst->rx_crc_errors = 0;
402 dst->rx_frame_errors = 0;
403 dst->rx_fifo_errors = 0;
404 dst->rx_missed_errors = 0;
405 dst->tx_aborted_errors = 0;
406 dst->tx_carrier_errors = 0;
407 dst->tx_fifo_errors = 0;
408 dst->tx_heartbeat_errors = 0;
409 dst->tx_window_errors = 0;
412 /* Copies 'src' into 'dst', performing format conversion in the process. */
414 netdev_stats_to_ovs_vport_stats(struct ovs_vport_stats *dst,
415 const struct netdev_stats *src)
417 dst->rx_packets = src->rx_packets;
418 dst->tx_packets = src->tx_packets;
419 dst->rx_bytes = src->rx_bytes;
420 dst->tx_bytes = src->tx_bytes;
421 dst->rx_errors = src->rx_errors;
422 dst->tx_errors = src->tx_errors;
423 dst->rx_dropped = src->rx_dropped;
424 dst->tx_dropped = src->tx_dropped;
428 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
430 struct dpif_linux_vport reply;
434 error = dpif_linux_vport_get(netdev_get_name(netdev), &reply, &buf);
437 } else if (!reply.stats) {
442 netdev_stats_from_ovs_vport_stats(stats, reply.stats);
450 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
452 struct ovs_vport_stats rtnl_stats;
453 struct dpif_linux_vport vport;
456 netdev_stats_to_ovs_vport_stats(&rtnl_stats, stats);
458 dpif_linux_vport_init(&vport);
459 vport.cmd = OVS_VPORT_CMD_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_drv_info(const struct netdev *netdev, struct smap *smap)
479 const char *iface = netdev_vport_get_tnl_iface(netdev);
482 struct netdev *egress_netdev;
484 smap_add(smap, "tunnel_egress_iface", iface);
486 if (!netdev_open(iface, "system", &egress_netdev)) {
487 smap_add(smap, "tunnel_egress_iface_carrier",
488 netdev_get_carrier(egress_netdev) ? "up" : "down");
489 netdev_close(egress_netdev);
497 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
498 enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
499 enum netdev_flags *old_flagsp)
501 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
505 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
510 netdev_vport_change_seq(const struct netdev *netdev)
512 return netdev_dev_vport_cast(netdev_get_dev(netdev))->change_seq;
516 netdev_vport_run(void)
522 netdev_vport_wait(void)
527 /* get_tnl_iface() implementation. */
529 netdev_vport_get_tnl_iface(const struct netdev *netdev)
531 struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
533 struct netdev_dev_vport *ndv;
534 static char name[IFNAMSIZ];
536 ndv = netdev_dev_vport_cast(netdev_get_dev(netdev));
537 if (tnl_port_config_from_nlattr(ndv->options->data, ndv->options->size,
541 route = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
543 if (route_table_get_name(route, name)) {
550 /* Helper functions. */
553 netdev_vport_poll_notify(const struct netdev *netdev)
555 struct netdev_dev_vport *ndv;
557 ndv = netdev_dev_vport_cast(netdev_get_dev(netdev));
560 if (!ndv->change_seq) {
565 /* Code specific to individual vport types. */
568 set_key(const struct smap *args, const char *name, uint16_t type,
569 struct ofpbuf *options)
573 s = smap_get(args, name);
575 s = smap_get(args, "key");
581 if (!strcmp(s, "flow")) {
582 /* This is the default if no attribute is present. */
584 nl_msg_put_be64(options, type, htonll(strtoull(s, NULL, 0)));
589 parse_tunnel_config(const char *name, const char *type,
590 const struct smap *args, struct ofpbuf *options)
593 bool is_ipsec = false;
594 bool needs_dst_port = false;
595 bool found_dst_port = false;
596 struct smap_node *node;
597 bool ipsec_mech_set = false;
598 ovs_be32 daddr = htonl(0);
599 ovs_be32 saddr = htonl(0);
602 if (!strcmp(type, "capwap")) {
603 VLOG_WARN_ONCE("CAPWAP tunnel support is deprecated.");
606 flags = TNL_F_DF_DEFAULT;
607 if (!strcmp(type, "gre") || !strcmp(type, "gre64")) {
609 } else if (!strcmp(type, "ipsec_gre") || !strcmp(type, "ipsec_gre64")) {
612 flags |= TNL_F_IPSEC;
613 } else if (!strcmp(type, "vxlan")) {
614 needs_dst_port = true;
617 SMAP_FOR_EACH (node, args) {
618 if (!strcmp(node->key, "remote_ip")) {
619 struct in_addr in_addr;
620 if (lookup_ip(node->value, &in_addr)) {
621 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
623 daddr = in_addr.s_addr;
625 } else if (!strcmp(node->key, "local_ip")) {
626 struct in_addr in_addr;
627 if (lookup_ip(node->value, &in_addr)) {
628 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
630 saddr = in_addr.s_addr;
632 } else if (!strcmp(node->key, "tos")) {
633 if (!strcmp(node->value, "inherit")) {
634 flags |= TNL_F_TOS_INHERIT;
638 tos = strtol(node->value, &endptr, 0);
639 if (*endptr == '\0' && tos == (tos & IP_DSCP_MASK)) {
640 nl_msg_put_u8(options, OVS_TUNNEL_ATTR_TOS, tos);
642 VLOG_WARN("%s: invalid TOS %s", name, node->value);
645 } else if (!strcmp(node->key, "ttl")) {
646 if (!strcmp(node->value, "inherit")) {
647 flags |= TNL_F_TTL_INHERIT;
649 nl_msg_put_u8(options, OVS_TUNNEL_ATTR_TTL, atoi(node->value));
651 } else if (!strcmp(node->key, "dst_port") && needs_dst_port) {
652 nl_msg_put_u16(options, OVS_TUNNEL_ATTR_DST_PORT,
654 found_dst_port = true;
655 } else if (!strcmp(node->key, "csum") && is_gre) {
656 if (!strcmp(node->value, "true")) {
659 } else if (!strcmp(node->key, "df_inherit")) {
660 if (!strcmp(node->value, "true")) {
661 flags |= TNL_F_DF_INHERIT;
663 } else if (!strcmp(node->key, "df_default")) {
664 if (!strcmp(node->value, "false")) {
665 flags &= ~TNL_F_DF_DEFAULT;
667 } else if (!strcmp(node->key, "pmtud")) {
668 if (!strcmp(node->value, "true")) {
669 VLOG_WARN_ONCE("%s: The tunnel Path MTU discovery is "
670 "deprecated and may be removed in February "
671 "2013. Please email dev@openvswitch.org with "
673 flags |= TNL_F_PMTUD;
675 } else if (!strcmp(node->key, "peer_cert") && is_ipsec) {
676 if (smap_get(args, "certificate")) {
677 ipsec_mech_set = true;
679 const char *use_ssl_cert;
681 /* If the "use_ssl_cert" is true, then "certificate" and
682 * "private_key" will be pulled from the SSL table. The
683 * use of this option is strongly discouraged, since it
684 * will like be removed when multiple SSL configurations
685 * are supported by OVS.
687 use_ssl_cert = smap_get(args, "use_ssl_cert");
688 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
689 VLOG_ERR("%s: 'peer_cert' requires 'certificate' argument",
693 ipsec_mech_set = true;
695 } else if (!strcmp(node->key, "psk") && is_ipsec) {
696 ipsec_mech_set = true;
698 && (!strcmp(node->key, "certificate")
699 || !strcmp(node->key, "private_key")
700 || !strcmp(node->key, "use_ssl_cert"))) {
701 /* Ignore options not used by the netdev. */
702 } else if (!strcmp(node->key, "key") ||
703 !strcmp(node->key, "in_key") ||
704 !strcmp(node->key, "out_key")) {
705 /* Handled separately below. */
707 VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->key);
711 /* Add a default destination port for VXLAN if none specified. */
712 if (needs_dst_port && !found_dst_port) {
713 nl_msg_put_u16(options, OVS_TUNNEL_ATTR_DST_PORT, VXLAN_DST_PORT);
717 static pid_t pid = 0;
719 char *file_name = xasprintf("%s/%s", ovs_rundir(),
720 "ovs-monitor-ipsec.pid");
721 pid = read_pidfile(file_name);
726 VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
731 if (smap_get(args, "peer_cert") && smap_get(args, "psk")) {
732 VLOG_ERR("%s: cannot define both 'peer_cert' and 'psk'", name);
736 if (!ipsec_mech_set) {
737 VLOG_ERR("%s: IPsec requires an 'peer_cert' or psk' argument",
743 set_key(args, "in_key", OVS_TUNNEL_ATTR_IN_KEY, options);
744 set_key(args, "out_key", OVS_TUNNEL_ATTR_OUT_KEY, options);
747 VLOG_ERR("%s: %s type requires valid 'remote_ip' argument",
751 nl_msg_put_be32(options, OVS_TUNNEL_ATTR_DST_IPV4, daddr);
754 if (ip_is_multicast(daddr)) {
755 VLOG_WARN("%s: remote_ip is multicast, ignoring local_ip", name);
757 nl_msg_put_be32(options, OVS_TUNNEL_ATTR_SRC_IPV4, saddr);
761 nl_msg_put_u32(options, OVS_TUNNEL_ATTR_FLAGS, flags);
767 tnl_port_config_from_nlattr(const struct nlattr *options, size_t options_len,
768 struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1])
770 static const struct nl_policy ovs_tunnel_policy[] = {
771 [OVS_TUNNEL_ATTR_FLAGS] = { .type = NL_A_U32 },
772 [OVS_TUNNEL_ATTR_DST_IPV4] = { .type = NL_A_BE32 },
773 [OVS_TUNNEL_ATTR_SRC_IPV4] = { .type = NL_A_BE32, .optional = true },
774 [OVS_TUNNEL_ATTR_IN_KEY] = { .type = NL_A_BE64, .optional = true },
775 [OVS_TUNNEL_ATTR_OUT_KEY] = { .type = NL_A_BE64, .optional = true },
776 [OVS_TUNNEL_ATTR_TOS] = { .type = NL_A_U8, .optional = true },
777 [OVS_TUNNEL_ATTR_TTL] = { .type = NL_A_U8, .optional = true },
778 [OVS_TUNNEL_ATTR_DST_PORT] = { .type = NL_A_U16, .optional = true },
782 ofpbuf_use_const(&buf, options, options_len);
783 if (!nl_policy_parse(&buf, 0, ovs_tunnel_policy,
784 a, ARRAY_SIZE(ovs_tunnel_policy))) {
791 get_be64_or_zero(const struct nlattr *a)
793 return a ? ntohll(nl_attr_get_be64(a)) : 0;
797 unparse_tunnel_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
798 const struct nlattr *options, size_t options_len,
801 struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
806 error = tnl_port_config_from_nlattr(options, options_len, a);
812 daddr = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
813 smap_add_format(args, "remote_ip", IP_FMT, IP_ARGS(daddr));
815 if (a[OVS_TUNNEL_ATTR_SRC_IPV4]) {
816 ovs_be32 saddr = nl_attr_get_be32(a[OVS_TUNNEL_ATTR_SRC_IPV4]);
817 smap_add_format(args, "local_ip", IP_FMT, IP_ARGS(saddr));
820 if (!a[OVS_TUNNEL_ATTR_IN_KEY] && !a[OVS_TUNNEL_ATTR_OUT_KEY]) {
821 smap_add(args, "key", "flow");
823 uint64_t in_key = get_be64_or_zero(a[OVS_TUNNEL_ATTR_IN_KEY]);
824 uint64_t out_key = get_be64_or_zero(a[OVS_TUNNEL_ATTR_OUT_KEY]);
826 if (in_key && in_key == out_key) {
827 smap_add_format(args, "key", "%"PRIu64, in_key);
829 if (!a[OVS_TUNNEL_ATTR_IN_KEY]) {
830 smap_add(args, "in_key", "flow");
832 smap_add_format(args, "in_key", "%"PRIu64, in_key);
835 if (!a[OVS_TUNNEL_ATTR_OUT_KEY]) {
836 smap_add(args, "out_key", "flow");
837 } else if (out_key) {
838 smap_add_format(args, "out_key", "%"PRIu64, out_key);
843 flags = nl_attr_get_u32(a[OVS_TUNNEL_ATTR_FLAGS]);
844 if (flags & TNL_F_TTL_INHERIT) {
845 smap_add(args, "ttl", "inherit");
846 } else if (a[OVS_TUNNEL_ATTR_TTL]) {
847 int ttl = nl_attr_get_u8(a[OVS_TUNNEL_ATTR_TTL]);
848 smap_add_format(args, "ttl", "%d", ttl);
851 if (flags & TNL_F_TOS_INHERIT) {
852 smap_add(args, "tos", "inherit");
853 } else if (a[OVS_TUNNEL_ATTR_TOS]) {
854 int tos = nl_attr_get_u8(a[OVS_TUNNEL_ATTR_TOS]);
855 smap_add_format(args, "tos", "0x%x", tos);
858 if (a[OVS_TUNNEL_ATTR_DST_PORT]) {
859 uint16_t dst_port = nl_attr_get_u16(a[OVS_TUNNEL_ATTR_DST_PORT]);
860 if (dst_port != VXLAN_DST_PORT) {
861 smap_add_format(args, "dst_port", "%d", dst_port);
865 if (flags & TNL_F_CSUM) {
866 smap_add(args, "csum", "true");
868 if (flags & TNL_F_DF_INHERIT) {
869 smap_add(args, "df_inherit", "true");
871 if (!(flags & TNL_F_DF_DEFAULT)) {
872 smap_add(args, "df_default", "false");
874 if (flags & TNL_F_PMTUD) {
875 smap_add(args, "pmtud", "true");
882 parse_patch_config(const char *name, const char *type OVS_UNUSED,
883 const struct smap *args, struct ofpbuf *options)
887 peer = smap_get(args, "peer");
889 VLOG_ERR("%s: patch type requires valid 'peer' argument", name);
893 if (smap_count(args) > 1) {
894 VLOG_ERR("%s: patch type takes only a 'peer' argument", name);
898 if (strlen(peer) >= IFNAMSIZ) {
899 VLOG_ERR("%s: patch 'peer' arg too long", name);
903 if (!strcmp(name, peer)) {
904 VLOG_ERR("%s: patch peer must not be self", name);
908 nl_msg_put_string(options, OVS_PATCH_ATTR_PEER, peer);
914 unparse_patch_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
915 const struct nlattr *options, size_t options_len,
918 static const struct nl_policy ovs_patch_policy[] = {
919 [OVS_PATCH_ATTR_PEER] = { .type = NL_A_STRING,
924 struct nlattr *a[ARRAY_SIZE(ovs_patch_policy)];
927 ofpbuf_use_const(&buf, options, options_len);
928 if (!nl_policy_parse(&buf, 0, ovs_patch_policy,
929 a, ARRAY_SIZE(ovs_patch_policy))) {
933 smap_add(args, "peer", nl_attr_get_string(a[OVS_PATCH_ATTR_PEER]));
937 #define VPORT_FUNCTIONS(GET_STATUS) \
942 netdev_vport_create, \
943 netdev_vport_destroy, \
944 netdev_vport_get_config, \
945 netdev_vport_set_config, \
948 netdev_vport_close, \
952 NULL, /* recv_wait */ \
955 netdev_vport_send, /* send */ \
956 NULL, /* send_wait */ \
958 netdev_vport_set_etheraddr, \
959 netdev_vport_get_etheraddr, \
960 NULL, /* get_mtu */ \
961 NULL, /* set_mtu */ \
962 NULL, /* get_ifindex */ \
963 NULL, /* get_carrier */ \
964 NULL, /* get_carrier_resets */ \
965 NULL, /* get_miimon */ \
966 netdev_vport_get_stats, \
967 netdev_vport_set_stats, \
969 NULL, /* get_features */ \
970 NULL, /* set_advertisements */ \
972 NULL, /* set_policing */ \
973 NULL, /* get_qos_types */ \
974 NULL, /* get_qos_capabilities */ \
975 NULL, /* get_qos */ \
976 NULL, /* set_qos */ \
977 NULL, /* get_queue */ \
978 NULL, /* set_queue */ \
979 NULL, /* delete_queue */ \
980 NULL, /* get_queue_stats */ \
981 NULL, /* dump_queues */ \
982 NULL, /* dump_queue_stats */ \
984 NULL, /* get_in4 */ \
985 NULL, /* set_in4 */ \
986 NULL, /* get_in6 */ \
987 NULL, /* add_router */ \
988 NULL, /* get_next_hop */ \
990 NULL, /* arp_lookup */ \
992 netdev_vport_update_flags, \
994 netdev_vport_change_seq
997 netdev_vport_register(void)
999 static const struct vport_class vport_classes[] = {
1000 { OVS_VPORT_TYPE_GRE,
1001 { "gre", VPORT_FUNCTIONS(netdev_vport_get_drv_info) },
1002 parse_tunnel_config, unparse_tunnel_config },
1004 { OVS_VPORT_TYPE_GRE,
1005 { "ipsec_gre", VPORT_FUNCTIONS(netdev_vport_get_drv_info) },
1006 parse_tunnel_config, unparse_tunnel_config },
1008 { OVS_VPORT_TYPE_GRE64,
1009 { "gre64", VPORT_FUNCTIONS(netdev_vport_get_drv_info) },
1010 parse_tunnel_config, unparse_tunnel_config },
1012 { OVS_VPORT_TYPE_GRE64,
1013 { "ipsec_gre64", VPORT_FUNCTIONS(netdev_vport_get_drv_info) },
1014 parse_tunnel_config, unparse_tunnel_config },
1016 { OVS_VPORT_TYPE_CAPWAP,
1017 { "capwap", VPORT_FUNCTIONS(netdev_vport_get_drv_info) },
1018 parse_tunnel_config, unparse_tunnel_config },
1020 { OVS_VPORT_TYPE_VXLAN,
1021 { "vxlan", VPORT_FUNCTIONS(netdev_vport_get_drv_info) },
1022 parse_tunnel_config, unparse_tunnel_config },
1024 { OVS_VPORT_TYPE_PATCH,
1025 { "patch", VPORT_FUNCTIONS(NULL) },
1026 parse_patch_config, unparse_patch_config }
1031 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
1032 netdev_register_provider(&vport_classes[i].netdev_class);