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