]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_conditional_adv.c
zebra: add support for DF delay timer
[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 "bgpd/bgp_conditional_adv.h"
22
23 const char *get_afi_safi_str(afi_t afi, safi_t safi, bool for_json);
24
25 static route_map_result_t
26 bgp_check_rmap_prefixes_in_bgp_table(struct bgp_table *table,
27 struct route_map *rmap)
28 {
29 struct attr dummy_attr = {0};
30 struct bgp_dest *dest;
31 struct bgp_path_info *pi;
32 struct bgp_path_info path = {0};
33 struct bgp_path_info_extra path_extra = {0};
34 const struct prefix *dest_p;
35 route_map_result_t ret = RMAP_DENYMATCH;
36
37 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
38 dest_p = bgp_dest_get_prefix(dest);
39 assert(dest_p);
40
41 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
42 dummy_attr = *pi->attr;
43
44 /* Fill temp path_info */
45 prep_for_rmap_apply(&path, &path_extra, dest, pi,
46 pi->peer, &dummy_attr);
47
48 RESET_FLAG(dummy_attr.rmap_change_flags);
49
50 ret = route_map_apply(rmap, dest_p, &path);
51 if (ret != RMAP_PERMITMATCH)
52 bgp_attr_flush(&dummy_attr);
53 else {
54 bgp_dest_unlock_node(dest);
55 if (BGP_DEBUG(update, UPDATE_OUT))
56 zlog_debug(
57 "%s: Condition map routes present in BGP table",
58 __func__);
59
60 return ret;
61 }
62 }
63 }
64
65 if (BGP_DEBUG(update, UPDATE_OUT))
66 zlog_debug("%s: Condition map routes not present in BGP table",
67 __func__);
68
69 return ret;
70 }
71
72 static void bgp_conditional_adv_routes(struct peer *peer, afi_t afi,
73 safi_t safi, struct bgp_table *table,
74 struct route_map *rmap,
75 enum update_type update_type)
76 {
77 int addpath_capable;
78 struct bgp_dest *dest;
79 struct bgp_path_info *pi;
80 struct bgp_path_info path;
81 struct peer_af *paf;
82 const struct prefix *dest_p;
83 struct update_subgroup *subgrp;
84 struct attr dummy_attr = {0}, attr = {0};
85 struct bgp_path_info_extra path_extra = {0};
86
87 paf = peer_af_find(peer, afi, safi);
88 if (!paf)
89 return;
90
91 subgrp = PAF_SUBGRP(paf);
92 /* Ignore if subgroup doesn't exist (implies AF is not negotiated) */
93 if (!subgrp)
94 return;
95
96 if (BGP_DEBUG(update, UPDATE_OUT))
97 zlog_debug("%s: %s routes to/from %s for %s", __func__,
98 update_type == ADVERTISE ? "Advertise" : "Withdraw",
99 peer->host, get_afi_safi_str(afi, safi, false));
100
101 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
102
103 for (dest = bgp_table_top(table); dest; dest = bgp_route_next(dest)) {
104 dest_p = bgp_dest_get_prefix(dest);
105 assert(dest_p);
106
107 for (pi = bgp_dest_get_bgp_path_info(dest); pi; pi = pi->next) {
108 dummy_attr = *pi->attr;
109
110 /* Fill temp path_info */
111 prep_for_rmap_apply(&path, &path_extra, dest, pi,
112 pi->peer, &dummy_attr);
113
114 RESET_FLAG(dummy_attr.rmap_change_flags);
115
116 if (route_map_apply(rmap, dest_p, &path)
117 != RMAP_PERMITMATCH) {
118 bgp_attr_flush(&dummy_attr);
119 continue;
120 }
121
122 if (CHECK_FLAG(pi->flags, BGP_PATH_SELECTED)
123 || (addpath_capable
124 && bgp_addpath_tx_path(
125 peer->addpath_type[afi][safi],
126 pi))) {
127
128 /* Skip route-map checks in
129 * subgroup_announce_check while executing from
130 * the conditional advertise scanner process.
131 * otherwise when route-map is also configured
132 * on same peer, routes in advertise-map may not
133 * be advertised as expected.
134 */
135 if ((update_type == ADVERTISE)
136 && subgroup_announce_check(dest, pi, subgrp,
137 dest_p, &attr,
138 true))
139 bgp_adj_out_set_subgroup(dest, subgrp,
140 &attr, pi);
141 else {
142 /* If default originate is enabled for
143 * the peer, do not send explicit
144 * withdraw. This will prevent deletion
145 * of default route advertised through
146 * default originate.
147 */
148 if (CHECK_FLAG(
149 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 }
161 }
162 }
163 }
164
165 /* Handler of conditional advertisement timer event.
166 * Each route in the condition-map is evaluated.
167 */
168 static int bgp_conditional_adv_timer(struct thread *t)
169 {
170 afi_t afi;
171 safi_t safi;
172 int pfx_rcd_safi;
173 struct bgp *bgp = NULL;
174 struct peer *peer = NULL;
175 struct peer_af *paf = NULL;
176 struct bgp_table *table = NULL;
177 struct bgp_filter *filter = NULL;
178 struct listnode *node, *nnode = NULL;
179 struct update_subgroup *subgrp = NULL;
180 route_map_result_t ret;
181
182 bgp = THREAD_ARG(t);
183 assert(bgp);
184
185 thread_add_timer(bm->master, bgp_conditional_adv_timer, bgp,
186 CONDITIONAL_ROUTES_POLL_TIME, &bgp->t_condition_check);
187
188 /* loop through each peer and advertise or withdraw routes if
189 * advertise-map is configured and prefix(es) in condition-map
190 * does exist(exist-map)/not exist(non-exist-map) in BGP table
191 * based on condition(exist-map or non-exist map)
192 */
193 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
194 if (!CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
195 continue;
196
197 if (peer->status != Established)
198 continue;
199
200 FOREACH_AFI_SAFI (afi, safi) {
201 if (strmatch(get_afi_safi_str(afi, safi, true),
202 "Unknown"))
203 continue;
204
205 if (!peer->afc_nego[afi][safi])
206 continue;
207
208 /* labeled-unicast routes are installed in the unicast
209 * table so in order to display the correct PfxRcd value
210 * we must look at SAFI_UNICAST
211 */
212 pfx_rcd_safi = (safi == SAFI_LABELED_UNICAST)
213 ? SAFI_UNICAST
214 : safi;
215
216 table = bgp->rib[afi][pfx_rcd_safi];
217 if (!table)
218 continue;
219
220 filter = &peer->filter[afi][safi];
221
222 if (!filter->advmap.aname || !filter->advmap.cname
223 || !filter->advmap.amap || !filter->advmap.cmap)
224 continue;
225
226 if (!peer->advmap_config_change[afi][safi]
227 && !peer->advmap_table_change)
228 continue;
229
230 if (BGP_DEBUG(update, UPDATE_OUT)) {
231 if (peer->advmap_table_change)
232 zlog_debug(
233 "%s: %s - routes changed in BGP table.",
234 __func__, peer->host);
235 if (peer->advmap_config_change[afi][safi])
236 zlog_debug(
237 "%s: %s for %s - advertise/condition map configuration is changed.",
238 __func__, peer->host,
239 get_afi_safi_str(afi, safi,
240 false));
241 }
242
243 /* cmap (route-map attached to exist-map or
244 * non-exist-map) map validation
245 */
246 ret = bgp_check_rmap_prefixes_in_bgp_table(
247 table, filter->advmap.cmap);
248
249 /* Derive conditional advertisement status from
250 * condition and return value of condition-map
251 * validation.
252 */
253 if (filter->advmap.condition == CONDITION_EXIST)
254 filter->advmap.update_type =
255 (ret == RMAP_PERMITMATCH) ? ADVERTISE
256 : WITHDRAW;
257 else
258 filter->advmap.update_type =
259 (ret == RMAP_PERMITMATCH) ? WITHDRAW
260 : ADVERTISE;
261
262 /* Send regular update as per the existing policy.
263 * There is a change in route-map, match-rule, ACLs,
264 * or route-map filter configuration on the same peer.
265 */
266 if (peer->advmap_config_change[afi][safi]) {
267
268 if (BGP_DEBUG(update, UPDATE_OUT))
269 zlog_debug(
270 "%s: Configuration is changed on peer %s for %s, send the normal update first.",
271 __func__, peer->host,
272 get_afi_safi_str(afi, safi,
273 false));
274
275 paf = peer_af_find(peer, afi, safi);
276 if (paf) {
277 update_subgroup_split_peer(paf, NULL);
278 subgrp = paf->subgroup;
279 if (subgrp && subgrp->update_group)
280 subgroup_announce_table(
281 paf->subgroup, NULL);
282 }
283 peer->advmap_config_change[afi][safi] = false;
284 }
285
286 /* Send update as per the conditional advertisement */
287 bgp_conditional_adv_routes(peer, afi, safi, table,
288 filter->advmap.amap,
289 filter->advmap.update_type);
290 }
291 peer->advmap_table_change = false;
292 }
293 return 0;
294 }
295
296 void bgp_conditional_adv_enable(struct peer *peer, afi_t afi, safi_t safi)
297 {
298 struct bgp *bgp = peer->bgp;
299
300 assert(bgp);
301
302 /* This flag is used to monitor conditional routes status in BGP table,
303 * and advertise/withdraw routes only when there is a change in BGP
304 * table w.r.t conditional routes
305 */
306 peer->advmap_config_change[afi][safi] = true;
307
308 /* advertise-map is already configured on atleast one of its
309 * neighbors (AFI/SAFI). So just increment the counter.
310 */
311 if (++bgp->condition_filter_count > 1) {
312 if (BGP_DEBUG(update, UPDATE_OUT))
313 zlog_debug("%s: condition_filter_count %d", __func__,
314 bgp->condition_filter_count);
315
316 return;
317 }
318
319 /* Register for conditional routes polling timer */
320 thread_add_timer(bm->master, bgp_conditional_adv_timer, bgp,
321 CONDITIONAL_ROUTES_POLL_TIME, &bgp->t_condition_check);
322 }
323
324 void bgp_conditional_adv_disable(struct peer *peer, afi_t afi, safi_t safi)
325 {
326 struct bgp *bgp = peer->bgp;
327
328 assert(bgp);
329
330 /* advertise-map is not configured on any of its neighbors or
331 * it is configured on more than one neighbor(AFI/SAFI).
332 * So there's nothing to do except decrementing the counter.
333 */
334 if (--bgp->condition_filter_count != 0) {
335 if (BGP_DEBUG(update, UPDATE_OUT))
336 zlog_debug("%s: condition_filter_count %d", __func__,
337 bgp->condition_filter_count);
338
339 return;
340 }
341
342 /* Last filter removed. So cancel conditional routes polling thread. */
343 THREAD_OFF(bgp->t_condition_check);
344 }