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/ethtool.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/sockios.h>
30 #include <sys/ioctl.h>
34 #include "dpif-provider.h"
37 #include "poll-loop.h"
38 #include "rtnetlink.h"
44 #define THIS_MODULE VLM_dpif_linux
46 /* Datapath interface for the openvswitch Linux kernel module. */
51 /* Used by dpif_linux_get_all_names(). */
55 /* Change notification. */
56 int local_ifindex; /* Ifindex of local port. */
57 struct shash changed_ports; /* Ports that have changed. */
58 struct rtnetlink_notifier port_notifier;
62 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
64 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
65 static int lookup_minor(const char *name, int *minor);
66 static int finish_open(struct dpif *, const char *local_ifname);
67 static int get_openvswitch_major(void);
68 static int create_minor(const char *name, int minor, struct dpif **dpifp);
69 static int open_minor(int minor, struct dpif **dpifp);
70 static int make_openvswitch_device(int minor, char **fnp);
71 static void dpif_linux_port_changed(const struct rtnetlink_change *,
74 static struct dpif_linux *
75 dpif_linux_cast(const struct dpif *dpif)
77 dpif_assert_class(dpif, &dpif_linux_class);
78 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
82 dpif_linux_enumerate(struct svec *all_dps)
88 /* Check that the Open vSwitch module is loaded. */
89 major = get_openvswitch_major();
95 for (i = 0; i < ODP_MAX; i++) {
100 sprintf(devname, "dp%d", i);
101 retval = dpif_open(devname, "system", &dpif);
103 svec_add(all_dps, devname);
104 dpif_uninit(dpif, true);
105 } else if (retval != ENODEV && !error) {
113 dpif_linux_open(const char *name, const char *type OVS_UNUSED, bool create,
118 minor = !strncmp(name, "dp", 2)
119 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
122 return create_minor(name, minor, dpifp);
124 /* Scan for unused minor number. */
125 for (minor = 0; minor < ODP_MAX; minor++) {
126 int error = create_minor(name, minor, dpifp);
127 if (error != EBUSY) {
132 /* All datapath numbers in use. */
136 struct dpif_linux *dpif;
137 struct odp_port port;
141 error = lookup_minor(name, &minor);
147 error = open_minor(minor, dpifp);
151 dpif = dpif_linux_cast(*dpifp);
153 /* We need the local port's ifindex for the poll function. Start by
154 * getting the local port's name. */
155 memset(&port, 0, sizeof port);
156 port.port = ODPP_LOCAL;
157 if (ioctl(dpif->fd, ODP_PORT_QUERY, &port)) {
159 if (error != ENODEV) {
160 VLOG_WARN("%s: probe returned unexpected error: %s",
161 dpif_name(*dpifp), strerror(error));
163 dpif_uninit(*dpifp, true);
167 /* Then use that to finish up opening. */
168 return finish_open(&dpif->dpif, port.devname);
173 dpif_linux_close(struct dpif *dpif_)
175 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
176 rtnetlink_notifier_unregister(&dpif->port_notifier);
177 shash_destroy(&dpif->changed_ports);
178 free(dpif->local_ifname);
184 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
186 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
188 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
189 svec_add(all_names, dpif->local_ifname);
194 dpif_linux_destroy(struct dpif *dpif_)
196 struct odp_port *ports;
201 err = dpif_port_list(dpif_, &ports, &n_ports);
206 for (i = 0; i < n_ports; i++) {
207 if (ports[i].port != ODPP_LOCAL) {
208 err = do_ioctl(dpif_, ODP_VPORT_DEL, ports[i].devname);
210 VLOG_WARN_RL(&error_rl, "%s: error deleting port %s (%s)",
211 dpif_name(dpif_), ports[i].devname, strerror(err));
218 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
222 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
224 memset(stats, 0, sizeof *stats);
225 return do_ioctl(dpif_, ODP_DP_STATS, stats);
229 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
234 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
236 *drop_fragsp = drop_frags & 1;
242 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
244 int drop_frags_int = drop_frags;
245 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
249 dpif_linux_port_add(struct dpif *dpif_, const char *devname, uint16_t flags,
252 struct odp_port port;
255 memset(&port, 0, sizeof port);
256 strncpy(port.devname, devname, sizeof port.devname);
258 error = do_ioctl(dpif_, ODP_PORT_ATTACH, &port);
260 *port_no = port.port;
266 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
270 struct odp_port port;
272 err = dpif_port_query_by_number(dpif_, port_no, &port);
277 err = do_ioctl(dpif_, ODP_PORT_DETACH, &tmp);
282 if (!netdev_is_open(port.devname)) {
283 /* Try deleting the port if no one has it open. This shouldn't
284 * actually be necessary unless the config changed while we weren't
285 * running but it won't hurt anything if the port is already gone. */
286 do_ioctl(dpif_, ODP_VPORT_DEL, port.devname);
293 dpif_linux_port_query_by_number(const struct dpif *dpif_, uint16_t port_no,
294 struct odp_port *port)
296 memset(port, 0, sizeof *port);
297 port->port = port_no;
298 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
302 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
303 struct odp_port *port)
305 memset(port, 0, sizeof *port);
306 strncpy(port->devname, devname, sizeof port->devname);
307 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
311 dpif_linux_flow_flush(struct dpif *dpif_)
313 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
317 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
319 struct odp_portvec pv;
324 error = do_ioctl(dpif_, ODP_PORT_LIST, &pv);
325 return error ? -error : pv.n_ports;
329 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
331 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
333 if (dpif->change_error) {
334 dpif->change_error = false;
335 shash_clear(&dpif->changed_ports);
337 } else if (!shash_is_empty(&dpif->changed_ports)) {
338 struct shash_node *node = shash_first(&dpif->changed_ports);
339 *devnamep = xstrdup(node->name);
340 shash_delete(&dpif->changed_ports, node);
348 dpif_linux_port_poll_wait(const struct dpif *dpif_)
350 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
351 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
352 poll_immediate_wake();
354 rtnetlink_notifier_wait();
359 dpif_linux_port_group_get(const struct dpif *dpif_, int group,
360 uint16_t ports[], int n)
362 struct odp_port_group pg;
365 assert(n <= UINT16_MAX);
369 error = do_ioctl(dpif_, ODP_PORT_GROUP_GET, &pg);
370 return error ? -error : pg.n_ports;
374 dpif_linux_port_group_set(struct dpif *dpif_, int group,
375 const uint16_t ports[], int n)
377 struct odp_port_group pg;
379 assert(n <= UINT16_MAX);
381 pg.ports = (uint16_t *) ports;
383 return do_ioctl(dpif_, ODP_PORT_GROUP_SET, &pg);
387 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
389 struct odp_flowvec fv;
392 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
396 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
398 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
402 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
404 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
408 dpif_linux_flow_list(const struct dpif *dpif_, struct odp_flow flows[], int n)
410 struct odp_flowvec fv;
415 error = do_ioctl(dpif_, ODP_FLOW_LIST, &fv);
416 return error ? -error : fv.n_flows;
420 dpif_linux_execute(struct dpif *dpif_, uint16_t in_port,
421 const union odp_action actions[], int n_actions,
422 const struct ofpbuf *buf)
424 struct odp_execute execute;
425 memset(&execute, 0, sizeof execute);
426 execute.in_port = in_port;
427 execute.actions = (union odp_action *) actions;
428 execute.n_actions = n_actions;
429 execute.data = buf->data;
430 execute.length = buf->size;
431 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
435 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
437 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
441 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
443 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
447 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
448 uint32_t *probability)
450 return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
454 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
456 return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
460 dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
462 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
467 buf = ofpbuf_new(65536 + DPIF_RECV_MSG_PADDING);
468 ofpbuf_reserve(buf, DPIF_RECV_MSG_PADDING);
469 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
472 if (error != EAGAIN) {
473 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
474 dpif_name(dpif_), strerror(error));
476 } else if (retval >= sizeof(struct odp_msg)) {
477 struct odp_msg *msg = buf->data;
478 if (msg->length <= retval) {
483 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
484 "from %"PRIu32" bytes to %d",
485 dpif_name(dpif_), msg->length, retval);
488 } else if (!retval) {
489 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
492 VLOG_WARN_RL(&error_rl,
493 "%s: discarding too-short message (%d bytes)",
494 dpif_name(dpif_), retval);
504 dpif_linux_recv_wait(struct dpif *dpif_)
506 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
507 poll_fd_wait(dpif->fd, POLLIN);
510 const struct dpif_class dpif_linux_class = {
514 dpif_linux_enumerate,
517 dpif_linux_get_all_names,
519 dpif_linux_get_stats,
520 dpif_linux_get_drop_frags,
521 dpif_linux_set_drop_frags,
524 dpif_linux_port_query_by_number,
525 dpif_linux_port_query_by_name,
526 dpif_linux_port_list,
527 dpif_linux_port_poll,
528 dpif_linux_port_poll_wait,
529 dpif_linux_port_group_get,
530 dpif_linux_port_group_set,
534 dpif_linux_flow_flush,
535 dpif_linux_flow_list,
537 dpif_linux_recv_get_mask,
538 dpif_linux_recv_set_mask,
539 dpif_linux_get_sflow_probability,
540 dpif_linux_set_sflow_probability,
542 dpif_linux_recv_wait,
545 static int get_openvswitch_major(void);
546 static int get_major(const char *target);
549 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
551 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
552 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
556 lookup_minor(const char *name, int *minorp)
558 struct ethtool_drvinfo drvinfo;
564 sock = socket(AF_INET, SOCK_DGRAM, 0);
566 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
571 memset(&ifr, 0, sizeof ifr);
572 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
573 ifr.ifr_data = (caddr_t) &drvinfo;
575 memset(&drvinfo, 0, sizeof drvinfo);
576 drvinfo.cmd = ETHTOOL_GDRVINFO;
577 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
578 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
580 goto error_close_sock;
583 if (strcmp(drvinfo.driver, "openvswitch")) {
584 VLOG_WARN("%s is not an openvswitch device", name);
586 goto error_close_sock;
589 if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
590 VLOG_WARN("%s ethtool bus_info has unexpected format", name);
592 goto error_close_sock;
593 } else if (port_no != ODPP_LOCAL) {
594 /* This is an Open vSwitch device but not the local port. We
595 * intentionally support only using the name of the local port as the
596 * name of a datapath; otherwise, it would be too difficult to
597 * enumerate all the names of a datapath. */
599 goto error_close_sock;
613 make_openvswitch_device(int minor, char **fnp)
615 const char dirname[] = "/dev/net";
623 major = get_openvswitch_major();
627 dev = makedev(major, minor);
629 sprintf(fn, "%s/dp%d", dirname, minor);
631 if (!S_ISCHR(s.st_mode)) {
632 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
634 } else if (s.st_rdev != dev) {
635 VLOG_WARN_RL(&error_rl,
636 "%s is device %u:%u but should be %u:%u, fixing",
637 fn, major(s.st_rdev), minor(s.st_rdev),
638 major(dev), minor(dev));
643 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
644 fn, strerror(errno));
647 } else if (errno == ENOENT) {
648 if (stat(dirname, &s)) {
649 if (errno == ENOENT) {
650 if (mkdir(dirname, 0755)) {
651 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
652 dirname, strerror(errno));
656 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
657 dirname, strerror(errno));
662 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
666 /* The device needs to be created. */
667 if (mknod(fn, S_IFCHR | 0700, dev)) {
668 VLOG_WARN_RL(&error_rl,
669 "%s: creating character device %u:%u failed (%s)",
670 fn, major(dev), minor(dev), strerror(errno));
679 /* Return the major device number of the Open vSwitch device. If it
680 * cannot be determined, a negative errno is returned. */
682 get_openvswitch_major(void)
684 static int openvswitch_major = -1;
685 if (openvswitch_major < 0) {
686 openvswitch_major = get_major("openvswitch");
688 return openvswitch_major;
692 get_major(const char *target)
694 const char fn[] = "/proc/devices";
699 file = fopen(fn, "r");
701 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
705 for (ln = 1; fgets(line, sizeof line, file); ln++) {
709 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
711 } else if (!strncmp(line, "Block", 5)) {
712 /* We only want character devices, so skip the rest of the file. */
714 } else if (sscanf(line, "%d %63s", &major, name)) {
715 if (!strcmp(name, target)) {
722 VLOG_WARN("%s:%d: syntax error", fn, ln);
730 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
735 finish_open(struct dpif *dpif_, const char *local_ifname)
737 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
738 dpif->local_ifname = xstrdup(local_ifname);
739 dpif->local_ifindex = if_nametoindex(local_ifname);
740 if (!dpif->local_ifindex) {
742 dpif_uninit(dpif_, true);
743 VLOG_WARN("could not get ifindex of %s device: %s",
744 local_ifname, strerror(errno));
751 create_minor(const char *name, int minor, struct dpif **dpifp)
753 int error = open_minor(minor, dpifp);
755 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
757 error = finish_open(*dpifp, name);
759 dpif_uninit(*dpifp, true);
766 open_minor(int minor, struct dpif **dpifp)
772 error = make_openvswitch_device(minor, &fn);
777 fd = open(fn, O_RDONLY | O_NONBLOCK);
779 struct dpif_linux *dpif = xmalloc(sizeof *dpif);
780 error = rtnetlink_notifier_register(&dpif->port_notifier,
781 dpif_linux_port_changed, dpif);
785 name = xasprintf("dp%d", minor);
786 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
790 dpif->local_ifname = NULL;
792 dpif->local_ifindex = 0;
793 shash_init(&dpif->changed_ports);
794 dpif->change_error = false;
795 *dpifp = &dpif->dpif;
801 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
809 dpif_linux_port_changed(const struct rtnetlink_change *change, void *dpif_)
811 struct dpif_linux *dpif = dpif_;
814 if (change->master_ifindex == dpif->local_ifindex
815 && (change->nlmsg_type == RTM_NEWLINK
816 || change->nlmsg_type == RTM_DELLINK))
818 /* Our datapath changed, either adding a new port or deleting an
820 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
823 dpif->change_error = true;