udp: fix csum computation when offload disabled
[vpp.git] / src / plugins / marvell / pp2 / input.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #define _GNU_SOURCE
19 #include <stdint.h>
20 #include <net/if.h>
21 #include <sys/ioctl.h>
22 #include <sys/uio.h>
23
24 #include <vlib/vlib.h>
25 #include <vlib/unix/unix.h>
26 #include <vnet/ethernet/ethernet.h>
27 #include <vnet/devices/devices.h>
28 #include <vnet/interface/rx_queue_funcs.h>
29
30 #include <marvell/pp2/pp2.h>
31
32 #define foreach_mrvl_pp2_input_error \
33   _(PPIO_RECV, "pp2_ppio_recv error") \
34   _(BPOOL_GET_NUM_BUFFS, "pp2_bpool_get_num_buffs error") \
35   _(BPOOL_PUT_BUFFS, "pp2_bpool_put_buffs error") \
36   _(BUFFER_ALLOC, "buffer alloc error") \
37   _(MAC_CE, "MAC error (CRC error)") \
38   _(MAC_OR, "overrun error") \
39   _(MAC_RSVD, "unknown MAC error") \
40   _(MAC_RE, "resource error") \
41   _(IP_HDR, "ip4 header error")
42
43 typedef enum
44 {
45 #define _(f,s) MRVL_PP2_INPUT_ERROR_##f,
46   foreach_mrvl_pp2_input_error
47 #undef _
48     MRVL_PP2_INPUT_N_ERROR,
49 } mrvl_pp2_input_error_t;
50
51 static __clib_unused char *mrvl_pp2_input_error_strings[] = {
52 #define _(n,s) s,
53   foreach_mrvl_pp2_input_error
54 #undef _
55 };
56
57 static_always_inline void
58 mrvl_pp2_input_trace (vlib_main_t * vm, vlib_node_runtime_t * node, u32 next0,
59                       vlib_buffer_t * b0, uword * n_trace,
60                       mrvl_pp2_if_t * ppif, struct pp2_ppio_desc *d)
61 {
62   if (PREDICT_TRUE (
63         vlib_trace_buffer (vm, node, next0, b0, /* follow_chain */ 0)))
64     {
65       mrvl_pp2_input_trace_t *tr;
66       vlib_set_trace_count (vm, node, --(*n_trace));
67       tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
68       tr->next_index = next0;
69       tr->hw_if_index = ppif->hw_if_index;
70       clib_memcpy_fast (&tr->desc, d, sizeof (struct pp2_ppio_desc));
71     }
72 }
73
74 static_always_inline u16
75 mrvl_pp2_set_buf_data_len_flags (vlib_buffer_t * b, struct pp2_ppio_desc *d,
76                                  u32 add_flags)
77 {
78   u16 len;
79   len = pp2_ppio_inq_desc_get_pkt_len (d);
80   b->total_length_not_including_first_buffer = 0;
81   b->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID | add_flags;
82
83   if (add_flags & VNET_BUFFER_F_L2_HDR_OFFSET_VALID)
84     vnet_buffer (b)->l2_hdr_offset = 2;
85
86   if (add_flags & VNET_BUFFER_F_L3_HDR_OFFSET_VALID)
87     {
88       u16 offset = DM_RXD_GET_L3_OFF (d);
89       vnet_buffer (b)->l3_hdr_offset = offset;
90       b->current_data = offset;
91       b->current_length = len - offset + 2;
92     }
93   else
94     {
95       b->current_data = 2;
96       b->current_length = len;
97     }
98
99   if (add_flags & VNET_BUFFER_F_L3_HDR_OFFSET_VALID)
100     vnet_buffer (b)->l4_hdr_offset = vnet_buffer (b)->l3_hdr_offset +
101       DM_RXD_GET_IPHDR_LEN (d) * 4;
102
103   return len;
104 }
105
106 static_always_inline u16
107 mrvl_pp2_next_from_desc (vlib_node_runtime_t * node, struct pp2_ppio_desc * d,
108                          vlib_buffer_t * b, u32 * next)
109 {
110   u8 l3_info;
111   /* ES bit set means MAC error  - drop and count */
112   if (PREDICT_FALSE (DM_RXD_GET_ES (d)))
113     {
114       *next = VNET_DEVICE_INPUT_NEXT_DROP;
115       u8 ec = DM_RXD_GET_EC (d);
116       if (ec == 0)
117         b->error = node->errors[MRVL_PP2_INPUT_ERROR_MAC_CE];
118       else if (ec == 1)
119         b->error = node->errors[MRVL_PP2_INPUT_ERROR_MAC_OR];
120       else if (ec == 2)
121         b->error = node->errors[MRVL_PP2_INPUT_ERROR_MAC_RSVD];
122       else if (ec == 3)
123         b->error = node->errors[MRVL_PP2_INPUT_ERROR_MAC_RE];
124       return mrvl_pp2_set_buf_data_len_flags (b, d, 0);
125     }
126   l3_info = DM_RXD_GET_L3_PRS_INFO (d);
127
128   /* ipv4 packet can be value 1, 2 or 3 */
129   if (PREDICT_TRUE ((l3_info - 1) < 3))
130     {
131       if (PREDICT_FALSE (DM_RXD_GET_L3_IP4_HDR_ERR (d) != 0))
132         {
133           *next = VNET_DEVICE_INPUT_NEXT_DROP;
134           b->error = node->errors[MRVL_PP2_INPUT_ERROR_IP_HDR];
135           return mrvl_pp2_set_buf_data_len_flags (b, d, 0);
136         }
137       *next = VNET_DEVICE_INPUT_NEXT_IP4_NCS_INPUT;
138       return mrvl_pp2_set_buf_data_len_flags
139         (b, d,
140          VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
141          VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
142          VNET_BUFFER_F_L4_HDR_OFFSET_VALID | VNET_BUFFER_F_IS_IP4);
143     }
144
145   /* ipv4 packet can be value 4 or 5 */
146   if (PREDICT_TRUE ((l3_info - 4) < 2))
147     {
148       *next = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
149       return mrvl_pp2_set_buf_data_len_flags
150         (b, d,
151          VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
152          VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
153          VNET_BUFFER_F_L4_HDR_OFFSET_VALID | VNET_BUFFER_F_IS_IP6);
154     }
155
156   *next = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
157   return mrvl_pp2_set_buf_data_len_flags (b, d,
158                                           VNET_BUFFER_F_L2_HDR_OFFSET_VALID);
159 }
160
161 static_always_inline uword
162 mrvl_pp2_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
163                               vlib_frame_t * frame, mrvl_pp2_if_t * ppif,
164                               u16 qid)
165 {
166   vnet_main_t *vnm = vnet_get_main ();
167   mrvl_pp2_main_t *ppm = &mrvl_pp2_main;
168   u32 thread_index = vm->thread_index;
169   mrvl_pp2_inq_t *inq = vec_elt_at_index (ppif->inqs, qid);
170   uword n_trace = vlib_get_trace_count (vm, node);
171   mrvl_pp2_per_thread_data_t *ptd =
172     vec_elt_at_index (ppm->per_thread_data, thread_index);
173   u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
174   u32 sw_if_index[VLIB_N_RX_TX];
175   u32 n_rx_packets = 0;
176   u32 n_rx_bytes = 0;
177   u32 *to_next = 0;
178   struct pp2_ppio_desc *d;
179   u16 n_desc = VLIB_FRAME_SIZE;
180   u32 n_bufs;
181   u32 *buffers;
182   int i;
183
184   vec_validate_aligned (ptd->descs, n_desc, CLIB_CACHE_LINE_BYTES);
185   if (PREDICT_FALSE (pp2_ppio_recv (ppif->ppio, 0, qid, ptd->descs, &n_desc)))
186     {
187       vlib_error_count (vm, node->node_index, MRVL_PP2_INPUT_ERROR_PPIO_RECV,
188                         1);
189       n_desc = 0;
190     }
191   n_rx_packets = n_desc;
192
193   for (i = 0; i < n_desc; i++)
194     ptd->buffers[i] = pp2_ppio_inq_desc_get_cookie (&ptd->descs[i]);
195
196   d = ptd->descs;
197   buffers = ptd->buffers;
198   sw_if_index[VLIB_RX] = ppif->sw_if_index;
199   sw_if_index[VLIB_TX] = (u32) ~ 0;
200   while (n_desc)
201     {
202       u32 n_left_to_next;
203       vlib_buffer_t *b0, *b1;
204       u32 bi0, bi1;
205       u32 next0, next1;
206       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
207       while (n_desc >= 4 && n_left_to_next >= 2)
208         {
209           /* prefetch */
210           bi0 = buffers[0];
211           bi1 = buffers[1];
212           to_next[0] = bi0;
213           to_next[1] = bi1;
214           b0 = vlib_get_buffer (vm, bi0);
215           b1 = vlib_get_buffer (vm, bi1);
216
217           if (PREDICT_TRUE (ppif->per_interface_next_index == ~0))
218             {
219               n_rx_bytes += mrvl_pp2_next_from_desc (node, d, b0, &next0);
220               n_rx_bytes += mrvl_pp2_next_from_desc (node, d + 1, b1, &next1);
221               vnet_feature_start_device_input (ppif->sw_if_index, &next0, b0);
222               vnet_feature_start_device_input (ppif->sw_if_index, &next1, b1);
223             }
224           else
225             {
226               n_rx_bytes += mrvl_pp2_set_buf_data_len_flags (b0, d, 0);
227               n_rx_bytes += mrvl_pp2_set_buf_data_len_flags (b1, d + 1, 0);
228               next0 = next1 = ppif->per_interface_next_index;
229             }
230
231           clib_memcpy_fast (vnet_buffer (b0)->sw_if_index, sw_if_index,
232                             sizeof (sw_if_index));
233           clib_memcpy_fast (vnet_buffer (b1)->sw_if_index, sw_if_index,
234                             sizeof (sw_if_index));
235
236           if (PREDICT_FALSE (n_trace > 0))
237             {
238               mrvl_pp2_input_trace (vm, node, next0, b0, &n_trace, ppif, d);
239               if (n_trace > 0)
240                 mrvl_pp2_input_trace (vm, node, next1, b1, &n_trace, ppif,
241                                       d + 1);
242             }
243
244           to_next += 2;
245           n_left_to_next -= 2;
246           d += 2;
247           buffers += 2;
248           n_desc -= 2;
249
250           /* enqueue */
251           vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
252                                            n_left_to_next, bi0, bi1, next0,
253                                            next1);
254
255         }
256       while (n_desc && n_left_to_next)
257         {
258           u32 bi0 = buffers[0];
259           to_next[0] = bi0;
260           b0 = vlib_get_buffer (vm, bi0);
261
262           if (PREDICT_TRUE (ppif->per_interface_next_index == ~0))
263             {
264               n_rx_bytes += mrvl_pp2_next_from_desc (node, d, b0, &next0);
265               vnet_feature_start_device_input (ppif->sw_if_index, &next0, b0);
266             }
267           else
268             {
269               n_rx_bytes += mrvl_pp2_set_buf_data_len_flags (b0, d, 0);
270               next0 = ppif->per_interface_next_index;
271             }
272
273           clib_memcpy_fast (vnet_buffer (b0)->sw_if_index, sw_if_index,
274                             sizeof (sw_if_index));
275
276           if (PREDICT_FALSE (n_trace > 0))
277             mrvl_pp2_input_trace (vm, node, next0, b0, &n_trace, ppif, d);
278
279           to_next += 1;
280           n_left_to_next--;
281           d++;
282           buffers++;
283           n_desc--;
284
285           /* enqueue */
286           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
287                                            n_left_to_next, bi0, next0);
288         }
289       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
290     }
291   vlib_increment_combined_counter (vnm->
292                                    interface_main.combined_sw_if_counters +
293                                    VNET_INTERFACE_COUNTER_RX, thread_index,
294                                    ppif->hw_if_index, n_rx_packets,
295                                    n_rx_bytes);
296
297   if (PREDICT_FALSE (pp2_bpool_get_num_buffs (inq->bpool, &n_bufs)))
298     {
299       vlib_error_count (vm, node->node_index,
300                         MRVL_PP2_INPUT_ERROR_BPOOL_GET_NUM_BUFFS, 1);
301       goto done;
302     }
303
304   n_bufs = inq->size - n_bufs;
305   while (n_bufs >= MRVL_PP2_BUFF_BATCH_SZ)
306     {
307       u16 n_alloc, i;
308       struct buff_release_entry *e = ptd->bre;
309       u32 *buffers = ptd->buffers;
310
311       n_alloc = vlib_buffer_alloc (vm, ptd->buffers, MRVL_PP2_BUFF_BATCH_SZ);
312       i = n_alloc;
313
314       if (PREDICT_FALSE (n_alloc == 0))
315         {
316           vlib_error_count (vm, node->node_index,
317                             MRVL_PP2_INPUT_ERROR_BUFFER_ALLOC, 1);
318           goto done;
319         }
320
321       while (i--)
322         {
323           u32 bi = buffers[0];
324           vlib_buffer_t *b = vlib_get_buffer (vm, bi);
325           e->buff.addr = vlib_buffer_get_pa (vm, b) - 64;
326           e->buff.cookie = bi;
327           e->bpool = inq->bpool;
328           e++;
329           buffers++;
330         }
331
332       i = n_alloc;
333       if (PREDICT_FALSE (pp2_bpool_put_buffs (ptd->hif, ptd->bre, &i)))
334         {
335           vlib_error_count (vm, node->node_index,
336                             MRVL_PP2_INPUT_ERROR_BPOOL_PUT_BUFFS, 1);
337           vlib_buffer_free (vm, ptd->buffers, n_alloc);
338           goto done;
339         }
340
341       if (PREDICT_FALSE (i != n_alloc))
342         vlib_buffer_free (vm, ptd->buffers + i, n_alloc - i);
343
344       n_bufs -= i;
345     }
346
347 done:
348   return n_rx_packets;
349 }
350
351 uword
352 mrvl_pp2_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
353                    vlib_frame_t * frame)
354 {
355   u32 n_rx = 0;
356   mrvl_pp2_main_t *ppm = &mrvl_pp2_main;
357   vnet_hw_if_rxq_poll_vector_t *pv;
358
359   pv = vnet_hw_if_get_rxq_poll_vector (vm, node);
360
361   for (int i = 0; i < vec_len (pv); i++)
362     {
363       mrvl_pp2_if_t *ppif;
364       ppif = vec_elt_at_index (ppm->interfaces, pv[i].dev_instance);
365       if (ppif->flags & MRVL_PP2_IF_F_ADMIN_UP)
366         n_rx +=
367           mrvl_pp2_device_input_inline (vm, node, frame, ppif, pv[i].queue_id);
368     }
369   return n_rx;
370 }
371
372 VLIB_REGISTER_NODE (mrvl_pp2_input_node) = {
373   .function = mrvl_pp2_input_fn,
374   .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
375   .name = "mrvl-pp2-input",
376   .sibling_of = "device-input",
377   .format_trace = format_mrvl_pp2_input_trace,
378   .type = VLIB_NODE_TYPE_INPUT,
379   .state = VLIB_NODE_STATE_POLLING,
380   .n_errors = MRVL_PP2_INPUT_N_ERROR,
381   .error_strings = mrvl_pp2_input_error_strings,
382 };
383
384
385
386 /*
387  * fd.io coding-style-patch-verification: ON
388  *
389  * Local Variables:
390  * eval: (c-set-style "gnu")
391  * End:
392  */