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/pkt_sched.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/sockios.h>
32 #include <sys/ioctl.h>
36 #include "dpif-provider.h"
38 #include "netdev-vport.h"
40 #include "poll-loop.h"
41 #include "rtnetlink.h"
47 VLOG_DEFINE_THIS_MODULE(dpif_linux);
49 /* Datapath interface for the openvswitch Linux kernel module. */
54 /* Used by dpif_linux_get_all_names(). */
58 /* Change notification. */
59 int local_ifindex; /* Ifindex of local port. */
60 struct shash changed_ports; /* Ports that have changed. */
61 struct rtnetlink_notifier port_notifier;
65 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
67 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
68 static int lookup_minor(const char *name, int *minor);
69 static int finish_open(struct dpif *, const char *local_ifname);
70 static int get_openvswitch_major(void);
71 static int create_minor(const char *name, int minor, struct dpif **dpifp);
72 static int open_minor(int minor, struct dpif **dpifp);
73 static int make_openvswitch_device(int minor, char **fnp);
74 static void dpif_linux_port_changed(const struct rtnetlink_change *,
77 static struct dpif_linux *
78 dpif_linux_cast(const struct dpif *dpif)
80 dpif_assert_class(dpif, &dpif_linux_class);
81 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
85 dpif_linux_enumerate(struct svec *all_dps)
91 /* Check that the Open vSwitch module is loaded. */
92 major = get_openvswitch_major();
98 for (i = 0; i < ODP_MAX; i++) {
103 sprintf(devname, "dp%d", i);
104 retval = dpif_open(devname, "system", &dpif);
106 svec_add(all_dps, devname);
107 dpif_uninit(dpif, true);
108 } else if (retval != ENODEV && !error) {
116 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
117 bool create, struct dpif **dpifp)
121 minor = !strncmp(name, "dp", 2)
122 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
125 return create_minor(name, minor, dpifp);
127 /* Scan for unused minor number. */
128 for (minor = 0; minor < ODP_MAX; minor++) {
129 int error = create_minor(name, minor, dpifp);
130 if (error != EBUSY) {
135 /* All datapath numbers in use. */
139 struct dpif_linux *dpif;
140 struct odp_port port;
144 error = lookup_minor(name, &minor);
150 error = open_minor(minor, dpifp);
154 dpif = dpif_linux_cast(*dpifp);
156 /* We need the local port's ifindex for the poll function. Start by
157 * getting the local port's name. */
158 memset(&port, 0, sizeof port);
159 port.port = ODPP_LOCAL;
160 if (ioctl(dpif->fd, ODP_VPORT_QUERY, &port)) {
162 if (error != ENODEV) {
163 VLOG_WARN("%s: probe returned unexpected error: %s",
164 dpif_name(*dpifp), strerror(error));
166 dpif_uninit(*dpifp, true);
170 /* Then use that to finish up opening. */
171 return finish_open(&dpif->dpif, port.devname);
176 dpif_linux_close(struct dpif *dpif_)
178 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
179 rtnetlink_notifier_unregister(&dpif->port_notifier);
180 shash_destroy(&dpif->changed_ports);
181 free(dpif->local_ifname);
187 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
189 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
191 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
192 svec_add(all_names, dpif->local_ifname);
197 dpif_linux_destroy(struct dpif *dpif_)
199 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
203 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
205 memset(stats, 0, sizeof *stats);
206 return do_ioctl(dpif_, ODP_DP_STATS, stats);
210 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
215 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
217 *drop_fragsp = drop_frags & 1;
223 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
225 int drop_frags_int = drop_frags;
226 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
230 translate_vport_type_to_netdev_type(char *type, size_t size)
232 if (!strcmp(type, "netdev")) {
233 ovs_strlcpy(type, "system", size);
238 translate_netdev_type_to_vport_type(char *type, size_t size)
240 if (!strcmp(type, "system")) {
241 ovs_strlcpy(type, "netdev", size);
246 dpif_linux_port_add(struct dpif *dpif, struct netdev *netdev,
249 const char *name = netdev_get_name(netdev);
250 const char *type = netdev_get_type(netdev);
251 struct odp_port port;
254 memset(&port, 0, sizeof port);
255 strncpy(port.devname, name, sizeof port.devname);
256 strncpy(port.type, type, sizeof port.type);
257 translate_netdev_type_to_vport_type(port.type, sizeof port.type);
258 netdev_vport_get_config(netdev, port.config);
260 error = do_ioctl(dpif, ODP_VPORT_ATTACH, &port);
262 *port_nop = port.port;
269 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no_)
271 int port_no = port_no_; /* Kernel expects an "int". */
272 return do_ioctl(dpif_, ODP_VPORT_DETACH, &port_no);
276 dpif_linux_port_query__(const struct dpif *dpif, struct odp_port *port)
278 int error = do_ioctl(dpif, ODP_VPORT_QUERY, port);
280 translate_vport_type_to_netdev_type(port->type, sizeof port->type);
286 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
287 struct odp_port *port)
289 memset(port, 0, sizeof *port);
290 port->port = port_no;
291 return dpif_linux_port_query__(dpif, port);
295 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
296 struct odp_port *port)
298 memset(port, 0, sizeof *port);
299 strncpy(port->devname, devname, sizeof port->devname);
300 return dpif_linux_port_query__(dpif, port);
304 dpif_linux_flow_flush(struct dpif *dpif_)
306 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
310 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
312 struct odp_portvec pv;
318 error = do_ioctl(dpif_, ODP_VPORT_LIST, &pv);
323 for (i = 0; i < pv.n_ports; i++) {
324 struct odp_port *port = &pv.ports[i];
326 translate_vport_type_to_netdev_type(port->type, sizeof port->type);
332 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
334 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
336 if (dpif->change_error) {
337 dpif->change_error = false;
338 shash_clear(&dpif->changed_ports);
340 } else if (!shash_is_empty(&dpif->changed_ports)) {
341 struct shash_node *node = shash_first(&dpif->changed_ports);
342 *devnamep = shash_steal(&dpif->changed_ports, node);
350 dpif_linux_port_poll_wait(const struct dpif *dpif_)
352 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
353 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
354 poll_immediate_wake();
356 rtnetlink_notifier_wait();
361 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
363 struct odp_flowvec fv;
366 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
370 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
372 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
376 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
378 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
382 dpif_linux_flow_list(const struct dpif *dpif_, struct odp_flow flows[], int n)
384 struct odp_flowvec fv;
389 error = do_ioctl(dpif_, ODP_FLOW_LIST, &fv);
390 return error ? -error : fv.n_flows;
394 dpif_linux_execute(struct dpif *dpif_,
395 const struct nlattr *actions, unsigned int actions_len,
396 const struct ofpbuf *buf)
398 struct odp_execute execute;
399 memset(&execute, 0, sizeof execute);
400 execute.actions = (struct nlattr *) actions;
401 execute.actions_len = actions_len;
402 execute.data = buf->data;
403 execute.length = buf->size;
404 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
408 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
410 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
414 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
416 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
420 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
421 uint32_t *probability)
423 return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
427 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
429 return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
433 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
434 uint32_t queue_id, uint32_t *priority)
436 if (queue_id < 0xf000) {
437 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
445 dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
447 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
452 buf = ofpbuf_new_with_headroom(65536, DPIF_RECV_MSG_PADDING);
453 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
456 if (error != EAGAIN) {
457 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
458 dpif_name(dpif_), strerror(error));
460 } else if (retval >= sizeof(struct odp_msg)) {
461 struct odp_msg *msg = buf->data;
462 if (msg->length <= retval) {
467 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
468 "from %"PRIu32" bytes to %d",
469 dpif_name(dpif_), msg->length, retval);
472 } else if (!retval) {
473 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
476 VLOG_WARN_RL(&error_rl,
477 "%s: discarding too-short message (%d bytes)",
478 dpif_name(dpif_), retval);
488 dpif_linux_recv_wait(struct dpif *dpif_)
490 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
491 poll_fd_wait(dpif->fd, POLLIN);
494 const struct dpif_class dpif_linux_class = {
498 dpif_linux_enumerate,
501 dpif_linux_get_all_names,
503 dpif_linux_get_stats,
504 dpif_linux_get_drop_frags,
505 dpif_linux_set_drop_frags,
508 dpif_linux_port_query_by_number,
509 dpif_linux_port_query_by_name,
510 dpif_linux_port_list,
511 dpif_linux_port_poll,
512 dpif_linux_port_poll_wait,
516 dpif_linux_flow_flush,
517 dpif_linux_flow_list,
519 dpif_linux_recv_get_mask,
520 dpif_linux_recv_set_mask,
521 dpif_linux_get_sflow_probability,
522 dpif_linux_set_sflow_probability,
523 dpif_linux_queue_to_priority,
525 dpif_linux_recv_wait,
528 static int get_openvswitch_major(void);
529 static int get_major(const char *target);
532 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
534 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
535 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
539 lookup_minor(const char *name, int *minorp)
541 struct ethtool_drvinfo drvinfo;
547 sock = socket(AF_INET, SOCK_DGRAM, 0);
549 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
554 memset(&ifr, 0, sizeof ifr);
555 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
556 ifr.ifr_data = (caddr_t) &drvinfo;
558 memset(&drvinfo, 0, sizeof drvinfo);
559 drvinfo.cmd = ETHTOOL_GDRVINFO;
560 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
561 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
563 goto error_close_sock;
566 if (strcmp(drvinfo.driver, "openvswitch")) {
567 VLOG_WARN("%s is not an openvswitch device", name);
569 goto error_close_sock;
572 if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
573 VLOG_WARN("%s ethtool bus_info has unexpected format", name);
575 goto error_close_sock;
576 } else if (port_no != ODPP_LOCAL) {
577 /* This is an Open vSwitch device but not the local port. We
578 * intentionally support only using the name of the local port as the
579 * name of a datapath; otherwise, it would be too difficult to
580 * enumerate all the names of a datapath. */
582 goto error_close_sock;
596 make_openvswitch_device(int minor, char **fnp)
598 const char dirname[] = "/dev/net";
606 major = get_openvswitch_major();
610 dev = makedev(major, minor);
612 sprintf(fn, "%s/dp%d", dirname, minor);
614 if (!S_ISCHR(s.st_mode)) {
615 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
617 } else if (s.st_rdev != dev) {
618 VLOG_WARN_RL(&error_rl,
619 "%s is device %u:%u but should be %u:%u, fixing",
620 fn, major(s.st_rdev), minor(s.st_rdev),
621 major(dev), minor(dev));
626 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
627 fn, strerror(errno));
630 } else if (errno == ENOENT) {
631 if (stat(dirname, &s)) {
632 if (errno == ENOENT) {
633 if (mkdir(dirname, 0755)) {
634 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
635 dirname, strerror(errno));
639 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
640 dirname, strerror(errno));
645 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
649 /* The device needs to be created. */
650 if (mknod(fn, S_IFCHR | 0700, dev)) {
651 VLOG_WARN_RL(&error_rl,
652 "%s: creating character device %u:%u failed (%s)",
653 fn, major(dev), minor(dev), strerror(errno));
662 /* Return the major device number of the Open vSwitch device. If it
663 * cannot be determined, a negative errno is returned. */
665 get_openvswitch_major(void)
667 static int openvswitch_major = -1;
668 if (openvswitch_major < 0) {
669 openvswitch_major = get_major("openvswitch");
671 return openvswitch_major;
675 get_major(const char *target)
677 const char fn[] = "/proc/devices";
682 file = fopen(fn, "r");
684 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
688 for (ln = 1; fgets(line, sizeof line, file); ln++) {
692 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
694 } else if (!strncmp(line, "Block", 5)) {
695 /* We only want character devices, so skip the rest of the file. */
697 } else if (sscanf(line, "%d %63s", &major, name)) {
698 if (!strcmp(name, target)) {
703 VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
709 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
714 finish_open(struct dpif *dpif_, const char *local_ifname)
716 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
717 dpif->local_ifname = xstrdup(local_ifname);
718 dpif->local_ifindex = if_nametoindex(local_ifname);
719 if (!dpif->local_ifindex) {
721 dpif_uninit(dpif_, true);
722 VLOG_WARN("could not get ifindex of %s device: %s",
723 local_ifname, strerror(errno));
730 create_minor(const char *name, int minor, struct dpif **dpifp)
732 int error = open_minor(minor, dpifp);
734 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
736 error = finish_open(*dpifp, name);
738 dpif_uninit(*dpifp, true);
745 open_minor(int minor, struct dpif **dpifp)
751 error = make_openvswitch_device(minor, &fn);
756 fd = open(fn, O_RDONLY | O_NONBLOCK);
758 struct dpif_linux *dpif = xmalloc(sizeof *dpif);
759 error = rtnetlink_notifier_register(&dpif->port_notifier,
760 dpif_linux_port_changed, dpif);
764 name = xasprintf("dp%d", minor);
765 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
769 dpif->local_ifname = NULL;
771 dpif->local_ifindex = 0;
772 shash_init(&dpif->changed_ports);
773 dpif->change_error = false;
774 *dpifp = &dpif->dpif;
780 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
788 dpif_linux_port_changed(const struct rtnetlink_change *change, void *dpif_)
790 struct dpif_linux *dpif = dpif_;
793 if (change->master_ifindex == dpif->local_ifindex
794 && (change->nlmsg_type == RTM_NEWLINK
795 || change->nlmsg_type == RTM_DELLINK))
797 /* Our datapath changed, either adding a new port or deleting an
799 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
802 dpif->change_error = true;