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 "openvswitch/tunnel.h"
41 #include "poll-loop.h"
42 #include "rtnetlink.h"
43 #include "rtnetlink-link.h"
49 VLOG_DEFINE_THIS_MODULE(dpif_linux);
51 /* Datapath interface for the openvswitch Linux kernel module. */
56 /* Used by dpif_linux_get_all_names(). */
60 /* Change notification. */
61 int local_ifindex; /* Ifindex of local port. */
62 struct shash changed_ports; /* Ports that have changed. */
63 struct rtnetlink_notifier port_notifier;
67 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
69 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
70 static int lookup_minor(const char *name, int *minor);
71 static int finish_open(struct dpif *, const char *local_ifname);
72 static int get_openvswitch_major(void);
73 static int create_minor(const char *name, int minor, struct dpif **dpifp);
74 static int open_minor(int minor, struct dpif **dpifp);
75 static int make_openvswitch_device(int minor, char **fnp);
76 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
79 static struct dpif_linux *
80 dpif_linux_cast(const struct dpif *dpif)
82 dpif_assert_class(dpif, &dpif_linux_class);
83 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
87 dpif_linux_enumerate(struct svec *all_dps)
93 /* Check that the Open vSwitch module is loaded. */
94 major = get_openvswitch_major();
100 for (i = 0; i < ODP_MAX; i++) {
105 sprintf(devname, "dp%d", i);
106 retval = dpif_open(devname, "system", &dpif);
108 svec_add(all_dps, devname);
109 dpif_uninit(dpif, true);
110 } else if (retval != ENODEV && !error) {
118 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
119 bool create, struct dpif **dpifp)
123 minor = !strncmp(name, "dp", 2)
124 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
127 return create_minor(name, minor, dpifp);
129 /* Scan for unused minor number. */
130 for (minor = 0; minor < ODP_MAX; minor++) {
131 int error = create_minor(name, minor, dpifp);
132 if (error != EBUSY) {
137 /* All datapath numbers in use. */
141 struct dpif_linux *dpif;
142 struct odp_port port;
146 error = lookup_minor(name, &minor);
152 error = open_minor(minor, dpifp);
156 dpif = dpif_linux_cast(*dpifp);
158 /* We need the local port's ifindex for the poll function. Start by
159 * getting the local port's name. */
160 memset(&port, 0, sizeof port);
161 port.port = ODPP_LOCAL;
162 if (ioctl(dpif->fd, ODP_VPORT_QUERY, &port)) {
164 if (error != ENODEV) {
165 VLOG_WARN("%s: probe returned unexpected error: %s",
166 dpif_name(*dpifp), strerror(error));
168 dpif_uninit(*dpifp, true);
172 /* Then use that to finish up opening. */
173 return finish_open(&dpif->dpif, port.devname);
178 dpif_linux_close(struct dpif *dpif_)
180 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
181 rtnetlink_link_notifier_unregister(&dpif->port_notifier);
182 shash_destroy(&dpif->changed_ports);
183 free(dpif->local_ifname);
189 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
191 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
193 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
194 svec_add(all_names, dpif->local_ifname);
199 dpif_linux_destroy(struct dpif *dpif_)
201 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
205 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
207 memset(stats, 0, sizeof *stats);
208 return do_ioctl(dpif_, ODP_DP_STATS, stats);
212 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
217 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
219 *drop_fragsp = drop_frags & 1;
225 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
227 int drop_frags_int = drop_frags;
228 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
232 translate_vport_type_to_netdev_type(struct odp_port *port)
234 char *type = port->type;
236 if (!strcmp(type, "netdev")) {
237 ovs_strlcpy(type, "system", sizeof port->type);
238 } else if (!strcmp(type, "gre")) {
239 const struct tnl_port_config *config;
241 config = (struct tnl_port_config *)port->config;
242 if (config->flags & TNL_F_IPSEC) {
243 ovs_strlcpy(type, "ipsec_gre", sizeof port->type);
249 translate_netdev_type_to_vport_type(struct odp_port *port)
251 char *type = port->type;
253 if (!strcmp(type, "system")) {
254 ovs_strlcpy(type, "netdev", sizeof port->type);
255 } else if (!strcmp(type, "ipsec_gre")) {
256 ovs_strlcpy(type, "gre", sizeof port->type);
261 dpif_linux_port_add(struct dpif *dpif, struct netdev *netdev,
264 const char *name = netdev_get_name(netdev);
265 const char *type = netdev_get_type(netdev);
266 struct odp_port port;
269 memset(&port, 0, sizeof port);
270 strncpy(port.devname, name, sizeof port.devname);
271 strncpy(port.type, type, sizeof port.type);
272 netdev_vport_get_config(netdev, port.config);
273 translate_netdev_type_to_vport_type(&port);
275 error = do_ioctl(dpif, ODP_VPORT_ATTACH, &port);
277 *port_nop = port.port;
284 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no_)
286 int port_no = port_no_; /* Kernel expects an "int". */
287 return do_ioctl(dpif_, ODP_VPORT_DETACH, &port_no);
291 dpif_linux_port_query__(const struct dpif *dpif, struct odp_port *port)
293 int error = do_ioctl(dpif, ODP_VPORT_QUERY, port);
295 translate_vport_type_to_netdev_type(port);
301 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
302 struct odp_port *port)
304 memset(port, 0, sizeof *port);
305 port->port = port_no;
306 return dpif_linux_port_query__(dpif, port);
310 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
311 struct odp_port *port)
313 memset(port, 0, sizeof *port);
314 strncpy(port->devname, devname, sizeof port->devname);
315 return dpif_linux_port_query__(dpif, port);
319 dpif_linux_flow_flush(struct dpif *dpif_)
321 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
325 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
327 struct odp_portvec pv;
333 error = do_ioctl(dpif_, ODP_VPORT_LIST, &pv);
338 for (i = 0; i < pv.n_ports; i++) {
339 struct odp_port *port = &pv.ports[i];
341 translate_vport_type_to_netdev_type(port);
347 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
349 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
351 if (dpif->change_error) {
352 dpif->change_error = false;
353 shash_clear(&dpif->changed_ports);
355 } else if (!shash_is_empty(&dpif->changed_ports)) {
356 struct shash_node *node = shash_first(&dpif->changed_ports);
357 *devnamep = shash_steal(&dpif->changed_ports, node);
365 dpif_linux_port_poll_wait(const struct dpif *dpif_)
367 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
368 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
369 poll_immediate_wake();
371 rtnetlink_link_notifier_wait();
376 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
378 struct odp_flowvec fv;
381 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
385 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
387 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
391 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
393 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
397 dpif_linux_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
399 *statep = xzalloc(sizeof(struct odp_flow_dump));
404 dpif_linux_flow_dump_next(const struct dpif *dpif, void *state,
405 struct odp_flow *flow)
407 struct odp_flow_dump *dump = state;
411 error = do_ioctl(dpif, ODP_FLOW_DUMP, dump);
412 return error ? error : flow->flags & ODPFF_EOF ? EOF : 0;
416 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
423 dpif_linux_execute(struct dpif *dpif_,
424 const struct nlattr *actions, size_t actions_len,
425 const struct ofpbuf *buf)
427 struct odp_execute execute;
428 memset(&execute, 0, sizeof execute);
429 execute.actions = (struct nlattr *) actions;
430 execute.actions_len = actions_len;
431 execute.data = buf->data;
432 execute.length = buf->size;
433 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
437 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
439 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
443 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
445 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
449 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
450 uint32_t *probability)
452 return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
456 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
458 return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
462 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
463 uint32_t queue_id, uint32_t *priority)
465 if (queue_id < 0xf000) {
466 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
474 dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
476 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
481 buf = ofpbuf_new_with_headroom(65536, DPIF_RECV_MSG_PADDING);
482 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
485 if (error != EAGAIN) {
486 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
487 dpif_name(dpif_), strerror(error));
489 } else if (retval >= sizeof(struct odp_msg)) {
490 struct odp_msg *msg = buf->data;
491 if (msg->length <= retval) {
496 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
497 "from %"PRIu32" bytes to %d",
498 dpif_name(dpif_), msg->length, retval);
501 } else if (!retval) {
502 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
505 VLOG_WARN_RL(&error_rl,
506 "%s: discarding too-short message (%d bytes)",
507 dpif_name(dpif_), retval);
517 dpif_linux_recv_wait(struct dpif *dpif_)
519 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
520 poll_fd_wait(dpif->fd, POLLIN);
523 const struct dpif_class dpif_linux_class = {
527 dpif_linux_enumerate,
530 dpif_linux_get_all_names,
532 dpif_linux_get_stats,
533 dpif_linux_get_drop_frags,
534 dpif_linux_set_drop_frags,
537 dpif_linux_port_query_by_number,
538 dpif_linux_port_query_by_name,
539 dpif_linux_port_list,
540 dpif_linux_port_poll,
541 dpif_linux_port_poll_wait,
545 dpif_linux_flow_flush,
546 dpif_linux_flow_dump_start,
547 dpif_linux_flow_dump_next,
548 dpif_linux_flow_dump_done,
550 dpif_linux_recv_get_mask,
551 dpif_linux_recv_set_mask,
552 dpif_linux_get_sflow_probability,
553 dpif_linux_set_sflow_probability,
554 dpif_linux_queue_to_priority,
556 dpif_linux_recv_wait,
559 static int get_openvswitch_major(void);
560 static int get_major(const char *target);
563 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
565 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
566 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
570 lookup_minor(const char *name, int *minorp)
572 struct ethtool_drvinfo drvinfo;
578 sock = socket(AF_INET, SOCK_DGRAM, 0);
580 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
585 memset(&ifr, 0, sizeof ifr);
586 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
587 ifr.ifr_data = (caddr_t) &drvinfo;
589 memset(&drvinfo, 0, sizeof drvinfo);
590 drvinfo.cmd = ETHTOOL_GDRVINFO;
591 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
592 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
594 goto error_close_sock;
597 if (strcmp(drvinfo.driver, "openvswitch")) {
598 VLOG_WARN("%s is not an openvswitch device", name);
600 goto error_close_sock;
603 if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
604 VLOG_WARN("%s ethtool bus_info has unexpected format", name);
606 goto error_close_sock;
607 } else if (port_no != ODPP_LOCAL) {
608 /* This is an Open vSwitch device but not the local port. We
609 * intentionally support only using the name of the local port as the
610 * name of a datapath; otherwise, it would be too difficult to
611 * enumerate all the names of a datapath. */
613 goto error_close_sock;
627 make_openvswitch_device(int minor, char **fnp)
629 const char dirname[] = "/dev/net";
637 major = get_openvswitch_major();
641 dev = makedev(major, minor);
643 sprintf(fn, "%s/dp%d", dirname, minor);
645 if (!S_ISCHR(s.st_mode)) {
646 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
648 } else if (s.st_rdev != dev) {
649 VLOG_WARN_RL(&error_rl,
650 "%s is device %u:%u but should be %u:%u, fixing",
651 fn, major(s.st_rdev), minor(s.st_rdev),
652 major(dev), minor(dev));
657 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
658 fn, strerror(errno));
661 } else if (errno == ENOENT) {
662 if (stat(dirname, &s)) {
663 if (errno == ENOENT) {
664 if (mkdir(dirname, 0755)) {
665 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
666 dirname, strerror(errno));
670 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
671 dirname, strerror(errno));
676 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
680 /* The device needs to be created. */
681 if (mknod(fn, S_IFCHR | 0700, dev)) {
682 VLOG_WARN_RL(&error_rl,
683 "%s: creating character device %u:%u failed (%s)",
684 fn, major(dev), minor(dev), strerror(errno));
693 /* Return the major device number of the Open vSwitch device. If it
694 * cannot be determined, a negative errno is returned. */
696 get_openvswitch_major(void)
698 static int openvswitch_major = -1;
699 if (openvswitch_major < 0) {
700 openvswitch_major = get_major("openvswitch");
702 return openvswitch_major;
706 get_major(const char *target)
708 const char fn[] = "/proc/devices";
713 file = fopen(fn, "r");
715 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
719 for (ln = 1; fgets(line, sizeof line, file); ln++) {
723 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
725 } else if (!strncmp(line, "Block", 5)) {
726 /* We only want character devices, so skip the rest of the file. */
728 } else if (sscanf(line, "%d %63s", &major, name)) {
729 if (!strcmp(name, target)) {
734 VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
740 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
745 finish_open(struct dpif *dpif_, const char *local_ifname)
747 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
748 dpif->local_ifname = xstrdup(local_ifname);
749 dpif->local_ifindex = if_nametoindex(local_ifname);
750 if (!dpif->local_ifindex) {
752 dpif_uninit(dpif_, true);
753 VLOG_WARN("could not get ifindex of %s device: %s",
754 local_ifname, strerror(errno));
761 create_minor(const char *name, int minor, struct dpif **dpifp)
763 int error = open_minor(minor, dpifp);
765 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
767 error = finish_open(*dpifp, name);
769 dpif_uninit(*dpifp, true);
776 open_minor(int minor, struct dpif **dpifp)
782 error = make_openvswitch_device(minor, &fn);
787 fd = open(fn, O_RDONLY | O_NONBLOCK);
789 struct dpif_linux *dpif = xmalloc(sizeof *dpif);
790 error = rtnetlink_link_notifier_register(&dpif->port_notifier,
791 dpif_linux_port_changed,
796 name = xasprintf("dp%d", minor);
797 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
801 dpif->local_ifname = NULL;
803 dpif->local_ifindex = 0;
804 shash_init(&dpif->changed_ports);
805 dpif->change_error = false;
806 *dpifp = &dpif->dpif;
812 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
820 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
823 struct dpif_linux *dpif = dpif_;
826 if (change->master_ifindex == dpif->local_ifindex
827 && (change->nlmsg_type == RTM_NEWLINK
828 || change->nlmsg_type == RTM_DELLINK))
830 /* Our datapath changed, either adding a new port or deleting an
832 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
835 dpif->change_error = true;