]> git.proxmox.com Git - mirror_frr.git/blob - pbrd/pbr_map.c
Merge pull request #4765 from opensourcerouting/defaults-v2
[mirror_frr.git] / pbrd / pbr_map.c
1 /*
2 * PBR-map 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 "thread.h"
23 #include "linklist.h"
24 #include "prefix.h"
25 #include "table.h"
26 #include "vrf.h"
27 #include "nexthop.h"
28 #include "nexthop_group.h"
29 #include "memory.h"
30 #include "log.h"
31 #include "vty.h"
32
33 #include "pbr_nht.h"
34 #include "pbr_map.h"
35 #include "pbr_zebra.h"
36 #include "pbr_memory.h"
37 #include "pbr_debug.h"
38 #include "pbr_vrf.h"
39
40 DEFINE_MTYPE_STATIC(PBRD, PBR_MAP, "PBR Map")
41 DEFINE_MTYPE_STATIC(PBRD, PBR_MAP_SEQNO, "PBR Map Sequence")
42 DEFINE_MTYPE_STATIC(PBRD, PBR_MAP_INTERFACE, "PBR Map Interface")
43
44 static uint32_t pbr_map_sequence_unique;
45
46 static bool pbr_map_check_valid_internal(struct pbr_map *pbrm);
47 static inline int pbr_map_compare(const struct pbr_map *pbrmap1,
48 const struct pbr_map *pbrmap2);
49
50 RB_GENERATE(pbr_map_entry_head, pbr_map, pbr_map_entry, pbr_map_compare)
51
52 struct pbr_map_entry_head pbr_maps = RB_INITIALIZER(&pbr_maps);
53
54 DEFINE_QOBJ_TYPE(pbr_map_sequence)
55
56 static inline int pbr_map_compare(const struct pbr_map *pbrmap1,
57 const struct pbr_map *pbrmap2)
58 {
59 return strcmp(pbrmap1->name, pbrmap2->name);
60 }
61
62 static int pbr_map_sequence_compare(const struct pbr_map_sequence *pbrms1,
63 const struct pbr_map_sequence *pbrms2)
64 {
65 if (pbrms1->seqno == pbrms2->seqno)
66 return 0;
67
68 if (pbrms1->seqno < pbrms2->seqno)
69 return -1;
70
71 return 1;
72 }
73
74 static void pbr_map_sequence_delete(struct pbr_map_sequence *pbrms)
75 {
76 XFREE(MTYPE_TMP, pbrms->internal_nhg_name);
77
78 XFREE(MTYPE_PBR_MAP_SEQNO, pbrms);
79 }
80
81 static int pbr_map_interface_compare(const struct pbr_map_interface *pmi1,
82 const struct pbr_map_interface *pmi2)
83 {
84 return strcmp(pmi1->ifp->name, pmi2->ifp->name);
85 }
86
87 static void pbr_map_interface_list_delete(struct pbr_map_interface *pmi)
88 {
89 struct pbr_map_interface *pmi_int;
90 struct listnode *node, *nnode;
91 struct pbr_map *pbrm;
92
93 RB_FOREACH (pbrm, pbr_map_entry_head, &pbr_maps) {
94 for (ALL_LIST_ELEMENTS(pbrm->incoming, node, nnode, pmi_int)) {
95 if (pmi == pmi_int) {
96 pbr_map_policy_delete(pbrm, pmi);
97 return;
98 }
99 }
100 }
101 }
102
103 static bool pbr_map_interface_is_valid(const struct pbr_map_interface *pmi)
104 {
105 /* Don't install rules without a real ifindex on the incoming interface.
106 *
107 * This can happen when we have config for an interface that does not
108 * exist or when an interface is changing vrfs.
109 */
110 if (pmi->ifp && pmi->ifp->ifindex != IFINDEX_INTERNAL)
111 return true;
112
113 return false;
114 }
115
116 static void pbr_map_pbrms_update_common(struct pbr_map_sequence *pbrms,
117 bool install)
118 {
119 struct pbr_map *pbrm;
120 struct listnode *node;
121 struct pbr_map_interface *pmi;
122
123 pbrm = pbrms->parent;
124
125 if (pbrms->nhs_installed && pbrm->incoming->count) {
126 for (ALL_LIST_ELEMENTS_RO(pbrm->incoming, node, pmi)) {
127 if (!pmi->ifp)
128 continue;
129
130 if (install && !pbr_map_interface_is_valid(pmi))
131 continue;
132
133 pbr_send_pbr_map(pbrms, pmi, install);
134 }
135 }
136 }
137
138 static void pbr_map_pbrms_install(struct pbr_map_sequence *pbrms)
139 {
140 pbr_map_pbrms_update_common(pbrms, true);
141 }
142
143 static void pbr_map_pbrms_uninstall(struct pbr_map_sequence *pbrms)
144 {
145 pbr_map_pbrms_update_common(pbrms, false);
146 }
147
148 static const char *const pbr_map_reason_str[] = {
149 "Invalid NH-group", "Invalid NH", "No Nexthops",
150 "Both NH and NH-Group", "Invalid Src or Dst", "Invalid VRF",
151 "Deleting Sequence",
152 };
153
154 void pbr_map_reason_string(unsigned int reason, char *buf, int size)
155 {
156 unsigned int bit;
157 int len = 0;
158
159 if (!buf)
160 return;
161
162 for (bit = 0; bit < array_size(pbr_map_reason_str); bit++) {
163 if ((reason & (1 << bit)) && (len < size)) {
164 len += snprintf((buf + len), (size - len), "%s%s",
165 (len > 0) ? ", " : "",
166 pbr_map_reason_str[bit]);
167 }
168 }
169 }
170
171 void pbr_map_final_interface_deletion(struct pbr_map *pbrm,
172 struct pbr_map_interface *pmi)
173 {
174 if (pmi->delete == true) {
175 listnode_delete(pbrm->incoming, pmi);
176 pmi->pbrm = NULL;
177
178 bf_release_index(pbrm->ifi_bitfield, pmi->install_bit);
179 XFREE(MTYPE_PBR_MAP_INTERFACE, pmi);
180 }
181 }
182
183 void pbr_map_interface_delete(struct pbr_map *pbrm, struct interface *ifp_del)
184 {
185
186 struct listnode *node;
187 struct pbr_map_interface *pmi;
188
189 for (ALL_LIST_ELEMENTS_RO(pbrm->incoming, node, pmi)) {
190 if (ifp_del == pmi->ifp)
191 break;
192 }
193
194 if (pmi)
195 pbr_map_policy_delete(pbrm, pmi);
196 }
197
198 void pbr_map_add_interface(struct pbr_map *pbrm, struct interface *ifp_add)
199 {
200 struct listnode *node;
201 struct pbr_map_interface *pmi;
202
203 for (ALL_LIST_ELEMENTS_RO(pbrm->incoming, node, pmi)) {
204 if (ifp_add == pmi->ifp)
205 return;
206 }
207
208 pmi = XCALLOC(MTYPE_PBR_MAP_INTERFACE, sizeof(*pmi));
209 pmi->ifp = ifp_add;
210 pmi->pbrm = pbrm;
211 listnode_add_sort(pbrm->incoming, pmi);
212
213 bf_assign_index(pbrm->ifi_bitfield, pmi->install_bit);
214 pbr_map_check_valid(pbrm->name);
215 if (pbrm->valid)
216 pbr_map_install(pbrm);
217 }
218
219 static int
220 pbr_map_policy_interface_update_common(const struct interface *ifp,
221 struct pbr_interface **pbr_ifp,
222 struct pbr_map **pbrm)
223 {
224 if (!ifp->info) {
225 DEBUGD(&pbr_dbg_map, "%s: %s has no pbr_interface info",
226 __func__, ifp->name);
227 return -1;
228 }
229
230 *pbr_ifp = ifp->info;
231
232 *pbrm = pbrm_find((*pbr_ifp)->mapname);
233
234 if (!*pbrm) {
235 DEBUGD(&pbr_dbg_map, "%s: applied PBR-MAP(%s) does not exist?",
236 __func__, (*pbr_ifp)->mapname);
237 return -1;
238 }
239
240 return 0;
241 }
242
243 void pbr_map_policy_interface_update(const struct interface *ifp, bool state_up)
244 {
245 struct pbr_interface *pbr_ifp;
246 struct pbr_map_sequence *pbrms;
247 struct pbr_map *pbrm;
248 struct listnode *node, *inode;
249 struct pbr_map_interface *pmi;
250
251 if (pbr_map_policy_interface_update_common(ifp, &pbr_ifp, &pbrm))
252 return;
253
254 DEBUGD(&pbr_dbg_map, "%s: %s %s rules on interface %s", __func__,
255 pbr_ifp->mapname, (state_up ? "installing" : "removing"),
256 ifp->name);
257
258 /*
259 * Walk the list and install/remove maps on the interface.
260 */
261 for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, node, pbrms))
262 for (ALL_LIST_ELEMENTS_RO(pbrm->incoming, inode, pmi))
263 if (pmi->ifp == ifp && pbr_map_interface_is_valid(pmi))
264 pbr_send_pbr_map(pbrms, pmi, state_up);
265 }
266
267 static void pbrms_vrf_update(struct pbr_map_sequence *pbrms,
268 const struct pbr_vrf *pbr_vrf)
269 {
270 const char *vrf_name = pbr_vrf_name(pbr_vrf);
271
272 if (pbrms->vrf_lookup
273 && (strncmp(vrf_name, pbrms->vrf_name, sizeof(pbrms->vrf_name))
274 == 0)) {
275 DEBUGD(&pbr_dbg_map, "\tSeq %u uses vrf %s (%u), updating map",
276 pbrms->seqno, vrf_name, pbr_vrf_id(pbr_vrf));
277
278 pbr_map_check(pbrms);
279 }
280 }
281
282 /* Vrf enabled/disabled */
283 void pbr_map_vrf_update(const struct pbr_vrf *pbr_vrf)
284 {
285 struct pbr_map *pbrm;
286 struct pbr_map_sequence *pbrms;
287 struct listnode *node;
288
289 if (!pbr_vrf)
290 return;
291
292 bool enabled = pbr_vrf_is_enabled(pbr_vrf);
293
294 DEBUGD(&pbr_dbg_map, "%s: %s (%u) %s, updating pbr maps", __func__,
295 pbr_vrf_name(pbr_vrf), pbr_vrf_id(pbr_vrf),
296 enabled ? "enabled" : "disabled");
297
298 RB_FOREACH (pbrm, pbr_map_entry_head, &pbr_maps) {
299 DEBUGD(&pbr_dbg_map, "%s: Looking at %s", __PRETTY_FUNCTION__,
300 pbrm->name);
301 for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, node, pbrms))
302 pbrms_vrf_update(pbrms, pbr_vrf);
303 }
304 }
305
306 void pbr_map_write_interfaces(struct vty *vty, struct interface *ifp)
307 {
308 struct pbr_interface *pbr_ifp = ifp->info;
309
310 if (pbr_ifp
311 && strncmp(pbr_ifp->mapname, "", sizeof(pbr_ifp->mapname)) != 0)
312 vty_out(vty, " pbr-policy %s\n", pbr_ifp->mapname);
313 }
314
315 struct pbr_map *pbrm_find(const char *name)
316 {
317 struct pbr_map pbrm;
318
319 strlcpy(pbrm.name, name, sizeof(pbrm.name));
320
321 return RB_FIND(pbr_map_entry_head, &pbr_maps, &pbrm);
322 }
323
324 extern void pbr_map_delete(struct pbr_map_sequence *pbrms)
325 {
326 struct pbr_map *pbrm;
327 struct listnode *inode;
328 struct pbr_map_interface *pmi;
329
330 pbrm = pbrms->parent;
331
332 for (ALL_LIST_ELEMENTS_RO(pbrm->incoming, inode, pmi))
333 pbr_send_pbr_map(pbrms, pmi, false);
334
335 if (pbrms->nhg)
336 pbr_nht_delete_individual_nexthop(pbrms);
337
338 listnode_delete(pbrm->seqnumbers, pbrms);
339
340 if (pbrm->seqnumbers->count == 0) {
341 RB_REMOVE(pbr_map_entry_head, &pbr_maps, pbrm);
342
343 bf_free(pbrm->ifi_bitfield);
344 XFREE(MTYPE_PBR_MAP, pbrm);
345 }
346 }
347
348 static void pbr_map_delete_common(struct pbr_map_sequence *pbrms)
349 {
350 struct pbr_map *pbrm = pbrms->parent;
351
352 pbr_map_pbrms_uninstall(pbrms);
353
354 pbrm->valid = false;
355 pbrms->nhs_installed = false;
356 pbrms->reason |= PBR_MAP_INVALID_NO_NEXTHOPS;
357 pbrms->nhgrp_name = NULL;
358 }
359
360 void pbr_map_delete_nexthops(struct pbr_map_sequence *pbrms)
361 {
362 pbr_map_delete_common(pbrms);
363 }
364
365 void pbr_map_delete_vrf(struct pbr_map_sequence *pbrms)
366 {
367 pbr_map_delete_common(pbrms);
368 }
369
370 struct pbr_map_sequence *pbrms_lookup_unique(uint32_t unique, ifindex_t ifindex,
371 struct pbr_map_interface **ppmi)
372 {
373 struct pbr_map_sequence *pbrms;
374 struct listnode *snode, *inode;
375 struct pbr_map_interface *pmi;
376 struct pbr_map *pbrm;
377
378 RB_FOREACH (pbrm, pbr_map_entry_head, &pbr_maps) {
379 for (ALL_LIST_ELEMENTS_RO(pbrm->incoming, inode, pmi)) {
380 if (pmi->ifp->ifindex != ifindex)
381 continue;
382
383 if (ppmi)
384 *ppmi = pmi;
385
386 for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, snode,
387 pbrms)) {
388 DEBUGD(&pbr_dbg_map, "%s: Comparing %u to %u",
389 __PRETTY_FUNCTION__, pbrms->unique,
390 unique);
391 if (pbrms->unique == unique)
392 return pbrms;
393 }
394 }
395 }
396
397 return NULL;
398 }
399
400 static void pbr_map_add_interfaces(struct pbr_map *pbrm)
401 {
402 struct interface *ifp;
403 struct pbr_interface *pbr_ifp;
404 struct vrf *vrf;
405
406 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
407 FOR_ALL_INTERFACES (vrf, ifp) {
408 if (ifp->info) {
409 pbr_ifp = ifp->info;
410 if (strcmp(pbrm->name, pbr_ifp->mapname) == 0)
411 pbr_map_add_interface(pbrm, ifp);
412 }
413 }
414 }
415 }
416
417 struct pbr_map_sequence *pbrms_get(const char *name, uint32_t seqno)
418 {
419 struct pbr_map *pbrm;
420 struct pbr_map_sequence *pbrms;
421 struct listnode *node;
422
423 pbrm = pbrm_find(name);
424 if (!pbrm) {
425 pbrm = XCALLOC(MTYPE_PBR_MAP, sizeof(*pbrm));
426 snprintf(pbrm->name, sizeof(pbrm->name), "%s", name);
427
428 pbrm->seqnumbers = list_new();
429 pbrm->seqnumbers->cmp =
430 (int (*)(void *, void *))pbr_map_sequence_compare;
431 pbrm->seqnumbers->del =
432 (void (*)(void *))pbr_map_sequence_delete;
433
434 pbrm->incoming = list_new();
435 pbrm->incoming->cmp =
436 (int (*)(void *, void *))pbr_map_interface_compare;
437 pbrm->incoming->del =
438 (void (*)(void *))pbr_map_interface_list_delete;
439
440 RB_INSERT(pbr_map_entry_head, &pbr_maps, pbrm);
441
442 bf_init(pbrm->ifi_bitfield, 64);
443 pbr_map_add_interfaces(pbrm);
444 }
445
446 for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, node, pbrms)) {
447 if (pbrms->seqno == seqno)
448 break;
449
450 }
451
452 if (!pbrms) {
453 pbrms = XCALLOC(MTYPE_PBR_MAP_SEQNO, sizeof(*pbrms));
454 pbrms->unique = pbr_map_sequence_unique++;
455 pbrms->seqno = seqno;
456 pbrms->ruleno = pbr_nht_get_next_rule(seqno);
457 pbrms->parent = pbrm;
458 pbrms->reason =
459 PBR_MAP_INVALID_EMPTY |
460 PBR_MAP_INVALID_NO_NEXTHOPS;
461 pbrms->vrf_name[0] = '\0';
462
463 QOBJ_REG(pbrms, pbr_map_sequence);
464 listnode_add_sort(pbrm->seqnumbers, pbrms);
465 }
466
467 return pbrms;
468 }
469
470 static void
471 pbr_map_sequence_check_nexthops_valid(struct pbr_map_sequence *pbrms)
472 {
473 /* Check if any are present first */
474 if (!pbrms->vrf_unchanged && !pbrms->vrf_lookup && !pbrms->nhg
475 && !pbrms->nhgrp_name) {
476 pbrms->reason |= PBR_MAP_INVALID_NO_NEXTHOPS;
477 return;
478 }
479
480 /*
481 * Check validness of vrf.
482 */
483
484 /* This one can be considered always valid */
485 if (pbrms->vrf_unchanged)
486 pbrms->nhs_installed = true;
487
488 if (pbrms->vrf_lookup) {
489 struct pbr_vrf *pbr_vrf =
490 pbr_vrf_lookup_by_name(pbrms->vrf_name);
491
492 if (pbr_vrf && pbr_vrf_is_valid(pbr_vrf))
493 pbrms->nhs_installed = true;
494 else
495 pbrms->reason |= PBR_MAP_INVALID_VRF;
496 }
497
498 /*
499 * Check validness of the nexthop or nexthop-group
500 */
501
502 /* Only nexthop or nexthop group allowed */
503 if (pbrms->nhg && pbrms->nhgrp_name)
504 pbrms->reason |= PBR_MAP_INVALID_BOTH_NHANDGRP;
505
506 if (pbrms->nhg &&
507 !pbr_nht_nexthop_group_valid(pbrms->internal_nhg_name))
508 pbrms->reason |= PBR_MAP_INVALID_NEXTHOP;
509
510 if (pbrms->nhgrp_name) {
511 if (!pbr_nht_nexthop_group_valid(pbrms->nhgrp_name))
512 pbrms->reason |= PBR_MAP_INVALID_NEXTHOP_GROUP;
513 else
514 pbrms->nhs_installed = true;
515 }
516 }
517
518 static void pbr_map_sequence_check_not_empty(struct pbr_map_sequence *pbrms)
519 {
520 if (!pbrms->src && !pbrms->dst && !pbrms->mark)
521 pbrms->reason |= PBR_MAP_INVALID_EMPTY;
522 }
523
524 /*
525 * Checks to see if we think that the pbmrs is valid. If we think
526 * the config is valid return true.
527 */
528 static void pbr_map_sequence_check_valid(struct pbr_map_sequence *pbrms)
529 {
530 pbr_map_sequence_check_nexthops_valid(pbrms);
531
532 pbr_map_sequence_check_not_empty(pbrms);
533 }
534
535 static bool pbr_map_check_valid_internal(struct pbr_map *pbrm)
536 {
537 struct pbr_map_sequence *pbrms;
538 struct listnode *node;
539
540 pbrm->valid = true;
541 for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, node, pbrms)) {
542 pbrms->reason = 0;
543 pbr_map_sequence_check_valid(pbrms);
544 /*
545 * A pbr_map_sequence that is invalid causes
546 * the whole shebang to be invalid
547 */
548 if (pbrms->reason != 0)
549 pbrm->valid = false;
550 }
551
552 return pbrm->valid;
553 }
554
555 /*
556 * For a given PBR-MAP check to see if we think it is a
557 * valid config or not. If so note that it is and return
558 * that we are valid.
559 */
560 bool pbr_map_check_valid(const char *name)
561 {
562 struct pbr_map *pbrm;
563
564 pbrm = pbrm_find(name);
565 if (!pbrm) {
566 DEBUGD(&pbr_dbg_map,
567 "%s: Specified PBR-MAP(%s) does not exist?",
568 __PRETTY_FUNCTION__, name);
569 return false;
570 }
571
572 pbr_map_check_valid_internal(pbrm);
573 return pbrm->valid;
574 }
575
576 void pbr_map_schedule_policy_from_nhg(const char *nh_group)
577 {
578 struct pbr_map_sequence *pbrms;
579 struct pbr_map *pbrm;
580 struct listnode *node;
581
582 RB_FOREACH (pbrm, pbr_map_entry_head, &pbr_maps) {
583 DEBUGD(&pbr_dbg_map, "%s: Looking at %s", __PRETTY_FUNCTION__,
584 pbrm->name);
585 for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, node, pbrms)) {
586 DEBUGD(&pbr_dbg_map, "\tNH Grp name: %s",
587 pbrms->nhgrp_name ?
588 pbrms->nhgrp_name : pbrms->internal_nhg_name);
589
590 if (pbrms->nhgrp_name
591 && (strcmp(nh_group, pbrms->nhgrp_name) == 0)) {
592 pbrms->nhs_installed = true;
593
594 pbr_map_check(pbrms);
595 }
596
597 if (pbrms->nhg
598 && (strcmp(nh_group, pbrms->internal_nhg_name)
599 == 0)) {
600 pbrms->nhs_installed = true;
601
602 pbr_map_check(pbrms);
603 }
604 }
605 }
606 }
607
608 void pbr_map_policy_install(const char *name)
609 {
610 struct pbr_map_sequence *pbrms;
611 struct pbr_map *pbrm;
612 struct listnode *node, *inode;
613 struct pbr_map_interface *pmi;
614
615 DEBUGD(&pbr_dbg_map, "%s: for %s", __PRETTY_FUNCTION__, name);
616 pbrm = pbrm_find(name);
617 if (!pbrm)
618 return;
619
620 for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, node, pbrms)) {
621 DEBUGD(&pbr_dbg_map,
622 "%s: Looking at what to install %s(%u) %d %d",
623 __PRETTY_FUNCTION__, name, pbrms->seqno, pbrm->valid,
624 pbrms->nhs_installed);
625
626 if (pbrm->valid && pbrms->nhs_installed
627 && pbrm->incoming->count) {
628 DEBUGD(&pbr_dbg_map, "\tInstalling %s %u", pbrm->name,
629 pbrms->seqno);
630 for (ALL_LIST_ELEMENTS_RO(pbrm->incoming, inode, pmi))
631 if (pbr_map_interface_is_valid(pmi))
632 pbr_send_pbr_map(pbrms, pmi, true);
633 }
634 }
635 }
636
637 void pbr_map_policy_delete(struct pbr_map *pbrm, struct pbr_map_interface *pmi)
638 {
639 struct listnode *node;
640 struct pbr_map_sequence *pbrms;
641
642
643 for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, node, pbrms))
644 pbr_send_pbr_map(pbrms, pmi, false);
645
646 pmi->delete = true;
647 }
648
649 /*
650 * For a nexthop group specified, see if any of the pbr-maps
651 * are using it and if so, check to see that we are still
652 * valid for usage. If we are valid then schedule the installation/deletion
653 * of the pbr-policy.
654 */
655 void pbr_map_check_nh_group_change(const char *nh_group)
656 {
657 struct pbr_map_sequence *pbrms;
658 struct pbr_map *pbrm;
659 struct listnode *node, *inode;
660 struct pbr_map_interface *pmi;
661 bool found_name;
662
663 RB_FOREACH (pbrm, pbr_map_entry_head, &pbr_maps) {
664 for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, node, pbrms)) {
665 found_name = false;
666 if (pbrms->nhgrp_name)
667 found_name =
668 !strcmp(nh_group, pbrms->nhgrp_name);
669 else if (pbrms->nhg)
670 found_name = !strcmp(nh_group,
671 pbrms->internal_nhg_name);
672
673 if (found_name) {
674 bool original = pbrm->valid;
675
676 pbr_map_check_valid_internal(pbrm);
677
678 if (pbrm->valid && (original != pbrm->valid))
679 pbr_map_install(pbrm);
680
681 if (pbrm->valid == false)
682 for (ALL_LIST_ELEMENTS_RO(
683 pbrm->incoming, inode,
684 pmi))
685 pbr_send_pbr_map(pbrms, pmi,
686 false);
687 }
688 }
689 }
690 }
691
692 void pbr_map_check(struct pbr_map_sequence *pbrms)
693 {
694 struct pbr_map *pbrm;
695 bool install;
696
697 pbrm = pbrms->parent;
698 DEBUGD(&pbr_dbg_map, "%s: for %s(%u)", __PRETTY_FUNCTION__,
699 pbrm->name, pbrms->seqno);
700 if (pbr_map_check_valid(pbrm->name))
701 DEBUGD(&pbr_dbg_map, "We are totally valid %s",
702 pbrm->name);
703
704 if (pbrms->reason == PBR_MAP_VALID_SEQUENCE_NUMBER) {
705 install = true;
706 DEBUGD(&pbr_dbg_map, "%s: Installing %s(%u) reason: %" PRIu64,
707 __PRETTY_FUNCTION__, pbrm->name, pbrms->seqno,
708 pbrms->reason);
709 DEBUGD(&pbr_dbg_map,
710 "\tSending PBR_MAP_POLICY_INSTALL event");
711 } else {
712 install = false;
713 DEBUGD(&pbr_dbg_map,
714 "%s: Removing %s(%u) reason: %" PRIu64,
715 __PRETTY_FUNCTION__, pbrm->name,
716 pbrms->seqno, pbrms->reason);
717 }
718
719 if (install)
720 pbr_map_pbrms_install(pbrms);
721 else
722 pbr_map_pbrms_uninstall(pbrms);
723 }
724
725 void pbr_map_install(struct pbr_map *pbrm)
726 {
727 struct pbr_map_sequence *pbrms;
728 struct listnode *node;
729
730 if (!pbrm->incoming->count)
731 return;
732
733 for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, node, pbrms))
734 pbr_map_pbrms_install(pbrms);
735 }
736
737 void pbr_map_init(void)
738 {
739 RB_INIT(pbr_map_entry_head, &pbr_maps);
740
741 pbr_map_sequence_unique = 1;
742 }