sharpd: add abilty to send a nhg to zebra

Modify the sharpd program to have the ability to pass down
a NHG and then operate on it for route installation.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
This commit is contained in:
Donald Sharp 2020-04-29 12:32:53 -04:00 committed by Stephen Worley
parent 27141ea94e
commit 569e87c0c8
7 changed files with 165 additions and 13 deletions

View File

@ -31,6 +31,7 @@ struct sharp_routes {
/* The nexthop info we are using for installation */
struct nexthop nhop;
struct nexthop backup_nhop;
uint32_t nhgid;
struct nexthop_group nhop_group;
struct nexthop_group backup_nhop_group;

View File

@ -47,6 +47,7 @@
#include "sharp_zebra.h"
#include "sharp_vty.h"
#include "sharp_globals.h"
#include "sharp_nht.h"
DEFINE_MGROUP(SHARPD, "sharpd")
@ -164,7 +165,7 @@ int main(int argc, char **argv, char **envp)
sharp_global_init();
nexthop_group_init(NULL, NULL, NULL, NULL);
sharp_nhgroup_init();
vrf_init(NULL, NULL, NULL, NULL, NULL);
sharp_zebra_init();

View File

@ -25,11 +25,15 @@
#include "nexthop.h"
#include "nexthop_group.h"
#include "vty.h"
#include "typesafe.h"
#include "zclient.h"
#include "sharp_nht.h"
#include "sharp_globals.h"
#include "sharp_zebra.h"
DEFINE_MTYPE_STATIC(SHARPD, NH_TRACKER, "Nexthop Tracker")
DEFINE_MTYPE_STATIC(SHARPD, NHG, "Nexthop Group")
struct sharp_nh_tracker *sharp_nh_tracker_get(struct prefix *p)
{
@ -65,3 +69,109 @@ void sharp_nh_tracker_dump(struct vty *vty)
nht->updates);
}
}
PREDECL_RBTREE_UNIQ(sharp_nhg_rb);
struct sharp_nhg {
struct sharp_nhg_rb_item mylistitem;
uint32_t id;
char name[256];
};
static uint32_t nhg_id;
static uint32_t sharp_get_next_nhid(void)
{
zlog_debug("Id assigned: %u", nhg_id + 1);
return nhg_id++;
}
struct sharp_nhg_rb_head nhg_head;
static int sharp_nhg_compare_func(const struct sharp_nhg *a,
const struct sharp_nhg *b)
{
return strncmp(a->name, b->name, strlen(a->name));
}
DECLARE_RBTREE_UNIQ(sharp_nhg_rb, struct sharp_nhg, mylistitem,
sharp_nhg_compare_func);
static void sharp_nhgroup_add_cb(const char *name)
{
struct sharp_nhg *snhg;
snhg = XCALLOC(MTYPE_NHG, sizeof(*snhg));
snhg->id = sharp_get_next_nhid();
strncpy(snhg->name, name, sizeof(snhg->name));
sharp_nhg_rb_add(&nhg_head, snhg);
return;
}
static void sharp_nhgroup_add_nexthop_cb(const struct nexthop_group_cmd *nhgc,
const struct nexthop *nhop)
{
struct sharp_nhg lookup;
struct sharp_nhg *snhg;
strncpy(lookup.name, nhgc->name, sizeof(lookup.name));
snhg = sharp_nhg_rb_find(&nhg_head, &lookup);
nhg_add(snhg->id, &nhgc->nhg);
return;
}
static void sharp_nhgroup_del_nexthop_cb(const struct nexthop_group_cmd *nhgc,
const struct nexthop *nhop)
{
struct sharp_nhg lookup;
struct sharp_nhg *snhg;
strncpy(lookup.name, nhgc->name, sizeof(lookup.name));
snhg = sharp_nhg_rb_find(&nhg_head, &lookup);
nhg_add(snhg->id, &nhgc->nhg);
return;
}
static void sharp_nhgroup_delete_cb(const char *name)
{
struct sharp_nhg lookup;
struct sharp_nhg *snhg;
strncpy(lookup.name, name, sizeof(lookup.name));
snhg = sharp_nhg_rb_find(&nhg_head, &lookup);
if (!snhg)
return;
nhg_del(snhg->id);
sharp_nhg_rb_del(&nhg_head, snhg);
XFREE(MTYPE_NHG, snhg);
return;
}
uint32_t sharp_nhgroup_get_id(const char *name)
{
struct sharp_nhg lookup;
struct sharp_nhg *snhg;
strncpy(lookup.name, name, sizeof(lookup.name));
snhg = sharp_nhg_rb_find(&nhg_head, &lookup);
if (!snhg)
return 0;
return snhg->id;
}
void sharp_nhgroup_init(void)
{
sharp_nhg_rb_init(&nhg_head);
nhg_id = zclient_get_nhg_start(ZEBRA_ROUTE_SHARP);
nexthop_group_init(sharp_nhgroup_add_cb, sharp_nhgroup_add_nexthop_cb,
sharp_nhgroup_del_nexthop_cb,
sharp_nhgroup_delete_cb);
}

View File

