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