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. */
361 dpif_linux_get_stats,
362 dpif_linux_get_drop_frags,
363 dpif_linux_set_drop_frags,
366 dpif_linux_port_query_by_number,
367 dpif_linux_port_query_by_name,
368 dpif_linux_port_list,
369 dpif_linux_port_group_get,
370 dpif_linux_port_group_set,
374 dpif_linux_flow_flush,
375 dpif_linux_flow_list,
377 dpif_linux_recv_get_mask,
378 dpif_linux_recv_set_mask,
380 dpif_linux_recv_wait,
383 static int get_openvswitch_major(void);
384 static int get_major(const char *target, int default_major);
387 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
389 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
390 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
394 lookup_minor(const char *name, int *minor)
396 struct ethtool_drvinfo drvinfo;
401 sock = socket(AF_INET, SOCK_DGRAM, 0);
403 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
408 memset(&ifr, 0, sizeof ifr);
409 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
410 ifr.ifr_data = (caddr_t) &drvinfo;
412 memset(&drvinfo, 0, sizeof drvinfo);
413 drvinfo.cmd = ETHTOOL_GDRVINFO;
414 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
415 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
417 goto error_close_sock;
420 if (strcmp(drvinfo.driver, "openvswitch")) {
421 VLOG_WARN("%s is not an openvswitch device", name);
423 goto error_close_sock;
426 if (!isdigit(drvinfo.bus_info[0])) {
427 VLOG_WARN("%s ethtool info does not contain an openvswitch minor",
430 goto error_close_sock;
433 *minor = atoi(drvinfo.bus_info);
444 make_openvswitch_device(int minor, char **fnp)
446 dev_t dev = makedev(get_openvswitch_major(), minor);
447 const char dirname[] = "/dev/net";
452 sprintf(fn, "%s/dp%d", dirname, minor);
454 if (!S_ISCHR(s.st_mode)) {
455 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
457 } else if (s.st_rdev != dev) {
458 VLOG_WARN_RL(&error_rl,
459 "%s is device %u:%u instead of %u:%u, fixing",
460 fn, major(s.st_rdev), minor(s.st_rdev),
461 major(dev), minor(dev));
466 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
467 fn, strerror(errno));
470 } else if (errno == ENOENT) {
471 if (stat(dirname, &s)) {
472 if (errno == ENOENT) {
473 if (mkdir(dirname, 0755)) {
474 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
475 dirname, strerror(errno));
479 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
480 dirname, strerror(errno));
485 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
489 /* The device needs to be created. */
490 if (mknod(fn, S_IFCHR | 0700, dev)) {
491 VLOG_WARN_RL(&error_rl,
492 "%s: creating character device %u:%u failed (%s)",
493 fn, major(dev), minor(dev), strerror(errno));
504 get_openvswitch_major(void)
506 static unsigned int openvswitch_major;
507 if (!openvswitch_major) {
508 enum { DEFAULT_MAJOR = 248 };
509 openvswitch_major = get_major("openvswitch", DEFAULT_MAJOR);
511 return openvswitch_major;
515 get_major(const char *target, int default_major)
517 const char fn[] = "/proc/devices";
522 file = fopen(fn, "r");
524 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
528 for (ln = 1; fgets(line, sizeof line, file); ln++) {
532 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
534 } else if (!strncmp(line, "Block", 5)) {
535 /* We only want character devices, so skip the rest of the file. */
537 } else if (sscanf(line, "%d %63s", &major, name)) {
538 if (!strcmp(name, target)) {
545 VLOG_WARN("%s:%d: syntax error", fn, ln);
551 VLOG_ERR("%s: %s major not found (is the module loaded?), using "
552 "default major %d", fn, target, default_major);
554 VLOG_INFO("using default major %d for %s", default_major, target);
555 return default_major;
559 create_minor(const char *name, int minor, struct dpif **dpifp)
561 int error = open_minor(minor, dpifp);
563 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
572 open_minor(int minor, struct dpif **dpifp)
578 error = make_openvswitch_device(minor, &fn);
583 fd = open(fn, O_RDONLY | O_NONBLOCK);
585 struct dpif_linux *dpif;
588 name = xasprintf("dp%d", minor);
590 dpif = xmalloc(sizeof *dpif);
591 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
593 *dpifp = &dpif->dpif;
598 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));