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.
19 #include "dpif-linux.h"
27 #include <linux/types.h>
28 #include <linux/ethtool.h>
29 #include <linux/pkt_sched.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/sockios.h>
33 #include <sys/ioctl.h>
37 #include "dpif-provider.h"
39 #include "netdev-vport.h"
42 #include "openvswitch/tunnel.h"
44 #include "poll-loop.h"
45 #include "rtnetlink.h"
46 #include "rtnetlink-link.h"
52 VLOG_DEFINE_THIS_MODULE(dpif_linux);
54 /* Datapath interface for the openvswitch Linux kernel module. */
59 /* Used by dpif_linux_get_all_names(). */
63 /* Change notification. */
64 int local_ifindex; /* Ifindex of local port. */
65 struct shash changed_ports; /* Ports that have changed. */
66 struct rtnetlink_notifier port_notifier;
70 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
72 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
73 static int lookup_internal_device(const char *name, int *dp_idx, int *port_no);
74 static int lookup_minor(const char *name, int *minor);
75 static int finish_open(struct dpif *, const char *local_ifname);
76 static int get_openvswitch_major(void);
77 static int create_minor(const char *name, int minor, struct dpif **dpifp);
78 static int open_minor(int minor, struct dpif **dpifp);
79 static int make_openvswitch_device(int minor, char **fnp);
80 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
83 static struct dpif_linux *
84 dpif_linux_cast(const struct dpif *dpif)
86 dpif_assert_class(dpif, &dpif_linux_class);
87 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
91 dpif_linux_enumerate(struct svec *all_dps)
97 /* Check that the Open vSwitch module is loaded. */
98 major = get_openvswitch_major();
104 for (i = 0; i < ODP_MAX; i++) {
109 sprintf(devname, "dp%d", i);
110 retval = dpif_open(devname, "system", &dpif);
112 svec_add(all_dps, devname);
113 dpif_uninit(dpif, true);
114 } else if (retval != ENODEV && !error) {
122 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
123 bool create, struct dpif **dpifp)
127 minor = !strncmp(name, "dp", 2)
128 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
131 return create_minor(name, minor, dpifp);
133 /* Scan for unused minor number. */
134 for (minor = 0; minor < ODP_MAX; minor++) {
135 int error = create_minor(name, minor, dpifp);
136 if (error != EBUSY) {
141 /* All datapath numbers in use. */
145 struct dpif_linux *dpif;
146 struct odp_port port;
150 error = lookup_minor(name, &minor);
156 error = open_minor(minor, dpifp);
160 dpif = dpif_linux_cast(*dpifp);
162 /* We need the local port's ifindex for the poll function. Start by
163 * getting the local port's name. */
164 memset(&port, 0, sizeof port);
165 port.port = ODPP_LOCAL;
166 if (ioctl(dpif->fd, ODP_VPORT_QUERY, &port)) {
168 if (error != ENODEV) {
169 VLOG_WARN("%s: probe returned unexpected error: %s",
170 dpif_name(*dpifp), strerror(error));
172 dpif_uninit(*dpifp, true);
176 /* Then use that to finish up opening. */
177 return finish_open(&dpif->dpif, port.devname);
182 dpif_linux_close(struct dpif *dpif_)
184 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
185 rtnetlink_link_notifier_unregister(&dpif->port_notifier);
186 shash_destroy(&dpif->changed_ports);
187 free(dpif->local_ifname);
193 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
195 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
197 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
198 svec_add(all_names, dpif->local_ifname);
203 dpif_linux_destroy(struct dpif *dpif_)
205 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
209 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
211 memset(stats, 0, sizeof *stats);
212 return do_ioctl(dpif_, ODP_DP_STATS, stats);
216 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
221 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
223 *drop_fragsp = drop_frags & 1;
229 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
231 int drop_frags_int = drop_frags;
232 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
236 translate_vport_type_to_netdev_type(struct odp_port *port)
238 char *type = port->type;
240 if (!strcmp(type, "netdev")) {
241 ovs_strlcpy(type, "system", sizeof port->type);
242 } else if (!strcmp(type, "gre")) {
243 const struct tnl_port_config *config;
245 config = (struct tnl_port_config *)port->config;
246 if (config->flags & TNL_F_IPSEC) {
247 ovs_strlcpy(type, "ipsec_gre", sizeof port->type);
253 translate_netdev_type_to_vport_type(struct odp_port *port)
255 char *type = port->type;
257 if (!strcmp(type, "system")) {
258 ovs_strlcpy(type, "netdev", sizeof port->type);
259 } else if (!strcmp(type, "ipsec_gre")) {
260 ovs_strlcpy(type, "gre", sizeof port->type);
265 dpif_linux_port_add(struct dpif *dpif, struct netdev *netdev,
268 const char *name = netdev_get_name(netdev);
269 const char *type = netdev_get_type(netdev);
270 struct odp_port port;
273 memset(&port, 0, sizeof port);
274 strncpy(port.devname, name, sizeof port.devname);
275 strncpy(port.type, type, sizeof port.type);
276 netdev_vport_get_config(netdev, port.config);
277 translate_netdev_type_to_vport_type(&port);
279 error = do_ioctl(dpif, ODP_VPORT_ATTACH, &port);
281 *port_nop = port.port;
288 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no_)
290 int port_no = port_no_; /* Kernel expects an "int". */
291 return do_ioctl(dpif_, ODP_VPORT_DETACH, &port_no);
295 dpif_linux_port_query__(const struct dpif *dpif, struct odp_port *port)
297 int error = do_ioctl(dpif, ODP_VPORT_QUERY, port);
299 translate_vport_type_to_netdev_type(port);
305 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
306 struct odp_port *port)
308 memset(port, 0, sizeof *port);
309 port->port = port_no;
310 return dpif_linux_port_query__(dpif, port);
314 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
315 struct odp_port *port)
317 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
320 memset(port, 0, sizeof *port);
321 strncpy(port->devname, devname, sizeof port->devname);
322 error = dpif_linux_port_query__(dpif_, port);
323 if (!error && port->dp_idx != dpif->minor) {
324 /* A vport named 'devname' exists but in some other datapath. */
331 dpif_linux_flow_flush(struct dpif *dpif_)
333 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
337 dpif_linux_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
339 *statep = xzalloc(sizeof(struct odp_vport_dump));
344 dpif_linux_port_dump_next(const struct dpif *dpif, void *state,
345 struct odp_port *port)
347 struct odp_vport_dump *dump = state;
351 error = do_ioctl(dpif, ODP_VPORT_DUMP, dump);
354 } else if (port->devname[0] == '\0') {
357 dump->port_no = port->port + 1;
358 translate_vport_type_to_netdev_type(port);
364 dpif_linux_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
371 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
373 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
375 if (dpif->change_error) {
376 dpif->change_error = false;
377 shash_clear(&dpif->changed_ports);
379 } else if (!shash_is_empty(&dpif->changed_ports)) {
380 struct shash_node *node = shash_first(&dpif->changed_ports);
381 *devnamep = shash_steal(&dpif->changed_ports, node);
389 dpif_linux_port_poll_wait(const struct dpif *dpif_)
391 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
392 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
393 poll_immediate_wake();
395 rtnetlink_link_notifier_wait();
400 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
402 struct odp_flowvec fv;
405 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
409 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
411 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
415 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
417 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
421 dpif_linux_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
423 *statep = xzalloc(sizeof(struct odp_flow_dump));
428 dpif_linux_flow_dump_next(const struct dpif *dpif, void *state,
429 struct odp_flow *flow)
431 struct odp_flow_dump *dump = state;
435 error = do_ioctl(dpif, ODP_FLOW_DUMP, dump);
436 return error ? error : flow->flags & ODPFF_EOF ? EOF : 0;
440 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
447 dpif_linux_execute(struct dpif *dpif_,
448 const struct nlattr *actions, size_t actions_len,
449 const struct ofpbuf *buf)
451 struct odp_execute execute;
452 memset(&execute, 0, sizeof execute);
453 execute.actions = (struct nlattr *) actions;
454 execute.actions_len = actions_len;
455 execute.data = buf->data;
456 execute.length = buf->size;
457 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
461 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
463 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
467 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
469 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
473 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
474 uint32_t *probability)
476 return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
480 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
482 return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
486 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
487 uint32_t queue_id, uint32_t *priority)
489 if (queue_id < 0xf000) {
490 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
498 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall)
500 static const struct nl_policy odp_packet_policy[] = {
501 /* Always present. */
502 [ODP_PACKET_ATTR_TYPE] = { .type = NL_A_U32 },
503 [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
504 .min_len = ETH_HEADER_LEN },
505 [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
507 /* _ODPL_ACTION_NR only. */
508 [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
510 /* _ODPL_SFLOW_NR only. */
511 [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
512 [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
515 struct odp_packet *odp_packet = buf->data;
516 struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
518 if (!nl_policy_parse(buf, sizeof *odp_packet, odp_packet_policy,
519 a, ARRAY_SIZE(odp_packet_policy))) {
523 memset(upcall, 0, sizeof *upcall);
524 upcall->type = nl_attr_get_u32(a[ODP_PACKET_ATTR_TYPE]);
525 upcall->packet = buf;
526 upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
527 upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
528 upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
529 upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
530 upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
531 ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
533 upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
534 ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
536 if (a[ODP_PACKET_ATTR_ACTIONS]) {
537 upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
538 upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
545 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
547 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
552 buf = ofpbuf_new(65536);
553 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
556 if (error != EAGAIN) {
557 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
558 dpif_name(dpif_), strerror(error));
560 } else if (retval >= sizeof(struct odp_packet)) {
561 struct odp_packet *odp_packet = buf->data;
564 if (odp_packet->len <= retval) {
565 error = parse_odp_packet(buf, upcall);
567 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
568 "from %"PRIu32" bytes to %d",
569 dpif_name(dpif_), odp_packet->len, retval);
572 } else if (!retval) {
573 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
576 VLOG_WARN_RL(&error_rl, "%s: discarding too-short message (%d bytes)",
577 dpif_name(dpif_), retval);
588 dpif_linux_recv_wait(struct dpif *dpif_)
590 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
591 poll_fd_wait(dpif->fd, POLLIN);
594 const struct dpif_class dpif_linux_class = {
598 dpif_linux_enumerate,
601 dpif_linux_get_all_names,
603 dpif_linux_get_stats,
604 dpif_linux_get_drop_frags,
605 dpif_linux_set_drop_frags,
608 dpif_linux_port_query_by_number,
609 dpif_linux_port_query_by_name,
610 dpif_linux_port_dump_start,
611 dpif_linux_port_dump_next,
612 dpif_linux_port_dump_done,
613 dpif_linux_port_poll,
614 dpif_linux_port_poll_wait,
618 dpif_linux_flow_flush,
619 dpif_linux_flow_dump_start,
620 dpif_linux_flow_dump_next,
621 dpif_linux_flow_dump_done,
623 dpif_linux_recv_get_mask,
624 dpif_linux_recv_set_mask,
625 dpif_linux_get_sflow_probability,
626 dpif_linux_set_sflow_probability,
627 dpif_linux_queue_to_priority,
629 dpif_linux_recv_wait,
632 static int get_openvswitch_major(void);
633 static int get_major(const char *target);
636 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
638 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
639 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
643 lookup_internal_device(const char *name, int *dp_idx, int *port_no)
645 struct odp_port odp_port;
646 static int dp0_fd = -1;
652 error = make_openvswitch_device(0, &fn);
657 dp0_fd = open(fn, O_RDONLY | O_NONBLOCK);
659 VLOG_WARN_RL(&error_rl, "%s: open failed (%s)",
660 fn, strerror(errno));
667 memset(&odp_port, 0, sizeof odp_port);
668 strncpy(odp_port.devname, name, sizeof odp_port.devname);
669 if (ioctl(dp0_fd, ODP_VPORT_QUERY, &odp_port)) {
670 if (errno != ENODEV) {
671 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
672 name, strerror(errno));
675 } else if (!strcmp(odp_port.type, "internal")) {
676 *dp_idx = odp_port.dp_idx;
677 *port_no = odp_port.port;
685 lookup_minor(const char *name, int *minorp)
690 error = lookup_internal_device(name, &minor, &port_no);
693 } else if (port_no != ODPP_LOCAL) {
694 /* This is an Open vSwitch device but not the local port. We
695 * intentionally support only using the name of the local port as the
696 * name of a datapath; otherwise, it would be too difficult to
697 * enumerate all the names of a datapath. */
706 dpif_linux_is_internal_device(const char *name)
710 return !lookup_internal_device(name, &minor, &port_no);
714 make_openvswitch_device(int minor, char **fnp)
716 const char dirname[] = "/dev/net";
724 major = get_openvswitch_major();
728 dev = makedev(major, minor);
730 sprintf(fn, "%s/dp%d", dirname, minor);
732 if (!S_ISCHR(s.st_mode)) {
733 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
735 } else if (s.st_rdev != dev) {
736 VLOG_WARN_RL(&error_rl,
737 "%s is device %u:%u but should be %u:%u, fixing",
738 fn, major(s.st_rdev), minor(s.st_rdev),
739 major(dev), minor(dev));
744 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
745 fn, strerror(errno));
748 } else if (errno == ENOENT) {
749 if (stat(dirname, &s)) {
750 if (errno == ENOENT) {
751 if (mkdir(dirname, 0755)) {
752 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
753 dirname, strerror(errno));
757 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
758 dirname, strerror(errno));
763 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
767 /* The device needs to be created. */
768 if (mknod(fn, S_IFCHR | 0700, dev)) {
769 VLOG_WARN_RL(&error_rl,
770 "%s: creating character device %u:%u failed (%s)",
771 fn, major(dev), minor(dev), strerror(errno));
780 /* Return the major device number of the Open vSwitch device. If it
781 * cannot be determined, a negative errno is returned. */
783 get_openvswitch_major(void)
785 static int openvswitch_major = -1;
786 if (openvswitch_major < 0) {
787 openvswitch_major = get_major("openvswitch");
789 return openvswitch_major;
793 get_major(const char *target)
795 const char fn[] = "/proc/devices";
800 file = fopen(fn, "r");
802 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
806 for (ln = 1; fgets(line, sizeof line, file); ln++) {
810 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
812 } else if (!strncmp(line, "Block", 5)) {
813 /* We only want character devices, so skip the rest of the file. */
815 } else if (sscanf(line, "%d %63s", &major, name)) {
816 if (!strcmp(name, target)) {
821 VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
827 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
832 finish_open(struct dpif *dpif_, const char *local_ifname)
834 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
835 dpif->local_ifname = xstrdup(local_ifname);
836 dpif->local_ifindex = if_nametoindex(local_ifname);
837 if (!dpif->local_ifindex) {
839 dpif_uninit(dpif_, true);
840 VLOG_WARN("could not get ifindex of %s device: %s",
841 local_ifname, strerror(errno));
848 create_minor(const char *name, int minor, struct dpif **dpifp)
850 int error = open_minor(minor, dpifp);
852 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
854 error = finish_open(*dpifp, name);
856 dpif_uninit(*dpifp, true);
863 open_minor(int minor, struct dpif **dpifp)
869 error = make_openvswitch_device(minor, &fn);
874 fd = open(fn, O_RDONLY | O_NONBLOCK);
876 struct dpif_linux *dpif = xmalloc(sizeof *dpif);
877 error = rtnetlink_link_notifier_register(&dpif->port_notifier,
878 dpif_linux_port_changed,
883 name = xasprintf("dp%d", minor);
884 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
888 dpif->local_ifname = NULL;
890 dpif->local_ifindex = 0;
891 shash_init(&dpif->changed_ports);
892 dpif->change_error = false;
893 *dpifp = &dpif->dpif;
899 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
907 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
910 struct dpif_linux *dpif = dpif_;
913 if (change->master_ifindex == dpif->local_ifindex
914 && (change->nlmsg_type == RTM_NEWLINK
915 || change->nlmsg_type == RTM_DELLINK))
917 /* Our datapath changed, either adding a new port or deleting an
919 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
922 dpif->change_error = true;