2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
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 "dp-packet.h"
29 #include "dpif-netdev.h"
30 #include "openvswitch/dynamic-string.h"
34 #include "odp-execute.h"
36 #include "openvswitch/ofp-print.h"
37 #include "openvswitch/ofpbuf.h"
39 #include "openvswitch/poll-loop.h"
40 #include "route-table.h"
42 #include "openvswitch/shash.h"
45 #include "tnl-neigh-cache.h"
46 #include "tnl-ports.h"
50 #include "openvswitch/ofp-errors.h"
51 #include "openvswitch/vlog.h"
52 #include "lib/netdev-provider.h"
54 VLOG_DEFINE_THIS_MODULE(dpif
);
56 COVERAGE_DEFINE(dpif_destroy
);
57 COVERAGE_DEFINE(dpif_port_add
);
58 COVERAGE_DEFINE(dpif_port_del
);
59 COVERAGE_DEFINE(dpif_flow_flush
);
60 COVERAGE_DEFINE(dpif_flow_get
);
61 COVERAGE_DEFINE(dpif_flow_put
);
62 COVERAGE_DEFINE(dpif_flow_del
);
63 COVERAGE_DEFINE(dpif_execute
);
64 COVERAGE_DEFINE(dpif_purge
);
65 COVERAGE_DEFINE(dpif_execute_with_help
);
66 COVERAGE_DEFINE(dpif_meter_set
);
67 COVERAGE_DEFINE(dpif_meter_get
);
68 COVERAGE_DEFINE(dpif_meter_del
);
70 static const struct dpif_class
*base_dpif_classes
[] = {
71 #if defined(__linux__) || defined(_WIN32)
77 struct registered_dpif_class
{
78 const struct dpif_class
*dpif_class
;
81 static struct shash dpif_classes
= SHASH_INITIALIZER(&dpif_classes
);
82 static struct sset dpif_blacklist
= SSET_INITIALIZER(&dpif_blacklist
);
84 /* Protects 'dpif_classes', including the refcount, and 'dpif_blacklist'. */
85 static struct ovs_mutex dpif_mutex
= OVS_MUTEX_INITIALIZER
;
87 /* Rate limit for individual messages going to or from the datapath, output at
88 * DBG level. This is very high because, if these are enabled, it is because
89 * we really need to see them. */
90 static struct vlog_rate_limit dpmsg_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
92 /* Not really much point in logging many dpif errors. */
93 static struct vlog_rate_limit error_rl
= VLOG_RATE_LIMIT_INIT(60, 5);
95 static void log_operation(const struct dpif
*, const char *operation
,
97 static bool should_log_flow_message(const struct vlog_module
*module
,
100 /* Incremented whenever tnl route, arp, etc changes. */
101 struct seq
*tnl_conf_seq
;
104 dpif_is_tap_port(const char *type
)
106 return !strcmp(type
, "tap");
112 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
114 if (ovsthread_once_start(&once
)) {
117 tnl_conf_seq
= seq_create();
118 dpctl_unixctl_register();
120 tnl_neigh_cache_init();
123 for (i
= 0; i
< ARRAY_SIZE(base_dpif_classes
); i
++) {
124 dp_register_provider(base_dpif_classes
[i
]);
127 ovsthread_once_done(&once
);
132 dp_register_provider__(const struct dpif_class
*new_class
)
134 struct registered_dpif_class
*registered_class
;
137 if (sset_contains(&dpif_blacklist
, new_class
->type
)) {
138 VLOG_DBG("attempted to register blacklisted provider: %s",
143 if (shash_find(&dpif_classes
, new_class
->type
)) {
144 VLOG_WARN("attempted to register duplicate datapath provider: %s",
149 error
= new_class
->init
? new_class
->init() : 0;
151 VLOG_WARN("failed to initialize %s datapath class: %s",
152 new_class
->type
, ovs_strerror(error
));
156 registered_class
= xmalloc(sizeof *registered_class
);
157 registered_class
->dpif_class
= new_class
;
158 registered_class
->refcount
= 0;
160 shash_add(&dpif_classes
, new_class
->type
, registered_class
);
165 /* Registers a new datapath provider. After successful registration, new
166 * datapaths of that type can be opened using dpif_open(). */
168 dp_register_provider(const struct dpif_class
*new_class
)
172 ovs_mutex_lock(&dpif_mutex
);
173 error
= dp_register_provider__(new_class
);
174 ovs_mutex_unlock(&dpif_mutex
);
179 /* Unregisters a datapath provider. 'type' must have been previously
180 * registered and not currently be in use by any dpifs. After unregistration
181 * new datapaths of that type cannot be opened using dpif_open(). */
183 dp_unregister_provider__(const char *type
)
185 struct shash_node
*node
;
186 struct registered_dpif_class
*registered_class
;
188 node
= shash_find(&dpif_classes
, type
);
193 registered_class
= node
->data
;
194 if (registered_class
->refcount
) {
195 VLOG_WARN("attempted to unregister in use datapath provider: %s", type
);
199 shash_delete(&dpif_classes
, node
);
200 free(registered_class
);
205 /* Unregisters a datapath provider. 'type' must have been previously
206 * registered and not currently be in use by any dpifs. After unregistration
207 * new datapaths of that type cannot be opened using dpif_open(). */
209 dp_unregister_provider(const char *type
)
215 ovs_mutex_lock(&dpif_mutex
);
216 error
= dp_unregister_provider__(type
);
217 ovs_mutex_unlock(&dpif_mutex
);
222 /* Blacklists a provider. Causes future calls of dp_register_provider() with
223 * a dpif_class which implements 'type' to fail. */
225 dp_blacklist_provider(const char *type
)
227 ovs_mutex_lock(&dpif_mutex
);
228 sset_add(&dpif_blacklist
, type
);
229 ovs_mutex_unlock(&dpif_mutex
);
232 /* Adds the types of all currently registered datapath providers to 'types'.
233 * The caller must first initialize the sset. */
235 dp_enumerate_types(struct sset
*types
)
237 struct shash_node
*node
;
241 ovs_mutex_lock(&dpif_mutex
);
242 SHASH_FOR_EACH(node
, &dpif_classes
) {
243 const struct registered_dpif_class
*registered_class
= node
->data
;
244 sset_add(types
, registered_class
->dpif_class
->type
);
246 ovs_mutex_unlock(&dpif_mutex
);
250 dp_class_unref(struct registered_dpif_class
*rc
)
252 ovs_mutex_lock(&dpif_mutex
);
253 ovs_assert(rc
->refcount
);
255 ovs_mutex_unlock(&dpif_mutex
);
258 static struct registered_dpif_class
*
259 dp_class_lookup(const char *type
)
261 struct registered_dpif_class
*rc
;
263 ovs_mutex_lock(&dpif_mutex
);
264 rc
= shash_find_data(&dpif_classes
, type
);
268 ovs_mutex_unlock(&dpif_mutex
);
273 /* Clears 'names' and enumerates the names of all known created datapaths with
274 * the given 'type'. The caller must first initialize the sset. Returns 0 if
275 * successful, otherwise a positive errno value.
277 * Some kinds of datapaths might not be practically enumerable. This is not
278 * considered an error. */
280 dp_enumerate_names(const char *type
, struct sset
*names
)
282 struct registered_dpif_class
*registered_class
;
283 const struct dpif_class
*dpif_class
;
289 registered_class
= dp_class_lookup(type
);
290 if (!registered_class
) {
291 VLOG_WARN("could not enumerate unknown type: %s", type
);
295 dpif_class
= registered_class
->dpif_class
;
296 error
= (dpif_class
->enumerate
297 ? dpif_class
->enumerate(names
, dpif_class
)
300 VLOG_WARN("failed to enumerate %s datapaths: %s", dpif_class
->type
,
301 ovs_strerror(error
));
303 dp_class_unref(registered_class
);
308 /* Parses 'datapath_name_', which is of the form [type@]name into its
309 * component pieces. 'name' and 'type' must be freed by the caller.
311 * The returned 'type' is normalized, as if by dpif_normalize_type(). */
313 dp_parse_name(const char *datapath_name_
, char **name
, char **type
)
315 char *datapath_name
= xstrdup(datapath_name_
);
318 separator
= strchr(datapath_name
, '@');
321 *type
= datapath_name
;
322 *name
= xstrdup(dpif_normalize_type(separator
+ 1));
324 *name
= datapath_name
;
325 *type
= xstrdup(dpif_normalize_type(NULL
));
330 do_open(const char *name
, const char *type
, bool create
, struct dpif
**dpifp
)
332 struct dpif
*dpif
= NULL
;
334 struct registered_dpif_class
*registered_class
;
338 type
= dpif_normalize_type(type
);
339 registered_class
= dp_class_lookup(type
);
340 if (!registered_class
) {
341 VLOG_WARN("could not create datapath %s of unknown type %s", name
,
343 error
= EAFNOSUPPORT
;
347 error
= registered_class
->dpif_class
->open(registered_class
->dpif_class
,
348 name
, create
, &dpif
);
350 struct dpif_port_dump port_dump
;
351 struct dpif_port dpif_port
;
353 ovs_assert(dpif
->dpif_class
== registered_class
->dpif_class
);
355 DPIF_PORT_FOR_EACH(&dpif_port
, &port_dump
, dpif
) {
356 struct netdev
*netdev
;
359 if (dpif_is_tap_port(dpif_port
.type
)) {
363 err
= netdev_open(dpif_port
.name
, dpif_port
.type
, &netdev
);
366 netdev_ports_insert(netdev
, dpif
->dpif_class
, &dpif_port
);
367 netdev_close(netdev
);
369 VLOG_WARN("could not open netdev %s type %s: %s",
370 dpif_port
.name
, dpif_port
.type
, ovs_strerror(err
));
374 dp_class_unref(registered_class
);
378 *dpifp
= error
? NULL
: dpif
;
382 /* Tries to open an existing datapath named 'name' and type 'type'. Will fail
383 * if no datapath with 'name' and 'type' exists. 'type' may be either NULL or
384 * the empty string to specify the default system type. Returns 0 if
385 * successful, otherwise a positive errno value. On success stores a pointer
386 * to the datapath in '*dpifp', otherwise a null pointer. */
388 dpif_open(const char *name
, const char *type
, struct dpif
**dpifp
)
390 return do_open(name
, type
, false, dpifp
);
393 /* Tries to create and open a new datapath with the given 'name' and 'type'.
394 * 'type' may be either NULL or the empty string to specify the default system
395 * type. Will fail if a datapath with 'name' and 'type' already exists.
396 * Returns 0 if successful, otherwise a positive errno value. On success
397 * stores a pointer to the datapath in '*dpifp', otherwise a null pointer. */
399 dpif_create(const char *name
, const char *type
, struct dpif
**dpifp
)
401 return do_open(name
, type
, true, dpifp
);
404 /* Tries to open a datapath with the given 'name' and 'type', creating it if it
405 * does not exist. 'type' may be either NULL or the empty string to specify
406 * the default system type. Returns 0 if successful, otherwise a positive
407 * errno value. On success stores a pointer to the datapath in '*dpifp',
408 * otherwise a null pointer. */
410 dpif_create_and_open(const char *name
, const char *type
, struct dpif
**dpifp
)
414 error
= dpif_create(name
, type
, dpifp
);
415 if (error
== EEXIST
|| error
== EBUSY
) {
416 error
= dpif_open(name
, type
, dpifp
);
418 VLOG_WARN("datapath %s already exists but cannot be opened: %s",
419 name
, ovs_strerror(error
));
422 VLOG_WARN("failed to create datapath %s: %s",
423 name
, ovs_strerror(error
));
429 dpif_remove_netdev_ports(struct dpif
*dpif
) {
430 struct dpif_port_dump port_dump
;
431 struct dpif_port dpif_port
;
433 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, dpif
) {
434 if (!dpif_is_tap_port(dpif_port
.type
)) {
435 netdev_ports_remove(dpif_port
.port_no
, dpif
->dpif_class
);
440 /* Closes and frees the connection to 'dpif'. Does not destroy the datapath
441 * itself; call dpif_delete() first, instead, if that is desirable. */
443 dpif_close(struct dpif
*dpif
)
446 struct registered_dpif_class
*rc
;
448 rc
= shash_find_data(&dpif_classes
, dpif
->dpif_class
->type
);
450 if (rc
->refcount
== 1) {
451 dpif_remove_netdev_ports(dpif
);
453 dpif_uninit(dpif
, true);
458 /* Performs periodic work needed by 'dpif'. */
460 dpif_run(struct dpif
*dpif
)
462 if (dpif
->dpif_class
->run
) {
463 return dpif
->dpif_class
->run(dpif
);
468 /* Arranges for poll_block() to wake up when dp_run() needs to be called for
471 dpif_wait(struct dpif
*dpif
)
473 if (dpif
->dpif_class
->wait
) {
474 dpif
->dpif_class
->wait(dpif
);
478 /* Returns the name of datapath 'dpif' prefixed with the type
479 * (for use in log messages). */
481 dpif_name(const struct dpif
*dpif
)
483 return dpif
->full_name
;
486 /* Returns the name of datapath 'dpif' without the type
487 * (for use in device names). */
489 dpif_base_name(const struct dpif
*dpif
)
491 return dpif
->base_name
;
494 /* Returns the type of datapath 'dpif'. */
496 dpif_type(const struct dpif
*dpif
)
498 return dpif
->dpif_class
->type
;
501 /* Checks if datapath 'dpif' requires cleanup. */
503 dpif_cleanup_required(const struct dpif
*dpif
)
505 return dpif
->dpif_class
->cleanup_required
;
508 /* Returns the fully spelled out name for the given datapath 'type'.
510 * Normalized type string can be compared with strcmp(). Unnormalized type
511 * string might be the same even if they have different spellings. */
513 dpif_normalize_type(const char *type
)
515 return type
&& type
[0] ? type
: "system";
518 /* Destroys the datapath that 'dpif' is connected to, first removing all of its
519 * ports. After calling this function, it does not make sense to pass 'dpif'
520 * to any functions other than dpif_name() or dpif_close(). */
522 dpif_delete(struct dpif
*dpif
)
526 COVERAGE_INC(dpif_destroy
);
528 error
= dpif
->dpif_class
->destroy(dpif
);
529 log_operation(dpif
, "delete", error
);
533 /* Retrieves statistics for 'dpif' into 'stats'. Returns 0 if successful,
534 * otherwise a positive errno value. */
536 dpif_get_dp_stats(const struct dpif
*dpif
, struct dpif_dp_stats
*stats
)
538 int error
= dpif
->dpif_class
->get_stats(dpif
, stats
);
540 memset(stats
, 0, sizeof *stats
);
542 log_operation(dpif
, "get_stats", error
);
547 dpif_set_features(struct dpif
*dpif
, uint32_t new_features
)
549 int error
= dpif
->dpif_class
->set_features(dpif
, new_features
);
551 log_operation(dpif
, "set_features", error
);
556 dpif_port_open_type(const char *datapath_type
, const char *port_type
)
558 struct registered_dpif_class
*rc
;
560 datapath_type
= dpif_normalize_type(datapath_type
);
562 ovs_mutex_lock(&dpif_mutex
);
563 rc
= shash_find_data(&dpif_classes
, datapath_type
);
564 if (rc
&& rc
->dpif_class
->port_open_type
) {
565 port_type
= rc
->dpif_class
->port_open_type(rc
->dpif_class
, port_type
);
567 ovs_mutex_unlock(&dpif_mutex
);
572 /* Attempts to add 'netdev' as a port on 'dpif'. If 'port_nop' is
573 * non-null and its value is not ODPP_NONE, then attempts to use the
574 * value as the port number.
576 * If successful, returns 0 and sets '*port_nop' to the new port's port
577 * number (if 'port_nop' is non-null). On failure, returns a positive
578 * errno value and sets '*port_nop' to ODPP_NONE (if 'port_nop' is
581 dpif_port_add(struct dpif
*dpif
, struct netdev
*netdev
, odp_port_t
*port_nop
)
583 const char *netdev_name
= netdev_get_name(netdev
);
584 odp_port_t port_no
= ODPP_NONE
;
587 COVERAGE_INC(dpif_port_add
);
593 error
= dpif
->dpif_class
->port_add(dpif
, netdev
, &port_no
);
595 VLOG_DBG_RL(&dpmsg_rl
, "%s: added %s as port %"PRIu32
,
596 dpif_name(dpif
), netdev_name
, port_no
);
598 if (!dpif_is_tap_port(netdev_get_type(netdev
))) {
600 struct dpif_port dpif_port
;
602 dpif_port
.type
= CONST_CAST(char *, netdev_get_type(netdev
));
603 dpif_port
.name
= CONST_CAST(char *, netdev_name
);
604 dpif_port
.port_no
= port_no
;
605 netdev_ports_insert(netdev
, dpif
->dpif_class
, &dpif_port
);
608 VLOG_WARN_RL(&error_rl
, "%s: failed to add %s as port: %s",
609 dpif_name(dpif
), netdev_name
, ovs_strerror(error
));
618 /* Attempts to remove 'dpif''s port number 'port_no'. Returns 0 if successful,
619 * otherwise a positive errno value. */
621 dpif_port_del(struct dpif
*dpif
, odp_port_t port_no
, bool local_delete
)
625 COVERAGE_INC(dpif_port_del
);
628 error
= dpif
->dpif_class
->port_del(dpif
, port_no
);
630 VLOG_DBG_RL(&dpmsg_rl
, "%s: port_del(%"PRIu32
")",
631 dpif_name(dpif
), port_no
);
633 log_operation(dpif
, "port_del", error
);
637 netdev_ports_remove(port_no
, dpif
->dpif_class
);
641 /* Makes a deep copy of 'src' into 'dst'. */
643 dpif_port_clone(struct dpif_port
*dst
, const struct dpif_port
*src
)
645 dst
->name
= xstrdup(src
->name
);
646 dst
->type
= xstrdup(src
->type
);
647 dst
->port_no
= src
->port_no
;
650 /* Frees memory allocated to members of 'dpif_port'.
652 * Do not call this function on a dpif_port obtained from
653 * dpif_port_dump_next(): that function retains ownership of the data in the
656 dpif_port_destroy(struct dpif_port
*dpif_port
)
658 free(dpif_port
->name
);
659 free(dpif_port
->type
);
662 /* Checks if port named 'devname' exists in 'dpif'. If so, returns
663 * true; otherwise, returns false. */
665 dpif_port_exists(const struct dpif
*dpif
, const char *devname
)
667 int error
= dpif
->dpif_class
->port_query_by_name(dpif
, devname
, NULL
);
668 if (error
!= 0 && error
!= ENODEV
) {
669 VLOG_WARN_RL(&error_rl
, "%s: failed to query port %s: %s",
670 dpif_name(dpif
), devname
, ovs_strerror(error
));
676 /* Refreshes configuration of 'dpif's port. */
678 dpif_port_set_config(struct dpif
*dpif
, odp_port_t port_no
,
679 const struct smap
*cfg
)
683 if (dpif
->dpif_class
->port_set_config
) {
684 error
= dpif
->dpif_class
->port_set_config(dpif
, port_no
, cfg
);
686 log_operation(dpif
, "port_set_config", error
);
693 /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and
694 * initializes '*port' appropriately; on failure, returns a positive errno
697 * Retuns ENODEV if the port doesn't exist.
699 * The caller owns the data in 'port' and must free it with
700 * dpif_port_destroy() when it is no longer needed. */
702 dpif_port_query_by_number(const struct dpif
*dpif
, odp_port_t port_no
,
703 struct dpif_port
*port
)
705 int error
= dpif
->dpif_class
->port_query_by_number(dpif
, port_no
, port
);
707 VLOG_DBG_RL(&dpmsg_rl
, "%s: port %"PRIu32
" is device %s",
708 dpif_name(dpif
), port_no
, port
->name
);
710 memset(port
, 0, sizeof *port
);
711 VLOG_WARN_RL(&error_rl
, "%s: failed to query port %"PRIu32
": %s",
712 dpif_name(dpif
), port_no
, ovs_strerror(error
));
717 /* Looks up port named 'devname' in 'dpif'. On success, returns 0 and
718 * initializes '*port' appropriately; on failure, returns a positive errno
721 * Retuns ENODEV if the port doesn't exist.
723 * The caller owns the data in 'port' and must free it with
724 * dpif_port_destroy() when it is no longer needed. */
726 dpif_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
727 struct dpif_port
*port
)
729 int error
= dpif
->dpif_class
->port_query_by_name(dpif
, devname
, port
);
731 VLOG_DBG_RL(&dpmsg_rl
, "%s: device %s is on port %"PRIu32
,
732 dpif_name(dpif
), devname
, port
->port_no
);
734 memset(port
, 0, sizeof *port
);
736 /* For ENODEV we use DBG level because the caller is probably
737 * interested in whether 'dpif' actually has a port 'devname', so that
738 * it's not an issue worth logging if it doesn't. Other errors are
739 * uncommon and more likely to indicate a real problem. */
740 VLOG_RL(&error_rl
, error
== ENODEV
? VLL_DBG
: VLL_WARN
,
741 "%s: failed to query port %s: %s",
742 dpif_name(dpif
), devname
, ovs_strerror(error
));
747 /* Returns the Netlink PID value to supply in OVS_ACTION_ATTR_USERSPACE
748 * actions as the OVS_USERSPACE_ATTR_PID attribute's value, for use in
749 * flows whose packets arrived on port 'port_no'.
751 * A 'port_no' of ODPP_NONE is a special case: it returns a reserved PID, not
752 * allocated to any port, that the client may use for special purposes.
754 * The return value is only meaningful when DPIF_UC_ACTION has been enabled in
755 * the 'dpif''s listen mask. It is allowed to change when DPIF_UC_ACTION is
756 * disabled and then re-enabled, so a client that does that must be prepared to
757 * update all of the flows that it installed that contain
758 * OVS_ACTION_ATTR_USERSPACE actions. */
760 dpif_port_get_pid(const struct dpif
*dpif
, odp_port_t port_no
)
762 return (dpif
->dpif_class
->port_get_pid
763 ? (dpif
->dpif_class
->port_get_pid
)(dpif
, port_no
)
767 /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and copies
768 * the port's name into the 'name_size' bytes in 'name', ensuring that the
769 * result is null-terminated. On failure, returns a positive errno value and
770 * makes 'name' the empty string. */
772 dpif_port_get_name(struct dpif
*dpif
, odp_port_t port_no
,
773 char *name
, size_t name_size
)
775 struct dpif_port port
;
778 ovs_assert(name_size
> 0);
780 error
= dpif_port_query_by_number(dpif
, port_no
, &port
);
782 ovs_strlcpy(name
, port
.name
, name_size
);
783 dpif_port_destroy(&port
);
790 /* Initializes 'dump' to begin dumping the ports in a dpif.
792 * This function provides no status indication. An error status for the entire
793 * dump operation is provided when it is completed by calling
794 * dpif_port_dump_done().
797 dpif_port_dump_start(struct dpif_port_dump
*dump
, const struct dpif
*dpif
)
800 dump
->error
= dpif
->dpif_class
->port_dump_start(dpif
, &dump
->state
);
801 log_operation(dpif
, "port_dump_start", dump
->error
);
804 /* Attempts to retrieve another port from 'dump', which must have been
805 * initialized with dpif_port_dump_start(). On success, stores a new dpif_port
806 * into 'port' and returns true. On failure, returns false.
808 * Failure might indicate an actual error or merely that the last port has been
809 * dumped. An error status for the entire dump operation is provided when it
810 * is completed by calling dpif_port_dump_done().
812 * The dpif owns the data stored in 'port'. It will remain valid until at
813 * least the next time 'dump' is passed to dpif_port_dump_next() or
814 * dpif_port_dump_done(). */
816 dpif_port_dump_next(struct dpif_port_dump
*dump
, struct dpif_port
*port
)
818 const struct dpif
*dpif
= dump
->dpif
;
824 dump
->error
= dpif
->dpif_class
->port_dump_next(dpif
, dump
->state
, port
);
825 if (dump
->error
== EOF
) {
826 VLOG_DBG_RL(&dpmsg_rl
, "%s: dumped all ports", dpif_name(dpif
));
828 log_operation(dpif
, "port_dump_next", dump
->error
);
832 dpif
->dpif_class
->port_dump_done(dpif
, dump
->state
);
838 /* Completes port table dump operation 'dump', which must have been initialized
839 * with dpif_port_dump_start(). Returns 0 if the dump operation was
840 * error-free, otherwise a positive errno value describing the problem. */
842 dpif_port_dump_done(struct dpif_port_dump
*dump
)
844 const struct dpif
*dpif
= dump
->dpif
;
846 dump
->error
= dpif
->dpif_class
->port_dump_done(dpif
, dump
->state
);
847 log_operation(dpif
, "port_dump_done", dump
->error
);
849 return dump
->error
== EOF
? 0 : dump
->error
;
852 /* Polls for changes in the set of ports in 'dpif'. If the set of ports in
853 * 'dpif' has changed, this function does one of the following:
855 * - Stores the name of the device that was added to or deleted from 'dpif' in
856 * '*devnamep' and returns 0. The caller is responsible for freeing
857 * '*devnamep' (with free()) when it no longer needs it.
859 * - Returns ENOBUFS and sets '*devnamep' to NULL.
861 * This function may also return 'false positives', where it returns 0 and
862 * '*devnamep' names a device that was not actually added or deleted or it
863 * returns ENOBUFS without any change.
865 * Returns EAGAIN if the set of ports in 'dpif' has not changed. May also
866 * return other positive errno values to indicate that something has gone
869 dpif_port_poll(const struct dpif
*dpif
, char **devnamep
)
871 int error
= dpif
->dpif_class
->port_poll(dpif
, devnamep
);
878 /* Arranges for the poll loop to wake up when port_poll(dpif) will return a
879 * value other than EAGAIN. */
881 dpif_port_poll_wait(const struct dpif
*dpif
)
883 dpif
->dpif_class
->port_poll_wait(dpif
);
886 /* Extracts the flow stats for a packet. The 'flow' and 'packet'
887 * arguments must have been initialized through a call to flow_extract().
888 * 'used' is stored into stats->used. */
890 dpif_flow_stats_extract(const struct flow
*flow
, const struct dp_packet
*packet
,
891 long long int used
, struct dpif_flow_stats
*stats
)
893 stats
->tcp_flags
= ntohs(flow
->tcp_flags
);
894 stats
->n_bytes
= dp_packet_size(packet
);
895 stats
->n_packets
= 1;
899 /* Appends a human-readable representation of 'stats' to 's'. */
901 dpif_flow_stats_format(const struct dpif_flow_stats
*stats
, struct ds
*s
)
903 ds_put_format(s
, "packets:%"PRIu64
", bytes:%"PRIu64
", used:",
904 stats
->n_packets
, stats
->n_bytes
);
906 ds_put_format(s
, "%.3fs", (time_msec() - stats
->used
) / 1000.0);
908 ds_put_format(s
, "never");
910 if (stats
->tcp_flags
) {
911 ds_put_cstr(s
, ", flags:");
912 packet_format_tcp_flags(s
, stats
->tcp_flags
);
916 /* Places the hash of the 'key_len' bytes starting at 'key' into '*hash'. */
918 dpif_flow_hash(const struct dpif
*dpif OVS_UNUSED
,
919 const void *key
, size_t key_len
, ovs_u128
*hash
)
921 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
922 static uint32_t secret
;
924 if (ovsthread_once_start(&once
)) {
925 secret
= random_uint32();
926 ovsthread_once_done(&once
);
928 hash_bytes128(key
, key_len
, secret
, hash
);
929 uuid_set_bits_v4((struct uuid
*)hash
);
932 /* Deletes all flows from 'dpif'. Returns 0 if successful, otherwise a
933 * positive errno value. */
935 dpif_flow_flush(struct dpif
*dpif
)
939 COVERAGE_INC(dpif_flow_flush
);
941 error
= dpif
->dpif_class
->flow_flush(dpif
);
942 log_operation(dpif
, "flow_flush", error
);
946 /* Attempts to install 'key' into the datapath, fetches it, then deletes it.
947 * Returns true if the datapath supported installing 'flow', false otherwise.
950 dpif_probe_feature(struct dpif
*dpif
, const char *name
,
951 const struct ofpbuf
*key
, const struct ofpbuf
*actions
,
952 const ovs_u128
*ufid
)
954 struct dpif_flow flow
;
956 uint64_t stub
[DPIF_FLOW_BUFSIZE
/ 8];
957 bool enable_feature
= false;
959 const struct nlattr
*nl_actions
= actions
? actions
->data
: NULL
;
960 const size_t nl_actions_size
= actions
? actions
->size
: 0;
962 /* Use DPIF_FP_MODIFY to cover the case where ovs-vswitchd is killed (and
963 * restarted) at just the right time such that feature probes from the
964 * previous run are still present in the datapath. */
965 error
= dpif_flow_put(dpif
, DPIF_FP_CREATE
| DPIF_FP_MODIFY
| DPIF_FP_PROBE
,
966 key
->data
, key
->size
, NULL
, 0,
967 nl_actions
, nl_actions_size
,
968 ufid
, NON_PMD_CORE_ID
, NULL
);
970 if (error
!= EINVAL
&& error
!= EOVERFLOW
) {
971 VLOG_WARN("%s: %s flow probe failed (%s)",
972 dpif_name(dpif
), name
, ovs_strerror(error
));
977 ofpbuf_use_stack(&reply
, &stub
, sizeof stub
);
978 error
= dpif_flow_get(dpif
, key
->data
, key
->size
, ufid
,
979 NON_PMD_CORE_ID
, &reply
, &flow
);
981 && (!ufid
|| (flow
.ufid_present
982 && ovs_u128_equals(*ufid
, flow
.ufid
)))) {
983 enable_feature
= true;
986 error
= dpif_flow_del(dpif
, key
->data
, key
->size
, ufid
,
987 NON_PMD_CORE_ID
, NULL
);
989 VLOG_WARN("%s: failed to delete %s feature probe flow",
990 dpif_name(dpif
), name
);
993 return enable_feature
;
996 /* A dpif_operate() wrapper for performing a single DPIF_OP_FLOW_GET. */
998 dpif_flow_get(struct dpif
*dpif
,
999 const struct nlattr
*key
, size_t key_len
, const ovs_u128
*ufid
,
1000 const unsigned pmd_id
, struct ofpbuf
*buf
, struct dpif_flow
*flow
)
1002 struct dpif_op
*opp
;
1005 op
.type
= DPIF_OP_FLOW_GET
;
1006 op
.flow_get
.key
= key
;
1007 op
.flow_get
.key_len
= key_len
;
1008 op
.flow_get
.ufid
= ufid
;
1009 op
.flow_get
.pmd_id
= pmd_id
;
1010 op
.flow_get
.buffer
= buf
;
1012 memset(flow
, 0, sizeof *flow
);
1013 op
.flow_get
.flow
= flow
;
1014 op
.flow_get
.flow
->key
= key
;
1015 op
.flow_get
.flow
->key_len
= key_len
;
1018 dpif_operate(dpif
, &opp
, 1, DPIF_OFFLOAD_AUTO
);
1023 /* A dpif_operate() wrapper for performing a single DPIF_OP_FLOW_PUT. */
1025 dpif_flow_put(struct dpif
*dpif
, enum dpif_flow_put_flags flags
,
1026 const struct nlattr
*key
, size_t key_len
,
1027 const struct nlattr
*mask
, size_t mask_len
,
1028 const struct nlattr
*actions
, size_t actions_len
,
1029 const ovs_u128
*ufid
, const unsigned pmd_id
,
1030 struct dpif_flow_stats
*stats
)
1032 struct dpif_op
*opp
;
1035 op
.type
= DPIF_OP_FLOW_PUT
;
1036 op
.flow_put
.flags
= flags
;
1037 op
.flow_put
.key
= key
;
1038 op
.flow_put
.key_len
= key_len
;
1039 op
.flow_put
.mask
= mask
;
1040 op
.flow_put
.mask_len
= mask_len
;
1041 op
.flow_put
.actions
= actions
;
1042 op
.flow_put
.actions_len
= actions_len
;
1043 op
.flow_put
.ufid
= ufid
;
1044 op
.flow_put
.pmd_id
= pmd_id
;
1045 op
.flow_put
.stats
= stats
;
1048 dpif_operate(dpif
, &opp
, 1, DPIF_OFFLOAD_AUTO
);
1053 /* A dpif_operate() wrapper for performing a single DPIF_OP_FLOW_DEL. */
1055 dpif_flow_del(struct dpif
*dpif
,
1056 const struct nlattr
*key
, size_t key_len
, const ovs_u128
*ufid
,
1057 const unsigned pmd_id
, struct dpif_flow_stats
*stats
)
1059 struct dpif_op
*opp
;
1062 op
.type
= DPIF_OP_FLOW_DEL
;
1063 op
.flow_del
.key
= key
;
1064 op
.flow_del
.key_len
= key_len
;
1065 op
.flow_del
.ufid
= ufid
;
1066 op
.flow_del
.pmd_id
= pmd_id
;
1067 op
.flow_del
.stats
= stats
;
1068 op
.flow_del
.terse
= false;
1071 dpif_operate(dpif
, &opp
, 1, DPIF_OFFLOAD_AUTO
);
1076 /* Creates and returns a new 'struct dpif_flow_dump' for iterating through the
1077 * flows in 'dpif'. If 'terse' is true, then only UFID and statistics will
1078 * be returned in the dump. Otherwise, all fields will be returned.
1080 * This function always successfully returns a dpif_flow_dump. Error
1081 * reporting is deferred to dpif_flow_dump_destroy(). */
1082 struct dpif_flow_dump
*
1083 dpif_flow_dump_create(const struct dpif
*dpif
, bool terse
,
1084 struct dpif_flow_dump_types
*types
)
1086 return dpif
->dpif_class
->flow_dump_create(dpif
, terse
, types
);
1089 /* Destroys 'dump', which must have been created with dpif_flow_dump_create().
1090 * All dpif_flow_dump_thread structures previously created for 'dump' must
1091 * previously have been destroyed.
1093 * Returns 0 if the dump operation was error-free, otherwise a positive errno
1094 * value describing the problem. */
1096 dpif_flow_dump_destroy(struct dpif_flow_dump
*dump
)
1098 const struct dpif
*dpif
= dump
->dpif
;
1099 int error
= dpif
->dpif_class
->flow_dump_destroy(dump
);
1100 log_operation(dpif
, "flow_dump_destroy", error
);
1101 return error
== EOF
? 0 : error
;
1104 /* Returns new thread-local state for use with dpif_flow_dump_next(). */
1105 struct dpif_flow_dump_thread
*
1106 dpif_flow_dump_thread_create(struct dpif_flow_dump
*dump
)
1108 return dump
->dpif
->dpif_class
->flow_dump_thread_create(dump
);
1111 /* Releases 'thread'. */
1113 dpif_flow_dump_thread_destroy(struct dpif_flow_dump_thread
*thread
)
1115 thread
->dpif
->dpif_class
->flow_dump_thread_destroy(thread
);
1118 /* Attempts to retrieve up to 'max_flows' more flows from 'thread'. Returns 0
1119 * if and only if no flows remained to be retrieved, otherwise a positive
1120 * number reflecting the number of elements in 'flows[]' that were updated.
1121 * The number of flows returned might be less than 'max_flows' because
1122 * fewer than 'max_flows' remained, because this particular datapath does not
1123 * benefit from batching, or because an error occurred partway through
1124 * retrieval. Thus, the caller should continue calling until a 0 return value,
1125 * even if intermediate return values are less than 'max_flows'.
1127 * No error status is immediately provided. An error status for the entire
1128 * dump operation is provided when it is completed by calling
1129 * dpif_flow_dump_destroy().
1131 * All of the data stored into 'flows' is owned by the datapath, not by the
1132 * caller, and the caller must not modify or free it. The datapath guarantees
1133 * that it remains accessible and unchanged until the first of:
1134 * - The next call to dpif_flow_dump_next() for 'thread', or
1135 * - The next rcu quiescent period. */
1137 dpif_flow_dump_next(struct dpif_flow_dump_thread
*thread
,
1138 struct dpif_flow
*flows
, int max_flows
)
1140 struct dpif
*dpif
= thread
->dpif
;
1143 ovs_assert(max_flows
> 0);
1144 n
= dpif
->dpif_class
->flow_dump_next(thread
, flows
, max_flows
);
1146 struct dpif_flow
*f
;
1148 for (f
= flows
; f
< &flows
[n
]
1149 && should_log_flow_message(&this_module
, 0); f
++) {
1150 log_flow_message(dpif
, 0, &this_module
, "flow_dump",
1151 f
->key
, f
->key_len
, f
->mask
, f
->mask_len
,
1152 &f
->ufid
, &f
->stats
, f
->actions
, f
->actions_len
);
1155 VLOG_DBG_RL(&dpmsg_rl
, "%s: dumped all flows", dpif_name(dpif
));
1160 struct dpif_execute_helper_aux
{
1162 const struct flow
*flow
;
1164 const struct nlattr
*meter_action
; /* Non-NULL, if have a meter action. */
1167 /* This is called for actions that need the context of the datapath to be
1170 dpif_execute_helper_cb(void *aux_
, struct dp_packet_batch
*packets_
,
1171 const struct nlattr
*action
, bool should_steal
)
1173 struct dpif_execute_helper_aux
*aux
= aux_
;
1174 int type
= nl_attr_type(action
);
1175 struct dp_packet
*packet
= packets_
->packets
[0];
1177 ovs_assert(dp_packet_batch_size(packets_
) == 1);
1179 switch ((enum ovs_action_attr
)type
) {
1180 case OVS_ACTION_ATTR_METER
:
1181 /* Maintain a pointer to the first meter action seen. */
1182 if (!aux
->meter_action
) {
1183 aux
->meter_action
= action
;
1187 case OVS_ACTION_ATTR_CT
:
1188 case OVS_ACTION_ATTR_OUTPUT
:
1189 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
1190 case OVS_ACTION_ATTR_TUNNEL_POP
:
1191 case OVS_ACTION_ATTR_USERSPACE
:
1192 case OVS_ACTION_ATTR_RECIRC
: {
1193 struct dpif_execute execute
;
1194 struct ofpbuf execute_actions
;
1195 uint64_t stub
[256 / 8];
1196 struct pkt_metadata
*md
= &packet
->md
;
1198 if (flow_tnl_dst_is_set(&md
->tunnel
) || aux
->meter_action
) {
1199 ofpbuf_use_stub(&execute_actions
, stub
, sizeof stub
);
1201 if (aux
->meter_action
) {
1202 const struct nlattr
*a
= aux
->meter_action
;
1204 /* XXX: This code collects meter actions since the last action
1205 * execution via the datapath to be executed right before the
1206 * current action that needs to be executed by the datapath.
1207 * This is only an approximation, but better than nothing.
1208 * Fundamentally, we should have a mechanism by which the
1209 * datapath could return the result of the meter action so that
1210 * we could execute them at the right order. */
1212 ofpbuf_put(&execute_actions
, a
, NLA_ALIGN(a
->nla_len
));
1213 /* Find next meter action before 'action', if any. */
1215 a
= nl_attr_next(a
);
1216 } while (a
!= action
&&
1217 nl_attr_type(a
) != OVS_ACTION_ATTR_METER
);
1218 } while (a
!= action
);
1221 /* The Linux kernel datapath throws away the tunnel information
1222 * that we supply as metadata. We have to use a "set" action to
1224 if (md
->tunnel
.ip_dst
) {
1225 odp_put_tunnel_action(&md
->tunnel
, &execute_actions
, NULL
);
1227 ofpbuf_put(&execute_actions
, action
, NLA_ALIGN(action
->nla_len
));
1229 execute
.actions
= execute_actions
.data
;
1230 execute
.actions_len
= execute_actions
.size
;
1232 execute
.actions
= action
;
1233 execute
.actions_len
= NLA_ALIGN(action
->nla_len
);
1236 struct dp_packet
*clone
= NULL
;
1237 uint32_t cutlen
= dp_packet_get_cutlen(packet
);
1238 if (cutlen
&& (type
== OVS_ACTION_ATTR_OUTPUT
1239 || type
== OVS_ACTION_ATTR_TUNNEL_PUSH
1240 || type
== OVS_ACTION_ATTR_TUNNEL_POP
1241 || type
== OVS_ACTION_ATTR_USERSPACE
)) {
1242 dp_packet_reset_cutlen(packet
);
1243 if (!should_steal
) {
1244 packet
= clone
= dp_packet_clone(packet
);
1246 dp_packet_set_size(packet
, dp_packet_size(packet
) - cutlen
);
1249 execute
.packet
= packet
;
1250 execute
.flow
= aux
->flow
;
1251 execute
.needs_help
= false;
1252 execute
.probe
= false;
1254 aux
->error
= dpif_execute(aux
->dpif
, &execute
);
1255 log_execute_message(aux
->dpif
, &this_module
, &execute
,
1258 dp_packet_delete(clone
);
1260 if (flow_tnl_dst_is_set(&md
->tunnel
) || aux
->meter_action
) {
1261 ofpbuf_uninit(&execute_actions
);
1263 /* Do not re-use the same meters for later output actions. */
1264 aux
->meter_action
= NULL
;
1269 case OVS_ACTION_ATTR_HASH
:
1270 case OVS_ACTION_ATTR_PUSH_VLAN
:
1271 case OVS_ACTION_ATTR_POP_VLAN
:
1272 case OVS_ACTION_ATTR_PUSH_MPLS
:
1273 case OVS_ACTION_ATTR_POP_MPLS
:
1274 case OVS_ACTION_ATTR_SET
:
1275 case OVS_ACTION_ATTR_SET_MASKED
:
1276 case OVS_ACTION_ATTR_SAMPLE
:
1277 case OVS_ACTION_ATTR_TRUNC
:
1278 case OVS_ACTION_ATTR_PUSH_ETH
:
1279 case OVS_ACTION_ATTR_POP_ETH
:
1280 case OVS_ACTION_ATTR_CLONE
:
1281 case OVS_ACTION_ATTR_PUSH_NSH
:
1282 case OVS_ACTION_ATTR_POP_NSH
:
1283 case OVS_ACTION_ATTR_CT_CLEAR
:
1284 case OVS_ACTION_ATTR_UNSPEC
:
1285 case OVS_ACTION_ATTR_CHECK_PKT_LEN
:
1286 case __OVS_ACTION_ATTR_MAX
:
1289 dp_packet_delete_batch(packets_
, should_steal
);
1292 /* Executes 'execute' by performing most of the actions in userspace and
1293 * passing the fully constructed packets to 'dpif' for output and userspace
1296 * This helps with actions that a given 'dpif' doesn't implement directly. */
1298 dpif_execute_with_help(struct dpif
*dpif
, struct dpif_execute
*execute
)
1300 struct dpif_execute_helper_aux aux
= {dpif
, execute
->flow
, 0, NULL
};
1301 struct dp_packet_batch pb
;
1303 COVERAGE_INC(dpif_execute_with_help
);
1305 dp_packet_batch_init_packet(&pb
, execute
->packet
);
1306 odp_execute_actions(&aux
, &pb
, false, execute
->actions
,
1307 execute
->actions_len
, dpif_execute_helper_cb
);
1311 /* Returns true if the datapath needs help executing 'execute'. */
1313 dpif_execute_needs_help(const struct dpif_execute
*execute
)
1315 return execute
->needs_help
|| nl_attr_oversized(execute
->actions_len
);
1318 /* A dpif_operate() wrapper for performing a single DPIF_OP_EXECUTE. */
1320 dpif_execute(struct dpif
*dpif
, struct dpif_execute
*execute
)
1322 if (execute
->actions_len
) {
1323 struct dpif_op
*opp
;
1326 op
.type
= DPIF_OP_EXECUTE
;
1327 op
.execute
= *execute
;
1330 dpif_operate(dpif
, &opp
, 1, DPIF_OFFLOAD_AUTO
);
1338 /* Executes each of the 'n_ops' operations in 'ops' on 'dpif', in the order in
1339 * which they are specified. Places each operation's results in the "output"
1340 * members documented in comments, and 0 in the 'error' member on success or a
1341 * positive errno on failure.
1344 dpif_operate(struct dpif
*dpif
, struct dpif_op
**ops
, size_t n_ops
,
1345 enum dpif_offload_type offload_type
)
1347 if (offload_type
== DPIF_OFFLOAD_ALWAYS
&& !netdev_is_flow_api_enabled()) {
1349 for (i
= 0; i
< n_ops
; i
++) {
1350 struct dpif_op
*op
= ops
[i
];
1359 /* Count 'chunk', the number of ops that can be executed without
1360 * needing any help. Ops that need help should be rare, so we
1361 * expect this to ordinarily be 'n_ops', that is, all the ops. */
1362 for (chunk
= 0; chunk
< n_ops
; chunk
++) {
1363 struct dpif_op
*op
= ops
[chunk
];
1365 if (op
->type
== DPIF_OP_EXECUTE
1366 && dpif_execute_needs_help(&op
->execute
)) {
1372 /* Execute a chunk full of ops that the dpif provider can
1373 * handle itself, without help. */
1376 dpif
->dpif_class
->operate(dpif
, ops
, chunk
, offload_type
);
1378 for (i
= 0; i
< chunk
; i
++) {
1379 struct dpif_op
*op
= ops
[i
];
1380 int error
= op
->error
;
1383 case DPIF_OP_FLOW_PUT
: {
1384 struct dpif_flow_put
*put
= &op
->flow_put
;
1386 COVERAGE_INC(dpif_flow_put
);
1387 log_flow_put_message(dpif
, &this_module
, put
, error
);
1388 if (error
&& put
->stats
) {
1389 memset(put
->stats
, 0, sizeof *put
->stats
);
1394 case DPIF_OP_FLOW_GET
: {
1395 struct dpif_flow_get
*get
= &op
->flow_get
;
1397 COVERAGE_INC(dpif_flow_get
);
1399 memset(get
->flow
, 0, sizeof *get
->flow
);
1401 log_flow_get_message(dpif
, &this_module
, get
, error
);
1406 case DPIF_OP_FLOW_DEL
: {
1407 struct dpif_flow_del
*del
= &op
->flow_del
;
1409 COVERAGE_INC(dpif_flow_del
);
1410 log_flow_del_message(dpif
, &this_module
, del
, error
);
1411 if (error
&& del
->stats
) {
1412 memset(del
->stats
, 0, sizeof *del
->stats
);
1417 case DPIF_OP_EXECUTE
:
1418 COVERAGE_INC(dpif_execute
);
1419 log_execute_message(dpif
, &this_module
, &op
->execute
,
1428 /* Help the dpif provider to execute one op. */
1429 struct dpif_op
*op
= ops
[0];
1431 COVERAGE_INC(dpif_execute
);
1432 op
->error
= dpif_execute_with_help(dpif
, &op
->execute
);
1439 /* Returns a string that represents 'type', for use in log messages. */
1441 dpif_upcall_type_to_string(enum dpif_upcall_type type
)
1444 case DPIF_UC_MISS
: return "miss";
1445 case DPIF_UC_ACTION
: return "action";
1446 case DPIF_N_UC_TYPES
: default: return "<unknown>";
1450 /* Enables or disables receiving packets with dpif_recv() on 'dpif'. Returns 0
1451 * if successful, otherwise a positive errno value.
1453 * Turning packet receive off and then back on may change the Netlink PID
1454 * assignments returned by dpif_port_get_pid(). If the client does this, it
1455 * must update all of the flows that have OVS_ACTION_ATTR_USERSPACE actions
1456 * using the new PID assignment. */
1458 dpif_recv_set(struct dpif
*dpif
, bool enable
)
1462 if (dpif
->dpif_class
->recv_set
) {
1463 error
= dpif
->dpif_class
->recv_set(dpif
, enable
);
1464 log_operation(dpif
, "recv_set", error
);
1469 /* Refreshes the poll loops and Netlink sockets associated to each port,
1470 * when the number of upcall handlers (upcall receiving thread) is changed
1471 * to 'n_handlers' and receiving packets for 'dpif' is enabled by
1474 * Since multiple upcall handlers can read upcalls simultaneously from
1475 * 'dpif', each port can have multiple Netlink sockets, one per upcall
1476 * handler. So, handlers_set() is responsible for the following tasks:
1478 * When receiving upcall is enabled, extends or creates the
1479 * configuration to support:
1481 * - 'n_handlers' Netlink sockets for each port.
1483 * - 'n_handlers' poll loops, one for each upcall handler.
1485 * - registering the Netlink sockets for the same upcall handler to
1486 * the corresponding poll loop.
1488 * Returns 0 if successful, otherwise a positive errno value. */
1490 dpif_handlers_set(struct dpif
*dpif
, uint32_t n_handlers
)
1494 if (dpif
->dpif_class
->handlers_set
) {
1495 error
= dpif
->dpif_class
->handlers_set(dpif
, n_handlers
);
1496 log_operation(dpif
, "handlers_set", error
);
1502 dpif_register_dp_purge_cb(struct dpif
*dpif
, dp_purge_callback
*cb
, void *aux
)
1504 if (dpif
->dpif_class
->register_dp_purge_cb
) {
1505 dpif
->dpif_class
->register_dp_purge_cb(dpif
, cb
, aux
);
1510 dpif_register_upcall_cb(struct dpif
*dpif
, upcall_callback
*cb
, void *aux
)
1512 if (dpif
->dpif_class
->register_upcall_cb
) {
1513 dpif
->dpif_class
->register_upcall_cb(dpif
, cb
, aux
);
1518 dpif_enable_upcall(struct dpif
*dpif
)
1520 if (dpif
->dpif_class
->enable_upcall
) {
1521 dpif
->dpif_class
->enable_upcall(dpif
);
1526 dpif_disable_upcall(struct dpif
*dpif
)
1528 if (dpif
->dpif_class
->disable_upcall
) {
1529 dpif
->dpif_class
->disable_upcall(dpif
);
1534 dpif_print_packet(struct dpif
*dpif
, struct dpif_upcall
*upcall
)
1536 if (!VLOG_DROP_DBG(&dpmsg_rl
)) {
1540 packet
= ofp_dp_packet_to_string(&upcall
->packet
);
1543 odp_flow_key_format(upcall
->key
, upcall
->key_len
, &flow
);
1545 VLOG_DBG("%s: %s upcall:\n%s\n%s",
1546 dpif_name(dpif
), dpif_upcall_type_to_string(upcall
->type
),
1547 ds_cstr(&flow
), packet
);
1554 /* Pass custom configuration to the datapath implementation. Some of the
1555 * changes can be postponed until dpif_run() is called. */
1557 dpif_set_config(struct dpif
*dpif
, const struct smap
*cfg
)
1561 if (dpif
->dpif_class
->set_config
) {
1562 error
= dpif
->dpif_class
->set_config(dpif
, cfg
);
1564 log_operation(dpif
, "set_config", error
);
1571 /* Polls for an upcall from 'dpif' for an upcall handler. Since there can
1572 * be multiple poll loops, 'handler_id' is needed as index to identify the
1573 * corresponding poll loop. If successful, stores the upcall into '*upcall',
1574 * using 'buf' for storage. Should only be called if 'recv_set' has been used
1575 * to enable receiving packets from 'dpif'.
1577 * 'upcall->key' and 'upcall->userdata' point into data in the caller-provided
1578 * 'buf', so their memory cannot be freed separately from 'buf'.
1580 * The caller owns the data of 'upcall->packet' and may modify it. If
1581 * packet's headroom is exhausted as it is manipulated, 'upcall->packet'
1582 * will be reallocated. This requires the data of 'upcall->packet' to be
1583 * released with ofpbuf_uninit() before 'upcall' is destroyed. However,
1584 * when an error is returned, the 'upcall->packet' may be uninitialized
1585 * and should not be released.
1587 * Returns 0 if successful, otherwise a positive errno value. Returns EAGAIN
1588 * if no upcall is immediately available. */
1590 dpif_recv(struct dpif
*dpif
, uint32_t handler_id
, struct dpif_upcall
*upcall
,
1595 if (dpif
->dpif_class
->recv
) {
1596 error
= dpif
->dpif_class
->recv(dpif
, handler_id
, upcall
, buf
);
1598 dpif_print_packet(dpif
, upcall
);
1599 } else if (error
!= EAGAIN
) {
1600 log_operation(dpif
, "recv", error
);
1606 /* Discards all messages that would otherwise be received by dpif_recv() on
1609 dpif_recv_purge(struct dpif
*dpif
)
1611 COVERAGE_INC(dpif_purge
);
1612 if (dpif
->dpif_class
->recv_purge
) {
1613 dpif
->dpif_class
->recv_purge(dpif
);
1617 /* Arranges for the poll loop for an upcall handler to wake up when 'dpif'
1618 * 'dpif' has a message queued to be received with the recv member
1619 * function. Since there can be multiple poll loops, 'handler_id' is
1620 * needed as index to identify the corresponding poll loop. */
1622 dpif_recv_wait(struct dpif
*dpif
, uint32_t handler_id
)
1624 if (dpif
->dpif_class
->recv_wait
) {
1625 dpif
->dpif_class
->recv_wait(dpif
, handler_id
);
1630 * Return the datapath version. Caller is responsible for freeing
1634 dpif_get_dp_version(const struct dpif
*dpif
)
1636 char *version
= NULL
;
1638 if (dpif
->dpif_class
->get_datapath_version
) {
1639 version
= dpif
->dpif_class
->get_datapath_version();
1645 /* Obtains the NetFlow engine type and engine ID for 'dpif' into '*engine_type'
1646 * and '*engine_id', respectively. */
1648 dpif_get_netflow_ids(const struct dpif
*dpif
,
1649 uint8_t *engine_type
, uint8_t *engine_id
)
1651 *engine_type
= dpif
->netflow_engine_type
;
1652 *engine_id
= dpif
->netflow_engine_id
;
1655 /* Translates OpenFlow queue ID 'queue_id' (in host byte order) into a priority
1656 * value used for setting packet priority.
1657 * On success, returns 0 and stores the priority into '*priority'.
1658 * On failure, returns a positive errno value and stores 0 into '*priority'. */
1660 dpif_queue_to_priority(const struct dpif
*dpif
, uint32_t queue_id
,
1663 int error
= (dpif
->dpif_class
->queue_to_priority
1664 ? dpif
->dpif_class
->queue_to_priority(dpif
, queue_id
,
1670 log_operation(dpif
, "queue_to_priority", error
);
1675 dpif_init(struct dpif
*dpif
, const struct dpif_class
*dpif_class
,
1677 uint8_t netflow_engine_type
, uint8_t netflow_engine_id
)
1679 dpif
->dpif_class
= dpif_class
;
1680 dpif
->base_name
= xstrdup(name
);
1681 dpif
->full_name
= xasprintf("%s@%s", dpif_class
->type
, name
);
1682 dpif
->netflow_engine_type
= netflow_engine_type
;
1683 dpif
->netflow_engine_id
= netflow_engine_id
;
1686 /* Undoes the results of initialization.
1688 * Normally this function only needs to be called from dpif_close().
1689 * However, it may be called by providers due to an error on opening
1690 * that occurs after initialization. It this case dpif_close() would
1691 * never be called. */
1693 dpif_uninit(struct dpif
*dpif
, bool close
)
1695 char *base_name
= dpif
->base_name
;
1696 char *full_name
= dpif
->full_name
;
1699 dpif
->dpif_class
->close(dpif
);
1707 log_operation(const struct dpif
*dpif
, const char *operation
, int error
)
1710 VLOG_DBG_RL(&dpmsg_rl
, "%s: %s success", dpif_name(dpif
), operation
);
1711 } else if (ofperr_is_valid(error
)) {
1712 VLOG_WARN_RL(&error_rl
, "%s: %s failed (%s)",
1713 dpif_name(dpif
), operation
, ofperr_get_name(error
));
1715 VLOG_WARN_RL(&error_rl
, "%s: %s failed (%s)",
1716 dpif_name(dpif
), operation
, ovs_strerror(error
));
1720 static enum vlog_level
1721 flow_message_log_level(int error
)
1723 /* If flows arrive in a batch, userspace may push down multiple
1724 * unique flow definitions that overlap when wildcards are applied.
1725 * Kernels that support flow wildcarding will reject these flows as
1726 * duplicates (EEXIST), so lower the log level to debug for these
1727 * types of messages. */
1728 return (error
&& error
!= EEXIST
) ? VLL_WARN
: VLL_DBG
;
1732 should_log_flow_message(const struct vlog_module
*module
, int error
)
1734 return !vlog_should_drop(module
, flow_message_log_level(error
),
1735 error
? &error_rl
: &dpmsg_rl
);
1739 log_flow_message(const struct dpif
*dpif
, int error
,
1740 const struct vlog_module
*module
,
1741 const char *operation
,
1742 const struct nlattr
*key
, size_t key_len
,
1743 const struct nlattr
*mask
, size_t mask_len
,
1744 const ovs_u128
*ufid
, const struct dpif_flow_stats
*stats
,
1745 const struct nlattr
*actions
, size_t actions_len
)
1747 struct ds ds
= DS_EMPTY_INITIALIZER
;
1748 ds_put_format(&ds
, "%s: ", dpif_name(dpif
));
1750 ds_put_cstr(&ds
, "failed to ");
1752 ds_put_format(&ds
, "%s ", operation
);
1754 ds_put_format(&ds
, "(%s) ", ovs_strerror(error
));
1757 odp_format_ufid(ufid
, &ds
);
1758 ds_put_cstr(&ds
, " ");
1760 odp_flow_format(key
, key_len
, mask
, mask_len
, NULL
, &ds
, true);
1762 ds_put_cstr(&ds
, ", ");
1763 dpif_flow_stats_format(stats
, &ds
);
1765 if (actions
|| actions_len
) {
1766 ds_put_cstr(&ds
, ", actions:");
1767 format_odp_actions(&ds
, actions
, actions_len
, NULL
);
1769 vlog(module
, flow_message_log_level(error
), "%s", ds_cstr(&ds
));
1774 log_flow_put_message(const struct dpif
*dpif
,
1775 const struct vlog_module
*module
,
1776 const struct dpif_flow_put
*put
,
1779 if (should_log_flow_message(module
, error
)
1780 && !(put
->flags
& DPIF_FP_PROBE
)) {
1784 ds_put_cstr(&s
, "put");
1785 if (put
->flags
& DPIF_FP_CREATE
) {
1786 ds_put_cstr(&s
, "[create]");
1788 if (put
->flags
& DPIF_FP_MODIFY
) {
1789 ds_put_cstr(&s
, "[modify]");
1791 if (put
->flags
& DPIF_FP_ZERO_STATS
) {
1792 ds_put_cstr(&s
, "[zero]");
1794 log_flow_message(dpif
, error
, module
, ds_cstr(&s
),
1795 put
->key
, put
->key_len
, put
->mask
, put
->mask_len
,
1796 put
->ufid
, put
->stats
, put
->actions
,
1803 log_flow_del_message(const struct dpif
*dpif
,
1804 const struct vlog_module
*module
,
1805 const struct dpif_flow_del
*del
,
1808 if (should_log_flow_message(module
, error
)) {
1809 log_flow_message(dpif
, error
, module
, "flow_del",
1810 del
->key
, del
->key_len
,
1811 NULL
, 0, del
->ufid
, !error
? del
->stats
: NULL
,
1816 /* Logs that 'execute' was executed on 'dpif' and completed with errno 'error'
1817 * (0 for success). 'subexecute' should be true if the execution is a result
1818 * of breaking down a larger execution that needed help, false otherwise.
1821 * XXX In theory, the log message could be deceptive because this function is
1822 * called after the dpif_provider's '->execute' function, which is allowed to
1823 * modify execute->packet and execute->md. In practice, though:
1825 * - dpif-netlink doesn't modify execute->packet or execute->md.
1827 * - dpif-netdev does modify them but it is less likely to have problems
1828 * because it is built into ovs-vswitchd and cannot have version skew,
1831 * It would still be better to avoid the potential problem. I don't know of a
1832 * good way to do that, though, that isn't expensive. */
1834 log_execute_message(const struct dpif
*dpif
,
1835 const struct vlog_module
*module
,
1836 const struct dpif_execute
*execute
,
1837 bool subexecute
, int error
)
1839 if (!(error
? VLOG_DROP_WARN(&error_rl
) : VLOG_DROP_DBG(&dpmsg_rl
))
1840 && !execute
->probe
) {
1841 struct ds ds
= DS_EMPTY_INITIALIZER
;
1843 uint64_t stub
[1024 / 8];
1844 struct ofpbuf md
= OFPBUF_STUB_INITIALIZER(stub
);
1846 packet
= ofp_packet_to_string(dp_packet_data(execute
->packet
),
1847 dp_packet_size(execute
->packet
),
1848 execute
->packet
->packet_type
);
1849 odp_key_from_dp_packet(&md
, execute
->packet
);
1850 ds_put_format(&ds
, "%s: %sexecute ",
1852 (subexecute
? "sub-"
1853 : dpif_execute_needs_help(execute
) ? "super-"
1855 format_odp_actions(&ds
, execute
->actions
, execute
->actions_len
, NULL
);
1857 ds_put_format(&ds
, " failed (%s)", ovs_strerror(error
));
1859 ds_put_format(&ds
, " on packet %s", packet
);
1860 ds_put_format(&ds
, " with metadata ");
1861 odp_flow_format(md
.data
, md
.size
, NULL
, 0, NULL
, &ds
, true);
1862 ds_put_format(&ds
, " mtu %d", execute
->mtu
);
1863 vlog(module
, error
? VLL_WARN
: VLL_DBG
, "%s", ds_cstr(&ds
));
1871 log_flow_get_message(const struct dpif
*dpif
,
1872 const struct vlog_module
*module
,
1873 const struct dpif_flow_get
*get
,
1876 if (should_log_flow_message(module
, error
)) {
1877 log_flow_message(dpif
, error
, module
, "flow_get",
1878 get
->key
, get
->key_len
,
1879 get
->flow
->mask
, get
->flow
->mask_len
,
1880 get
->ufid
, &get
->flow
->stats
,
1881 get
->flow
->actions
, get
->flow
->actions_len
);
1886 dpif_supports_tnl_push_pop(const struct dpif
*dpif
)
1888 return dpif_is_netdev(dpif
);
1893 dpif_meter_get_features(const struct dpif
*dpif
,
1894 struct ofputil_meter_features
*features
)
1896 memset(features
, 0, sizeof *features
);
1897 if (dpif
->dpif_class
->meter_get_features
) {
1898 dpif
->dpif_class
->meter_get_features(dpif
, features
);
1902 /* Adds or modifies the meter in 'dpif' with the given 'meter_id' and
1903 * the configuration in 'config'.
1905 * The meter id specified through 'config->meter_id' is ignored. */
1907 dpif_meter_set(struct dpif
*dpif
, ofproto_meter_id meter_id
,
1908 struct ofputil_meter_config
*config
)
1910 COVERAGE_INC(dpif_meter_set
);
1912 if (!(config
->flags
& (OFPMF13_KBPS
| OFPMF13_PKTPS
))) {
1913 return EBADF
; /* Rate unit type not set. */
1916 if ((config
->flags
& OFPMF13_KBPS
) && (config
->flags
& OFPMF13_PKTPS
)) {
1917 return EBADF
; /* Both rate units may not be set. */
1920 if (config
->n_bands
== 0) {
1924 for (size_t i
= 0; i
< config
->n_bands
; i
++) {
1925 if (config
->bands
[i
].rate
== 0) {
1926 return EDOM
; /* Rate must be non-zero */
1930 int error
= dpif
->dpif_class
->meter_set(dpif
, meter_id
, config
);
1932 VLOG_DBG_RL(&dpmsg_rl
, "%s: DPIF meter %"PRIu32
" set",
1933 dpif_name(dpif
), meter_id
.uint32
);
1935 VLOG_WARN_RL(&error_rl
, "%s: failed to set DPIF meter %"PRIu32
": %s",
1936 dpif_name(dpif
), meter_id
.uint32
, ovs_strerror(error
));
1942 dpif_meter_get(const struct dpif
*dpif
, ofproto_meter_id meter_id
,
1943 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
1947 COVERAGE_INC(dpif_meter_get
);
1949 error
= dpif
->dpif_class
->meter_get(dpif
, meter_id
, stats
, n_bands
);
1951 VLOG_DBG_RL(&dpmsg_rl
, "%s: DPIF meter %"PRIu32
" get stats",
1952 dpif_name(dpif
), meter_id
.uint32
);
1954 VLOG_WARN_RL(&error_rl
,
1955 "%s: failed to get DPIF meter %"PRIu32
" stats: %s",
1956 dpif_name(dpif
), meter_id
.uint32
, ovs_strerror(error
));
1957 stats
->packet_in_count
= ~0;
1958 stats
->byte_in_count
= ~0;
1965 dpif_meter_del(struct dpif
*dpif
, ofproto_meter_id meter_id
,
1966 struct ofputil_meter_stats
*stats
, uint16_t n_bands
)
1970 COVERAGE_INC(dpif_meter_del
);
1972 error
= dpif
->dpif_class
->meter_del(dpif
, meter_id
, stats
, n_bands
);
1974 VLOG_DBG_RL(&dpmsg_rl
, "%s: DPIF meter %"PRIu32
" deleted",
1975 dpif_name(dpif
), meter_id
.uint32
);
1977 VLOG_WARN_RL(&error_rl
,
1978 "%s: failed to delete DPIF meter %"PRIu32
": %s",
1979 dpif_name(dpif
), meter_id
.uint32
, ovs_strerror(error
));
1981 stats
->packet_in_count
= ~0;
1982 stats
->byte_in_count
= ~0;