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