2 * Copyright (c) 2008, 2009 Nicira Networks.
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.
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.
26 #include <linux/rtnetlink.h>
27 #include <linux/ethtool.h>
28 #include <linux/sockios.h>
29 #include <netinet/in.h>
32 #include <sys/ioctl.h>
34 #include <sys/sysmacros.h>
38 #include "dynamic-string.h"
42 #include "ofp-print.h"
45 #include "poll-loop.h"
50 #define THIS_MODULE VLM_dpif
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);
57 /* Not really much point in logging many dpif errors. */
58 static struct vlog_rate_limit error_rl
= VLOG_RATE_LIMIT_INIT(9999, 5);
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
*);
68 dpif_open(const char *name
, struct dpif
*dpif
)
75 error
= name_to_minor(name
, &dpif
->minor
);
80 error
= open_by_minor(dpif
->minor
, dpif
);
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
)) {
91 if (error
!= ENODEV
) {
92 VLOG_WARN("dp%u: probe returned unexpected error: %s",
93 dpif
->minor
, strerror(error
));
102 dpif_close(struct dpif
*dpif
)
111 do_ioctl(const struct dpif
*dpif
, int cmd
, const char *cmd_name
,
114 int error
= ioctl(dpif
->fd
, cmd
, arg
) ? errno
: 0;
117 VLOG_WARN_RL(&error_rl
, "dp%u: ioctl(%s) failed (%s)",
118 dpif
->minor
, cmd_name
, strerror(error
));
120 VLOG_DBG_RL(&dpmsg_rl
, "dp%u: ioctl(%s): success",
121 dpif
->minor
, cmd_name
);
128 dpif_create(const char *name
, struct dpif
*dpif
)
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
);
140 if (!strncmp(name
, "nl:", 3)) {
142 sprintf(devname
, "of%u", minor
);
143 error
= ioctl(dpif
->fd
, ODP_DP_CREATE
, devname
) < 0 ? errno
: 0;
145 error
= ioctl(dpif
->fd
, ODP_DP_CREATE
, name
) < 0 ? errno
: 0;
152 for (minor
= 0; minor
< ODP_MAX
; minor
++) {
153 error
= open_by_minor(minor
, dpif
);
158 error
= ioctl(dpif
->fd
, ODP_DP_CREATE
, name
) < 0 ? errno
: 0;
163 if (error
!= EBUSY
) {
172 dpif_get_name(struct dpif
*dpif
, char *name
, size_t name_size
)
174 struct odp_port port
;
177 assert(name_size
> 0);
180 error
= dpif_port_query_by_number(dpif
, ODPP_LOCAL
, &port
);
182 ovs_strlcpy(name
, port
.devname
, name_size
);
188 dpif_delete(struct dpif
*dpif
)
190 COVERAGE_INC(dpif_destroy
);
191 return do_ioctl(dpif
, ODP_DP_DESTROY
, "ODP_DP_DESTROY", NULL
);
195 dpif_get_dp_stats(const struct dpif
*dpif
, struct odp_stats
*stats
)
197 memset(stats
, 0, sizeof *stats
);
198 return do_ioctl(dpif
, ODP_DP_STATS
, "ODP_DP_STATS", stats
);
202 dpif_get_drop_frags(const struct dpif
*dpif
, bool *drop_frags
)
205 int error
= do_ioctl(dpif
, ODP_GET_DROP_FRAGS
, "ODP_GET_DROP_FRAGS", &tmp
);
206 *drop_frags
= error
? tmp
& 1 : false;
211 dpif_set_drop_frags(struct dpif
*dpif
, bool drop_frags
)
213 int tmp
= drop_frags
;
214 return do_ioctl(dpif
, ODP_SET_DROP_FRAGS
, "ODP_SET_DROP_FRAGS", &tmp
);
218 dpif_get_listen_mask(const struct dpif
*dpif
, int *listen_mask
)
220 int error
= do_ioctl(dpif
, ODP_GET_LISTEN_MASK
, "ODP_GET_LISTEN_MASK",
229 dpif_set_listen_mask(struct dpif
*dpif
, int listen_mask
)
231 return do_ioctl(dpif
, ODP_SET_LISTEN_MASK
, "ODP_SET_LISTEN_MASK",
236 dpif_purge(struct dpif
*dpif
)
238 struct odp_stats stats
;
242 COVERAGE_INC(dpif_purge
);
244 error
= dpif_get_dp_stats(dpif
, &stats
);
249 for (i
= 0; i
< stats
.max_miss_queue
+ stats
.max_action_queue
; i
++) {
251 error
= dpif_recv(dpif
, &buf
);
253 return error
== EAGAIN
? 0 : error
;
261 dpif_port_add(struct dpif
*dpif
, const char *devname
, uint16_t port_no
,
264 struct odp_port port
;
266 COVERAGE_INC(dpif_port_add
);
267 memset(&port
, 0, sizeof port
);
268 strncpy(port
.devname
, devname
, sizeof port
.devname
);
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
);
276 VLOG_WARN_RL(&error_rl
, "dp%u: failed to add %s as port "
277 "%"PRIu16
": %s", dpif
->minor
, devname
, port_no
,
284 dpif_port_del(struct dpif
*dpif
, uint16_t port_no
)
287 COVERAGE_INC(dpif_port_del
);
288 return do_ioctl(dpif
, ODP_PORT_DEL
, "ODP_PORT_DEL", &tmp
);
292 dpif_port_query_by_number(const struct dpif
*dpif
, uint16_t port_no
,
293 struct odp_port
*port
)
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
);
302 VLOG_WARN_RL(&error_rl
, "dp%u: failed to query port %"PRIu16
": %s",
303 dpif
->minor
, port_no
, strerror(errno
));
309 dpif_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
310 struct odp_port
*port
)
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
);
319 VLOG_WARN_RL(&error_rl
, "dp%u: failed to query port %s: %s",
320 dpif
->minor
, devname
, strerror(errno
));
326 dpif_port_list(const struct dpif
*dpif
,
327 struct odp_port
**ports
, size_t *n_ports
)
329 struct odp_portvec pv
;
330 struct odp_stats stats
;
334 error
= dpif_get_dp_stats(dpif
, &stats
);
339 *ports
= xcalloc(1, stats
.n_ports
* sizeof **ports
);
341 pv
.n_ports
= stats
.n_ports
;
342 error
= do_ioctl(dpif
, ODP_PORT_LIST
, "ODP_PORT_LIST", &pv
);
347 } while (pv
.n_ports
!= stats
.n_ports
);
348 *n_ports
= pv
.n_ports
;
358 dpif_port_group_set(struct dpif
*dpif
, uint16_t group
,
359 const uint16_t ports
[], size_t n_ports
)
361 struct odp_port_group pg
;
363 COVERAGE_INC(dpif_port_group_set
);
364 assert(n_ports
<= UINT16_MAX
);
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
);
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'. */
374 dpif_port_group_get(const struct dpif
*dpif
, uint16_t group
,
375 uint16_t ports
[], size_t n_ports
, size_t *n_out
)
377 struct odp_port_group pg
;
380 assert(n_ports
<= UINT16_MAX
);
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
;
390 dpif_flow_flush(struct dpif
*dpif
)
392 COVERAGE_INC(dpif_flow_flush
);
393 return do_ioctl(dpif
, ODP_FLOW_FLUSH
, "ODP_FLOW_FLUSH", NULL
);
396 static enum vlog_level
397 flow_message_log_level(int error
)
399 return error
? VLL_WARN
: VLL_DBG
;
403 should_log_flow_message(int error
)
405 return !vlog_should_drop(THIS_MODULE
, flow_message_log_level(error
),
406 error
? &error_rl
: &dpmsg_rl
);
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
)
415 struct ds ds
= DS_EMPTY_INITIALIZER
;
416 ds_put_format(&ds
, "dp%u: ", dpif
->minor
);
418 ds_put_cstr(&ds
, "failed to ");
420 ds_put_format(&ds
, "%s ", operation
);
422 ds_put_format(&ds
, "(%s) ", strerror(error
));
424 flow_format(&ds
, flow
);
426 ds_put_cstr(&ds
, ", ");
427 format_odp_flow_stats(&ds
, stats
);
429 if (actions
|| n_actions
) {
430 ds_put_cstr(&ds
, ", actions:");
431 format_odp_actions(&ds
, actions
, n_actions
);
433 vlog(THIS_MODULE
, flow_message_log_level(error
), "%s", ds_cstr(&ds
));
438 do_flow_ioctl(const struct dpif
*dpif
, int cmd
, struct odp_flow
*flow
,
439 const char *operation
, bool show_stats
)
441 int error
= do_ioctl(dpif
, cmd
, NULL
, flow
);
442 if (error
&& show_stats
) {
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
);
454 dpif_flow_put(struct dpif
*dpif
, struct odp_flow_put
*put
)
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]");
464 if (put
->flags
& ODPPF_MODIFY
) {
465 ds_put_cstr(&operation
, "[modify]");
467 if (put
->flags
& ODPPF_ZERO_STATS
) {
468 ds_put_cstr(&operation
, "[zero]");
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
);
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
);
483 dpif_flow_del(struct dpif
*dpif
, struct odp_flow
*flow
)
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);
492 dpif_flow_get(const struct dpif
*dpif
, struct odp_flow
*flow
)
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);
501 dpif_flow_get_multiple(const struct dpif
*dpif
,
502 struct odp_flow flows
[], size_t n
)
504 struct odp_flowvec fv
;
507 COVERAGE_ADD(dpif_flow_query_multiple
, n
);
510 for (i
= 0; i
< n
; i
++) {
511 check_rw_odp_flow(&flows
[i
]);
513 return do_ioctl(dpif
, ODP_FLOW_GET_MULTIPLE
, "ODP_FLOW_GET_MULTIPLE",
518 dpif_flow_list(const struct dpif
*dpif
, struct odp_flow flows
[], size_t n
,
521 struct odp_flowvec fv
;
525 COVERAGE_INC(dpif_flow_query_list
);
528 if (RUNNING_ON_VALGRIND
) {
529 memset(flows
, 0, n
* sizeof *flows
);
531 for (i
= 0; i
< n
; i
++) {
532 flows
[i
].actions
= NULL
;
533 flows
[i
].n_actions
= 0;
536 error
= do_ioctl(dpif
, ODP_FLOW_LIST
, NULL
, &fv
);
539 VLOG_WARN_RL(&error_rl
, "dp%u: flow list failed (%s)",
540 dpif
->minor
, strerror(error
));
542 COVERAGE_ADD(dpif_flow_query_list_n
, fv
.n_flows
);
544 VLOG_DBG_RL(&dpmsg_rl
, "dp%u: listed %zu flows", dpif
->minor
, *n_out
);
550 dpif_flow_list_all(const struct dpif
*dpif
,
551 struct odp_flow
**flowsp
, size_t *np
)
553 struct odp_stats stats
;
554 struct odp_flow
*flows
;
561 error
= dpif_get_dp_stats(dpif
, &stats
);
566 flows
= xmalloc(sizeof *flows
* stats
.n_flows
);
567 error
= dpif_flow_list(dpif
, flows
, stats
.n_flows
, &n_flows
);
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
);
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
)
590 COVERAGE_INC(dpif_execute
);
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
);
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
);
610 ds_put_format(&ds
, " failed (%s)", strerror(error
));
612 ds_put_format(&ds
, " on packet %s", packet
);
613 vlog(THIS_MODULE
, error
? VLL_WARN
: VLL_DBG
, "%s", ds_cstr(&ds
));
621 dpif_recv(struct dpif
*dpif
, struct ofpbuf
**bufp
)
627 buf
= ofpbuf_new(65536);
628 retval
= read(dpif
->fd
, ofpbuf_tail(buf
), ofpbuf_tailroom(buf
));
631 if (error
!= EAGAIN
) {
632 VLOG_WARN_RL(&error_rl
, "dp%u: read failed: %s",
633 dpif
->minor
, strerror(error
));
635 } else if (retval
>= sizeof(struct odp_msg
)) {
636 struct odp_msg
*msg
= buf
->data
;
637 if (msg
->length
<= 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"
648 msg
->length
- sizeof(struct odp_msg
),
653 COVERAGE_INC(dpif_recv
);
656 VLOG_WARN_RL(&error_rl
, "dp%u: discarding message truncated "
657 "from %zu bytes to %d",
658 dpif
->minor
, msg
->length
, retval
);
661 } else if (!retval
) {
662 VLOG_WARN_RL(&error_rl
, "dp%u: unexpected end of file", dpif
->minor
);
665 VLOG_WARN_RL(&error_rl
,
666 "dp%u: discarding too-short message (%d bytes)",
667 dpif
->minor
, retval
);
677 dpif_recv_wait(struct dpif
*dpif
)
679 poll_fd_wait(dpif
->fd
, POLLIN
);
684 struct nl_sock
*sock
;
689 dpifmon_create(const char *datapath_name
, struct dpifmon
**monp
)
692 char local_name
[IFNAMSIZ
];
695 mon
= *monp
= xmalloc(sizeof *mon
);
697 error
= dpif_open(datapath_name
, &mon
->dpif
);
701 error
= dpif_get_name(&mon
->dpif
, local_name
, sizeof local_name
);
703 goto error_close_dpif
;
706 mon
->local_ifindex
= if_nametoindex(local_name
);
707 if (!mon
->local_ifindex
) {
709 VLOG_WARN("could not get ifindex of %s device: %s",
710 local_name
, strerror(errno
));
711 goto error_close_dpif
;
714 error
= nl_sock_create(NETLINK_ROUTE
, RTNLGRP_LINK
, 0, 0, &mon
->sock
);
716 VLOG_WARN("could not create rtnetlink socket: %s", strerror(error
));
717 goto error_close_dpif
;
723 dpif_close(&mon
->dpif
);
731 dpifmon_destroy(struct dpifmon
*mon
)
734 dpif_close(&mon
->dpif
);
735 nl_sock_destroy(mon
->sock
);
740 dpifmon_poll(struct dpifmon
*mon
, char **devnamep
)
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 },
747 struct nlattr
*attrs
[ARRAY_SIZE(rtnlgrp_link_policy
)];
753 error
= nl_sock_recv(mon
->sock
, &buf
, false);
756 if (!nl_policy_parse(buf
, NLMSG_HDRLEN
+ sizeof(struct ifinfomsg
),
758 attrs
, ARRAY_SIZE(rtnlgrp_link_policy
))) {
759 VLOG_WARN_RL(&slow_rl
, "received bad rtnl message");
762 const char *devname
= nl_attr_get_string(attrs
[IFLA_IFNAME
]);
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
;
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
);
779 /* Not for us, try again. */
781 COVERAGE_INC(dpifmon_poll_false_wakeup
);
784 COVERAGE_INC(dpifmon_poll_changed
);
785 *devnamep
= xstrdup(devname
);
795 VLOG_WARN_RL(&slow_rl
, "dpifmon socket overflowed");
799 VLOG_WARN_RL(&slow_rl
, "error on dpifmon socket: %s", strerror(error
));
806 dpifmon_run(struct dpifmon
*mon UNUSED
)
808 /* Nothing to do in this implementation. */
812 dpifmon_wait(struct dpifmon
*mon
)
814 nl_sock_wait(mon
->sock
, POLLIN
);
817 static int get_openvswitch_major(void);
818 static int get_major(const char *target
, int default_major
);
821 lookup_minor(const char *name
, unsigned int *minor
)
823 struct ethtool_drvinfo drvinfo
;
829 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
831 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno
));
836 memset(&ifr
, 0, sizeof ifr
);
837 strncpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
838 ifr
.ifr_data
= (caddr_t
) &drvinfo
;
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
));
845 goto error_close_sock
;
848 if (strcmp(drvinfo
.driver
, "openvswitch")) {
849 VLOG_WARN("%s is not an openvswitch device", name
);
851 goto error_close_sock
;
854 if (!isdigit(drvinfo
.bus_info
[0])) {
855 VLOG_WARN("%s ethtool info does not contain an openvswitch minor",
858 goto error_close_sock
;
861 *minor
= atoi(drvinfo
.bus_info
);
872 make_openvswitch_device(unsigned int minor
, char **fnp
)
874 dev_t dev
= makedev(get_openvswitch_major(), minor
);
875 const char dirname
[] = "/dev/net";
880 sprintf(fn
, "%s/dp%d", dirname
, minor
);
882 if (!S_ISCHR(s
.st_mode
)) {
883 VLOG_WARN_RL(&error_rl
, "%s is not a character device, fixing",
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
));
894 VLOG_WARN_RL(&error_rl
, "%s: unlink failed (%s)",
895 fn
, strerror(errno
));
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
));
907 VLOG_WARN_RL(&error_rl
, "%s: stat failed (%s)",
908 dirname
, strerror(errno
));
913 VLOG_WARN_RL(&error_rl
, "%s: stat failed (%s)", fn
, strerror(errno
));
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
));
932 get_openvswitch_major(void)
934 static unsigned int openvswitch_major
;
935 if (!openvswitch_major
) {
936 enum { DEFAULT_MAJOR
= 248 };
937 openvswitch_major
= get_major("openvswitch", DEFAULT_MAJOR
);
939 return openvswitch_major
;
943 get_major(const char *target
, int default_major
)
945 const char fn
[] = "/proc/devices";
950 file
= fopen(fn
, "r");
952 VLOG_ERR("opening %s failed (%s)", fn
, strerror(errno
));
956 for (ln
= 1; fgets(line
, sizeof line
, file
); ln
++) {
960 if (!strncmp(line
, "Character", 9) || line
[0] == '\0') {
962 } else if (!strncmp(line
, "Block", 5)) {
963 /* We only want character devices, so skip the rest of the file. */
965 } else if (sscanf(line
, "%d %63s", &major
, name
)) {
966 if (!strcmp(name
, target
)) {
973 VLOG_WARN("%s:%d: syntax error", fn
, ln
);
979 VLOG_ERR("%s: %s major not found (is the module loaded?), using "
980 "default major %d", fn
, target
, default_major
);
982 VLOG_INFO("using default major %d for %s", default_major
, target
);
983 return default_major
;
987 name_to_minor(const char *name
, unsigned int *minor
)
989 if (!get_minor_from_name(name
, minor
)) {
992 return lookup_minor(name
, minor
);
996 get_minor_from_name(const char *name
, unsigned int *minor
)
998 if (!strncmp(name
, "dp", 2) && isdigit(name
[2])) {
999 *minor
= atoi(name
+ 2);
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);
1011 open_by_minor(unsigned int minor
, struct dpif
*dpif
)
1019 error
= make_openvswitch_device(minor
, &fn
);
1024 fd
= open(fn
, O_RDONLY
| O_NONBLOCK
);
1027 VLOG_WARN("%s: open failed (%s)", fn
, strerror(error
));
1033 dpif
->minor
= minor
;
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.
1043 * This function attempts to combat the problem by:
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).
1048 * - Storing a distinctive value that is likely to cause an
1049 * easy-to-identify error later if it is dereferenced, etc.
1051 * - Triggering a warning on uninitialized memory from Valgrind if
1052 * "actions" or "n_actions" was not initialized.
1055 check_rw_odp_flow(struct odp_flow
*flow
)
1057 if (flow
->n_actions
) {
1058 memset(&flow
->actions
[0], 0xcc, sizeof flow
->actions
[0]);