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 struct sfc_mae_field_locator {
225 efx_mae_field_id_t field_id;
227 /* Field offset in the corresponding rte_flow_item_ struct */
232 sfc_mae_item_build_supp_mask(const struct sfc_mae_field_locator *field_locators,
233 unsigned int nb_field_locators, void *mask_ptr,
238 memset(mask_ptr, 0, mask_size);
240 for (i = 0; i < nb_field_locators; ++i) {
241 const struct sfc_mae_field_locator *fl = &field_locators[i];
243 SFC_ASSERT(fl->ofst + fl->size <= mask_size);
244 memset(RTE_PTR_ADD(mask_ptr, fl->ofst), 0xff, fl->size);
249 sfc_mae_parse_item(const struct sfc_mae_field_locator *field_locators,
250 unsigned int nb_field_locators, const uint8_t *spec,
251 const uint8_t *mask, efx_mae_match_spec_t *efx_spec,
252 struct rte_flow_error *error)
257 for (i = 0; i < nb_field_locators; ++i) {
258 const struct sfc_mae_field_locator *fl = &field_locators[i];
260 rc = efx_mae_match_spec_field_set(efx_spec, fl->field_id,
261 fl->size, spec + fl->ofst,
262 fl->size, mask + fl->ofst);
268 rc = rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ITEM,
269 NULL, "Failed to process item fields");
275 static const struct sfc_mae_field_locator flocs_eth[] = {
277 EFX_MAE_FIELD_ETHER_TYPE_BE,
278 RTE_SIZEOF_FIELD(struct rte_flow_item_eth, type),
279 offsetof(struct rte_flow_item_eth, type),
282 EFX_MAE_FIELD_ETH_DADDR_BE,
283 RTE_SIZEOF_FIELD(struct rte_flow_item_eth, dst),
284 offsetof(struct rte_flow_item_eth, dst),
287 EFX_MAE_FIELD_ETH_SADDR_BE,
288 RTE_SIZEOF_FIELD(struct rte_flow_item_eth, src),
289 offsetof(struct rte_flow_item_eth, src),
294 sfc_mae_rule_parse_item_eth(const struct rte_flow_item *item,
295 struct sfc_flow_parse_ctx *ctx,
296 struct rte_flow_error *error)
298 struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
299 struct rte_flow_item_eth supp_mask;
300 const uint8_t *spec = NULL;
301 const uint8_t *mask = NULL;
304 sfc_mae_item_build_supp_mask(flocs_eth, RTE_DIM(flocs_eth),
305 &supp_mask, sizeof(supp_mask));
307 rc = sfc_flow_parse_init(item,
308 (const void **)&spec, (const void **)&mask,
309 (const void *)&supp_mask,
310 &rte_flow_item_eth_mask,
311 sizeof(struct rte_flow_item_eth), error);
315 /* If "spec" is not set, could be any Ethernet */
319 return sfc_mae_parse_item(flocs_eth, RTE_DIM(flocs_eth), spec, mask,
320 ctx_mae->match_spec_action, error);
323 static const struct sfc_flow_item sfc_flow_items[] = {
325 .type = RTE_FLOW_ITEM_TYPE_PHY_PORT,
327 * In terms of RTE flow, this item is a META one,
328 * and its position in the pattern is don't care.
330 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
331 .layer = SFC_FLOW_ITEM_ANY_LAYER,
332 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
333 .parse = sfc_mae_rule_parse_item_phy_port,
336 .type = RTE_FLOW_ITEM_TYPE_ETH,
337 .prev_layer = SFC_FLOW_ITEM_START_LAYER,
338 .layer = SFC_FLOW_ITEM_L2,
339 .ctx_type = SFC_FLOW_PARSE_CTX_MAE,
340 .parse = sfc_mae_rule_parse_item_eth,
345 sfc_mae_rule_parse_pattern(struct sfc_adapter *sa,
346 const struct rte_flow_item pattern[],
347 struct sfc_flow_spec_mae *spec,
348 struct rte_flow_error *error)
350 struct sfc_mae_parse_ctx ctx_mae;
351 struct sfc_flow_parse_ctx ctx;
354 memset(&ctx_mae, 0, sizeof(ctx_mae));
356 rc = efx_mae_match_spec_init(sa->nic, EFX_MAE_RULE_ACTION,
358 &ctx_mae.match_spec_action);
360 rc = rte_flow_error_set(error, rc,
361 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
362 "Failed to initialise action rule match specification");
363 goto fail_init_match_spec_action;
366 ctx.type = SFC_FLOW_PARSE_CTX_MAE;
369 rc = sfc_flow_parse_pattern(sfc_flow_items, RTE_DIM(sfc_flow_items),
370 pattern, &ctx, error);
372 goto fail_parse_pattern;
374 if (!efx_mae_match_spec_is_valid(sa->nic, ctx_mae.match_spec_action)) {
375 rc = rte_flow_error_set(error, ENOTSUP,
376 RTE_FLOW_ERROR_TYPE_ITEM, NULL,
377 "Inconsistent pattern");
378 goto fail_validate_match_spec_action;
381 spec->match_spec = ctx_mae.match_spec_action;
385 fail_validate_match_spec_action:
387 efx_mae_match_spec_fini(sa->nic, ctx_mae.match_spec_action);
389 fail_init_match_spec_action:
394 sfc_mae_rule_parse_action_phy_port(struct sfc_adapter *sa,
395 const struct rte_flow_action_phy_port *conf,
396 efx_mae_actions_t *spec)
398 efx_mport_sel_t mport;
402 if (conf->original != 0)
403 phy_port = efx_nic_cfg_get(sa->nic)->enc_assigned_port;
405 phy_port = conf->index;
407 rc = efx_mae_mport_by_phy_port(phy_port, &mport);
411 return efx_mae_action_set_populate_deliver(spec, &mport);
415 sfc_mae_rule_parse_action(struct sfc_adapter *sa,
416 const struct rte_flow_action *action,
417 efx_mae_actions_t *spec,
418 struct rte_flow_error *error)
422 switch (action->type) {
423 case RTE_FLOW_ACTION_TYPE_PHY_PORT:
424 rc = sfc_mae_rule_parse_action_phy_port(sa, action->conf, spec);
427 return rte_flow_error_set(error, ENOTSUP,
428 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
429 "Unsupported action");
433 rc = rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ACTION,
434 NULL, "Failed to request the action");
441 sfc_mae_rule_parse_actions(struct sfc_adapter *sa,
442 const struct rte_flow_action actions[],
443 struct sfc_mae_action_set **action_setp,
444 struct rte_flow_error *error)
446 const struct rte_flow_action *action;
447 efx_mae_actions_t *spec;
450 if (actions == NULL) {
451 return rte_flow_error_set(error, EINVAL,
452 RTE_FLOW_ERROR_TYPE_ACTION_NUM, NULL,
456 rc = efx_mae_action_set_spec_init(sa->nic, &spec);
458 goto fail_action_set_spec_init;
460 for (action = actions;
461 action->type != RTE_FLOW_ACTION_TYPE_END; ++action) {
462 rc = sfc_mae_rule_parse_action(sa, action, spec, error);
464 goto fail_rule_parse_action;
467 *action_setp = sfc_mae_action_set_attach(sa, spec);
468 if (*action_setp != NULL) {
469 efx_mae_action_set_spec_fini(sa->nic, spec);
473 rc = sfc_mae_action_set_add(sa, spec, action_setp);
475 goto fail_action_set_add;
480 fail_rule_parse_action:
481 efx_mae_action_set_spec_fini(sa->nic, spec);
483 fail_action_set_spec_init:
485 rc = rte_flow_error_set(error, rc,
486 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
487 NULL, "Failed to process the action");
493 sfc_mae_rules_class_cmp(struct sfc_adapter *sa,
494 const efx_mae_match_spec_t *left,
495 const efx_mae_match_spec_t *right)
497 bool have_same_class;
500 rc = efx_mae_match_specs_class_cmp(sa->nic, left, right,
503 return (rc == 0) ? have_same_class : false;
507 sfc_mae_action_rule_class_verify(struct sfc_adapter *sa,
508 struct sfc_flow_spec_mae *spec)
510 const struct rte_flow *entry;
512 TAILQ_FOREACH_REVERSE(entry, &sa->flow_list, sfc_flow_list, entries) {
513 const struct sfc_flow_spec *entry_spec = &entry->spec;
514 const struct sfc_flow_spec_mae *es_mae = &entry_spec->mae;
515 const efx_mae_match_spec_t *left = es_mae->match_spec;
516 const efx_mae_match_spec_t *right = spec->match_spec;
518 switch (entry_spec->type) {
519 case SFC_FLOW_SPEC_FILTER:
520 /* Ignore VNIC-level flows */
522 case SFC_FLOW_SPEC_MAE:
523 if (sfc_mae_rules_class_cmp(sa, left, right))
531 sfc_info(sa, "for now, the HW doesn't support rule validation, and HW "
532 "support for inner frame pattern items is not guaranteed; "
533 "other than that, the items are valid from SW standpoint");
538 * Confirm that a given flow can be accepted by the FW.
541 * Software adapter context
543 * Flow to be verified
545 * Zero on success and non-zero in the case of error.
546 * A special value of EAGAIN indicates that the adapter is
547 * not in started state. This state is compulsory because
548 * it only makes sense to compare the rule class of the flow
549 * being validated with classes of the active rules.
550 * Such classes are wittingly supported by the FW.
553 sfc_mae_flow_verify(struct sfc_adapter *sa,
554 struct rte_flow *flow)
556 struct sfc_flow_spec *spec = &flow->spec;
557 struct sfc_flow_spec_mae *spec_mae = &spec->mae;
559 SFC_ASSERT(sfc_adapter_is_locked(sa));
561 if (sa->state != SFC_ADAPTER_STARTED)
564 return sfc_mae_action_rule_class_verify(sa, spec_mae);