2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 * buffer_node.h: VLIB buffer handling node helper macros/inlines
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #ifndef included_vlib_buffer_node_h
41 #define included_vlib_buffer_node_h
44 vlib buffer/node functions
47 /** \brief Finish enqueueing two buffers forward in the graph.
48 Standard dual loop boilerplate element. This is a MACRO,
49 with MULTIPLE SIDE EFFECTS. In the ideal case,
50 <code>next_index == next0 == next1</code>,
51 which means that the speculative enqueue at the top of the dual loop
52 has correctly dealt with both packets. In that case, the macro does
55 @param vm vlib_main_t pointer, varies by thread
56 @param node current node vlib_node_runtime_t pointer
57 @param next_index speculated next index used for both packets
58 @param to_next speculated vector pointer used for both packets
59 @param n_left_to_next number of slots left in speculated vector
60 @param bi0 first buffer index
61 @param bi1 second buffer index
62 @param next0 actual next index to be used for the first packet
63 @param next1 actual next index to be used for the second packet
65 @return @c next_index -- speculative next index to be used for future packets
66 @return @c to_next -- speculative frame to be used for future packets
67 @return @c n_left_to_next -- number of slots left in speculative frame
70 #define vlib_validate_buffer_enqueue_x2(vm,node,next_index,to_next,n_left_to_next,bi0,bi1,next0,next1) \
74 int enqueue_code = (next0 != next_index) + 2*(next1 != next_index); \
76 if (PREDICT_FALSE (enqueue_code != 0)) \
78 switch (enqueue_code) \
84 n_left_to_next += 1; \
85 vlib_set_next_frame_buffer (vm, node, next0, bi0); \
91 n_left_to_next += 1; \
92 vlib_set_next_frame_buffer (vm, node, next1, bi1); \
96 /* A B B or A B C */ \
98 n_left_to_next += 2; \
99 vlib_set_next_frame_buffer (vm, node, next0, bi0); \
100 vlib_set_next_frame_buffer (vm, node, next1, bi1); \
101 if (next0 == next1) \
103 vlib_put_next_frame (vm, node, next_index, \
105 next_index = next1; \
106 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); \
113 /** \brief Finish enqueueing four buffers forward in the graph.
114 Standard quad loop boilerplate element. This is a MACRO,
115 with MULTIPLE SIDE EFFECTS. In the ideal case,
116 <code>next_index == next0 == next1 == next2 == next3</code>,
117 which means that the speculative enqueue at the top of the quad loop
118 has correctly dealt with all four packets. In that case, the macro does
121 @param vm vlib_main_t pointer, varies by thread
122 @param node current node vlib_node_runtime_t pointer
123 @param next_index speculated next index used for both packets
124 @param to_next speculated vector pointer used for both packets
125 @param n_left_to_next number of slots left in speculated vector
126 @param bi0 first buffer index
127 @param bi1 second buffer index
128 @param bi2 third buffer index
129 @param bi3 fourth buffer index
130 @param next0 actual next index to be used for the first packet
131 @param next1 actual next index to be used for the second packet
132 @param next2 actual next index to be used for the third packet
133 @param next3 actual next index to be used for the fourth packet
135 @return @c next_index -- speculative next index to be used for future packets
136 @return @c to_next -- speculative frame to be used for future packets
137 @return @c n_left_to_next -- number of slots left in speculative frame
140 #define vlib_validate_buffer_enqueue_x4(vm,node,next_index,to_next,n_left_to_next,bi0,bi1,bi2,bi3,next0,next1,next2,next3) \
146 /* After the fact: check the [speculative] enqueue to "next" */ \
147 u32 fix_speculation = (next_index ^ next0) | (next_index ^ next1) \
148 | (next_index ^ next2) | (next_index ^ next3); \
149 if (PREDICT_FALSE(fix_speculation)) \
153 n_left_to_next += 4; \
155 /* If bi0 belongs to "next", send it there */ \
156 if (next_index == next0) \
162 else /* send it where it needs to go */ \
163 vlib_set_next_frame_buffer (vm, node, next0, bi0); \
165 if (next_index == next1) \
172 vlib_set_next_frame_buffer (vm, node, next1, bi1); \
174 if (next_index == next2) \
181 vlib_set_next_frame_buffer (vm, node, next2, bi2); \
183 if (next_index == next3) \
191 vlib_set_next_frame_buffer (vm, node, next3, bi3); \
193 /* Change speculation: last 2 packets went to the same node*/ \
194 if (next2 == next3) \
196 vlib_put_next_frame (vm, node, next_index, n_left_to_next); \
197 next_index = next3; \
198 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); \
204 /** \brief Finish enqueueing one buffer forward in the graph.
205 Standard single loop boilerplate element. This is a MACRO,
206 with MULTIPLE SIDE EFFECTS. In the ideal case,
207 <code>next_index == next0</code>,
208 which means that the speculative enqueue at the top of the single loop
209 has correctly dealt with the packet in hand. In that case, the macro does
212 @param vm vlib_main_t pointer, varies by thread
213 @param node current node vlib_node_runtime_t pointer
214 @param next_index speculated next index used for both packets
215 @param to_next speculated vector pointer used for both packets
216 @param n_left_to_next number of slots left in speculated vector
217 @param bi0 first buffer index
218 @param next0 actual next index to be used for the first packet
220 @return @c next_index -- speculative next index to be used for future packets
221 @return @c to_next -- speculative frame to be used for future packets
222 @return @c n_left_to_next -- number of slots left in speculative frame
224 #define vlib_validate_buffer_enqueue_x1(vm,node,next_index,to_next,n_left_to_next,bi0,next0) \
227 if (PREDICT_FALSE (next0 != next_index)) \
229 vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1); \
230 next_index = next0; \
231 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); \
235 n_left_to_next -= 1; \
240 generic_buffer_node_inline (vlib_main_t * vm,
241 vlib_node_runtime_t * node,
242 vlib_frame_t * frame,
246 void (*two_buffers) (vlib_main_t * vm,
251 u32 * next0, u32 * next1),
252 void (*one_buffer) (vlib_main_t * vm,
253 void *opaque1, uword opaque2,
257 u32 n_left_from, *from, *to_next;
260 from = vlib_frame_vector_args (frame);
261 n_left_from = frame->n_vectors;
262 next_index = node->cached_next_index;
264 if (node->flags & VLIB_NODE_FLAG_TRACE)
265 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
266 /* stride */ 1, sizeof_trace);
268 while (n_left_from > 0)
272 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
274 while (n_left_from >= 4 && n_left_to_next >= 2)
276 vlib_buffer_t *p0, *p1;
280 /* Prefetch next iteration. */
282 vlib_buffer_t *p2, *p3;
284 p2 = vlib_get_buffer (vm, from[2]);
285 p3 = vlib_get_buffer (vm, from[3]);
287 vlib_prefetch_buffer_header (p2, LOAD);
288 vlib_prefetch_buffer_header (p3, LOAD);
290 clib_prefetch_load (p2->data);
291 clib_prefetch_load (p3->data);
294 pi0 = to_next[0] = from[0];
295 pi1 = to_next[1] = from[1];
301 p0 = vlib_get_buffer (vm, pi0);
302 p1 = vlib_get_buffer (vm, pi1);
304 two_buffers (vm, opaque1, opaque2, p0, p1, &next0, &next1);
306 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
307 to_next, n_left_to_next,
308 pi0, pi1, next0, next1);
311 while (n_left_from > 0 && n_left_to_next > 0)
323 p0 = vlib_get_buffer (vm, pi0);
325 one_buffer (vm, opaque1, opaque2, p0, &next0);
327 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
328 to_next, n_left_to_next,
332 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
335 return frame->n_vectors;
338 /* Minimum size for the 'buffers' and 'nexts' arrays to be used when calling
339 * vlib_buffer_enqueue_to_next().
340 * Because of optimizations, vlib_buffer_enqueue_to_next() will access
341 * past 'count' elements in the 'buffers' and 'nexts' arrays, IOW it
343 * Those overflow elements are ignored in the final result so they do not
344 * need to be properly initialized, however if the array is allocated right
345 * before the end of a page and the next page is not mapped, accessing the
346 * overflow elements will trigger a segfault. */
347 #define VLIB_BUFFER_ENQUEUE_MIN_SIZE(n) round_pow2 ((n), 64)
349 static_always_inline void
350 vlib_buffer_enqueue_to_next (vlib_main_t * vm, vlib_node_runtime_t * node,
351 u32 * buffers, u16 * nexts, uword count)
353 vlib_buffer_enqueue_to_next_fn_t *fn;
354 fn = vlib_buffer_func_main.buffer_enqueue_to_next_fn;
355 (fn) (vm, node, buffers, nexts, count);
358 static_always_inline void
359 vlib_buffer_enqueue_to_next_vec (vlib_main_t *vm, vlib_node_runtime_t *node,
360 u32 **buffers, u16 **nexts, uword count)
362 const u32 bl = vec_len (*buffers), nl = vec_len (*nexts);
363 const u32 c = VLIB_BUFFER_ENQUEUE_MIN_SIZE (count);
364 ASSERT (bl >= count && nl >= count);
365 vec_validate (*buffers, c);
366 vec_validate (*nexts, c);
367 vlib_buffer_enqueue_to_next (vm, node, *buffers, *nexts, count);
368 vec_set_len (*buffers, bl);
369 vec_set_len (*nexts, nl);
372 static_always_inline void
373 vlib_buffer_enqueue_to_single_next (vlib_main_t * vm,
374 vlib_node_runtime_t * node, u32 * buffers,
375 u16 next_index, u32 count)
377 vlib_buffer_enqueue_to_single_next_fn_t *fn;
378 fn = vlib_buffer_func_main.buffer_enqueue_to_single_next_fn;
379 (fn) (vm, node, buffers, next_index, count);
382 static_always_inline u32
383 vlib_buffer_enqueue_to_thread (vlib_main_t *vm, vlib_node_runtime_t *node,
384 u32 frame_queue_index, u32 *buffer_indices,
385 u16 *thread_indices, u32 n_packets,
386 int drop_on_congestion)
388 vlib_buffer_enqueue_to_thread_fn_t *fn;
389 fn = vlib_buffer_func_main.buffer_enqueue_to_thread_fn;
390 return (fn) (vm, node, frame_queue_index, buffer_indices, thread_indices,
391 n_packets, drop_on_congestion);
394 #endif /* included_vlib_buffer_node_h */
397 * fd.io coding-style-patch-verification: ON
400 * eval: (c-set-style "gnu")