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