net/sfc: implement control path operations in tunnel offload
[dpdk.git] / drivers / net / sfc / sfc_flow_tunnel.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2021 Xilinx, Inc.
4  */
5
6 #include <stdbool.h>
7 #include <stdint.h>
8
9 #include <rte_flow.h>
10
11 #include "sfc.h"
12 #include "sfc_dp.h"
13 #include "sfc_flow.h"
14 #include "sfc_dp_rx.h"
15 #include "sfc_flow_tunnel.h"
16 #include "sfc_mae.h"
17
18 bool
19 sfc_flow_tunnel_is_supported(struct sfc_adapter *sa)
20 {
21         SFC_ASSERT(sfc_adapter_is_locked(sa));
22
23         return ((sa->priv.dp_rx->features & SFC_DP_RX_FEAT_FLOW_MARK) != 0 &&
24                 sa->mae.status == SFC_MAE_STATUS_SUPPORTED);
25 }
26
27 bool
28 sfc_flow_tunnel_is_active(struct sfc_adapter *sa)
29 {
30         SFC_ASSERT(sfc_adapter_is_locked(sa));
31
32         return ((sa->negotiated_rx_metadata &
33                  RTE_ETH_RX_METADATA_TUNNEL_ID) != 0);
34 }
35
36 struct sfc_flow_tunnel *
37 sfc_flow_tunnel_pick(struct sfc_adapter *sa, uint32_t ft_mark)
38 {
39         uint32_t tunnel_mark = SFC_FT_GET_TUNNEL_MARK(ft_mark);
40
41         SFC_ASSERT(sfc_adapter_is_locked(sa));
42
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];
46
47                 ft->id = ft_id;
48
49                 return ft;
50         }
51
52         return NULL;
53 }
54
55 int
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)
60 {
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;
64         uint32_t ft_mark = 0;
65         int rc = 0;
66
67         SFC_ASSERT(sfc_adapter_is_locked(sa));
68
69         if (!sfc_flow_tunnel_is_active(sa)) {
70                 /* Tunnel-related actions (if any) will be turned down later. */
71                 return 0;
72         }
73
74         if (actions == NULL) {
75                 rte_flow_error_set(error, EINVAL,
76                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM, NULL,
77                                    "NULL actions");
78                 return -rte_errno;
79         }
80
81         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; ++actions) {
82                 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID)
83                         continue;
84
85                 if (actions->conf == NULL) {
86                         rc = EINVAL;
87                         continue;
88                 }
89
90                 switch (actions->type) {
91                 case RTE_FLOW_ACTION_TYPE_MARK:
92                         if (action_mark == NULL) {
93                                 action_mark = actions->conf;
94                                 ft_mark = action_mark->id;
95                         } else {
96                                 rc = EINVAL;
97                         }
98                         break;
99                 case RTE_FLOW_ACTION_TYPE_JUMP:
100                         if (action_jump == NULL) {
101                                 action_jump = actions->conf;
102                                 if (action_jump->group != 0)
103                                         rc = EINVAL;
104                         } else {
105                                 rc = EINVAL;
106                         }
107                         break;
108                 default:
109                         rc = ENOTSUP;
110                         break;
111                 }
112         }
113
114         ft = sfc_flow_tunnel_pick(sa, ft_mark);
115         if (ft != NULL && action_jump != 0) {
116                 sfc_dbg(sa, "tunnel offload: JUMP: detected");
117
118                 if (rc != 0) {
119                         /* The loop above might have spotted wrong actions. */
120                         sfc_err(sa, "tunnel offload: JUMP: invalid actions: %s",
121                                 strerror(rc));
122                         goto fail;
123                 }
124
125                 if (ft->refcnt == 0) {
126                         sfc_err(sa, "tunnel offload: JUMP: tunnel=%u does not exist",
127                                 ft->id);
128                         rc = ENOENT;
129                         goto fail;
130                 }
131
132                 if (ft->jump_rule_is_set) {
133                         sfc_err(sa, "tunnel offload: JUMP: already exists in tunnel=%u",
134                                 ft->id);
135                         rc = EEXIST;
136                         goto fail;
137                 }
138
139                 spec->ft_rule_type = SFC_FT_RULE_JUMP;
140                 spec->ft = ft;
141         }
142
143         return 0;
144
145 fail:
146         return rte_flow_error_set(error, rc,
147                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
148                                   "tunnel offload: JUMP: preparsing failed");
149 }
150
151 static int
152 sfc_flow_tunnel_attach(struct sfc_adapter *sa,
153                        struct rte_flow_tunnel *tunnel,
154                        struct sfc_flow_tunnel **ftp)
155 {
156         struct sfc_flow_tunnel *ft;
157         const char *ft_status;
158         int ft_id_free = -1;
159         sfc_ft_id_t ft_id;
160         int rc;
161
162         SFC_ASSERT(sfc_adapter_is_locked(sa));
163
164         rc = sfc_dp_ft_id_register();
165         if (rc != 0)
166                 return rc;
167
168         if (tunnel->type != RTE_FLOW_ITEM_TYPE_VXLAN) {
169                 sfc_err(sa, "tunnel offload: unsupported tunnel (encapsulation) type");
170                 return ENOTSUP;
171         }
172
173         for (ft_id = 0; ft_id < SFC_FT_MAX_NTUNNELS; ++ft_id) {
174                 ft = &sa->flow_tunnels[ft_id];
175
176                 if (ft->refcnt == 0) {
177                         if (ft_id_free == -1)
178                                 ft_id_free = ft_id;
179
180                         continue;
181                 }
182
183                 if (memcmp(tunnel, &ft->rte_tunnel, sizeof(*tunnel)) == 0) {
184                         ft_status = "existing";
185                         goto attach;
186                 }
187         }
188
189         if (ft_id_free == -1) {
190                 sfc_err(sa, "tunnel offload: no free slot for the new tunnel");
191                 return ENOBUFS;
192         }
193
194         ft_id = ft_id_free;
195         ft = &sa->flow_tunnels[ft_id];
196
197         memcpy(&ft->rte_tunnel, tunnel, sizeof(*tunnel));
198
199         ft->encap_type = EFX_TUNNEL_PROTOCOL_VXLAN;
200
201         ft->action_mark.id = SFC_FT_ID_TO_MARK(ft_id_free);
202         ft->action.type = RTE_FLOW_ACTION_TYPE_MARK;
203         ft->action.conf = &ft->action_mark;
204
205         ft->item.type = RTE_FLOW_ITEM_TYPE_MARK;
206         ft->item_mark_v.id = ft->action_mark.id;
207         ft->item.spec = &ft->item_mark_v;
208         ft->item.mask = &ft->item_mark_m;
209         ft->item_mark_m.id = UINT32_MAX;
210
211         ft->jump_rule_is_set = B_FALSE;
212
213         ft->refcnt = 0;
214
215         ft_status = "newly added";
216
217 attach:
218         sfc_dbg(sa, "tunnel offload: attaching to %s tunnel=%u",
219                 ft_status, ft_id);
220
221         ++(ft->refcnt);
222         *ftp = ft;
223
224         return 0;
225 }
226
227 static int
228 sfc_flow_tunnel_detach(struct sfc_adapter *sa,
229                        uint32_t ft_mark)
230 {
231         struct sfc_flow_tunnel *ft;
232
233         SFC_ASSERT(sfc_adapter_is_locked(sa));
234
235         ft = sfc_flow_tunnel_pick(sa, ft_mark);
236         if (ft == NULL) {
237                 sfc_err(sa, "tunnel offload: invalid tunnel");
238                 return EINVAL;
239         }
240
241         if (ft->refcnt == 0) {
242                 sfc_err(sa, "tunnel offload: tunnel=%u does not exist", ft->id);
243                 return ENOENT;
244         }
245
246         --(ft->refcnt);
247
248         return 0;
249 }
250
251 int
252 sfc_flow_tunnel_decap_set(struct rte_eth_dev *dev,
253                           struct rte_flow_tunnel *tunnel,
254                           struct rte_flow_action **pmd_actions,
255                           uint32_t *num_of_actions,
256                           struct rte_flow_error *err)
257 {
258         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
259         struct sfc_flow_tunnel *ft;
260         int rc;
261
262         sfc_adapter_lock(sa);
263
264         if (!sfc_flow_tunnel_is_active(sa)) {
265                 rc = ENOTSUP;
266                 goto fail;
267         }
268
269         rc = sfc_flow_tunnel_attach(sa, tunnel, &ft);
270         if (rc != 0)
271                 goto fail;
272
273         *pmd_actions = &ft->action;
274         *num_of_actions = 1;
275
276         sfc_adapter_unlock(sa);
277
278         return 0;
279
280 fail:
281         sfc_adapter_unlock(sa);
282
283         return rte_flow_error_set(err, rc,
284                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
285                                   "tunnel offload: decap_set failed");
286 }
287
288 int
289 sfc_flow_tunnel_match(struct rte_eth_dev *dev,
290                       struct rte_flow_tunnel *tunnel,
291                       struct rte_flow_item **pmd_items,
292                       uint32_t *num_of_items,
293                       struct rte_flow_error *err)
294 {
295         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
296         struct sfc_flow_tunnel *ft;
297         int rc;
298
299         sfc_adapter_lock(sa);
300
301         if (!sfc_flow_tunnel_is_active(sa)) {
302                 rc = ENOTSUP;
303                 goto fail;
304         }
305
306         rc = sfc_flow_tunnel_attach(sa, tunnel, &ft);
307         if (rc != 0)
308                 goto fail;
309
310         *pmd_items = &ft->item;
311         *num_of_items = 1;
312
313         sfc_adapter_unlock(sa);
314
315         return 0;
316
317 fail:
318         sfc_adapter_unlock(sa);
319
320         return rte_flow_error_set(err, rc,
321                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
322                                   "tunnel offload: tunnel_match failed");
323 }
324
325 int
326 sfc_flow_tunnel_item_release(struct rte_eth_dev *dev,
327                              struct rte_flow_item *pmd_items,
328                              uint32_t num_items,
329                              struct rte_flow_error *err)
330 {
331         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
332         const struct rte_flow_item_mark *item_mark;
333         struct rte_flow_item *item = pmd_items;
334         int rc;
335
336         sfc_adapter_lock(sa);
337
338         if (!sfc_flow_tunnel_is_active(sa)) {
339                 rc = ENOTSUP;
340                 goto fail;
341         }
342
343         if (num_items != 1 || item == NULL || item->spec == NULL ||
344             item->type != RTE_FLOW_ITEM_TYPE_MARK) {
345                 sfc_err(sa, "tunnel offload: item_release: wrong input");
346                 rc = EINVAL;
347                 goto fail;
348         }
349
350         item_mark = item->spec;
351
352         rc = sfc_flow_tunnel_detach(sa, item_mark->id);
353         if (rc != 0)
354                 goto fail;
355
356         sfc_adapter_unlock(sa);
357
358         return 0;
359
360 fail:
361         sfc_adapter_unlock(sa);
362
363         return rte_flow_error_set(err, rc,
364                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
365                                   "tunnel offload: item_release failed");
366 }
367
368 int
369 sfc_flow_tunnel_action_decap_release(struct rte_eth_dev *dev,
370                                      struct rte_flow_action *pmd_actions,
371                                      uint32_t num_actions,
372                                      struct rte_flow_error *err)
373 {
374         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
375         const struct rte_flow_action_mark *action_mark;
376         struct rte_flow_action *action = pmd_actions;
377         int rc;
378
379         sfc_adapter_lock(sa);
380
381         if (!sfc_flow_tunnel_is_active(sa)) {
382                 rc = ENOTSUP;
383                 goto fail;
384         }
385
386         if (num_actions != 1 || action == NULL || action->conf == NULL ||
387             action->type != RTE_FLOW_ACTION_TYPE_MARK) {
388                 sfc_err(sa, "tunnel offload: action_decap_release: wrong input");
389                 rc = EINVAL;
390                 goto fail;
391         }
392
393         action_mark = action->conf;
394
395         rc = sfc_flow_tunnel_detach(sa, action_mark->id);
396         if (rc != 0)
397                 goto fail;
398
399         sfc_adapter_unlock(sa);
400
401         return 0;
402
403 fail:
404         sfc_adapter_unlock(sa);
405
406         return rte_flow_error_set(err, rc,
407                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
408                                   "tunnel offload: item_release failed");
409 }
410
411 int
412 sfc_flow_tunnel_get_restore_info(struct rte_eth_dev *dev,
413                                  struct rte_mbuf *m,
414                                  struct rte_flow_restore_info *info,
415                                  struct rte_flow_error *err)
416 {
417         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
418         const struct sfc_flow_tunnel *ft;
419         sfc_ft_id_t ft_id;
420         int rc;
421
422         sfc_adapter_lock(sa);
423
424         if ((m->ol_flags & sfc_dp_ft_id_valid) == 0) {
425                 sfc_dbg(sa, "tunnel offload: get_restore_info: no tunnel mark in the packet");
426                 rc = EINVAL;
427                 goto fail;
428         }
429
430         ft_id = *RTE_MBUF_DYNFIELD(m, sfc_dp_ft_id_offset, sfc_ft_id_t *);
431         ft = &sa->flow_tunnels[ft_id];
432
433         if (ft->refcnt == 0) {
434                 sfc_err(sa, "tunnel offload: get_restore_info: tunnel=%u does not exist",
435                         ft_id);
436                 rc = ENOENT;
437                 goto fail;
438         }
439
440         memcpy(&info->tunnel, &ft->rte_tunnel, sizeof(info->tunnel));
441
442         /*
443          * The packet still has encapsulation header; JUMP rules never
444          * strip it. Therefore, set RTE_FLOW_RESTORE_INFO_ENCAPSULATED.
445          */
446         info->flags = RTE_FLOW_RESTORE_INFO_ENCAPSULATED |
447                       RTE_FLOW_RESTORE_INFO_GROUP_ID |
448                       RTE_FLOW_RESTORE_INFO_TUNNEL;
449
450         info->group_id = 0;
451
452         sfc_adapter_unlock(sa);
453
454         return 0;
455
456 fail:
457         sfc_adapter_unlock(sa);
458
459         return rte_flow_error_set(err, rc,
460                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
461                                   "tunnel offload: get_restore_info failed");
462 }