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>
33 #include "dpif-provider.h"
36 #include "poll-loop.h"
37 #include "rtnetlink.h"
43 #define THIS_MODULE VLM_dpif_linux
45 /* Datapath interface for the openvswitch Linux kernel module. */
50 /* Used by dpif_linux_get_all_names(). */
54 /* Change notification. */
55 int local_ifindex; /* Ifindex of local port. */
56 struct shash changed_ports; /* Ports that have changed. */
57 struct rtnetlink_notifier port_notifier;
61 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
63 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
64 static int lookup_minor(const char *name, int *minor);
65 static int finish_open(struct dpif *, const char *local_ifname);
66 static int get_openvswitch_major(void);
67 static int create_minor(const char *name, int minor, struct dpif **dpifp);
68 static int open_minor(int minor, struct dpif **dpifp);
69 static int make_openvswitch_device(int minor, char **fnp);
70 static void dpif_linux_port_changed(const struct rtnetlink_change *,
73 static struct dpif_linux *
74 dpif_linux_cast(const struct dpif *dpif)
76 dpif_assert_class(dpif, &dpif_linux_class);
77 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
81 dpif_linux_enumerate(struct svec *all_dps)
87 /* Check that the Open vSwitch module is loaded. */
88 major = get_openvswitch_major();
94 for (i = 0; i < ODP_MAX; i++) {
99 sprintf(devname, "dp%d", i);
100 retval = dpif_open(devname, "system", &dpif);
102 svec_add(all_dps, devname);
103 dpif_uninit(dpif, true);
104 } else if (retval != ENODEV && !error) {
112 dpif_linux_open(const char *name, const char *type OVS_UNUSED, bool create,
117 minor = !strncmp(name, "dp", 2)
118 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
121 return create_minor(name, minor, dpifp);
123 /* Scan for unused minor number. */
124 for (minor = 0; minor < ODP_MAX; minor++) {
125 int error = create_minor(name, minor, dpifp);
126 if (error != EBUSY) {
131 /* All datapath numbers in use. */
135 struct dpif_linux *dpif;
136 struct odp_port port;
140 error = lookup_minor(name, &minor);
146 error = open_minor(minor, dpifp);
150 dpif = dpif_linux_cast(*dpifp);
152 /* We need the local port's ifindex for the poll function. Start by
153 * getting the local port's name. */
154 memset(&port, 0, sizeof port);
155 port.port = ODPP_LOCAL;
156 if (ioctl(dpif->fd, ODP_PORT_QUERY, &port)) {
158 if (error != ENODEV) {
159 VLOG_WARN("%s: probe returned unexpected error: %s",
160 dpif_name(*dpifp), strerror(error));
162 dpif_uninit(*dpifp, true);
166 /* Then use that to finish up opening. */
167 return finish_open(&dpif->dpif, port.devname);
172 dpif_linux_close(struct dpif *dpif_)
174 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
175 rtnetlink_notifier_unregister(&dpif->port_notifier);
176 shash_destroy(&dpif->changed_ports);
177 free(dpif->local_ifname);
183 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
185 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
187 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
188 svec_add(all_names, dpif->local_ifname);
193 dpif_linux_destroy(struct dpif *dpif_)
195 struct odp_port *ports;
200 err = dpif_port_list(dpif_, &ports, &n_ports);
205 for (i = 0; i < n_ports; i++) {
206 if (ports[i].port != ODPP_LOCAL) {
207 err = do_ioctl(dpif_, ODP_VPORT_DEL, ports[i].devname);
209 VLOG_WARN_RL(&error_rl, "%s: error deleting port %s (%s)",
210 dpif_name(dpif_), ports[i].devname, strerror(err));
217 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
221 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
223 memset(stats, 0, sizeof *stats);
224 return do_ioctl(dpif_, ODP_DP_STATS, stats);
228 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
233 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
235 *drop_fragsp = drop_frags & 1;
241 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
243 int drop_frags_int = drop_frags;
244 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
248 dpif_linux_port_add(struct dpif *dpif_, const char *devname, uint16_t flags,
251 struct odp_port port;
254 memset(&port, 0, sizeof port);
255 strncpy(port.devname, devname, sizeof port.devname);
257 error = do_ioctl(dpif_, ODP_PORT_ATTACH, &port);
259 *port_no = port.port;
265 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
269 struct odp_port port;
271 err = dpif_port_query_by_number(dpif_, port_no, &port);
276 err = do_ioctl(dpif_, ODP_PORT_DETACH, &tmp);
281 if (!netdev_is_open(port.devname)) {
282 /* Try deleting the port if no one has it open. This shouldn't
283 * actually be necessary unless the config changed while we weren't
284 * running but it won't hurt anything if the port is already gone. */
285 do_ioctl(dpif_, ODP_VPORT_DEL, port.devname);
292 dpif_linux_port_query_by_number(const struct dpif *dpif_, uint16_t port_no,
293 struct odp_port *port)
295 memset(port, 0, sizeof *port);
296 port->port = port_no;
297 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
301 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
302 struct odp_port *port)
304 memset(port, 0, sizeof *port);
305 strncpy(port->devname, devname, sizeof port->devname);
306 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
310 dpif_linux_flow_flush(struct dpif *dpif_)
312 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
316 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
318 struct odp_portvec pv;
323 error = do_ioctl(dpif_, ODP_PORT_LIST, &pv);
324 return error ? -error : pv.n_ports;
328 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
330 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
332 if (dpif->change_error) {
333 dpif->change_error = false;
334 shash_clear(&dpif->changed_ports);
336 } else if (!shash_is_empty(&dpif->changed_ports)) {
337 struct shash_node *node = shash_first(&dpif->changed_ports);
338 *devnamep = xstrdup(node->name);
339 shash_delete(&dpif->changed_ports, node);
347 dpif_linux_port_poll_wait(const struct dpif *dpif_)
349 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
350 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
351 poll_immediate_wake();
353 rtnetlink_notifier_wait();
358 dpif_linux_port_group_get(const struct dpif *dpif_, int group,
359 uint16_t ports[], int n)
361 struct odp_port_group pg;
364 assert(n <= UINT16_MAX);
368 error = do_ioctl(dpif_, ODP_PORT_GROUP_GET, &pg);
369 return error ? -error : pg.n_ports;
373 dpif_linux_port_group_set(struct dpif *dpif_, int group,
374 const uint16_t ports[], int n)
376 struct odp_port_group pg;
378 assert(n <= UINT16_MAX);
380 pg.ports = (uint16_t *) ports;
382 return do_ioctl(dpif_, ODP_PORT_GROUP_SET, &pg);
386 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
388 struct odp_flowvec fv;
391 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
395 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
397 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
401 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
403 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
407 dpif_linux_flow_list(const struct dpif *dpif_, struct odp_flow flows[], int n)
409 struct odp_flowvec fv;
414 error = do_ioctl(dpif_, ODP_FLOW_LIST, &fv);
415 return error ? -error : fv.n_flows;
419 dpif_linux_execute(struct dpif *dpif_, uint16_t in_port,
420 const union odp_action actions[], int n_actions,
421 const struct ofpbuf *buf)
423 struct odp_execute execute;
424 memset(&execute, 0, sizeof execute);
425 execute.in_port = in_port;
426 execute.actions = (union odp_action *) actions;
427 execute.n_actions = n_actions;
428 execute.data = buf->data;
429 execute.length = buf->size;
430 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
434 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
436 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
440 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
442 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
446 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
447 uint32_t *probability)
449 return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
453 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
455 return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
459 dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
461 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
466 buf = ofpbuf_new(65536 + DPIF_RECV_MSG_PADDING);
467 ofpbuf_reserve(buf, DPIF_RECV_MSG_PADDING);
468 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
471 if (error != EAGAIN) {
472 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
473 dpif_name(dpif_), strerror(error));
475 } else if (retval >= sizeof(struct odp_msg)) {
476 struct odp_msg *msg = buf->data;
477 if (msg->length <= retval) {
482 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
483 "from %"PRIu32" bytes to %d",
484 dpif_name(dpif_), msg->length, retval);
487 } else if (!retval) {
488 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
491 VLOG_WARN_RL(&error_rl,
492 "%s: discarding too-short message (%d bytes)",
493 dpif_name(dpif_), retval);
503 dpif_linux_recv_wait(struct dpif *dpif_)
505 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
506 poll_fd_wait(dpif->fd, POLLIN);
509 const struct dpif_class dpif_linux_class = {
513 dpif_linux_enumerate,
516 dpif_linux_get_all_names,
518 dpif_linux_get_stats,
519 dpif_linux_get_drop_frags,
520 dpif_linux_set_drop_frags,
523 dpif_linux_port_query_by_number,
524 dpif_linux_port_query_by_name,
525 dpif_linux_port_list,
526 dpif_linux_port_poll,
527 dpif_linux_port_poll_wait,
528 dpif_linux_port_group_get,
529 dpif_linux_port_group_set,
533 dpif_linux_flow_flush,
534 dpif_linux_flow_list,
536 dpif_linux_recv_get_mask,
537 dpif_linux_recv_set_mask,
538 dpif_linux_get_sflow_probability,
539 dpif_linux_set_sflow_probability,
541 dpif_linux_recv_wait,
544 static int get_openvswitch_major(void);
545 static int get_major(const char *target);
548 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
550 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
551 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
555 lookup_minor(const char *name, int *minorp)
557 struct ethtool_drvinfo drvinfo;
563 sock = socket(AF_INET, SOCK_DGRAM, 0);
565 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
570 memset(&ifr, 0, sizeof ifr);
571 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
572 ifr.ifr_data = (caddr_t) &drvinfo;
574 memset(&drvinfo, 0, sizeof drvinfo);
575 drvinfo.cmd = ETHTOOL_GDRVINFO;
576 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
577 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
579 goto error_close_sock;
582 if (strcmp(drvinfo.driver, "openvswitch")) {
583 VLOG_WARN("%s is not an openvswitch device", name);
585 goto error_close_sock;
588 if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
589 VLOG_WARN("%s ethtool bus_info has unexpected format", name);
591 goto error_close_sock;
592 } else if (port_no != ODPP_LOCAL) {
593 /* This is an Open vSwitch device but not the local port. We
594 * intentionally support only using the name of the local port as the
595 * name of a datapath; otherwise, it would be too difficult to
596 * enumerate all the names of a datapath. */
598 goto error_close_sock;
612 make_openvswitch_device(int minor, char **fnp)
614 const char dirname[] = "/dev/net";
622 major = get_openvswitch_major();
626 dev = makedev(major, minor);
628 sprintf(fn, "%s/dp%d", dirname, minor);
630 if (!S_ISCHR(s.st_mode)) {
631 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
633 } else if (s.st_rdev != dev) {
634 VLOG_WARN_RL(&error_rl,
635 "%s is device %u:%u but should be %u:%u, fixing",
636 fn, major(s.st_rdev), minor(s.st_rdev),
637 major(dev), minor(dev));
642 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
643 fn, strerror(errno));
646 } else if (errno == ENOENT) {
647 if (stat(dirname, &s)) {
648 if (errno == ENOENT) {
649 if (mkdir(dirname, 0755)) {
650 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
651 dirname, strerror(errno));
655 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
656 dirname, strerror(errno));
661 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
665 /* The device needs to be created. */
666 if (mknod(fn, S_IFCHR | 0700, dev)) {
667 VLOG_WARN_RL(&error_rl,
668 "%s: creating character device %u:%u failed (%s)",
669 fn, major(dev), minor(dev), strerror(errno));
678 /* Return the major device number of the Open vSwitch device. If it
679 * cannot be determined, a negative errno is returned. */
681 get_openvswitch_major(void)
683 static int openvswitch_major = -1;
684 if (openvswitch_major < 0) {
685 openvswitch_major = get_major("openvswitch");
687 return openvswitch_major;
691 get_major(const char *target)
693 const char fn[] = "/proc/devices";
698 file = fopen(fn, "r");
700 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
704 for (ln = 1; fgets(line, sizeof line, file); ln++) {
708 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
710 } else if (!strncmp(line, "Block", 5)) {
711 /* We only want character devices, so skip the rest of the file. */
713 } else if (sscanf(line, "%d %63s", &major, name)) {
714 if (!strcmp(name, target)) {
721 VLOG_WARN("%s:%d: syntax error", fn, ln);
729 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
734 finish_open(struct dpif *dpif_, const char *local_ifname)
736 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
737 dpif->local_ifname = xstrdup(local_ifname);
738 dpif->local_ifindex = if_nametoindex(local_ifname);
739 if (!dpif->local_ifindex) {
741 dpif_uninit(dpif_, true);
742 VLOG_WARN("could not get ifindex of %s device: %s",
743 local_ifname, strerror(errno));
750 create_minor(const char *name, int minor, struct dpif **dpifp)
752 int error = open_minor(minor, dpifp);
754 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
756 error = finish_open(*dpifp, name);
758 dpif_uninit(*dpifp, true);
765 open_minor(int minor, struct dpif **dpifp)
771 error = make_openvswitch_device(minor, &fn);
776 fd = open(fn, O_RDONLY | O_NONBLOCK);
778 struct dpif_linux *dpif = xmalloc(sizeof *dpif);
779 error = rtnetlink_notifier_register(&dpif->port_notifier,
780 dpif_linux_port_changed, dpif);
784 name = xasprintf("dp%d", minor);
785 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
789 dpif->local_ifname = NULL;
791 dpif->local_ifindex = 0;
792 shash_init(&dpif->changed_ports);
793 dpif->change_error = false;
794 *dpifp = &dpif->dpif;
800 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
808 dpif_linux_port_changed(const struct rtnetlink_change *change, void *dpif_)
810 struct dpif_linux *dpif = dpif_;
813 if (change->master_ifindex == dpif->local_ifindex
814 && (change->nlmsg_type == RTM_NEWLINK
815 || change->nlmsg_type == RTM_DELLINK))
817 /* Our datapath changed, either adding a new port or deleting an
819 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
822 dpif->change_error = true;