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