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"
35 #include "poll-loop.h"
36 #include "rtnetlink.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 rtnetlink_notifier port_notifier;
59 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
61 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
62 static int lookup_minor(const char *name, int *minor);
63 static int finish_open(struct dpif *, const char *local_ifname);
64 static int create_minor(const char *name, int minor, struct dpif **dpifp);
65 static int open_minor(int minor, struct dpif **dpifp);
66 static int make_openvswitch_device(int minor, char **fnp);
67 static void dpif_linux_port_changed(const struct rtnetlink_change *,
70 static struct dpif_linux *
71 dpif_linux_cast(const struct dpif *dpif)
73 dpif_assert_class(dpif, &dpif_linux_class);
74 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
78 dpif_linux_enumerate(struct svec *all_dps)
84 for (i = 0; i < ODP_MAX; i++) {
89 sprintf(devname, "dp%d", i);
90 retval = dpif_open(devname, &dpif);
92 svec_add(all_dps, devname);
94 } else if (retval != ENODEV && !error) {
102 dpif_linux_open(const char *name UNUSED, char *suffix, bool create,
107 minor = !strncmp(name, "dp", 2)
108 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
111 return create_minor(suffix, minor, dpifp);
113 /* Scan for unused minor number. */
114 for (minor = 0; minor < ODP_MAX; minor++) {
115 int error = create_minor(suffix, minor, dpifp);
116 if (error != EBUSY) {
121 /* All datapath numbers in use. */
125 struct dpif_linux *dpif;
126 struct odp_port port;
130 error = lookup_minor(suffix, &minor);
136 error = open_minor(minor, dpifp);
140 dpif = dpif_linux_cast(*dpifp);
142 /* We need the local port's ifindex for the poll function. Start by
143 * getting the local port's name. */
144 memset(&port, 0, sizeof port);
145 port.port = ODPP_LOCAL;
146 if (ioctl(dpif->fd, ODP_PORT_QUERY, &port)) {
148 if (error != ENODEV) {
149 VLOG_WARN("%s: probe returned unexpected error: %s",
150 dpif_name(*dpifp), strerror(error));
156 /* Then use that to finish up opening. */
157 return finish_open(&dpif->dpif, port.devname);
162 dpif_linux_close(struct dpif *dpif_)
164 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
165 rtnetlink_notifier_unregister(&dpif->port_notifier);
166 svec_destroy(&dpif->changed_ports);
167 free(dpif->local_ifname);
173 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
175 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
177 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
178 svec_add(all_names, dpif->local_ifname);
183 dpif_linux_delete(struct dpif *dpif_)
185 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
189 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
191 return do_ioctl(dpif_, ODP_DP_STATS, stats);
195 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
200 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
202 *drop_fragsp = drop_frags & 1;
208 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
210 int drop_frags_int = drop_frags;
211 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
215 dpif_linux_port_add(struct dpif *dpif_, const char *devname, uint16_t flags,
218 struct odp_port port;
221 memset(&port, 0, sizeof port);
222 strncpy(port.devname, devname, sizeof port.devname);
224 error = do_ioctl(dpif_, ODP_PORT_ADD, &port);
226 *port_no = port.port;
232 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
235 return do_ioctl(dpif_, ODP_PORT_DEL, &tmp);
239 dpif_linux_port_query_by_number(const struct dpif *dpif_, uint16_t port_no,
240 struct odp_port *port)
242 memset(port, 0, sizeof *port);
243 port->port = port_no;
244 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
248 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
249 struct odp_port *port)
251 memset(port, 0, sizeof *port);
252 strncpy(port->devname, devname, sizeof port->devname);
253 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
257 dpif_linux_flow_flush(struct dpif *dpif_)
259 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
263 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
265 struct odp_portvec pv;
270 error = do_ioctl(dpif_, ODP_PORT_LIST, &pv);
271 return error ? -error : pv.n_ports;
275 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
277 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
279 if (dpif->change_error) {
280 dpif->change_error = false;
281 svec_clear(&dpif->changed_ports);
283 } else if (dpif->changed_ports.n) {
284 *devnamep = dpif->changed_ports.names[--dpif->changed_ports.n];
292 dpif_linux_port_poll_wait(const struct dpif *dpif_)
294 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
295 if (dpif->changed_ports.n || dpif->change_error) {
296 poll_immediate_wake();
298 rtnetlink_notifier_wait();
303 dpif_linux_port_group_get(const struct dpif *dpif_, int group,
304 uint16_t ports[], int n)
306 struct odp_port_group pg;
309 assert(n <= UINT16_MAX);
313 error = do_ioctl(dpif_, ODP_PORT_GROUP_GET, &pg);
314 return error ? -error : pg.n_ports;
318 dpif_linux_port_group_set(struct dpif *dpif_, int group,
319 const uint16_t ports[], int n)
321 struct odp_port_group pg;
323 assert(n <= UINT16_MAX);
325 pg.ports = (uint16_t *) ports;
327 return do_ioctl(dpif_, ODP_PORT_GROUP_SET, &pg);
331 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
333 struct odp_flowvec fv;
336 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
340 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
342 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
346 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
348 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
352 dpif_linux_flow_list(const struct dpif *dpif_, struct odp_flow flows[], int n)
354 struct odp_flowvec fv;
359 error = do_ioctl(dpif_, ODP_FLOW_LIST, &fv);
360 return error ? -error : fv.n_flows;
364 dpif_linux_execute(struct dpif *dpif_, uint16_t in_port,
365 const union odp_action actions[], int n_actions,
366 const struct ofpbuf *buf)
368 struct odp_execute execute;
369 memset(&execute, 0, sizeof execute);
370 execute.in_port = in_port;
371 execute.actions = (union odp_action *) actions;
372 execute.n_actions = n_actions;
373 execute.data = buf->data;
374 execute.length = buf->size;
375 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
379 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
381 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
385 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
387 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
391 dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
393 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
398 buf = ofpbuf_new(65536);
399 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
402 if (error != EAGAIN) {
403 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
404 dpif_name(dpif_), strerror(error));
406 } else if (retval >= sizeof(struct odp_msg)) {
407 struct odp_msg *msg = buf->data;
408 if (msg->length <= retval) {
413 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
414 "from %zu bytes to %d",
415 dpif_name(dpif_), msg->length, retval);
418 } else if (!retval) {
419 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
422 VLOG_WARN_RL(&error_rl,
423 "%s: discarding too-short message (%d bytes)",
424 dpif_name(dpif_), retval);
434 dpif_linux_recv_wait(struct dpif *dpif_)
436 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
437 poll_fd_wait(dpif->fd, POLLIN);
440 const struct dpif_class dpif_linux_class = {
441 "", /* This is the default class. */
445 dpif_linux_enumerate,
448 dpif_linux_get_all_names,
450 dpif_linux_get_stats,
451 dpif_linux_get_drop_frags,
452 dpif_linux_set_drop_frags,
455 dpif_linux_port_query_by_number,
456 dpif_linux_port_query_by_name,
457 dpif_linux_port_list,
458 dpif_linux_port_poll,
459 dpif_linux_port_poll_wait,
460 dpif_linux_port_group_get,
461 dpif_linux_port_group_set,
465 dpif_linux_flow_flush,
466 dpif_linux_flow_list,
468 dpif_linux_recv_get_mask,
469 dpif_linux_recv_set_mask,
471 dpif_linux_recv_wait,
474 static int get_openvswitch_major(void);
475 static int get_major(const char *target, int default_major);
478 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
480 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
481 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
485 lookup_minor(const char *name, int *minorp)
487 struct ethtool_drvinfo drvinfo;
493 sock = socket(AF_INET, SOCK_DGRAM, 0);
495 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
500 memset(&ifr, 0, sizeof ifr);
501 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
502 ifr.ifr_data = (caddr_t) &drvinfo;
504 memset(&drvinfo, 0, sizeof drvinfo);
505 drvinfo.cmd = ETHTOOL_GDRVINFO;
506 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
507 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
509 goto error_close_sock;
512 if (strcmp(drvinfo.driver, "openvswitch")) {
513 VLOG_WARN("%s is not an openvswitch device", name);
515 goto error_close_sock;
518 if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
519 VLOG_WARN("%s ethtool bus_info has unexpected format", name);
521 goto error_close_sock;
522 } else if (port_no != ODPP_LOCAL) {
523 /* This is an Open vSwitch device but not the local port. We
524 * intentionally support only using the name of the local port as the
525 * name of a datapath; otherwise, it would be too difficult to
526 * enumerate all the names of a datapath. */
528 goto error_close_sock;
542 make_openvswitch_device(int minor, char **fnp)
544 dev_t dev = makedev(get_openvswitch_major(), minor);
545 const char dirname[] = "/dev/net";
550 sprintf(fn, "%s/dp%d", dirname, minor);
552 if (!S_ISCHR(s.st_mode)) {
553 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
555 } else if (s.st_rdev != dev) {
556 VLOG_WARN_RL(&error_rl,
557 "%s is device %u:%u instead of %u:%u, fixing",
558 fn, major(s.st_rdev), minor(s.st_rdev),
559 major(dev), minor(dev));
564 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
565 fn, strerror(errno));
568 } else if (errno == ENOENT) {
569 if (stat(dirname, &s)) {
570 if (errno == ENOENT) {
571 if (mkdir(dirname, 0755)) {
572 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
573 dirname, strerror(errno));
577 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
578 dirname, strerror(errno));
583 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
587 /* The device needs to be created. */
588 if (mknod(fn, S_IFCHR | 0700, dev)) {
589 VLOG_WARN_RL(&error_rl,
590 "%s: creating character device %u:%u failed (%s)",
591 fn, major(dev), minor(dev), strerror(errno));
602 get_openvswitch_major(void)
604 static unsigned int openvswitch_major;
605 if (!openvswitch_major) {
606 enum { DEFAULT_MAJOR = 248 };
607 openvswitch_major = get_major("openvswitch", DEFAULT_MAJOR);
609 return openvswitch_major;
613 get_major(const char *target, int default_major)
615 const char fn[] = "/proc/devices";
620 file = fopen(fn, "r");
622 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
626 for (ln = 1; fgets(line, sizeof line, file); ln++) {
630 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
632 } else if (!strncmp(line, "Block", 5)) {
633 /* We only want character devices, so skip the rest of the file. */
635 } else if (sscanf(line, "%d %63s", &major, name)) {
636 if (!strcmp(name, target)) {
643 VLOG_WARN("%s:%d: syntax error", fn, ln);
649 VLOG_ERR("%s: %s major not found (is the module loaded?), using "
650 "default major %d", fn, target, default_major);
652 VLOG_INFO("using default major %d for %s", default_major, target);
653 return default_major;
657 finish_open(struct dpif *dpif_, const char *local_ifname)
659 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
660 dpif->local_ifname = strdup(local_ifname);
661 dpif->local_ifindex = if_nametoindex(local_ifname);
662 if (!dpif->local_ifindex) {
665 VLOG_WARN("could not get ifindex of %s device: %s",
666 local_ifname, strerror(errno));
673 create_minor(const char *name, int minor, struct dpif **dpifp)
675 int error = open_minor(minor, dpifp);
677 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
679 error = finish_open(*dpifp, name);
688 open_minor(int minor, struct dpif **dpifp)
694 error = make_openvswitch_device(minor, &fn);
699 fd = open(fn, O_RDONLY | O_NONBLOCK);
701 struct dpif_linux *dpif = xmalloc(sizeof *dpif);
702 error = rtnetlink_notifier_register(&dpif->port_notifier,
703 dpif_linux_port_changed, dpif);
707 name = xasprintf("dp%d", minor);
708 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
712 dpif->local_ifname = NULL;
714 dpif->local_ifindex = 0;
715 svec_init(&dpif->changed_ports);
716 dpif->change_error = false;
717 *dpifp = &dpif->dpif;
723 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
731 dpif_linux_port_changed(const struct rtnetlink_change *change, void *dpif_)
733 struct dpif_linux *dpif = dpif_;
736 if (change->master_ifindex == dpif->local_ifindex
737 && (change->nlmsg_type == RTM_NEWLINK
738 || change->nlmsg_type == RTM_DELLINK))
740 /* Our datapath changed, either adding a new port or deleting an
742 if (!svec_contains(&dpif->changed_ports, change->ifname)) {
743 svec_add(&dpif->changed_ports, change->ifname);
744 svec_sort(&dpif->changed_ports);
748 dpif->change_error = true;