Make bgpctl less depend on bgpd internals. Track the size of memory used by prefixes explicitly. This will allow to move the various pt_entry structs out of rde.h.
-- :wq Claudio Index: bgpctl/bgpctl.h =================================================================== RCS file: /cvs/src/usr.sbin/bgpctl/bgpctl.h,v retrieving revision 1.19 diff -u -p -r1.19 bgpctl.h --- bgpctl/bgpctl.h 24 Jan 2023 11:29:34 -0000 1.19 +++ bgpctl/bgpctl.h 28 Mar 2023 11:44:36 -0000 @@ -38,7 +38,6 @@ struct output { }; extern const struct output show_output, json_output, ometric_output; -extern const size_t pt_sizes[]; #define EOL0(flag) ((flag & F_CTL_SSV) ? ';' : '\n') Index: bgpctl/output.c =================================================================== RCS file: /cvs/src/usr.sbin/bgpctl/output.c,v retrieving revision 1.37 diff -u -p -r1.37 output.c --- bgpctl/output.c 9 Mar 2023 13:13:14 -0000 1.37 +++ bgpctl/output.c 28 Mar 2023 11:44:36 -0000 @@ -33,8 +33,6 @@ #include "bgpctl.h" #include "parser.h" -const size_t pt_sizes[AID_MAX] = AID_PTSIZE; - static void show_head(struct parse_result *res) { @@ -994,10 +992,10 @@ show_rib_mem(struct rde_memstats *stats) for (i = 0; i < AID_MAX; i++) { if (stats->pt_cnt[i] == 0) continue; - pts += stats->pt_cnt[i] * pt_sizes[i]; + pts += stats->pt_size[i]; printf("%10lld %s network entries using %s of memory\n", stats->pt_cnt[i], aid_vals[i].name, - fmt_mem(stats->pt_cnt[i] * pt_sizes[i])); + fmt_mem(stats->pt_size[i])); } printf("%10lld rib entries using %s of memory\n", stats->rib_cnt, fmt_mem(stats->rib_cnt * Index: bgpctl/output_json.c =================================================================== RCS file: /cvs/src/usr.sbin/bgpctl/output_json.c,v retrieving revision 1.30 diff -u -p -r1.30 output_json.c --- bgpctl/output_json.c 9 Mar 2023 13:13:14 -0000 1.30 +++ bgpctl/output_json.c 28 Mar 2023 11:44:36 -0000 @@ -939,9 +939,9 @@ json_rib_mem(struct rde_memstats *stats) for (i = 0; i < AID_MAX; i++) { if (stats->pt_cnt[i] == 0) continue; - pts += stats->pt_cnt[i] * pt_sizes[i]; + pts += stats->pt_size[i]; json_rib_mem_element(aid_vals[i].name, stats->pt_cnt[i], - stats->pt_cnt[i] * pt_sizes[i], UINT64_MAX); + stats->pt_size[i], UINT64_MAX); } json_rib_mem_element("rib", stats->rib_cnt, stats->rib_cnt * sizeof(struct rib_entry), UINT64_MAX); Index: bgpctl/output_ometric.c =================================================================== RCS file: /cvs/src/usr.sbin/bgpctl/output_ometric.c,v retrieving revision 1.10 diff -u -p -r1.10 output_ometric.c --- bgpctl/output_ometric.c 12 Dec 2022 09:51:04 -0000 1.10 +++ bgpctl/output_ometric.c 28 Mar 2023 11:44:36 -0000 @@ -275,9 +275,9 @@ ometric_rib_mem(struct rde_memstats *sta for (i = 0; i < AID_MAX; i++) { if (stats->pt_cnt[i] == 0) continue; - pts += stats->pt_cnt[i] * pt_sizes[i]; + pts += stats->pt_size[i]; ometric_rib_mem_element(aid_vals[i].name, stats->pt_cnt[i], - stats->pt_cnt[i] * pt_sizes[i], UINT64_MAX); + stats->pt_size[i], UINT64_MAX); } ometric_rib_mem_element("rib", stats->rib_cnt, stats->rib_cnt * sizeof(struct rib_entry), UINT64_MAX); Index: bgpd/bgpd.h =================================================================== RCS file: /cvs/src/usr.sbin/bgpd/bgpd.h,v retrieving revision 1.465 diff -u -p -r1.465 bgpd.h --- bgpd/bgpd.h 13 Mar 2023 16:52:41 -0000 1.465 +++ bgpd/bgpd.h 28 Mar 2023 11:44:12 -0000 @@ -1246,6 +1246,7 @@ struct rde_memstats { long long prefix_cnt; long long rib_cnt; long long pt_cnt[AID_MAX]; + long long pt_size[AID_MAX]; long long nexthop_cnt; long long aspath_cnt; long long aspath_size; Index: bgpd/rde_prefix.c =================================================================== RCS file: /cvs/src/usr.sbin/bgpd/rde_prefix.c,v retrieving revision 1.42 diff -u -p -r1.42 rde_prefix.c --- bgpd/rde_prefix.c 17 Aug 2022 15:15:26 -0000 1.42 +++ bgpd/rde_prefix.c 28 Mar 2023 11:44:12 -0000 @@ -310,6 +310,7 @@ pt_alloc(struct pt_entry *op) if (p == NULL) fatal("pt_alloc"); rdemem.pt_cnt[op->aid]++; + rdemem.pt_size[op->aid] += pt_sizes[op->aid]; memcpy(p, op, pt_sizes[op->aid]); return (p); @@ -319,5 +320,6 @@ static void pt_free(struct pt_entry *pte) { rdemem.pt_cnt[pte->aid]--; + rdemem.pt_size[pte->aid] -= pt_sizes[pte->aid]; free(pte); }