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"
39 #include "poll-loop.h"
40 #include "rtnetlink.h"
46 VLOG_DEFINE_THIS_MODULE(dpif_linux);
48 /* Datapath interface for the openvswitch Linux kernel module. */
53 /* Used by dpif_linux_get_all_names(). */
57 /* Change notification. */
58 int local_ifindex; /* Ifindex of local port. */
59 struct shash changed_ports; /* Ports that have changed. */
60 struct rtnetlink_notifier port_notifier;
64 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
66 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
67 static int lookup_minor(const char *name, int *minor);
68 static int finish_open(struct dpif *, const char *local_ifname);
69 static int get_openvswitch_major(void);
70 static int create_minor(const char *name, int minor, struct dpif **dpifp);
71 static int open_minor(int minor, struct dpif **dpifp);
72 static int make_openvswitch_device(int minor, char **fnp);
73 static void dpif_linux_port_changed(const struct rtnetlink_change *,
76 static struct dpif_linux *
77 dpif_linux_cast(const struct dpif *dpif)
79 dpif_assert_class(dpif, &dpif_linux_class);
80 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
84 dpif_linux_enumerate(struct svec *all_dps)
90 /* Check that the Open vSwitch module is loaded. */
91 major = get_openvswitch_major();
97 for (i = 0; i < ODP_MAX; i++) {
102 sprintf(devname, "dp%d", i);
103 retval = dpif_open(devname, "system", &dpif);
105 svec_add(all_dps, devname);
106 dpif_uninit(dpif, true);
107 } else if (retval != ENODEV && !error) {
115 dpif_linux_open(const char *name, const char *type OVS_UNUSED, bool create,
120 minor = !strncmp(name, "dp", 2)
121 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
124 return create_minor(name, minor, dpifp);
126 /* Scan for unused minor number. */
127 for (minor = 0; minor < ODP_MAX; minor++) {
128 int error = create_minor(name, minor, dpifp);
129 if (error != EBUSY) {
134 /* All datapath numbers in use. */
138 struct dpif_linux *dpif;
139 struct odp_port port;
143 error = lookup_minor(name, &minor);
149 error = open_minor(minor, dpifp);
153 dpif = dpif_linux_cast(*dpifp);
155 /* We need the local port's ifindex for the poll function. Start by
156 * getting the local port's name. */
157 memset(&port, 0, sizeof port);
158 port.port = ODPP_LOCAL;
159 if (ioctl(dpif->fd, ODP_PORT_QUERY, &port)) {
161 if (error != ENODEV) {
162 VLOG_WARN("%s: probe returned unexpected error: %s",
163 dpif_name(*dpifp), strerror(error));
165 dpif_uninit(*dpifp, true);
169 /* Then use that to finish up opening. */
170 return finish_open(&dpif->dpif, port.devname);
175 dpif_linux_close(struct dpif *dpif_)
177 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
178 rtnetlink_notifier_unregister(&dpif->port_notifier);
179 shash_destroy(&dpif->changed_ports);
180 free(dpif->local_ifname);
186 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
188 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
190 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
191 svec_add(all_names, dpif->local_ifname);
196 dpif_linux_destroy(struct dpif *dpif_)
198 struct odp_port *ports;
203 err = dpif_port_list(dpif_, &ports, &n_ports);
208 for (i = 0; i < n_ports; i++) {
209 if (ports[i].port != ODPP_LOCAL) {
210 err = do_ioctl(dpif_, ODP_VPORT_DEL, ports[i].devname);
212 VLOG_WARN_RL(&error_rl, "%s: error deleting port %s (%s)",
213 dpif_name(dpif_), ports[i].devname, strerror(err));
220 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
224 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
226 memset(stats, 0, sizeof *stats);
227 return do_ioctl(dpif_, ODP_DP_STATS, stats);
231 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
236 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
238 *drop_fragsp = drop_frags & 1;
244 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
246 int drop_frags_int = drop_frags;
247 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
251 dpif_linux_port_add(struct dpif *dpif_, const char *devname, uint16_t flags,
254 struct odp_port port;
257 memset(&port, 0, sizeof port);
258 strncpy(port.devname, devname, sizeof port.devname);
260 error = do_ioctl(dpif_, ODP_PORT_ATTACH, &port);
262 *port_no = port.port;
268 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
272 struct odp_port port;
274 err = dpif_port_query_by_number(dpif_, port_no, &port);
279 err = do_ioctl(dpif_, ODP_PORT_DETACH, &tmp);
284 if (!netdev_is_open(port.devname)) {
285 /* Try deleting the port if no one has it open. This shouldn't
286 * actually be necessary unless the config changed while we weren't
287 * running but it won't hurt anything if the port is already gone. */
288 do_ioctl(dpif_, ODP_VPORT_DEL, port.devname);
295 dpif_linux_port_query_by_number(const struct dpif *dpif_, uint16_t port_no,
296 struct odp_port *port)
298 memset(port, 0, sizeof *port);
299 port->port = port_no;
300 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
304 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
305 struct odp_port *port)
307 memset(port, 0, sizeof *port);
308 strncpy(port->devname, devname, sizeof port->devname);
309 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
313 dpif_linux_flow_flush(struct dpif *dpif_)
315 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
319 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
321 struct odp_portvec pv;
326 error = do_ioctl(dpif_, ODP_PORT_LIST, &pv);
327 return error ? -error : pv.n_ports;
331 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
333 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
335 if (dpif->change_error) {
336 dpif->change_error = false;
337 shash_clear(&dpif->changed_ports);
339 } else if (!shash_is_empty(&dpif->changed_ports)) {
340 struct shash_node *node = shash_first(&dpif->changed_ports);
341 *devnamep = shash_steal(&dpif->changed_ports, node);
349 dpif_linux_port_poll_wait(const struct dpif *dpif_)
351 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
352 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
353 poll_immediate_wake();
355 rtnetlink_notifier_wait();
360 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
362 struct odp_flowvec fv;
365 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
369 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
371 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
375 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
377 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
381 dpif_linux_flow_list(const struct dpif *dpif_, struct odp_flow flows[], int n)
383 struct odp_flowvec fv;
388 error = do_ioctl(dpif_, ODP_FLOW_LIST, &fv);
389 return error ? -error : fv.n_flows;
393 dpif_linux_execute(struct dpif *dpif_,
394 const union odp_action actions[], int n_actions,
395 const struct ofpbuf *buf)
397 struct odp_execute execute;
398 memset(&execute, 0, sizeof execute);
399 execute.actions = (union odp_action *) actions;
400 execute.n_actions = n_actions;
401 execute.data = buf->data;
402 execute.length = buf->size;
403 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
407 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
409 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
413 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
415 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
419 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
420 uint32_t *probability)
422 return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
426 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
428 return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
432 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
433 uint32_t queue_id, uint32_t *priority)
435 if (queue_id < 0xf000) {
436 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
444 dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
446 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
451 buf = ofpbuf_new_with_headroom(65536, DPIF_RECV_MSG_PADDING);
452 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
455 if (error != EAGAIN) {
456 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
457 dpif_name(dpif_), strerror(error));
459 } else if (retval >= sizeof(struct odp_msg)) {
460 struct odp_msg *msg = buf->data;
461 if (msg->length <= retval) {
466 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
467 "from %"PRIu32" bytes to %d",
468 dpif_name(dpif_), msg->length, retval);
471 } else if (!retval) {
472 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
475 VLOG_WARN_RL(&error_rl,
476 "%s: discarding too-short message (%d bytes)",
477 dpif_name(dpif_), retval);
487 dpif_linux_recv_wait(struct dpif *dpif_)
489 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
490 poll_fd_wait(dpif->fd, POLLIN);
493 const struct dpif_class dpif_linux_class = {
497 dpif_linux_enumerate,
500 dpif_linux_get_all_names,
502 dpif_linux_get_stats,
503 dpif_linux_get_drop_frags,
504 dpif_linux_set_drop_frags,
507 dpif_linux_port_query_by_number,
508 dpif_linux_port_query_by_name,
509 dpif_linux_port_list,
510 dpif_linux_port_poll,
511 dpif_linux_port_poll_wait,
515 dpif_linux_flow_flush,
516 dpif_linux_flow_list,
518 dpif_linux_recv_get_mask,
519 dpif_linux_recv_set_mask,
520 dpif_linux_get_sflow_probability,
521 dpif_linux_set_sflow_probability,
522 dpif_linux_queue_to_priority,
524 dpif_linux_recv_wait,
527 static int get_openvswitch_major(void);
528 static int get_major(const char *target);
531 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
533 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
534 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
538 lookup_minor(const char *name, int *minorp)
540 struct ethtool_drvinfo drvinfo;
546 sock = socket(AF_INET, SOCK_DGRAM, 0);
548 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
553 memset(&ifr, 0, sizeof ifr);
554 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
555 ifr.ifr_data = (caddr_t) &drvinfo;
557 memset(&drvinfo, 0, sizeof drvinfo);
558 drvinfo.cmd = ETHTOOL_GDRVINFO;
559 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
560 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
562 goto error_close_sock;
565 if (strcmp(drvinfo.driver, "openvswitch")) {
566 VLOG_WARN("%s is not an openvswitch device", name);
568 goto error_close_sock;
571 if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
572 VLOG_WARN("%s ethtool bus_info has unexpected format", name);
574 goto error_close_sock;
575 } else if (port_no != ODPP_LOCAL) {
576 /* This is an Open vSwitch device but not the local port. We
577 * intentionally support only using the name of the local port as the
578 * name of a datapath; otherwise, it would be too difficult to
579 * enumerate all the names of a datapath. */
581 goto error_close_sock;
595 make_openvswitch_device(int minor, char **fnp)
597 const char dirname[] = "/dev/net";
605 major = get_openvswitch_major();
609 dev = makedev(major, minor);
611 sprintf(fn, "%s/dp%d", dirname, minor);
613 if (!S_ISCHR(s.st_mode)) {
614 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
616 } else if (s.st_rdev != dev) {
617 VLOG_WARN_RL(&error_rl,
618 "%s is device %u:%u but should be %u:%u, fixing",
619 fn, major(s.st_rdev), minor(s.st_rdev),
620 major(dev), minor(dev));
625 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
626 fn, strerror(errno));
629 } else if (errno == ENOENT) {
630 if (stat(dirname, &s)) {
631 if (errno == ENOENT) {
632 if (mkdir(dirname, 0755)) {
633 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
634 dirname, strerror(errno));
638 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
639 dirname, strerror(errno));
644 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
648 /* The device needs to be created. */
649 if (mknod(fn, S_IFCHR | 0700, dev)) {
650 VLOG_WARN_RL(&error_rl,
651 "%s: creating character device %u:%u failed (%s)",
652 fn, major(dev), minor(dev), strerror(errno));
661 /* Return the major device number of the Open vSwitch device. If it
662 * cannot be determined, a negative errno is returned. */
664 get_openvswitch_major(void)
666 static int openvswitch_major = -1;
667 if (openvswitch_major < 0) {
668 openvswitch_major = get_major("openvswitch");
670 return openvswitch_major;
674 get_major(const char *target)
676 const char fn[] = "/proc/devices";
681 file = fopen(fn, "r");
683 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
687 for (ln = 1; fgets(line, sizeof line, file); ln++) {
691 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
693 } else if (!strncmp(line, "Block", 5)) {
694 /* We only want character devices, so skip the rest of the file. */
696 } else if (sscanf(line, "%d %63s", &major, name)) {
697 if (!strcmp(name, target)) {
702 VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
708 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
713 finish_open(struct dpif *dpif_, const char *local_ifname)
715 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
716 dpif->local_ifname = xstrdup(local_ifname);
717 dpif->local_ifindex = if_nametoindex(local_ifname);
718 if (!dpif->local_ifindex) {
720 dpif_uninit(dpif_, true);
721 VLOG_WARN("could not get ifindex of %s device: %s",
722 local_ifname, strerror(errno));
729 create_minor(const char *name, int minor, struct dpif **dpifp)
731 int error = open_minor(minor, dpifp);
733 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
735 error = finish_open(*dpifp, name);
737 dpif_uninit(*dpifp, true);
744 open_minor(int minor, struct dpif **dpifp)
750 error = make_openvswitch_device(minor, &fn);
755 fd = open(fn, O_RDONLY | O_NONBLOCK);
757 struct dpif_linux *dpif = xmalloc(sizeof *dpif);
758 error = rtnetlink_notifier_register(&dpif->port_notifier,
759 dpif_linux_port_changed, dpif);
763 name = xasprintf("dp%d", minor);
764 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
768 dpif->local_ifname = NULL;
770 dpif->local_ifindex = 0;
771 shash_init(&dpif->changed_ports);
772 dpif->change_error = false;
773 *dpifp = &dpif->dpif;
779 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
787 dpif_linux_port_changed(const struct rtnetlink_change *change, void *dpif_)
789 struct dpif_linux *dpif = dpif_;
792 if (change->master_ifindex == dpif->local_ifindex
793 && (change->nlmsg_type == RTM_NEWLINK
794 || change->nlmsg_type == RTM_DELLINK))
796 /* Our datapath changed, either adding a new port or deleting an
798 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
801 dpif->change_error = true;