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