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