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