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