Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[cascardo/ovs.git] / lib / dpif.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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/rtnetlink.h>
27 #include <linux/ethtool.h>
28 #include <linux/sockios.h>
29 #include <netinet/in.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/ioctl.h>
33 #include <sys/stat.h>
34 #include <sys/sysmacros.h>
35 #include <unistd.h>
36
37 #include "coverage.h"
38 #include "dynamic-string.h"
39 #include "flow.h"
40 #include "netlink.h"
41 #include "odp-util.h"
42 #include "ofp-print.h"
43 #include "ofpbuf.h"
44 #include "packets.h"
45 #include "poll-loop.h"
46 #include "util.h"
47 #include "valgrind.h"
48
49 #include "vlog.h"
50 #define THIS_MODULE VLM_dpif
51
52 /* Rate limit for individual messages going to or from the datapath, output at
53  * DBG level.  This is very high because, if these are enabled, it is because
54  * we really need to see them. */
55 static struct vlog_rate_limit dpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
56
57 /* Not really much point in logging many dpif errors. */
58 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
59
60 static int get_minor_from_name(const char *name, unsigned int *minor);
61 static int name_to_minor(const char *name, unsigned int *minor);
62 static int lookup_minor(const char *name, unsigned int *minor);
63 static int open_by_minor(unsigned int minor, struct dpif *);
64 static int make_openvswitch_device(unsigned int minor, char **fnp);
65 static void check_rw_odp_flow(struct odp_flow *);
66
67 int
68 dpif_open(const char *name, struct dpif *dpif)
69 {
70     int listen_mask;
71     int error;
72
73     dpif->fd = -1;
74
75     error = name_to_minor(name, &dpif->minor);
76     if (error) {
77         return error;
78     }
79
80     error = open_by_minor(dpif->minor, dpif);
81     if (error) {
82         return error;
83     }
84
85     /* We can open the device, but that doesn't mean that it's been created.
86      * If it hasn't been, then any command other than ODP_DP_CREATE will
87      * return ENODEV.  Try something innocuous. */
88     listen_mask = 0;            /* Make Valgrind happy. */
89     if (ioctl(dpif->fd, ODP_GET_LISTEN_MASK, &listen_mask)) {
90         error = errno;
91         if (error != ENODEV) {
92             VLOG_WARN("dp%u: probe returned unexpected error: %s",
93                       dpif->minor, strerror(error));
94         }
95         dpif_close(dpif);
96         return error;
97     }
98     return 0;
99 }
100
101 void
102 dpif_close(struct dpif *dpif)
103 {
104     if (dpif) {
105         close(dpif->fd);
106         dpif->fd = -1;
107     }
108 }
109
110 static int
111 do_ioctl(const struct dpif *dpif, int cmd, const char *cmd_name,
112          const void *arg)
113 {
114     int error = ioctl(dpif->fd, cmd, arg) ? errno : 0;
115     if (cmd_name) {
116         if (error) {
117             VLOG_WARN_RL(&error_rl, "dp%u: ioctl(%s) failed (%s)",
118                          dpif->minor, cmd_name, strerror(error));
119         } else {
120             VLOG_DBG_RL(&dpmsg_rl, "dp%u: ioctl(%s): success",
121                         dpif->minor, cmd_name);
122         }
123     }
124     return error;
125 }
126
127 int
128 dpif_create(const char *name, struct dpif *dpif)
129 {
130     unsigned int minor;
131     int error;
132
133     if (!get_minor_from_name(name, &minor)) {
134         /* Minor was specified in 'name', go ahead and create it. */
135         error = open_by_minor(minor, dpif);
136         if (error) {
137             return error;
138         }
139
140         if (!strncmp(name, "nl:", 3)) {
141             char devname[128];
142             sprintf(devname, "of%u", minor);
143             error = ioctl(dpif->fd, ODP_DP_CREATE, devname) < 0 ? errno : 0;
144         } else {
145             error = ioctl(dpif->fd, ODP_DP_CREATE, name) < 0 ? errno : 0;
146         }
147         if (error) {
148             dpif_close(dpif);
149         }
150         return error;
151     } else {
152         for (minor = 0; minor < ODP_MAX; minor++) {
153             error = open_by_minor(minor, dpif);
154             if (error) {
155                 return error;
156             }
157
158             error = ioctl(dpif->fd, ODP_DP_CREATE, name) < 0 ? errno : 0;
159             if (!error) {
160                 return 0;
161             }
162             dpif_close(dpif);
163             if (error != EBUSY) {
164                 return error;
165             }
166         }
167         return ENOBUFS;
168     }
169 }
170
171 int
172 dpif_get_name(struct dpif *dpif, char *name, size_t name_size)
173 {
174     struct odp_port port;
175     int error;
176
177     assert(name_size > 0);
178     *name = '\0';
179
180     error = dpif_port_query_by_number(dpif, ODPP_LOCAL, &port);
181     if (!error) {
182         ovs_strlcpy(name, port.devname, name_size);
183     }
184     return error;
185 }
186
187 int
188 dpif_delete(struct dpif *dpif)
189 {
190     COVERAGE_INC(dpif_destroy);
191     return do_ioctl(dpif, ODP_DP_DESTROY, "ODP_DP_DESTROY", NULL);
192 }
193
194 int
195 dpif_get_dp_stats(const struct dpif *dpif, struct odp_stats *stats)
196 {
197     memset(stats, 0, sizeof *stats);
198     return do_ioctl(dpif, ODP_DP_STATS, "ODP_DP_STATS", stats);
199 }
200
201 int
202 dpif_get_drop_frags(const struct dpif *dpif, bool *drop_frags)
203 {
204     int tmp;
205     int error = do_ioctl(dpif, ODP_GET_DROP_FRAGS, "ODP_GET_DROP_FRAGS", &tmp);
206     *drop_frags = error ? tmp & 1 : false;
207     return error;
208 }
209
210 int
211 dpif_set_drop_frags(struct dpif *dpif, bool drop_frags)
212 {
213     int tmp = drop_frags;
214     return do_ioctl(dpif, ODP_SET_DROP_FRAGS, "ODP_SET_DROP_FRAGS", &tmp);
215 }
216
217 int
218 dpif_get_listen_mask(const struct dpif *dpif, int *listen_mask)
219 {
220     int error = do_ioctl(dpif, ODP_GET_LISTEN_MASK, "ODP_GET_LISTEN_MASK",
221                          listen_mask);
222     if (error) {
223         *listen_mask = 0;
224     }
225     return error;
226 }
227
228 int
229 dpif_set_listen_mask(struct dpif *dpif, int listen_mask)
230 {
231     return do_ioctl(dpif, ODP_SET_LISTEN_MASK, "ODP_SET_LISTEN_MASK",
232                     &listen_mask);
233 }
234
235 int
236 dpif_purge(struct dpif *dpif)
237 {
238     struct odp_stats stats;
239     unsigned int i;
240     int error;
241
242     COVERAGE_INC(dpif_purge);
243
244     error = dpif_get_dp_stats(dpif, &stats);
245     if (error) {
246         return error;
247     }
248
249     for (i = 0; i < stats.max_miss_queue + stats.max_action_queue; i++) {
250         struct ofpbuf *buf;
251         error = dpif_recv(dpif, &buf);
252         if (error) {
253             return error == EAGAIN ? 0 : error;
254         }
255         ofpbuf_delete(buf);
256     }
257     return 0;
258 }
259
260 int
261 dpif_port_add(struct dpif *dpif, const char *devname, uint16_t port_no,
262               uint16_t flags)
263 {
264     struct odp_port port;
265
266     COVERAGE_INC(dpif_port_add);
267     memset(&port, 0, sizeof port);
268     strncpy(port.devname, devname, sizeof port.devname);
269     port.port = port_no;
270     port.flags = flags;
271     if (!ioctl(dpif->fd, ODP_PORT_ADD, &port)) {
272         VLOG_DBG_RL(&dpmsg_rl, "dp%u: added %s as port %"PRIu16,
273                     dpif->minor, devname, port_no);
274         return 0;
275     } else {
276         VLOG_WARN_RL(&error_rl, "dp%u: failed to add %s as port "
277                      "%"PRIu16": %s", dpif->minor, devname, port_no,
278                      strerror(errno));
279         return errno;
280     }
281 }
282
283 int
284 dpif_port_del(struct dpif *dpif, uint16_t port_no)
285 {
286     int tmp = port_no;
287     COVERAGE_INC(dpif_port_del);
288     return do_ioctl(dpif, ODP_PORT_DEL, "ODP_PORT_DEL", &tmp);
289 }
290
291 int
292 dpif_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
293                           struct odp_port *port)
294 {
295     memset(port, 0, sizeof *port);
296     port->port = port_no;
297     if (!ioctl(dpif->fd, ODP_PORT_QUERY, port)) {
298         VLOG_DBG_RL(&dpmsg_rl, "dp%u: port %"PRIu16" is device %s",
299                     dpif->minor, port_no, port->devname);
300         return 0;
301     } else {
302         VLOG_WARN_RL(&error_rl, "dp%u: failed to query port %"PRIu16": %s",
303                      dpif->minor, port_no, strerror(errno));
304         return errno;
305     }
306 }
307
308 int
309 dpif_port_query_by_name(const struct dpif *dpif, const char *devname,
310                         struct odp_port *port)
311 {
312     memset(port, 0, sizeof *port);
313     strncpy(port->devname, devname, sizeof port->devname);
314     if (!ioctl(dpif->fd, ODP_PORT_QUERY, port)) {
315         VLOG_DBG_RL(&dpmsg_rl, "dp%u: device %s is on port %"PRIu16,
316                     dpif->minor, devname, port->port);
317         return 0;
318     } else {
319         VLOG_WARN_RL(&error_rl, "dp%u: failed to query port %s: %s",
320                      dpif->minor, devname, strerror(errno));
321         return errno;
322     }
323 }
324
325 int
326 dpif_port_list(const struct dpif *dpif,
327                struct odp_port **ports, size_t *n_ports)
328 {
329     struct odp_portvec pv;
330     struct odp_stats stats;
331     int error;
332
333     do {
334         error = dpif_get_dp_stats(dpif, &stats);
335         if (error) {
336             goto error;
337         }
338
339         *ports = xcalloc(1, stats.n_ports * sizeof **ports);
340         pv.ports = *ports;
341         pv.n_ports = stats.n_ports;
342         error = do_ioctl(dpif, ODP_PORT_LIST, "ODP_PORT_LIST", &pv);
343         if (error) {
344             free(*ports);
345             goto error;
346         }
347     } while (pv.n_ports != stats.n_ports);
348     *n_ports = pv.n_ports;
349     return 0;
350
351 error:
352     *ports = NULL;
353     *n_ports = 0;
354     return error;
355 }
356
357 int
358 dpif_port_group_set(struct dpif *dpif, uint16_t group,
359                     const uint16_t ports[], size_t n_ports)
360 {
361     struct odp_port_group pg;
362
363     COVERAGE_INC(dpif_port_group_set);
364     assert(n_ports <= UINT16_MAX);
365     pg.group = group;
366     pg.ports = (uint16_t *) ports;
367     pg.n_ports = n_ports;
368     return do_ioctl(dpif, ODP_PORT_GROUP_SET, "ODP_PORT_GROUP_SET", &pg);
369 }
370
371 /* Careful: '*n_out' can be greater than 'n_ports' on return, if 'n_ports' is
372  * less than the number of ports in 'group'. */
373 int
374 dpif_port_group_get(const struct dpif *dpif, uint16_t group,
375                     uint16_t ports[], size_t n_ports, size_t *n_out)
376 {
377     struct odp_port_group pg;
378     int error;
379
380     assert(n_ports <= UINT16_MAX);
381     pg.group = group;
382     pg.ports = ports;
383     pg.n_ports = n_ports;
384     error = do_ioctl(dpif, ODP_PORT_GROUP_GET, "ODP_PORT_GROUP_GET", &pg);
385     *n_out = error ? 0 : pg.n_ports;
386     return error;
387 }
388
389 int
390 dpif_flow_flush(struct dpif *dpif)
391 {
392     COVERAGE_INC(dpif_flow_flush);
393     return do_ioctl(dpif, ODP_FLOW_FLUSH, "ODP_FLOW_FLUSH", NULL);
394 }
395
396 static enum vlog_level
397 flow_message_log_level(int error)
398 {
399     return error ? VLL_WARN : VLL_DBG;
400 }
401
402 static bool
403 should_log_flow_message(int error)
404 {
405     return !vlog_should_drop(THIS_MODULE, flow_message_log_level(error),
406                              error ? &error_rl : &dpmsg_rl);
407 }
408
409 static void
410 log_flow_message(const struct dpif *dpif, int error,
411                  const char *operation,
412                  const flow_t *flow, const struct odp_flow_stats *stats,
413                  const union odp_action *actions, size_t n_actions)
414 {
415     struct ds ds = DS_EMPTY_INITIALIZER;
416     ds_put_format(&ds, "dp%u: ", dpif->minor);
417     if (error) {
418         ds_put_cstr(&ds, "failed to ");
419     }
420     ds_put_format(&ds, "%s ", operation);
421     if (error) {
422         ds_put_format(&ds, "(%s) ", strerror(error));
423     }
424     flow_format(&ds, flow);
425     if (stats) {
426         ds_put_cstr(&ds, ", ");
427         format_odp_flow_stats(&ds, stats);
428     }
429     if (actions || n_actions) {
430         ds_put_cstr(&ds, ", actions:");
431         format_odp_actions(&ds, actions, n_actions);
432     }
433     vlog(THIS_MODULE, flow_message_log_level(error), "%s", ds_cstr(&ds));
434     ds_destroy(&ds);
435 }
436
437 static int
438 do_flow_ioctl(const struct dpif *dpif, int cmd, struct odp_flow *flow,
439               const char *operation, bool show_stats)
440 {
441     int error = do_ioctl(dpif, cmd, NULL, flow);
442     if (error && show_stats) {
443         flow->n_actions = 0;
444     }
445     if (should_log_flow_message(error)) {
446         log_flow_message(dpif, error, operation, &flow->key,
447                          show_stats && !error ? &flow->stats : NULL,
448                          flow->actions, flow->n_actions);
449     }
450     return error;
451 }
452
453 int
454 dpif_flow_put(struct dpif *dpif, struct odp_flow_put *put)
455 {
456     int error = do_ioctl(dpif, ODP_FLOW_PUT, NULL, put);
457     COVERAGE_INC(dpif_flow_put);
458     if (should_log_flow_message(error)) {
459         struct ds operation = DS_EMPTY_INITIALIZER;
460         ds_put_cstr(&operation, "put");
461         if (put->flags & ODPPF_CREATE) {
462             ds_put_cstr(&operation, "[create]");
463         }
464         if (put->flags & ODPPF_MODIFY) {
465             ds_put_cstr(&operation, "[modify]");
466         }
467         if (put->flags & ODPPF_ZERO_STATS) {
468             ds_put_cstr(&operation, "[zero]");
469         }
470 #define ODPPF_ALL (ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS)
471         if (put->flags & ~ODPPF_ALL) {
472             ds_put_format(&operation, "[%x]", put->flags & ~ODPPF_ALL);
473         }
474         log_flow_message(dpif, error, ds_cstr(&operation), &put->flow.key,
475                          !error ? &put->flow.stats : NULL,
476                          put->flow.actions, put->flow.n_actions);
477         ds_destroy(&operation);
478     }
479     return error;
480 }
481
482 int
483 dpif_flow_del(struct dpif *dpif, struct odp_flow *flow)
484 {
485     COVERAGE_INC(dpif_flow_del);
486     check_rw_odp_flow(flow);
487     memset(&flow->stats, 0, sizeof flow->stats);
488     return do_flow_ioctl(dpif, ODP_FLOW_DEL, flow, "delete flow", true);
489 }
490
491 int
492 dpif_flow_get(const struct dpif *dpif, struct odp_flow *flow)
493 {
494     COVERAGE_INC(dpif_flow_query);
495     check_rw_odp_flow(flow);
496     memset(&flow->stats, 0, sizeof flow->stats);
497     return do_flow_ioctl(dpif, ODP_FLOW_GET, flow, "get flow", true);
498 }
499
500 int
501 dpif_flow_get_multiple(const struct dpif *dpif,
502                        struct odp_flow flows[], size_t n)
503 {
504     struct odp_flowvec fv;
505     size_t i;
506
507     COVERAGE_ADD(dpif_flow_query_multiple, n);
508     fv.flows = flows;
509     fv.n_flows = n;
510     for (i = 0; i < n; i++) {
511         check_rw_odp_flow(&flows[i]);
512     }
513     return do_ioctl(dpif, ODP_FLOW_GET_MULTIPLE, "ODP_FLOW_GET_MULTIPLE",
514                     &fv);
515 }
516
517 int
518 dpif_flow_list(const struct dpif *dpif, struct odp_flow flows[], size_t n,
519                size_t *n_out)
520 {
521     struct odp_flowvec fv;
522     uint32_t i;
523     int error;
524
525     COVERAGE_INC(dpif_flow_query_list);
526     fv.flows = flows;
527     fv.n_flows = n;
528     if (RUNNING_ON_VALGRIND) {
529         memset(flows, 0, n * sizeof *flows);
530     } else {
531         for (i = 0; i < n; i++) {
532             flows[i].actions = NULL;
533             flows[i].n_actions = 0;
534         }
535     }
536     error = do_ioctl(dpif, ODP_FLOW_LIST, NULL, &fv);
537     if (error) {
538         *n_out = 0;
539         VLOG_WARN_RL(&error_rl, "dp%u: flow list failed (%s)",
540                      dpif->minor, strerror(error));
541     } else {
542         COVERAGE_ADD(dpif_flow_query_list_n, fv.n_flows);
543         *n_out = fv.n_flows;
544         VLOG_DBG_RL(&dpmsg_rl, "dp%u: listed %zu flows", dpif->minor, *n_out);
545     }
546     return error;
547 }
548
549 int
550 dpif_flow_list_all(const struct dpif *dpif,
551                    struct odp_flow **flowsp, size_t *np)
552 {
553     struct odp_stats stats;
554     struct odp_flow *flows;
555     size_t n_flows;
556     int error;
557
558     *flowsp = NULL;
559     *np = 0;
560
561     error = dpif_get_dp_stats(dpif, &stats);
562     if (error) {
563         return error;
564     }
565
566     flows = xmalloc(sizeof *flows * stats.n_flows);
567     error = dpif_flow_list(dpif, flows, stats.n_flows, &n_flows);
568     if (error) {
569         free(flows);
570         return error;
571     }
572
573     if (stats.n_flows != n_flows) {
574         VLOG_WARN_RL(&error_rl, "dp%u: datapath stats reported %"PRIu32" "
575                      "flows but flow listing reported %zu",
576                      dpif->minor, stats.n_flows, n_flows);
577     }
578     *flowsp = flows;
579     *np = n_flows;
580     return 0;
581 }
582
583 int
584 dpif_execute(struct dpif *dpif, uint16_t in_port,
585              const union odp_action actions[], size_t n_actions,
586              const struct ofpbuf *buf)
587 {
588     int error;
589
590     COVERAGE_INC(dpif_execute);
591     if (n_actions > 0) {
592         struct odp_execute execute;
593         memset(&execute, 0, sizeof execute);
594         execute.in_port = in_port;
595         execute.actions = (union odp_action *) actions;
596         execute.n_actions = n_actions;
597         execute.data = buf->data;
598         execute.length = buf->size;
599         error = do_ioctl(dpif, ODP_EXECUTE, NULL, &execute);
600     } else {
601         error = 0;
602     }
603
604     if (!(error ? VLOG_DROP_WARN(&error_rl) : VLOG_DROP_DBG(&dpmsg_rl))) {
605         struct ds ds = DS_EMPTY_INITIALIZER;
606         char *packet = ofp_packet_to_string(buf->data, buf->size, buf->size);
607         ds_put_format(&ds, "dp%u: execute ", dpif->minor);
608         format_odp_actions(&ds, actions, n_actions);
609         if (error) {
610             ds_put_format(&ds, " failed (%s)", strerror(error));
611         }
612         ds_put_format(&ds, " on packet %s", packet);
613         vlog(THIS_MODULE, error ? VLL_WARN : VLL_DBG, "%s", ds_cstr(&ds));
614         ds_destroy(&ds);
615         free(packet);
616     }
617     return error;
618 }
619
620 int
621 dpif_recv(struct dpif *dpif, struct ofpbuf **bufp)
622 {
623     struct ofpbuf *buf;
624     int retval;
625     int error;
626
627     buf = ofpbuf_new(65536);
628     retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
629     if (retval < 0) {
630         error = errno;
631         if (error != EAGAIN) {
632             VLOG_WARN_RL(&error_rl, "dp%u: read failed: %s",
633                          dpif->minor, strerror(error));
634         }
635     } else if (retval >= sizeof(struct odp_msg)) {
636         struct odp_msg *msg = buf->data;
637         if (msg->length <= retval) {
638             buf->size += retval;
639             if (VLOG_IS_DBG_ENABLED()) {
640                 void *payload = msg + 1;
641                 size_t length = buf->size - sizeof *msg;
642                 char *s = ofp_packet_to_string(payload, length, length);
643                 VLOG_DBG_RL(&dpmsg_rl, "dp%u: received %s message of length "
644                             "%zu on port %"PRIu16": %s", dpif->minor,
645                             (msg->type == _ODPL_MISS_NR ? "miss"
646                              : msg->type == _ODPL_ACTION_NR ? "action"
647                              : "<unknown>"),
648                             msg->length - sizeof(struct odp_msg),
649                             msg->port, s);
650                 free(s);
651             }
652             *bufp = buf;
653             COVERAGE_INC(dpif_recv);
654             return 0;
655         } else {
656             VLOG_WARN_RL(&error_rl, "dp%u: discarding message truncated "
657                          "from %zu bytes to %d",
658                          dpif->minor, msg->length, retval);
659             error = ERANGE;
660         }
661     } else if (!retval) {
662         VLOG_WARN_RL(&error_rl, "dp%u: unexpected end of file", dpif->minor);
663         error = EPROTO;
664     } else {
665         VLOG_WARN_RL(&error_rl,
666                      "dp%u: discarding too-short message (%d bytes)",
667                      dpif->minor, retval);
668         error = ERANGE;
669     }
670
671     *bufp = NULL;
672     ofpbuf_delete(buf);
673     return error;
674 }
675
676 void
677 dpif_recv_wait(struct dpif *dpif)
678 {
679     poll_fd_wait(dpif->fd, POLLIN);
680 }
681 \f
682 struct dpifmon {
683     struct dpif dpif;
684     struct nl_sock *sock;
685     int local_ifindex;
686 };
687
688 int
689 dpifmon_create(const char *datapath_name, struct dpifmon **monp)
690 {
691     struct dpifmon *mon;
692     char local_name[IFNAMSIZ];
693     int error;
694
695     mon = *monp = xmalloc(sizeof *mon);
696
697     error = dpif_open(datapath_name, &mon->dpif);
698     if (error) {
699         goto error;
700     }
701     error = dpif_get_name(&mon->dpif, local_name, sizeof local_name);
702     if (error) {
703         goto error_close_dpif;
704     }
705
706     mon->local_ifindex = if_nametoindex(local_name);
707     if (!mon->local_ifindex) {
708         error = errno;
709         VLOG_WARN("could not get ifindex of %s device: %s",
710                   local_name, strerror(errno));
711         goto error_close_dpif;
712     }
713
714     error = nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0, &mon->sock);
715     if (error) {
716         VLOG_WARN("could not create rtnetlink socket: %s", strerror(error));
717         goto error_close_dpif;
718     }
719
720     return 0;
721
722 error_close_dpif:
723     dpif_close(&mon->dpif);
724 error:
725     free(mon);
726     *monp = NULL;
727     return error;
728 }
729
730 void
731 dpifmon_destroy(struct dpifmon *mon)
732 {
733     if (mon) {
734         dpif_close(&mon->dpif);
735         nl_sock_destroy(mon->sock);
736     }
737 }
738
739 int
740 dpifmon_poll(struct dpifmon *mon, char **devnamep)
741 {
742     static struct vlog_rate_limit slow_rl = VLOG_RATE_LIMIT_INIT(1, 5);
743     static const struct nl_policy rtnlgrp_link_policy[] = {
744         [IFLA_IFNAME] = { .type = NL_A_STRING },
745         [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
746     };
747     struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
748     struct ofpbuf *buf;
749     int error;
750
751     *devnamep = NULL;
752 again:
753     error = nl_sock_recv(mon->sock, &buf, false);
754     switch (error) {
755     case 0:
756         if (!nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
757                              rtnlgrp_link_policy,
758                              attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
759             VLOG_WARN_RL(&slow_rl, "received bad rtnl message");
760             error = ENOBUFS;
761         } else {
762             const char *devname = nl_attr_get_string(attrs[IFLA_IFNAME]);
763             bool for_us;
764
765             if (attrs[IFLA_MASTER]) {
766                 uint32_t master_ifindex = nl_attr_get_u32(attrs[IFLA_MASTER]);
767                 for_us = master_ifindex == mon->local_ifindex;
768             } else {
769                 /* It's for us if that device is one of our ports.  This is
770                  * open-coded instead of using dpif_port_query_by_name() to
771                  * avoid logging a warning on failure. */
772                 struct odp_port port;
773                 memset(&port, 0, sizeof port);
774                 strncpy(port.devname, devname, sizeof port.devname);
775                 for_us = !ioctl(mon->dpif.fd, ODP_PORT_QUERY, &port);
776             }
777
778             if (!for_us) {
779                 /* Not for us, try again. */
780                 ofpbuf_delete(buf);
781                 COVERAGE_INC(dpifmon_poll_false_wakeup);
782                 goto again;
783             }
784             COVERAGE_INC(dpifmon_poll_changed);
785             *devnamep = xstrdup(devname);
786         }
787         ofpbuf_delete(buf);
788         break;
789
790     case EAGAIN:
791         /* Nothing to do. */
792         break;
793
794     case ENOBUFS:
795         VLOG_WARN_RL(&slow_rl, "dpifmon socket overflowed");
796         break;
797
798     default:
799         VLOG_WARN_RL(&slow_rl, "error on dpifmon socket: %s", strerror(error));
800         break;
801     }
802     return error;
803 }
804
805 void
806 dpifmon_run(struct dpifmon *mon UNUSED)
807 {
808     /* Nothing to do in this implementation. */
809 }
810
811 void
812 dpifmon_wait(struct dpifmon *mon)
813 {
814     nl_sock_wait(mon->sock, POLLIN);
815 }
816 \f
817 static int get_openvswitch_major(void);
818 static int get_major(const char *target, int default_major);
819
820 static int
821 lookup_minor(const char *name, unsigned int *minor)
822 {
823     struct ethtool_drvinfo drvinfo;
824     struct ifreq ifr;
825     int error;
826     int sock;
827
828     *minor = -1;
829     sock = socket(AF_INET, SOCK_DGRAM, 0);
830     if (sock < 0) {
831         VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
832         error = errno;
833         goto error;
834     }
835
836     memset(&ifr, 0, sizeof ifr);
837     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
838     ifr.ifr_data = (caddr_t) &drvinfo;
839
840     memset(&drvinfo, 0, sizeof drvinfo);
841     drvinfo.cmd = ETHTOOL_GDRVINFO;
842     if (ioctl(sock, SIOCETHTOOL, &ifr)) {
843         VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
844         error = errno;
845         goto error_close_sock;
846     }
847
848     if (strcmp(drvinfo.driver, "openvswitch")) {
849         VLOG_WARN("%s is not an openvswitch device", name);
850         error = EOPNOTSUPP;
851         goto error_close_sock;
852     }
853
854     if (!isdigit(drvinfo.bus_info[0])) {
855         VLOG_WARN("%s ethtool info does not contain an openvswitch minor",
856                   name);
857         error = EPROTOTYPE;
858         goto error_close_sock;
859     }
860
861     *minor = atoi(drvinfo.bus_info);
862     close(sock);
863     return 0;
864
865 error_close_sock:
866     close(sock);
867 error:
868     return error;
869 }
870
871 static int
872 make_openvswitch_device(unsigned int minor, char **fnp)
873 {
874     dev_t dev = makedev(get_openvswitch_major(), minor);
875     const char dirname[] = "/dev/net";
876     struct stat s;
877     char fn[128];
878
879     *fnp = NULL;
880     sprintf(fn, "%s/dp%d", dirname, minor);
881     if (!stat(fn, &s)) {
882         if (!S_ISCHR(s.st_mode)) {
883             VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
884                          fn);
885         } else if (s.st_rdev != dev) {
886             VLOG_WARN_RL(&error_rl,
887                          "%s is device %u:%u instead of %u:%u, fixing",
888                          fn, major(s.st_rdev), minor(s.st_rdev),
889                          major(dev), minor(dev));
890         } else {
891             goto success;
892         }
893         if (unlink(fn)) {
894             VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
895                          fn, strerror(errno));
896             return errno;
897         }
898     } else if (errno == ENOENT) {
899         if (stat(dirname, &s)) {
900             if (errno == ENOENT) {
901                 if (mkdir(dirname, 0755)) {
902                     VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
903                                  dirname, strerror(errno));
904                     return errno;
905                 }
906             } else {
907                 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
908                              dirname, strerror(errno));
909                 return errno;
910             }
911         }
912     } else {
913         VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
914         return errno;
915     }
916
917     /* The device needs to be created. */
918     if (mknod(fn, S_IFCHR | 0700, dev)) {
919         VLOG_WARN_RL(&error_rl,
920                      "%s: creating character device %u:%u failed (%s)",
921                      fn, major(dev), minor(dev), strerror(errno));
922         return errno;
923     }
924
925 success:
926     *fnp = xstrdup(fn);
927     return 0;
928 }
929
930
931 static int
932 get_openvswitch_major(void)
933 {
934     static unsigned int openvswitch_major;
935     if (!openvswitch_major) {
936         enum { DEFAULT_MAJOR = 248 };
937         openvswitch_major = get_major("openvswitch", DEFAULT_MAJOR);
938     }
939     return openvswitch_major;
940 }
941
942 static int
943 get_major(const char *target, int default_major)
944 {
945     const char fn[] = "/proc/devices";
946     char line[128];
947     FILE *file;
948     int ln;
949
950     file = fopen(fn, "r");
951     if (!file) {
952         VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
953         goto error;
954     }
955
956     for (ln = 1; fgets(line, sizeof line, file); ln++) {
957         char name[64];
958         int major;
959
960         if (!strncmp(line, "Character", 9) || line[0] == '\0') {
961             /* Nothing to do. */
962         } else if (!strncmp(line, "Block", 5)) {
963             /* We only want character devices, so skip the rest of the file. */
964             break;
965         } else if (sscanf(line, "%d %63s", &major, name)) {
966             if (!strcmp(name, target)) {
967                 fclose(file);
968                 return major;
969             }
970         } else {
971             static bool warned;
972             if (!warned) {
973                 VLOG_WARN("%s:%d: syntax error", fn, ln);
974             }
975             warned = true;
976         }
977     }
978
979     VLOG_ERR("%s: %s major not found (is the module loaded?), using "
980              "default major %d", fn, target, default_major);
981 error:
982     VLOG_INFO("using default major %d for %s", default_major, target);
983     return default_major;
984 }
985
986 static int
987 name_to_minor(const char *name, unsigned int *minor)
988 {
989     if (!get_minor_from_name(name, minor)) {
990         return 0;
991     }
992     return lookup_minor(name, minor);
993 }
994
995 static int
996 get_minor_from_name(const char *name, unsigned int *minor)
997 {
998     if (!strncmp(name, "dp", 2) && isdigit(name[2])) {
999         *minor = atoi(name + 2);
1000         return 0;
1001     } else if (!strncmp(name, "nl:", 3) && isdigit(name[3])) {
1002         /* This is for compatibility only and will be dropped. */
1003         *minor = atoi(name + 3);
1004         return 0;
1005     } else {
1006         return EINVAL;
1007     }
1008 }
1009
1010 static int
1011 open_by_minor(unsigned int minor, struct dpif *dpif)
1012 {
1013     int error;
1014     char *fn;
1015     int fd;
1016
1017     dpif->minor = -1;
1018     dpif->fd = -1;
1019     error = make_openvswitch_device(minor, &fn);
1020     if (error) {
1021         return error;
1022     }
1023
1024     fd = open(fn, O_RDONLY | O_NONBLOCK);
1025     if (fd < 0) {
1026         error = errno;
1027         VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
1028         free(fn);
1029         return error;
1030     }
1031
1032     free(fn);
1033     dpif->minor = minor;
1034     dpif->fd = fd;
1035     return 0;
1036 }
1037 \f
1038 /* There is a tendency to construct odp_flow objects on the stack and to
1039  * forget to properly initialize their "actions" and "n_actions" members.
1040  * When this happens, we get memory corruption because the kernel
1041  * writes through the random pointer that is in the "actions" member.
1042  *
1043  * This function attempts to combat the problem by:
1044  *
1045  *      - Forcing a segfault if "actions" points to an invalid region (instead
1046  *        of just getting back EFAULT, which can be easily missed in the log).
1047  *
1048  *      - Storing a distinctive value that is likely to cause an
1049  *        easy-to-identify error later if it is dereferenced, etc.
1050  *
1051  *      - Triggering a warning on uninitialized memory from Valgrind if
1052  *        "actions" or "n_actions" was not initialized.
1053  */
1054 static void
1055 check_rw_odp_flow(struct odp_flow *flow)
1056 {
1057     if (flow->n_actions) {
1058         memset(&flow->actions[0], 0xcc, sizeof flow->actions[0]);
1059     }
1060 }