]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_conditional_adv.c
Merge pull request #11842 from opensourcerouting/fix/topotests_platform_check
[mirror_frr.git] / bgpd / bgp_conditional_adv.c
1 /*
2 * BGP Conditional advertisement
3 * Copyright (C) 2020 Samsung R&D Institute India - Bangalore.
4 * Madhurilatha Kuruganti
5 *
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)
9 * any later version.
10 *
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
14 * 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
21 #include <zebra.h>
22
23 #include "bgpd/bgp_conditional_adv.h"
24 #include "bgpd/bgp_vty.h"
25
26 static route_map_result_t
27 bgp_check_rmap_prefixes_in_bgp_table(struct bgp_table *table,
28 struct route_map *rmap)
29 {
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;
37
38 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
39 dest_p = bgp_dest_get_prefix(dest);
40 assert(dest_p);
41
42 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
43 dummy_attr = *pi->attr;
44
45 /* Fill temp path_info */
46 prep_for_rmap_apply(&path, &path_extra, dest, pi,
47 pi->peer, &dummy_attr);
48
49 RESET_FLAG(dummy_attr.rmap_change_flags);
50
51 ret = route_map_apply(rmap, dest_p, &path);
52 bgp_attr_flush(&dummy_attr);
53
54 if (ret == RMAP_PERMITMATCH) {
55 bgp_dest_unlock_node(dest);
56 if (BGP_DEBUG(update, UPDATE_OUT))
57 zlog_debug(
58 "%s: Condition map routes present in BGP table",
59 __func__);
60
61 return ret;
62 }
63 }
64 }
65
66 if (BGP_DEBUG(update, UPDATE_OUT))
67 zlog_debug("%s: Condition map routes not present in BGP table",
68 __func__);
69
70 return ret;
71 }
72
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)
77 {
78 bool addpath_capable;
79 struct bgp_dest *dest;
80 struct bgp_path_info *pi;
81 struct bgp_path_info path;
82 struct peer_af *paf;
83 const struct prefix *dest_p;
84 struct update_subgroup *subgrp;
85 struct attr advmap_attr = {0}, attr = {0};
86 struct bgp_path_info_extra path_extra = {0};
87 route_map_result_t ret;
88
89 paf = peer_af_find(peer, afi, safi);
90 if (!paf)
91 return;
92
93 subgrp = PAF_SUBGRP(paf);
94 /* Ignore if subgroup doesn't exist (implies AF is not negotiated) */
95 if (!subgrp)
96 return;
97
98 subgrp->pscount = 0;
99 SET_FLAG(subgrp->sflags, SUBGRP_STATUS_TABLE_REPARSING);
100
101 if (BGP_DEBUG(update, UPDATE_OUT))
102 zlog_debug("%s: %s routes to/from %s for %s", __func__,
103 update_type == UPDATE_TYPE_ADVERTISE ? "Advertise"
104 : "Withdraw",
105 peer->host, get_afi_safi_str(afi, safi, false));
106
107 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
108
109 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
110 dest_p = bgp_dest_get_prefix(dest);
111 assert(dest_p);
112
113 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
114 advmap_attr = *pi->attr;
115
116 /* Fill temp path_info */
117 prep_for_rmap_apply(&path, &path_extra, dest, pi,
118 pi->peer, &advmap_attr);
119
120 RESET_FLAG(advmap_attr.rmap_change_flags);
121
122 ret = route_map_apply(rmap, dest_p, &path);
123 if (ret != RMAP_PERMITMATCH ||
124 !bgp_check_selected(pi, peer, addpath_capable, afi,
125 safi)) {
126 bgp_attr_flush(&advmap_attr);
127 continue;
128 }
129
130 /* Skip route-map checks in
131 * subgroup_announce_check while executing from
132 * the conditional advertise scanner process.
133 * otherwise when route-map is also configured
134 * on same peer, routes in advertise-map may not
135 * be advertised as expected.
136 */
137 if (update_type == UPDATE_TYPE_ADVERTISE &&
138 subgroup_announce_check(dest, pi, subgrp, dest_p,
139 &attr, &advmap_attr)) {
140 bgp_adj_out_set_subgroup(dest, subgrp, &attr,
141 pi);
142 } else {
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
147 * default originate.
148 */
149 if (CHECK_FLAG(peer->af_flags[afi][safi],
150 PEER_FLAG_DEFAULT_ORIGINATE) &&
151 is_default_prefix(dest_p))
152 break;
153
154 bgp_adj_out_unset_subgroup(
155 dest, subgrp, 1,
156 bgp_addpath_id_for_peer(
157 peer, afi, safi,
158 &pi->tx_addpath));
159 }
160 bgp_attr_flush(&advmap_attr);
161 }
162 }
163 UNSET_FLAG(subgrp->sflags, SUBGRP_STATUS_TABLE_REPARSING);
164 }
165
166 /* Handler of conditional advertisement timer event.
167 * Each route in the condition-map is evaluated.
168 */
169 static void bgp_conditional_adv_timer(struct thread *t)
170 {
171 afi_t afi;
172 safi_t safi;
173 int pfx_rcd_safi;
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;
182
183 bgp = THREAD_ARG(t);
184 assert(bgp);
185
186 thread_add_timer(bm->master, bgp_conditional_adv_timer, bgp,
187 bgp->condition_check_period, &bgp->t_condition_check);
188
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)
193 */
194 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
195 if (!CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
196 continue;
197
198 if (!peer_established(peer))
199 continue;
200
201 FOREACH_AFI_SAFI (afi, safi) {
202 if (!peer->afc_nego[afi][safi])
203 continue;
204
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
208 */
209 pfx_rcd_safi = (safi == SAFI_LABELED_UNICAST)
210 ? SAFI_UNICAST
211 : safi;
212
213 table = bgp->rib[afi][pfx_rcd_safi];
214 if (!table)
215 continue;
216
217 filter = &peer->filter[afi][safi];
218
219 if (!filter->advmap.aname || !filter->advmap.cname
220 || !filter->advmap.amap || !filter->advmap.cmap)
221 continue;
222
223 if (!peer->advmap_config_change[afi][safi]
224 && !peer->advmap_table_change)
225 continue;
226
227 if (BGP_DEBUG(update, UPDATE_OUT)) {
228 if (peer->advmap_table_change)
229 zlog_debug(
230 "%s: %s - routes changed in BGP table.",
231 __func__, peer->host);
232 if (peer->advmap_config_change[afi][safi])
233 zlog_debug(
234 "%s: %s for %s - advertise/condition map configuration is changed.",
235 __func__, peer->host,
236 get_afi_safi_str(afi, safi,
237 false));
238 }
239
240 /* cmap (route-map attached to exist-map or
241 * non-exist-map) map validation
242 */
243 ret = bgp_check_rmap_prefixes_in_bgp_table(
244 table, filter->advmap.cmap);
245
246 /* Derive conditional advertisement status from
247 * condition and return value of condition-map
248 * validation.
249 */
250 if (filter->advmap.condition == CONDITION_EXIST)
251 filter->advmap.update_type =
252 (ret == RMAP_PERMITMATCH)
253 ? UPDATE_TYPE_ADVERTISE
254 : UPDATE_TYPE_WITHDRAW;
255 else
256 filter->advmap.update_type =
257 (ret == RMAP_PERMITMATCH)
258 ? UPDATE_TYPE_WITHDRAW
259 : UPDATE_TYPE_ADVERTISE;
260
261 /*
262 * Update condadv update type so
263 * subgroup_announce_check() can properly apply
264 * outbound policy according to advertisement state
265 */
266 paf = peer_af_find(peer, afi, safi);
267 if (paf && (SUBGRP_PEER(PAF_SUBGRP(paf))
268 ->filter[afi][safi]
269 .advmap.update_type !=
270 filter->advmap.update_type)) {
271 /* Handle change to peer advmap */
272 if (BGP_DEBUG(update, UPDATE_OUT))
273 zlog_debug(
274 "%s: advmap.update_type changed for peer %s, adjusting update_group.",
275 __func__, peer->host);
276
277 update_group_adjust_peer(paf);
278 }
279
280 /* Send regular update as per the existing policy.
281 * There is a change in route-map, match-rule, ACLs,
282 * or route-map filter configuration on the same peer.
283 */
284 if (peer->advmap_config_change[afi][safi]) {
285
286 if (BGP_DEBUG(update, UPDATE_OUT))
287 zlog_debug(
288 "%s: Configuration is changed on peer %s for %s, send the normal update first.",
289 __func__, peer->host,
290 get_afi_safi_str(afi, safi,
291 false));
292 if (paf) {
293 update_subgroup_split_peer(paf, NULL);
294 subgrp = paf->subgroup;
295
296 if (subgrp && subgrp->update_group)
297 subgroup_announce_table(
298 paf->subgroup, NULL);
299 }
300 peer->advmap_config_change[afi][safi] = false;
301 }
302
303 /* Send update as per the conditional advertisement */
304 bgp_conditional_adv_routes(peer, afi, safi, table,
305 filter->advmap.amap,
306 filter->advmap.update_type);
307 }
308 peer->advmap_table_change = false;
309 }
310 }
311
312 void bgp_conditional_adv_enable(struct peer *peer, afi_t afi, safi_t safi)
313 {
314 struct bgp *bgp = peer->bgp;
315
316 assert(bgp);
317
318 /* This flag is used to monitor conditional routes status in BGP table,
319 * and advertise/withdraw routes only when there is a change in BGP
320 * table w.r.t conditional routes
321 */
322 peer->advmap_config_change[afi][safi] = true;
323
324 /* advertise-map is already configured on at least one of its
325 * neighbors (AFI/SAFI). So just increment the counter.
326 */
327 if (++bgp->condition_filter_count > 1) {
328 if (BGP_DEBUG(update, UPDATE_OUT))
329 zlog_debug("%s: condition_filter_count %d", __func__,
330 bgp->condition_filter_count);
331
332 return;
333 }
334
335 /* Register for conditional routes polling timer */
336 if (!thread_is_scheduled(bgp->t_condition_check))
337 thread_add_timer(bm->master, bgp_conditional_adv_timer, bgp, 0,
338 &bgp->t_condition_check);
339 }
340
341 void bgp_conditional_adv_disable(struct peer *peer, afi_t afi, safi_t safi)
342 {
343 struct bgp *bgp = peer->bgp;
344
345 assert(bgp);
346
347 /* advertise-map is not configured on any of its neighbors or
348 * it is configured on more than one neighbor(AFI/SAFI).
349 * So there's nothing to do except decrementing the counter.
350 */
351 if (--bgp->condition_filter_count != 0) {
352 if (BGP_DEBUG(update, UPDATE_OUT))
353 zlog_debug("%s: condition_filter_count %d", __func__,
354 bgp->condition_filter_count);
355
356 return;
357 }
358
359 /* Last filter removed. So cancel conditional routes polling thread. */
360 THREAD_OFF(bgp->t_condition_check);
361 }