net/cnxk: support meter action to flow destroy
[dpdk.git] / drivers / net / cnxk / cnxk_rte_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4 #include <cnxk_rte_flow.h>
5
6 const struct cnxk_rte_flow_term_info term[] = {
7         [RTE_FLOW_ITEM_TYPE_ETH] = {ROC_NPC_ITEM_TYPE_ETH,
8                                     sizeof(struct rte_flow_item_eth)},
9         [RTE_FLOW_ITEM_TYPE_VLAN] = {ROC_NPC_ITEM_TYPE_VLAN,
10                                      sizeof(struct rte_flow_item_vlan)},
11         [RTE_FLOW_ITEM_TYPE_E_TAG] = {ROC_NPC_ITEM_TYPE_E_TAG,
12                                       sizeof(struct rte_flow_item_e_tag)},
13         [RTE_FLOW_ITEM_TYPE_IPV4] = {ROC_NPC_ITEM_TYPE_IPV4,
14                                      sizeof(struct rte_flow_item_ipv4)},
15         [RTE_FLOW_ITEM_TYPE_IPV6] = {ROC_NPC_ITEM_TYPE_IPV6,
16                                      sizeof(struct rte_flow_item_ipv6)},
17         [RTE_FLOW_ITEM_TYPE_ARP_ETH_IPV4] = {
18                         ROC_NPC_ITEM_TYPE_ARP_ETH_IPV4,
19                         sizeof(struct rte_flow_item_arp_eth_ipv4)},
20         [RTE_FLOW_ITEM_TYPE_MPLS] = {ROC_NPC_ITEM_TYPE_MPLS,
21                                      sizeof(struct rte_flow_item_mpls)},
22         [RTE_FLOW_ITEM_TYPE_ICMP] = {ROC_NPC_ITEM_TYPE_ICMP,
23                                      sizeof(struct rte_flow_item_icmp)},
24         [RTE_FLOW_ITEM_TYPE_UDP] = {ROC_NPC_ITEM_TYPE_UDP,
25                                     sizeof(struct rte_flow_item_udp)},
26         [RTE_FLOW_ITEM_TYPE_TCP] = {ROC_NPC_ITEM_TYPE_TCP,
27                                     sizeof(struct rte_flow_item_tcp)},
28         [RTE_FLOW_ITEM_TYPE_SCTP] = {ROC_NPC_ITEM_TYPE_SCTP,
29                                      sizeof(struct rte_flow_item_sctp)},
30         [RTE_FLOW_ITEM_TYPE_ESP] = {ROC_NPC_ITEM_TYPE_ESP,
31                                     sizeof(struct rte_flow_item_esp)},
32         [RTE_FLOW_ITEM_TYPE_GRE] = {ROC_NPC_ITEM_TYPE_GRE,
33                                     sizeof(struct rte_flow_item_gre)},
34         [RTE_FLOW_ITEM_TYPE_NVGRE] = {ROC_NPC_ITEM_TYPE_NVGRE,
35                                       sizeof(struct rte_flow_item_nvgre)},
36         [RTE_FLOW_ITEM_TYPE_VXLAN] = {ROC_NPC_ITEM_TYPE_VXLAN,
37                                       sizeof(struct rte_flow_item_vxlan)},
38         [RTE_FLOW_ITEM_TYPE_GTPC] = {ROC_NPC_ITEM_TYPE_GTPC,
39                                      sizeof(struct rte_flow_item_gtp)},
40         [RTE_FLOW_ITEM_TYPE_GTPU] = {ROC_NPC_ITEM_TYPE_GTPU,
41                                      sizeof(struct rte_flow_item_gtp)},
42         [RTE_FLOW_ITEM_TYPE_GENEVE] = {ROC_NPC_ITEM_TYPE_GENEVE,
43                                        sizeof(struct rte_flow_item_geneve)},
44         [RTE_FLOW_ITEM_TYPE_VXLAN_GPE] = {
45                         ROC_NPC_ITEM_TYPE_VXLAN_GPE,
46                         sizeof(struct rte_flow_item_vxlan_gpe)},
47         [RTE_FLOW_ITEM_TYPE_IPV6_EXT] = {ROC_NPC_ITEM_TYPE_IPV6_EXT,
48                                          sizeof(struct rte_flow_item_ipv6_ext)},
49         [RTE_FLOW_ITEM_TYPE_VOID] = {ROC_NPC_ITEM_TYPE_VOID, 0},
50         [RTE_FLOW_ITEM_TYPE_ANY] = {ROC_NPC_ITEM_TYPE_ANY, 0},
51         [RTE_FLOW_ITEM_TYPE_GRE_KEY] = {ROC_NPC_ITEM_TYPE_GRE_KEY,
52                                         sizeof(uint32_t)},
53         [RTE_FLOW_ITEM_TYPE_HIGIG2] = {ROC_NPC_ITEM_TYPE_HIGIG2,
54                                        sizeof(struct rte_flow_item_higig2_hdr)},
55         [RTE_FLOW_ITEM_TYPE_RAW] = {ROC_NPC_ITEM_TYPE_RAW,
56                                     sizeof(struct rte_flow_item_raw)}};
57
58 static int
59 npc_rss_action_validate(struct rte_eth_dev *eth_dev,
60                         const struct rte_flow_attr *attr,
61                         const struct rte_flow_action *act)
62 {
63         const struct rte_flow_action_rss *rss;
64
65         rss = (const struct rte_flow_action_rss *)act->conf;
66
67         if (attr->egress) {
68                 plt_err("No support of RSS in egress");
69                 return -EINVAL;
70         }
71
72         if (eth_dev->data->dev_conf.rxmode.mq_mode != ETH_MQ_RX_RSS) {
73                 plt_err("multi-queue mode is disabled");
74                 return -ENOTSUP;
75         }
76
77         if (!rss || !rss->queue_num) {
78                 plt_err("no valid queues");
79                 return -EINVAL;
80         }
81
82         if (rss->func != RTE_ETH_HASH_FUNCTION_DEFAULT) {
83                 plt_err("non-default RSS hash functions are not supported");
84                 return -ENOTSUP;
85         }
86
87         if (rss->key_len && rss->key_len > ROC_NIX_RSS_KEY_LEN) {
88                 plt_err("RSS hash key too large");
89                 return -ENOTSUP;
90         }
91
92         return 0;
93 }
94
95 static void
96 npc_rss_flowkey_get(struct cnxk_eth_dev *eth_dev,
97                     const struct roc_npc_action *rss_action,
98                     uint32_t *flowkey_cfg)
99 {
100         const struct roc_npc_action_rss *rss;
101
102         rss = (const struct roc_npc_action_rss *)rss_action->conf;
103
104         *flowkey_cfg = cnxk_rss_ethdev_to_nix(eth_dev, rss->types, rss->level);
105 }
106
107 static int
108 cnxk_map_actions(struct rte_eth_dev *eth_dev, const struct rte_flow_attr *attr,
109                  const struct rte_flow_action actions[],
110                  struct roc_npc_action in_actions[], uint32_t *flowkey_cfg)
111 {
112         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
113         const struct rte_flow_action_queue *act_q;
114         int i = 0, rc = 0;
115         int rq;
116
117         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
118                 switch (actions->type) {
119                 case RTE_FLOW_ACTION_TYPE_VOID:
120                         in_actions[i].type = ROC_NPC_ACTION_TYPE_VOID;
121                         break;
122
123                 case RTE_FLOW_ACTION_TYPE_MARK:
124                         in_actions[i].type = ROC_NPC_ACTION_TYPE_MARK;
125                         in_actions[i].conf = actions->conf;
126                         break;
127
128                 case RTE_FLOW_ACTION_TYPE_FLAG:
129                         in_actions[i].type = ROC_NPC_ACTION_TYPE_FLAG;
130                         break;
131
132                 case RTE_FLOW_ACTION_TYPE_COUNT:
133                         in_actions[i].type = ROC_NPC_ACTION_TYPE_COUNT;
134                         break;
135
136                 case RTE_FLOW_ACTION_TYPE_DROP:
137                         in_actions[i].type = ROC_NPC_ACTION_TYPE_DROP;
138                         break;
139
140                 case RTE_FLOW_ACTION_TYPE_PF:
141                         in_actions[i].type = ROC_NPC_ACTION_TYPE_PF;
142                         break;
143
144                 case RTE_FLOW_ACTION_TYPE_VF:
145                         in_actions[i].type = ROC_NPC_ACTION_TYPE_VF;
146                         in_actions[i].conf = actions->conf;
147                         break;
148
149                 case RTE_FLOW_ACTION_TYPE_QUEUE:
150                         act_q = (const struct rte_flow_action_queue *)
151                                         actions->conf;
152                         rq = act_q->index;
153                         if (rq >= eth_dev->data->nb_rx_queues) {
154                                 plt_npc_dbg("Invalid queue index");
155                                 goto err_exit;
156                         }
157                         in_actions[i].type = ROC_NPC_ACTION_TYPE_QUEUE;
158                         in_actions[i].conf = actions->conf;
159                         break;
160
161                 case RTE_FLOW_ACTION_TYPE_RSS:
162                         rc = npc_rss_action_validate(eth_dev, attr, actions);
163                         if (rc)
164                                 goto err_exit;
165                         in_actions[i].type = ROC_NPC_ACTION_TYPE_RSS;
166                         in_actions[i].conf = actions->conf;
167                         npc_rss_flowkey_get(dev, &in_actions[i], flowkey_cfg);
168                         break;
169
170                 case RTE_FLOW_ACTION_TYPE_SECURITY:
171                         in_actions[i].type = ROC_NPC_ACTION_TYPE_SEC;
172                         break;
173                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
174                         in_actions[i].type = ROC_NPC_ACTION_TYPE_VLAN_STRIP;
175                         break;
176                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
177                         in_actions[i].type = ROC_NPC_ACTION_TYPE_VLAN_INSERT;
178                         in_actions[i].conf = actions->conf;
179                         break;
180                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
181                         in_actions[i].type =
182                                 ROC_NPC_ACTION_TYPE_VLAN_ETHTYPE_INSERT;
183                         in_actions[i].conf = actions->conf;
184                         break;
185                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
186                         in_actions[i].type =
187                                 ROC_NPC_ACTION_TYPE_VLAN_PCP_INSERT;
188                         in_actions[i].conf = actions->conf;
189                         break;
190                 case RTE_FLOW_ACTION_TYPE_METER:
191                         in_actions[i].type = ROC_NPC_ACTION_TYPE_METER;
192                         in_actions[i].conf = actions->conf;
193                         break;
194                 default:
195                         plt_npc_dbg("Action is not supported = %d",
196                                     actions->type);
197                         goto err_exit;
198                 }
199                 i++;
200         }
201         in_actions[i].type = ROC_NPC_ACTION_TYPE_END;
202         return 0;
203
204 err_exit:
205         return -EINVAL;
206 }
207
208 static int
209 cnxk_map_flow_data(struct rte_eth_dev *eth_dev,
210                    const struct rte_flow_attr *attr,
211                    const struct rte_flow_item pattern[],
212                    const struct rte_flow_action actions[],
213                    struct roc_npc_attr *in_attr,
214                    struct roc_npc_item_info in_pattern[],
215                    struct roc_npc_action in_actions[], uint32_t *flowkey_cfg)
216 {
217         int i = 0;
218
219         in_attr->priority = attr->priority;
220         in_attr->ingress = attr->ingress;
221         in_attr->egress = attr->egress;
222
223         while (pattern->type != RTE_FLOW_ITEM_TYPE_END) {
224                 in_pattern[i].spec = pattern->spec;
225                 in_pattern[i].last = pattern->last;
226                 in_pattern[i].mask = pattern->mask;
227                 in_pattern[i].type = term[pattern->type].item_type;
228                 in_pattern[i].size = term[pattern->type].item_size;
229                 pattern++;
230                 i++;
231         }
232         in_pattern[i].type = ROC_NPC_ITEM_TYPE_END;
233
234         return cnxk_map_actions(eth_dev, attr, actions, in_actions,
235                                 flowkey_cfg);
236 }
237
238 static int
239 cnxk_flow_validate(struct rte_eth_dev *eth_dev,
240                    const struct rte_flow_attr *attr,
241                    const struct rte_flow_item pattern[],
242                    const struct rte_flow_action actions[],
243                    struct rte_flow_error *error)
244 {
245         struct roc_npc_item_info in_pattern[ROC_NPC_ITEM_TYPE_END + 1];
246         struct roc_npc_action in_actions[ROC_NPC_MAX_ACTION_COUNT];
247         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
248         struct roc_npc *npc = &dev->npc;
249         struct roc_npc_attr in_attr;
250         struct roc_npc_flow flow;
251         uint32_t flowkey_cfg = 0;
252         int rc;
253
254         memset(&flow, 0, sizeof(flow));
255
256         rc = cnxk_map_flow_data(eth_dev, attr, pattern, actions, &in_attr,
257                                 in_pattern, in_actions, &flowkey_cfg);
258         if (rc) {
259                 rte_flow_error_set(error, 0, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
260                                    NULL, "Failed to map flow data");
261                 return rc;
262         }
263
264         return roc_npc_flow_parse(npc, &in_attr, in_pattern, in_actions, &flow);
265 }
266
267 struct roc_npc_flow *
268 cnxk_flow_create(struct rte_eth_dev *eth_dev, const struct rte_flow_attr *attr,
269                  const struct rte_flow_item pattern[],
270                  const struct rte_flow_action actions[],
271                  struct rte_flow_error *error)
272 {
273         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
274         struct roc_npc_item_info in_pattern[ROC_NPC_ITEM_TYPE_END + 1];
275         struct roc_npc_action in_actions[ROC_NPC_MAX_ACTION_COUNT];
276         struct roc_npc *npc = &dev->npc;
277         struct roc_npc_attr in_attr;
278         struct roc_npc_flow *flow;
279         int errcode = 0;
280         int rc;
281
282         rc = cnxk_map_flow_data(eth_dev, attr, pattern, actions, &in_attr,
283                                 in_pattern, in_actions,
284                                 &npc->flowkey_cfg_state);
285         if (rc) {
286                 rte_flow_error_set(error, 0, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
287                                    NULL, "Failed to map flow data");
288                 return NULL;
289         }
290
291         flow = roc_npc_flow_create(npc, &in_attr, in_pattern, in_actions,
292                                    &errcode);
293         if (errcode != 0) {
294                 rte_flow_error_set(error, errcode, errcode, NULL,
295                                    roc_error_msg_get(errcode));
296                 return NULL;
297         }
298
299         return flow;
300 }
301
302 int
303 cnxk_flow_destroy(struct rte_eth_dev *eth_dev, struct roc_npc_flow *flow,
304                   struct rte_flow_error *error)
305 {
306         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
307         struct roc_npc *npc = &dev->npc;
308         int rc;
309
310         rc = roc_npc_flow_destroy(npc, flow);
311         if (rc)
312                 rte_flow_error_set(error, rc, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
313                                    NULL, "Flow Destroy failed");
314         return rc;
315 }
316
317 static int
318 cnxk_flow_flush(struct rte_eth_dev *eth_dev, struct rte_flow_error *error)
319 {
320         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
321         struct roc_npc *npc = &dev->npc;
322         int rc;
323
324         rc = roc_npc_mcam_free_all_resources(npc);
325         if (rc) {
326                 rte_flow_error_set(error, EIO, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
327                                    NULL, "Failed to flush filter");
328                 return -rte_errno;
329         }
330
331         return 0;
332 }
333
334 static int
335 cnxk_flow_query(struct rte_eth_dev *eth_dev, struct rte_flow *flow,
336                 const struct rte_flow_action *action, void *data,
337                 struct rte_flow_error *error)
338 {
339         struct roc_npc_flow *in_flow = (struct roc_npc_flow *)flow;
340         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
341         struct roc_npc *npc = &dev->npc;
342         struct rte_flow_query_count *query = data;
343         const char *errmsg = NULL;
344         int errcode = ENOTSUP;
345         int rc;
346
347         if (action->type != RTE_FLOW_ACTION_TYPE_COUNT) {
348                 errmsg = "Only COUNT is supported in query";
349                 goto err_exit;
350         }
351
352         if (in_flow->ctr_id == NPC_COUNTER_NONE) {
353                 errmsg = "Counter is not available";
354                 goto err_exit;
355         }
356
357         rc = roc_npc_mcam_read_counter(npc, in_flow->ctr_id, &query->hits);
358         if (rc != 0) {
359                 errcode = EIO;
360                 errmsg = "Error reading flow counter";
361                 goto err_exit;
362         }
363         query->hits_set = 1;
364         query->bytes_set = 0;
365
366         if (query->reset)
367                 rc = roc_npc_mcam_clear_counter(npc, in_flow->ctr_id);
368         if (rc != 0) {
369                 errcode = EIO;
370                 errmsg = "Error clearing flow counter";
371                 goto err_exit;
372         }
373
374         return 0;
375
376 err_exit:
377         rte_flow_error_set(error, errcode, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
378                            NULL, errmsg);
379         return -rte_errno;
380 }
381
382 static int
383 cnxk_flow_isolate(struct rte_eth_dev *eth_dev __rte_unused,
384                   int enable __rte_unused, struct rte_flow_error *error)
385 {
386         /* If we support, we need to un-install the default mcam
387          * entry for this port.
388          */
389
390         rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
391                            NULL, "Flow isolation not supported");
392
393         return -rte_errno;
394 }
395
396 static int
397 cnxk_flow_dev_dump(struct rte_eth_dev *eth_dev, struct rte_flow *flow,
398                    FILE *file, struct rte_flow_error *error)
399 {
400         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
401         struct roc_npc *npc = &dev->npc;
402
403         if (file == NULL) {
404                 rte_flow_error_set(error, EINVAL,
405                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
406                                    "Invalid file");
407                 return -rte_errno;
408         }
409
410         if (flow != NULL) {
411                 rte_flow_error_set(error, EINVAL,
412                                    RTE_FLOW_ERROR_TYPE_HANDLE,
413                                    NULL,
414                                    "Invalid argument");
415                 return -EINVAL;
416         }
417
418         roc_npc_flow_dump(file, npc);
419
420         return 0;
421 }
422
423 struct rte_flow_ops cnxk_flow_ops = {
424         .validate = cnxk_flow_validate,
425         .flush = cnxk_flow_flush,
426         .query = cnxk_flow_query,
427         .isolate = cnxk_flow_isolate,
428         .dev_dump = cnxk_flow_dev_dump,
429 };