2 * BGP Conditional advertisement
3 * Copyright (C) 2020 Samsung R&D Institute India - Bangalore.
4 * Madhurilatha Kuruganti
6 * This program 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 Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
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
21 #include "bgpd/bgp_conditional_adv.h"
22 #include "bgpd/bgp_vty.h"
24 static route_map_result_t
25 bgp_check_rmap_prefixes_in_bgp_table(struct bgp_table
*table
,
26 struct route_map
*rmap
)
28 struct attr dummy_attr
= {0};
29 struct bgp_dest
*dest
;
30 struct bgp_path_info
*pi
;
31 struct bgp_path_info path
= {0};
32 struct bgp_path_info_extra path_extra
= {0};
33 const struct prefix
*dest_p
;
34 route_map_result_t ret
= RMAP_DENYMATCH
;
36 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
37 dest_p
= bgp_dest_get_prefix(dest
);
40 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
41 dummy_attr
= *pi
->attr
;
43 /* Fill temp path_info */
44 prep_for_rmap_apply(&path
, &path_extra
, dest
, pi
,
45 pi
->peer
, &dummy_attr
);
47 RESET_FLAG(dummy_attr
.rmap_change_flags
);
49 ret
= route_map_apply(rmap
, dest_p
, &path
);
50 if (ret
!= RMAP_PERMITMATCH
)
51 bgp_attr_flush(&dummy_attr
);
53 bgp_dest_unlock_node(dest
);
54 if (BGP_DEBUG(update
, UPDATE_OUT
))
56 "%s: Condition map routes present in BGP table",
64 if (BGP_DEBUG(update
, UPDATE_OUT
))
65 zlog_debug("%s: Condition map routes not present in BGP table",
71 static void bgp_conditional_adv_routes(struct peer
*peer
, afi_t afi
,
72 safi_t safi
, struct bgp_table
*table
,
73 struct route_map
*rmap
,
74 enum update_type update_type
)
77 struct bgp_dest
*dest
;
78 struct bgp_path_info
*pi
;
79 struct bgp_path_info path
;
81 const struct prefix
*dest_p
;
82 struct update_subgroup
*subgrp
;
83 struct attr dummy_attr
= {0}, attr
= {0};
84 struct bgp_path_info_extra path_extra
= {0};
86 paf
= peer_af_find(peer
, afi
, safi
);
90 subgrp
= PAF_SUBGRP(paf
);
91 /* Ignore if subgroup doesn't exist (implies AF is not negotiated) */
95 if (BGP_DEBUG(update
, UPDATE_OUT
))
96 zlog_debug("%s: %s routes to/from %s for %s", __func__
,
97 update_type
== ADVERTISE
? "Advertise" : "Withdraw",
98 peer
->host
, get_afi_safi_str(afi
, safi
, false));
100 addpath_capable
= bgp_addpath_encode_tx(peer
, afi
, safi
);
102 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
103 dest_p
= bgp_dest_get_prefix(dest
);
106 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
107 dummy_attr
= *pi
->attr
;
109 /* Fill temp path_info */
110 prep_for_rmap_apply(&path
, &path_extra
, dest
, pi
,
111 pi
->peer
, &dummy_attr
);
113 RESET_FLAG(dummy_attr
.rmap_change_flags
);
115 if (route_map_apply(rmap
, dest_p
, &path
)
116 != RMAP_PERMITMATCH
) {
117 bgp_attr_flush(&dummy_attr
);
121 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
123 && bgp_addpath_tx_path(
124 peer
->addpath_type
[afi
][safi
],
127 /* Skip route-map checks in
128 * subgroup_announce_check while executing from
129 * the conditional advertise scanner process.
130 * otherwise when route-map is also configured
131 * on same peer, routes in advertise-map may not
132 * be advertised as expected.
134 if ((update_type
== ADVERTISE
)
135 && subgroup_announce_check(dest
, pi
, subgrp
,
138 bgp_adj_out_set_subgroup(dest
, subgrp
,
141 /* If default originate is enabled for
142 * the peer, do not send explicit
143 * withdraw. This will prevent deletion
144 * of default route advertised through
148 peer
->af_flags
[afi
][safi
],
149 PEER_FLAG_DEFAULT_ORIGINATE
)
150 && is_default_prefix(dest_p
))
153 bgp_adj_out_unset_subgroup(
155 bgp_addpath_id_for_peer(
164 /* Handler of conditional advertisement timer event.
165 * Each route in the condition-map is evaluated.
167 static int bgp_conditional_adv_timer(struct thread
*t
)
172 struct bgp
*bgp
= NULL
;
173 struct peer
*peer
= NULL
;
174 struct peer_af
*paf
= NULL
;
175 struct bgp_table
*table
= NULL
;
176 struct bgp_filter
*filter
= NULL
;
177 struct listnode
*node
, *nnode
= NULL
;
178 struct update_subgroup
*subgrp
= NULL
;
179 route_map_result_t ret
;
184 thread_add_timer(bm
->master
, bgp_conditional_adv_timer
, bgp
,
185 CONDITIONAL_ROUTES_POLL_TIME
, &bgp
->t_condition_check
);
187 /* loop through each peer and advertise or withdraw routes if
188 * advertise-map is configured and prefix(es) in condition-map
189 * does exist(exist-map)/not exist(non-exist-map) in BGP table
190 * based on condition(exist-map or non-exist map)
192 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
193 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
196 if (peer
->status
!= Established
)
199 FOREACH_AFI_SAFI (afi
, safi
) {
200 if (!peer
->afc_nego
[afi
][safi
])
203 /* labeled-unicast routes are installed in the unicast
204 * table so in order to display the correct PfxRcd value
205 * we must look at SAFI_UNICAST
207 pfx_rcd_safi
= (safi
== SAFI_LABELED_UNICAST
)
211 table
= bgp
->rib
[afi
][pfx_rcd_safi
];
215 filter
= &peer
->filter
[afi
][safi
];
217 if (!filter
->advmap
.aname
|| !filter
->advmap
.cname
218 || !filter
->advmap
.amap
|| !filter
->advmap
.cmap
)
221 if (!peer
->advmap_config_change
[afi
][safi
]
222 && !peer
->advmap_table_change
)
225 if (BGP_DEBUG(update
, UPDATE_OUT
)) {
226 if (peer
->advmap_table_change
)
228 "%s: %s - routes changed in BGP table.",
229 __func__
, peer
->host
);
230 if (peer
->advmap_config_change
[afi
][safi
])
232 "%s: %s for %s - advertise/condition map configuration is changed.",
233 __func__
, peer
->host
,
234 get_afi_safi_str(afi
, safi
,
238 /* cmap (route-map attached to exist-map or
239 * non-exist-map) map validation
241 ret
= bgp_check_rmap_prefixes_in_bgp_table(
242 table
, filter
->advmap
.cmap
);
244 /* Derive conditional advertisement status from
245 * condition and return value of condition-map
248 if (filter
->advmap
.condition
== CONDITION_EXIST
)
249 filter
->advmap
.update_type
=
250 (ret
== RMAP_PERMITMATCH
) ? ADVERTISE
253 filter
->advmap
.update_type
=
254 (ret
== RMAP_PERMITMATCH
) ? WITHDRAW
257 /* Send regular update as per the existing policy.
258 * There is a change in route-map, match-rule, ACLs,
259 * or route-map filter configuration on the same peer.
261 if (peer
->advmap_config_change
[afi
][safi
]) {
263 if (BGP_DEBUG(update
, UPDATE_OUT
))
265 "%s: Configuration is changed on peer %s for %s, send the normal update first.",
266 __func__
, peer
->host
,
267 get_afi_safi_str(afi
, safi
,
270 paf
= peer_af_find(peer
, afi
, safi
);
272 update_subgroup_split_peer(paf
, NULL
);
273 subgrp
= paf
->subgroup
;
274 if (subgrp
&& subgrp
->update_group
)
275 subgroup_announce_table(
276 paf
->subgroup
, NULL
);
278 peer
->advmap_config_change
[afi
][safi
] = false;
281 /* Send update as per the conditional advertisement */
282 bgp_conditional_adv_routes(peer
, afi
, safi
, table
,
284 filter
->advmap
.update_type
);
286 peer
->advmap_table_change
= false;
291 void bgp_conditional_adv_enable(struct peer
*peer
, afi_t afi
, safi_t safi
)
293 struct bgp
*bgp
= peer
->bgp
;
297 /* This flag is used to monitor conditional routes status in BGP table,
298 * and advertise/withdraw routes only when there is a change in BGP
299 * table w.r.t conditional routes
301 peer
->advmap_config_change
[afi
][safi
] = true;
303 /* advertise-map is already configured on atleast one of its
304 * neighbors (AFI/SAFI). So just increment the counter.
306 if (++bgp
->condition_filter_count
> 1) {
307 if (BGP_DEBUG(update
, UPDATE_OUT
))
308 zlog_debug("%s: condition_filter_count %d", __func__
,
309 bgp
->condition_filter_count
);
314 /* Register for conditional routes polling timer */
315 thread_add_timer(bm
->master
, bgp_conditional_adv_timer
, bgp
,
316 CONDITIONAL_ROUTES_POLL_TIME
, &bgp
->t_condition_check
);
319 void bgp_conditional_adv_disable(struct peer
*peer
, afi_t afi
, safi_t safi
)
321 struct bgp
*bgp
= peer
->bgp
;
325 /* advertise-map is not configured on any of its neighbors or
326 * it is configured on more than one neighbor(AFI/SAFI).
327 * So there's nothing to do except decrementing the counter.
329 if (--bgp
->condition_filter_count
!= 0) {
330 if (BGP_DEBUG(update
, UPDATE_OUT
))
331 zlog_debug("%s: condition_filter_count %d", __func__
,
332 bgp
->condition_filter_count
);
337 /* Last filter removed. So cancel conditional routes polling thread. */
338 THREAD_OFF(bgp
->t_condition_check
);