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_flow_tunnel_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_SUPPORTED);
28 sfc_flow_tunnel_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);
36 struct sfc_flow_tunnel *
37 sfc_flow_tunnel_pick(struct sfc_adapter *sa, uint32_t ft_mark)
39 uint32_t tunnel_mark = SFC_FT_GET_TUNNEL_MARK(ft_mark);
41 SFC_ASSERT(sfc_adapter_is_locked(sa));
43 if (tunnel_mark != SFC_FT_TUNNEL_MARK_INVALID) {
44 sfc_ft_id_t ft_id = SFC_FT_TUNNEL_MARK_TO_ID(tunnel_mark);
45 struct sfc_flow_tunnel *ft = &sa->flow_tunnels[ft_id];
56 sfc_flow_tunnel_detect_jump_rule(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_flow_tunnel *ft;
67 SFC_ASSERT(sfc_adapter_is_locked(sa));
69 if (!sfc_flow_tunnel_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 ft_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 = sfc_flow_tunnel_pick(sa, ft_mark);
117 if (ft != NULL && action_jump != 0) {
118 sfc_dbg(sa, "tunnel offload: JUMP: detected");
121 /* The loop above might have spotted wrong actions. */
122 sfc_err(sa, "tunnel offload: JUMP: invalid actions: %s",
127 if (ft->refcnt == 0) {
128 sfc_err(sa, "tunnel offload: JUMP: tunnel=%u does not exist",
134 if (ft->jump_rule_is_set) {
135 sfc_err(sa, "tunnel offload: JUMP: already exists in tunnel=%u",
141 spec->ft_rule_type = SFC_FT_RULE_JUMP;
148 return rte_flow_error_set(error, rc,
149 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
150 "tunnel offload: JUMP: preparsing failed");
154 sfc_flow_tunnel_attach(struct sfc_adapter *sa,
155 struct rte_flow_tunnel *tunnel,
156 struct sfc_flow_tunnel **ftp)
158 struct sfc_flow_tunnel *ft;
159 const char *ft_status;
164 SFC_ASSERT(sfc_adapter_is_locked(sa));
166 rc = sfc_dp_ft_id_register();
170 if (tunnel->type != RTE_FLOW_ITEM_TYPE_VXLAN) {
171 sfc_err(sa, "tunnel offload: unsupported tunnel (encapsulation) type");
175 for (ft_id = 0; ft_id < SFC_FT_MAX_NTUNNELS; ++ft_id) {
176 ft = &sa->flow_tunnels[ft_id];
178 if (ft->refcnt == 0) {
179 if (ft_id_free == -1)
185 if (memcmp(tunnel, &ft->rte_tunnel, sizeof(*tunnel)) == 0) {
186 ft_status = "existing";
191 if (ft_id_free == -1) {
192 sfc_err(sa, "tunnel offload: no free slot for the new tunnel");
197 ft = &sa->flow_tunnels[ft_id];
199 memcpy(&ft->rte_tunnel, tunnel, sizeof(*tunnel));
201 ft->encap_type = EFX_TUNNEL_PROTOCOL_VXLAN;
203 ft->action_mark.id = SFC_FT_ID_TO_MARK(ft_id_free);
204 ft->action.type = RTE_FLOW_ACTION_TYPE_MARK;
205 ft->action.conf = &ft->action_mark;
207 ft->item.type = RTE_FLOW_ITEM_TYPE_MARK;
208 ft->item_mark_v.id = ft->action_mark.id;
209 ft->item.spec = &ft->item_mark_v;
210 ft->item.mask = &ft->item_mark_m;
211 ft->item_mark_m.id = UINT32_MAX;
213 ft->jump_rule_is_set = B_FALSE;
217 ft_status = "newly added";
220 sfc_dbg(sa, "tunnel offload: attaching to %s tunnel=%u",
230 sfc_flow_tunnel_detach(struct sfc_adapter *sa,
233 struct sfc_flow_tunnel *ft;
235 SFC_ASSERT(sfc_adapter_is_locked(sa));
237 ft = sfc_flow_tunnel_pick(sa, ft_mark);
239 sfc_err(sa, "tunnel offload: invalid tunnel");
243 if (ft->refcnt == 0) {
244 sfc_err(sa, "tunnel offload: tunnel=%u does not exist", ft->id);
254 sfc_flow_tunnel_decap_set(struct rte_eth_dev *dev,
255 struct rte_flow_tunnel *tunnel,
256 struct rte_flow_action **pmd_actions,
257 uint32_t *num_of_actions,
258 struct rte_flow_error *err)
260 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
261 struct sfc_flow_tunnel *ft;
264 sfc_adapter_lock(sa);
266 if (!sfc_flow_tunnel_is_active(sa)) {
271 rc = sfc_flow_tunnel_attach(sa, tunnel, &ft);
275 *pmd_actions = &ft->action;
278 sfc_adapter_unlock(sa);
283 sfc_adapter_unlock(sa);
285 return rte_flow_error_set(err, rc,
286 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
287 "tunnel offload: decap_set failed");
291 sfc_flow_tunnel_match(struct rte_eth_dev *dev,
292 struct rte_flow_tunnel *tunnel,
293 struct rte_flow_item **pmd_items,
294 uint32_t *num_of_items,
295 struct rte_flow_error *err)
297 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
298 struct sfc_flow_tunnel *ft;
301 sfc_adapter_lock(sa);
303 if (!sfc_flow_tunnel_is_active(sa)) {
308 rc = sfc_flow_tunnel_attach(sa, tunnel, &ft);
312 *pmd_items = &ft->item;
315 sfc_adapter_unlock(sa);
320 sfc_adapter_unlock(sa);
322 return rte_flow_error_set(err, rc,
323 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
324 "tunnel offload: tunnel_match failed");
328 sfc_flow_tunnel_item_release(struct rte_eth_dev *dev,
329 struct rte_flow_item *pmd_items,
331 struct rte_flow_error *err)
333 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
334 const struct rte_flow_item_mark *item_mark;
335 struct rte_flow_item *item = pmd_items;
338 sfc_adapter_lock(sa);
340 if (!sfc_flow_tunnel_is_active(sa)) {
345 if (num_items != 1 || item == NULL || item->spec == NULL ||
346 item->type != RTE_FLOW_ITEM_TYPE_MARK) {
347 sfc_err(sa, "tunnel offload: item_release: wrong input");
352 item_mark = item->spec;
354 rc = sfc_flow_tunnel_detach(sa, item_mark->id);
358 sfc_adapter_unlock(sa);
363 sfc_adapter_unlock(sa);
365 return rte_flow_error_set(err, rc,
366 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
367 "tunnel offload: item_release failed");
371 sfc_flow_tunnel_action_decap_release(struct rte_eth_dev *dev,
372 struct rte_flow_action *pmd_actions,
373 uint32_t num_actions,
374 struct rte_flow_error *err)
376 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
377 const struct rte_flow_action_mark *action_mark;
378 struct rte_flow_action *action = pmd_actions;
381 sfc_adapter_lock(sa);
383 if (!sfc_flow_tunnel_is_active(sa)) {
388 if (num_actions != 1 || action == NULL || action->conf == NULL ||
389 action->type != RTE_FLOW_ACTION_TYPE_MARK) {
390 sfc_err(sa, "tunnel offload: action_decap_release: wrong input");
395 action_mark = action->conf;
397 rc = sfc_flow_tunnel_detach(sa, action_mark->id);
401 sfc_adapter_unlock(sa);
406 sfc_adapter_unlock(sa);
408 return rte_flow_error_set(err, rc,
409 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
410 "tunnel offload: item_release failed");
414 sfc_flow_tunnel_get_restore_info(struct rte_eth_dev *dev,
416 struct rte_flow_restore_info *info,
417 struct rte_flow_error *err)
419 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
420 const struct sfc_flow_tunnel *ft;
424 sfc_adapter_lock(sa);
426 if ((m->ol_flags & sfc_dp_ft_id_valid) == 0) {
427 sfc_dbg(sa, "tunnel offload: get_restore_info: no tunnel mark in the packet");
432 ft_id = *RTE_MBUF_DYNFIELD(m, sfc_dp_ft_id_offset, sfc_ft_id_t *);
433 ft = &sa->flow_tunnels[ft_id];
435 if (ft->refcnt == 0) {
436 sfc_err(sa, "tunnel offload: get_restore_info: tunnel=%u does not exist",
442 memcpy(&info->tunnel, &ft->rte_tunnel, sizeof(info->tunnel));
445 * The packet still has encapsulation header; JUMP rules never
446 * strip it. Therefore, set RTE_FLOW_RESTORE_INFO_ENCAPSULATED.
448 info->flags = RTE_FLOW_RESTORE_INFO_ENCAPSULATED |
449 RTE_FLOW_RESTORE_INFO_GROUP_ID |
450 RTE_FLOW_RESTORE_INFO_TUNNEL;
454 sfc_adapter_unlock(sa);
459 sfc_adapter_unlock(sa);
461 return rte_flow_error_set(err, rc,
462 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
463 "tunnel offload: get_restore_info failed");
467 sfc_flow_tunnel_reset_hit_counters(struct sfc_adapter *sa)
471 SFC_ASSERT(sfc_adapter_is_locked(sa));
472 SFC_ASSERT(sa->state != SFC_ETHDEV_STARTED);
474 for (i = 0; i < RTE_DIM(sa->flow_tunnels); ++i) {
475 struct sfc_flow_tunnel *ft = &sa->flow_tunnels[i];
477 ft->reset_jump_hit_counter = 0;
478 ft->group_hit_counter = 0;