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