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 /* A datapath interface. */
59 /* Rate limit for individual messages going to or from the datapath, output at
60 * DBG level. This is very high because, if these are enabled, it is because
61 * we really need to see them. */
62 static struct vlog_rate_limit dpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
64 /* Not really much point in logging many dpif errors. */
65 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
67 static int get_minor_from_name(const char *name, unsigned int *minor);
68 static int name_to_minor(const char *name, unsigned int *minor);
69 static int lookup_minor(const char *name, unsigned int *minor);
70 static int open_by_minor(unsigned int minor, struct dpif **dpifp);
71 static int make_openvswitch_device(unsigned int minor, char **fnp);
72 static void check_rw_odp_flow(struct odp_flow *);
75 dpif_open(const char *name, struct dpif **dpifp)
84 error = name_to_minor(name, &minor);
89 error = open_by_minor(minor, &dpif);
94 /* We can open the device, but that doesn't mean that it's been created.
95 * If it hasn't been, then any command other than ODP_DP_CREATE will
96 * return ENODEV. Try something innocuous. */
97 listen_mask = 0; /* Make Valgrind happy. */
98 if (ioctl(dpif->fd, ODP_GET_LISTEN_MASK, &listen_mask)) {
100 if (error != ENODEV) {
101 VLOG_WARN("%s: probe returned unexpected error: %s",
102 dpif_name(dpif), strerror(error));
112 dpif_close(struct dpif *dpif)
122 do_ioctl(const struct dpif *dpif, int cmd, const char *cmd_name,
125 int error = ioctl(dpif->fd, cmd, arg) ? errno : 0;
128 VLOG_WARN_RL(&error_rl, "%s: ioctl(%s) failed (%s)",
129 dpif_name(dpif), cmd_name, strerror(error));
131 VLOG_DBG_RL(&dpmsg_rl, "%s: ioctl(%s): success",
132 dpif_name(dpif), cmd_name);
139 dpif_create(const char *name, struct dpif **dpifp)
145 if (!get_minor_from_name(name, &minor)) {
146 /* Minor was specified in 'name', go ahead and create it. */
149 error = open_by_minor(minor, &dpif);
154 if (!strncmp(name, "nl:", 3)) {
156 sprintf(devname, "of%u", minor);
157 error = ioctl(dpif->fd, ODP_DP_CREATE, devname) < 0 ? errno : 0;
159 error = ioctl(dpif->fd, ODP_DP_CREATE, name) < 0 ? errno : 0;
168 for (minor = 0; minor < ODP_MAX; minor++) {
171 error = open_by_minor(minor, &dpif);
176 error = ioctl(dpif->fd, ODP_DP_CREATE, name) < 0 ? errno : 0;
182 if (error != EBUSY) {
191 dpif_name(const struct dpif *dpif)
197 dpif_delete(struct dpif *dpif)
199 COVERAGE_INC(dpif_destroy);
200 return do_ioctl(dpif, ODP_DP_DESTROY, "ODP_DP_DESTROY", NULL);
204 dpif_get_dp_stats(const struct dpif *dpif, struct odp_stats *stats)
206 memset(stats, 0, sizeof *stats);
207 return do_ioctl(dpif, ODP_DP_STATS, "ODP_DP_STATS", stats);
211 dpif_get_drop_frags(const struct dpif *dpif, bool *drop_frags)
214 int error = do_ioctl(dpif, ODP_GET_DROP_FRAGS, "ODP_GET_DROP_FRAGS", &tmp);
215 *drop_frags = error ? tmp & 1 : false;
220 dpif_set_drop_frags(struct dpif *dpif, bool drop_frags)
222 int tmp = drop_frags;
223 return do_ioctl(dpif, ODP_SET_DROP_FRAGS, "ODP_SET_DROP_FRAGS", &tmp);
227 dpif_get_listen_mask(const struct dpif *dpif, int *listen_mask)
229 int error = do_ioctl(dpif, ODP_GET_LISTEN_MASK, "ODP_GET_LISTEN_MASK",
238 dpif_set_listen_mask(struct dpif *dpif, int listen_mask)
240 return do_ioctl(dpif, ODP_SET_LISTEN_MASK, "ODP_SET_LISTEN_MASK",
245 dpif_purge(struct dpif *dpif)
247 struct odp_stats stats;
251 COVERAGE_INC(dpif_purge);
253 error = dpif_get_dp_stats(dpif, &stats);
258 for (i = 0; i < stats.max_miss_queue + stats.max_action_queue; i++) {
260 error = dpif_recv(dpif, &buf);
262 return error == EAGAIN ? 0 : error;
270 dpif_port_add(struct dpif *dpif, const char *devname, uint16_t port_no,
273 struct odp_port port;
275 COVERAGE_INC(dpif_port_add);
276 memset(&port, 0, sizeof port);
277 strncpy(port.devname, devname, sizeof port.devname);
280 if (!ioctl(dpif->fd, ODP_PORT_ADD, &port)) {
281 VLOG_DBG_RL(&dpmsg_rl, "%s: added %s as port %"PRIu16,
282 dpif_name(dpif), devname, port_no);
285 VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port %"PRIu16": %s",
286 dpif_name(dpif), devname, port_no, strerror(errno));
292 dpif_port_del(struct dpif *dpif, uint16_t port_no)
295 COVERAGE_INC(dpif_port_del);
296 return do_ioctl(dpif, ODP_PORT_DEL, "ODP_PORT_DEL", &tmp);
300 dpif_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
301 struct odp_port *port)
303 memset(port, 0, sizeof *port);
304 port->port = port_no;
305 if (!ioctl(dpif->fd, ODP_PORT_QUERY, port)) {
306 VLOG_DBG_RL(&dpmsg_rl, "%s: port %"PRIu16" is device %s",
307 dpif_name(dpif), port_no, port->devname);
310 VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu16": %s",
311 dpif_name(dpif), port_no, strerror(errno));
317 dpif_port_query_by_name(const struct dpif *dpif, const char *devname,
318 struct odp_port *port)
320 memset(port, 0, sizeof *port);
321 strncpy(port->devname, devname, sizeof port->devname);
322 if (!ioctl(dpif->fd, ODP_PORT_QUERY, port)) {
323 VLOG_DBG_RL(&dpmsg_rl, "%s: device %s is on port %"PRIu16,
324 dpif_name(dpif), devname, port->port);
327 /* Log level is DBG here because all the current callers are interested
328 * in whether 'dpif' actually has a port 'devname', so that it's not an
329 * issue worth logging if it doesn't. */
330 VLOG_DBG_RL(&error_rl, "%s: failed to query port %s: %s",
331 dpif_name(dpif), devname, strerror(errno));
337 dpif_port_get_name(struct dpif *dpif, uint16_t port_no,
338 char *name, size_t name_size)
340 struct odp_port port;
343 assert(name_size > 0);
345 error = dpif_port_query_by_number(dpif, port_no, &port);
347 ovs_strlcpy(name, port.devname, name_size);
355 dpif_port_list(const struct dpif *dpif,
356 struct odp_port **ports, size_t *n_ports)
358 struct odp_portvec pv;
359 struct odp_stats stats;
363 error = dpif_get_dp_stats(dpif, &stats);
368 *ports = xcalloc(1, stats.n_ports * sizeof **ports);
370 pv.n_ports = stats.n_ports;
371 error = do_ioctl(dpif, ODP_PORT_LIST, "ODP_PORT_LIST", &pv);
376 } while (pv.n_ports != stats.n_ports);
377 *n_ports = pv.n_ports;
387 dpif_port_group_set(struct dpif *dpif, uint16_t group,
388 const uint16_t ports[], size_t n_ports)
390 struct odp_port_group pg;
392 COVERAGE_INC(dpif_port_group_set);
393 assert(n_ports <= UINT16_MAX);
395 pg.ports = (uint16_t *) ports;
396 pg.n_ports = n_ports;
397 return do_ioctl(dpif, ODP_PORT_GROUP_SET, "ODP_PORT_GROUP_SET", &pg);
400 /* Careful: '*n_out' can be greater than 'n_ports' on return, if 'n_ports' is
401 * less than the number of ports in 'group'. */
403 dpif_port_group_get(const struct dpif *dpif, uint16_t group,
404 uint16_t ports[], size_t n_ports, size_t *n_out)
406 struct odp_port_group pg;
409 assert(n_ports <= UINT16_MAX);
412 pg.n_ports = n_ports;
413 error = do_ioctl(dpif, ODP_PORT_GROUP_GET, "ODP_PORT_GROUP_GET", &pg);
414 *n_out = error ? 0 : pg.n_ports;
419 dpif_flow_flush(struct dpif *dpif)
421 COVERAGE_INC(dpif_flow_flush);
422 return do_ioctl(dpif, ODP_FLOW_FLUSH, "ODP_FLOW_FLUSH", NULL);
425 static enum vlog_level
426 flow_message_log_level(int error)
428 return error ? VLL_WARN : VLL_DBG;
432 should_log_flow_message(int error)
434 return !vlog_should_drop(THIS_MODULE, flow_message_log_level(error),
435 error ? &error_rl : &dpmsg_rl);
439 log_flow_message(const struct dpif *dpif, int error,
440 const char *operation,
441 const flow_t *flow, const struct odp_flow_stats *stats,
442 const union odp_action *actions, size_t n_actions)
444 struct ds ds = DS_EMPTY_INITIALIZER;
445 ds_put_format(&ds, "%s: ", dpif_name(dpif));
447 ds_put_cstr(&ds, "failed to ");
449 ds_put_format(&ds, "%s ", operation);
451 ds_put_format(&ds, "(%s) ", strerror(error));
453 flow_format(&ds, flow);
455 ds_put_cstr(&ds, ", ");
456 format_odp_flow_stats(&ds, stats);
458 if (actions || n_actions) {
459 ds_put_cstr(&ds, ", actions:");
460 format_odp_actions(&ds, actions, n_actions);
462 vlog(THIS_MODULE, flow_message_log_level(error), "%s", ds_cstr(&ds));
467 do_flow_ioctl(const struct dpif *dpif, int cmd, struct odp_flow *flow,
468 const char *operation, bool show_stats)
470 int error = do_ioctl(dpif, cmd, NULL, flow);
471 if (error && show_stats) {
474 if (should_log_flow_message(error)) {
475 log_flow_message(dpif, error, operation, &flow->key,
476 show_stats && !error ? &flow->stats : NULL,
477 flow->actions, flow->n_actions);
483 dpif_flow_put(struct dpif *dpif, struct odp_flow_put *put)
485 int error = do_ioctl(dpif, ODP_FLOW_PUT, NULL, put);
486 COVERAGE_INC(dpif_flow_put);
487 if (should_log_flow_message(error)) {
488 struct ds operation = DS_EMPTY_INITIALIZER;
489 ds_put_cstr(&operation, "put");
490 if (put->flags & ODPPF_CREATE) {
491 ds_put_cstr(&operation, "[create]");
493 if (put->flags & ODPPF_MODIFY) {
494 ds_put_cstr(&operation, "[modify]");
496 if (put->flags & ODPPF_ZERO_STATS) {
497 ds_put_cstr(&operation, "[zero]");
499 #define ODPPF_ALL (ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS)
500 if (put->flags & ~ODPPF_ALL) {
501 ds_put_format(&operation, "[%x]", put->flags & ~ODPPF_ALL);
503 log_flow_message(dpif, error, ds_cstr(&operation), &put->flow.key,
504 !error ? &put->flow.stats : NULL,
505 put->flow.actions, put->flow.n_actions);
506 ds_destroy(&operation);
512 dpif_flow_del(struct dpif *dpif, struct odp_flow *flow)
514 COVERAGE_INC(dpif_flow_del);
515 check_rw_odp_flow(flow);
516 memset(&flow->stats, 0, sizeof flow->stats);
517 return do_flow_ioctl(dpif, ODP_FLOW_DEL, flow, "delete flow", true);
521 dpif_flow_get(const struct dpif *dpif, struct odp_flow *flow)
523 COVERAGE_INC(dpif_flow_query);
524 check_rw_odp_flow(flow);
525 memset(&flow->stats, 0, sizeof flow->stats);
526 return do_flow_ioctl(dpif, ODP_FLOW_GET, flow, "get flow", true);
530 dpif_flow_get_multiple(const struct dpif *dpif,
531 struct odp_flow flows[], size_t n)
533 struct odp_flowvec fv;
536 COVERAGE_ADD(dpif_flow_query_multiple, n);
539 for (i = 0; i < n; i++) {
540 check_rw_odp_flow(&flows[i]);
542 return do_ioctl(dpif, ODP_FLOW_GET_MULTIPLE, "ODP_FLOW_GET_MULTIPLE",
547 dpif_flow_list(const struct dpif *dpif, struct odp_flow flows[], size_t n,
550 struct odp_flowvec fv;
554 COVERAGE_INC(dpif_flow_query_list);
557 if (RUNNING_ON_VALGRIND) {
558 memset(flows, 0, n * sizeof *flows);
560 for (i = 0; i < n; i++) {
561 flows[i].actions = NULL;
562 flows[i].n_actions = 0;
565 error = do_ioctl(dpif, ODP_FLOW_LIST, NULL, &fv);
568 VLOG_WARN_RL(&error_rl, "%s: flow list failed (%s)",
569 dpif_name(dpif), strerror(error));
571 COVERAGE_ADD(dpif_flow_query_list_n, fv.n_flows);
573 VLOG_DBG_RL(&dpmsg_rl, "%s: listed %zu flows",
574 dpif_name(dpif), *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, "%s: datapath stats reported %"PRIu32" "
605 "flows but flow listing reported %zu",
606 dpif_name(dpif), 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, "%s: execute ", dpif_name(dpif));
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, "%s: read failed: %s",
663 dpif_name(dpif), 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, "%s: received %s message of length "
674 "%zu on port %"PRIu16": %s", dpif_name(dpif),
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, "%s: discarding message truncated "
687 "from %zu bytes to %d",
688 dpif_name(dpif), msg->length, retval);
691 } else if (!retval) {
692 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif));
695 VLOG_WARN_RL(&error_rl,
696 "%s: discarding too-short message (%d bytes)",
697 dpif_name(dpif), retval);
707 dpif_recv_wait(struct dpif *dpif)
709 poll_fd_wait(dpif->fd, POLLIN);
713 dpif_get_netflow_ids(const struct dpif *dpif,
714 uint8_t *engine_type, uint8_t *engine_id)
716 *engine_type = *engine_id = dpif->minor;
721 struct nl_sock *sock;
726 dpifmon_create(const char *datapath_name, struct dpifmon **monp)
729 char local_name[IFNAMSIZ];
732 mon = *monp = xmalloc(sizeof *mon);
734 error = dpif_open(datapath_name, &mon->dpif);
738 error = dpif_port_get_name(mon->dpif, ODPP_LOCAL,
739 local_name, sizeof local_name);
741 goto error_close_dpif;
744 mon->local_ifindex = if_nametoindex(local_name);
745 if (!mon->local_ifindex) {
747 VLOG_WARN("could not get ifindex of %s device: %s",
748 local_name, strerror(errno));
749 goto error_close_dpif;
752 error = nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0, &mon->sock);
754 VLOG_WARN("could not create rtnetlink socket: %s", strerror(error));
755 goto error_close_dpif;
761 dpif_close(mon->dpif);
769 dpifmon_destroy(struct dpifmon *mon)
772 dpif_close(mon->dpif);
773 nl_sock_destroy(mon->sock);
778 dpifmon_poll(struct dpifmon *mon, char **devnamep)
780 static struct vlog_rate_limit slow_rl = VLOG_RATE_LIMIT_INIT(1, 5);
781 static const struct nl_policy rtnlgrp_link_policy[] = {
782 [IFLA_IFNAME] = { .type = NL_A_STRING },
783 [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
785 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
791 error = nl_sock_recv(mon->sock, &buf, false);
794 if (!nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
796 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
797 VLOG_WARN_RL(&slow_rl, "received bad rtnl message");
800 const char *devname = nl_attr_get_string(attrs[IFLA_IFNAME]);
803 if (attrs[IFLA_MASTER]) {
804 uint32_t master_ifindex = nl_attr_get_u32(attrs[IFLA_MASTER]);
805 for_us = master_ifindex == mon->local_ifindex;
807 /* It's for us if that device is one of our ports. */
808 struct odp_port port;
809 for_us = !dpif_port_query_by_name(mon->dpif, devname, &port);
813 /* Not for us, try again. */
815 COVERAGE_INC(dpifmon_poll_false_wakeup);
818 COVERAGE_INC(dpifmon_poll_changed);
819 *devnamep = xstrdup(devname);
829 VLOG_WARN_RL(&slow_rl, "dpifmon socket overflowed");
833 VLOG_WARN_RL(&slow_rl, "error on dpifmon socket: %s", strerror(error));
840 dpifmon_run(struct dpifmon *mon UNUSED)
842 /* Nothing to do in this implementation. */
846 dpifmon_wait(struct dpifmon *mon)
848 nl_sock_wait(mon->sock, POLLIN);
851 static int get_openvswitch_major(void);
852 static int get_major(const char *target, int default_major);
855 lookup_minor(const char *name, unsigned int *minor)
857 struct ethtool_drvinfo drvinfo;
863 sock = socket(AF_INET, SOCK_DGRAM, 0);
865 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
870 memset(&ifr, 0, sizeof ifr);
871 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
872 ifr.ifr_data = (caddr_t) &drvinfo;
874 memset(&drvinfo, 0, sizeof drvinfo);
875 drvinfo.cmd = ETHTOOL_GDRVINFO;
876 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
877 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
879 goto error_close_sock;
882 if (strcmp(drvinfo.driver, "openvswitch")) {
883 VLOG_WARN("%s is not an openvswitch device", name);
885 goto error_close_sock;
888 if (!isdigit(drvinfo.bus_info[0])) {
889 VLOG_WARN("%s ethtool info does not contain an openvswitch minor",
892 goto error_close_sock;
895 *minor = atoi(drvinfo.bus_info);
906 make_openvswitch_device(unsigned int minor, char **fnp)
908 dev_t dev = makedev(get_openvswitch_major(), minor);
909 const char dirname[] = "/dev/net";
914 sprintf(fn, "%s/dp%d", dirname, minor);
916 if (!S_ISCHR(s.st_mode)) {
917 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
919 } else if (s.st_rdev != dev) {
920 VLOG_WARN_RL(&error_rl,
921 "%s is device %u:%u instead of %u:%u, fixing",
922 fn, major(s.st_rdev), minor(s.st_rdev),
923 major(dev), minor(dev));
928 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
929 fn, strerror(errno));
932 } else if (errno == ENOENT) {
933 if (stat(dirname, &s)) {
934 if (errno == ENOENT) {
935 if (mkdir(dirname, 0755)) {
936 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
937 dirname, strerror(errno));
941 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
942 dirname, strerror(errno));
947 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
951 /* The device needs to be created. */
952 if (mknod(fn, S_IFCHR | 0700, dev)) {
953 VLOG_WARN_RL(&error_rl,
954 "%s: creating character device %u:%u failed (%s)",
955 fn, major(dev), minor(dev), strerror(errno));
966 get_openvswitch_major(void)
968 static unsigned int openvswitch_major;
969 if (!openvswitch_major) {
970 enum { DEFAULT_MAJOR = 248 };
971 openvswitch_major = get_major("openvswitch", DEFAULT_MAJOR);
973 return openvswitch_major;
977 get_major(const char *target, int default_major)
979 const char fn[] = "/proc/devices";
984 file = fopen(fn, "r");
986 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
990 for (ln = 1; fgets(line, sizeof line, file); ln++) {
994 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
996 } else if (!strncmp(line, "Block", 5)) {
997 /* We only want character devices, so skip the rest of the file. */
999 } else if (sscanf(line, "%d %63s", &major, name)) {
1000 if (!strcmp(name, target)) {
1007 VLOG_WARN("%s:%d: syntax error", fn, ln);
1013 VLOG_ERR("%s: %s major not found (is the module loaded?), using "
1014 "default major %d", fn, target, default_major);
1016 VLOG_INFO("using default major %d for %s", default_major, target);
1017 return default_major;
1021 name_to_minor(const char *name, unsigned int *minor)
1023 if (!get_minor_from_name(name, minor)) {
1026 return lookup_minor(name, minor);
1030 get_minor_from_name(const char *name, unsigned int *minor)
1032 if (!strncmp(name, "dp", 2) && isdigit(name[2])) {
1033 *minor = atoi(name + 2);
1035 } else if (!strncmp(name, "nl:", 3) && isdigit(name[3])) {
1036 /* This is for compatibility only and will be dropped. */
1037 *minor = atoi(name + 3);
1045 open_by_minor(unsigned int minor, struct dpif **dpifp)
1053 error = make_openvswitch_device(minor, &fn);
1058 fd = open(fn, O_RDONLY | O_NONBLOCK);
1061 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
1067 dpif = xmalloc(sizeof *dpif);
1068 dpif->name = xasprintf("dp%u", dpif->minor);
1069 dpif->minor = minor;
1075 /* There is a tendency to construct odp_flow objects on the stack and to
1076 * forget to properly initialize their "actions" and "n_actions" members.
1077 * When this happens, we get memory corruption because the kernel
1078 * writes through the random pointer that is in the "actions" member.
1080 * This function attempts to combat the problem by:
1082 * - Forcing a segfault if "actions" points to an invalid region (instead
1083 * of just getting back EFAULT, which can be easily missed in the log).
1085 * - Storing a distinctive value that is likely to cause an
1086 * easy-to-identify error later if it is dereferenced, etc.
1088 * - Triggering a warning on uninitialized memory from Valgrind if
1089 * "actions" or "n_actions" was not initialized.
1092 check_rw_odp_flow(struct odp_flow *flow)
1094 if (flow->n_actions) {
1095 memset(&flow->actions[0], 0xcc, sizeof flow->actions[0]);