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