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