1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* SHARP - code to track nexthops
* Copyright (C) Cumulus Networks, Inc.
* Donald Sharp
*/
#include <zebra.h>
#include "memory.h"
#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)
{
struct listnode *node;
struct sharp_nh_tracker *nht;
for (ALL_LIST_ELEMENTS_RO(sg.nhs, node, nht)) {
if (prefix_same(&nht->p, p))
break;
}
if (nht)
return nht;
nht = XCALLOC(MTYPE_NH_TRACKER, sizeof(*nht));
prefix_copy(&nht->p, p);
listnode_add(sg.nhs, nht);
return nht;
}
void sharp_nh_tracker_free(struct sharp_nh_tracker *nht)
{
XFREE(MTYPE_NH_TRACKER, nht);
}
void sharp_nh_tracker_dump(struct vty *vty)
{
struct listnode *node;
struct sharp_nh_tracker *nht;
for (ALL_LIST_ELEMENTS_RO(sg.nhs, node, nht))
vty_out(vty, "%pFX: Nexthops: %u Updates: %u\n", &nht->p,
nht->nhop_num, nht->updates);
}
PREDECL_RBTREE_UNIQ(sharp_nhg_rb);
struct sharp_nhg {
struct sharp_nhg_rb_item mylistitem;
uint32_t id;
#define NHG_NAME_LEN 256
char name[NHG_NAME_LEN];
bool installed;
};
static uint32_t nhg_id;
static uint32_t sharp_get_next_nhid(void)
{
zlog_debug("NHG ID assigned: %u", nhg_id);
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, NHG_NAME_LEN);
}
DECLARE_RBTREE_UNIQ(sharp_nhg_rb, struct sharp_nhg, mylistitem,
sharp_nhg_compare_func);
static struct sharp_nhg *sharp_nhgroup_find_id(uint32_t id)
{
struct sharp_nhg *lookup;
/* Yea its just a for loop, I don't want add complexity
* to sharpd with another RB tree for just IDs
*/
frr_each (sharp_nhg_rb, &nhg_head, lookup) {
if (lookup->id == id)
return lookup;
}
return NULL;
}
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();
strlcpy(snhg->name, name, sizeof(snhg->name));
sharp_nhg_rb_add(&nhg_head, snhg);
}
static void sharp_nhgroup_modify_cb(const struct nexthop_group_cmd *nhgc)
{
struct sharp_nhg lookup;
struct sharp_nhg *snhg;
struct nexthop_group_cmd *bnhgc = NULL;
strlcpy(lookup.name, nhgc->name, sizeof(lookup.name));
snhg = sharp_nhg_rb_find(&nhg_head, &lookup);
if (!nhgc->nhg.nexthop)
return;
if (nhgc->backup_list_name[0])
bnhgc = nhgc_find(nhgc->backup_list_name);
nhg_add(snhg->id, &nhgc->nhg, (bnhgc ? &bnhgc->nhg : NULL));
}
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;
struct nexthop_group_cmd *bnhgc = NULL;
strlcpy(lookup.name, nhgc->name, sizeof(lookup.name));
snhg = sharp_nhg_rb_find(&nhg_head, &lookup);
if (nhgc->backup_list_name[0])
bnhgc = nhgc_find(nhgc->backup_list_name);
nhg_add(snhg->id, &nhgc->nhg, (bnhgc ? &bnhgc->nhg : NULL));
}
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;
struct nexthop_group_cmd *bnhgc = NULL;
strlcpy(lookup.name, nhgc->name, sizeof(lookup.name));
snhg = sharp_nhg_rb_find(&nhg_head, &lookup);
if (nhgc->backup_list_name[0])
bnhgc = nhgc_find(nhgc->backup_list_name);
nhg_add(snhg->id, &nhgc->nhg, (bnhgc ? &bnhgc->nhg : NULL));
}
static void sharp_nhgroup_delete_cb(const char *name)
{
struct sharp_nhg lookup;
struct sharp_nhg *snhg;
strlcpy(lookup.name, name, sizeof(lookup.name));
snhg = sharp_nhg_rb_find(&nhg_head, &lookup);
if (!snhg)
return;
if (sharp_nhgroup_id_is_installed(snhg->id))
nhg_del(snhg->id);
sharp_nhg_rb_del(&nhg_head, snhg);
XFREE(MTYPE_NHG, snhg);
}
uint32_t sharp_nhgroup_get_id(const char *name)
{
struct sharp_nhg lookup;
struct sharp_nhg *snhg;
strlcpy(lookup.name, name, sizeof(lookup.name));
snhg = sharp_nhg_rb_find(&nhg_head, &lookup);
if (!snhg)
return 0;
return snhg->id;
}
void sharp_nhgroup_id_set_installed(uint32_t id, bool installed)
{
struct sharp_nhg *snhg;
snhg = sharp_nhgroup_find_id(id);
if (!snhg) {
zlog_debug("%s: nhg %u not found", __func__, id);
return;
}
snhg->installed = installed;
}
bool sharp_nhgroup_id_is_installed(uint32_t id)
{
struct sharp_nhg *snhg;
snhg = sharp_nhgroup_find_id(id);
if (!snhg) {
zlog_debug("%s: nhg %u not found", __func__, id);
return false;
}
return snhg->installed;
}
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_modify_cb,
sharp_nhgroup_add_nexthop_cb,
sharp_nhgroup_del_nexthop_cb,
sharp_nhgroup_delete_cb);
}
|