]>
Commit | Line | Data |
---|---|---|
e5c83d9b DS |
1 | /* |
2 | * PBR-nht Code | |
3 | * Copyright (C) 2018 Cumulus Networks, Inc. | |
4 | * Donald Sharp | |
5 | * | |
6 | * FRR is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * FRR is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | #include <zebra.h> | |
21 | ||
22 | #include <log.h> | |
23 | #include <nexthop.h> | |
50d89650 SW |
24 | #include "nexthop_group.h" |
25 | #include "nexthop_group_private.h" | |
e5c83d9b DS |
26 | #include <hash.h> |
27 | #include <jhash.h> | |
28 | #include <vty.h> | |
29 | #include <zclient.h> | |
b13e5ad6 | 30 | #include <debug.h> |
e5c83d9b DS |
31 | |
32 | #include "pbrd/pbr_nht.h" | |
33 | #include "pbrd/pbr_map.h" | |
e5c83d9b DS |
34 | #include "pbrd/pbr_zebra.h" |
35 | #include "pbrd/pbr_memory.h" | |
36 | #include "pbrd/pbr_debug.h" | |
37 | ||
38 | DEFINE_MTYPE_STATIC(PBRD, PBR_NHG, "PBR Nexthop Groups") | |
39 | ||
40 | static struct hash *pbr_nhg_hash; | |
b13e5ad6 | 41 | static struct hash *pbr_nhrc_hash; |
e5c83d9b DS |
42 | |
43 | static uint32_t pbr_nhg_low_table; | |
44 | static uint32_t pbr_nhg_high_table; | |
45 | static uint32_t pbr_nhg_low_rule; | |
46 | static uint32_t pbr_nhg_high_rule; | |
47 | static bool nhg_tableid[65535]; | |
48 | ||
b13e5ad6 DS |
49 | static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache *pnhgc, |
50 | struct nexthop_group nhg); | |
ff9799c3 DS |
51 | static void |
52 | pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache *pnhgc, | |
53 | struct nexthop_group nhg, | |
aafac994 | 54 | enum nexthop_types_t nh_type); |
b13e5ad6 DS |
55 | |
56 | /* | |
57 | * Nexthop refcount. | |
58 | */ | |
59 | struct nhrc { | |
60 | struct nexthop nexthop; | |
61 | unsigned int refcount; | |
62 | }; | |
63 | ||
64 | /* Hash functions for pbr_nhrc_hash ---------------------------------------- */ | |
65 | ||
66 | static void *pbr_nhrc_hash_alloc(void *p) | |
67 | { | |
68 | struct nhrc *nhrc = XCALLOC(MTYPE_PBR_NHG, sizeof(struct nhrc)); | |
69 | nhrc->nexthop = *(struct nexthop *)p; | |
ad9255f8 SW |
70 | nhrc->nexthop.next = NULL; |
71 | nhrc->nexthop.prev = NULL; | |
b13e5ad6 DS |
72 | return nhrc; |
73 | } | |
74 | ||
74df8d6d | 75 | static bool pbr_nhrc_hash_equal(const void *arg1, const void *arg2) |
b13e5ad6 DS |
76 | { |
77 | const struct nexthop *nh1, *nh2; | |
78 | ||
79 | nh1 = arg1; | |
80 | nh2 = arg2; | |
81 | ||
82 | return nexthop_same(nh1, nh2); | |
83 | } | |
84 | ||
85 | /* ------------------------------------------------------------------------- */ | |
86 | ||
e5c83d9b DS |
87 | static void *pbr_nh_alloc(void *p) |
88 | { | |
89 | struct pbr_nexthop_cache *new; | |
90 | struct pbr_nexthop_cache *pnhc = (struct pbr_nexthop_cache *)p; | |
b13e5ad6 | 91 | struct nhrc *nhrc; |
e5c83d9b DS |
92 | |
93 | new = XCALLOC(MTYPE_PBR_NHG, sizeof(*new)); | |
b13e5ad6 DS |
94 | nhrc = hash_get(pbr_nhrc_hash, pnhc->nexthop, pbr_nhrc_hash_alloc); |
95 | new->nexthop = &nhrc->nexthop; | |
96 | ||
97 | /* Decremented again in pbr_nh_delete */ | |
98 | ++nhrc->refcount; | |
e5c83d9b DS |
99 | |
100 | DEBUGD(&pbr_dbg_nht, "%s: Sending nexthop to Zebra", | |
101 | __PRETTY_FUNCTION__); | |
102 | ||
b13e5ad6 | 103 | pbr_send_rnh(new->nexthop, true); |
e5c83d9b DS |
104 | |
105 | new->valid = false; | |
106 | return new; | |
107 | } | |
108 | ||
109 | static void pbr_nh_delete(struct pbr_nexthop_cache **pnhc) | |
110 | { | |
b13e5ad6 DS |
111 | struct nhrc *nhrc; |
112 | ||
113 | nhrc = hash_lookup(pbr_nhrc_hash, (*pnhc)->nexthop); | |
114 | ||
115 | if (nhrc) | |
116 | --nhrc->refcount; | |
117 | if (!nhrc || nhrc->refcount == 0) { | |
118 | DEBUGD(&pbr_dbg_nht, "%s: Removing nexthop from Zebra", | |
119 | __PRETTY_FUNCTION__); | |
120 | pbr_send_rnh((*pnhc)->nexthop, false); | |
121 | } | |
122 | if (nhrc && nhrc->refcount == 0) { | |
123 | hash_release(pbr_nhrc_hash, nhrc); | |
124 | XFREE(MTYPE_PBR_NHG, nhrc); | |
125 | } | |
e5c83d9b DS |
126 | |
127 | XFREE(MTYPE_PBR_NHG, *pnhc); | |
128 | } | |
129 | ||
e3b78da8 | 130 | static void pbr_nh_delete_iterate(struct hash_bucket *b, void *p) |
b13e5ad6 DS |
131 | { |
132 | pbr_nh_delete((struct pbr_nexthop_cache **)&b->data); | |
133 | } | |
134 | ||
d8b87afe | 135 | static uint32_t pbr_nh_hash_key(const void *arg) |
e5c83d9b DS |
136 | { |
137 | uint32_t key; | |
d8b87afe | 138 | const struct pbr_nexthop_cache *pbrnc = arg; |
e5c83d9b | 139 | |
b13e5ad6 | 140 | key = nexthop_hash(pbrnc->nexthop); |
e5c83d9b DS |
141 | |
142 | return key; | |
143 | } | |
144 | ||
74df8d6d | 145 | static bool pbr_nh_hash_equal(const void *arg1, const void *arg2) |
e5c83d9b DS |
146 | { |
147 | const struct pbr_nexthop_cache *pbrnc1 = | |
148 | (const struct pbr_nexthop_cache *)arg1; | |
149 | const struct pbr_nexthop_cache *pbrnc2 = | |
150 | (const struct pbr_nexthop_cache *)arg2; | |
151 | ||
b13e5ad6 | 152 | if (pbrnc1->nexthop->vrf_id != pbrnc2->nexthop->vrf_id) |
74df8d6d | 153 | return false; |
e5c83d9b | 154 | |
b13e5ad6 | 155 | if (pbrnc1->nexthop->ifindex != pbrnc2->nexthop->ifindex) |
74df8d6d | 156 | return false; |
e5c83d9b | 157 | |
b13e5ad6 | 158 | if (pbrnc1->nexthop->type != pbrnc2->nexthop->type) |
74df8d6d | 159 | return false; |
e5c83d9b | 160 | |
b13e5ad6 | 161 | switch (pbrnc1->nexthop->type) { |
e5c83d9b | 162 | case NEXTHOP_TYPE_IFINDEX: |
a106a408 | 163 | return pbrnc1->nexthop->ifindex == pbrnc2->nexthop->ifindex; |
e5c83d9b DS |
164 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
165 | case NEXTHOP_TYPE_IPV4: | |
b13e5ad6 DS |
166 | return pbrnc1->nexthop->gate.ipv4.s_addr |
167 | == pbrnc2->nexthop->gate.ipv4.s_addr; | |
e5c83d9b DS |
168 | case NEXTHOP_TYPE_IPV6_IFINDEX: |
169 | case NEXTHOP_TYPE_IPV6: | |
f24f3450 RW |
170 | return !memcmp(&pbrnc1->nexthop->gate.ipv6, |
171 | &pbrnc2->nexthop->gate.ipv6, 16); | |
e5c83d9b | 172 | case NEXTHOP_TYPE_BLACKHOLE: |
b13e5ad6 | 173 | return pbrnc1->nexthop->bh_type == pbrnc2->nexthop->bh_type; |
e5c83d9b DS |
174 | } |
175 | ||
176 | /* | |
177 | * We should not get here | |
178 | */ | |
74df8d6d | 179 | return false; |
e5c83d9b DS |
180 | } |
181 | ||
b13e5ad6 DS |
182 | static void pbr_nhgc_delete(struct pbr_nexthop_group_cache *p) |
183 | { | |
184 | hash_iterate(p->nhh, pbr_nh_delete_iterate, NULL); | |
185 | hash_free(p->nhh); | |
186 | XFREE(MTYPE_PBR_NHG, p); | |
187 | } | |
188 | ||
189 | static void *pbr_nhgc_alloc(void *p) | |
190 | { | |
191 | struct pbr_nexthop_group_cache *new; | |
192 | struct pbr_nexthop_group_cache *pnhgc = | |
193 | (struct pbr_nexthop_group_cache *)p; | |
194 | ||
195 | new = XCALLOC(MTYPE_PBR_NHG, sizeof(*new)); | |
196 | ||
65b88efa | 197 | strlcpy(new->name, pnhgc->name, sizeof(pnhgc->name)); |
a4044dc1 | 198 | new->table_id = pbr_nht_get_next_tableid(false); |
b13e5ad6 DS |
199 | |
200 | DEBUGD(&pbr_dbg_nht, "%s: NHT: %s assigned Table ID: %u", | |
201 | __PRETTY_FUNCTION__, new->name, new->table_id); | |
202 | ||
203 | new->nhh = hash_create_size(8, pbr_nh_hash_key, pbr_nh_hash_equal, | |
204 | "PBR NH Cache Hash"); | |
205 | return new; | |
206 | } | |
207 | ||
208 | ||
e5c83d9b DS |
209 | void pbr_nhgroup_add_cb(const char *name) |
210 | { | |
b13e5ad6 DS |
211 | struct pbr_nexthop_group_cache *pnhgc; |
212 | struct nexthop_group_cmd *nhgc; | |
e5c83d9b | 213 | |
b13e5ad6 | 214 | nhgc = nhgc_find(name); |
68a63f60 QY |
215 | |
216 | if (!nhgc) { | |
217 | DEBUGD(&pbr_dbg_nht, "%s: Could not find nhgc with name: %s\n", | |
218 | __PRETTY_FUNCTION__, name); | |
219 | return; | |
220 | } | |
221 | ||
b13e5ad6 | 222 | pnhgc = pbr_nht_add_group(name); |
e5c83d9b | 223 | |
a4044dc1 QY |
224 | if (!pnhgc) |
225 | return; | |
226 | ||
b13e5ad6 | 227 | DEBUGD(&pbr_dbg_nht, "%s: Added nexthop-group %s", __PRETTY_FUNCTION__, |
e5c83d9b | 228 | name); |
b13e5ad6 | 229 | |
b13e5ad6 | 230 | pbr_map_check_nh_group_change(name); |
e5c83d9b DS |
231 | } |
232 | ||
b13e5ad6 | 233 | void pbr_nhgroup_add_nexthop_cb(const struct nexthop_group_cmd *nhgc, |
e5c83d9b DS |
234 | const struct nexthop *nhop) |
235 | { | |
b13e5ad6 | 236 | char debugstr[256]; |
3e300703 | 237 | struct pbr_nexthop_group_cache pnhgc_find = {}; |
b13e5ad6 | 238 | struct pbr_nexthop_group_cache *pnhgc; |
3e300703 | 239 | struct pbr_nexthop_cache pnhc_find = {}; |
b13e5ad6 DS |
240 | struct pbr_nexthop_cache *pnhc; |
241 | ||
a4044dc1 QY |
242 | if (!pbr_nht_get_next_tableid(true)) { |
243 | zlog_warn( | |
244 | "%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'", | |
245 | __PRETTY_FUNCTION__, nhgc->name); | |
246 | return; | |
247 | } | |
248 | ||
b13e5ad6 DS |
249 | /* find pnhgc by name */ |
250 | strlcpy(pnhgc_find.name, nhgc->name, sizeof(pnhgc_find.name)); | |
251 | pnhgc = hash_get(pbr_nhg_hash, &pnhgc_find, pbr_nhgc_alloc); | |
e5c83d9b | 252 | |
b13e5ad6 DS |
253 | /* create & insert new pnhc into pnhgc->nhh */ |
254 | pnhc_find.nexthop = (struct nexthop *)nhop; | |
255 | pnhc = hash_get(pnhgc->nhh, &pnhc_find, pbr_nh_alloc); | |
256 | pnhc_find.nexthop = NULL; | |
257 | ||
258 | /* set parent pnhgc */ | |
259 | pnhc->parent = pnhgc; | |
e5c83d9b | 260 | |
b13e5ad6 DS |
261 | if (DEBUG_MODE_CHECK(&pbr_dbg_nht, DEBUG_MODE_ALL)) { |
262 | nexthop2str(nhop, debugstr, sizeof(debugstr)); | |
263 | DEBUGD(&pbr_dbg_nht, "%s: Added %s to nexthop-group %s", | |
264 | __PRETTY_FUNCTION__, debugstr, nhgc->name); | |
265 | } | |
266 | ||
267 | pbr_nht_install_nexthop_group(pnhgc, nhgc->nhg); | |
268 | pbr_map_check_nh_group_change(nhgc->name); | |
a106a408 | 269 | |
cb254f41 SW |
270 | if (nhop->type == NEXTHOP_TYPE_IFINDEX |
271 | || (nhop->type == NEXTHOP_TYPE_IPV6_IFINDEX | |
272 | && IN6_IS_ADDR_LINKLOCAL(&nhop->gate.ipv6))) { | |
a106a408 RW |
273 | struct interface *ifp; |
274 | ||
275 | ifp = if_lookup_by_index(nhop->ifindex, nhop->vrf_id); | |
276 | if (ifp) | |
277 | pbr_nht_nexthop_interface_update(ifp); | |
278 | } | |
e5c83d9b DS |
279 | } |
280 | ||
b13e5ad6 | 281 | void pbr_nhgroup_del_nexthop_cb(const struct nexthop_group_cmd *nhgc, |
e5c83d9b DS |
282 | const struct nexthop *nhop) |
283 | { | |
b13e5ad6 | 284 | char debugstr[256]; |
3e300703 | 285 | struct pbr_nexthop_group_cache pnhgc_find = {}; |
b13e5ad6 | 286 | struct pbr_nexthop_group_cache *pnhgc; |
3e300703 | 287 | struct pbr_nexthop_cache pnhc_find = {}; |
b13e5ad6 | 288 | struct pbr_nexthop_cache *pnhc; |
aafac994 | 289 | enum nexthop_types_t nh_type = nhop->type; |
b13e5ad6 DS |
290 | |
291 | /* find pnhgc by name */ | |
292 | strlcpy(pnhgc_find.name, nhgc->name, sizeof(pnhgc_find.name)); | |
a4044dc1 | 293 | pnhgc = hash_lookup(pbr_nhg_hash, &pnhgc_find); |
b13e5ad6 DS |
294 | |
295 | /* delete pnhc from pnhgc->nhh */ | |
296 | pnhc_find.nexthop = (struct nexthop *)nhop; | |
297 | pnhc = hash_release(pnhgc->nhh, &pnhc_find); | |
298 | ||
299 | /* delete pnhc */ | |
300 | pbr_nh_delete(&pnhc); | |
e5c83d9b | 301 | |
b13e5ad6 DS |
302 | if (DEBUG_MODE_CHECK(&pbr_dbg_nht, DEBUG_MODE_ALL)) { |
303 | nexthop2str(nhop, debugstr, sizeof(debugstr)); | |
304 | DEBUGD(&pbr_dbg_nht, "%s: Removed %s from nexthop-group %s", | |
305 | __PRETTY_FUNCTION__, debugstr, nhgc->name); | |
306 | } | |
e5c83d9b | 307 | |
ff9799c3 DS |
308 | if (pnhgc->nhh->count) |
309 | pbr_nht_install_nexthop_group(pnhgc, nhgc->nhg); | |
310 | else | |
aafac994 | 311 | pbr_nht_uninstall_nexthop_group(pnhgc, nhgc->nhg, nh_type); |
ff9799c3 | 312 | |
b13e5ad6 | 313 | pbr_map_check_nh_group_change(nhgc->name); |
e5c83d9b DS |
314 | } |
315 | ||
316 | void pbr_nhgroup_delete_cb(const char *name) | |
317 | { | |
b13e5ad6 | 318 | DEBUGD(&pbr_dbg_nht, "%s: Removed nexthop-group %s", |
e5c83d9b | 319 | __PRETTY_FUNCTION__, name); |
b13e5ad6 | 320 | |
ff9799c3 DS |
321 | /* delete group from all pbrms's */ |
322 | pbr_nht_delete_group(name); | |
323 | ||
b13e5ad6 | 324 | pbr_map_check_nh_group_change(name); |
e5c83d9b DS |
325 | } |
326 | ||
327 | #if 0 | |
328 | static struct pbr_nexthop_cache *pbr_nht_lookup_nexthop(struct nexthop *nexthop) | |
329 | { | |
330 | return NULL; | |
331 | } | |
332 | #endif | |
333 | ||
e3b78da8 | 334 | static void pbr_nht_find_nhg_from_table_install(struct hash_bucket *b, |
e5c83d9b DS |
335 | void *data) |
336 | { | |
337 | struct pbr_nexthop_group_cache *pnhgc = | |
338 | (struct pbr_nexthop_group_cache *)b->data; | |
339 | uint32_t *table_id = (uint32_t *)data; | |
340 | ||
341 | if (pnhgc->table_id == *table_id) { | |
342 | DEBUGD(&pbr_dbg_nht, "%s: Table ID (%u) matches %s", | |
343 | __PRETTY_FUNCTION__, *table_id, pnhgc->name); | |
2fb7892e DS |
344 | |
345 | /* | |
346 | * If the table has been re-handled by zebra | |
347 | * and we are already installed no need to do | |
348 | * anything here. | |
349 | */ | |
350 | if (!pnhgc->installed) { | |
351 | pnhgc->installed = true; | |
352 | pbr_map_schedule_policy_from_nhg(pnhgc->name); | |
353 | } | |
e5c83d9b DS |
354 | } |
355 | } | |
356 | ||
357 | void pbr_nht_route_installed_for_table(uint32_t table_id) | |
358 | { | |
359 | hash_iterate(pbr_nhg_hash, pbr_nht_find_nhg_from_table_install, | |
360 | &table_id); | |
361 | } | |
362 | ||
e3b78da8 | 363 | static void pbr_nht_find_nhg_from_table_remove(struct hash_bucket *b, |
e5c83d9b DS |
364 | void *data) |
365 | { | |
366 | ; | |
367 | } | |
368 | ||
369 | void pbr_nht_route_removed_for_table(uint32_t table_id) | |
370 | { | |
371 | hash_iterate(pbr_nhg_hash, pbr_nht_find_nhg_from_table_remove, | |
372 | &table_id); | |
373 | } | |
374 | ||
375 | /* | |
376 | * Loop through all nexthops in a nexthop group to check that they are all the | |
377 | * same. If they are not all the same, log this peculiarity. | |
378 | * | |
379 | * nhg | |
380 | * The nexthop group to check | |
381 | * | |
382 | * Returns: | |
383 | * - AFI of last nexthop in the group | |
384 | * - AFI_MAX on error | |
385 | */ | |
ff9799c3 | 386 | static afi_t pbr_nht_which_afi(struct nexthop_group nhg, |
aafac994 | 387 | enum nexthop_types_t nh_type) |
e5c83d9b DS |
388 | { |
389 | struct nexthop *nexthop; | |
390 | afi_t install_afi = AFI_MAX; | |
391 | bool v6, v4, bh; | |
d3765386 | 392 | |
268c24ee RW |
393 | if (nh_type) { |
394 | switch (nh_type) { | |
395 | case NEXTHOP_TYPE_IPV4: | |
396 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
397 | return AFI_IP; | |
398 | case NEXTHOP_TYPE_IPV6: | |
399 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
400 | return AFI_IP6; | |
401 | case NEXTHOP_TYPE_IFINDEX: | |
402 | case NEXTHOP_TYPE_BLACKHOLE: | |
403 | return AFI_MAX; | |
404 | } | |
405 | } | |
406 | ||
e5c83d9b DS |
407 | v6 = v4 = bh = false; |
408 | ||
268c24ee RW |
409 | for (ALL_NEXTHOPS(nhg, nexthop)) { |
410 | nh_type = nexthop->type; | |
411 | ||
412 | switch (nh_type) { | |
413 | case NEXTHOP_TYPE_IFINDEX: | |
414 | break; | |
415 | case NEXTHOP_TYPE_IPV4: | |
416 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
417 | v6 = true; | |
418 | install_afi = AFI_IP; | |
419 | break; | |
420 | case NEXTHOP_TYPE_IPV6: | |
421 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
422 | v4 = true; | |
423 | install_afi = AFI_IP6; | |
424 | break; | |
425 | case NEXTHOP_TYPE_BLACKHOLE: | |
426 | bh = true; | |
e5c83d9b DS |
427 | break; |
428 | } | |
429 | } | |
430 | ||
268c24ee RW |
431 | /* Interface and/or blackhole nexthops only. */ |
432 | if (!v4 && !v6) | |
ff9799c3 | 433 | install_afi = AFI_MAX; |
ff9799c3 | 434 | |
e5c83d9b DS |
435 | if (!bh && v6 && v4) |
436 | DEBUGD(&pbr_dbg_nht, | |
437 | "%s: Saw both V6 and V4 nexthops...using %s", | |
438 | __PRETTY_FUNCTION__, afi2str(install_afi)); | |
439 | if (bh && (v6 || v4)) | |
440 | DEBUGD(&pbr_dbg_nht, | |
441 | "%s: Saw blackhole nexthop(s) with %s%s%s nexthop(s), using AFI_MAX.", | |
442 | __PRETTY_FUNCTION__, v4 ? "v4" : "", | |
443 | (v4 && v6) ? " and " : "", v6 ? "v6" : ""); | |
444 | ||
445 | return install_afi; | |
446 | } | |
447 | ||
448 | static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache *pnhgc, | |
449 | struct nexthop_group nhg) | |
450 | { | |
451 | afi_t install_afi; | |
aafac994 | 452 | enum nexthop_types_t nh_type = 0; |
e5c83d9b | 453 | |
aafac994 | 454 | install_afi = pbr_nht_which_afi(nhg, nh_type); |
e5c83d9b | 455 | |
e5c83d9b DS |
456 | route_add(pnhgc, nhg, install_afi); |
457 | } | |
458 | ||
459 | static void | |
460 | pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache *pnhgc, | |
ff9799c3 | 461 | struct nexthop_group nhg, |
aafac994 | 462 | enum nexthop_types_t nh_type) |
e5c83d9b DS |
463 | { |
464 | afi_t install_afi; | |
465 | ||
aafac994 | 466 | install_afi = pbr_nht_which_afi(nhg, nh_type); |
e5c83d9b DS |
467 | |
468 | pnhgc->installed = false; | |
469 | pnhgc->valid = false; | |
470 | route_delete(pnhgc, install_afi); | |
471 | } | |
472 | ||
473 | void pbr_nht_change_group(const char *name) | |
474 | { | |
475 | struct nexthop_group_cmd *nhgc; | |
476 | struct pbr_nexthop_group_cache *pnhgc; | |
477 | struct pbr_nexthop_group_cache find; | |
478 | struct nexthop *nhop; | |
479 | ||
480 | nhgc = nhgc_find(name); | |
481 | if (!nhgc) | |
482 | return; | |
483 | ||
484 | memset(&find, 0, sizeof(find)); | |
6612590d | 485 | snprintf(find.name, sizeof(find.name), "%s", name); |
e5c83d9b DS |
486 | pnhgc = hash_lookup(pbr_nhg_hash, &find); |
487 | ||
488 | if (!pnhgc) { | |
489 | DEBUGD(&pbr_dbg_nht, | |
490 | "%s: Could not find nexthop-group cache w/ name '%s'", | |
491 | __PRETTY_FUNCTION__, name); | |
492 | return; | |
493 | } | |
494 | ||
495 | for (ALL_NEXTHOPS(nhgc->nhg, nhop)) { | |
496 | struct pbr_nexthop_cache lookup; | |
497 | struct pbr_nexthop_cache *pnhc; | |
498 | ||
b13e5ad6 | 499 | lookup.nexthop = nhop; |
e5c83d9b DS |
500 | pnhc = hash_lookup(pnhgc->nhh, &lookup); |
501 | if (!pnhc) { | |
502 | pnhc = hash_get(pnhgc->nhh, &lookup, pbr_nh_alloc); | |
503 | pnhc->parent = pnhgc; | |
504 | } | |
505 | } | |
506 | pbr_nht_install_nexthop_group(pnhgc, nhgc->nhg); | |
507 | } | |
508 | ||
509 | char *pbr_nht_nexthop_make_name(char *name, size_t l, | |
510 | uint32_t seqno, char *buffer) | |
511 | { | |
512 | snprintf(buffer, l, "%s%u", name, seqno); | |
513 | return buffer; | |
514 | } | |
515 | ||
b13e5ad6 | 516 | void pbr_nht_add_individual_nexthop(struct pbr_map_sequence *pbrms) |
e5c83d9b DS |
517 | { |
518 | struct pbr_nexthop_group_cache *pnhgc; | |
519 | struct pbr_nexthop_group_cache find; | |
520 | struct pbr_nexthop_cache *pnhc; | |
e5c83d9b DS |
521 | struct pbr_nexthop_cache lookup; |
522 | ||
e5c83d9b | 523 | memset(&find, 0, sizeof(find)); |
06210d1f | 524 | pbr_nht_nexthop_make_name(pbrms->parent->name, PBR_NHC_NAMELEN, |
e5c83d9b | 525 | pbrms->seqno, find.name); |
a4044dc1 QY |
526 | |
527 | if (!pbr_nht_get_next_tableid(true)) { | |
528 | zlog_warn( | |
529 | "%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'", | |
530 | __PRETTY_FUNCTION__, find.name); | |
531 | return; | |
532 | } | |
533 | ||
e5c83d9b DS |
534 | if (!pbrms->internal_nhg_name) |
535 | pbrms->internal_nhg_name = XSTRDUP(MTYPE_TMP, find.name); | |
536 | ||
537 | pnhgc = hash_get(pbr_nhg_hash, &find, pbr_nhgc_alloc); | |
538 | ||
b13e5ad6 | 539 | lookup.nexthop = pbrms->nhg->nexthop; |
e5c83d9b DS |
540 | pnhc = hash_get(pnhgc->nhh, &lookup, pbr_nh_alloc); |
541 | pnhc->parent = pnhgc; | |
542 | pbr_nht_install_nexthop_group(pnhgc, *pbrms->nhg); | |
543 | } | |
544 | ||
b13e5ad6 | 545 | void pbr_nht_delete_individual_nexthop(struct pbr_map_sequence *pbrms) |
e5c83d9b DS |
546 | { |
547 | struct pbr_nexthop_group_cache *pnhgc; | |
548 | struct pbr_nexthop_group_cache find; | |
549 | struct pbr_nexthop_cache *pnhc; | |
550 | struct pbr_nexthop_cache lup; | |
b13e5ad6 DS |
551 | struct pbr_map *pbrm = pbrms->parent; |
552 | struct listnode *node; | |
553 | struct pbr_map_interface *pmi; | |
e5c83d9b | 554 | struct nexthop *nh; |
aafac994 | 555 | enum nexthop_types_t nh_type = 0; |
e5c83d9b | 556 | |
b13e5ad6 DS |
557 | if (pbrm->valid && pbrms->nhs_installed && pbrm->incoming->count) { |
558 | for (ALL_LIST_ELEMENTS_RO(pbrm->incoming, node, pmi)) | |
559 | pbr_send_pbr_map(pbrms, pmi, false); | |
560 | } | |
561 | ||
562 | pbrm->valid = false; | |
563 | pbrms->nhs_installed = false; | |
b13e5ad6 | 564 | pbrms->reason |= PBR_MAP_INVALID_NO_NEXTHOPS; |
e5c83d9b DS |
565 | |
566 | memset(&find, 0, sizeof(find)); | |
6612590d | 567 | snprintf(find.name, sizeof(find.name), "%s", pbrms->internal_nhg_name); |
e5c83d9b DS |
568 | pnhgc = hash_lookup(pbr_nhg_hash, &find); |
569 | ||
570 | nh = pbrms->nhg->nexthop; | |
aafac994 | 571 | nh_type = nh->type; |
b13e5ad6 | 572 | lup.nexthop = nh; |
e5c83d9b DS |
573 | pnhc = hash_lookup(pnhgc->nhh, &lup); |
574 | pnhc->parent = NULL; | |
575 | hash_release(pnhgc->nhh, pnhc); | |
576 | pbr_nh_delete(&pnhc); | |
aafac994 | 577 | pbr_nht_uninstall_nexthop_group(pnhgc, *pbrms->nhg, nh_type); |
e5c83d9b DS |
578 | |
579 | hash_release(pbr_nhg_hash, pnhgc); | |
580 | ||
e5c83d9b DS |
581 | nexthop_group_delete(&pbrms->nhg); |
582 | XFREE(MTYPE_TMP, pbrms->internal_nhg_name); | |
583 | } | |
584 | ||
b13e5ad6 | 585 | struct pbr_nexthop_group_cache *pbr_nht_add_group(const char *name) |
e5c83d9b DS |
586 | { |
587 | struct nexthop *nhop; | |
588 | struct nexthop_group_cmd *nhgc; | |
589 | struct pbr_nexthop_group_cache *pnhgc; | |
590 | struct pbr_nexthop_group_cache lookup; | |
591 | ||
a4044dc1 QY |
592 | if (!pbr_nht_get_next_tableid(true)) { |
593 | zlog_warn( | |
594 | "%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'", | |
595 | __PRETTY_FUNCTION__, name); | |
596 | return NULL; | |
597 | } | |
598 | ||
e5c83d9b DS |
599 | nhgc = nhgc_find(name); |
600 | ||
601 | if (!nhgc) { | |
a4044dc1 QY |
602 | DEBUGD(&pbr_dbg_nht, "%s: Could not find nhgc with name: %s\n", |
603 | __PRETTY_FUNCTION__, name); | |
b13e5ad6 | 604 | return NULL; |
e5c83d9b DS |
605 | } |
606 | ||
6612590d | 607 | snprintf(lookup.name, sizeof(lookup.name), "%s", name); |
e5c83d9b DS |
608 | pnhgc = hash_get(pbr_nhg_hash, &lookup, pbr_nhgc_alloc); |
609 | DEBUGD(&pbr_dbg_nht, "%s: Retrieved NHGC @ %p", __PRETTY_FUNCTION__, | |
610 | pnhgc); | |
611 | ||
612 | for (ALL_NEXTHOPS(nhgc->nhg, nhop)) { | |
7fe96307 | 613 | struct pbr_nexthop_cache lookupc; |
e5c83d9b DS |
614 | struct pbr_nexthop_cache *pnhc; |
615 | ||
7fe96307 A |
616 | lookupc.nexthop = nhop; |
617 | pnhc = hash_lookup(pnhgc->nhh, &lookupc); | |
e5c83d9b | 618 | if (!pnhc) { |
7fe96307 | 619 | pnhc = hash_get(pnhgc->nhh, &lookupc, pbr_nh_alloc); |
e5c83d9b DS |
620 | pnhc->parent = pnhgc; |
621 | } | |
622 | } | |
b13e5ad6 DS |
623 | |
624 | return pnhgc; | |
e5c83d9b DS |
625 | } |
626 | ||
627 | void pbr_nht_delete_group(const char *name) | |
628 | { | |
629 | struct pbr_map_sequence *pbrms; | |
630 | struct listnode *snode; | |
631 | struct pbr_map *pbrm; | |
b13e5ad6 DS |
632 | struct pbr_nexthop_group_cache pnhgc_find; |
633 | struct pbr_nexthop_group_cache *pnhgc; | |
e5c83d9b DS |
634 | |
635 | RB_FOREACH (pbrm, pbr_map_entry_head, &pbr_maps) { | |
636 | for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, snode, pbrms)) { | |
637 | if (pbrms->nhgrp_name | |
b13e5ad6 | 638 | && strmatch(pbrms->nhgrp_name, name)) { |
e5c83d9b | 639 | pbrms->reason |= PBR_MAP_INVALID_NO_NEXTHOPS; |
b13e5ad6 DS |
640 | pbrms->nhg = NULL; |
641 | pbrms->internal_nhg_name = NULL; | |
e5c83d9b DS |
642 | pbrm->valid = false; |
643 | } | |
644 | } | |
645 | } | |
b13e5ad6 DS |
646 | |
647 | strlcpy(pnhgc_find.name, name, sizeof(pnhgc_find.name)); | |
648 | pnhgc = hash_release(pbr_nhg_hash, &pnhgc_find); | |
649 | pbr_nhgc_delete(pnhgc); | |
e5c83d9b DS |
650 | } |
651 | ||
652 | bool pbr_nht_nexthop_valid(struct nexthop_group *nhg) | |
653 | { | |
654 | DEBUGD(&pbr_dbg_nht, "%s: %p", __PRETTY_FUNCTION__, nhg); | |
655 | return true; | |
656 | } | |
657 | ||
658 | bool pbr_nht_nexthop_group_valid(const char *name) | |
659 | { | |
660 | struct pbr_nexthop_group_cache *pnhgc; | |
661 | struct pbr_nexthop_group_cache lookup; | |
662 | ||
663 | DEBUGD(&pbr_dbg_nht, "%s: %s", __PRETTY_FUNCTION__, name); | |
664 | ||
6612590d | 665 | snprintf(lookup.name, sizeof(lookup.name), "%s", name); |
e5c83d9b DS |
666 | pnhgc = hash_get(pbr_nhg_hash, &lookup, NULL); |
667 | if (!pnhgc) | |
668 | return false; | |
669 | DEBUGD(&pbr_dbg_nht, "%s: \t%d %d", __PRETTY_FUNCTION__, pnhgc->valid, | |
670 | pnhgc->installed); | |
671 | if (pnhgc->valid && pnhgc->installed) | |
672 | return true; | |
673 | ||
674 | return false; | |
675 | } | |
676 | ||
677 | struct pbr_nht_individual { | |
678 | struct zapi_route *nhr; | |
a106a408 | 679 | struct interface *ifp; |
e5c83d9b DS |
680 | |
681 | uint32_t valid; | |
682 | }; | |
683 | ||
8babeb1a SW |
684 | static bool |
685 | pbr_nht_individual_nexthop_gw_update(struct pbr_nexthop_cache *pnhc, | |
686 | const struct pbr_nht_individual *pnhi) | |
e5c83d9b | 687 | { |
8babeb1a | 688 | bool is_valid = pnhc->valid; |
e5c83d9b | 689 | |
8babeb1a SW |
690 | if (!pnhi->nhr) /* It doesn't care about non-nexthop updates */ |
691 | goto done; | |
e5c83d9b DS |
692 | |
693 | switch (pnhi->nhr->prefix.family) { | |
694 | case AF_INET: | |
b13e5ad6 | 695 | if (pnhc->nexthop->gate.ipv4.s_addr |
8babeb1a SW |
696 | != pnhi->nhr->prefix.u.prefix4.s_addr) |
697 | goto done; /* Unrelated change */ | |
e5c83d9b DS |
698 | break; |
699 | case AF_INET6: | |
b13e5ad6 DS |
700 | if (memcmp(&pnhc->nexthop->gate.ipv6, |
701 | &pnhi->nhr->prefix.u.prefix6, 16) | |
8babeb1a SW |
702 | != 0) |
703 | goto done; /* Unrelated change */ | |
704 | break; | |
705 | } | |
706 | ||
707 | if (!pnhi->nhr->nexthop_num) { | |
708 | is_valid = false; | |
709 | goto done; | |
710 | } | |
711 | ||
712 | if (pnhc->nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX | |
4550d5df | 713 | || pnhc->nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) { |
8babeb1a SW |
714 | |
715 | /* GATEWAY_IFINDEX type shouldn't resolve to group */ | |
716 | if (pnhi->nhr->nexthop_num > 1) { | |
717 | is_valid = false; | |
718 | goto done; | |
719 | } | |
720 | ||
721 | /* If whatever we resolved to wasn't on the interface we | |
722 | * specified. (i.e. not a connected route), its invalid. | |
723 | */ | |
724 | if (pnhi->nhr->nexthops[0].ifindex != pnhc->nexthop->ifindex) { | |
725 | is_valid = false; | |
726 | goto done; | |
727 | } | |
728 | } | |
729 | ||
730 | is_valid = true; | |
731 | ||
732 | done: | |
733 | pnhc->valid = is_valid; | |
734 | ||
735 | return pnhc->valid; | |
736 | } | |
737 | ||
738 | static bool pbr_nht_individual_nexthop_interface_update( | |
739 | struct pbr_nexthop_cache *pnhc, const struct pbr_nht_individual *pnhi) | |
740 | { | |
741 | bool is_valid = pnhc->valid; | |
742 | ||
743 | if (!pnhi->ifp) /* It doesn't care about non-interface updates */ | |
744 | goto done; | |
745 | ||
746 | if (pnhc->nexthop->ifindex | |
747 | != pnhi->ifp->ifindex) /* Un-related interface */ | |
748 | goto done; | |
749 | ||
750 | is_valid = !!if_is_up(pnhi->ifp); | |
751 | ||
752 | done: | |
753 | pnhc->valid = is_valid; | |
754 | ||
755 | return pnhc->valid; | |
756 | } | |
757 | ||
758 | /* Given this update either from interface or nexthop tracking, re-validate this | |
759 | * nexthop. | |
760 | * | |
761 | * If the update is un-related, the subroutines shoud just return their cached | |
762 | * valid state. | |
763 | */ | |
764 | static void | |
765 | pbr_nht_individual_nexthop_update(struct pbr_nexthop_cache *pnhc, | |
766 | const struct pbr_nht_individual *pnhi) | |
767 | { | |
768 | assert(pnhi->nhr || pnhi->ifp); /* Either nexthop or interface update */ | |
769 | ||
770 | switch (pnhc->nexthop->type) { | |
771 | case NEXTHOP_TYPE_IFINDEX: | |
772 | pbr_nht_individual_nexthop_interface_update(pnhc, pnhi); | |
773 | break; | |
cb254f41 SW |
774 | case NEXTHOP_TYPE_IPV6_IFINDEX: |
775 | if (IN6_IS_ADDR_LINKLOCAL(&pnhc->nexthop->gate.ipv6)) { | |
776 | pbr_nht_individual_nexthop_interface_update(pnhc, pnhi); | |
777 | break; | |
778 | } | |
779 | /* Intentional fall thru */ | |
780 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
8babeb1a SW |
781 | case NEXTHOP_TYPE_IPV4: |
782 | case NEXTHOP_TYPE_IPV6: | |
8babeb1a SW |
783 | pbr_nht_individual_nexthop_gw_update(pnhc, pnhi); |
784 | break; | |
785 | case NEXTHOP_TYPE_BLACKHOLE: | |
786 | pnhc->valid = true; | |
e5c83d9b DS |
787 | break; |
788 | } | |
8babeb1a SW |
789 | } |
790 | ||
791 | static void pbr_nht_individual_nexthop_update_lookup(struct hash_bucket *b, | |
792 | void *data) | |
793 | { | |
794 | struct pbr_nexthop_cache *pnhc = b->data; | |
795 | struct pbr_nht_individual *pnhi = data; | |
796 | char buf[PREFIX_STRLEN]; | |
797 | bool old_valid; | |
798 | ||
799 | old_valid = pnhc->valid; | |
800 | ||
801 | pbr_nht_individual_nexthop_update(pnhc, pnhi); | |
e5c83d9b DS |
802 | |
803 | DEBUGD(&pbr_dbg_nht, "\tFound %s: old: %d new: %d", | |
804 | prefix2str(&pnhi->nhr->prefix, buf, sizeof(buf)), old_valid, | |
805 | pnhc->valid); | |
806 | ||
e5c83d9b DS |
807 | if (pnhc->valid) |
808 | pnhi->valid += 1; | |
809 | } | |
810 | ||
b822b93a SW |
811 | static void pbr_nexthop_group_cache_iterate_to_group(struct hash_bucket *b, |
812 | void *data) | |
813 | { | |
814 | struct pbr_nexthop_cache *pnhc = b->data; | |
815 | struct nexthop_group *nhg = data; | |
816 | struct nexthop *nh = NULL; | |
817 | ||
818 | copy_nexthops(&nh, pnhc->nexthop, NULL); | |
819 | ||
50d89650 | 820 | _nexthop_add(&nhg->nexthop, nh); |
b822b93a SW |
821 | } |
822 | ||
823 | static void | |
824 | pbr_nexthop_group_cache_to_nexthop_group(struct nexthop_group *nhg, | |
825 | struct pbr_nexthop_group_cache *pnhgc) | |
826 | { | |
827 | hash_iterate(pnhgc->nhh, pbr_nexthop_group_cache_iterate_to_group, nhg); | |
828 | } | |
829 | ||
e3b78da8 | 830 | static void pbr_nht_nexthop_update_lookup(struct hash_bucket *b, void *data) |
e5c83d9b DS |
831 | { |
832 | struct pbr_nexthop_group_cache *pnhgc = b->data; | |
8babeb1a | 833 | struct pbr_nht_individual pnhi = {}; |
b822b93a | 834 | struct nexthop_group nhg = {}; |
b13e5ad6 DS |
835 | bool old_valid; |
836 | ||
837 | old_valid = pnhgc->valid; | |
e5c83d9b DS |
838 | |
839 | pnhi.nhr = (struct zapi_route *)data; | |
840 | pnhi.valid = 0; | |
841 | hash_iterate(pnhgc->nhh, pbr_nht_individual_nexthop_update_lookup, | |
842 | &pnhi); | |
843 | ||
844 | /* | |
845 | * If any of the specified nexthops are valid we are valid | |
846 | */ | |
847 | pnhgc->valid = !!pnhi.valid; | |
b13e5ad6 | 848 | |
b822b93a SW |
849 | if (pnhgc->valid) { |
850 | pbr_nexthop_group_cache_to_nexthop_group(&nhg, pnhgc); | |
851 | pbr_nht_install_nexthop_group(pnhgc, nhg); | |
852 | /* Don't need copied nexthops anymore */ | |
853 | nexthops_free(nhg.nexthop); | |
854 | } | |
855 | ||
b13e5ad6 DS |
856 | if (old_valid != pnhgc->valid) |
857 | pbr_map_check_nh_group_change(pnhgc->name); | |
e5c83d9b DS |
858 | } |
859 | ||
860 | void pbr_nht_nexthop_update(struct zapi_route *nhr) | |
861 | { | |
862 | hash_iterate(pbr_nhg_hash, pbr_nht_nexthop_update_lookup, nhr); | |
863 | } | |
864 | ||
a106a408 RW |
865 | static void |
866 | pbr_nht_individual_nexthop_interface_update_lookup(struct hash_backet *b, | |
867 | void *data) | |
868 | { | |
869 | struct pbr_nexthop_cache *pnhc = b->data; | |
870 | struct pbr_nht_individual *pnhi = data; | |
871 | bool old_valid; | |
872 | ||
873 | old_valid = pnhc->valid; | |
874 | ||
8babeb1a | 875 | pbr_nht_individual_nexthop_update(pnhc, pnhi); |
a106a408 RW |
876 | |
877 | DEBUGD(&pbr_dbg_nht, "\tFound %s: old: %d new: %d", pnhi->ifp->name, | |
878 | old_valid, pnhc->valid); | |
879 | ||
880 | if (pnhc->valid) | |
881 | pnhi->valid += 1; | |
882 | } | |
883 | ||
884 | static void pbr_nht_nexthop_interface_update_lookup(struct hash_backet *b, | |
885 | void *data) | |
886 | { | |
887 | struct pbr_nexthop_group_cache *pnhgc = b->data; | |
8babeb1a | 888 | struct pbr_nht_individual pnhi = {}; |
a106a408 RW |
889 | bool old_valid; |
890 | ||
891 | old_valid = pnhgc->valid; | |
892 | ||
893 | pnhi.ifp = data; | |
894 | pnhi.valid = 0; | |
895 | hash_iterate(pnhgc->nhh, | |
896 | pbr_nht_individual_nexthop_interface_update_lookup, &pnhi); | |
897 | ||
898 | /* | |
899 | * If any of the specified nexthops are valid we are valid | |
900 | */ | |
901 | pnhgc->valid = !!pnhi.valid; | |
902 | ||
903 | if (old_valid != pnhgc->valid) | |
904 | pbr_map_check_nh_group_change(pnhgc->name); | |
905 | } | |
906 | ||
907 | void pbr_nht_nexthop_interface_update(struct interface *ifp) | |
908 | { | |
909 | hash_iterate(pbr_nhg_hash, pbr_nht_nexthop_interface_update_lookup, | |
910 | ifp); | |
911 | } | |
912 | ||
d8b87afe | 913 | static uint32_t pbr_nhg_hash_key(const void *arg) |
e5c83d9b | 914 | { |
d8b87afe | 915 | const struct pbr_nexthop_group_cache *nhgc = arg; |
e5c83d9b DS |
916 | |
917 | return jhash(&nhgc->name, strlen(nhgc->name), 0x52c34a96); | |
918 | } | |
919 | ||
74df8d6d | 920 | static bool pbr_nhg_hash_equal(const void *arg1, const void *arg2) |
e5c83d9b DS |
921 | { |
922 | const struct pbr_nexthop_group_cache *nhgc1 = | |
923 | (const struct pbr_nexthop_group_cache *)arg1; | |
924 | const struct pbr_nexthop_group_cache *nhgc2 = | |
925 | (const struct pbr_nexthop_group_cache *)arg2; | |
926 | ||
927 | return !strcmp(nhgc1->name, nhgc2->name); | |
928 | } | |
929 | ||
a4044dc1 | 930 | uint32_t pbr_nht_get_next_tableid(bool peek) |
e5c83d9b DS |
931 | { |
932 | uint32_t i; | |
933 | bool found = false; | |
934 | ||
935 | for (i = pbr_nhg_low_table; i <= pbr_nhg_high_table; i++) { | |
d8729f8c | 936 | if (!nhg_tableid[i]) { |
e5c83d9b DS |
937 | found = true; |
938 | break; | |
939 | } | |
940 | } | |
941 | ||
942 | if (found) { | |
a4044dc1 | 943 | nhg_tableid[i] = !peek; |
e5c83d9b DS |
944 | return i; |
945 | } else | |
946 | return 0; | |
947 | } | |
948 | ||
949 | void pbr_nht_set_tableid_range(uint32_t low, uint32_t high) | |
950 | { | |
951 | pbr_nhg_low_table = low; | |
952 | pbr_nhg_high_table = high; | |
953 | } | |
954 | ||
955 | void pbr_nht_write_table_range(struct vty *vty) | |
956 | { | |
957 | if (pbr_nhg_low_table != PBR_NHT_DEFAULT_LOW_TABLEID | |
958 | || pbr_nhg_high_table != PBR_NHT_DEFAULT_HIGH_TABLEID) { | |
959 | vty_out(vty, "pbr table range %u %u\n", pbr_nhg_low_table, | |
960 | pbr_nhg_high_table); | |
961 | } | |
962 | } | |
963 | ||
964 | uint32_t pbr_nht_get_next_rule(uint32_t seqno) | |
965 | { | |
966 | return seqno + pbr_nhg_low_rule - 1; | |
967 | } | |
968 | void pbr_nht_set_rule_range(uint32_t low, uint32_t high) | |
969 | { | |
970 | pbr_nhg_low_rule = low; | |
971 | pbr_nhg_high_rule = high; | |
972 | } | |
973 | ||
974 | void pbr_nht_write_rule_range(struct vty *vty) | |
975 | { | |
976 | if (pbr_nhg_low_rule != PBR_NHT_DEFAULT_LOW_RULE | |
977 | || pbr_nhg_high_rule != PBR_NHT_DEFAULT_HIGH_RULE) { | |
978 | vty_out(vty, "pbr rule range %u %u\n", pbr_nhg_low_rule, | |
979 | pbr_nhg_high_rule); | |
980 | } | |
981 | } | |
982 | ||
983 | uint32_t pbr_nht_get_table(const char *name) | |
984 | { | |
985 | struct pbr_nexthop_group_cache find; | |
986 | struct pbr_nexthop_group_cache *pnhgc; | |
987 | ||
988 | memset(&find, 0, sizeof(find)); | |
6612590d | 989 | snprintf(find.name, sizeof(find.name), "%s", name); |
e5c83d9b DS |
990 | pnhgc = hash_lookup(pbr_nhg_hash, &find); |
991 | ||
992 | if (!pnhgc) { | |
993 | DEBUGD(&pbr_dbg_nht, | |
994 | "%s: Could not find nexthop-group cache w/ name '%s'", | |
995 | __PRETTY_FUNCTION__, name); | |
996 | return 5000; | |
997 | } | |
998 | ||
999 | return pnhgc->table_id; | |
1000 | } | |
1001 | ||
1002 | bool pbr_nht_get_installed(const char *name) | |
1003 | { | |
1004 | struct pbr_nexthop_group_cache find; | |
1005 | struct pbr_nexthop_group_cache *pnhgc; | |
1006 | ||
1007 | memset(&find, 0, sizeof(find)); | |
6612590d | 1008 | snprintf(find.name, sizeof(find.name), "%s", name); |
e5c83d9b DS |
1009 | |
1010 | pnhgc = hash_lookup(pbr_nhg_hash, &find); | |
1011 | ||
d3765386 | 1012 | if (!pnhgc) |
e5c83d9b | 1013 | return false; |
e5c83d9b DS |
1014 | |
1015 | return pnhgc->installed; | |
1016 | } | |
1017 | ||
e3b78da8 | 1018 | static void pbr_nht_show_nhg_nexthops(struct hash_bucket *b, void *data) |
e5c83d9b DS |
1019 | { |
1020 | struct pbr_nexthop_cache *pnhc = b->data; | |
1021 | struct vty *vty = data; | |
1022 | ||
57cdafc4 | 1023 | vty_out(vty, "\tValid: %d ", pnhc->valid); |
b13e5ad6 | 1024 | nexthop_group_write_nexthop(vty, pnhc->nexthop); |
e5c83d9b DS |
1025 | } |
1026 | ||
1027 | struct pbr_nht_show { | |
1028 | struct vty *vty; | |
1029 | const char *name; | |
1030 | }; | |
1031 | ||
e3b78da8 | 1032 | static void pbr_nht_show_nhg(struct hash_bucket *b, void *data) |
e5c83d9b DS |
1033 | { |
1034 | struct pbr_nexthop_group_cache *pnhgc = b->data; | |
1035 | struct pbr_nht_show *pns = data; | |
1036 | struct vty *vty; | |
1037 | ||
1038 | if (pns->name && strcmp(pns->name, pnhgc->name) != 0) | |
1039 | return; | |
1040 | ||
1041 | vty = pns->vty; | |
1042 | vty_out(vty, "Nexthop-Group: %s Table: %u Valid: %d Installed: %d\n", | |
1043 | pnhgc->name, pnhgc->table_id, pnhgc->valid, pnhgc->installed); | |
1044 | ||
1045 | hash_iterate(pnhgc->nhh, pbr_nht_show_nhg_nexthops, vty); | |
1046 | } | |
1047 | ||
1048 | void pbr_nht_show_nexthop_group(struct vty *vty, const char *name) | |
1049 | { | |
1050 | struct pbr_nht_show pns; | |
1051 | ||
1052 | pns.vty = vty; | |
1053 | pns.name = name; | |
1054 | ||
1055 | hash_iterate(pbr_nhg_hash, pbr_nht_show_nhg, &pns); | |
1056 | } | |
1057 | ||
1058 | void pbr_nht_init(void) | |
1059 | { | |
1060 | pbr_nhg_hash = hash_create_size( | |
1061 | 16, pbr_nhg_hash_key, pbr_nhg_hash_equal, "PBR NHG Cache Hash"); | |
b13e5ad6 | 1062 | pbr_nhrc_hash = |
d8b87afe | 1063 | hash_create_size(16, (unsigned int (*)(const void *))nexthop_hash, |
b13e5ad6 | 1064 | pbr_nhrc_hash_equal, "PBR NH Hash"); |
e5c83d9b DS |
1065 | |
1066 | pbr_nhg_low_table = PBR_NHT_DEFAULT_LOW_TABLEID; | |
1067 | pbr_nhg_high_table = PBR_NHT_DEFAULT_HIGH_TABLEID; | |
1068 | pbr_nhg_low_rule = PBR_NHT_DEFAULT_LOW_RULE; | |
1069 | pbr_nhg_high_rule = PBR_NHT_DEFAULT_HIGH_RULE; | |
1070 | memset(&nhg_tableid, 0, 65535 * sizeof(uint8_t)); | |
1071 | } |