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