Punt Infra
[vpp.git] / src / vlib / punt_node.c
1 /*
2  * Copyright (c) 2018 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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vlib/punt.h>
17
18 #define foreach_punt_error                     \
19   _(DISPATCHED, "dispatched")                  \
20   _(NO_REASON, "No such punt reason")          \
21   _(NO_REG, "No registrations")                \
22   _(REP_FAIL, "Replication Faliure")
23
24 typedef enum punt_error_t_
25 {
26 #define _(v,s) PUNT_ERROR_##v,
27   foreach_punt_error
28 #undef _
29     PUNT_N_ERRORS,
30 } punt_error_t;
31
32 static char *punt_error_strings[] = {
33 #define _(v,s) [PUNT_ERROR_##v] = s,
34   foreach_punt_error
35 #undef _
36 };
37
38 typedef enum punt_next_t_
39 {
40   PUNT_NEXT_DROP,
41   PUNT_N_NEXT,
42 } punt_next_t;
43
44 typedef struct punt_trace_t_
45 {
46   vlib_punt_reason_t pt_reason;
47 } punt_trace_t;
48
49 /**
50  * Per-thread clone vectors
51  */
52 #ifndef CLIB_MARCH_VARIANT
53 u32 **punt_clones;
54 #else
55 extern u32 **punt_clones;
56 #endif
57
58 static u8 *
59 format_punt_trace (u8 * s, va_list * args)
60 {
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 *);
64
65   s = format (s, "reason: %U", format_vlib_punt_reason, t->pt_reason);
66
67   return s;
68 }
69
70 always_inline u32
71 punt_replicate (vlib_main_t * vm,
72                 vlib_node_runtime_t * node,
73                 u32 thread_index,
74                 vlib_buffer_t * b0,
75                 u32 bi0,
76                 vlib_punt_reason_t pr0,
77                 u32 * next_index,
78                 u32 * n_left_to_next, u32 ** to_next, u32 * pkts_rep_fail)
79 {
80   /* multiple clients => replicate a copy to each */
81   u16 n_clones0, n_cloned0, clone0;
82   u32 ci0, next0;
83
84   n_clones0 = vec_len (punt_dp_db[pr0]);
85   vec_validate (punt_clones[thread_index], n_clones0);
86
87   n_cloned0 = vlib_buffer_clone (vm, bi0,
88                                  punt_clones[thread_index],
89                                  n_clones0, 2 * CLIB_CACHE_LINE_BYTES);
90
91   if (PREDICT_FALSE (n_cloned0 != n_clones0))
92     {
93       b0->error = node->errors[PUNT_ERROR_REP_FAIL];
94       *pkts_rep_fail += 1;
95     }
96
97   for (clone0 = 1; clone0 < n_cloned0; clone0++)
98     {
99       ci0 = punt_clones[thread_index][clone0];
100
101       *to_next[0] = ci0;
102       *to_next += 1;
103       *n_left_to_next -= 1;
104
105       next0 = punt_dp_db[pr0][clone0];
106
107       if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
108         {
109           vlib_buffer_t *c0;
110           punt_trace_t *t;
111
112           c0 = vlib_get_buffer (vm, ci0);
113
114           if (c0 != b0)
115             vlib_buffer_copy_trace_flag (vm, b0, ci0);
116
117           t = vlib_add_trace (vm, node, c0, sizeof (*t));
118           t->pt_reason = pr0;
119         }
120
121       vlib_validate_buffer_enqueue_x1 (vm, node, *next_index,
122                                        *to_next, *n_left_to_next, ci0, next0);
123
124       /* replications here always go to different next-nodes
125        * so there's no need to check if the to_next frame
126        * is full */
127     }
128
129   /* The original buffer is the first clone */
130   next0 = punt_dp_db[pr0][0];
131   *to_next[0] = bi0;
132   return next0;
133 }
134
135 always_inline u32
136 punt_dispatch_one (vlib_main_t * vm,
137                    vlib_node_runtime_t * node,
138                    vlib_combined_counter_main_t * cm,
139                    u32 thread_index,
140                    u32 bi0,
141                    u32 * next_index,
142                    u32 * n_left_to_next, u32 ** to_next, u32 * pkts_errors)
143 {
144   vlib_punt_reason_t pr0;
145   vlib_buffer_t *b0;
146   u32 next0;
147
148   b0 = vlib_get_buffer (vm, bi0);
149   pr0 = b0->punt_reason;
150
151   if (PREDICT_FALSE (pr0 >= vec_len (punt_dp_db)))
152     {
153       b0->error = node->errors[PUNT_ERROR_NO_REASON];
154       next0 = PUNT_NEXT_DROP;
155       pkts_errors[PUNT_ERROR_NO_REASON] += 1;
156     }
157   else
158     {
159       vlib_increment_combined_counter
160         (cm, thread_index, pr0, 1, vlib_buffer_length_in_chain (vm, b0));
161
162       if (PREDICT_TRUE (1 == vec_len (punt_dp_db[pr0])))
163         {
164           /*
165            * one registered client => give it the packet
166            * This is the most likely outcome.
167            */
168           next0 = punt_dp_db[pr0][0];
169           pkts_errors[PUNT_ERROR_DISPATCHED] += 1;
170         }
171       else if (0 == vec_len (punt_dp_db[pr0]))
172         {
173           /* no registered clients => drop */
174           next0 = PUNT_NEXT_DROP;
175           pkts_errors[PUNT_ERROR_NO_REG] += 1;
176         }
177       else
178         {
179           /*
180            * multiple registered clients => replicate
181            */
182           pkts_errors[PUNT_ERROR_DISPATCHED] += 1;
183
184           next0 = punt_replicate (vm, node, thread_index, b0, bi0, pr0,
185                                   next_index, n_left_to_next, to_next,
186                                   &pkts_errors[PUNT_ERROR_REP_FAIL]);
187         }
188     }
189
190   if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
191     {
192       punt_trace_t *t;
193
194       t = vlib_add_trace (vm, node, b0, sizeof (*t));
195       t->pt_reason = pr0;
196     }
197
198   return (next0);
199 }
200
201 VLIB_NODE_FN (punt_dispatch_node) (vlib_main_t * vm,
202                                    vlib_node_runtime_t * node,
203                                    vlib_frame_t * frame)
204 {
205   u32 n_left_from, *from, *to_next, next_index, thread_index;
206   vlib_combined_counter_main_t *cm;
207   u32 pkt_errors[PUNT_N_ERRORS] = { 0 };
208
209   cm = &punt_counters;
210   from = vlib_frame_vector_args (frame);
211   n_left_from = frame->n_vectors;
212   next_index = node->cached_next_index;
213   thread_index = vlib_get_thread_index ();
214
215   while (n_left_from > 0)
216     {
217       u32 n_left_to_next;
218
219       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
220
221       while (n_left_from > 4 && n_left_to_next > 2)
222         {
223           punt_next_t next0, next1;
224           u32 bi0, bi1;
225
226           {
227             vlib_buffer_t *b2, *b3;
228
229             b2 = vlib_get_buffer (vm, from[2]);
230             b3 = vlib_get_buffer (vm, from[3]);
231
232             vlib_prefetch_buffer_header (b2, LOAD);
233             vlib_prefetch_buffer_header (b3, LOAD);
234           }
235
236           bi0 = to_next[0] = from[0];
237           bi1 = to_next[1] = from[1];
238           from += 2;
239           n_left_from -= 2;
240
241           next0 = punt_dispatch_one (vm, node, cm, thread_index, bi0,
242                                      &next_index, &n_left_to_next,
243                                      &to_next, pkt_errors);
244           next1 = punt_dispatch_one (vm, node, cm, thread_index, bi1,
245                                      &next_index, &n_left_to_next,
246                                      &to_next, pkt_errors);
247
248           to_next += 2;
249           n_left_to_next -= 2;
250
251           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
252                                            to_next, n_left_to_next,
253                                            bi0, bi1, next0, next1);
254         }
255       while (n_left_from > 0 && n_left_to_next > 0)
256         {
257           punt_next_t next0;
258           u32 bi0;
259
260           bi0 = to_next[0] = from[0];
261           from += 1;
262           n_left_from -= 1;
263
264           next0 = punt_dispatch_one (vm, node, cm, thread_index, bi0,
265                                      &next_index, &n_left_to_next,
266                                      &to_next, pkt_errors);
267
268           to_next += 1;
269           n_left_to_next -= 1;
270
271           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
272                                            to_next, n_left_to_next,
273                                            bi0, next0);
274         }
275       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
276     }
277
278   vlib_node_increment_counter (vm, node->node_index,
279                                PUNT_ERROR_DISPATCHED,
280                                pkt_errors[PUNT_ERROR_DISPATCHED]);
281   vlib_node_increment_counter (vm, node->node_index,
282                                PUNT_ERROR_NO_REASON,
283                                pkt_errors[PUNT_ERROR_NO_REASON]);
284   vlib_node_increment_counter (vm, node->node_index,
285                                PUNT_ERROR_NO_REG,
286                                pkt_errors[PUNT_ERROR_NO_REG]);
287   vlib_node_increment_counter (vm, node->node_index,
288                                PUNT_ERROR_REP_FAIL,
289                                pkt_errors[PUNT_ERROR_REP_FAIL]);
290
291   return frame->n_vectors;
292 }
293
294 /* *INDENT-OFF* */
295 VLIB_REGISTER_NODE (punt_dispatch_node) = {
296   .name = "punt-dispatch",
297   .vector_size = sizeof (u32),
298   .format_trace = format_punt_trace,
299   .n_errors = PUNT_N_ERRORS,
300   .error_strings = punt_error_strings,
301   .n_next_nodes = PUNT_N_NEXT,
302   .next_nodes = {
303     [PUNT_NEXT_DROP] = "drop",
304   },
305 };
306
307 /* *INDENT-ON* */
308
309 #ifndef CLIB_MARCH_VARIANT
310 clib_error_t *
311 punt_node_init (vlib_main_t * vm)
312 {
313   vec_validate (punt_clones, vlib_num_workers ());
314
315   return NULL;
316 }
317
318 VLIB_INIT_FUNCTION (punt_node_init);
319 #endif
320
321 /*
322  * fd.io coding-style-patch-verification: ON
323  *
324  * Local Variables:
325  * eval: (c-set-style "gnu")
326  * End:
327  */