]> git.proxmox.com Git - mirror_frr.git/blame - pbrd/pbr_nht.c
Merge pull request #4949 from opensourcerouting/mpls-zapi-improvements
[mirror_frr.git] / pbrd / pbr_nht.c
CommitLineData
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
38DEFINE_MTYPE_STATIC(PBRD, PBR_NHG, "PBR Nexthop Groups")
39
40static struct hash *pbr_nhg_hash;
b13e5ad6 41static struct hash *pbr_nhrc_hash;
e5c83d9b
DS
42
43static uint32_t pbr_nhg_low_table;
44static uint32_t pbr_nhg_high_table;
45static uint32_t pbr_nhg_low_rule;
46static uint32_t pbr_nhg_high_rule;
47static bool nhg_tableid[65535];
48
b13e5ad6
DS
49static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache *pnhgc,
50 struct nexthop_group nhg);
ff9799c3
DS
51static void
52pbr_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 */
59struct nhrc {
60 struct nexthop nexthop;
61 unsigned int refcount;
62};
63
64/* Hash functions for pbr_nhrc_hash ---------------------------------------- */
65
66static 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 75static 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
87static 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
109static 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 130static 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 135static 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 145static 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
182static 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
189static 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
209void 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 233void 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
RW
269
270 if (nhop->type == NEXTHOP_TYPE_IFINDEX) {
271 struct interface *ifp;
272
273 ifp = if_lookup_by_index(nhop->ifindex, nhop->vrf_id);
274 if (ifp)
275 pbr_nht_nexthop_interface_update(ifp);
276 }
e5c83d9b
DS
277}
278
b13e5ad6 279void pbr_nhgroup_del_nexthop_cb(const struct nexthop_group_cmd *nhgc,
e5c83d9b
DS
280 const struct nexthop *nhop)
281{
b13e5ad6 282 char debugstr[256];
3e300703 283 struct pbr_nexthop_group_cache pnhgc_find = {};
b13e5ad6 284 struct pbr_nexthop_group_cache *pnhgc;
3e300703 285 struct pbr_nexthop_cache pnhc_find = {};
b13e5ad6 286 struct pbr_nexthop_cache *pnhc;
aafac994 287 enum nexthop_types_t nh_type = nhop->type;
b13e5ad6
DS
288
289 /* find pnhgc by name */
290 strlcpy(pnhgc_find.name, nhgc->name, sizeof(pnhgc_find.name));
a4044dc1 291 pnhgc = hash_lookup(pbr_nhg_hash, &pnhgc_find);
b13e5ad6
DS
292
293 /* delete pnhc from pnhgc->nhh */
294 pnhc_find.nexthop = (struct nexthop *)nhop;
295 pnhc = hash_release(pnhgc->nhh, &pnhc_find);
296
297 /* delete pnhc */
298 pbr_nh_delete(&pnhc);
e5c83d9b 299
b13e5ad6
DS
300 if (DEBUG_MODE_CHECK(&pbr_dbg_nht, DEBUG_MODE_ALL)) {
301 nexthop2str(nhop, debugstr, sizeof(debugstr));
302 DEBUGD(&pbr_dbg_nht, "%s: Removed %s from nexthop-group %s",
303 __PRETTY_FUNCTION__, debugstr, nhgc->name);
304 }
e5c83d9b 305
ff9799c3
DS
306 if (pnhgc->nhh->count)
307 pbr_nht_install_nexthop_group(pnhgc, nhgc->nhg);
308 else
aafac994 309 pbr_nht_uninstall_nexthop_group(pnhgc, nhgc->nhg, nh_type);
ff9799c3 310
b13e5ad6 311 pbr_map_check_nh_group_change(nhgc->name);
e5c83d9b
DS
312}
313
314void pbr_nhgroup_delete_cb(const char *name)
315{
b13e5ad6 316 DEBUGD(&pbr_dbg_nht, "%s: Removed nexthop-group %s",
e5c83d9b 317 __PRETTY_FUNCTION__, name);
b13e5ad6 318
ff9799c3
DS
319 /* delete group from all pbrms's */
320 pbr_nht_delete_group(name);
321
b13e5ad6 322 pbr_map_check_nh_group_change(name);
e5c83d9b
DS
323}
324
325#if 0
326static struct pbr_nexthop_cache *pbr_nht_lookup_nexthop(struct nexthop *nexthop)
327{
328 return NULL;
329}
330#endif
331
e3b78da8 332static void pbr_nht_find_nhg_from_table_install(struct hash_bucket *b,
e5c83d9b
DS
333 void *data)
334{
335 struct pbr_nexthop_group_cache *pnhgc =
336 (struct pbr_nexthop_group_cache *)b->data;
337 uint32_t *table_id = (uint32_t *)data;
338
339 if (pnhgc->table_id == *table_id) {
340 DEBUGD(&pbr_dbg_nht, "%s: Table ID (%u) matches %s",
341 __PRETTY_FUNCTION__, *table_id, pnhgc->name);
2fb7892e
DS
342
343 /*
344 * If the table has been re-handled by zebra
345 * and we are already installed no need to do
346 * anything here.
347 */
348 if (!pnhgc->installed) {
349 pnhgc->installed = true;
350 pbr_map_schedule_policy_from_nhg(pnhgc->name);
351 }
e5c83d9b
DS
352 }
353}
354
355void pbr_nht_route_installed_for_table(uint32_t table_id)
356{
357 hash_iterate(pbr_nhg_hash, pbr_nht_find_nhg_from_table_install,
358 &table_id);
359}
360
e3b78da8 361static void pbr_nht_find_nhg_from_table_remove(struct hash_bucket *b,
e5c83d9b
DS
362 void *data)
363{
364 ;
365}
366
367void pbr_nht_route_removed_for_table(uint32_t table_id)
368{
369 hash_iterate(pbr_nhg_hash, pbr_nht_find_nhg_from_table_remove,
370 &table_id);
371}
372
373/*
374 * Loop through all nexthops in a nexthop group to check that they are all the
375 * same. If they are not all the same, log this peculiarity.
376 *
377 * nhg
378 * The nexthop group to check
379 *
380 * Returns:
381 * - AFI of last nexthop in the group
382 * - AFI_MAX on error
383 */
ff9799c3 384static afi_t pbr_nht_which_afi(struct nexthop_group nhg,
aafac994 385 enum nexthop_types_t nh_type)
e5c83d9b
DS
386{
387 struct nexthop *nexthop;
388 afi_t install_afi = AFI_MAX;
389 bool v6, v4, bh;
d3765386 390
268c24ee
RW
391 if (nh_type) {
392 switch (nh_type) {
393 case NEXTHOP_TYPE_IPV4:
394 case NEXTHOP_TYPE_IPV4_IFINDEX:
395 return AFI_IP;
396 case NEXTHOP_TYPE_IPV6:
397 case NEXTHOP_TYPE_IPV6_IFINDEX:
398 return AFI_IP6;
399 case NEXTHOP_TYPE_IFINDEX:
400 case NEXTHOP_TYPE_BLACKHOLE:
401 return AFI_MAX;
402 }
403 }
404
e5c83d9b
DS
405 v6 = v4 = bh = false;
406
268c24ee
RW
407 for (ALL_NEXTHOPS(nhg, nexthop)) {
408 nh_type = nexthop->type;
409
410 switch (nh_type) {
411 case NEXTHOP_TYPE_IFINDEX:
412 break;
413 case NEXTHOP_TYPE_IPV4:
414 case NEXTHOP_TYPE_IPV4_IFINDEX:
415 v6 = true;
416 install_afi = AFI_IP;
417 break;
418 case NEXTHOP_TYPE_IPV6:
419 case NEXTHOP_TYPE_IPV6_IFINDEX:
420 v4 = true;
421 install_afi = AFI_IP6;
422 break;
423 case NEXTHOP_TYPE_BLACKHOLE:
424 bh = true;
e5c83d9b
DS
425 break;
426 }
427 }
428
268c24ee
RW
429 /* Interface and/or blackhole nexthops only. */
430 if (!v4 && !v6)
ff9799c3 431 install_afi = AFI_MAX;
ff9799c3 432
e5c83d9b
DS
433 if (!bh && v6 && v4)
434 DEBUGD(&pbr_dbg_nht,
435 "%s: Saw both V6 and V4 nexthops...using %s",
436 __PRETTY_FUNCTION__, afi2str(install_afi));
437 if (bh && (v6 || v4))
438 DEBUGD(&pbr_dbg_nht,
439 "%s: Saw blackhole nexthop(s) with %s%s%s nexthop(s), using AFI_MAX.",
440 __PRETTY_FUNCTION__, v4 ? "v4" : "",
441 (v4 && v6) ? " and " : "", v6 ? "v6" : "");
442
443 return install_afi;
444}
445
446static void pbr_nht_install_nexthop_group(struct pbr_nexthop_group_cache *pnhgc,
447 struct nexthop_group nhg)
448{
449 afi_t install_afi;
aafac994 450 enum nexthop_types_t nh_type = 0;
e5c83d9b 451
aafac994 452 install_afi = pbr_nht_which_afi(nhg, nh_type);
e5c83d9b 453
e5c83d9b
DS
454 route_add(pnhgc, nhg, install_afi);
455}
456
457static void
458pbr_nht_uninstall_nexthop_group(struct pbr_nexthop_group_cache *pnhgc,
ff9799c3 459 struct nexthop_group nhg,
aafac994 460 enum nexthop_types_t nh_type)
e5c83d9b
DS
461{
462 afi_t install_afi;
463
aafac994 464 install_afi = pbr_nht_which_afi(nhg, nh_type);
e5c83d9b
DS
465
466 pnhgc->installed = false;
467 pnhgc->valid = false;
468 route_delete(pnhgc, install_afi);
469}
470
471void pbr_nht_change_group(const char *name)
472{
473 struct nexthop_group_cmd *nhgc;
474 struct pbr_nexthop_group_cache *pnhgc;
475 struct pbr_nexthop_group_cache find;
476 struct nexthop *nhop;
477
478 nhgc = nhgc_find(name);
479 if (!nhgc)
480 return;
481
482 memset(&find, 0, sizeof(find));
6612590d 483 snprintf(find.name, sizeof(find.name), "%s", name);
e5c83d9b
DS
484 pnhgc = hash_lookup(pbr_nhg_hash, &find);
485
486 if (!pnhgc) {
487 DEBUGD(&pbr_dbg_nht,
488 "%s: Could not find nexthop-group cache w/ name '%s'",
489 __PRETTY_FUNCTION__, name);
490 return;
491 }
492
493 for (ALL_NEXTHOPS(nhgc->nhg, nhop)) {
494 struct pbr_nexthop_cache lookup;
495 struct pbr_nexthop_cache *pnhc;
496
b13e5ad6 497 lookup.nexthop = nhop;
e5c83d9b
DS
498 pnhc = hash_lookup(pnhgc->nhh, &lookup);
499 if (!pnhc) {
500 pnhc = hash_get(pnhgc->nhh, &lookup, pbr_nh_alloc);
501 pnhc->parent = pnhgc;
502 }
503 }
504 pbr_nht_install_nexthop_group(pnhgc, nhgc->nhg);
505}
506
507char *pbr_nht_nexthop_make_name(char *name, size_t l,
508 uint32_t seqno, char *buffer)
509{
510 snprintf(buffer, l, "%s%u", name, seqno);
511 return buffer;
512}
513
b13e5ad6 514void pbr_nht_add_individual_nexthop(struct pbr_map_sequence *pbrms)
e5c83d9b
DS
515{
516 struct pbr_nexthop_group_cache *pnhgc;
517 struct pbr_nexthop_group_cache find;
518 struct pbr_nexthop_cache *pnhc;
e5c83d9b
DS
519 struct pbr_nexthop_cache lookup;
520
e5c83d9b 521 memset(&find, 0, sizeof(find));
06210d1f 522 pbr_nht_nexthop_make_name(pbrms->parent->name, PBR_NHC_NAMELEN,
e5c83d9b 523 pbrms->seqno, find.name);
a4044dc1
QY
524
525 if (!pbr_nht_get_next_tableid(true)) {
526 zlog_warn(
527 "%s: Exhausted all table identifiers; cannot create nexthop-group cache for nexthop-group '%s'",
528 __PRETTY_FUNCTION__, find.name);
529 return;
530 }
531
e5c83d9b
DS
532 if (!pbrms->internal_nhg_name)
533 pbrms->internal_nhg_name = XSTRDUP(MTYPE_TMP, find.name);
534
535 pnhgc = hash_get(pbr_nhg_hash, &find, pbr_nhgc_alloc);
536
b13e5ad6 537 lookup.nexthop = pbrms->nhg->nexthop;
e5c83d9b
DS
538 pnhc = hash_get(pnhgc->nhh, &lookup, pbr_nh_alloc);
539 pnhc->parent = pnhgc;
540 pbr_nht_install_nexthop_group(pnhgc, *pbrms->nhg);
541}
542
b13e5ad6 543void pbr_nht_delete_individual_nexthop(struct pbr_map_sequence *pbrms)
e5c83d9b
DS
544{
545 struct pbr_nexthop_group_cache *pnhgc;
546 struct pbr_nexthop_group_cache find;
547 struct pbr_nexthop_cache *pnhc;
548 struct pbr_nexthop_cache lup;
b13e5ad6
DS
549 struct pbr_map *pbrm = pbrms->parent;
550 struct listnode *node;
551 struct pbr_map_interface *pmi;
e5c83d9b 552 struct nexthop *nh;
aafac994 553 enum nexthop_types_t nh_type = 0;
e5c83d9b 554
b13e5ad6
DS
555 if (pbrm->valid && pbrms->nhs_installed && pbrm->incoming->count) {
556 for (ALL_LIST_ELEMENTS_RO(pbrm->incoming, node, pmi))
557 pbr_send_pbr_map(pbrms, pmi, false);
558 }
559
560 pbrm->valid = false;
561 pbrms->nhs_installed = false;
b13e5ad6 562 pbrms->reason |= PBR_MAP_INVALID_NO_NEXTHOPS;
e5c83d9b
DS
563
564 memset(&find, 0, sizeof(find));
6612590d 565 snprintf(find.name, sizeof(find.name), "%s", pbrms->internal_nhg_name);
e5c83d9b
DS
566 pnhgc = hash_lookup(pbr_nhg_hash, &find);
567
568 nh = pbrms->nhg->nexthop;
aafac994 569 nh_type = nh->type;
b13e5ad6 570 lup.nexthop = nh;
e5c83d9b
DS
571 pnhc = hash_lookup(pnhgc->nhh, &lup);
572 pnhc->parent = NULL;
573 hash_release(pnhgc->nhh, pnhc);
574 pbr_nh_delete(&pnhc);
aafac994 575 pbr_nht_uninstall_nexthop_group(pnhgc, *pbrms->nhg, nh_type);
e5c83d9b
DS
576
577 hash_release(pbr_nhg_hash, pnhgc);
578
50d89650 579 _nexthop_del(pbrms->nhg, nh);
e5c83d9b
DS
580 nexthop_free(nh);
581 nexthop_group_delete(&pbrms->nhg);
582 XFREE(MTYPE_TMP, pbrms->internal_nhg_name);
583}
584
b13e5ad6 585struct 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
627void 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 nexthop_group_delete(&pbrms->nhg);
641 pbrms->nhg = NULL;
642 pbrms->internal_nhg_name = NULL;
e5c83d9b
DS
643 pbrm->valid = false;
644 }
645 }
646 }
b13e5ad6
DS
647
648 strlcpy(pnhgc_find.name, name, sizeof(pnhgc_find.name));
649 pnhgc = hash_release(pbr_nhg_hash, &pnhgc_find);
650 pbr_nhgc_delete(pnhgc);
e5c83d9b
DS
651}
652
653bool pbr_nht_nexthop_valid(struct nexthop_group *nhg)
654{
655 DEBUGD(&pbr_dbg_nht, "%s: %p", __PRETTY_FUNCTION__, nhg);
656 return true;
657}
658
659bool pbr_nht_nexthop_group_valid(const char *name)
660{
661 struct pbr_nexthop_group_cache *pnhgc;
662 struct pbr_nexthop_group_cache lookup;
663
664 DEBUGD(&pbr_dbg_nht, "%s: %s", __PRETTY_FUNCTION__, name);
665
6612590d 666 snprintf(lookup.name, sizeof(lookup.name), "%s", name);
e5c83d9b
DS
667 pnhgc = hash_get(pbr_nhg_hash, &lookup, NULL);
668 if (!pnhgc)
669 return false;
670 DEBUGD(&pbr_dbg_nht, "%s: \t%d %d", __PRETTY_FUNCTION__, pnhgc->valid,
671 pnhgc->installed);
672 if (pnhgc->valid && pnhgc->installed)
673 return true;
674
675 return false;
676}
677
678struct pbr_nht_individual {
679 struct zapi_route *nhr;
a106a408 680 struct interface *ifp;
e5c83d9b
DS
681
682 uint32_t valid;
683};
684
e3b78da8 685static void pbr_nht_individual_nexthop_update_lookup(struct hash_bucket *b,
e5c83d9b
DS
686 void *data)
687{
688 struct pbr_nexthop_cache *pnhc = b->data;
689 struct pbr_nht_individual *pnhi = data;
690 char buf[PREFIX_STRLEN];
691 bool old_valid;
692
693 old_valid = pnhc->valid;
694
695 switch (pnhi->nhr->prefix.family) {
696 case AF_INET:
b13e5ad6 697 if (pnhc->nexthop->gate.ipv4.s_addr
e5c83d9b
DS
698 == pnhi->nhr->prefix.u.prefix4.s_addr)
699 pnhc->valid = !!pnhi->nhr->nexthop_num;
700 break;
701 case AF_INET6:
b13e5ad6
DS
702 if (memcmp(&pnhc->nexthop->gate.ipv6,
703 &pnhi->nhr->prefix.u.prefix6, 16)
704 == 0)
e5c83d9b
DS
705 pnhc->valid = !!pnhi->nhr->nexthop_num;
706 break;
707 }
708
709 DEBUGD(&pbr_dbg_nht, "\tFound %s: old: %d new: %d",
710 prefix2str(&pnhi->nhr->prefix, buf, sizeof(buf)), old_valid,
711 pnhc->valid);
712
e5c83d9b
DS
713 if (pnhc->valid)
714 pnhi->valid += 1;
715}
716
b822b93a
SW
717static void pbr_nexthop_group_cache_iterate_to_group(struct hash_bucket *b,
718 void *data)
719{
720 struct pbr_nexthop_cache *pnhc = b->data;
721 struct nexthop_group *nhg = data;
722 struct nexthop *nh = NULL;
723
724 copy_nexthops(&nh, pnhc->nexthop, NULL);
725
50d89650 726 _nexthop_add(&nhg->nexthop, nh);
b822b93a
SW
727}
728
729static void
730pbr_nexthop_group_cache_to_nexthop_group(struct nexthop_group *nhg,
731 struct pbr_nexthop_group_cache *pnhgc)
732{
733 hash_iterate(pnhgc->nhh, pbr_nexthop_group_cache_iterate_to_group, nhg);
734}
735
e3b78da8 736static void pbr_nht_nexthop_update_lookup(struct hash_bucket *b, void *data)
e5c83d9b
DS
737{
738 struct pbr_nexthop_group_cache *pnhgc = b->data;
739 struct pbr_nht_individual pnhi;
b822b93a 740 struct nexthop_group nhg = {};
b13e5ad6
DS
741 bool old_valid;
742
743 old_valid = pnhgc->valid;
e5c83d9b
DS
744
745 pnhi.nhr = (struct zapi_route *)data;
746 pnhi.valid = 0;
747 hash_iterate(pnhgc->nhh, pbr_nht_individual_nexthop_update_lookup,
748 &pnhi);
749
750 /*
751 * If any of the specified nexthops are valid we are valid
752 */
753 pnhgc->valid = !!pnhi.valid;
b13e5ad6 754
b822b93a
SW
755 if (pnhgc->valid) {
756 pbr_nexthop_group_cache_to_nexthop_group(&nhg, pnhgc);
757 pbr_nht_install_nexthop_group(pnhgc, nhg);
758 /* Don't need copied nexthops anymore */
759 nexthops_free(nhg.nexthop);
760 }
761
b13e5ad6
DS
762 if (old_valid != pnhgc->valid)
763 pbr_map_check_nh_group_change(pnhgc->name);
e5c83d9b
DS
764}
765
766void pbr_nht_nexthop_update(struct zapi_route *nhr)
767{
768 hash_iterate(pbr_nhg_hash, pbr_nht_nexthop_update_lookup, nhr);
769}
770
a106a408
RW
771static void
772pbr_nht_individual_nexthop_interface_update_lookup(struct hash_backet *b,
773 void *data)
774{
775 struct pbr_nexthop_cache *pnhc = b->data;
776 struct pbr_nht_individual *pnhi = data;
777 bool old_valid;
778
779 old_valid = pnhc->valid;
780
781 if (pnhc->nexthop->type == NEXTHOP_TYPE_IFINDEX
782 && pnhc->nexthop->ifindex == pnhi->ifp->ifindex)
783 pnhc->valid = !!if_is_up(pnhi->ifp);
784
785 DEBUGD(&pbr_dbg_nht, "\tFound %s: old: %d new: %d", pnhi->ifp->name,
786 old_valid, pnhc->valid);
787
788 if (pnhc->valid)
789 pnhi->valid += 1;
790}
791
792static void pbr_nht_nexthop_interface_update_lookup(struct hash_backet *b,
793 void *data)
794{
795 struct pbr_nexthop_group_cache *pnhgc = b->data;
796 struct pbr_nht_individual pnhi;
797 bool old_valid;
798
799 old_valid = pnhgc->valid;
800
801 pnhi.ifp = data;
802 pnhi.valid = 0;
803 hash_iterate(pnhgc->nhh,
804 pbr_nht_individual_nexthop_interface_update_lookup, &pnhi);
805
806 /*
807 * If any of the specified nexthops are valid we are valid
808 */
809 pnhgc->valid = !!pnhi.valid;
810
811 if (old_valid != pnhgc->valid)
812 pbr_map_check_nh_group_change(pnhgc->name);
813}
814
815void pbr_nht_nexthop_interface_update(struct interface *ifp)
816{
817 hash_iterate(pbr_nhg_hash, pbr_nht_nexthop_interface_update_lookup,
818 ifp);
819}
820
d8b87afe 821static uint32_t pbr_nhg_hash_key(const void *arg)
e5c83d9b 822{
d8b87afe 823 const struct pbr_nexthop_group_cache *nhgc = arg;
e5c83d9b
DS
824
825 return jhash(&nhgc->name, strlen(nhgc->name), 0x52c34a96);
826}
827
74df8d6d 828static bool pbr_nhg_hash_equal(const void *arg1, const void *arg2)
e5c83d9b
DS
829{
830 const struct pbr_nexthop_group_cache *nhgc1 =
831 (const struct pbr_nexthop_group_cache *)arg1;
832 const struct pbr_nexthop_group_cache *nhgc2 =
833 (const struct pbr_nexthop_group_cache *)arg2;
834
835 return !strcmp(nhgc1->name, nhgc2->name);
836}
837
a4044dc1 838uint32_t pbr_nht_get_next_tableid(bool peek)
e5c83d9b
DS
839{
840 uint32_t i;
841 bool found = false;
842
843 for (i = pbr_nhg_low_table; i <= pbr_nhg_high_table; i++) {
d8729f8c 844 if (!nhg_tableid[i]) {
e5c83d9b
DS
845 found = true;
846 break;
847 }
848 }
849
850 if (found) {
a4044dc1 851 nhg_tableid[i] = !peek;
e5c83d9b
DS
852 return i;
853 } else
854 return 0;
855}
856
857void pbr_nht_set_tableid_range(uint32_t low, uint32_t high)
858{
859 pbr_nhg_low_table = low;
860 pbr_nhg_high_table = high;
861}
862
863void pbr_nht_write_table_range(struct vty *vty)
864{
865 if (pbr_nhg_low_table != PBR_NHT_DEFAULT_LOW_TABLEID
866 || pbr_nhg_high_table != PBR_NHT_DEFAULT_HIGH_TABLEID) {
867 vty_out(vty, "pbr table range %u %u\n", pbr_nhg_low_table,
868 pbr_nhg_high_table);
869 }
870}
871
872uint32_t pbr_nht_get_next_rule(uint32_t seqno)
873{
874 return seqno + pbr_nhg_low_rule - 1;
875}
876void pbr_nht_set_rule_range(uint32_t low, uint32_t high)
877{
878 pbr_nhg_low_rule = low;
879 pbr_nhg_high_rule = high;
880}
881
882void pbr_nht_write_rule_range(struct vty *vty)
883{
884 if (pbr_nhg_low_rule != PBR_NHT_DEFAULT_LOW_RULE
885 || pbr_nhg_high_rule != PBR_NHT_DEFAULT_HIGH_RULE) {
886 vty_out(vty, "pbr rule range %u %u\n", pbr_nhg_low_rule,
887 pbr_nhg_high_rule);
888 }
889}
890
891uint32_t pbr_nht_get_table(const char *name)
892{
893 struct pbr_nexthop_group_cache find;
894 struct pbr_nexthop_group_cache *pnhgc;
895
896 memset(&find, 0, sizeof(find));
6612590d 897 snprintf(find.name, sizeof(find.name), "%s", name);
e5c83d9b
DS
898 pnhgc = hash_lookup(pbr_nhg_hash, &find);
899
900 if (!pnhgc) {
901 DEBUGD(&pbr_dbg_nht,
902 "%s: Could not find nexthop-group cache w/ name '%s'",
903 __PRETTY_FUNCTION__, name);
904 return 5000;
905 }
906
907 return pnhgc->table_id;
908}
909
910bool pbr_nht_get_installed(const char *name)
911{
912 struct pbr_nexthop_group_cache find;
913 struct pbr_nexthop_group_cache *pnhgc;
914
915 memset(&find, 0, sizeof(find));
6612590d 916 snprintf(find.name, sizeof(find.name), "%s", name);
e5c83d9b
DS
917
918 pnhgc = hash_lookup(pbr_nhg_hash, &find);
919
d3765386 920 if (!pnhgc)
e5c83d9b 921 return false;
e5c83d9b
DS
922
923 return pnhgc->installed;
924}
925
e3b78da8 926static void pbr_nht_show_nhg_nexthops(struct hash_bucket *b, void *data)
e5c83d9b
DS
927{
928 struct pbr_nexthop_cache *pnhc = b->data;
929 struct vty *vty = data;
930
57cdafc4 931 vty_out(vty, "\tValid: %d ", pnhc->valid);
b13e5ad6 932 nexthop_group_write_nexthop(vty, pnhc->nexthop);
e5c83d9b
DS
933}
934
935struct pbr_nht_show {
936 struct vty *vty;
937 const char *name;
938};
939
e3b78da8 940static void pbr_nht_show_nhg(struct hash_bucket *b, void *data)
e5c83d9b
DS
941{
942 struct pbr_nexthop_group_cache *pnhgc = b->data;
943 struct pbr_nht_show *pns = data;
944 struct vty *vty;
945
946 if (pns->name && strcmp(pns->name, pnhgc->name) != 0)
947 return;
948
949 vty = pns->vty;
950 vty_out(vty, "Nexthop-Group: %s Table: %u Valid: %d Installed: %d\n",
951 pnhgc->name, pnhgc->table_id, pnhgc->valid, pnhgc->installed);
952
953 hash_iterate(pnhgc->nhh, pbr_nht_show_nhg_nexthops, vty);
954}
955
956void pbr_nht_show_nexthop_group(struct vty *vty, const char *name)
957{
958 struct pbr_nht_show pns;
959
960 pns.vty = vty;
961 pns.name = name;
962
963 hash_iterate(pbr_nhg_hash, pbr_nht_show_nhg, &pns);
964}
965
966void pbr_nht_init(void)
967{
968 pbr_nhg_hash = hash_create_size(
969 16, pbr_nhg_hash_key, pbr_nhg_hash_equal, "PBR NHG Cache Hash");
b13e5ad6 970 pbr_nhrc_hash =
d8b87afe 971 hash_create_size(16, (unsigned int (*)(const void *))nexthop_hash,
b13e5ad6 972 pbr_nhrc_hash_equal, "PBR NH Hash");
e5c83d9b
DS
973
974 pbr_nhg_low_table = PBR_NHT_DEFAULT_LOW_TABLEID;
975 pbr_nhg_high_table = PBR_NHT_DEFAULT_HIGH_TABLEID;
976 pbr_nhg_low_rule = PBR_NHT_DEFAULT_LOW_RULE;
977 pbr_nhg_high_rule = PBR_NHT_DEFAULT_HIGH_RULE;
978 memset(&nhg_tableid, 0, 65535 * sizeof(uint8_t));
979}