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) && isdigit(name[2]) ? atoi(name + 2) : -1;
110 return create_minor(suffix, minor, dpifp);
112 /* Scan for unused minor number. */
113 for (minor = 0; minor < ODP_MAX; minor++) {
114 int error = create_minor(suffix, minor, dpifp);
115 if (error != EBUSY) {
120 /* All datapath numbers in use. */
124 struct dpif_linux *dpif;
125 struct odp_port port;
129 error = lookup_minor(suffix, &minor);
135 error = open_minor(minor, dpifp);
139 dpif = dpif_linux_cast(*dpifp);
141 /* We need the local port's ifindex for the poll function. Start by
142 * getting the local port's name. */
143 memset(&port, 0, sizeof port);
144 port.port = ODPP_LOCAL;
145 if (ioctl(dpif->fd, ODP_PORT_QUERY, &port)) {
147 if (error != ENODEV) {
148 VLOG_WARN("%s: probe returned unexpected error: %s",
149 dpif_name(*dpifp), strerror(error));
155 /* Then use that to finish up opening. */
156 return finish_open(&dpif->dpif, port.devname);
161 dpif_linux_close(struct dpif *dpif_)
163 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
164 rtnetlink_notifier_unregister(&dpif->port_notifier);
165 svec_destroy(&dpif->changed_ports);
166 free(dpif->local_ifname);
172 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
174 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
176 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
177 svec_add(all_names, dpif->local_ifname);
182 dpif_linux_delete(struct dpif *dpif_)
184 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
188 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
190 return do_ioctl(dpif_, ODP_DP_STATS, stats);
194 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
199 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
201 *drop_fragsp = drop_frags & 1;
207 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
209 int drop_frags_int = drop_frags;
210 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
214 dpif_linux_port_add(struct dpif *dpif_, const char *devname, uint16_t flags,
217 struct odp_port port;
220 memset(&port, 0, sizeof port);
221 strncpy(port.devname, devname, sizeof port.devname);
223 error = do_ioctl(dpif_, ODP_PORT_ADD, &port);
225 *port_no = port.port;
231 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
234 return do_ioctl(dpif_, ODP_PORT_DEL, &tmp);
238 dpif_linux_port_query_by_number(const struct dpif *dpif_, uint16_t port_no,
239 struct odp_port *port)
241 memset(port, 0, sizeof *port);
242 port->port = port_no;
243 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
247 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
248 struct odp_port *port)
250 memset(port, 0, sizeof *port);
251 strncpy(port->devname, devname, sizeof port->devname);
252 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
256 dpif_linux_flow_flush(struct dpif *dpif_)
258 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
262 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
264 struct odp_portvec pv;
269 error = do_ioctl(dpif_, ODP_PORT_LIST, &pv);
270 return error ? -error : pv.n_ports;
274 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
276 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
278 if (dpif->change_error) {
279 dpif->change_error = false;
280 svec_clear(&dpif->changed_ports);
282 } else if (dpif->changed_ports.n) {
283 *devnamep = dpif->changed_ports.names[--dpif->changed_ports.n];
291 dpif_linux_port_poll_wait(const struct dpif *dpif_)
293 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
294 if (dpif->changed_ports.n || dpif->change_error) {
295 poll_immediate_wake();
297 rtnetlink_notifier_wait();
302 dpif_linux_port_group_get(const struct dpif *dpif_, int group,
303 uint16_t ports[], int n)
305 struct odp_port_group pg;
308 assert(n <= UINT16_MAX);
312 error = do_ioctl(dpif_, ODP_PORT_GROUP_GET, &pg);
313 return error ? -error : pg.n_ports;
317 dpif_linux_port_group_set(struct dpif *dpif_, int group,
318 const uint16_t ports[], int n)
320 struct odp_port_group pg;
322 assert(n <= UINT16_MAX);
324 pg.ports = (uint16_t *) ports;
326 return do_ioctl(dpif_, ODP_PORT_GROUP_SET, &pg);
330 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
332 struct odp_flowvec fv;
335 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
339 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
341 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
345 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
347 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
351 dpif_linux_flow_list(const struct dpif *dpif_, struct odp_flow flows[], int n)
353 struct odp_flowvec fv;
358 error = do_ioctl(dpif_, ODP_FLOW_LIST, &fv);
359 return error ? -error : fv.n_flows;
363 dpif_linux_execute(struct dpif *dpif_, uint16_t in_port,
364 const union odp_action actions[], int n_actions,
365 const struct ofpbuf *buf)
367 struct odp_execute execute;
368 memset(&execute, 0, sizeof execute);
369 execute.in_port = in_port;
370 execute.actions = (union odp_action *) actions;
371 execute.n_actions = n_actions;
372 execute.data = buf->data;
373 execute.length = buf->size;
374 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
378 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
380 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
384 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
386 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
390 dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
392 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
397 buf = ofpbuf_new(65536);
398 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
401 if (error != EAGAIN) {
402 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
403 dpif_name(dpif_), strerror(error));
405 } else if (retval >= sizeof(struct odp_msg)) {
406 struct odp_msg *msg = buf->data;
407 if (msg->length <= retval) {
412 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
413 "from %zu bytes to %d",
414 dpif_name(dpif_), msg->length, retval);
417 } else if (!retval) {
418 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
421 VLOG_WARN_RL(&error_rl,
422 "%s: discarding too-short message (%d bytes)",
423 dpif_name(dpif_), retval);
433 dpif_linux_recv_wait(struct dpif *dpif_)
435 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
436 poll_fd_wait(dpif->fd, POLLIN);
439 const struct dpif_class dpif_linux_class = {
440 "", /* This is the default class. */
444 dpif_linux_enumerate,
447 dpif_linux_get_all_names,
449 dpif_linux_get_stats,
450 dpif_linux_get_drop_frags,
451 dpif_linux_set_drop_frags,
454 dpif_linux_port_query_by_number,
455 dpif_linux_port_query_by_name,
456 dpif_linux_port_list,
457 dpif_linux_port_poll,
458 dpif_linux_port_poll_wait,
459 dpif_linux_port_group_get,
460 dpif_linux_port_group_set,
464 dpif_linux_flow_flush,
465 dpif_linux_flow_list,
467 dpif_linux_recv_get_mask,
468 dpif_linux_recv_set_mask,
470 dpif_linux_recv_wait,
473 static int get_openvswitch_major(void);
474 static int get_major(const char *target, int default_major);
477 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
479 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
480 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
484 lookup_minor(const char *name, int *minorp)
486 struct ethtool_drvinfo drvinfo;
492 sock = socket(AF_INET, SOCK_DGRAM, 0);
494 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
499 memset(&ifr, 0, sizeof ifr);
500 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
501 ifr.ifr_data = (caddr_t) &drvinfo;
503 memset(&drvinfo, 0, sizeof drvinfo);
504 drvinfo.cmd = ETHTOOL_GDRVINFO;
505 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
506 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
508 goto error_close_sock;
511 if (strcmp(drvinfo.driver, "openvswitch")) {
512 VLOG_WARN("%s is not an openvswitch device", name);
514 goto error_close_sock;
517 if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
518 VLOG_WARN("%s ethtool bus_info has unexpected format", name);
520 goto error_close_sock;
521 } else if (port_no != ODPP_LOCAL) {
522 /* This is an Open vSwitch device but not the local port. We
523 * intentionally support only using the name of the local port as the
524 * name of a datapath; otherwise, it would be too difficult to
525 * enumerate all the names of a datapath. */
527 goto error_close_sock;
541 make_openvswitch_device(int minor, char **fnp)
543 dev_t dev = makedev(get_openvswitch_major(), minor);
544 const char dirname[] = "/dev/net";
549 sprintf(fn, "%s/dp%d", dirname, minor);
551 if (!S_ISCHR(s.st_mode)) {
552 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
554 } else if (s.st_rdev != dev) {
555 VLOG_WARN_RL(&error_rl,
556 "%s is device %u:%u instead of %u:%u, fixing",
557 fn, major(s.st_rdev), minor(s.st_rdev),
558 major(dev), minor(dev));
563 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
564 fn, strerror(errno));
567 } else if (errno == ENOENT) {
568 if (stat(dirname, &s)) {
569 if (errno == ENOENT) {
570 if (mkdir(dirname, 0755)) {
571 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
572 dirname, strerror(errno));
576 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
577 dirname, strerror(errno));
582 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
586 /* The device needs to be created. */
587 if (mknod(fn, S_IFCHR | 0700, dev)) {
588 VLOG_WARN_RL(&error_rl,
589 "%s: creating character device %u:%u failed (%s)",
590 fn, major(dev), minor(dev), strerror(errno));
601 get_openvswitch_major(void)
603 static unsigned int openvswitch_major;
604 if (!openvswitch_major) {
605 enum { DEFAULT_MAJOR = 248 };
606 openvswitch_major = get_major("openvswitch", DEFAULT_MAJOR);
608 return openvswitch_major;
612 get_major(const char *target, int default_major)
614 const char fn[] = "/proc/devices";
619 file = fopen(fn, "r");
621 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
625 for (ln = 1; fgets(line, sizeof line, file); ln++) {
629 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
631 } else if (!strncmp(line, "Block", 5)) {
632 /* We only want character devices, so skip the rest of the file. */
634 } else if (sscanf(line, "%d %63s", &major, name)) {
635 if (!strcmp(name, target)) {
642 VLOG_WARN("%s:%d: syntax error", fn, ln);
648 VLOG_ERR("%s: %s major not found (is the module loaded?), using "
649 "default major %d", fn, target, default_major);
651 VLOG_INFO("using default major %d for %s", default_major, target);
652 return default_major;
656 finish_open(struct dpif *dpif_, const char *local_ifname)
658 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
659 dpif->local_ifname = strdup(local_ifname);
660 dpif->local_ifindex = if_nametoindex(local_ifname);
661 if (!dpif->local_ifindex) {
664 VLOG_WARN("could not get ifindex of %s device: %s",
665 local_ifname, strerror(errno));
672 create_minor(const char *name, int minor, struct dpif **dpifp)
674 int error = open_minor(minor, dpifp);
676 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
678 error = finish_open(*dpifp, name);
687 open_minor(int minor, struct dpif **dpifp)
693 error = make_openvswitch_device(minor, &fn);
698 fd = open(fn, O_RDONLY | O_NONBLOCK);
700 struct dpif_linux *dpif = xmalloc(sizeof *dpif);
701 error = rtnetlink_notifier_register(&dpif->port_notifier,
702 dpif_linux_port_changed, dpif);
706 name = xasprintf("dp%d", minor);
707 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
711 dpif->local_ifname = NULL;
713 dpif->local_ifindex = 0;
714 svec_init(&dpif->changed_ports);
715 dpif->change_error = false;
716 *dpifp = &dpif->dpif;
722 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
730 dpif_linux_port_changed(const struct rtnetlink_change *change, void *dpif_)
732 struct dpif_linux *dpif = dpif_;
735 if (change->master_ifindex == dpif->local_ifindex
736 && (change->nlmsg_type == RTM_NEWLINK
737 || change->nlmsg_type == RTM_DELLINK))
739 /* Our datapath changed, either adding a new port or deleting an
741 if (!svec_contains(&dpif->changed_ports, change->ifname)) {
742 svec_add(&dpif->changed_ports, change->ifname);
743 svec_sort(&dpif->changed_ports);
747 dpif->change_error = true;