2 * Copyright (c) 2010 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"
24 #include <sys/ioctl.h>
26 #include "byte-order.h"
28 #include "netdev-provider.h"
29 #include "openvswitch/datapath-protocol.h"
30 #include "openvswitch/tunnel.h"
33 #include "socket-util.h"
36 VLOG_DEFINE_THIS_MODULE(netdev_vport);
38 struct netdev_vport_notifier {
39 struct netdev_notifier notifier;
40 struct list list_node;
41 struct shash_node *shash_node;
44 struct netdev_dev_vport {
45 struct netdev_dev netdev_dev;
46 uint64_t config[VPORT_CONFIG_SIZE / 8];
54 struct netdev_class netdev_class;
55 int (*parse_config)(const struct netdev_dev *, const struct shash *args,
59 static struct shash netdev_vport_notifiers =
60 SHASH_INITIALIZER(&netdev_vport_notifiers);
62 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
64 static int netdev_vport_do_ioctl(int cmd, void *arg);
65 static int netdev_vport_create(const struct netdev_class *, const char *,
66 const struct shash *, struct netdev_dev **);
67 static void netdev_vport_poll_notify(const struct netdev *);
70 is_vport_class(const struct netdev_class *class)
72 return class->create == netdev_vport_create;
75 static const struct vport_class *
76 vport_class_cast(const struct netdev_class *class)
78 assert(is_vport_class(class));
79 return CONTAINER_OF(class, struct vport_class, netdev_class);
82 static struct netdev_dev_vport *
83 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
85 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
86 return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
89 static struct netdev_vport *
90 netdev_vport_cast(const struct netdev *netdev)
92 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
93 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
94 return CONTAINER_OF(netdev, struct netdev_vport, netdev);
97 /* If 'netdev' is a vport netdev, copies its kernel configuration into
98 * 'config'. Otherwise leaves 'config' untouched. */
100 netdev_vport_get_config(const struct netdev *netdev, void *config)
102 const struct netdev_dev *dev = netdev_get_dev(netdev);
104 if (is_vport_class(netdev_dev_get_class(dev))) {
105 const struct netdev_dev_vport *vport = netdev_dev_vport_cast(dev);
106 memcpy(config, vport->config, VPORT_CONFIG_SIZE);
111 netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
112 const struct shash *args,
113 struct netdev_dev **netdev_devp)
115 const struct vport_class *vport_class = vport_class_cast(netdev_class);
116 struct netdev_dev_vport *dev;
119 dev = xmalloc(sizeof *dev);
120 *netdev_devp = &dev->netdev_dev;
121 netdev_dev_init(&dev->netdev_dev, name, netdev_class);
123 memset(dev->config, 0, sizeof dev->config);
124 error = vport_class->parse_config(&dev->netdev_dev, args, dev->config);
127 netdev_dev_uninit(&dev->netdev_dev, true);
133 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
135 struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
141 netdev_vport_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
142 struct netdev **netdevp)
144 struct netdev_vport *netdev;
146 netdev = xmalloc(sizeof *netdev);
147 netdev_init(&netdev->netdev, netdev_dev_);
149 *netdevp = &netdev->netdev;
154 netdev_vport_close(struct netdev *netdev_)
156 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
161 netdev_vport_reconfigure(struct netdev_dev *dev_,
162 const struct shash *args)
164 const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
165 const struct vport_class *vport_class = vport_class_cast(netdev_class);
166 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
167 struct odp_port port;
170 memset(&port, 0, sizeof port);
171 strncpy(port.devname, netdev_dev_get_name(dev_), sizeof port.devname);
172 strncpy(port.type, netdev_dev_get_type(dev_), sizeof port.type);
173 error = vport_class->parse_config(dev_, args, port.config);
174 if (!error && memcmp(port.config, dev->config, sizeof dev->config)) {
175 error = netdev_vport_do_ioctl(ODP_VPORT_MOD, &port);
176 if (!error || error == ENODEV) {
177 /* Either reconfiguration succeeded or this vport is not installed
178 * in the kernel (e.g. it hasn't been added to a dpif yet with
179 * dpif_port_add()). */
180 memcpy(dev->config, port.config, sizeof dev->config);
187 netdev_vport_set_etheraddr(struct netdev *netdev,
188 const uint8_t mac[ETH_ADDR_LEN])
190 struct odp_vport_ether vport_ether;
193 ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
194 sizeof vport_ether.devname);
196 memcpy(vport_ether.ether_addr, mac, ETH_ADDR_LEN);
198 err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_SET, &vport_ether);
203 netdev_vport_poll_notify(netdev);
208 netdev_vport_get_etheraddr(const struct netdev *netdev,
209 uint8_t mac[ETH_ADDR_LEN])
211 struct odp_vport_ether vport_ether;
214 ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
215 sizeof vport_ether.devname);
217 err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_GET, &vport_ether);
222 memcpy(mac, vport_ether.ether_addr, ETH_ADDR_LEN);
227 netdev_vport_get_mtu(const struct netdev *netdev, int *mtup)
229 struct odp_vport_mtu vport_mtu;
232 ovs_strlcpy(vport_mtu.devname, netdev_get_name(netdev),
233 sizeof vport_mtu.devname);
235 err = netdev_vport_do_ioctl(ODP_VPORT_MTU_GET, &vport_mtu);
240 *mtup = vport_mtu.mtu;
245 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
247 const char *name = netdev_get_name(netdev);
248 struct odp_vport_stats_req ovsr;
251 ovs_strlcpy(ovsr.devname, name, sizeof ovsr.devname);
252 err = netdev_vport_do_ioctl(ODP_VPORT_STATS_GET, &ovsr);
257 stats->rx_packets = ovsr.stats.rx_packets;
258 stats->tx_packets = ovsr.stats.tx_packets;
259 stats->rx_bytes = ovsr.stats.rx_bytes;
260 stats->tx_bytes = ovsr.stats.tx_bytes;
261 stats->rx_errors = ovsr.stats.rx_errors;
262 stats->tx_errors = ovsr.stats.tx_errors;
263 stats->rx_dropped = ovsr.stats.rx_dropped;
264 stats->tx_dropped = ovsr.stats.tx_dropped;
265 stats->multicast = ovsr.stats.multicast;
266 stats->collisions = ovsr.stats.collisions;
267 stats->rx_length_errors = ovsr.stats.rx_length_errors;
268 stats->rx_over_errors = ovsr.stats.rx_over_errors;
269 stats->rx_crc_errors = ovsr.stats.rx_crc_errors;
270 stats->rx_frame_errors = ovsr.stats.rx_frame_errors;
271 stats->rx_fifo_errors = ovsr.stats.rx_fifo_errors;
272 stats->rx_missed_errors = ovsr.stats.rx_missed_errors;
273 stats->tx_aborted_errors = ovsr.stats.tx_aborted_errors;
274 stats->tx_carrier_errors = ovsr.stats.tx_carrier_errors;
275 stats->tx_fifo_errors = ovsr.stats.tx_fifo_errors;
276 stats->tx_heartbeat_errors = ovsr.stats.tx_heartbeat_errors;
277 stats->tx_window_errors = ovsr.stats.tx_window_errors;
283 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
285 struct odp_vport_stats_req ovsr;
288 ovs_strlcpy(ovsr.devname, netdev_get_name(netdev), sizeof ovsr.devname);
290 ovsr.stats.rx_packets = stats->rx_packets;
291 ovsr.stats.tx_packets = stats->tx_packets;
292 ovsr.stats.rx_bytes = stats->rx_bytes;
293 ovsr.stats.tx_bytes = stats->tx_bytes;
294 ovsr.stats.rx_errors = stats->rx_errors;
295 ovsr.stats.tx_errors = stats->tx_errors;
296 ovsr.stats.rx_dropped = stats->rx_dropped;
297 ovsr.stats.tx_dropped = stats->tx_dropped;
298 ovsr.stats.multicast = stats->multicast;
299 ovsr.stats.collisions = stats->collisions;
300 ovsr.stats.rx_length_errors = stats->rx_length_errors;
301 ovsr.stats.rx_over_errors = stats->rx_over_errors;
302 ovsr.stats.rx_crc_errors = stats->rx_crc_errors;
303 ovsr.stats.rx_frame_errors = stats->rx_frame_errors;
304 ovsr.stats.rx_fifo_errors = stats->rx_fifo_errors;
305 ovsr.stats.rx_missed_errors = stats->rx_missed_errors;
306 ovsr.stats.tx_aborted_errors = stats->tx_aborted_errors;
307 ovsr.stats.tx_carrier_errors = stats->tx_carrier_errors;
308 ovsr.stats.tx_fifo_errors = stats->tx_fifo_errors;
309 ovsr.stats.tx_heartbeat_errors = stats->tx_heartbeat_errors;
310 ovsr.stats.tx_window_errors = stats->tx_window_errors;
312 err = netdev_vport_do_ioctl(ODP_VPORT_STATS_SET, &ovsr);
314 /* If the vport layer doesn't know about the device, that doesn't mean it
315 * doesn't exist (after all were able to open it when netdev_open() was
316 * called), it just means that it isn't attached and we'll be getting
317 * stats a different way. */
326 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
327 enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
328 enum netdev_flags *old_flagsp)
330 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
334 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
339 make_poll_name(const struct netdev *netdev)
341 return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
345 netdev_vport_poll_add(struct netdev *netdev,
346 void (*cb)(struct netdev_notifier *), void *aux,
347 struct netdev_notifier **notifierp)
349 char *poll_name = make_poll_name(netdev);
350 struct netdev_vport_notifier *notifier;
352 struct shash_node *shash_node;
354 shash_node = shash_find_data(&netdev_vport_notifiers, poll_name);
356 list = xmalloc(sizeof *list);
358 shash_node = shash_add(&netdev_vport_notifiers, poll_name, list);
360 list = shash_node->data;
363 notifier = xmalloc(sizeof *notifier);
364 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
365 list_push_back(list, ¬ifier->list_node);
366 notifier->shash_node = shash_node;
368 *notifierp = ¬ifier->notifier;
375 netdev_vport_poll_remove(struct netdev_notifier *notifier_)
377 struct netdev_vport_notifier *notifier =
378 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
382 list = list_remove(¬ifier->list_node);
383 if (list_is_empty(list)) {
384 shash_delete(&netdev_vport_notifiers, notifier->shash_node);
391 /* Helper functions. */
394 netdev_vport_do_ioctl(int cmd, void *arg)
396 static int ioctl_fd = -1;
399 ioctl_fd = open("/dev/net/dp0", O_RDONLY | O_NONBLOCK);
401 VLOG_ERR_RL(&rl, "failed to open ioctl fd: %s", strerror(errno));
406 return ioctl(ioctl_fd, cmd, arg) ? errno : 0;
410 netdev_vport_poll_notify(const struct netdev *netdev)
412 char *poll_name = make_poll_name(netdev);
413 struct list *list = shash_find_data(&netdev_vport_notifiers,
417 struct netdev_vport_notifier *notifier;
419 LIST_FOR_EACH (notifier, list_node, list) {
420 struct netdev_notifier *n = ¬ifier->notifier;
428 /* Code specific to individual vport types. */
431 parse_tunnel_config(const struct netdev_dev *dev, const struct shash *args,
434 const char *name = netdev_dev_get_name(dev);
435 const char *type = netdev_dev_get_type(dev);
437 bool is_ipsec = false;
438 struct tnl_port_config config;
439 struct shash_node *node;
440 bool ipsec_mech_set = false;
442 memset(&config, 0, sizeof config);
443 config.flags |= TNL_F_PMTUD;
444 config.flags |= TNL_F_HDR_CACHE;
446 if (!strcmp(type, "gre")) {
448 } else if (!strcmp(type, "ipsec_gre")) {
452 config.flags |= TNL_F_IPSEC;
454 /* IPsec doesn't work when header caching is enabled. */
455 config.flags &= ~TNL_F_HDR_CACHE;
458 SHASH_FOR_EACH (node, args) {
459 if (!strcmp(node->name, "remote_ip")) {
460 struct in_addr in_addr;
461 if (lookup_ip(node->data, &in_addr)) {
462 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
464 config.daddr = in_addr.s_addr;
466 } else if (!strcmp(node->name, "local_ip")) {
467 struct in_addr in_addr;
468 if (lookup_ip(node->data, &in_addr)) {
469 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
471 config.saddr = in_addr.s_addr;
473 } else if (!strcmp(node->name, "key") && is_gre) {
474 if (!strcmp(node->data, "flow")) {
475 config.flags |= TNL_F_IN_KEY_MATCH;
476 config.flags |= TNL_F_OUT_KEY_ACTION;
478 uint64_t key = strtoull(node->data, NULL, 0);
479 config.out_key = config.in_key = htonll(key);
481 } else if (!strcmp(node->name, "in_key") && is_gre) {
482 if (!strcmp(node->data, "flow")) {
483 config.flags |= TNL_F_IN_KEY_MATCH;
485 config.in_key = htonll(strtoull(node->data, NULL, 0));
487 } else if (!strcmp(node->name, "out_key") && is_gre) {
488 if (!strcmp(node->data, "flow")) {
489 config.flags |= TNL_F_OUT_KEY_ACTION;
491 config.out_key = htonll(strtoull(node->data, NULL, 0));
493 } else if (!strcmp(node->name, "tos")) {
494 if (!strcmp(node->data, "inherit")) {
495 config.flags |= TNL_F_TOS_INHERIT;
497 config.tos = atoi(node->data);
499 } else if (!strcmp(node->name, "ttl")) {
500 if (!strcmp(node->data, "inherit")) {
501 config.flags |= TNL_F_TTL_INHERIT;
503 config.ttl = atoi(node->data);
505 } else if (!strcmp(node->name, "csum") && is_gre) {
506 if (!strcmp(node->data, "true")) {
507 config.flags |= TNL_F_CSUM;
509 } else if (!strcmp(node->name, "pmtud")) {
510 if (!strcmp(node->data, "false")) {
511 config.flags &= ~TNL_F_PMTUD;
513 } else if (!strcmp(node->name, "header_cache")) {
514 if (!strcmp(node->data, "false")) {
515 config.flags &= ~TNL_F_HDR_CACHE;
517 } else if (!strcmp(node->name, "peer_cert") && is_ipsec) {
518 if (shash_find(args, "certificate")) {
519 ipsec_mech_set = true;
521 const char *use_ssl_cert;
523 /* If the "use_ssl_cert" is true, then "certificate" and
524 * "private_key" will be pulled from the SSL table. The
525 * use of this option is strongly discouraged, since it
526 * will like be removed when multiple SSL configurations
527 * are supported by OVS.
529 use_ssl_cert = shash_find_data(args, "use_ssl_cert");
530 if (!use_ssl_cert || strcmp(use_ssl_cert, "true")) {
531 VLOG_WARN("%s: 'peer_cert' requires 'certificate' argument",
535 ipsec_mech_set = true;
537 } else if (!strcmp(node->name, "psk") && is_ipsec) {
538 ipsec_mech_set = true;
540 && (!strcmp(node->name, "certificate")
541 || !strcmp(node->name, "private_key")
542 || !strcmp(node->name, "use_ssl_cert"))) {
543 /* Ignore options not used by the netdev. */
545 VLOG_WARN("%s: unknown %s argument '%s'",
546 name, type, node->name);
551 if (shash_find(args, "peer_cert") && shash_find(args, "psk")) {
552 VLOG_WARN("%s: cannot define both 'peer_cert' and 'psk'", name);
556 if (!ipsec_mech_set) {
557 VLOG_WARN("%s: IPsec requires an 'peer_cert' or psk' argument",
564 VLOG_WARN("%s: %s type requires valid 'remote_ip' argument",
569 BUILD_ASSERT(sizeof config <= VPORT_CONFIG_SIZE);
570 memcpy(configp, &config, sizeof config);
575 parse_patch_config(const struct netdev_dev *dev, const struct shash *args,
578 const char *name = netdev_dev_get_name(dev);
581 peer = shash_find_data(args, "peer");
583 VLOG_WARN("%s: patch type requires valid 'peer' argument", name);
587 if (shash_count(args) > 1) {
588 VLOG_WARN("%s: patch type takes only a 'peer' argument", name);
592 if (strlen(peer) >= MIN(IFNAMSIZ, VPORT_CONFIG_SIZE)) {
593 VLOG_WARN("%s: patch 'peer' arg too long", name);
597 if (!strcmp(name, peer)) {
598 VLOG_WARN("%s: patch peer must not be self", name);
602 strncpy(configp, peer, VPORT_CONFIG_SIZE);
607 #define VPORT_FUNCTIONS \
612 netdev_vport_create, \
613 netdev_vport_destroy, \
614 netdev_vport_reconfigure, \
617 netdev_vport_close, \
619 NULL, /* enumerate */ \
622 NULL, /* recv_wait */ \
626 NULL, /* send_wait */ \
628 netdev_vport_set_etheraddr, \
629 netdev_vport_get_etheraddr, \
630 netdev_vport_get_mtu, \
631 NULL, /* get_ifindex */ \
632 NULL, /* get_carrier */ \
633 netdev_vport_get_stats, \
634 netdev_vport_set_stats, \
636 NULL, /* get_features */ \
637 NULL, /* set_advertisements */ \
638 NULL, /* get_vlan_vid */ \
640 NULL, /* set_policing */ \
641 NULL, /* get_qos_types */ \
642 NULL, /* get_qos_capabilities */ \
643 NULL, /* get_qos */ \
644 NULL, /* set_qos */ \
645 NULL, /* get_queue */ \
646 NULL, /* set_queue */ \
647 NULL, /* delete_queue */ \
648 NULL, /* get_queue_stats */ \
649 NULL, /* dump_queues */ \
650 NULL, /* dump_queue_stats */ \
652 NULL, /* get_in4 */ \
653 NULL, /* set_in4 */ \
654 NULL, /* get_in6 */ \
655 NULL, /* add_router */ \
656 NULL, /* get_next_hop */ \
657 NULL, /* arp_lookup */ \
659 netdev_vport_update_flags, \
661 netdev_vport_poll_add, \
662 netdev_vport_poll_remove,
665 netdev_vport_register(void)
667 static const struct vport_class vport_classes[] = {
668 { { "gre", VPORT_FUNCTIONS }, parse_tunnel_config },
669 { { "ipsec_gre", VPORT_FUNCTIONS }, parse_tunnel_config },
670 { { "capwap", VPORT_FUNCTIONS }, parse_tunnel_config },
671 { { "patch", VPORT_FUNCTIONS }, parse_patch_config }
676 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
677 netdev_register_provider(&vport_classes[i].netdev_class);