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
21 static vlib_punt_reason_t punt_reason_last;
24 * Counters per punt-reason
26 vlib_combined_counter_main_t punt_counters = {
28 .stat_segment_name = "/net/punt",
34 typedef struct punt_reason_data_t_
42 * The allocated reason value
44 vlib_punt_reason_t pd_reason;
47 * Clients/owners that have registered this reason
52 * clients interested/listening to this reason
57 * function to invoke if a client becomes interested in the code.
59 punt_interested_listener_t pd_fn;
62 * Data to pass to the callback
68 * data for each punt reason
70 static punt_reason_data_t *punt_reason_data;
72 typedef enum punt_format_flags_t_
74 PUNT_FORMAT_FLAG_NONE = 0,
75 PUNT_FORMAT_FLAG_DETAIL = (1 << 0),
76 } punt_format_flags_t;
79 * A registration, by a client, to direct punted traffic to a given node
81 typedef struct punt_reg_t_
84 * Reason the packets were punted
86 vlib_punt_reason_t pr_reason;
89 * number of clients that have made this registration
94 * The edge from the punt dispatch node to the requested node
99 * node-index to send punted packets to
105 * Pool of registrations
107 static punt_reg_t *punt_reg_pool;
110 * A DB of all the register nodes against punt reason and node index
112 static uword *punt_reg_db;
115 * A DB used in the DP per-reason to dispatch packets to the requested nodes.
116 * this is a vector of edges per-reason
121 * A client using the punt serivce and its registrations
123 typedef struct punt_client_t_
126 * The name of the client
131 * The registrations is has made
139 static punt_client_t *punt_client_pool;
142 * DB of clients key'd by their name
144 static uword *punt_client_db;
147 format_vlib_punt_reason (u8 * s, va_list * args)
149 vlib_punt_reason_t pr = va_arg (*args, int);
151 return (format (s, "[%d] %v", pr, punt_reason_data[pr].pd_name));
155 vlib_punt_client_register (const char *who)
161 pc_name = format (NULL, "%s", who);
162 p = hash_get_mem (punt_client_db, pc_name);
168 pool_get (punt_client_pool, pc);
169 pci = pc - punt_client_pool;
171 pc->pc_name = pc_name;
173 hash_set_mem (punt_client_db, pc->pc_name, pci);
185 punt_validate_client (vlib_punt_hdl_t client)
187 return (!pool_is_free_index (punt_client_pool, client));
191 punt_reg_mk_key (vlib_punt_reason_t reason, u32 node_index)
193 return (((u64) node_index) << 32 | reason);
197 punt_reg_find (vlib_punt_reason_t reason, u32 node_index)
201 p = hash_get (punt_reg_db, punt_reg_mk_key (reason, node_index));
210 punt_reg_add (const punt_reg_t * pr)
212 hash_set (punt_reg_db, punt_reg_mk_key (pr->pr_reason,
218 punt_reg_remove (const punt_reg_t * pr)
220 hash_unset (punt_reg_db, punt_reg_mk_key (pr->pr_reason,
225 * reconstruct the DP per-reason DB
228 punt_reg_mk_dp (vlib_punt_reason_t reason)
230 u32 pri, *prip, *pris;
231 const punt_reg_t *pr;
237 vec_validate (punt_dp_db, reason);
239 old = punt_dp_db[reason];
242 hash_foreach (key, pri, punt_reg_db,
249 * A check for an empty vector is done in the DP, so the a zero
250 * length vector here is ok
252 vec_foreach (prip, pris)
254 pr = pool_elt_at_index (punt_reg_pool, *prip);
256 if (pr->pr_reason == reason)
257 vec_add1 (edges, pr->pr_edge);
260 /* atomic update of the DP */
261 punt_dp_db[reason] = edges;
267 vlib_punt_register (vlib_punt_hdl_t client,
268 vlib_punt_reason_t reason, const char *node_name)
270 vlib_node_t *punt_to, *punt_from;
276 if (reason >= punt_reason_last)
278 if (!punt_validate_client (client))
281 vm = vlib_get_main ();
282 pc = pool_elt_at_index (punt_client_pool, client);
283 punt_to = vlib_get_node_by_name (vm, (u8 *) node_name);
284 punt_from = vlib_get_node_by_name (vm, (u8 *) "punt-dispatch");
287 * find a global matching registration
289 pri = punt_reg_find (reason, punt_to->index);
295 pos = vec_search (pc->pc_regs, pri);
299 /* duplicate registration for this client */
303 pr = pool_elt_at_index (punt_reg_pool, pri);
307 pool_get (punt_reg_pool, pr);
309 pr->pr_reason = reason;
310 pr->pr_node_index = punt_to->index;
311 pr->pr_edge = vlib_node_add_next (vm,
312 punt_from->index, pr->pr_node_index);
314 pri = pr - punt_reg_pool;
316 if (0 == punt_reason_data[reason].pd_users++ &&
317 NULL != punt_reason_data[reason].pd_fn)
318 punt_reason_data[reason].pd_fn (VLIB_ENABLE,
319 punt_reason_data[reason].pd_data);
325 * add this reg to the list the client has made
328 vec_add1 (pc->pc_regs, pri);
330 punt_reg_mk_dp (reason);
336 vlib_punt_unregister (vlib_punt_hdl_t client,
337 vlib_punt_reason_t reason, const char *node_name)
339 vlib_node_t *punt_to;
345 if (reason >= punt_reason_last)
348 vm = vlib_get_main ();
349 pc = pool_elt_at_index (punt_client_pool, client);
350 punt_to = vlib_get_node_by_name (vm, (u8 *) node_name);
353 * construct a registration and check if it's one this client already has
355 pri = punt_reg_find (reason, punt_to->index);
361 pos = vec_search (pc->pc_regs, pri);
365 /* not a registration for this client */
368 vec_del1 (pc->pc_regs, pos);
370 pr = pool_elt_at_index (punt_reg_pool, pri);
374 if (0 == pr->pr_locks)
376 if (0 == --punt_reason_data[reason].pd_users &&
377 NULL != punt_reason_data[reason].pd_fn)
378 punt_reason_data[reason].pd_fn (VLIB_DISABLE,
379 punt_reason_data[reason].pd_data);
380 punt_reg_remove (pr);
381 pool_put (punt_reg_pool, pr);
386 * rebuild the DP data-base
388 punt_reg_mk_dp (reason);
394 vlib_punt_reason_validate (vlib_punt_reason_t reason)
396 if (reason < punt_reason_last)
403 vlib_punt_reason_alloc (vlib_punt_hdl_t client,
404 const char *reason_name,
405 punt_interested_listener_t fn,
406 void *data, vlib_punt_reason_t * reason)
408 vlib_punt_reason_t new;
410 if (!punt_validate_client (client))
413 new = punt_reason_last++;
414 vec_validate (punt_reason_data, new);
415 punt_reason_data[new].pd_name = format (NULL, "%s", reason_name);
416 punt_reason_data[new].pd_reason = new;
417 punt_reason_data[new].pd_fn = fn;
418 punt_reason_data[new].pd_data = data;
419 vec_add1 (punt_reason_data[new].pd_owners, client);
421 vlib_validate_combined_counter (&punt_counters, new);
422 vlib_zero_combined_counter (&punt_counters, new);
426 /* build the DP data-base */
427 punt_reg_mk_dp (*reason);
433 punt_reason_walk (punt_reason_walk_cb_t cb, void *ctx)
435 punt_reason_data_t *pd;
437 vec_foreach (pd, punt_reason_data)
439 cb (pd->pd_reason, pd->pd_name, ctx);
443 /* Parse node name -> node index. */
445 unformat_punt_client (unformat_input_t * input, va_list * args)
447 u32 *result = va_arg (*args, u32 *);
449 return unformat_user (input, unformat_hash_vec_string,
450 punt_client_db, result);
454 format_punt_reg (u8 * s, va_list * args)
456 u32 pri = va_arg (*args, u32);
459 pr = pool_elt_at_index (punt_reg_pool, pri);
461 s = format (s, "%U -> %U",
462 format_vlib_punt_reason, pr->pr_reason,
463 format_vlib_node_name, vlib_get_main (), pr->pr_node_index);
469 format_punt_reason_data (u8 * s, va_list * args)
471 punt_reason_data_t *pd = va_arg (*args, punt_reason_data_t *);
475 s = format (s, "[%d] %v from:[", pd->pd_reason, pd->pd_name);
476 vec_foreach (pci, pd->pd_owners)
478 pc = pool_elt_at_index (punt_client_pool, *pci);
479 s = format (s, "%v ", pc->pc_name);
487 format_punt_client (u8 * s, va_list * args)
489 u32 pci = va_arg (*args, u32);
490 punt_format_flags_t flags = va_arg (*args, punt_format_flags_t);
493 pc = pool_elt_at_index (punt_client_pool, pci);
495 s = format (s, "%v", pc->pc_name);
497 if (flags & PUNT_FORMAT_FLAG_DETAIL)
499 punt_reason_data_t *pd;
502 s = format (s, "\n registrations:");
503 vec_foreach (pri, pc->pc_regs)
505 s = format (s, "\n [%U]", format_punt_reg, *pri);
508 s = format (s, "\n reasons:");
510 vec_foreach (pd, punt_reason_data)
514 vec_foreach (tmp, pd->pd_owners)
517 s = format (s, "\n %U", format_punt_reason_data, pd);
524 static clib_error_t *
525 punt_client_show (vlib_main_t * vm,
526 unformat_input_t * input, vlib_cli_command_t * cmd)
530 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
532 if (unformat (input, "%U", unformat_punt_client, &pci))
540 vlib_cli_output (vm, "%U", format_punt_client, pci,
541 PUNT_FORMAT_FLAG_DETAIL);
548 hash_foreach(name, pci, punt_client_db,
550 vlib_cli_output (vm, "%U", format_punt_client, pci,
551 PUNT_FORMAT_FLAG_NONE);
560 VLIB_CLI_COMMAND (punt_client_show_command, static) =
562 .path = "show punt client",
563 .short_help = "show client[s] registered with the punt infra",
564 .function = punt_client_show,
568 static clib_error_t *
569 punt_reason_show (vlib_main_t * vm,
570 unformat_input_t * input, vlib_cli_command_t * cmd)
572 const punt_reason_data_t *pd;
574 vec_foreach (pd, punt_reason_data)
576 vlib_cli_output (vm, "%U", format_punt_reason_data, pd);
583 VLIB_CLI_COMMAND (punt_reason_show_command, static) =
585 .path = "show punt reasons",
586 .short_help = "show all punt reasons",
587 .function = punt_reason_show,
591 static clib_error_t *
592 punt_db_show (vlib_main_t * vm,
593 unformat_input_t * input, vlib_cli_command_t * cmd)
599 hash_foreach (key, pri, punt_reg_db,
601 vlib_cli_output (vm, " %U", format_punt_reg, pri);
605 vlib_cli_output (vm, "\nDerived data-plane data-base:");
607 " (for each punt-reason the edge[s] from punt-dispatch)");
609 vec_foreach_index (ii, punt_dp_db)
612 vlib_cli_output (vm, " %U", format_vlib_punt_reason, ii);
614 vec_foreach_index (jj, punt_dp_db[ii])
616 s = format (s, "%d ", punt_dp_db[ii][jj]);
618 vlib_cli_output (vm, " [%v]", s);
626 VLIB_CLI_COMMAND (punt_db_show_command, static) =
628 .path = "show punt db",
629 .short_help = "show the punt DB",
630 .function = punt_db_show,
634 static clib_error_t *
635 punt_stats_show (vlib_main_t * vm,
636 unformat_input_t * input, vlib_cli_command_t * cmd)
638 vlib_combined_counter_main_t *cm = &punt_counters;
642 for (ii = 0; ii < vlib_combined_counter_n_counters (cm); ii++)
644 vlib_get_combined_counter (cm, ii, &c);
645 vlib_cli_output (vm, "%U packets:%lld bytes:%lld",
646 format_vlib_punt_reason, ii, c.packets, c.bytes);
653 VLIB_CLI_COMMAND (punt_stats_show_command, static) =
655 .path = "show punt stats",
656 .short_help = "show the punt stats",
657 .function = punt_stats_show,
661 static clib_error_t *
662 punt_init (vlib_main_t * vm)
664 punt_client_db = hash_create_vec (0, sizeof (u8), sizeof (u32));
669 VLIB_INIT_FUNCTION (punt_init);
672 * fd.io coding-style-patch-verification: ON
675 * eval: (c-set-style "gnu")