1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SHARP - code to track nexthops
4 * Copyright (C) Cumulus Networks, Inc.
11 #include "nexthop_group.h"
16 #include "sharp_nht.h"
17 #include "sharp_globals.h"
18 #include "sharp_zebra.h"
20 DEFINE_MTYPE_STATIC(SHARPD
, NH_TRACKER
, "Nexthop Tracker");
21 DEFINE_MTYPE_STATIC(SHARPD
, NHG
, "Nexthop Group");
23 struct sharp_nh_tracker
*sharp_nh_tracker_get(struct prefix
*p
)
25 struct listnode
*node
;
26 struct sharp_nh_tracker
*nht
;
28 for (ALL_LIST_ELEMENTS_RO(sg
.nhs
, node
, nht
)) {
29 if (prefix_same(&nht
->p
, p
))
36 nht
= XCALLOC(MTYPE_NH_TRACKER
, sizeof(*nht
));
37 prefix_copy(&nht
->p
, p
);
39 listnode_add(sg
.nhs
, nht
);
43 void sharp_nh_tracker_dump(struct vty
*vty
)
45 struct listnode
*node
;
46 struct sharp_nh_tracker
*nht
;
48 for (ALL_LIST_ELEMENTS_RO(sg
.nhs
, node
, nht
))
49 vty_out(vty
, "%pFX: Nexthops: %u Updates: %u\n", &nht
->p
,
50 nht
->nhop_num
, nht
->updates
);
53 PREDECL_RBTREE_UNIQ(sharp_nhg_rb
);
56 struct sharp_nhg_rb_item mylistitem
;
60 #define NHG_NAME_LEN 256
61 char name
[NHG_NAME_LEN
];
66 static uint32_t nhg_id
;
68 static uint32_t sharp_get_next_nhid(void)
70 zlog_debug("NHG ID assigned: %u", nhg_id
);
74 struct sharp_nhg_rb_head nhg_head
;
76 static int sharp_nhg_compare_func(const struct sharp_nhg
*a
,
77 const struct sharp_nhg
*b
)
79 return strncmp(a
->name
, b
->name
, NHG_NAME_LEN
);
82 DECLARE_RBTREE_UNIQ(sharp_nhg_rb
, struct sharp_nhg
, mylistitem
,
83 sharp_nhg_compare_func
);
85 static struct sharp_nhg
*sharp_nhgroup_find_id(uint32_t id
)
87 struct sharp_nhg
*lookup
;
89 /* Yea its just a for loop, I don't want add complexity
90 * to sharpd with another RB tree for just IDs
93 frr_each (sharp_nhg_rb
, &nhg_head
, lookup
) {
101 static void sharp_nhgroup_add_cb(const char *name
)
103 struct sharp_nhg
*snhg
;
105 snhg
= XCALLOC(MTYPE_NHG
, sizeof(*snhg
));
106 snhg
->id
= sharp_get_next_nhid();
107 strlcpy(snhg
->name
, name
, sizeof(snhg
->name
));
109 sharp_nhg_rb_add(&nhg_head
, snhg
);
112 static void sharp_nhgroup_modify_cb(const struct nexthop_group_cmd
*nhgc
)
114 struct sharp_nhg lookup
;
115 struct sharp_nhg
*snhg
;
116 struct nexthop_group_cmd
*bnhgc
= NULL
;
118 strlcpy(lookup
.name
, nhgc
->name
, sizeof(lookup
.name
));
119 snhg
= sharp_nhg_rb_find(&nhg_head
, &lookup
);
121 if (!nhgc
->nhg
.nexthop
)
124 if (nhgc
->backup_list_name
[0])
125 bnhgc
= nhgc_find(nhgc
->backup_list_name
);
127 nhg_add(snhg
->id
, &nhgc
->nhg
, (bnhgc
? &bnhgc
->nhg
: NULL
));
130 static void sharp_nhgroup_add_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
131 const struct nexthop
*nhop
)
133 struct sharp_nhg lookup
;
134 struct sharp_nhg
*snhg
;
135 struct nexthop_group_cmd
*bnhgc
= NULL
;
137 strlcpy(lookup
.name
, nhgc
->name
, sizeof(lookup
.name
));
138 snhg
= sharp_nhg_rb_find(&nhg_head
, &lookup
);
140 if (nhgc
->backup_list_name
[0])
141 bnhgc
= nhgc_find(nhgc
->backup_list_name
);
143 nhg_add(snhg
->id
, &nhgc
->nhg
, (bnhgc
? &bnhgc
->nhg
: NULL
));
146 static void sharp_nhgroup_del_nexthop_cb(const struct nexthop_group_cmd
*nhgc
,
147 const struct nexthop
*nhop
)
149 struct sharp_nhg lookup
;
150 struct sharp_nhg
*snhg
;
151 struct nexthop_group_cmd
*bnhgc
= NULL
;
153 strlcpy(lookup
.name
, nhgc
->name
, sizeof(lookup
.name
));
154 snhg
= sharp_nhg_rb_find(&nhg_head
, &lookup
);
156 if (nhgc
->backup_list_name
[0])
157 bnhgc
= nhgc_find(nhgc
->backup_list_name
);
159 nhg_add(snhg
->id
, &nhgc
->nhg
, (bnhgc
? &bnhgc
->nhg
: NULL
));
162 static void sharp_nhgroup_delete_cb(const char *name
)
164 struct sharp_nhg lookup
;
165 struct sharp_nhg
*snhg
;
167 strlcpy(lookup
.name
, name
, sizeof(lookup
.name
));
168 snhg
= sharp_nhg_rb_find(&nhg_head
, &lookup
);
173 sharp_nhg_rb_del(&nhg_head
, snhg
);
174 XFREE(MTYPE_NHG
, snhg
);
177 uint32_t sharp_nhgroup_get_id(const char *name
)
179 struct sharp_nhg lookup
;
180 struct sharp_nhg
*snhg
;
182 strlcpy(lookup
.name
, name
, sizeof(lookup
.name
));
183 snhg
= sharp_nhg_rb_find(&nhg_head
, &lookup
);
190 void sharp_nhgroup_id_set_installed(uint32_t id
, bool installed
)
192 struct sharp_nhg
*snhg
;
194 snhg
= sharp_nhgroup_find_id(id
);
196 zlog_debug("%s: nhg %u not found", __func__
, id
);
200 snhg
->installed
= installed
;
203 bool sharp_nhgroup_id_is_installed(uint32_t id
)
205 struct sharp_nhg
*snhg
;
207 snhg
= sharp_nhgroup_find_id(id
);
209 zlog_debug("%s: nhg %u not found", __func__
, id
);
213 return snhg
->installed
;
216 void sharp_nhgroup_init(void)
218 sharp_nhg_rb_init(&nhg_head
);
219 nhg_id
= zclient_get_nhg_start(ZEBRA_ROUTE_SHARP
);
221 nexthop_group_init(sharp_nhgroup_add_cb
, sharp_nhgroup_modify_cb
,
222 sharp_nhgroup_add_nexthop_cb
,
223 sharp_nhgroup_del_nexthop_cb
,
224 sharp_nhgroup_delete_cb
);