sharpd: Abstract the route install/delete functions a bit

Abstract the route install/delete functions a bit to allow me to
expand on them in the with future commits.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
This commit is contained in:
Donald Sharp 2019-01-02 14:38:15 -05:00
parent 6205e5dc07
commit 6b98d34fdf
3 changed files with 63 additions and 19 deletions

View File

@ -80,6 +80,8 @@ DEFPY(watch_nexthop_v4, watch_nexthop_v4_cmd,
return CMD_SUCCESS; return CMD_SUCCESS;
} }
DEFPY (install_routes, DEFPY (install_routes,
install_routes_cmd, install_routes_cmd,
"sharp install routes A.B.C.D$start <nexthop <A.B.C.D$nexthop4|X:X::X:X$nexthop6>|nexthop-group NAME$nexthop_group> (1-1000000)$routes [instance (0-255)$instance]", "sharp install routes A.B.C.D$start <nexthop <A.B.C.D$nexthop4|X:X::X:X$nexthop6>|nexthop-group NAME$nexthop_group> (1-1000000)$routes [instance (0-255)$instance]",
@ -96,11 +98,9 @@ DEFPY (install_routes,
"Instance to use\n" "Instance to use\n"
"Instance\n") "Instance\n")
{ {
int i;
struct prefix p; struct prefix p;
struct nexthop nhop; struct nexthop nhop;
struct nexthop_group nhg; struct nexthop_group nhg;
uint32_t temp;
total_routes = routes; total_routes = routes;
installed_routes = 0; installed_routes = 0;
@ -134,13 +134,8 @@ DEFPY (install_routes,
nhg.nexthop = &nhop; nhg.nexthop = &nhop;
} }
zlog_debug("Inserting %ld routes", routes);
temp = ntohl(p.u.prefix4.s_addr); sharp_install_routes_helper(&p, instance, &nhg, routes);
for (i = 0; i < routes; i++) {
route_add(&p, (uint8_t)instance, &nhg);
p.u.prefix4.s_addr = htonl(++temp);
}
return CMD_SUCCESS; return CMD_SUCCESS;
} }
@ -186,9 +181,7 @@ DEFPY (remove_routes,
"instance to use\n" "instance to use\n"
"Value of instance\n") "Value of instance\n")
{ {
int i;
struct prefix p; struct prefix p;
uint32_t temp;
total_routes = routes; total_routes = routes;
removed_routes = 0; removed_routes = 0;
@ -198,13 +191,7 @@ DEFPY (remove_routes,
p.prefixlen = 32; p.prefixlen = 32;
p.u.prefix4 = start; p.u.prefix4 = start;
zlog_debug("Removing %ld routes", routes); sharp_remove_routes_helper(&p, instance, routes);
temp = ntohl(p.u.prefix4.s_addr);
for (i = 0; i < routes; i++) {
route_delete(&p, (uint8_t)instance);
p.u.prefix4.s_addr = htonl(++temp);
}
return CMD_SUCCESS; return CMD_SUCCESS;
} }

View File

@ -133,6 +133,53 @@ extern uint32_t total_routes;
extern uint32_t installed_routes; extern uint32_t installed_routes;
extern uint32_t removed_routes; extern uint32_t removed_routes;
void sharp_install_routes_helper(struct prefix *p, uint8_t instance,
struct nexthop_group *nhg,
uint32_t routes)
{
uint32_t temp, i;
zlog_debug("Inserting %u routes", routes);
temp = ntohl(p->u.prefix4.s_addr);
for (i = 0; i < routes; i++) {
route_add(p, (uint8_t)instance, nhg);
p->u.prefix4.s_addr = htonl(++temp);
}
}
void sharp_remove_routes_helper(struct prefix *p, uint8_t instance,
uint32_t routes)
{
uint32_t temp, i;
zlog_debug("Removing %u routes", routes);
temp = ntohl(p->u.prefix4.s_addr);
for (i = 0; i < routes; i++) {
route_delete(p, (uint8_t)instance);
p->u.prefix4.s_addr = htonl(++temp);
}
}
static int handle_repeated(bool installed)
{
repeat--;
if (repeat <= 0)
return;
if (installed) {
removed_routes = 0;
sharp_remove_routes_helper(&prefix, inst, total_routes);
}
if (!installed) {
installed_routes = 0;
sharp_remove_routes
}
}
static int route_notify_owner(int command, struct zclient *zclient, static int route_notify_owner(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id) zebra_size_t length, vrf_id_t vrf_id)
{ {
@ -146,8 +193,10 @@ static int route_notify_owner(int command, struct zclient *zclient,
switch (note) { switch (note) {
case ZAPI_ROUTE_INSTALLED: case ZAPI_ROUTE_INSTALLED:
installed_routes++; installed_routes++;
if (total_routes == installed_routes) if (total_routes == installed_routes) {
zlog_debug("Installed All Items"); zlog_debug("Installed All Items");
handle_repeated(true);
}
break; break;
case ZAPI_ROUTE_FAIL_INSTALL: case ZAPI_ROUTE_FAIL_INSTALL:
zlog_debug("Failed install of route"); zlog_debug("Failed install of route");
@ -157,8 +206,10 @@ static int route_notify_owner(int command, struct zclient *zclient,
break; break;
case ZAPI_ROUTE_REMOVED: case ZAPI_ROUTE_REMOVED:
removed_routes++; removed_routes++;
if (total_routes == removed_routes) if (total_routes == removed_routes) {
zlog_debug("Removed all Items"); zlog_debug("Removed all Items");
handle_repeated(false);
}
break; break;
case ZAPI_ROUTE_REMOVE_FAIL: case ZAPI_ROUTE_REMOVE_FAIL:
zlog_debug("Route removal Failure"); zlog_debug("Route removal Failure");

View File

@ -29,4 +29,10 @@ extern void route_add(struct prefix *p, uint8_t instance,
struct nexthop_group *nhg); struct nexthop_group *nhg);
extern void route_delete(struct prefix *p, uint8_t instance); extern void route_delete(struct prefix *p, uint8_t instance);
extern void sharp_zebra_nexthop_watch(struct prefix *p, bool watch); extern void sharp_zebra_nexthop_watch(struct prefix *p, bool watch);
extern void sharp_install_routes_helper(struct prefix *p, uint8_t instance,
struct nexthop_group *nhg,
uint32_t routes);
extern void sharp_remove_routes_helper(struct prefix *p, uint8_t instance,
uint32_t routes);
#endif #endif