dpif: Add new functions dp_run() and dp_wait().
[openvswitch] / lib / dpif-linux.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include "dpif.h"
19
20 #include <assert.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <net/if.h>
26 #include <linux/ethtool.h>
27 #include <linux/sockios.h>
28 #include <stdlib.h>
29 #include <sys/ioctl.h>
30 #include <unistd.h>
31
32 #include "dpif-provider.h"
33 #include "ofpbuf.h"
34 #include "poll-loop.h"
35 #include "util.h"
36
37 #include "vlog.h"
38 #define THIS_MODULE VLM_dpif_linux
39
40 /* Datapath interface for the openvswitch Linux kernel module. */
41 struct dpif_linux {
42     struct dpif dpif;
43     int fd;
44 };
45
46 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
47
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);
53
54 static struct dpif_linux *
55 dpif_linux_cast(const struct dpif *dpif)
56 {
57     dpif_assert_class(dpif, &dpif_linux_class);
58     return CONTAINER_OF(dpif, struct dpif_linux, dpif);
59 }
60
61 static int
62 dpif_linux_open(const char *name UNUSED, char *suffix, bool create,
63                 struct dpif **dpifp)
64 {
65     int minor;
66
67     minor = !strncmp(name, "dp", 2) && isdigit(name[2]) ? atoi(name + 2) : -1;
68     if (create) {
69         if (minor >= 0) {
70             return create_minor(suffix, minor, dpifp);
71         } else {
72             /* Scan for unused minor number. */
73             for (minor = 0; minor < ODP_MAX; minor++) {
74                 int error = create_minor(suffix, minor, dpifp);
75                 if (error != EBUSY) {
76                     return error;
77                 }
78             }
79
80             /* All datapath numbers in use. */
81             return ENOBUFS;
82         }
83     } else {
84         struct dpif_linux *dpif;
85         int listen_mask;
86         int error;
87
88         if (minor < 0) {
89             error = lookup_minor(suffix, &minor);
90             if (error) {
91                 return error;
92             }
93         }
94
95         error = open_minor(minor, dpifp);
96         if (error) {
97             return error;
98         }
99         dpif = dpif_linux_cast(*dpifp);
100
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);
106         if (error) {
107             if (error != ENODEV) {
108                 VLOG_WARN("%s: probe returned unexpected error: %s",
109                           dpif_name(*dpifp), strerror(error));
110             }
111             dpif_close(*dpifp);
112         }
113         return error;
114     }
115 }
116
117 static void
118 dpif_linux_close(struct dpif *dpif_)
119 {
120     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
121     close(dpif->fd);
122     free(dpif);
123 }
124
125 static int
126 dpif_linux_delete(struct dpif *dpif_)
127 {
128     return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
129 }
130
131 static int
132 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
133 {
134     return do_ioctl(dpif_, ODP_DP_STATS, stats);
135 }
136
137 static int
138 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
139 {
140     int drop_frags;
141     int error;
142
143     error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
144     if (!error) {
145         *drop_fragsp = drop_frags & 1;
146     }
147     return error;
148 }
149
150 static int
151 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
152 {
153     int drop_frags_int = drop_frags;
154     return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
155 }
156
157 static int
158 dpif_linux_port_add(struct dpif *dpif_, const char *devname, uint16_t flags,
159                     uint16_t *port_no)
160 {
161     struct odp_port port;
162     int error;
163
164     memset(&port, 0, sizeof port);
165     strncpy(port.devname, devname, sizeof port.devname);
166     port.flags = flags;
167     error = do_ioctl(dpif_, ODP_PORT_ADD, &port);
168     if (!error) {
169         *port_no = port.port;
170     }
171     return error;
172 }
173
174 static int
175 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
176 {
177     int tmp = port_no;
178     return do_ioctl(dpif_, ODP_PORT_DEL, &tmp);
179 }
180
181 static int
182 dpif_linux_port_query_by_number(const struct dpif *dpif_, uint16_t port_no,
183                           struct odp_port *port)
184 {
185     memset(port, 0, sizeof *port);
186     port->port = port_no;
187     return do_ioctl(dpif_, ODP_PORT_QUERY, port);
188 }
189
190 static int
191 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
192                               struct odp_port *port)
193 {
194     memset(port, 0, sizeof *port);
195     strncpy(port->devname, devname, sizeof port->devname);
196     return do_ioctl(dpif_, ODP_PORT_QUERY, port);
197 }
198
199 static int
200 dpif_linux_flow_flush(struct dpif *dpif_)
201 {
202     return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
203 }
204
205 static int
206 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
207 {
208     struct odp_portvec pv;
209     int error;
210
211     pv.ports = ports;
212     pv.n_ports = n;
213     error = do_ioctl(dpif_, ODP_PORT_LIST, &pv);
214     return error ? -error : pv.n_ports;
215 }
216
217 static int
218 dpif_linux_port_group_get(const struct dpif *dpif_, int group,
219                           uint16_t ports[], int n)
220 {
221     struct odp_port_group pg;
222     int error;
223
224     assert(n <= UINT16_MAX);
225     pg.group = group;
226     pg.ports = ports;
227     pg.n_ports = n;
228     error = do_ioctl(dpif_, ODP_PORT_GROUP_GET, &pg);
229     return error ? -error : pg.n_ports;
230 }
231
232 static int
233 dpif_linux_port_group_set(struct dpif *dpif_, int group,
234                           const uint16_t ports[], int n)
235 {
236     struct odp_port_group pg;
237
238     assert(n <= UINT16_MAX);
239     pg.group = group;
240     pg.ports = (uint16_t *) ports;
241     pg.n_ports = n;
242     return do_ioctl(dpif_, ODP_PORT_GROUP_SET, &pg);
243 }
244
245 static int
246 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
247 {
248     struct odp_flowvec fv;
249     fv.flows = flows;
250     fv.n_flows = n;
251     return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
252 }
253
254 static int
255 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
256 {
257     return do_ioctl(dpif_, ODP_FLOW_PUT, put);
258 }
259
260 static int
261 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
262 {
263     return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
264 }
265
266 static int
267 dpif_linux_flow_list(const struct dpif *dpif_, struct odp_flow flows[], int n)
268 {
269     struct odp_flowvec fv;
270     int error;
271
272     fv.flows = flows;
273     fv.n_flows = n;
274     error = do_ioctl(dpif_, ODP_FLOW_LIST, &fv);
275     return error ? -error : fv.n_flows;
276 }
277
278 static int
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)
282 {
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);
291 }
292
293 static int
294 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
295 {
296     return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
297 }
298
299 static int
300 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
301 {
302     return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
303 }
304
305 static int
306 dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
307 {
308     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
309     struct ofpbuf *buf;
310     int retval;
311     int error;
312
313     buf = ofpbuf_new(65536);
314     retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
315     if (retval < 0) {
316         error = errno;
317         if (error != EAGAIN) {
318             VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
319                          dpif_name(dpif_), strerror(error));
320         }
321     } else if (retval >= sizeof(struct odp_msg)) {
322         struct odp_msg *msg = buf->data;
323         if (msg->length <= retval) {
324             buf->size += retval;
325             *bufp = buf;
326             return 0;
327         } else {
328             VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
329                          "from %zu bytes to %d",
330                          dpif_name(dpif_), msg->length, retval);
331             error = ERANGE;
332         }
333     } else if (!retval) {
334         VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
335         error = EPROTO;
336     } else {
337         VLOG_WARN_RL(&error_rl,
338                      "%s: discarding too-short message (%d bytes)",
339                      dpif_name(dpif_), retval);
340         error = ERANGE;
341     }
342
343     *bufp = NULL;
344     ofpbuf_delete(buf);
345     return error;
346 }
347
348 static void
349 dpif_linux_recv_wait(struct dpif *dpif_)
350 {
351     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
352     poll_fd_wait(dpif->fd, POLLIN);
353 }
354
355 const struct dpif_class dpif_linux_class = {
356     "",                         /* This is the default class. */
357     "linux",
358     NULL,                       /* run */
359     NULL,                       /* wait */
360     dpif_linux_open,
361     dpif_linux_close,
362     dpif_linux_delete,
363     dpif_linux_get_stats,
364     dpif_linux_get_drop_frags,
365     dpif_linux_set_drop_frags,
366     dpif_linux_port_add,
367     dpif_linux_port_del,
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,
373     dpif_linux_flow_get,
374     dpif_linux_flow_put,
375     dpif_linux_flow_del,
376     dpif_linux_flow_flush,
377     dpif_linux_flow_list,
378     dpif_linux_execute,
379     dpif_linux_recv_get_mask,
380     dpif_linux_recv_set_mask,
381     dpif_linux_recv,
382     dpif_linux_recv_wait,
383 };
384 \f
385 static int get_openvswitch_major(void);
386 static int get_major(const char *target, int default_major);
387
388 static int
389 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
390 {
391     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
392     return ioctl(dpif->fd, cmd, arg) ? errno : 0;
393 }
394
395 static int
396 lookup_minor(const char *name, int *minor)
397 {
398     struct ethtool_drvinfo drvinfo;
399     struct ifreq ifr;
400     int error;
401     int sock;
402
403     sock = socket(AF_INET, SOCK_DGRAM, 0);
404     if (sock < 0) {
405         VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
406         error = errno;
407         goto error;
408     }
409
410     memset(&ifr, 0, sizeof ifr);
411     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
412     ifr.ifr_data = (caddr_t) &drvinfo;
413
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));
418         error = errno;
419         goto error_close_sock;
420     }
421
422     if (strcmp(drvinfo.driver, "openvswitch")) {
423         VLOG_WARN("%s is not an openvswitch device", name);
424         error = EOPNOTSUPP;
425         goto error_close_sock;
426     }
427
428     if (!isdigit(drvinfo.bus_info[0])) {
429         VLOG_WARN("%s ethtool info does not contain an openvswitch minor",
430                   name);
431         error = EPROTOTYPE;
432         goto error_close_sock;
433     }
434
435     *minor = atoi(drvinfo.bus_info);
436     close(sock);
437     return 0;
438
439 error_close_sock:
440     close(sock);
441 error:
442     return error;
443 }
444
445 static int
446 make_openvswitch_device(int minor, char **fnp)
447 {
448     dev_t dev = makedev(get_openvswitch_major(), minor);
449     const char dirname[] = "/dev/net";
450     struct stat s;
451     char fn[128];
452
453     *fnp = NULL;
454     sprintf(fn, "%s/dp%d", dirname, minor);
455     if (!stat(fn, &s)) {
456         if (!S_ISCHR(s.st_mode)) {
457             VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
458                          fn);
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));
464         } else {
465             goto success;
466         }
467         if (unlink(fn)) {
468             VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
469                          fn, strerror(errno));
470             return errno;
471         }
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));
478                     return errno;
479                 }
480             } else {
481                 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
482                              dirname, strerror(errno));
483                 return errno;
484             }
485         }
486     } else {
487         VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
488         return errno;
489     }
490
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));
496         return errno;
497     }
498
499 success:
500     *fnp = xstrdup(fn);
501     return 0;
502 }
503
504
505 static int
506 get_openvswitch_major(void)
507 {
508     static unsigned int openvswitch_major;
509     if (!openvswitch_major) {
510         enum { DEFAULT_MAJOR = 248 };
511         openvswitch_major = get_major("openvswitch", DEFAULT_MAJOR);
512     }
513     return openvswitch_major;
514 }
515
516 static int
517 get_major(const char *target, int default_major)
518 {
519     const char fn[] = "/proc/devices";
520     char line[128];
521     FILE *file;
522     int ln;
523
524     file = fopen(fn, "r");
525     if (!file) {
526         VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
527         goto error;
528     }
529
530     for (ln = 1; fgets(line, sizeof line, file); ln++) {
531         char name[64];
532         int major;
533
534         if (!strncmp(line, "Character", 9) || line[0] == '\0') {
535             /* Nothing to do. */
536         } else if (!strncmp(line, "Block", 5)) {
537             /* We only want character devices, so skip the rest of the file. */
538             break;
539         } else if (sscanf(line, "%d %63s", &major, name)) {
540             if (!strcmp(name, target)) {
541                 fclose(file);
542                 return major;
543             }
544         } else {
545             static bool warned;
546             if (!warned) {
547                 VLOG_WARN("%s:%d: syntax error", fn, ln);
548             }
549             warned = true;
550         }
551     }
552
553     VLOG_ERR("%s: %s major not found (is the module loaded?), using "
554              "default major %d", fn, target, default_major);
555 error:
556     VLOG_INFO("using default major %d for %s", default_major, target);
557     return default_major;
558 }
559
560 static int
561 create_minor(const char *name, int minor, struct dpif **dpifp)
562 {
563     int error = open_minor(minor, dpifp);
564     if (!error) {
565         error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
566         if (error) {
567             dpif_close(*dpifp);
568         }
569     }
570     return error;
571 }
572
573 static int
574 open_minor(int minor, struct dpif **dpifp)
575 {
576     int error;
577     char *fn;
578     int fd;
579
580     error = make_openvswitch_device(minor, &fn);
581     if (error) {
582         return error;
583     }
584
585     fd = open(fn, O_RDONLY | O_NONBLOCK);
586     if (fd >= 0) {
587         struct dpif_linux *dpif;
588         char *name;
589
590         name = xasprintf("dp%d", minor);
591
592         dpif = xmalloc(sizeof *dpif);
593         dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
594         dpif->fd = fd;
595         *dpifp = &dpif->dpif;
596
597         free(name);
598     } else {
599         error = errno;
600         VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
601     }
602     free(fn);
603
604     return error;
605 }