Skip to content

Commit

Permalink
bfdd: add memory allocation registration
Browse files Browse the repository at this point in the history
Count all memory allocations so we can detect memory leaks more easily.

Signed-off-by: Rafael Zalamena <rzalamena@opensourcerouting.org>
  • Loading branch information
rzalamena committed Jun 4, 2018
1 parent e208e2f commit c4b2766
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 32 deletions.
4 changes: 2 additions & 2 deletions bfdd/bfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ static bfd_session *bfd_session_new(int sd)
{
bfd_session *bs;

bs = calloc(1, sizeof(*bs));
bs = XCALLOC(MTYPE_BFDD_CONFIG, sizeof(*bs));
if (bs == NULL)
return NULL;

Expand Down Expand Up @@ -705,7 +705,7 @@ static void bfd_session_free(bfd_session *bs)
pl_free(bs->pl);

QOBJ_UNREG(bs);
free(bs);
XFREE(MTYPE_BFDD_CONFIG, bs);
}

bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc)
Expand Down
15 changes: 11 additions & 4 deletions bfdd/bfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
#define BFDD_JSON_CONV_OPTIONS (0)
#endif

DECLARE_MGROUP(BFDD);
DECLARE_MTYPE(BFDD_TMP);
DECLARE_MTYPE(BFDD_CONFIG);
DECLARE_MTYPE(BFDD_LABEL);
DECLARE_MTYPE(BFDD_CONTROL);
DECLARE_MTYPE(BFDD_NOTIFICATION);

typedef struct bfd_timers {
uint32_t desired_min_tx;
uint32_t required_min_rx;
Expand Down Expand Up @@ -489,10 +496,10 @@ void log_error(const char *fmt, ...);
void log_fatal(const char *fmt, ...);

/* Compatibility code: code to avoid touching ported code debug messages. */
#define DLOG(fmt, args...) log_debug(fmt "\n", ##args)
#define INFOLOG(fmt, args...) log_info(fmt "\n", ##args)
#define ERRLOG(fmt, args...) log_error(fmt "\n", ##args)
#define CRITLOG(fmt, args...) log_fatal(fmt "\n", ##args)
#define DLOG(fmt, args...) log_debug(fmt, ##args)
#define INFOLOG(fmt, args...) log_info(fmt, ##args)
#define ERRLOG(fmt, args...) log_error(fmt, ##args)
#define CRITLOG(fmt, args...) log_fatal(fmt, ##args)

/*
* bfd_packet.c
Expand Down
7 changes: 7 additions & 0 deletions bfdd/bfdd.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
/*
* FRR related code.
*/
DEFINE_MGROUP(BFDD, "Bidirectional Forwarding Detection Daemon");
DEFINE_MTYPE(BFDD, BFDD_TMP, "short-lived temporary memory");
DEFINE_MTYPE(BFDD, BFDD_CONFIG, "long-lived configuration memory");
DEFINE_MTYPE(BFDD, BFDD_LABEL, "long-lived label memory");
DEFINE_MTYPE(BFDD, BFDD_CONTROL, "long-lived control socket memory");
DEFINE_MTYPE(BFDD, BFDD_NOTIFICATION, "short-lived control notification data");

/* Master of threads. */
struct thread_master *master;

Expand Down
4 changes: 2 additions & 2 deletions bfdd/bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ uint16_t udp4_checksum(struct ip *ip, uint8_t *buf, int len)
pudp_hdr.protocol = ip->ip_p;
pudp_hdr.len = htons(len);

ptr = malloc(UDP_PSUEDO_HDR_LEN + len);
ptr = XMALLOC(MTYPE_BFDD_TMP, UDP_PSUEDO_HDR_LEN + len);
memcpy(ptr, &pudp_hdr, UDP_PSUEDO_HDR_LEN);
memcpy(ptr + UDP_PSUEDO_HDR_LEN, buf, len);

csum = checksum((uint16_t *)ptr, UDP_PSUEDO_HDR_LEN + len);
free(ptr);
XFREE(MTYPE_BFDD_TMP, ptr);
return csum;
}

Expand Down
13 changes: 8 additions & 5 deletions bfdd/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ char *config_response(const char *status, const char *error)
}

