2 * Copyright (c) 2008, 2009, 2010, 2011 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.
26 #include <linux/types.h>
27 #include <linux/ethtool.h>
28 #include <linux/pkt_sched.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/sockios.h>
32 #include <sys/ioctl.h>
36 #include "dpif-provider.h"
38 #include "netdev-vport.h"
41 #include "openvswitch/tunnel.h"
43 #include "poll-loop.h"
44 #include "rtnetlink.h"
45 #include "rtnetlink-link.h"
51 VLOG_DEFINE_THIS_MODULE(dpif_linux);
53 /* Datapath interface for the openvswitch Linux kernel module. */
58 /* Used by dpif_linux_get_all_names(). */
62 /* Change notification. */
63 int local_ifindex; /* Ifindex of local port. */
64 struct shash changed_ports; /* Ports that have changed. */
65 struct rtnetlink_notifier port_notifier;
69 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
71 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
72 static int lookup_minor(const char *name, int *minor);
73 static int finish_open(struct dpif *, const char *local_ifname);
74 static int get_openvswitch_major(void);
75 static int create_minor(const char *name, int minor, struct dpif **dpifp);
76 static int open_minor(int minor, struct dpif **dpifp);
77 static int make_openvswitch_device(int minor, char **fnp);
78 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
81 static struct dpif_linux *
82 dpif_linux_cast(const struct dpif *dpif)
84 dpif_assert_class(dpif, &dpif_linux_class);
85 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
89 dpif_linux_enumerate(struct svec *all_dps)
95 /* Check that the Open vSwitch module is loaded. */
96 major = get_openvswitch_major();
102 for (i = 0; i < ODP_MAX; i++) {
107 sprintf(devname, "dp%d", i);
108 retval = dpif_open(devname, "system", &dpif);
110 svec_add(all_dps, devname);
111 dpif_uninit(dpif, true);
112 } else if (retval != ENODEV && !error) {
120 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
121 bool create, struct dpif **dpifp)
125 minor = !strncmp(name, "dp", 2)
126 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
129 return create_minor(name, minor, dpifp);
131 /* Scan for unused minor number. */
132 for (minor = 0; minor < ODP_MAX; minor++) {
133 int error = create_minor(name, minor, dpifp);
134 if (error != EBUSY) {
139 /* All datapath numbers in use. */
143 struct dpif_linux *dpif;
144 struct odp_port port;
148 error = lookup_minor(name, &minor);
154 error = open_minor(minor, dpifp);
158 dpif = dpif_linux_cast(*dpifp);
160 /* We need the local port's ifindex for the poll function. Start by
161 * getting the local port's name. */
162 memset(&port, 0, sizeof port);
163 port.port = ODPP_LOCAL;
164 if (ioctl(dpif->fd, ODP_VPORT_QUERY, &port)) {
166 if (error != ENODEV) {
167 VLOG_WARN("%s: probe returned unexpected error: %s",
168 dpif_name(*dpifp), strerror(error));
170 dpif_uninit(*dpifp, true);
174 /* Then use that to finish up opening. */
175 return finish_open(&dpif->dpif, port.devname);
180 dpif_linux_close(struct dpif *dpif_)
182 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
183 rtnetlink_link_notifier_unregister(&dpif->port_notifier);
184 shash_destroy(&dpif->changed_ports);
185 free(dpif->local_ifname);
191 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
193 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
195 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
196 svec_add(all_names, dpif->local_ifname);
201 dpif_linux_destroy(struct dpif *dpif_)
203 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
207 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
209 memset(stats, 0, sizeof *stats);
210 return do_ioctl(dpif_, ODP_DP_STATS, stats);
214 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
219 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
221 *drop_fragsp = drop_frags & 1;
227 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
229 int drop_frags_int = drop_frags;
230 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
234 translate_vport_type_to_netdev_type(struct odp_port *port)
236 char *type = port->type;
238 if (!strcmp(type, "netdev")) {
239 ovs_strlcpy(type, "system", sizeof port->type);
240 } else if (!strcmp(type, "gre")) {
241 const struct tnl_port_config *config;
243 config = (struct tnl_port_config *)port->config;
244 if (config->flags & TNL_F_IPSEC) {
245 ovs_strlcpy(type, "ipsec_gre", sizeof port->type);
251 translate_netdev_type_to_vport_type(struct odp_port *port)
253 char *type = port->type;
255 if (!strcmp(type, "system")) {
256 ovs_strlcpy(type, "netdev", sizeof port->type);
257 } else if (!strcmp(type, "ipsec_gre")) {
258 ovs_strlcpy(type, "gre", sizeof port->type);
263 dpif_linux_port_add(struct dpif *dpif, struct netdev *netdev,
266 const char *name = netdev_get_name(netdev);
267 const char *type = netdev_get_type(netdev);
268 struct odp_port port;
271 memset(&port, 0, sizeof port);
272 strncpy(port.devname, name, sizeof port.devname);
273 strncpy(port.type, type, sizeof port.type);
274 netdev_vport_get_config(netdev, port.config);
275 translate_netdev_type_to_vport_type(&port);
277 error = do_ioctl(dpif, ODP_VPORT_ATTACH, &port);
279 *port_nop = port.port;
286 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no_)
288 int port_no = port_no_; /* Kernel expects an "int". */
289 return do_ioctl(dpif_, ODP_VPORT_DETACH, &port_no);
293 dpif_linux_port_query__(const struct dpif *dpif, struct odp_port *port)
295 int error = do_ioctl(dpif, ODP_VPORT_QUERY, port);
297 translate_vport_type_to_netdev_type(port);
303 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
304 struct odp_port *port)
306 memset(port, 0, sizeof *port);
307 port->port = port_no;
308 return dpif_linux_port_query__(dpif, port);
312 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
313 struct odp_port *port)
315 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
318 memset(port, 0, sizeof *port);
319 strncpy(port->devname, devname, sizeof port->devname);
320 error = dpif_linux_port_query__(dpif_, port);
321 if (!error && port->dp_idx != dpif->minor) {
322 /* A vport named 'devname' exists but in some other datapath. */
329 dpif_linux_flow_flush(struct dpif *dpif_)
331 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
335 dpif_linux_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
337 *statep = xzalloc(sizeof(struct odp_vport_dump));
342 dpif_linux_port_dump_next(const struct dpif *dpif, void *state,
343 struct odp_port *port)
345 struct odp_vport_dump *dump = state;
349 error = do_ioctl(dpif, ODP_VPORT_DUMP, dump);
352 } else if (port->devname[0] == '\0') {
355 dump->port_no = port->port + 1;
356 translate_vport_type_to_netdev_type(port);
362 dpif_linux_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
369 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
371 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
373 if (dpif->change_error) {
374 dpif->change_error = false;
375 shash_clear(&dpif->changed_ports);
377 } else if (!shash_is_empty(&dpif->changed_ports)) {
378 struct shash_node *node = shash_first(&dpif->changed_ports);
379 *devnamep = shash_steal(&dpif->changed_ports, node);
387 dpif_linux_port_poll_wait(const struct dpif *dpif_)
389 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
390 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
391 poll_immediate_wake();
393 rtnetlink_link_notifier_wait();
398 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
400 struct odp_flowvec fv;
403 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
407 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
409 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
413 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
415 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
419 dpif_linux_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
421 *statep = xzalloc(sizeof(struct odp_flow_dump));
426 dpif_linux_flow_dump_next(const struct dpif *dpif, void *state,
427 struct odp_flow *flow)
429 struct odp_flow_dump *dump = state;
433 error = do_ioctl(dpif, ODP_FLOW_DUMP, dump);
434 return error ? error : flow->flags & ODPFF_EOF ? EOF : 0;
438 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
445 dpif_linux_execute(struct dpif *dpif_,
446 const struct nlattr *actions, size_t actions_len,
447 const struct ofpbuf *buf)
449 struct odp_execute execute;
450 memset(&execute, 0, sizeof execute);
451 execute.actions = (struct nlattr *) actions;
452 execute.actions_len = actions_len;
453 execute.data = buf->data;
454 execute.length = buf->size;
455 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
459 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
461 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
465 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
467 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
471 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
472 uint32_t *probability)
474 return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
478 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
480 return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
484 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
485 uint32_t queue_id, uint32_t *priority)
487 if (queue_id < 0xf000) {
488 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
496 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall)
498 static const struct nl_policy odp_packet_policy[] = {
499 /* Always present. */
500 [ODP_PACKET_ATTR_TYPE] = { .type = NL_A_U32 },
501 [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
502 .min_len = ETH_HEADER_LEN },
503 [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
505 /* _ODPL_ACTION_NR only. */
506 [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
508 /* _ODPL_SFLOW_NR only. */
509 [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
510 [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
513 struct odp_packet *odp_packet = buf->data;
514 struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
516 if (!nl_policy_parse(buf, sizeof *odp_packet, odp_packet_policy,
517 a, ARRAY_SIZE(odp_packet_policy))) {
521 memset(upcall, 0, sizeof *upcall);
522 upcall->type = nl_attr_get_u32(a[ODP_PACKET_ATTR_TYPE]);
523 upcall->packet = buf;
524 upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
525 upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
526 upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
527 upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
528 upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
529 ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
531 upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
532 ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
534 if (a[ODP_PACKET_ATTR_ACTIONS]) {
535 upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
536 upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
543 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
545 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
550 buf = ofpbuf_new(65536);
551 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
554 if (error != EAGAIN) {
555 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
556 dpif_name(dpif_), strerror(error));
558 } else if (retval >= sizeof(struct odp_packet)) {
559 struct odp_packet *odp_packet = buf->data;
562 if (odp_packet->len <= retval) {
563 error = parse_odp_packet(buf, upcall);
565 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
566 "from %"PRIu32" bytes to %d",
567 dpif_name(dpif_), odp_packet->len, retval);
570 } else if (!retval) {
571 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
574 VLOG_WARN_RL(&error_rl, "%s: discarding too-short message (%d bytes)",
575 dpif_name(dpif_), retval);
586 dpif_linux_recv_wait(struct dpif *dpif_)
588 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
589 poll_fd_wait(dpif->fd, POLLIN);
592 const struct dpif_class dpif_linux_class = {
596 dpif_linux_enumerate,
599 dpif_linux_get_all_names,
601 dpif_linux_get_stats,
602 dpif_linux_get_drop_frags,
603 dpif_linux_set_drop_frags,
606 dpif_linux_port_query_by_number,
607 dpif_linux_port_query_by_name,
608 dpif_linux_port_dump_start,
609 dpif_linux_port_dump_next,
610 dpif_linux_port_dump_done,
611 dpif_linux_port_poll,
612 dpif_linux_port_poll_wait,
616 dpif_linux_flow_flush,
617 dpif_linux_flow_dump_start,
618 dpif_linux_flow_dump_next,
619 dpif_linux_flow_dump_done,
621 dpif_linux_recv_get_mask,
622 dpif_linux_recv_set_mask,
623 dpif_linux_get_sflow_probability,
624 dpif_linux_set_sflow_probability,
625 dpif_linux_queue_to_priority,
627 dpif_linux_recv_wait,
630 static int get_openvswitch_major(void);
631 static int get_major(const char *target);
634 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
636 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
637 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
641 lookup_minor(const char *name, int *minorp)
643 struct ethtool_drvinfo drvinfo;
649 sock = socket(AF_INET, SOCK_DGRAM, 0);
651 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
656 memset(&ifr, 0, sizeof ifr);
657 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
658 ifr.ifr_data = (caddr_t) &drvinfo;
660 memset(&drvinfo, 0, sizeof drvinfo);
661 drvinfo.cmd = ETHTOOL_GDRVINFO;
662 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
663 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
665 goto error_close_sock;
668 if (strcmp(drvinfo.driver, "openvswitch")) {
669 VLOG_WARN("%s is not an openvswitch device", name);
671 goto error_close_sock;
674 if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
675 VLOG_WARN("%s ethtool bus_info has unexpected format", name);
677 goto error_close_sock;
678 } else if (port_no != ODPP_LOCAL) {
679 /* This is an Open vSwitch device but not the local port. We
680 * intentionally support only using the name of the local port as the
681 * name of a datapath; otherwise, it would be too difficult to
682 * enumerate all the names of a datapath. */
684 goto error_close_sock;
698 make_openvswitch_device(int minor, char **fnp)
700 const char dirname[] = "/dev/net";
708 major = get_openvswitch_major();
712 dev = makedev(major, minor);
714 sprintf(fn, "%s/dp%d", dirname, minor);
716 if (!S_ISCHR(s.st_mode)) {
717 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
719 } else if (s.st_rdev != dev) {
720 VLOG_WARN_RL(&error_rl,
721 "%s is device %u:%u but should be %u:%u, fixing",
722 fn, major(s.st_rdev), minor(s.st_rdev),
723 major(dev), minor(dev));
728 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
729 fn, strerror(errno));
732 } else if (errno == ENOENT) {
733 if (stat(dirname, &s)) {
734 if (errno == ENOENT) {
735 if (mkdir(dirname, 0755)) {
736 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
737 dirname, strerror(errno));
741 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
742 dirname, strerror(errno));
747 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
751 /* The device needs to be created. */
752 if (mknod(fn, S_IFCHR | 0700, dev)) {
753 VLOG_WARN_RL(&error_rl,
754 "%s: creating character device %u:%u failed (%s)",
755 fn, major(dev), minor(dev), strerror(errno));
764 /* Return the major device number of the Open vSwitch device. If it
765 * cannot be determined, a negative errno is returned. */
767 get_openvswitch_major(void)
769 static int openvswitch_major = -1;
770 if (openvswitch_major < 0) {
771 openvswitch_major = get_major("openvswitch");
773 return openvswitch_major;
777 get_major(const char *target)
779 const char fn[] = "/proc/devices";
784 file = fopen(fn, "r");
786 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
790 for (ln = 1; fgets(line, sizeof line, file); ln++) {
794 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
796 } else if (!strncmp(line, "Block", 5)) {
797 /* We only want character devices, so skip the rest of the file. */
799 } else if (sscanf(line, "%d %63s", &major, name)) {
800 if (!strcmp(name, target)) {
805 VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
811 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
816 finish_open(struct dpif *dpif_, const char *local_ifname)
818 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
819 dpif->local_ifname = xstrdup(local_ifname);
820 dpif->local_ifindex = if_nametoindex(local_ifname);
821 if (!dpif->local_ifindex) {
823 dpif_uninit(dpif_, true);
824 VLOG_WARN("could not get ifindex of %s device: %s",
825 local_ifname, strerror(errno));
832 create_minor(const char *name, int minor, struct dpif **dpifp)
834 int error = open_minor(minor, dpifp);
836 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
838 error = finish_open(*dpifp, name);
840 dpif_uninit(*dpifp, true);
847 open_minor(int minor, struct dpif **dpifp)
853 error = make_openvswitch_device(minor, &fn);
858 fd = open(fn, O_RDONLY | O_NONBLOCK);
860 struct dpif_linux *dpif = xmalloc(sizeof *dpif);
861 error = rtnetlink_link_notifier_register(&dpif->port_notifier,
862 dpif_linux_port_changed,
867 name = xasprintf("dp%d", minor);
868 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
872 dpif->local_ifname = NULL;
874 dpif->local_ifindex = 0;
875 shash_init(&dpif->changed_ports);
876 dpif->change_error = false;
877 *dpifp = &dpif->dpif;
883 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
891 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
894 struct dpif_linux *dpif = dpif_;
897 if (change->master_ifindex == dpif->local_ifindex
898 && (change->nlmsg_type == RTM_NEWLINK
899 || change->nlmsg_type == RTM_DELLINK))
901 /* Our datapath changed, either adding a new port or deleting an
903 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
906 dpif->change_error = true;