vppinfra: add spinlock inline functions
[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
30 #define foreach_memif_tx_func_error            \
31 _(NO_FREE_SLOTS, "no free tx slots")           \
32 _(PENDING_MSGS, "pending msgs in tx ring")
33
34 typedef enum
35 {
36 #define _(f,s) MEMIF_TX_ERROR_##f,
37   foreach_memif_tx_func_error
38 #undef _
39     MEMIF_TX_N_ERROR,
40 } memif_tx_func_error_t;
41
42 static char *memif_tx_func_error_strings[] = {
43 #define _(n,s) s,
44   foreach_memif_tx_func_error
45 #undef _
46 };
47
48
49 static 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 rid = 0;
95   memif_ring_t *ring = memif_get_ring (mif, type, rid);
96   u32 *buffers = vlib_frame_args (frame);
97   u32 n_left = frame->n_vectors;
98   u16 ring_size = 1 << mif->log2_ring_size;
99   u16 mask = ring_size - 1;
100   u16 head, tail;
101   u16 free_slots;
102
103   clib_spinlock_lock_if_init (&mif->lockp);
104
105   /* free consumed buffers */
106
107   head = ring->head;
108   tail = ring->tail;
109
110   if (tail > head)
111     free_slots = tail - head;
112   else
113     free_slots = ring_size - head + tail;
114
115   while (n_left > 5 && free_slots > 1)
116     {
117       if (PREDICT_TRUE (head + 5 < ring_size))
118         {
119           CLIB_PREFETCH (memif_get_buffer (mif, ring, head + 2),
120                          CLIB_CACHE_LINE_BYTES, STORE);
121           CLIB_PREFETCH (memif_get_buffer (mif, ring, head + 3),
122                          CLIB_CACHE_LINE_BYTES, STORE);
123           CLIB_PREFETCH (&ring->desc[head + 4], CLIB_CACHE_LINE_BYTES, STORE);
124           CLIB_PREFETCH (&ring->desc[head + 5], CLIB_CACHE_LINE_BYTES, STORE);
125         }
126       else
127         {
128           CLIB_PREFETCH (memif_get_buffer (mif, ring, (head + 2) % mask),
129                          CLIB_CACHE_LINE_BYTES, STORE);
130           CLIB_PREFETCH (memif_get_buffer (mif, ring, (head + 3) % mask),
131                          CLIB_CACHE_LINE_BYTES, STORE);
132           CLIB_PREFETCH (&ring->desc[(head + 4) % mask],
133                          CLIB_CACHE_LINE_BYTES, STORE);
134           CLIB_PREFETCH (&ring->desc[(head + 5) % mask],
135                          CLIB_CACHE_LINE_BYTES, STORE);
136         }
137
138       memif_prefetch_buffer_and_data (vm, buffers[2]);
139       memif_prefetch_buffer_and_data (vm, buffers[3]);
140
141       vlib_buffer_t *b0 = vlib_get_buffer (vm, buffers[0]);
142       vlib_buffer_t *b1 = vlib_get_buffer (vm, buffers[1]);
143
144       void *mb0 = memif_get_buffer (mif, ring, head);
145       clib_memcpy (mb0, vlib_buffer_get_current (b0), CLIB_CACHE_LINE_BYTES);
146       ring->desc[head].length = b0->current_length;
147       head = (head + 1) & mask;
148
149       void *mb1 = memif_get_buffer (mif, ring, head);
150       clib_memcpy (mb1, vlib_buffer_get_current (b1), CLIB_CACHE_LINE_BYTES);
151       ring->desc[head].length = b1->current_length;
152       head = (head + 1) & mask;
153
154       if (b0->current_length > CLIB_CACHE_LINE_BYTES)
155         {
156           clib_memcpy (mb0 + CLIB_CACHE_LINE_BYTES,
157                        vlib_buffer_get_current (b0) + CLIB_CACHE_LINE_BYTES,
158                        b0->current_length - CLIB_CACHE_LINE_BYTES);
159         }
160       if (b1->current_length > CLIB_CACHE_LINE_BYTES)
161         {
162           clib_memcpy (mb1 + CLIB_CACHE_LINE_BYTES,
163                        vlib_buffer_get_current (b1) + CLIB_CACHE_LINE_BYTES,
164                        b1->current_length - CLIB_CACHE_LINE_BYTES);
165         }
166
167
168       buffers += 2;
169       n_left -= 2;
170       free_slots -= 2;
171     }
172
173   while (n_left && free_slots)
174     {
175       vlib_buffer_t *b0 = vlib_get_buffer (vm, buffers[0]);
176       void *mb0 = memif_get_buffer (mif, ring, head);
177       clib_memcpy (mb0, vlib_buffer_get_current (b0), CLIB_CACHE_LINE_BYTES);
178
179       if (b0->current_length > CLIB_CACHE_LINE_BYTES)
180         {
181           clib_memcpy (mb0 + CLIB_CACHE_LINE_BYTES,
182                        vlib_buffer_get_current (b0) + CLIB_CACHE_LINE_BYTES,
183                        b0->current_length - CLIB_CACHE_LINE_BYTES);
184         }
185       ring->desc[head].length = b0->current_length;
186       head = (head + 1) & mask;
187
188       buffers++;
189       n_left--;
190       free_slots--;
191     }
192
193   CLIB_MEMORY_STORE_BARRIER ();
194   ring->head = head;
195
196   clib_spinlock_unlock (&mif->lockp);
197
198   if (n_left)
199     {
200       vlib_error_count (vm, node->node_index, MEMIF_TX_ERROR_NO_FREE_SLOTS,
201                         n_left);
202       vlib_buffer_free (vm, buffers, n_left);
203     }
204
205   vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
206   if (mif->interrupt_line.fd > 0)
207     {
208       u8 b = rid;
209       CLIB_UNUSED (int r) = write (mif->interrupt_line.fd, &b, sizeof (b));
210     }
211
212   return frame->n_vectors;
213 }
214
215 static uword
216 memif_interface_tx (vlib_main_t * vm,
217                     vlib_node_runtime_t * node, vlib_frame_t * frame)
218 {
219   memif_main_t *nm = &memif_main;
220   vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
221   memif_if_t *mif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
222
223   if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
224     return memif_interface_tx_inline (vm, node, frame, mif, MEMIF_RING_S2M);
225   else
226     return memif_interface_tx_inline (vm, node, frame, mif, MEMIF_RING_M2S);
227 }
228
229 static void
230 memif_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
231                                u32 node_index)
232 {
233   memif_main_t *apm = &memif_main;
234   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
235   memif_if_t *mif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
236
237   /* Shut off redirection */
238   if (node_index == ~0)
239     {
240       mif->per_interface_next_index = node_index;
241       return;
242     }
243
244   mif->per_interface_next_index =
245     vlib_node_add_next (vlib_get_main (), memif_input_node.index, node_index);
246 }
247
248 static void
249 memif_clear_hw_interface_counters (u32 instance)
250 {
251   /* Nothing for now */
252 }
253
254 static clib_error_t *
255 memif_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
256 {
257   memif_main_t *apm = &memif_main;
258   memif_msg_t msg;
259   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
260   memif_if_t *mif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
261
262   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
263     mif->flags |= MEMIF_IF_FLAG_ADMIN_UP;
264   else
265     {
266       mif->flags &= ~MEMIF_IF_FLAG_ADMIN_UP;
267       if (!(mif->flags & MEMIF_IF_FLAG_DELETING)
268           && mif->connection.index != ~0)
269         {
270           msg.version = MEMIF_VERSION;
271           msg.type = MEMIF_MSG_TYPE_DISCONNECT;
272           send (mif->connection.fd, &msg, sizeof (msg), 0);
273         }
274     }
275
276   return 0;
277 }
278
279 static clib_error_t *
280 memif_subif_add_del_function (vnet_main_t * vnm,
281                               u32 hw_if_index,
282                               struct vnet_sw_interface_t *st, int is_add)
283 {
284   /* Nothing for now */
285   return 0;
286 }
287
288 /* *INDENT-OFF* */
289 VNET_DEVICE_CLASS (memif_device_class) = {
290   .name = "memif",
291   .tx_function = memif_interface_tx,
292   .format_device_name = format_memif_device_name,
293   .format_device = format_memif_device,
294   .format_tx_trace = format_memif_tx_trace,
295   .tx_function_n_errors = MEMIF_TX_N_ERROR,
296   .tx_function_error_strings = memif_tx_func_error_strings,
297   .rx_redirect_to_node = memif_set_interface_next_node,
298   .clear_counters = memif_clear_hw_interface_counters,
299   .admin_up_down_function = memif_interface_admin_up_down,
300   .subif_add_del_function = memif_subif_add_del_function,
301 };
302
303 VLIB_DEVICE_TX_FUNCTION_MULTIARCH(memif_device_class,
304                                   memif_interface_tx)
305 /* *INDENT-ON* */
306
307 /*
308  * fd.io coding-style-patch-verification: ON
309  *
310  * Local Variables:
311  * eval: (c-set-style "gnu")
312  * End:
313  */