1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
5 #include "mlx5_flow_os.h"
6 #include "mlx5_win_ext.h"
8 #include <rte_thread.h>
11 * Verify the @p attributes will be correctly understood by the NIC and store
12 * them in the @p flow if everything is correct.
15 * Pointer to dev struct.
16 * @param[in] attributes
17 * Pointer to flow attributes
19 * This flow rule is created by request external to PMD.
21 * Pointer to error structure.
24 * - 0 on success and non root table (not a valid option for Windows yet).
25 * - 1 on success and root table.
26 * - a negative errno value otherwise and rte_errno is set.
29 mlx5_flow_os_validate_flow_attributes(struct rte_eth_dev *dev,
30 const struct rte_flow_attr *attributes,
32 struct rte_flow_error *error)
37 RTE_SET_USED(external);
38 if (attributes->group)
39 return rte_flow_error_set(error, ENOTSUP,
40 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
42 "groups are not supported");
43 if (attributes->priority)
44 return rte_flow_error_set(error, ENOTSUP,
45 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
47 "priorities are not supported");
48 if (attributes->transfer)
49 return rte_flow_error_set(error, ENOTSUP,
50 RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
52 "transfer not supported");
53 if (!(attributes->ingress))
54 return rte_flow_error_set(error, ENOTSUP,
55 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
56 NULL, "must specify ingress only");
61 * Create flow matcher in a flow table.
64 * Pointer to relevant device context.
66 * Pointer to relevant attributes.
68 * Pointer to table object.
70 * Pointer to a valid flow matcher object on success, NULL otherwise.
73 * 0 on success, or errno on failure.
76 mlx5_flow_os_create_flow_matcher(void *ctx,
81 struct mlx5dv_flow_matcher_attr *mattr;
86 if (mattr->type != IBV_FLOW_ATTR_NORMAL) {
90 struct mlx5_matcher *mlx5_matcher =
91 mlx5_malloc(MLX5_MEM_ZERO,
92 sizeof(struct mlx5_matcher) +
93 MLX5_ST_SZ_BYTES(fte_match_param),
99 mlx5_matcher->ctx = ctx;
100 memcpy(&mlx5_matcher->attr, attr, sizeof(mlx5_matcher->attr));
101 memcpy(&mlx5_matcher->match_buf,
102 mattr->match_mask->match_buf,
103 MLX5_ST_SZ_BYTES(fte_match_param));
104 *matcher = mlx5_matcher;
109 * Destroy flow matcher.
112 * Pointer to matcher object to destroy.
115 * 0 on success, or the value of errno on failure.
118 mlx5_flow_os_destroy_flow_matcher(void *matcher)
125 * Create flow action: dest_devx_tir
128 * Pointer to DevX tir object
130 * Pointer to a valid action on success, NULL otherwise.
133 * 0 on success, or errno on failure.
136 mlx5_flow_os_create_flow_action_dest_devx_tir(struct mlx5_devx_obj *tir,
139 struct mlx5_action *mlx5_action =
140 mlx5_malloc(MLX5_MEM_ZERO,
141 sizeof(struct mlx5_action),
148 mlx5_action->type = MLX5_FLOW_CONTEXT_DEST_TYPE_TIR;
149 mlx5_action->dest_tir.id = tir->id;
150 *action = mlx5_action;
155 * Destroy flow action.
158 * Pointer to action object to destroy.
161 * 0 on success, or the value of errno on failure.
164 mlx5_flow_os_destroy_flow_action(void *action)
174 * Pointer to match mask structure.
175 * @param[in] match_value
176 * Pointer to match value structure.
177 * @param[in] num_actions
178 * Number of actions in flow rule.
180 * Pointer to array of flow rule actions.
182 * Pointer to a valid flow rule object on success, NULL otherwise.
185 * 0 on success, or errno on failure.
188 mlx5_flow_os_create_flow(void *matcher, void *match_value,
190 void *actions[], void **flow)
192 struct mlx5_action *action;
194 struct mlx5_matcher *mlx5_matcher = matcher;
195 struct mlx5_flow_dv_match_params *mlx5_match_value = match_value;
196 uint32_t in[MLX5_ST_SZ_DW(devx_fs_rule_add_in)] = {0};
197 void *matcher_c = MLX5_ADDR_OF(devx_fs_rule_add_in, in,
199 void *matcher_v = MLX5_ADDR_OF(devx_fs_rule_add_in, in,
202 MLX5_ASSERT(mlx5_matcher->ctx);
203 memcpy(matcher_c, mlx5_matcher->match_buf,
204 mlx5_match_value->size);
205 /* Use mlx5_match_value->size for match criteria */
206 memcpy(matcher_v, mlx5_match_value->buf,
207 mlx5_match_value->size);
208 for (i = 0; i < num_actions; i++) {
210 switch (action->type) {
211 case MLX5_FLOW_CONTEXT_DEST_TYPE_TIR:
212 MLX5_SET(devx_fs_rule_add_in, in,
213 dest.destination_type,
214 MLX5_FLOW_CONTEXT_DEST_TYPE_TIR);
215 MLX5_SET(devx_fs_rule_add_in, in,
217 action->dest_tir.id);
222 MLX5_SET(devx_fs_rule_add_in, in, match_criteria_enable,
223 MLX5_MATCH_OUTER_HEADERS);
225 *flow = mlx5_glue->devx_fs_rule_add(mlx5_matcher->ctx, in, sizeof(in));
226 return (*flow) ? 0 : -1;
232 * @param[in] drv_flow_ptr
233 * Pointer to flow rule object.
236 * 0 on success, errno on failure.
239 mlx5_flow_os_destroy_flow(void *drv_flow_ptr)
241 return mlx5_glue->devx_fs_rule_del(drv_flow_ptr);
244 struct mlx5_workspace_thread {
245 HANDLE thread_handle;
246 struct mlx5_flow_workspace *mlx5_ws;
247 struct mlx5_workspace_thread *next;
251 * Static pointer array for multi thread support of mlx5_flow_workspace.
253 static struct mlx5_workspace_thread *curr;
254 static struct mlx5_workspace_thread *first;
255 rte_tls_key ws_tls_index;
256 static pthread_mutex_t lock_thread_list;
259 mlx5_is_thread_alive(HANDLE thread_handle)
261 DWORD result = WaitForSingleObject(thread_handle, 0);
263 if (result == WAIT_OBJECT_0)
269 mlx5_get_current_thread(HANDLE *p_handle)
271 BOOL ret = DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
272 GetCurrentProcess(), p_handle, 0, 0, DUPLICATE_SAME_ACCESS);
275 RTE_LOG_WIN32_ERR("DuplicateHandle()");
282 mlx5_clear_thread_list(void)
284 struct mlx5_workspace_thread *temp = first;
285 struct mlx5_workspace_thread *next, *prev = NULL;
290 if (mlx5_get_current_thread(&curr_thread)) {
291 DRV_LOG(ERR, "Failed to get current thread "
297 if (temp->thread_handle != curr_thread &&
298 !mlx5_is_thread_alive(temp->thread_handle)) {
303 } else if (temp == curr) {
306 flow_release_workspace(temp->mlx5_ws);
307 CloseHandle(temp->thread_handle);
317 CloseHandle(curr_thread);
321 * Release workspaces before exit.
324 mlx5_flow_os_release_workspace(void)
326 mlx5_clear_thread_list();
328 MLX5_ASSERT(!first->next);
329 flow_release_workspace(first->mlx5_ws);
332 rte_thread_tls_key_delete(ws_tls_index);
333 pthread_mutex_destroy(&lock_thread_list);
337 mlx5_add_workspace_to_list(struct mlx5_flow_workspace *data)
340 struct mlx5_workspace_thread *temp = calloc(1, sizeof(*temp));
343 DRV_LOG(ERR, "Failed to allocate thread workspace "
347 if (mlx5_get_current_thread(&curr_thread)) {
348 DRV_LOG(ERR, "Failed to get current thread "
353 temp->mlx5_ws = data;
354 temp->thread_handle = curr_thread;
355 pthread_mutex_lock(&lock_thread_list);
356 mlx5_clear_thread_list();
364 pthread_mutex_unlock(&lock_thread_list);
369 mlx5_flow_os_init_workspace_once(void)
371 int err = rte_thread_tls_key_create(&ws_tls_index, NULL);
374 DRV_LOG(ERR, "Can't create flow workspace data thread key.");
377 pthread_mutex_init(&lock_thread_list, NULL);
382 mlx5_flow_os_get_specific_workspace(void)
384 return rte_thread_tls_value_get(ws_tls_index);
388 mlx5_flow_os_set_specific_workspace(struct mlx5_flow_workspace *data)
391 int old_err = rte_errno;
394 if (!rte_thread_tls_value_get(ws_tls_index)) {
396 DRV_LOG(ERR, "Failed checking specific workspace.");
401 * set_specific_workspace when current value is NULL
402 * can happen only once per thread, mark this thread in
403 * linked list to be able to release reasorces later on.
405 err = mlx5_add_workspace_to_list(data);
407 DRV_LOG(ERR, "Failed adding workspace to list.");
412 if (rte_thread_tls_value_set(ws_tls_index, data)) {
413 DRV_LOG(ERR, "Failed setting specific workspace.");