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"
48 VLOG_DEFINE_THIS_MODULE(dpif_linux);
50 /* Datapath interface for the openvswitch Linux kernel module. */
55 /* Used by dpif_linux_get_all_names(). */
59 /* Change notification. */
60 int local_ifindex; /* Ifindex of local port. */
61 struct shash changed_ports; /* Ports that have changed. */
62 struct rtnetlink_notifier port_notifier;
66 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
68 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
69 static int lookup_minor(const char *name, int *minor);
70 static int finish_open(struct dpif *, const char *local_ifname);
71 static int get_openvswitch_major(void);
72 static int create_minor(const char *name, int minor, struct dpif **dpifp);
73 static int open_minor(int minor, struct dpif **dpifp);
74 static int make_openvswitch_device(int minor, char **fnp);
75 static void dpif_linux_port_changed(const struct rtnetlink_change *,
78 static struct dpif_linux *
79 dpif_linux_cast(const struct dpif *dpif)
81 dpif_assert_class(dpif, &dpif_linux_class);
82 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
86 dpif_linux_enumerate(struct svec *all_dps)
92 /* Check that the Open vSwitch module is loaded. */
93 major = get_openvswitch_major();
99 for (i = 0; i < ODP_MAX; i++) {
104 sprintf(devname, "dp%d", i);
105 retval = dpif_open(devname, "system", &dpif);
107 svec_add(all_dps, devname);
108 dpif_uninit(dpif, true);
109 } else if (retval != ENODEV && !error) {
117 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
118 bool create, struct dpif **dpifp)
122 minor = !strncmp(name, "dp", 2)
123 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
126 return create_minor(name, minor, dpifp);
128 /* Scan for unused minor number. */
129 for (minor = 0; minor < ODP_MAX; minor++) {
130 int error = create_minor(name, minor, dpifp);
131 if (error != EBUSY) {
136 /* All datapath numbers in use. */
140 struct dpif_linux *dpif;
141 struct odp_port port;
145 error = lookup_minor(name, &minor);
151 error = open_minor(minor, dpifp);
155 dpif = dpif_linux_cast(*dpifp);
157 /* We need the local port's ifindex for the poll function. Start by
158 * getting the local port's name. */
159 memset(&port, 0, sizeof port);
160 port.port = ODPP_LOCAL;
161 if (ioctl(dpif->fd, ODP_VPORT_QUERY, &port)) {
163 if (error != ENODEV) {
164 VLOG_WARN("%s: probe returned unexpected error: %s",
165 dpif_name(*dpifp), strerror(error));
167 dpif_uninit(*dpifp, true);
171 /* Then use that to finish up opening. */
172 return finish_open(&dpif->dpif, port.devname);
177 dpif_linux_close(struct dpif *dpif_)
179 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
180 rtnetlink_notifier_unregister(&dpif->port_notifier);
181 shash_destroy(&dpif->changed_ports);
182 free(dpif->local_ifname);
188 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
190 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
192 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
193 svec_add(all_names, dpif->local_ifname);
198 dpif_linux_destroy(struct dpif *dpif_)
200 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
204 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
206 memset(stats, 0, sizeof *stats);
207 return do_ioctl(dpif_, ODP_DP_STATS, stats);
211 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
216 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
218 *drop_fragsp = drop_frags & 1;
224 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
226 int drop_frags_int = drop_frags;
227 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
231 translate_vport_type_to_netdev_type(struct odp_port *port)
233 char *type = port->type;
235 if (!strcmp(type, "netdev")) {
236 ovs_strlcpy(type, "system", sizeof port->type);
237 } else if (!strcmp(type, "gre")) {
238 const struct tnl_port_config *config;
240 config = (struct tnl_port_config *)port->config;
241 if (config->flags & TNL_F_IPSEC) {
242 ovs_strlcpy(type, "ipsec_gre", sizeof port->type);
248 translate_netdev_type_to_vport_type(struct odp_port *port)
250 char *type = port->type;
252 if (!strcmp(type, "system")) {
253 ovs_strlcpy(type, "netdev", sizeof port->type);
254 } else if (!strcmp(type, "ipsec_gre")) {
255 ovs_strlcpy(type, "gre", sizeof port->type);
260 dpif_linux_port_add(struct dpif *dpif, struct netdev *netdev,
263 const char *name = netdev_get_name(netdev);
264 const char *type = netdev_get_type(netdev);
265 struct odp_port port;
268 memset(&port, 0, sizeof port);
269 strncpy(port.devname, name, sizeof port.devname);
270 strncpy(port.type, type, sizeof port.type);
271 netdev_vport_get_config(netdev, port.config);
272 translate_netdev_type_to_vport_type(&port);
274 error = do_ioctl(dpif, ODP_VPORT_ATTACH, &port);
276 *port_nop = port.port;
283 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no_)
285 int port_no = port_no_; /* Kernel expects an "int". */
286 return do_ioctl(dpif_, ODP_VPORT_DETACH, &port_no);
290 dpif_linux_port_query__(const struct dpif *dpif, struct odp_port *port)
292 int error = do_ioctl(dpif, ODP_VPORT_QUERY, port);
294 translate_vport_type_to_netdev_type(port);
300 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
301 struct odp_port *port)
303 memset(port, 0, sizeof *port);
304 port->port = port_no;
305 return dpif_linux_port_query__(dpif, port);
309 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
310 struct odp_port *port)
312 memset(port, 0, sizeof *port);
313 strncpy(port->devname, devname, sizeof port->devname);
314 return dpif_linux_port_query__(dpif, port);
318 dpif_linux_flow_flush(struct dpif *dpif_)
320 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
324 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
326 struct odp_portvec pv;
332 error = do_ioctl(dpif_, ODP_VPORT_LIST, &pv);
337 for (i = 0; i < pv.n_ports; i++) {
338 struct odp_port *port = &pv.ports[i];
340 translate_vport_type_to_netdev_type(port);
346 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
348 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
350 if (dpif->change_error) {
351 dpif->change_error = false;
352 shash_clear(&dpif->changed_ports);
354 } else if (!shash_is_empty(&dpif->changed_ports)) {
355 struct shash_node *node = shash_first(&dpif->changed_ports);
356 *devnamep = shash_steal(&dpif->changed_ports, node);
364 dpif_linux_port_poll_wait(const struct dpif *dpif_)
366 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
367 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
368 poll_immediate_wake();
370 rtnetlink_notifier_wait();
375 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
377 struct odp_flowvec fv;
380 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
384 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
386 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
390 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
392 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
396 dpif_linux_flow_list(const struct dpif *dpif_, struct odp_flow flows[], int n)
398 struct odp_flowvec fv;
403 error = do_ioctl(dpif_, ODP_FLOW_LIST, &fv);
404 return error ? -error : fv.n_flows;
408 dpif_linux_execute(struct dpif *dpif_,
409 const struct nlattr *actions, size_t actions_len,
410 const struct ofpbuf *buf)
412 struct odp_execute execute;
413 memset(&execute, 0, sizeof execute);
414 execute.actions = (struct nlattr *) actions;
415 execute.actions_len = actions_len;
416 execute.data = buf->data;
417 execute.length = buf->size;
418 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
422 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
424 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
428 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
430 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
434 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
435 uint32_t *probability)
437 return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
441 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
443 return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
447 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
448 uint32_t queue_id, uint32_t *priority)
450 if (queue_id < 0xf000) {
451 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
459 dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
461 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
466 buf = ofpbuf_new_with_headroom(65536, DPIF_RECV_MSG_PADDING);
467 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
470 if (error != EAGAIN) {
471 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
472 dpif_name(dpif_), strerror(error));
474 } else if (retval >= sizeof(struct odp_msg)) {
475 struct odp_msg *msg = buf->data;
476 if (msg->length <= retval) {
481 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
482 "from %"PRIu32" bytes to %d",
483 dpif_name(dpif_), msg->length, retval);
486 } else if (!retval) {
487 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
490 VLOG_WARN_RL(&error_rl,
491 "%s: discarding too-short message (%d bytes)",
492 dpif_name(dpif_), retval);
502 dpif_linux_recv_wait(struct dpif *dpif_)
504 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
505 poll_fd_wait(dpif->fd, POLLIN);
508 const struct dpif_class dpif_linux_class = {
512 dpif_linux_enumerate,
515 dpif_linux_get_all_names,
517 dpif_linux_get_stats,
518 dpif_linux_get_drop_frags,
519 dpif_linux_set_drop_frags,
522 dpif_linux_port_query_by_number,
523 dpif_linux_port_query_by_name,
524 dpif_linux_port_list,
525 dpif_linux_port_poll,
526 dpif_linux_port_poll_wait,
530 dpif_linux_flow_flush,
531 dpif_linux_flow_list,
533 dpif_linux_recv_get_mask,
534 dpif_linux_recv_set_mask,
535 dpif_linux_get_sflow_probability,
536 dpif_linux_set_sflow_probability,
537 dpif_linux_queue_to_priority,
539 dpif_linux_recv_wait,
542 static int get_openvswitch_major(void);
543 static int get_major(const char *target);
546 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
548 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
549 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
553 lookup_minor(const char *name, int *minorp)
555 struct ethtool_drvinfo drvinfo;
561 sock = socket(AF_INET, SOCK_DGRAM, 0);
563 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
568 memset(&ifr, 0, sizeof ifr);
569 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
570 ifr.ifr_data = (caddr_t) &drvinfo;
572 memset(&drvinfo, 0, sizeof drvinfo);
573 drvinfo.cmd = ETHTOOL_GDRVINFO;
574 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
575 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
577 goto error_close_sock;
580 if (strcmp(drvinfo.driver, "openvswitch")) {
581 VLOG_WARN("%s is not an openvswitch device", name);
583 goto error_close_sock;
586 if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
587 VLOG_WARN("%s ethtool bus_info has unexpected format", name);
589 goto error_close_sock;
590 } else if (port_no != ODPP_LOCAL) {
591 /* This is an Open vSwitch device but not the local port. We
592 * intentionally support only using the name of the local port as the
593 * name of a datapath; otherwise, it would be too difficult to
594 * enumerate all the names of a datapath. */
596 goto error_close_sock;
610 make_openvswitch_device(int minor, char **fnp)
612 const char dirname[] = "/dev/net";
620 major = get_openvswitch_major();
624 dev = makedev(major, minor);
626 sprintf(fn, "%s/dp%d", dirname, minor);
628 if (!S_ISCHR(s.st_mode)) {
629 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
631 } else if (s.st_rdev != dev) {
632 VLOG_WARN_RL(&error_rl,
633 "%s is device %u:%u but should be %u:%u, fixing",
634 fn, major(s.st_rdev), minor(s.st_rdev),
635 major(dev), minor(dev));
640 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
641 fn, strerror(errno));
644 } else if (errno == ENOENT) {
645 if (stat(dirname, &s)) {
646 if (errno == ENOENT) {
647 if (mkdir(dirname, 0755)) {
648 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
649 dirname, strerror(errno));
653 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
654 dirname, strerror(errno));
659 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
663 /* The device needs to be created. */
664 if (mknod(fn, S_IFCHR | 0700, dev)) {
665 VLOG_WARN_RL(&error_rl,
666 "%s: creating character device %u:%u failed (%s)",
667 fn, major(dev), minor(dev), strerror(errno));
676 /* Return the major device number of the Open vSwitch device. If it
677 * cannot be determined, a negative errno is returned. */
679 get_openvswitch_major(void)
681 static int openvswitch_major = -1;
682 if (openvswitch_major < 0) {
683 openvswitch_major = get_major("openvswitch");
685 return openvswitch_major;
689 get_major(const char *target)
691 const char fn[] = "/proc/devices";
696 file = fopen(fn, "r");
698 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
702 for (ln = 1; fgets(line, sizeof line, file); ln++) {
706 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
708 } else if (!strncmp(line, "Block", 5)) {
709 /* We only want character devices, so skip the rest of the file. */
711 } else if (sscanf(line, "%d %63s", &major, name)) {
712 if (!strcmp(name, target)) {
717 VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
723 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
728 finish_open(struct dpif *dpif_, const char *local_ifname)
730 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
731 dpif->local_ifname = xstrdup(local_ifname);
732 dpif->local_ifindex = if_nametoindex(local_ifname);
733 if (!dpif->local_ifindex) {
735 dpif_uninit(dpif_, true);
736 VLOG_WARN("could not get ifindex of %s device: %s",
737 local_ifname, strerror(errno));
744 create_minor(const char *name, int minor, struct dpif **dpifp)
746 int error = open_minor(minor, dpifp);
748 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
750 error = finish_open(*dpifp, name);
752 dpif_uninit(*dpifp, true);
759 open_minor(int minor, struct dpif **dpifp)
765 error = make_openvswitch_device(minor, &fn);
770 fd = open(fn, O_RDONLY | O_NONBLOCK);
772 struct dpif_linux *dpif = xmalloc(sizeof *dpif);
773 error = rtnetlink_notifier_register(&dpif->port_notifier,
774 dpif_linux_port_changed, dpif);
778 name = xasprintf("dp%d", minor);
779 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
783 dpif->local_ifname = NULL;
785 dpif->local_ifindex = 0;
786 shash_init(&dpif->changed_ports);
787 dpif->change_error = false;
788 *dpifp = &dpif->dpif;
794 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
802 dpif_linux_port_changed(const struct rtnetlink_change *change, void *dpif_)
804 struct dpif_linux *dpif = dpif_;
807 if (change->master_ifindex == dpif->local_ifindex
808 && (change->nlmsg_type == RTM_NEWLINK
809 || change->nlmsg_type == RTM_DELLINK))
811 /* Our datapath changed, either adding a new port or deleting an
813 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
816 dpif->change_error = true;