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