2bf329f453732137c1d08c936ecae7c5e151ee4e
[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/rtnetlink.h>
28 #include <linux/sockios.h>
29 #include <stdlib.h>
30 #include <sys/ioctl.h>
31 #include <unistd.h>
32
33 #include "dpif-provider.h"
34 #include "ofpbuf.h"
35 #include "poll-loop.h"
36 #include "rtnetlink.h"
37 #include "svec.h"
38 #include "util.h"
39
40 #include "vlog.h"
41 #define THIS_MODULE VLM_dpif_linux
42
43 /* Datapath interface for the openvswitch Linux kernel module. */
44 struct dpif_linux {
45     struct dpif dpif;
46     int fd;
47
48     /* Used by dpif_linux_get_all_names(). */
49     char *local_ifname;
50     int minor;
51
52     /* Change notification. */
53     int local_ifindex;          /* Ifindex of local port. */
54     struct svec changed_ports;  /* Ports that have changed. */
55     struct rtnetlink_notifier port_notifier;
56     bool change_error;
57 };
58
59 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
60
61 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
62 static int lookup_minor(const char *name, int *minor);
63 static int finish_open(struct dpif *, const char *local_ifname);
64 static int get_openvswitch_major(void);
65 static int create_minor(const char *name, int minor, struct dpif **dpifp);
66 static int open_minor(int minor, struct dpif **dpifp);
67 static int make_openvswitch_device(int minor, char **fnp);
68 static void dpif_linux_port_changed(const struct rtnetlink_change *,
69                                     void *dpif);
70
71 static struct dpif_linux *
72 dpif_linux_cast(const struct dpif *dpif)
73 {
74     dpif_assert_class(dpif, &dpif_linux_class);
75     return CONTAINER_OF(dpif, struct dpif_linux, dpif);
76 }
77
78 static int
79 dpif_linux_enumerate(struct svec *all_dps)
80 {
81     int major;
82     int error;
83     int i;
84
85     /* Check that the Open vSwitch module is loaded. */
86     major = get_openvswitch_major();
87     if (major < 0) {
88         return -major;
89     }
90
91     error = 0;
92     for (i = 0; i < ODP_MAX; i++) {
93         struct dpif *dpif;
94         char devname[16];
95         int retval;
96
97         sprintf(devname, "dp%d", i);
98         retval = dpif_open(devname, &dpif);
99         if (!retval) {
100             svec_add(all_dps, devname);
101             dpif_close(dpif);
102         } else if (retval != ENODEV && !error) {
103             error = retval;
104         }
105     }
106     return error;
107 }
108
109 static int
110 dpif_linux_open(const char *name UNUSED, char *suffix, bool create,
111                 struct dpif **dpifp)
112 {
113     int minor;
114
115     minor = !strncmp(name, "dp", 2)
116             && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
117     if (create) {
118         if (minor >= 0) {
119             return create_minor(suffix, minor, dpifp);
120         } else {
121             /* Scan for unused minor number. */
122             for (minor = 0; minor < ODP_MAX; minor++) {
123                 int error = create_minor(suffix, minor, dpifp);
124                 if (error != EBUSY) {
125                     return error;
126                 }
127             }
128
129             /* All datapath numbers in use. */
130             return ENOBUFS;
131         }
132     } else {
133         struct dpif_linux *dpif;
134         struct odp_port port;
135         int error;
136
137         if (minor < 0) {
138             error = lookup_minor(suffix, &minor);
139             if (error) {
140                 return error;
141             }
142         }
143
144         error = open_minor(minor, dpifp);
145         if (error) {
146             return error;
147         }
148         dpif = dpif_linux_cast(*dpifp);
149
150         /* We need the local port's ifindex for the poll function.  Start by
151          * getting the local port's name. */
152         memset(&port, 0, sizeof port);
153         port.port = ODPP_LOCAL;
154         if (ioctl(dpif->fd, ODP_PORT_QUERY, &port)) {
155             error = errno;
156             if (error != ENODEV) {
157                 VLOG_WARN("%s: probe returned unexpected error: %s",
158                           dpif_name(*dpifp), strerror(error));
159             }
160             dpif_close(*dpifp);
161             return error;
162         }
163
164         /* Then use that to finish up opening. */
165         return finish_open(&dpif->dpif, port.devname);
166     }
167 }
168
169 static void
170 dpif_linux_close(struct dpif *dpif_)
171 {
172     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
173     rtnetlink_notifier_unregister(&dpif->port_notifier);
174     svec_destroy(&dpif->changed_ports);
175     free(dpif->local_ifname);
176     close(dpif->fd);
177     free(dpif);
178 }
179
180 static int
181 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
182 {
183     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
184
185     svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
186     svec_add(all_names, dpif->local_ifname);
187     return 0;
188 }
189
190 static int
191 dpif_linux_delete(struct dpif *dpif_)
192 {
193     return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
194 }
195
196 static int
197 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
198 {
199     return do_ioctl(dpif_, ODP_DP_STATS, stats);
200 }
201
202 static int
203 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
204 {
205     int drop_frags;
206     int error;
207
208     error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
209     if (!error) {
210         *drop_fragsp = drop_frags & 1;
211     }
212     return error;
213 }
214
215 static int
216 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
217 {
218     int drop_frags_int = drop_frags;
219     return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
220 }
221
222 static int
223 dpif_linux_port_add(struct dpif *dpif_, const char *devname, uint16_t flags,
224                     uint16_t *port_no)
225 {
226     struct odp_port port;
227     int error;
228
229     memset(&port, 0, sizeof port);
230     strncpy(port.devname, devname, sizeof port.devname);
231     port.flags = flags;
232     error = do_ioctl(dpif_, ODP_PORT_ADD, &port);
233     if (!error) {
234         *port_no = port.port;
235     }
236     return error;
237 }
238
239 static int
240 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
241 {
242     int tmp = port_no;
243     return do_ioctl(dpif_, ODP_PORT_DEL, &tmp);
244 }
245
246 static int
247 dpif_linux_port_query_by_number(const struct dpif *dpif_, uint16_t port_no,
248                           struct odp_port *port)
249 {
250     memset(port, 0, sizeof *port);
251     port->port = port_no;
252     return do_ioctl(dpif_, ODP_PORT_QUERY, port);
253 }
254
255 static int
256 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
257                               struct odp_port *port)
258 {
259     memset(port, 0, sizeof *port);
260     strncpy(port->devname, devname, sizeof port->devname);
261     return do_ioctl(dpif_, ODP_PORT_QUERY, port);
262 }
263
264 static int
265 dpif_linux_flow_flush(struct dpif *dpif_)
266 {
267     return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
268 }
269
270 static int
271 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
272 {
273     struct odp_portvec pv;
274     int error;
275
276     pv.ports = ports;
277     pv.n_ports = n;
278     error = do_ioctl(dpif_, ODP_PORT_LIST, &pv);
279     return error ? -error : pv.n_ports;
280 }
281
282 static int
283 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
284 {
285     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
286
287     if (dpif->change_error) {
288         dpif->change_error = false;
289         svec_clear(&dpif->changed_ports);
290         return ENOBUFS;
291     } else if (dpif->changed_ports.n) {
292         *devnamep = dpif->changed_ports.names[--dpif->changed_ports.n];
293         return 0;
294     } else {
295         return EAGAIN;
296     }
297 }
298
299 static void
300 dpif_linux_port_poll_wait(const struct dpif *dpif_)
301 {
302     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
303     if (dpif->changed_ports.n || dpif->change_error) {
304         poll_immediate_wake();
305     } else {
306         rtnetlink_notifier_wait();
307     }
308 }
309
310 static int
311 dpif_linux_port_group_get(const struct dpif *dpif_, int group,
312                           uint16_t ports[], int n)
313 {
314     struct odp_port_group pg;
315     int error;
316
317     assert(n <= UINT16_MAX);
318     pg.group = group;
319     pg.ports = ports;
320     pg.n_ports = n;
321     error = do_ioctl(dpif_, ODP_PORT_GROUP_GET, &pg);
322     return error ? -error : pg.n_ports;
323 }
324
325 static int
326 dpif_linux_port_group_set(struct dpif *dpif_, int group,
327                           const uint16_t ports[], int n)
328 {
329     struct odp_port_group pg;
330
331     assert(n <= UINT16_MAX);
332     pg.group = group;
333     pg.ports = (uint16_t *) ports;
334     pg.n_ports = n;
335     return do_ioctl(dpif_, ODP_PORT_GROUP_SET, &pg);
336 }
337
338 static int
339 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
340 {
341     struct odp_flowvec fv;
342     fv.flows = flows;
343     fv.n_flows = n;
344     return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
345 }
346
347 static int
348 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
349 {
350     return do_ioctl(dpif_, ODP_FLOW_PUT, put);
351 }
352
353 static int
354 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
355 {
356     return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
357 }
358
359 static int
360 dpif_linux_flow_list(const struct dpif *dpif_, struct odp_flow flows[], int n)
361 {
362     struct odp_flowvec fv;
363     int error;
364
365     fv.flows = flows;
366     fv.n_flows = n;
367     error = do_ioctl(dpif_, ODP_FLOW_LIST, &fv);
368     return error ? -error : fv.n_flows;
369 }
370
371 static int
372 dpif_linux_execute(struct dpif *dpif_, uint16_t in_port,
373                    const union odp_action actions[], int n_actions,
374                    const struct ofpbuf *buf)
375 {
376     struct odp_execute execute;
377     memset(&execute, 0, sizeof execute);
378     execute.in_port = in_port;
379     execute.actions = (union odp_action *) actions;
380     execute.n_actions = n_actions;
381     execute.data = buf->data;
382     execute.length = buf->size;
383     return do_ioctl(dpif_, ODP_EXECUTE, &execute);
384 }
385
386 static int
387 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
388 {
389     return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
390 }
391
392 static int
393 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
394 {
395     return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
396 }
397
398 static int
399 dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
400 {
401     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
402     struct ofpbuf *buf;
403     int retval;
404     int error;
405
406     buf = ofpbuf_new(65536);
407     retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
408     if (retval < 0) {
409         error = errno;
410         if (error != EAGAIN) {
411             VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
412                          dpif_name(dpif_), strerror(error));
413         }
414     } else if (retval >= sizeof(struct odp_msg)) {
415         struct odp_msg *msg = buf->data;
416         if (msg->length <= retval) {
417             buf->size += retval;
418             *bufp = buf;
419             return 0;
420         } else {
421             VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
422                          "from %"PRIu32" bytes to %d",
423                          dpif_name(dpif_), msg->length, retval);
424             error = ERANGE;
425         }
426     } else if (!retval) {
427         VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
428         error = EPROTO;
429     } else {
430         VLOG_WARN_RL(&error_rl,
431                      "%s: discarding too-short message (%d bytes)",
432                      dpif_name(dpif_), retval);
433         error = ERANGE;
434     }
435
436     *bufp = NULL;
437     ofpbuf_delete(buf);
438     return error;
439 }
440
441 static void
442 dpif_linux_recv_wait(struct dpif *dpif_)
443 {
444     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
445     poll_fd_wait(dpif->fd, POLLIN);
446 }
447
448 const struct dpif_class dpif_linux_class = {
449     "",                         /* This is the default class. */
450     "linux",
451     NULL,
452     NULL,
453     dpif_linux_enumerate,
454     dpif_linux_open,
455     dpif_linux_close,
456     dpif_linux_get_all_names,
457     dpif_linux_delete,
458     dpif_linux_get_stats,
459     dpif_linux_get_drop_frags,
460     dpif_linux_set_drop_frags,
461     dpif_linux_port_add,
462     dpif_linux_port_del,
463     dpif_linux_port_query_by_number,
464     dpif_linux_port_query_by_name,
465     dpif_linux_port_list,
466     dpif_linux_port_poll,
467     dpif_linux_port_poll_wait,
468     dpif_linux_port_group_get,
469     dpif_linux_port_group_set,
470     dpif_linux_flow_get,
471     dpif_linux_flow_put,
472     dpif_linux_flow_del,
473     dpif_linux_flow_flush,
474     dpif_linux_flow_list,
475     dpif_linux_execute,
476     dpif_linux_recv_get_mask,
477     dpif_linux_recv_set_mask,
478     dpif_linux_recv,
479     dpif_linux_recv_wait,
480 };
481 \f
482 static int get_openvswitch_major(void);
483 static int get_major(const char *target);
484
485 static int
486 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
487 {
488     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
489     return ioctl(dpif->fd, cmd, arg) ? errno : 0;
490 }
491
492 static int
493 lookup_minor(const char *name, int *minorp)
494 {
495     struct ethtool_drvinfo drvinfo;
496     int minor, port_no;
497     struct ifreq ifr;
498     int error;
499     int sock;
500
501     sock = socket(AF_INET, SOCK_DGRAM, 0);
502     if (sock < 0) {
503         VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
504         error = errno;
505         goto error;
506     }
507
508     memset(&ifr, 0, sizeof ifr);
509     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
510     ifr.ifr_data = (caddr_t) &drvinfo;
511
512     memset(&drvinfo, 0, sizeof drvinfo);
513     drvinfo.cmd = ETHTOOL_GDRVINFO;
514     if (ioctl(sock, SIOCETHTOOL, &ifr)) {
515         VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
516         error = errno;
517         goto error_close_sock;
518     }
519
520     if (strcmp(drvinfo.driver, "openvswitch")) {
521         VLOG_WARN("%s is not an openvswitch device", name);
522         error = EOPNOTSUPP;
523         goto error_close_sock;
524     }
525
526     if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
527         VLOG_WARN("%s ethtool bus_info has unexpected format", name);
528         error = EPROTOTYPE;
529         goto error_close_sock;
530     } else if (port_no != ODPP_LOCAL) {
531         /* This is an Open vSwitch device but not the local port.  We
532          * intentionally support only using the name of the local port as the
533          * name of a datapath; otherwise, it would be too difficult to
534          * enumerate all the names of a datapath. */
535         error = EOPNOTSUPP;
536         goto error_close_sock;
537     }
538
539     *minorp = minor;
540     close(sock);
541     return 0;
542
543 error_close_sock:
544     close(sock);
545 error:
546     return error;
547 }
548
549 static int
550 make_openvswitch_device(int minor, char **fnp)
551 {
552     const char dirname[] = "/dev/net";
553     int major;
554     dev_t dev;
555     struct stat s;
556     char fn[128];
557
558     major = get_openvswitch_major();
559     if (major < 0) {
560         return -major;
561     }
562     dev = makedev(major, minor);
563
564     *fnp = NULL;
565     sprintf(fn, "%s/dp%d", dirname, minor);
566     if (!stat(fn, &s)) {
567         if (!S_ISCHR(s.st_mode)) {
568             VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
569                          fn);
570         } else if (s.st_rdev != dev) {
571             VLOG_WARN_RL(&error_rl,
572                          "%s is device %u:%u but should be %u:%u, fixing",
573                          fn, major(s.st_rdev), minor(s.st_rdev),
574                          major(dev), minor(dev));
575         } else {
576             goto success;
577         }
578         if (unlink(fn)) {
579             VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
580                          fn, strerror(errno));
581             return errno;
582         }
583     } else if (errno == ENOENT) {
584         if (stat(dirname, &s)) {
585             if (errno == ENOENT) {
586                 if (mkdir(dirname, 0755)) {
587                     VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
588                                  dirname, strerror(errno));
589                     return errno;
590                 }
591             } else {
592                 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
593                              dirname, strerror(errno));
594                 return errno;
595             }
596         }
597     } else {
598         VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
599         return errno;
600     }
601
602     /* The device needs to be created. */
603     if (mknod(fn, S_IFCHR | 0700, dev)) {
604         VLOG_WARN_RL(&error_rl,
605                      "%s: creating character device %u:%u failed (%s)",
606                      fn, major(dev), minor(dev), strerror(errno));
607         return errno;
608     }
609
610 success:
611     *fnp = xstrdup(fn);
612     return 0;
613 }
614
615 /* Return the major device number of the Open vSwitch device.  If it
616  * cannot be determined, a negative errno is returned. */
617 static int
618 get_openvswitch_major(void)
619 {
620     static int openvswitch_major = -1;
621     if (openvswitch_major < 0) {
622         openvswitch_major = get_major("openvswitch");
623     }
624     return openvswitch_major;
625 }
626
627 static int
628 get_major(const char *target)
629 {
630     const char fn[] = "/proc/devices";
631     char line[128];
632     FILE *file;
633     int ln;
634
635     file = fopen(fn, "r");
636     if (!file) {
637         VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
638         return -errno;
639     }
640
641     for (ln = 1; fgets(line, sizeof line, file); ln++) {
642         char name[64];
643         int major;
644
645         if (!strncmp(line, "Character", 9) || line[0] == '\0') {
646             /* Nothing to do. */
647         } else if (!strncmp(line, "Block", 5)) {
648             /* We only want character devices, so skip the rest of the file. */
649             break;
650         } else if (sscanf(line, "%d %63s", &major, name)) {
651             if (!strcmp(name, target)) {
652                 fclose(file);
653                 return major;
654             }
655         } else {
656             static bool warned;
657             if (!warned) {
658                 VLOG_WARN("%s:%d: syntax error", fn, ln);
659             }
660             warned = true;
661         }
662     }
663
664     VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
665     return -ENODEV;
666 }
667
668 static int
669 finish_open(struct dpif *dpif_, const char *local_ifname)
670 {
671     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
672     dpif->local_ifname = strdup(local_ifname);
673     dpif->local_ifindex = if_nametoindex(local_ifname);
674     if (!dpif->local_ifindex) {
675         int error = errno;
676         dpif_close(dpif_);
677         VLOG_WARN("could not get ifindex of %s device: %s",
678                   local_ifname, strerror(errno));
679         return error;
680     }
681     return 0;
682 }
683
684 static int
685 create_minor(const char *name, int minor, struct dpif **dpifp)
686 {
687     int error = open_minor(minor, dpifp);
688     if (!error) {
689         error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
690         if (!error) {
691             error = finish_open(*dpifp, name);
692         } else {
693             dpif_close(*dpifp);
694         }
695     }
696     return error;
697 }
698
699 static int
700 open_minor(int minor, struct dpif **dpifp)
701 {
702     int error;
703     char *fn;
704     int fd;
705
706     error = make_openvswitch_device(minor, &fn);
707     if (error) {
708         return error;
709     }
710
711     fd = open(fn, O_RDONLY | O_NONBLOCK);
712     if (fd >= 0) {
713         struct dpif_linux *dpif = xmalloc(sizeof *dpif);
714         error = rtnetlink_notifier_register(&dpif->port_notifier,
715                                            dpif_linux_port_changed, dpif);
716         if (!error) {
717             char *name;
718
719             name = xasprintf("dp%d", minor);
720             dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
721             free(name);
722
723             dpif->fd = fd;
724             dpif->local_ifname = NULL;
725             dpif->minor = minor;
726             dpif->local_ifindex = 0;
727             svec_init(&dpif->changed_ports);
728             dpif->change_error = false;
729             *dpifp = &dpif->dpif;
730         } else {
731             free(dpif);
732         }
733     } else {
734         error = errno;
735         VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
736     }
737     free(fn);
738
739     return error;
740 }
741
742 static void
743 dpif_linux_port_changed(const struct rtnetlink_change *change, void *dpif_)
744 {
745     struct dpif_linux *dpif = dpif_;
746
747     if (change) {
748         if (change->master_ifindex == dpif->local_ifindex
749             && (change->nlmsg_type == RTM_NEWLINK
750                 || change->nlmsg_type == RTM_DELLINK))
751         {
752             /* Our datapath changed, either adding a new port or deleting an
753              * existing one. */
754             if (!svec_contains(&dpif->changed_ports, change->ifname)) {
755                 svec_add(&dpif->changed_ports, change->ifname);
756                 svec_sort(&dpif->changed_ports);
757             }
758         }
759     } else {
760         dpif->change_error = true;
761     }
762 }