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
43 #define vlib_validate_buffer_enqueue_x2(vm,node,next_index,to_next,n_left_to_next,bi0,bi1,next0,next1) \
45 int enqueue_code = (next0 != next_index) + 2*(next1 != next_index); \
47 if (PREDICT_FALSE (enqueue_code != 0)) \
49 switch (enqueue_code) \
55 n_left_to_next += 1; \
56 vlib_set_next_frame_buffer (vm, node, next0, bi0); \
62 n_left_to_next += 1; \
63 vlib_set_next_frame_buffer (vm, node, next1, bi1); \
67 /* A B B or A B C */ \
69 n_left_to_next += 2; \
70 vlib_set_next_frame_buffer (vm, node, next0, bi0); \
71 vlib_set_next_frame_buffer (vm, node, next1, bi1); \
74 vlib_put_next_frame (vm, node, next_index, \
77 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); \
83 #define vlib_validate_buffer_enqueue_x1(vm,node,next_index,to_next,n_left_to_next,bi0,next0) \
85 if (PREDICT_FALSE (next0 != next_index)) \
87 vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1); \
89 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); \
93 n_left_to_next -= 1; \
98 generic_buffer_node_inline (vlib_main_t * vm,
99 vlib_node_runtime_t * node,
100 vlib_frame_t * frame,
104 void (* two_buffers) (vlib_main_t * vm,
107 vlib_buffer_t * b0, vlib_buffer_t * b1,
108 u32 * next0, u32 * next1),
109 void (* one_buffer) (vlib_main_t * vm,
115 u32 n_left_from, * from, * to_next;
118 from = vlib_frame_vector_args (frame);
119 n_left_from = frame->n_vectors;
120 next_index = node->cached_next_index;
122 if (node->flags & VLIB_NODE_FLAG_TRACE)
123 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
124 /* stride */ 1, sizeof_trace);
126 while (n_left_from > 0)
130 vlib_get_next_frame (vm, node, next_index,
131 to_next, n_left_to_next);
133 while (n_left_from >= 4 && n_left_to_next >= 2)
135 vlib_buffer_t * p0, * p1;
139 /* Prefetch next iteration. */
141 vlib_buffer_t * p2, * p3;
143 p2 = vlib_get_buffer (vm, from[2]);
144 p3 = vlib_get_buffer (vm, from[3]);
146 vlib_prefetch_buffer_header (p2, LOAD);
147 vlib_prefetch_buffer_header (p3, LOAD);
149 CLIB_PREFETCH (p2->data, 64, LOAD);
150 CLIB_PREFETCH (p3->data, 64, LOAD);
153 pi0 = to_next[0] = from[0];
154 pi1 = to_next[1] = from[1];
160 p0 = vlib_get_buffer (vm, pi0);
161 p1 = vlib_get_buffer (vm, pi1);
163 two_buffers (vm, opaque1, opaque2, p0, p1, &next0, &next1);
165 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
166 to_next, n_left_to_next,
167 pi0, pi1, next0, next1);
170 while (n_left_from > 0 && n_left_to_next > 0)
182 p0 = vlib_get_buffer (vm, pi0);
184 one_buffer (vm, opaque1, opaque2, p0, &next0);
186 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
187 to_next, n_left_to_next,
191 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
194 return frame->n_vectors;
197 #endif /* included_vlib_buffer_node_h */