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"
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("%s: probe returned unexpected error: %s",
93 dpif_name(dpif
), strerror(error
));
102 dpif_close(struct dpif
*dpif
)
113 do_ioctl(const struct dpif
*dpif
, int cmd
, const char *cmd_name
,
116 int error
= ioctl(dpif
->fd
, cmd
, arg
) ? errno
: 0;
119 VLOG_WARN_RL(&error_rl
, "%s: ioctl(%s) failed (%s)",
120 dpif_name(dpif
), cmd_name
, strerror(error
));
122 VLOG_DBG_RL(&dpmsg_rl
, "%s: ioctl(%s): success",
123 dpif_name(dpif
), cmd_name
);
130 dpif_create(const char *name
, struct dpif
*dpif
)
135 if (!get_minor_from_name(name
, &minor
)) {
136 /* Minor was specified in 'name', go ahead and create it. */
137 error
= open_by_minor(minor
, dpif
);
142 if (!strncmp(name
, "nl:", 3)) {
144 sprintf(devname
, "of%u", minor
);
145 error
= ioctl(dpif
->fd
, ODP_DP_CREATE
, devname
) < 0 ? errno
: 0;
147 error
= ioctl(dpif
->fd
, ODP_DP_CREATE
, name
) < 0 ? errno
: 0;
154 for (minor
= 0; minor
< ODP_MAX
; minor
++) {
155 error
= open_by_minor(minor
, dpif
);
160 error
= ioctl(dpif
->fd
, ODP_DP_CREATE
, name
) < 0 ? errno
: 0;
165 if (error
!= EBUSY
) {
174 dpif_name(const struct dpif
*dpif
)
180 dpif_delete(struct dpif
*dpif
)
182 COVERAGE_INC(dpif_destroy
);
183 return do_ioctl(dpif
, ODP_DP_DESTROY
, "ODP_DP_DESTROY", NULL
);
187 dpif_get_dp_stats(const struct dpif
*dpif
, struct odp_stats
*stats
)
189 memset(stats
, 0, sizeof *stats
);
190 return do_ioctl(dpif
, ODP_DP_STATS
, "ODP_DP_STATS", stats
);
194 dpif_get_drop_frags(const struct dpif
*dpif
, bool *drop_frags
)
197 int error
= do_ioctl(dpif
, ODP_GET_DROP_FRAGS
, "ODP_GET_DROP_FRAGS", &tmp
);
198 *drop_frags
= error
? tmp
& 1 : false;
203 dpif_set_drop_frags(struct dpif
*dpif
, bool drop_frags
)
205 int tmp
= drop_frags
;
206 return do_ioctl(dpif
, ODP_SET_DROP_FRAGS
, "ODP_SET_DROP_FRAGS", &tmp
);
210 dpif_get_listen_mask(const struct dpif
*dpif
, int *listen_mask
)
212 int error
= do_ioctl(dpif
, ODP_GET_LISTEN_MASK
, "ODP_GET_LISTEN_MASK",
221 dpif_set_listen_mask(struct dpif
*dpif
, int listen_mask
)
223 return do_ioctl(dpif
, ODP_SET_LISTEN_MASK
, "ODP_SET_LISTEN_MASK",
228 dpif_purge(struct dpif
*dpif
)
230 struct odp_stats stats
;
234 COVERAGE_INC(dpif_purge
);
236 error
= dpif_get_dp_stats(dpif
, &stats
);
241 for (i
= 0; i
< stats
.max_miss_queue
+ stats
.max_action_queue
; i
++) {
243 error
= dpif_recv(dpif
, &buf
);
245 return error
== EAGAIN
? 0 : error
;
253 dpif_port_add(struct dpif
*dpif
, const char *devname
, uint16_t port_no
,
256 struct odp_port port
;
258 COVERAGE_INC(dpif_port_add
);
259 memset(&port
, 0, sizeof port
);
260 strncpy(port
.devname
, devname
, sizeof port
.devname
);
263 if (!ioctl(dpif
->fd
, ODP_PORT_ADD
, &port
)) {
264 VLOG_DBG_RL(&dpmsg_rl
, "%s: added %s as port %"PRIu16
,
265 dpif_name(dpif
), devname
, port_no
);
268 VLOG_WARN_RL(&error_rl
, "%s: failed to add %s as port %"PRIu16
": %s",
269 dpif_name(dpif
), devname
, port_no
, strerror(errno
));
275 dpif_port_del(struct dpif
*dpif
, uint16_t port_no
)
278 COVERAGE_INC(dpif_port_del
);
279 return do_ioctl(dpif
, ODP_PORT_DEL
, "ODP_PORT_DEL", &tmp
);
283 dpif_port_query_by_number(const struct dpif
*dpif
, uint16_t port_no
,
284 struct odp_port
*port
)
286 memset(port
, 0, sizeof *port
);
287 port
->port
= port_no
;
288 if (!ioctl(dpif
->fd
, ODP_PORT_QUERY
, port
)) {
289 VLOG_DBG_RL(&dpmsg_rl
, "%s: port %"PRIu16
" is device %s",
290 dpif_name(dpif
), port_no
, port
->devname
);
293 VLOG_WARN_RL(&error_rl
, "%s: failed to query port %"PRIu16
": %s",
294 dpif_name(dpif
), port_no
, strerror(errno
));
300 dpif_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
301 struct odp_port
*port
)
303 memset(port
, 0, sizeof *port
);
304 strncpy(port
->devname
, devname
, sizeof port
->devname
);
305 if (!ioctl(dpif
->fd
, ODP_PORT_QUERY
, port
)) {
306 VLOG_DBG_RL(&dpmsg_rl
, "%s: device %s is on port %"PRIu16
,
307 dpif_name(dpif
), devname
, port
->port
);
310 /* Log level is DBG here because all the current callers are interested
311 * in whether 'dpif' actually has a port 'devname', so that it's not an
312 * issue worth logging if it doesn't. */
313 VLOG_DBG_RL(&error_rl
, "%s: failed to query port %s: %s",
314 dpif_name(dpif
), devname
, strerror(errno
));
320 dpif_port_get_name(struct dpif
*dpif
, uint16_t port_no
,
321 char *name
, size_t name_size
)
323 struct odp_port port
;
326 assert(name_size
> 0);
328 error
= dpif_port_query_by_number(dpif
, port_no
, &port
);
330 ovs_strlcpy(name
, port
.devname
, name_size
);
338 dpif_port_list(const struct dpif
*dpif
,
339 struct odp_port
**ports
, size_t *n_ports
)
341 struct odp_portvec pv
;
342 struct odp_stats stats
;
346 error
= dpif_get_dp_stats(dpif
, &stats
);
351 *ports
= xcalloc(1, stats
.n_ports
* sizeof **ports
);
353 pv
.n_ports
= stats
.n_ports
;
354 error
= do_ioctl(dpif
, ODP_PORT_LIST
, "ODP_PORT_LIST", &pv
);
359 } while (pv
.n_ports
!= stats
.n_ports
);
360 *n_ports
= pv
.n_ports
;
370 dpif_port_group_set(struct dpif
*dpif
, uint16_t group
,
371 const uint16_t ports
[], size_t n_ports
)
373 struct odp_port_group pg
;
375 COVERAGE_INC(dpif_port_group_set
);
376 assert(n_ports
<= UINT16_MAX
);
378 pg
.ports
= (uint16_t *) ports
;
379 pg
.n_ports
= n_ports
;
380 return do_ioctl(dpif
, ODP_PORT_GROUP_SET
, "ODP_PORT_GROUP_SET", &pg
);
383 /* Careful: '*n_out' can be greater than 'n_ports' on return, if 'n_ports' is
384 * less than the number of ports in 'group'. */
386 dpif_port_group_get(const struct dpif
*dpif
, uint16_t group
,
387 uint16_t ports
[], size_t n_ports
, size_t *n_out
)
389 struct odp_port_group pg
;
392 assert(n_ports
<= UINT16_MAX
);
395 pg
.n_ports
= n_ports
;
396 error
= do_ioctl(dpif
, ODP_PORT_GROUP_GET
, "ODP_PORT_GROUP_GET", &pg
);
397 *n_out
= error
? 0 : pg
.n_ports
;
402 dpif_flow_flush(struct dpif
*dpif
)
404 COVERAGE_INC(dpif_flow_flush
);
405 return do_ioctl(dpif
, ODP_FLOW_FLUSH
, "ODP_FLOW_FLUSH", NULL
);
408 static enum vlog_level
409 flow_message_log_level(int error
)
411 return error
? VLL_WARN
: VLL_DBG
;
415 should_log_flow_message(int error
)
417 return !vlog_should_drop(THIS_MODULE
, flow_message_log_level(error
),
418 error
? &error_rl
: &dpmsg_rl
);
422 log_flow_message(const struct dpif
*dpif
, int error
,
423 const char *operation
,
424 const flow_t
*flow
, const struct odp_flow_stats
*stats
,
425 const union odp_action
*actions
, size_t n_actions
)
427 struct ds ds
= DS_EMPTY_INITIALIZER
;
428 ds_put_format(&ds
, "%s: ", dpif_name(dpif
));
430 ds_put_cstr(&ds
, "failed to ");
432 ds_put_format(&ds
, "%s ", operation
);
434 ds_put_format(&ds
, "(%s) ", strerror(error
));
436 flow_format(&ds
, flow
);
438 ds_put_cstr(&ds
, ", ");
439 format_odp_flow_stats(&ds
, stats
);
441 if (actions
|| n_actions
) {
442 ds_put_cstr(&ds
, ", actions:");
443 format_odp_actions(&ds
, actions
, n_actions
);
445 vlog(THIS_MODULE
, flow_message_log_level(error
), "%s", ds_cstr(&ds
));
450 do_flow_ioctl(const struct dpif
*dpif
, int cmd
, struct odp_flow
*flow
,
451 const char *operation
, bool show_stats
)
453 int error
= do_ioctl(dpif
, cmd
, NULL
, flow
);
454 if (error
&& show_stats
) {
457 if (should_log_flow_message(error
)) {
458 log_flow_message(dpif
, error
, operation
, &flow
->key
,
459 show_stats
&& !error
? &flow
->stats
: NULL
,
460 flow
->actions
, flow
->n_actions
);
466 dpif_flow_put(struct dpif
*dpif
, struct odp_flow_put
*put
)
468 int error
= do_ioctl(dpif
, ODP_FLOW_PUT
, NULL
, put
);
469 COVERAGE_INC(dpif_flow_put
);
470 if (should_log_flow_message(error
)) {
471 struct ds operation
= DS_EMPTY_INITIALIZER
;
472 ds_put_cstr(&operation
, "put");
473 if (put
->flags
& ODPPF_CREATE
) {
474 ds_put_cstr(&operation
, "[create]");
476 if (put
->flags
& ODPPF_MODIFY
) {
477 ds_put_cstr(&operation
, "[modify]");
479 if (put
->flags
& ODPPF_ZERO_STATS
) {
480 ds_put_cstr(&operation
, "[zero]");
482 #define ODPPF_ALL (ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS)
483 if (put
->flags
& ~ODPPF_ALL
) {
484 ds_put_format(&operation
, "[%x]", put
->flags
& ~ODPPF_ALL
);
486 log_flow_message(dpif
, error
, ds_cstr(&operation
), &put
->flow
.key
,
487 !error
? &put
->flow
.stats
: NULL
,
488 put
->flow
.actions
, put
->flow
.n_actions
);
489 ds_destroy(&operation
);
495 dpif_flow_del(struct dpif
*dpif
, struct odp_flow
*flow
)
497 COVERAGE_INC(dpif_flow_del
);
498 check_rw_odp_flow(flow
);
499 memset(&flow
->stats
, 0, sizeof flow
->stats
);
500 return do_flow_ioctl(dpif
, ODP_FLOW_DEL
, flow
, "delete flow", true);
504 dpif_flow_get(const struct dpif
*dpif
, struct odp_flow
*flow
)
506 COVERAGE_INC(dpif_flow_query
);
507 check_rw_odp_flow(flow
);
508 memset(&flow
->stats
, 0, sizeof flow
->stats
);
509 return do_flow_ioctl(dpif
, ODP_FLOW_GET
, flow
, "get flow", true);
513 dpif_flow_get_multiple(const struct dpif
*dpif
,
514 struct odp_flow flows
[], size_t n
)
516 struct odp_flowvec fv
;
519 COVERAGE_ADD(dpif_flow_query_multiple
, n
);
522 for (i
= 0; i
< n
; i
++) {
523 check_rw_odp_flow(&flows
[i
]);
525 return do_ioctl(dpif
, ODP_FLOW_GET_MULTIPLE
, "ODP_FLOW_GET_MULTIPLE",
530 dpif_flow_list(const struct dpif
*dpif
, struct odp_flow flows
[], size_t n
,
533 struct odp_flowvec fv
;
537 COVERAGE_INC(dpif_flow_query_list
);
540 if (RUNNING_ON_VALGRIND
) {
541 memset(flows
, 0, n
* sizeof *flows
);
543 for (i
= 0; i
< n
; i
++) {
544 flows
[i
].actions
= NULL
;
545 flows
[i
].n_actions
= 0;
548 error
= do_ioctl(dpif
, ODP_FLOW_LIST
, NULL
, &fv
);
551 VLOG_WARN_RL(&error_rl
, "%s: flow list failed (%s)",
552 dpif_name(dpif
), strerror(error
));
554 COVERAGE_ADD(dpif_flow_query_list_n
, fv
.n_flows
);
556 VLOG_DBG_RL(&dpmsg_rl
, "%s: listed %zu flows",
557 dpif_name(dpif
), *n_out
);
563 dpif_flow_list_all(const struct dpif
*dpif
,
564 struct odp_flow
**flowsp
, size_t *np
)
566 struct odp_stats stats
;
567 struct odp_flow
*flows
;
574 error
= dpif_get_dp_stats(dpif
, &stats
);
579 flows
= xmalloc(sizeof *flows
* stats
.n_flows
);
580 error
= dpif_flow_list(dpif
, flows
, stats
.n_flows
, &n_flows
);
586 if (stats
.n_flows
!= n_flows
) {
587 VLOG_WARN_RL(&error_rl
, "%s: datapath stats reported %"PRIu32
" "
588 "flows but flow listing reported %zu",
589 dpif_name(dpif
), stats
.n_flows
, n_flows
);
597 dpif_execute(struct dpif
*dpif
, uint16_t in_port
,
598 const union odp_action actions
[], size_t n_actions
,
599 const struct ofpbuf
*buf
)
603 COVERAGE_INC(dpif_execute
);
605 struct odp_execute execute
;
606 memset(&execute
, 0, sizeof execute
);
607 execute
.in_port
= in_port
;
608 execute
.actions
= (union odp_action
*) actions
;
609 execute
.n_actions
= n_actions
;
610 execute
.data
= buf
->data
;
611 execute
.length
= buf
->size
;
612 error
= do_ioctl(dpif
, ODP_EXECUTE
, NULL
, &execute
);
617 if (!(error
? VLOG_DROP_WARN(&error_rl
) : VLOG_DROP_DBG(&dpmsg_rl
))) {
618 struct ds ds
= DS_EMPTY_INITIALIZER
;
619 char *packet
= ofp_packet_to_string(buf
->data
, buf
->size
, buf
->size
);
620 ds_put_format(&ds
, "%s: execute ", dpif_name(dpif
));
621 format_odp_actions(&ds
, actions
, n_actions
);
623 ds_put_format(&ds
, " failed (%s)", strerror(error
));
625 ds_put_format(&ds
, " on packet %s", packet
);
626 vlog(THIS_MODULE
, error
? VLL_WARN
: VLL_DBG
, "%s", ds_cstr(&ds
));
634 dpif_recv(struct dpif
*dpif
, struct ofpbuf
**bufp
)
640 buf
= ofpbuf_new(65536);
641 retval
= read(dpif
->fd
, ofpbuf_tail(buf
), ofpbuf_tailroom(buf
));
644 if (error
!= EAGAIN
) {
645 VLOG_WARN_RL(&error_rl
, "%s: read failed: %s",
646 dpif_name(dpif
), strerror(error
));
648 } else if (retval
>= sizeof(struct odp_msg
)) {
649 struct odp_msg
*msg
= buf
->data
;
650 if (msg
->length
<= retval
) {
652 if (VLOG_IS_DBG_ENABLED()) {
653 void *payload
= msg
+ 1;
654 size_t length
= buf
->size
- sizeof *msg
;
655 char *s
= ofp_packet_to_string(payload
, length
, length
);
656 VLOG_DBG_RL(&dpmsg_rl
, "%s: received %s message of length "
657 "%zu on port %"PRIu16
": %s", dpif_name(dpif
),
658 (msg
->type
== _ODPL_MISS_NR
? "miss"
659 : msg
->type
== _ODPL_ACTION_NR
? "action"
661 msg
->length
- sizeof(struct odp_msg
),
666 COVERAGE_INC(dpif_recv
);
669 VLOG_WARN_RL(&error_rl
, "%s: discarding message truncated "
670 "from %zu bytes to %d",
671 dpif_name(dpif
), msg
->length
, retval
);
674 } else if (!retval
) {
675 VLOG_WARN_RL(&error_rl
, "%s: unexpected end of file", dpif_name(dpif
));
678 VLOG_WARN_RL(&error_rl
,
679 "%s: discarding too-short message (%d bytes)",
680 dpif_name(dpif
), retval
);
690 dpif_recv_wait(struct dpif
*dpif
)
692 poll_fd_wait(dpif
->fd
, POLLIN
);
696 dpif_get_netflow_ids(const struct dpif
*dpif
,
697 uint8_t *engine_type
, uint8_t *engine_id
)
699 *engine_type
= *engine_id
= dpif
->minor
;
704 struct nl_sock
*sock
;
709 dpifmon_create(const char *datapath_name
, struct dpifmon
**monp
)
712 char local_name
[IFNAMSIZ
];
715 mon
= *monp
= xmalloc(sizeof *mon
);
717 error
= dpif_open(datapath_name
, &mon
->dpif
);
721 error
= dpif_port_get_name(&mon
->dpif
, ODPP_LOCAL
,
722 local_name
, sizeof local_name
);
724 goto error_close_dpif
;
727 mon
->local_ifindex
= if_nametoindex(local_name
);
728 if (!mon
->local_ifindex
) {
730 VLOG_WARN("could not get ifindex of %s device: %s",
731 local_name
, strerror(errno
));
732 goto error_close_dpif
;
735 error
= nl_sock_create(NETLINK_ROUTE
, RTNLGRP_LINK
, 0, 0, &mon
->sock
);
737 VLOG_WARN("could not create rtnetlink socket: %s", strerror(error
));
738 goto error_close_dpif
;
744 dpif_close(&mon
->dpif
);
752 dpifmon_destroy(struct dpifmon
*mon
)
755 dpif_close(&mon
->dpif
);
756 nl_sock_destroy(mon
->sock
);
761 dpifmon_poll(struct dpifmon
*mon
, char **devnamep
)
763 static struct vlog_rate_limit slow_rl
= VLOG_RATE_LIMIT_INIT(1, 5);
764 static const struct nl_policy rtnlgrp_link_policy
[] = {
765 [IFLA_IFNAME
] = { .type
= NL_A_STRING
},
766 [IFLA_MASTER
] = { .type
= NL_A_U32
, .optional
= true },
768 struct nlattr
*attrs
[ARRAY_SIZE(rtnlgrp_link_policy
)];
774 error
= nl_sock_recv(mon
->sock
, &buf
, false);
777 if (!nl_policy_parse(buf
, NLMSG_HDRLEN
+ sizeof(struct ifinfomsg
),
779 attrs
, ARRAY_SIZE(rtnlgrp_link_policy
))) {
780 VLOG_WARN_RL(&slow_rl
, "received bad rtnl message");
783 const char *devname
= nl_attr_get_string(attrs
[IFLA_IFNAME
]);
786 if (attrs
[IFLA_MASTER
]) {
787 uint32_t master_ifindex
= nl_attr_get_u32(attrs
[IFLA_MASTER
]);
788 for_us
= master_ifindex
== mon
->local_ifindex
;
790 /* It's for us if that device is one of our ports. */
791 struct odp_port port
;
792 for_us
= !dpif_port_query_by_name(mon
->dpif
, devname
, &port
);
796 /* Not for us, try again. */
798 COVERAGE_INC(dpifmon_poll_false_wakeup
);
801 COVERAGE_INC(dpifmon_poll_changed
);
802 *devnamep
= xstrdup(devname
);
812 VLOG_WARN_RL(&slow_rl
, "dpifmon socket overflowed");
816 VLOG_WARN_RL(&slow_rl
, "error on dpifmon socket: %s", strerror(error
));
823 dpifmon_run(struct dpifmon
*mon UNUSED
)
825 /* Nothing to do in this implementation. */
829 dpifmon_wait(struct dpifmon
*mon
)
831 nl_sock_wait(mon
->sock
, POLLIN
);
834 static int get_openvswitch_major(void);
835 static int get_major(const char *target
, int default_major
);
838 lookup_minor(const char *name
, unsigned int *minor
)
840 struct ethtool_drvinfo drvinfo
;
846 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
848 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno
));
853 memset(&ifr
, 0, sizeof ifr
);
854 strncpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
855 ifr
.ifr_data
= (caddr_t
) &drvinfo
;
857 memset(&drvinfo
, 0, sizeof drvinfo
);
858 drvinfo
.cmd
= ETHTOOL_GDRVINFO
;
859 if (ioctl(sock
, SIOCETHTOOL
, &ifr
)) {
860 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno
));
862 goto error_close_sock
;
865 if (strcmp(drvinfo
.driver
, "openvswitch")) {
866 VLOG_WARN("%s is not an openvswitch device", name
);
868 goto error_close_sock
;
871 if (!isdigit(drvinfo
.bus_info
[0])) {
872 VLOG_WARN("%s ethtool info does not contain an openvswitch minor",
875 goto error_close_sock
;
878 *minor
= atoi(drvinfo
.bus_info
);
889 make_openvswitch_device(unsigned int minor
, char **fnp
)
891 dev_t dev
= makedev(get_openvswitch_major(), minor
);
892 const char dirname
[] = "/dev/net";
897 sprintf(fn
, "%s/dp%d", dirname
, minor
);
899 if (!S_ISCHR(s
.st_mode
)) {
900 VLOG_WARN_RL(&error_rl
, "%s is not a character device, fixing",
902 } else if (s
.st_rdev
!= dev
) {
903 VLOG_WARN_RL(&error_rl
,
904 "%s is device %u:%u instead of %u:%u, fixing",
905 fn
, major(s
.st_rdev
), minor(s
.st_rdev
),
906 major(dev
), minor(dev
));
911 VLOG_WARN_RL(&error_rl
, "%s: unlink failed (%s)",
912 fn
, strerror(errno
));
915 } else if (errno
== ENOENT
) {
916 if (stat(dirname
, &s
)) {
917 if (errno
== ENOENT
) {
918 if (mkdir(dirname
, 0755)) {
919 VLOG_WARN_RL(&error_rl
, "%s: mkdir failed (%s)",
920 dirname
, strerror(errno
));
924 VLOG_WARN_RL(&error_rl
, "%s: stat failed (%s)",
925 dirname
, strerror(errno
));
930 VLOG_WARN_RL(&error_rl
, "%s: stat failed (%s)", fn
, strerror(errno
));
934 /* The device needs to be created. */
935 if (mknod(fn
, S_IFCHR
| 0700, dev
)) {
936 VLOG_WARN_RL(&error_rl
,
937 "%s: creating character device %u:%u failed (%s)",
938 fn
, major(dev
), minor(dev
), strerror(errno
));
949 get_openvswitch_major(void)
951 static unsigned int openvswitch_major
;
952 if (!openvswitch_major
) {
953 enum { DEFAULT_MAJOR
= 248 };
954 openvswitch_major
= get_major("openvswitch", DEFAULT_MAJOR
);
956 return openvswitch_major
;
960 get_major(const char *target
, int default_major
)
962 const char fn
[] = "/proc/devices";
967 file
= fopen(fn
, "r");
969 VLOG_ERR("opening %s failed (%s)", fn
, strerror(errno
));
973 for (ln
= 1; fgets(line
, sizeof line
, file
); ln
++) {
977 if (!strncmp(line
, "Character", 9) || line
[0] == '\0') {
979 } else if (!strncmp(line
, "Block", 5)) {
980 /* We only want character devices, so skip the rest of the file. */
982 } else if (sscanf(line
, "%d %63s", &major
, name
)) {
983 if (!strcmp(name
, target
)) {
990 VLOG_WARN("%s:%d: syntax error", fn
, ln
);
996 VLOG_ERR("%s: %s major not found (is the module loaded?), using "
997 "default major %d", fn
, target
, default_major
);
999 VLOG_INFO("using default major %d for %s", default_major
, target
);
1000 return default_major
;
1004 name_to_minor(const char *name
, unsigned int *minor
)
1006 if (!get_minor_from_name(name
, minor
)) {
1009 return lookup_minor(name
, minor
);
1013 get_minor_from_name(const char *name
, unsigned int *minor
)
1015 if (!strncmp(name
, "dp", 2) && isdigit(name
[2])) {
1016 *minor
= atoi(name
+ 2);
1018 } else if (!strncmp(name
, "nl:", 3) && isdigit(name
[3])) {
1019 /* This is for compatibility only and will be dropped. */
1020 *minor
= atoi(name
+ 3);
1028 open_by_minor(unsigned int minor
, struct dpif
*dpif
)
1036 error
= make_openvswitch_device(minor
, &fn
);
1041 fd
= open(fn
, O_RDONLY
| O_NONBLOCK
);
1044 VLOG_WARN("%s: open failed (%s)", fn
, strerror(error
));
1050 dpif
->name
= xasprintf("dp%u", dpif
->minor
);
1051 dpif
->minor
= minor
;
1056 /* There is a tendency to construct odp_flow objects on the stack and to
1057 * forget to properly initialize their "actions" and "n_actions" members.
1058 * When this happens, we get memory corruption because the kernel
1059 * writes through the random pointer that is in the "actions" member.
1061 * This function attempts to combat the problem by:
1063 * - Forcing a segfault if "actions" points to an invalid region (instead
1064 * of just getting back EFAULT, which can be easily missed in the log).
1066 * - Storing a distinctive value that is likely to cause an
1067 * easy-to-identify error later if it is dereferenced, etc.
1069 * - Triggering a warning on uninitialized memory from Valgrind if
1070 * "actions" or "n_actions" was not initialized.
1073 check_rw_odp_flow(struct odp_flow
*flow
)
1075 if (flow
->n_actions
) {
1076 memset(&flow
->actions
[0], 0xcc, sizeof flow
->actions
[0]);