memif: version 2
[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 _(ROLLBACK, "no enough space in tx buffers")
34
35 typedef enum
36 {
37 #define _(f,s) MEMIF_TX_ERROR_##f,
38   foreach_memif_tx_func_error
39 #undef _
40     MEMIF_TX_N_ERROR,
41 } memif_tx_func_error_t;
42
43 static __clib_unused char *memif_tx_func_error_strings[] = {
44 #define _(n,s) s,
45   foreach_memif_tx_func_error
46 #undef _
47 };
48
49 #ifndef CLIB_MULTIARCH_VARIANT
50 u8 *
51 format_memif_device_name (u8 * s, va_list * args)
52 {
53   u32 dev_instance = va_arg (*args, u32);
54   memif_main_t *mm = &memif_main;
55   memif_if_t *mif = pool_elt_at_index (mm->interfaces, dev_instance);
56   memif_socket_file_t *msf;
57
58   msf = pool_elt_at_index (mm->socket_files, mif->socket_file_index);
59   s = format (s, "memif%lu/%lu", msf->socket_id, 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_add_copy_op (memif_per_thread_data_t * ptd, void *data, u32 len,
89                    u16 buffer_offset, u16 buffer_vec_index)
90 {
91   memif_copy_op_t *co;
92   vec_add2_aligned (ptd->copy_ops, co, 1, CLIB_CACHE_LINE_BYTES);
93   co->data = data;
94   co->data_len = len;
95   co->buffer_offset = buffer_offset;
96   co->buffer_vec_index = buffer_vec_index;
97 }
98
99 static_always_inline uword
100 memif_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
101                            vlib_frame_t * frame, memif_if_t * mif,
102                            memif_ring_type_t type)
103 {
104   u8 qid;
105   memif_ring_t *ring;
106   u32 *buffers = vlib_frame_args (frame);
107   u32 n_left = frame->n_vectors;
108   u32 n_copy_op;
109   u16 ring_size, mask, slot, free_slots;
110   u32 thread_index = vlib_get_thread_index ();
111   memif_per_thread_data_t *ptd = vec_elt_at_index (memif_main.per_thread_data,
112                                                    thread_index);
113   u8 tx_queues = vec_len (mif->tx_queues);
114   memif_queue_t *mq;
115   int n_retries = 5;
116   vlib_buffer_t *b0, *b1, *b2, *b3;
117   memif_copy_op_t *co;
118   memif_region_index_t last_region = ~0;
119   void *last_region_shm = 0;
120
121   if (tx_queues < vec_len (vlib_mains))
122     {
123       ASSERT (tx_queues > 0);
124       qid = thread_index % tx_queues;
125       clib_spinlock_lock_if_init (&mif->lockp);
126     }
127   else
128     qid = thread_index;
129
130   mq = vec_elt_at_index (mif->tx_queues, qid);
131   ring = mq->ring;
132   ring_size = 1 << mq->log2_ring_size;
133   mask = ring_size - 1;
134
135 retry:
136
137   free_slots = ring->tail - mq->last_tail;
138   mq->last_tail += free_slots;
139   slot = (type == MEMIF_RING_S2M) ? ring->head : ring->tail;
140
141   if (type == MEMIF_RING_S2M)
142     free_slots = ring_size - ring->head + mq->last_tail;
143   else
144     free_slots = ring->head - ring->tail;
145
146   while (n_left && free_slots)
147     {
148       memif_desc_t *d0;
149       void *mb0;
150       i32 src_off;
151       u32 bi0, dst_off, src_left, dst_left, bytes_to_copy;
152       u32 saved_ptd_copy_ops_len = _vec_len (ptd->copy_ops);
153       u32 saved_ptd_buffers_len = _vec_len (ptd->buffers);
154       u16 saved_slot = slot;
155
156       CLIB_PREFETCH (&ring->desc[(slot + 8) & mask], CLIB_CACHE_LINE_BYTES,
157                      LOAD);
158
159       d0 = &ring->desc[slot & mask];
160       if (PREDICT_FALSE (last_region != d0->region))
161         {
162           last_region_shm = mif->regions[d0->region].shm;
163           last_region = d0->region;
164         }
165       mb0 = last_region_shm + d0->offset;
166
167       dst_off = 0;
168
169       /* slave is the producer, so it should be able to reset buffer length */
170       dst_left = (type == MEMIF_RING_S2M) ? mif->run.buffer_size : d0->length;
171
172       if (PREDICT_TRUE (n_left >= 4))
173         vlib_prefetch_buffer_header (vlib_get_buffer (vm, buffers[3]), LOAD);
174       bi0 = buffers[0];
175
176     next_in_chain:
177
178       b0 = vlib_get_buffer (vm, bi0);
179       src_off = b0->current_data;
180       src_left = b0->current_length;
181
182       while (src_left)
183         {
184           if (PREDICT_FALSE (dst_left == 0))
185             {
186               if (free_slots)
187                 {
188                   slot++;
189                   free_slots--;
190                   d0->flags = MEMIF_DESC_FLAG_NEXT;
191                   d0 = &ring->desc[slot & mask];
192                   dst_off = 0;
193                   dst_left =
194                     (type ==
195                      MEMIF_RING_S2M) ? mif->run.buffer_size : d0->length;
196
197                   if (PREDICT_FALSE (last_region != d0->region))
198                     {
199                       last_region_shm = mif->regions[d0->region].shm;
200                       last_region = d0->region;
201                     }
202                   mb0 = last_region_shm + d0->offset;
203                 }
204               else
205                 {
206                   /* we need to rollback vectors before bailing out */
207                   _vec_len (ptd->buffers) = saved_ptd_buffers_len;
208                   _vec_len (ptd->copy_ops) = saved_ptd_copy_ops_len;
209                   vlib_error_count (vm, node->node_index,
210                                     MEMIF_TX_ERROR_ROLLBACK, 1);
211                   slot = saved_slot;
212                   goto no_free_slots;
213                 }
214             }
215           bytes_to_copy = clib_min (src_left, dst_left);
216           memif_add_copy_op (ptd, mb0 + dst_off, bytes_to_copy, src_off,
217                              vec_len (ptd->buffers));
218           vec_add1_aligned (ptd->buffers, bi0, CLIB_CACHE_LINE_BYTES);
219           src_off += bytes_to_copy;
220           dst_off += bytes_to_copy;
221           src_left -= bytes_to_copy;
222           dst_left -= bytes_to_copy;
223         }
224
225       if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_NEXT_PRESENT))
226         {
227           bi0 = b0->next_buffer;
228           goto next_in_chain;
229         }
230
231       d0->length = dst_off;
232       d0->flags = 0;
233
234       free_slots -= 1;
235       slot += 1;
236
237       buffers++;
238       n_left--;
239     }
240 no_free_slots:
241
242   /* copy data */
243   n_copy_op = vec_len (ptd->copy_ops);
244   co = ptd->copy_ops;
245   while (n_copy_op >= 8)
246     {
247       CLIB_PREFETCH (co[4].data, CLIB_CACHE_LINE_BYTES, LOAD);
248       CLIB_PREFETCH (co[5].data, CLIB_CACHE_LINE_BYTES, LOAD);
249       CLIB_PREFETCH (co[6].data, CLIB_CACHE_LINE_BYTES, LOAD);
250       CLIB_PREFETCH (co[7].data, CLIB_CACHE_LINE_BYTES, LOAD);
251
252       b0 = vlib_get_buffer (vm, ptd->buffers[co[0].buffer_vec_index]);
253       b1 = vlib_get_buffer (vm, ptd->buffers[co[1].buffer_vec_index]);
254       b2 = vlib_get_buffer (vm, ptd->buffers[co[2].buffer_vec_index]);
255       b3 = vlib_get_buffer (vm, ptd->buffers[co[3].buffer_vec_index]);
256
257       clib_memcpy (co[0].data, b0->data + co[0].buffer_offset,
258                    co[0].data_len);
259       clib_memcpy (co[1].data, b1->data + co[1].buffer_offset,
260                    co[1].data_len);
261       clib_memcpy (co[2].data, b2->data + co[2].buffer_offset,
262                    co[2].data_len);
263       clib_memcpy (co[3].data, b3->data + co[3].buffer_offset,
264                    co[3].data_len);
265
266       co += 4;
267       n_copy_op -= 4;
268     }
269   while (n_copy_op)
270     {
271       b0 = vlib_get_buffer (vm, ptd->buffers[co[0].buffer_vec_index]);
272       clib_memcpy (co[0].data, b0->data + co[0].buffer_offset,
273                    co[0].data_len);
274       co += 1;
275       n_copy_op -= 1;
276     }
277
278   vec_reset_length (ptd->copy_ops);
279   vec_reset_length (ptd->buffers);
280
281   CLIB_MEMORY_STORE_BARRIER ();
282   if (type == MEMIF_RING_S2M)
283     ring->head = slot;
284   else
285     ring->tail = slot;
286
287   if (n_left && n_retries--)
288     goto retry;
289
290   clib_spinlock_unlock_if_init (&mif->lockp);
291
292   if (n_left)
293     {
294       vlib_error_count (vm, node->node_index, MEMIF_TX_ERROR_NO_FREE_SLOTS,
295                         n_left);
296     }
297
298   if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0 && mq->int_fd > -1)
299     {
300       u64 b = 1;
301       CLIB_UNUSED (int r) = write (mq->int_fd, &b, sizeof (b));
302       mq->int_count++;
303     }
304
305   vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
306
307   return frame->n_vectors;
308 }
309
310 uword
311 CLIB_MULTIARCH_FN (memif_interface_tx) (vlib_main_t * vm,
312                                         vlib_node_runtime_t * node,
313                                         vlib_frame_t * frame)
314 {
315   memif_main_t *nm = &memif_main;
316   vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
317   memif_if_t *mif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
318
319   if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
320     return memif_interface_tx_inline (vm, node, frame, mif, MEMIF_RING_S2M);
321   else
322     return memif_interface_tx_inline (vm, node, frame, mif, MEMIF_RING_M2S);
323 }
324
325 static __clib_unused void
326 memif_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
327                                u32 node_index)
328 {
329   memif_main_t *apm = &memif_main;
330   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
331   memif_if_t *mif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
332
333   /* Shut off redirection */
334   if (node_index == ~0)
335     {
336       mif->per_interface_next_index = node_index;
337       return;
338     }
339
340   mif->per_interface_next_index =
341     vlib_node_add_next (vlib_get_main (), memif_input_node.index, node_index);
342 }
343
344 static __clib_unused void
345 memif_clear_hw_interface_counters (u32 instance)
346 {
347   /* Nothing for now */
348 }
349
350 static __clib_unused clib_error_t *
351 memif_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
352                                 vnet_hw_interface_rx_mode mode)
353 {
354   memif_main_t *mm = &memif_main;
355   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
356   memif_if_t *mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
357   memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, qid);
358
359   if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
360     mq->ring->flags |= MEMIF_RING_FLAG_MASK_INT;
361   else
362     mq->ring->flags &= ~MEMIF_RING_FLAG_MASK_INT;
363
364   return 0;
365 }
366
367 static __clib_unused clib_error_t *
368 memif_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
369 {
370   memif_main_t *mm = &memif_main;
371   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
372   memif_if_t *mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
373   static clib_error_t *error = 0;
374
375   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
376     mif->flags |= MEMIF_IF_FLAG_ADMIN_UP;
377   else
378     mif->flags &= ~MEMIF_IF_FLAG_ADMIN_UP;
379
380   return error;
381 }
382
383 static __clib_unused clib_error_t *
384 memif_subif_add_del_function (vnet_main_t * vnm,
385                               u32 hw_if_index,
386                               struct vnet_sw_interface_t *st, int is_add)
387 {
388   /* Nothing for now */
389   return 0;
390 }
391
392 #ifndef CLIB_MULTIARCH_VARIANT
393 /* *INDENT-OFF* */
394 VNET_DEVICE_CLASS (memif_device_class) = {
395   .name = "memif",
396   .tx_function = memif_interface_tx,
397   .format_device_name = format_memif_device_name,
398   .format_device = format_memif_device,
399   .format_tx_trace = format_memif_tx_trace,
400   .tx_function_n_errors = MEMIF_TX_N_ERROR,
401   .tx_function_error_strings = memif_tx_func_error_strings,
402   .rx_redirect_to_node = memif_set_interface_next_node,
403   .clear_counters = memif_clear_hw_interface_counters,
404   .admin_up_down_function = memif_interface_admin_up_down,
405   .subif_add_del_function = memif_subif_add_del_function,
406   .rx_mode_change_function = memif_interface_rx_mode_change,
407 };
408
409 #if __x86_64__
410 vlib_node_function_t __clib_weak memif_interface_tx_avx512;
411 vlib_node_function_t __clib_weak memif_interface_tx_avx2;
412 static void __clib_constructor
413 dpdk_interface_tx_multiarch_select (void)
414 {
415   if (memif_interface_tx_avx512 && clib_cpu_supports_avx512f ())
416     memif_device_class.tx_function = memif_interface_tx_avx512;
417   else if (memif_interface_tx_avx2 && clib_cpu_supports_avx2 ())
418     memif_device_class.tx_function = memif_interface_tx_avx2;
419 }
420 #endif
421 #endif
422
423 /* *INDENT-ON* */
424
425 /*
426  * fd.io coding-style-patch-verification: ON
427  *
428  * Local Variables:
429  * eval: (c-set-style "gnu")
430  * End:
431  */