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"
42 #define THIS_MODULE VLM_dpif_linux
44 /* Datapath interface for the openvswitch Linux kernel module. */
49 /* Used by dpif_linux_get_all_names(). */
53 /* Change notification. */
54 int local_ifindex; /* Ifindex of local port. */
55 struct svec changed_ports; /* Ports that have changed. */
56 struct rtnetlink_notifier port_notifier;
60 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
62 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
63 static int lookup_minor(const char *name, int *minor);
64 static int finish_open(struct dpif *, const char *local_ifname);
65 static int get_openvswitch_major(void);
66 static int create_minor(const char *name, int minor, struct dpif **dpifp);
67 static int open_minor(int minor, struct dpif **dpifp);
68 static int make_openvswitch_device(int minor, char **fnp);
69 static void dpif_linux_port_changed(const struct rtnetlink_change *,
72 static struct dpif_linux *
73 dpif_linux_cast(const struct dpif *dpif)
75 dpif_assert_class(dpif, &dpif_linux_class);
76 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
80 dpif_linux_enumerate(struct svec *all_dps)
86 /* Check that the Open vSwitch module is loaded. */
87 major = get_openvswitch_major();
93 for (i = 0; i < ODP_MAX; i++) {
98 sprintf(devname, "dp%d", i);
99 retval = dpif_open(devname, "system", &dpif);
101 svec_add(all_dps, devname);
102 dpif_uninit(dpif, true);
103 } else if (retval != ENODEV && !error) {
111 dpif_linux_open(const char *name, const char *type OVS_UNUSED, bool create,
116 minor = !strncmp(name, "dp", 2)
117 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
120 return create_minor(name, minor, dpifp);
122 /* Scan for unused minor number. */
123 for (minor = 0; minor < ODP_MAX; minor++) {
124 int error = create_minor(name, minor, dpifp);
125 if (error != EBUSY) {
130 /* All datapath numbers in use. */
134 struct dpif_linux *dpif;
135 struct odp_port port;
139 error = lookup_minor(name, &minor);
145 error = open_minor(minor, dpifp);
149 dpif = dpif_linux_cast(*dpifp);
151 /* We need the local port's ifindex for the poll function. Start by
152 * getting the local port's name. */
153 memset(&port, 0, sizeof port);
154 port.port = ODPP_LOCAL;
155 if (ioctl(dpif->fd, ODP_PORT_QUERY, &port)) {
157 if (error != ENODEV) {
158 VLOG_WARN("%s: probe returned unexpected error: %s",
159 dpif_name(*dpifp), strerror(error));
161 dpif_uninit(*dpifp, true);
165 /* Then use that to finish up opening. */
166 return finish_open(&dpif->dpif, port.devname);
171 dpif_linux_close(struct dpif *dpif_)
173 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
174 rtnetlink_notifier_unregister(&dpif->port_notifier);
175 svec_destroy(&dpif->changed_ports);
176 free(dpif->local_ifname);
182 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
184 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
186 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
187 svec_add(all_names, dpif->local_ifname);
192 dpif_linux_destroy(struct dpif *dpif_)
194 struct odp_port *ports;
199 err = dpif_port_list(dpif_, &ports, &n_ports);
204 for (i = 0; i < n_ports; i++) {
205 if (ports[i].port != ODPP_LOCAL) {
206 err = do_ioctl(dpif_, ODP_VPORT_DEL, ports[i].devname);
208 VLOG_WARN_RL(&error_rl, "%s: error deleting port %s (%s)",
209 dpif_name(dpif_), ports[i].devname, strerror(err));
216 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
220 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
222 memset(stats, 0, sizeof *stats);
223 return do_ioctl(dpif_, ODP_DP_STATS, stats);
227 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
232 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
234 *drop_fragsp = drop_frags & 1;
240 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
242 int drop_frags_int = drop_frags;
243 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
247 dpif_linux_port_add(struct dpif *dpif_, const char *devname, uint16_t flags,
250 struct odp_port port;
253 memset(&port, 0, sizeof port);
254 strncpy(port.devname, devname, sizeof port.devname);
256 error = do_ioctl(dpif_, ODP_PORT_ATTACH, &port);
258 *port_no = port.port;
264 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
268 struct odp_port port;
270 err = dpif_port_query_by_number(dpif_, port_no, &port);
275 err = do_ioctl(dpif_, ODP_PORT_DETACH, &tmp);
280 if (!netdev_is_open(port.devname)) {
281 /* Try deleting the port if no one has it open. This shouldn't
282 * actually be necessary unless the config changed while we weren't
283 * running but it won't hurt anything if the port is already gone. */
284 do_ioctl(dpif_, ODP_VPORT_DEL, port.devname);
291 dpif_linux_port_query_by_number(const struct dpif *dpif_, uint16_t port_no,
292 struct odp_port *port)
294 memset(port, 0, sizeof *port);
295 port->port = port_no;
296 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
300 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
301 struct odp_port *port)
303 memset(port, 0, sizeof *port);
304 strncpy(port->devname, devname, sizeof port->devname);
305 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
309 dpif_linux_flow_flush(struct dpif *dpif_)
311 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
315 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
317 struct odp_portvec pv;
322 error = do_ioctl(dpif_, ODP_PORT_LIST, &pv);
323 return error ? -error : pv.n_ports;
327 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
329 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
331 if (dpif->change_error) {
332 dpif->change_error = false;
333 svec_clear(&dpif->changed_ports);
335 } else if (dpif->changed_ports.n) {
336 *devnamep = dpif->changed_ports.names[--dpif->changed_ports.n];
344 dpif_linux_port_poll_wait(const struct dpif *dpif_)
346 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
347 if (dpif->changed_ports.n || dpif->change_error) {
348 poll_immediate_wake();
350 rtnetlink_notifier_wait();
355 dpif_linux_port_group_get(const struct dpif *dpif_, int group,
356 uint16_t ports[], int n)
358 struct odp_port_group pg;
361 assert(n <= UINT16_MAX);
365 error = do_ioctl(dpif_, ODP_PORT_GROUP_GET, &pg);
366 return error ? -error : pg.n_ports;
370 dpif_linux_port_group_set(struct dpif *dpif_, int group,
371 const uint16_t ports[], int n)
373 struct odp_port_group pg;
375 assert(n <= UINT16_MAX);
377 pg.ports = (uint16_t *) ports;
379 return do_ioctl(dpif_, ODP_PORT_GROUP_SET, &pg);
383 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
385 struct odp_flowvec fv;
388 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
392 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
394 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
398 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
400 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
404 dpif_linux_flow_list(const struct dpif *dpif_, struct odp_flow flows[], int n)
406 struct odp_flowvec fv;
411 error = do_ioctl(dpif_, ODP_FLOW_LIST, &fv);
412 return error ? -error : fv.n_flows;
416 dpif_linux_execute(struct dpif *dpif_, uint16_t in_port,
417 const union odp_action actions[], int n_actions,
418 const struct ofpbuf *buf)
420 struct odp_execute execute;
421 memset(&execute, 0, sizeof execute);
422 execute.in_port = in_port;
423 execute.actions = (union odp_action *) actions;
424 execute.n_actions = n_actions;
425 execute.data = buf->data;
426 execute.length = buf->size;
427 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
431 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
433 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
437 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
439 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
443 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
444 uint32_t *probability)
446 return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
450 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
452 return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
456 dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
458 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
463 buf = ofpbuf_new(65536);
464 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
467 if (error != EAGAIN) {
468 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
469 dpif_name(dpif_), strerror(error));
471 } else if (retval >= sizeof(struct odp_msg)) {
472 struct odp_msg *msg = buf->data;
473 if (msg->length <= retval) {
478 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
479 "from %"PRIu32" bytes to %d",
480 dpif_name(dpif_), msg->length, retval);
483 } else if (!retval) {
484 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
487 VLOG_WARN_RL(&error_rl,
488 "%s: discarding too-short message (%d bytes)",
489 dpif_name(dpif_), retval);
499 dpif_linux_recv_wait(struct dpif *dpif_)
501 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
502 poll_fd_wait(dpif->fd, POLLIN);
505 const struct dpif_class dpif_linux_class = {
509 dpif_linux_enumerate,
512 dpif_linux_get_all_names,
514 dpif_linux_get_stats,
515 dpif_linux_get_drop_frags,
516 dpif_linux_set_drop_frags,
519 dpif_linux_port_query_by_number,
520 dpif_linux_port_query_by_name,
521 dpif_linux_port_list,
522 dpif_linux_port_poll,
523 dpif_linux_port_poll_wait,
524 dpif_linux_port_group_get,
525 dpif_linux_port_group_set,
529 dpif_linux_flow_flush,
530 dpif_linux_flow_list,
532 dpif_linux_recv_get_mask,
533 dpif_linux_recv_set_mask,
534 dpif_linux_get_sflow_probability,
535 dpif_linux_set_sflow_probability,
537 dpif_linux_recv_wait,
540 static int get_openvswitch_major(void);
541 static int get_major(const char *target);
544 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
546 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
547 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
551 lookup_minor(const char *name, int *minorp)
553 struct ethtool_drvinfo drvinfo;
559 sock = socket(AF_INET, SOCK_DGRAM, 0);
561 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
566 memset(&ifr, 0, sizeof ifr);
567 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
568 ifr.ifr_data = (caddr_t) &drvinfo;
570 memset(&drvinfo, 0, sizeof drvinfo);
571 drvinfo.cmd = ETHTOOL_GDRVINFO;
572 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
573 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
575 goto error_close_sock;
578 if (strcmp(drvinfo.driver, "openvswitch")) {
579 VLOG_WARN("%s is not an openvswitch device", name);
581 goto error_close_sock;
584 if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
585 VLOG_WARN("%s ethtool bus_info has unexpected format", name);
587 goto error_close_sock;
588 } else if (port_no != ODPP_LOCAL) {
589 /* This is an Open vSwitch device but not the local port. We
590 * intentionally support only using the name of the local port as the
591 * name of a datapath; otherwise, it would be too difficult to
592 * enumerate all the names of a datapath. */
594 goto error_close_sock;
608 make_openvswitch_device(int minor, char **fnp)
610 const char dirname[] = "/dev/net";
618 major = get_openvswitch_major();
622 dev = makedev(major, minor);
624 sprintf(fn, "%s/dp%d", dirname, minor);
626 if (!S_ISCHR(s.st_mode)) {
627 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
629 } else if (s.st_rdev != dev) {
630 VLOG_WARN_RL(&error_rl,
631 "%s is device %u:%u but should be %u:%u, fixing",
632 fn, major(s.st_rdev), minor(s.st_rdev),
633 major(dev), minor(dev));
638 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
639 fn, strerror(errno));
642 } else if (errno == ENOENT) {
643 if (stat(dirname, &s)) {
644 if (errno == ENOENT) {
645 if (mkdir(dirname, 0755)) {
646 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
647 dirname, strerror(errno));
651 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
652 dirname, strerror(errno));
657 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
661 /* The device needs to be created. */
662 if (mknod(fn, S_IFCHR | 0700, dev)) {
663 VLOG_WARN_RL(&error_rl,
664 "%s: creating character device %u:%u failed (%s)",
665 fn, major(dev), minor(dev), strerror(errno));
674 /* Return the major device number of the Open vSwitch device. If it
675 * cannot be determined, a negative errno is returned. */
677 get_openvswitch_major(void)
679 static int openvswitch_major = -1;
680 if (openvswitch_major < 0) {
681 openvswitch_major = get_major("openvswitch");
683 return openvswitch_major;
687 get_major(const char *target)
689 const char fn[] = "/proc/devices";
694 file = fopen(fn, "r");
696 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
700 for (ln = 1; fgets(line, sizeof line, file); ln++) {
704 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
706 } else if (!strncmp(line, "Block", 5)) {
707 /* We only want character devices, so skip the rest of the file. */
709 } else if (sscanf(line, "%d %63s", &major, name)) {
710 if (!strcmp(name, target)) {
717 VLOG_WARN("%s:%d: syntax error", fn, ln);
725 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
730 finish_open(struct dpif *dpif_, const char *local_ifname)
732 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
733 dpif->local_ifname = xstrdup(local_ifname);
734 dpif->local_ifindex = if_nametoindex(local_ifname);
735 if (!dpif->local_ifindex) {
737 dpif_uninit(dpif_, true);
738 VLOG_WARN("could not get ifindex of %s device: %s",
739 local_ifname, strerror(errno));
746 create_minor(const char *name, int minor, struct dpif **dpifp)
748 int error = open_minor(minor, dpifp);
750 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
752 error = finish_open(*dpifp, name);
754 dpif_uninit(*dpifp, true);
761 open_minor(int minor, struct dpif **dpifp)
767 error = make_openvswitch_device(minor, &fn);
772 fd = open(fn, O_RDONLY | O_NONBLOCK);
774 struct dpif_linux *dpif = xmalloc(sizeof *dpif);
775 error = rtnetlink_notifier_register(&dpif->port_notifier,
776 dpif_linux_port_changed, dpif);
780 name = xasprintf("dp%d", minor);
781 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
785 dpif->local_ifname = NULL;
787 dpif->local_ifindex = 0;
788 svec_init(&dpif->changed_ports);
789 dpif->change_error = false;
790 *dpifp = &dpif->dpif;
796 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
804 dpif_linux_port_changed(const struct rtnetlink_change *change, void *dpif_)
806 struct dpif_linux *dpif = dpif_;
809 if (change->master_ifindex == dpif->local_ifindex
810 && (change->nlmsg_type == RTM_NEWLINK
811 || change->nlmsg_type == RTM_DELLINK))
813 /* Our datapath changed, either adding a new port or deleting an
815 if (!svec_contains(&dpif->changed_ports, change->ifname)) {
816 svec_add(&dpif->changed_ports, change->ifname);
817 svec_sort(&dpif->changed_ports);
821 dpif->change_error = true;