dpdk:enable flow director perfect mode
[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 <dpdk/device/dpdk.h>
26
27 #include <dpdk/device/dpdk_priv.h>
28 #include <vppinfra/error.h>
29
30 /* constant structs */
31 static const struct rte_flow_attr ingress = {.ingress = 1 };
32 static const struct rte_flow_item_eth any_eth[2] = { };
33 static const struct rte_flow_item_vlan any_vlan[2] = { };
34
35 static int
36 dpdk_flow_add_n_touple (dpdk_device_t * xd, vnet_flow_t * f,
37                         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   u16 src_port, dst_port, src_port_mask, dst_port_mask;
47   u8 protocol;
48   int rv = 0;
49
50   if (f->actions & (~xd->supported_flow_actions))
51     return VNET_FLOW_ERROR_NOT_SUPPORTED;
52
53   /* Ethernet */
54   vec_add2 (items, item, 1);
55   item->type = RTE_FLOW_ITEM_TYPE_ETH;
56   item->spec = any_eth;
57   item->mask = any_eth + 1;
58
59   /* VLAN */
60   vec_add2 (items, item, 1);
61   item->type = RTE_FLOW_ITEM_TYPE_VLAN;
62   item->spec = any_vlan;
63   item->mask = any_vlan + 1;
64
65   /* IP */
66   vec_add2 (items, item, 1);
67   if (f->type == VNET_FLOW_TYPE_IP6_N_TUPLE)
68     {
69       vnet_flow_ip6_n_tuple_t *t6 = &f->ip6_n_tuple;
70       clib_memcpy (ip6[0].hdr.src_addr, &t6->src_addr.addr, 16);
71       clib_memcpy (ip6[1].hdr.src_addr, &t6->src_addr.mask, 16);
72       clib_memcpy (ip6[0].hdr.dst_addr, &t6->dst_addr.addr, 16);
73       clib_memcpy (ip6[1].hdr.dst_addr, &t6->dst_addr.mask, 16);
74       item->type = RTE_FLOW_ITEM_TYPE_IPV6;
75       item->spec = ip6;
76       item->mask = ip6 + 1;
77
78       src_port = t6->src_port.port;
79       dst_port = t6->dst_port.port;
80       src_port_mask = t6->src_port.mask;
81       dst_port_mask = t6->dst_port.mask;
82       protocol = t6->protocol;
83     }
84   else
85     {
86       vnet_flow_ip4_n_tuple_t *t4 = &f->ip4_n_tuple;
87       ASSERT (f->type == VNET_FLOW_TYPE_IP4_N_TUPLE);
88       ip4[0].hdr.src_addr = t4->src_addr.mask.as_u32;
89       ip4[1].hdr.src_addr = t4->src_addr.mask.as_u32;
90       ip4[0].hdr.dst_addr = t4->dst_addr.addr.as_u32;
91       ip4[1].hdr.dst_addr = t4->dst_addr.mask.as_u32;
92       item->type = RTE_FLOW_ITEM_TYPE_IPV4;
93       item->spec = ip4;
94       item->mask = ip4 + 1;
95
96       src_port = t4->src_port.port;
97       dst_port = t4->dst_port.mask;
98       src_port_mask = t4->src_port.mask;
99       dst_port_mask = t4->dst_port.mask;
100       protocol = t4->protocol;
101     }
102
103   /* Layer 4 */
104   vec_add2 (items, item, 1);
105   if (protocol == IP_PROTOCOL_UDP)
106     {
107       udp[0].hdr.src_port = clib_host_to_net_u16 (src_port);
108       udp[1].hdr.src_port = clib_host_to_net_u16 (src_port_mask);
109       udp[0].hdr.dst_port = clib_host_to_net_u16 (dst_port);
110       udp[1].hdr.dst_port = clib_host_to_net_u16 (dst_port_mask);
111       item->type = RTE_FLOW_ITEM_TYPE_UDP;
112       item->spec = udp;
113       item->mask = udp + 1;
114     }
115   else if (protocol == IP_PROTOCOL_TCP)
116     {
117       tcp[0].hdr.src_port = clib_host_to_net_u16 (src_port);
118       tcp[1].hdr.src_port = clib_host_to_net_u16 (src_port_mask);
119       tcp[0].hdr.dst_port = clib_host_to_net_u16 (dst_port);
120       tcp[1].hdr.dst_port = clib_host_to_net_u16 (dst_port_mask);
121       item->type = RTE_FLOW_ITEM_TYPE_TCP;
122       item->spec = tcp;
123       item->mask = tcp + 1;
124     }
125   else
126     {
127       rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
128       goto done;
129     }
130
131   /* The End */
132   vec_add2 (items, item, 1);
133   item->type = RTE_FLOW_ITEM_TYPE_END;
134
135   vec_add2 (actions, action, 1);
136   action->type = RTE_FLOW_ACTION_TYPE_PASSTHRU;
137
138   vec_add2 (actions, action, 1);
139   mark.id = fe->mark;
140   action->type = RTE_FLOW_ACTION_TYPE_MARK;
141   action->conf = &mark;
142
143   vec_add2 (actions, action, 1);
144   action->type = RTE_FLOW_ACTION_TYPE_END;
145
146   fe->handle = rte_flow_create (xd->device_index, &ingress, items, actions,
147                                 &xd->last_flow_error);
148
149   if (!fe->handle)
150     rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
151
152 done:
153   vec_free (items);
154   vec_free (actions);
155   return rv;
156 }
157
158 int
159 dpdk_flow_ops_fn (vnet_main_t * vnm, vnet_flow_dev_op_t op, u32 dev_instance,
160                   u32 flow_index, uword * private_data)
161 {
162   dpdk_main_t *dm = &dpdk_main;
163   vnet_flow_t *flow = vnet_get_flow (flow_index);
164   dpdk_device_t *xd = vec_elt_at_index (dm->devices, dev_instance);
165   dpdk_flow_entry_t *fe;
166   dpdk_flow_lookup_entry_t *fle = 0;
167   int rv;
168
169   if (op == VNET_FLOW_DEV_OP_DEL_FLOW)
170     {
171       ASSERT (*private_data >= vec_len (xd->flow_entries));
172
173       fe = vec_elt_at_index (xd->flow_entries, *private_data);
174
175       if ((rv = rte_flow_destroy (xd->device_index, fe->handle,
176                                   &xd->last_flow_error)))
177         return VNET_FLOW_ERROR_INTERNAL;
178
179       memset (fe, 0, sizeof (*fe));
180       pool_put (xd->flow_entries, fe);
181
182       goto disable_rx_offload;
183     }
184
185   if (op != VNET_FLOW_DEV_OP_ADD_FLOW)
186     return VNET_FLOW_ERROR_NOT_SUPPORTED;
187
188   pool_get (xd->flow_entries, fe);
189   fe->flow_index = flow->index;
190
191   if (flow->actions == 0)
192     {
193       rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
194       goto done;
195     }
196
197   /* if we need to mark packets, assign one mark */
198   if (flow->actions & (VNET_FLOW_ACTION_MARK |
199                        VNET_FLOW_ACTION_REDIRECT_TO_NODE |
200                        VNET_FLOW_ACTION_BUFFER_ADVANCE))
201     {
202       /* reserve slot 0 */
203       if (xd->flow_lookup_entries == 0)
204         pool_get_aligned (xd->flow_lookup_entries, fle,
205                           CLIB_CACHE_LINE_BYTES);
206       pool_get_aligned (xd->flow_lookup_entries, fle, CLIB_CACHE_LINE_BYTES);
207       fe->mark = fle - xd->flow_lookup_entries;
208     }
209   else
210     fe->mark = 0;
211
212   if ((xd->flags & DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD) == 0)
213     {
214       xd->flags |= DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD;
215       dpdk_device_setup (xd);
216     }
217
218   switch (flow->type)
219     {
220     case VNET_FLOW_TYPE_IP4_N_TUPLE:
221     case VNET_FLOW_TYPE_IP6_N_TUPLE:
222       if ((rv = dpdk_flow_add_n_touple (xd, flow, fe)))
223         goto done;
224       break;
225     default:
226       rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
227       goto done;
228     }
229
230   *private_data = fe - xd->flow_entries;
231
232   /* install entry in the lookup table */
233   memset (fle, -1, sizeof (*fle));
234   if (flow->actions & VNET_FLOW_ACTION_MARK)
235     fle->flow_id = flow->mark_flow_id;
236   if (flow->actions & VNET_FLOW_ACTION_REDIRECT_TO_NODE)
237     fle->next_index = flow->redirect_device_input_next_index;
238   if (flow->actions & VNET_FLOW_ACTION_BUFFER_ADVANCE)
239     fle->buffer_advance = flow->buffer_advance;
240
241 done:
242   if (rv)
243     {
244       memset (fe, 0, sizeof (*fe));
245       pool_put (xd->flow_entries, fe);
246       if (fle)
247         {
248           memset (fle, 0, sizeof (*fle));
249           pool_put (xd->flow_lookup_entries, fle);
250         }
251     }
252 disable_rx_offload:
253   if ((xd->flags & DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD) != 0
254       && pool_elts (xd->flow_entries) == 0)
255     {
256       xd->flags &= ~DPDK_DEVICE_FLAG_RX_FLOW_OFFLOAD;
257       dpdk_device_setup (xd);
258     }
259
260   return rv;
261 }
262
263 u8 *
264 format_dpdk_flow (u8 * s, va_list * args)
265 {
266   u32 dev_instance = va_arg (*args, u32);
267   u32 flow_index = va_arg (*args, u32);
268   uword private_data = va_arg (*args, uword);
269   dpdk_main_t *dm = &dpdk_main;
270   dpdk_device_t *xd = vec_elt_at_index (dm->devices, dev_instance);
271   dpdk_flow_entry_t *fe;
272
273   if (flow_index == ~0)
274     {
275       s = format (s, "%-25s: %U\n", "supported flow actions",
276                   format_flow_actions, xd->supported_flow_actions);
277       s = format (s, "%-25s: %d\n", "last DPDK error type",
278                   xd->last_flow_error.type);
279       s = format (s, "%-25s: %s\n", "last DPDK error message",
280                   xd->last_flow_error.message ? xd->last_flow_error.message :
281                   "n/a");
282       return s;
283     }
284
285   fe = vec_elt_at_index (xd->flow_entries, private_data);
286
287   if (!fe)
288     return format (s, "unknown flow");
289
290   s = format (s, "mark %u", fe->mark);
291   return s;
292 }
293
294 /*
295  * fd.io coding-style-patch-verification: ON
296  *
297  * Local Variables:
298  * eval: (c-set-style "gnu")
299  * End:
300  */