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
));
356 dpif_port_list(const struct dpif
*dpif
,
357 struct odp_port
**ports
, size_t *n_ports
)
359 struct odp_portvec pv
;
360 struct odp_stats stats
;
364 error
= dpif_get_dp_stats(dpif
, &stats
);
369 *ports
= xcalloc(1, stats
.n_ports
* sizeof **ports
);
371 pv
.n_ports
= stats
.n_ports
;
372 error
= do_ioctl(dpif
, ODP_PORT_LIST
, "ODP_PORT_LIST", &pv
);
377 } while (pv
.n_ports
!= stats
.n_ports
);
378 *n_ports
= pv
.n_ports
;
388 dpif_port_group_set(struct dpif
*dpif
, uint16_t group
,
389 const uint16_t ports
[], size_t n_ports
)
391 struct odp_port_group pg
;
393 COVERAGE_INC(dpif_port_group_set
);
394 assert(n_ports
<= UINT16_MAX
);
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
);
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'. */
404 dpif_port_group_get(const struct dpif
*dpif
, uint16_t group
,
405 uint16_t ports
[], size_t n_ports
, size_t *n_out
)
407 struct odp_port_group pg
;
410 assert(n_ports
<= UINT16_MAX
);
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
;
420 dpif_flow_flush(struct dpif
*dpif
)
422 COVERAGE_INC(dpif_flow_flush
);
423 return do_ioctl(dpif
, ODP_FLOW_FLUSH
, "ODP_FLOW_FLUSH", NULL
);
426 static enum vlog_level
427 flow_message_log_level(int error
)
429 return error
? VLL_WARN
: VLL_DBG
;
433 should_log_flow_message(int error
)
435 return !vlog_should_drop(THIS_MODULE
, flow_message_log_level(error
),
436 error
? &error_rl
: &dpmsg_rl
);
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
)
445 struct ds ds
= DS_EMPTY_INITIALIZER
;
446 ds_put_format(&ds
, "dp%u: ", dpif
->minor
);
448 ds_put_cstr(&ds
, "failed to ");
450 ds_put_format(&ds
, "%s ", operation
);
452 ds_put_format(&ds
, "(%s) ", strerror(error
));
454 flow_format(&ds
, flow
);
456 ds_put_cstr(&ds
, ", ");
457 format_odp_flow_stats(&ds
, stats
);
459 if (actions
|| n_actions
) {
460 ds_put_cstr(&ds
, ", actions:");
461 format_odp_actions(&ds
, actions
, n_actions
);
463 vlog(THIS_MODULE
, flow_message_log_level(error
), "%s", ds_cstr(&ds
));
468 do_flow_ioctl(const struct dpif
*dpif
, int cmd
, struct odp_flow
*flow
,
469 const char *operation
, bool show_stats
)
471 int error
= do_ioctl(dpif
, cmd
, NULL
, flow
);
472 if (error
&& show_stats
) {
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
);
484 dpif_flow_put(struct dpif
*dpif
, struct odp_flow_put
*put
)
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]");
494 if (put
->flags
& ODPPF_MODIFY
) {
495 ds_put_cstr(&operation
, "[modify]");
497 if (put
->flags
& ODPPF_ZERO_STATS
) {
498 ds_put_cstr(&operation
, "[zero]");
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
);
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
);
513 dpif_flow_del(struct dpif
*dpif
, struct odp_flow
*flow
)
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);
522 dpif_flow_get(const struct dpif
*dpif
, struct odp_flow
*flow
)
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);
531 dpif_flow_get_multiple(const struct dpif
*dpif
,
532 struct odp_flow flows
[], size_t n
)
534 struct odp_flowvec fv
;
537 COVERAGE_ADD(dpif_flow_query_multiple
, n
);
540 for (i
= 0; i
< n
; i
++) {
541 check_rw_odp_flow(&flows
[i
]);
543 return do_ioctl(dpif
, ODP_FLOW_GET_MULTIPLE
, "ODP_FLOW_GET_MULTIPLE",
548 dpif_flow_list(const struct dpif
*dpif
, struct odp_flow flows
[], size_t n
,
551 struct odp_flowvec fv
;
555 COVERAGE_INC(dpif_flow_query_list
);
558 if (RUNNING_ON_VALGRIND
) {
559 memset(flows
, 0, n
* sizeof *flows
);
561 for (i
= 0; i
< n
; i
++) {
562 flows
[i
].actions
= NULL
;
563 flows
[i
].n_actions
= 0;
566 error
= do_ioctl(dpif
, ODP_FLOW_LIST
, NULL
, &fv
);
569 VLOG_WARN_RL(&error_rl
, "dp%u: flow list failed (%s)",
570 dpif
->minor
, strerror(error
));
572 COVERAGE_ADD(dpif_flow_query_list_n
, fv
.n_flows
);
574 VLOG_DBG_RL(&dpmsg_rl
, "dp%u: listed %zu flows", dpif
->minor
, *n_out
);
580 dpif_flow_list_all(const struct dpif
*dpif
,
581 struct odp_flow
**flowsp
, size_t *np
)
583 struct odp_stats stats
;
584 struct odp_flow
*flows
;
591 error
= dpif_get_dp_stats(dpif
, &stats
);
596 flows
= xmalloc(sizeof *flows
* stats
.n_flows
);
597 error
= dpif_flow_list(dpif
, flows
, stats
.n_flows
, &n_flows
);
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
);
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
)
620 COVERAGE_INC(dpif_execute
);
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
);
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
);
640 ds_put_format(&ds
, " failed (%s)", strerror(error
));
642 ds_put_format(&ds
, " on packet %s", packet
);
643 vlog(THIS_MODULE
, error
? VLL_WARN
: VLL_DBG
, "%s", ds_cstr(&ds
));
651 dpif_recv(struct dpif
*dpif
, struct ofpbuf
**bufp
)
657 buf
= ofpbuf_new(65536);
658 retval
= read(dpif
->fd
, ofpbuf_tail(buf
), ofpbuf_tailroom(buf
));
661 if (error
!= EAGAIN
) {
662 VLOG_WARN_RL(&error_rl
, "dp%u: read failed: %s",
663 dpif
->minor
, strerror(error
));
665 } else if (retval
>= sizeof(struct odp_msg
)) {
666 struct odp_msg
*msg
= buf
->data
;
667 if (msg
->length
<= 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"
678 msg
->length
- sizeof(struct odp_msg
),
683 COVERAGE_INC(dpif_recv
);
686 VLOG_WARN_RL(&error_rl
, "dp%u: discarding message truncated "
687 "from %zu bytes to %d",
688 dpif
->minor
, msg
->length
, retval
);
691 } else if (!retval
) {
692 VLOG_WARN_RL(&error_rl
, "dp%u: unexpected end of file", dpif
->minor
);
695 VLOG_WARN_RL(&error_rl
,
696 "dp%u: discarding too-short message (%d bytes)",
697 dpif
->minor
, retval
);
707 dpif_recv_wait(struct dpif
*dpif
)
709 poll_fd_wait(dpif
->fd
, POLLIN
);
714 struct nl_sock
*sock
;
719 dpifmon_create(const char *datapath_name
, struct dpifmon
**monp
)
722 char local_name
[IFNAMSIZ
];
725 mon
= *monp
= xmalloc(sizeof *mon
);
727 error
= dpif_open(datapath_name
, &mon
->dpif
);
731 error
= dpif_get_name(&mon
->dpif
, local_name
, sizeof local_name
);
733 goto error_close_dpif
;
736 mon
->local_ifindex
= if_nametoindex(local_name
);
737 if (!mon
->local_ifindex
) {
739 VLOG_WARN("could not get ifindex of %s device: %s",
740 local_name
, strerror(errno
));
741 goto error_close_dpif
;
744 error
= nl_sock_create(NETLINK_ROUTE
, RTNLGRP_LINK
, 0, 0, &mon
->sock
);
746 VLOG_WARN("could not create rtnetlink socket: %s", strerror(error
));
747 goto error_close_dpif
;
753 dpif_close(&mon
->dpif
);
761 dpifmon_destroy(struct dpifmon
*mon
)
764 dpif_close(&mon
->dpif
);
765 nl_sock_destroy(mon
->sock
);
770 dpifmon_poll(struct dpifmon
*mon
, char **devnamep
)
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 },
777 struct nlattr
*attrs
[ARRAY_SIZE(rtnlgrp_link_policy
)];
783 error
= nl_sock_recv(mon
->sock
, &buf
, false);
786 if (!nl_policy_parse(buf
, NLMSG_HDRLEN
+ sizeof(struct ifinfomsg
),
788 attrs
, ARRAY_SIZE(rtnlgrp_link_policy
))) {
789 VLOG_WARN_RL(&slow_rl
, "received bad rtnl message");
792 const char *devname
= nl_attr_get_string(attrs
[IFLA_IFNAME
]);
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
;
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
);
809 /* Not for us, try again. */
811 COVERAGE_INC(dpifmon_poll_false_wakeup
);
814 COVERAGE_INC(dpifmon_poll_changed
);
815 *devnamep
= xstrdup(devname
);
825 VLOG_WARN_RL(&slow_rl
, "dpifmon socket overflowed");
829 VLOG_WARN_RL(&slow_rl
, "error on dpifmon socket: %s", strerror(error
));
836 dpifmon_run(struct dpifmon
*mon UNUSED
)
838 /* Nothing to do in this implementation. */
842 dpifmon_wait(struct dpifmon
*mon
)
844 nl_sock_wait(mon
->sock
, POLLIN
);
847 static int get_openvswitch_major(void);
848 static int get_major(const char *target
, int default_major
);
851 lookup_minor(const char *name
, unsigned int *minor
)
853 struct ethtool_drvinfo drvinfo
;
859 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
861 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno
));
866 memset(&ifr
, 0, sizeof ifr
);
867 strncpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
868 ifr
.ifr_data
= (caddr_t
) &drvinfo
;
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
));
875 goto error_close_sock
;
878 if (strcmp(drvinfo
.driver
, "openvswitch")) {
879 VLOG_WARN("%s is not an openvswitch device", name
);
881 goto error_close_sock
;
884 if (!isdigit(drvinfo
.bus_info
[0])) {
885 VLOG_WARN("%s ethtool info does not contain an openvswitch minor",
888 goto error_close_sock
;
891 *minor
= atoi(drvinfo
.bus_info
);
902 make_openvswitch_device(unsigned int minor
, char **fnp
)
904 dev_t dev
= makedev(get_openvswitch_major(), minor
);
905 const char dirname
[] = "/dev/net";
910 sprintf(fn
, "%s/dp%d", dirname
, minor
);
912 if (!S_ISCHR(s
.st_mode
)) {
913 VLOG_WARN_RL(&error_rl
, "%s is not a character device, fixing",
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
));
924 VLOG_WARN_RL(&error_rl
, "%s: unlink failed (%s)",
925 fn
, strerror(errno
));
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
));
937 VLOG_WARN_RL(&error_rl
, "%s: stat failed (%s)",
938 dirname
, strerror(errno
));
943 VLOG_WARN_RL(&error_rl
, "%s: stat failed (%s)", fn
, strerror(errno
));
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
));
962 get_openvswitch_major(void)
964 static unsigned int openvswitch_major
;
965 if (!openvswitch_major
) {
966 enum { DEFAULT_MAJOR
= 248 };
967 openvswitch_major
= get_major("openvswitch", DEFAULT_MAJOR
);
969 return openvswitch_major
;
973 get_major(const char *target
, int default_major
)
975 const char fn
[] = "/proc/devices";
980 file
= fopen(fn
, "r");
982 VLOG_ERR("opening %s failed (%s)", fn
, strerror(errno
));
986 for (ln
= 1; fgets(line
, sizeof line
, file
); ln
++) {
990 if (!strncmp(line
, "Character", 9) || line
[0] == '\0') {
992 } else if (!strncmp(line
, "Block", 5)) {
993 /* We only want character devices, so skip the rest of the file. */
995 } else if (sscanf(line
, "%d %63s", &major
, name
)) {
996 if (!strcmp(name
, target
)) {
1003 VLOG_WARN("%s:%d: syntax error", fn
, ln
);
1009 VLOG_ERR("%s: %s major not found (is the module loaded?), using "
1010 "default major %d", fn
, target
, default_major
);
1012 VLOG_INFO("using default major %d for %s", default_major
, target
);
1013 return default_major
;
1017 name_to_minor(const char *name
, unsigned int *minor
)
1019 if (!get_minor_from_name(name
, minor
)) {
1022 return lookup_minor(name
, minor
);
1026 get_minor_from_name(const char *name
, unsigned int *minor
)
1028 if (!strncmp(name
, "dp", 2) && isdigit(name
[2])) {
1029 *minor
= atoi(name
+ 2);
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);
1041 open_by_minor(unsigned int minor
, struct dpif
*dpif
)
1049 error
= make_openvswitch_device(minor
, &fn
);
1054 fd
= open(fn
, O_RDONLY
| O_NONBLOCK
);
1057 VLOG_WARN("%s: open failed (%s)", fn
, strerror(error
));
1063 dpif
->minor
= minor
;
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.
1073 * This function attempts to combat the problem by:
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).
1078 * - Storing a distinctive value that is likely to cause an
1079 * easy-to-identify error later if it is dereferenced, etc.
1081 * - Triggering a warning on uninitialized memory from Valgrind if
1082 * "actions" or "n_actions" was not initialized.
1085 check_rw_odp_flow(struct odp_flow
*flow
)
1087 if (flow
->n_actions
) {
1088 memset(&flow
->actions
[0], 0xcc, sizeof flow
->actions
[0]);