Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pbr_event_new cleanup #19

Merged
merged 2 commits into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 55 additions & 37 deletions lib/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,76 +104,77 @@ struct debug_callbacks {
*
* MT-Safe
*/
#define DEBUG_MODE_CHECK(name, type) \
CHECK_FLAG_ATOMIC(&(name)->flags, (type)&DEBUG_MODE_ALL)
#define DEBUG_MODE_CHECK(name, mode) \
CHECK_FLAG_ATOMIC(&(name)->flags, (mode)&DEBUG_MODE_ALL)

/*
* Check if an option bit is set for a debug.
*
* MT-Safe
*/
#define DEBUG_OPT_CHECK(name, type) \
CHECK_FLAG_ATOMIC(&(name)->flags, (type)&DEBUG_OPT_ALL)
#define DEBUG_OPT_CHECK(name, opt) \
CHECK_FLAG_ATOMIC(&(name)->flags, (opt)&DEBUG_OPT_ALL)

/*
* Check if bits are set for a debug.
*
* MT-Safe
*/
#define DEBUG_FLAGS_CHECK(name, type) CHECK_FLAG_ATOMIC(&(name)->flags, (type))

/*
* Check if any mode is on for a debug.
*
* MT-Safe
*/
#define DEBUG(name) DEBUG_MODE_CHECK((name), DEBUG_MODE_ALL)
#define DEBUG_FLAGS_CHECK(name, fl) CHECK_FLAG_ATOMIC(&(name)->flags, (fl))

/*
* Set modes on a debug.
*
* MT-Safe
*/
#define DEBUG_MODE_SET(name, type) \
SET_FLAG_ATOMIC(&(name)->flags, (type)&DEBUG_MODE_ALL)
#define DEBUG_MODE_SET(name, mode, onoff) \
do { \
if (onoff) \
SET_FLAG_ATOMIC(&(name)->flags, \
(mode)&DEBUG_MODE_ALL); \
else \
UNSET_FLAG_ATOMIC(&(name)->flags, \
(mode)&DEBUG_MODE_ALL); \
} while (0)

/*
* Unset modes on a debug.
*
* MT-Safe
*/
#define DEBUG_MODE_UNSET(name, type) \
UNSET_FLAG_ATOMIC(&(name)->flags, (type)&DEBUG_MODE_ALL)
/* Convenience macros for specific set operations. */
#define DEBUG_MODE_ON(name, mode) DEBUG_MODE_SET(name, mode, true)
#define DEBUG_MODE_OFF(name, mode) DEBUG_MODE_SET(name, mode, false)

/*
* Set options on a debug.
*
* MT-Safe
*/
#define DEBUG_OPT_SET(name, type) \
SET_FLAG_ATOMIC(&(name)->flags, (type)&DEBUG_OPT_ALL)
#define DEBUG_OPT_SET(name, opt, onoff) \
do { \
if (onoff) \
SET_FLAG_ATOMIC(&(name)->flags, (opt)&DEBUG_OPT_ALL); \
else \
UNSET_FLAG_ATOMIC(&(name)->flags, \
(opt)&DEBUG_OPT_ALL); \
} while (0)

/*
* Unset options on a debug.
*
* MT-Safe
*/
#define DEBUG_OPT_UNSET(name, type) \
UNSET_FLAG_ATOMIC(&(name)->flags, (type)&DEBUG_OPT_ALL)
/* Convenience macros for specific set operations. */
#define DEBUG_OPT_ON(name, opt) DEBUG_OPT_SET(name, opt, true)
#define DEBUG_OPT_OFF(name, opt) DEBUG_OPT_SET(name, opt, true)

/*
* Set bits on a debug.
*
* MT-Safe
*/
#define DEBUG_FLAGS_SET(name, type) SET_FLAG_ATOMIC(&(name)->flags, (type))
#define DEBUG_FLAGS_SET(name, fl, onoff) \
do { \
if (onoff) \
SET_FLAG_ATOMIC(&(name)->flags, (fl)); \
else \
UNSET_FLAG_ATOMIC(&(name)->flags, (fl)); \
} while (0)

/*
* Unset bits on a debug.
*
* MT-Safe
*/
#define DEBUG_FLAGS_UNSET(name, type) UNSET_FLAG_ATOMIC(&(name)->flags, (type))
/* Convenience macros for specific set operations. */
#define DEBUG_FLAGS_ON(name, fl) DEBUG_FLAGS_SET(&(name)->flags, (type), true)
#define DEBUG_FLAGS_OFF(name, fl) DEBUG_FLAGS_SET(&(name)->flags, (type), false)

/*
* Unset all modes and options on a debug.
Expand Down Expand Up @@ -201,6 +202,23 @@ struct debug_callbacks {
#define DEBUG_NODE2MODE(vtynode) \
(((vtynode) == CONFIG_NODE) ? DEBUG_MODE_ALL : DEBUG_MODE_TERM)

/*
* Debug at the given level to the default logging destination.
*
* MT-Safe
*/
#define DEBUG(level, name, fmt, ...) \
do { \
if (DEBUG_MODE_CHECK(name, DEBUG_MODE_ALL)) \
zlog_##level(fmt, ##__VA_ARGS__); \
} while (0)

/* Convenience macros for the various levels. */
#define DEBUGE(name, fmt, ...) DEBUG(err, name, fmt, ##__VA_ARGS__)
#define DEBUGW(name, fmt, ...) DEBUG(warn, name, fmt, ##__VA_ARGS__)
#define DEBUGI(name, fmt, ...) DEBUG(info, name, fmt, ##__VA_ARGS__)
#define DEBUGN(name, fmt, ...) DEBUG(notice, name, fmt, ##__VA_ARGS__)
#define DEBUGD(name, fmt, ...) DEBUG(debug, name, fmt, ##__VA_ARGS__)

/*
* Optional initializer for debugging. Highly recommended.
Expand Down
9 changes: 7 additions & 2 deletions pbrd/pbr_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,14 @@ void pbr_event_enqueue(struct pbr_event *pbre)
work_queue_add(pbr_event_wq, pbre);
}

struct pbr_event *pbr_event_new(void)
struct pbr_event *pbr_event_new(enum pbr_events ev, const char *name)
{
return XCALLOC(MTYPE_PBR_EVENT, sizeof(struct pbr_event));
struct pbr_event *event;
event = XCALLOC(MTYPE_PBR_EVENT, sizeof(struct pbr_event));
event->event = ev;
if (name)
strlcpy(event->name, name, sizeof(event->name));
return event;
}

extern struct thread_master *master;
Expand Down
2 changes: 1 addition & 1 deletion pbrd/pbr_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ struct pbr_event {
* Return a event structure that can be filled in and enqueued.
* Assume this memory is owned by the event subsystem.
*/
extern struct pbr_event *pbr_event_new(void);
extern struct pbr_event *pbr_event_new(enum pbr_events ev, const char *name);

/*
* Free the associated pbr_event item
Expand Down
35 changes: 12 additions & 23 deletions pbrd/pbr_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ void pbr_map_interface_delete(struct pbr_map *pbrm, struct interface *ifp_del)
if (pmi) {
pmi->delete = true;

pbre = pbr_event_new();
pbre->event = PBR_POLICY_DELETED;
strcpy(pbre->name, pmi->ifp->name);
pbre = pbr_event_new(PBR_POLICY_DELETED, pmi->ifp->name);
pbr_event_enqueue(pbre);
}
}
Expand All @@ -151,9 +149,7 @@ void pbr_map_add_interface(struct pbr_map *pbrm, struct interface *ifp_add)
pmi->pbrm = pbrm;
listnode_add_sort(pbrm->incoming, pmi);

pbre = pbr_event_new();
pbre->event = PBR_POLICY_CHANGED;
strcpy(pbre->name, pbrm->name);
pbre = pbr_event_new(PBR_POLICY_CHANGED, pbrm->name);
pbr_event_enqueue(pbre);
}

Expand Down Expand Up @@ -258,9 +254,7 @@ extern struct pbr_map_sequence *pbrms_get(const char *name, uint32_t seqno)

RB_INSERT(pbr_map_entry_head, &pbr_maps, pbrm);

pbre = pbr_event_new();
pbre->event = PBR_MAP_ADD;
strlcpy(pbre->name, name, sizeof(pbre->name));
pbre = pbr_event_new(PBR_MAP_ADD, name);
} else
pbre = NULL;

Expand Down Expand Up @@ -392,9 +386,8 @@ extern void pbr_map_schedule_policy_from_nhg(const char *nh_group)
&& (strcmp(nh_group, pbrms->nhgrp_name) == 0)) {
pbrms->nhs_installed = true;

pbre = pbr_event_new();
pbre->event = PBR_MAP_MODIFY;
strcpy(pbre->name, pbrm->name);
pbre = pbr_event_new(PBR_MAP_MODIFY,
pbrm->name);
pbre->seqno = pbrms->seqno;

pbr_event_enqueue(pbre);
Expand All @@ -405,9 +398,8 @@ extern void pbr_map_schedule_policy_from_nhg(const char *nh_group)
== 0)) {
pbrms->nhs_installed = true;

pbre = pbr_event_new();
pbre->event = PBR_MAP_MODIFY;
strcpy(pbre->name, pbrm->name);
pbre = pbr_event_new(PBR_MAP_MODIFY,
pbrm->name);
pbre->seqno = pbrms->seqno;

pbr_event_enqueue(pbre);
Expand Down Expand Up @@ -498,10 +490,8 @@ extern void pbr_map_check_nh_group_change(const char *nh_group)
if (original != pbrm->valid) {
struct pbr_event *pbre;

pbre = pbr_event_new();
pbre->event = PBR_MAP_INSTALL;
strcpy(pbre->name, pbrm->name);

pbre = pbr_event_new(PBR_MAP_INSTALL,
pbrm->name);
pbr_event_enqueue(pbre);
}
break;
Expand Down Expand Up @@ -534,7 +524,8 @@ extern void pbr_map_check(const char *name, uint32_t seqno)
zlog_debug("%s: Installing %s(%u) reason: %" PRIu64,
__PRETTY_FUNCTION__, name, seqno,
pbrms->reason);
pbre = pbr_event_new();
pbre = pbr_event_new(PBR_MAP_POLICY_INSTALL,
pbrm->name);
pbre->event = PBR_MAP_POLICY_INSTALL;
strcpy(pbre->name, pbrm->name);

Expand Down Expand Up @@ -610,9 +601,7 @@ extern void pbr_map_check_policy_change(const char *name)
if (pbrm->valid && !pbrm->installed) {
struct pbr_event *pbre;

pbre = pbr_event_new();
pbre->event = PBR_MAP_INSTALL;
strcpy(pbre->name, name);
pbre = pbr_event_new(PBR_MAP_INSTALL, name);

pbr_event_enqueue(pbre);
}
Expand Down
24 changes: 5 additions & 19 deletions pbrd/pbr_nht.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ void pbr_nhgroup_add_cb(const char *name)
{
struct pbr_event *pbre;

pbre = pbr_event_new();

pbre->event = PBR_NHG_NEW;
strlcpy(pbre->name, name, sizeof(pbre->name));
pbre = pbr_event_new(PBR_NHG_NEW, name);

pbr_event_enqueue(pbre);
zlog_debug("Received ADD cb for %s", name);
Expand All @@ -134,10 +131,7 @@ void pbr_nhgroup_add_nexthop_cb(const char *name)
{
struct pbr_event *pbre;

pbre = pbr_event_new();

pbre->event = PBR_NHG_ADD_NEXTHOP;
strlcpy(pbre->name, name, sizeof(pbre->name));
pbre = pbr_event_new(PBR_NHG_ADD_NEXTHOP, name);

pbr_event_enqueue(pbre);
zlog_debug("Received NEXTHOP_ADD cb for %s", name);
Expand All @@ -147,10 +141,7 @@ void pbr_nhgroup_del_nexthop_cb(const char *name)
{
struct pbr_event *pbre;

pbre = pbr_event_new();

pbre->event = PBR_NHG_DEL_NEXTHOP;
strlcpy(pbre->name, name, sizeof(pbre->name));
pbre = pbr_event_new(PBR_NHG_DEL_NEXTHOP, name);

pbr_event_enqueue(pbre);
zlog_debug("Received NEXTHOP_DEL cb for %s", name);
Expand All @@ -160,10 +151,7 @@ void pbr_nhgroup_delete_cb(const char *name)
{
struct pbr_event *pbre;

pbre = pbr_event_new();

pbre->event = PBR_NHG_DELETE;
strlcpy(pbre->name, name, sizeof(pbre->name));
pbre = pbr_event_new(PBR_NHG_DELETE, name);

pbr_event_enqueue(pbre);
zlog_debug("Received DELETE cb for %s", name);
Expand Down Expand Up @@ -471,9 +459,7 @@ static void pbr_nht_individual_nexthop_update_lookup(struct hash_backet *b,
if (old_valid != pnhc->valid) {
struct pbr_event *pbre;

pbre = pbr_event_new();
pbre->event = PBR_NH_CHANGED;
strcpy(pbre->name, pnhc->parent->name);
pbre = pbr_event_new(PBR_NH_CHANGED, pnhc->parent->name);

pbr_event_enqueue(pbre);
}
Expand Down
Loading