2 * Copyright (c) 2008, 2009 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
51 #define THIS_MODULE VLM_dpif
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);
58 /* Not really much point in logging many dpif errors. */
59 static struct vlog_rate_limit error_rl
= VLOG_RATE_LIMIT_INIT(9999, 5);
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
*);
69 /* Clears 'all_dps' and enumerates the names of all known created
70 * datapaths into it. Returns 0 if successful, otherwise a positive
73 dp_enumerate(struct svec
*all_dps
)
80 for (i
= 0; i
< ODP_MAX
; i
++) {
85 sprintf(devname
, "dp%d", i
);
86 retval
= dpif_open(devname
, &dpif
);
88 svec_add(all_dps
, devname
);
90 } else if (retval
!= ENODEV
&& !error
) {
98 dpif_open(const char *name
, struct dpif
*dpif
)
105 error
= name_to_minor(name
, &dpif
->minor
);
110 error
= open_by_minor(dpif
->minor
, dpif
);
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
)) {
121 if (error
!= ENODEV
) {
122 VLOG_WARN("dp%u: probe returned unexpected error: %s",
123 dpif
->minor
, strerror(error
));
132 dpif_close(struct dpif
*dpif
)
141 do_ioctl(const struct dpif
*dpif
, int cmd
, const char *cmd_name
,
144 int error
= ioctl(dpif
->fd
, cmd
, arg
) ? errno
: 0;
147 VLOG_WARN_RL(&error_rl
, "dp%u: ioctl(%s) failed (%s)",
148 dpif
->minor
, cmd_name
, strerror(error
));
150 VLOG_DBG_RL(&dpmsg_rl
, "dp%u: ioctl(%s): success",
151 dpif
->minor
, cmd_name
);
158 dpif_create(const char *name
, struct dpif
*dpif
)
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
);
170 if (!strncmp(name
, "nl:", 3)) {
172 sprintf(devname
, "of%u", minor
);
173 error
= ioctl(dpif
->fd
, ODP_DP_CREATE
, devname
) < 0 ? errno
: 0;
175 error
= ioctl(dpif
->fd
, ODP_DP_CREATE
, name
) < 0 ? errno
: 0;
182 for (minor
= 0; minor
< ODP_MAX
; minor
++) {
183 error
= open_by_minor(minor
, dpif
);
188 error
= ioctl(dpif
->fd
, ODP_DP_CREATE
, name
) < 0 ? errno
: 0;
193 if (error
!= EBUSY
) {
202 dpif_get_name(struct dpif
*dpif
, char *name
, size_t name_size
)
204 struct odp_port port
;
207 assert(name_size
> 0);
210 error
= dpif_port_query_by_number(dpif
, ODPP_LOCAL
, &port
);
212 ovs_strlcpy(name
, port
.devname
, name_size
);
218 dpif_delete(struct dpif
*dpif
)
220 COVERAGE_INC(dpif_destroy
);
221 return do_ioctl(dpif
, ODP_DP_DESTROY
, "ODP_DP_DESTROY", NULL
);
225 dpif_get_dp_stats(const struct dpif
*dpif
, struct odp_stats
*stats
)
227 memset(stats
, 0, sizeof *stats
);
228 return do_ioctl(dpif
, ODP_DP_STATS
, "ODP_DP_STATS", stats
);
232 dpif_get_drop_frags(const struct dpif
*dpif
, bool *drop_frags
)
235 int error
= do_ioctl(dpif
, ODP_GET_DROP_FRAGS
, "ODP_GET_DROP_FRAGS", &tmp
);
236 *drop_frags
= error
? tmp
& 1 : false;
241 dpif_set_drop_frags(struct dpif
*dpif
, bool drop_frags
)
243 int tmp
= drop_frags
;
244 return do_ioctl(dpif
, ODP_SET_DROP_FRAGS
, "ODP_SET_DROP_FRAGS", &tmp
);
248 dpif_get_listen_mask(const struct dpif
*dpif
, int *listen_mask
)
250 int error
= do_ioctl(dpif
, ODP_GET_LISTEN_MASK
, "ODP_GET_LISTEN_MASK",
259 dpif_set_listen_mask(struct dpif
*dpif
, int listen_mask
)
261 return do_ioctl(dpif
, ODP_SET_LISTEN_MASK
, "ODP_SET_LISTEN_MASK",
266 dpif_purge(struct dpif
*dpif
)
268 struct odp_stats stats
;
272 COVERAGE_INC(dpif_purge
);
274 error
= dpif_get_dp_stats(dpif
, &stats
);
279 for (i
= 0; i
< stats
.max_miss_queue
+ stats
.max_action_queue
; i
++) {
281 error
= dpif_recv(dpif
, &buf
);
283 return error
== EAGAIN
? 0 : error
;
291 dpif_port_add(struct dpif
*dpif
, const char *devname
, uint16_t port_no
,
294 struct odp_port port
;
296 COVERAGE_INC(dpif_port_add
);
297 memset(&port
, 0, sizeof port
);
298 strncpy(port
.devname
, devname
, sizeof port
.devname
);
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
);
306 VLOG_WARN_RL(&error_rl
, "dp%u: failed to add %s as port "
307 "%"PRIu16
": %s", dpif
->minor
, devname
, port_no
,
314 dpif_port_del(struct dpif
*dpif
, uint16_t port_no
)
317 COVERAGE_INC(dpif_port_del
);
318 return do_ioctl(dpif
, ODP_PORT_DEL
, "ODP_PORT_DEL", &tmp
);
322 dpif_port_query_by_number(const struct dpif
*dpif
, uint16_t port_no
,
323 struct odp_port
*port
)
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
);
332 VLOG_WARN_RL(&error_rl
, "dp%u: failed to query port %"PRIu16
": %s",
333 dpif
->minor
, port_no
, strerror(errno
));
339 dpif_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
340 struct odp_port
*port
)
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
);
349 VLOG_WARN_RL(&error_rl
, "dp%u: failed to query port %s: %s",
350 dpif
->minor
, devname
, strerror(errno
));
355 /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and copies
356 * the port's name into the 'name_size' bytes in 'name', ensuring that the
357 * result is null-terminated. On failure, returns a positive errno value and
358 * makes 'name' the empty string. */
360 dpif_port_get_name(struct dpif
*dpif
, uint16_t port_no
,
361 char *name
, size_t name_size
)
363 struct odp_port port
;
366 assert(name_size
> 0);
368 error
= dpif_port_query_by_number(dpif
, port_no
, &port
);
370 ovs_strlcpy(name
, port
.devname
, name_size
);
378 dpif_port_list(const struct dpif
*dpif
,
379 struct odp_port
**ports
, size_t *n_ports
)
381 struct odp_portvec pv
;
382 struct odp_stats stats
;
386 error
= dpif_get_dp_stats(dpif
, &stats
);
391 *ports
= xcalloc(1, stats
.n_ports
* sizeof **ports
);
393 pv
.n_ports
= stats
.n_ports
;
394 error
= do_ioctl(dpif
, ODP_PORT_LIST
, "ODP_PORT_LIST", &pv
);
399 } while (pv
.n_ports
!= stats
.n_ports
);
400 *n_ports
= pv
.n_ports
;
410 dpif_port_group_set(struct dpif
*dpif
, uint16_t group
,
411 const uint16_t ports
[], size_t n_ports
)
413 struct odp_port_group pg
;
415 COVERAGE_INC(dpif_port_group_set
);
416 assert(n_ports
<= UINT16_MAX
);
418 pg
.ports
= (uint16_t *) ports
;
419 pg
.n_ports
= n_ports
;
420 return do_ioctl(dpif
, ODP_PORT_GROUP_SET
, "ODP_PORT_GROUP_SET", &pg
);
423 /* Careful: '*n_out' can be greater than 'n_ports' on return, if 'n_ports' is
424 * less than the number of ports in 'group'. */
426 dpif_port_group_get(const struct dpif
*dpif
, uint16_t group
,
427 uint16_t ports
[], size_t n_ports
, size_t *n_out
)
429 struct odp_port_group pg
;
432 assert(n_ports
<= UINT16_MAX
);
435 pg
.n_ports
= n_ports
;
436 error
= do_ioctl(dpif
, ODP_PORT_GROUP_GET
, "ODP_PORT_GROUP_GET", &pg
);
437 *n_out
= error
? 0 : pg
.n_ports
;
442 dpif_flow_flush(struct dpif
*dpif
)
444 COVERAGE_INC(dpif_flow_flush
);
445 return do_ioctl(dpif
, ODP_FLOW_FLUSH
, "ODP_FLOW_FLUSH", NULL
);
448 static enum vlog_level
449 flow_message_log_level(int error
)
451 return error
? VLL_WARN
: VLL_DBG
;
455 should_log_flow_message(int error
)
457 return !vlog_should_drop(THIS_MODULE
, flow_message_log_level(error
),
458 error
? &error_rl
: &dpmsg_rl
);
462 log_flow_message(const struct dpif
*dpif
, int error
,
463 const char *operation
,
464 const flow_t
*flow
, const struct odp_flow_stats
*stats
,
465 const union odp_action
*actions
, size_t n_actions
)
467 struct ds ds
= DS_EMPTY_INITIALIZER
;
468 ds_put_format(&ds
, "dp%u: ", dpif
->minor
);
470 ds_put_cstr(&ds
, "failed to ");
472 ds_put_format(&ds
, "%s ", operation
);
474 ds_put_format(&ds
, "(%s) ", strerror(error
));
476 flow_format(&ds
, flow
);
478 ds_put_cstr(&ds
, ", ");
479 format_odp_flow_stats(&ds
, stats
);
481 if (actions
|| n_actions
) {
482 ds_put_cstr(&ds
, ", actions:");
483 format_odp_actions(&ds
, actions
, n_actions
);
485 vlog(THIS_MODULE
, flow_message_log_level(error
), "%s", ds_cstr(&ds
));
490 do_flow_ioctl(const struct dpif
*dpif
, int cmd
, struct odp_flow
*flow
,
491 const char *operation
, bool show_stats
)
493 int error
= do_ioctl(dpif
, cmd
, NULL
, flow
);
494 if (error
&& show_stats
) {
497 if (should_log_flow_message(error
)) {
498 log_flow_message(dpif
, error
, operation
, &flow
->key
,
499 show_stats
&& !error
? &flow
->stats
: NULL
,
500 flow
->actions
, flow
->n_actions
);
506 dpif_flow_put(struct dpif
*dpif
, struct odp_flow_put
*put
)
508 int error
= do_ioctl(dpif
, ODP_FLOW_PUT
, NULL
, put
);
509 COVERAGE_INC(dpif_flow_put
);
510 if (should_log_flow_message(error
)) {
511 struct ds operation
= DS_EMPTY_INITIALIZER
;
512 ds_put_cstr(&operation
, "put");
513 if (put
->flags
& ODPPF_CREATE
) {
514 ds_put_cstr(&operation
, "[create]");
516 if (put
->flags
& ODPPF_MODIFY
) {
517 ds_put_cstr(&operation
, "[modify]");
519 if (put
->flags
& ODPPF_ZERO_STATS
) {
520 ds_put_cstr(&operation
, "[zero]");
522 #define ODPPF_ALL (ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS)
523 if (put
->flags
& ~ODPPF_ALL
) {
524 ds_put_format(&operation
, "[%x]", put
->flags
& ~ODPPF_ALL
);
526 log_flow_message(dpif
, error
, ds_cstr(&operation
), &put
->flow
.key
,
527 !error
? &put
->flow
.stats
: NULL
,
528 put
->flow
.actions
, put
->flow
.n_actions
);
529 ds_destroy(&operation
);
535 dpif_flow_del(struct dpif
*dpif
, struct odp_flow
*flow
)
537 COVERAGE_INC(dpif_flow_del
);
538 check_rw_odp_flow(flow
);
539 memset(&flow
->stats
, 0, sizeof flow
->stats
);
540 return do_flow_ioctl(dpif
, ODP_FLOW_DEL
, flow
, "delete flow", true);
544 dpif_flow_get(const struct dpif
*dpif
, struct odp_flow
*flow
)
546 COVERAGE_INC(dpif_flow_query
);
547 check_rw_odp_flow(flow
);
548 memset(&flow
->stats
, 0, sizeof flow
->stats
);
549 return do_flow_ioctl(dpif
, ODP_FLOW_GET
, flow
, "get flow", true);
553 dpif_flow_get_multiple(const struct dpif
*dpif
,
554 struct odp_flow flows
[], size_t n
)
556 struct odp_flowvec fv
;
559 COVERAGE_ADD(dpif_flow_query_multiple
, n
);
562 for (i
= 0; i
< n
; i
++) {
563 check_rw_odp_flow(&flows
[i
]);
565 return do_ioctl(dpif
, ODP_FLOW_GET_MULTIPLE
, "ODP_FLOW_GET_MULTIPLE",
570 dpif_flow_list(const struct dpif
*dpif
, struct odp_flow flows
[], size_t n
,
573 struct odp_flowvec fv
;
577 COVERAGE_INC(dpif_flow_query_list
);
580 if (RUNNING_ON_VALGRIND
) {
581 memset(flows
, 0, n
* sizeof *flows
);
583 for (i
= 0; i
< n
; i
++) {
584 flows
[i
].actions
= NULL
;
585 flows
[i
].n_actions
= 0;
588 error
= do_ioctl(dpif
, ODP_FLOW_LIST
, NULL
, &fv
);
591 VLOG_WARN_RL(&error_rl
, "dp%u: flow list failed (%s)",
592 dpif
->minor
, strerror(error
));
594 COVERAGE_ADD(dpif_flow_query_list_n
, fv
.n_flows
);
596 VLOG_DBG_RL(&dpmsg_rl
, "dp%u: listed %zu flows", dpif
->minor
, *n_out
);
602 dpif_flow_list_all(const struct dpif
*dpif
,
603 struct odp_flow
**flowsp
, size_t *np
)
605 struct odp_stats stats
;
606 struct odp_flow
*flows
;
613 error
= dpif_get_dp_stats(dpif
, &stats
);
618 flows
= xmalloc(sizeof *flows
* stats
.n_flows
);
619 error
= dpif_flow_list(dpif
, flows
, stats
.n_flows
, &n_flows
);
625 if (stats
.n_flows
!= n_flows
) {
626 VLOG_WARN_RL(&error_rl
, "dp%u: datapath stats reported %"PRIu32
" "
627 "flows but flow listing reported %zu",
628 dpif
->minor
, stats
.n_flows
, n_flows
);
636 dpif_execute(struct dpif
*dpif
, uint16_t in_port
,
637 const union odp_action actions
[], size_t n_actions
,
638 const struct ofpbuf
*buf
)
642 COVERAGE_INC(dpif_execute
);
644 struct odp_execute execute
;
645 memset(&execute
, 0, sizeof execute
);
646 execute
.in_port
= in_port
;
647 execute
.actions
= (union odp_action
*) actions
;
648 execute
.n_actions
= n_actions
;
649 execute
.data
= buf
->data
;
650 execute
.length
= buf
->size
;
651 error
= do_ioctl(dpif
, ODP_EXECUTE
, NULL
, &execute
);
656 if (!(error
? VLOG_DROP_WARN(&error_rl
) : VLOG_DROP_DBG(&dpmsg_rl
))) {
657 struct ds ds
= DS_EMPTY_INITIALIZER
;
658 char *packet
= ofp_packet_to_string(buf
->data
, buf
->size
, buf
->size
);
659 ds_put_format(&ds
, "dp%u: execute ", dpif
->minor
);
660 format_odp_actions(&ds
, actions
, n_actions
);
662 ds_put_format(&ds
, " failed (%s)", strerror(error
));
664 ds_put_format(&ds
, " on packet %s", packet
);
665 vlog(THIS_MODULE
, error
? VLL_WARN
: VLL_DBG
, "%s", ds_cstr(&ds
));
673 dpif_recv(struct dpif
*dpif
, struct ofpbuf
**bufp
)
679 buf
= ofpbuf_new(65536);
680 retval
= read(dpif
->fd
, ofpbuf_tail(buf
), ofpbuf_tailroom(buf
));
683 if (error
!= EAGAIN
) {
684 VLOG_WARN_RL(&error_rl
, "dp%u: read failed: %s",
685 dpif
->minor
, strerror(error
));
687 } else if (retval
>= sizeof(struct odp_msg
)) {
688 struct odp_msg
*msg
= buf
->data
;
689 if (msg
->length
<= retval
) {
691 if (VLOG_IS_DBG_ENABLED()) {
692 void *payload
= msg
+ 1;
693 size_t length
= buf
->size
- sizeof *msg
;
694 char *s
= ofp_packet_to_string(payload
, length
, length
);
695 VLOG_DBG_RL(&dpmsg_rl
, "dp%u: received %s message of length "
696 "%zu on port %"PRIu16
": %s", dpif
->minor
,
697 (msg
->type
== _ODPL_MISS_NR
? "miss"
698 : msg
->type
== _ODPL_ACTION_NR
? "action"
700 msg
->length
- sizeof(struct odp_msg
),
705 COVERAGE_INC(dpif_recv
);
708 VLOG_WARN_RL(&error_rl
, "dp%u: discarding message truncated "
709 "from %"PRIu32
" bytes to %d",
710 dpif
->minor
, msg
->length
, retval
);
713 } else if (!retval
) {
714 VLOG_WARN_RL(&error_rl
, "dp%u: unexpected end of file", dpif
->minor
);
717 VLOG_WARN_RL(&error_rl
,
718 "dp%u: discarding too-short message (%d bytes)",
719 dpif
->minor
, retval
);
729 dpif_recv_wait(struct dpif
*dpif
)
731 poll_fd_wait(dpif
->fd
, POLLIN
);
736 struct nl_sock
*sock
;
741 dpifmon_create(const char *datapath_name
, struct dpifmon
**monp
)
744 char local_name
[IFNAMSIZ
];
747 mon
= *monp
= xmalloc(sizeof *mon
);
749 error
= dpif_open(datapath_name
, &mon
->dpif
);
753 error
= dpif_get_name(&mon
->dpif
, local_name
, sizeof local_name
);
755 goto error_close_dpif
;
758 mon
->local_ifindex
= if_nametoindex(local_name
);
759 if (!mon
->local_ifindex
) {
761 VLOG_WARN("could not get ifindex of %s device: %s",
762 local_name
, strerror(errno
));
763 goto error_close_dpif
;
766 error
= nl_sock_create(NETLINK_ROUTE
, RTNLGRP_LINK
, 0, 0, &mon
->sock
);
768 VLOG_WARN("could not create rtnetlink socket: %s", strerror(error
));
769 goto error_close_dpif
;
775 dpif_close(&mon
->dpif
);
783 dpifmon_destroy(struct dpifmon
*mon
)
786 dpif_close(&mon
->dpif
);
787 nl_sock_destroy(mon
->sock
);
792 dpifmon_poll(struct dpifmon
*mon
, char **devnamep
)
794 static struct vlog_rate_limit slow_rl
= VLOG_RATE_LIMIT_INIT(1, 5);
795 static const struct nl_policy rtnlgrp_link_policy
[] = {
796 [IFLA_IFNAME
] = { .type
= NL_A_STRING
},
797 [IFLA_MASTER
] = { .type
= NL_A_U32
, .optional
= true },
799 struct nlattr
*attrs
[ARRAY_SIZE(rtnlgrp_link_policy
)];
805 error
= nl_sock_recv(mon
->sock
, &buf
, false);
808 if (!nl_policy_parse(buf
, NLMSG_HDRLEN
+ sizeof(struct ifinfomsg
),
810 attrs
, ARRAY_SIZE(rtnlgrp_link_policy
))) {
811 VLOG_WARN_RL(&slow_rl
, "received bad rtnl message");
814 const char *devname
= nl_attr_get_string(attrs
[IFLA_IFNAME
]);
817 if (attrs
[IFLA_MASTER
]) {
818 uint32_t master_ifindex
= nl_attr_get_u32(attrs
[IFLA_MASTER
]);
819 for_us
= master_ifindex
== mon
->local_ifindex
;
821 /* It's for us if that device is one of our ports. This is
822 * open-coded instead of using dpif_port_query_by_name() to
823 * avoid logging a warning on failure. */
824 struct odp_port port
;
825 memset(&port
, 0, sizeof port
);
826 strncpy(port
.devname
, devname
, sizeof port
.devname
);
827 for_us
= !ioctl(mon
->dpif
.fd
, ODP_PORT_QUERY
, &port
);
831 /* Not for us, try again. */
833 COVERAGE_INC(dpifmon_poll_false_wakeup
);
836 COVERAGE_INC(dpifmon_poll_changed
);
837 *devnamep
= xstrdup(devname
);
847 VLOG_WARN_RL(&slow_rl
, "dpifmon socket overflowed");
851 VLOG_WARN_RL(&slow_rl
, "error on dpifmon socket: %s", strerror(error
));
858 dpifmon_run(struct dpifmon
*mon UNUSED
)
860 /* Nothing to do in this implementation. */
864 dpifmon_wait(struct dpifmon
*mon
)
866 nl_sock_wait(mon
->sock
, POLLIN
);
869 static int get_openvswitch_major(void);
870 static int get_major(const char *target
, int default_major
);
873 lookup_minor(const char *name
, unsigned int *minor
)
875 struct ethtool_drvinfo drvinfo
;
881 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
883 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno
));
888 memset(&ifr
, 0, sizeof ifr
);
889 strncpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
890 ifr
.ifr_data
= (caddr_t
) &drvinfo
;
892 memset(&drvinfo
, 0, sizeof drvinfo
);
893 drvinfo
.cmd
= ETHTOOL_GDRVINFO
;
894 if (ioctl(sock
, SIOCETHTOOL
, &ifr
)) {
895 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno
));
897 goto error_close_sock
;
900 if (strcmp(drvinfo
.driver
, "openvswitch")) {
901 VLOG_WARN("%s is not an openvswitch device", name
);
903 goto error_close_sock
;
906 if (!isdigit(drvinfo
.bus_info
[0])) {
907 VLOG_WARN("%s ethtool info does not contain an openvswitch minor",
910 goto error_close_sock
;
913 *minor
= atoi(drvinfo
.bus_info
);
924 make_openvswitch_device(unsigned int minor
, char **fnp
)
926 dev_t dev
= makedev(get_openvswitch_major(), minor
);
927 const char dirname
[] = "/dev/net";
932 sprintf(fn
, "%s/dp%d", dirname
, minor
);
934 if (!S_ISCHR(s
.st_mode
)) {
935 VLOG_WARN_RL(&error_rl
, "%s is not a character device, fixing",
937 } else if (s
.st_rdev
!= dev
) {
938 VLOG_WARN_RL(&error_rl
,
939 "%s is device %u:%u instead of %u:%u, fixing",
940 fn
, major(s
.st_rdev
), minor(s
.st_rdev
),
941 major(dev
), minor(dev
));
946 VLOG_WARN_RL(&error_rl
, "%s: unlink failed (%s)",
947 fn
, strerror(errno
));
950 } else if (errno
== ENOENT
) {
951 if (stat(dirname
, &s
)) {
952 if (errno
== ENOENT
) {
953 if (mkdir(dirname
, 0755)) {
954 VLOG_WARN_RL(&error_rl
, "%s: mkdir failed (%s)",
955 dirname
, strerror(errno
));
959 VLOG_WARN_RL(&error_rl
, "%s: stat failed (%s)",
960 dirname
, strerror(errno
));
965 VLOG_WARN_RL(&error_rl
, "%s: stat failed (%s)", fn
, strerror(errno
));
969 /* The device needs to be created. */
970 if (mknod(fn
, S_IFCHR
| 0700, dev
)) {
971 VLOG_WARN_RL(&error_rl
,
972 "%s: creating character device %u:%u failed (%s)",
973 fn
, major(dev
), minor(dev
), strerror(errno
));
984 get_openvswitch_major(void)
986 static unsigned int openvswitch_major
;
987 if (!openvswitch_major
) {
988 enum { DEFAULT_MAJOR
= 248 };
989 openvswitch_major
= get_major("openvswitch", DEFAULT_MAJOR
);
991 return openvswitch_major
;
995 get_major(const char *target
, int default_major
)
997 const char fn
[] = "/proc/devices";
1002 file
= fopen(fn
, "r");
1004 VLOG_ERR("opening %s failed (%s)", fn
, strerror(errno
));
1008 for (ln
= 1; fgets(line
, sizeof line
, file
); ln
++) {
1012 if (!strncmp(line
, "Character", 9) || line
[0] == '\0') {
1013 /* Nothing to do. */
1014 } else if (!strncmp(line
, "Block", 5)) {
1015 /* We only want character devices, so skip the rest of the file. */
1017 } else if (sscanf(line
, "%d %63s", &major
, name
)) {
1018 if (!strcmp(name
, target
)) {
1025 VLOG_WARN("%s:%d: syntax error", fn
, ln
);
1031 VLOG_ERR("%s: %s major not found (is the module loaded?), using "
1032 "default major %d", fn
, target
, default_major
);
1034 VLOG_INFO("using default major %d for %s", default_major
, target
);
1035 return default_major
;
1039 name_to_minor(const char *name
, unsigned int *minor
)
1041 if (!get_minor_from_name(name
, minor
)) {
1044 return lookup_minor(name
, minor
);
1048 get_minor_from_name(const char *name
, unsigned int *minor
)
1050 if (!strncmp(name
, "dp", 2) && isdigit(name
[2])) {
1051 *minor
= atoi(name
+ 2);
1053 } else if (!strncmp(name
, "nl:", 3) && isdigit(name
[3])) {
1054 /* This is for compatibility only and will be dropped. */
1055 *minor
= atoi(name
+ 3);
1063 open_by_minor(unsigned int minor
, struct dpif
*dpif
)
1071 error
= make_openvswitch_device(minor
, &fn
);
1076 fd
= open(fn
, O_RDONLY
| O_NONBLOCK
);
1079 VLOG_WARN("%s: open failed (%s)", fn
, strerror(error
));
1085 dpif
->minor
= minor
;
1090 /* There is a tendency to construct odp_flow objects on the stack and to
1091 * forget to properly initialize their "actions" and "n_actions" members.
1092 * When this happens, we get memory corruption because the kernel
1093 * writes through the random pointer that is in the "actions" member.
1095 * This function attempts to combat the problem by:
1097 * - Forcing a segfault if "actions" points to an invalid region (instead
1098 * of just getting back EFAULT, which can be easily missed in the log).
1100 * - Storing a distinctive value that is likely to cause an
1101 * easy-to-identify error later if it is dereferenced, etc.
1103 * - Triggering a warning on uninitialized memory from Valgrind if
1104 * "actions" or "n_actions" was not initialized.
1107 check_rw_odp_flow(struct odp_flow
*flow
)
1109 if (flow
->n_actions
) {
1110 memset(&flow
->actions
[0], 0xcc, sizeof flow
->actions
[0]);