3 * Copyright (C) 2018 Cumulus Networks, Inc.
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
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.
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
28 #include "nexthop_group.h"
35 #include "pbr_zebra.h"
36 #include "pbr_memory.h"
37 #include "pbr_debug.h"
39 DEFINE_MTYPE_STATIC(PBRD
, PBR_MAP
, "PBR Map")
40 DEFINE_MTYPE_STATIC(PBRD
, PBR_MAP_SEQNO
, "PBR Map Sequence")
41 DEFINE_MTYPE_STATIC(PBRD
, PBR_MAP_INTERFACE
, "PBR Map Interface")
43 static uint32_t pbr_map_sequence_unique
;
45 static inline int pbr_map_compare(const struct pbr_map
*pbrmap1
,
46 const struct pbr_map
*pbrmap2
);
48 RB_GENERATE(pbr_map_entry_head
, pbr_map
, pbr_map_entry
, pbr_map_compare
)
50 struct pbr_map_entry_head pbr_maps
= RB_INITIALIZER(&pbr_maps
);
52 DEFINE_QOBJ_TYPE(pbr_map_sequence
)
54 static inline int pbr_map_compare(const struct pbr_map
*pbrmap1
,
55 const struct pbr_map
*pbrmap2
)
57 return strcmp(pbrmap1
->name
, pbrmap2
->name
);
60 static int pbr_map_sequence_compare(const struct pbr_map_sequence
*pbrms1
,
61 const struct pbr_map_sequence
*pbrms2
)
63 if (pbrms1
->seqno
== pbrms2
->seqno
)
66 if (pbrms1
->seqno
< pbrms2
->seqno
)
72 static void pbr_map_sequence_delete(struct pbr_map_sequence
*pbrms
)
74 if (pbrms
->internal_nhg_name
)
75 XFREE(MTYPE_TMP
, pbrms
->internal_nhg_name
);
77 XFREE(MTYPE_PBR_MAP_SEQNO
, pbrms
);
80 static int pbr_map_interface_compare(const struct pbr_map_interface
*pmi1
,
81 const struct pbr_map_interface
*pmi2
)
83 return strcmp(pmi1
->ifp
->name
, pmi2
->ifp
->name
);
86 static void pbr_map_interface_list_delete(struct pbr_map_interface
*pmi
)
88 struct pbr_map_interface
*pmi_int
;
89 struct listnode
*node
, *nnode
;
92 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
) {
93 for (ALL_LIST_ELEMENTS(pbrm
->incoming
, node
, nnode
, pmi_int
)) {
95 pbr_map_policy_delete(pbrm
, pmi
);
102 static const char *pbr_map_reason_str
[] = {
103 "Invalid NH-group", "Invalid NH", "No Nexthops",
104 "Both NH and NH-Group", "Invalid Src or Dst", "Deleting Sequence",
107 void pbr_map_reason_string(unsigned int reason
, char *buf
, int size
)
115 for (bit
= 0; bit
< array_size(pbr_map_reason_str
); bit
++) {
116 if ((reason
& (1 << bit
)) && (len
< size
)) {
117 len
+= snprintf((buf
+ len
), (size
- len
), "%s%s",
118 (len
> 0) ? ", " : "",
119 pbr_map_reason_str
[bit
]);
125 void pbr_map_interface_delete(struct pbr_map
*pbrm
, struct interface
*ifp_del
)
128 struct listnode
*node
;
129 struct pbr_map_interface
*pmi
;
131 for (ALL_LIST_ELEMENTS_RO(pbrm
->incoming
, node
, pmi
)) {
132 if (ifp_del
== pmi
->ifp
)
137 pbr_map_policy_delete(pbrm
, pmi
);
140 void pbr_map_add_interface(struct pbr_map
*pbrm
, struct interface
*ifp_add
)
142 struct listnode
*node
;
143 struct pbr_map_interface
*pmi
;
145 for (ALL_LIST_ELEMENTS_RO(pbrm
->incoming
, node
, pmi
)) {
146 if (ifp_add
== pmi
->ifp
)
150 pmi
= XCALLOC(MTYPE_PBR_MAP_INTERFACE
, sizeof(*pmi
));
153 listnode_add_sort(pbrm
->incoming
, pmi
);
155 pbr_map_check_valid(pbrm
->name
);
156 if (pbrm
->valid
&& !pbrm
->installed
)
157 pbr_map_install(pbrm
);
160 void pbr_map_write_interfaces(struct vty
*vty
, struct interface
*ifp
)
162 struct pbr_interface
*pbr_ifp
= ifp
->info
;
165 && strncmp(pbr_ifp
->mapname
, "", sizeof(pbr_ifp
->mapname
)) != 0)
166 vty_out(vty
, " pbr-policy %s\n", pbr_ifp
->mapname
);
169 struct pbr_map
*pbrm_find(const char *name
)
173 strlcpy(pbrm
.name
, name
, sizeof(pbrm
.name
));
175 return RB_FIND(pbr_map_entry_head
, &pbr_maps
, &pbrm
);
178 extern void pbr_map_delete(struct pbr_map_sequence
*pbrms
)
180 struct pbr_map
*pbrm
;
181 struct listnode
*inode
;
182 struct pbr_map_interface
*pmi
;
184 pbrm
= pbrms
->parent
;
186 for (ALL_LIST_ELEMENTS_RO(pbrm
->incoming
, inode
, pmi
))
187 pbr_send_pbr_map(pbrms
, pmi
, false);
190 pbr_nht_delete_individual_nexthop(pbrms
);
192 listnode_delete(pbrm
->seqnumbers
, pbrms
);
194 if (pbrm
->seqnumbers
->count
== 0) {
195 RB_REMOVE(pbr_map_entry_head
, &pbr_maps
, pbrm
);
196 XFREE(MTYPE_PBR_MAP
, pbrm
);
200 void pbr_map_delete_nexthop_group(struct pbr_map_sequence
*pbrms
)
202 struct pbr_map
*pbrm
= pbrms
->parent
;
203 struct listnode
*node
;
204 struct pbr_map_interface
*pmi
;
206 if (pbrm
->valid
&& pbrms
->nhs_installed
&& pbrm
->incoming
->count
) {
207 for (ALL_LIST_ELEMENTS_RO(pbrm
->incoming
, node
, pmi
))
208 pbr_send_pbr_map(pbrms
, pmi
, false);
212 pbrms
->nhs_installed
= false;
213 pbrms
->installed
= false;
214 pbrms
->reason
|= PBR_MAP_INVALID_NO_NEXTHOPS
;
215 pbrms
->nhgrp_name
= NULL
;
218 struct pbr_map_sequence
*pbrms_lookup_unique(uint32_t unique
,
221 struct pbr_map_sequence
*pbrms
;
222 struct listnode
*snode
, *inode
;
223 struct pbr_map_interface
*pmi
;
224 struct pbr_map
*pbrm
;
226 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
) {
227 for (ALL_LIST_ELEMENTS_RO(pbrm
->incoming
, inode
, pmi
)) {
228 if (pmi
->ifp
->ifindex
!= ifindex
)
231 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, snode
,
233 DEBUGD(&pbr_dbg_map
, "%s: Comparing %u to %u",
234 __PRETTY_FUNCTION__
, pbrms
->unique
,
236 if (pbrms
->unique
== unique
)
245 static void pbr_map_add_interfaces(struct pbr_map
*pbrm
)
247 struct interface
*ifp
;
248 struct pbr_interface
*pbr_ifp
;
251 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
252 FOR_ALL_INTERFACES (vrf
, ifp
) {
255 if (strcmp(pbrm
->name
, pbr_ifp
->mapname
) == 0)
256 pbr_map_add_interface(pbrm
, ifp
);
262 struct pbr_map_sequence
*pbrms_get(const char *name
, uint32_t seqno
)
264 struct pbr_map
*pbrm
;
265 struct pbr_map_sequence
*pbrms
;
266 struct listnode
*node
;
268 pbrm
= pbrm_find(name
);
270 pbrm
= XCALLOC(MTYPE_PBR_MAP
, sizeof(*pbrm
));
271 strcpy(pbrm
->name
, name
);
273 pbrm
->seqnumbers
= list_new();
274 pbrm
->seqnumbers
->cmp
=
275 (int (*)(void *, void *))pbr_map_sequence_compare
;
276 pbrm
->seqnumbers
->del
=
277 (void (*)(void *))pbr_map_sequence_delete
;
279 pbrm
->incoming
= list_new();
280 pbrm
->incoming
->cmp
=
281 (int (*)(void *, void *))pbr_map_interface_compare
;
282 pbrm
->incoming
->del
=
283 (void (*)(void *))pbr_map_interface_list_delete
;
285 RB_INSERT(pbr_map_entry_head
, &pbr_maps
, pbrm
);
287 pbr_map_add_interfaces(pbrm
);
290 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
)) {
291 if (pbrms
->seqno
== seqno
)
297 pbrms
= XCALLOC(MTYPE_PBR_MAP_SEQNO
, sizeof(*pbrms
));
298 pbrms
->unique
= pbr_map_sequence_unique
++;
299 pbrms
->seqno
= seqno
;
300 pbrms
->ruleno
= pbr_nht_get_next_rule(seqno
);
301 pbrms
->parent
= pbrm
;
303 PBR_MAP_INVALID_SRCDST
|
304 PBR_MAP_INVALID_NO_NEXTHOPS
;
306 QOBJ_REG(pbrms
, pbr_map_sequence
);
307 listnode_add_sort(pbrm
->seqnumbers
, pbrms
);
309 pbrm
->installed
= false;
316 pbr_map_sequence_check_nexthops_valid(struct pbr_map_sequence
*pbrms
)
319 * Check validness of the nexthop or nexthop-group
321 if (!pbrms
->nhg
&& !pbrms
->nhgrp_name
)
322 pbrms
->reason
|= PBR_MAP_INVALID_NO_NEXTHOPS
;
324 if (pbrms
->nhg
&& pbrms
->nhgrp_name
)
325 pbrms
->reason
|= PBR_MAP_INVALID_BOTH_NHANDGRP
;
328 !pbr_nht_nexthop_group_valid(pbrms
->internal_nhg_name
))
329 pbrms
->reason
|= PBR_MAP_INVALID_NEXTHOP
;
331 if (pbrms
->nhgrp_name
) {
332 if (!pbr_nht_nexthop_group_valid(pbrms
->nhgrp_name
))
333 pbrms
->reason
|= PBR_MAP_INVALID_NEXTHOP_GROUP
;
335 pbrms
->nhs_installed
= true;
339 static void pbr_map_sequence_check_src_dst_valid(struct pbr_map_sequence
*pbrms
)
341 if (!pbrms
->src
&& !pbrms
->dst
)
342 pbrms
->reason
|= PBR_MAP_INVALID_SRCDST
;
346 * Checks to see if we think that the pbmrs is valid. If we think
347 * the config is valid return true.
349 static void pbr_map_sequence_check_valid(struct pbr_map_sequence
*pbrms
)
351 pbr_map_sequence_check_nexthops_valid(pbrms
);
353 pbr_map_sequence_check_src_dst_valid(pbrms
);
356 static bool pbr_map_check_valid_internal(struct pbr_map
*pbrm
)
358 struct pbr_map_sequence
*pbrms
;
359 struct listnode
*node
;
362 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
)) {
364 pbr_map_sequence_check_valid(pbrms
);
366 * A pbr_map_sequence that is invalid causes
367 * the whole shebang to be invalid
369 if (pbrms
->reason
!= 0)
377 * For a given PBR-MAP check to see if we think it is a
378 * valid config or not. If so note that it is and return
381 bool pbr_map_check_valid(const char *name
)
383 struct pbr_map
*pbrm
;
385 pbrm
= pbrm_find(name
);
388 "%s: Specified PBR-MAP(%s) does not exist?",
389 __PRETTY_FUNCTION__
, name
);
393 pbr_map_check_valid_internal(pbrm
);
397 void pbr_map_schedule_policy_from_nhg(const char *nh_group
)
399 struct pbr_map_sequence
*pbrms
;
400 struct pbr_map
*pbrm
;
401 struct listnode
*node
;
403 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
) {
404 DEBUGD(&pbr_dbg_map
, "%s: Looking at %s", __PRETTY_FUNCTION__
,
406 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
)) {
407 DEBUGD(&pbr_dbg_map
, "\tNH Grp name: %s",
408 pbrms
->nhgrp_name
? pbrms
->nhgrp_name
: "NULL");
410 if (pbrms
->nhgrp_name
411 && (strcmp(nh_group
, pbrms
->nhgrp_name
) == 0)) {
412 pbrms
->nhs_installed
= true;
414 pbr_map_check(pbrms
);
418 && (strcmp(nh_group
, pbrms
->internal_nhg_name
)
420 pbrms
->nhs_installed
= true;
422 pbr_map_check(pbrms
);
428 void pbr_map_policy_install(const char *name
)
430 struct pbr_map_sequence
*pbrms
;
431 struct pbr_map
*pbrm
;
432 struct listnode
*node
, *inode
;
433 struct pbr_map_interface
*pmi
;
435 DEBUGD(&pbr_dbg_map
, "%s: for %s", __PRETTY_FUNCTION__
, name
);
436 pbrm
= pbrm_find(name
);
440 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
)) {
442 "%s: Looking at what to install %s(%u) %d %d",
443 __PRETTY_FUNCTION__
, name
, pbrms
->seqno
, pbrm
->valid
,
444 pbrms
->nhs_installed
);
446 if (pbrm
->valid
&& pbrms
->nhs_installed
&& pbrm
->incoming
->count
) {
447 DEBUGD(&pbr_dbg_map
, "\tInstalling %s %u",
448 pbrm
->name
, pbrms
->seqno
);
449 for (ALL_LIST_ELEMENTS_RO(pbrm
->incoming
, inode
, pmi
))
450 pbr_send_pbr_map(pbrms
, pmi
, true);
455 void pbr_map_policy_delete(struct pbr_map
*pbrm
, struct pbr_map_interface
*pmi
)
457 struct listnode
*node
;
458 struct pbr_map_sequence
*pbrms
;
461 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
))
462 pbr_send_pbr_map(pbrms
, pmi
, false);
464 listnode_delete(pbrm
->incoming
, pmi
);
466 XFREE(MTYPE_PBR_MAP_INTERFACE
, pmi
);
470 * For a nexthop group specified, see if any of the pbr-maps
471 * are using it and if so, check to see that we are still
472 * valid for usage. If we are valid then schedule the installation/deletion
475 void pbr_map_check_nh_group_change(const char *nh_group
)
477 struct pbr_map_sequence
*pbrms
;
478 struct pbr_map
*pbrm
;
479 struct listnode
*node
, *inode
;
480 struct pbr_map_interface
*pmi
;
483 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
) {
484 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
)) {
486 if (pbrms
->nhgrp_name
)
488 !strcmp(nh_group
, pbrms
->nhgrp_name
);
490 found_name
= !strcmp(nh_group
,
491 pbrms
->internal_nhg_name
);
494 bool original
= pbrm
->valid
;
496 pbr_map_check_valid_internal(pbrm
);
498 if (pbrm
->valid
&& (original
!= pbrm
->valid
))
499 pbr_map_install(pbrm
);
501 if (pbrm
->valid
== false)
502 for (ALL_LIST_ELEMENTS_RO(
503 pbrm
->incoming
, inode
,
505 pbr_send_pbr_map(pbrms
, pmi
,
512 void pbr_map_check(struct pbr_map_sequence
*pbrms
)
514 struct pbr_map
*pbrm
;
515 struct listnode
*inode
;
516 struct pbr_map_interface
*pmi
;
519 pbrm
= pbrms
->parent
;
520 DEBUGD(&pbr_dbg_map
, "%s: for %s(%u)", __PRETTY_FUNCTION__
,
521 pbrm
->name
, pbrms
->seqno
);
522 if (pbr_map_check_valid(pbrm
->name
))
523 DEBUGD(&pbr_dbg_map
, "We are totally valid %s\n",
526 DEBUGD(&pbr_dbg_map
, "%s: Installing %s(%u) reason: %" PRIu64
,
527 __PRETTY_FUNCTION__
, pbrm
->name
, pbrms
->seqno
, pbrms
->reason
);
529 if (pbrms
->reason
== PBR_MAP_VALID_SEQUENCE_NUMBER
) {
530 if (pbrms
->installed
) {
532 for (ALL_LIST_ELEMENTS_RO(pbrm
->incoming
, inode
, pmi
)) {
533 pbr_send_pbr_map(pbrms
, pmi
, install
);
538 DEBUGD(&pbr_dbg_map
, "%s: Installing %s(%u) reason: %" PRIu64
,
539 __PRETTY_FUNCTION__
, pbrm
->name
, pbrms
->seqno
,
542 "\tSending PBR_MAP_POLICY_INSTALL event");
546 "%s: Removing %s(%u) reason: %" PRIu64
,
547 __PRETTY_FUNCTION__
, pbrm
->name
,
548 pbrms
->seqno
, pbrms
->reason
);
551 for (ALL_LIST_ELEMENTS_RO(pbrm
->incoming
, inode
, pmi
))
552 pbr_send_pbr_map(pbrms
, pmi
, install
);
555 void pbr_map_install(struct pbr_map
*pbrm
)
557 struct listnode
*node
, *inode
;
558 struct pbr_map_sequence
*pbrms
;
559 struct pbr_map_interface
*pmi
;
561 if (!pbrm
->incoming
->count
)
564 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
))
565 for (ALL_LIST_ELEMENTS_RO(pbrm
->incoming
, inode
, pmi
))
566 pbr_send_pbr_map(pbrms
, pmi
, true);
568 pbrm
->installed
= true;
571 void pbr_map_init(void)
573 RB_INIT(pbr_map_entry_head
, &pbr_maps
);
575 pbr_map_sequence_unique
= 1;