057eef537baefbef3c122d63211efe98aded7402
[dpdk.git] / drivers / net / sfc / sfc_mae.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2019-2020 Xilinx, Inc.
4  * Copyright(c) 2019 Solarflare Communications Inc.
5  *
6  * This software was jointly developed between OKTET Labs (under contract
7  * for Solarflare) and Solarflare Communications, Inc.
8  */
9
10 #include <stdbool.h>
11
12 #include <rte_common.h>
13
14 #include "efx.h"
15
16 #include "sfc.h"
17 #include "sfc_log.h"
18
19 int
20 sfc_mae_attach(struct sfc_adapter *sa)
21 {
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;
25         int rc;
26
27         sfc_log_init(sa, "entry");
28
29         if (!encp->enc_mae_supported) {
30                 mae->status = SFC_MAE_STATUS_UNSUPPORTED;
31                 return 0;
32         }
33
34         sfc_log_init(sa, "init MAE");
35         rc = efx_mae_init(sa->nic);
36         if (rc != 0)
37                 goto fail_mae_init;
38
39         sfc_log_init(sa, "get MAE limits");
40         rc = efx_mae_get_limits(sa->nic, &limits);
41         if (rc != 0)
42                 goto fail_mae_get_limits;
43
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);
47
48         sfc_log_init(sa, "done");
49
50         return 0;
51
52 fail_mae_get_limits:
53         efx_mae_fini(sa->nic);
54
55 fail_mae_init:
56         sfc_log_init(sa, "failed %d", rc);
57
58         return rc;
59 }
60
61 void
62 sfc_mae_detach(struct sfc_adapter *sa)
63 {
64         struct sfc_mae *mae = &sa->mae;
65         enum sfc_mae_status status_prev = mae->status;
66
67         sfc_log_init(sa, "entry");
68
69         mae->nb_action_rule_prios_max = 0;
70         mae->status = SFC_MAE_STATUS_UNKNOWN;
71
72         if (status_prev != SFC_MAE_STATUS_SUPPORTED)
73                 return;
74
75         efx_mae_fini(sa->nic);
76
77         sfc_log_init(sa, "done");
78 }
79
80 static struct sfc_mae_action_set *
81 sfc_mae_action_set_attach(struct sfc_adapter *sa,
82                           const efx_mae_actions_t *spec)
83 {
84         struct sfc_mae_action_set *action_set;
85         struct sfc_mae *mae = &sa->mae;
86
87         SFC_ASSERT(sfc_adapter_is_locked(sa));
88
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);
92                         return action_set;
93                 }
94         }
95
96         return NULL;
97 }
98
99 static int
100 sfc_mae_action_set_add(struct sfc_adapter *sa,
101                        efx_mae_actions_t *spec,
102                        struct sfc_mae_action_set **action_setp)
103 {
104         struct sfc_mae_action_set *action_set;
105         struct sfc_mae *mae = &sa->mae;
106
107         SFC_ASSERT(sfc_adapter_is_locked(sa));
108
109         action_set = rte_zmalloc("sfc_mae_action_set", sizeof(*action_set), 0);
110         if (action_set == NULL)
111                 return ENOMEM;
112
113         action_set->refcnt = 1;
114         action_set->spec = spec;
115
116         TAILQ_INSERT_TAIL(&mae->action_sets, action_set, entries);
117
118         *action_setp = action_set;
119
120         return 0;
121 }
122
123 static void
124 sfc_mae_action_set_del(struct sfc_adapter *sa,
125                        struct sfc_mae_action_set *action_set)
126 {
127         struct sfc_mae *mae = &sa->mae;
128
129         SFC_ASSERT(sfc_adapter_is_locked(sa));
130         SFC_ASSERT(action_set->refcnt != 0);
131
132         --(action_set->refcnt);
133
134         if (action_set->refcnt != 0)
135                 return;
136
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);
140 }
141
142 void
143 sfc_mae_flow_cleanup(struct sfc_adapter *sa,
144                      struct rte_flow *flow)
145 {
146         struct sfc_flow_spec *spec;
147         struct sfc_flow_spec_mae *spec_mae;
148
149         if (flow == NULL)
150                 return;
151
152         spec = &flow->spec;
153
154         if (spec == NULL)
155                 return;
156
157         spec_mae = &spec->mae;
158
159         if (spec_mae->action_set != NULL)
160                 sfc_mae_action_set_del(sa, spec_mae->action_set);
161
162         if (spec_mae->match_spec != NULL)
163                 efx_mae_match_spec_fini(sa->nic, spec_mae->match_spec);
164 }
165
166 static int
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)
170 {
171         struct sfc_mae_parse_ctx *ctx_mae = ctx->mae;
172         const struct rte_flow_item_phy_port supp_mask = {
173                 .index = 0xffffffff,
174         };
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;
179         int rc;
180
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");
185         }
186
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);
191         if (rc != 0)
192                 return rc;
193
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");
198         }
199
200         /* If "spec" is not set, could be any physical port */
201         if (spec == NULL)
202                 return 0;
203
204         rc = efx_mae_mport_by_phy_port(spec->index, &mport_v);
205         if (rc != 0) {
206                 return rte_flow_error_set(error, rc,
207                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
208                                 "Failed to convert the PHY_PORT index");
209         }
210
211         rc = efx_mae_match_spec_mport_set(ctx_mae->match_spec_action,
212                                           &mport_v, NULL);
213         if (rc != 0) {
214                 return rte_flow_error_set(error, rc,
215                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
216                                 "Failed to set MPORT for the PHY_PORT");
217         }
218
219         ctx_mae->match_mport_set = B_TRUE;
220
221         return 0;
222 }
223
224 struct sfc_mae_field_locator {
225         efx_mae_field_id_t              field_id;
226         size_t                          size;
227         /* Field offset in the corresponding rte_flow_item_ struct */
228         size_t                          ofst;
229 };
230
231 static void
232 sfc_mae_item_build_supp_mask(const struct sfc_mae_field_locator *field_locators,
233                              unsigned int nb_field_locators, void *mask_ptr,
234                              size_t mask_size)
235 {
236         unsigned int i;
237
238         memset(mask_ptr, 0, mask_size);
239
240         for (i = 0; i < nb_field_locators; ++i) {
241                 const struct sfc_mae_field_locator *fl = &field_locators[i];
242
243                 SFC_ASSERT(fl->ofst + fl->size <= mask_size);
244                 memset(RTE_PTR_ADD(mask_ptr, fl->ofst), 0xff, fl->size);
245         }
246 }
247
248 static int
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)
253 {
254         unsigned int i;
255         int rc = 0;
256
257         for (i = 0; i < nb_field_locators; ++i) {
258                 const struct sfc_mae_field_locator *fl = &field_locators[i];
259
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);
263                 if (rc != 0)
264                         break;
265         }
266
267         if (rc != 0) {
268                 rc = rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ITEM,
269                                 NULL, "Failed to process item fields");
270         }
271
272         return rc;
273 }
274
275 static const struct sfc_mae_field_locator flocs_eth[] = {
276         {
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),
280         },
281         {
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),
285         },
286         {
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),
290         },
291 };
292
293 static int
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)
297 {
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;
302         int rc;
303
304         sfc_mae_item_build_supp_mask(flocs_eth, RTE_DIM(flocs_eth),
305                                      &supp_mask, sizeof(supp_mask));
306
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);
312         if (rc != 0)
313                 return rc;
314
315         /* If "spec" is not set, could be any Ethernet */
316         if (spec == NULL)
317                 return 0;
318
319         return sfc_mae_parse_item(flocs_eth, RTE_DIM(flocs_eth), spec, mask,
320                                   ctx_mae->match_spec_action, error);
321 }
322
323 static const struct sfc_flow_item sfc_flow_items[] = {
324         {
325                 .type = RTE_FLOW_ITEM_TYPE_PHY_PORT,
326                 /*
327                  * In terms of RTE flow, this item is a META one,
328                  * and its position in the pattern is don't care.
329                  */
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,
334         },
335         {
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,
341         },
342 };
343
344 int
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)
349 {
350         struct sfc_mae_parse_ctx ctx_mae;
351         struct sfc_flow_parse_ctx ctx;
352         int rc;
353
354         memset(&ctx_mae, 0, sizeof(ctx_mae));
355
356         rc = efx_mae_match_spec_init(sa->nic, EFX_MAE_RULE_ACTION,
357                                      spec->priority,
358                                      &ctx_mae.match_spec_action);
359         if (rc != 0) {
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;
364         }
365
366         ctx.type = SFC_FLOW_PARSE_CTX_MAE;
367         ctx.mae = &ctx_mae;
368
369         rc = sfc_flow_parse_pattern(sfc_flow_items, RTE_DIM(sfc_flow_items),
370                                     pattern, &ctx, error);
371         if (rc != 0)
372                 goto fail_parse_pattern;
373
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;
379         }
380
381         spec->match_spec = ctx_mae.match_spec_action;
382
383         return 0;
384
385 fail_validate_match_spec_action:
386 fail_parse_pattern:
387         efx_mae_match_spec_fini(sa->nic, ctx_mae.match_spec_action);
388
389 fail_init_match_spec_action:
390         return rc;
391 }
392
393 static int
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)
397 {
398         efx_mport_sel_t mport;
399         uint32_t phy_port;
400         int rc;
401
402         if (conf->original != 0)
403                 phy_port = efx_nic_cfg_get(sa->nic)->enc_assigned_port;
404         else
405                 phy_port = conf->index;
406
407         rc = efx_mae_mport_by_phy_port(phy_port, &mport);
408         if (rc != 0)
409                 return rc;
410
411         return efx_mae_action_set_populate_deliver(spec, &mport);
412 }
413
414 static int
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)
419 {
420         int rc;
421
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);
425                 break;
426         default:
427                 return rte_flow_error_set(error, ENOTSUP,
428                                 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
429                                 "Unsupported action");
430         }
431
432         if (rc != 0) {
433                 rc = rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_ACTION,
434                                 NULL, "Failed to request the action");
435         }
436
437         return rc;
438 }
439
440 int
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)
445 {
446         const struct rte_flow_action *action;
447         efx_mae_actions_t *spec;
448         int rc;
449
450         if (actions == NULL) {
451                 return rte_flow_error_set(error, EINVAL,
452                                 RTE_FLOW_ERROR_TYPE_ACTION_NUM, NULL,
453                                 "NULL actions");
454         }
455
456         rc = efx_mae_action_set_spec_init(sa->nic, &spec);
457         if (rc != 0)
458                 goto fail_action_set_spec_init;
459
460         for (action = actions;
461              action->type != RTE_FLOW_ACTION_TYPE_END; ++action) {
462                 rc = sfc_mae_rule_parse_action(sa, action, spec, error);
463                 if (rc != 0)
464                         goto fail_rule_parse_action;
465         }
466
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);
470                 return 0;
471         }
472
473         rc = sfc_mae_action_set_add(sa, spec, action_setp);
474         if (rc != 0)
475                 goto fail_action_set_add;
476
477         return 0;
478
479 fail_action_set_add:
480 fail_rule_parse_action:
481         efx_mae_action_set_spec_fini(sa->nic, spec);
482
483 fail_action_set_spec_init:
484         if (rc > 0) {
485                 rc = rte_flow_error_set(error, rc,
486                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
487                         NULL, "Failed to process the action");
488         }
489         return rc;
490 }
491
492 static bool
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)
496 {
497         bool have_same_class;
498         int rc;
499
500         rc = efx_mae_match_specs_class_cmp(sa->nic, left, right,
501                                            &have_same_class);
502
503         return (rc == 0) ? have_same_class : false;
504 }
505
506 static int
507 sfc_mae_action_rule_class_verify(struct sfc_adapter *sa,
508                                  struct sfc_flow_spec_mae *spec)
509 {
510         const struct rte_flow *entry;
511
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;
517
518                 switch (entry_spec->type) {
519                 case SFC_FLOW_SPEC_FILTER:
520                         /* Ignore VNIC-level flows */
521                         break;
522                 case SFC_FLOW_SPEC_MAE:
523                         if (sfc_mae_rules_class_cmp(sa, left, right))
524                                 return 0;
525                         break;
526                 default:
527                         SFC_ASSERT(false);
528                 }
529         }
530
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");
534         return 0;
535 }
536
537 /**
538  * Confirm that a given flow can be accepted by the FW.
539  *
540  * @param sa
541  *   Software adapter context
542  * @param flow
543  *   Flow to be verified
544  * @return
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.
551  */
552 int
553 sfc_mae_flow_verify(struct sfc_adapter *sa,
554                     struct rte_flow *flow)
555 {
556         struct sfc_flow_spec *spec = &flow->spec;
557         struct sfc_flow_spec_mae *spec_mae = &spec->mae;
558
559         SFC_ASSERT(sfc_adapter_is_locked(sa));
560
561         if (sa->state != SFC_ADAPTER_STARTED)
562                 return EAGAIN;
563
564         return sfc_mae_action_rule_class_verify(sa, spec_mae);
565 }