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_get_listen_mask(const struct dpif *dpif, int *listen_mask)
223 int error = do_ioctl(dpif, ODP_GET_LISTEN_MASK, "ODP_GET_LISTEN_MASK",
232 dpif_set_listen_mask(struct dpif *dpif, int listen_mask)
234 return do_ioctl(dpif, ODP_SET_LISTEN_MASK, "ODP_SET_LISTEN_MASK",
239 dpif_purge(struct dpif *dpif)
241 struct odp_stats stats;
245 COVERAGE_INC(dpif_purge);
247 error = dpif_get_dp_stats(dpif, &stats);
252 for (i = 0; i < stats.max_miss_queue + stats.max_action_queue; i++) {
254 error = dpif_recv(dpif, &buf);
256 return error == EAGAIN ? 0 : error;
264 dpif_port_add(struct dpif *dpif, const char *devname, uint16_t port_no,
267 struct odp_port port;
269 COVERAGE_INC(dpif_port_add);
270 memset(&port, 0, sizeof port);
271 strncpy(port.devname, devname, sizeof port.devname);
274 if (!ioctl(dpif->fd, ODP_PORT_ADD, &port)) {
275 VLOG_DBG_RL(&dpmsg_rl, "%s: added %s as port %"PRIu16,
276 dpif_name(dpif), devname, port_no);
279 VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port %"PRIu16": %s",
280 dpif_name(dpif), devname, port_no, strerror(errno));
286 dpif_port_del(struct dpif *dpif, uint16_t port_no)
289 COVERAGE_INC(dpif_port_del);
290 return do_ioctl(dpif, ODP_PORT_DEL, "ODP_PORT_DEL", &tmp);
294 dpif_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
295 struct odp_port *port)
297 memset(port, 0, sizeof *port);
298 port->port = port_no;
299 if (!ioctl(dpif->fd, ODP_PORT_QUERY, port)) {
300 VLOG_DBG_RL(&dpmsg_rl, "%s: port %"PRIu16" is device %s",
301 dpif_name(dpif), port_no, port->devname);
304 VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu16": %s",
305 dpif_name(dpif), port_no, strerror(errno));
311 dpif_port_query_by_name(const struct dpif *dpif, const char *devname,
312 struct odp_port *port)
314 memset(port, 0, sizeof *port);
315 strncpy(port->devname, devname, sizeof port->devname);
316 if (!ioctl(dpif->fd, ODP_PORT_QUERY, port)) {
317 VLOG_DBG_RL(&dpmsg_rl, "%s: device %s is on port %"PRIu16,
318 dpif_name(dpif), devname, port->port);
321 /* Log level is DBG here because all the current callers are interested
322 * in whether 'dpif' actually has a port 'devname', so that it's not an
323 * issue worth logging if it doesn't. */
324 VLOG_DBG_RL(&error_rl, "%s: failed to query port %s: %s",
325 dpif_name(dpif), devname, strerror(errno));
331 dpif_port_get_name(struct dpif *dpif, uint16_t port_no,
332 char *name, size_t name_size)
334 struct odp_port port;
337 assert(name_size > 0);
339 error = dpif_port_query_by_number(dpif, port_no, &port);
341 ovs_strlcpy(name, port.devname, name_size);
349 dpif_port_list(const struct dpif *dpif,
350 struct odp_port **ports, size_t *n_ports)
352 struct odp_portvec pv;
353 struct odp_stats stats;
357 error = dpif_get_dp_stats(dpif, &stats);
362 *ports = xcalloc(1, stats.n_ports * sizeof **ports);
364 pv.n_ports = stats.n_ports;
365 error = do_ioctl(dpif, ODP_PORT_LIST, "ODP_PORT_LIST", &pv);
370 } while (pv.n_ports != stats.n_ports);
371 *n_ports = pv.n_ports;
381 dpif_port_group_set(struct dpif *dpif, uint16_t group,
382 const uint16_t ports[], size_t n_ports)
384 struct odp_port_group pg;
386 COVERAGE_INC(dpif_port_group_set);
387 assert(n_ports <= UINT16_MAX);
389 pg.ports = (uint16_t *) ports;
390 pg.n_ports = n_ports;
391 return do_ioctl(dpif, ODP_PORT_GROUP_SET, "ODP_PORT_GROUP_SET", &pg);
395 dpif_port_group_get(const struct dpif *dpif, uint16_t group,
396 uint16_t **ports, size_t *n_ports)
403 struct odp_port_group pg;
406 pg.n_ports = *n_ports;
408 error = do_ioctl(dpif, ODP_PORT_GROUP_GET, "ODP_PORT_GROUP_GET", &pg);
415 } else if (pg.n_ports <= *n_ports) {
417 *n_ports = pg.n_ports;
420 /* Soft error: there were more ports than we expected in the
421 * group. Try again. */
423 *ports = xcalloc(pg.n_ports, sizeof **ports);
424 *n_ports = pg.n_ports;
431 dpif_flow_flush(struct dpif *dpif)
433 COVERAGE_INC(dpif_flow_flush);
434 return do_ioctl(dpif, ODP_FLOW_FLUSH, "ODP_FLOW_FLUSH", NULL);
437 static enum vlog_level
438 flow_message_log_level(int error)
440 return error ? VLL_WARN : VLL_DBG;
444 should_log_flow_message(int error)
446 return !vlog_should_drop(THIS_MODULE, flow_message_log_level(error),
447 error ? &error_rl : &dpmsg_rl);
451 log_flow_message(const struct dpif *dpif, int error,
452 const char *operation,
453 const flow_t *flow, const struct odp_flow_stats *stats,
454 const union odp_action *actions, size_t n_actions)
456 struct ds ds = DS_EMPTY_INITIALIZER;
457 ds_put_format(&ds, "%s: ", dpif_name(dpif));
459 ds_put_cstr(&ds, "failed to ");
461 ds_put_format(&ds, "%s ", operation);
463 ds_put_format(&ds, "(%s) ", strerror(error));
465 flow_format(&ds, flow);
467 ds_put_cstr(&ds, ", ");
468 format_odp_flow_stats(&ds, stats);
470 if (actions || n_actions) {
471 ds_put_cstr(&ds, ", actions:");
472 format_odp_actions(&ds, actions, n_actions);
474 vlog(THIS_MODULE, flow_message_log_level(error), "%s", ds_cstr(&ds));
479 do_flow_ioctl(const struct dpif *dpif, int cmd, struct odp_flow *flow,
480 const char *operation, bool show_stats)
482 int error = do_ioctl(dpif, cmd, NULL, flow);
483 if (error && show_stats) {
486 if (should_log_flow_message(error)) {
487 log_flow_message(dpif, error, operation, &flow->key,
488 show_stats && !error ? &flow->stats : NULL,
489 flow->actions, flow->n_actions);
495 dpif_flow_put(struct dpif *dpif, struct odp_flow_put *put)
497 int error = do_ioctl(dpif, ODP_FLOW_PUT, NULL, put);
498 COVERAGE_INC(dpif_flow_put);
499 if (should_log_flow_message(error)) {
500 struct ds operation = DS_EMPTY_INITIALIZER;
501 ds_put_cstr(&operation, "put");
502 if (put->flags & ODPPF_CREATE) {
503 ds_put_cstr(&operation, "[create]");
505 if (put->flags & ODPPF_MODIFY) {
506 ds_put_cstr(&operation, "[modify]");
508 if (put->flags & ODPPF_ZERO_STATS) {
509 ds_put_cstr(&operation, "[zero]");
511 #define ODPPF_ALL (ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS)
512 if (put->flags & ~ODPPF_ALL) {
513 ds_put_format(&operation, "[%x]", put->flags & ~ODPPF_ALL);
515 log_flow_message(dpif, error, ds_cstr(&operation), &put->flow.key,
516 !error ? &put->flow.stats : NULL,
517 put->flow.actions, put->flow.n_actions);
518 ds_destroy(&operation);
524 dpif_flow_del(struct dpif *dpif, struct odp_flow *flow)
526 COVERAGE_INC(dpif_flow_del);
527 check_rw_odp_flow(flow);
528 memset(&flow->stats, 0, sizeof flow->stats);
529 return do_flow_ioctl(dpif, ODP_FLOW_DEL, flow, "delete flow", true);
533 dpif_flow_get(const struct dpif *dpif, struct odp_flow *flow)
535 COVERAGE_INC(dpif_flow_query);
536 check_rw_odp_flow(flow);
537 memset(&flow->stats, 0, sizeof flow->stats);
538 return do_flow_ioctl(dpif, ODP_FLOW_GET, flow, "get flow", true);
542 dpif_flow_get_multiple(const struct dpif *dpif,
543 struct odp_flow flows[], size_t n)
545 struct odp_flowvec fv;
548 COVERAGE_ADD(dpif_flow_query_multiple, n);
551 for (i = 0; i < n; i++) {
552 check_rw_odp_flow(&flows[i]);
554 return do_ioctl(dpif, ODP_FLOW_GET_MULTIPLE, "ODP_FLOW_GET_MULTIPLE",
559 dpif_flow_list(const struct dpif *dpif, struct odp_flow flows[], size_t n,
562 struct odp_flowvec fv;
566 COVERAGE_INC(dpif_flow_query_list);
569 if (RUNNING_ON_VALGRIND) {
570 memset(flows, 0, n * sizeof *flows);
572 for (i = 0; i < n; i++) {
573 flows[i].actions = NULL;
574 flows[i].n_actions = 0;
577 error = do_ioctl(dpif, ODP_FLOW_LIST, NULL, &fv);
580 VLOG_WARN_RL(&error_rl, "%s: flow list failed (%s)",
581 dpif_name(dpif), strerror(error));
583 COVERAGE_ADD(dpif_flow_query_list_n, fv.n_flows);
585 VLOG_DBG_RL(&dpmsg_rl, "%s: listed %zu flows",
586 dpif_name(dpif), *n_out);
592 dpif_flow_list_all(const struct dpif *dpif,
593 struct odp_flow **flowsp, size_t *np)
595 struct odp_stats stats;
596 struct odp_flow *flows;
603 error = dpif_get_dp_stats(dpif, &stats);
608 flows = xmalloc(sizeof *flows * stats.n_flows);
609 error = dpif_flow_list(dpif, flows, stats.n_flows, &n_flows);
615 if (stats.n_flows != n_flows) {
616 VLOG_WARN_RL(&error_rl, "%s: datapath stats reported %"PRIu32" "
617 "flows but flow listing reported %zu",
618 dpif_name(dpif), stats.n_flows, n_flows);
626 dpif_execute(struct dpif *dpif, uint16_t in_port,
627 const union odp_action actions[], size_t n_actions,
628 const struct ofpbuf *buf)
632 COVERAGE_INC(dpif_execute);
634 struct odp_execute execute;
635 memset(&execute, 0, sizeof execute);
636 execute.in_port = in_port;
637 execute.actions = (union odp_action *) actions;
638 execute.n_actions = n_actions;
639 execute.data = buf->data;
640 execute.length = buf->size;
641 error = do_ioctl(dpif, ODP_EXECUTE, NULL, &execute);
646 if (!(error ? VLOG_DROP_WARN(&error_rl) : VLOG_DROP_DBG(&dpmsg_rl))) {
647 struct ds ds = DS_EMPTY_INITIALIZER;
648 char *packet = ofp_packet_to_string(buf->data, buf->size, buf->size);
649 ds_put_format(&ds, "%s: execute ", dpif_name(dpif));
650 format_odp_actions(&ds, actions, n_actions);
652 ds_put_format(&ds, " failed (%s)", strerror(error));
654 ds_put_format(&ds, " on packet %s", packet);
655 vlog(THIS_MODULE, error ? VLL_WARN : VLL_DBG, "%s", ds_cstr(&ds));
663 dpif_recv(struct dpif *dpif, struct ofpbuf **bufp)
669 buf = ofpbuf_new(65536);
670 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
673 if (error != EAGAIN) {
674 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
675 dpif_name(dpif), strerror(error));
677 } else if (retval >= sizeof(struct odp_msg)) {
678 struct odp_msg *msg = buf->data;
679 if (msg->length <= retval) {
681 if (VLOG_IS_DBG_ENABLED()) {
682 void *payload = msg + 1;
683 size_t length = buf->size - sizeof *msg;
684 char *s = ofp_packet_to_string(payload, length, length);
685 VLOG_DBG_RL(&dpmsg_rl, "%s: received %s message of length "
686 "%zu on port %"PRIu16": %s", dpif_name(dpif),
687 (msg->type == _ODPL_MISS_NR ? "miss"
688 : msg->type == _ODPL_ACTION_NR ? "action"
690 msg->length - sizeof(struct odp_msg),
695 COVERAGE_INC(dpif_recv);
698 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
699 "from %zu bytes to %d",
700 dpif_name(dpif), msg->length, retval);
703 } else if (!retval) {
704 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif));
707 VLOG_WARN_RL(&error_rl,
708 "%s: discarding too-short message (%d bytes)",
709 dpif_name(dpif), retval);
719 dpif_recv_wait(struct dpif *dpif)
721 poll_fd_wait(dpif->fd, POLLIN);
725 dpif_get_netflow_ids(const struct dpif *dpif,
726 uint8_t *engine_type, uint8_t *engine_id)
728 *engine_type = *engine_id = dpif->minor;
733 struct nl_sock *sock;
738 dpifmon_create(const char *datapath_name, struct dpifmon **monp)
741 char local_name[IFNAMSIZ];
744 mon = *monp = xmalloc(sizeof *mon);
746 error = dpif_open(datapath_name, &mon->dpif);
750 error = dpif_port_get_name(mon->dpif, ODPP_LOCAL,
751 local_name, sizeof local_name);
753 goto error_close_dpif;
756 mon->local_ifindex = if_nametoindex(local_name);
757 if (!mon->local_ifindex) {
759 VLOG_WARN("could not get ifindex of %s device: %s",
760 local_name, strerror(errno));
761 goto error_close_dpif;
764 error = nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0, &mon->sock);
766 VLOG_WARN("could not create rtnetlink socket: %s", strerror(error));
767 goto error_close_dpif;
773 dpif_close(mon->dpif);
781 dpifmon_destroy(struct dpifmon *mon)
784 dpif_close(mon->dpif);
785 nl_sock_destroy(mon->sock);
790 dpifmon_poll(struct dpifmon *mon, char **devnamep)
792 static struct vlog_rate_limit slow_rl = VLOG_RATE_LIMIT_INIT(1, 5);
793 static const struct nl_policy rtnlgrp_link_policy[] = {
794 [IFLA_IFNAME] = { .type = NL_A_STRING },
795 [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
797 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
803 error = nl_sock_recv(mon->sock, &buf, false);
806 if (!nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
808 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
809 VLOG_WARN_RL(&slow_rl, "received bad rtnl message");
812 const char *devname = nl_attr_get_string(attrs[IFLA_IFNAME]);
815 if (attrs[IFLA_MASTER]) {
816 uint32_t master_ifindex = nl_attr_get_u32(attrs[IFLA_MASTER]);
817 for_us = master_ifindex == mon->local_ifindex;
819 /* It's for us if that device is one of our ports. */
820 struct odp_port port;
821 for_us = !dpif_port_query_by_name(mon->dpif, devname, &port);
825 /* Not for us, try again. */
827 COVERAGE_INC(dpifmon_poll_false_wakeup);
830 COVERAGE_INC(dpifmon_poll_changed);
831 *devnamep = xstrdup(devname);
841 VLOG_WARN_RL(&slow_rl, "dpifmon socket overflowed");
845 VLOG_WARN_RL(&slow_rl, "error on dpifmon socket: %s", strerror(error));
852 dpifmon_run(struct dpifmon *mon UNUSED)
854 /* Nothing to do in this implementation. */
858 dpifmon_wait(struct dpifmon *mon)
860 nl_sock_wait(mon->sock, POLLIN);
863 static int get_openvswitch_major(void);
864 static int get_major(const char *target, int default_major);
867 lookup_minor(const char *name, unsigned int *minor)
869 struct ethtool_drvinfo drvinfo;
875 sock = socket(AF_INET, SOCK_DGRAM, 0);
877 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
882 memset(&ifr, 0, sizeof ifr);
883 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
884 ifr.ifr_data = (caddr_t) &drvinfo;
886 memset(&drvinfo, 0, sizeof drvinfo);
887 drvinfo.cmd = ETHTOOL_GDRVINFO;
888 if (ioctl(sock, SIOCETHTOOL, &ifr)) {
889 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
891 goto error_close_sock;
894 if (strcmp(drvinfo.driver, "openvswitch")) {
895 VLOG_WARN("%s is not an openvswitch device", name);
897 goto error_close_sock;
900 if (!isdigit(drvinfo.bus_info[0])) {
901 VLOG_WARN("%s ethtool info does not contain an openvswitch minor",
904 goto error_close_sock;
907 *minor = atoi(drvinfo.bus_info);
918 make_openvswitch_device(unsigned int minor, char **fnp)
920 dev_t dev = makedev(get_openvswitch_major(), minor);
921 const char dirname[] = "/dev/net";
926 sprintf(fn, "%s/dp%d", dirname, minor);
928 if (!S_ISCHR(s.st_mode)) {
929 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
931 } else if (s.st_rdev != dev) {
932 VLOG_WARN_RL(&error_rl,
933 "%s is device %u:%u instead of %u:%u, fixing",
934 fn, major(s.st_rdev), minor(s.st_rdev),
935 major(dev), minor(dev));
940 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
941 fn, strerror(errno));
944 } else if (errno == ENOENT) {
945 if (stat(dirname, &s)) {
946 if (errno == ENOENT) {
947 if (mkdir(dirname, 0755)) {
948 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
949 dirname, strerror(errno));
953 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
954 dirname, strerror(errno));
959 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
963 /* The device needs to be created. */
964 if (mknod(fn, S_IFCHR | 0700, dev)) {
965 VLOG_WARN_RL(&error_rl,
966 "%s: creating character device %u:%u failed (%s)",
967 fn, major(dev), minor(dev), strerror(errno));
978 get_openvswitch_major(void)
980 static unsigned int openvswitch_major;
981 if (!openvswitch_major) {
982 enum { DEFAULT_MAJOR = 248 };
983 openvswitch_major = get_major("openvswitch", DEFAULT_MAJOR);
985 return openvswitch_major;
989 get_major(const char *target, int default_major)
991 const char fn[] = "/proc/devices";
996 file = fopen(fn, "r");
998 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
1002 for (ln = 1; fgets(line, sizeof line, file); ln++) {
1006 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
1007 /* Nothing to do. */
1008 } else if (!strncmp(line, "Block", 5)) {
1009 /* We only want character devices, so skip the rest of the file. */
1011 } else if (sscanf(line, "%d %63s", &major, name)) {
1012 if (!strcmp(name, target)) {
1019 VLOG_WARN("%s:%d: syntax error", fn, ln);
1025 VLOG_ERR("%s: %s major not found (is the module loaded?), using "
1026 "default major %d", fn, target, default_major);
1028 VLOG_INFO("using default major %d for %s", default_major, target);
1029 return default_major;
1033 name_to_minor(const char *name, unsigned int *minor)
1035 if (!get_minor_from_name(name, minor)) {
1038 return lookup_minor(name, minor);
1042 get_minor_from_name(const char *name, unsigned int *minor)
1044 if (!strncmp(name, "dp", 2) && isdigit(name[2])) {
1045 *minor = atoi(name + 2);
1053 open_by_minor(unsigned int minor, struct dpif **dpifp)
1061 error = make_openvswitch_device(minor, &fn);
1066 fd = open(fn, O_RDONLY | O_NONBLOCK);
1069 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
1075 dpif = xmalloc(sizeof *dpif);
1076 dpif->name = xasprintf("dp%u", dpif->minor);
1077 dpif->minor = minor;
1083 /* There is a tendency to construct odp_flow objects on the stack and to
1084 * forget to properly initialize their "actions" and "n_actions" members.
1085 * When this happens, we get memory corruption because the kernel
1086 * writes through the random pointer that is in the "actions" member.
1088 * This function attempts to combat the problem by:
1090 * - Forcing a segfault if "actions" points to an invalid region (instead
1091 * of just getting back EFAULT, which can be easily missed in the log).
1093 * - Storing a distinctive value that is likely to cause an
1094 * easy-to-identify error later if it is dereferenced, etc.
1096 * - Triggering a warning on uninitialized memory from Valgrind if
1097 * "actions" or "n_actions" was not initialized.
1100 check_rw_odp_flow(struct odp_flow *flow)
1102 if (flow->n_actions) {
1103 memset(&flow->actions[0], 0xcc, sizeof flow->actions[0]);