[aarch64] Fixes CLI crashes on dpaa2 platform.
[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
113   mb0 = memif_get_buffer (mif, ring, *head);
114   ring->desc[*head].flags = 0;
115   do
116     {
117       b0 = vlib_get_buffer (vm, bi);
118       len = b0->current_length;
119       if (PREDICT_FALSE (ring->desc[*head].buffer_length < (total + len)))
120         {
121           if (PREDICT_TRUE (total))
122             {
123               ring->desc[*head].length = total;
124               total = 0;
125               ring->desc[*head].flags |= MEMIF_DESC_FLAG_NEXT;
126               *head = (*head + 1) & mask;
127               mb0 = memif_get_buffer (mif, ring, *head);
128               ring->desc[*head].flags = 0;
129             }
130         }
131       if (PREDICT_TRUE (ring->desc[*head].buffer_length >= (total + len)))
132         {
133           clib_memcpy (mb0 + total, vlib_buffer_get_current (b0),
134                        CLIB_CACHE_LINE_BYTES);
135           if (len > CLIB_CACHE_LINE_BYTES)
136             clib_memcpy (mb0 + CLIB_CACHE_LINE_BYTES + total,
137                          vlib_buffer_get_current (b0) + CLIB_CACHE_LINE_BYTES,
138                          len - CLIB_CACHE_LINE_BYTES);
139           total += len;
140         }
141       else
142         {
143           vlib_error_count (vm, node->node_index, MEMIF_TX_ERROR_TRUNC_PACKET,
144                             1);
145           break;
146         }
147     }
148   while ((bi = (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0));
149
150   if (PREDICT_TRUE (total))
151     {
152       ring->desc[*head].length = total;
153       *head = (*head + 1) & mask;
154     }
155 }
156
157 static_always_inline uword
158 memif_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
159                            vlib_frame_t * frame, memif_if_t * mif,
160                            memif_ring_type_t type)
161 {
162   u8 qid;
163   memif_ring_t *ring;
164   u32 *buffers = vlib_frame_args (frame);
165   u32 n_left = frame->n_vectors;
166   u16 ring_size, mask;
167   u16 head, tail;
168   u16 free_slots;
169   u32 thread_index = vlib_get_thread_index ();
170   u8 tx_queues = vec_len (mif->tx_queues);
171   memif_queue_t *mq;
172
173   if (PREDICT_FALSE (tx_queues == 0))
174     {
175       vlib_error_count (vm, node->node_index, MEMIF_TX_ERROR_NO_TX_QUEUES,
176                         n_left);
177       goto error;
178     }
179
180   if (tx_queues < vec_len (vlib_mains))
181     {
182       qid = thread_index % tx_queues;
183       clib_spinlock_lock_if_init (&mif->lockp);
184     }
185   else
186     {
187       qid = thread_index;
188     }
189   mq = vec_elt_at_index (mif->tx_queues, qid);
190   ring = mq->ring;
191   ring_size = 1 << mq->log2_ring_size;
192   mask = ring_size - 1;
193
194   /* free consumed buffers */
195
196   head = ring->head;
197   tail = ring->tail;
198
199   if (tail > head)
200     free_slots = tail - head;
201   else
202     free_slots = ring_size - head + tail;
203
204   while (n_left > 5 && free_slots > 1)
205     {
206       if (PREDICT_TRUE (head + 5 < ring_size))
207         {
208           CLIB_PREFETCH (memif_get_buffer (mif, ring, head + 2),
209                          CLIB_CACHE_LINE_BYTES, STORE);
210           CLIB_PREFETCH (memif_get_buffer (mif, ring, head + 3),
211                          CLIB_CACHE_LINE_BYTES, STORE);
212           CLIB_PREFETCH (&ring->desc[head + 4], CLIB_CACHE_LINE_BYTES, STORE);
213           CLIB_PREFETCH (&ring->desc[head + 5], CLIB_CACHE_LINE_BYTES, STORE);
214         }
215       else
216         {
217           CLIB_PREFETCH (memif_get_buffer (mif, ring, (head + 2) % mask),
218                          CLIB_CACHE_LINE_BYTES, STORE);
219           CLIB_PREFETCH (memif_get_buffer (mif, ring, (head + 3) % mask),
220                          CLIB_CACHE_LINE_BYTES, STORE);
221           CLIB_PREFETCH (&ring->desc[(head + 4) % mask],
222                          CLIB_CACHE_LINE_BYTES, STORE);
223           CLIB_PREFETCH (&ring->desc[(head + 5) % mask],
224                          CLIB_CACHE_LINE_BYTES, STORE);
225         }
226
227       memif_prefetch_buffer_and_data (vm, buffers[2]);
228       memif_prefetch_buffer_and_data (vm, buffers[3]);
229
230       memif_copy_buffer_to_tx_ring (vm, node, mif, buffers[0], ring, &head,
231                                     mask);
232       memif_copy_buffer_to_tx_ring (vm, node, mif, buffers[1], ring, &head,
233                                     mask);
234
235       buffers += 2;
236       n_left -= 2;
237       free_slots -= 2;
238     }
239
240   while (n_left && free_slots)
241     {
242       memif_copy_buffer_to_tx_ring (vm, node, mif, buffers[0], ring, &head,
243                                     mask);
244       buffers++;
245       n_left--;
246       free_slots--;
247     }
248
249   CLIB_MEMORY_STORE_BARRIER ();
250   ring->head = head;
251
252   clib_spinlock_unlock_if_init (&mif->lockp);
253
254   if (n_left)
255     {
256       vlib_error_count (vm, node->node_index, MEMIF_TX_ERROR_NO_FREE_SLOTS,
257                         n_left);
258     }
259
260   if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0 && mq->int_fd > -1)
261     {
262       u64 b = 1;
263       CLIB_UNUSED (int r) = write (mq->int_fd, &b, sizeof (b));
264       mq->int_count++;
265     }
266
267 error:
268   vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
269
270   return frame->n_vectors;
271 }
272
273 static uword
274 memif_interface_tx (vlib_main_t * vm,
275                     vlib_node_runtime_t * node, vlib_frame_t * frame)
276 {
277   memif_main_t *nm = &memif_main;
278   vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
279   memif_if_t *mif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
280
281   if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
282     return memif_interface_tx_inline (vm, node, frame, mif, MEMIF_RING_S2M);
283   else
284     return memif_interface_tx_inline (vm, node, frame, mif, MEMIF_RING_M2S);
285 }
286
287 static void
288 memif_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
289                                u32 node_index)
290 {
291   memif_main_t *apm = &memif_main;
292   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
293   memif_if_t *mif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
294
295   /* Shut off redirection */
296   if (node_index == ~0)
297     {
298       mif->per_interface_next_index = node_index;
299       return;
300     }
301
302   mif->per_interface_next_index =
303     vlib_node_add_next (vlib_get_main (), memif_input_node.index, node_index);
304 }
305
306 static void
307 memif_clear_hw_interface_counters (u32 instance)
308 {
309   /* Nothing for now */
310 }
311
312 static clib_error_t *
313 memif_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
314                                 vnet_hw_interface_rx_mode mode)
315 {
316   memif_main_t *mm = &memif_main;
317   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
318   memif_if_t *mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
319   memif_queue_t *mq = vec_elt_at_index (mif->rx_queues, qid);
320
321   if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
322     mq->ring->flags |= MEMIF_RING_FLAG_MASK_INT;
323   else
324     mq->ring->flags &= ~MEMIF_RING_FLAG_MASK_INT;
325
326   return 0;
327 }
328
329 static clib_error_t *
330 memif_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
331 {
332   memif_main_t *mm = &memif_main;
333   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
334   memif_if_t *mif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
335   static clib_error_t *error = 0;
336
337   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
338     mif->flags |= MEMIF_IF_FLAG_ADMIN_UP;
339   else
340     mif->flags &= ~MEMIF_IF_FLAG_ADMIN_UP;
341
342   return error;
343 }
344
345 static clib_error_t *
346 memif_subif_add_del_function (vnet_main_t * vnm,
347                               u32 hw_if_index,
348                               struct vnet_sw_interface_t *st, int is_add)
349 {
350   /* Nothing for now */
351   return 0;
352 }
353
354 /* *INDENT-OFF* */
355 VNET_DEVICE_CLASS (memif_device_class) = {
356   .name = "memif",
357   .tx_function = memif_interface_tx,
358   .format_device_name = format_memif_device_name,
359   .format_device = format_memif_device,
360   .format_tx_trace = format_memif_tx_trace,
361   .tx_function_n_errors = MEMIF_TX_N_ERROR,
362   .tx_function_error_strings = memif_tx_func_error_strings,
363   .rx_redirect_to_node = memif_set_interface_next_node,
364   .clear_counters = memif_clear_hw_interface_counters,
365   .admin_up_down_function = memif_interface_admin_up_down,
366   .subif_add_del_function = memif_subif_add_del_function,
367   .rx_mode_change_function = memif_interface_rx_mode_change,
368 };
369
370 VLIB_DEVICE_TX_FUNCTION_MULTIARCH(memif_device_class,
371                                   memif_interface_tx)
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  */