]>
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> | |
24 | #include <nexthop_group.h> | |
25 | #include <hash.h> | |
26 | #include <jhash.h> | |
27 | #include <vty.h> | |
28 | #include <zclient.h> | |
b13e5ad6 | 29 | #include <debug.h> |
e5c83d9b DS |
30 | |
31 | #include "pbrd/pbr_nht.h" | |
32 | #include "pbrd/pbr_map.h" | |
e5c83d9b DS |
33 | #include "pbrd/pbr_zebra.h" |
34 | #include "pbrd/pbr_memory.h" | |
35 | #include "pbrd/pbr_debug.h" | |
36 | ||
37 | DEFINE_MTYPE_STATIC(PBRD, PBR_NHG, "PBR Nexthop Groups") | |
38 | ||
39 | static struct hash *pbr_nhg_hash; | |
b13e5ad6 | 40 | static struct hash *pbr_nhrc_hash; |
e5c83d9b DS |
41 | |
42 | static uint32_t pbr_nhg_low_table; | |
43 | static uint32_t pbr_nhg_high_table; | |
44 | static uint32_t pbr_nhg_low_rule; | |
45 | static uint32_t pbr_nhg_high_rule; | |
46 | static bool nhg_tableid[65535]; | |
47 | ||
b13e5ad6 DS |
48 | static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache *pnhgc, |
49 | struct nexthop_group nhg); | |
ff9799c3 DS |
50 | static void |
51 | pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache *pnhgc, | |
52 | struct nexthop_group nhg, | |
53 | enum nexthop_types_t nh_afi); | |
b13e5ad6 DS |
54 | |
55 | /* | |
56 | * Nexthop refcount. | |
57 | */ | |
58 | struct nhrc { | |
59 | struct nexthop nexthop; | |
60 | unsigned int refcount; | |
61 | }; | |
62 | ||
63 | /* Hash functions for pbr_nhrc_hash ---------------------------------------- */ | |
64 | ||
65 | static void *pbr_nhrc_hash_alloc(void *p) | |
66 | { | |
67 | struct nhrc *nhrc = XCALLOC(MTYPE_PBR_NHG, sizeof(struct nhrc)); | |
68 | nhrc->nexthop = *(struct nexthop *)p; | |
69 | return nhrc; | |
70 | } | |
71 | ||
72 | static int pbr_nhrc_hash_equal(const void *arg1, const void *arg2) | |
73 | { | |
74 | const struct nexthop *nh1, *nh2; | |
75 | ||
76 | nh1 = arg1; | |
77 | nh2 = arg2; | |
78 | ||
79 | return nexthop_same(nh1, nh2); | |
80 | } | |
81 | ||
82 | /* ------------------------------------------------------------------------- */ | |
83 | ||
e5c83d9b DS |
84 | static void *pbr_nh_alloc(void *p) |
85 | { | |
86 | struct pbr_nexthop_cache *new; | |
87 | struct pbr_nexthop_cache *pnhc = (struct pbr_nexthop_cache *)p; | |
b13e5ad6 | 88 | struct nhrc *nhrc; |
e5c83d9b DS |
89 | |
90 | new = XCALLOC(MTYPE_PBR_NHG, sizeof(*new)); | |
b13e5ad6 DS |
91 | nhrc = hash_get(pbr_nhrc_hash, pnhc->nexthop, pbr_nhrc_hash_alloc); |
92 | new->nexthop = &nhrc->nexthop; | |
93 | ||
94 | /* Decremented again in pbr_nh_delete */ | |
95 | ++nhrc->refcount; | |
e5c83d9b DS |
96 | |
97 | DEBUGD(&pbr_dbg_nht, "%s: Sending nexthop to Zebra", | |
98 | __PRETTY_FUNCTION__); | |
99 | ||
b13e5ad6 | 100 | pbr_send_rnh(new->nexthop, true); |
e5c83d9b DS |
101 | |
102 | new->valid = false; | |
103 | return new; | |
104 | } | |
105 | ||
106 | static void pbr_nh_delete(struct pbr_nexthop_cache **pnhc) | |
107 | { | |
b13e5ad6 DS |
108 | struct nhrc *nhrc; |
109 | ||
110 | nhrc = hash_lookup(pbr_nhrc_hash, (*pnhc)->nexthop); | |
111 | ||
112 | if (nhrc) | |
113 | --nhrc->refcount; | |
114 | if (!nhrc || nhrc->refcount == 0) { | |
115 | DEBUGD(&pbr_dbg_nht, "%s: Removing nexthop from Zebra", | |
116 | __PRETTY_FUNCTION__); | |
117 | pbr_send_rnh((*pnhc)->nexthop, false); | |
118 | } | |
119 | if (nhrc && nhrc->refcount == 0) { | |
120 | hash_release(pbr_nhrc_hash, nhrc); | |
121 | XFREE(MTYPE_PBR_NHG, nhrc); | |
122 | } | |
e5c83d9b DS |
123 | |
124 | XFREE(MTYPE_PBR_NHG, *pnhc); | |
125 | } | |
126 | ||
b13e5ad6 DS |
127 | static void pbr_nh_delete_iterate(struct hash_backet *b, void *p) |
128 | { | |
129 | pbr_nh_delete((struct pbr_nexthop_cache **)&b->data); | |
130 | } | |
131 | ||
e5c83d9b DS |
132 | static uint32_t pbr_nh_hash_key(void *arg) |
133 | { | |
134 | uint32_t key; | |
135 | struct pbr_nexthop_cache *pbrnc = (struct pbr_nexthop_cache *)arg; | |
136 | ||
b13e5ad6 | 137 | key = nexthop_hash(pbrnc->nexthop); |
e5c83d9b DS |
138 | |
139 | return key; | |
140 | } | |
141 | ||
142 | static int pbr_nh_hash_equal(const void *arg1, const void *arg2) | |
143 | { | |
144 | const struct pbr_nexthop_cache *pbrnc1 = | |
145 | (const struct pbr_nexthop_cache *)arg1; | |
146 | const struct pbr_nexthop_cache *pbrnc2 = | |
147 | (const struct pbr_nexthop_cache *)arg2; | |
148 | ||
b13e5ad6 | 149 | if (pbrnc1->nexthop->vrf_id != pbrnc2->nexthop->vrf_id) |
e5c83d9b DS |
150 | return 0; |
151 | ||
b13e5ad6 | 152 | if (pbrnc1->nexthop->ifindex != pbrnc2->nexthop->ifindex) |
e5c83d9b DS |
153 | return 0; |
154 | ||
b13e5ad6 | 155 | if (pbrnc1->nexthop->type != pbrnc2->nexthop->type) |
e5c83d9b DS |
156 | return 0; |
157 | ||
b13e5ad6 | 158 | switch (pbrnc1->nexthop->type) { |
e5c83d9b DS |
159 | case NEXTHOP_TYPE_IFINDEX: |
160 | return 1; | |
161 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
162 | case NEXTHOP_TYPE_IPV4: | |
b13e5ad6 DS |
163 | return pbrnc1->nexthop->gate.ipv4.s_addr |
164 | == pbrnc2->nexthop->gate.ipv4.s_addr; | |
e5c83d9b DS |
165 | case NEXTHOP_TYPE_IPV6_IFINDEX: |
166 | case NEXTHOP_TYPE_IPV6: | |
b13e5ad6 DS |
167 | return !memcmp(&pbrnc1->nexthop->gate.ipv6, |
168 | &pbrnc2->nexthop->gate.ipv6, 16); | |
e5c83d9b | 169 | case NEXTHOP_TYPE_BLACKHOLE: |
b13e5ad6 | 170 | return pbrnc1->nexthop->bh_type == pbrnc2->nexthop->bh_type; |
e5c83d9b DS |
171 | } |
172 | ||
173 | /* | |
174 | * We should not get here | |
175 | */ | |
176 | return 0; | |
177 | } | |
178 | ||
b13e5ad6 DS |
179 | static void pbr_nhgc_delete(struct pbr_nexthop_group_cache *p) |
180 | { | |
181 | hash_iterate(p->nhh, pbr_nh_delete_iterate, NULL); | |
182 | hash_free(p->nhh); | |
183 | XFREE(MTYPE_PBR_NHG, p); | |
184 | } | |
185 | ||
186 | static void *pbr_nhgc_alloc(void *p) | |
187 | { | |
188 | struct pbr_nexthop_group_cache *new; | |
189 | struct pbr_nexthop_group_cache *pnhgc = | |
190 | (struct pbr_nexthop_group_cache *)p; | |
191 | ||
192 | new = XCALLOC(MTYPE_PBR_NHG, sizeof(*new)); | |
193 | ||
194 | strcpy(new->name, pnhgc->name); | |
195 | new->table_id = pbr_nht_get_next_tableid(); | |
196 | ||
197 | DEBUGD(&pbr_dbg_nht, "%s: NHT: %s assigned Table ID: %u", | |
198 | __PRETTY_FUNCTION__, new->name, new->table_id); | |
199 | ||
200 | new->nhh = hash_create_size(8, pbr_nh_hash_key, pbr_nh_hash_equal, | |
201 | "PBR NH Cache Hash"); | |
202 | return new; | |
203 | } | |
204 | ||
205 | ||
e5c83d9b DS |
206 | void pbr_nhgroup_add_cb(const char *name) |
207 | { | |
b13e5ad6 DS |
208 | struct pbr_nexthop_group_cache *pnhgc; |
209 | struct nexthop_group_cmd *nhgc; | |
e5c83d9b | 210 | |
b13e5ad6 | 211 | nhgc = nhgc_find(name); |
68a63f60 QY |
212 | |
213 | if (!nhgc) { | |
214 | DEBUGD(&pbr_dbg_nht, "%s: Could not find nhgc with name: %s\n", | |
215 | __PRETTY_FUNCTION__, name); | |
216 | return; | |
217 | } | |
218 | ||
b13e5ad6 | 219 | pnhgc = pbr_nht_add_group(name); |
e5c83d9b | 220 | |
b13e5ad6 | 221 | DEBUGD(&pbr_dbg_nht, "%s: Added nexthop-group %s", __PRETTY_FUNCTION__, |
e5c83d9b | 222 | name); |
b13e5ad6 DS |
223 | |
224 | pbr_nht_install_nexthop_group(pnhgc, nhgc->nhg); | |
225 | pbr_map_check_nh_group_change(name); | |
e5c83d9b DS |
226 | } |
227 | ||
b13e5ad6 | 228 | void pbr_nhgroup_add_nexthop_cb(const struct nexthop_group_cmd *nhgc, |
e5c83d9b DS |
229 | const struct nexthop *nhop) |
230 | { | |
b13e5ad6 DS |
231 | char debugstr[256]; |
232 | struct pbr_nexthop_group_cache pnhgc_find = {}; | |
233 | struct pbr_nexthop_group_cache *pnhgc; | |
234 | struct pbr_nexthop_cache pnhc_find = {}; | |
235 | struct pbr_nexthop_cache *pnhc; | |
236 | ||
237 | /* find pnhgc by name */ | |
238 | strlcpy(pnhgc_find.name, nhgc->name, sizeof(pnhgc_find.name)); | |
239 | pnhgc = hash_get(pbr_nhg_hash, &pnhgc_find, pbr_nhgc_alloc); | |
e5c83d9b | 240 | |
b13e5ad6 DS |
241 | /* create & insert new pnhc into pnhgc->nhh */ |
242 | pnhc_find.nexthop = (struct nexthop *)nhop; | |
243 | pnhc = hash_get(pnhgc->nhh, &pnhc_find, pbr_nh_alloc); | |
244 | pnhc_find.nexthop = NULL; | |
245 | ||
246 | /* set parent pnhgc */ | |
247 | pnhc->parent = pnhgc; | |
e5c83d9b | 248 | |
b13e5ad6 DS |
249 | if (DEBUG_MODE_CHECK(&pbr_dbg_nht, DEBUG_MODE_ALL)) { |
250 | nexthop2str(nhop, debugstr, sizeof(debugstr)); | |
251 | DEBUGD(&pbr_dbg_nht, "%s: Added %s to nexthop-group %s", | |
252 | __PRETTY_FUNCTION__, debugstr, nhgc->name); | |
253 | } | |
254 | ||
255 | pbr_nht_install_nexthop_group(pnhgc, nhgc->nhg); | |
256 | pbr_map_check_nh_group_change(nhgc->name); | |
e5c83d9b DS |
257 | } |
258 | ||
b13e5ad6 | 259 | void pbr_nhgroup_del_nexthop_cb(const struct nexthop_group_cmd *nhgc, |
e5c83d9b DS |
260 | const struct nexthop *nhop) |
261 | { | |
b13e5ad6 DS |
262 | char debugstr[256]; |
263 | struct pbr_nexthop_group_cache pnhgc_find = {}; | |
264 | struct pbr_nexthop_group_cache *pnhgc; | |
265 | struct pbr_nexthop_cache pnhc_find = {}; | |
266 | struct pbr_nexthop_cache *pnhc; | |
ff9799c3 | 267 | enum nexthop_types_t nh_afi = nhop->type; |
b13e5ad6 DS |
268 | |
269 | /* find pnhgc by name */ | |
270 | strlcpy(pnhgc_find.name, nhgc->name, sizeof(pnhgc_find.name)); | |
271 | pnhgc = hash_get(pbr_nhg_hash, &pnhgc_find, pbr_nhgc_alloc); | |
272 | ||
273 | /* delete pnhc from pnhgc->nhh */ | |
274 | pnhc_find.nexthop = (struct nexthop *)nhop; | |
275 | pnhc = hash_release(pnhgc->nhh, &pnhc_find); | |
276 | ||
277 | /* delete pnhc */ | |
278 | pbr_nh_delete(&pnhc); | |
e5c83d9b | 279 | |
b13e5ad6 DS |
280 | if (DEBUG_MODE_CHECK(&pbr_dbg_nht, DEBUG_MODE_ALL)) { |
281 | nexthop2str(nhop, debugstr, sizeof(debugstr)); | |
282 | DEBUGD(&pbr_dbg_nht, "%s: Removed %s from nexthop-group %s", | |
283 | __PRETTY_FUNCTION__, debugstr, nhgc->name); | |
284 | } | |
e5c83d9b | 285 | |
ff9799c3 DS |
286 | if (pnhgc->nhh->count) |
287 | pbr_nht_install_nexthop_group(pnhgc, nhgc->nhg); | |
288 | else | |
289 | pbr_nht_uninstall_nexthop_group(pnhgc, nhgc->nhg, nh_afi); | |
290 | ||
b13e5ad6 | 291 | pbr_map_check_nh_group_change(nhgc->name); |
e5c83d9b DS |
292 | } |
293 | ||
294 | void pbr_nhgroup_delete_cb(const char *name) | |
295 | { | |
b13e5ad6 | 296 | DEBUGD(&pbr_dbg_nht, "%s: Removed nexthop-group %s", |
e5c83d9b | 297 | __PRETTY_FUNCTION__, name); |
b13e5ad6 | 298 | |
ff9799c3 DS |
299 | /* delete group from all pbrms's */ |
300 | pbr_nht_delete_group(name); | |
301 | ||
b13e5ad6 | 302 | pbr_map_check_nh_group_change(name); |
e5c83d9b DS |
303 | } |
304 | ||
305 | #if 0 | |
306 | static struct pbr_nexthop_cache *pbr_nht_lookup_nexthop(struct nexthop *nexthop) | |
307 | { | |
308 | return NULL; | |
309 | } | |
310 | #endif | |
311 | ||
312 | static void pbr_nht_find_nhg_from_table_install(struct hash_backet *b, | |
313 | void *data) | |
314 | { | |
315 | struct pbr_nexthop_group_cache *pnhgc = | |
316 | (struct pbr_nexthop_group_cache *)b->data; | |
317 | uint32_t *table_id = (uint32_t *)data; | |
318 | ||
319 | if (pnhgc->table_id == *table_id) { | |
320 | DEBUGD(&pbr_dbg_nht, "%s: Table ID (%u) matches %s", | |
321 | __PRETTY_FUNCTION__, *table_id, pnhgc->name); | |
2fb7892e DS |
322 | |
323 | /* | |
324 | * If the table has been re-handled by zebra | |
325 | * and we are already installed no need to do | |
326 | * anything here. | |
327 | */ | |
328 | if (!pnhgc->installed) { | |
329 | pnhgc->installed = true; | |
330 | pbr_map_schedule_policy_from_nhg(pnhgc->name); | |
331 | } | |
e5c83d9b DS |
332 | } |
333 | } | |
334 | ||
335 | void pbr_nht_route_installed_for_table(uint32_t table_id) | |
336 | { | |
337 | hash_iterate(pbr_nhg_hash, pbr_nht_find_nhg_from_table_install, | |
338 | &table_id); | |
339 | } | |
340 | ||
341 | static void pbr_nht_find_nhg_from_table_remove(struct hash_backet *b, | |
342 | void *data) | |
343 | { | |
344 | ; | |
345 | } | |
346 | ||
347 | void pbr_nht_route_removed_for_table(uint32_t table_id) | |
348 | { | |
349 | hash_iterate(pbr_nhg_hash, pbr_nht_find_nhg_from_table_remove, | |
350 | &table_id); | |
351 | } | |
352 | ||
353 | /* | |
354 | * Loop through all nexthops in a nexthop group to check that they are all the | |
355 | * same. If they are not all the same, log this peculiarity. | |
356 | * | |
357 | * nhg | |
358 | * The nexthop group to check | |
359 | * | |
360 | * Returns: | |
361 | * - AFI of last nexthop in the group | |
362 | * - AFI_MAX on error | |
363 | */ | |
ff9799c3 DS |
364 | static afi_t pbr_nht_which_afi(struct nexthop_group nhg, |
365 | enum nexthop_types_t nh_afi) | |
e5c83d9b DS |
366 | { |
367 | struct nexthop *nexthop; | |
368 | afi_t install_afi = AFI_MAX; | |
369 | bool v6, v4, bh; | |
d3765386 | 370 | |
e5c83d9b DS |
371 | v6 = v4 = bh = false; |
372 | ||
ff9799c3 DS |
373 | if (!nh_afi) { |
374 | for (ALL_NEXTHOPS(nhg, nexthop)) { | |
375 | nh_afi = nexthop->type; | |
e5c83d9b DS |
376 | break; |
377 | } | |
378 | } | |
379 | ||
ff9799c3 DS |
380 | switch (nh_afi) { |
381 | case NEXTHOP_TYPE_IFINDEX: | |
382 | break; | |
383 | case NEXTHOP_TYPE_IPV4: | |
384 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
385 | v6 = true; | |
386 | install_afi = AFI_IP; | |
387 | break; | |
388 | case NEXTHOP_TYPE_IPV6: | |
389 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
390 | v4 = true; | |
391 | install_afi = AFI_IP6; | |
392 | break; | |
393 | case NEXTHOP_TYPE_BLACKHOLE: | |
394 | bh = true; | |
395 | install_afi = AFI_MAX; | |
396 | break; | |
397 | } | |
398 | ||
e5c83d9b DS |
399 | if (!bh && v6 && v4) |
400 | DEBUGD(&pbr_dbg_nht, | |
401 | "%s: Saw both V6 and V4 nexthops...using %s", | |
402 | __PRETTY_FUNCTION__, afi2str(install_afi)); | |
403 | if (bh && (v6 || v4)) | |
404 | DEBUGD(&pbr_dbg_nht, | |
405 | "%s: Saw blackhole nexthop(s) with %s%s%s nexthop(s), using AFI_MAX.", | |
406 | __PRETTY_FUNCTION__, v4 ? "v4" : "", | |
407 | (v4 && v6) ? " and " : "", v6 ? "v6" : ""); | |
408 | ||
409 | return install_afi; | |
410 | } | |
411 | ||
412 | static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache *pnhgc, | |
413 | struct nexthop_group nhg) | |
414 | { | |
415 | afi_t install_afi; | |
ff9799c3 | 416 | enum nexthop_types_t nh_afi = 0; |
e5c83d9b | 417 | |
ff9799c3 | 418 | install_afi = pbr_nht_which_afi(nhg, nh_afi); |
e5c83d9b | 419 | |
e5c83d9b DS |
420 | route_add(pnhgc, nhg, install_afi); |
421 | } | |
422 | ||
423 | static void | |
424 | pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache *pnhgc, | |
ff9799c3 DS |
425 | struct nexthop_group nhg, |
426 | enum nexthop_types_t nh_afi) | |
e5c83d9b DS |
427 | { |
428 | afi_t install_afi; | |
429 | ||
ff9799c3 | 430 | install_afi = pbr_nht_which_afi(nhg, nh_afi); |
e5c83d9b DS |
431 | |
432 | pnhgc->installed = false; | |
433 | pnhgc->valid = false; | |
434 | route_delete(pnhgc, install_afi); | |
435 | } | |
436 | ||
437 | void pbr_nht_change_group(const char *name) | |
438 | { | |
439 | struct nexthop_group_cmd *nhgc; | |
440 | struct pbr_nexthop_group_cache *pnhgc; | |
441 | struct pbr_nexthop_group_cache find; | |
442 | struct nexthop *nhop; | |
443 | ||
444 | nhgc = nhgc_find(name); | |
445 | if (!nhgc) | |
446 | return; | |
447 | ||
448 | memset(&find, 0, sizeof(find)); | |
6612590d | 449 | snprintf(find.name, sizeof(find.name), "%s", name); |
e5c83d9b DS |
450 | pnhgc = hash_lookup(pbr_nhg_hash, &find); |
451 | ||
452 | if (!pnhgc) { | |
453 | DEBUGD(&pbr_dbg_nht, | |
454 | "%s: Could not find nexthop-group cache w/ name '%s'", | |
455 | __PRETTY_FUNCTION__, name); | |
456 | return; | |
457 | } | |
458 | ||
459 | for (ALL_NEXTHOPS(nhgc->nhg, nhop)) { | |
460 | struct pbr_nexthop_cache lookup; | |
461 | struct pbr_nexthop_cache *pnhc; | |
462 | ||
b13e5ad6 | 463 | lookup.nexthop = nhop; |
e5c83d9b DS |
464 | pnhc = hash_lookup(pnhgc->nhh, &lookup); |
465 | if (!pnhc) { | |
466 | pnhc = hash_get(pnhgc->nhh, &lookup, pbr_nh_alloc); | |
467 | pnhc->parent = pnhgc; | |
468 | } | |
469 | } | |
470 | pbr_nht_install_nexthop_group(pnhgc, nhgc->nhg); | |
471 | } | |
472 | ||
29d5a146 DS |
473 | /* |
474 | * Since we are writing into the name field which is PBR_MAP_NAMELEN | |
475 | * size, we are expecting this to field to be at max 100 bytes. | |
476 | * Newer compilers understand that the %s portion may be up to | |
477 | * 100 bytes( because of the size of the string. The %u portion | |
478 | * is expected to be 10 bytes. So in `theory` there are situations | |
479 | * where we might truncate. The reality this is never going to | |
480 | * happen( who is going to create a nexthop group name that is | |
481 | * over say 30 characters? ). As such we are expecting the | |
482 | * calling function to subtract 10 from the size_t l before | |
483 | * we pass it in to get around this new gcc fun. | |
484 | */ | |
e5c83d9b DS |
485 | char *pbr_nht_nexthop_make_name(char *name, size_t l, |
486 | uint32_t seqno, char *buffer) | |
487 | { | |
488 | snprintf(buffer, l, "%s%u", name, seqno); | |
489 | return buffer; | |
490 | } | |
491 | ||
b13e5ad6 | 492 | void pbr_nht_add_individual_nexthop(struct pbr_map_sequence *pbrms) |
e5c83d9b DS |
493 | { |
494 | struct pbr_nexthop_group_cache *pnhgc; | |
495 | struct pbr_nexthop_group_cache find; | |
496 | struct pbr_nexthop_cache *pnhc; | |
e5c83d9b DS |
497 | struct pbr_nexthop_cache lookup; |
498 | ||
e5c83d9b | 499 | memset(&find, 0, sizeof(find)); |
29d5a146 | 500 | pbr_nht_nexthop_make_name(pbrms->parent->name, PBR_MAP_NAMELEN - 10, |
e5c83d9b DS |
501 | pbrms->seqno, find.name); |
502 | if (!pbrms->internal_nhg_name) | |
503 | pbrms->internal_nhg_name = XSTRDUP(MTYPE_TMP, find.name); | |
504 | ||
505 | pnhgc = hash_get(pbr_nhg_hash, &find, pbr_nhgc_alloc); | |
506 | ||
b13e5ad6 | 507 | lookup.nexthop = pbrms->nhg->nexthop; |
e5c83d9b DS |
508 | pnhc = hash_get(pnhgc->nhh, &lookup, pbr_nh_alloc); |
509 | pnhc->parent = pnhgc; | |
510 | pbr_nht_install_nexthop_group(pnhgc, *pbrms->nhg); | |
511 | } | |
512 | ||
b13e5ad6 | 513 | void pbr_nht_delete_individual_nexthop(struct pbr_map_sequence *pbrms) |
e5c83d9b DS |
514 | { |
515 | struct pbr_nexthop_group_cache *pnhgc; | |
516 | struct pbr_nexthop_group_cache find; | |
517 | struct pbr_nexthop_cache *pnhc; | |
518 | struct pbr_nexthop_cache lup; | |
b13e5ad6 DS |
519 | struct pbr_map *pbrm = pbrms->parent; |
520 | struct listnode *node; | |
521 | struct pbr_map_interface *pmi; | |
e5c83d9b | 522 | struct nexthop *nh; |
ff9799c3 | 523 | enum nexthop_types_t nh_afi = 0; |
e5c83d9b | 524 | |
b13e5ad6 DS |
525 | if (pbrm->valid && pbrms->nhs_installed && pbrm->incoming->count) { |
526 | for (ALL_LIST_ELEMENTS_RO(pbrm->incoming, node, pmi)) | |
527 | pbr_send_pbr_map(pbrms, pmi, false); | |
528 | } | |
529 | ||
530 | pbrm->valid = false; | |
531 | pbrms->nhs_installed = false; | |
b13e5ad6 | 532 | pbrms->reason |= PBR_MAP_INVALID_NO_NEXTHOPS; |
e5c83d9b DS |
533 | |
534 | memset(&find, 0, sizeof(find)); | |
6612590d | 535 | snprintf(find.name, sizeof(find.name), "%s", pbrms->internal_nhg_name); |
e5c83d9b DS |
536 | pnhgc = hash_lookup(pbr_nhg_hash, &find); |
537 | ||
538 | nh = pbrms->nhg->nexthop; | |
ff9799c3 | 539 | nh_afi = nh->type; |
b13e5ad6 | 540 | lup.nexthop = nh; |
e5c83d9b DS |
541 | pnhc = hash_lookup(pnhgc->nhh, &lup); |
542 | pnhc->parent = NULL; | |
543 | hash_release(pnhgc->nhh, pnhc); | |
544 | pbr_nh_delete(&pnhc); | |
ff9799c3 | 545 | pbr_nht_uninstall_nexthop_group(pnhgc, *pbrms->nhg, nh_afi); |
e5c83d9b DS |
546 | |
547 | hash_release(pbr_nhg_hash, pnhgc); | |
548 | ||
549 | nexthop_del(pbrms->nhg, nh); | |
550 | nexthop_free(nh); | |
551 | nexthop_group_delete(&pbrms->nhg); | |
552 | XFREE(MTYPE_TMP, pbrms->internal_nhg_name); | |
553 | } | |
554 | ||
b13e5ad6 | 555 | struct pbr_nexthop_group_cache *pbr_nht_add_group(const char *name) |
e5c83d9b DS |
556 | { |
557 | struct nexthop *nhop; | |
558 | struct nexthop_group_cmd *nhgc; | |
559 | struct pbr_nexthop_group_cache *pnhgc; | |
560 | struct pbr_nexthop_group_cache lookup; | |
561 | ||
562 | nhgc = nhgc_find(name); | |
563 | ||
564 | if (!nhgc) { | |
565 | zlog_warn("%s: Could not find group %s to add", | |
566 | __PRETTY_FUNCTION__, name); | |
b13e5ad6 | 567 | return NULL; |
e5c83d9b DS |
568 | } |
569 | ||
6612590d | 570 | snprintf(lookup.name, sizeof(lookup.name), "%s", name); |
e5c83d9b DS |
571 | pnhgc = hash_get(pbr_nhg_hash, &lookup, pbr_nhgc_alloc); |
572 | DEBUGD(&pbr_dbg_nht, "%s: Retrieved NHGC @ %p", __PRETTY_FUNCTION__, | |
573 | pnhgc); | |
574 | ||
575 | for (ALL_NEXTHOPS(nhgc->nhg, nhop)) { | |
576 | struct pbr_nexthop_cache lookup; | |
577 | struct pbr_nexthop_cache *pnhc; | |
578 | ||
b13e5ad6 | 579 | lookup.nexthop = nhop; |
e5c83d9b DS |
580 | pnhc = hash_lookup(pnhgc->nhh, &lookup); |
581 | if (!pnhc) { | |
582 | pnhc = hash_get(pnhgc->nhh, &lookup, pbr_nh_alloc); | |
583 | pnhc->parent = pnhgc; | |
584 | } | |
585 | } | |
b13e5ad6 DS |
586 | |
587 | return pnhgc; | |
e5c83d9b DS |
588 | } |
589 | ||
590 | void pbr_nht_delete_group(const char *name) | |
591 | { | |
592 | struct pbr_map_sequence *pbrms; | |
593 | struct listnode *snode; | |
594 | struct pbr_map *pbrm; | |
b13e5ad6 DS |
595 | struct pbr_nexthop_group_cache pnhgc_find; |
596 | struct pbr_nexthop_group_cache *pnhgc; | |
e5c83d9b DS |
597 | |
598 | RB_FOREACH (pbrm, pbr_map_entry_head, &pbr_maps) { | |
599 | for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, snode, pbrms)) { | |
600 | if (pbrms->nhgrp_name | |
b13e5ad6 | 601 | && strmatch(pbrms->nhgrp_name, name)) { |
e5c83d9b | 602 | pbrms->reason |= PBR_MAP_INVALID_NO_NEXTHOPS; |
b13e5ad6 DS |
603 | nexthop_group_delete(&pbrms->nhg); |
604 | pbrms->nhg = NULL; | |
605 | pbrms->internal_nhg_name = NULL; | |
e5c83d9b DS |
606 | pbrm->valid = false; |
607 | } | |
608 | } | |
609 | } | |
b13e5ad6 DS |
610 | |
611 | strlcpy(pnhgc_find.name, name, sizeof(pnhgc_find.name)); | |
612 | pnhgc = hash_release(pbr_nhg_hash, &pnhgc_find); | |
613 | pbr_nhgc_delete(pnhgc); | |
e5c83d9b DS |
614 | } |
615 | ||
616 | bool pbr_nht_nexthop_valid(struct nexthop_group *nhg) | |
617 | { | |
618 | DEBUGD(&pbr_dbg_nht, "%s: %p", __PRETTY_FUNCTION__, nhg); | |
619 | return true; | |
620 | } | |
621 | ||
622 | bool pbr_nht_nexthop_group_valid(const char *name) | |
623 | { | |
624 | struct pbr_nexthop_group_cache *pnhgc; | |
625 | struct pbr_nexthop_group_cache lookup; | |
626 | ||
627 | DEBUGD(&pbr_dbg_nht, "%s: %s", __PRETTY_FUNCTION__, name); | |
628 | ||
6612590d | 629 | snprintf(lookup.name, sizeof(lookup.name), "%s", name); |
e5c83d9b DS |
630 | pnhgc = hash_get(pbr_nhg_hash, &lookup, NULL); |
631 | if (!pnhgc) | |
632 | return false; | |
633 | DEBUGD(&pbr_dbg_nht, "%s: \t%d %d", __PRETTY_FUNCTION__, pnhgc->valid, | |
634 | pnhgc->installed); | |
635 | if (pnhgc->valid && pnhgc->installed) | |
636 | return true; | |
637 | ||
638 | return false; | |
639 | } | |
640 | ||
641 | struct pbr_nht_individual { | |
642 | struct zapi_route *nhr; | |
643 | ||
644 | uint32_t valid; | |
645 | }; | |
646 | ||
647 | static void pbr_nht_individual_nexthop_update_lookup(struct hash_backet *b, | |
648 | void *data) | |
649 | { | |
650 | struct pbr_nexthop_cache *pnhc = b->data; | |
651 | struct pbr_nht_individual *pnhi = data; | |
652 | char buf[PREFIX_STRLEN]; | |
653 | bool old_valid; | |
654 | ||
655 | old_valid = pnhc->valid; | |
656 | ||
657 | switch (pnhi->nhr->prefix.family) { | |
658 | case AF_INET: | |
b13e5ad6 | 659 | if (pnhc->nexthop->gate.ipv4.s_addr |
e5c83d9b DS |
660 | == pnhi->nhr->prefix.u.prefix4.s_addr) |
661 | pnhc->valid = !!pnhi->nhr->nexthop_num; | |
662 | break; | |
663 | case AF_INET6: | |
b13e5ad6 DS |
664 | if (memcmp(&pnhc->nexthop->gate.ipv6, |
665 | &pnhi->nhr->prefix.u.prefix6, 16) | |
666 | == 0) | |
e5c83d9b DS |
667 | pnhc->valid = !!pnhi->nhr->nexthop_num; |
668 | break; | |
669 | } | |
670 | ||
671 | DEBUGD(&pbr_dbg_nht, "\tFound %s: old: %d new: %d", | |
672 | prefix2str(&pnhi->nhr->prefix, buf, sizeof(buf)), old_valid, | |
673 | pnhc->valid); | |
674 | ||
e5c83d9b DS |
675 | if (pnhc->valid) |
676 | pnhi->valid += 1; | |
677 | } | |
678 | ||
679 | static void pbr_nht_nexthop_update_lookup(struct hash_backet *b, void *data) | |
680 | { | |
681 | struct pbr_nexthop_group_cache *pnhgc = b->data; | |
682 | struct pbr_nht_individual pnhi; | |
b13e5ad6 DS |
683 | bool old_valid; |
684 | ||
685 | old_valid = pnhgc->valid; | |
e5c83d9b DS |
686 | |
687 | pnhi.nhr = (struct zapi_route *)data; | |
688 | pnhi.valid = 0; | |
689 | hash_iterate(pnhgc->nhh, pbr_nht_individual_nexthop_update_lookup, | |
690 | &pnhi); | |
691 | ||
692 | /* | |
693 | * If any of the specified nexthops are valid we are valid | |
694 | */ | |
695 | pnhgc->valid = !!pnhi.valid; | |
b13e5ad6 DS |
696 | |
697 | if (old_valid != pnhgc->valid) | |
698 | pbr_map_check_nh_group_change(pnhgc->name); | |
e5c83d9b DS |
699 | } |
700 | ||
701 | void pbr_nht_nexthop_update(struct zapi_route *nhr) | |
702 | { | |
703 | hash_iterate(pbr_nhg_hash, pbr_nht_nexthop_update_lookup, nhr); | |
704 | } | |
705 | ||
706 | static uint32_t pbr_nhg_hash_key(void *arg) | |
707 | { | |
708 | struct pbr_nexthop_group_cache *nhgc = | |
709 | (struct pbr_nexthop_group_cache *)arg; | |
710 | ||
711 | return jhash(&nhgc->name, strlen(nhgc->name), 0x52c34a96); | |
712 | } | |
713 | ||
714 | static int pbr_nhg_hash_equal(const void *arg1, const void *arg2) | |
715 | { | |
716 | const struct pbr_nexthop_group_cache *nhgc1 = | |
717 | (const struct pbr_nexthop_group_cache *)arg1; | |
718 | const struct pbr_nexthop_group_cache *nhgc2 = | |
719 | (const struct pbr_nexthop_group_cache *)arg2; | |
720 | ||
721 | return !strcmp(nhgc1->name, nhgc2->name); | |
722 | } | |
723 | ||
724 | ||
725 | uint32_t pbr_nht_get_next_tableid(void) | |
726 | { | |
727 | uint32_t i; | |
728 | bool found = false; | |
729 | ||
730 | for (i = pbr_nhg_low_table; i <= pbr_nhg_high_table; i++) { | |
731 | if (nhg_tableid[i] == false) { | |
732 | found = true; | |
733 | break; | |
734 | } | |
735 | } | |
736 | ||
737 | if (found) { | |
738 | nhg_tableid[i] = true; | |
739 | return i; | |
740 | } else | |
741 | return 0; | |
742 | } | |
743 | ||
744 | void pbr_nht_set_tableid_range(uint32_t low, uint32_t high) | |
745 | { | |
746 | pbr_nhg_low_table = low; | |
747 | pbr_nhg_high_table = high; | |
748 | } | |
749 | ||
750 | void pbr_nht_write_table_range(struct vty *vty) | |
751 | { | |
752 | if (pbr_nhg_low_table != PBR_NHT_DEFAULT_LOW_TABLEID | |
753 | || pbr_nhg_high_table != PBR_NHT_DEFAULT_HIGH_TABLEID) { | |
754 | vty_out(vty, "pbr table range %u %u\n", pbr_nhg_low_table, | |
755 | pbr_nhg_high_table); | |
756 | } | |
757 | } | |
758 | ||
759 | uint32_t pbr_nht_get_next_rule(uint32_t seqno) | |
760 | { | |
761 | return seqno + pbr_nhg_low_rule - 1; | |
762 | } | |
763 | void pbr_nht_set_rule_range(uint32_t low, uint32_t high) | |
764 | { | |
765 | pbr_nhg_low_rule = low; | |
766 | pbr_nhg_high_rule = high; | |
767 | } | |
768 | ||
769 | void pbr_nht_write_rule_range(struct vty *vty) | |
770 | { | |
771 | if (pbr_nhg_low_rule != PBR_NHT_DEFAULT_LOW_RULE | |
772 | || pbr_nhg_high_rule != PBR_NHT_DEFAULT_HIGH_RULE) { | |
773 | vty_out(vty, "pbr rule range %u %u\n", pbr_nhg_low_rule, | |
774 | pbr_nhg_high_rule); | |
775 | } | |
776 | } | |
777 | ||
778 | uint32_t pbr_nht_get_table(const char *name) | |
779 | { | |
780 | struct pbr_nexthop_group_cache find; | |
781 | struct pbr_nexthop_group_cache *pnhgc; | |
782 | ||
783 | memset(&find, 0, sizeof(find)); | |
6612590d | 784 | snprintf(find.name, sizeof(find.name), "%s", name); |
e5c83d9b DS |
785 | pnhgc = hash_lookup(pbr_nhg_hash, &find); |
786 | ||
787 | if (!pnhgc) { | |
788 | DEBUGD(&pbr_dbg_nht, | |
789 | "%s: Could not find nexthop-group cache w/ name '%s'", | |
790 | __PRETTY_FUNCTION__, name); | |
791 | return 5000; | |
792 | } | |
793 | ||
794 | return pnhgc->table_id; | |
795 | } | |
796 | ||
797 | bool pbr_nht_get_installed(const char *name) | |
798 | { | |
799 | struct pbr_nexthop_group_cache find; | |
800 | struct pbr_nexthop_group_cache *pnhgc; | |
801 | ||
802 | memset(&find, 0, sizeof(find)); | |
6612590d | 803 | snprintf(find.name, sizeof(find.name), "%s", name); |
e5c83d9b DS |
804 | |
805 | pnhgc = hash_lookup(pbr_nhg_hash, &find); | |
806 | ||
d3765386 | 807 | if (!pnhgc) |
e5c83d9b | 808 | return false; |
e5c83d9b DS |
809 | |
810 | return pnhgc->installed; | |
811 | } | |
812 | ||
813 | static void pbr_nht_show_nhg_nexthops(struct hash_backet *b, void *data) | |
814 | { | |
815 | struct pbr_nexthop_cache *pnhc = b->data; | |
816 | struct vty *vty = data; | |
817 | ||
57cdafc4 | 818 | vty_out(vty, "\tValid: %d ", pnhc->valid); |
b13e5ad6 | 819 | nexthop_group_write_nexthop(vty, pnhc->nexthop); |
e5c83d9b DS |
820 | } |
821 | ||
822 | struct pbr_nht_show { | |
823 | struct vty *vty; | |
824 | const char *name; | |
825 | }; | |
826 | ||
827 | static void pbr_nht_show_nhg(struct hash_backet *b, void *data) | |
828 | { | |
829 | struct pbr_nexthop_group_cache *pnhgc = b->data; | |
830 | struct pbr_nht_show *pns = data; | |
831 | struct vty *vty; | |
832 | ||
833 | if (pns->name && strcmp(pns->name, pnhgc->name) != 0) | |
834 | return; | |
835 | ||
836 | vty = pns->vty; | |
837 | vty_out(vty, "Nexthop-Group: %s Table: %u Valid: %d Installed: %d\n", | |
838 | pnhgc->name, pnhgc->table_id, pnhgc->valid, pnhgc->installed); | |
839 | ||
840 | hash_iterate(pnhgc->nhh, pbr_nht_show_nhg_nexthops, vty); | |
841 | } | |
842 | ||
843 | void pbr_nht_show_nexthop_group(struct vty *vty, const char *name) | |
844 | { | |
845 | struct pbr_nht_show pns; | |
846 | ||
847 | pns.vty = vty; | |
848 | pns.name = name; | |
849 | ||
850 | hash_iterate(pbr_nhg_hash, pbr_nht_show_nhg, &pns); | |
851 | } | |
852 | ||
853 | void pbr_nht_init(void) | |
854 | { | |
855 | pbr_nhg_hash = hash_create_size( | |
856 | 16, pbr_nhg_hash_key, pbr_nhg_hash_equal, "PBR NHG Cache Hash"); | |
b13e5ad6 DS |
857 | pbr_nhrc_hash = |
858 | hash_create_size(16, (unsigned int (*)(void *))nexthop_hash, | |
859 | pbr_nhrc_hash_equal, "PBR NH Hash"); | |
e5c83d9b DS |
860 | |
861 | pbr_nhg_low_table = PBR_NHT_DEFAULT_LOW_TABLEID; | |
862 | pbr_nhg_high_table = PBR_NHT_DEFAULT_HIGH_TABLEID; | |
863 | pbr_nhg_low_rule = PBR_NHT_DEFAULT_LOW_RULE; | |
864 | pbr_nhg_high_rule = PBR_NHT_DEFAULT_HIGH_RULE; | |
865 | memset(&nhg_tableid, 0, 65535 * sizeof(uint8_t)); | |
866 | } |