|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// Copyright (c) 2026 Fabien Dupont |
| 3 | + |
| 4 | +#include <gr_api.h> |
| 5 | +#include <gr_cli.h> |
| 6 | +#include <gr_cli_iface.h> |
| 7 | +#include <gr_l2.h> |
| 8 | + |
| 9 | +#include <ecoli.h> |
| 10 | + |
| 11 | +#include <errno.h> |
| 12 | +#include <stdio.h> |
| 13 | + |
| 14 | +static cmd_status_t bridge_stats_show(struct gr_api_client *c, const struct ec_pnode *p) { |
| 15 | + const struct gr_l2_bridge_stats *stats; |
| 16 | + struct gr_l2_stats_get_req req; |
| 17 | + void *resp_ptr = NULL; |
| 18 | + struct gr_iface *iface; |
| 19 | + |
| 20 | + iface = iface_from_name(c, arg_str(p, "NAME")); |
| 21 | + if (iface == NULL) |
| 22 | + return CMD_ERROR; |
| 23 | + |
| 24 | + req.bridge_id = iface->id; |
| 25 | + free(iface); |
| 26 | + |
| 27 | + if (gr_api_client_send_recv(c, GR_L2_STATS_GET, sizeof(req), &req, &resp_ptr) < 0) |
| 28 | + return CMD_ERROR; |
| 29 | + |
| 30 | + stats = resp_ptr; |
| 31 | + |
| 32 | + printf("forwarding:\n"); |
| 33 | + printf(" unicast: %lu\n", stats->unicast_fwd); |
| 34 | + printf(" broadcast: %lu\n", stats->broadcast_fwd); |
| 35 | + printf(" multicast: %lu\n", stats->multicast_fwd); |
| 36 | + printf(" flood: %lu\n", stats->flood_fwd); |
| 37 | + printf("drops:\n"); |
| 38 | + printf(" no_fdb: %lu\n", stats->no_fdb_drop); |
| 39 | + printf(" hairpin: %lu\n", stats->hairpin_drop); |
| 40 | + printf(" iface_down: %lu\n", stats->iface_down_drop); |
| 41 | + printf("learning:\n"); |
| 42 | + printf(" learned: %lu\n", stats->learn_ok); |
| 43 | + printf(" updated: %lu\n", stats->learn_update); |
| 44 | + printf(" failed: %lu\n", stats->learn_fail); |
| 45 | + printf(" skipped: %lu\n", stats->learn_skip); |
| 46 | + printf(" limit_bridge: %lu\n", stats->learn_limit_bridge); |
| 47 | + printf(" limit_iface: %lu\n", stats->learn_limit_iface); |
| 48 | + printf(" shutdown: %lu\n", stats->learn_shutdown); |
| 49 | + printf("fdb:\n"); |
| 50 | + printf(" lookup_hit: %lu\n", stats->fdb_lookup_hit); |
| 51 | + printf(" lookup_miss: %lu\n", stats->fdb_lookup_miss); |
| 52 | + printf(" entries_aged: %lu\n", stats->fdb_entries_aged); |
| 53 | + |
| 54 | + free(resp_ptr); |
| 55 | + return CMD_SUCCESS; |
| 56 | +} |
| 57 | + |
| 58 | +static cmd_status_t bridge_stats_reset(struct gr_api_client *c, const struct ec_pnode *p) { |
| 59 | + struct gr_l2_stats_reset_req req; |
| 60 | + struct gr_iface *iface; |
| 61 | + |
| 62 | + iface = iface_from_name(c, arg_str(p, "NAME")); |
| 63 | + if (iface == NULL) |
| 64 | + return CMD_ERROR; |
| 65 | + |
| 66 | + req.bridge_id = iface->id; |
| 67 | + free(iface); |
| 68 | + |
| 69 | + if (gr_api_client_send_recv(c, GR_L2_STATS_RESET, sizeof(req), &req, NULL) < 0) |
| 70 | + return CMD_ERROR; |
| 71 | + |
| 72 | + return CMD_SUCCESS; |
| 73 | +} |
| 74 | + |
| 75 | +#define BRIDGE_STATS_CTX(root) \ |
| 76 | + CLI_CONTEXT(root, CTX_ARG("bridge", "Bridge management."), CTX_ARG("stats", "Statistics.")) |
| 77 | + |
| 78 | +static int ctx_init(struct ec_node *root) { |
| 79 | + int ret; |
| 80 | + |
| 81 | + ret = CLI_COMMAND( |
| 82 | + BRIDGE_STATS_CTX(root), |
| 83 | + "reset NAME", |
| 84 | + bridge_stats_reset, |
| 85 | + "Reset bridge statistics.", |
| 86 | + with_help( |
| 87 | + "Bridge interface name.", |
| 88 | + ec_node_dyn("NAME", complete_iface_names, INT2PTR(GR_IFACE_TYPE_BRIDGE)) |
| 89 | + ) |
| 90 | + ); |
| 91 | + if (ret < 0) |
| 92 | + return ret; |
| 93 | + |
| 94 | + ret = CLI_COMMAND( |
| 95 | + BRIDGE_STATS_CTX(root), |
| 96 | + "[show] NAME", |
| 97 | + bridge_stats_show, |
| 98 | + "Show bridge statistics.", |
| 99 | + with_help( |
| 100 | + "Bridge interface name.", |
| 101 | + ec_node_dyn("NAME", complete_iface_names, INT2PTR(GR_IFACE_TYPE_BRIDGE)) |
| 102 | + ) |
| 103 | + ); |
| 104 | + if (ret < 0) |
| 105 | + return ret; |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | +static struct cli_context ctx = { |
| 111 | + .name = "bridge stats", |
| 112 | + .init = ctx_init, |
| 113 | +}; |
| 114 | + |
| 115 | +static void __attribute__((constructor, used)) init(void) { |
| 116 | + cli_context_register(&ctx); |
| 117 | +} |
0 commit comments