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