2 * Copyright (c) 2008, 2009, 2010, 2011 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"
33 #include "ofp-errors.h"
34 #include "ofp-print.h"
38 #include "poll-loop.h"
46 VLOG_DEFINE_THIS_MODULE(dpif
);
48 COVERAGE_DEFINE(dpif_destroy
);
49 COVERAGE_DEFINE(dpif_port_add
);
50 COVERAGE_DEFINE(dpif_port_del
);
51 COVERAGE_DEFINE(dpif_flow_flush
);
52 COVERAGE_DEFINE(dpif_flow_get
);
53 COVERAGE_DEFINE(dpif_flow_put
);
54 COVERAGE_DEFINE(dpif_flow_del
);
55 COVERAGE_DEFINE(dpif_flow_query_list
);
56 COVERAGE_DEFINE(dpif_flow_query_list_n
);
57 COVERAGE_DEFINE(dpif_execute
);
58 COVERAGE_DEFINE(dpif_purge
);
60 static const struct dpif_class
*base_dpif_classes
[] = {
67 struct registered_dpif_class
{
68 const struct dpif_class
*dpif_class
;
71 static struct shash dpif_classes
= SHASH_INITIALIZER(&dpif_classes
);
72 static struct sset dpif_blacklist
= SSET_INITIALIZER(&dpif_blacklist
);
74 /* Rate limit for individual messages going to or from the datapath, output at
75 * DBG level. This is very high because, if these are enabled, it is because
76 * we really need to see them. */
77 static struct vlog_rate_limit dpmsg_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
79 /* Not really much point in logging many dpif errors. */
80 static struct vlog_rate_limit error_rl
= VLOG_RATE_LIMIT_INIT(60, 5);
82 static void log_flow_message(const struct dpif
*dpif
, int error
,
83 const char *operation
,
84 const struct nlattr
*key
, size_t key_len
,
85 const struct dpif_flow_stats
*stats
,
86 const struct nlattr
*actions
, size_t actions_len
);
87 static void log_operation(const struct dpif
*, const char *operation
,
89 static bool should_log_flow_message(int error
);
94 static int status
= -1;
100 for (i
= 0; i
< ARRAY_SIZE(base_dpif_classes
); i
++) {
101 dp_register_provider(base_dpif_classes
[i
]);
106 /* Registers a new datapath provider. After successful registration, new
107 * datapaths of that type can be opened using dpif_open(). */
109 dp_register_provider(const struct dpif_class
*new_class
)
111 struct registered_dpif_class
*registered_class
;
113 if (sset_contains(&dpif_blacklist
, new_class
->type
)) {
114 VLOG_DBG("attempted to register blacklisted provider: %s",
119 if (shash_find(&dpif_classes
, new_class
->type
)) {
120 VLOG_WARN("attempted to register duplicate datapath provider: %s",
125 registered_class
= xmalloc(sizeof *registered_class
);
126 registered_class
->dpif_class
= new_class
;
127 registered_class
->refcount
= 0;
129 shash_add(&dpif_classes
, new_class
->type
, registered_class
);
134 /* Unregisters a datapath provider. 'type' must have been previously
135 * registered and not currently be in use by any dpifs. After unregistration
136 * new datapaths of that type cannot be opened using dpif_open(). */
138 dp_unregister_provider(const char *type
)
140 struct shash_node
*node
;
141 struct registered_dpif_class
*registered_class
;
143 node
= shash_find(&dpif_classes
, type
);
145 VLOG_WARN("attempted to unregister a datapath provider that is not "
146 "registered: %s", type
);
150 registered_class
= node
->data
;
151 if (registered_class
->refcount
) {
152 VLOG_WARN("attempted to unregister in use datapath provider: %s", type
);
156 shash_delete(&dpif_classes
, node
);
157 free(registered_class
);
162 /* Blacklists a provider. Causes future calls of dp_register_provider() with
163 * a dpif_class which implements 'type' to fail. */
165 dp_blacklist_provider(const char *type
)
167 sset_add(&dpif_blacklist
, type
);
170 /* Clears 'types' and enumerates the types of all currently registered datapath
171 * providers into it. The caller must first initialize the sset. */
173 dp_enumerate_types(struct sset
*types
)
175 struct shash_node
*node
;
180 SHASH_FOR_EACH(node
, &dpif_classes
) {
181 const struct registered_dpif_class
*registered_class
= node
->data
;
182 sset_add(types
, registered_class
->dpif_class
->type
);
186 /* Clears 'names' and enumerates the names of all known created datapaths with
187 * the given 'type'. The caller must first initialize the sset. Returns 0 if
188 * successful, otherwise a positive errno value.
190 * Some kinds of datapaths might not be practically enumerable. This is not
191 * considered an error. */
193 dp_enumerate_names(const char *type
, struct sset
*names
)
195 const struct registered_dpif_class
*registered_class
;
196 const struct dpif_class
*dpif_class
;
202 registered_class
= shash_find_data(&dpif_classes
, type
);
203 if (!registered_class
) {
204 VLOG_WARN("could not enumerate unknown type: %s", type
);
208 dpif_class
= registered_class
->dpif_class
;
209 error
= dpif_class
->enumerate
? dpif_class
->enumerate(names
) : 0;
212 VLOG_WARN("failed to enumerate %s datapaths: %s", dpif_class
->type
,
219 /* Parses 'datapath_name_', which is of the form [type@]name into its
220 * component pieces. 'name' and 'type' must be freed by the caller.
222 * The returned 'type' is normalized, as if by dpif_normalize_type(). */
224 dp_parse_name(const char *datapath_name_
, char **name
, char **type
)
226 char *datapath_name
= xstrdup(datapath_name_
);
229 separator
= strchr(datapath_name
, '@');
232 *type
= datapath_name
;
233 *name
= xstrdup(dpif_normalize_type(separator
+ 1));
235 *name
= datapath_name
;
236 *type
= xstrdup(dpif_normalize_type(NULL
));
241 do_open(const char *name
, const char *type
, bool create
, struct dpif
**dpifp
)
243 struct dpif
*dpif
= NULL
;
245 struct registered_dpif_class
*registered_class
;
249 type
= dpif_normalize_type(type
);
251 registered_class
= shash_find_data(&dpif_classes
, type
);
252 if (!registered_class
) {
253 VLOG_WARN("could not create datapath %s of unknown type %s", name
,
255 error
= EAFNOSUPPORT
;
259 error
= registered_class
->dpif_class
->open(registered_class
->dpif_class
,
260 name
, create
, &dpif
);
262 assert(dpif
->dpif_class
== registered_class
->dpif_class
);
263 registered_class
->refcount
++;
267 *dpifp
= error
? NULL
: dpif
;
271 /* Tries to open an existing datapath named 'name' and type 'type'. Will fail
272 * if no datapath with 'name' and 'type' exists. 'type' may be either NULL or
273 * the empty string to specify the default system type. Returns 0 if
274 * successful, otherwise a positive errno value. On success stores a pointer
275 * to the datapath in '*dpifp', otherwise a null pointer. */
277 dpif_open(const char *name
, const char *type
, struct dpif
**dpifp
)
279 return do_open(name
, type
, false, dpifp
);
282 /* Tries to create and open a new datapath with the given 'name' and 'type'.
283 * 'type' may be either NULL or the empty string to specify the default system
284 * type. Will fail if a datapath with 'name' and 'type' already exists.
285 * Returns 0 if successful, otherwise a positive errno value. On success
286 * stores a pointer to the datapath in '*dpifp', otherwise a null pointer. */
288 dpif_create(const char *name
, const char *type
, struct dpif
**dpifp
)
290 return do_open(name
, type
, true, dpifp
);
293 /* Tries to open a datapath with the given 'name' and 'type', creating it if it
294 * does not exist. 'type' may be either NULL or the empty string to specify
295 * the default system type. Returns 0 if successful, otherwise a positive
296 * errno value. On success stores a pointer to the datapath in '*dpifp',
297 * otherwise a null pointer. */
299 dpif_create_and_open(const char *name
, const char *type
, struct dpif
**dpifp
)
303 error
= dpif_create(name
, type
, dpifp
);
304 if (error
== EEXIST
|| error
== EBUSY
) {
305 error
= dpif_open(name
, type
, dpifp
);
307 VLOG_WARN("datapath %s already exists but cannot be opened: %s",
308 name
, strerror(error
));
311 VLOG_WARN("failed to create datapath %s: %s", name
, strerror(error
));
316 /* Closes and frees the connection to 'dpif'. Does not destroy the datapath
317 * itself; call dpif_delete() first, instead, if that is desirable. */
319 dpif_close(struct dpif
*dpif
)
322 struct registered_dpif_class
*registered_class
;
324 registered_class
= shash_find_data(&dpif_classes
,
325 dpif
->dpif_class
->type
);
326 assert(registered_class
);
327 assert(registered_class
->refcount
);
329 registered_class
->refcount
--;
330 dpif_uninit(dpif
, true);
334 /* Performs periodic work needed by 'dpif'. */
336 dpif_run(struct dpif
*dpif
)
338 if (dpif
->dpif_class
->run
) {
339 dpif
->dpif_class
->run(dpif
);
343 /* Arranges for poll_block() to wake up when dp_run() needs to be called for
346 dpif_wait(struct dpif
*dpif
)
348 if (dpif
->dpif_class
->wait
) {
349 dpif
->dpif_class
->wait(dpif
);
353 /* Returns the name of datapath 'dpif' prefixed with the type
354 * (for use in log messages). */
356 dpif_name(const struct dpif
*dpif
)
358 return dpif
->full_name
;
361 /* Returns the name of datapath 'dpif' without the type
362 * (for use in device names). */
364 dpif_base_name(const struct dpif
*dpif
)
366 return dpif
->base_name
;
369 /* Returns the fully spelled out name for the given datapath 'type'.
371 * Normalized type string can be compared with strcmp(). Unnormalized type
372 * string might be the same even if they have different spellings. */
374 dpif_normalize_type(const char *type
)
376 return type
&& type
[0] ? type
: "system";
379 /* Destroys the datapath that 'dpif' is connected to, first removing all of its
380 * ports. After calling this function, it does not make sense to pass 'dpif'
381 * to any functions other than dpif_name() or dpif_close(). */
383 dpif_delete(struct dpif
*dpif
)
387 COVERAGE_INC(dpif_destroy
);
389 error
= dpif
->dpif_class
->destroy(dpif
);
390 log_operation(dpif
, "delete", error
);
394 /* Retrieves statistics for 'dpif' into 'stats'. Returns 0 if successful,
395 * otherwise a positive errno value. */
397 dpif_get_dp_stats(const struct dpif
*dpif
, struct dpif_dp_stats
*stats
)
399 int error
= dpif
->dpif_class
->get_stats(dpif
, stats
);
401 memset(stats
, 0, sizeof *stats
);
403 log_operation(dpif
, "get_stats", error
);
407 /* Attempts to add 'netdev' as a port on 'dpif'. If successful, returns 0 and
408 * sets '*port_nop' to the new port's port number (if 'port_nop' is non-null).
409 * On failure, returns a positive errno value and sets '*port_nop' to
410 * UINT16_MAX (if 'port_nop' is non-null). */
412 dpif_port_add(struct dpif
*dpif
, struct netdev
*netdev
, uint16_t *port_nop
)
414 const char *netdev_name
= netdev_get_name(netdev
);
418 COVERAGE_INC(dpif_port_add
);
420 error
= dpif
->dpif_class
->port_add(dpif
, netdev
, &port_no
);
422 VLOG_DBG_RL(&dpmsg_rl
, "%s: added %s as port %"PRIu16
,
423 dpif_name(dpif
), netdev_name
, port_no
);
425 VLOG_WARN_RL(&error_rl
, "%s: failed to add %s as port: %s",
426 dpif_name(dpif
), netdev_name
, strerror(error
));
427 port_no
= UINT16_MAX
;
435 /* Attempts to remove 'dpif''s port number 'port_no'. Returns 0 if successful,
436 * otherwise a positive errno value. */
438 dpif_port_del(struct dpif
*dpif
, uint16_t port_no
)
442 COVERAGE_INC(dpif_port_del
);
444 error
= dpif
->dpif_class
->port_del(dpif
, port_no
);
446 VLOG_DBG_RL(&dpmsg_rl
, "%s: port_del(%"PRIu16
")",
447 dpif_name(dpif
), port_no
);
449 log_operation(dpif
, "port_del", error
);
454 /* Makes a deep copy of 'src' into 'dst'. */
456 dpif_port_clone(struct dpif_port
*dst
, const struct dpif_port
*src
)
458 dst
->name
= xstrdup(src
->name
);
459 dst
->type
= xstrdup(src
->type
);
460 dst
->port_no
= src
->port_no
;
463 /* Frees memory allocated to members of 'dpif_port'.
465 * Do not call this function on a dpif_port obtained from
466 * dpif_port_dump_next(): that function retains ownership of the data in the
469 dpif_port_destroy(struct dpif_port
*dpif_port
)
471 free(dpif_port
->name
);
472 free(dpif_port
->type
);
475 /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and
476 * initializes '*port' appropriately; on failure, returns a positive errno
479 * The caller owns the data in 'port' and must free it with
480 * dpif_port_destroy() when it is no longer needed. */
482 dpif_port_query_by_number(const struct dpif
*dpif
, uint16_t port_no
,
483 struct dpif_port
*port
)
485 int error
= dpif
->dpif_class
->port_query_by_number(dpif
, port_no
, port
);
487 VLOG_DBG_RL(&dpmsg_rl
, "%s: port %"PRIu16
" is device %s",
488 dpif_name(dpif
), port_no
, port
->name
);
490 memset(port
, 0, sizeof *port
);
491 VLOG_WARN_RL(&error_rl
, "%s: failed to query port %"PRIu16
": %s",
492 dpif_name(dpif
), port_no
, strerror(error
));
497 /* Looks up port named 'devname' in 'dpif'. On success, returns 0 and
498 * initializes '*port' appropriately; on failure, returns a positive errno
501 * The caller owns the data in 'port' and must free it with
502 * dpif_port_destroy() when it is no longer needed. */
504 dpif_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
505 struct dpif_port
*port
)
507 int error
= dpif
->dpif_class
->port_query_by_name(dpif
, devname
, port
);
509 VLOG_DBG_RL(&dpmsg_rl
, "%s: device %s is on port %"PRIu16
,
510 dpif_name(dpif
), devname
, port
->port_no
);
512 memset(port
, 0, sizeof *port
);
514 /* For ENOENT or ENODEV we use DBG level because the caller is probably
515 * interested in whether 'dpif' actually has a port 'devname', so that
516 * it's not an issue worth logging if it doesn't. Other errors are
517 * uncommon and more likely to indicate a real problem. */
519 error
== ENOENT
|| error
== ENODEV
? VLL_DBG
: VLL_WARN
,
520 "%s: failed to query port %s: %s",
521 dpif_name(dpif
), devname
, strerror(error
));
526 /* Returns one greater than the maximum port number accepted in flow
529 dpif_get_max_ports(const struct dpif
*dpif
)
531 return dpif
->dpif_class
->get_max_ports(dpif
);
534 /* Returns the Netlink PID value to supply in OVS_ACTION_ATTR_USERSPACE actions
535 * as the OVS_USERSPACE_ATTR_PID attribute's value, for use in flows whose
536 * packets arrived on port 'port_no'.
538 * The return value is only meaningful when DPIF_UC_ACTION has been enabled in
539 * the 'dpif''s listen mask. It is allowed to change when DPIF_UC_ACTION is
540 * disabled and then re-enabled, so a client that does that must be prepared to
541 * update all of the flows that it installed that contain
542 * OVS_ACTION_ATTR_USERSPACE actions. */
544 dpif_port_get_pid(const struct dpif
*dpif
, uint16_t port_no
)
546 return (dpif
->dpif_class
->port_get_pid
547 ? (dpif
->dpif_class
->port_get_pid
)(dpif
, port_no
)
551 /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and copies
552 * the port's name into the 'name_size' bytes in 'name', ensuring that the
553 * result is null-terminated. On failure, returns a positive errno value and
554 * makes 'name' the empty string. */
556 dpif_port_get_name(struct dpif
*dpif
, uint16_t port_no
,
557 char *name
, size_t name_size
)
559 struct dpif_port port
;
562 assert(name_size
> 0);
564 error
= dpif_port_query_by_number(dpif
, port_no
, &port
);
566 ovs_strlcpy(name
, port
.name
, name_size
);
567 dpif_port_destroy(&port
);
574 /* Initializes 'dump' to begin dumping the ports in a dpif.
576 * This function provides no status indication. An error status for the entire
577 * dump operation is provided when it is completed by calling
578 * dpif_port_dump_done().
581 dpif_port_dump_start(struct dpif_port_dump
*dump
, const struct dpif
*dpif
)
584 dump
->error
= dpif
->dpif_class
->port_dump_start(dpif
, &dump
->state
);
585 log_operation(dpif
, "port_dump_start", dump
->error
);
588 /* Attempts to retrieve another port from 'dump', which must have been
589 * initialized with dpif_port_dump_start(). On success, stores a new dpif_port
590 * into 'port' and returns true. On failure, returns false.
592 * Failure might indicate an actual error or merely that the last port has been
593 * dumped. An error status for the entire dump operation is provided when it
594 * is completed by calling dpif_port_dump_done().
596 * The dpif owns the data stored in 'port'. It will remain valid until at
597 * least the next time 'dump' is passed to dpif_port_dump_next() or
598 * dpif_port_dump_done(). */
600 dpif_port_dump_next(struct dpif_port_dump
*dump
, struct dpif_port
*port
)
602 const struct dpif
*dpif
= dump
->dpif
;
608 dump
->error
= dpif
->dpif_class
->port_dump_next(dpif
, dump
->state
, port
);
609 if (dump
->error
== EOF
) {
610 VLOG_DBG_RL(&dpmsg_rl
, "%s: dumped all ports", dpif_name(dpif
));
612 log_operation(dpif
, "port_dump_next", dump
->error
);
616 dpif
->dpif_class
->port_dump_done(dpif
, dump
->state
);
622 /* Completes port table dump operation 'dump', which must have been initialized
623 * with dpif_port_dump_start(). Returns 0 if the dump operation was
624 * error-free, otherwise a positive errno value describing the problem. */
626 dpif_port_dump_done(struct dpif_port_dump
*dump
)
628 const struct dpif
*dpif
= dump
->dpif
;
630 dump
->error
= dpif
->dpif_class
->port_dump_done(dpif
, dump
->state
);
631 log_operation(dpif
, "port_dump_done", dump
->error
);
633 return dump
->error
== EOF
? 0 : dump
->error
;
636 /* Polls for changes in the set of ports in 'dpif'. If the set of ports in
637 * 'dpif' has changed, this function does one of the following:
639 * - Stores the name of the device that was added to or deleted from 'dpif' in
640 * '*devnamep' and returns 0. The caller is responsible for freeing
641 * '*devnamep' (with free()) when it no longer needs it.
643 * - Returns ENOBUFS and sets '*devnamep' to NULL.
645 * This function may also return 'false positives', where it returns 0 and
646 * '*devnamep' names a device that was not actually added or deleted or it
647 * returns ENOBUFS without any change.
649 * Returns EAGAIN if the set of ports in 'dpif' has not changed. May also
650 * return other positive errno values to indicate that something has gone
653 dpif_port_poll(const struct dpif
*dpif
, char **devnamep
)
655 int error
= dpif
->dpif_class
->port_poll(dpif
, devnamep
);
662 /* Arranges for the poll loop to wake up when port_poll(dpif) will return a
663 * value other than EAGAIN. */
665 dpif_port_poll_wait(const struct dpif
*dpif
)
667 dpif
->dpif_class
->port_poll_wait(dpif
);
670 /* Extracts the flow stats for a packet. The 'flow' and 'packet'
671 * arguments must have been initialized through a call to flow_extract().
674 dpif_flow_stats_extract(const struct flow
*flow
, struct ofpbuf
*packet
,
675 struct dpif_flow_stats
*stats
)
677 memset(stats
, 0, sizeof(*stats
));
679 if ((flow
->dl_type
== htons(ETH_TYPE_IP
)) && packet
->l4
) {
680 if ((flow
->nw_proto
== IPPROTO_TCP
) && packet
->l7
) {
681 struct tcp_header
*tcp
= packet
->l4
;
682 stats
->tcp_flags
= TCP_FLAGS(tcp
->tcp_ctl
);
686 stats
->n_bytes
= packet
->size
;
687 stats
->n_packets
= 1;
690 /* Appends a human-readable representation of 'stats' to 's'. */
692 dpif_flow_stats_format(const struct dpif_flow_stats
*stats
, struct ds
*s
)
694 ds_put_format(s
, "packets:%"PRIu64
", bytes:%"PRIu64
", used:",
695 stats
->n_packets
, stats
->n_bytes
);
697 ds_put_format(s
, "%.3fs", (time_msec() - stats
->used
) / 1000.0);
699 ds_put_format(s
, "never");
704 /* Deletes all flows from 'dpif'. Returns 0 if successful, otherwise a
705 * positive errno value. */
707 dpif_flow_flush(struct dpif
*dpif
)
711 COVERAGE_INC(dpif_flow_flush
);
713 error
= dpif
->dpif_class
->flow_flush(dpif
);
714 log_operation(dpif
, "flow_flush", error
);
718 /* Queries 'dpif' for a flow entry. The flow is specified by the Netlink
719 * attributes with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at
722 * Returns 0 if successful. If no flow matches, returns ENOENT. On other
723 * failure, returns a positive errno value.
725 * If 'actionsp' is nonnull, then on success '*actionsp' will be set to an
726 * ofpbuf owned by the caller that contains the Netlink attributes for the
727 * flow's actions. The caller must free the ofpbuf (with ofpbuf_delete()) when
728 * it is no longer needed.
730 * If 'stats' is nonnull, then on success it will be updated with the flow's
733 dpif_flow_get(const struct dpif
*dpif
,
734 const struct nlattr
*key
, size_t key_len
,
735 struct ofpbuf
**actionsp
, struct dpif_flow_stats
*stats
)
739 COVERAGE_INC(dpif_flow_get
);
741 error
= dpif
->dpif_class
->flow_get(dpif
, key
, key_len
, actionsp
, stats
);
747 memset(stats
, 0, sizeof *stats
);
750 if (should_log_flow_message(error
)) {
751 const struct nlattr
*actions
;
754 if (!error
&& actionsp
) {
755 actions
= (*actionsp
)->data
;
756 actions_len
= (*actionsp
)->size
;
761 log_flow_message(dpif
, error
, "flow_get", key
, key_len
, stats
,
762 actions
, actions_len
);
767 /* Adds or modifies a flow in 'dpif'. The flow is specified by the Netlink
768 * attributes with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at
769 * 'key'. The associated actions are specified by the Netlink attributes with
770 * types OVS_ACTION_ATTR_* in the 'actions_len' bytes starting at 'actions'.
772 * - If the flow's key does not exist in 'dpif', then the flow will be added if
773 * 'flags' includes DPIF_FP_CREATE. Otherwise the operation will fail with
776 * If the operation succeeds, then 'stats', if nonnull, will be zeroed.
778 * - If the flow's key does exist in 'dpif', then the flow's actions will be
779 * updated if 'flags' includes DPIF_FP_MODIFY. Otherwise the operation will
780 * fail with EEXIST. If the flow's actions are updated, then its statistics
781 * will be zeroed if 'flags' includes DPIF_FP_ZERO_STATS, and left as-is
784 * If the operation succeeds, then 'stats', if nonnull, will be set to the
785 * flow's statistics before the update.
788 dpif_flow_put(struct dpif
*dpif
, enum dpif_flow_put_flags flags
,
789 const struct nlattr
*key
, size_t key_len
,
790 const struct nlattr
*actions
, size_t actions_len
,
791 struct dpif_flow_stats
*stats
)
795 COVERAGE_INC(dpif_flow_put
);
796 assert(!(flags
& ~(DPIF_FP_CREATE
| DPIF_FP_MODIFY
| DPIF_FP_ZERO_STATS
)));
798 error
= dpif
->dpif_class
->flow_put(dpif
, flags
, key
, key_len
,
799 actions
, actions_len
, stats
);
800 if (error
&& stats
) {
801 memset(stats
, 0, sizeof *stats
);
803 if (should_log_flow_message(error
)) {
807 ds_put_cstr(&s
, "put");
808 if (flags
& DPIF_FP_CREATE
) {
809 ds_put_cstr(&s
, "[create]");
811 if (flags
& DPIF_FP_MODIFY
) {
812 ds_put_cstr(&s
, "[modify]");
814 if (flags
& DPIF_FP_ZERO_STATS
) {
815 ds_put_cstr(&s
, "[zero]");
817 log_flow_message(dpif
, error
, ds_cstr(&s
), key
, key_len
, stats
,
818 actions
, actions_len
);
824 /* Deletes a flow from 'dpif' and returns 0, or returns ENOENT if 'dpif' does
825 * not contain such a flow. The flow is specified by the Netlink attributes
826 * with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at 'key'.
828 * If the operation succeeds, then 'stats', if nonnull, will be set to the
829 * flow's statistics before its deletion. */
831 dpif_flow_del(struct dpif
*dpif
,
832 const struct nlattr
*key
, size_t key_len
,
833 struct dpif_flow_stats
*stats
)
837 COVERAGE_INC(dpif_flow_del
);
839 error
= dpif
->dpif_class
->flow_del(dpif
, key
, key_len
, stats
);
840 if (error
&& stats
) {
841 memset(stats
, 0, sizeof *stats
);
843 if (should_log_flow_message(error
)) {
844 log_flow_message(dpif
, error
, "flow_del", key
, key_len
,
845 !error
? stats
: NULL
, NULL
, 0);
850 /* Initializes 'dump' to begin dumping the flows in a dpif.
852 * This function provides no status indication. An error status for the entire
853 * dump operation is provided when it is completed by calling
854 * dpif_flow_dump_done().
857 dpif_flow_dump_start(struct dpif_flow_dump
*dump
, const struct dpif
*dpif
)
860 dump
->error
= dpif
->dpif_class
->flow_dump_start(dpif
, &dump
->state
);
861 log_operation(dpif
, "flow_dump_start", dump
->error
);
864 /* Attempts to retrieve another flow from 'dump', which must have been
865 * initialized with dpif_flow_dump_start(). On success, updates the output
866 * parameters as described below and returns true. Otherwise, returns false.
867 * Failure might indicate an actual error or merely the end of the flow table.
868 * An error status for the entire dump operation is provided when it is
869 * completed by calling dpif_flow_dump_done().
871 * On success, if 'key' and 'key_len' are nonnull then '*key' and '*key_len'
872 * will be set to Netlink attributes with types OVS_KEY_ATTR_* representing the
873 * dumped flow's key. If 'actions' and 'actions_len' are nonnull then they are
874 * set to Netlink attributes with types OVS_ACTION_ATTR_* representing the
875 * dumped flow's actions. If 'stats' is nonnull then it will be set to the
876 * dumped flow's statistics.
878 * All of the returned data is owned by 'dpif', not by the caller, and the
879 * caller must not modify or free it. 'dpif' guarantees that it remains
880 * accessible and unchanging until at least the next call to 'flow_dump_next'
881 * or 'flow_dump_done' for 'dump'. */
883 dpif_flow_dump_next(struct dpif_flow_dump
*dump
,
884 const struct nlattr
**key
, size_t *key_len
,
885 const struct nlattr
**actions
, size_t *actions_len
,
886 const struct dpif_flow_stats
**stats
)
888 const struct dpif
*dpif
= dump
->dpif
;
889 int error
= dump
->error
;
892 error
= dpif
->dpif_class
->flow_dump_next(dpif
, dump
->state
,
894 actions
, actions_len
,
897 dpif
->dpif_class
->flow_dump_done(dpif
, dump
->state
);
915 VLOG_DBG_RL(&dpmsg_rl
, "%s: dumped all flows", dpif_name(dpif
));
916 } else if (should_log_flow_message(error
)) {
917 log_flow_message(dpif
, error
, "flow_dump",
918 key
? *key
: NULL
, key
? *key_len
: 0,
919 stats
? *stats
: NULL
, actions
? *actions
: NULL
,
920 actions
? *actions_len
: 0);
927 /* Completes flow table dump operation 'dump', which must have been initialized
928 * with dpif_flow_dump_start(). Returns 0 if the dump operation was
929 * error-free, otherwise a positive errno value describing the problem. */
931 dpif_flow_dump_done(struct dpif_flow_dump
*dump
)
933 const struct dpif
*dpif
= dump
->dpif
;
935 dump
->error
= dpif
->dpif_class
->flow_dump_done(dpif
, dump
->state
);
936 log_operation(dpif
, "flow_dump_done", dump
->error
);
938 return dump
->error
== EOF
? 0 : dump
->error
;
941 /* Causes 'dpif' to perform the 'actions_len' bytes of actions in 'actions' on
942 * the Ethernet frame specified in 'packet' taken from the flow specified in
943 * the 'key_len' bytes of 'key'. ('key' is mostly redundant with 'packet', but
944 * it contains some metadata that cannot be recovered from 'packet', such as
945 * tun_id and in_port.)
947 * Returns 0 if successful, otherwise a positive errno value. */
949 dpif_execute(struct dpif
*dpif
,
950 const struct nlattr
*key
, size_t key_len
,
951 const struct nlattr
*actions
, size_t actions_len
,
952 const struct ofpbuf
*buf
)
956 COVERAGE_INC(dpif_execute
);
957 if (actions_len
> 0) {
958 error
= dpif
->dpif_class
->execute(dpif
, key
, key_len
,
959 actions
, actions_len
, buf
);
964 if (!(error
? VLOG_DROP_WARN(&error_rl
) : VLOG_DROP_DBG(&dpmsg_rl
))) {
965 struct ds ds
= DS_EMPTY_INITIALIZER
;
966 char *packet
= ofp_packet_to_string(buf
->data
, buf
->size
);
967 ds_put_format(&ds
, "%s: execute ", dpif_name(dpif
));
968 format_odp_actions(&ds
, actions
, actions_len
);
970 ds_put_format(&ds
, " failed (%s)", strerror(error
));
972 ds_put_format(&ds
, " on packet %s", packet
);
973 vlog(THIS_MODULE
, error
? VLL_WARN
: VLL_DBG
, "%s", ds_cstr(&ds
));
980 /* Executes each of the 'n_ops' operations in 'ops' on 'dpif', in the order in
981 * which they are specified, placing each operation's results in the "output"
982 * members documented in comments.
984 * This function exists because some datapaths can perform batched operations
985 * faster than individual operations. */
987 dpif_operate(struct dpif
*dpif
, union dpif_op
**ops
, size_t n_ops
)
991 if (dpif
->dpif_class
->operate
) {
992 dpif
->dpif_class
->operate(dpif
, ops
, n_ops
);
996 for (i
= 0; i
< n_ops
; i
++) {
997 union dpif_op
*op
= ops
[i
];
998 struct dpif_flow_put
*put
;
999 struct dpif_execute
*execute
;
1002 case DPIF_OP_FLOW_PUT
:
1003 put
= &op
->flow_put
;
1004 put
->error
= dpif_flow_put(dpif
, put
->flags
,
1005 put
->key
, put
->key_len
,
1006 put
->actions
, put
->actions_len
,
1010 case DPIF_OP_EXECUTE
:
1011 execute
= &op
->execute
;
1012 execute
->error
= dpif_execute(dpif
, execute
->key
, execute
->key_len
,
1014 execute
->actions_len
,
1025 /* Returns a string that represents 'type', for use in log messages. */
1027 dpif_upcall_type_to_string(enum dpif_upcall_type type
)
1030 case DPIF_UC_MISS
: return "miss";
1031 case DPIF_UC_ACTION
: return "action";
1032 case DPIF_N_UC_TYPES
: default: return "<unknown>";
1036 /* Enables or disables receiving packets with dpif_recv() on 'dpif'. Returns 0
1037 * if successful, otherwise a positive errno value.
1039 * Turning packet receive off and then back on may change the Netlink PID
1040 * assignments returned by dpif_port_get_pid(). If the client does this, it
1041 * must update all of the flows that have OVS_ACTION_ATTR_USERSPACE actions
1042 * using the new PID assignment. */
1044 dpif_recv_set(struct dpif
*dpif
, bool enable
)
1046 int error
= dpif
->dpif_class
->recv_set(dpif
, enable
);
1047 log_operation(dpif
, "recv_set", error
);
1051 /* Polls for an upcall from 'dpif'. If successful, stores the upcall into
1052 * '*upcall'. Should only be called if dpif_recv_set() has been used to enable
1053 * receiving packets on 'dpif'.
1055 * The caller takes ownership of the data that 'upcall' points to.
1056 * 'upcall->key' and 'upcall->actions' (if nonnull) point into data owned by
1057 * 'upcall->packet', so their memory cannot be freed separately. (This is
1058 * hardly a great way to do things but it works out OK for the dpif providers
1059 * and clients that exist so far.)
1061 * Returns 0 if successful, otherwise a positive errno value. Returns EAGAIN
1062 * if no upcall is immediately available. */
1064 dpif_recv(struct dpif
*dpif
, struct dpif_upcall
*upcall
)
1066 int error
= dpif
->dpif_class
->recv(dpif
, upcall
);
1067 if (!error
&& !VLOG_DROP_DBG(&dpmsg_rl
)) {
1071 packet
= ofp_packet_to_string(upcall
->packet
->data
,
1072 upcall
->packet
->size
);
1075 odp_flow_key_format(upcall
->key
, upcall
->key_len
, &flow
);
1077 VLOG_DBG("%s: %s upcall:\n%s\n%s",
1078 dpif_name(dpif
), dpif_upcall_type_to_string(upcall
->type
),
1079 ds_cstr(&flow
), packet
);
1083 } else if (error
&& error
!= EAGAIN
) {
1084 log_operation(dpif
, "recv", error
);
1089 /* Discards all messages that would otherwise be received by dpif_recv() on
1092 dpif_recv_purge(struct dpif
*dpif
)
1094 COVERAGE_INC(dpif_purge
);
1095 if (dpif
->dpif_class
->recv_purge
) {
1096 dpif
->dpif_class
->recv_purge(dpif
);
1100 /* Arranges for the poll loop to wake up when 'dpif' has a message queued to be
1101 * received with dpif_recv(). */
1103 dpif_recv_wait(struct dpif
*dpif
)
1105 dpif
->dpif_class
->recv_wait(dpif
);
1108 /* Obtains the NetFlow engine type and engine ID for 'dpif' into '*engine_type'
1109 * and '*engine_id', respectively. */
1111 dpif_get_netflow_ids(const struct dpif
*dpif
,
1112 uint8_t *engine_type
, uint8_t *engine_id
)
1114 *engine_type
= dpif
->netflow_engine_type
;
1115 *engine_id
= dpif
->netflow_engine_id
;
1118 /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a priority
1119 * value used for setting packet priority.
1120 * On success, returns 0 and stores the priority into '*priority'.
1121 * On failure, returns a positive errno value and stores 0 into '*priority'. */
1123 dpif_queue_to_priority(const struct dpif
*dpif
, uint32_t queue_id
,
1126 int error
= (dpif
->dpif_class
->queue_to_priority
1127 ? dpif
->dpif_class
->queue_to_priority(dpif
, queue_id
,
1133 log_operation(dpif
, "queue_to_priority", error
);
1138 dpif_init(struct dpif
*dpif
, const struct dpif_class
*dpif_class
,
1140 uint8_t netflow_engine_type
, uint8_t netflow_engine_id
)
1142 dpif
->dpif_class
= dpif_class
;
1143 dpif
->base_name
= xstrdup(name
);
1144 dpif
->full_name
= xasprintf("%s@%s", dpif_class
->type
, name
);
1145 dpif
->netflow_engine_type
= netflow_engine_type
;
1146 dpif
->netflow_engine_id
= netflow_engine_id
;
1149 /* Undoes the results of initialization.
1151 * Normally this function only needs to be called from dpif_close().
1152 * However, it may be called by providers due to an error on opening
1153 * that occurs after initialization. It this case dpif_close() would
1154 * never be called. */
1156 dpif_uninit(struct dpif
*dpif
, bool close
)
1158 char *base_name
= dpif
->base_name
;
1159 char *full_name
= dpif
->full_name
;
1162 dpif
->dpif_class
->close(dpif
);
1170 log_operation(const struct dpif
*dpif
, const char *operation
, int error
)
1173 VLOG_DBG_RL(&dpmsg_rl
, "%s: %s success", dpif_name(dpif
), operation
);
1174 } else if (ofperr_is_valid(error
)) {
1175 VLOG_WARN_RL(&error_rl
, "%s: %s failed (%s)",
1176 dpif_name(dpif
), operation
, ofperr_get_name(error
));
1178 VLOG_WARN_RL(&error_rl
, "%s: %s failed (%s)",
1179 dpif_name(dpif
), operation
, strerror(error
));
1183 static enum vlog_level
1184 flow_message_log_level(int error
)
1186 return error
? VLL_WARN
: VLL_DBG
;
1190 should_log_flow_message(int error
)
1192 return !vlog_should_drop(THIS_MODULE
, flow_message_log_level(error
),
1193 error
? &error_rl
: &dpmsg_rl
);
1197 log_flow_message(const struct dpif
*dpif
, int error
, const char *operation
,
1198 const struct nlattr
*key
, size_t key_len
,
1199 const struct dpif_flow_stats
*stats
,
1200 const struct nlattr
*actions
, size_t actions_len
)
1202 struct ds ds
= DS_EMPTY_INITIALIZER
;
1203 ds_put_format(&ds
, "%s: ", dpif_name(dpif
));
1205 ds_put_cstr(&ds
, "failed to ");
1207 ds_put_format(&ds
, "%s ", operation
);
1209 ds_put_format(&ds
, "(%s) ", strerror(error
));
1211 odp_flow_key_format(key
, key_len
, &ds
);
1213 ds_put_cstr(&ds
, ", ");
1214 dpif_flow_stats_format(stats
, &ds
);
1216 if (actions
|| actions_len
) {
1217 ds_put_cstr(&ds
, ", actions:");
1218 format_odp_actions(&ds
, actions
, actions_len
);
1220 vlog(THIS_MODULE
, flow_message_log_level(error
), "%s", ds_cstr(&ds
));