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