2 * Copyright (c) 2008, 2009, 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.
26 #include <linux/types.h>
27 #include <linux/ethtool.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/sockios.h>
31 #include <sys/ioctl.h>
35 #include "dpif-provider.h"
38 #include "poll-loop.h"
39 #include "rtnetlink.h"
45 #define THIS_MODULE VLM_dpif_linux
47 /* Datapath interface for the openvswitch Linux kernel module. */
52 /* Used by dpif_linux_get_all_names(). */
56 /* Change notification. */
57 int local_ifindex; /* Ifindex of local port. */
58 struct shash changed_ports; /* Ports that have changed. */
59 struct rtnetlink_notifier port_notifier;
63 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
65 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
66 static int lookup_minor(const char *name, int *minor);
67 static int finish_open(struct dpif *, const char *local_ifname);
68 static int get_openvswitch_major(void);
69 static int create_minor(const char *name, int minor, struct dpif **dpifp);
70 static int open_minor(int minor, struct dpif **dpifp);
71 static int make_openvswitch_device(int minor, char **fnp);
72 static void dpif_linux_port_changed(const struct rtnetlink_change *,
75 static struct dpif_linux *
76 dpif_linux_cast(const struct dpif *dpif)
78 dpif_assert_class(dpif, &dpif_linux_class);
79 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
83 dpif_linux_enumerate(struct svec *all_dps)
89 /* Check that the Open vSwitch module is loaded. */
90 major = get_openvswitch_major();
96 for (i = 0; i < ODP_MAX; i++) {
101 sprintf(devname, "dp%d", i);
102 retval = dpif_open(devname, "system", &dpif);
104 svec_add(all_dps, devname);
105 dpif_uninit(dpif, true);
106 } else if (retval != ENODEV && !error) {
114 dpif_linux_open(const char *name, const char *type OVS_UNUSED, bool create,
119 minor = !strncmp(name, "dp", 2)
120 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
123 return create_minor(name, minor, dpifp);
125 /* Scan for unused minor number. */
126 for (minor = 0; minor < ODP_MAX; minor++) {
127 int error = create_minor(name, minor, dpifp);
128 if (error != EBUSY) {
133 /* All datapath numbers in use. */
137 struct dpif_linux *dpif;
138 struct odp_port port;
142 error = lookup_minor(name, &minor);
148 error = open_minor(minor, dpifp);
152 dpif = dpif_linux_cast(*dpifp);
154 /* We need the local port's ifindex for the poll function. Start by
155 * getting the local port's name. */
156 memset(&port, 0, sizeof port);
157 port.port = ODPP_LOCAL;
158 if (ioctl(dpif->fd, ODP_PORT_QUERY, &port)) {
160 if (error != ENODEV) {
161 VLOG_WARN("%s: probe returned unexpected error: %s",
162 dpif_name(*dpifp), strerror(error));
164 dpif_uninit(*dpifp, true);
168 /* Then use that to finish up opening. */
169 return finish_open(&dpif->dpif, port.devname);
174 dpif_linux_close(struct dpif *dpif_)
176 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
177 rtnetlink_notifier_unregister(&dpif->port_notifier);
178 shash_destroy(&dpif->changed_ports);
179 free(dpif->local_ifname);
185 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
187 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
189 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
190 svec_add(all_names, dpif->local_ifname);
195 dpif_linux_destroy(struct dpif *dpif_)
197 struct odp_port *ports;
202 err = dpif_port_list(dpif_, &ports, &n_ports);
207 for (i = 0; i < n_ports; i++) {
208 if (ports[i].port != ODPP_LOCAL) {
209 err = do_ioctl(dpif_, ODP_VPORT_DEL, ports[i].devname);
211 VLOG_WARN_RL(&error_rl, "%s: error deleting port %s (%s)",
212 dpif_name(dpif_), ports[i].devname, strerror(err));
219 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
223 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
225 memset(stats, 0, sizeof *stats);
226 return do_ioctl(dpif_, ODP_DP_STATS, stats);
230 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
235 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
237 *drop_fragsp = drop_frags & 1;
243 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
245 int drop_frags_int = drop_frags;
246 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
250 dpif_linux_port_add(struct dpif *dpif_, const char *devname, uint16_t flags,
253 struct odp_port port;
256 memset(&port, 0, sizeof port);
257 strncpy(port.devname, devname, sizeof port.devname);
259 error = do_ioctl(dpif_, ODP_PORT_ATTACH, &port);
261 *port_no = port.port;
267 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
271 struct odp_port port;
273 err = dpif_port_query_by_number(dpif_, port_no, &port);
278 err = do_ioctl(dpif_, ODP_PORT_DETACH, &tmp);
283 if (!netdev_is_open(port.devname)) {
284 /* Try deleting the port if no one has it open. This shouldn't
285 * actually be necessary unless the config changed while we weren't
286 * running but it won't hurt anything if the port is already gone. */
287 do_ioctl(dpif_, ODP_VPORT_DEL, port.devname);
294 dpif_linux_port_query_by_number(const struct dpif *dpif_, uint16_t port_no,
295 struct odp_port *port)
297 memset(port, 0, sizeof *port);
298 port->port = port_no;
299 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
303 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
304 struct odp_port *port)
306 memset(port, 0, sizeof *port);
307 strncpy(port->devname, devname, sizeof port->devname);
308 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
312 dpif_linux_flow_flush(struct dpif *dpif_)
314 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
318 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
320 struct odp_portvec pv;
325 error = do_ioctl(dpif_, ODP_PORT_LIST, &pv);
326 return error ? -error : pv.n_ports;
330 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
332 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
334 if (dpif->change_error) {
335 dpif->change_error = false;
336 shash_clear(&dpif->changed_ports);
338 } else if (!shash_is_empty(&dpif->changed_ports)) {
339 struct shash_node *node = shash_first(&dpif->changed_ports);
340 *devnamep = xstrdup(node->name);
341 shash_delete(&dpif->changed_ports, node);
349 dpif_linux_port_poll_wait(const struct dpif *dpif_)
351 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
352 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
353 poll_immediate_wake();
355 rtnetlink_notifier_wait();
360 dpif_linux_port_group_get(const struct dpif *dpif_, int group,
361 uint16_t ports[], int n)
363 struct odp_port_group pg;
366 assert(n <= UINT16_MAX);
370 error = do_ioctl(dpif_, ODP_PORT_GROUP_GET, &pg);
371 return error ? -error : pg.n_ports;
375 dpif_linux_port_group_set(struct dpif *dpif_, int group,
376 const uint16_t ports[], int n)
378 struct odp_port_group pg;
380 assert(n <= UINT16_MAX);
382 pg.ports = (uint16_t *) ports;
384 return do_ioctl(dpif_, ODP_PORT_GROUP_SET, &pg);
388 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
390 struct odp_flowvec fv;
393 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
397 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
399 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
403 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
405 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
409 dpif_linux_flow_list(const struct dpif *dpif_, struct odp_flow flows[], int n)
411 struct odp_flowvec fv;
416 error = do_ioctl(dpif_, ODP_FLOW_LIST, &fv);
417 return error ? -error : fv.n_flows;
421 dpif_linux_execute(struct dpif *dpif_, uint16_t in_port,
422 const union odp_action actions[], int n_actions,
423 const struct ofpbuf *buf)
425 struct odp_execute execute;
426 memset(&execute, 0, sizeof execute);
427 execute.in_port = in_port;
428 execute.actions = (union odp_action *) actions;
429 execute.n_actions = n_actions;
430 execute.data = buf->data;
431 execute.length = buf->size;
432 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
436 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
438 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
442 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
444 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
448 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
449 uint32_t *probability)
451 return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
455 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
457 return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
461 dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
463 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
468 buf = ofpbuf_new(65536 + DPIF_RECV_MSG_PADDING);
469 ofpbuf_reserve(buf, DPIF_RECV_MSG_PADDING);
470 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
473 if (error != EAGAIN) {
474 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
475 dpif_name(dpif_), strerror(error));
477 } else if (retval >= sizeof(struct odp_msg)) {
478 struct odp_msg *msg = buf->data;
479 if (msg->length <= retval) {
484 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
485 "from %"PRIu32" bytes to %d",
486 dpif_name(dpif_), msg->length, retval);
489 } else if (!retval) {
490 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
493 VLOG_WARN_RL(&error_rl,
494 "%s: discarding too-short message (%d bytes)",
495 dpif_name(dpif_), retval);
505 dpif_linux_recv_wait(struct dpif *dpif_)
507 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
508 poll_fd_wait(dpif->fd, POLLIN);
511 const struct dpif_class dpif_linux_class = {
515 dpif_linux_enumerate,
518 dpif_linux_get_all_names,
520 dpif_linux_get_stats,
521 dpif_linux_get_drop_frags,
522 dpif_linux_set_drop_frags,
525 dpif_linux_port_query_by_number,
526 dpif_linux_port_query_by_name,
527 dpif_linux_port_list,
528 dpif_linux_port_poll,
529 dpif_linux_port_poll_wait,
530 dpif_linux_port_group_get,
531 dpif_linux_port_group_set,
535 dpif_linux_flow_flush,
536 dpif_linux_flow_list,
538 dpif_linux_recv_get_mask,
539 dpif_linux_recv_set_mask,
540 dpif_linux_get_sflow_probability,
541 dpif_linux_set_sflow_probability,
543 dpif_linux_recv_wait,
546 static int get_openvswitch_major(void);
547 static int get_major(const char *target);
550 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
552 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
553 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
557 lookup_minor(const char *name, int *minorp)
559 struct ethtool_drvinfo drvinfo;
565 sock = socket(AF_INET, SOCK_DGRAM, 0);
567 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
572 memset(&ifr, 0, sizeof ifr);
573 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
574 ifr.ifr_data = (caddr_t) &drvinfo;
576 memset(&drvinfo, 0, sizeof drvinfo);
577 drvinfo.cmd = ETHTOOL_GDRVINFO;
578 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
579 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
581 goto error_close_sock;
584 if (strcmp(drvinfo.driver, "openvswitch")) {
585 VLOG_WARN("%s is not an openvswitch device", name);
587 goto error_close_sock;
590 if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
591 VLOG_WARN("%s ethtool bus_info has unexpected format", name);
593 goto error_close_sock;
594 } else if (port_no != ODPP_LOCAL) {
595 /* This is an Open vSwitch device but not the local port. We
596 * intentionally support only using the name of the local port as the
597 * name of a datapath; otherwise, it would be too difficult to
598 * enumerate all the names of a datapath. */
600 goto error_close_sock;
614 make_openvswitch_device(int minor, char **fnp)
616 const char dirname[] = "/dev/net";
624 major = get_openvswitch_major();
628 dev = makedev(major, minor);
630 sprintf(fn, "%s/dp%d", dirname, minor);
632 if (!S_ISCHR(s.st_mode)) {
633 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
635 } else if (s.st_rdev != dev) {
636 VLOG_WARN_RL(&error_rl,
637 "%s is device %u:%u but should be %u:%u, fixing",
638 fn, major(s.st_rdev), minor(s.st_rdev),
639 major(dev), minor(dev));
644 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
645 fn, strerror(errno));
648 } else if (errno == ENOENT) {
649 if (stat(dirname, &s)) {
650 if (errno == ENOENT) {
651 if (mkdir(dirname, 0755)) {
652 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
653 dirname, strerror(errno));
657 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
658 dirname, strerror(errno));
663 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
667 /* The device needs to be created. */
668 if (mknod(fn, S_IFCHR | 0700, dev)) {
669 VLOG_WARN_RL(&error_rl,
670 "%s: creating character device %u:%u failed (%s)",
671 fn, major(dev), minor(dev), strerror(errno));
680 /* Return the major device number of the Open vSwitch device. If it
681 * cannot be determined, a negative errno is returned. */
683 get_openvswitch_major(void)
685 static int openvswitch_major = -1;
686 if (openvswitch_major < 0) {
687 openvswitch_major = get_major("openvswitch");
689 return openvswitch_major;
693 get_major(const char *target)
695 const char fn[] = "/proc/devices";
700 file = fopen(fn, "r");
702 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
706 for (ln = 1; fgets(line, sizeof line, file); ln++) {
710 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
712 } else if (!strncmp(line, "Block", 5)) {
713 /* We only want character devices, so skip the rest of the file. */
715 } else if (sscanf(line, "%d %63s", &major, name)) {
716 if (!strcmp(name, target)) {
723 VLOG_WARN("%s:%d: syntax error", fn, ln);
731 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
736 finish_open(struct dpif *dpif_, const char *local_ifname)
738 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
739 dpif->local_ifname = xstrdup(local_ifname);
740 dpif->local_ifindex = if_nametoindex(local_ifname);
741 if (!dpif->local_ifindex) {
743 dpif_uninit(dpif_, true);
744 VLOG_WARN("could not get ifindex of %s device: %s",
745 local_ifname, strerror(errno));
752 create_minor(const char *name, int minor, struct dpif **dpifp)
754 int error = open_minor(minor, dpifp);
756 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
758 error = finish_open(*dpifp, name);
760 dpif_uninit(*dpifp, true);
767 open_minor(int minor, struct dpif **dpifp)
773 error = make_openvswitch_device(minor, &fn);
778 fd = open(fn, O_RDONLY | O_NONBLOCK);
780 struct dpif_linux *dpif = xmalloc(sizeof *dpif);
781 error = rtnetlink_notifier_register(&dpif->port_notifier,
782 dpif_linux_port_changed, dpif);
786 name = xasprintf("dp%d", minor);
787 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
791 dpif->local_ifname = NULL;
793 dpif->local_ifindex = 0;
794 shash_init(&dpif->changed_ports);
795 dpif->change_error = false;
796 *dpifp = &dpif->dpif;
802 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
810 dpif_linux_port_changed(const struct rtnetlink_change *change, void *dpif_)
812 struct dpif_linux *dpif = dpif_;
815 if (change->master_ifindex == dpif->local_ifindex
816 && (change->nlmsg_type == RTM_NEWLINK
817 || change->nlmsg_type == RTM_DELLINK))
819 /* Our datapath changed, either adding a new port or deleting an
821 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
824 dpif->change_error = true;