flow: explicit rss function enum cast
[vpp.git] / src / plugins / dpdk / device / flow.c
1 /*
2  * Copyright (c) 2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/vnet.h>
17 #include <vppinfra/vec.h>
18 #include <vppinfra/format.h>
19 #include <assert.h>
20
21 #include <vnet/ip/ip.h>
22 #include <vnet/ethernet/ethernet.h>
23 #include <vnet/ethernet/arp_packet.h>
24 #include <vnet/vxlan/vxlan.h>
25 #include <dpdk/device/dpdk.h>
26
27 #include <dpdk/device/dpdk_priv.h>
28 #include <vppinfra/error.h>
29
30 /* check if flow is L2 flow */
31 #define FLOW_IS_L2_LAYER(f) \
32   (f->type == VNET_FLOW_TYPE_ETHERNET)
33
34 /* check if flow is VLAN sensitive */
35 #define FLOW_IS_VLAN_TAGGED(f) \
36   ((f->type == VNET_FLOW_TYPE_IP4_N_TUPLE_TAGGED) || \
37    (f->type == VNET_FLOW_TYPE_IP6_N_TUPLE_TAGGED))
38
39 /* check if flow is L4 type */
40 #define FLOW_IS_L4_LAYER(f) \
41   ((f->type == VNET_FLOW_TYPE_IP4_N_TUPLE) || \
42    (f->type == VNET_FLOW_TYPE_IP6_N_TUPLE))
43
44 /* check if flow is L4 tunnel type */
45 #define FLOW_IS_L4_TUNNEL_LAYER(f) \
46   ((f->type >= VNET_FLOW_TYPE_IP4_VXLAN) || \
47    (f->type <= VNET_FLOW_TYPE_IP6_GTPU_IP6))
48
49 /* constant structs */
50 static const struct rte_flow_attr ingress = {.ingress = 1 };
51
52 static inline bool
53 mac_address_is_all_zero (const u8 addr[6])
54 {
55   int i = 0;
56
57   for (i = 0; i < 6; i++)
58     if (addr[i] != 0)
59       return false;
60
61   return true;
62 }
63
64 static inline void
65 dpdk_flow_convert_rss_types (u64 type, u64 * dpdk_rss_type)
66 {
67 #define BIT_IS_SET(v, b) \
68   ((v) & (u64)1<<(b))
69
70   *dpdk_rss_type = 0;
71
72 #undef _
73 #define _(n, f, s) \
74       if (n != -1 && BIT_IS_SET(type, n)) \
75         *dpdk_rss_type |= f;
76
77   foreach_dpdk_rss_hf
78 #undef _
79     return;
80 }
81
82 static int
83 dpdk_flow_add (dpdk_device_t * xd, vnet_flow_t * f, dpdk_flow_entry_t * fe)
84 {
85   struct rte_flow_item_eth eth[2] = { };
86   struct rte_flow_item_ipv4 ip4[2] = { };
87   struct rte_flow_item_ipv4 inner_ip4[2] = { };
88   struct rte_flow_item_ipv6 ip6[2] = { };
89   struct rte_flow_item_ipv6 inner_ip6[2] = { };
90   struct rte_flow_item_udp udp[2] = { };
91   struct rte_flow_item_tcp tcp[2] = { };
92   struct rte_flow_item_gtp gtp[2] = { };
93   struct rte_flow_action_mark mark = { 0 };
94   struct rte_flow_action_queue queue = { 0 };
95   struct rte_flow_action_rss rss = { 0 };
96   struct rte_flow_item *item, *items = 0;
97   struct rte_flow_action *action, *actions = 0;
98   bool fate = false;
99
100   enum
101   {
102     vxlan_hdr_sz = sizeof (vxlan_header_t),
103     raw_sz = sizeof (struct rte_flow_item_raw)
104   };
105
106   union
107   {
108     struct rte_flow_item_raw item;
109     u8 val[raw_sz + vxlan_hdr_sz];
110   } raw[2];
111
112   u16 src_port, dst_port, src_port_mask, dst_port_mask;
113   u8 protocol;
114   int rv = 0;
115
116   if (f->actions & (~xd->supported_flow_actions))
117     return VNET_FLOW_ERROR_NOT_SUPPORTED;
118
119   /* Match items */
120   /* Ethernet */
121   vec_add2 (items, item, 1);
122   item->type = RTE_FLOW_ITEM_TYPE_ETH;
123   if (f->type == VNET_FLOW_TYPE_ETHERNET)
124     {
125       vnet_flow_ethernet_t *te = &f->ethernet;
126
127       clib_memset (&eth[0], 0, sizeof (eth[0]));
128       clib_memset (&eth[1], 0, sizeof (eth[1]));
129
130       /* check if SMAC/DMAC/Ether_type assigned */
131       if (!mac_address_is_all_zero (te->eth_hdr.dst_address))
132         {
133           clib_memcpy_fast (&eth[0].dst, &te->eth_hdr.dst_address,
134                             sizeof (eth[0].dst));
135           clib_memset (&eth[1].dst, 0xFF, sizeof (eth[1].dst));
136         }
137
138       if (!mac_address_is_all_zero (te->eth_hdr.src_address))
139         {
140           clib_memcpy_fast (&eth[0].src, &te->eth_hdr.src_address,
141                             sizeof (eth[0].src));
142           clib_memset (&eth[1].src, 0xFF, sizeof (eth[1].src));
143         }
144
145       if (te->eth_hdr.type)
146         {
147           eth[0].type = clib_host_to_net_u16 (te->eth_hdr.type);
148           eth[1].type = clib_host_to_net_u16 (0xFFFF);
149         }
150
151       item->spec = eth;
152       item->mask = eth + 1;
153     }
154   else
155     {
156       item->spec = NULL;
157       item->mask = NULL;
158     }
159
160   if (FLOW_IS_VLAN_TAGGED (f))
161     {
162       vec_add2 (items, item, 1);
163       item->type = RTE_FLOW_ITEM_TYPE_VLAN;
164       item->spec = NULL;
165       item->mask = NULL;
166     }
167
168   if (FLOW_IS_L2_LAYER (f))
169     goto pattern_end;
170
171   /* IP */
172   vec_add2 (items, item, 1);
173   if ((f->type == VNET_FLOW_TYPE_IP6_N_TUPLE) ||
174       (f->type == VNET_FLOW_TYPE_IP6_GTPC) ||
175       (f->type == VNET_FLOW_TYPE_IP6_GTPU) ||
176       (f->type == VNET_FLOW_TYPE_IP6_GTPU_IP4) ||
177       (f->type == VNET_FLOW_TYPE_IP6_GTPU_IP6))
178     {
179       vnet_flow_ip6_n_tuple_t *t6 = &f->ip6_n_tuple;
180       item->type = RTE_FLOW_ITEM_TYPE_IPV6;
181
182       if (!clib_memcmp (&t6->src_addr.mask, &zero_addr, 16) &&
183           !clib_memcmp (&t6->dst_addr.mask, &zero_addr, 16))
184         {
185           item->spec = NULL;
186           item->mask = NULL;
187         }
188       else
189         {
190           clib_memcpy_fast (ip6[0].hdr.src_addr, &t6->src_addr.addr, 16);
191           clib_memcpy_fast (ip6[1].hdr.src_addr, &t6->src_addr.mask, 16);
192           clib_memcpy_fast (ip6[0].hdr.dst_addr, &t6->dst_addr.addr, 16);
193           clib_memcpy_fast (ip6[1].hdr.dst_addr, &t6->dst_addr.mask, 16);
194           item->spec = ip6;
195           item->mask = ip6 + 1;
196         }
197
198       src_port = t6->src_port.port;
199       dst_port = t6->dst_port.port;
200       src_port_mask = t6->src_port.mask;
201       dst_port_mask = t6->dst_port.mask;
202       protocol = t6->protocol;
203     }
204   else if ((f->type == VNET_FLOW_TYPE_IP4_N_TUPLE) ||
205            (f->type == VNET_FLOW_TYPE_IP4_GTPC) ||
206            (f->type == VNET_FLOW_TYPE_IP4_GTPU) ||
207            (f->type == VNET_FLOW_TYPE_IP4_GTPU_IP4) ||
208            (f->type == VNET_FLOW_TYPE_IP4_GTPU_IP6))
209     {
210       vnet_flow_ip4_n_tuple_t *t4 = &f->ip4_n_tuple;
211       item->type = RTE_FLOW_ITEM_TYPE_IPV4;
212
213       if (!t4->src_addr.mask.as_u32 && !t4->dst_addr.mask.as_u32)
214         {
215           item->spec = NULL;
216           item->mask = NULL;
217         }
218       else
219         {
220           ip4[0].hdr.src_addr = t4->src_addr.addr.as_u32;
221           ip4[1].hdr.src_addr = t4->src_addr.mask.as_u32;
222           ip4[0].hdr.dst_addr = t4->dst_addr.addr.as_u32;
223           ip4[1].hdr.dst_addr = t4->dst_addr.mask.as_u32;
224           item->spec = ip4;
225           item->mask = ip4 + 1;
226         }
227
228       src_port = t4->src_port.port;
229       dst_port = t4->dst_port.port;
230       src_port_mask = t4->src_port.mask;
231       dst_port_mask = t4->dst_port.mask;
232       protocol = t4->protocol;
233     }
234   else if (f->type == VNET_FLOW_TYPE_IP4_VXLAN)
235     {
236       vnet_flow_ip4_vxlan_t *v4 = &f->ip4_vxlan;
237       ip4[0].hdr.src_addr = v4->src_addr.as_u32;
238       ip4[1].hdr.src_addr = -1;
239       ip4[0].hdr.dst_addr = v4->dst_addr.as_u32;
240       ip4[1].hdr.dst_addr = -1;
241       item->type = RTE_FLOW_ITEM_TYPE_IPV4;
242       item->spec = ip4;
243       item->mask = ip4 + 1;
244
245       dst_port = v4->dst_port;
246       dst_port_mask = -1;
247       src_port = 0;
248       src_port_mask = 0;
249       protocol = IP_PROTOCOL_UDP;
250     }
251   else
252     {
253       rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
254       goto done;
255     }
256
257   /* Layer 4 */
258   vec_add2 (items, item, 1);
259   if (protocol == IP_PROTOCOL_UDP)
260     {
261       item->type = RTE_FLOW_ITEM_TYPE_UDP;
262
263       if ((src_port_mask == 0) && (dst_port_mask == 0))
264         {
265           item->spec = NULL;
266           item->mask = NULL;
267         }
268       else
269         {
270           udp[0].hdr.src_port = clib_host_to_net_u16 (src_port);
271           udp[1].hdr.src_port = clib_host_to_net_u16 (src_port_mask);
272           udp[0].hdr.dst_port = clib_host_to_net_u16 (dst_port);
273           udp[1].hdr.dst_port = clib_host_to_net_u16 (dst_port_mask);
274           item->spec = udp;
275           item->mask = udp + 1;
276         }
277     }
278   else if (protocol == IP_PROTOCOL_TCP)
279     {
280       item->type = RTE_FLOW_ITEM_TYPE_TCP;
281
282       if ((src_port_mask == 0) && (dst_port_mask == 0))
283         {
284           item->spec = NULL;
285           item->mask = NULL;
286         }
287       else
288         {
289           tcp[0].hdr.src_port = clib_host_to_net_u16 (src_port);
290           tcp[1].hdr.src_port = clib_host_to_net_u16 (src_port_mask);
291           tcp[0].hdr.dst_port = clib_host_to_net_u16 (dst_port);
292           tcp[1].hdr.dst_port = clib_host_to_net_u16 (dst_port_mask);
293           item->spec = tcp;
294           item->mask = tcp + 1;
295         }
296     }
297   else
298     {
299       rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
300       goto done;
301     }
302
303   /* Tunnel header match */
304   if (f->type == VNET_FLOW_TYPE_IP4_VXLAN)
305     {
306       u32 vni = f->ip4_vxlan.vni;
307       vxlan_header_t spec_hdr = {
308         .flags = VXLAN_FLAGS_I,
309         .vni_reserved = clib_host_to_net_u32 (vni << 8)
310       };
311       vxlan_header_t mask_hdr = {
312         .flags = 0xff,
313         .vni_reserved = clib_host_to_net_u32 (((u32) - 1) << 8)
314       };
315
316       clib_memset (raw, 0, sizeof raw);
317       raw[0].item.relative = 1;
318       raw[0].item.length = vxlan_hdr_sz;
319
320       clib_memcpy_fast (raw[0].val + raw_sz, &spec_hdr, vxlan_hdr_sz);
321       raw[0].item.pattern = raw[0].val + raw_sz;
322       clib_memcpy_fast (raw[1].val + raw_sz, &mask_hdr, vxlan_hdr_sz);
323       raw[1].item.pattern = raw[1].val + raw_sz;
324
325       vec_add2 (items, item, 1);
326       item->type = RTE_FLOW_ITEM_TYPE_RAW;
327       item->spec = raw;
328       item->mask = raw + 1;
329     }
330   else if (f->type == VNET_FLOW_TYPE_IP4_GTPC)
331     {
332       vnet_flow_ip4_gtpc_t *gc = &f->ip4_gtpc;
333       gtp[0].teid = clib_host_to_net_u32 (gc->teid);
334       gtp[1].teid = ~0;
335
336       vec_add2 (items, item, 1);
337       item->type = RTE_FLOW_ITEM_TYPE_GTPC;
338       item->spec = gtp;
339       item->mask = gtp + 1;
340     }
341   else if (f->type == VNET_FLOW_TYPE_IP4_GTPU)
342     {
343       vnet_flow_ip4_gtpu_t *gu = &f->ip4_gtpu;
344       gtp[0].teid = clib_host_to_net_u32 (gu->teid);
345       gtp[1].teid = ~0;
346
347       vec_add2 (items, item, 1);
348       item->type = RTE_FLOW_ITEM_TYPE_GTPU;
349       item->spec = gtp;
350       item->mask = gtp + 1;
351     }
352   else if ((f->type == VNET_FLOW_TYPE_IP4_GTPU_IP4) ||
353            (f->type == VNET_FLOW_TYPE_IP4_GTPU_IP6))
354     {
355       vnet_flow_ip4_gtpu_t *gu = &f->ip4_gtpu;
356       gtp[0].teid = clib_host_to_net_u32 (gu->teid);
357       gtp[1].teid = ~0;
358
359       vec_add2 (items, item, 1);
360       item->type = RTE_FLOW_ITEM_TYPE_GTPU;
361       item->spec = gtp;
362       item->mask = gtp + 1;
363
364       /* inner IP4 header */
365       if (f->type == VNET_FLOW_TYPE_IP4_GTPU_IP4)
366         {
367           vec_add2 (items, item, 1);
368           item->type = RTE_FLOW_ITEM_TYPE_IPV4;
369
370           vnet_flow_ip4_gtpu_ip4_t *gu4 = &f->ip4_gtpu_ip4;
371           if (!gu4->inner_src_addr.mask.as_u32 &&
372               !gu4->inner_dst_addr.mask.as_u32)
373             {
374               item->spec = NULL;
375               item->mask = NULL;
376             }
377           else
378             {
379               inner_ip4[0].hdr.src_addr = gu4->inner_src_addr.addr.as_u32;
380               inner_ip4[1].hdr.src_addr = gu4->inner_src_addr.mask.as_u32;
381               inner_ip4[0].hdr.dst_addr = gu4->inner_dst_addr.addr.as_u32;
382               inner_ip4[1].hdr.dst_addr = gu4->inner_dst_addr.mask.as_u32;
383               item->spec = inner_ip4;
384               item->mask = inner_ip4 + 1;
385             }
386         }
387       else if (f->type == VNET_FLOW_TYPE_IP4_GTPU_IP6)
388         {
389           ip6_address_t zero_addr;
390           vnet_flow_ip4_gtpu_ip6_t *gu6 = &f->ip4_gtpu_ip6;
391
392           clib_memset (&zero_addr, 0, sizeof (ip6_address_t));
393
394           vec_add2 (items, item, 1);
395           item->type = RTE_FLOW_ITEM_TYPE_IPV6;
396
397           if (!clib_memcmp (&gu6->inner_src_addr.mask, &zero_addr, 16) &&
398               !clib_memcmp (&gu6->inner_dst_addr.mask, &zero_addr, 16))
399             {
400               item->spec = NULL;
401               item->mask = NULL;
402             }
403           else
404             {
405               clib_memcpy_fast (inner_ip6[0].hdr.src_addr,
406                                 &gu6->inner_src_addr.addr, 16);
407               clib_memcpy_fast (inner_ip6[1].hdr.src_addr,
408                                 &gu6->inner_src_addr.mask, 16);
409               clib_memcpy_fast (inner_ip6[0].hdr.dst_addr,
410                                 &gu6->inner_dst_addr.addr, 16);
411               clib_memcpy_fast (inner_ip6[1].hdr.dst_addr,
412                                 &gu6->inner_dst_addr.mask, 16);
413               item->spec = inner_ip6;
414               item->mask = inner_ip6 + 1;
415             }
416         }
417     }
418   else if (f->type == VNET_FLOW_TYPE_IP6_GTPC)
419     {
420       vnet_flow_ip6_gtpc_t *gc = &f->ip6_gtpc;
421       gtp[0].teid = clib_host_to_net_u32 (gc->teid);
422       gtp[1].teid = ~0;
423
424       vec_add2 (items, item, 1);
425       item->type = RTE_FLOW_ITEM_TYPE_GTPC;
426       item->spec = gtp;
427       item->mask = gtp + 1;
428     }
429   else if (f->type == VNET_FLOW_TYPE_IP6_GTPU)
430     {
431       vnet_flow_ip6_gtpu_t *gu = &f->ip6_gtpu;
432       gtp[0].teid = clib_host_to_net_u32 (gu->teid);
433       gtp[1].teid = ~0;
434
435       vec_add2 (items, item, 1);
436       item->type = RTE_FLOW_ITEM_TYPE_GTPU;
437       item->spec = gtp;
438       item->mask = gtp + 1;
439     }
440   else if ((f->type == VNET_FLOW_TYPE_IP6_GTPU_IP4) ||
441            (f->type == VNET_FLOW_TYPE_IP6_GTPU_IP6))
442     {
443       vnet_flow_ip6_gtpu_t *gu = &f->ip6_gtpu;
444       gtp[0].teid = clib_host_to_net_u32 (gu->teid);
445       gtp[1].teid = ~0;
446
447       vec_add2 (items, item, 1);
448       item->type = RTE_FLOW_ITEM_TYPE_GTPU;
449       item->spec = gtp;
450       item->mask = gtp + 1;
451
452       /* inner IP4 header */
453       if (f->type == VNET_FLOW_TYPE_IP6_GTPU_IP4)
454         {
455           vec_add2 (items, item, 1);
456           item->type = RTE_FLOW_ITEM_TYPE_IPV4;
457
458           vnet_flow_ip6_gtpu_ip4_t *gu4 = &f->ip6_gtpu_ip4;
459
460           if (!gu4->inner_src_addr.mask.as_u32 &&
461               !gu4->inner_dst_addr.mask.as_u32)
462             {
463               item->spec = NULL;
464               item->mask = NULL;
465             }
466           else
467             {
468               inner_ip4[0].hdr.src_addr = gu4->inner_src_addr.addr.as_u32;
469               inner_ip4[1].hdr.src_addr = gu4->inner_src_addr.mask.as_u32;
470               inner_ip4[0].hdr.dst_addr = gu4->inner_dst_addr.addr.as_u32;
471               inner_ip4[1].hdr.dst_addr = gu4->inner_dst_addr.mask.as_u32;
472               item->spec = inner_ip4;
473               item->mask = inner_ip4 + 1;
474             }
475         }
476
477       if (f->type == VNET_FLOW_TYPE_IP6_GTPU_IP6)
478         {
479           ip6_address_t zero_addr;
480           vnet_flow_ip6_gtpu_ip6_t *gu6 = &f->ip6_gtpu_ip6;
481
482           clib_memset (&zero_addr, 0, sizeof (ip6_address_t));
483
484           vec_add2 (items, item, 1);
485           item->type = RTE_FLOW_ITEM_TYPE_IPV6;
486
487           if (!clib_memcmp (&gu6->inner_src_addr.mask, &zero_addr, 16) &&
488               !clib_memcmp (&gu6->inner_dst_addr.mask, &zero_addr, 16))
489             {
490               item->spec = NULL;
491               item->mask = NULL;
492             }
493           else
494             {
495               clib_memcpy_fast (inner_ip6[0].hdr.src_addr,
496                                 &gu6->inner_src_addr.addr, 16);
497               clib_memcpy_fast (inner_ip6[1].hdr.src_addr,
498                                 &gu6->inner_src_addr.mask, 16);
499               clib_memcpy_fast (inner_ip6[0].hdr.dst_addr,
500                                 &gu6->inner_dst_addr.addr, 16);
501               clib_memcpy_fast (inner_ip6[1].hdr.dst_addr,
502                                 &gu6->inner_dst_addr.mask, 16);
503               item->spec = inner_ip6;
504               item->mask = inner_ip6 + 1;
505             }
506
507         }
508     }
509
510 pattern_end:
511   vec_add2 (items, item, 1);
512   item->type = RTE_FLOW_ITEM_TYPE_END;
513
514   /* Actions */
515   /* Only one 'fate' can be assigned */
516   if (f->actions & VNET_FLOW_ACTION_REDIRECT_TO_QUEUE)
517     {
518       vec_add2 (actions, action, 1);
519       queue.index = f->redirect_queue;
520       action->type = RTE_FLOW_ACTION_TYPE_QUEUE;
521       action->conf = &queue;
522       fate = true;
523     }
524   if (f->actions & VNET_FLOW_ACTION_DROP)
525     {
526       vec_add2 (actions, action, 1);
527       action->type = RTE_FLOW_ACTION_TYPE_DROP;
528       if (fate == true)
529         {
530           rv = VNET_FLOW_ERROR_INTERNAL;
531           goto done;
532         }
533       else
534         fate = true;
535     }
536   if (f->actions & VNET_FLOW_ACTION_RSS)
537     {
538       u64 rss_type = 0;
539       vec_add2 (actions, action, 1);
540       action->type = RTE_FLOW_ACTION_TYPE_RSS;
541       action->conf = &rss;
542
543       /* convert types to DPDK rss bitmask */
544       dpdk_flow_convert_rss_types (f->rss_types, &rss_type);
545
546       rss.types = rss_type;
547       rss.func = (enum rte_eth_hash_function) f->rss_fun;
548
549       if (fate == true)
550         {
551           rv = VNET_FLOW_ERROR_INTERNAL;
552           goto done;
553         }
554       else
555         fate = true;
556     }
557   if (fate == false)
558     {
559       vec_add2 (actions, action, 1);
560       action->type = RTE_FLOW_ACTION_TYPE_PASSTHRU;
561     }
562
563   if (f->actions & VNET_FLOW_ACTION_MARK)
564     {
565       vec_add2 (actions, action, 1);
566       mark.id = fe->mark;
567       action->type = RTE_FLOW_ACTION_TYPE_MARK;
568       action->conf = &mark;
569     }
570
571   vec_add2 (actions, action, 1);
572   action->type = RTE_FLOW_ACTION_TYPE_END;
573
574   rv = rte_flow_validate (xd->device_index, &ingress, items, actions,
575                           &xd->last_flow_error);
576
577   if (rv)
578     {
579       if (rv == -EINVAL)
580         rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
581       else if (rv == -EEXIST)
582         rv = VNET_FLOW_ERROR_ALREADY_EXISTS;
583       else
584         rv = VNET_FLOW_ERROR_INTERNAL;
585       goto done;
586     }
587
588   fe->handle = rte_flow_create (xd->device_index, &ingress, items, actions,
589                                 &xd->last_flow_error);
590
591   if (!fe->handle)
592     rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
593
594 done:
595   vec_free (items);
596   vec_free (actions);
597   return rv;
598 }
599
600 int
601 dpdk_flow_ops_fn (vnet_main_t * vnm, vnet_flow_dev_op_t op, u32 dev_instance,
602                   u32 flow_index, uword * private_data)
603 {
604   dpdk_main_t *dm = &dpdk_main;
605   vnet_flow_t *flow = vnet_get_flow (flow_index);
606   dpdk_device_t *xd = vec_elt_at_index (dm->devices, dev_instance);
607   dpdk_flow_entry_t *fe;
608   dpdk_flow_lookup_entry_t *fle = 0;
609   int rv;
610
611   /* recycle old flow lookup entries only after the main loop counter
612      increases - i.e. previously DMA'ed packets were handled */
613   if (vec_len (xd->parked_lookup_indexes) > 0 &&
614       xd->parked_loop_count != dm->vlib_main->main_loop_count)
615     {
616       u32 *fl_index;
617
618       vec_foreach (fl_index, xd->parked_lookup_indexes)
619         pool_put_index (xd->flow_lookup_entries, *fl_index);
620       vec_reset_length (xd->parked_lookup_indexes);
621     }
622
623   if (op == VNET_FLOW_DEV_OP_DEL_FLOW)
624     {
625       fe = vec_elt_at_index (xd->flow_entries, *private_data);
626
627       if ((rv = rte_flow_destroy (xd->device_index, fe->handle,
628                                   &xd->last_flow_error)))
629         return VNET_FLOW_ERROR_INTERNAL;
630
631       if (fe->mark)
632         {
633           /* make sure no action is taken for in-flight (marked) packets */
634           fle = pool_elt_at_index (xd->flow_lookup_entries, fe->mark);
635           clib_memset (fle, -1, sizeof (*fle));
636           vec_add1 (xd->parked_lookup_indexes, fe->mark);
637           xd->parked_loop_count = dm->vlib_main->main_loop_count;
638         }
639
640       clib_memset (fe, 0, sizeof (*fe));
641       pool_put (xd->flow_entries, fe);
642
643       goto disable_rx_offload;
644     }
645
646   if (op != VNET_FLOW_DEV_OP_ADD_FLOW)
647     return VNET_FLOW_ERROR_NOT_SUPPORTED;
648
649   pool_get (xd->flow_entries, fe);
650   fe->flow_index = flow->index;
651
652   if (flow->actions == 0)
653     {
654       rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
655       goto done;
656     }
657
658   /* if we need to mark packets, assign one mark */
659   if (flow->actions & (VNET_FLOW_ACTION_MARK |
660                        VNET_FLOW_ACTION_REDIRECT_TO_NODE |
661                        VNET_FLOW_ACTION_BUFFER_ADVANCE))
662     {
663       /* reserve slot 0 */
664       if (xd->flow_lookup_entries == 0)
665         pool_get_aligned (xd->flow_lookup_entries, fle,
666                           CLIB_CACHE_LINE_BYTES);
667       pool_get_aligned (xd->flow_lookup_entries, fle, CLIB_CACHE_LINE_BYTES);
668       fe->mark = fle - xd->flow_lookup_entries;
669
670       /* install entry in the lookup table */
671       clib_memset (fle, -1, sizeof (*fle));
672       if (flow->actions & VNET_FLOW_ACTION_MARK)
673         fle->flow_id = flow->mark_flow_id;
674       if (flow->actions & VNET_FLOW_ACTION_REDIRECT_TO_NODE)
675         fle->next_index = flow->redirect_device_input_next_index;
676       if (flow->actions & VNET_FLOW_ACTION_BUFFER_ADVANCE)
677         fle->buffer_advance = flow->buffer_advance;
678     }
679   else
680     fe->mark = 0;
681
682   if ((xd->flags & DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD) == 0)
683     {
684       xd->flags |= DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD;
685       dpdk_device_setup (xd);
686     }
687
688   switch (flow->type)
689     {
690     case VNET_FLOW_TYPE_ETHERNET:
691     case VNET_FLOW_TYPE_IP4_N_TUPLE:
692     case VNET_FLOW_TYPE_IP6_N_TUPLE:
693     case VNET_FLOW_TYPE_IP4_VXLAN:
694     case VNET_FLOW_TYPE_IP4_GTPC:
695     case VNET_FLOW_TYPE_IP4_GTPU:
696     case VNET_FLOW_TYPE_IP4_GTPU_IP4:
697     case VNET_FLOW_TYPE_IP4_GTPU_IP6:
698     case VNET_FLOW_TYPE_IP6_GTPC:
699     case VNET_FLOW_TYPE_IP6_GTPU:
700     case VNET_FLOW_TYPE_IP6_GTPU_IP4:
701     case VNET_FLOW_TYPE_IP6_GTPU_IP6:
702       if ((rv = dpdk_flow_add (xd, flow, fe)))
703         goto done;
704       break;
705     default:
706       rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
707       goto done;
708     }
709
710   *private_data = fe - xd->flow_entries;
711
712 done:
713   if (rv)
714     {
715       clib_memset (fe, 0, sizeof (*fe));
716       pool_put (xd->flow_entries, fe);
717       if (fle)
718         {
719           clib_memset (fle, -1, sizeof (*fle));
720           pool_put (xd->flow_lookup_entries, fle);
721         }
722     }
723 disable_rx_offload:
724   if ((xd->flags & DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD) != 0
725       && pool_elts (xd->flow_entries) == 0)
726     {
727       xd->flags &= ~DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD;
728       dpdk_device_setup (xd);
729     }
730
731   return rv;
732 }
733
734 u8 *
735 format_dpdk_flow (u8 * s, va_list * args)
736 {
737   u32 dev_instance = va_arg (*args, u32);
738   u32 flow_index = va_arg (*args, u32);
739   uword private_data = va_arg (*args, uword);
740   dpdk_main_t *dm = &dpdk_main;
741   dpdk_device_t *xd = vec_elt_at_index (dm->devices, dev_instance);
742   dpdk_flow_entry_t *fe;
743
744   if (flow_index == ~0)
745     {
746       s = format (s, "%-25s: %U\n", "supported flow actions",
747                   format_flow_actions, xd->supported_flow_actions);
748       s = format (s, "%-25s: %d\n", "last DPDK error type",
749                   xd->last_flow_error.type);
750       s = format (s, "%-25s: %s\n", "last DPDK error message",
751                   xd->last_flow_error.message ? xd->last_flow_error.message :
752                   "n/a");
753       return s;
754     }
755
756   if (private_data >= vec_len (xd->flow_entries))
757     return format (s, "unknown flow");
758
759   fe = vec_elt_at_index (xd->flow_entries, private_data);
760   s = format (s, "mark %u", fe->mark);
761   return s;
762 }
763
764 /*
765  * fd.io coding-style-patch-verification: ON
766  *
767  * Local Variables:
768  * eval: (c-set-style "gnu")
769  * End:
770  */