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
69 * data for each punt reason
71 static punt_reason_data_t *punt_reason_data;
73 typedef enum punt_format_flags_t_
75 PUNT_FORMAT_FLAG_NONE = 0,
76 PUNT_FORMAT_FLAG_DETAIL = (1 << 0),
77 } punt_format_flags_t;
80 * A registration, by a client, to direct punted traffic to a given node
82 typedef struct punt_reg_t_
85 * Reason the packets were punted
87 vlib_punt_reason_t pr_reason;
90 * number of clients that have made this registration
95 * The edge from the punt dispatch node to the requested node
100 * node-index to send punted packets to
106 * Pool of registrations
108 static punt_reg_t *punt_reg_pool;
111 * A DB of all the register nodes against punt reason and node index
113 static uword *punt_reg_db;
116 * A DB used in the DP per-reason to dispatch packets to the requested nodes.
117 * this is a vector of edges per-reason
122 * A client using the punt serivce and its registrations
124 typedef struct punt_client_t_
127 * The name of the client
132 * The registrations is has made
140 static punt_client_t *punt_client_pool;
143 * DB of clients key'd by their name
145 static uword *punt_client_db;
148 format_vlib_punt_reason (u8 * s, va_list * args)
150 vlib_punt_reason_t pr = va_arg (*args, int);
152 return (format (s, "[%d] %v", pr, punt_reason_data[pr].pd_name));
156 vlib_punt_client_register (const char *who)
162 pc_name = format (NULL, "%s", who);
163 p = hash_get_mem (punt_client_db, pc_name);
169 pool_get (punt_client_pool, pc);
170 pci = pc - punt_client_pool;
172 pc->pc_name = pc_name;
174 hash_set_mem (punt_client_db, pc->pc_name, pci);
186 punt_validate_client (vlib_punt_hdl_t client)
188 return (!pool_is_free_index (punt_client_pool, client));
192 punt_reg_mk_key (vlib_punt_reason_t reason, u32 node_index)
194 return (((u64) node_index) << 32 | reason);
198 punt_reg_find (vlib_punt_reason_t reason, u32 node_index)
202 p = hash_get (punt_reg_db, punt_reg_mk_key (reason, node_index));
211 punt_reg_add (const punt_reg_t * pr)
213 hash_set (punt_reg_db, punt_reg_mk_key (pr->pr_reason,
219 punt_reg_remove (const punt_reg_t * pr)
221 hash_unset (punt_reg_db, punt_reg_mk_key (pr->pr_reason,
226 * reconstruct the DP per-reason DB
229 punt_reg_mk_dp (vlib_punt_reason_t reason)
231 u32 pri, *prip, *pris;
232 const punt_reg_t *pr;
238 vec_validate (punt_dp_db, reason);
240 old = punt_dp_db[reason];
243 hash_foreach (key, pri, punt_reg_db,
250 * A check for an empty vector is done in the DP, so the a zero
251 * length vector here is ok
253 vec_foreach (prip, pris)
255 pr = pool_elt_at_index (punt_reg_pool, *prip);
257 if (pr->pr_reason == reason)
258 vec_add1 (edges, pr->pr_edge);
261 /* atomic update of the DP */
262 punt_dp_db[reason] = edges;
268 vlib_punt_register (vlib_punt_hdl_t client,
269 vlib_punt_reason_t reason, const char *node_name)
271 vlib_node_t *punt_to, *punt_from;
277 if (reason >= punt_reason_last)
279 if (!punt_validate_client (client))
282 vm = vlib_get_main ();
283 pc = pool_elt_at_index (punt_client_pool, client);
284 punt_to = vlib_get_node_by_name (vm, (u8 *) node_name);
285 punt_from = vlib_get_node_by_name (vm, (u8 *) "punt-dispatch");
288 * find a global matching registration
290 pri = punt_reg_find (reason, punt_to->index);
296 pos = vec_search (pc->pc_regs, pri);
300 /* duplicate registration for this client */
304 pr = pool_elt_at_index (punt_reg_pool, pri);
308 pool_get (punt_reg_pool, pr);
310 pr->pr_reason = reason;
311 pr->pr_node_index = punt_to->index;
312 pr->pr_edge = vlib_node_add_next (vm,
313 punt_from->index, pr->pr_node_index);
315 pri = pr - punt_reg_pool;
317 if (0 == punt_reason_data[reason].pd_users++ &&
318 NULL != punt_reason_data[reason].pd_fn)
319 punt_reason_data[reason].pd_fn (VLIB_ENABLE,
320 punt_reason_data[reason].pd_data);
326 * add this reg to the list the client has made
329 vec_add1 (pc->pc_regs, pri);
331 punt_reg_mk_dp (reason);
337 vlib_punt_unregister (vlib_punt_hdl_t client,
338 vlib_punt_reason_t reason, const char *node_name)
340 vlib_node_t *punt_to;
346 if (reason >= punt_reason_last)
349 vm = vlib_get_main ();
350 pc = pool_elt_at_index (punt_client_pool, client);
351 punt_to = vlib_get_node_by_name (vm, (u8 *) node_name);
354 * construct a registration and check if it's one this client already has
356 pri = punt_reg_find (reason, punt_to->index);
362 pos = vec_search (pc->pc_regs, pri);
366 /* not a registration for this client */
369 vec_del1 (pc->pc_regs, pos);
371 pr = pool_elt_at_index (punt_reg_pool, pri);
375 if (0 == pr->pr_locks)
377 if (0 == --punt_reason_data[reason].pd_users &&
378 NULL != punt_reason_data[reason].pd_fn)
379 punt_reason_data[reason].pd_fn (VLIB_DISABLE,
380 punt_reason_data[reason].pd_data);
381 punt_reg_remove (pr);
382 pool_put (punt_reg_pool, pr);
387 * rebuild the DP data-base
389 punt_reg_mk_dp (reason);
395 vlib_punt_reason_validate (vlib_punt_reason_t reason)
397 if (reason < punt_reason_last)
404 vlib_punt_reason_alloc (vlib_punt_hdl_t client,
405 const char *reason_name,
406 punt_interested_listener_t fn,
407 void *data, vlib_punt_reason_t * reason)
409 vlib_punt_reason_t new;
411 if (!punt_validate_client (client))
414 new = punt_reason_last++;
415 vec_validate (punt_reason_data, new);
416 punt_reason_data[new].pd_name = format (NULL, "%s", reason_name);
417 punt_reason_data[new].pd_reason = new;
418 punt_reason_data[new].pd_fn = fn;
419 punt_reason_data[new].pd_data = data;
420 vec_add1 (punt_reason_data[new].pd_owners, client);
422 vlib_validate_combined_counter (&punt_counters, new);
423 vlib_zero_combined_counter (&punt_counters, new);
427 /* build the DP data-base */
428 punt_reg_mk_dp (*reason);
434 punt_reason_walk (punt_reason_walk_cb_t cb, void *ctx)
436 punt_reason_data_t *pd;
438 for (pd = punt_reason_data + 1; pd < vec_end (punt_reason_data); pd++)
440 cb (pd->pd_reason, pd->pd_name, ctx);
444 /* Parse node name -> node index. */
446 unformat_punt_client (unformat_input_t * input, va_list * args)
448 u32 *result = va_arg (*args, u32 *);
450 return unformat_user (input, unformat_hash_vec_string,
451 punt_client_db, result);
455 format_punt_reg (u8 * s, va_list * args)
457 u32 pri = va_arg (*args, u32);
460 pr = pool_elt_at_index (punt_reg_pool, pri);
462 s = format (s, "%U -> %U",
463 format_vlib_punt_reason, pr->pr_reason,
464 format_vlib_node_name, vlib_get_main (), pr->pr_node_index);
470 format_punt_reason_data (u8 * s, va_list * args)
472 punt_reason_data_t *pd = va_arg (*args, punt_reason_data_t *);
476 s = format (s, "[%d] %v from:[", pd->pd_reason, pd->pd_name);
477 vec_foreach (pci, pd->pd_owners)
479 pc = pool_elt_at_index (punt_client_pool, *pci);
480 s = format (s, "%v ", pc->pc_name);
488 format_punt_client (u8 * s, va_list * args)
490 u32 pci = va_arg (*args, u32);
491 punt_format_flags_t flags = va_arg (*args, punt_format_flags_t);
494 pc = pool_elt_at_index (punt_client_pool, pci);
496 s = format (s, "%v", pc->pc_name);
498 if (flags & PUNT_FORMAT_FLAG_DETAIL)
500 punt_reason_data_t *pd;
503 s = format (s, "\n registrations:");
504 vec_foreach (pri, pc->pc_regs)
506 s = format (s, "\n [%U]", format_punt_reg, *pri);
509 s = format (s, "\n reasons:");
511 vec_foreach (pd, punt_reason_data)
515 vec_foreach (tmp, pd->pd_owners)
518 s = format (s, "\n %U", format_punt_reason_data, pd);
525 static clib_error_t *
526 punt_client_show (vlib_main_t * vm,
527 unformat_input_t * input, vlib_cli_command_t * cmd)
531 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
533 if (unformat (input, "%U", unformat_punt_client, &pci))
541 vlib_cli_output (vm, "%U", format_punt_client, pci,
542 PUNT_FORMAT_FLAG_DETAIL);
549 hash_foreach(name, pci, punt_client_db,
551 vlib_cli_output (vm, "%U", format_punt_client, pci,
552 PUNT_FORMAT_FLAG_NONE);
561 VLIB_CLI_COMMAND (punt_client_show_command, static) =
563 .path = "show punt client",
564 .short_help = "show client[s] registered with the punt infra",
565 .function = punt_client_show,
569 static clib_error_t *
570 punt_reason_show (vlib_main_t * vm,
571 unformat_input_t * input, vlib_cli_command_t * cmd)
573 const punt_reason_data_t *pd;
575 vec_foreach (pd, punt_reason_data)
577 vlib_cli_output (vm, "%U", format_punt_reason_data, pd);
584 VLIB_CLI_COMMAND (punt_reason_show_command, static) =
586 .path = "show punt reasons",
587 .short_help = "show all punt reasons",
588 .function = punt_reason_show,
592 static clib_error_t *
593 punt_db_show (vlib_main_t * vm,
594 unformat_input_t * input, vlib_cli_command_t * cmd)
600 hash_foreach (key, pri, punt_reg_db,
602 vlib_cli_output (vm, " %U", format_punt_reg, pri);
606 vlib_cli_output (vm, "\nDerived data-plane data-base:");
608 " (for each punt-reason the edge[s] from punt-dispatch)");
610 vec_foreach_index (ii, punt_dp_db)
613 vlib_cli_output (vm, " %U", format_vlib_punt_reason, ii);
615 vec_foreach_index (jj, punt_dp_db[ii])
617 s = format (s, "%d ", punt_dp_db[ii][jj]);
619 vlib_cli_output (vm, " [%v]", s);
627 VLIB_CLI_COMMAND (punt_db_show_command, static) =
629 .path = "show punt db",
630 .short_help = "show the punt DB",
631 .function = punt_db_show,
635 static clib_error_t *
636 punt_stats_show (vlib_main_t * vm,
637 unformat_input_t * input, vlib_cli_command_t * cmd)
639 vlib_combined_counter_main_t *cm = &punt_counters;
643 for (ii = 0; ii < vlib_combined_counter_n_counters (cm); ii++)
645 vlib_get_combined_counter (cm, ii, &c);
646 vlib_cli_output (vm, "%U packets:%lld bytes:%lld",
647 format_vlib_punt_reason, ii, c.packets, c.bytes);
654 VLIB_CLI_COMMAND (punt_stats_show_command, static) =
656 .path = "show punt stats",
657 .short_help = "show the punt stats",
658 .function = punt_stats_show,
662 static clib_error_t *
663 punt_init (vlib_main_t * vm)
665 punt_client_db = hash_create_vec (0, sizeof (u8), sizeof (u32));
670 VLIB_INIT_FUNCTION (punt_init);
673 * fd.io coding-style-patch-verification: ON
676 * eval: (c-set-style "gnu")