1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright(c) 2019-2020 Xilinx, Inc.
4 * Copyright(c) 2019 Solarflare Communications Inc.
6 * This software was jointly developed between OKTET Labs (under contract
7 * for Solarflare) and Solarflare Communications, Inc.
12 #include <rte_common.h>
20 sfc_mae_attach(struct sfc_adapter *sa)
22 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
23 struct sfc_mae *mae = &sa->mae;
24 efx_mae_limits_t limits;
27 sfc_log_init(sa, "entry");
29 if (!encp->enc_mae_supported) {
30 mae->status = SFC_MAE_STATUS_UNSUPPORTED;
34 sfc_log_init(sa, "init MAE");
35 rc = efx_mae_init(sa->nic);
39 sfc_log_init(sa, "get MAE limits");
40 rc = efx_mae_get_limits(sa->nic, &limits);
42 goto fail_mae_get_limits;
44 mae->status = SFC_MAE_STATUS_SUPPORTED;
45 mae->nb_action_rule_prios_max = limits.eml_max_n_action_prios;
46 TAILQ_INIT(&mae->action_sets);
48 sfc_log_init(sa, "done");
53 efx_mae_fini(sa->nic);
56 sfc_log_init(sa, "failed %d", rc);
62 sfc_mae_detach(struct sfc_adapter *sa)
64 struct sfc_mae *mae = &sa->mae;
65 enum sfc_mae_status status_prev = mae->status;
67 sfc_log_init(sa, "entry");
69 mae->nb_action_rule_prios_max = 0;
70 mae->status = SFC_MAE_STATUS_UNKNOWN;
72 if (status_prev != SFC_MAE_STATUS_SUPPORTED)
75 efx_mae_fini(sa->nic);
77 sfc_log_init(sa, "done");
80 static struct sfc_mae_action_set *
81 sfc_mae_action_set_attach(struct sfc_adapter *sa,
82 const efx_mae_actions_t *spec)
84 struct sfc_mae_action_set *action_set;
85 struct sfc_mae *mae = &sa->mae;
87 SFC_ASSERT(sfc_adapter_is_locked(sa));
89 TAILQ_FOREACH(action_set, &mae->action_sets, entries) {
90 if (efx_mae_action_set_specs_equal(action_set->spec, spec)) {
91 ++(action_set->refcnt);
100 sfc_mae_action_set_add(struct sfc_adapter *sa,
101 efx_mae_actions_t *spec,
102 struct sfc_mae_action_set **action_setp)
104 struct sfc_mae_action_set *action_set;
105 struct sfc_mae *mae = &sa->mae;
107 SFC_ASSERT(sfc_adapter_is_locked(sa));
109 action_set = rte_zmalloc("sfc_mae_action_set", sizeof(*action_set), 0);
110 if (action_set == NULL)
113 action_set->refcnt = 1;
114 action_set->spec = spec;
116 TAILQ_INSERT_TAIL(&mae->action_sets, action_set, entries);
118 *action_setp = action_set;
124 sfc_mae_action_set_del(struct sfc_adapter *sa,
125 struct sfc_mae_action_set *action_set)
127 struct sfc_mae *mae = &sa->mae;
129 SFC_ASSERT(sfc_adapter_is_locked(sa));
130 SFC_ASSERT(action_set->refcnt != 0);
132 --(action_set->refcnt);
134 if (action_set->refcnt != 0)
137 efx_mae_action_set_spec_fini(sa->nic, action_set->spec);
138 TAILQ_REMOVE(&mae->action_sets, action_set, entries);
139 rte_free(action_set);
143 sfc_mae_flow_cleanup(struct sfc_adapter *sa,
144 struct rte_flow *flow)
146 struct sfc_flow_spec *spec;
147 struct sfc_flow_spec_mae *spec_mae;
157 spec_mae = &spec->mae;
159 if (spec_mae->action_set != NULL)
160 sfc_mae_action_set_del(sa, spec_mae->action_set);
162 if (spec_mae->match_spec != NULL)
163 efx_mae_match_spec_fini(sa->nic, spec_mae->match_spec);
167 sfc_mae_rule_parse_item_phy_port(const struct rte_flow_item *item,
168 struct sfc_flow_parse_ctx *ctx,
169 struct rte_flow_error *error)
171 struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
172 const struct rte_flow_item_phy_port supp_mask = {
175 const void *def_mask = &rte_flow_item_phy_port_mask;
176 const struct rte_flow_item_phy_port *spec = NULL;
177 const struct rte_flow_item_phy_port *mask = NULL;
178 efx_mport_sel_t mport_v;
181 if (ctx_mae->match_mport_set) {
182 return rte_flow_error_set(error, ENOTSUP,
183 RTE_FLOW_ERROR_TYPE_ITEM, item,
184 "Can't handle multiple traffic source items");
187 rc = sfc_flow_parse_init(item,
188 (const void **)&spec, (const void **)&mask,
189 (const void *)&supp_mask, def_mask,
190 sizeof(struct rte_flow_item_phy_port), error);
194 if (mask->index != supp_mask.index) {
195 return rte_flow_error_set(error, EINVAL,
196 RTE_FLOW_ERROR_TYPE_ITEM, item,
197 "Bad mask in the PHY_PORT pattern item");
200 /* If "spec" is not set, could be any physical port */
204 rc = efx_mae_mport_by_phy_port(spec->index, &mport_v);
206 return rte_flow_error_set(error, rc,
207 RTE_FLOW_ERROR_TYPE_ITEM, item,
208 "Failed to convert the PHY_PORT index");
211 rc = efx_mae_match_spec_mport_set(ctx_mae->match_spec_action,
214 return rte_flow_error_set(error, rc,
215 RTE_FLOW_ERROR_TYPE_ITEM, item,
216 "Failed to set MPORT for the PHY_PORT");
219 ctx_mae->match_mport_set = B_TRUE;
224 static const struct sfc_flow_item sfc_flow_items[] = {
226 .type = RTE_FLOW_ITEM_TYPE_PHY_PORT,
228 * In terms of RTE flow, this item is a META one,
229 * and its position in the pattern is don't care.
231 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
232 .layer = SFC_FLOW_ITEM_ANY_LAYER,
233 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
234 .parse = sfc_mae_rule_parse_item_phy_port,
239 sfc_mae_rule_parse_pattern(struct sfc_adapter *sa,
240 const struct rte_flow_item pattern[],
241 struct sfc_flow_spec_mae *spec,
242 struct rte_flow_error *error)
244 struct sfc_mae_parse_ctx ctx_mae;
245 struct sfc_flow_parse_ctx ctx;
248 memset(&ctx_mae, 0, sizeof(ctx_mae));
250 rc = efx_mae_match_spec_init(sa->nic, EFX_MAE_RULE_ACTION,
252 &ctx_mae.match_spec_action);
254 rc = rte_flow_error_set(error, rc,
255 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
256 "Failed to initialise action rule match specification");
257 goto fail_init_match_spec_action;
260 ctx.type = SFC_FLOW_PARSE_CTX_MAE;
263 rc = sfc_flow_parse_pattern(sfc_flow_items, RTE_DIM(sfc_flow_items),
264 pattern, &ctx, error);
266 goto fail_parse_pattern;
268 if (!efx_mae_match_spec_is_valid(sa->nic, ctx_mae.match_spec_action)) {
269 rc = rte_flow_error_set(error, ENOTSUP,
270 RTE_FLOW_ERROR_TYPE_ITEM, NULL,
271 "Inconsistent pattern");
272 goto fail_validate_match_spec_action;
275 spec->match_spec = ctx_mae.match_spec_action;
279 fail_validate_match_spec_action:
281 efx_mae_match_spec_fini(sa->nic, ctx_mae.match_spec_action);
283 fail_init_match_spec_action:
288 sfc_mae_rule_parse_action(const struct rte_flow_action *action,
289 __rte_unused efx_mae_actions_t *spec,
290 struct rte_flow_error *error)
292 switch (action->type) {
294 return rte_flow_error_set(error, ENOTSUP,
295 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
296 "Unsupported action");
303 sfc_mae_rule_parse_actions(struct sfc_adapter *sa,
304 const struct rte_flow_action actions[],
305 struct sfc_mae_action_set **action_setp,
306 struct rte_flow_error *error)
308 const struct rte_flow_action *action;
309 efx_mae_actions_t *spec;
312 if (actions == NULL) {
313 return rte_flow_error_set(error, EINVAL,
314 RTE_FLOW_ERROR_TYPE_ACTION_NUM, NULL,
318 rc = efx_mae_action_set_spec_init(sa->nic, &spec);
320 goto fail_action_set_spec_init;
322 for (action = actions;
323 action->type != RTE_FLOW_ACTION_TYPE_END; ++action) {
324 rc = sfc_mae_rule_parse_action(action, spec, error);
326 goto fail_rule_parse_action;
329 *action_setp = sfc_mae_action_set_attach(sa, spec);
330 if (*action_setp != NULL) {
331 efx_mae_action_set_spec_fini(sa->nic, spec);
335 rc = sfc_mae_action_set_add(sa, spec, action_setp);
337 goto fail_action_set_add;
342 fail_rule_parse_action:
343 efx_mae_action_set_spec_fini(sa->nic, spec);
345 fail_action_set_spec_init:
347 rc = rte_flow_error_set(error, rc,
348 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
349 NULL, "Failed to process the action");
355 sfc_mae_rules_class_cmp(struct sfc_adapter *sa,
356 const efx_mae_match_spec_t *left,
357 const efx_mae_match_spec_t *right)
359 bool have_same_class;
362 rc = efx_mae_match_specs_class_cmp(sa->nic, left, right,
365 return (rc == 0) ? have_same_class : false;
369 sfc_mae_action_rule_class_verify(struct sfc_adapter *sa,
370 struct sfc_flow_spec_mae *spec)
372 const struct rte_flow *entry;
374 TAILQ_FOREACH_REVERSE(entry, &sa->flow_list, sfc_flow_list, entries) {
375 const struct sfc_flow_spec *entry_spec = &entry->spec;
376 const struct sfc_flow_spec_mae *es_mae = &entry_spec->mae;
377 const efx_mae_match_spec_t *left = es_mae->match_spec;
378 const efx_mae_match_spec_t *right = spec->match_spec;
380 switch (entry_spec->type) {
381 case SFC_FLOW_SPEC_FILTER:
382 /* Ignore VNIC-level flows */
384 case SFC_FLOW_SPEC_MAE:
385 if (sfc_mae_rules_class_cmp(sa, left, right))
393 sfc_info(sa, "for now, the HW doesn't support rule validation, and HW "
394 "support for inner frame pattern items is not guaranteed; "
395 "other than that, the items are valid from SW standpoint");
400 * Confirm that a given flow can be accepted by the FW.
403 * Software adapter context
405 * Flow to be verified
407 * Zero on success and non-zero in the case of error.
408 * A special value of EAGAIN indicates that the adapter is
409 * not in started state. This state is compulsory because
410 * it only makes sense to compare the rule class of the flow
411 * being validated with classes of the active rules.
412 * Such classes are wittingly supported by the FW.
415 sfc_mae_flow_verify(struct sfc_adapter *sa,
416 struct rte_flow *flow)
418 struct sfc_flow_spec *spec = &flow->spec;
419 struct sfc_flow_spec_mae *spec_mae = &spec->mae;
421 SFC_ASSERT(sfc_adapter_is_locked(sa));
423 if (sa->state != SFC_ADAPTER_STARTED)
426 return sfc_mae_action_rule_class_verify(sa, spec_mae);