2 * Copyright (c) 2019 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 #include <vlib/punt.h>
18 #define foreach_punt_error \
19 _(DISPATCHED, "dispatched") \
20 _(NO_REASON, "No such punt reason") \
21 _(NO_REG, "No registrations") \
22 _(REP_FAIL, "Replication Failure")
24 typedef enum punt_error_t_
26 #define _(v,s) PUNT_ERROR_##v,
32 static char *punt_error_strings[] = {
33 #define _(v,s) [PUNT_ERROR_##v] = s,
38 typedef enum punt_next_t_
44 typedef struct punt_trace_t_
46 vlib_punt_reason_t pt_reason;
50 * Per-thread clone vectors
52 #ifndef CLIB_MARCH_VARIANT
55 extern u32 **punt_clones;
59 format_punt_trace (u8 * s, va_list * args)
61 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
62 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
63 punt_trace_t *t = va_arg (*args, punt_trace_t *);
65 s = format (s, "reason: %U", format_vlib_punt_reason, t->pt_reason);
71 punt_replicate (vlib_main_t * vm,
72 vlib_node_runtime_t * node,
76 vlib_punt_reason_t pr0,
78 u32 * n_left_to_next, u32 ** to_next, u32 * n_dispatched)
80 /* multiple clients => replicate a copy to each */
81 u16 n_clones0, n_cloned0, clone0;
84 n_clones0 = vec_len (punt_dp_db[pr0]);
85 vec_validate (punt_clones[thread_index], n_clones0);
87 n_cloned0 = vlib_buffer_clone (vm, bi0,
88 punt_clones[thread_index],
89 n_clones0, 2 * CLIB_CACHE_LINE_BYTES);
91 if (PREDICT_FALSE (n_cloned0 != n_clones0))
93 b0->error = node->errors[PUNT_ERROR_REP_FAIL];
96 for (clone0 = 1; clone0 < n_cloned0; clone0++)
98 ci0 = punt_clones[thread_index][clone0];
102 *n_left_to_next -= 1;
104 next0 = punt_dp_db[pr0][clone0];
106 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
111 c0 = vlib_get_buffer (vm, ci0);
112 t = vlib_add_trace (vm, node, c0, sizeof (*t));
116 vlib_validate_buffer_enqueue_x1 (vm, node, *next_index,
117 *to_next, *n_left_to_next, ci0, next0);
119 /* replications here always go to different next-nodes
120 * so there's no need to check if the to_next frame
123 *n_dispatched = *n_dispatched + n_clones0;
125 /* The original buffer is the first clone */
126 next0 = punt_dp_db[pr0][0];
132 punt_dispatch_one (vlib_main_t * vm,
133 vlib_node_runtime_t * node,
134 vlib_combined_counter_main_t * cm,
138 u32 * n_left_to_next, u32 ** to_next, u32 * n_dispatched)
140 vlib_punt_reason_t pr0;
144 b0 = vlib_get_buffer (vm, bi0);
145 pr0 = b0->punt_reason;
147 if (PREDICT_FALSE (pr0 >= vec_len (punt_dp_db)))
149 b0->error = node->errors[PUNT_ERROR_NO_REASON];
150 next0 = PUNT_NEXT_DROP;
154 vlib_increment_combined_counter
155 (cm, thread_index, pr0, 1, vlib_buffer_length_in_chain (vm, b0));
157 if (PREDICT_TRUE (1 == vec_len (punt_dp_db[pr0])))
160 * one registered client => give it the packet
161 * This is the most likely outcome.
163 next0 = punt_dp_db[pr0][0];
164 *n_dispatched = *n_dispatched + 1;
166 else if (0 == vec_len (punt_dp_db[pr0]))
168 /* no registered clients => drop */
169 next0 = PUNT_NEXT_DROP;
170 b0->error = node->errors[PUNT_ERROR_NO_REG];
175 * multiple registered clients => replicate
177 next0 = punt_replicate (vm, node, thread_index, b0, bi0, pr0,
178 next_index, n_left_to_next, to_next,
183 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
187 t = vlib_add_trace (vm, node, b0, sizeof (*t));
194 VLIB_NODE_FN (punt_dispatch_node) (vlib_main_t * vm,
195 vlib_node_runtime_t * node,
196 vlib_frame_t * frame)
198 u32 n_left_from, *from, *to_next, next_index, thread_index;
199 vlib_combined_counter_main_t *cm;
203 from = vlib_frame_vector_args (frame);
204 n_left_from = frame->n_vectors;
205 next_index = node->cached_next_index;
206 thread_index = vlib_get_thread_index ();
209 while (n_left_from > 0)
213 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
215 while (n_left_from > 4 && n_left_to_next > 2)
217 punt_next_t next0, next1;
221 vlib_buffer_t *b2, *b3;
223 b2 = vlib_get_buffer (vm, from[2]);
224 b3 = vlib_get_buffer (vm, from[3]);
226 vlib_prefetch_buffer_header (b2, LOAD);
227 vlib_prefetch_buffer_header (b3, LOAD);
230 bi0 = to_next[0] = from[0];
231 bi1 = to_next[1] = from[1];
235 next0 = punt_dispatch_one (vm, node, cm, thread_index, bi0,
236 &next_index, &n_left_to_next,
237 &to_next, &n_dispatched);
238 next1 = punt_dispatch_one (vm, node, cm, thread_index, bi1,
239 &next_index, &n_left_to_next,
240 &to_next, &n_dispatched);
245 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
246 to_next, n_left_to_next,
247 bi0, bi1, next0, next1);
249 while (n_left_from > 0 && n_left_to_next > 0)
254 bi0 = to_next[0] = from[0];
258 next0 = punt_dispatch_one (vm, node, cm, thread_index, bi0,
259 &next_index, &n_left_to_next,
260 &to_next, &n_dispatched);
265 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
266 to_next, n_left_to_next,
269 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
272 vlib_node_increment_counter (vm, node->node_index,
273 PUNT_ERROR_DISPATCHED, n_dispatched);
275 return frame->n_vectors;
279 VLIB_REGISTER_NODE (punt_dispatch_node) = {
280 .name = "punt-dispatch",
281 .vector_size = sizeof (u32),
282 .format_trace = format_punt_trace,
283 .n_errors = PUNT_N_ERRORS,
284 .error_strings = punt_error_strings,
285 .n_next_nodes = PUNT_N_NEXT,
287 [PUNT_NEXT_DROP] = "drop",
293 #ifndef CLIB_MARCH_VARIANT
295 punt_node_init (vlib_main_t * vm)
297 vec_validate (punt_clones, vlib_num_workers ());
302 VLIB_INIT_FUNCTION (punt_node_init);
306 * fd.io coding-style-patch-verification: ON
309 * eval: (c-set-style "gnu")