datapath: Change listing flows to use an iterator concept.
[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_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
398 {
399     *statep = xzalloc(sizeof(struct odp_flow_dump));
400     return 0;
401 }
402
403 static int
404 dpif_linux_flow_dump_next(const struct dpif *dpif, void *state,
405                           struct odp_flow *flow)
406 {
407     struct odp_flow_dump *dump = state;
408     int error;
409
410     dump->flow = flow;
411     error = do_ioctl(dpif, ODP_FLOW_DUMP, dump);
412     return error ? error : flow->flags & ODPFF_EOF ? EOF : 0;
413 }
414
415 static int
416 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
417 {
418     free(state);
419     return 0;
420 }
421
422 static int
423 dpif_linux_execute(struct dpif *dpif_,
424                    const struct nlattr *actions, size_t actions_len,
425                    const struct ofpbuf *buf)
426 {
427     struct odp_execute execute;
428     memset(&execute, 0, sizeof execute);
429     execute.actions = (struct nlattr *) actions;
430     execute.actions_len = actions_len;
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     dpif_linux_enumerate,
528     dpif_linux_open,
529     dpif_linux_close,
530     dpif_linux_get_all_names,
531     dpif_linux_destroy,
532     dpif_linux_get_stats,
533     dpif_linux_get_drop_frags,
534     dpif_linux_set_drop_frags,
535     dpif_linux_port_add,
536     dpif_linux_port_del,
537     dpif_linux_port_query_by_number,
538     dpif_linux_port_query_by_name,
539     dpif_linux_port_list,
540     dpif_linux_port_poll,
541     dpif_linux_port_poll_wait,
542     dpif_linux_flow_get,
543     dpif_linux_flow_put,
544     dpif_linux_flow_del,
545     dpif_linux_flow_flush,
546     dpif_linux_flow_dump_start,
547     dpif_linux_flow_dump_next,
548     dpif_linux_flow_dump_done,
549     dpif_linux_execute,
550     dpif_linux_recv_get_mask,
551     dpif_linux_recv_set_mask,
552     dpif_linux_get_sflow_probability,
553     dpif_linux_set_sflow_probability,
554     dpif_linux_queue_to_priority,
555     dpif_linux_recv,
556     dpif_linux_recv_wait,
557 };
558 \f
559 static int get_openvswitch_major(void);
560 static int get_major(const char *target);
561
562 static int
563 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
564 {
565     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
566     return ioctl(dpif->fd, cmd, arg) ? errno : 0;
567 }
568
569 static int
570 lookup_minor(const char *name, int *minorp)
571 {
572     struct ethtool_drvinfo drvinfo;
573     int minor, port_no;
574     struct ifreq ifr;
575     int error;
576     int sock;
577
578     sock = socket(AF_INET, SOCK_DGRAM, 0);
579     if (sock < 0) {
580         VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
581         error = errno;
582         goto error;
583     }
584
585     memset(&ifr, 0, sizeof ifr);
586     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
587     ifr.ifr_data = (caddr_t) &drvinfo;
588
589     memset(&drvinfo, 0, sizeof drvinfo);
590     drvinfo.cmd = ETHTOOL_GDRVINFO;
591     if (ioctl(sock, SIOCETHTOOL, &ifr)) {
592         VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
593         error = errno;
594         goto error_close_sock;
595     }
596
597     if (strcmp(drvinfo.driver, "openvswitch")) {
598         VLOG_WARN("%s is not an openvswitch device", name);
599         error = EOPNOTSUPP;
600         goto error_close_sock;
601     }
602
603     if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
604         VLOG_WARN("%s ethtool bus_info has unexpected format", name);
605         error = EPROTOTYPE;
606         goto error_close_sock;
607     } else if (port_no != ODPP_LOCAL) {
608         /* This is an Open vSwitch device but not the local port.  We
609          * intentionally support only using the name of the local port as the
610          * name of a datapath; otherwise, it would be too difficult to
611          * enumerate all the names of a datapath. */
612         error = EOPNOTSUPP;
613         goto error_close_sock;
614     }
615
616     *minorp = minor;
617     close(sock);
618     return 0;
619
620 error_close_sock:
621     close(sock);
622 error:
623     return error;
624 }
625
626 static int
627 make_openvswitch_device(int minor, char **fnp)
628 {
629     const char dirname[] = "/dev/net";
630     int major;
631     dev_t dev;
632     struct stat s;
633     char fn[128];
634
635     *fnp = NULL;
636
637     major = get_openvswitch_major();
638     if (major < 0) {
639         return -major;
640     }
641     dev = makedev(major, minor);
642
643     sprintf(fn, "%s/dp%d", dirname, minor);
644     if (!stat(fn, &s)) {
645         if (!S_ISCHR(s.st_mode)) {
646             VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
647                          fn);
648         } else if (s.st_rdev != dev) {
649             VLOG_WARN_RL(&error_rl,
650                          "%s is device %u:%u but should be %u:%u, fixing",
651                          fn, major(s.st_rdev), minor(s.st_rdev),
652                          major(dev), minor(dev));
653         } else {
654             goto success;
655         }
656         if (unlink(fn)) {
657             VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
658                          fn, strerror(errno));
659             return errno;
660         }
661     } else if (errno == ENOENT) {
662         if (stat(dirname, &s)) {
663             if (errno == ENOENT) {
664                 if (mkdir(dirname, 0755)) {
665                     VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
666                                  dirname, strerror(errno));
667                     return errno;
668                 }
669             } else {
670                 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
671                              dirname, strerror(errno));
672                 return errno;
673             }
674         }
675     } else {
676         VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
677         return errno;
678     }
679
680     /* The device needs to be created. */
681     if (mknod(fn, S_IFCHR | 0700, dev)) {
682         VLOG_WARN_RL(&error_rl,
683                      "%s: creating character device %u:%u failed (%s)",
684                      fn, major(dev), minor(dev), strerror(errno));
685         return errno;
686     }
687
688 success:
689     *fnp = xstrdup(fn);
690     return 0;
691 }
692
693 /* Return the major device number of the Open vSwitch device.  If it
694  * cannot be determined, a negative errno is returned. */
695 static int
696 get_openvswitch_major(void)
697 {
698     static int openvswitch_major = -1;
699     if (openvswitch_major < 0) {
700         openvswitch_major = get_major("openvswitch");
701     }
702     return openvswitch_major;
703 }
704
705 static int
706 get_major(const char *target)
707 {
708     const char fn[] = "/proc/devices";
709     char line[128];
710     FILE *file;
711     int ln;
712
713     file = fopen(fn, "r");
714     if (!file) {
715         VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
716         return -errno;
717     }
718
719     for (ln = 1; fgets(line, sizeof line, file); ln++) {
720         char name[64];
721         int major;
722
723         if (!strncmp(line, "Character", 9) || line[0] == '\0') {
724             /* Nothing to do. */
725         } else if (!strncmp(line, "Block", 5)) {
726             /* We only want character devices, so skip the rest of the file. */
727             break;
728         } else if (sscanf(line, "%d %63s", &major, name)) {
729             if (!strcmp(name, target)) {
730                 fclose(file);
731                 return major;
732             }
733         } else {
734             VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
735         }
736     }
737
738     fclose(file);
739
740     VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
741     return -ENODEV;
742 }
743
744 static int
745 finish_open(struct dpif *dpif_, const char *local_ifname)
746 {
747     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
748     dpif->local_ifname = xstrdup(local_ifname);
749     dpif->local_ifindex = if_nametoindex(local_ifname);
750     if (!dpif->local_ifindex) {
751         int error = errno;
752         dpif_uninit(dpif_, true);
753         VLOG_WARN("could not get ifindex of %s device: %s",
754                   local_ifname, strerror(errno));
755         return error;
756     }
757     return 0;
758 }
759
760 static int
761 create_minor(const char *name, int minor, struct dpif **dpifp)
762 {
763     int error = open_minor(minor, dpifp);
764     if (!error) {
765         error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
766         if (!error) {
767             error = finish_open(*dpifp, name);
768         } else {
769             dpif_uninit(*dpifp, true);
770         }
771     }
772     return error;
773 }
774
775 static int
776 open_minor(int minor, struct dpif **dpifp)
777 {
778     int error;
779     char *fn;
780     int fd;
781
782     error = make_openvswitch_device(minor, &fn);
783     if (error) {
784         return error;
785     }
786
787     fd = open(fn, O_RDONLY | O_NONBLOCK);
788     if (fd >= 0) {
789         struct dpif_linux *dpif = xmalloc(sizeof *dpif);
790         error = rtnetlink_link_notifier_register(&dpif->port_notifier,
791                                                  dpif_linux_port_changed,
792                                                  dpif);
793         if (!error) {
794             char *name;
795
796             name = xasprintf("dp%d", minor);
797             dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
798             free(name);
799
800             dpif->fd = fd;
801             dpif->local_ifname = NULL;
802             dpif->minor = minor;
803             dpif->local_ifindex = 0;
804             shash_init(&dpif->changed_ports);
805             dpif->change_error = false;
806             *dpifp = &dpif->dpif;
807         } else {
808             free(dpif);
809         }
810     } else {
811         error = errno;
812         VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
813     }
814     free(fn);
815
816     return error;
817 }
818
819 static void
820 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
821                         void *dpif_)
822 {
823     struct dpif_linux *dpif = dpif_;
824
825     if (change) {
826         if (change->master_ifindex == dpif->local_ifindex
827             && (change->nlmsg_type == RTM_NEWLINK
828                 || change->nlmsg_type == RTM_DELLINK))
829         {
830             /* Our datapath changed, either adding a new port or deleting an
831              * existing one. */
832             shash_add_once(&dpif->changed_ports, change->ifname, NULL);
833         }
834     } else {
835         dpif->change_error = true;
836     }
837 }