2 * Copyright (c) 2008, 2009 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/sockios.h>
29 #include <sys/ioctl.h>
32 #include "dpif-provider.h"
34 #include "poll-loop.h"
38 #define THIS_MODULE VLM_dpif_linux
40 /* Datapath interface for the openvswitch Linux kernel module. */
46 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
48 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
49 static int lookup_minor(const char *name, int *minor);
50 static int create_minor(const char *name, int minor, struct dpif **dpifp);
51 static int open_minor(int minor, struct dpif **dpifp);
52 static int make_openvswitch_device(int minor, char **fnp);
54 static struct dpif_linux *
55 dpif_linux_cast(const struct dpif *dpif)
57 dpif_assert_class(dpif, &dpif_linux_class);
58 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
62 dpif_linux_open(const char *name UNUSED, char *suffix, bool create,
67 minor = !strncmp(name, "dp", 2) && isdigit(name[2]) ? atoi(name + 2) : -1;
70 return create_minor(suffix, minor, dpifp);
72 /* Scan for unused minor number. */
73 for (minor = 0; minor < ODP_MAX; minor++) {
74 int error = create_minor(suffix, minor, dpifp);
80 /* All datapath numbers in use. */
84 struct dpif_linux *dpif;
89 error = lookup_minor(suffix, &minor);
95 error = open_minor(minor, dpifp);
99 dpif = dpif_linux_cast(*dpifp);
101 /* We can open the device, but that doesn't mean that it's been
102 * created. If it hasn't been, then any command other than
103 * ODP_DP_CREATE will return ENODEV. Try something innocuous. */
104 listen_mask = 0; /* Make Valgrind happy. */
105 error = do_ioctl(*dpifp, ODP_GET_LISTEN_MASK, &listen_mask);
107 if (error != ENODEV) {
108 VLOG_WARN("%s: probe returned unexpected error: %s",
109 dpif_name(*dpifp), strerror(error));
118 dpif_linux_close(struct dpif *dpif_)
120 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
126 dpif_linux_delete(struct dpif *dpif_)
128 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
132 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
134 return do_ioctl(dpif_, ODP_DP_STATS, stats);
138 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
143 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
145 *drop_fragsp = drop_frags & 1;
151 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
153 int drop_frags_int = drop_frags;
154 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
158 dpif_linux_port_add(struct dpif *dpif_, const char *devname, uint16_t flags,
161 struct odp_port port;
164 memset(&port, 0, sizeof port);
165 strncpy(port.devname, devname, sizeof port.devname);
167 error = do_ioctl(dpif_, ODP_PORT_ADD, &port);
169 *port_no = port.port;
175 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
178 return do_ioctl(dpif_, ODP_PORT_DEL, &tmp);
182 dpif_linux_port_query_by_number(const struct dpif *dpif_, uint16_t port_no,
183 struct odp_port *port)
185 memset(port, 0, sizeof *port);
186 port->port = port_no;
187 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
191 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
192 struct odp_port *port)
194 memset(port, 0, sizeof *port);
195 strncpy(port->devname, devname, sizeof port->devname);
196 return do_ioctl(dpif_, ODP_PORT_QUERY, port);
200 dpif_linux_flow_flush(struct dpif *dpif_)
202 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
206 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
208 struct odp_portvec pv;
213 error = do_ioctl(dpif_, ODP_PORT_LIST, &pv);
214 return error ? -error : pv.n_ports;
218 dpif_linux_port_group_get(const struct dpif *dpif_, int group,
219 uint16_t ports[], int n)
221 struct odp_port_group pg;
224 assert(n <= UINT16_MAX);
228 error = do_ioctl(dpif_, ODP_PORT_GROUP_GET, &pg);
229 return error ? -error : pg.n_ports;
233 dpif_linux_port_group_set(struct dpif *dpif_, int group,
234 const uint16_t ports[], int n)
236 struct odp_port_group pg;
238 assert(n <= UINT16_MAX);
240 pg.ports = (uint16_t *) ports;
242 return do_ioctl(dpif_, ODP_PORT_GROUP_SET, &pg);
246 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
248 struct odp_flowvec fv;
251 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
255 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
257 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
261 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
263 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
267 dpif_linux_flow_list(const struct dpif *dpif_, struct odp_flow flows[], int n)
269 struct odp_flowvec fv;
274 error = do_ioctl(dpif_, ODP_FLOW_LIST, &fv);
275 return error ? -error : fv.n_flows;
279 dpif_linux_execute(struct dpif *dpif_, uint16_t in_port,
280 const union odp_action actions[], int n_actions,
281 const struct ofpbuf *buf)
283 struct odp_execute execute;
284 memset(&execute, 0, sizeof execute);
285 execute.in_port = in_port;
286 execute.actions = (union odp_action *) actions;
287 execute.n_actions = n_actions;
288 execute.data = buf->data;
289 execute.length = buf->size;
290 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
294 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
296 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
300 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
302 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
306 dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
308 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
313 buf = ofpbuf_new(65536);
314 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
317 if (error != EAGAIN) {
318 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
319 dpif_name(dpif_), strerror(error));
321 } else if (retval >= sizeof(struct odp_msg)) {
322 struct odp_msg *msg = buf->data;
323 if (msg->length <= retval) {
328 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
329 "from %zu bytes to %d",
330 dpif_name(dpif_), msg->length, retval);
333 } else if (!retval) {
334 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
337 VLOG_WARN_RL(&error_rl,
338 "%s: discarding too-short message (%d bytes)",
339 dpif_name(dpif_), retval);
349 dpif_linux_recv_wait(struct dpif *dpif_)
351 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
352 poll_fd_wait(dpif->fd, POLLIN);
355 const struct dpif_class dpif_linux_class = {
356 "", /* This is the default class. */
363 dpif_linux_get_stats,
364 dpif_linux_get_drop_frags,
365 dpif_linux_set_drop_frags,
368 dpif_linux_port_query_by_number,
369 dpif_linux_port_query_by_name,
370 dpif_linux_port_list,
371 dpif_linux_port_group_get,
372 dpif_linux_port_group_set,
376 dpif_linux_flow_flush,
377 dpif_linux_flow_list,
379 dpif_linux_recv_get_mask,
380 dpif_linux_recv_set_mask,
382 dpif_linux_recv_wait,
385 static int get_openvswitch_major(void);
386 static int get_major(const char *target, int default_major);
389 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
391 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
392 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
396 lookup_minor(const char *name, int *minor)
398 struct ethtool_drvinfo drvinfo;
403 sock = socket(AF_INET, SOCK_DGRAM, 0);
405 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
410 memset(&ifr, 0, sizeof ifr);
411 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
412 ifr.ifr_data = (caddr_t) &drvinfo;
414 memset(&drvinfo, 0, sizeof drvinfo);
415 drvinfo.cmd = ETHTOOL_GDRVINFO;
416 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
417 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
419 goto error_close_sock;
422 if (strcmp(drvinfo.driver, "openvswitch")) {
423 VLOG_WARN("%s is not an openvswitch device", name);
425 goto error_close_sock;
428 if (!isdigit(drvinfo.bus_info[0])) {
429 VLOG_WARN("%s ethtool info does not contain an openvswitch minor",
432 goto error_close_sock;
435 *minor = atoi(drvinfo.bus_info);
446 make_openvswitch_device(int minor, char **fnp)
448 dev_t dev = makedev(get_openvswitch_major(), minor);
449 const char dirname[] = "/dev/net";
454 sprintf(fn, "%s/dp%d", dirname, minor);
456 if (!S_ISCHR(s.st_mode)) {
457 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
459 } else if (s.st_rdev != dev) {
460 VLOG_WARN_RL(&error_rl,
461 "%s is device %u:%u instead of %u:%u, fixing",
462 fn, major(s.st_rdev), minor(s.st_rdev),
463 major(dev), minor(dev));
468 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
469 fn, strerror(errno));
472 } else if (errno == ENOENT) {
473 if (stat(dirname, &s)) {
474 if (errno == ENOENT) {
475 if (mkdir(dirname, 0755)) {
476 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
477 dirname, strerror(errno));
481 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
482 dirname, strerror(errno));
487 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
491 /* The device needs to be created. */
492 if (mknod(fn, S_IFCHR | 0700, dev)) {
493 VLOG_WARN_RL(&error_rl,
494 "%s: creating character device %u:%u failed (%s)",
495 fn, major(dev), minor(dev), strerror(errno));
506 get_openvswitch_major(void)
508 static unsigned int openvswitch_major;
509 if (!openvswitch_major) {
510 enum { DEFAULT_MAJOR = 248 };
511 openvswitch_major = get_major("openvswitch", DEFAULT_MAJOR);
513 return openvswitch_major;
517 get_major(const char *target, int default_major)
519 const char fn[] = "/proc/devices";
524 file = fopen(fn, "r");
526 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
530 for (ln = 1; fgets(line, sizeof line, file); ln++) {
534 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
536 } else if (!strncmp(line, "Block", 5)) {
537 /* We only want character devices, so skip the rest of the file. */
539 } else if (sscanf(line, "%d %63s", &major, name)) {
540 if (!strcmp(name, target)) {
547 VLOG_WARN("%s:%d: syntax error", fn, ln);
553 VLOG_ERR("%s: %s major not found (is the module loaded?), using "
554 "default major %d", fn, target, default_major);
556 VLOG_INFO("using default major %d for %s", default_major, target);
557 return default_major;
561 create_minor(const char *name, int minor, struct dpif **dpifp)
563 int error = open_minor(minor, dpifp);
565 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
574 open_minor(int minor, struct dpif **dpifp)
580 error = make_openvswitch_device(minor, &fn);
585 fd = open(fn, O_RDONLY | O_NONBLOCK);
587 struct dpif_linux *dpif;
590 name = xasprintf("dp%d", minor);
592 dpif = xmalloc(sizeof *dpif);
593 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
595 *dpifp = &dpif->dpif;
600 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));