2 * Copyright (c) 2008, 2009 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/ethtool.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/sockios.h>
30 #include <sys/ioctl.h>
33 #include "dpif-provider.h"
34 #include "netdev-linux.h"
36 #include "poll-loop.h"
41 #define THIS_MODULE VLM_dpif_linux
43 /* Datapath interface for the openvswitch Linux kernel module. */
48 /* Used by dpif_linux_get_all_names(). */
52 /* Change notification. */
53 int local_ifindex; /* Ifindex of local port. */
54 struct svec changed_ports; /* Ports that have changed. */
55 struct linux_netdev_notifier port_notifier;
58 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
60 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
61 static int lookup_minor(const char *name, int *minor);
62 static int finish_open(struct dpif *, const char *local_ifname);
63 static int create_minor(const char *name, int minor, struct dpif **dpifp);
64 static int open_minor(int minor, struct dpif **dpifp);
65 static int make_openvswitch_device(int minor, char **fnp);
66 static void dpif_linux_port_changed(const struct linux_netdev_change *,
69 static struct dpif_linux *
70 dpif_linux_cast(const struct dpif *dpif)
72 dpif_assert_class(dpif, &dpif_linux_class);
73 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
79 linux_netdev_notifier_run();
85 linux_netdev_notifier_wait();
89 dpif_linux_enumerate(struct svec *all_dps)
95 for (i = 0; i < ODP_MAX; i++) {
100 sprintf(devname, "dp%d", i);
101 retval = dpif_open(devname, &dpif);
103 svec_add(all_dps, devname);
105 } else if (retval != ENODEV && !error) {
113 dpif_linux_open(const char *name UNUSED, char *suffix, bool create,
118 minor = !strncmp(name, "dp", 2) && isdigit(name[2]) ? atoi(name + 2) : -1;
121 return create_minor(suffix, minor, dpifp);
123 /* Scan for unused minor number. */
124 for (minor = 0; minor < ODP_MAX; minor++) {
125 int error = create_minor(suffix, minor, dpifp);
126 if (error != EBUSY) {
131 /* All datapath numbers in use. */
135 struct dpif_linux *dpif;
136 struct odp_port port;
140 error = lookup_minor(suffix, &minor);
146 error = open_minor(minor, dpifp);
150 dpif = dpif_linux_cast(*dpifp);
152 /* We need the local port's ifindex for the poll function. Start by
153 * getting the local port's name. */
154 memset(&port, 0, sizeof port);
155 port.port = ODPP_LOCAL;
156 if (ioctl(dpif->fd, ODP_PORT_QUERY, &port)) {
158 if (error != ENODEV) {
159 VLOG_WARN("%s: probe returned unexpected error: %s",
160 dpif_name(*dpifp), strerror(error));
166 /* Then use that to finish up opening. */
167 return finish_open(&dpif->dpif, port.devname);
172 dpif_linux_close(struct dpif *dpif_)
174 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
175 linux_netdev_notifier_unregister(&dpif->port_notifier);
176 svec_destroy(&dpif->changed_ports);
177 free(dpif->local_ifname);
183 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
185 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
187 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
188 svec_add(all_names, dpif->local_ifname);
193 dpif_linux_delete(struct dpif *dpif_)
195 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
199 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
201 return do_ioctl(dpif_, ODP_DP_STATS, stats);
205 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
210 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
212 *drop_fragsp = drop_frags & 1;
218 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
220 int drop_frags_int = drop_frags;
221 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
225 dpif_linux_port_add(struct dpif *dpif_, const char *devname, uint16_t flags,
228 struct odp_port port;
231 memset(&port, 0, sizeof port);
232 strncpy(port.devname, devname, sizeof port.devname);
234 error = do_ioctl(dpif_, ODP_PORT_ADD, &port);
236 *port_no = port.port;
242 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
245 return do_ioctl(dpif_, ODP_PORT_DEL, &tmp);
249 dpif_linux_port_query_by_number(const struct dpif *dpif_, uint16_t port_no,
250 struct odp_port *port)
252 memset(port, 0, sizeof *port);
253 port->port = port_no;
254 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
258 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
259 struct odp_port *port)
261 memset(port, 0, sizeof *port);
262 strncpy(port->devname, devname, sizeof port->devname);
263 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
267 dpif_linux_flow_flush(struct dpif *dpif_)
269 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
273 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
275 struct odp_portvec pv;
280 error = do_ioctl(dpif_, ODP_PORT_LIST, &pv);
281 return error ? -error : pv.n_ports;
285 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
287 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
290 error = linux_netdev_notifier_get_error(&dpif->port_notifier);
292 if (!dpif->changed_ports.n) {
295 *devnamep = dpif->changed_ports.names[--dpif->changed_ports.n];
297 svec_clear(&dpif->changed_ports);
303 dpif_linux_port_poll_wait(const struct dpif *dpif_)
305 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
306 if (dpif->changed_ports.n
307 || linux_netdev_notifier_peek_error(&dpif->port_notifier)) {
308 poll_immediate_wake();
310 linux_netdev_notifier_wait();
315 dpif_linux_port_group_get(const struct dpif *dpif_, int group,
316 uint16_t ports[], int n)
318 struct odp_port_group pg;
321 assert(n <= UINT16_MAX);
325 error = do_ioctl(dpif_, ODP_PORT_GROUP_GET, &pg);
326 return error ? -error : pg.n_ports;
330 dpif_linux_port_group_set(struct dpif *dpif_, int group,
331 const uint16_t ports[], int n)
333 struct odp_port_group pg;
335 assert(n <= UINT16_MAX);
337 pg.ports = (uint16_t *) ports;
339 return do_ioctl(dpif_, ODP_PORT_GROUP_SET, &pg);
343 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
345 struct odp_flowvec fv;
348 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
352 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
354 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
358 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
360 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
364 dpif_linux_flow_list(const struct dpif *dpif_, struct odp_flow flows[], int n)
366 struct odp_flowvec fv;
371 error = do_ioctl(dpif_, ODP_FLOW_LIST, &fv);
372 return error ? -error : fv.n_flows;
376 dpif_linux_execute(struct dpif *dpif_, uint16_t in_port,
377 const union odp_action actions[], int n_actions,
378 const struct ofpbuf *buf)
380 struct odp_execute execute;
381 memset(&execute, 0, sizeof execute);
382 execute.in_port = in_port;
383 execute.actions = (union odp_action *) actions;
384 execute.n_actions = n_actions;
385 execute.data = buf->data;
386 execute.length = buf->size;
387 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
391 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
393 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
397 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
399 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
403 dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
405 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
410 buf = ofpbuf_new(65536);
411 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
414 if (error != EAGAIN) {
415 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
416 dpif_name(dpif_), strerror(error));
418 } else if (retval >= sizeof(struct odp_msg)) {
419 struct odp_msg *msg = buf->data;
420 if (msg->length <= retval) {
425 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
426 "from %zu bytes to %d",
427 dpif_name(dpif_), msg->length, retval);
430 } else if (!retval) {
431 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
434 VLOG_WARN_RL(&error_rl,
435 "%s: discarding too-short message (%d bytes)",
436 dpif_name(dpif_), retval);
446 dpif_linux_recv_wait(struct dpif *dpif_)
448 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
449 poll_fd_wait(dpif->fd, POLLIN);
452 const struct dpif_class dpif_linux_class = {
453 "", /* This is the default class. */
457 dpif_linux_enumerate,
460 dpif_linux_get_all_names,
462 dpif_linux_get_stats,
463 dpif_linux_get_drop_frags,
464 dpif_linux_set_drop_frags,
467 dpif_linux_port_query_by_number,
468 dpif_linux_port_query_by_name,
469 dpif_linux_port_list,
470 dpif_linux_port_poll,
471 dpif_linux_port_poll_wait,
472 dpif_linux_port_group_get,
473 dpif_linux_port_group_set,
477 dpif_linux_flow_flush,
478 dpif_linux_flow_list,
480 dpif_linux_recv_get_mask,
481 dpif_linux_recv_set_mask,
483 dpif_linux_recv_wait,
486 static int get_openvswitch_major(void);
487 static int get_major(const char *target, int default_major);
490 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
492 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
493 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
497 lookup_minor(const char *name, int *minorp)
499 struct ethtool_drvinfo drvinfo;
505 sock = socket(AF_INET, SOCK_DGRAM, 0);
507 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
512 memset(&ifr, 0, sizeof ifr);
513 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
514 ifr.ifr_data = (caddr_t) &drvinfo;
516 memset(&drvinfo, 0, sizeof drvinfo);
517 drvinfo.cmd = ETHTOOL_GDRVINFO;
518 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
519 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
521 goto error_close_sock;
524 if (strcmp(drvinfo.driver, "openvswitch")) {
525 VLOG_WARN("%s is not an openvswitch device", name);
527 goto error_close_sock;
530 if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
531 VLOG_WARN("%s ethtool bus_info has unexpected format", name);
533 goto error_close_sock;
534 } else if (port_no != ODPP_LOCAL) {
535 /* This is an Open vSwitch device but not the local port. We
536 * intentionally support only using the name of the local port as the
537 * name of a datapath; otherwise, it would be too difficult to
538 * enumerate all the names of a datapath. */
540 goto error_close_sock;
554 make_openvswitch_device(int minor, char **fnp)
556 dev_t dev = makedev(get_openvswitch_major(), minor);
557 const char dirname[] = "/dev/net";
562 sprintf(fn, "%s/dp%d", dirname, minor);
564 if (!S_ISCHR(s.st_mode)) {
565 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
567 } else if (s.st_rdev != dev) {
568 VLOG_WARN_RL(&error_rl,
569 "%s is device %u:%u instead of %u:%u, fixing",
570 fn, major(s.st_rdev), minor(s.st_rdev),
571 major(dev), minor(dev));
576 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
577 fn, strerror(errno));
580 } else if (errno == ENOENT) {
581 if (stat(dirname, &s)) {
582 if (errno == ENOENT) {
583 if (mkdir(dirname, 0755)) {
584 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
585 dirname, strerror(errno));
589 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
590 dirname, strerror(errno));
595 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
599 /* The device needs to be created. */
600 if (mknod(fn, S_IFCHR | 0700, dev)) {
601 VLOG_WARN_RL(&error_rl,
602 "%s: creating character device %u:%u failed (%s)",
603 fn, major(dev), minor(dev), strerror(errno));
614 get_openvswitch_major(void)
616 static unsigned int openvswitch_major;
617 if (!openvswitch_major) {
618 enum { DEFAULT_MAJOR = 248 };
619 openvswitch_major = get_major("openvswitch", DEFAULT_MAJOR);
621 return openvswitch_major;
625 get_major(const char *target, int default_major)
627 const char fn[] = "/proc/devices";
632 file = fopen(fn, "r");
634 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
638 for (ln = 1; fgets(line, sizeof line, file); ln++) {
642 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
644 } else if (!strncmp(line, "Block", 5)) {
645 /* We only want character devices, so skip the rest of the file. */
647 } else if (sscanf(line, "%d %63s", &major, name)) {
648 if (!strcmp(name, target)) {
655 VLOG_WARN("%s:%d: syntax error", fn, ln);
661 VLOG_ERR("%s: %s major not found (is the module loaded?), using "
662 "default major %d", fn, target, default_major);
664 VLOG_INFO("using default major %d for %s", default_major, target);
665 return default_major;
669 finish_open(struct dpif *dpif_, const char *local_ifname)
671 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
672 dpif->local_ifname = strdup(local_ifname);
673 dpif->local_ifindex = if_nametoindex(local_ifname);
674 if (!dpif->local_ifindex) {
677 VLOG_WARN("could not get ifindex of %s device: %s",
678 local_ifname, strerror(errno));
685 create_minor(const char *name, int minor, struct dpif **dpifp)
687 int error = open_minor(minor, dpifp);
689 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
691 error = finish_open(*dpifp, name);
700 open_minor(int minor, struct dpif **dpifp)
706 error = make_openvswitch_device(minor, &fn);
711 fd = open(fn, O_RDONLY | O_NONBLOCK);
713 struct dpif_linux *dpif = xmalloc(sizeof *dpif);
714 error = linux_netdev_notifier_register(&dpif->port_notifier,
715 dpif_linux_port_changed, dpif);
719 name = xasprintf("dp%d", minor);
720 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
724 dpif->local_ifname = NULL;
726 dpif->local_ifindex = 0;
727 svec_init(&dpif->changed_ports);
728 *dpifp = &dpif->dpif;
734 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
742 dpif_linux_port_changed(const struct linux_netdev_change *change, void *dpif_)
744 struct dpif_linux *dpif = dpif_;
746 if (change->master_ifindex == dpif->local_ifindex
747 && (change->nlmsg_type == RTM_NEWLINK
748 || change->nlmsg_type == RTM_DELLINK))
750 /* Our datapath changed, either adding a new port or deleting an
752 if (!svec_contains(&dpif->changed_ports, change->ifname)) {
753 svec_add(&dpif->changed_ports, change->ifname);
754 svec_sort(&dpif->changed_ports);