flow: Add 'drop' and 'redirect-to-queue' actions support
[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 <vlib/unix/cj.h>
20 #include <assert.h>
21
22 #include <vnet/ip/ip.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/ethernet/arp_packet.h>
25 #include <vnet/vxlan/vxlan.h>
26 #include <dpdk/device/dpdk.h>
27
28 #include <dpdk/device/dpdk_priv.h>
29 #include <vppinfra/error.h>
30
31 /* constant structs */
32 static const struct rte_flow_attr ingress = {.ingress = 1 };
33 static const struct rte_flow_item_eth any_eth[2] = { };
34 static const struct rte_flow_item_vlan any_vlan[2] = { };
35
36 static int
37 dpdk_flow_add (dpdk_device_t * xd, vnet_flow_t * f, dpdk_flow_entry_t * fe)
38 {
39   struct rte_flow_item_ipv4 ip4[2] = { };
40   struct rte_flow_item_ipv6 ip6[2] = { };
41   struct rte_flow_item_udp udp[2] = { };
42   struct rte_flow_item_tcp tcp[2] = { };
43   struct rte_flow_action_mark mark = { 0 };
44   struct rte_flow_action_queue queue = { 0 };
45   struct rte_flow_item *item, *items = 0;
46   struct rte_flow_action *action, *actions = 0;
47   bool fate = false;
48
49   enum
50   {
51     vxlan_hdr_sz = sizeof (vxlan_header_t),
52     raw_sz = sizeof (struct rte_flow_item_raw)
53   };
54
55   union
56   {
57     struct rte_flow_item_raw item;
58     u8 val[raw_sz + vxlan_hdr_sz];
59   } raw[2];
60
61   u16 src_port, dst_port, src_port_mask, dst_port_mask;
62   u8 protocol;
63   int rv = 0;
64
65   if (f->actions & (~xd->supported_flow_actions))
66     return VNET_FLOW_ERROR_NOT_SUPPORTED;
67
68   /* Match items */
69   /* Ethernet */
70   vec_add2 (items, item, 1);
71   item->type = RTE_FLOW_ITEM_TYPE_ETH;
72   item->spec = any_eth;
73   item->mask = any_eth + 1;
74
75   /* VLAN */
76   if (f->type != VNET_FLOW_TYPE_IP4_VXLAN)
77     {
78       vec_add2 (items, item, 1);
79       item->type = RTE_FLOW_ITEM_TYPE_VLAN;
80       item->spec = any_vlan;
81       item->mask = any_vlan + 1;
82     }
83
84   /* IP */
85   vec_add2 (items, item, 1);
86   if (f->type == VNET_FLOW_TYPE_IP6_N_TUPLE)
87     {
88       vnet_flow_ip6_n_tuple_t *t6 = &f->ip6_n_tuple;
89       clib_memcpy_fast (ip6[0].hdr.src_addr, &t6->src_addr.addr, 16);
90       clib_memcpy_fast (ip6[1].hdr.src_addr, &t6->src_addr.mask, 16);
91       clib_memcpy_fast (ip6[0].hdr.dst_addr, &t6->dst_addr.addr, 16);
92       clib_memcpy_fast (ip6[1].hdr.dst_addr, &t6->dst_addr.mask, 16);
93       item->type = RTE_FLOW_ITEM_TYPE_IPV6;
94       item->spec = ip6;
95       item->mask = ip6 + 1;
96
97       src_port = t6->src_port.port;
98       dst_port = t6->dst_port.port;
99       src_port_mask = t6->src_port.mask;
100       dst_port_mask = t6->dst_port.mask;
101       protocol = t6->protocol;
102     }
103   else if (f->type == VNET_FLOW_TYPE_IP4_N_TUPLE)
104     {
105       vnet_flow_ip4_n_tuple_t *t4 = &f->ip4_n_tuple;
106       ip4[0].hdr.src_addr = t4->src_addr.addr.as_u32;
107       ip4[1].hdr.src_addr = t4->src_addr.mask.as_u32;
108       ip4[0].hdr.dst_addr = t4->dst_addr.addr.as_u32;
109       ip4[1].hdr.dst_addr = t4->dst_addr.mask.as_u32;
110       item->type = RTE_FLOW_ITEM_TYPE_IPV4;
111       item->spec = ip4;
112       item->mask = ip4 + 1;
113
114       src_port = t4->src_port.port;
115       dst_port = t4->dst_port.port;
116       src_port_mask = t4->src_port.mask;
117       dst_port_mask = t4->dst_port.mask;
118       protocol = t4->protocol;
119     }
120   else if (f->type == VNET_FLOW_TYPE_IP4_VXLAN)
121     {
122       vnet_flow_ip4_vxlan_t *v4 = &f->ip4_vxlan;
123       ip4[0].hdr.src_addr = v4->src_addr.as_u32;
124       ip4[1].hdr.src_addr = -1;
125       ip4[0].hdr.dst_addr = v4->dst_addr.as_u32;
126       ip4[1].hdr.dst_addr = -1;
127       item->type = RTE_FLOW_ITEM_TYPE_IPV4;
128       item->spec = ip4;
129       item->mask = ip4 + 1;
130
131       dst_port = v4->dst_port;
132       dst_port_mask = -1;
133       src_port = 0;
134       src_port_mask = 0;
135       protocol = IP_PROTOCOL_UDP;
136     }
137   else
138     {
139       rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
140       goto done;
141     }
142
143   /* Layer 4 */
144   vec_add2 (items, item, 1);
145   if (protocol == IP_PROTOCOL_UDP)
146     {
147       udp[0].hdr.src_port = clib_host_to_net_u16 (src_port);
148       udp[1].hdr.src_port = clib_host_to_net_u16 (src_port_mask);
149       udp[0].hdr.dst_port = clib_host_to_net_u16 (dst_port);
150       udp[1].hdr.dst_port = clib_host_to_net_u16 (dst_port_mask);
151       item->type = RTE_FLOW_ITEM_TYPE_UDP;
152       item->spec = udp;
153       item->mask = udp + 1;
154     }
155   else if (protocol == IP_PROTOCOL_TCP)
156     {
157       tcp[0].hdr.src_port = clib_host_to_net_u16 (src_port);
158       tcp[1].hdr.src_port = clib_host_to_net_u16 (src_port_mask);
159       tcp[0].hdr.dst_port = clib_host_to_net_u16 (dst_port);
160       tcp[1].hdr.dst_port = clib_host_to_net_u16 (dst_port_mask);
161       item->type = RTE_FLOW_ITEM_TYPE_TCP;
162       item->spec = tcp;
163       item->mask = tcp + 1;
164     }
165   else
166     {
167       rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
168       goto done;
169     }
170
171   /* Tunnel header match */
172   if (f->type == VNET_FLOW_TYPE_IP4_VXLAN)
173     {
174       u32 vni = f->ip4_vxlan.vni;
175       vxlan_header_t spec_hdr = {
176         .flags = VXLAN_FLAGS_I,
177         .vni_reserved = clib_host_to_net_u32 (vni << 8)
178       };
179       vxlan_header_t mask_hdr = {
180         .flags = 0xff,
181         .vni_reserved = clib_host_to_net_u32 (((u32) - 1) << 8)
182       };
183
184       clib_memset (raw, 0, sizeof raw);
185       raw[0].item.relative = 1;
186       raw[0].item.length = vxlan_hdr_sz;
187
188       clib_memcpy_fast (raw[0].val + raw_sz, &spec_hdr, vxlan_hdr_sz);
189       raw[0].item.pattern = raw[0].val + raw_sz;
190       clib_memcpy_fast (raw[1].val + raw_sz, &mask_hdr, vxlan_hdr_sz);
191       raw[1].item.pattern = raw[1].val + raw_sz;
192
193       vec_add2 (items, item, 1);
194       item->type = RTE_FLOW_ITEM_TYPE_RAW;
195       item->spec = raw;
196       item->mask = raw + 1;
197     }
198
199   vec_add2 (items, item, 1);
200   item->type = RTE_FLOW_ITEM_TYPE_END;
201
202   /* Actions */
203   /* Only one 'fate' can be assigned */
204   if (f->actions & VNET_FLOW_ACTION_REDIRECT_TO_QUEUE)
205     {
206       vec_add2 (actions, action, 1);
207       queue.index = f->redirect_queue;
208       action->type = RTE_FLOW_ACTION_TYPE_QUEUE;
209       action->conf = &queue;
210       fate = true;
211     }
212   if (f->actions & VNET_FLOW_ACTION_DROP)
213     {
214       vec_add2 (actions, action, 1);
215       action->type = RTE_FLOW_ACTION_TYPE_DROP;
216       if (fate == true)
217         {
218           rv = VNET_FLOW_ERROR_INTERNAL;
219           goto done;
220         }
221       else
222         fate = true;
223     }
224   if (fate == false)
225     {
226       vec_add2 (actions, action, 1);
227       action->type = RTE_FLOW_ACTION_TYPE_PASSTHRU;
228     }
229
230   if (f->actions & VNET_FLOW_ACTION_MARK)
231     {
232       vec_add2 (actions, action, 1);
233       mark.id = fe->mark;
234       action->type = RTE_FLOW_ACTION_TYPE_MARK;
235       action->conf = &mark;
236     }
237
238   vec_add2 (actions, action, 1);
239   action->type = RTE_FLOW_ACTION_TYPE_END;
240
241   rv = rte_flow_validate (xd->device_index, &ingress, items, actions,
242                           &xd->last_flow_error);
243
244   if (rv)
245     {
246       if (rv == -EINVAL)
247         rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
248       else if (rv == -EEXIST)
249         rv = VNET_FLOW_ERROR_ALREADY_EXISTS;
250       else
251         rv = VNET_FLOW_ERROR_INTERNAL;
252       goto done;
253     }
254
255   fe->handle = rte_flow_create (xd->device_index, &ingress, items, actions,
256                                 &xd->last_flow_error);
257
258   if (!fe->handle)
259     rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
260
261 done:
262   vec_free (items);
263   vec_free (actions);
264   return rv;
265 }
266
267 int
268 dpdk_flow_ops_fn (vnet_main_t * vnm, vnet_flow_dev_op_t op, u32 dev_instance,
269                   u32 flow_index, uword * private_data)
270 {
271   dpdk_main_t *dm = &dpdk_main;
272   vnet_flow_t *flow = vnet_get_flow (flow_index);
273   dpdk_device_t *xd = vec_elt_at_index (dm->devices, dev_instance);
274   dpdk_flow_entry_t *fe;
275   dpdk_flow_lookup_entry_t *fle = 0;
276   int rv;
277
278   /* recycle old flow lookup entries only after the main loop counter
279      increases - i.e. previously DMA'ed packets were handled */
280   if (vec_len (xd->parked_lookup_indexes) > 0 &&
281       xd->parked_loop_count != dm->vlib_main->main_loop_count)
282     {
283       u32 *fl_index;
284
285       vec_foreach (fl_index, xd->parked_lookup_indexes)
286         pool_put_index (xd->flow_lookup_entries, *fl_index);
287       vec_reset_length (xd->flow_lookup_entries);
288     }
289
290   if (op == VNET_FLOW_DEV_OP_DEL_FLOW)
291     {
292       ASSERT (*private_data >= vec_len (xd->flow_entries));
293
294       fe = vec_elt_at_index (xd->flow_entries, *private_data);
295
296       if ((rv = rte_flow_destroy (xd->device_index, fe->handle,
297                                   &xd->last_flow_error)))
298         return VNET_FLOW_ERROR_INTERNAL;
299
300       if (fe->mark)
301         {
302           /* make sure no action is taken for in-flight (marked) packets */
303           fle = pool_elt_at_index (xd->flow_lookup_entries, fe->mark);
304           clib_memset (fle, -1, sizeof (*fle));
305           vec_add1 (xd->parked_lookup_indexes, fe->mark);
306           xd->parked_loop_count = dm->vlib_main->main_loop_count;
307         }
308
309       clib_memset (fe, 0, sizeof (*fe));
310       pool_put (xd->flow_entries, fe);
311
312       goto disable_rx_offload;
313     }
314
315   if (op != VNET_FLOW_DEV_OP_ADD_FLOW)
316     return VNET_FLOW_ERROR_NOT_SUPPORTED;
317
318   pool_get (xd->flow_entries, fe);
319   fe->flow_index = flow->index;
320
321   if (flow->actions == 0)
322     {
323       rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
324       goto done;
325     }
326
327   /* if we need to mark packets, assign one mark */
328   if (flow->actions & (VNET_FLOW_ACTION_MARK |
329                        VNET_FLOW_ACTION_REDIRECT_TO_NODE |
330                        VNET_FLOW_ACTION_BUFFER_ADVANCE))
331     {
332       /* reserve slot 0 */
333       if (xd->flow_lookup_entries == 0)
334         pool_get_aligned (xd->flow_lookup_entries, fle,
335                           CLIB_CACHE_LINE_BYTES);
336       pool_get_aligned (xd->flow_lookup_entries, fle, CLIB_CACHE_LINE_BYTES);
337       fe->mark = fle - xd->flow_lookup_entries;
338
339       /* install entry in the lookup table */
340       clib_memset (fle, -1, sizeof (*fle));
341       if (flow->actions & VNET_FLOW_ACTION_MARK)
342         fle->flow_id = flow->mark_flow_id;
343       if (flow->actions & VNET_FLOW_ACTION_REDIRECT_TO_NODE)
344         fle->next_index = flow->redirect_device_input_next_index;
345       if (flow->actions & VNET_FLOW_ACTION_BUFFER_ADVANCE)
346         fle->buffer_advance = flow->buffer_advance;
347     }
348   else
349     fe->mark = 0;
350
351   if ((xd->flags & DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD) == 0)
352     {
353       xd->flags |= DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD;
354       dpdk_device_setup (xd);
355     }
356
357   switch (flow->type)
358     {
359     case VNET_FLOW_TYPE_IP4_N_TUPLE:
360     case VNET_FLOW_TYPE_IP6_N_TUPLE:
361     case VNET_FLOW_TYPE_IP4_VXLAN:
362       if ((rv = dpdk_flow_add (xd, flow, fe)))
363         goto done;
364       break;
365     default:
366       rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
367       goto done;
368     }
369
370   *private_data = fe - xd->flow_entries;
371
372 done:
373   if (rv)
374     {
375       clib_memset (fe, 0, sizeof (*fe));
376       pool_put (xd->flow_entries, fe);
377       if (fle)
378         {
379           clib_memset (fle, -1, sizeof (*fle));
380           pool_put (xd->flow_lookup_entries, fle);
381         }
382     }
383 disable_rx_offload:
384   if ((xd->flags & DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD) != 0
385       && pool_elts (xd->flow_entries) == 0)
386     {
387       xd->flags &= ~DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD;
388       dpdk_device_setup (xd);
389     }
390
391   return rv;
392 }
393
394 u8 *
395 format_dpdk_flow (u8 * s, va_list * args)
396 {
397   u32 dev_instance = va_arg (*args, u32);
398   u32 flow_index = va_arg (*args, u32);
399   uword private_data = va_arg (*args, uword);
400   dpdk_main_t *dm = &dpdk_main;
401   dpdk_device_t *xd = vec_elt_at_index (dm->devices, dev_instance);
402   dpdk_flow_entry_t *fe;
403
404   if (flow_index == ~0)
405     {
406       s = format (s, "%-25s: %U\n", "supported flow actions",
407                   format_flow_actions, xd->supported_flow_actions);
408       s = format (s, "%-25s: %d\n", "last DPDK error type",
409                   xd->last_flow_error.type);
410       s = format (s, "%-25s: %s\n", "last DPDK error message",
411                   xd->last_flow_error.message ? xd->last_flow_error.message :
412                   "n/a");
413       return s;
414     }
415
416   if (private_data >= vec_len (xd->flow_entries))
417     return format (s, "unknown flow");
418
419   fe = vec_elt_at_index (xd->flow_entries, private_data);
420   s = format (s, "mark %u", fe->mark);
421   return s;
422 }
423
424 /*
425  * fd.io coding-style-patch-verification: ON
426  *
427  * Local Variables:
428  * eval: (c-set-style "gnu")
429  * End:
430  */