1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright(c) 2021 Xilinx, Inc.
14 #include "sfc_dp_rx.h"
15 #include "sfc_flow_tunnel.h"
19 sfc_ft_is_supported(struct sfc_adapter *sa)
21 SFC_ASSERT(sfc_adapter_is_locked(sa));
23 return ((sa->priv.dp_rx->features & SFC_DP_RX_FEAT_FLOW_MARK) != 0 &&
24 sa->mae.status == SFC_MAE_STATUS_ADMIN);
28 sfc_ft_is_active(struct sfc_adapter *sa)
30 SFC_ASSERT(sfc_adapter_is_locked(sa));
32 return ((sa->negotiated_rx_metadata &
33 RTE_ETH_RX_METADATA_TUNNEL_ID) != 0);
37 sfc_ft_ctx_pick(struct sfc_adapter *sa, uint32_t flow_mark)
39 uint8_t ft_ctx_mark = SFC_FT_FLOW_MARK_TO_CTX_MARK(flow_mark);
41 SFC_ASSERT(sfc_adapter_is_locked(sa));
43 if (ft_ctx_mark != SFC_FT_CTX_MARK_INVALID) {
44 sfc_ft_ctx_id_t ft_ctx_id = SFC_FT_CTX_MARK_TO_CTX_ID(ft_ctx_mark);
45 struct sfc_ft_ctx *ft_ctx = &sa->ft_ctx_pool[ft_ctx_id];
47 ft_ctx->id = ft_ctx_id;
56 sfc_ft_tunnel_rule_detect(struct sfc_adapter *sa,
57 const struct rte_flow_action *actions,
58 struct sfc_flow_spec_mae *spec,
59 struct rte_flow_error *error)
61 const struct rte_flow_action_mark *action_mark = NULL;
62 const struct rte_flow_action_jump *action_jump = NULL;
63 struct sfc_ft_ctx *ft_ctx;
64 uint32_t flow_mark = 0;
67 SFC_ASSERT(sfc_adapter_is_locked(sa));
69 if (!sfc_ft_is_active(sa)) {
70 /* Tunnel-related actions (if any) will be turned down later. */
74 if (actions == NULL) {
75 rte_flow_error_set(error, EINVAL,
76 RTE_FLOW_ERROR_TYPE_ACTION_NUM, NULL,
81 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; ++actions) {
82 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID)
85 if (actions->conf == NULL) {
90 switch (actions->type) {
91 case RTE_FLOW_ACTION_TYPE_COUNT:
93 case RTE_FLOW_ACTION_TYPE_MARK:
94 if (action_mark == NULL) {
95 action_mark = actions->conf;
96 flow_mark = action_mark->id;
101 case RTE_FLOW_ACTION_TYPE_JUMP:
102 if (action_jump == NULL) {
103 action_jump = actions->conf;
104 if (action_jump->group != 0)
116 ft_ctx = sfc_ft_ctx_pick(sa, flow_mark);
117 if (ft_ctx != NULL && action_jump != 0) {
118 sfc_dbg(sa, "FT: TUNNEL: detected");
121 /* The loop above might have spotted wrong actions. */
122 sfc_err(sa, "FT: TUNNEL: invalid actions: %s",
127 if (ft_ctx->refcnt == 0) {
128 sfc_err(sa, "FT: TUNNEL: inactive context (ID=%u)",
134 if (ft_ctx->tunnel_rule_is_set) {
135 sfc_err(sa, "FT: TUNNEL: already setup context (ID=%u)",
141 spec->ft_rule_type = SFC_FT_RULE_TUNNEL;
142 spec->ft_ctx = ft_ctx;
148 return rte_flow_error_set(error, rc,
149 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
150 "FT: TUNNEL: preparsing failed");
154 sfc_ft_ctx_attach(struct sfc_adapter *sa, const struct rte_flow_tunnel *tunnel,
155 struct sfc_ft_ctx **ft_ctxp)
157 sfc_ft_ctx_id_t ft_ctx_id;
158 struct sfc_ft_ctx *ft_ctx;
159 const char *ft_ctx_status;
160 int ft_ctx_id_free = -1;
163 SFC_ASSERT(sfc_adapter_is_locked(sa));
165 rc = sfc_dp_ft_ctx_id_register();
169 if (tunnel->type != RTE_FLOW_ITEM_TYPE_VXLAN) {
170 sfc_err(sa, "FT: unsupported tunnel (encapsulation) type");
174 for (ft_ctx_id = 0; ft_ctx_id < SFC_FT_MAX_NTUNNELS; ++ft_ctx_id) {
175 ft_ctx = &sa->ft_ctx_pool[ft_ctx_id];
177 if (ft_ctx->refcnt == 0) {
178 if (ft_ctx_id_free == -1)
179 ft_ctx_id_free = ft_ctx_id;
184 if (memcmp(tunnel, &ft_ctx->tunnel, sizeof(*tunnel)) == 0) {
185 ft_ctx_status = "existing";
190 if (ft_ctx_id_free == -1) {
191 sfc_err(sa, "FT: no free slot for the new context");
195 ft_ctx_id = ft_ctx_id_free;
196 ft_ctx = &sa->ft_ctx_pool[ft_ctx_id];
198 memcpy(&ft_ctx->tunnel, tunnel, sizeof(*tunnel));
200 ft_ctx->encap_type = EFX_TUNNEL_PROTOCOL_VXLAN;
202 ft_ctx->action_mark.id = SFC_FT_CTX_ID_TO_FLOW_MARK(ft_ctx_id);
203 ft_ctx->action.type = RTE_FLOW_ACTION_TYPE_MARK;
204 ft_ctx->action.conf = &ft_ctx->action_mark;
206 ft_ctx->item_mark_v.id = ft_ctx->action_mark.id;
207 ft_ctx->item.type = RTE_FLOW_ITEM_TYPE_MARK;
208 ft_ctx->item.spec = &ft_ctx->item_mark_v;
209 ft_ctx->item.mask = &ft_ctx->item_mark_m;
210 ft_ctx->item_mark_m.id = UINT32_MAX;
212 ft_ctx->tunnel_rule_is_set = B_FALSE;
216 ft_ctx_status = "newly added";
219 sfc_dbg(sa, "FT: attaching to %s context (ID=%u)",
220 ft_ctx_status, ft_ctx_id);
229 sfc_ft_ctx_detach(struct sfc_adapter *sa, uint32_t flow_mark)
231 struct sfc_ft_ctx *ft_ctx;
233 SFC_ASSERT(sfc_adapter_is_locked(sa));
235 ft_ctx = sfc_ft_ctx_pick(sa, flow_mark);
236 if (ft_ctx == NULL) {
237 sfc_err(sa, "FT: invalid context");
241 if (ft_ctx->refcnt == 0) {
242 sfc_err(sa, "FT: inactive context (ID=%u)", ft_ctx->id);
252 sfc_ft_decap_set(struct rte_eth_dev *dev, struct rte_flow_tunnel *tunnel,
253 struct rte_flow_action **pmd_actions, uint32_t *num_of_actions,
254 struct rte_flow_error *err)
256 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
257 struct sfc_ft_ctx *ft_ctx;
260 sfc_adapter_lock(sa);
262 if (!sfc_ft_is_active(sa)) {
267 rc = sfc_ft_ctx_attach(sa, tunnel, &ft_ctx);
271 *pmd_actions = &ft_ctx->action;
274 sfc_adapter_unlock(sa);
279 sfc_adapter_unlock(sa);
281 return rte_flow_error_set(err, rc,
282 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
283 "FT: decap_set failed");
287 sfc_ft_match(struct rte_eth_dev *dev, struct rte_flow_tunnel *tunnel,
288 struct rte_flow_item **pmd_items, uint32_t *num_of_items,
289 struct rte_flow_error *err)
291 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
292 struct sfc_ft_ctx *ft_ctx;
295 sfc_adapter_lock(sa);
297 if (!sfc_ft_is_active(sa)) {
302 rc = sfc_ft_ctx_attach(sa, tunnel, &ft_ctx);
306 *pmd_items = &ft_ctx->item;
309 sfc_adapter_unlock(sa);
314 sfc_adapter_unlock(sa);
316 return rte_flow_error_set(err, rc,
317 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
318 "FT: tunnel_match failed");
322 sfc_ft_item_release(struct rte_eth_dev *dev, struct rte_flow_item *pmd_items,
323 uint32_t num_items, struct rte_flow_error *err)
325 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
326 const struct rte_flow_item_mark *item_mark;
327 struct rte_flow_item *item = pmd_items;
330 sfc_adapter_lock(sa);
332 if (!sfc_ft_is_active(sa)) {
337 if (num_items != 1 || item == NULL || item->spec == NULL ||
338 item->type != RTE_FLOW_ITEM_TYPE_MARK) {
339 sfc_err(sa, "FT: item_release: wrong input");
344 item_mark = item->spec;
346 rc = sfc_ft_ctx_detach(sa, item_mark->id);
350 sfc_adapter_unlock(sa);
355 sfc_adapter_unlock(sa);
357 return rte_flow_error_set(err, rc,
358 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
359 "FT: item_release failed");
363 sfc_ft_action_decap_release(struct rte_eth_dev *dev,
364 struct rte_flow_action *pmd_actions,
365 uint32_t num_actions, struct rte_flow_error *err)
367 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
368 const struct rte_flow_action_mark *action_mark;
369 struct rte_flow_action *action = pmd_actions;
372 sfc_adapter_lock(sa);
374 if (!sfc_ft_is_active(sa)) {
379 if (num_actions != 1 || action == NULL || action->conf == NULL ||
380 action->type != RTE_FLOW_ACTION_TYPE_MARK) {
381 sfc_err(sa, "FT: action_decap_release: wrong input");
386 action_mark = action->conf;
388 rc = sfc_ft_ctx_detach(sa, action_mark->id);
392 sfc_adapter_unlock(sa);
397 sfc_adapter_unlock(sa);
399 return rte_flow_error_set(err, rc,
400 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
401 "FT: item_release failed");
405 sfc_ft_get_restore_info(struct rte_eth_dev *dev, struct rte_mbuf *m,
406 struct rte_flow_restore_info *info,
407 struct rte_flow_error *err)
409 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
410 const struct sfc_ft_ctx *ft_ctx;
411 sfc_ft_ctx_id_t ft_ctx_id;
414 sfc_adapter_lock(sa);
416 if ((m->ol_flags & sfc_dp_ft_ctx_id_valid) == 0) {
417 sfc_dbg(sa, "FT: get_restore_info: no FT context mark in the packet");
422 ft_ctx_id = *RTE_MBUF_DYNFIELD(m, sfc_dp_ft_ctx_id_offset,
424 ft_ctx = &sa->ft_ctx_pool[ft_ctx_id];
426 if (ft_ctx->refcnt == 0) {
427 sfc_dbg(sa, "FT: get_restore_info: inactive context (ID=%u)",
433 memcpy(&info->tunnel, &ft_ctx->tunnel, sizeof(info->tunnel));
436 * The packet still has encapsulation header; TUNNEL rules never
437 * strip it. Therefore, set RTE_FLOW_RESTORE_INFO_ENCAPSULATED.
439 info->flags = RTE_FLOW_RESTORE_INFO_ENCAPSULATED |
440 RTE_FLOW_RESTORE_INFO_GROUP_ID |
441 RTE_FLOW_RESTORE_INFO_TUNNEL;
445 sfc_adapter_unlock(sa);
450 sfc_adapter_unlock(sa);
452 return rte_flow_error_set(err, rc,
453 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
454 "FT: get_restore_info failed");
458 sfc_ft_counters_reset(struct sfc_adapter *sa)
462 SFC_ASSERT(sfc_adapter_is_locked(sa));
463 SFC_ASSERT(sa->state != SFC_ETHDEV_STARTED);
465 for (i = 0; i < RTE_DIM(sa->ft_ctx_pool); ++i) {
466 struct sfc_ft_ctx *ft_ctx = &sa->ft_ctx_pool[i];
468 ft_ctx->reset_tunnel_hit_counter = 0;
469 ft_ctx->switch_hit_counter = 0;