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>
27 #include <vnet/devices/devices.h>
28 #include <vnet/feature/feature.h>
30 #include <memif/memif.h>
31 #include <memif/private.h>
33 #define foreach_memif_input_error
37 #define _(f,s) MEMIF_INPUT_ERROR_##f,
38 foreach_memif_input_error
41 } memif_input_error_t;
43 static char *memif_input_error_strings[] = {
45 foreach_memif_input_error
54 } memif_input_trace_t;
57 format_memif_input_trace (u8 * s, va_list * args)
59 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
60 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
61 memif_input_trace_t *t = va_arg (*args, memif_input_trace_t *);
62 uword indent = format_get_indent (s);
64 s = format (s, "memif: hw_if_index %d next-index %d",
65 t->hw_if_index, t->next_index);
66 s = format (s, "\n%Uslot: ring %u", format_white_space, indent + 2,
71 static_always_inline void
72 memif_prefetch (vlib_main_t * vm, u32 bi)
74 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
75 vlib_prefetch_buffer_header (b, STORE);
76 CLIB_PREFETCH (b->data, CLIB_CACHE_LINE_BYTES, STORE);
79 static_always_inline void
80 memif_buffer_add_to_chain (vlib_main_t * vm, u32 bi, u32 first_bi,
83 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
84 vlib_buffer_t *first_b = vlib_get_buffer (vm, first_bi);
85 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_bi);
87 /* update first buffer */
88 first_b->total_length_not_including_first_buffer += b->current_length;
90 /* update previous buffer */
91 prev_b->next_buffer = bi;
92 prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
94 /* update current buffer */
99 * @brief Copy buffer from rx ring
102 * @param * mif (in) pointer to memif interface
103 * @param * ring (in) pointer to memif ring
104 * @param * rd (in) pointer to ring data
105 * @param ring_size (in) ring size
106 * @param * n_free_bufs (in/out) the number of free vlib buffers available
107 * @param ** first_b (out) the first vlib buffer pointer
108 * @param * first_bi (out) the first vlib buffer index
109 * @param * bi (in/out) the current buffer index
110 * #param * num_slots (in/out) the number of descriptors available to read
112 * @return total bytes read from rx ring also written to vlib buffers
114 static_always_inline uword
115 memif_copy_buffer_from_rx_ring (vlib_main_t * vm, memif_if_t * mif,
116 memif_ring_t * ring, memif_queue_t * mq,
117 u16 ring_size, u32 n_buffer_bytes,
118 u32 * n_free_bufs, vlib_buffer_t ** first_b,
119 u32 * first_bi, u32 * bi, u16 * num_slots)
121 memif_main_t *nm = &memif_main;
122 u32 thread_index = vlib_get_thread_index ();
123 u32 total_bytes = 0, offset = 0;
128 u16 mask = ring_size - 1;
134 data_len = ring->desc[mq->last_head].length;
135 while (data_len && (*n_free_bufs))
137 /* get empty buffer */
138 u32 last_buf = vec_len (nm->rx_buffers[thread_index]) - 1;
140 *bi = nm->rx_buffers[thread_index][last_buf];
141 b = vlib_get_buffer (vm, *bi);
142 _vec_len (nm->rx_buffers[thread_index]) = last_buf;
144 if (PREDICT_FALSE (*n_free_bufs == 0))
147 vlib_buffer_alloc (vm,
148 &nm->rx_buffers[thread_index]
149 [*n_free_bufs], ring_size);
150 _vec_len (nm->rx_buffers[thread_index]) = *n_free_bufs;
155 memif_prefetch (vm, nm->rx_buffers[thread_index][last_buf - 2]);
156 memif_prefetch (vm, nm->rx_buffers[thread_index][last_buf - 3]);
161 data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
163 mb = memif_get_buffer (mif, ring, mq->last_head);
164 clib_memcpy (vlib_buffer_get_current (b), mb + offset,
165 CLIB_CACHE_LINE_BYTES);
166 if (bytes_to_copy > CLIB_CACHE_LINE_BYTES)
167 clib_memcpy (vlib_buffer_get_current (b) + CLIB_CACHE_LINE_BYTES,
168 mb + CLIB_CACHE_LINE_BYTES + offset,
169 bytes_to_copy - CLIB_CACHE_LINE_BYTES);
171 /* fill buffer header */
172 b->current_length = bytes_to_copy;
174 if (total_bytes == 0)
176 /* fill buffer metadata */
177 b->total_length_not_including_first_buffer = 0;
178 b->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
179 vnet_buffer (b)->sw_if_index[VLIB_RX] = mif->sw_if_index;
180 vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32) ~ 0;
182 *first_b = vlib_get_buffer (vm, *first_bi);
185 memif_buffer_add_to_chain (vm, *bi, *first_bi, prev_bi);
187 offset += bytes_to_copy;
188 total_bytes += bytes_to_copy;
189 data_len -= bytes_to_copy;
191 last_head = mq->last_head;
192 /* Advance to next descriptor */
193 mq->last_head = (mq->last_head + 1) & mask;
196 if ((ring->desc[last_head].flags & MEMIF_DESC_FLAG_NEXT) == 0)
200 return (total_bytes);
203 static_always_inline uword
204 memif_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
205 vlib_frame_t * frame, memif_if_t * mif,
206 memif_ring_type_t type, u16 qid)
208 vnet_main_t *vnm = vnet_get_main ();
212 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
213 uword n_trace = vlib_get_trace_count (vm, node);
214 memif_main_t *nm = &memif_main;
215 u32 n_rx_packets = 0;
219 u32 b0_total, b1_total;
220 u32 thread_index = vlib_get_thread_index ();
221 u16 ring_size, mask, num_slots;
222 u32 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm,
223 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
225 mq = vec_elt_at_index (mif->rx_queues, qid);
227 ring_size = 1 << mq->log2_ring_size;
228 mask = ring_size - 1;
230 if (mif->per_interface_next_index != ~0)
231 next_index = mif->per_interface_next_index;
233 n_free_bufs = vec_len (nm->rx_buffers[thread_index]);
234 if (PREDICT_FALSE (n_free_bufs < ring_size))
236 vec_validate (nm->rx_buffers[thread_index],
237 ring_size + n_free_bufs - 1);
239 vlib_buffer_alloc (vm, &nm->rx_buffers[thread_index][n_free_bufs],
241 _vec_len (nm->rx_buffers[thread_index]) = n_free_bufs;
245 if (head == mq->last_head)
248 if (head > mq->last_head)
249 num_slots = head - mq->last_head;
251 num_slots = ring_size - mq->last_head + head;
256 u32 next0 = next_index;
257 u32 next1 = next_index;
258 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
260 while (num_slots > 11 && n_left_to_next > 2)
262 if (PREDICT_TRUE (mq->last_head + 5 < ring_size))
264 CLIB_PREFETCH (memif_get_buffer (mif, ring, mq->last_head + 2),
265 CLIB_CACHE_LINE_BYTES, LOAD);
266 CLIB_PREFETCH (memif_get_buffer (mif, ring, mq->last_head + 3),
267 CLIB_CACHE_LINE_BYTES, LOAD);
268 CLIB_PREFETCH (&ring->desc[mq->last_head + 4],
269 CLIB_CACHE_LINE_BYTES, LOAD);
270 CLIB_PREFETCH (&ring->desc[mq->last_head + 5],
271 CLIB_CACHE_LINE_BYTES, LOAD);
275 CLIB_PREFETCH (memif_get_buffer
276 (mif, ring, (mq->last_head + 2) % mask),
277 CLIB_CACHE_LINE_BYTES, LOAD);
278 CLIB_PREFETCH (memif_get_buffer
279 (mif, ring, (mq->last_head + 3) % mask),
280 CLIB_CACHE_LINE_BYTES, LOAD);
281 CLIB_PREFETCH (&ring->desc[(mq->last_head + 4) % mask],
282 CLIB_CACHE_LINE_BYTES, LOAD);
283 CLIB_PREFETCH (&ring->desc[(mq->last_head + 5) % mask],
284 CLIB_CACHE_LINE_BYTES, LOAD);
287 vlib_buffer_t *first_b0 = 0;
288 u32 bi0 = 0, first_bi0 = 0;
289 b0_total = memif_copy_buffer_from_rx_ring (vm, mif, ring, mq,
292 &n_free_bufs, &first_b0,
296 vlib_buffer_t *first_b1 = 0;
297 u32 bi1 = 0, first_bi1 = 0;
298 b1_total = memif_copy_buffer_from_rx_ring (vm, mif, ring, mq,
301 &n_free_bufs, &first_b1,
306 to_next[0] = first_bi0;
307 to_next[1] = first_bi1;
312 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (first_b0);
313 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (first_b1);
315 if (PREDICT_FALSE (n_trace > 0))
318 if (PREDICT_TRUE (first_b0 != 0))
320 memif_input_trace_t *tr;
321 vlib_trace_buffer (vm, node, next0, first_b0,
322 /* follow_chain */ 0);
323 vlib_set_trace_count (vm, node, --n_trace);
324 tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
325 tr->next_index = next0;
326 tr->hw_if_index = mif->hw_if_index;
332 if (PREDICT_TRUE (first_b1 != 0))
334 memif_input_trace_t *tr;
335 vlib_trace_buffer (vm, node, next1, first_b1,
336 /* follow_chain */ 0);
337 vlib_set_trace_count (vm, node, --n_trace);
338 tr = vlib_add_trace (vm, node, first_b1, sizeof (*tr));
339 tr->next_index = next1;
340 tr->hw_if_index = mif->hw_if_index;
346 /* redirect if feature path enabled */
347 vnet_feature_start_device_input_x2 (mif->sw_if_index,
348 &next0, &next1, first_b0,
352 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
353 n_left_to_next, first_bi0,
354 first_bi1, next0, next1);
358 n_rx_bytes += b0_total + b1_total;
360 while (num_slots && n_left_to_next)
362 vlib_buffer_t *first_b0 = 0;
363 u32 bi0 = 0, first_bi0 = 0;
364 b0_total = memif_copy_buffer_from_rx_ring (vm, mif, ring, mq,
367 &n_free_bufs, &first_b0,
372 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (first_b0);
374 if (PREDICT_FALSE (n_trace > 0))
376 if (PREDICT_TRUE (first_b0 != 0))
378 memif_input_trace_t *tr;
379 vlib_trace_buffer (vm, node, next0, first_b0,
380 /* follow_chain */ 0);
381 vlib_set_trace_count (vm, node, --n_trace);
382 tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
383 tr->next_index = next0;
384 tr->hw_if_index = mif->hw_if_index;
390 to_next[0] = first_bi0;
394 /* redirect if feature path enabled */
395 vnet_feature_start_device_input_x1 (mif->sw_if_index, &next0,
399 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
400 n_left_to_next, first_bi0, next0);
404 n_rx_bytes += b0_total;
406 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
408 CLIB_MEMORY_STORE_BARRIER ();
411 vlib_increment_combined_counter (vnm->interface_main.combined_sw_if_counters
412 + VNET_INTERFACE_COUNTER_RX, thread_index,
413 mif->hw_if_index, n_rx_packets,
420 memif_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
421 vlib_frame_t * frame)
424 memif_main_t *nm = &memif_main;
425 vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
426 vnet_device_and_queue_t *dq;
428 foreach_device_and_queue (dq, rt->devices_and_queues)
431 mif = vec_elt_at_index (nm->interfaces, dq->dev_instance);
432 if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) &&
433 (mif->flags & MEMIF_IF_FLAG_CONNECTED))
435 if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
436 n_rx += memif_device_input_inline (vm, node, frame, mif,
437 MEMIF_RING_M2S, dq->queue_id);
439 n_rx += memif_device_input_inline (vm, node, frame, mif,
440 MEMIF_RING_S2M, dq->queue_id);
448 VLIB_REGISTER_NODE (memif_input_node) = {
449 .function = memif_input_fn,
450 .name = "memif-input",
451 .sibling_of = "device-input",
452 .format_trace = format_memif_input_trace,
453 .type = VLIB_NODE_TYPE_INPUT,
454 .state = VLIB_NODE_STATE_INTERRUPT,
455 .n_errors = MEMIF_INPUT_N_ERROR,
456 .error_strings = memif_input_error_strings,
459 VLIB_NODE_FUNCTION_MULTIARCH (memif_input_node, memif_input_fn)
464 * fd.io coding-style-patch-verification: ON
467 * eval: (c-set-style "gnu")