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