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 error = ioctl(dpif->fd, ODP_DP_CREATE, name) < 0 ? errno : 0;
162 for (minor = 0; minor < ODP_MAX; minor++) {
165 error = open_by_minor(minor, &dpif);
170 error = ioctl(dpif->fd, ODP_DP_CREATE, name) < 0 ? errno : 0;
176 if (error != EBUSY) {
185 dpif_name(const struct dpif *dpif)
191 dpif_delete(struct dpif *dpif)
193 COVERAGE_INC(dpif_destroy);
194 return do_ioctl(dpif, ODP_DP_DESTROY, "ODP_DP_DESTROY", NULL);
198 dpif_get_dp_stats(const struct dpif *dpif, struct odp_stats *stats)
200 memset(stats, 0, sizeof *stats);
201 return do_ioctl(dpif, ODP_DP_STATS, "ODP_DP_STATS", stats);
205 dpif_get_drop_frags(const struct dpif *dpif, bool *drop_frags)
208 int error = do_ioctl(dpif, ODP_GET_DROP_FRAGS, "ODP_GET_DROP_FRAGS", &tmp);
209 *drop_frags = error ? tmp & 1 : false;
214 dpif_set_drop_frags(struct dpif *dpif, bool drop_frags)
216 int tmp = drop_frags;
217 return do_ioctl(dpif, ODP_SET_DROP_FRAGS, "ODP_SET_DROP_FRAGS", &tmp);
221 dpif_recv_purge(struct dpif *dpif)
223 struct odp_stats stats;
227 COVERAGE_INC(dpif_purge);
229 error = dpif_get_dp_stats(dpif, &stats);
234 for (i = 0; i < stats.max_miss_queue + stats.max_action_queue; i++) {
236 error = dpif_recv(dpif, &buf);
238 return error == EAGAIN ? 0 : error;
246 dpif_port_add(struct dpif *dpif, const char *devname, uint16_t flags,
249 struct odp_port port;
253 COVERAGE_INC(dpif_port_add);
255 memset(&port, 0, sizeof port);
256 strncpy(port.devname, devname, sizeof port.devname);
259 error = do_ioctl(dpif, ODP_PORT_ADD, NULL, &port);
262 VLOG_DBG_RL(&dpmsg_rl, "%s: added %s as port %"PRIu16,
263 dpif_name(dpif), devname, port_no);
265 port_no = UINT16_MAX;
266 VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
267 dpif_name(dpif), devname, strerror(errno));
276 dpif_port_del(struct dpif *dpif, uint16_t port_no)
279 COVERAGE_INC(dpif_port_del);
280 return do_ioctl(dpif, ODP_PORT_DEL, "ODP_PORT_DEL", &tmp);
284 dpif_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
285 struct odp_port *port)
287 memset(port, 0, sizeof *port);
288 port->port = port_no;
289 if (!ioctl(dpif->fd, ODP_PORT_QUERY, port)) {
290 VLOG_DBG_RL(&dpmsg_rl, "%s: port %"PRIu16" is device %s",
291 dpif_name(dpif), port_no, port->devname);
294 VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu16": %s",
295 dpif_name(dpif), port_no, strerror(errno));
301 dpif_port_query_by_name(const struct dpif *dpif, const char *devname,
302 struct odp_port *port)
304 memset(port, 0, sizeof *port);
305 strncpy(port->devname, devname, sizeof port->devname);
306 if (!ioctl(dpif->fd, ODP_PORT_QUERY, port)) {
307 VLOG_DBG_RL(&dpmsg_rl, "%s: device %s is on port %"PRIu16,
308 dpif_name(dpif), devname, port->port);
311 /* Log level is DBG here because all the current callers are interested
312 * in whether 'dpif' actually has a port 'devname', so that it's not an
313 * issue worth logging if it doesn't. */
314 VLOG_DBG_RL(&error_rl, "%s: failed to query port %s: %s",
315 dpif_name(dpif), devname, strerror(errno));
321 dpif_port_get_name(struct dpif *dpif, uint16_t port_no,
322 char *name, size_t name_size)
324 struct odp_port port;
327 assert(name_size > 0);
329 error = dpif_port_query_by_number(dpif, port_no, &port);
331 ovs_strlcpy(name, port.devname, name_size);
339 dpif_port_list(const struct dpif *dpif,
340 struct odp_port **ports, size_t *n_ports)
342 struct odp_portvec pv;
343 struct odp_stats stats;
347 error = dpif_get_dp_stats(dpif, &stats);
352 *ports = xcalloc(1, stats.n_ports * sizeof **ports);
354 pv.n_ports = stats.n_ports;
355 error = do_ioctl(dpif, ODP_PORT_LIST, "ODP_PORT_LIST", &pv);
360 } while (pv.n_ports != stats.n_ports);
361 *n_ports = pv.n_ports;
371 dpif_port_group_set(struct dpif *dpif, uint16_t group,
372 const uint16_t ports[], size_t n_ports)
374 struct odp_port_group pg;
376 COVERAGE_INC(dpif_port_group_set);
377 assert(n_ports <= UINT16_MAX);
379 pg.ports = (uint16_t *) ports;
380 pg.n_ports = n_ports;
381 return do_ioctl(dpif, ODP_PORT_GROUP_SET, "ODP_PORT_GROUP_SET", &pg);
385 dpif_port_group_get(const struct dpif *dpif, uint16_t group,
386 uint16_t **ports, size_t *n_ports)
393 struct odp_port_group pg;
396 pg.n_ports = *n_ports;
398 error = do_ioctl(dpif, ODP_PORT_GROUP_GET, "ODP_PORT_GROUP_GET", &pg);
405 } else if (pg.n_ports <= *n_ports) {
407 *n_ports = pg.n_ports;
410 /* Soft error: there were more ports than we expected in the
411 * group. Try again. */
413 *ports = xcalloc(pg.n_ports, sizeof **ports);
414 *n_ports = pg.n_ports;
421 dpif_flow_flush(struct dpif *dpif)
423 COVERAGE_INC(dpif_flow_flush);
424 return do_ioctl(dpif, ODP_FLOW_FLUSH, "ODP_FLOW_FLUSH", NULL);
427 static enum vlog_level
428 flow_message_log_level(int error)
430 return error ? VLL_WARN : VLL_DBG;
434 should_log_flow_message(int error)
436 return !vlog_should_drop(THIS_MODULE, flow_message_log_level(error),
437 error ? &error_rl : &dpmsg_rl);
441 log_flow_message(const struct dpif *dpif, int error,
442 const char *operation,
443 const flow_t *flow, const struct odp_flow_stats *stats,
444 const union odp_action *actions, size_t n_actions)
446 struct ds ds = DS_EMPTY_INITIALIZER;
447 ds_put_format(&ds, "%s: ", dpif_name(dpif));
449 ds_put_cstr(&ds, "failed to ");
451 ds_put_format(&ds, "%s ", operation);
453 ds_put_format(&ds, "(%s) ", strerror(error));
455 flow_format(&ds, flow);
457 ds_put_cstr(&ds, ", ");
458 format_odp_flow_stats(&ds, stats);
460 if (actions || n_actions) {
461 ds_put_cstr(&ds, ", actions:");
462 format_odp_actions(&ds, actions, n_actions);
464 vlog(THIS_MODULE, flow_message_log_level(error), "%s", ds_cstr(&ds));
469 do_flow_ioctl(const struct dpif *dpif, int cmd, struct odp_flow *flow,
470 const char *operation, bool show_stats)
472 int error = do_ioctl(dpif, cmd, NULL, flow);
473 if (error && show_stats) {
476 if (should_log_flow_message(error)) {
477 log_flow_message(dpif, error, operation, &flow->key,
478 show_stats && !error ? &flow->stats : NULL,
479 flow->actions, flow->n_actions);
485 dpif_flow_put(struct dpif *dpif, struct odp_flow_put *put)
487 int error = do_ioctl(dpif, ODP_FLOW_PUT, NULL, put);
488 COVERAGE_INC(dpif_flow_put);
489 if (should_log_flow_message(error)) {
490 struct ds operation = DS_EMPTY_INITIALIZER;
491 ds_put_cstr(&operation, "put");
492 if (put->flags & ODPPF_CREATE) {
493 ds_put_cstr(&operation, "[create]");
495 if (put->flags & ODPPF_MODIFY) {
496 ds_put_cstr(&operation, "[modify]");
498 if (put->flags & ODPPF_ZERO_STATS) {
499 ds_put_cstr(&operation, "[zero]");
501 #define ODPPF_ALL (ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS)
502 if (put->flags & ~ODPPF_ALL) {
503 ds_put_format(&operation, "[%x]", put->flags & ~ODPPF_ALL);
505 log_flow_message(dpif, error, ds_cstr(&operation), &put->flow.key,
506 !error ? &put->flow.stats : NULL,
507 put->flow.actions, put->flow.n_actions);
508 ds_destroy(&operation);
514 dpif_flow_del(struct dpif *dpif, struct odp_flow *flow)
516 COVERAGE_INC(dpif_flow_del);
517 check_rw_odp_flow(flow);
518 memset(&flow->stats, 0, sizeof flow->stats);
519 return do_flow_ioctl(dpif, ODP_FLOW_DEL, flow, "delete flow", true);
523 dpif_flow_get(const struct dpif *dpif, struct odp_flow *flow)
525 struct odp_flowvec fv;
528 COVERAGE_INC(dpif_flow_get);
530 check_rw_odp_flow(flow);
533 error = do_ioctl(dpif, ODP_FLOW_GET, "ODP_FLOW_GET", &fv);
535 error = flow->stats.error;
537 VLOG_WARN_RL(&error_rl, "%s: ioctl(ODP_FLOW_GET) failed (%s)",
538 dpif_name(dpif), strerror(error));
545 dpif_flow_get_multiple(const struct dpif *dpif,
546 struct odp_flow flows[], size_t n)
548 struct odp_flowvec fv;
551 COVERAGE_ADD(dpif_flow_query_multiple, n);
554 for (i = 0; i < n; i++) {
555 check_rw_odp_flow(&flows[i]);
557 return do_ioctl(dpif, ODP_FLOW_GET, "ODP_FLOW_GET",
562 dpif_flow_list(const struct dpif *dpif, struct odp_flow flows[], size_t n,
565 struct odp_flowvec fv;
569 COVERAGE_INC(dpif_flow_query_list);
572 if (RUNNING_ON_VALGRIND) {
573 memset(flows, 0, n * sizeof *flows);
575 for (i = 0; i < n; i++) {
576 flows[i].actions = NULL;
577 flows[i].n_actions = 0;
580 error = do_ioctl(dpif, ODP_FLOW_LIST, NULL, &fv);
583 VLOG_WARN_RL(&error_rl, "%s: flow list failed (%s)",
584 dpif_name(dpif), strerror(error));
586 COVERAGE_ADD(dpif_flow_query_list_n, fv.n_flows);
588 VLOG_DBG_RL(&dpmsg_rl, "%s: listed %zu flows",
589 dpif_name(dpif), *n_out);
595 dpif_flow_list_all(const struct dpif *dpif,
596 struct odp_flow **flowsp, size_t *np)
598 struct odp_stats stats;
599 struct odp_flow *flows;
606 error = dpif_get_dp_stats(dpif, &stats);
611 flows = xmalloc(sizeof *flows * stats.n_flows);
612 error = dpif_flow_list(dpif, flows, stats.n_flows, &n_flows);
618 if (stats.n_flows != n_flows) {
619 VLOG_WARN_RL(&error_rl, "%s: datapath stats reported %"PRIu32" "
620 "flows but flow listing reported %zu",
621 dpif_name(dpif), stats.n_flows, n_flows);
629 dpif_execute(struct dpif *dpif, uint16_t in_port,
630 const union odp_action actions[], size_t n_actions,
631 const struct ofpbuf *buf)
635 COVERAGE_INC(dpif_execute);
637 struct odp_execute execute;
638 memset(&execute, 0, sizeof execute);
639 execute.in_port = in_port;
640 execute.actions = (union odp_action *) actions;
641 execute.n_actions = n_actions;
642 execute.data = buf->data;
643 execute.length = buf->size;
644 error = do_ioctl(dpif, ODP_EXECUTE, NULL, &execute);
649 if (!(error ? VLOG_DROP_WARN(&error_rl) : VLOG_DROP_DBG(&dpmsg_rl))) {
650 struct ds ds = DS_EMPTY_INITIALIZER;
651 char *packet = ofp_packet_to_string(buf->data, buf->size, buf->size);
652 ds_put_format(&ds, "%s: execute ", dpif_name(dpif));
653 format_odp_actions(&ds, actions, n_actions);
655 ds_put_format(&ds, " failed (%s)", strerror(error));
657 ds_put_format(&ds, " on packet %s", packet);
658 vlog(THIS_MODULE, error ? VLL_WARN : VLL_DBG, "%s", ds_cstr(&ds));
666 dpif_recv_get_mask(const struct dpif *dpif, int *listen_mask)
668 int error = do_ioctl(dpif, ODP_GET_LISTEN_MASK, "ODP_GET_LISTEN_MASK",
677 dpif_recv_set_mask(struct dpif *dpif, int listen_mask)
679 return do_ioctl(dpif, ODP_SET_LISTEN_MASK, "ODP_SET_LISTEN_MASK",
684 dpif_recv(struct dpif *dpif, struct ofpbuf **bufp)
690 buf = ofpbuf_new(65536);
691 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
694 if (error != EAGAIN) {
695 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
696 dpif_name(dpif), strerror(error));
698 } else if (retval >= sizeof(struct odp_msg)) {
699 struct odp_msg *msg = buf->data;
700 if (msg->length <= retval) {
702 if (VLOG_IS_DBG_ENABLED()) {
703 void *payload = msg + 1;
704 size_t length = buf->size - sizeof *msg;
705 char *s = ofp_packet_to_string(payload, length, length);
706 VLOG_DBG_RL(&dpmsg_rl, "%s: received %s message of length "
707 "%zu on port %"PRIu16": %s", dpif_name(dpif),
708 (msg->type == _ODPL_MISS_NR ? "miss"
709 : msg->type == _ODPL_ACTION_NR ? "action"
711 msg->length - sizeof(struct odp_msg),
716 COVERAGE_INC(dpif_recv);
719 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
720 "from %zu bytes to %d",
721 dpif_name(dpif), msg->length, retval);
724 } else if (!retval) {
725 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif));
728 VLOG_WARN_RL(&error_rl,
729 "%s: discarding too-short message (%d bytes)",
730 dpif_name(dpif), retval);
740 dpif_recv_wait(struct dpif *dpif)
742 poll_fd_wait(dpif->fd, POLLIN);
746 dpif_get_netflow_ids(const struct dpif *dpif,
747 uint8_t *engine_type, uint8_t *engine_id)
749 *engine_type = *engine_id = dpif->minor;
754 struct nl_sock *sock;
759 dpifmon_create(const char *datapath_name, struct dpifmon **monp)
762 char local_name[IFNAMSIZ];
765 mon = *monp = xmalloc(sizeof *mon);
767 error = dpif_open(datapath_name, &mon->dpif);
771 error = dpif_port_get_name(mon->dpif, ODPP_LOCAL,
772 local_name, sizeof local_name);
774 goto error_close_dpif;
777 mon->local_ifindex = if_nametoindex(local_name);
778 if (!mon->local_ifindex) {
780 VLOG_WARN("could not get ifindex of %s device: %s",
781 local_name, strerror(errno));
782 goto error_close_dpif;
785 error = nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0, &mon->sock);
787 VLOG_WARN("could not create rtnetlink socket: %s", strerror(error));
788 goto error_close_dpif;
794 dpif_close(mon->dpif);
802 dpifmon_destroy(struct dpifmon *mon)
805 dpif_close(mon->dpif);
806 nl_sock_destroy(mon->sock);
811 dpifmon_poll(struct dpifmon *mon, char **devnamep)
813 static struct vlog_rate_limit slow_rl = VLOG_RATE_LIMIT_INIT(1, 5);
814 static const struct nl_policy rtnlgrp_link_policy[] = {
815 [IFLA_IFNAME] = { .type = NL_A_STRING },
816 [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
818 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
824 error = nl_sock_recv(mon->sock, &buf, false);
827 if (!nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
829 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
830 VLOG_WARN_RL(&slow_rl, "received bad rtnl message");
833 const char *devname = nl_attr_get_string(attrs[IFLA_IFNAME]);
836 if (attrs[IFLA_MASTER]) {
837 uint32_t master_ifindex = nl_attr_get_u32(attrs[IFLA_MASTER]);
838 for_us = master_ifindex == mon->local_ifindex;
840 /* It's for us if that device is one of our ports. */
841 struct odp_port port;
842 for_us = !dpif_port_query_by_name(mon->dpif, devname, &port);
846 /* Not for us, try again. */
848 COVERAGE_INC(dpifmon_poll_false_wakeup);
851 COVERAGE_INC(dpifmon_poll_changed);
852 *devnamep = xstrdup(devname);
862 VLOG_WARN_RL(&slow_rl, "dpifmon socket overflowed");
866 VLOG_WARN_RL(&slow_rl, "error on dpifmon socket: %s", strerror(error));
873 dpifmon_run(struct dpifmon *mon UNUSED)
875 /* Nothing to do in this implementation. */
879 dpifmon_wait(struct dpifmon *mon)
881 nl_sock_wait(mon->sock, POLLIN);
884 static int get_openvswitch_major(void);
885 static int get_major(const char *target, int default_major);
888 lookup_minor(const char *name, unsigned int *minor)
890 struct ethtool_drvinfo drvinfo;
896 sock = socket(AF_INET, SOCK_DGRAM, 0);
898 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
903 memset(&ifr, 0, sizeof ifr);
904 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
905 ifr.ifr_data = (caddr_t) &drvinfo;
907 memset(&drvinfo, 0, sizeof drvinfo);
908 drvinfo.cmd = ETHTOOL_GDRVINFO;
909 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
910 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
912 goto error_close_sock;
915 if (strcmp(drvinfo.driver, "openvswitch")) {
916 VLOG_WARN("%s is not an openvswitch device", name);
918 goto error_close_sock;
921 if (!isdigit(drvinfo.bus_info[0])) {
922 VLOG_WARN("%s ethtool info does not contain an openvswitch minor",
925 goto error_close_sock;
928 *minor = atoi(drvinfo.bus_info);
939 make_openvswitch_device(unsigned int minor, char **fnp)
941 dev_t dev = makedev(get_openvswitch_major(), minor);
942 const char dirname[] = "/dev/net";
947 sprintf(fn, "%s/dp%d", dirname, minor);
949 if (!S_ISCHR(s.st_mode)) {
950 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
952 } else if (s.st_rdev != dev) {
953 VLOG_WARN_RL(&error_rl,
954 "%s is device %u:%u instead of %u:%u, fixing",
955 fn, major(s.st_rdev), minor(s.st_rdev),
956 major(dev), minor(dev));
961 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
962 fn, strerror(errno));
965 } else if (errno == ENOENT) {
966 if (stat(dirname, &s)) {
967 if (errno == ENOENT) {
968 if (mkdir(dirname, 0755)) {
969 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
970 dirname, strerror(errno));
974 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
975 dirname, strerror(errno));
980 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
984 /* The device needs to be created. */
985 if (mknod(fn, S_IFCHR | 0700, dev)) {
986 VLOG_WARN_RL(&error_rl,
987 "%s: creating character device %u:%u failed (%s)",
988 fn, major(dev), minor(dev), strerror(errno));
999 get_openvswitch_major(void)
1001 static unsigned int openvswitch_major;
1002 if (!openvswitch_major) {
1003 enum { DEFAULT_MAJOR = 248 };
1004 openvswitch_major = get_major("openvswitch", DEFAULT_MAJOR);
1006 return openvswitch_major;
1010 get_major(const char *target, int default_major)
1012 const char fn[] = "/proc/devices";
1017 file = fopen(fn, "r");
1019 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
1023 for (ln = 1; fgets(line, sizeof line, file); ln++) {
1027 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
1028 /* Nothing to do. */
1029 } else if (!strncmp(line, "Block", 5)) {
1030 /* We only want character devices, so skip the rest of the file. */
1032 } else if (sscanf(line, "%d %63s", &major, name)) {
1033 if (!strcmp(name, target)) {
1040 VLOG_WARN("%s:%d: syntax error", fn, ln);
1046 VLOG_ERR("%s: %s major not found (is the module loaded?), using "
1047 "default major %d", fn, target, default_major);
1049 VLOG_INFO("using default major %d for %s", default_major, target);
1050 return default_major;
1054 name_to_minor(const char *name, unsigned int *minor)
1056 if (!get_minor_from_name(name, minor)) {
1059 return lookup_minor(name, minor);
1063 get_minor_from_name(const char *name, unsigned int *minor)
1065 if (!strncmp(name, "dp", 2) && isdigit(name[2])) {
1066 *minor = atoi(name + 2);
1074 open_by_minor(unsigned int minor, struct dpif **dpifp)
1082 error = make_openvswitch_device(minor, &fn);
1087 fd = open(fn, O_RDONLY | O_NONBLOCK);
1090 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
1096 dpif = xmalloc(sizeof *dpif);
1097 dpif->name = xasprintf("dp%u", dpif->minor);
1098 dpif->minor = minor;
1104 /* There is a tendency to construct odp_flow objects on the stack and to
1105 * forget to properly initialize their "actions" and "n_actions" members.
1106 * When this happens, we get memory corruption because the kernel
1107 * writes through the random pointer that is in the "actions" member.
1109 * This function attempts to combat the problem by:
1111 * - Forcing a segfault if "actions" points to an invalid region (instead
1112 * of just getting back EFAULT, which can be easily missed in the log).
1114 * - Storing a distinctive value that is likely to cause an
1115 * easy-to-identify error later if it is dereferenced, etc.
1117 * - Triggering a warning on uninitialized memory from Valgrind if
1118 * "actions" or "n_actions" was not initialized.
1121 check_rw_odp_flow(struct odp_flow *flow)
1123 if (flow->n_actions) {
1124 memset(&flow->actions[0], 0xcc, sizeof flow->actions[0]);