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