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;
58 const struct netdev_class netdev_class;
59 int (*parse_config)(struct vport_info *port, const struct shash *args);
62 static struct shash netdev_vport_notifiers =
63 SHASH_INITIALIZER(&netdev_vport_notifiers);
65 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
67 static int netdev_vport_do_ioctl(int cmd, void *arg);
68 static int netdev_vport_create(const struct netdev_class *, const char *,
69 const struct shash *, struct netdev_dev **);
70 static void netdev_vport_poll_notify(const struct netdev *);
73 is_vport_class(const struct netdev_class *class)
75 return class->create == netdev_vport_create;
78 static const struct vport_class *
79 vport_class_cast(const struct netdev_class *class)
81 assert(is_vport_class(class));
82 return CONTAINER_OF(class, struct vport_class, netdev_class);
85 static struct netdev_dev_vport *
86 netdev_dev_vport_cast(const struct netdev_dev *netdev_dev)
88 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
89 return CONTAINER_OF(netdev_dev, struct netdev_dev_vport, netdev_dev);
92 static struct netdev_vport *
93 netdev_vport_cast(const struct netdev *netdev)
95 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
96 assert(is_vport_class(netdev_dev_get_class(netdev_dev)));
97 return CONTAINER_OF(netdev, struct netdev_vport, netdev);
101 netdev_vport_parse_config(const struct netdev_class *netdev_class,
102 const char *name, const struct shash *args,
105 const struct vport_class *c = vport_class_cast(netdev_class);
106 if (c->parse_config) {
107 struct vport_info info;
111 info.type = netdev_class->type;
112 error = (c->parse_config)(&info, args);
113 *configp = info.config;
116 if (!shash_is_empty(args)) {
117 VLOG_WARN("%s: arguments for %s vports should be empty",
118 name, netdev_class->type);
126 netdev_vport_create(const struct netdev_class *class, const char *name,
127 const struct shash *args OVS_UNUSED,
128 struct netdev_dev **netdev_devp)
131 struct odp_vport_add ova;
132 struct netdev_dev_vport *netdev_dev;
134 ovs_strlcpy(ova.port_type, class->type, sizeof ova.port_type);
135 ovs_strlcpy(ova.devname, name, sizeof ova.devname);
136 err = netdev_vport_parse_config(class, name, args, &ova.config);
141 err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
144 VLOG_WARN("%s: destroying existing device", name);
146 err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
151 err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
157 netdev_dev = xmalloc(sizeof *netdev_dev);
158 netdev_dev_init(&netdev_dev->netdev_dev, name, class);
160 *netdev_devp = &netdev_dev->netdev_dev;
168 netdev_vport_destroy(struct netdev_dev *netdev_dev_)
170 struct netdev_dev_vport *netdev_dev = netdev_dev_vport_cast(netdev_dev_);
176 netdev_vport_open(struct netdev_dev *netdev_dev_, int ethertype OVS_UNUSED,
177 struct netdev **netdevp)
179 struct netdev_vport *netdev;
181 netdev = xmalloc(sizeof *netdev);
182 netdev_init(&netdev->netdev, netdev_dev_);
184 *netdevp = &netdev->netdev;
189 netdev_vport_close(struct netdev *netdev_)
191 struct netdev_vport *netdev = netdev_vport_cast(netdev_);
196 netdev_vport_reconfigure(struct netdev_dev *netdev_dev,
197 const struct shash *args)
199 const char *name = netdev_dev_get_name(netdev_dev);
200 struct odp_vport_mod ovm;
203 ovs_strlcpy(ovm.devname, name, sizeof ovm.devname);
204 err = netdev_vport_parse_config(netdev_dev_get_class(netdev_dev), name,
210 err = netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
216 netdev_vport_set_etheraddr(struct netdev *netdev,
217 const uint8_t mac[ETH_ADDR_LEN])
219 struct odp_vport_ether vport_ether;
222 ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
223 sizeof vport_ether.devname);
225 memcpy(vport_ether.ether_addr, mac, ETH_ADDR_LEN);
227 err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_SET, &vport_ether);
232 netdev_vport_poll_notify(netdev);
237 netdev_vport_get_etheraddr(const struct netdev *netdev,
238 uint8_t mac[ETH_ADDR_LEN])
240 struct odp_vport_ether vport_ether;
243 ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
244 sizeof vport_ether.devname);
246 err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_GET, &vport_ether);
251 memcpy(mac, vport_ether.ether_addr, ETH_ADDR_LEN);
256 netdev_vport_get_mtu(const struct netdev *netdev, int *mtup)
258 struct odp_vport_mtu vport_mtu;
261 ovs_strlcpy(vport_mtu.devname, netdev_get_name(netdev),
262 sizeof vport_mtu.devname);
264 err = netdev_vport_do_ioctl(ODP_VPORT_MTU_GET, &vport_mtu);
269 *mtup = vport_mtu.mtu;
274 netdev_vport_get_carrier(const struct netdev *netdev OVS_UNUSED, bool *carrier)
281 netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
283 const char *name = netdev_get_name(netdev);
284 struct odp_vport_stats_req ovsr;
287 ovs_strlcpy(ovsr.devname, name, sizeof ovsr.devname);
288 err = netdev_vport_do_ioctl(ODP_VPORT_STATS_GET, &ovsr);
293 stats->rx_packets = ovsr.stats.rx_packets;
294 stats->tx_packets = ovsr.stats.tx_packets;
295 stats->rx_bytes = ovsr.stats.rx_bytes;
296 stats->tx_bytes = ovsr.stats.tx_bytes;
297 stats->rx_errors = ovsr.stats.rx_errors;
298 stats->tx_errors = ovsr.stats.tx_errors;
299 stats->rx_dropped = ovsr.stats.rx_dropped;
300 stats->tx_dropped = ovsr.stats.tx_dropped;
301 stats->multicast = UINT64_MAX;
302 stats->collisions = ovsr.stats.collisions;
303 stats->rx_length_errors = UINT64_MAX;
304 stats->rx_over_errors = ovsr.stats.rx_over_err;
305 stats->rx_crc_errors = ovsr.stats.rx_crc_err;
306 stats->rx_frame_errors = ovsr.stats.rx_frame_err;
307 stats->rx_fifo_errors = UINT64_MAX;
308 stats->rx_missed_errors = UINT64_MAX;
309 stats->tx_aborted_errors = UINT64_MAX;
310 stats->tx_carrier_errors = UINT64_MAX;
311 stats->tx_fifo_errors = UINT64_MAX;
312 stats->tx_heartbeat_errors = UINT64_MAX;
313 stats->tx_window_errors = UINT64_MAX;
319 netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
321 struct odp_vport_stats_req ovsr;
324 ovs_strlcpy(ovsr.devname, netdev_get_name(netdev), sizeof ovsr.devname);
326 ovsr.stats.rx_packets = stats->rx_packets;
327 ovsr.stats.tx_packets = stats->tx_packets;
328 ovsr.stats.rx_bytes = stats->rx_bytes;
329 ovsr.stats.tx_bytes = stats->tx_bytes;
330 ovsr.stats.rx_errors = stats->rx_errors;
331 ovsr.stats.tx_errors = stats->tx_errors;
332 ovsr.stats.rx_dropped = stats->rx_dropped;
333 ovsr.stats.tx_dropped = stats->tx_dropped;
334 ovsr.stats.collisions = stats->collisions;
335 ovsr.stats.rx_over_err = stats->rx_over_errors;
336 ovsr.stats.rx_crc_err = stats->rx_crc_errors;
337 ovsr.stats.rx_frame_err = stats->rx_frame_errors;
339 err = netdev_vport_do_ioctl(ODP_VPORT_STATS_SET, &ovsr);
341 /* If the vport layer doesn't know about the device, that doesn't mean it
342 * doesn't exist (after all were able to open it when netdev_open() was
343 * called), it just means that it isn't attached and we'll be getting
344 * stats a different way. */
353 netdev_vport_update_flags(struct netdev *netdev OVS_UNUSED,
354 enum netdev_flags off, enum netdev_flags on OVS_UNUSED,
355 enum netdev_flags *old_flagsp)
357 if (off & (NETDEV_UP | NETDEV_PROMISC)) {
361 *old_flagsp = NETDEV_UP | NETDEV_PROMISC;
366 make_poll_name(const struct netdev *netdev)
368 return xasprintf("%s:%s", netdev_get_type(netdev), netdev_get_name(netdev));
372 netdev_vport_poll_add(struct netdev *netdev,
373 void (*cb)(struct netdev_notifier *), void *aux,
374 struct netdev_notifier **notifierp)
376 char *poll_name = make_poll_name(netdev);
377 struct netdev_vport_notifier *notifier;
379 struct shash_node *shash_node;
381 shash_node = shash_find_data(&netdev_vport_notifiers, poll_name);
383 list = xmalloc(sizeof *list);
385 shash_node = shash_add(&netdev_vport_notifiers,
386 netdev_get_name(netdev), list);
388 list = shash_node->data;
391 notifier = xmalloc(sizeof *notifier);
392 netdev_notifier_init(¬ifier->notifier, netdev, cb, aux);
393 list_push_back(list, ¬ifier->list_node);
394 notifier->shash_node = shash_node;
396 *notifierp = ¬ifier->notifier;
403 netdev_vport_poll_remove(struct netdev_notifier *notifier_)
405 struct netdev_vport_notifier *notifier =
406 CONTAINER_OF(notifier_, struct netdev_vport_notifier, notifier);
410 list = list_remove(¬ifier->list_node);
411 if (list_is_empty(list)) {
412 shash_delete(&netdev_vport_notifiers, notifier->shash_node);
419 /* Helper functions. */
422 netdev_vport_do_ioctl(int cmd, void *arg)
424 static int ioctl_fd = -1;
427 ioctl_fd = open("/dev/net/dp0", O_RDONLY | O_NONBLOCK);
429 VLOG_ERR_RL(&rl, "failed to open ioctl fd: %s", strerror(errno));
434 return ioctl(ioctl_fd, cmd, arg) ? errno : 0;
438 netdev_vport_poll_notify(const struct netdev *netdev)
440 char *poll_name = make_poll_name(netdev);
441 struct list *list = shash_find_data(&netdev_vport_notifiers,
445 struct netdev_vport_notifier *notifier;
447 LIST_FOR_EACH (notifier, list_node, list) {
448 struct netdev_notifier *n = ¬ifier->notifier;
456 /* Code specific to individual vport types. */
459 parse_tunnel_config(struct vport_info *port, const struct shash *args)
461 const char *name = port->devname;
462 bool is_gre = !strcmp(port->type, "gre");
463 struct tnl_port_config *config;
464 struct shash_node *node;
465 bool ipsec_ip_set = false;
466 bool ipsec_mech_set = false;
468 config = port->config = xzalloc(sizeof *config);
469 config->flags |= TNL_F_PMTUD;
470 config->flags |= TNL_F_HDR_CACHE;
472 SHASH_FOR_EACH (node, args) {
473 if (!strcmp(node->name, "remote_ip")) {
474 struct in_addr in_addr;
475 if (lookup_ip(node->data, &in_addr)) {
476 VLOG_WARN("%s: bad %s 'remote_ip'", name, port->type);
478 config->daddr = in_addr.s_addr;
480 } else if (!strcmp(node->name, "local_ip")) {
481 struct in_addr in_addr;
482 if (lookup_ip(node->data, &in_addr)) {
483 VLOG_WARN("%s: bad %s 'local_ip'", name, port->type);
485 config->saddr = in_addr.s_addr;
487 } else if (!strcmp(node->name, "key") && is_gre) {
488 if (!strcmp(node->data, "flow")) {
489 config->flags |= TNL_F_IN_KEY_MATCH;
490 config->flags |= TNL_F_OUT_KEY_ACTION;
492 config->out_key = config->in_key = htonl(atoi(node->data));
494 } else if (!strcmp(node->name, "in_key") && is_gre) {
495 if (!strcmp(node->data, "flow")) {
496 config->flags |= TNL_F_IN_KEY_MATCH;
498 config->in_key = htonl(atoi(node->data));
500 } else if (!strcmp(node->name, "out_key") && is_gre) {
501 if (!strcmp(node->data, "flow")) {
502 config->flags |= TNL_F_OUT_KEY_ACTION;
504 config->out_key = htonl(atoi(node->data));
506 } else if (!strcmp(node->name, "tos")) {
507 if (!strcmp(node->data, "inherit")) {
508 config->flags |= TNL_F_TOS_INHERIT;
510 config->tos = atoi(node->data);
512 } else if (!strcmp(node->name, "ttl")) {
513 if (!strcmp(node->data, "inherit")) {
514 config->flags |= TNL_F_TTL_INHERIT;
516 config->ttl = atoi(node->data);
518 } else if (!strcmp(node->name, "csum") && is_gre) {
519 if (!strcmp(node->data, "true")) {
520 config->flags |= TNL_F_CSUM;
522 } else if (!strcmp(node->name, "pmtud")) {
523 if (!strcmp(node->data, "false")) {
524 config->flags &= ~TNL_F_PMTUD;
526 } else if (!strcmp(node->name, "header_cache")) {
527 if (!strcmp(node->data, "false")) {
528 config->flags &= ~TNL_F_HDR_CACHE;
530 } else if (!strcmp(node->name, "ipsec_local_ip")) {
532 } else if (!strcmp(node->name, "ipsec_cert")
533 || !strcmp(node->name, "ipsec_psk")) {
534 ipsec_mech_set = true;
536 VLOG_WARN("%s: unknown %s argument '%s'",
537 name, port->type, node->name);
541 /* IPsec doesn't work when header caching is enabled. Disable it if the
542 * IPsec local IP address and authentication mechanism have been defined. */
543 if (ipsec_ip_set && ipsec_mech_set) {
544 VLOG_INFO("%s: header caching disabled due to use of IPsec", name);
545 config->flags &= ~TNL_F_HDR_CACHE;
548 if (!config->daddr) {
549 VLOG_WARN("%s: %s type requires valid 'remote_ip' argument",
558 parse_patch_config(struct vport_info *port, const struct shash *args)
560 const char *name = port->devname;
563 peer = shash_find_data(args, "peer");
565 VLOG_WARN("%s: patch type requires valid 'peer' argument", name);
569 if (shash_count(args) > 1) {
570 VLOG_WARN("%s: patch type takes only a 'peer' argument", name);
574 if (strlen(peer) >= IFNAMSIZ) {
575 VLOG_WARN("%s: patch 'peer' arg too long", name);
579 if (!strcmp(name, peer)) {
580 VLOG_WARN("%s: patch peer must not be self", name);
584 port->config = xstrdup(peer);
589 #define VPORT_FUNCTIONS \
594 netdev_vport_create, \
595 netdev_vport_destroy, \
596 netdev_vport_reconfigure, \
599 netdev_vport_close, \
601 NULL, /* enumerate */ \
604 NULL, /* recv_wait */ \
608 NULL, /* send_wait */ \
610 netdev_vport_set_etheraddr, \
611 netdev_vport_get_etheraddr, \
612 netdev_vport_get_mtu, \
613 NULL, /* get_ifindex */ \
614 netdev_vport_get_carrier, \
615 netdev_vport_get_stats, \
616 netdev_vport_set_stats, \
618 NULL, /* get_features */ \
619 NULL, /* set_advertisements */ \
620 NULL, /* get_vlan_vid */ \
622 NULL, /* set_policing */ \
623 NULL, /* get_qos_types */ \
624 NULL, /* get_qos_capabilities */ \
625 NULL, /* get_qos */ \
626 NULL, /* set_qos */ \
627 NULL, /* get_queue */ \
628 NULL, /* set_queue */ \
629 NULL, /* delete_queue */ \
630 NULL, /* get_queue_stats */ \
631 NULL, /* dump_queues */ \
632 NULL, /* dump_queue_stats */ \
634 NULL, /* get_in4 */ \
635 NULL, /* set_in4 */ \
636 NULL, /* get_in6 */ \
637 NULL, /* add_router */ \
638 NULL, /* get_next_hop */ \
639 NULL, /* arp_lookup */ \
641 netdev_vport_update_flags, \
643 netdev_vport_poll_add, \
644 netdev_vport_poll_remove,
646 static const struct vport_class vport_gre_class
647 = { { "gre", VPORT_FUNCTIONS }, parse_tunnel_config };
649 static const struct vport_class vport_capwap_class
650 = { { "capwap", VPORT_FUNCTIONS }, parse_tunnel_config };
652 static const struct vport_class vport_patch_class
653 = { { "patch", VPORT_FUNCTIONS }, parse_patch_config };
656 netdev_vport_register(void)
658 netdev_register_provider(&vport_gre_class.netdev_class);
659 netdev_register_provider(&vport_capwap_class.netdev_class);
660 netdev_register_provider(&vport_patch_class.netdev_class);