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