@ -35,4 +35,7 @@ struct sharp_nh_tracker {
extern struct sharp_nh_tracker *sharp_nh_tracker_get(struct prefix *p);
extern void sharp_nh_tracker_dump(struct vty *vty);
extern uint32_t sharp_nhgroup_get_id(const char *name);
extern void sharp_nhgroup_init(void);
#endif

View File

@ -192,6 +192,7 @@ DEFPY (install_routes,
struct vrf *vrf;
struct prefix prefix;
uint32_t rts;
uint32_t nhgid = 0;
sg.r.total_routes = routes;
sg.r.installed_routes = 0;
@ -244,6 +245,8 @@ DEFPY (install_routes,
return CMD_WARNING;
}
nhgid = sharp_nhgroup_get_id(nexthop_group);
sg.r.nhgid = nhgid;
sg.r.nhop_group.nexthop = nhgc->nhg.nexthop;
/* Use group's backup nexthop info if present */
@ -297,8 +300,8 @@ DEFPY (install_routes,
sg.r.vrf_id = vrf->vrf_id;
rts = routes;
sharp_install_routes_helper(&prefix, sg.r.vrf_id, sg.r.inst,
&sg.r.nhop_group, &sg.r.backup_nhop_group,
rts);
nhgid, &sg.r.nhop_group,
&sg.r.backup_nhop_group, rts);
return CMD_SUCCESS;
}

View File

@ -217,7 +217,7 @@ int sharp_install_lsps_helper(bool install_p, bool update_p,
}
void sharp_install_routes_helper(struct prefix *p, vrf_id_t vrf_id,
uint8_t instance,
uint8_t instance, uint32_t nhgid,
const struct nexthop_group *nhg,
const struct nexthop_group *backup_nhg,
uint32_t routes)
@ -239,7 +239,7 @@ void sharp_install_routes_helper(struct prefix *p, vrf_id_t vrf_id,
monotime(&sg.r.t_start);
for (i = 0; i < routes; i++) {
route_add(p, vrf_id, (uint8_t)instance, nhg, backup_nhg);
route_add(p, vrf_id, (uint8_t)instance, nhgid, nhg, backup_nhg);
if (v4)
p->u.prefix4.s_addr = htonl(++temp);
else
@ -288,6 +288,7 @@ static void handle_repeated(bool installed)
if (!installed) {
sg.r.installed_routes = 0;
sharp_install_routes_helper(&p, sg.r.vrf_id, sg.r.inst,
sg.r.nhgid,
&sg.r.nhop_group,
&sg.r.backup_nhop_group,
sg.r.total_routes);
@ -357,8 +358,34 @@ void vrf_label_add(vrf_id_t vrf_id, afi_t afi, mpls_label_t label)
zclient_send_vrf_label(zclient, vrf_id, afi, label, ZEBRA_LSP_SHARP);
}
void nhg_add(uint32_t id, const struct nexthop_group *nhg)
{
struct zapi_nexthop nh_array[MULTIPATH_NUM];
struct zapi_nexthop *api_nh;
uint16_t nexthop_num = 0;
struct nexthop *nh;
for (ALL_NEXTHOPS_PTR(nhg, nh)) {
api_nh = &nh_array[nexthop_num];
zapi_nexthop_from_nexthop(api_nh, nh);
nexthop_num++;
}
zclient_nhg_add(zclient, id, nexthop_num, nh_array);
zclient_send_message(zclient);
}
void nhg_del(uint32_t id)
{
zclient_nhg_del(zclient, id);
zclient_send_message(zclient);
}
void route_add(const struct prefix *p, vrf_id_t vrf_id,
uint8_t instance, const struct nexthop_group *nhg,
uint8_t instance, uint32_t nhgid,
const struct nexthop_group *nhg,
const struct nexthop_group *backup_nhg)
{
struct zapi_route api;
@ -376,14 +403,19 @@ void route_add(const struct prefix *p, vrf_id_t vrf_id,
SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
for (ALL_NEXTHOPS_PTR(nhg, nh)) {
api_nh = &api.nexthops[i];
if (nhgid) {
SET_FLAG(api.message, ZAPI_MESSAGE_NHG);
api.nhgid = nhgid;
} else {
for (ALL_NEXTHOPS_PTR(nhg, nh)) {
api_nh = &api.nexthops[i];
zapi_nexthop_from_nexthop(api_nh, nh);
zapi_nexthop_from_nexthop(api_nh, nh);
i++;
i++;
}
api.nexthop_num = i;
}
api.nexthop_num = i;
/* Include backup nexthops, if present */
if (backup_nhg && backup_nhg->nexthop) {

View File

@ -29,15 +29,17 @@ int sharp_zclient_create(uint32_t session_id);
int sharp_zclient_delete(uint32_t session_id);
extern void vrf_label_add(vrf_id_t vrf_id, afi_t afi, mpls_label_t label);
extern void nhg_add(uint32_t id, const struct nexthop_group *nhg);
extern void nhg_del(uint32_t id);
extern void route_add(const struct prefix *p, vrf_id_t, uint8_t instance,
const struct nexthop_group *nhg,
uint32_t nhgid, const struct nexthop_group *nhg,
const struct nexthop_group *backup_nhg);
extern void route_delete(struct prefix *p, vrf_id_t vrf_id, uint8_t instance);
extern void sharp_zebra_nexthop_watch(struct prefix *p, vrf_id_t vrf_id,
bool import, bool watch, bool connected);
extern void sharp_install_routes_helper(struct prefix *p, vrf_id_t vrf_id,
uint8_t instance,
uint8_t instance, uint32_t nhgid,
const struct nexthop_group *nhg,
const struct nexthop_group *backup_nhg,
uint32_t routes);