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.
18 #include "dpif-provider.h"
28 #include "dynamic-string.h"
32 #include "ofp-print.h"
35 #include "poll-loop.h"
40 #define THIS_MODULE VLM_dpif
42 static struct dpif_class *dpif_classes[] = {
45 enum { N_DPIF_CLASSES = ARRAY_SIZE(dpif_classes) };
47 /* Rate limit for individual messages going to or from the datapath, output at
48 * DBG level. This is very high because, if these are enabled, it is because
49 * we really need to see them. */
50 static struct vlog_rate_limit dpmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
52 /* Not really much point in logging many dpif errors. */
53 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
55 static void log_operation(const struct dpif *, const char *operation,
57 static void log_flow_operation(const struct dpif *, const char *operation,
58 int error, struct odp_flow *flow);
59 static void log_flow_put(struct dpif *, int error,
60 const struct odp_flow_put *);
61 static bool should_log_flow_message(int error);
62 static void check_rw_odp_flow(struct odp_flow *);
64 /* Performs periodic work needed by all the various kinds of dpifs.
66 * If your program opens any dpifs, it must call this function within its main
72 for (i = 0; i < N_DPIF_CLASSES; i++) {
73 const struct dpif_class *class = dpif_classes[i];
80 /* Arranges for poll_block() to wake up when dp_run() needs to be called.
82 * If your program opens any dpifs, it must call this function within its main
88 for (i = 0; i < N_DPIF_CLASSES; i++) {
89 const struct dpif_class *class = dpif_classes[i];
97 do_open(const char *name_, bool create, struct dpif **dpifp)
99 char *name = xstrdup(name_);
100 char *prefix, *suffix, *colon;
101 struct dpif *dpif = NULL;
105 colon = strchr(name, ':');
115 for (i = 0; i < N_DPIF_CLASSES; i++) {
116 const struct dpif_class *class = dpif_classes[i];
117 if (!strcmp(prefix, class->prefix)) {
118 error = class->open(name_, suffix, create, &dpif);
122 error = EAFNOSUPPORT;
125 *dpifp = error ? NULL : dpif;
129 /* Tries to open an existing datapath named 'name'. Will fail if no datapath
130 * named 'name' exists. Returns 0 if successful, otherwise a positive errno
131 * value. On success stores a pointer to the datapath in '*dpifp', otherwise a
134 dpif_open(const char *name, struct dpif **dpifp)
136 return do_open(name, false, dpifp);
139 /* Tries to create and open a new datapath with the given 'name'. Will fail if
140 * a datapath named 'name' already exists. Returns 0 if successful, otherwise
141 * a positive errno value. On success stores a pointer to the datapath in
142 * '*dpifp', otherwise a null pointer.*/
144 dpif_create(const char *name, struct dpif **dpifp)
146 return do_open(name, true, dpifp);
149 /* Closes and frees the connection to 'dpif'. Does not destroy the datapath
150 * itself; call dpif_delete() first, instead, if that is desirable. */
152 dpif_close(struct dpif *dpif)
155 char *name = dpif->name;
156 dpif->class->close(dpif);
161 /* Returns the name of datapath 'dpif' (for use in log messages). */
163 dpif_name(const struct dpif *dpif)
168 /* Destroys the datapath that 'dpif' is connected to, first removing all of its
169 * ports. After calling this function, it does not make sense to pass 'dpif'
170 * to any functions other than dpif_name() or dpif_close(). */
172 dpif_delete(struct dpif *dpif)
176 COVERAGE_INC(dpif_destroy);
178 error = dpif->class->delete(dpif);
179 log_operation(dpif, "delete", error);
183 /* Retrieves statistics for 'dpif' into 'stats'. Returns 0 if successful,
184 * otherwise a positive errno value. */
186 dpif_get_dp_stats(const struct dpif *dpif, struct odp_stats *stats)
188 int error = dpif->class->get_stats(dpif, stats);
190 memset(stats, 0, sizeof *stats);
192 log_operation(dpif, "get_stats", error);
196 /* Retrieves the current IP fragment handling policy for 'dpif' into
197 * '*drop_frags': true indicates that fragments are dropped, false indicates
198 * that fragments are treated in the same way as other IP packets (except that
199 * the L4 header cannot be read). Returns 0 if successful, otherwise a
200 * positive errno value. */
202 dpif_get_drop_frags(const struct dpif *dpif, bool *drop_frags)
204 int error = dpif->class->get_drop_frags(dpif, drop_frags);
208 log_operation(dpif, "get_drop_frags", error);
212 /* Changes 'dpif''s treatment of IP fragments to 'drop_frags', whose meaning is
213 * the same as for the get_drop_frags member function. Returns 0 if
214 * successful, otherwise a positive errno value. */
216 dpif_set_drop_frags(struct dpif *dpif, bool drop_frags)
218 int error = dpif->class->set_drop_frags(dpif, drop_frags);
219 log_operation(dpif, "set_drop_frags", error);
223 /* Attempts to add 'devname' as a port on 'dpif', given the combination of
224 * ODP_PORT_* flags in 'flags'. If successful, returns 0 and sets '*port_nop'
225 * to the new port's port number (if 'port_nop' is non-null). On failure,
226 * returns a positive errno value and sets '*port_nop' to UINT16_MAX (if
227 * 'port_nop' is non-null). */
229 dpif_port_add(struct dpif *dpif, const char *devname, uint16_t flags,
235 COVERAGE_INC(dpif_port_add);
237 error = dpif->class->port_add(dpif, devname, flags, &port_no);
239 VLOG_DBG_RL(&dpmsg_rl, "%s: added %s as port %"PRIu16,
240 dpif_name(dpif), devname, port_no);
242 VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
243 dpif_name(dpif), devname, strerror(error));
244 port_no = UINT16_MAX;
252 /* Attempts to remove 'dpif''s port number 'port_no'. Returns 0 if successful,
253 * otherwise a positive errno value. */
255 dpif_port_del(struct dpif *dpif, uint16_t port_no)
259 COVERAGE_INC(dpif_port_del);
261 error = dpif->class->port_del(dpif, port_no);
262 log_operation(dpif, "port_del", error);
266 /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and
267 * initializes '*port' appropriately; on failure, returns a positive errno
270 dpif_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
271 struct odp_port *port)
273 int error = dpif->class->port_query_by_number(dpif, port_no, port);
275 VLOG_DBG_RL(&dpmsg_rl, "%s: port %"PRIu16" is device %s",
276 dpif_name(dpif), port_no, port->devname);
278 memset(port, 0, sizeof *port);
279 VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu16": %s",
280 dpif_name(dpif), port_no, strerror(error));
285 /* Looks up port named 'devname' in 'dpif'. On success, returns 0 and
286 * initializes '*port' appropriately; on failure, returns a positive errno
289 dpif_port_query_by_name(const struct dpif *dpif, const char *devname,
290 struct odp_port *port)
292 int error = dpif->class->port_query_by_name(dpif, devname, port);
294 VLOG_DBG_RL(&dpmsg_rl, "%s: device %s is on port %"PRIu16,
295 dpif_name(dpif), devname, port->port);
297 memset(port, 0, sizeof *port);
299 /* Log level is DBG here because all the current callers are interested
300 * in whether 'dpif' actually has a port 'devname', so that it's not an
301 * issue worth logging if it doesn't. */
302 VLOG_DBG_RL(&error_rl, "%s: failed to query port %s: %s",
303 dpif_name(dpif), devname, strerror(error));
308 /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and copies
309 * the port's name into the 'name_size' bytes in 'name', ensuring that the
310 * result is null-terminated. On failure, returns a positive errno value and
311 * makes 'name' the empty string. */
313 dpif_port_get_name(struct dpif *dpif, uint16_t port_no,
314 char *name, size_t name_size)
316 struct odp_port port;
319 assert(name_size > 0);
321 error = dpif_port_query_by_number(dpif, port_no, &port);
323 ovs_strlcpy(name, port.devname, name_size);
330 /* Obtains a list of all the ports in 'dpif'.
332 * If successful, returns 0 and sets '*portsp' to point to an array of
333 * appropriately initialized port structures and '*n_portsp' to the number of
334 * ports in the array. The caller is responsible for freeing '*portp' by
337 * On failure, returns a positive errno value and sets '*portsp' to NULL and
338 * '*n_portsp' to 0. */
340 dpif_port_list(const struct dpif *dpif,
341 struct odp_port **portsp, size_t *n_portsp)
343 struct odp_port *ports;
348 struct odp_stats stats;
351 error = dpif_get_dp_stats(dpif, &stats);
356 ports = xcalloc(stats.n_ports, sizeof *ports);
357 retval = dpif->class->port_list(dpif, ports, stats.n_ports);
363 } else if (retval <= stats.n_ports) {
369 /* Soft error: port count increased behind our back. Try again. */
382 log_operation(dpif, "port_list", error);
386 /* Retrieves a list of the port numbers in port group 'group' in 'dpif'.
388 * On success, returns 0 and points '*ports' to a newly allocated array of
389 * integers, each of which is a 'dpif' port number for a port in
390 * 'group'. Stores the number of elements in the array in '*n_ports'. The
391 * caller is responsible for freeing '*ports' by calling free().
393 * On failure, returns a positive errno value and sets '*ports' to NULL and
394 * '*n_ports' to 0. */
396 dpif_port_group_get(const struct dpif *dpif, uint16_t group,
397 uint16_t **ports, size_t *n_ports)
404 int retval = dpif->class->port_group_get(dpif, group,
413 } else if (retval <= *n_ports) {
419 /* Soft error: there were more ports than we expected in the
420 * group. Try again. */
422 *ports = xcalloc(retval, sizeof **ports);
426 log_operation(dpif, "port_group_get", error);
430 /* Updates port group 'group' in 'dpif', making it contain the 'n_ports' ports
431 * whose 'dpif' port numbers are given in 'n_ports'. Returns 0 if
432 * successful, otherwise a positive errno value.
434 * Behavior is undefined if the values in ports[] are not unique. */
436 dpif_port_group_set(struct dpif *dpif, uint16_t group,
437 const uint16_t ports[], size_t n_ports)
441 COVERAGE_INC(dpif_port_group_set);
443 error = dpif->class->port_group_set(dpif, group, ports, n_ports);
444 log_operation(dpif, "port_group_set", error);
448 /* Deletes all flows from 'dpif'. Returns 0 if successful, otherwise a
449 * positive errno value. */
451 dpif_flow_flush(struct dpif *dpif)
455 COVERAGE_INC(dpif_flow_flush);
457 error = dpif->class->flow_flush(dpif);
458 log_operation(dpif, "flow_flush", error);
462 /* Queries 'dpif' for a flow entry matching 'flow->key'.
464 * If a flow matching 'flow->key' exists in 'dpif', stores statistics for the
465 * flow into 'flow->stats'. If 'flow->n_actions' is zero, then 'flow->actions'
466 * is ignored. If 'flow->n_actions' is nonzero, then 'flow->actions' should
467 * point to an array of the specified number of actions. At most that many of
468 * the flow's actions will be copied into that array. 'flow->n_actions' will
469 * be updated to the number of actions actually present in the flow, which may
470 * be greater than the number stored if the flow has more actions than space
471 * available in the array.
473 * If no flow matching 'flow->key' exists in 'dpif', returns ENOENT. On other
474 * failure, returns a positive errno value. */
476 dpif_flow_get(const struct dpif *dpif, struct odp_flow *flow)
480 COVERAGE_INC(dpif_flow_get);
482 check_rw_odp_flow(flow);
483 error = dpif->class->flow_get(dpif, flow, 1);
485 error = flow->stats.error;
487 if (should_log_flow_message(error)) {
488 log_flow_operation(dpif, "flow_get", error, flow);
493 /* For each flow 'flow' in the 'n' flows in 'flows':
495 * - If a flow matching 'flow->key' exists in 'dpif':
497 * Stores 0 into 'flow->stats.error' and stores statistics for the flow
498 * into 'flow->stats'.
500 * If 'flow->n_actions' is zero, then 'flow->actions' is ignored. If
501 * 'flow->n_actions' is nonzero, then 'flow->actions' should point to an
502 * array of the specified number of actions. At most that many of the
503 * flow's actions will be copied into that array. 'flow->n_actions' will
504 * be updated to the number of actions actually present in the flow, which
505 * may be greater than the number stored if the flow has more actions than
506 * space available in the array.
508 * - Flow-specific errors are indicated by a positive errno value in
509 * 'flow->stats.error'. In particular, ENOENT indicates that no flow
510 * matching 'flow->key' exists in 'dpif'. When an error value is stored, the
511 * contents of 'flow->key' are preserved but other members of 'flow' should
512 * be treated as indeterminate.
514 * Returns 0 if all 'n' flows in 'flows' were updated (whether they were
515 * individually successful or not is indicated by 'flow->stats.error',
516 * however). Returns a positive errno value if an error that prevented this
517 * update occurred, in which the caller must not depend on any elements in
518 * 'flows' being updated or not updated.
521 dpif_flow_get_multiple(const struct dpif *dpif,
522 struct odp_flow flows[], size_t n)
527 COVERAGE_ADD(dpif_flow_get, n);
529 for (i = 0; i < n; i++) {
530 check_rw_odp_flow(&flows[i]);
533 error = dpif->class->flow_get(dpif, flows, n);
534 log_operation(dpif, "flow_get_multiple", error);
538 /* Adds or modifies a flow in 'dpif' as specified in 'put':
540 * - If the flow specified in 'put->flow' does not exist in 'dpif', then
541 * behavior depends on whether ODPPF_CREATE is specified in 'put->flags': if
542 * it is, the flow will be added, otherwise the operation will fail with
545 * - Otherwise, the flow specified in 'put->flow' does exist in 'dpif'.
546 * Behavior in this case depends on whether ODPPF_MODIFY is specified in
547 * 'put->flags': if it is, the flow's actions will be updated, otherwise the
548 * operation will fail with EEXIST. If the flow's actions are updated, then
549 * its statistics will be zeroed if ODPPF_ZERO_STATS is set in 'put->flags',
550 * left as-is otherwise.
552 * Returns 0 if successful, otherwise a positive errno value.
555 dpif_flow_put(struct dpif *dpif, struct odp_flow_put *put)
559 COVERAGE_INC(dpif_flow_put);
561 error = dpif->class->flow_put(dpif, put);
562 if (should_log_flow_message(error)) {
563 log_flow_put(dpif, error, put);
568 /* Deletes a flow matching 'flow->key' from 'dpif' or returns ENOENT if 'dpif'
569 * does not contain such a flow.
571 * If successful, updates 'flow->stats', 'flow->n_actions', and 'flow->actions'
572 * as described for dpif_flow_get(). */
574 dpif_flow_del(struct dpif *dpif, struct odp_flow *flow)
578 COVERAGE_INC(dpif_flow_del);
580 check_rw_odp_flow(flow);
581 memset(&flow->stats, 0, sizeof flow->stats);
583 error = dpif->class->flow_del(dpif, flow);
584 if (should_log_flow_message(error)) {
585 log_flow_operation(dpif, "delete flow", error, flow);
590 /* Stores up to 'n' flows in 'dpif' into 'flows', including their statistics
591 * but not including any information about their actions. If successful,
592 * returns 0 and sets '*n_out' to the number of flows actually present in
593 * 'dpif', which might be greater than the number stored (if 'dpif' has more
594 * than 'n' flows). On failure, returns a negative errno value and sets
597 dpif_flow_list(const struct dpif *dpif, struct odp_flow flows[], size_t n,
603 COVERAGE_INC(dpif_flow_query_list);
604 if (RUNNING_ON_VALGRIND) {
605 memset(flows, 0, n * sizeof *flows);
607 for (i = 0; i < n; i++) {
608 flows[i].actions = NULL;
609 flows[i].n_actions = 0;
612 retval = dpif->class->flow_list(dpif, flows, n);
615 VLOG_WARN_RL(&error_rl, "%s: flow list failed (%s)",
616 dpif_name(dpif), strerror(-retval));
619 COVERAGE_ADD(dpif_flow_query_list_n, retval);
620 *n_out = MIN(n, retval);
621 VLOG_DBG_RL(&dpmsg_rl, "%s: listed %zu flows (of %d)",
622 dpif_name(dpif), *n_out, retval);
627 /* Retrieves all of the flows in 'dpif'.
629 * If successful, returns 0 and stores in '*flowsp' a pointer to a newly
630 * allocated array of flows, including their statistics but not including any
631 * information about their actions, and sets '*np' to the number of flows in
632 * '*flowsp'. The caller is responsible for freeing '*flowsp' by calling
635 * On failure, returns a positive errno value and sets '*flowsp' to NULL and
638 dpif_flow_list_all(const struct dpif *dpif,
639 struct odp_flow **flowsp, size_t *np)
641 struct odp_stats stats;
642 struct odp_flow *flows;
649 error = dpif_get_dp_stats(dpif, &stats);
654 flows = xmalloc(sizeof *flows * stats.n_flows);
655 error = dpif_flow_list(dpif, flows, stats.n_flows, &n_flows);
661 if (stats.n_flows != n_flows) {
662 VLOG_WARN_RL(&error_rl, "%s: datapath stats reported %"PRIu32" "
663 "flows but flow listing reported %zu",
664 dpif_name(dpif), stats.n_flows, n_flows);
671 /* Causes 'dpif' to perform the 'n_actions' actions in 'actions' on the
672 * Ethernet frame specified in 'packet'.
674 * Pretends that the frame was originally received on the port numbered
675 * 'in_port'. This affects only ODPAT_OUTPUT_GROUP actions, which will not
676 * send a packet out their input port. Specify the number of an unused port
677 * (e.g. UINT16_MAX is currently always unused) to avoid this behavior.
679 * Returns 0 if successful, otherwise a positive errno value. */
681 dpif_execute(struct dpif *dpif, uint16_t in_port,
682 const union odp_action actions[], size_t n_actions,
683 const struct ofpbuf *buf)
687 COVERAGE_INC(dpif_execute);
689 error = dpif->class->execute(dpif, in_port, actions, n_actions, buf);
694 if (!(error ? VLOG_DROP_WARN(&error_rl) : VLOG_DROP_DBG(&dpmsg_rl))) {
695 struct ds ds = DS_EMPTY_INITIALIZER;
696 char *packet = ofp_packet_to_string(buf->data, buf->size, buf->size);
697 ds_put_format(&ds, "%s: execute ", dpif_name(dpif));
698 format_odp_actions(&ds, actions, n_actions);
700 ds_put_format(&ds, " failed (%s)", strerror(error));
702 ds_put_format(&ds, " on packet %s", packet);
703 vlog(THIS_MODULE, error ? VLL_WARN : VLL_DBG, "%s", ds_cstr(&ds));
710 /* Retrieves 'dpif''s "listen mask" into '*listen_mask'. Each ODPL_* bit set
711 * in '*listen_mask' indicates that dpif_recv() will receive messages of that
712 * type. Returns 0 if successful, otherwise a positive errno value. */
714 dpif_recv_get_mask(const struct dpif *dpif, int *listen_mask)
716 int error = dpif->class->recv_get_mask(dpif, listen_mask);
720 log_operation(dpif, "recv_get_mask", error);
724 /* Sets 'dpif''s "listen mask" to 'listen_mask'. Each ODPL_* bit set in
725 * '*listen_mask' requests that dpif_recv() receive messages of that type.
726 * Returns 0 if successful, otherwise a positive errno value. */
728 dpif_recv_set_mask(struct dpif *dpif, int listen_mask)
730 int error = dpif->class->recv_set_mask(dpif, listen_mask);
731 log_operation(dpif, "recv_set_mask", error);
735 /* Attempts to receive a message from 'dpif'. If successful, stores the
736 * message into '*packetp'. The message, if one is received, will begin with
737 * 'struct odp_msg' as a header. Only messages of the types selected with
738 * dpif_set_listen_mask() will ordinarily be received (but if a message type is
739 * enabled and then later disabled, some stragglers might pop up).
741 * Returns 0 if successful, otherwise a positive errno value. Returns EAGAIN
742 * if no message is immediately available. */
744 dpif_recv(struct dpif *dpif, struct ofpbuf **packetp)
746 int error = dpif->class->recv(dpif, packetp);
748 if (VLOG_IS_DBG_ENABLED()) {
749 struct ofpbuf *buf = *packetp;
750 struct odp_msg *msg = buf->data;
751 void *payload = msg + 1;
752 size_t payload_len = buf->size - sizeof *msg;
753 char *s = ofp_packet_to_string(payload, payload_len, payload_len);
754 VLOG_DBG_RL(&dpmsg_rl, "%s: received %s message of length "
755 "%zu on port %"PRIu16": %s", dpif_name(dpif),
756 (msg->type == _ODPL_MISS_NR ? "miss"
757 : msg->type == _ODPL_ACTION_NR ? "action"
759 payload_len, msg->port, s);
768 /* Discards all messages that would otherwise be received by dpif_recv() on
769 * 'dpif'. Returns 0 if successful, otherwise a positive errno value. */
771 dpif_recv_purge(struct dpif *dpif)
773 struct odp_stats stats;
777 COVERAGE_INC(dpif_purge);
779 error = dpif_get_dp_stats(dpif, &stats);
784 for (i = 0; i < stats.max_miss_queue + stats.max_action_queue; i++) {
786 error = dpif_recv(dpif, &buf);
788 return error == EAGAIN ? 0 : error;
795 /* Arranges for the poll loop to wake up when 'dpif' has a message queued to be
796 * received with dpif_recv(). */
798 dpif_recv_wait(struct dpif *dpif)
800 dpif->class->recv_wait(dpif);
803 /* Obtains the NetFlow engine type and engine ID for 'dpif' into '*engine_type'
804 * and '*engine_id', respectively. */
806 dpif_get_netflow_ids(const struct dpif *dpif,
807 uint8_t *engine_type, uint8_t *engine_id)
809 *engine_type = dpif->netflow_engine_type;
810 *engine_id = dpif->netflow_engine_id;
814 dpif_init(struct dpif *dpif, const struct dpif_class *class, const char *name,
815 uint8_t netflow_engine_type, uint8_t netflow_engine_id)
818 dpif->name = xstrdup(name);
819 dpif->netflow_engine_type = netflow_engine_type;
820 dpif->netflow_engine_id = netflow_engine_id;
824 log_operation(const struct dpif *dpif, const char *operation, int error)
827 VLOG_DBG_RL(&dpmsg_rl, "%s: %s success", dpif_name(dpif), operation);
829 VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
830 dpif_name(dpif), operation, strerror(error));
834 static enum vlog_level
835 flow_message_log_level(int error)
837 return error ? VLL_WARN : VLL_DBG;
841 should_log_flow_message(int error)
843 return !vlog_should_drop(THIS_MODULE, flow_message_log_level(error),
844 error ? &error_rl : &dpmsg_rl);
848 log_flow_message(const struct dpif *dpif, int error, const char *operation,
849 const flow_t *flow, const struct odp_flow_stats *stats,
850 const union odp_action *actions, size_t n_actions)
852 struct ds ds = DS_EMPTY_INITIALIZER;
853 ds_put_format(&ds, "%s: ", dpif_name(dpif));
855 ds_put_cstr(&ds, "failed to ");
857 ds_put_format(&ds, "%s ", operation);
859 ds_put_format(&ds, "(%s) ", strerror(error));
861 flow_format(&ds, flow);
863 ds_put_cstr(&ds, ", ");
864 format_odp_flow_stats(&ds, stats);
866 if (actions || n_actions) {
867 ds_put_cstr(&ds, ", actions:");
868 format_odp_actions(&ds, actions, n_actions);
870 vlog(THIS_MODULE, flow_message_log_level(error), "%s", ds_cstr(&ds));
875 log_flow_operation(const struct dpif *dpif, const char *operation, int error,
876 struct odp_flow *flow)
881 log_flow_message(dpif, error, operation, &flow->key,
882 !error ? &flow->stats : NULL,
883 flow->actions, flow->n_actions);
887 log_flow_put(struct dpif *dpif, int error, const struct odp_flow_put *put)
889 enum { ODPPF_ALL = ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS };
893 ds_put_cstr(&s, "put");
894 if (put->flags & ODPPF_CREATE) {
895 ds_put_cstr(&s, "[create]");
897 if (put->flags & ODPPF_MODIFY) {
898 ds_put_cstr(&s, "[modify]");
900 if (put->flags & ODPPF_ZERO_STATS) {
901 ds_put_cstr(&s, "[zero]");
903 if (put->flags & ~ODPPF_ALL) {
904 ds_put_format(&s, "[%x]", put->flags & ~ODPPF_ALL);
906 log_flow_message(dpif, error, ds_cstr(&s), &put->flow.key,
907 !error ? &put->flow.stats : NULL,
908 put->flow.actions, put->flow.n_actions);
912 /* There is a tendency to construct odp_flow objects on the stack and to
913 * forget to properly initialize their "actions" and "n_actions" members.
914 * When this happens, we get memory corruption because the kernel
915 * writes through the random pointer that is in the "actions" member.
917 * This function attempts to combat the problem by:
919 * - Forcing a segfault if "actions" points to an invalid region (instead
920 * of just getting back EFAULT, which can be easily missed in the log).
922 * - Storing a distinctive value that is likely to cause an
923 * easy-to-identify error later if it is dereferenced, etc.
925 * - Triggering a warning on uninitialized memory from Valgrind if
926 * "actions" or "n_actions" was not initialized.
929 check_rw_odp_flow(struct odp_flow *flow)
931 if (flow->n_actions) {
932 memset(&flow->actions[0], 0xcc, sizeof flow->actions[0]);
937 #include <linux/rtnetlink.h>
938 #include <linux/ethtool.h>
939 #include <linux/sockios.h>
940 #include <netinet/in.h>
941 #include <sys/ioctl.h>
942 #include <sys/stat.h>
943 #include <sys/sysmacros.h>
948 struct nl_sock *sock;
953 dpifmon_create(const char *datapath_name, struct dpifmon **monp)
956 char local_name[IFNAMSIZ];
959 mon = *monp = xmalloc(sizeof *mon);
961 error = dpif_open(datapath_name, &mon->dpif);
965 error = dpif_port_get_name(mon->dpif, ODPP_LOCAL,
966 local_name, sizeof local_name);
968 goto error_close_dpif;
971 mon->local_ifindex = if_nametoindex(local_name);
972 if (!mon->local_ifindex) {
974 VLOG_WARN("could not get ifindex of %s device: %s",
975 local_name, strerror(errno));
976 goto error_close_dpif;
979 error = nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0, &mon->sock);
981 VLOG_WARN("could not create rtnetlink socket: %s", strerror(error));
982 goto error_close_dpif;
988 dpif_close(mon->dpif);
996 dpifmon_destroy(struct dpifmon *mon)
999 dpif_close(mon->dpif);
1000 nl_sock_destroy(mon->sock);
1005 dpifmon_poll(struct dpifmon *mon, char **devnamep)
1007 static struct vlog_rate_limit slow_rl = VLOG_RATE_LIMIT_INIT(1, 5);
1008 static const struct nl_policy rtnlgrp_link_policy[] = {
1009 [IFLA_IFNAME] = { .type = NL_A_STRING },
1010 [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
1012 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
1018 error = nl_sock_recv(mon->sock, &buf, false);
1021 if (!nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
1022 rtnlgrp_link_policy,
1023 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
1024 VLOG_WARN_RL(&slow_rl, "received bad rtnl message");
1027 const char *devname = nl_attr_get_string(attrs[IFLA_IFNAME]);
1030 if (attrs[IFLA_MASTER]) {
1031 uint32_t master_ifindex = nl_attr_get_u32(attrs[IFLA_MASTER]);
1032 for_us = master_ifindex == mon->local_ifindex;
1034 /* It's for us if that device is one of our ports. */
1035 struct odp_port port;
1036 for_us = !dpif_port_query_by_name(mon->dpif, devname, &port);
1040 /* Not for us, try again. */
1042 COVERAGE_INC(dpifmon_poll_false_wakeup);
1045 COVERAGE_INC(dpifmon_poll_changed);
1046 *devnamep = xstrdup(devname);
1052 /* Nothing to do. */
1056 VLOG_WARN_RL(&slow_rl, "dpifmon socket overflowed");
1060 VLOG_WARN_RL(&slow_rl, "error on dpifmon socket: %s", strerror(error));
1067 dpifmon_run(struct dpifmon *mon UNUSED)
1069 /* Nothing to do in this implementation. */
1073 dpifmon_wait(struct dpifmon *mon)
1075 nl_sock_wait(mon->sock, POLLIN);