]> git.proxmox.com Git - mirror_frr.git/blob - sharpd/sharp_nht.c
Merge pull request #12263 from anlancs/fix/pimd-log-bug
[mirror_frr.git] / sharpd / sharp_nht.c
1 /*
2 * SHARP - code to track nexthops
3 * Copyright (C) Cumulus Networks, Inc.
4 * Donald Sharp
5 *
6 * This file is part of FRR.
7 *
8 * FRR is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * FRR is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22 #include <zebra.h>
23
24 #include "memory.h"
25 #include "nexthop.h"
26 #include "nexthop_group.h"
27 #include "vty.h"
28 #include "typesafe.h"
29 #include "zclient.h"
30
31 #include "sharp_nht.h"
32 #include "sharp_globals.h"
33 #include "sharp_zebra.h"
34
35 DEFINE_MTYPE_STATIC(SHARPD, NH_TRACKER, "Nexthop Tracker");
36 DEFINE_MTYPE_STATIC(SHARPD, NHG, "Nexthop Group");
37
38 struct sharp_nh_tracker *sharp_nh_tracker_get(struct prefix *p)
39 {
40 struct listnode *node;
41 struct sharp_nh_tracker *nht;
42
43 for (ALL_LIST_ELEMENTS_RO(sg.nhs, node, nht)) {
44 if (prefix_same(&nht->p, p))
45 break;
46 }
47
48 if (nht)
49 return nht;
50
51 nht = XCALLOC(MTYPE_NH_TRACKER, sizeof(*nht));
52 prefix_copy(&nht->p, p);
53
54 listnode_add(sg.nhs, nht);
55 return nht;
56 }
57
58 void sharp_nh_tracker_dump(struct vty *vty)
59 {
60 struct listnode *node;
61 struct sharp_nh_tracker *nht;
62
63 for (ALL_LIST_ELEMENTS_RO(sg.nhs, node, nht))
64 vty_out(vty, "%pFX: Nexthops: %u Updates: %u\n", &nht->p,
65 nht->nhop_num, nht->updates);
66 }
67
68 PREDECL_RBTREE_UNIQ(sharp_nhg_rb);
69
70 struct sharp_nhg {
71 struct sharp_nhg_rb_item mylistitem;
72
73 uint32_t id;
74
75 #define NHG_NAME_LEN 256
76 char name[NHG_NAME_LEN];
77
78 bool installed;
79 };
80
81 static uint32_t nhg_id;
82
83 static uint32_t sharp_get_next_nhid(void)
84 {
85 zlog_debug("NHG ID assigned: %u", nhg_id);
86 return nhg_id++;
87 }
88
89 struct sharp_nhg_rb_head nhg_head;
90
91 static int sharp_nhg_compare_func(const struct sharp_nhg *a,
92 const struct sharp_nhg *b)
93 {
94 return strncmp(a->name, b->name, NHG_NAME_LEN);
95 }
96
97 DECLARE_RBTREE_UNIQ(sharp_nhg_rb, struct sharp_nhg, mylistitem,
98 sharp_nhg_compare_func);
99
100 static struct sharp_nhg *sharp_nhgroup_find_id(uint32_t id)
101 {
102 struct sharp_nhg *lookup;
103
104 /* Yea its just a for loop, I don't want add complexity
105 * to sharpd with another RB tree for just IDs
106 */
107
108 frr_each (sharp_nhg_rb, &nhg_head, lookup) {
109 if (lookup->id == id)
110 return lookup;
111 }
112
113 return NULL;
114 }
115
116 static void sharp_nhgroup_add_cb(const char *name)
117 {
118 struct sharp_nhg *snhg;
119
120 snhg = XCALLOC(MTYPE_NHG, sizeof(*snhg));
121 snhg->id = sharp_get_next_nhid();
122 strlcpy(snhg->name, name, sizeof(snhg->name));
123
124 sharp_nhg_rb_add(&nhg_head, snhg);
125 }
126
127 static void sharp_nhgroup_modify_cb(const struct nexthop_group_cmd *nhgc)
128 {
129 struct sharp_nhg lookup;
130 struct sharp_nhg *snhg;
131 struct nexthop_group_cmd *bnhgc = NULL;
132
133 strlcpy(lookup.name, nhgc->name, sizeof(lookup.name));
134 snhg = sharp_nhg_rb_find(&nhg_head, &lookup);
135
136 if (!nhgc->nhg.nexthop)
137 return;
138
139 if (nhgc->backup_list_name[0])
140 bnhgc = nhgc_find(nhgc->backup_list_name);
141
142 nhg_add(snhg->id, &nhgc->nhg, (bnhgc ? &bnhgc->nhg : NULL));
143 }
144
145 static void sharp_nhgroup_add_nexthop_cb(const struct nexthop_group_cmd *nhgc,
146 const struct nexthop *nhop)
147 {
148 struct sharp_nhg lookup;
149 struct sharp_nhg *snhg;
150 struct nexthop_group_cmd *bnhgc = NULL;
151
152 strlcpy(lookup.name, nhgc->name, sizeof(lookup.name));
153 snhg = sharp_nhg_rb_find(&nhg_head, &lookup);
154
155 if (nhgc->backup_list_name[0])
156 bnhgc = nhgc_find(nhgc->backup_list_name);
157
158 nhg_add(snhg->id, &nhgc->nhg, (bnhgc ? &bnhgc->nhg : NULL));
159 }
160
161 static void sharp_nhgroup_del_nexthop_cb(const struct nexthop_group_cmd *nhgc,
162 const struct nexthop *nhop)
163 {
164 struct sharp_nhg lookup;
165 struct sharp_nhg *snhg;
166 struct nexthop_group_cmd *bnhgc = NULL;
167
168 strlcpy(lookup.name, nhgc->name, sizeof(lookup.name));
169 snhg = sharp_nhg_rb_find(&nhg_head, &lookup);
170
171 if (nhgc->backup_list_name[0])
172 bnhgc = nhgc_find(nhgc->backup_list_name);
173
174 nhg_add(snhg->id, &nhgc->nhg, (bnhgc ? &bnhgc->nhg : NULL));
175 }
176
177 static void sharp_nhgroup_delete_cb(const char *name)
178 {
179 struct sharp_nhg lookup;
180 struct sharp_nhg *snhg;
181
182 strlcpy(lookup.name, name, sizeof(lookup.name));
183 snhg = sharp_nhg_rb_find(&nhg_head, &lookup);
184 if (!snhg)
185 return;
186
187 nhg_del(snhg->id);
188 sharp_nhg_rb_del(&nhg_head, snhg);
189 XFREE(MTYPE_NHG, snhg);
190 }
191
192 uint32_t sharp_nhgroup_get_id(const char *name)
193 {
194 struct sharp_nhg lookup;
195 struct sharp_nhg *snhg;
196
197 strlcpy(lookup.name, name, sizeof(lookup.name));
198 snhg = sharp_nhg_rb_find(&nhg_head, &lookup);
199 if (!snhg)
200 return 0;
201
202 return snhg->id;
203 }
204
205 void sharp_nhgroup_id_set_installed(uint32_t id, bool installed)
206 {
207 struct sharp_nhg *snhg;
208
209 snhg = sharp_nhgroup_find_id(id);
210 if (!snhg) {
211 zlog_debug("%s: nhg %u not found", __func__, id);
212 return;
213 }
214
215 snhg->installed = installed;
216 }
217
218 bool sharp_nhgroup_id_is_installed(uint32_t id)
219 {
220 struct sharp_nhg *snhg;
221
222 snhg = sharp_nhgroup_find_id(id);
223 if (!snhg) {
224 zlog_debug("%s: nhg %u not found", __func__, id);
225 return false;
226 }
227
228 return snhg->installed;
229 }
230
231 void sharp_nhgroup_init(void)
232 {
233 sharp_nhg_rb_init(&nhg_head);
234 nhg_id = zclient_get_nhg_start(ZEBRA_ROUTE_SHARP);
235
236 nexthop_group_init(sharp_nhgroup_add_cb, sharp_nhgroup_modify_cb,
237 sharp_nhgroup_add_nexthop_cb,
238 sharp_nhgroup_del_nexthop_cb,
239 sharp_nhgroup_delete_cb);
240 }