3809061e4346e22d75cbac0954352e01baaefec0
[vpp.git] / src / plugins / memif / device.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
28 #include <memif/memif.h>
29 #include <memif/private.h>
30
31 #define foreach_memif_tx_func_error            \
32 _(NO_FREE_SLOTS, "no free tx slots")           \
33 _(TRUNC_PACKET, "packet > buffer size -- truncated in tx ring") \
34 _(PENDING_MSGS, "pending msgs in tx ring")
35
36 typedef enum
37 {
38 #define _(f,s) MEMIF_TX_ERROR_##f,
39   foreach_memif_tx_func_error
40 #undef _
41     MEMIF_TX_N_ERROR,
42 } memif_tx_func_error_t;
43
44 static __clib_unused char *memif_tx_func_error_strings[] = {
45 #define _(n,s) s,
46   foreach_memif_tx_func_error
47 #undef _
48 };
49
50 #ifndef CLIB_MULTIARCH_VARIANT
51 u8 *
52 format_memif_device_name (u8 * s, va_list * args)
53 {
54   u32 dev_instance = va_arg (*args, u32);
55   memif_main_t *mm = &memif_main;
56   memif_if_t *mif = pool_elt_at_index (mm->interfaces, dev_instance);
57
58   s = format (s, "memif%lu/%lu", mif->socket_file_index, mif->id);
59   return s;
60 }
61 #endif
62
63 static __clib_unused u8 *
64 format_memif_device (u8 * s, va_list * args)
65 {
66   u32 dev_instance = va_arg (*args, u32);
67   int verbose = va_arg (*args, int);
68   u32 indent = format_get_indent (s);
69
70   s = format (s, "MEMIF interface");
71   if (verbose)
72     {
73       s = format (s, "\n%U instance %u", format_white_space, indent + 2,
74                   dev_instance);
75     }
76   return s;
77 }
78
79 static __clib_unused u8 *
80 format_memif_tx_trace (u8 * s, va_list * args)
81 {
82   s = format (s, "Unimplemented...");
83   return s;
84 }
85
86 static_always_inline void
87 memif_prefetch_buffer_and_data (vlib_main_t * vm, u32 bi)
88 {
89   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
90   vlib_prefetch_buffer_header (b, LOAD);
91   CLIB_PREFETCH (b->data, CLIB_CACHE_LINE_BYTES, LOAD);
92 }
93
94 /**
95  * @brief Copy buffer to tx ring
96  *
97  * @param * vm (in)
98  * @param * node (in)
99  * @param * mif (in) pointer to memif interface
100  * @param bi (in) vlib buffer index
101  * @param * ring (in) pointer to memif ring
102  * @param * head (in/out) ring head
103  * @param mask (in) ring size - 1
104  */
105 static_always_inline void
106 memif_copy_buffer_to_tx_ring (vlib_main_t * vm, vlib_node_runtime_t * node,
107                               memif_if_t * mif, u32 bi, memif_ring_t * ring,
108                               u16 * head, u16 mask)
109 {
110   vlib_buffer_t *b0;
111   void *mb0;
112   u32 total = 0, len;
113   u16 slot = (*head) & mask;
114
115   mb0 = memif_get_buffer (mif, ring, slot);
116   ring->desc[slot].flags = 0;
117   do
118     {
119       b0 = vlib_get_buffer (vm, bi);
120       len = b0->current_length;
121       if (PREDICT_FALSE (ring->desc[slot].buffer_length < (total + len)))
122         {
123           if (PREDICT_TRUE (total))
124             {
125               ring->desc[slot].length = total;
126               total = 0;
127               ring->desc[slot].flags |= MEMIF_DESC_FLAG_NEXT;
128               (*head)++;
129               slot = (*head) & mask;
130               mb0 = memif_get_buffer (mif, ring, slot);
131               ring->desc[slot].flags = 0;
132             }
133         }
134       if (PREDICT_TRUE (ring->desc[slot].buffer_length >= (total + len)))
135         {
136           clib_memcpy (mb0 + total, vlib_buffer_get_current (b0),
137                        CLIB_CACHE_LINE_BYTES);
138           if (len > CLIB_CACHE_LINE_BYTES)
139             clib_memcpy (mb0 + CLIB_CACHE_LINE_BYTES + total,
140                          vlib_buffer_get_current (b0) + CLIB_CACHE_LINE_BYTES,
141                          len - CLIB_CACHE_LINE_BYTES);
142           total += len;
143         }
144       else
145         {
146           vlib_error_count (vm, node->node_index, MEMIF_TX_ERROR_TRUNC_PACKET,
147                             1);
148           break;
149         }
150     }
151   while ((bi = (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0));
152
153   if (PREDICT_TRUE (total))
154     {
155       ring->desc[slot].length = total;
156       (*head)++;
157     }
158 }
159
160 static_always_inline uword
161 memif_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
162                            vlib_frame_t * frame, memif_if_t * mif,
163                            memif_ring_type_t type)
164 {
165   u8 qid;
166   memif_ring_t *ring;
167   u32 *buffers = vlib_frame_args (frame);
168   u32 n_left = frame->n_vectors;
169   u16 ring_size, mask;
170   u16 head, tail;
171   u16 free_slots;
172   u32 thread_index = vlib_get_thread_index ();
173   u8 tx_queues = vec_len (mif->tx_queues);
174   memif_queue_t *mq;
175   int n_retries = 5;
176
177   if (tx_queues < vec_len (vlib_mains))
178     {
179       ASSERT (tx_queues > 0);
180       qid = thread_index % tx_queues;
181       clib_spinlock_lock_if_init (&mif->lockp);
182     }
183   else
184     qid = thread_index;
185
186   mq = vec_elt_at_index (mif->tx_queues, qid);
187   ring = mq->ring;
188   ring_size = 1 << mq->log2_ring_size;
189   mask = ring_size - 1;
190 retry:
191
192   /* free consumed buffers */
193
194   head = ring->head;
195   tail = ring->tail;
196
197   free_slots = ring_size - head + tail;
198
199   while (n_left > 5 && free_slots > 1)
200     {
201       CLIB_PREFETCH (memif_get_buffer (mif, ring, (head + 2) & mask),
202                      CLIB_CACHE_LINE_BYTES, STORE);
203       CLIB_PREFETCH (memif_get_buffer (mif, ring, (head + 3) & mask),
204                      CLIB_CACHE_LINE_BYTES, STORE);
205       CLIB_PREFETCH (&ring->desc[(head + 4) & mask], CLIB_CACHE_LINE_BYTES,
206                      STORE);
207       CLIB_PREFETCH (&ring->desc[(head + 5) & mask], CLIB_CACHE_LINE_BYTES,
208                      STORE);
209       memif_prefetch_buffer_and_data (vm, buffers[2]);
210       memif_prefetch_buffer_and_data (vm, buffers[3]);
211
212       memif_copy_buffer_to_tx_ring (vm, node, mif, buffers[0], ring, &head,
213                                     mask);
214       memif_copy_buffer_to_tx_ring (vm, node, mif, buffers[1], ring, &head,
215                                     mask);
216
217       buffers += 2;
218       n_left -= 2;
219       free_slots -= 2;
220     }
221
222   while (n_left && free_slots)
223     {
224       memif_copy_buffer_to_tx_ring (vm, node, mif, buffers[0], ring, &head,
225                                     mask);
226       buffers++;
227       n_left--;
228       free_slots--;
229     }
230
231   CLIB_MEMORY_STORE_BARRIER ();
232   ring->head = head;
233
234   if (n_left && n_retries--)
235     goto retry;
236
237   clib_spinlock_unlock_if_init (&mif->lockp);
238
239   if (n_left)
240     {
241       vlib_error_count (vm, node->node_index, MEMIF_TX_ERROR_NO_FREE_SLOTS,
242                         n_left);
243     }
244
245   if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0 && mq->int_fd > -1)
246     {
247       u64 b = 1;
248       CLIB_UNUSED (int r) = write (mq->int_fd, &b, sizeof (b));
249       mq->int_count++;
250     }
251
252   vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
253
254   return frame->n_vectors;
255 }
256
257 uword
258 CLIB_MULTIARCH_FN (memif_interface_tx) (vlib_main_t * vm,
259                                         vlib_node_runtime_t * node,
260                                         vlib_frame_t * frame)
261 {
262   memif_main_t *nm = &memif_main;
263   vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
264   memif_if_t *mif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
265
266   if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
267     return memif_interface_tx_inline (vm, node, frame, mif, MEMIF_RING_S2M);
268   else
269     return memif_interface_tx_inline (vm, node, frame, mif, MEMIF_RING_M2S);
270 }
271
272 static __clib_unused void
273 memif_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
274                                u32 node_index)
275 {
276   memif_main_t *apm = &memif_main;
277   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
278   memif_if_t *mif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
279
280   /* Shut off redirection */
281   if (node_index == ~0)
282     {
283       mif->per_interface_next_index = node_index;
284       return;
285     }
286
287   mif->per_interface_next_index =
288     vlib_node_add_next (vlib_get_main (), memif_input_node.index, node_index);
289 }
290
291 static __clib_unused void
292 memif_clear_hw_interface_counters (u32 instance)
293 {
294   /* Nothing for now */
295 }
296
297 static __clib_unused clib_error_t *
298 memif_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
299                                 vnet_hw_interface_rx_mode mode)
300 {
301   memif_main_t *mm = &memif_main;
302   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
303   memif_if_t *mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
304   memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, qid);
305
306   if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
307     mq->ring->flags |= MEMIF_RING_FLAG_MASK_INT;
308   else
309     mq->ring->flags &= ~MEMIF_RING_FLAG_MASK_INT;
310
311   return 0;
312 }
313
314 static __clib_unused clib_error_t *
315 memif_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
316 {
317   memif_main_t *mm = &memif_main;
318   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
319   memif_if_t *mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
320   static clib_error_t *error = 0;
321
322   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
323     mif->flags |= MEMIF_IF_FLAG_ADMIN_UP;
324   else
325     mif->flags &= ~MEMIF_IF_FLAG_ADMIN_UP;
326
327   return error;
328 }
329
330 static __clib_unused clib_error_t *
331 memif_subif_add_del_function (vnet_main_t * vnm,
332                               u32 hw_if_index,
333                               struct vnet_sw_interface_t *st, int is_add)
334 {
335   /* Nothing for now */
336   return 0;
337 }
338
339 #ifndef CLIB_MULTIARCH_VARIANT
340 /* *INDENT-OFF* */
341 VNET_DEVICE_CLASS (memif_device_class) = {
342   .name = "memif",
343   .tx_function = memif_interface_tx,
344   .format_device_name = format_memif_device_name,
345   .format_device = format_memif_device,
346   .format_tx_trace = format_memif_tx_trace,
347   .tx_function_n_errors = MEMIF_TX_N_ERROR,
348   .tx_function_error_strings = memif_tx_func_error_strings,
349   .rx_redirect_to_node = memif_set_interface_next_node,
350   .clear_counters = memif_clear_hw_interface_counters,
351   .admin_up_down_function = memif_interface_admin_up_down,
352   .subif_add_del_function = memif_subif_add_del_function,
353   .rx_mode_change_function = memif_interface_rx_mode_change,
354 };
355
356 #if __x86_64__
357 vlib_node_function_t __clib_weak memif_interface_tx_avx512;
358 vlib_node_function_t __clib_weak memif_interface_tx_avx2;
359 static void __clib_constructor
360 dpdk_interface_tx_multiarch_select (void)
361 {
362   if (memif_interface_tx_avx512 && clib_cpu_supports_avx512f ())
363     memif_device_class.tx_function = memif_interface_tx_avx512;
364   else if (memif_interface_tx_avx2 && clib_cpu_supports_avx2 ())
365     memif_device_class.tx_function = memif_interface_tx_avx2;
366 }
367 #endif
368 #endif
369
370 /* *INDENT-ON* */
371
372 /*
373  * fd.io coding-style-patch-verification: ON
374  *
375  * Local Variables:
376  * eval: (c-set-style "gnu")
377  * End:
378  */