Merge "master" into "wdp".
[sliver-openvswitch.git] / lib / xfif-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 "xfif.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 "netdev.h"
37 #include "ofpbuf.h"
38 #include "poll-loop.h"
39 #include "rtnetlink.h"
40 #include "shash.h"
41 #include "svec.h"
42 #include "util.h"
43 #include "vlog.h"
44 #include "xfif-provider.h"
45
46 VLOG_DEFINE_THIS_MODULE(xfif_linux)
47
48 /* Datapath interface for the openvswitch Linux kernel module. */
49 struct xfif_linux {
50     struct xfif xfif;
51     int fd;
52
53     /* Used by xfif_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 xfif *, int cmd, const void *arg);
67 static int lookup_minor(const char *name, int *minor);
68 static int finish_open(struct xfif *, const char *local_ifname);
69 static int get_openvswitch_major(void);
70 static int create_minor(const char *name, int minor, struct xfif **xfifp);
71 static int open_minor(int minor, struct xfif **xfifp);
72 static int make_openvswitch_device(int minor, char **fnp);
73 static void xfif_linux_port_changed(const struct rtnetlink_change *,
74                                     void *xfif);
75
76 static struct xfif_linux *
77 xfif_linux_cast(const struct xfif *xfif)
78 {
79     xfif_assert_class(xfif, &xfif_linux_class);
80     return CONTAINER_OF(xfif, struct xfif_linux, xfif);
81 }
82
83 static int
84 xfif_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 < XFLOW_MAX; i++) {
98         struct xfif *xfif;
99         char devname[16];
100         int retval;
101
102         sprintf(devname, "dp%d", i);
103         retval = xfif_open(devname, "system", &xfif);
104         if (!retval) {
105             svec_add(all_dps, devname);
106             xfif_uninit(xfif, true);
107         } else if (retval != ENODEV && !error) {
108             error = retval;
109         }
110     }
111     return error;
112 }
113
114 static int
115 xfif_linux_open(const char *name, const char *type OVS_UNUSED, bool create,
116                 struct xfif **xfifp)
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, xfifp);
125         } else {
126             /* Scan for unused minor number. */
127             for (minor = 0; minor < XFLOW_MAX; minor++) {
128                 int error = create_minor(name, minor, xfifp);
129                 if (error != EBUSY) {
130                     return error;
131                 }
132             }
133
134             /* All datapath numbers in use. */
135             return ENOBUFS;
136         }
137     } else {
138         struct xfif_linux *xfif;
139         struct xflow_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, xfifp);
150         if (error) {
151             return error;
152         }
153         xfif = xfif_linux_cast(*xfifp);
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 = XFLOWP_LOCAL;
159         if (ioctl(xfif->fd, XFLOW_PORT_QUERY, &port)) {
160             error = errno;
161             if (error != ENODEV) {
162                 VLOG_WARN("%s: probe returned unexpected error: %s",
163                           xfif_name(*xfifp), strerror(error));
164             }
165             xfif_uninit(*xfifp, true);
166             return error;
167         }
168
169         /* Then use that to finish up opening. */
170         return finish_open(&xfif->xfif, port.devname);
171     }
172 }
173
174 static void
175 xfif_linux_close(struct xfif *xfif_)
176 {
177     struct xfif_linux *xfif = xfif_linux_cast(xfif_);
178     rtnetlink_notifier_unregister(&xfif->port_notifier);
179     shash_destroy(&xfif->changed_ports);
180     free(xfif->local_ifname);
181     close(xfif->fd);
182     free(xfif);
183 }
184
185 static int
186 xfif_linux_get_all_names(const struct xfif *xfif_, struct svec *all_names)
187 {
188     struct xfif_linux *xfif = xfif_linux_cast(xfif_);
189
190     svec_add_nocopy(all_names, xasprintf("dp%d", xfif->minor));
191     svec_add(all_names, xfif->local_ifname);
192     return 0;
193 }
194
195 static int
196 xfif_linux_destroy(struct xfif *xfif_)
197 {
198     struct xflow_port *ports;
199     size_t n_ports;
200     int err;
201     int i;
202
203     err = xfif_port_list(xfif_, &ports, &n_ports);
204     if (err) {
205         return err;
206     }
207
208     for (i = 0; i < n_ports; i++) {
209         if (ports[i].port != XFLOWP_LOCAL) {
210             err = do_ioctl(xfif_, XFLOW_VPORT_DEL, ports[i].devname);
211             if (err) {
212                 VLOG_WARN_RL(&error_rl, "%s: error deleting port %s (%s)",
213                              xfif_name(xfif_), ports[i].devname, strerror(err));
214             }
215         }
216     }
217
218     free(ports);
219
220     return do_ioctl(xfif_, XFLOW_DP_DESTROY, NULL);
221 }
222
223 static int
224 xfif_linux_get_stats(const struct xfif *xfif_, struct xflow_stats *stats)
225 {
226     memset(stats, 0, sizeof *stats);
227     return do_ioctl(xfif_, XFLOW_DP_STATS, stats);
228 }
229
230 static int
231 xfif_linux_get_drop_frags(const struct xfif *xfif_, bool *drop_fragsp)
232 {
233     int drop_frags;
234     int error;
235
236     error = do_ioctl(xfif_, XFLOW_GET_DROP_FRAGS, &drop_frags);
237     if (!error) {
238         *drop_fragsp = drop_frags & 1;
239     }
240     return error;
241 }
242
243 static int
244 xfif_linux_set_drop_frags(struct xfif *xfif_, bool drop_frags)
245 {
246     int drop_frags_int = drop_frags;
247     return do_ioctl(xfif_, XFLOW_SET_DROP_FRAGS, &drop_frags_int);
248 }
249
250 static int
251 xfif_linux_port_add(struct xfif *xfif_, const char *devname, uint16_t flags,
252                     uint16_t *port_no)
253 {
254     struct xflow_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(xfif_, XFLOW_PORT_ATTACH, &port);
261     if (!error) {
262         *port_no = port.port;
263     }
264     return error;
265 }
266
267 static int
268 xfif_linux_port_del(struct xfif *xfif_, uint16_t port_no)
269 {
270     int tmp = port_no;
271     int err;
272     struct xflow_port port;
273
274     err = xfif_port_query_by_number(xfif_, port_no, &port);
275     if (err) {
276         return err;
277     }
278
279     err = do_ioctl(xfif_, XFLOW_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(xfif_, XFLOW_VPORT_DEL, port.devname);
289     }
290
291     return 0;
292 }
293
294 static int
295 xfif_linux_port_query_by_number(const struct xfif *xfif_, uint16_t port_no,
296                           struct xflow_port *port)
297 {
298     memset(port, 0, sizeof *port);
299     port->port = port_no;
300     return do_ioctl(xfif_, XFLOW_PORT_QUERY, port);
301 }
302
303 static int
304 xfif_linux_port_query_by_name(const struct xfif *xfif_, const char *devname,
305                               struct xflow_port *port)
306 {
307     memset(port, 0, sizeof *port);
308     strncpy(port->devname, devname, sizeof port->devname);
309     return do_ioctl(xfif_, XFLOW_PORT_QUERY, port);
310 }
311
312 static int
313 xfif_linux_flow_flush(struct xfif *xfif_)
314 {
315     return do_ioctl(xfif_, XFLOW_FLOW_FLUSH, NULL);
316 }
317
318 static int
319 xfif_linux_port_list(const struct xfif *xfif_, struct xflow_port *ports, int n)
320 {
321     struct xflow_portvec pv;
322     int error;
323
324     pv.ports = ports;
325     pv.n_ports = n;
326     error = do_ioctl(xfif_, XFLOW_PORT_LIST, &pv);
327     return error ? -error : pv.n_ports;
328 }
329
330 static int
331 xfif_linux_port_poll(const struct xfif *xfif_, char **devnamep)
332 {
333     struct xfif_linux *xfif = xfif_linux_cast(xfif_);
334
335     if (xfif->change_error) {
336         xfif->change_error = false;
337         shash_clear(&xfif->changed_ports);
338         return ENOBUFS;
339     } else if (!shash_is_empty(&xfif->changed_ports)) {
340         struct shash_node *node = shash_first(&xfif->changed_ports);
341         *devnamep = shash_steal(&xfif->changed_ports, node);
342         return 0;
343     } else {
344         return EAGAIN;
345     }
346 }
347
348 static void
349 xfif_linux_port_poll_wait(const struct xfif *xfif_)
350 {
351     struct xfif_linux *xfif = xfif_linux_cast(xfif_);
352     if (!shash_is_empty(&xfif->changed_ports) || xfif->change_error) {
353         poll_immediate_wake();
354     } else {
355         rtnetlink_notifier_wait();
356     }
357 }
358
359 static int
360 xfif_linux_port_group_get(const struct xfif *xfif_, int group,
361                           uint16_t ports[], int n)
362 {
363     struct xflow_port_group pg;
364     int error;
365
366     assert(n <= UINT16_MAX);
367     pg.group = group;
368     pg.ports = ports;
369     pg.n_ports = n;
370     error = do_ioctl(xfif_, XFLOW_PORT_GROUP_GET, &pg);
371     return error ? -error : pg.n_ports;
372 }
373
374 static int
375 xfif_linux_port_group_set(struct xfif *xfif_, int group,
376                           const uint16_t ports[], int n)
377 {
378     struct xflow_port_group pg;
379
380     assert(n <= UINT16_MAX);
381     pg.group = group;
382     pg.ports = (uint16_t *) ports;
383     pg.n_ports = n;
384     return do_ioctl(xfif_, XFLOW_PORT_GROUP_SET, &pg);
385 }
386
387 static int
388 xfif_linux_flow_get(const struct xfif *xfif_, struct xflow_flow flows[], int n)
389 {
390     struct xflow_flowvec fv;
391     fv.flows = flows;
392     fv.n_flows = n;
393     return do_ioctl(xfif_, XFLOW_FLOW_GET, &fv);
394 }
395
396 static int
397 xfif_linux_flow_put(struct xfif *xfif_, struct xflow_flow_put *put)
398 {
399     return do_ioctl(xfif_, XFLOW_FLOW_PUT, put);
400 }
401
402 static int
403 xfif_linux_flow_del(struct xfif *xfif_, struct xflow_flow *flow)
404 {
405     return do_ioctl(xfif_, XFLOW_FLOW_DEL, flow);
406 }
407
408 static int
409 xfif_linux_flow_list(const struct xfif *xfif_, struct xflow_flow flows[], int n)
410 {
411     struct xflow_flowvec fv;
412     int error;
413
414     fv.flows = flows;
415     fv.n_flows = n;
416     error = do_ioctl(xfif_, XFLOW_FLOW_LIST, &fv);
417     return error ? -error : fv.n_flows;
418 }
419
420 static int
421 xfif_linux_execute(struct xfif *xfif_, uint16_t in_port,
422                    const union xflow_action actions[], int n_actions,
423                    const struct ofpbuf *buf)
424 {
425     struct xflow_execute execute;
426     memset(&execute, 0, sizeof execute);
427     execute.in_port = in_port;
428     execute.actions = (union xflow_action *) actions;
429     execute.n_actions = n_actions;
430     execute.data = buf->data;
431     execute.length = buf->size;
432     return do_ioctl(xfif_, XFLOW_EXECUTE, &execute);
433 }
434
435 static int
436 xfif_linux_recv_get_mask(const struct xfif *xfif_, int *listen_mask)
437 {
438     return do_ioctl(xfif_, XFLOW_GET_LISTEN_MASK, listen_mask);
439 }
440
441 static int
442 xfif_linux_recv_set_mask(struct xfif *xfif_, int listen_mask)
443 {
444     return do_ioctl(xfif_, XFLOW_SET_LISTEN_MASK, &listen_mask);
445 }
446
447 static int
448 xfif_linux_get_sflow_probability(const struct xfif *xfif_,
449                                  uint32_t *probability)
450 {
451     return do_ioctl(xfif_, XFLOW_GET_SFLOW_PROBABILITY, probability);
452 }
453
454 static int
455 xfif_linux_set_sflow_probability(struct xfif *xfif_, uint32_t probability)
456 {
457     return do_ioctl(xfif_, XFLOW_SET_SFLOW_PROBABILITY, &probability);
458 }
459
460 static int
461 xfif_linux_queue_to_priority(const struct xfif *xfif OVS_UNUSED,
462                              uint32_t queue_id, uint32_t *priority)
463 {
464     if (queue_id < 0xf000) {
465         *priority = TC_H_MAKE(1 << 16, queue_id + 1);
466         return 0;
467     } else {
468         return EINVAL;
469     }
470 }
471
472 static int
473 xfif_linux_recv(struct xfif *xfif_, struct ofpbuf **bufp)
474 {
475     struct xfif_linux *xfif = xfif_linux_cast(xfif_);
476     struct ofpbuf *buf;
477     int retval;
478     int error;
479
480     buf = ofpbuf_new_with_headroom(65536, XFIF_RECV_MSG_PADDING);
481     retval = read(xfif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
482     if (retval < 0) {
483         error = errno;
484         if (error != EAGAIN) {
485             VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
486                          xfif_name(xfif_), strerror(error));
487         }
488     } else if (retval >= sizeof(struct xflow_msg)) {
489         struct xflow_msg *msg = buf->data;
490         if (msg->length <= retval) {
491             buf->size += retval;
492             *bufp = buf;
493             return 0;
494         } else {
495             VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
496                          "from %"PRIu32" bytes to %d",
497                          xfif_name(xfif_), msg->length, retval);
498             error = ERANGE;
499         }
500     } else if (!retval) {
501         VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", xfif_name(xfif_));
502         error = EPROTO;
503     } else {
504         VLOG_WARN_RL(&error_rl,
505                      "%s: discarding too-short message (%d bytes)",
506                      xfif_name(xfif_), retval);
507         error = ERANGE;
508     }
509
510     *bufp = NULL;
511     ofpbuf_delete(buf);
512     return error;
513 }
514
515 static void
516 xfif_linux_recv_wait(struct xfif *xfif_)
517 {
518     struct xfif_linux *xfif = xfif_linux_cast(xfif_);
519     poll_fd_wait(xfif->fd, POLLIN);
520 }
521
522 const struct xfif_class xfif_linux_class = {
523     "system",
524     NULL,
525     NULL,
526     xfif_linux_enumerate,
527     xfif_linux_open,
528     xfif_linux_close,
529     xfif_linux_get_all_names,
530     xfif_linux_destroy,
531     xfif_linux_get_stats,
532     xfif_linux_get_drop_frags,
533     xfif_linux_set_drop_frags,
534     xfif_linux_port_add,
535     xfif_linux_port_del,
536     xfif_linux_port_query_by_number,
537     xfif_linux_port_query_by_name,
538     xfif_linux_port_list,
539     xfif_linux_port_poll,
540     xfif_linux_port_poll_wait,
541     xfif_linux_port_group_get,
542     xfif_linux_port_group_set,
543     xfif_linux_flow_get,
544     xfif_linux_flow_put,
545     xfif_linux_flow_del,
546     xfif_linux_flow_flush,
547     xfif_linux_flow_list,
548     xfif_linux_execute,
549     xfif_linux_recv_get_mask,
550     xfif_linux_recv_set_mask,
551     xfif_linux_get_sflow_probability,
552     xfif_linux_set_sflow_probability,
553     xfif_linux_queue_to_priority,
554     xfif_linux_recv,
555     xfif_linux_recv_wait,
556 };
557 \f
558 static int get_openvswitch_major(void);
559 static int get_major(const char *target);
560
561 static int
562 do_ioctl(const struct xfif *xfif_, int cmd, const void *arg)
563 {
564     struct xfif_linux *xfif = xfif_linux_cast(xfif_);
565     return ioctl(xfif->fd, cmd, arg) ? errno : 0;
566 }
567
568 static int
569 lookup_minor(const char *name, int *minorp)
570 {
571     struct ethtool_drvinfo drvinfo;
572     int minor, port_no;
573     struct ifreq ifr;
574     int error;
575     int sock;
576
577     sock = socket(AF_INET, SOCK_DGRAM, 0);
578     if (sock < 0) {
579         VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
580         error = errno;
581         goto error;
582     }
583
584     memset(&ifr, 0, sizeof ifr);
585     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
586     ifr.ifr_data = (caddr_t) &drvinfo;
587
588     memset(&drvinfo, 0, sizeof drvinfo);
589     drvinfo.cmd = ETHTOOL_GDRVINFO;
590     if (ioctl(sock, SIOCETHTOOL, &ifr)) {
591         VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
592         error = errno;
593         goto error_close_sock;
594     }
595
596     if (strcmp(drvinfo.driver, "openvswitch")) {
597         VLOG_WARN("%s is not an openvswitch device", name);
598         error = EOPNOTSUPP;
599         goto error_close_sock;
600     }
601
602     if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
603         VLOG_WARN("%s ethtool bus_info has unexpected format", name);
604         error = EPROTOTYPE;
605         goto error_close_sock;
606     } else if (port_no != XFLOWP_LOCAL) {
607         /* This is an Open vSwitch device but not the local port.  We
608          * intentionally support only using the name of the local port as the
609          * name of a datapath; otherwise, it would be too difficult to
610          * enumerate all the names of a datapath. */
611         error = EOPNOTSUPP;
612         goto error_close_sock;
613     }
614
615     *minorp = minor;
616     close(sock);
617     return 0;
618
619 error_close_sock:
620     close(sock);
621 error:
622     return error;
623 }
624
625 static int
626 make_openvswitch_device(int minor, char **fnp)
627 {
628     const char dirname[] = "/dev/net";
629     int major;
630     dev_t dev;
631     struct stat s;
632     char fn[128];
633
634     *fnp = NULL;
635
636     major = get_openvswitch_major();
637     if (major < 0) {
638         return -major;
639     }
640     dev = makedev(major, minor);
641
642     sprintf(fn, "%s/dp%d", dirname, minor);
643     if (!stat(fn, &s)) {
644         if (!S_ISCHR(s.st_mode)) {
645             VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
646                          fn);
647         } else if (s.st_rdev != dev) {
648             VLOG_WARN_RL(&error_rl,
649                          "%s is device %u:%u but should be %u:%u, fixing",
650                          fn, major(s.st_rdev), minor(s.st_rdev),
651                          major(dev), minor(dev));
652         } else {
653             goto success;
654         }
655         if (unlink(fn)) {
656             VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
657                          fn, strerror(errno));
658             return errno;
659         }
660     } else if (errno == ENOENT) {
661         if (stat(dirname, &s)) {
662             if (errno == ENOENT) {
663                 if (mkdir(dirname, 0755)) {
664                     VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
665                                  dirname, strerror(errno));
666                     return errno;
667                 }
668             } else {
669                 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
670                              dirname, strerror(errno));
671                 return errno;
672             }
673         }
674     } else {
675         VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
676         return errno;
677     }
678
679     /* The device needs to be created. */
680     if (mknod(fn, S_IFCHR | 0700, dev)) {
681         VLOG_WARN_RL(&error_rl,
682                      "%s: creating character device %u:%u failed (%s)",
683                      fn, major(dev), minor(dev), strerror(errno));
684         return errno;
685     }
686
687 success:
688     *fnp = xstrdup(fn);
689     return 0;
690 }
691
692 /* Return the major device number of the Open vSwitch device.  If it
693  * cannot be determined, a negative errno is returned. */
694 static int
695 get_openvswitch_major(void)
696 {
697     static int openvswitch_major = -1;
698     if (openvswitch_major < 0) {
699         openvswitch_major = get_major("openvswitch");
700     }
701     return openvswitch_major;
702 }
703
704 static int
705 get_major(const char *target)
706 {
707     const char fn[] = "/proc/devices";
708     char line[128];
709     FILE *file;
710     int ln;
711
712     file = fopen(fn, "r");
713     if (!file) {
714         VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
715         return -errno;
716     }
717
718     for (ln = 1; fgets(line, sizeof line, file); ln++) {
719         char name[64];
720         int major;
721
722         if (!strncmp(line, "Character", 9) || line[0] == '\0') {
723             /* Nothing to do. */
724         } else if (!strncmp(line, "Block", 5)) {
725             /* We only want character devices, so skip the rest of the file. */
726             break;
727         } else if (sscanf(line, "%d %63s", &major, name)) {
728             if (!strcmp(name, target)) {
729                 fclose(file);
730                 return major;
731             }
732         } else {
733             VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
734         }
735     }
736
737     fclose(file);
738
739     VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
740     return -ENODEV;
741 }
742
743 static int
744 finish_open(struct xfif *xfif_, const char *local_ifname)
745 {
746     struct xfif_linux *xfif = xfif_linux_cast(xfif_);
747     xfif->local_ifname = xstrdup(local_ifname);
748     xfif->local_ifindex = if_nametoindex(local_ifname);
749     if (!xfif->local_ifindex) {
750         int error = errno;
751         xfif_uninit(xfif_, true);
752         VLOG_WARN("could not get ifindex of %s device: %s",
753                   local_ifname, strerror(errno));
754         return error;
755     }
756     return 0;
757 }
758
759 static int
760 create_minor(const char *name, int minor, struct xfif **xfifp)
761 {
762     int error = open_minor(minor, xfifp);
763     if (!error) {
764         error = do_ioctl(*xfifp, XFLOW_DP_CREATE, name);
765         if (!error) {
766             error = finish_open(*xfifp, name);
767         } else {
768             xfif_uninit(*xfifp, true);
769         }
770     }
771     return error;
772 }
773
774 static int
775 open_minor(int minor, struct xfif **xfifp)
776 {
777     int error;
778     char *fn;
779     int fd;
780
781     error = make_openvswitch_device(minor, &fn);
782     if (error) {
783         return error;
784     }
785
786     fd = open(fn, O_RDONLY | O_NONBLOCK);
787     if (fd >= 0) {
788         struct xfif_linux *xfif = xmalloc(sizeof *xfif);
789         error = rtnetlink_notifier_register(&xfif->port_notifier,
790                                            xfif_linux_port_changed, xfif);
791         if (!error) {
792             char *name;
793
794             name = xasprintf("dp%d", minor);
795             xfif_init(&xfif->xfif, &xfif_linux_class, name, minor, minor);
796             free(name);
797
798             xfif->fd = fd;
799             xfif->local_ifname = NULL;
800             xfif->minor = minor;
801             xfif->local_ifindex = 0;
802             shash_init(&xfif->changed_ports);
803             xfif->change_error = false;
804             *xfifp = &xfif->xfif;
805         } else {
806             free(xfif);
807         }
808     } else {
809         error = errno;
810         VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
811     }
812     free(fn);
813
814     return error;
815 }
816
817 static void
818 xfif_linux_port_changed(const struct rtnetlink_change *change, void *xfif_)
819 {
820     struct xfif_linux *xfif = xfif_;
821
822     if (change) {
823         if (change->master_ifindex == xfif->local_ifindex
824             && (change->nlmsg_type == RTM_NEWLINK
825                 || change->nlmsg_type == RTM_DELLINK))
826         {
827             /* Our datapath changed, either adding a new port or deleting an
828              * existing one. */
829             shash_add_once(&xfif->changed_ports, change->ifname, NULL);
830         }
831     } else {
832         xfif->change_error = true;
833     }
834 }