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>
19 * The last allocated punt reason
20 * Value 0 is reserved for invalid index.
22 static vlib_punt_reason_t punt_reason_last = 1;
25 * Counters per punt-reason
27 vlib_combined_counter_main_t punt_counters = {
29 .stat_segment_name = "/net/punt",
35 typedef struct punt_reason_data_t_
43 * The allocated reason value
45 vlib_punt_reason_t pd_reason;
48 * Clients/owners that have registered this reason
53 * clients interested/listening to this reason
58 * function to invoke if a client becomes interested in the code.
60 punt_interested_listener_t pd_fn;
63 * Data to pass to the callback
68 * Flags associated to the reason
73 * Formatting function for flags;
75 format_function_t *flags_format;
79 * data for each punt reason
81 static punt_reason_data_t *punt_reason_data;
83 typedef enum punt_format_flags_t_
85 PUNT_FORMAT_FLAG_NONE = 0,
86 PUNT_FORMAT_FLAG_DETAIL = (1 << 0),
87 } punt_format_flags_t;
90 * A registration, by a client, to direct punted traffic to a given node
92 typedef struct punt_reg_t_
95 * Reason the packets were punted
97 vlib_punt_reason_t pr_reason;
100 * number of clients that have made this registration
105 * The edge from the punt dispatch node to the requested node
110 * node-index to send punted packets to
116 * Pool of registrations
118 static punt_reg_t *punt_reg_pool;
121 * A DB of all the register nodes against punt reason and node index
123 static uword *punt_reg_db;
126 * A DB used in the DP per-reason to dispatch packets to the requested nodes.
127 * this is a vector of edges per-reason
132 * A client using the punt serivce and its registrations
134 typedef struct punt_client_t_
137 * The name of the client
142 * The registrations is has made
150 static punt_client_t *punt_client_pool;
153 * DB of clients key'd by their name
155 static uword *punt_client_db;
158 format_vlib_punt_reason (u8 * s, va_list * args)
160 vlib_punt_reason_t pr = va_arg (*args, int);
161 format_function_t *flags_format = punt_reason_data[pr].flags_format;
162 u32 flags = punt_reason_data[pr].flags;
164 return (format (s, "[%d] %v flags: %U", pr, punt_reason_data[pr].pd_name,
165 flags_format, flags));
167 return (format (s, "[%d] %v", pr, punt_reason_data[pr].pd_name));
171 vlib_punt_client_register (const char *who)
177 pc_name = format (NULL, "%s", who);
178 p = hash_get_mem (punt_client_db, pc_name);
184 pool_get (punt_client_pool, pc);
185 pci = pc - punt_client_pool;
187 pc->pc_name = pc_name;
189 hash_set_mem (punt_client_db, pc->pc_name, pci);
201 punt_validate_client (vlib_punt_hdl_t client)
203 return (!pool_is_free_index (punt_client_pool, client));
207 punt_reg_mk_key (vlib_punt_reason_t reason, u32 node_index)
209 return (((u64) node_index) << 32 | reason);
213 punt_reg_find (vlib_punt_reason_t reason, u32 node_index)
217 p = hash_get (punt_reg_db, punt_reg_mk_key (reason, node_index));
226 punt_reg_add (const punt_reg_t * pr)
228 hash_set (punt_reg_db, punt_reg_mk_key (pr->pr_reason,
234 punt_reg_remove (const punt_reg_t * pr)
236 hash_unset (punt_reg_db, punt_reg_mk_key (pr->pr_reason,
241 * reconstruct the DP per-reason DB
244 punt_reg_mk_dp (vlib_punt_reason_t reason)
246 u32 pri, *prip, *pris;
247 const punt_reg_t *pr;
253 vec_validate (punt_dp_db, reason);
255 old = punt_dp_db[reason];
257 hash_foreach (key, pri, punt_reg_db,
263 * A check for an empty vector is done in the DP, so the a zero
264 * length vector here is ok
266 vec_foreach (prip, pris)
268 pr = pool_elt_at_index (punt_reg_pool, *prip);
270 if (pr->pr_reason == reason)
271 vec_add1 (edges, pr->pr_edge);
274 /* atomic update of the DP */
275 punt_dp_db[reason] = edges;
281 vlib_punt_register (vlib_punt_hdl_t client,
282 vlib_punt_reason_t reason, const char *node_name)
284 vlib_node_t *punt_to, *punt_from;
290 if (reason >= punt_reason_last)
292 if (!punt_validate_client (client))
295 vm = vlib_get_main ();
296 pc = pool_elt_at_index (punt_client_pool, client);
297 punt_to = vlib_get_node_by_name (vm, (u8 *) node_name);
298 punt_from = vlib_get_node_by_name (vm, (u8 *) "punt-dispatch");
301 * find a global matching registration
303 pri = punt_reg_find (reason, punt_to->index);
309 pos = vec_search (pc->pc_regs, pri);
313 /* duplicate registration for this client */
317 pr = pool_elt_at_index (punt_reg_pool, pri);
321 pool_get (punt_reg_pool, pr);
323 pr->pr_reason = reason;
324 pr->pr_node_index = punt_to->index;
325 pr->pr_edge = vlib_node_add_next (vm,
326 punt_from->index, pr->pr_node_index);
328 pri = pr - punt_reg_pool;
330 if (0 == punt_reason_data[reason].pd_users++ &&
331 NULL != punt_reason_data[reason].pd_fn)
332 punt_reason_data[reason].pd_fn (VLIB_ENABLE,
333 punt_reason_data[reason].pd_data);
339 * add this reg to the list the client has made
342 vec_add1 (pc->pc_regs, pri);
344 punt_reg_mk_dp (reason);
350 vlib_punt_unregister (vlib_punt_hdl_t client,
351 vlib_punt_reason_t reason, const char *node_name)
353 vlib_node_t *punt_to;
359 if (reason >= punt_reason_last)
362 vm = vlib_get_main ();
363 pc = pool_elt_at_index (punt_client_pool, client);
364 punt_to = vlib_get_node_by_name (vm, (u8 *) node_name);
367 * construct a registration and check if it's one this client already has
369 pri = punt_reg_find (reason, punt_to->index);
375 pos = vec_search (pc->pc_regs, pri);
379 /* not a registration for this client */
382 vec_del1 (pc->pc_regs, pos);
384 pr = pool_elt_at_index (punt_reg_pool, pri);
388 if (0 == pr->pr_locks)
390 if (0 == --punt_reason_data[reason].pd_users &&
391 NULL != punt_reason_data[reason].pd_fn)
392 punt_reason_data[reason].pd_fn (VLIB_DISABLE,
393 punt_reason_data[reason].pd_data);
394 punt_reg_remove (pr);
395 pool_put (punt_reg_pool, pr);
400 * rebuild the DP data-base
402 punt_reg_mk_dp (reason);
408 vlib_punt_reason_validate (vlib_punt_reason_t reason)
410 if (reason < punt_reason_last)
417 vlib_punt_reason_get_flags (vlib_punt_reason_t pr)
419 return pr < punt_reason_last ? punt_reason_data[pr].flags : 0;
423 vlib_punt_reason_alloc (vlib_punt_hdl_t client, const char *reason_name,
424 punt_interested_listener_t fn, void *data,
425 vlib_punt_reason_t *reason, u32 flags,
426 format_function_t *flags_format)
428 vlib_punt_reason_t new;
430 if (!punt_validate_client (client))
433 new = punt_reason_last++;
434 vec_validate (punt_reason_data, new);
435 punt_reason_data[new].pd_name = format (NULL, "%s", reason_name);
436 punt_reason_data[new].pd_reason = new;
437 punt_reason_data[new].pd_fn = fn;
438 punt_reason_data[new].pd_data = data;
439 punt_reason_data[new].flags = flags;
440 punt_reason_data[new].flags_format = flags_format;
441 vec_add1 (punt_reason_data[new].pd_owners, client);
443 vlib_validate_combined_counter (&punt_counters, new);
444 vlib_zero_combined_counter (&punt_counters, new);
448 /* build the DP data-base */
449 punt_reg_mk_dp (*reason);
455 punt_reason_walk (punt_reason_walk_cb_t cb, void *ctx)
457 punt_reason_data_t *pd;
459 for (pd = punt_reason_data + 1; pd < vec_end (punt_reason_data); pd++)
461 cb (pd->pd_reason, pd->pd_name, ctx);
465 /* Parse node name -> node index. */
467 unformat_punt_client (unformat_input_t * input, va_list * args)
469 u32 *result = va_arg (*args, u32 *);
471 return unformat_user (input, unformat_hash_vec_string,
472 punt_client_db, result);
475 /* Parse punt reason */
477 unformat_punt_reason (unformat_input_t *input, va_list *args)
479 u32 *result = va_arg (*args, u32 *);
482 for (int i = 0; i < punt_reason_last - 1; i++)
484 punt_reason_data_t *pd = vec_elt_at_index (punt_reason_data, 1 + i);
485 vec_reset_length (s);
486 s = format (0, "%v%c", pd->pd_name, 0);
487 if (unformat (input, (const char *) s))
489 *result = pd->pd_reason;
499 format_punt_reg (u8 * s, va_list * args)
501 u32 pri = va_arg (*args, u32);
504 pr = pool_elt_at_index (punt_reg_pool, pri);
506 s = format (s, "%U -> %U",
507 format_vlib_punt_reason, pr->pr_reason,
508 format_vlib_node_name, vlib_get_main (), pr->pr_node_index);
514 format_punt_reason_data (u8 * s, va_list * args)
516 punt_reason_data_t *pd = va_arg (*args, punt_reason_data_t *);
519 if (pd->flags_format)
520 s = format (s, "[%d] %v flags: %U from:[", pd->pd_reason, pd->pd_name,
521 pd->flags_format, pd->flags);
523 s = format (s, "[%d] %v from:[", pd->pd_reason, pd->pd_name);
524 vec_foreach (pci, pd->pd_owners)
526 pc = pool_elt_at_index (punt_client_pool, *pci);
527 s = format (s, "%v ", pc->pc_name);
535 format_punt_client (u8 * s, va_list * args)
537 u32 pci = va_arg (*args, u32);
538 punt_format_flags_t flags = va_arg (*args, punt_format_flags_t);
541 pc = pool_elt_at_index (punt_client_pool, pci);
543 s = format (s, "%v", pc->pc_name);
545 if (flags & PUNT_FORMAT_FLAG_DETAIL)
547 punt_reason_data_t *pd;
550 s = format (s, "\n registrations:");
551 vec_foreach (pri, pc->pc_regs)
553 s = format (s, "\n [%U]", format_punt_reg, *pri);
556 s = format (s, "\n reasons:");
558 vec_foreach (pd, punt_reason_data)
562 vec_foreach (tmp, pd->pd_owners)
565 s = format (s, "\n %U", format_punt_reason_data, pd);
572 static clib_error_t *
573 punt_client_show (vlib_main_t * vm,
574 unformat_input_t * input, vlib_cli_command_t * cmd)
578 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
580 if (unformat (input, "%U", unformat_punt_client, &pci))
588 vlib_cli_output (vm, "%U", format_punt_client, pci,
589 PUNT_FORMAT_FLAG_DETAIL);
595 hash_foreach(name, pci, punt_client_db,
597 vlib_cli_output (vm, "%U", format_punt_client, pci,
598 PUNT_FORMAT_FLAG_NONE);
605 VLIB_CLI_COMMAND (punt_client_show_command, static) =
607 .path = "show punt client",
608 .short_help = "show client[s] registered with the punt infra",
609 .function = punt_client_show,
612 static clib_error_t *
613 punt_reason_show (vlib_main_t * vm,
614 unformat_input_t * input, vlib_cli_command_t * cmd)
616 const punt_reason_data_t *pd;
618 vec_foreach (pd, punt_reason_data)
620 vlib_cli_output (vm, "%U", format_punt_reason_data, pd);
626 VLIB_CLI_COMMAND (punt_reason_show_command, static) =
628 .path = "show punt reasons",
629 .short_help = "show all punt reasons",
630 .function = punt_reason_show,
633 static clib_error_t *
634 punt_db_show (vlib_main_t * vm,
635 unformat_input_t * input, vlib_cli_command_t * cmd)
640 hash_foreach (key, pri, punt_reg_db,
642 vlib_cli_output (vm, " %U", format_punt_reg, pri);
645 vlib_cli_output (vm, "\nDerived data-plane data-base:");
647 " (for each punt-reason the edge[s] from punt-dispatch)");
649 vec_foreach_index (ii, punt_dp_db)
652 vlib_cli_output (vm, " %U", format_vlib_punt_reason, ii);
654 vec_foreach_index (jj, punt_dp_db[ii])
656 s = format (s, "%d ", punt_dp_db[ii][jj]);
658 vlib_cli_output (vm, " [%v]", s);
665 VLIB_CLI_COMMAND (punt_db_show_command, static) =
667 .path = "show punt db",
668 .short_help = "show the punt DB",
669 .function = punt_db_show,
672 static clib_error_t *
673 punt_stats_show (vlib_main_t * vm,
674 unformat_input_t * input, vlib_cli_command_t * cmd)
676 vlib_combined_counter_main_t *cm = &punt_counters;
680 for (ii = 0; ii < vlib_combined_counter_n_counters (cm); ii++)
682 vlib_get_combined_counter (cm, ii, &c);
683 vlib_cli_output (vm, "%U packets:%lld bytes:%lld",
684 format_vlib_punt_reason, ii, c.packets, c.bytes);
690 VLIB_CLI_COMMAND (punt_stats_show_command, static) =
692 .path = "show punt stats",
693 .short_help = "show the punt stats",
694 .function = punt_stats_show,
697 static clib_error_t *
698 punt_init (vlib_main_t * vm)
700 punt_client_db = hash_create_vec (0, sizeof (u8), sizeof (u32));
705 VLIB_INIT_FUNCTION (punt_init);
708 * fd.io coding-style-patch-verification: ON
711 * eval: (c-set-style "gnu")