memif: complete refactor of socket handling code
[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 uword
80 memif_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
81                            vlib_frame_t * frame, memif_if_t * mif,
82                            memif_ring_type_t type, u16 qid)
83 {
84   vnet_main_t *vnm = vnet_get_main ();
85   memif_ring_t *ring;
86   memif_queue_t *mq;
87   u16 head;
88   u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
89   uword n_trace = vlib_get_trace_count (vm, node);
90   memif_main_t *nm = &memif_main;
91   u32 n_rx_packets = 0;
92   u32 n_rx_bytes = 0;
93   u32 *to_next = 0;
94   u32 n_free_bufs;
95   u32 thread_index = vlib_get_thread_index ();
96   u32 bi0, bi1;
97   vlib_buffer_t *b0, *b1;
98   u16 ring_size, mask, num_slots;
99   void *mb0, *mb1;
100
101   mq = vec_elt_at_index (mif->rx_queues, qid);
102   ring = mq->ring;
103   ring_size = 1 << mq->log2_ring_size;
104   mask = ring_size - 1;
105
106   if (mif->per_interface_next_index != ~0)
107     next_index = mif->per_interface_next_index;
108
109   n_free_bufs = vec_len (nm->rx_buffers[thread_index]);
110   if (PREDICT_FALSE (n_free_bufs < ring_size))
111     {
112       vec_validate (nm->rx_buffers[thread_index],
113                     ring_size + n_free_bufs - 1);
114       n_free_bufs +=
115         vlib_buffer_alloc (vm, &nm->rx_buffers[thread_index][n_free_bufs],
116                            ring_size);
117       _vec_len (nm->rx_buffers[thread_index]) = n_free_bufs;
118     }
119
120   head = ring->head;
121   if (head == mq->last_head)
122     return 0;
123
124   if (head > mq->last_head)
125     num_slots = head - mq->last_head;
126   else
127     num_slots = ring_size - mq->last_head + head;
128
129   while (num_slots)
130     {
131       u32 n_left_to_next;
132       u32 next0 = next_index;
133       u32 next1 = next_index;
134       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
135
136       while (num_slots > 5 && n_left_to_next > 2)
137         {
138           if (PREDICT_TRUE (mq->last_head + 5 < ring_size))
139             {
140               CLIB_PREFETCH (memif_get_buffer (mif, ring, mq->last_head + 2),
141                              CLIB_CACHE_LINE_BYTES, LOAD);
142               CLIB_PREFETCH (memif_get_buffer (mif, ring, mq->last_head + 3),
143                              CLIB_CACHE_LINE_BYTES, LOAD);
144               CLIB_PREFETCH (&ring->desc[mq->last_head + 4],
145                              CLIB_CACHE_LINE_BYTES, LOAD);
146               CLIB_PREFETCH (&ring->desc[mq->last_head + 5],
147                              CLIB_CACHE_LINE_BYTES, LOAD);
148             }
149           else
150             {
151               CLIB_PREFETCH (memif_get_buffer
152                              (mif, ring, (mq->last_head + 2) % mask),
153                              CLIB_CACHE_LINE_BYTES, LOAD);
154               CLIB_PREFETCH (memif_get_buffer
155                              (mif, ring, (mq->last_head + 3) % mask),
156                              CLIB_CACHE_LINE_BYTES, LOAD);
157               CLIB_PREFETCH (&ring->desc[(mq->last_head + 4) % mask],
158                              CLIB_CACHE_LINE_BYTES, LOAD);
159               CLIB_PREFETCH (&ring->desc[(mq->last_head + 5) % mask],
160                              CLIB_CACHE_LINE_BYTES, LOAD);
161             }
162           /* get empty buffer */
163           u32 last_buf = vec_len (nm->rx_buffers[thread_index]) - 1;
164           bi0 = nm->rx_buffers[thread_index][last_buf];
165           bi1 = nm->rx_buffers[thread_index][last_buf - 1];
166           _vec_len (nm->rx_buffers[thread_index]) -= 2;
167
168           if (last_buf > 4)
169             {
170               memif_prefetch (vm, nm->rx_buffers[thread_index][last_buf - 2]);
171               memif_prefetch (vm, nm->rx_buffers[thread_index][last_buf - 3]);
172             }
173
174           /* enqueue buffer */
175           to_next[0] = bi0;
176           to_next[1] = bi1;
177           to_next += 2;
178           n_left_to_next -= 2;
179
180           /* fill buffer metadata */
181           b0 = vlib_get_buffer (vm, bi0);
182           b1 = vlib_get_buffer (vm, bi1);
183
184           vnet_buffer (b0)->sw_if_index[VLIB_RX] = mif->sw_if_index;
185           vnet_buffer (b1)->sw_if_index[VLIB_RX] = mif->sw_if_index;
186
187           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
188           vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
189
190           /* copy buffer */
191           mb0 = memif_get_buffer (mif, ring, mq->last_head);
192           clib_memcpy (vlib_buffer_get_current (b0), mb0,
193                        CLIB_CACHE_LINE_BYTES);
194           b0->current_length = ring->desc[mq->last_head].length;
195           mq->last_head = (mq->last_head + 1) & mask;
196
197           mb1 = memif_get_buffer (mif, ring, mq->last_head);
198           clib_memcpy (vlib_buffer_get_current (b1), mb1,
199                        CLIB_CACHE_LINE_BYTES);
200           b1->current_length = ring->desc[mq->last_head].length;
201           mq->last_head = (mq->last_head + 1) & mask;
202
203           if (b0->current_length > CLIB_CACHE_LINE_BYTES)
204             clib_memcpy (vlib_buffer_get_current (b0) + CLIB_CACHE_LINE_BYTES,
205                          mb0 + CLIB_CACHE_LINE_BYTES,
206                          b0->current_length - CLIB_CACHE_LINE_BYTES);
207
208           if (b1->current_length > CLIB_CACHE_LINE_BYTES)
209             clib_memcpy (vlib_buffer_get_current (b1) + CLIB_CACHE_LINE_BYTES,
210                          mb1 + CLIB_CACHE_LINE_BYTES,
211                          b1->current_length - CLIB_CACHE_LINE_BYTES);
212
213           /* trace */
214           VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
215           VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b1);
216
217           if (PREDICT_FALSE (n_trace > 0))
218             {
219               /* b0 */
220               memif_input_trace_t *tr;
221               vlib_trace_buffer (vm, node, next0, b0,
222                                  /* follow_chain */ 0);
223               vlib_set_trace_count (vm, node, --n_trace);
224               tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
225               tr->next_index = next0;
226               tr->hw_if_index = mif->hw_if_index;
227               tr->ring = qid;
228
229               if (n_trace)
230                 {
231                   /* b1 */
232                   memif_input_trace_t *tr;
233                   vlib_trace_buffer (vm, node, next1, b1,
234                                      /* follow_chain */ 0);
235                   vlib_set_trace_count (vm, node, --n_trace);
236                   tr = vlib_add_trace (vm, node, b1, sizeof (*tr));
237                   tr->next_index = next1;
238                   tr->hw_if_index = mif->hw_if_index;
239                   tr->ring = qid;
240                 }
241             }
242
243           /* redirect if feature path enabled */
244           vnet_feature_start_device_input_x2 (mif->sw_if_index,
245                                               &next0, &next1, b0, b1);
246
247           /* enqueue */
248           vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
249                                            n_left_to_next,
250                                            bi0, bi1, next0, next1);
251
252           /* next packet */
253           num_slots -= 2;
254           n_rx_packets += 2;
255           n_rx_bytes += b0->current_length;
256           n_rx_bytes += b1->current_length;
257         }
258       while (num_slots && n_left_to_next)
259         {
260           /* get empty buffer */
261           u32 last_buf = vec_len (nm->rx_buffers[thread_index]) - 1;
262           bi0 = nm->rx_buffers[thread_index][last_buf];
263           _vec_len (nm->rx_buffers[thread_index]) = last_buf;
264
265           /* enqueue buffer */
266           to_next[0] = bi0;
267           to_next += 1;
268           n_left_to_next--;
269
270           /* fill buffer metadata */
271           b0 = vlib_get_buffer (vm, bi0);
272           b0->current_length = ring->desc[mq->last_head].length;
273           vnet_buffer (b0)->sw_if_index[VLIB_RX] = mif->sw_if_index;
274           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
275
276           /* copy buffer */
277           mb0 = memif_get_buffer (mif, ring, mq->last_head);
278           clib_memcpy (vlib_buffer_get_current (b0), mb0,
279                        CLIB_CACHE_LINE_BYTES);
280           if (b0->current_length > CLIB_CACHE_LINE_BYTES)
281             clib_memcpy (vlib_buffer_get_current (b0) + CLIB_CACHE_LINE_BYTES,
282                          mb0 + CLIB_CACHE_LINE_BYTES,
283                          b0->current_length - CLIB_CACHE_LINE_BYTES);
284
285           /* trace */
286           VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
287
288           if (PREDICT_FALSE (n_trace > 0))
289             {
290               memif_input_trace_t *tr;
291               vlib_trace_buffer (vm, node, next0, b0,
292                                  /* follow_chain */ 0);
293               vlib_set_trace_count (vm, node, --n_trace);
294               tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
295               tr->next_index = next0;
296               tr->hw_if_index = mif->hw_if_index;
297               tr->ring = qid;
298             }
299
300
301           /* redirect if feature path enabled */
302           vnet_feature_start_device_input_x1 (mif->sw_if_index, &next0, b0);
303
304           /* enqueue */
305           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
306                                            n_left_to_next, bi0, next0);
307
308           /* next packet */
309           mq->last_head = (mq->last_head + 1) & mask;
310           num_slots--;
311           n_rx_packets++;
312           n_rx_bytes += b0->current_length;
313         }
314       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
315     }
316   CLIB_MEMORY_STORE_BARRIER ();
317   ring->tail = head;
318
319   vlib_increment_combined_counter (vnm->interface_main.combined_sw_if_counters
320                                    + VNET_INTERFACE_COUNTER_RX, thread_index,
321                                    mif->hw_if_index, n_rx_packets,
322                                    n_rx_bytes);
323
324   return n_rx_packets;
325 }
326
327 static uword
328 memif_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
329                 vlib_frame_t * frame)
330 {
331   u32 n_rx = 0;
332   memif_main_t *nm = &memif_main;
333   vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
334   vnet_device_and_queue_t *dq;
335
336   foreach_device_and_queue (dq, rt->devices_and_queues)
337   {
338     memif_if_t *mif;
339     mif = vec_elt_at_index (nm->interfaces, dq->dev_instance);
340     if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) &&
341         (mif->flags & MEMIF_IF_FLAG_CONNECTED))
342       {
343         if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
344           n_rx += memif_device_input_inline (vm, node, frame, mif,
345                                              MEMIF_RING_M2S, dq->queue_id);
346         else
347           n_rx += memif_device_input_inline (vm, node, frame, mif,
348                                              MEMIF_RING_S2M, dq->queue_id);
349       }
350   }
351
352   return n_rx;
353 }
354
355 /* *INDENT-OFF* */
356 VLIB_REGISTER_NODE (memif_input_node) = {
357   .function = memif_input_fn,
358   .name = "memif-input",
359   .sibling_of = "device-input",
360   .format_trace = format_memif_input_trace,
361   .type = VLIB_NODE_TYPE_INPUT,
362   .state = VLIB_NODE_STATE_INTERRUPT,
363   .n_errors = MEMIF_INPUT_N_ERROR,
364   .error_strings = memif_input_error_strings,
365 };
366
367 VLIB_NODE_FUNCTION_MULTIARCH (memif_input_node, memif_input_fn)
368 /* *INDENT-ON* */
369
370
371 /*
372  * fd.io coding-style-patch-verification: ON
373  *
374  * Local Variables:
375  * eval: (c-set-style "gnu")
376  * End:
377  */