2 * Copyright (c) 2008, 2009, 2010, 2011 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"
41 #include "openvswitch/tunnel.h"
43 #include "poll-loop.h"
44 #include "rtnetlink.h"
45 #include "rtnetlink-link.h"
51 VLOG_DEFINE_THIS_MODULE(dpif_linux);
53 /* Datapath interface for the openvswitch Linux kernel module. */
58 /* Used by dpif_linux_get_all_names(). */
62 /* Change notification. */
63 int local_ifindex; /* Ifindex of local port. */
64 struct shash changed_ports; /* Ports that have changed. */
65 struct rtnetlink_notifier port_notifier;
69 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
71 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
72 static int lookup_minor(const char *name, int *minor);
73 static int finish_open(struct dpif *, const char *local_ifname);
74 static int get_openvswitch_major(void);
75 static int create_minor(const char *name, int minor, struct dpif **dpifp);
76 static int open_minor(int minor, struct dpif **dpifp);
77 static int make_openvswitch_device(int minor, char **fnp);
78 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
81 static struct dpif_linux *
82 dpif_linux_cast(const struct dpif *dpif)
84 dpif_assert_class(dpif, &dpif_linux_class);
85 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
89 dpif_linux_enumerate(struct svec *all_dps)
95 /* Check that the Open vSwitch module is loaded. */
96 major = get_openvswitch_major();
102 for (i = 0; i < ODP_MAX; i++) {
107 sprintf(devname, "dp%d", i);
108 retval = dpif_open(devname, "system", &dpif);
110 svec_add(all_dps, devname);
111 dpif_uninit(dpif, true);
112 } else if (retval != ENODEV && !error) {
120 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
121 bool create, struct dpif **dpifp)
125 minor = !strncmp(name, "dp", 2)
126 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
129 return create_minor(name, minor, dpifp);
131 /* Scan for unused minor number. */
132 for (minor = 0; minor < ODP_MAX; minor++) {
133 int error = create_minor(name, minor, dpifp);
134 if (error != EBUSY) {
139 /* All datapath numbers in use. */
143 struct dpif_linux *dpif;
144 struct odp_port port;
148 error = lookup_minor(name, &minor);
154 error = open_minor(minor, dpifp);
158 dpif = dpif_linux_cast(*dpifp);
160 /* We need the local port's ifindex for the poll function. Start by
161 * getting the local port's name. */
162 memset(&port, 0, sizeof port);
163 port.port = ODPP_LOCAL;
164 if (ioctl(dpif->fd, ODP_VPORT_QUERY, &port)) {
166 if (error != ENODEV) {
167 VLOG_WARN("%s: probe returned unexpected error: %s",
168 dpif_name(*dpifp), strerror(error));
170 dpif_uninit(*dpifp, true);
174 /* Then use that to finish up opening. */
175 return finish_open(&dpif->dpif, port.devname);
180 dpif_linux_close(struct dpif *dpif_)
182 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
183 rtnetlink_link_notifier_unregister(&dpif->port_notifier);
184 shash_destroy(&dpif->changed_ports);
185 free(dpif->local_ifname);
191 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
193 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
195 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
196 svec_add(all_names, dpif->local_ifname);
201 dpif_linux_destroy(struct dpif *dpif_)
203 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
207 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
209 memset(stats, 0, sizeof *stats);
210 return do_ioctl(dpif_, ODP_DP_STATS, stats);
214 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
219 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
221 *drop_fragsp = drop_frags & 1;
227 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
229 int drop_frags_int = drop_frags;
230 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
234 translate_vport_type_to_netdev_type(struct odp_port *port)
236 char *type = port->type;
238 if (!strcmp(type, "netdev")) {
239 ovs_strlcpy(type, "system", sizeof port->type);
240 } else if (!strcmp(type, "gre")) {
241 const struct tnl_port_config *config;
243 config = (struct tnl_port_config *)port->config;
244 if (config->flags & TNL_F_IPSEC) {
245 ovs_strlcpy(type, "ipsec_gre", sizeof port->type);
251 translate_netdev_type_to_vport_type(struct odp_port *port)
253 char *type = port->type;
255 if (!strcmp(type, "system")) {
256 ovs_strlcpy(type, "netdev", sizeof port->type);
257 } else if (!strcmp(type, "ipsec_gre")) {
258 ovs_strlcpy(type, "gre", sizeof port->type);
263 dpif_linux_port_add(struct dpif *dpif, struct netdev *netdev,
266 const char *name = netdev_get_name(netdev);
267 const char *type = netdev_get_type(netdev);
268 struct odp_port port;
271 memset(&port, 0, sizeof port);
272 strncpy(port.devname, name, sizeof port.devname);
273 strncpy(port.type, type, sizeof port.type);
274 netdev_vport_get_config(netdev, port.config);
275 translate_netdev_type_to_vport_type(&port);
277 error = do_ioctl(dpif, ODP_VPORT_ATTACH, &port);
279 *port_nop = port.port;
286 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no_)
288 int port_no = port_no_; /* Kernel expects an "int". */
289 return do_ioctl(dpif_, ODP_VPORT_DETACH, &port_no);
293 dpif_linux_port_query__(const struct dpif *dpif, struct odp_port *port)
295 int error = do_ioctl(dpif, ODP_VPORT_QUERY, port);
297 translate_vport_type_to_netdev_type(port);
303 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
304 struct odp_port *port)
306 memset(port, 0, sizeof *port);
307 port->port = port_no;
308 return dpif_linux_port_query__(dpif, port);
312 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
313 struct odp_port *port)
315 memset(port, 0, sizeof *port);
316 strncpy(port->devname, devname, sizeof port->devname);
317 return dpif_linux_port_query__(dpif, port);
321 dpif_linux_flow_flush(struct dpif *dpif_)
323 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
327 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
329 struct odp_portvec pv;
335 error = do_ioctl(dpif_, ODP_VPORT_LIST, &pv);
340 for (i = 0; i < pv.n_ports; i++) {
341 struct odp_port *port = &pv.ports[i];
343 translate_vport_type_to_netdev_type(port);
349 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
351 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
353 if (dpif->change_error) {
354 dpif->change_error = false;
355 shash_clear(&dpif->changed_ports);
357 } else if (!shash_is_empty(&dpif->changed_ports)) {
358 struct shash_node *node = shash_first(&dpif->changed_ports);
359 *devnamep = shash_steal(&dpif->changed_ports, node);
367 dpif_linux_port_poll_wait(const struct dpif *dpif_)
369 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
370 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
371 poll_immediate_wake();
373 rtnetlink_link_notifier_wait();
378 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
380 struct odp_flowvec fv;
383 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
387 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
389 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
393 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
395 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
399 dpif_linux_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
401 *statep = xzalloc(sizeof(struct odp_flow_dump));
406 dpif_linux_flow_dump_next(const struct dpif *dpif, void *state,
407 struct odp_flow *flow)
409 struct odp_flow_dump *dump = state;
413 error = do_ioctl(dpif, ODP_FLOW_DUMP, dump);
414 return error ? error : flow->flags & ODPFF_EOF ? EOF : 0;
418 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
425 dpif_linux_execute(struct dpif *dpif_,
426 const struct nlattr *actions, size_t actions_len,
427 const struct ofpbuf *buf)
429 struct odp_execute execute;
430 memset(&execute, 0, sizeof execute);
431 execute.actions = (struct nlattr *) actions;
432 execute.actions_len = actions_len;
433 execute.data = buf->data;
434 execute.length = buf->size;
435 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
439 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
441 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
445 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
447 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
451 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
452 uint32_t *probability)
454 return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
458 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
460 return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
464 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
465 uint32_t queue_id, uint32_t *priority)
467 if (queue_id < 0xf000) {
468 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
476 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall)
478 static const struct nl_policy odp_packet_policy[] = {
479 /* Always present. */
480 [ODP_PACKET_ATTR_TYPE] = { .type = NL_A_U32 },
481 [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
482 .min_len = ETH_HEADER_LEN },
483 [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
485 /* _ODPL_ACTION_NR only. */
486 [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
488 /* _ODPL_SFLOW_NR only. */
489 [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
490 [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
493 struct odp_packet *odp_packet = buf->data;
494 struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
496 if (!nl_policy_parse(buf, sizeof *odp_packet, odp_packet_policy,
497 a, ARRAY_SIZE(odp_packet_policy))) {
501 memset(upcall, 0, sizeof *upcall);
502 upcall->type = nl_attr_get_u32(a[ODP_PACKET_ATTR_TYPE]);
503 upcall->packet = buf;
504 upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
505 upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
506 upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
507 upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
508 upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
509 ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
511 upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
512 ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
514 if (a[ODP_PACKET_ATTR_ACTIONS]) {
515 upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
516 upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
523 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
525 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
530 buf = ofpbuf_new(65536);
531 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
534 if (error != EAGAIN) {
535 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
536 dpif_name(dpif_), strerror(error));
538 } else if (retval >= sizeof(struct odp_packet)) {
539 struct odp_packet *odp_packet = buf->data;
542 if (odp_packet->len <= retval) {
543 error = parse_odp_packet(buf, upcall);
545 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
546 "from %"PRIu32" bytes to %d",
547 dpif_name(dpif_), odp_packet->len, retval);
550 } else if (!retval) {
551 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
554 VLOG_WARN_RL(&error_rl, "%s: discarding too-short message (%d bytes)",
555 dpif_name(dpif_), retval);
566 dpif_linux_recv_wait(struct dpif *dpif_)
568 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
569 poll_fd_wait(dpif->fd, POLLIN);
572 const struct dpif_class dpif_linux_class = {
576 dpif_linux_enumerate,
579 dpif_linux_get_all_names,
581 dpif_linux_get_stats,
582 dpif_linux_get_drop_frags,
583 dpif_linux_set_drop_frags,
586 dpif_linux_port_query_by_number,
587 dpif_linux_port_query_by_name,
588 dpif_linux_port_list,
589 dpif_linux_port_poll,
590 dpif_linux_port_poll_wait,
594 dpif_linux_flow_flush,
595 dpif_linux_flow_dump_start,
596 dpif_linux_flow_dump_next,
597 dpif_linux_flow_dump_done,
599 dpif_linux_recv_get_mask,
600 dpif_linux_recv_set_mask,
601 dpif_linux_get_sflow_probability,
602 dpif_linux_set_sflow_probability,
603 dpif_linux_queue_to_priority,
605 dpif_linux_recv_wait,
608 static int get_openvswitch_major(void);
609 static int get_major(const char *target);
612 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
614 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
615 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
619 lookup_minor(const char *name, int *minorp)
621 struct ethtool_drvinfo drvinfo;
627 sock = socket(AF_INET, SOCK_DGRAM, 0);
629 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
634 memset(&ifr, 0, sizeof ifr);
635 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
636 ifr.ifr_data = (caddr_t) &drvinfo;
638 memset(&drvinfo, 0, sizeof drvinfo);
639 drvinfo.cmd = ETHTOOL_GDRVINFO;
640 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
641 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
643 goto error_close_sock;
646 if (strcmp(drvinfo.driver, "openvswitch")) {
647 VLOG_WARN("%s is not an openvswitch device", name);
649 goto error_close_sock;
652 if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
653 VLOG_WARN("%s ethtool bus_info has unexpected format", name);
655 goto error_close_sock;
656 } else if (port_no != ODPP_LOCAL) {
657 /* This is an Open vSwitch device but not the local port. We
658 * intentionally support only using the name of the local port as the
659 * name of a datapath; otherwise, it would be too difficult to
660 * enumerate all the names of a datapath. */
662 goto error_close_sock;
676 make_openvswitch_device(int minor, char **fnp)
678 const char dirname[] = "/dev/net";
686 major = get_openvswitch_major();
690 dev = makedev(major, minor);
692 sprintf(fn, "%s/dp%d", dirname, minor);
694 if (!S_ISCHR(s.st_mode)) {
695 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
697 } else if (s.st_rdev != dev) {
698 VLOG_WARN_RL(&error_rl,
699 "%s is device %u:%u but should be %u:%u, fixing",
700 fn, major(s.st_rdev), minor(s.st_rdev),
701 major(dev), minor(dev));
706 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
707 fn, strerror(errno));
710 } else if (errno == ENOENT) {
711 if (stat(dirname, &s)) {
712 if (errno == ENOENT) {
713 if (mkdir(dirname, 0755)) {
714 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
715 dirname, strerror(errno));
719 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
720 dirname, strerror(errno));
725 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
729 /* The device needs to be created. */
730 if (mknod(fn, S_IFCHR | 0700, dev)) {
731 VLOG_WARN_RL(&error_rl,
732 "%s: creating character device %u:%u failed (%s)",
733 fn, major(dev), minor(dev), strerror(errno));
742 /* Return the major device number of the Open vSwitch device. If it
743 * cannot be determined, a negative errno is returned. */
745 get_openvswitch_major(void)
747 static int openvswitch_major = -1;
748 if (openvswitch_major < 0) {
749 openvswitch_major = get_major("openvswitch");
751 return openvswitch_major;
755 get_major(const char *target)
757 const char fn[] = "/proc/devices";
762 file = fopen(fn, "r");
764 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
768 for (ln = 1; fgets(line, sizeof line, file); ln++) {
772 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
774 } else if (!strncmp(line, "Block", 5)) {
775 /* We only want character devices, so skip the rest of the file. */
777 } else if (sscanf(line, "%d %63s", &major, name)) {
778 if (!strcmp(name, target)) {
783 VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
789 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
794 finish_open(struct dpif *dpif_, const char *local_ifname)
796 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
797 dpif->local_ifname = xstrdup(local_ifname);
798 dpif->local_ifindex = if_nametoindex(local_ifname);
799 if (!dpif->local_ifindex) {
801 dpif_uninit(dpif_, true);
802 VLOG_WARN("could not get ifindex of %s device: %s",
803 local_ifname, strerror(errno));
810 create_minor(const char *name, int minor, struct dpif **dpifp)
812 int error = open_minor(minor, dpifp);
814 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
816 error = finish_open(*dpifp, name);
818 dpif_uninit(*dpifp, true);
825 open_minor(int minor, struct dpif **dpifp)
831 error = make_openvswitch_device(minor, &fn);
836 fd = open(fn, O_RDONLY | O_NONBLOCK);
838 struct dpif_linux *dpif = xmalloc(sizeof *dpif);
839 error = rtnetlink_link_notifier_register(&dpif->port_notifier,
840 dpif_linux_port_changed,
845 name = xasprintf("dp%d", minor);
846 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
850 dpif->local_ifname = NULL;
852 dpif->local_ifindex = 0;
853 shash_init(&dpif->changed_ports);
854 dpif->change_error = false;
855 *dpifp = &dpif->dpif;
861 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
869 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
872 struct dpif_linux *dpif = dpif_;
875 if (change->master_ifindex == dpif->local_ifindex
876 && (change->nlmsg_type == RTM_NEWLINK
877 || change->nlmsg_type == RTM_DELLINK))
879 /* Our datapath changed, either adding a new port or deleting an
881 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
884 dpif->change_error = true;