dpif: Make dpifs abstract, to allow multiple datapath implementations.
[sliver-openvswitch.git] / 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     dpif_linux_open,
359     dpif_linux_close,
360     dpif_linux_delete,
361     dpif_linux_get_stats,
362     dpif_linux_get_drop_frags,
363     dpif_linux_set_drop_frags,
364     dpif_linux_port_add,
365     dpif_linux_port_del,
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,
371     dpif_linux_flow_get,
372     dpif_linux_flow_put,
373     dpif_linux_flow_del,
374     dpif_linux_flow_flush,
375     dpif_linux_flow_list,
376     dpif_linux_execute,
377     dpif_linux_recv_get_mask,
378     dpif_linux_recv_set_mask,
379     dpif_linux_recv,
380     dpif_linux_recv_wait,
381 };
382 \f
383 static int get_openvswitch_major(void);
384 static int get_major(const char *target, int default_major);
385
386 static int
387 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
388 {
389     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
390     return ioctl(dpif->fd, cmd, arg) ? errno : 0;
391 }
392
393 static int
394 lookup_minor(const char *name, int *minor)
395 {
396     struct ethtool_drvinfo drvinfo;
397     struct ifreq ifr;
398     int error;
399     int sock;
400
401     sock = socket(AF_INET, SOCK_DGRAM, 0);
402     if (sock < 0) {
403         VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
404         error = errno;
405         goto error;
406     }
407
408     memset(&ifr, 0, sizeof ifr);
409     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
410     ifr.ifr_data = (caddr_t) &drvinfo;
411
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));
416         error = errno;
417         goto error_close_sock;
418     }
419
420     if (strcmp(drvinfo.driver, "openvswitch")) {
421         VLOG_WARN("%s is not an openvswitch device", name);
422         error = EOPNOTSUPP;
423         goto error_close_sock;
424     }
425
426     if (!isdigit(drvinfo.bus_info[0])) {
427         VLOG_WARN("%s ethtool info does not contain an openvswitch minor",
428                   name);
429         error = EPROTOTYPE;
430         goto error_close_sock;
431     }
432
433     *minor = atoi(drvinfo.bus_info);
434     close(sock);
435     return 0;
436
437 error_close_sock:
438     close(sock);
439 error:
440     return error;
441 }
442
443 static int
444 make_openvswitch_device(int minor, char **fnp)
445 {
446     dev_t dev = makedev(get_openvswitch_major(), minor);
447     const char dirname[] = "/dev/net";
448     struct stat s;
449     char fn[128];
450
451     *fnp = NULL;
452     sprintf(fn, "%s/dp%d", dirname, minor);
453     if (!stat(fn, &s)) {
454         if (!S_ISCHR(s.st_mode)) {
455             VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
456                          fn);
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));
462         } else {
463             goto success;
464         }
465         if (unlink(fn)) {
466             VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
467                          fn, strerror(errno));
468             return errno;
469         }
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));
476                     return errno;
477                 }
478             } else {
479                 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
480                              dirname, strerror(errno));
481                 return errno;
482             }
483         }
484     } else {
485         VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
486         return errno;
487     }
488
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));
494         return errno;
495     }
496
497 success:
498     *fnp = xstrdup(fn);
499     return 0;
500 }
501
502
503 static int
504 get_openvswitch_major(void)
505 {
506     static unsigned int openvswitch_major;
507     if (!openvswitch_major) {
508         enum { DEFAULT_MAJOR = 248 };
509         openvswitch_major = get_major("openvswitch", DEFAULT_MAJOR);
510     }
511     return openvswitch_major;
512 }
513
514 static int
515 get_major(const char *target, int default_major)
516 {
517     const char fn[] = "/proc/devices";
518     char line[128];
519     FILE *file;
520     int ln;
521
522     file = fopen(fn, "r");
523     if (!file) {
524         VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
525         goto error;
526     }
527
528     for (ln = 1; fgets(line, sizeof line, file); ln++) {
529         char name[64];
530         int major;
531
532         if (!strncmp(line, "Character", 9) || line[0] == '\0') {
533             /* Nothing to do. */
534         } else if (!strncmp(line, "Block", 5)) {
535             /* We only want character devices, so skip the rest of the file. */
536             break;
537         } else if (sscanf(line, "%d %63s", &major, name)) {
538             if (!strcmp(name, target)) {
539                 fclose(file);
540                 return major;
541             }
542         } else {
543             static bool warned;
544             if (!warned) {
545                 VLOG_WARN("%s:%d: syntax error", fn, ln);
546             }
547             warned = true;
548         }
549     }
550
551     VLOG_ERR("%s: %s major not found (is the module loaded?), using "
552              "default major %d", fn, target, default_major);
553 error:
554     VLOG_INFO("using default major %d for %s", default_major, target);
555     return default_major;
556 }
557
558 static int
559 create_minor(const char *name, int minor, struct dpif **dpifp)
560 {
561     int error = open_minor(minor, dpifp);
562     if (!error) {
563         error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
564         if (error) {
565             dpif_close(*dpifp);
566         }
567     }
568     return error;
569 }
570
571 static int
572 open_minor(int minor, struct dpif **dpifp)
573 {
574     int error;
575     char *fn;
576     int fd;
577
578     error = make_openvswitch_device(minor, &fn);
579     if (error) {
580         return error;
581     }
582
583     fd = open(fn, O_RDONLY | O_NONBLOCK);
584     if (fd >= 0) {
585         struct dpif_linux *dpif;
586         char *name;
587
588         name = xasprintf("dp%d", minor);
589
590         dpif = xmalloc(sizeof *dpif);
591         dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
592         dpif->fd = fd;
593         *dpifp = &dpif->dpif;
594
595         free(name);
596     } else {
597         error = errno;
598         VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
599     }
600     free(fn);
601
602     return error;
603 }