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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
21 #include <sys/ioctl.h>
24 #include <vlib/vlib.h>
25 #include <vlib/unix/unix.h>
26 #include <vnet/ethernet/ethernet.h>
28 #include <memif/memif.h>
30 #define foreach_memif_tx_func_error \
31 _(NO_FREE_SLOTS, "no free tx slots") \
32 _(PENDING_MSGS, "pending msgs in tx ring")
36 #define _(f,s) MEMIF_TX_ERROR_##f,
37 foreach_memif_tx_func_error
40 } memif_tx_func_error_t;
42 static char *memif_tx_func_error_strings[] = {
44 foreach_memif_tx_func_error
50 format_memif_device_name (u8 * s, va_list * args)
52 u32 i = va_arg (*args, u32);
54 s = format (s, "memif%u", i);
59 format_memif_device (u8 * s, va_list * args)
61 u32 dev_instance = va_arg (*args, u32);
62 int verbose = va_arg (*args, int);
63 uword indent = format_get_indent (s);
65 s = format (s, "MEMIF interface");
68 s = format (s, "\n%U instance %u", format_white_space, indent + 2,
75 format_memif_tx_trace (u8 * s, va_list * args)
77 s = format (s, "Unimplemented...");
81 static_always_inline void
82 memif_interface_lock (memif_if_t * mif)
84 if (PREDICT_FALSE (mif->lockp != 0))
86 while (__sync_lock_test_and_set (mif->lockp, 1))
91 static_always_inline void
92 memif_interface_unlock (memif_if_t * mif)
94 if (PREDICT_FALSE (mif->lockp != 0))
98 static_always_inline void
99 memif_prefetch_buffer_and_data (vlib_main_t * vm, u32 bi)
101 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
102 vlib_prefetch_buffer_header (b, LOAD);
103 CLIB_PREFETCH (b->data, CLIB_CACHE_LINE_BYTES, LOAD);
106 static_always_inline uword
107 memif_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
108 vlib_frame_t * frame, memif_if_t * mif,
109 memif_ring_type_t type)
112 memif_ring_t *ring = memif_get_ring (mif, type, rid);
113 u32 *buffers = vlib_frame_args (frame);
114 u32 n_left = frame->n_vectors;
115 u16 ring_size = 1 << mif->log2_ring_size;
116 u16 mask = ring_size - 1;
120 memif_interface_lock (mif);
122 /* free consumed buffers */
128 free_slots = tail - head;
130 free_slots = ring_size - head + tail;
132 while (n_left > 5 && free_slots > 1)
134 if (PREDICT_TRUE (head + 5 < ring_size))
136 CLIB_PREFETCH (memif_get_buffer (mif, ring, head + 2),
137 CLIB_CACHE_LINE_BYTES, STORE);
138 CLIB_PREFETCH (memif_get_buffer (mif, ring, head + 3),
139 CLIB_CACHE_LINE_BYTES, STORE);
140 CLIB_PREFETCH (&ring->desc[head + 4], CLIB_CACHE_LINE_BYTES, STORE);
141 CLIB_PREFETCH (&ring->desc[head + 5], CLIB_CACHE_LINE_BYTES, STORE);
145 CLIB_PREFETCH (memif_get_buffer (mif, ring, (head + 2) % mask),
146 CLIB_CACHE_LINE_BYTES, STORE);
147 CLIB_PREFETCH (memif_get_buffer (mif, ring, (head + 3) % mask),
148 CLIB_CACHE_LINE_BYTES, STORE);
149 CLIB_PREFETCH (&ring->desc[(head + 4) % mask],
150 CLIB_CACHE_LINE_BYTES, STORE);
151 CLIB_PREFETCH (&ring->desc[(head + 5) % mask],
152 CLIB_CACHE_LINE_BYTES, STORE);
155 memif_prefetch_buffer_and_data (vm, buffers[2]);
156 memif_prefetch_buffer_and_data (vm, buffers[3]);
158 vlib_buffer_t *b0 = vlib_get_buffer (vm, buffers[0]);
159 vlib_buffer_t *b1 = vlib_get_buffer (vm, buffers[1]);
161 void *mb0 = memif_get_buffer (mif, ring, head);
162 clib_memcpy (mb0, vlib_buffer_get_current (b0), CLIB_CACHE_LINE_BYTES);
163 ring->desc[head].length = b0->current_length;
164 head = (head + 1) & mask;
166 void *mb1 = memif_get_buffer (mif, ring, head);
167 clib_memcpy (mb1, vlib_buffer_get_current (b1), CLIB_CACHE_LINE_BYTES);
168 ring->desc[head].length = b1->current_length;
169 head = (head + 1) & mask;
171 if (b0->current_length > CLIB_CACHE_LINE_BYTES)
173 clib_memcpy (mb0 + CLIB_CACHE_LINE_BYTES,
174 vlib_buffer_get_current (b0) + CLIB_CACHE_LINE_BYTES,
175 b0->current_length - CLIB_CACHE_LINE_BYTES);
177 if (b1->current_length > CLIB_CACHE_LINE_BYTES)
179 clib_memcpy (mb1 + CLIB_CACHE_LINE_BYTES,
180 vlib_buffer_get_current (b1) + CLIB_CACHE_LINE_BYTES,
181 b1->current_length - CLIB_CACHE_LINE_BYTES);
190 while (n_left && free_slots)
192 vlib_buffer_t *b0 = vlib_get_buffer (vm, buffers[0]);
193 void *mb0 = memif_get_buffer (mif, ring, head);
194 clib_memcpy (mb0, vlib_buffer_get_current (b0), CLIB_CACHE_LINE_BYTES);
196 if (b0->current_length > CLIB_CACHE_LINE_BYTES)
198 clib_memcpy (mb0 + CLIB_CACHE_LINE_BYTES,
199 vlib_buffer_get_current (b0) + CLIB_CACHE_LINE_BYTES,
200 b0->current_length - CLIB_CACHE_LINE_BYTES);
202 ring->desc[head].length = b0->current_length;
203 head = (head + 1) & mask;
210 CLIB_MEMORY_STORE_BARRIER ();
213 memif_interface_unlock (mif);
217 vlib_error_count (vm, node->node_index, MEMIF_TX_ERROR_NO_FREE_SLOTS,
219 vlib_buffer_free (vm, buffers, n_left);
222 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
223 if (mif->interrupt_line.fd > 0)
226 CLIB_UNUSED (int r) = write (mif->interrupt_line.fd, &b, sizeof (b));
229 return frame->n_vectors;
233 memif_interface_tx (vlib_main_t * vm,
234 vlib_node_runtime_t * node, vlib_frame_t * frame)
236 memif_main_t *nm = &memif_main;
237 vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
238 memif_if_t *mif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
240 if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
241 return memif_interface_tx_inline (vm, node, frame, mif, MEMIF_RING_S2M);
243 return memif_interface_tx_inline (vm, node, frame, mif, MEMIF_RING_M2S);
247 memif_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
250 memif_main_t *apm = &memif_main;
251 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
252 memif_if_t *mif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
254 /* Shut off redirection */
255 if (node_index == ~0)
257 mif->per_interface_next_index = node_index;
261 mif->per_interface_next_index =
262 vlib_node_add_next (vlib_get_main (), memif_input_node.index, node_index);
266 memif_clear_hw_interface_counters (u32 instance)
268 /* Nothing for now */
271 static clib_error_t *
272 memif_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
274 memif_main_t *apm = &memif_main;
276 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
277 memif_if_t *mif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
279 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
280 mif->flags |= MEMIF_IF_FLAG_ADMIN_UP;
283 mif->flags &= ~MEMIF_IF_FLAG_ADMIN_UP;
284 if (!(mif->flags & MEMIF_IF_FLAG_DELETING)
285 && mif->connection.index != ~0)
287 msg.version = MEMIF_VERSION;
288 msg.type = MEMIF_MSG_TYPE_DISCONNECT;
289 send (mif->connection.fd, &msg, sizeof (msg), 0);
296 static clib_error_t *
297 memif_subif_add_del_function (vnet_main_t * vnm,
299 struct vnet_sw_interface_t *st, int is_add)
301 /* Nothing for now */
306 VNET_DEVICE_CLASS (memif_device_class) = {
308 .tx_function = memif_interface_tx,
309 .format_device_name = format_memif_device_name,
310 .format_device = format_memif_device,
311 .format_tx_trace = format_memif_tx_trace,
312 .tx_function_n_errors = MEMIF_TX_N_ERROR,
313 .tx_function_error_strings = memif_tx_func_error_strings,
314 .rx_redirect_to_node = memif_set_interface_next_node,
315 .clear_counters = memif_clear_hw_interface_counters,
316 .admin_up_down_function = memif_interface_admin_up_down,
317 .subif_add_del_function = memif_subif_add_del_function,
320 VLIB_DEVICE_TX_FUNCTION_MULTIARCH(memif_device_class,
325 * fd.io coding-style-patch-verification: ON
328 * eval: (c-set-style "gnu")