datapath: Report kernel's flow key when passing packets up to userspace.
[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_list(const struct dpif *dpif_, struct odp_port *ports, int n)
328 {
329     struct odp_portvec pv;
330     unsigned int i;
331     int error;
332
333     pv.ports = ports;
334     pv.n_ports = n;
335     error = do_ioctl(dpif_, ODP_VPORT_LIST, &pv);
336     if (error) {
337         return -error;
338     }
339
340     for (i = 0; i < pv.n_ports; i++) {
341         struct odp_port *port = &pv.ports[i];
342
343         translate_vport_type_to_netdev_type(port);
344     }
345     return pv.n_ports;
346 }
347
348 static int
349 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
350 {
351     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
352
353     if (dpif->change_error) {
354         dpif->change_error = false;
355         shash_clear(&dpif->changed_ports);
356         return ENOBUFS;
357     } else if (!shash_is_empty(&dpif->changed_ports)) {
358         struct shash_node *node = shash_first(&dpif->changed_ports);
359         *devnamep = shash_steal(&dpif->changed_ports, node);
360         return 0;
361     } else {
362         return EAGAIN;
363     }
364 }
365
366 static void
367 dpif_linux_port_poll_wait(const struct dpif *dpif_)
368 {
369     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
370     if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
371         poll_immediate_wake();
372     } else {
373         rtnetlink_link_notifier_wait();
374     }
375 }
376
377 static int
378 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
379 {
380     struct odp_flowvec fv;
381     fv.flows = flows;
382     fv.n_flows = n;
383     return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
384 }
385
386 static int
387 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
388 {
389     return do_ioctl(dpif_, ODP_FLOW_PUT, put);
390 }
391
392 static int
393 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
394 {
395     return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
396 }
397
398 static int
399 dpif_linux_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
400 {
401     *statep = xzalloc(sizeof(struct odp_flow_dump));
402     return 0;
403 }
404
405 static int
406 dpif_linux_flow_dump_next(const struct dpif *dpif, void *state,
407                           struct odp_flow *flow)
408 {
409     struct odp_flow_dump *dump = state;
410     int error;
411
412     dump->flow = flow;
413     error = do_ioctl(dpif, ODP_FLOW_DUMP, dump);
414     return error ? error : flow->flags & ODPFF_EOF ? EOF : 0;
415 }
416
417 static int
418 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
419 {
420     free(state);
421     return 0;
422 }
423
424 static int
425 dpif_linux_execute(struct dpif *dpif_,
426                    const struct nlattr *actions, size_t actions_len,
427                    const struct ofpbuf *buf)
428 {
429     struct odp_execute execute;
430     memset(&execute, 0, sizeof execute);
431     execute.actions = (struct nlattr *) actions;
432     execute.actions_len = actions_len;
433     execute.data = buf->data;
434     execute.length = buf->size;
435     return do_ioctl(dpif_, ODP_EXECUTE, &execute);
436 }
437
438 static int
439 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
440 {
441     return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
442 }
443
444 static int
445 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
446 {
447     return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
448 }
449
450 static int
451 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
452                                  uint32_t *probability)
453 {
454     return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
455 }
456
457 static int
458 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
459 {
460     return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
461 }
462
463 static int
464 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
465                              uint32_t queue_id, uint32_t *priority)
466 {
467     if (queue_id < 0xf000) {
468         *priority = TC_H_MAKE(1 << 16, queue_id + 1);
469         return 0;
470     } else {
471         return EINVAL;
472     }
473 }
474
475 static int
476 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall)
477 {
478     static const struct nl_policy odp_packet_policy[] = {
479         /* Always present. */
480         [ODP_PACKET_ATTR_TYPE] = { .type = NL_A_U32 },
481         [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
482                                      .min_len = ETH_HEADER_LEN },
483         [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
484
485         /* _ODPL_ACTION_NR only. */
486         [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
487
488         /* _ODPL_SFLOW_NR only. */
489         [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
490         [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
491     };
492
493     struct odp_packet *odp_packet = buf->data;
494     struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
495
496     if (!nl_policy_parse(buf, sizeof *odp_packet, odp_packet_policy,
497                          a, ARRAY_SIZE(odp_packet_policy))) {
498         return EINVAL;
499     }
500
501     memset(upcall, 0, sizeof *upcall);
502     upcall->type = nl_attr_get_u32(a[ODP_PACKET_ATTR_TYPE]);
503     upcall->packet = buf;
504     upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
505     upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
506     upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
507     upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
508     upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
509                         ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
510                         : 0);
511     upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
512                         ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
513                            : 0);
514     if (a[ODP_PACKET_ATTR_ACTIONS]) {
515         upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
516         upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
517     }
518
519     return 0;
520 }
521
522 static int
523 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
524 {
525     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
526     struct ofpbuf *buf;
527     int retval;
528     int error;
529
530     buf = ofpbuf_new(65536);
531     retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
532     if (retval < 0) {
533         error = errno;
534         if (error != EAGAIN) {
535             VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
536                          dpif_name(dpif_), strerror(error));
537         }
538     } else if (retval >= sizeof(struct odp_packet)) {
539         struct odp_packet *odp_packet = buf->data;
540         buf->size += retval;
541
542         if (odp_packet->len <= retval) {
543             error = parse_odp_packet(buf, upcall);
544         } else {
545             VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
546                          "from %"PRIu32" bytes to %d",
547                          dpif_name(dpif_), odp_packet->len, retval);
548             error = ERANGE;
549         }
550     } else if (!retval) {
551         VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
552         error = EPROTO;
553     } else {
554         VLOG_WARN_RL(&error_rl, "%s: discarding too-short message (%d bytes)",
555                      dpif_name(dpif_), retval);
556         error = ERANGE;
557     }
558
559     if (error) {
560         ofpbuf_delete(buf);
561     }
562     return error;
563 }
564
565 static void
566 dpif_linux_recv_wait(struct dpif *dpif_)
567 {
568     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
569     poll_fd_wait(dpif->fd, POLLIN);
570 }
571
572 const struct dpif_class dpif_linux_class = {
573     "system",
574     NULL,
575     NULL,
576     dpif_linux_enumerate,
577     dpif_linux_open,
578     dpif_linux_close,
579     dpif_linux_get_all_names,
580     dpif_linux_destroy,
581     dpif_linux_get_stats,
582     dpif_linux_get_drop_frags,
583     dpif_linux_set_drop_frags,
584     dpif_linux_port_add,
585     dpif_linux_port_del,
586     dpif_linux_port_query_by_number,
587     dpif_linux_port_query_by_name,
588     dpif_linux_port_list,
589     dpif_linux_port_poll,
590     dpif_linux_port_poll_wait,
591     dpif_linux_flow_get,
592     dpif_linux_flow_put,
593     dpif_linux_flow_del,
594     dpif_linux_flow_flush,
595     dpif_linux_flow_dump_start,
596     dpif_linux_flow_dump_next,
597     dpif_linux_flow_dump_done,
598     dpif_linux_execute,
599     dpif_linux_recv_get_mask,
600     dpif_linux_recv_set_mask,
601     dpif_linux_get_sflow_probability,
602     dpif_linux_set_sflow_probability,
603     dpif_linux_queue_to_priority,
604     dpif_linux_recv,
605     dpif_linux_recv_wait,
606 };
607 \f
608 static int get_openvswitch_major(void);
609 static int get_major(const char *target);
610
611 static int
612 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
613 {
614     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
615     return ioctl(dpif->fd, cmd, arg) ? errno : 0;
616 }
617
618 static int
619 lookup_minor(const char *name, int *minorp)
620 {
621     struct ethtool_drvinfo drvinfo;
622     int minor, port_no;
623     struct ifreq ifr;
624     int error;
625     int sock;
626
627     sock = socket(AF_INET, SOCK_DGRAM, 0);
628     if (sock < 0) {
629         VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
630         error = errno;
631         goto error;
632     }
633
634     memset(&ifr, 0, sizeof ifr);
635     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
636     ifr.ifr_data = (caddr_t) &drvinfo;
637
638     memset(&drvinfo, 0, sizeof drvinfo);
639     drvinfo.cmd = ETHTOOL_GDRVINFO;
640     if (ioctl(sock, SIOCETHTOOL, &ifr)) {
641         VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
642         error = errno;
643         goto error_close_sock;
644     }
645
646     if (strcmp(drvinfo.driver, "openvswitch")) {
647         VLOG_WARN("%s is not an openvswitch device", name);
648         error = EOPNOTSUPP;
649         goto error_close_sock;
650     }
651
652     if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
653         VLOG_WARN("%s ethtool bus_info has unexpected format", name);
654         error = EPROTOTYPE;
655         goto error_close_sock;
656     } else if (port_no != ODPP_LOCAL) {
657         /* This is an Open vSwitch device but not the local port.  We
658          * intentionally support only using the name of the local port as the
659          * name of a datapath; otherwise, it would be too difficult to
660          * enumerate all the names of a datapath. */
661         error = EOPNOTSUPP;
662         goto error_close_sock;
663     }
664
665     *minorp = minor;
666     close(sock);
667     return 0;
668
669 error_close_sock:
670     close(sock);
671 error:
672     return error;
673 }
674
675 static int
676 make_openvswitch_device(int minor, char **fnp)
677 {
678     const char dirname[] = "/dev/net";
679     int major;
680     dev_t dev;
681     struct stat s;
682     char fn[128];
683
684     *fnp = NULL;
685
686     major = get_openvswitch_major();
687     if (major < 0) {
688         return -major;
689     }
690     dev = makedev(major, minor);
691
692     sprintf(fn, "%s/dp%d", dirname, minor);
693     if (!stat(fn, &s)) {
694         if (!S_ISCHR(s.st_mode)) {
695             VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
696                          fn);
697         } else if (s.st_rdev != dev) {
698             VLOG_WARN_RL(&error_rl,
699                          "%s is device %u:%u but should be %u:%u, fixing",
700                          fn, major(s.st_rdev), minor(s.st_rdev),
701                          major(dev), minor(dev));
702         } else {
703             goto success;
704         }
705         if (unlink(fn)) {
706             VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
707                          fn, strerror(errno));
708             return errno;
709         }
710     } else if (errno == ENOENT) {
711         if (stat(dirname, &s)) {
712             if (errno == ENOENT) {
713                 if (mkdir(dirname, 0755)) {
714                     VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
715                                  dirname, strerror(errno));
716                     return errno;
717                 }
718             } else {
719                 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
720                              dirname, strerror(errno));
721                 return errno;
722             }
723         }
724     } else {
725         VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
726         return errno;
727     }
728
729     /* The device needs to be created. */
730     if (mknod(fn, S_IFCHR | 0700, dev)) {
731         VLOG_WARN_RL(&error_rl,
732                      "%s: creating character device %u:%u failed (%s)",
733                      fn, major(dev), minor(dev), strerror(errno));
734         return errno;
735     }
736
737 success:
738     *fnp = xstrdup(fn);
739     return 0;
740 }
741
742 /* Return the major device number of the Open vSwitch device.  If it
743  * cannot be determined, a negative errno is returned. */
744 static int
745 get_openvswitch_major(void)
746 {
747     static int openvswitch_major = -1;
748     if (openvswitch_major < 0) {
749         openvswitch_major = get_major("openvswitch");
750     }
751     return openvswitch_major;
752 }
753
754 static int
755 get_major(const char *target)
756 {
757     const char fn[] = "/proc/devices";
758     char line[128];
759     FILE *file;
760     int ln;
761
762     file = fopen(fn, "r");
763     if (!file) {
764         VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
765         return -errno;
766     }
767
768     for (ln = 1; fgets(line, sizeof line, file); ln++) {
769         char name[64];
770         int major;
771
772         if (!strncmp(line, "Character", 9) || line[0] == '\0') {
773             /* Nothing to do. */
774         } else if (!strncmp(line, "Block", 5)) {
775             /* We only want character devices, so skip the rest of the file. */
776             break;
777         } else if (sscanf(line, "%d %63s", &major, name)) {
778             if (!strcmp(name, target)) {
779                 fclose(file);
780                 return major;
781             }
782         } else {
783             VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
784         }
785     }
786
787     fclose(file);
788
789     VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
790     return -ENODEV;
791 }
792
793 static int
794 finish_open(struct dpif *dpif_, const char *local_ifname)
795 {
796     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
797     dpif->local_ifname = xstrdup(local_ifname);
798     dpif->local_ifindex = if_nametoindex(local_ifname);
799     if (!dpif->local_ifindex) {
800         int error = errno;
801         dpif_uninit(dpif_, true);
802         VLOG_WARN("could not get ifindex of %s device: %s",
803                   local_ifname, strerror(errno));
804         return error;
805     }
806     return 0;
807 }
808
809 static int
810 create_minor(const char *name, int minor, struct dpif **dpifp)
811 {
812     int error = open_minor(minor, dpifp);
813     if (!error) {
814         error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
815         if (!error) {
816             error = finish_open(*dpifp, name);
817         } else {
818             dpif_uninit(*dpifp, true);
819         }
820     }
821     return error;
822 }
823
824 static int
825 open_minor(int minor, struct dpif **dpifp)
826 {
827     int error;
828     char *fn;
829     int fd;
830
831     error = make_openvswitch_device(minor, &fn);
832     if (error) {
833         return error;
834     }
835
836     fd = open(fn, O_RDONLY | O_NONBLOCK);
837     if (fd >= 0) {
838         struct dpif_linux *dpif = xmalloc(sizeof *dpif);
839         error = rtnetlink_link_notifier_register(&dpif->port_notifier,
840                                                  dpif_linux_port_changed,
841                                                  dpif);
842         if (!error) {
843             char *name;
844
845             name = xasprintf("dp%d", minor);
846             dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
847             free(name);
848
849             dpif->fd = fd;
850             dpif->local_ifname = NULL;
851             dpif->minor = minor;
852             dpif->local_ifindex = 0;
853             shash_init(&dpif->changed_ports);
854             dpif->change_error = false;
855             *dpifp = &dpif->dpif;
856         } else {
857             free(dpif);
858         }
859     } else {
860         error = errno;
861         VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
862     }
863     free(fn);
864
865     return error;
866 }
867
868 static void
869 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
870                         void *dpif_)
871 {
872     struct dpif_linux *dpif = dpif_;
873
874     if (change) {
875         if (change->master_ifindex == dpif->local_ifindex
876             && (change->nlmsg_type == RTM_NEWLINK
877                 || change->nlmsg_type == RTM_DELLINK))
878         {
879             /* Our datapath changed, either adding a new port or deleting an
880              * existing one. */
881             shash_add_once(&dpif->changed_ports, change->ifname, NULL);
882         }
883     } else {
884         dpif->change_error = true;
885     }
886 }