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