memif: jumbo frames support
[vpp.git] / src / plugins / memif / node.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2016 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/feature/feature.h>
29
30 #include <memif/memif.h>
31 #include <memif/private.h>
32
33 #define foreach_memif_input_error
34
35 typedef enum
36 {
37 #define _(f,s) MEMIF_INPUT_ERROR_##f,
38   foreach_memif_input_error
39 #undef _
40     MEMIF_INPUT_N_ERROR,
41 } memif_input_error_t;
42
43 static char *memif_input_error_strings[] = {
44 #define _(n,s) s,
45   foreach_memif_input_error
46 #undef _
47 };
48
49 typedef struct
50 {
51   u32 next_index;
52   u32 hw_if_index;
53   u16 ring;
54 } memif_input_trace_t;
55
56 static u8 *
57 format_memif_input_trace (u8 * s, va_list * args)
58 {
59   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
60   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
61   memif_input_trace_t *t = va_arg (*args, memif_input_trace_t *);
62   uword indent = format_get_indent (s);
63
64   s = format (s, "memif: hw_if_index %d next-index %d",
65               t->hw_if_index, t->next_index);
66   s = format (s, "\n%Uslot: ring %u", format_white_space, indent + 2,
67               t->ring);
68   return s;
69 }
70
71 static_always_inline void
72 memif_prefetch (vlib_main_t * vm, u32 bi)
73 {
74   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
75   vlib_prefetch_buffer_header (b, STORE);
76   CLIB_PREFETCH (b->data, CLIB_CACHE_LINE_BYTES, STORE);
77 }
78
79 static_always_inline void
80 memif_buffer_add_to_chain (vlib_main_t * vm, u32 bi, u32 first_bi,
81                            u32 prev_bi)
82 {
83   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
84   vlib_buffer_t *first_b = vlib_get_buffer (vm, first_bi);
85   vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_bi);
86
87   /* update first buffer */
88   first_b->total_length_not_including_first_buffer += b->current_length;
89
90   /* update previous buffer */
91   prev_b->next_buffer = bi;
92   prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
93
94   /* update current buffer */
95   b->next_buffer = 0;
96 }
97
98 /**
99  * @brief Copy buffer from rx ring
100  *
101  * @param * vm (in)
102  * @param * mif (in) pointer to memif interface
103  * @param * ring (in) pointer to memif ring
104  * @param * rd (in) pointer to ring data
105  * @param ring_size (in) ring size
106  * @param * n_free_bufs (in/out) the number of free vlib buffers available
107  * @param ** first_b (out) the first vlib buffer pointer
108  * @param * first_bi (out) the first vlib buffer index
109  * @param * bi (in/out) the current buffer index
110  * #param * num_slots (in/out) the number of descriptors available to read
111  *
112  * @return total bytes read from rx ring also written to vlib buffers
113  */
114 static_always_inline uword
115 memif_copy_buffer_from_rx_ring (vlib_main_t * vm, memif_if_t * mif,
116                                 memif_ring_t * ring, memif_queue_t * mq,
117                                 u16 ring_size, u32 n_buffer_bytes,
118                                 u32 * n_free_bufs, vlib_buffer_t ** first_b,
119                                 u32 * first_bi, u32 * bi, u16 * num_slots)
120 {
121   memif_main_t *nm = &memif_main;
122   u32 thread_index = vlib_get_thread_index ();
123   u32 total_bytes = 0, offset = 0;
124   u32 data_len;
125   u32 bytes_to_copy;
126   void *mb;
127   vlib_buffer_t *b;
128   u16 mask = ring_size - 1;
129   u32 prev_bi;
130   u16 last_head;
131
132   while (*num_slots)
133     {
134       data_len = ring->desc[mq->last_head].length;
135       while (data_len && (*n_free_bufs))
136         {
137           /* get empty buffer */
138           u32 last_buf = vec_len (nm->rx_buffers[thread_index]) - 1;
139           prev_bi = *bi;
140           *bi = nm->rx_buffers[thread_index][last_buf];
141           b = vlib_get_buffer (vm, *bi);
142           _vec_len (nm->rx_buffers[thread_index]) = last_buf;
143           (*n_free_bufs)--;
144           if (PREDICT_FALSE (*n_free_bufs == 0))
145             {
146               *n_free_bufs +=
147                 vlib_buffer_alloc (vm,
148                                    &nm->rx_buffers[thread_index]
149                                    [*n_free_bufs], ring_size);
150               _vec_len (nm->rx_buffers[thread_index]) = *n_free_bufs;
151             }
152
153           if (last_buf > 4)
154             {
155               memif_prefetch (vm, nm->rx_buffers[thread_index][last_buf - 2]);
156               memif_prefetch (vm, nm->rx_buffers[thread_index][last_buf - 3]);
157             }
158
159           /* copy buffer */
160           bytes_to_copy =
161             data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
162           b->current_data = 0;
163           mb = memif_get_buffer (mif, ring, mq->last_head);
164           clib_memcpy (vlib_buffer_get_current (b), mb + offset,
165                        CLIB_CACHE_LINE_BYTES);
166           if (bytes_to_copy > CLIB_CACHE_LINE_BYTES)
167             clib_memcpy (vlib_buffer_get_current (b) + CLIB_CACHE_LINE_BYTES,
168                          mb + CLIB_CACHE_LINE_BYTES + offset,
169                          bytes_to_copy - CLIB_CACHE_LINE_BYTES);
170
171           /* fill buffer header */
172           b->current_length = bytes_to_copy;
173
174           if (total_bytes == 0)
175             {
176               /* fill buffer metadata */
177               b->total_length_not_including_first_buffer = 0;
178               b->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
179               vnet_buffer (b)->sw_if_index[VLIB_RX] = mif->sw_if_index;
180               vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32) ~ 0;
181               *first_bi = *bi;
182               *first_b = vlib_get_buffer (vm, *first_bi);
183             }
184           else
185             memif_buffer_add_to_chain (vm, *bi, *first_bi, prev_bi);
186
187           offset += bytes_to_copy;
188           total_bytes += bytes_to_copy;
189           data_len -= bytes_to_copy;
190         }
191       last_head = mq->last_head;
192       /* Advance to next descriptor */
193       mq->last_head = (mq->last_head + 1) & mask;
194       offset = 0;
195       (*num_slots)--;
196       if ((ring->desc[last_head].flags & MEMIF_DESC_FLAG_NEXT) == 0)
197         break;
198     }
199
200   return (total_bytes);
201 }
202
203 static_always_inline uword
204 memif_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
205                            vlib_frame_t * frame, memif_if_t * mif,
206                            memif_ring_type_t type, u16 qid)
207 {
208   vnet_main_t *vnm = vnet_get_main ();
209   memif_ring_t *ring;
210   memif_queue_t *mq;
211   u16 head;
212   u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
213   uword n_trace = vlib_get_trace_count (vm, node);
214   memif_main_t *nm = &memif_main;
215   u32 n_rx_packets = 0;
216   u32 n_rx_bytes = 0;
217   u32 *to_next = 0;
218   u32 n_free_bufs;
219   u32 b0_total, b1_total;
220   u32 thread_index = vlib_get_thread_index ();
221   u16 ring_size, mask, num_slots;
222   u32 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm,
223                                                           VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
224
225   mq = vec_elt_at_index (mif->rx_queues, qid);
226   ring = mq->ring;
227   ring_size = 1 << mq->log2_ring_size;
228   mask = ring_size - 1;
229
230   if (mif->per_interface_next_index != ~0)
231     next_index = mif->per_interface_next_index;
232
233   n_free_bufs = vec_len (nm->rx_buffers[thread_index]);
234   if (PREDICT_FALSE (n_free_bufs < ring_size))
235     {
236       vec_validate (nm->rx_buffers[thread_index],
237                     ring_size + n_free_bufs - 1);
238       n_free_bufs +=
239         vlib_buffer_alloc (vm, &nm->rx_buffers[thread_index][n_free_bufs],
240                            ring_size);
241       _vec_len (nm->rx_buffers[thread_index]) = n_free_bufs;
242     }
243
244   head = ring->head;
245   if (head == mq->last_head)
246     return 0;
247
248   if (head > mq->last_head)
249     num_slots = head - mq->last_head;
250   else
251     num_slots = ring_size - mq->last_head + head;
252
253   while (num_slots)
254     {
255       u32 n_left_to_next;
256       u32 next0 = next_index;
257       u32 next1 = next_index;
258       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
259
260       while (num_slots > 11 && n_left_to_next > 2)
261         {
262           if (PREDICT_TRUE (mq->last_head + 5 < ring_size))
263             {
264               CLIB_PREFETCH (memif_get_buffer (mif, ring, mq->last_head + 2),
265                              CLIB_CACHE_LINE_BYTES, LOAD);
266               CLIB_PREFETCH (memif_get_buffer (mif, ring, mq->last_head + 3),
267                              CLIB_CACHE_LINE_BYTES, LOAD);
268               CLIB_PREFETCH (&ring->desc[mq->last_head + 4],
269                              CLIB_CACHE_LINE_BYTES, LOAD);
270               CLIB_PREFETCH (&ring->desc[mq->last_head + 5],
271                              CLIB_CACHE_LINE_BYTES, LOAD);
272             }
273           else
274             {
275               CLIB_PREFETCH (memif_get_buffer
276                              (mif, ring, (mq->last_head + 2) % mask),
277                              CLIB_CACHE_LINE_BYTES, LOAD);
278               CLIB_PREFETCH (memif_get_buffer
279                              (mif, ring, (mq->last_head + 3) % mask),
280                              CLIB_CACHE_LINE_BYTES, LOAD);
281               CLIB_PREFETCH (&ring->desc[(mq->last_head + 4) % mask],
282                              CLIB_CACHE_LINE_BYTES, LOAD);
283               CLIB_PREFETCH (&ring->desc[(mq->last_head + 5) % mask],
284                              CLIB_CACHE_LINE_BYTES, LOAD);
285             }
286
287           vlib_buffer_t *first_b0 = 0;
288           u32 bi0 = 0, first_bi0 = 0;
289           b0_total = memif_copy_buffer_from_rx_ring (vm, mif, ring, mq,
290                                                      ring_size,
291                                                      n_buffer_bytes,
292                                                      &n_free_bufs, &first_b0,
293                                                      &first_bi0, &bi0,
294                                                      &num_slots);
295
296           vlib_buffer_t *first_b1 = 0;
297           u32 bi1 = 0, first_bi1 = 0;
298           b1_total = memif_copy_buffer_from_rx_ring (vm, mif, ring, mq,
299                                                      ring_size,
300                                                      n_buffer_bytes,
301                                                      &n_free_bufs, &first_b1,
302                                                      &first_bi1, &bi1,
303                                                      &num_slots);
304
305           /* enqueue buffer */
306           to_next[0] = first_bi0;
307           to_next[1] = first_bi1;
308           to_next += 2;
309           n_left_to_next -= 2;
310
311           /* trace */
312           VLIB_BUFFER_TRACE_TRAJECTORY_INIT (first_b0);
313           VLIB_BUFFER_TRACE_TRAJECTORY_INIT (first_b1);
314
315           if (PREDICT_FALSE (n_trace > 0))
316             {
317               /* b0 */
318               if (PREDICT_TRUE (first_b0 != 0))
319                 {
320                   memif_input_trace_t *tr;
321                   vlib_trace_buffer (vm, node, next0, first_b0,
322                                      /* follow_chain */ 0);
323                   vlib_set_trace_count (vm, node, --n_trace);
324                   tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
325                   tr->next_index = next0;
326                   tr->hw_if_index = mif->hw_if_index;
327                   tr->ring = qid;
328                 }
329               if (n_trace)
330                 {
331                   /* b1 */
332                   if (PREDICT_TRUE (first_b1 != 0))
333                     {
334                       memif_input_trace_t *tr;
335                       vlib_trace_buffer (vm, node, next1, first_b1,
336                                          /* follow_chain */ 0);
337                       vlib_set_trace_count (vm, node, --n_trace);
338                       tr = vlib_add_trace (vm, node, first_b1, sizeof (*tr));
339                       tr->next_index = next1;
340                       tr->hw_if_index = mif->hw_if_index;
341                       tr->ring = qid;
342                     }
343                 }
344             }
345
346           /* redirect if feature path enabled */
347           vnet_feature_start_device_input_x2 (mif->sw_if_index,
348                                               &next0, &next1, first_b0,
349                                               first_b1);
350
351           /* enqueue */
352           vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
353                                            n_left_to_next, first_bi0,
354                                            first_bi1, next0, next1);
355
356           /* next packet */
357           n_rx_packets += 2;
358           n_rx_bytes += b0_total + b1_total;
359         }
360       while (num_slots && n_left_to_next)
361         {
362           vlib_buffer_t *first_b0 = 0;
363           u32 bi0 = 0, first_bi0 = 0;
364           b0_total = memif_copy_buffer_from_rx_ring (vm, mif, ring, mq,
365                                                      ring_size,
366                                                      n_buffer_bytes,
367                                                      &n_free_bufs, &first_b0,
368                                                      &first_bi0, &bi0,
369                                                      &num_slots);
370
371           /* trace */
372           VLIB_BUFFER_TRACE_TRAJECTORY_INIT (first_b0);
373
374           if (PREDICT_FALSE (n_trace > 0))
375             {
376               if (PREDICT_TRUE (first_b0 != 0))
377                 {
378                   memif_input_trace_t *tr;
379                   vlib_trace_buffer (vm, node, next0, first_b0,
380                                      /* follow_chain */ 0);
381                   vlib_set_trace_count (vm, node, --n_trace);
382                   tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
383                   tr->next_index = next0;
384                   tr->hw_if_index = mif->hw_if_index;
385                   tr->ring = qid;
386                 }
387             }
388
389           /* enqueue buffer */
390           to_next[0] = first_bi0;
391           to_next += 1;
392           n_left_to_next--;
393
394           /* redirect if feature path enabled */
395           vnet_feature_start_device_input_x1 (mif->sw_if_index, &next0,
396                                               first_b0);
397
398           /* enqueue */
399           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
400                                            n_left_to_next, first_bi0, next0);
401
402           /* next packet */
403           n_rx_packets++;
404           n_rx_bytes += b0_total;
405         }
406       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
407     }
408   CLIB_MEMORY_STORE_BARRIER ();
409   ring->tail = head;
410
411   vlib_increment_combined_counter (vnm->interface_main.combined_sw_if_counters
412                                    + VNET_INTERFACE_COUNTER_RX, thread_index,
413                                    mif->hw_if_index, n_rx_packets,
414                                    n_rx_bytes);
415
416   return n_rx_packets;
417 }
418
419 static uword
420 memif_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
421                 vlib_frame_t * frame)
422 {
423   u32 n_rx = 0;
424   memif_main_t *nm = &memif_main;
425   vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
426   vnet_device_and_queue_t *dq;
427
428   foreach_device_and_queue (dq, rt->devices_and_queues)
429   {
430     memif_if_t *mif;
431     mif = vec_elt_at_index (nm->interfaces, dq->dev_instance);
432     if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) &&
433         (mif->flags & MEMIF_IF_FLAG_CONNECTED))
434       {
435         if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
436           n_rx += memif_device_input_inline (vm, node, frame, mif,
437                                              MEMIF_RING_M2S, dq->queue_id);
438         else
439           n_rx += memif_device_input_inline (vm, node, frame, mif,
440                                              MEMIF_RING_S2M, dq->queue_id);
441       }
442   }
443
444   return n_rx;
445 }
446
447 /* *INDENT-OFF* */
448 VLIB_REGISTER_NODE (memif_input_node) = {
449   .function = memif_input_fn,
450   .name = "memif-input",
451   .sibling_of = "device-input",
452   .format_trace = format_memif_input_trace,
453   .type = VLIB_NODE_TYPE_INPUT,
454   .state = VLIB_NODE_STATE_INTERRUPT,
455   .n_errors = MEMIF_INPUT_N_ERROR,
456   .error_strings = memif_input_error_strings,
457 };
458
459 VLIB_NODE_FUNCTION_MULTIARCH (memif_input_node, memif_input_fn)
460 /* *INDENT-ON* */
461
462
463 /*
464  * fd.io coding-style-patch-verification: ON
465  *
466  * Local Variables:
467  * eval: (c-set-style "gnu")
468  * End:
469  */