vnet: device flow offload infra
[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       return 0;
182     }
183
184   if (op != VNET_FLOW_DEV_OP_ADD_FLOW)
185     return VNET_FLOW_ERROR_NOT_SUPPORTED;
186
187   pool_get (xd->flow_entries, fe);
188   fe->flow_index = flow->index;
189
190   if (flow->actions == 0)
191     {
192       rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
193       goto done;
194     }
195
196   /* if we need to mark packets, assign one mark */
197   if (flow->actions & (VNET_FLOW_ACTION_MARK |
198                        VNET_FLOW_ACTION_REDIRECT_TO_NODE |
199                        VNET_FLOW_ACTION_BUFFER_ADVANCE))
200     {
201       /* reserve slot 0 */
202       if (xd->flow_lookup_entries == 0)
203         pool_get_aligned (xd->flow_lookup_entries, fle,
204                           CLIB_CACHE_LINE_BYTES);
205       pool_get_aligned (xd->flow_lookup_entries, fle, CLIB_CACHE_LINE_BYTES);
206       fe->mark = fle - xd->flow_lookup_entries;
207     }
208   else
209     fe->mark = 0;
210
211   switch (flow->type)
212     {
213     case VNET_FLOW_TYPE_IP4_N_TUPLE:
214     case VNET_FLOW_TYPE_IP6_N_TUPLE:
215       if ((rv = dpdk_flow_add_n_touple (xd, flow, fe)))
216         goto done;
217       break;
218     default:
219       rv = VNET_FLOW_ERROR_NOT_SUPPORTED;
220       goto done;
221     }
222
223
224   *private_data = fe - xd->flow_entries;
225
226   /* install entry in the lookup table */
227   memset (fle, ~1, sizeof (*fle));
228   if (flow->actions & VNET_FLOW_ACTION_MARK)
229     fle->flow_id = flow->mark_flow_id;
230   if (flow->actions & VNET_FLOW_ACTION_REDIRECT_TO_NODE)
231     fle->next_index = flow->redirect_device_input_next_index;
232   if (flow->actions & VNET_FLOW_ACTION_BUFFER_ADVANCE)
233     fle->buffer_advance = flow->buffer_advance;
234
235 done:
236   if (rv)
237     {
238       memset (fe, 0, sizeof (*fe));
239       pool_put (xd->flow_entries, fe);
240       if (fle)
241         {
242           memset (fle, 0, sizeof (*fle));
243           pool_put (xd->flow_lookup_entries, fle);
244         }
245     }
246   return rv;
247 }
248
249 u8 *
250 format_dpdk_flow (u8 * s, va_list * args)
251 {
252   u32 dev_instance = va_arg (*args, u32);
253   u32 flow_index = va_arg (*args, u32);
254   uword private_data = va_arg (*args, uword);
255   dpdk_main_t *dm = &dpdk_main;
256   dpdk_device_t *xd = vec_elt_at_index (dm->devices, dev_instance);
257   dpdk_flow_entry_t *fe;
258
259   if (flow_index == ~0)
260     {
261       s = format (s, "%-25s: %U\n", "supported flow actions",
262                   format_flow_actions, xd->supported_flow_actions);
263       s = format (s, "%-25s: %d\n", "last DPDK error type",
264                   xd->last_flow_error.type);
265       s = format (s, "%-25s: %s\n", "last DPDK error message",
266                   xd->last_flow_error.message ? xd->last_flow_error.message :
267                   "n/a");
268       return s;
269     }
270
271   fe = vec_elt_at_index (xd->flow_entries, private_data);
272
273   if (!fe)
274     return format (s, "unknown flow");
275
276   s = format (s, "mark %u", fe->mark);
277   return s;
278 }
279
280 /*
281  * fd.io coding-style-patch-verification: ON
282  *
283  * Local Variables:
284  * eval: (c-set-style "gnu")
285  * End:
286  */