From 064aed4897766db296e0e27ace4dfa2929586f8f Mon Sep 17 00:00:00 2001 From: claudio Date: Thu, 30 Mar 2023 12:11:18 +0000 Subject: [PATCH] Put the size of the pt_entry object into the struct itself. Increase the refcnt to a 32bit int and while there reorder the vpn specific structs a bit so the IPv4 and IPv6 types are more equal. OK tb@ --- usr.sbin/bgpd/rde.h | 5 ++-- usr.sbin/bgpd/rde_prefix.c | 54 ++++++++++++++++++++------------------ 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/usr.sbin/bgpd/rde.h b/usr.sbin/bgpd/rde.h index cd33d5f2b1b..8999272df78 100644 --- a/usr.sbin/bgpd/rde.h +++ b/usr.sbin/bgpd/rde.h @@ -1,4 +1,4 @@ -/* $OpenBSD: rde.h,v 1.289 2023/03/29 10:46:11 claudio Exp $ */ +/* $OpenBSD: rde.h,v 1.290 2023/03/30 12:11:18 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Claudio Jeker and @@ -265,7 +265,8 @@ struct pt_entry { RB_ENTRY(pt_entry) pt_e; uint8_t aid; uint8_t prefixlen; - uint16_t refcnt; + uint16_t len; + uint32_t refcnt; uint8_t data[4]; /* data depending on aid */ }; diff --git a/usr.sbin/bgpd/rde_prefix.c b/usr.sbin/bgpd/rde_prefix.c index c17541d1611..17b6beddd10 100644 --- a/usr.sbin/bgpd/rde_prefix.c +++ b/usr.sbin/bgpd/rde_prefix.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde_prefix.c,v 1.46 2023/03/28 17:47:29 claudio Exp $ */ +/* $OpenBSD: rde_prefix.c,v 1.47 2023/03/30 12:11:18 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Claudio Jeker @@ -21,7 +21,7 @@ #include #include -#include +#include #include #include @@ -46,14 +46,15 @@ */ /* internal prototypes */ -static struct pt_entry *pt_alloc(struct pt_entry *); +static struct pt_entry *pt_alloc(struct pt_entry *, int len); static void pt_free(struct pt_entry *); struct pt_entry4 { RB_ENTRY(pt_entry) pt_e; uint8_t aid; uint8_t prefixlen; - uint16_t refcnt; + uint16_t len; + uint32_t refcnt; struct in_addr prefix4; }; @@ -61,7 +62,8 @@ struct pt_entry6 { RB_ENTRY(pt_entry) pt_e; uint8_t aid; uint8_t prefixlen; - uint16_t refcnt; + uint16_t len; + uint32_t refcnt; struct in6_addr prefix6; }; @@ -69,9 +71,10 @@ struct pt_entry_vpn4 { RB_ENTRY(pt_entry) pt_e; uint8_t aid; uint8_t prefixlen; - uint16_t refcnt; - struct in_addr prefix4; + uint16_t len; + uint32_t refcnt; uint64_t rd; + struct in_addr prefix4; uint8_t labelstack[21]; uint8_t labellen; uint8_t pad1; @@ -82,22 +85,16 @@ struct pt_entry_vpn6 { RB_ENTRY(pt_entry) pt_e; uint8_t aid; uint8_t prefixlen; - uint16_t refcnt; - struct in6_addr prefix6; + uint16_t len; + uint32_t refcnt; uint64_t rd; + struct in6_addr prefix6; uint8_t labelstack[21]; uint8_t labellen; uint8_t pad1; uint8_t pad2; }; -size_t pt_sizes[AID_MAX] = { - 0, - sizeof(struct pt_entry4), - sizeof(struct pt_entry6), - sizeof(struct pt_entry_vpn4), - sizeof(struct pt_entry_vpn6) -}; RB_HEAD(pt_tree, pt_entry); RB_PROTOTYPE(pt_tree, pt_entry, pt_e, pt_prefix_cmp); @@ -163,7 +160,8 @@ pt_fill(struct bgpd_addr *prefix, int prefixlen) switch (prefix->aid) { case AID_INET: memset(&pte4, 0, sizeof(pte4)); - pte4.refcnt = USHRT_MAX; + pte4.len = sizeof(pte4); + pte4.refcnt = UINT32_MAX; pte4.aid = prefix->aid; if (prefixlen > 32) fatalx("pt_fill: bad IPv4 prefixlen"); @@ -172,7 +170,8 @@ pt_fill(struct bgpd_addr *prefix, int prefixlen) return ((struct pt_entry *)&pte4); case AID_INET6: memset(&pte6, 0, sizeof(pte6)); - pte6.refcnt = USHRT_MAX; + pte6.len = sizeof(pte6); + pte6.refcnt = UINT32_MAX; pte6.aid = prefix->aid; if (prefixlen > 128) fatalx("pt_fill: bad IPv6 prefixlen"); @@ -181,7 +180,8 @@ pt_fill(struct bgpd_addr *prefix, int prefixlen) return ((struct pt_entry *)&pte6); case AID_VPN_IPv4: memset(&pte_vpn4, 0, sizeof(pte_vpn4)); - pte_vpn4.refcnt = USHRT_MAX; + pte_vpn4.len = sizeof(pte_vpn4); + pte_vpn4.refcnt = UINT32_MAX; pte_vpn4.aid = prefix->aid; if (prefixlen > 32) fatalx("pt_fill: bad IPv4 prefixlen"); @@ -194,7 +194,8 @@ pt_fill(struct bgpd_addr *prefix, int prefixlen) return ((struct pt_entry *)&pte_vpn4); case AID_VPN_IPv6: memset(&pte_vpn6, 0, sizeof(pte_vpn6)); - pte_vpn6.refcnt = USHRT_MAX; + pte_vpn6.len = sizeof(pte_vpn6); + pte_vpn6.refcnt = UINT32_MAX; pte_vpn6.aid = prefix->aid; if (prefixlen > 128) fatalx("pt_get: bad IPv6 prefixlen"); @@ -224,7 +225,8 @@ pt_add(struct bgpd_addr *prefix, int prefixlen) { struct pt_entry *p = NULL; - p = pt_alloc(pt_fill(prefix, prefixlen)); + p = pt_fill(prefix, prefixlen); + p = pt_alloc(p, p->len); if (RB_INSERT(pt_tree, &pttable, p) != NULL) fatalx("pt_add: insert failed"); @@ -355,16 +357,16 @@ pt_prefix_cmp(const struct pt_entry *a, const struct pt_entry *b) * Function may not return on failure. */ static struct pt_entry * -pt_alloc(struct pt_entry *op) +pt_alloc(struct pt_entry *op, int len) { struct pt_entry *p; - p = malloc(pt_sizes[op->aid]); + p = malloc(len); 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]); + rdemem.pt_size[op->aid] += len; + memcpy(p, op, len); p->refcnt = 0; return (p); @@ -374,7 +376,7 @@ static void pt_free(struct pt_entry *pte) { rdemem.pt_cnt[pte->aid]--; - rdemem.pt_size[pte->aid] -= pt_sizes[pte->aid]; + rdemem.pt_size[pte->aid] -= pte->len; free(pte); } -- 2.20.1