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>
27 #include "netdev-provider.h"
28 #include "openvswitch/datapath-protocol.h"
29 #include "openvswitch/tunnel.h"
32 #include "socket-util.h"
35 VLOG_DEFINE_THIS_MODULE(netdev_vport);
37 struct netdev_vport_notifier {
38 struct netdev_notifier notifier;
39 struct list list_node;
40 struct shash_node *shash_node;
43 struct netdev_dev_vport {
44 struct netdev_dev netdev_dev;
45 uint64_t config[VPORT_CONFIG_SIZE / 8];
53 struct netdev_class netdev_class;
54 int (*parse_config)(const struct netdev_dev *, const struct shash *args,
58 static struct shash netdev_vport_notifiers =
59 SHASH_INITIALIZER(&netdev_vport_notifiers);
61 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
63 static int netdev_vport_do_ioctl(int cmd, void *arg);
64 static int netdev_vport_create(const struct netdev_class *, const char *,
65 const struct shash *, struct netdev_dev **);
66 static void netdev_vport_poll_notify(const struct netdev *);
69 is_vport_class(const struct netdev_class *class)
71 return class->create == netdev_vport_create;
74 static const struct vport_class *
75 vport_class_cast(const struct netdev_class *class)
77 assert(is_vport_class(class));
78 return CONTAINER_OF(class, struct vport_class, netdev_class);
81 static struct netdev_dev_vport *
82 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
84 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
85 return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
88 static struct netdev_vport *
89 netdev_vport_cast(const struct netdev *netdev)
91 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
92 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
93 return CONTAINER_OF(netdev, struct netdev_vport, netdev);
96 /* If 'netdev' is a vport netdev, copies its kernel configuration into
97 * 'config'. Otherwise leaves 'config' untouched. */
99 netdev_vport_get_config(const struct netdev *netdev, void *config)
101 const struct netdev_dev *dev = netdev_get_dev(netdev);
103 if (is_vport_class(netdev_dev_get_class(dev))) {
104 const struct netdev_dev_vport *vport = netdev_dev_vport_cast(dev);
105 memcpy(config, vport->config, VPORT_CONFIG_SIZE);
110 netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
111 const struct shash *args,
112 struct netdev_dev **netdev_devp)
114 const struct vport_class *vport_class = vport_class_cast(netdev_class);
115 struct netdev_dev_vport *dev;
118 dev = xmalloc(sizeof *dev);
119 *netdev_devp = &dev->netdev_dev;
120 netdev_dev_init(&dev->netdev_dev, name, netdev_class);
122 memset(dev->config, 0, sizeof dev->config);
123 error = vport_class->parse_config(&dev->netdev_dev, args, dev->config);
126 netdev_dev_uninit(&dev->netdev_dev, true);
132 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
134 struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
140 netdev_vport_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
141 struct netdev **netdevp)
143 struct netdev_vport *netdev;
145 netdev = xmalloc(sizeof *netdev);
146 netdev_init(&netdev->netdev, netdev_dev_);
148 *netdevp = &netdev->netdev;
153 netdev_vport_close(struct netdev *netdev_)
155 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
160 netdev_vport_reconfigure(struct netdev_dev *dev_,
161 const struct shash *args)
163 const struct netdev_class *netdev_class = netdev_dev_get_class(dev_);
164 const struct vport_class *vport_class = vport_class_cast(netdev_class);
165 struct netdev_dev_vport *dev = netdev_dev_vport_cast(dev_);
166 struct odp_port port;
169 memset(&port, 0, sizeof port);
170 strncpy(port.devname, netdev_dev_get_name(dev_), sizeof port.devname);
171 strncpy(port.type, netdev_dev_get_type(dev_), sizeof port.type);
172 error = vport_class->parse_config(dev_, args, port.config);
173 if (!error && memcmp(port.config, dev->config, sizeof dev->config)) {
174 error = netdev_vport_do_ioctl(ODP_VPORT_MOD, &port);
175 if (!error || error == ENODEV) {
176 /* Either reconfiguration succeeded or this vport is not installed
177 * in the kernel (e.g. it hasn't been added to a dpif yet with
178 * dpif_port_add()). */
179 memcpy(dev->config, port.config, sizeof dev->config);
186 netdev_vport_set_etheraddr(struct netdev *netdev,
187 const uint8_t mac[ETH_ADDR_LEN])
189 struct odp_vport_ether vport_ether;
192 ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
193 sizeof vport_ether.devname);
195 memcpy(vport_ether.ether_addr, mac, ETH_ADDR_LEN);
197 err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_SET, &vport_ether);
202 netdev_vport_poll_notify(netdev);
207 netdev_vport_get_etheraddr(const struct netdev *netdev,
208 uint8_t mac[ETH_ADDR_LEN])
210 struct odp_vport_ether vport_ether;
213 ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
214 sizeof vport_ether.devname);
216 err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_GET, &vport_ether);
221 memcpy(mac, vport_ether.ether_addr, ETH_ADDR_LEN);
226 netdev_vport_get_mtu(const struct netdev *netdev, int *mtup)
228 struct odp_vport_mtu vport_mtu;
231 ovs_strlcpy(vport_mtu.devname, netdev_get_name(netdev),
232 sizeof vport_mtu.devname);
234 err = netdev_vport_do_ioctl(ODP_VPORT_MTU_GET, &vport_mtu);
239 *mtup = vport_mtu.mtu;
244 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
246 const char *name = netdev_get_name(netdev);
247 struct odp_vport_stats_req ovsr;
250 ovs_strlcpy(ovsr.devname, name, sizeof ovsr.devname);
251 err = netdev_vport_do_ioctl(ODP_VPORT_STATS_GET, &ovsr);
256 stats->rx_packets = ovsr.stats.rx_packets;
257 stats->tx_packets = ovsr.stats.tx_packets;
258 stats->rx_bytes = ovsr.stats.rx_bytes;
259 stats->tx_bytes = ovsr.stats.tx_bytes;
260 stats->rx_errors = ovsr.stats.rx_errors;
261 stats->tx_errors = ovsr.stats.tx_errors;
262 stats->rx_dropped = ovsr.stats.rx_dropped;
263 stats->tx_dropped = ovsr.stats.tx_dropped;
264 stats->multicast = ovsr.stats.multicast;
265 stats->collisions = ovsr.stats.collisions;
266 stats->rx_length_errors = ovsr.stats.rx_length_errors;
267 stats->rx_over_errors = ovsr.stats.rx_over_errors;
268 stats->rx_crc_errors = ovsr.stats.rx_crc_errors;
269 stats->rx_frame_errors = ovsr.stats.rx_frame_errors;
270 stats->rx_fifo_errors = ovsr.stats.rx_fifo_errors;
271 stats->rx_missed_errors = ovsr.stats.rx_missed_errors;
272 stats->tx_aborted_errors = ovsr.stats.tx_aborted_errors;
273 stats->tx_carrier_errors = ovsr.stats.tx_carrier_errors;
274 stats->tx_fifo_errors = ovsr.stats.tx_fifo_errors;
275 stats->tx_heartbeat_errors = ovsr.stats.tx_heartbeat_errors;
276 stats->tx_window_errors = ovsr.stats.tx_window_errors;
282 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
284 struct odp_vport_stats_req ovsr;
287 ovs_strlcpy(ovsr.devname, netdev_get_name(netdev), sizeof ovsr.devname);
289 ovsr.stats.rx_packets = stats->rx_packets;
290 ovsr.stats.tx_packets = stats->tx_packets;
291 ovsr.stats.rx_bytes = stats->rx_bytes;
292 ovsr.stats.tx_bytes = stats->tx_bytes;
293 ovsr.stats.rx_errors = stats->rx_errors;
294 ovsr.stats.tx_errors = stats->tx_errors;
295 ovsr.stats.rx_dropped = stats->rx_dropped;
296 ovsr.stats.tx_dropped = stats->tx_dropped;
297 ovsr.stats.multicast = stats->multicast;
298 ovsr.stats.collisions = stats->collisions;
299 ovsr.stats.rx_length_errors = stats->rx_length_errors;
300 ovsr.stats.rx_over_errors = stats->rx_over_errors;
301 ovsr.stats.rx_crc_errors = stats->rx_crc_errors;
302 ovsr.stats.rx_frame_errors = stats->rx_frame_errors;
303 ovsr.stats.rx_fifo_errors = stats->rx_fifo_errors;
304 ovsr.stats.rx_missed_errors = stats->rx_missed_errors;
305 ovsr.stats.tx_aborted_errors = stats->tx_aborted_errors;
306 ovsr.stats.tx_carrier_errors = stats->tx_carrier_errors;
307 ovsr.stats.tx_fifo_errors = stats->tx_fifo_errors;
308 ovsr.stats.tx_heartbeat_errors = stats->tx_heartbeat_errors;
309 ovsr.stats.tx_window_errors = stats->tx_window_errors;
311 err = netdev_vport_do_ioctl(ODP_VPORT_STATS_SET, &ovsr);
313 /* If the vport layer doesn't know about the device, that doesn't mean it
314 * doesn't exist (after all were able to open it when netdev_open() was
315 * called), it just means that it isn't attached and we'll be getting
316 * stats a different way. */
325 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
326 enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
327 enum netdev_flags *old_flagsp)
329 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
333 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
338 make_poll_name(const struct netdev *netdev)
340 return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
344 netdev_vport_poll_add(struct netdev *netdev,
345 void (*cb)(struct netdev_notifier *), void *aux,
346 struct netdev_notifier **notifierp)
348 char *poll_name = make_poll_name(netdev);
349 struct netdev_vport_notifier *notifier;
351 struct shash_node *shash_node;
353 shash_node = shash_find_data(&netdev_vport_notifiers, poll_name);
355 list = xmalloc(sizeof *list);
357 shash_node = shash_add(&netdev_vport_notifiers, poll_name, list);
359 list = shash_node->data;
362 notifier = xmalloc(sizeof *notifier);
363 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
364 list_push_back(list, ¬ifier->list_node);
365 notifier->shash_node = shash_node;
367 *notifierp = ¬ifier->notifier;
374 netdev_vport_poll_remove(struct netdev_notifier *notifier_)
376 struct netdev_vport_notifier *notifier =
377 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
381 list = list_remove(¬ifier->list_node);
382 if (list_is_empty(list)) {
383 shash_delete(&netdev_vport_notifiers, notifier->shash_node);
390 /* Helper functions. */
393 netdev_vport_do_ioctl(int cmd, void *arg)
395 static int ioctl_fd = -1;
398 ioctl_fd = open("/dev/net/dp0", O_RDONLY | O_NONBLOCK);
400 VLOG_ERR_RL(&rl, "failed to open ioctl fd: %s", strerror(errno));
405 return ioctl(ioctl_fd, cmd, arg) ? errno : 0;
409 netdev_vport_poll_notify(const struct netdev *netdev)
411 char *poll_name = make_poll_name(netdev);
412 struct list *list = shash_find_data(&netdev_vport_notifiers,
416 struct netdev_vport_notifier *notifier;
418 LIST_FOR_EACH (notifier, list_node, list) {
419 struct netdev_notifier *n = ¬ifier->notifier;
427 /* Code specific to individual vport types. */
430 parse_tunnel_config(const struct netdev_dev *dev, const struct shash *args,
433 const char *name = netdev_dev_get_name(dev);
434 const char *type = netdev_dev_get_type(dev);
435 bool is_gre = !strcmp(type, "gre");
436 struct tnl_port_config config;
437 struct shash_node *node;
438 bool ipsec_ip_set = false;
439 bool ipsec_mech_set = false;
441 config.flags |= TNL_F_PMTUD;
442 config.flags |= TNL_F_HDR_CACHE;
444 SHASH_FOR_EACH (node, args) {
445 if (!strcmp(node->name, "remote_ip")) {
446 struct in_addr in_addr;
447 if (lookup_ip(node->data, &in_addr)) {
448 VLOG_WARN("%s: bad %s 'remote_ip'", name, type);
450 config.daddr = in_addr.s_addr;
452 } else if (!strcmp(node->name, "local_ip")) {
453 struct in_addr in_addr;
454 if (lookup_ip(node->data, &in_addr)) {
455 VLOG_WARN("%s: bad %s 'local_ip'", name, type);
457 config.saddr = in_addr.s_addr;
459 } else if (!strcmp(node->name, "key") && is_gre) {
460 if (!strcmp(node->data, "flow")) {
461 config.flags |= TNL_F_IN_KEY_MATCH;
462 config.flags |= TNL_F_OUT_KEY_ACTION;
464 config.out_key = config.in_key = htonl(atoi(node->data));
466 } else if (!strcmp(node->name, "in_key") && is_gre) {
467 if (!strcmp(node->data, "flow")) {
468 config.flags |= TNL_F_IN_KEY_MATCH;
470 config.in_key = htonl(atoi(node->data));
472 } else if (!strcmp(node->name, "out_key") && is_gre) {
473 if (!strcmp(node->data, "flow")) {
474 config.flags |= TNL_F_OUT_KEY_ACTION;
476 config.out_key = htonl(atoi(node->data));
478 } else if (!strcmp(node->name, "tos")) {
479 if (!strcmp(node->data, "inherit")) {
480 config.flags |= TNL_F_TOS_INHERIT;
482 config.tos = atoi(node->data);
484 } else if (!strcmp(node->name, "ttl")) {
485 if (!strcmp(node->data, "inherit")) {
486 config.flags |= TNL_F_TTL_INHERIT;
488 config.ttl = atoi(node->data);
490 } else if (!strcmp(node->name, "csum") && is_gre) {
491 if (!strcmp(node->data, "true")) {
492 config.flags |= TNL_F_CSUM;
494 } else if (!strcmp(node->name, "pmtud")) {
495 if (!strcmp(node->data, "false")) {
496 config.flags &= ~TNL_F_PMTUD;
498 } else if (!strcmp(node->name, "header_cache")) {
499 if (!strcmp(node->data, "false")) {
500 config.flags &= ~TNL_F_HDR_CACHE;
502 } else if (!strcmp(node->name, "ipsec_local_ip")) {
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_ip_set && 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);