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
23 #include "bgpd/bgp_conditional_adv.h"
24 #include "bgpd/bgp_vty.h"
26 static route_map_result_t
27 bgp_check_rmap_prefixes_in_bgp_table(struct bgp_table
*table
,
28 struct route_map
*rmap
)
30 struct attr dummy_attr
= {0};
31 struct bgp_dest
*dest
;
32 struct bgp_path_info
*pi
;
33 struct bgp_path_info path
= {0};
34 struct bgp_path_info_extra path_extra
= {0};
35 const struct prefix
*dest_p
;
36 route_map_result_t ret
= RMAP_DENYMATCH
;
38 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
39 dest_p
= bgp_dest_get_prefix(dest
);
42 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
43 dummy_attr
= *pi
->attr
;
45 /* Fill temp path_info */
46 prep_for_rmap_apply(&path
, &path_extra
, dest
, pi
,
47 pi
->peer
, &dummy_attr
);
49 RESET_FLAG(dummy_attr
.rmap_change_flags
);
51 ret
= route_map_apply(rmap
, dest_p
, &path
);
52 if (ret
!= RMAP_PERMITMATCH
)
53 bgp_attr_flush(&dummy_attr
);
55 bgp_dest_unlock_node(dest
);
56 if (BGP_DEBUG(update
, UPDATE_OUT
))
58 "%s: Condition map routes present in BGP table",
66 if (BGP_DEBUG(update
, UPDATE_OUT
))
67 zlog_debug("%s: Condition map routes not present in BGP table",
73 static void bgp_conditional_adv_routes(struct peer
*peer
, afi_t afi
,
74 safi_t safi
, struct bgp_table
*table
,
75 struct route_map
*rmap
,
76 enum update_type update_type
)
79 struct bgp_dest
*dest
;
80 struct bgp_path_info
*pi
;
81 struct bgp_path_info path
;
83 const struct prefix
*dest_p
;
84 struct update_subgroup
*subgrp
;
85 struct attr dummy_attr
= {0}, attr
= {0};
86 struct bgp_path_info_extra path_extra
= {0};
88 paf
= peer_af_find(peer
, afi
, safi
);
92 subgrp
= PAF_SUBGRP(paf
);
93 /* Ignore if subgroup doesn't exist (implies AF is not negotiated) */
97 if (BGP_DEBUG(update
, UPDATE_OUT
))
98 zlog_debug("%s: %s routes to/from %s for %s", __func__
,
99 update_type
== ADVERTISE
? "Advertise" : "Withdraw",
100 peer
->host
, get_afi_safi_str(afi
, safi
, false));
102 addpath_capable
= bgp_addpath_encode_tx(peer
, afi
, safi
);
104 for (dest
= bgp_table_top(table
); dest
; dest
= bgp_route_next(dest
)) {
105 dest_p
= bgp_dest_get_prefix(dest
);
108 for (pi
= bgp_dest_get_bgp_path_info(dest
); pi
; pi
= pi
->next
) {
109 dummy_attr
= *pi
->attr
;
111 /* Fill temp path_info */
112 prep_for_rmap_apply(&path
, &path_extra
, dest
, pi
,
113 pi
->peer
, &dummy_attr
);
115 RESET_FLAG(dummy_attr
.rmap_change_flags
);
117 if (route_map_apply(rmap
, dest_p
, &path
)
118 != RMAP_PERMITMATCH
) {
119 bgp_attr_flush(&dummy_attr
);
123 if (CHECK_FLAG(pi
->flags
, BGP_PATH_SELECTED
)
125 && bgp_addpath_tx_path(
126 peer
->addpath_type
[afi
][safi
],
129 /* Skip route-map checks in
130 * subgroup_announce_check while executing from
131 * the conditional advertise scanner process.
132 * otherwise when route-map is also configured
133 * on same peer, routes in advertise-map may not
134 * be advertised as expected.
136 if ((update_type
== ADVERTISE
)
137 && subgroup_announce_check(dest
, pi
, subgrp
,
140 bgp_adj_out_set_subgroup(dest
, subgrp
,
143 /* If default originate is enabled for
144 * the peer, do not send explicit
145 * withdraw. This will prevent deletion
146 * of default route advertised through
150 peer
->af_flags
[afi
][safi
],
151 PEER_FLAG_DEFAULT_ORIGINATE
)
152 && is_default_prefix(dest_p
))
155 bgp_adj_out_unset_subgroup(
157 bgp_addpath_id_for_peer(
166 /* Handler of conditional advertisement timer event.
167 * Each route in the condition-map is evaluated.
169 static int bgp_conditional_adv_timer(struct thread
*t
)
174 struct bgp
*bgp
= NULL
;
175 struct peer
*peer
= NULL
;
176 struct peer_af
*paf
= NULL
;
177 struct bgp_table
*table
= NULL
;
178 struct bgp_filter
*filter
= NULL
;
179 struct listnode
*node
, *nnode
= NULL
;
180 struct update_subgroup
*subgrp
= NULL
;
181 route_map_result_t ret
;
186 thread_add_timer(bm
->master
, bgp_conditional_adv_timer
, bgp
,
187 CONDITIONAL_ROUTES_POLL_TIME
, &bgp
->t_condition_check
);
189 /* loop through each peer and advertise or withdraw routes if
190 * advertise-map is configured and prefix(es) in condition-map
191 * does exist(exist-map)/not exist(non-exist-map) in BGP table
192 * based on condition(exist-map or non-exist map)
194 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
195 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
))
198 if (peer
->status
!= Established
)
201 FOREACH_AFI_SAFI (afi
, safi
) {
202 if (!peer
->afc_nego
[afi
][safi
])
205 /* labeled-unicast routes are installed in the unicast
206 * table so in order to display the correct PfxRcd value
207 * we must look at SAFI_UNICAST
209 pfx_rcd_safi
= (safi
== SAFI_LABELED_UNICAST
)
213 table
= bgp
->rib
[afi
][pfx_rcd_safi
];
217 filter
= &peer
->filter
[afi
][safi
];
219 if (!filter
->advmap
.aname
|| !filter
->advmap
.cname
220 || !filter
->advmap
.amap
|| !filter
->advmap
.cmap
)
223 if (!peer
->advmap_config_change
[afi
][safi
]
224 && !peer
->advmap_table_change
)
227 if (BGP_DEBUG(update
, UPDATE_OUT
)) {
228 if (peer
->advmap_table_change
)
230 "%s: %s - routes changed in BGP table.",
231 __func__
, peer
->host
);
232 if (peer
->advmap_config_change
[afi
][safi
])
234 "%s: %s for %s - advertise/condition map configuration is changed.",
235 __func__
, peer
->host
,
236 get_afi_safi_str(afi
, safi
,
240 /* cmap (route-map attached to exist-map or
241 * non-exist-map) map validation
243 ret
= bgp_check_rmap_prefixes_in_bgp_table(
244 table
, filter
->advmap
.cmap
);
246 /* Derive conditional advertisement status from
247 * condition and return value of condition-map
250 if (filter
->advmap
.condition
== CONDITION_EXIST
)
251 filter
->advmap
.update_type
=
252 (ret
== RMAP_PERMITMATCH
) ? ADVERTISE
255 filter
->advmap
.update_type
=
256 (ret
== RMAP_PERMITMATCH
) ? WITHDRAW
259 /* Send regular update as per the existing policy.
260 * There is a change in route-map, match-rule, ACLs,
261 * or route-map filter configuration on the same peer.
263 if (peer
->advmap_config_change
[afi
][safi
]) {
265 if (BGP_DEBUG(update
, UPDATE_OUT
))
267 "%s: Configuration is changed on peer %s for %s, send the normal update first.",
268 __func__
, peer
->host
,
269 get_afi_safi_str(afi
, safi
,
272 paf
= peer_af_find(peer
, afi
, safi
);
274 update_subgroup_split_peer(paf
, NULL
);
275 subgrp
= paf
->subgroup
;
276 if (subgrp
&& subgrp
->update_group
)
277 subgroup_announce_table(
278 paf
->subgroup
, NULL
);
280 peer
->advmap_config_change
[afi
][safi
] = false;
283 /* Send update as per the conditional advertisement */
284 bgp_conditional_adv_routes(peer
, afi
, safi
, table
,
286 filter
->advmap
.update_type
);
288 peer
->advmap_table_change
= false;
293 void bgp_conditional_adv_enable(struct peer
*peer
, afi_t afi
, safi_t safi
)
295 struct bgp
*bgp
= peer
->bgp
;
299 /* This flag is used to monitor conditional routes status in BGP table,
300 * and advertise/withdraw routes only when there is a change in BGP
301 * table w.r.t conditional routes
303 peer
->advmap_config_change
[afi
][safi
] = true;
305 /* advertise-map is already configured on atleast one of its
306 * neighbors (AFI/SAFI). So just increment the counter.
308 if (++bgp
->condition_filter_count
> 1) {
309 if (BGP_DEBUG(update
, UPDATE_OUT
))
310 zlog_debug("%s: condition_filter_count %d", __func__
,
311 bgp
->condition_filter_count
);
316 /* Register for conditional routes polling timer */
317 thread_add_timer(bm
->master
, bgp_conditional_adv_timer
, bgp
,
318 CONDITIONAL_ROUTES_POLL_TIME
, &bgp
->t_condition_check
);
321 void bgp_conditional_adv_disable(struct peer
*peer
, afi_t afi
, safi_t safi
)
323 struct bgp
*bgp
= peer
->bgp
;
327 /* advertise-map is not configured on any of its neighbors or
328 * it is configured on more than one neighbor(AFI/SAFI).
329 * So there's nothing to do except decrementing the counter.
331 if (--bgp
->condition_filter_count
!= 0) {
332 if (BGP_DEBUG(update
, UPDATE_OUT
))
333 zlog_debug("%s: condition_filter_count %d", __func__
,
334 bgp
->condition_filter_count
);
339 /* Last filter removed. So cancel conditional routes polling thread. */
340 THREAD_OFF(bgp
->t_condition_check
);