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