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);
436 bool is_gre = !strcmp(type, "gre");
437 struct tnl_port_config config;
438 struct shash_node *node;
439 bool ipsec_mech_set = false;
441 memset(&config, 0, sizeof config);
442 config.flags |= TNL_F_PMTUD;
443 config.flags |= TNL_F_HDR_CACHE;
445 SHASH_FOR_EACH (node, args) {
446 if (!strcmp(node->name, "remote_ip")) {
447 struct in_addr in_addr;
448 if (lookup_ip(node->data, &in_addr)) {
449 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
451 config.daddr = in_addr.s_addr;
453 } else if (!strcmp(node->name, "local_ip")) {
454 struct in_addr in_addr;
455 if (lookup_ip(node->data, &in_addr)) {
456 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
458 config.saddr = in_addr.s_addr;
460 } else if (!strcmp(node->name, "key") && is_gre) {
461 if (!strcmp(node->data, "flow")) {
462 config.flags |= TNL_F_IN_KEY_MATCH;
463 config.flags |= TNL_F_OUT_KEY_ACTION;
465 uint64_t key = strtoull(node->data, NULL, 0);
466 config.out_key = config.in_key = htonll(key);
468 } else if (!strcmp(node->name, "in_key") && is_gre) {
469 if (!strcmp(node->data, "flow")) {
470 config.flags |= TNL_F_IN_KEY_MATCH;
472 config.in_key = htonll(strtoull(node->data, NULL, 0));
474 } else if (!strcmp(node->name, "out_key") && is_gre) {
475 if (!strcmp(node->data, "flow")) {
476 config.flags |= TNL_F_OUT_KEY_ACTION;
478 config.out_key = htonll(strtoull(node->data, NULL, 0));
480 } else if (!strcmp(node->name, "tos")) {
481 if (!strcmp(node->data, "inherit")) {
482 config.flags |= TNL_F_TOS_INHERIT;
484 config.tos = atoi(node->data);
486 } else if (!strcmp(node->name, "ttl")) {
487 if (!strcmp(node->data, "inherit")) {
488 config.flags |= TNL_F_TTL_INHERIT;
490 config.ttl = atoi(node->data);
492 } else if (!strcmp(node->name, "csum") && is_gre) {
493 if (!strcmp(node->data, "true")) {
494 config.flags |= TNL_F_CSUM;
496 } else if (!strcmp(node->name, "pmtud")) {
497 if (!strcmp(node->data, "false")) {
498 config.flags &= ~TNL_F_PMTUD;
500 } else if (!strcmp(node->name, "header_cache")) {
501 if (!strcmp(node->data, "false")) {
502 config.flags &= ~TNL_F_HDR_CACHE;
504 } else if (!strcmp(node->name, "ipsec_cert")
505 || !strcmp(node->name, "ipsec_psk")) {
506 ipsec_mech_set = true;
508 VLOG_WARN("%s: unknown %s argument '%s'",
509 name, type, node->name);
513 /* IPsec doesn't work when header caching is enabled. Disable it if the
514 * IPsec local IP address and authentication mechanism have been defined. */
515 if (ipsec_mech_set) {
516 VLOG_INFO("%s: header caching disabled due to use of IPsec", name);
517 config.flags &= ~TNL_F_HDR_CACHE;
521 VLOG_WARN("%s: %s type requires valid 'remote_ip' argument",
526 BUILD_ASSERT(sizeof config <= VPORT_CONFIG_SIZE);
527 memcpy(configp, &config, sizeof config);
532 parse_patch_config(const struct netdev_dev *dev, const struct shash *args,
535 const char *name = netdev_dev_get_name(dev);
538 peer = shash_find_data(args, "peer");
540 VLOG_WARN("%s: patch type requires valid 'peer' argument", name);
544 if (shash_count(args) > 1) {
545 VLOG_WARN("%s: patch type takes only a 'peer' argument", name);
549 if (strlen(peer) >= MIN(IFNAMSIZ, VPORT_CONFIG_SIZE)) {
550 VLOG_WARN("%s: patch 'peer' arg too long", name);
554 if (!strcmp(name, peer)) {
555 VLOG_WARN("%s: patch peer must not be self", name);
559 strncpy(configp, peer, VPORT_CONFIG_SIZE);
564 #define VPORT_FUNCTIONS \
569 netdev_vport_create, \
570 netdev_vport_destroy, \
571 netdev_vport_reconfigure, \
574 netdev_vport_close, \
576 NULL, /* enumerate */ \
579 NULL, /* recv_wait */ \
583 NULL, /* send_wait */ \
585 netdev_vport_set_etheraddr, \
586 netdev_vport_get_etheraddr, \
587 netdev_vport_get_mtu, \
588 NULL, /* get_ifindex */ \
589 NULL, /* get_carrier */ \
590 netdev_vport_get_stats, \
591 netdev_vport_set_stats, \
593 NULL, /* get_features */ \
594 NULL, /* set_advertisements */ \
595 NULL, /* get_vlan_vid */ \
597 NULL, /* set_policing */ \
598 NULL, /* get_qos_types */ \
599 NULL, /* get_qos_capabilities */ \
600 NULL, /* get_qos */ \
601 NULL, /* set_qos */ \
602 NULL, /* get_queue */ \
603 NULL, /* set_queue */ \
604 NULL, /* delete_queue */ \
605 NULL, /* get_queue_stats */ \
606 NULL, /* dump_queues */ \
607 NULL, /* dump_queue_stats */ \
609 NULL, /* get_in4 */ \
610 NULL, /* set_in4 */ \
611 NULL, /* get_in6 */ \
612 NULL, /* add_router */ \
613 NULL, /* get_next_hop */ \
614 NULL, /* arp_lookup */ \
616 netdev_vport_update_flags, \
618 netdev_vport_poll_add, \
619 netdev_vport_poll_remove,
622 netdev_vport_register(void)
624 static const struct vport_class vport_classes[] = {
625 { { "gre", VPORT_FUNCTIONS }, parse_tunnel_config },
626 { { "capwap", VPORT_FUNCTIONS }, parse_tunnel_config },
627 { { "patch", VPORT_FUNCTIONS }, parse_patch_config }
632 for (i = 0; i < ARRAY_SIZE(vport_classes); i++) {
633 netdev_register_provider(&vport_classes[i].netdev_class);