Skip to content

Commit

Permalink
bgpd: peer hash expands until we are out of memory
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Walton <dwalton@cumulusnetworks.com>
Reviewed-by:   CCR-6531

Ticket: CM-17352
  • Loading branch information
Daniel Walton committed Jul 31, 2017
1 parent ee19fe2 commit 4709b4f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions bgpd/bgpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ static unsigned int peer_hash_key_make(void *p)
return sockunion_hash(&peer->su);
}

static int peer_hash_cmp(const void *p1, const void *p2)
static int peer_hash_same(const void *p1, const void *p2)
{
const struct peer *peer1 = p1;
const struct peer *peer2 = p2;
Expand Down Expand Up @@ -2758,7 +2758,8 @@ static struct bgp *bgp_create(as_t *as, const char *name,
XSTRDUP(MTYPE_BGP_PEER_HOST, "Static announcement");
bgp->peer = list_new();
bgp->peer->cmp = (int (*)(void *, void *))peer_cmp;
bgp->peerhash = hash_create(peer_hash_key_make, peer_hash_cmp, NULL);
bgp->peerhash = hash_create(peer_hash_key_make, peer_hash_same, NULL);
bgp->peerhash->max_size = BGP_PEER_MAX_HASH_SIZE;

bgp->group = list_new();
bgp->group->cmp = (int (*)(void *, void *))peer_group_cmp;
Expand Down
1 change: 1 addition & 0 deletions bgpd/bgpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "bitfield.h"

#define BGP_MAX_HOSTNAME 64 /* Linux max, is larger than most other sys */
#define BGP_PEER_MAX_HASH_SIZE 16384

/* Default interval for IPv6 RAs when triggered by BGP unnumbered neighbor. */
#define BGP_UNNUM_DEFAULT_RA_INTERVAL 10
Expand Down
4 changes: 4 additions & 0 deletions lib/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ static void hash_expand(struct hash *hash)
struct hash_backet *hb, *hbnext, **new_index;

new_size = hash->size * 2;

if (hash->max_size && new_size > hash->max_size)
return;

new_index = XCALLOC(MTYPE_HASH_INDEX,
sizeof(struct hash_backet *) * new_size);
if (new_index == NULL)
Expand Down
3 changes: 3 additions & 0 deletions lib/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ struct hash {
/* Hash table size. Must be power of 2 */
unsigned int size;

/* If max_size is 0 there is no limit */
unsigned int max_size;

/* Key make function. */
unsigned int (*hash_key)(void *);

Expand Down

0 comments on commit 4709b4f

Please sign in to comment.