/* Generate JSON response. */
jsonstr = strdup(
jsonstr = XSTRDUP(
MTYPE_BFDD_NOTIFICATION,
json_object_to_json_string_ext(resp, BFDD_JSON_CONV_OPTIONS));
json_object_put(resp);

Expand Down Expand Up @@ -443,7 +444,8 @@ char *config_notify(bfd_session *bs)
json_object_add_int(resp, "remote-diagnostics", bs->remote_diag);

/* Generate JSON response. */
jsonstr = strdup(
jsonstr = XSTRDUP(
MTYPE_BFDD_NOTIFICATION,
json_object_to_json_string_ext(resp, BFDD_JSON_CONV_OPTIONS));
json_object_put(resp);

Expand Down Expand Up @@ -490,7 +492,8 @@ char *config_notify_config(const char *op, bfd_session *bs)

skip_config:
/* Generate JSON response. */
jsonstr = strdup(
jsonstr = XSTRDUP(
MTYPE_BFDD_NOTIFICATION,
json_object_to_json_string_ext(resp, BFDD_JSON_CONV_OPTIONS));
json_object_put(resp);

Expand Down Expand Up @@ -563,7 +566,7 @@ struct peer_label *pl_new(const char *label, bfd_session *bs)
{
struct peer_label *pl;

pl = calloc(1, sizeof(*pl));
pl = XCALLOC(MTYPE_BFDD_LABEL, sizeof(*pl));
if (pl == NULL)
return NULL;

Expand All @@ -588,5 +591,5 @@ void pl_free(struct peer_label *pl)
pl->pl_bs->pl = NULL;

TAILQ_REMOVE(&bglobal.bg_pllist, pl, pl_entry);
free(pl);
XFREE(MTYPE_BFDD_LABEL, pl);
}
38 changes: 21 additions & 17 deletions bfdd/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ struct bfd_control_socket *control_new(int sd)
{
struct bfd_control_socket *bcs;

bcs = calloc(1, sizeof(*bcs));
bcs = XCALLOC(MTYPE_BFDD_CONTROL, sizeof(*bcs));
if (bcs == NULL)
return NULL;

Expand Down Expand Up @@ -224,7 +224,7 @@ static void control_free(struct bfd_control_socket *bcs)
}

control_reset_buf(&bcs->bcs_bin);
free(bcs);
XFREE(MTYPE_BFDD_CONTROL, bcs);
}

struct bfd_notify_peer *control_notifypeer_new(struct bfd_control_socket *bcs,
Expand All @@ -236,7 +236,7 @@ struct bfd_notify_peer *control_notifypeer_new(struct bfd_control_socket *bcs,
if (bnp)
return bnp;

bnp = calloc(1, sizeof(*bnp));
bnp = XCALLOC(MTYPE_BFDD_CONTROL, sizeof(*bnp));
if (bnp == NULL) {
log_warning("%s: calloc: %s", __func__, strerror(errno));
return NULL;
Expand All @@ -254,7 +254,7 @@ static void control_notifypeer_free(struct bfd_control_socket *bcs,
{
TAILQ_REMOVE(&bcs->bcs_bnplist, bnp, bnp_entry);
bnp->bnp_bs->refcount--;
free(bnp);
XFREE(MTYPE_BFDD_CONTROL, bnp);
}

struct bfd_notify_peer *control_notifypeer_find(struct bfd_control_socket *bcs,
Expand All @@ -274,7 +274,7 @@ struct bfd_control_queue *control_queue_new(struct bfd_control_socket *bcs)
{
struct bfd_control_queue *bcq;

bcq = calloc(1, sizeof(*bcq));
bcq = XCALLOC(MTYPE_BFDD_NOTIFICATION, sizeof(*bcq));
if (bcq == NULL) {
log_warning("%s: calloc: %s", __func__, strerror(errno));
return NULL;
Expand All @@ -291,7 +291,7 @@ static void control_queue_free(struct bfd_control_socket *bcs,
{
control_reset_buf(&bcq->bcq_bcb);
TAILQ_REMOVE(&bcs->bcs_bcqueue, bcq, bcq_entry);
free(bcq);
XFREE(MTYPE_BFDD_NOTIFICATION, bcq);
}

static int control_queue_dequeue(struct bfd_control_socket *bcs)
Expand Down Expand Up @@ -406,7 +406,7 @@ static int control_queue_enqueue_first(struct bfd_control_socket *bcs,
static void control_reset_buf(struct bfd_control_buffer *bcb)
{
/* Get ride of old data. */
free(bcb->bcb_buf);
XFREE(MTYPE_BFDD_NOTIFICATION, bcb->bcb_buf);
bcb->bcb_buf = NULL;
bcb->bcb_pos = 0;
bcb->bcb_left = 0;
Expand Down Expand Up @@ -468,7 +468,8 @@ static int control_read(struct thread *t)

bcb->bcb_pos = sizeof(bcm);
bcb->bcb_left = plen;
bcb->bcb_buf = malloc(sizeof(bcm) + bcb->bcb_left + 1);
bcb->bcb_buf = XMALLOC(MTYPE_BFDD_NOTIFICATION,
sizeof(bcm) + bcb->bcb_left + 1);
if (bcb->bcb_buf == NULL) {
log_warning("%s: not enough memory for message size: %u",
__func__, bcb->bcb_left);
Expand Down Expand Up @@ -759,10 +760,11 @@ static void control_response(struct bfd_control_socket *bcs, uint16_t id,

/* Allocate data and answer. */
jsonstrlen = strlen(jsonstr);
bcm = malloc(sizeof(struct bfd_control_msg) + jsonstrlen);
bcm = XMALLOC(MTYPE_BFDD_NOTIFICATION,
sizeof(struct bfd_control_msg) + jsonstrlen);
if (bcm == NULL) {
log_warning("%s: malloc: %s", __func__, strerror(errno));
free(jsonstr);
XFREE(MTYPE_BFDD_NOTIFICATION, jsonstr);
return;
}

Expand All @@ -771,7 +773,7 @@ static void control_response(struct bfd_control_socket *bcs, uint16_t id,
bcm->bcm_type = BMT_RESPONSE;
bcm->bcm_id = id;
memcpy(bcm->bcm_data, jsonstr, jsonstrlen);
free(jsonstr);
XFREE(MTYPE_BFDD_NOTIFICATION, jsonstr);

control_queue_enqueue_first(bcs, bcm);
}
Expand All @@ -792,10 +794,11 @@ static void _control_notify(struct bfd_control_socket *bcs, bfd_session *bs)

/* Allocate data and answer. */
jsonstrlen = strlen(jsonstr);
bcm = malloc(sizeof(struct bfd_control_msg) + jsonstrlen);
bcm = XMALLOC(MTYPE_BFDD_NOTIFICATION,
sizeof(struct bfd_control_msg) + jsonstrlen);
if (bcm == NULL) {
log_warning("%s: malloc: %s", __func__, strerror(errno));
free(jsonstr);
XFREE(MTYPE_BFDD_NOTIFICATION, jsonstr);
return;
}

Expand All @@ -804,7 +807,7 @@ static void _control_notify(struct bfd_control_socket *bcs, bfd_session *bs)
bcm->bcm_type = BMT_NOTIFY;
bcm->bcm_id = htons(BCM_NOTIFY_ID);
memcpy(bcm->bcm_data, jsonstr, jsonstrlen);
free(jsonstr);
XFREE(MTYPE_BFDD_NOTIFICATION, jsonstr);

control_queue_enqueue(bcs, bcm);
}
Expand Down Expand Up @@ -857,10 +860,11 @@ static void _control_notify_config(struct bfd_control_socket *bcs,

/* Allocate data and answer. */
jsonstrlen = strlen(jsonstr);
bcm = malloc(sizeof(struct bfd_control_msg) + jsonstrlen);
bcm = XMALLOC(MTYPE_BFDD_NOTIFICATION,
sizeof(struct bfd_control_msg) + jsonstrlen);
if (bcm == NULL) {
log_warning("%s: malloc: %s", __func__, strerror(errno));
free(jsonstr);
XFREE(MTYPE_BFDD_NOTIFICATION, jsonstr);
return;
}

Expand All @@ -869,7 +873,7 @@ static void _control_notify_config(struct bfd_control_socket *bcs,
bcm->bcm_type = BMT_NOTIFY;
bcm->bcm_id = htons(BCM_NOTIFY_ID);
memcpy(bcm->bcm_data, jsonstr, jsonstrlen);
free(jsonstr);
XFREE(MTYPE_BFDD_NOTIFICATION, jsonstr);

control_queue_enqueue(bcs, bcm);
}
Expand Down
4 changes: 2 additions & 2 deletions bfdd/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ uint16_t udp4_checksum(struct iphdr *iph, uint8_t *buf, int len)
pudp_hdr.protocol = iph->protocol;
pudp_hdr.len = htons(len);

ptr = malloc(UDP_PSUEDO_HDR_LEN + len);
ptr = XMALLOC(MTYPE_BFDD_TMP, UDP_PSUEDO_HDR_LEN + len);
memcpy(ptr, &pudp_hdr, UDP_PSUEDO_HDR_LEN);
memcpy(ptr + UDP_PSUEDO_HDR_LEN, buf, len);

csum = checksum((uint16_t *)ptr, UDP_PSUEDO_HDR_LEN + len);
free(ptr);
XFREE(MTYPE_BFDD_TMP, ptr);
return csum;
}

Expand Down

0 comments on commit c4b2766

Please sign in to comment.