7c1ac361789c11f496e5b1e6072698138437276f
[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_slots, ERROR, "no free tx slots")                 \
33   _ (ROLLBACK, rollback, ERROR, "no enough space in tx buffers")
34
35 typedef enum
36 {
37 #define _(f, n, s, d) 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 vlib_error_desc_t memif_tx_func_error_counters[] = {
44 #define _(f, n, s, d) { #n, d, VL_COUNTER_SEVERITY_##s },
45   foreach_memif_tx_func_error
46 #undef _
47 };
48
49 #ifndef CLIB_MARCH_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 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 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, memif_queue_t * mq,
103                            memif_per_thread_data_t * ptd)
104 {
105   memif_ring_t *ring;
106   u32 *buffers = vlib_frame_vector_args (frame);
107   u32 n_left = frame->n_vectors;
108   u32 n_copy_op;
109   u16 ring_size, mask, slot, free_slots;
110   int n_retries = 5;
111   vlib_buffer_t *b0, *b1, *b2, *b3;
112   memif_copy_op_t *co;
113   memif_region_index_t last_region = ~0;
114   void *last_region_shm = 0;
115   u16 head, tail;
116
117   ring = mq->ring;
118   ring_size = 1 << mq->log2_ring_size;
119   mask = ring_size - 1;
120
121 retry:
122
123   if (type == MEMIF_RING_S2M)
124     {
125       slot = head = ring->head;
126       tail = __atomic_load_n (&ring->tail, __ATOMIC_ACQUIRE);
127       mq->last_tail += tail - mq->last_tail;
128       free_slots = ring_size - head + mq->last_tail;
129     }
130   else
131     {
132       slot = tail = ring->tail;
133       head = __atomic_load_n (&ring->head, __ATOMIC_ACQUIRE);
134       mq->last_tail += tail - mq->last_tail;
135       free_slots = head - tail;
136     }
137
138   while (n_left && free_slots)
139     {
140       memif_desc_t *d0;
141       void *mb0;
142       i32 src_off;
143       u32 bi0, dst_off, src_left, dst_left, bytes_to_copy;
144       u32 saved_ptd_copy_ops_len = _vec_len (ptd->copy_ops);
145       u32 saved_ptd_buffers_len = _vec_len (ptd->buffers);
146       u16 saved_slot = slot;
147
148       CLIB_PREFETCH (&ring->desc[(slot + 8) & mask], CLIB_CACHE_LINE_BYTES,
149                      LOAD);
150
151       d0 = &ring->desc[slot & mask];
152       if (PREDICT_FALSE (last_region != d0->region))
153         {
154           last_region_shm = mif->regions[d0->region].shm;
155           last_region = d0->region;
156         }
157       mb0 = last_region_shm + d0->offset;
158
159       dst_off = 0;
160
161       /* slave is the producer, so it should be able to reset buffer length */
162       dst_left = (type == MEMIF_RING_S2M) ? mif->run.buffer_size : d0->length;
163
164       if (PREDICT_TRUE (n_left >= 4))
165         vlib_prefetch_buffer_header (vlib_get_buffer (vm, buffers[3]), LOAD);
166       bi0 = buffers[0];
167
168     next_in_chain:
169
170       b0 = vlib_get_buffer (vm, bi0);
171       src_off = b0->current_data;
172       src_left = b0->current_length;
173
174       while (src_left)
175         {
176           if (PREDICT_FALSE (dst_left == 0))
177             {
178               if (free_slots)
179                 {
180                   slot++;
181                   free_slots--;
182                   d0->flags = MEMIF_DESC_FLAG_NEXT;
183                   d0 = &ring->desc[slot & mask];
184                   dst_off = 0;
185                   dst_left =
186                     (type ==
187                      MEMIF_RING_S2M) ? mif->run.buffer_size : d0->length;
188
189                   if (PREDICT_FALSE (last_region != d0->region))
190                     {
191                       last_region_shm = mif->regions[d0->region].shm;
192                       last_region = d0->region;
193                     }
194                   mb0 = last_region_shm + d0->offset;
195                 }
196               else
197                 {
198                   /* we need to rollback vectors before bailing out */
199                   _vec_len (ptd->buffers) = saved_ptd_buffers_len;
200                   _vec_len (ptd->copy_ops) = saved_ptd_copy_ops_len;
201                   vlib_error_count (vm, node->node_index,
202                                     MEMIF_TX_ERROR_ROLLBACK, 1);
203                   slot = saved_slot;
204                   goto no_free_slots;
205                 }
206             }
207           bytes_to_copy = clib_min (src_left, dst_left);
208           memif_add_copy_op (ptd, mb0 + dst_off, bytes_to_copy, src_off,
209                              vec_len (ptd->buffers));
210           vec_add1_aligned (ptd->buffers, bi0, CLIB_CACHE_LINE_BYTES);
211           src_off += bytes_to_copy;
212           dst_off += bytes_to_copy;
213           src_left -= bytes_to_copy;
214           dst_left -= bytes_to_copy;
215         }
216
217       if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_NEXT_PRESENT))
218         {
219           bi0 = b0->next_buffer;
220           goto next_in_chain;
221         }
222
223       d0->length = dst_off;
224       d0->flags = 0;
225
226       free_slots -= 1;
227       slot += 1;
228
229       buffers++;
230       n_left--;
231     }
232 no_free_slots:
233
234   /* copy data */
235   n_copy_op = vec_len (ptd->copy_ops);
236   co = ptd->copy_ops;
237   while (n_copy_op >= 8)
238     {
239       CLIB_PREFETCH (co[4].data, CLIB_CACHE_LINE_BYTES, LOAD);
240       CLIB_PREFETCH (co[5].data, CLIB_CACHE_LINE_BYTES, LOAD);
241       CLIB_PREFETCH (co[6].data, CLIB_CACHE_LINE_BYTES, LOAD);
242       CLIB_PREFETCH (co[7].data, CLIB_CACHE_LINE_BYTES, LOAD);
243
244       b0 = vlib_get_buffer (vm, ptd->buffers[co[0].buffer_vec_index]);
245       b1 = vlib_get_buffer (vm, ptd->buffers[co[1].buffer_vec_index]);
246       b2 = vlib_get_buffer (vm, ptd->buffers[co[2].buffer_vec_index]);
247       b3 = vlib_get_buffer (vm, ptd->buffers[co[3].buffer_vec_index]);
248
249       clib_memcpy_fast (co[0].data, b0->data + co[0].buffer_offset,
250                         co[0].data_len);
251       clib_memcpy_fast (co[1].data, b1->data + co[1].buffer_offset,
252                         co[1].data_len);
253       clib_memcpy_fast (co[2].data, b2->data + co[2].buffer_offset,
254                         co[2].data_len);
255       clib_memcpy_fast (co[3].data, b3->data + co[3].buffer_offset,
256                         co[3].data_len);
257
258       co += 4;
259       n_copy_op -= 4;
260     }
261   while (n_copy_op)
262     {
263       b0 = vlib_get_buffer (vm, ptd->buffers[co[0].buffer_vec_index]);
264       clib_memcpy_fast (co[0].data, b0->data + co[0].buffer_offset,
265                         co[0].data_len);
266       co += 1;
267       n_copy_op -= 1;
268     }
269
270   vec_reset_length (ptd->copy_ops);
271   vec_reset_length (ptd->buffers);
272
273   if (type == MEMIF_RING_S2M)
274     __atomic_store_n (&ring->head, slot, __ATOMIC_RELEASE);
275   else
276     __atomic_store_n (&ring->tail, slot, __ATOMIC_RELEASE);
277
278   if (n_left && n_retries--)
279     goto retry;
280
281   clib_spinlock_unlock_if_init (&mif->lockp);
282
283   if (n_left)
284     {
285       vlib_error_count (vm, node->node_index, MEMIF_TX_ERROR_NO_FREE_SLOTS,
286                         n_left);
287     }
288
289   if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0 && mq->int_fd > -1)
290     {
291       u64 b = 1;
292       CLIB_UNUSED (int r) = write (mq->int_fd, &b, sizeof (b));
293       mq->int_count++;
294     }
295
296   vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
297
298   return frame->n_vectors;
299 }
300
301 static_always_inline uword
302 memif_interface_tx_zc_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
303                               vlib_frame_t * frame, memif_if_t * mif,
304                               memif_queue_t * mq,
305                               memif_per_thread_data_t * ptd)
306 {
307   memif_ring_t *ring = mq->ring;
308   u32 *buffers = vlib_frame_vector_args (frame);
309   u32 n_left = frame->n_vectors;
310   u16 slot, free_slots, n_free;
311   u16 ring_size = 1 << mq->log2_ring_size;
312   u16 mask = ring_size - 1;
313   int n_retries = 5;
314   vlib_buffer_t *b0;
315   u16 head, tail;
316
317 retry:
318   tail = __atomic_load_n (&ring->tail, __ATOMIC_ACQUIRE);
319   slot = head = ring->head;
320
321   n_free = tail - mq->last_tail;
322   if (n_free >= 16)
323     {
324       vlib_buffer_free_from_ring_no_next (vm, mq->buffers,
325                                           mq->last_tail & mask,
326                                           ring_size, n_free);
327       mq->last_tail += n_free;
328     }
329
330   free_slots = ring_size - head + mq->last_tail;
331
332   while (n_left && free_slots)
333     {
334       u16 s0;
335       u16 slots_in_packet = 1;
336       memif_desc_t *d0;
337       u32 bi0;
338
339       CLIB_PREFETCH (&ring->desc[(slot + 8) & mask], CLIB_CACHE_LINE_BYTES,
340                      STORE);
341
342       if (PREDICT_TRUE (n_left >= 4))
343         vlib_prefetch_buffer_header (vlib_get_buffer (vm, buffers[3]), LOAD);
344
345       bi0 = buffers[0];
346
347     next_in_chain:
348       s0 = slot & mask;
349       d0 = &ring->desc[s0];
350       mq->buffers[s0] = bi0;
351       b0 = vlib_get_buffer (vm, bi0);
352
353       d0->region = b0->buffer_pool_index + 1;
354       d0->offset = (void *) b0->data + b0->current_data -
355         mif->regions[d0->region].shm;
356       d0->length = b0->current_length;
357
358       free_slots--;
359       slot++;
360
361       if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_NEXT_PRESENT))
362         {
363           if (PREDICT_FALSE (free_slots == 0))
364             {
365               /* revert to last fully processed packet */
366               free_slots += slots_in_packet;
367               slot -= slots_in_packet;
368               goto no_free_slots;
369             }
370
371           d0->flags = MEMIF_DESC_FLAG_NEXT;
372           bi0 = b0->next_buffer;
373
374           /* next */
375           slots_in_packet++;
376           goto next_in_chain;
377         }
378
379       d0->flags = 0;
380
381       /* next from */
382       buffers++;
383       n_left--;
384     }
385 no_free_slots:
386
387   __atomic_store_n (&ring->head, slot, __ATOMIC_RELEASE);
388
389   if (n_left && n_retries--)
390     goto retry;
391
392   clib_spinlock_unlock_if_init (&mif->lockp);
393
394   if (n_left)
395     {
396       vlib_error_count (vm, node->node_index, MEMIF_TX_ERROR_NO_FREE_SLOTS,
397                         n_left);
398       vlib_buffer_free (vm, buffers, n_left);
399     }
400
401   if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0 && mq->int_fd > -1)
402     {
403       u64 b = 1;
404       CLIB_UNUSED (int r) = write (mq->int_fd, &b, sizeof (b));
405       mq->int_count++;
406     }
407
408   return frame->n_vectors;
409 }
410
411 VNET_DEVICE_CLASS_TX_FN (memif_device_class) (vlib_main_t * vm,
412                                               vlib_node_runtime_t * node,
413                                               vlib_frame_t * frame)
414 {
415   memif_main_t *nm = &memif_main;
416   vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
417   memif_if_t *mif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
418   memif_queue_t *mq;
419   u32 thread_index = vm->thread_index;
420   memif_per_thread_data_t *ptd = vec_elt_at_index (memif_main.per_thread_data,
421                                                    thread_index);
422   u8 tx_queues = vec_len (mif->tx_queues);
423
424   if (tx_queues < vlib_get_n_threads ())
425     {
426       ASSERT (tx_queues > 0);
427       mq = vec_elt_at_index (mif->tx_queues, thread_index % tx_queues);
428       clib_spinlock_lock_if_init (&mif->lockp);
429     }
430   else
431     mq = vec_elt_at_index (mif->tx_queues, thread_index);
432
433   if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
434     return memif_interface_tx_zc_inline (vm, node, frame, mif, mq, ptd);
435   else if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
436     return memif_interface_tx_inline (vm, node, frame, mif, MEMIF_RING_S2M,
437                                       mq, ptd);
438   else
439     return memif_interface_tx_inline (vm, node, frame, mif, MEMIF_RING_M2S,
440                                       mq, ptd);
441 }
442
443 static void
444 memif_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
445                                u32 node_index)
446 {
447   memif_main_t *apm = &memif_main;
448   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
449   memif_if_t *mif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
450
451   /* Shut off redirection */
452   if (node_index == ~0)
453     {
454       mif->per_interface_next_index = node_index;
455       return;
456     }
457
458   mif->per_interface_next_index =
459     vlib_node_add_next (vlib_get_main (), memif_input_node.index, node_index);
460 }
461
462 static void
463 memif_clear_hw_interface_counters (u32 instance)
464 {
465   /* Nothing for now */
466 }
467
468 static clib_error_t *
469 memif_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
470                                 vnet_hw_if_rx_mode mode)
471 {
472   memif_main_t *mm = &memif_main;
473   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
474   memif_if_t *mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
475   memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, qid);
476
477   if (mode == VNET_HW_IF_RX_MODE_POLLING)
478     mq->ring->flags |= MEMIF_RING_FLAG_MASK_INT;
479   else
480     mq->ring->flags &= ~MEMIF_RING_FLAG_MASK_INT;
481
482   return 0;
483 }
484
485 static clib_error_t *
486 memif_subif_add_del_function (vnet_main_t * vnm,
487                               u32 hw_if_index,
488                               struct vnet_sw_interface_t *st, int is_add)
489 {
490   /* Nothing for now */
491   return 0;
492 }
493
494 /* *INDENT-OFF* */
495 VNET_DEVICE_CLASS (memif_device_class) = {
496   .name = "memif",
497   .format_device_name = format_memif_device_name,
498   .format_device = format_memif_device,
499   .format_tx_trace = format_memif_tx_trace,
500   .tx_function_n_errors = MEMIF_TX_N_ERROR,
501   .tx_function_error_counters = memif_tx_func_error_counters,
502   .rx_redirect_to_node = memif_set_interface_next_node,
503   .clear_counters = memif_clear_hw_interface_counters,
504   .admin_up_down_function = memif_interface_admin_up_down,
505   .subif_add_del_function = memif_subif_add_del_function,
506   .rx_mode_change_function = memif_interface_rx_mode_change,
507 };
508
509 /* *INDENT-ON* */
510
511 /*
512  * fd.io coding-style-patch-verification: ON
513  *
514  * Local Variables:
515  * eval: (c-set-style "gnu")
516  * End:
517  */