lib/zebra: remove code duplication in redist_del_instance()

Change redist_check_instance() to return a pointer instead of returning 1
on success. This way this function can be reused in redist_del_instance()
instead of duplicating the same logic there.

Also, remove unnecessary call to redist_check_instance() in
zebra_redistribute_delete().

While here, remove unnecessary cast from void* in redist_add_instance().

Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
This commit is contained in:
Renato Westphal 2016-10-13 13:06:10 -03:00 committed by Donald Sharp
parent 4e1cadf011
commit 43e7c3b421
3 changed files with 15 additions and 25 deletions

View File

@ -80,20 +80,20 @@ zclient_free (struct zclient *zclient)
XFREE (MTYPE_ZCLIENT, zclient); XFREE (MTYPE_ZCLIENT, zclient);
} }
int u_short *
redist_check_instance (struct redist_proto *red, u_short instance) redist_check_instance (struct redist_proto *red, u_short instance)
{ {
struct listnode *node; struct listnode *node;
u_short *id; u_short *id;
if (!red->instances) if (!red->instances)
return 0; return NULL;
for (ALL_LIST_ELEMENTS_RO (red->instances, node, id)) for (ALL_LIST_ELEMENTS_RO (red->instances, node, id))
if (*id == instance) if (*id == instance)
return 1; return id;
return 0; return NULL;
} }
void void
@ -106,7 +106,7 @@ redist_add_instance (struct redist_proto *red, u_short instance)
if (!red->instances) if (!red->instances)
red->instances = list_new(); red->instances = list_new();
in = (u_short *)calloc(1, sizeof(u_short)); in = calloc (1, sizeof(u_short));
*in = instance; *in = instance;
listnode_add (red->instances, in); listnode_add (red->instances, in);
} }
@ -114,18 +114,12 @@ redist_add_instance (struct redist_proto *red, u_short instance)
void void
redist_del_instance (struct redist_proto *red, u_short instance) redist_del_instance (struct redist_proto *red, u_short instance)
{ {
struct listnode *node; u_short *id;
u_short *id = NULL;
if (!red->instances) id = redist_check_instance (red, instance);
if (! id)
return; return;
for (ALL_LIST_ELEMENTS_RO (red->instances, node, id))
if (*id == instance)
break;
if (id)
{
listnode_delete(red->instances, id); listnode_delete(red->instances, id);
if (!red->instances->count) if (!red->instances->count)
{ {
@ -134,7 +128,6 @@ redist_del_instance (struct redist_proto *red, u_short instance)
red->instances = NULL; red->instances = NULL;
} }
} }
}
/* Stop zebra client services. */ /* Stop zebra client services. */
void void

View File

@ -169,7 +169,7 @@ extern int zclient_socket_connect (struct zclient *);
extern void zclient_serv_path_set (char *path); extern void zclient_serv_path_set (char *path);
extern const char *zclient_serv_path_get (void); extern const char *zclient_serv_path_get (void);
extern int redist_check_instance (struct redist_proto *, u_short); extern u_short *redist_check_instance (struct redist_proto *, u_short);
extern void redist_add_instance (struct redist_proto *, u_short); extern void redist_add_instance (struct redist_proto *, u_short);
extern void redist_del_instance (struct redist_proto *, u_short); extern void redist_del_instance (struct redist_proto *, u_short);

View File

@ -294,10 +294,7 @@ zebra_redistribute_delete (int command, struct zserv *client, int length,
* withdraw them when necessary. * withdraw them when necessary.
*/ */
if (instance) if (instance)
{
if (redist_check_instance (&client->mi_redist[afi][type], instance))
redist_del_instance (&client->mi_redist[afi][type], instance); redist_del_instance (&client->mi_redist[afi][type], instance);
}
else else
vrf_bitmap_unset (client->redist[afi][type], zvrf->vrf_id); vrf_bitmap_unset (client->redist[afi][type], zvrf->vrf_id);
} }