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