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