]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_updgrp.c
Merge pull request #7003 from GalaxyGorilla/ospf_ti_lfa_prep
[mirror_frr.git] / bgpd / bgp_updgrp.c
CommitLineData
3f9c7369
DS
1/**
2 * bgp_updgrp.c: BGP update group structures
3 *
4 * @copyright Copyright (C) 2014 Cumulus Networks, Inc.
5 *
6 * @author Avneesh Sachdev <avneesh@sproute.net>
7 * @author Rajesh Varadarajan <rajesh@sproute.net>
8 * @author Pradosh Mohapatra <pradosh@sproute.net>
9 *
10 * This file is part of GNU Zebra.
11 *
12 * GNU Zebra is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2, or (at your option) any
15 * later version.
16 *
17 * GNU Zebra is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
896014f4
DL
22 * You should have received a copy of the GNU General Public License along
23 * with this program; see the file COPYING; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
3f9c7369
DS
25 */
26
27#include <zebra.h>
28
29#include "prefix.h"
30#include "thread.h"
31#include "buffer.h"
32#include "stream.h"
33#include "command.h"
34#include "sockunion.h"
35#include "network.h"
36#include "memory.h"
37#include "filter.h"
38#include "routemap.h"
3f9c7369
DS
39#include "log.h"
40#include "plist.h"
41#include "linklist.h"
42#include "workqueue.h"
43#include "hash.h"
44#include "jhash.h"
45#include "queue.h"
46
47#include "bgpd/bgpd.h"
48#include "bgpd/bgp_table.h"
49#include "bgpd/bgp_debug.h"
14454c9f 50#include "bgpd/bgp_errors.h"
3f9c7369
DS
51#include "bgpd/bgp_fsm.h"
52#include "bgpd/bgp_advertise.h"
53#include "bgpd/bgp_packet.h"
54#include "bgpd/bgp_updgrp.h"
55#include "bgpd/bgp_route.h"
56#include "bgpd/bgp_filter.h"
2fc102e1 57#include "bgpd/bgp_io.h"
3f9c7369
DS
58
59/********************
60 * PRIVATE FUNCTIONS
61 ********************/
62
63/**
64 * assign a unique ID to update group and subgroup. Mostly for display/
65 * debugging purposes. It's a 64-bit space - used leisurely without a
66 * worry about its wrapping and about filling gaps. While at it, timestamp
67 * the creation.
68 */
d62a17ae 69static void update_group_checkin(struct update_group *updgrp)
3f9c7369 70{
d62a17ae 71 updgrp->id = ++bm->updgrp_idspace;
72 updgrp->uptime = bgp_clock();
3f9c7369
DS
73}
74
d62a17ae 75static void update_subgroup_checkin(struct update_subgroup *subgrp,
76 struct update_group *updgrp)
3f9c7369 77{
d62a17ae 78 subgrp->id = ++bm->subgrp_idspace;
79 subgrp->uptime = bgp_clock();
3f9c7369
DS
80}
81
d62a17ae 82static void sync_init(struct update_subgroup *subgrp)
3f9c7369 83{
d62a17ae 84 subgrp->sync =
85 XCALLOC(MTYPE_BGP_SYNCHRONISE, sizeof(struct bgp_synchronize));
a274fef8
DL
86 bgp_adv_fifo_init(&subgrp->sync->update);
87 bgp_adv_fifo_init(&subgrp->sync->withdraw);
88 bgp_adv_fifo_init(&subgrp->sync->withdraw_low);
996c9314
LB
89 subgrp->hash =
90 hash_create(baa_hash_key, baa_hash_cmp, "BGP SubGroup Hash");
d62a17ae 91
92 /* We use a larger buffer for subgrp->work in the event that:
93 * - We RX a BGP_UPDATE where the attributes alone are just
94 * under BGP_MAX_PACKET_SIZE
95 * - The user configures an outbound route-map that does many as-path
96 * prepends or adds many communities. At most they can have
97 * CMD_ARGC_MAX
98 * args in a route-map so there is a finite limit on how large they
99 * can
100 * make the attributes.
101 *
102 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
103 * bounds
104 * checking for every single attribute as we construct an UPDATE.
105 */
106 subgrp->work =
107 stream_new(BGP_MAX_PACKET_SIZE + BGP_MAX_PACKET_SIZE_OVERFLOW);
108 subgrp->scratch = stream_new(BGP_MAX_PACKET_SIZE);
3f9c7369
DS
109}
110
d62a17ae 111static void sync_delete(struct update_subgroup *subgrp)
3f9c7369 112{
0a22ddfb 113 XFREE(MTYPE_BGP_SYNCHRONISE, subgrp->sync);
d62a17ae 114 if (subgrp->hash)
115 hash_free(subgrp->hash);
116 subgrp->hash = NULL;
117 if (subgrp->work)
118 stream_free(subgrp->work);
119 subgrp->work = NULL;
120 if (subgrp->scratch)
121 stream_free(subgrp->scratch);
122 subgrp->scratch = NULL;
3f9c7369
DS
123}
124
125/**
126 * conf_copy
127 *
128 * copy only those fields that are relevant to update group match
129 */
d62a17ae 130static void conf_copy(struct peer *dst, struct peer *src, afi_t afi,
131 safi_t safi)
3f9c7369 132{
d62a17ae 133 struct bgp_filter *srcfilter;
134 struct bgp_filter *dstfilter;
135
136 srcfilter = &src->filter[afi][safi];
137 dstfilter = &dst->filter[afi][safi];
138
139 dst->bgp = src->bgp;
140 dst->sort = src->sort;
141 dst->as = src->as;
142 dst->v_routeadv = src->v_routeadv;
143 dst->flags = src->flags;
144 dst->af_flags[afi][safi] = src->af_flags[afi][safi];
fde246e8 145 dst->pmax_out[afi][safi] = src->pmax_out[afi][safi];
0a22ddfb 146 XFREE(MTYPE_BGP_PEER_HOST, dst->host);
d62a17ae 147
148 dst->host = XSTRDUP(MTYPE_BGP_PEER_HOST, src->host);
149 dst->cap = src->cap;
150 dst->af_cap[afi][safi] = src->af_cap[afi][safi];
151 dst->afc_nego[afi][safi] = src->afc_nego[afi][safi];
152 dst->orf_plist[afi][safi] = src->orf_plist[afi][safi];
dcc68b5e 153 dst->addpath_type[afi][safi] = src->addpath_type[afi][safi];
d62a17ae 154 dst->local_as = src->local_as;
155 dst->change_local_as = src->change_local_as;
156 dst->shared_network = src->shared_network;
157 memcpy(&(dst->nexthop), &(src->nexthop), sizeof(struct bgp_nexthop));
158
159 dst->group = src->group;
160
161 if (src->default_rmap[afi][safi].name) {
162 dst->default_rmap[afi][safi].name =
163 XSTRDUP(MTYPE_ROUTE_MAP_NAME,
164 src->default_rmap[afi][safi].name);
165 dst->default_rmap[afi][safi].map =
166 src->default_rmap[afi][safi].map;
167 }
168
169 if (DISTRIBUTE_OUT_NAME(srcfilter)) {
170 DISTRIBUTE_OUT_NAME(dstfilter) = XSTRDUP(
171 MTYPE_BGP_FILTER_NAME, DISTRIBUTE_OUT_NAME(srcfilter));
172 DISTRIBUTE_OUT(dstfilter) = DISTRIBUTE_OUT(srcfilter);
173 }
174
175 if (PREFIX_LIST_OUT_NAME(srcfilter)) {
176 PREFIX_LIST_OUT_NAME(dstfilter) = XSTRDUP(
177 MTYPE_BGP_FILTER_NAME, PREFIX_LIST_OUT_NAME(srcfilter));
178 PREFIX_LIST_OUT(dstfilter) = PREFIX_LIST_OUT(srcfilter);
179 }
180
181 if (FILTER_LIST_OUT_NAME(srcfilter)) {
182 FILTER_LIST_OUT_NAME(dstfilter) = XSTRDUP(
183 MTYPE_BGP_FILTER_NAME, FILTER_LIST_OUT_NAME(srcfilter));
184 FILTER_LIST_OUT(dstfilter) = FILTER_LIST_OUT(srcfilter);
185 }
186
187 if (ROUTE_MAP_OUT_NAME(srcfilter)) {
188 ROUTE_MAP_OUT_NAME(dstfilter) = XSTRDUP(
189 MTYPE_BGP_FILTER_NAME, ROUTE_MAP_OUT_NAME(srcfilter));
190 ROUTE_MAP_OUT(dstfilter) = ROUTE_MAP_OUT(srcfilter);
191 }
192
193 if (UNSUPPRESS_MAP_NAME(srcfilter)) {
194 UNSUPPRESS_MAP_NAME(dstfilter) = XSTRDUP(
195 MTYPE_BGP_FILTER_NAME, UNSUPPRESS_MAP_NAME(srcfilter));
196 UNSUPPRESS_MAP(dstfilter) = UNSUPPRESS_MAP(srcfilter);
197 }
3f9c7369
DS
198}
199
200/**
6e919709 201 * since we did a bunch of XSTRDUP's in conf_copy, time to free them up
3f9c7369 202 */
d62a17ae 203static void conf_release(struct peer *src, afi_t afi, safi_t safi)
3f9c7369 204{
d62a17ae 205 struct bgp_filter *srcfilter;
3f9c7369 206
d62a17ae 207 srcfilter = &src->filter[afi][safi];
3f9c7369 208
0a22ddfb 209 XFREE(MTYPE_ROUTE_MAP_NAME, src->default_rmap[afi][safi].name);
3f9c7369 210
0a22ddfb 211 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->dlist[FILTER_OUT].name);
3f9c7369 212
0a22ddfb 213 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->plist[FILTER_OUT].name);
3f9c7369 214
0a22ddfb 215 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->aslist[FILTER_OUT].name);
3f9c7369 216
0a22ddfb 217 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->map[RMAP_OUT].name);
3f9c7369 218
0a22ddfb 219 XFREE(MTYPE_BGP_FILTER_NAME, srcfilter->usmap.name);
495f0b13 220
0a22ddfb 221 XFREE(MTYPE_BGP_PEER_HOST, src->host);
3f9c7369
DS
222}
223
d62a17ae 224static void peer2_updgrp_copy(struct update_group *updgrp, struct peer_af *paf)
3f9c7369 225{
d62a17ae 226 struct peer *src;
227 struct peer *dst;
3f9c7369 228
d62a17ae 229 if (!updgrp || !paf)
230 return;
3f9c7369 231
d62a17ae 232 src = paf->peer;
233 dst = updgrp->conf;
234 if (!src || !dst)
235 return;
3f9c7369 236
d62a17ae 237 updgrp->afi = paf->afi;
238 updgrp->safi = paf->safi;
239 updgrp->afid = paf->afid;
240 updgrp->bgp = src->bgp;
3f9c7369 241
d62a17ae 242 conf_copy(dst, src, paf->afi, paf->safi);
3f9c7369
DS
243}
244
245/**
246 * auxiliary functions to maintain the hash table.
247 * - updgrp_hash_alloc - to create a new entry, passed to hash_get
248 * - updgrp_hash_key_make - makes the key for update group search
249 * - updgrp_hash_cmp - compare two update groups.
250 */
d62a17ae 251static void *updgrp_hash_alloc(void *p)
3f9c7369 252{
d62a17ae 253 struct update_group *updgrp;
254 const struct update_group *in;
255
256 in = (const struct update_group *)p;
257 updgrp = XCALLOC(MTYPE_BGP_UPDGRP, sizeof(struct update_group));
258 memcpy(updgrp, in, sizeof(struct update_group));
259 updgrp->conf = XCALLOC(MTYPE_BGP_PEER, sizeof(struct peer));
260 conf_copy(updgrp->conf, in->conf, in->afi, in->safi);
261 return updgrp;
3f9c7369
DS
262}
263
264/**
265 * The hash value for a peer is computed from the following variables:
266 * v = f(
267 * 1. IBGP (1) or EBGP (2)
268 * 2. FLAGS based on configuration:
269 * LOCAL_AS_NO_PREPEND
270 * LOCAL_AS_REPLACE_AS
271 * 3. AF_FLAGS based on configuration:
272 * Refer to definition in bgp_updgrp.h
273 * 4. (AF-independent) Capability flags:
274 * AS4_RCV capability
275 * 5. (AF-dependent) Capability flags:
276 * ORF_PREFIX_SM_RCV (peer can send prefix ORF)
277 * 6. MRAI
278 * 7. peer-group name
279 * 8. Outbound route-map name (neighbor route-map <> out)
280 * 9. Outbound distribute-list name (neighbor distribute-list <> out)
281 * 10. Outbound prefix-list name (neighbor prefix-list <> out)
282 * 11. Outbound as-list name (neighbor filter-list <> out)
283 * 12. Unsuppress map name (neighbor unsuppress-map <>)
284 * 13. default rmap name (neighbor default-originate route-map <>)
285 * 14. encoding both global and link-local nexthop?
286 * 15. If peer is configured to be a lonesoul, peer ip address
287 * 16. Local-as should match, if configured.
288 * )
289 */
d8b87afe 290static unsigned int updgrp_hash_key_make(const void *p)
3f9c7369 291{
d62a17ae 292 const struct update_group *updgrp;
293 const struct peer *peer;
294 const struct bgp_filter *filter;
295 uint32_t flags;
296 uint32_t key;
297 afi_t afi;
298 safi_t safi;
3f9c7369
DS
299
300#define SEED1 999331
301#define SEED2 2147483647
302
d62a17ae 303 updgrp = p;
304 peer = updgrp->conf;
305 afi = updgrp->afi;
306 safi = updgrp->safi;
307 flags = peer->af_flags[afi][safi];
308 filter = &peer->filter[afi][safi];
309
310 key = 0;
311
312 key = jhash_1word(peer->sort, key); /* EBGP or IBGP */
313 key = jhash_1word((peer->flags & PEER_UPDGRP_FLAGS), key);
314 key = jhash_1word((flags & PEER_UPDGRP_AF_FLAGS), key);
dcc68b5e 315 key = jhash_1word((uint32_t)peer->addpath_type[afi][safi], key);
d62a17ae 316 key = jhash_1word((peer->cap & PEER_UPDGRP_CAP_FLAGS), key);
317 key = jhash_1word((peer->af_cap[afi][safi] & PEER_UPDGRP_AF_CAP_FLAGS),
318 key);
319 key = jhash_1word(peer->v_routeadv, key);
320 key = jhash_1word(peer->change_local_as, key);
321
322 if (peer->group)
323 key = jhash_1word(jhash(peer->group->name,
324 strlen(peer->group->name), SEED1),
325 key);
326
327 if (filter->map[RMAP_OUT].name)
328 key = jhash_1word(jhash(filter->map[RMAP_OUT].name,
329 strlen(filter->map[RMAP_OUT].name),
330 SEED1),
331 key);
332
333 if (filter->dlist[FILTER_OUT].name)
334 key = jhash_1word(jhash(filter->dlist[FILTER_OUT].name,
335 strlen(filter->dlist[FILTER_OUT].name),
336 SEED1),
337 key);
338
339 if (filter->plist[FILTER_OUT].name)
340 key = jhash_1word(jhash(filter->plist[FILTER_OUT].name,
341 strlen(filter->plist[FILTER_OUT].name),
342 SEED1),
343 key);
344
345 if (filter->aslist[FILTER_OUT].name)
346 key = jhash_1word(jhash(filter->aslist[FILTER_OUT].name,
347 strlen(filter->aslist[FILTER_OUT].name),
348 SEED1),
349 key);
350
351 if (filter->usmap.name)
352 key = jhash_1word(jhash(filter->usmap.name,
353 strlen(filter->usmap.name), SEED1),
354 key);
355
356 if (peer->default_rmap[afi][safi].name)
357 key = jhash_1word(
358 jhash(peer->default_rmap[afi][safi].name,
359 strlen(peer->default_rmap[afi][safi].name),
360 SEED1),
361 key);
362
363 /* If peer is on a shared network and is exchanging IPv6 prefixes,
364 * it needs to include link-local address. That's different from
365 * non-shared-network peers (nexthop encoded with 32 bytes vs 16
366 * bytes). We create different update groups to take care of that.
367 */
368 key = jhash_1word(
369 (peer->shared_network && peer_afi_active_nego(peer, AFI_IP6)),
370 key);
371
372 /*
373 * There are certain peers that must get their own update-group:
374 * - lonesoul peers
375 * - peers that negotiated ORF
376 */
377 if (CHECK_FLAG(peer->flags, PEER_FLAG_LONESOUL)
378 || CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
379 || CHECK_FLAG(peer->af_cap[afi][safi],
380 PEER_CAP_ORF_PREFIX_SM_OLD_RCV))
381 key = jhash_1word(jhash(peer->host, strlen(peer->host), SEED2),
382 key);
383
384 return key;
3f9c7369
DS
385}
386
74df8d6d 387static bool updgrp_hash_cmp(const void *p1, const void *p2)
3f9c7369 388{
d62a17ae 389 const struct update_group *grp1;
390 const struct update_group *grp2;
391 const struct peer *pe1;
392 const struct peer *pe2;
393 uint32_t flags1;
394 uint32_t flags2;
395 const struct bgp_filter *fl1;
396 const struct bgp_filter *fl2;
397 afi_t afi;
398 safi_t safi;
399
400 if (!p1 || !p2)
74df8d6d 401 return false;
d62a17ae 402
403 grp1 = p1;
404 grp2 = p2;
405 pe1 = grp1->conf;
406 pe2 = grp2->conf;
407 afi = grp1->afi;
408 safi = grp1->safi;
409 flags1 = pe1->af_flags[afi][safi];
410 flags2 = pe2->af_flags[afi][safi];
411 fl1 = &pe1->filter[afi][safi];
412 fl2 = &pe2->filter[afi][safi];
413
414 /* put EBGP and IBGP peers in different update groups */
415 if (pe1->sort != pe2->sort)
74df8d6d 416 return false;
d62a17ae 417
418 /* check peer flags */
419 if ((pe1->flags & PEER_UPDGRP_FLAGS)
420 != (pe2->flags & PEER_UPDGRP_FLAGS))
74df8d6d 421 return false;
d62a17ae 422
423 /* If there is 'local-as' configured, it should match. */
424 if (pe1->change_local_as != pe2->change_local_as)
74df8d6d 425 return false;
d62a17ae 426
427 /* flags like route reflector client */
428 if ((flags1 & PEER_UPDGRP_AF_FLAGS) != (flags2 & PEER_UPDGRP_AF_FLAGS))
74df8d6d 429 return false;
d62a17ae 430
dcc68b5e 431 if (pe1->addpath_type[afi][safi] != pe2->addpath_type[afi][safi])
b08047f8 432 return false;
dcc68b5e 433
d62a17ae 434 if ((pe1->cap & PEER_UPDGRP_CAP_FLAGS)
435 != (pe2->cap & PEER_UPDGRP_CAP_FLAGS))
74df8d6d 436 return false;
d62a17ae 437
438 if ((pe1->af_cap[afi][safi] & PEER_UPDGRP_AF_CAP_FLAGS)
439 != (pe2->af_cap[afi][safi] & PEER_UPDGRP_AF_CAP_FLAGS))
74df8d6d 440 return false;
d62a17ae 441
442 if (pe1->v_routeadv != pe2->v_routeadv)
74df8d6d 443 return false;
d62a17ae 444
445 if (pe1->group != pe2->group)
74df8d6d 446 return false;
d62a17ae 447
448 /* route-map names should be the same */
449 if ((fl1->map[RMAP_OUT].name && !fl2->map[RMAP_OUT].name)
450 || (!fl1->map[RMAP_OUT].name && fl2->map[RMAP_OUT].name)
451 || (fl1->map[RMAP_OUT].name && fl2->map[RMAP_OUT].name
452 && strcmp(fl1->map[RMAP_OUT].name, fl2->map[RMAP_OUT].name)))
74df8d6d 453 return false;
d62a17ae 454
455 if ((fl1->dlist[FILTER_OUT].name && !fl2->dlist[FILTER_OUT].name)
456 || (!fl1->dlist[FILTER_OUT].name && fl2->dlist[FILTER_OUT].name)
457 || (fl1->dlist[FILTER_OUT].name && fl2->dlist[FILTER_OUT].name
458 && strcmp(fl1->dlist[FILTER_OUT].name,
459 fl2->dlist[FILTER_OUT].name)))
74df8d6d 460 return false;
d62a17ae 461
462 if ((fl1->plist[FILTER_OUT].name && !fl2->plist[FILTER_OUT].name)
463 || (!fl1->plist[FILTER_OUT].name && fl2->plist[FILTER_OUT].name)
464 || (fl1->plist[FILTER_OUT].name && fl2->plist[FILTER_OUT].name
465 && strcmp(fl1->plist[FILTER_OUT].name,
466 fl2->plist[FILTER_OUT].name)))
74df8d6d 467 return false;
d62a17ae 468
469 if ((fl1->aslist[FILTER_OUT].name && !fl2->aslist[FILTER_OUT].name)
470 || (!fl1->aslist[FILTER_OUT].name && fl2->aslist[FILTER_OUT].name)
471 || (fl1->aslist[FILTER_OUT].name && fl2->aslist[FILTER_OUT].name
472 && strcmp(fl1->aslist[FILTER_OUT].name,
473 fl2->aslist[FILTER_OUT].name)))
74df8d6d 474 return false;
d62a17ae 475
476 if ((fl1->usmap.name && !fl2->usmap.name)
477 || (!fl1->usmap.name && fl2->usmap.name)
478 || (fl1->usmap.name && fl2->usmap.name
479 && strcmp(fl1->usmap.name, fl2->usmap.name)))
74df8d6d 480 return false;
d62a17ae 481
482 if ((pe1->default_rmap[afi][safi].name
483 && !pe2->default_rmap[afi][safi].name)
484 || (!pe1->default_rmap[afi][safi].name
485 && pe2->default_rmap[afi][safi].name)
486 || (pe1->default_rmap[afi][safi].name
487 && pe2->default_rmap[afi][safi].name
488 && strcmp(pe1->default_rmap[afi][safi].name,
489 pe2->default_rmap[afi][safi].name)))
74df8d6d 490 return false;
d62a17ae 491
492 if ((afi == AFI_IP6) && (pe1->shared_network != pe2->shared_network))
74df8d6d 493 return false;
d62a17ae 494
495 if ((CHECK_FLAG(pe1->flags, PEER_FLAG_LONESOUL)
496 || CHECK_FLAG(pe1->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_RCV)
497 || CHECK_FLAG(pe1->af_cap[afi][safi],
498 PEER_CAP_ORF_PREFIX_SM_OLD_RCV))
499 && !sockunion_same(&pe1->su, &pe2->su))
74df8d6d 500 return false;
d62a17ae 501
74df8d6d 502 return true;
3f9c7369
DS
503}
504
d62a17ae 505static void peer_lonesoul_or_not(struct peer *peer, int set)
3f9c7369 506{
d62a17ae 507 /* no change in status? */
508 if (set == (CHECK_FLAG(peer->flags, PEER_FLAG_LONESOUL) > 0))
509 return;
3f9c7369 510
d62a17ae 511 if (set)
512 SET_FLAG(peer->flags, PEER_FLAG_LONESOUL);
513 else
514 UNSET_FLAG(peer->flags, PEER_FLAG_LONESOUL);
3f9c7369 515
d62a17ae 516 update_group_adjust_peer_afs(peer);
3f9c7369
DS
517}
518
519/*
520 * subgroup_total_packets_enqueued
521 *
522 * Returns the total number of packets enqueued to a subgroup.
523 */
524static unsigned int
d62a17ae 525subgroup_total_packets_enqueued(struct update_subgroup *subgrp)
3f9c7369 526{
d62a17ae 527 struct bpacket *pkt;
3f9c7369 528
d62a17ae 529 pkt = bpacket_queue_last(SUBGRP_PKTQ(subgrp));
3f9c7369 530
d62a17ae 531 return pkt->ver - 1;
3f9c7369
DS
532}
533
d62a17ae 534static int update_group_show_walkcb(struct update_group *updgrp, void *arg)
3f9c7369 535{
d62a17ae 536 struct updwalk_context *ctx = arg;
537 struct vty *vty;
538 struct update_subgroup *subgrp;
539 struct peer_af *paf;
540 struct bgp_filter *filter;
541 int match = 0;
542
543 if (!ctx)
544 return CMD_SUCCESS;
545
546 if (ctx->subgrp_id) {
a2addae8 547 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 548 if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id))
549 continue;
550 else {
551 match = 1;
552 break;
553 }
554 }
555 } else {
556 match = 1;
557 }
558
559 if (!match) {
560 /* Since this routine is invoked from a walk, we cannot signal
561 * any */
562 /* error here, can only return. */
563 return CMD_SUCCESS;
564 }
565
566 vty = ctx->vty;
567
568 vty_out(vty, "Update-group %" PRIu64 ":\n", updgrp->id);
569 vty_out(vty, " Created: %s", timestamp_string(updgrp->uptime));
570 filter = &updgrp->conf->filter[updgrp->afi][updgrp->safi];
571 if (filter->map[RMAP_OUT].name)
7dba67ff 572 vty_out(vty, " Outgoing route map: %s\n",
d62a17ae 573 filter->map[RMAP_OUT].name);
574 vty_out(vty, " MRAI value (seconds): %d\n", updgrp->conf->v_routeadv);
575 if (updgrp->conf->change_local_as)
576 vty_out(vty, " Local AS %u%s%s\n",
577 updgrp->conf->change_local_as,
578 CHECK_FLAG(updgrp->conf->flags,
579 PEER_FLAG_LOCAL_AS_NO_PREPEND)
580 ? " no-prepend"
581 : "",
582 CHECK_FLAG(updgrp->conf->flags,
583 PEER_FLAG_LOCAL_AS_REPLACE_AS)
584 ? " replace-as"
585 : "");
586
a2addae8 587 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 588 if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id))
589 continue;
590 vty_out(vty, "\n");
591 vty_out(vty, " Update-subgroup %" PRIu64 ":\n", subgrp->id);
592 vty_out(vty, " Created: %s",
593 timestamp_string(subgrp->uptime));
594
595 if (subgrp->split_from.update_group_id
596 || subgrp->split_from.subgroup_id) {
597 vty_out(vty, " Split from group id: %" PRIu64 "\n",
598 subgrp->split_from.update_group_id);
599 vty_out(vty,
600 " Split from subgroup id: %" PRIu64 "\n",
601 subgrp->split_from.subgroup_id);
602 }
603
604 vty_out(vty, " Join events: %u\n", subgrp->join_events);
605 vty_out(vty, " Prune events: %u\n", subgrp->prune_events);
606 vty_out(vty, " Merge events: %u\n", subgrp->merge_events);
607 vty_out(vty, " Split events: %u\n", subgrp->split_events);
608 vty_out(vty, " Update group switch events: %u\n",
609 subgrp->updgrp_switch_events);
610 vty_out(vty, " Peer refreshes combined: %u\n",
611 subgrp->peer_refreshes_combined);
612 vty_out(vty, " Merge checks triggered: %u\n",
613 subgrp->merge_checks_triggered);
5b18ef82
DS
614 vty_out(vty, " Coalesce Time: %u%s\n",
615 (UPDGRP_INST(subgrp->update_group))->coalesce_time,
616 subgrp->t_coalesce ? "(Running)" : "");
d62a17ae 617 vty_out(vty, " Version: %" PRIu64 "\n", subgrp->version);
618 vty_out(vty, " Packet queue length: %d\n",
619 bpacket_queue_length(SUBGRP_PKTQ(subgrp)));
620 vty_out(vty, " Total packets enqueued: %u\n",
621 subgroup_total_packets_enqueued(subgrp));
622 vty_out(vty, " Packet queue high watermark: %d\n",
623 bpacket_queue_hwm_length(SUBGRP_PKTQ(subgrp)));
624 vty_out(vty, " Adj-out list count: %u\n", subgrp->adj_count);
625 vty_out(vty, " Advertise list: %s\n",
626 advertise_list_is_empty(subgrp) ? "empty"
627 : "not empty");
628 vty_out(vty, " Flags: %s\n",
629 CHECK_FLAG(subgrp->flags, SUBGRP_FLAG_NEEDS_REFRESH)
630 ? "R"
631 : "");
632 if (subgrp->peer_count > 0) {
633 vty_out(vty, " Peers:\n");
a2addae8
RW
634 SUBGRP_FOREACH_PEER (subgrp, paf)
635 vty_out(vty, " - %s\n", paf->peer->host);
d62a17ae 636 }
8fe8a7f6 637 }
d62a17ae 638 return UPDWALK_CONTINUE;
3f9c7369
DS
639}
640
641/*
642 * Helper function to show the packet queue for each subgroup of update group.
643 * Will be constrained to a particular subgroup id if id !=0
644 */
d62a17ae 645static int updgrp_show_packet_queue_walkcb(struct update_group *updgrp,
646 void *arg)
3f9c7369 647{
d62a17ae 648 struct updwalk_context *ctx = arg;
649 struct update_subgroup *subgrp;
650 struct vty *vty;
651
652 vty = ctx->vty;
a2addae8 653 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 654 if (ctx->subgrp_id && (ctx->subgrp_id != subgrp->id))
655 continue;
656 vty_out(vty, "update group %" PRIu64 ", subgroup %" PRIu64 "\n",
657 updgrp->id, subgrp->id);
658 bpacket_queue_show_vty(SUBGRP_PKTQ(subgrp), vty);
659 }
660 return UPDWALK_CONTINUE;
3f9c7369
DS
661}
662
663/*
664 * Show the packet queue for each subgroup of update group. Will be
665 * constrained to a particular subgroup id if id !=0
666 */
d62a17ae 667void update_group_show_packet_queue(struct bgp *bgp, afi_t afi, safi_t safi,
668 struct vty *vty, uint64_t id)
3f9c7369 669{
d62a17ae 670 struct updwalk_context ctx;
671
672 memset(&ctx, 0, sizeof(ctx));
673 ctx.vty = vty;
674 ctx.subgrp_id = id;
675 ctx.flags = 0;
676 update_group_af_walk(bgp, afi, safi, updgrp_show_packet_queue_walkcb,
677 &ctx);
3f9c7369
DS
678}
679
d62a17ae 680static struct update_group *update_group_find(struct peer_af *paf)
3f9c7369 681{
d62a17ae 682 struct update_group *updgrp;
683 struct update_group tmp;
684 struct peer tmp_conf;
3f9c7369 685
d62a17ae 686 if (!peer_established(PAF_PEER(paf)))
687 return NULL;
3f9c7369 688
d62a17ae 689 memset(&tmp, 0, sizeof(tmp));
690 memset(&tmp_conf, 0, sizeof(tmp_conf));
691 tmp.conf = &tmp_conf;
692 peer2_updgrp_copy(&tmp, paf);
3f9c7369 693
d62a17ae 694 updgrp = hash_lookup(paf->peer->bgp->update_groups[paf->afid], &tmp);
695 conf_release(&tmp_conf, paf->afi, paf->safi);
696 return updgrp;
3f9c7369
DS
697}
698
d62a17ae 699static struct update_group *update_group_create(struct peer_af *paf)
3f9c7369 700{
d62a17ae 701 struct update_group *updgrp;
702 struct update_group tmp;
703 struct peer tmp_conf;
3f9c7369 704
d62a17ae 705 memset(&tmp, 0, sizeof(tmp));
706 memset(&tmp_conf, 0, sizeof(tmp_conf));
707 tmp.conf = &tmp_conf;
708 peer2_updgrp_copy(&tmp, paf);
3f9c7369 709
d62a17ae 710 updgrp = hash_get(paf->peer->bgp->update_groups[paf->afid], &tmp,
711 updgrp_hash_alloc);
712 if (!updgrp)
713 return NULL;
714 update_group_checkin(updgrp);
3f9c7369 715
d62a17ae 716 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
717 zlog_debug("create update group %" PRIu64, updgrp->id);
3f9c7369 718
d62a17ae 719 UPDGRP_GLOBAL_STAT(updgrp, updgrps_created) += 1;
3f9c7369 720
d62a17ae 721 conf_release(&tmp_conf, paf->afi, paf->safi);
722 return updgrp;
3f9c7369
DS
723}
724
d62a17ae 725static void update_group_delete(struct update_group *updgrp)
3f9c7369 726{
d62a17ae 727 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
728 zlog_debug("delete update group %" PRIu64, updgrp->id);
3f9c7369 729
d62a17ae 730 UPDGRP_GLOBAL_STAT(updgrp, updgrps_deleted) += 1;
3f9c7369 731
d62a17ae 732 hash_release(updgrp->bgp->update_groups[updgrp->afid], updgrp);
733 conf_release(updgrp->conf, updgrp->afi, updgrp->safi);
3d68677e 734
0a22ddfb 735 XFREE(MTYPE_BGP_PEER_HOST, updgrp->conf->host);
6e919709 736
0a22ddfb 737 XFREE(MTYPE_BGP_PEER_IFNAME, updgrp->conf->ifname);
6e919709 738
d62a17ae 739 XFREE(MTYPE_BGP_PEER, updgrp->conf);
740 XFREE(MTYPE_BGP_UPDGRP, updgrp);
3f9c7369
DS
741}
742
d62a17ae 743static void update_group_add_subgroup(struct update_group *updgrp,
744 struct update_subgroup *subgrp)
3f9c7369 745{
d62a17ae 746 if (!updgrp || !subgrp)
747 return;
3f9c7369 748
d62a17ae 749 LIST_INSERT_HEAD(&(updgrp->subgrps), subgrp, updgrp_train);
750 subgrp->update_group = updgrp;
3f9c7369
DS
751}
752
d62a17ae 753static void update_group_remove_subgroup(struct update_group *updgrp,
754 struct update_subgroup *subgrp)
3f9c7369 755{
d62a17ae 756 if (!updgrp || !subgrp)
757 return;
3f9c7369 758
d62a17ae 759 LIST_REMOVE(subgrp, updgrp_train);
760 subgrp->update_group = NULL;
761 if (LIST_EMPTY(&(updgrp->subgrps)))
762 update_group_delete(updgrp);
3f9c7369
DS
763}
764
765static struct update_subgroup *
d62a17ae 766update_subgroup_create(struct update_group *updgrp)
3f9c7369 767{
d62a17ae 768 struct update_subgroup *subgrp;
3f9c7369 769
d62a17ae 770 subgrp = XCALLOC(MTYPE_BGP_UPD_SUBGRP, sizeof(struct update_subgroup));
771 update_subgroup_checkin(subgrp, updgrp);
772 subgrp->v_coalesce = (UPDGRP_INST(updgrp))->coalesce_time;
773 sync_init(subgrp);
774 bpacket_queue_init(SUBGRP_PKTQ(subgrp));
775 bpacket_queue_add(SUBGRP_PKTQ(subgrp), NULL, NULL);
776 TAILQ_INIT(&(subgrp->adjq));
777 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
778 zlog_debug("create subgroup u%" PRIu64 ":s%" PRIu64, updgrp->id,
779 subgrp->id);
3f9c7369 780
d62a17ae 781 update_group_add_subgroup(updgrp, subgrp);
3f9c7369 782
d62a17ae 783 UPDGRP_INCR_STAT(updgrp, subgrps_created);
3f9c7369 784
d62a17ae 785 return subgrp;
3f9c7369
DS
786}
787
d62a17ae 788static void update_subgroup_delete(struct update_subgroup *subgrp)
3f9c7369 789{
d62a17ae 790 if (!subgrp)
791 return;
3f9c7369 792
d62a17ae 793 if (subgrp->update_group)
794 UPDGRP_INCR_STAT(subgrp->update_group, subgrps_deleted);
3f9c7369 795
d62a17ae 796 if (subgrp->t_merge_check)
797 THREAD_OFF(subgrp->t_merge_check);
3f9c7369 798
d62a17ae 799 if (subgrp->t_coalesce)
800 THREAD_TIMER_OFF(subgrp->t_coalesce);
3f9c7369 801
d62a17ae 802 bpacket_queue_cleanup(SUBGRP_PKTQ(subgrp));
803 subgroup_clear_table(subgrp);
3f9c7369 804
d62a17ae 805 if (subgrp->t_coalesce)
806 THREAD_TIMER_OFF(subgrp->t_coalesce);
807 sync_delete(subgrp);
3f9c7369 808
4f9a63ad 809 if (BGP_DEBUG(update_groups, UPDATE_GROUPS) && subgrp->update_group)
d62a17ae 810 zlog_debug("delete subgroup u%" PRIu64 ":s%" PRIu64,
811 subgrp->update_group->id, subgrp->id);
3f9c7369 812
d62a17ae 813 update_group_remove_subgroup(subgrp->update_group, subgrp);
3f9c7369 814
d62a17ae 815 XFREE(MTYPE_BGP_UPD_SUBGRP, subgrp);
3f9c7369
DS
816}
817
d62a17ae 818void update_subgroup_inherit_info(struct update_subgroup *to,
819 struct update_subgroup *from)
3f9c7369 820{
d62a17ae 821 if (!to || !from)
822 return;
3f9c7369 823
d62a17ae 824 to->sflags = from->sflags;
3f9c7369
DS
825}
826
827/*
828 * update_subgroup_check_delete
829 *
830 * Delete a subgroup if it is ready to be deleted.
831 *
2951a7a4 832 * Returns true if the subgroup was deleted.
3f9c7369 833 */
3dc339cd 834static bool update_subgroup_check_delete(struct update_subgroup *subgrp)
3f9c7369 835{
d62a17ae 836 if (!subgrp)
3dc339cd 837 return false;
3f9c7369 838
d62a17ae 839 if (!LIST_EMPTY(&(subgrp->peers)))
3dc339cd 840 return false;
3f9c7369 841
d62a17ae 842 update_subgroup_delete(subgrp);
3f9c7369 843
3dc339cd 844 return true;
3f9c7369
DS
845}
846
847/*
848 * update_subgroup_add_peer
849 *
850 * @param send_enqueued_packets If true all currently enqueued packets will
851 * also be sent to the peer.
852 */
d62a17ae 853static void update_subgroup_add_peer(struct update_subgroup *subgrp,
854 struct peer_af *paf,
855 int send_enqueued_pkts)
3f9c7369 856{
d62a17ae 857 struct bpacket *pkt;
3f9c7369 858
d62a17ae 859 if (!subgrp || !paf)
860 return;
3f9c7369 861
d62a17ae 862 LIST_INSERT_HEAD(&(subgrp->peers), paf, subgrp_train);
863 paf->subgroup = subgrp;
864 subgrp->peer_count++;
3f9c7369 865
d62a17ae 866 if (bgp_debug_peer_updout_enabled(paf->peer->host)) {
867 UPDGRP_PEER_DBG_EN(subgrp->update_group);
868 }
3f9c7369 869
d62a17ae 870 SUBGRP_INCR_STAT(subgrp, join_events);
3f9c7369 871
d62a17ae 872 if (send_enqueued_pkts) {
873 pkt = bpacket_queue_first(SUBGRP_PKTQ(subgrp));
874 } else {
3f9c7369 875
d62a17ae 876 /*
877 * Hang the peer off of the last, placeholder, packet in the
878 * queue. This means it won't see any of the packets that are
879 * currently the queue.
880 */
881 pkt = bpacket_queue_last(SUBGRP_PKTQ(subgrp));
882 assert(pkt->buffer == NULL);
883 }
3f9c7369 884
d62a17ae 885 bpacket_add_peer(pkt, paf);
3f9c7369 886
d62a17ae 887 bpacket_queue_sanity_check(SUBGRP_PKTQ(subgrp));
7bfdba54
S
888 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
889 zlog_debug("peer %s added to subgroup s%" PRIu64,
890 paf->peer->host, subgrp->id);
3f9c7369
DS
891}
892
893/*
894 * update_subgroup_remove_peer_internal
895 *
896 * Internal function that removes a peer from a subgroup, but does not
897 * delete the subgroup. A call to this function must almost always be
898 * followed by a call to update_subgroup_check_delete().
899 *
900 * @see update_subgroup_remove_peer
901 */
d62a17ae 902static void update_subgroup_remove_peer_internal(struct update_subgroup *subgrp,
903 struct peer_af *paf)
3f9c7369 904{
d3e51db0 905 assert(subgrp && paf && subgrp->update_group);
3f9c7369 906
d62a17ae 907 if (bgp_debug_peer_updout_enabled(paf->peer->host)) {
908 UPDGRP_PEER_DBG_DIS(subgrp->update_group);
909 }
3f9c7369 910
d62a17ae 911 bpacket_queue_remove_peer(paf);
912 LIST_REMOVE(paf, subgrp_train);
913 paf->subgroup = NULL;
914 subgrp->peer_count--;
3f9c7369 915
7bfdba54
S
916 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
917 zlog_debug("peer %s deleted from subgroup s%"
4882d296 918 PRIu64 " peer cnt %d",
7bfdba54 919 paf->peer->host, subgrp->id, subgrp->peer_count);
d62a17ae 920 SUBGRP_INCR_STAT(subgrp, prune_events);
3f9c7369
DS
921}
922
923/*
924 * update_subgroup_remove_peer
925 */
d62a17ae 926void update_subgroup_remove_peer(struct update_subgroup *subgrp,
927 struct peer_af *paf)
3f9c7369 928{
d62a17ae 929 if (!subgrp || !paf)
930 return;
3f9c7369 931
d62a17ae 932 update_subgroup_remove_peer_internal(subgrp, paf);
3f9c7369 933
d62a17ae 934 if (update_subgroup_check_delete(subgrp))
935 return;
3f9c7369 936
d62a17ae 937 /*
938 * The deletion of the peer may have caused some packets to be
939 * deleted from the subgroup packet queue. Check if the subgroup can
940 * be merged now.
941 */
942 update_subgroup_check_merge(subgrp, "removed peer from subgroup");
3f9c7369
DS
943}
944
d62a17ae 945static struct update_subgroup *update_subgroup_find(struct update_group *updgrp,
946 struct peer_af *paf)
3f9c7369 947{
d62a17ae 948 struct update_subgroup *subgrp = NULL;
949 uint64_t version;
950
951 if (paf->subgroup) {
952 assert(0);
953 return NULL;
954 } else
955 version = 0;
956
957 if (!peer_established(PAF_PEER(paf)))
958 return NULL;
959
a2addae8 960 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 961 if (subgrp->version != version
962 || CHECK_FLAG(subgrp->sflags,
963 SUBGRP_STATUS_DEFAULT_ORIGINATE))
964 continue;
965
966 /*
967 * The version number is not meaningful on a subgroup that needs
968 * a refresh.
969 */
970 if (update_subgroup_needs_refresh(subgrp))
971 continue;
972
973 break;
974 }
975
976 return subgrp;
3f9c7369
DS
977}
978
979/*
980 * update_subgroup_ready_for_merge
981 *
2951a7a4 982 * Returns true if this subgroup is in a state that allows it to be
3f9c7369
DS
983 * merged into another subgroup.
984 */
3dc339cd 985static bool update_subgroup_ready_for_merge(struct update_subgroup *subgrp)
3f9c7369
DS
986{
987
d62a17ae 988 /*
989 * Not ready if there are any encoded packets waiting to be written
990 * out to peers.
991 */
992 if (!bpacket_queue_is_empty(SUBGRP_PKTQ(subgrp)))
3dc339cd 993 return false;
d62a17ae 994
995 /*
996 * Not ready if there enqueued updates waiting to be encoded.
997 */
998 if (!advertise_list_is_empty(subgrp))
3dc339cd 999 return false;
d62a17ae 1000
1001 /*
1002 * Don't attempt to merge a subgroup that needs a refresh. For one,
1003 * we can't determine if the adj_out of such a group matches that of
1004 * another group.
1005 */
1006 if (update_subgroup_needs_refresh(subgrp))
3dc339cd 1007 return false;
d62a17ae 1008
3dc339cd 1009 return true;
3f9c7369
DS
1010}
1011
1012/*
1013 * update_subgrp_can_merge_into
1014 *
2951a7a4 1015 * Returns true if the first subgroup can merge into the second
3f9c7369
DS
1016 * subgroup.
1017 */
d62a17ae 1018static int update_subgroup_can_merge_into(struct update_subgroup *subgrp,
1019 struct update_subgroup *target)
3f9c7369
DS
1020{
1021
d62a17ae 1022 if (subgrp == target)
1023 return 0;
3f9c7369 1024
d62a17ae 1025 /*
1026 * Both must have processed the BRIB to the same point in order to
1027 * be merged.
1028 */
1029 if (subgrp->version != target->version)
1030 return 0;
3f9c7369 1031
d62a17ae 1032 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)
1033 != CHECK_FLAG(target->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE))
1034 return 0;
f910ef58 1035
d62a17ae 1036 if (subgrp->adj_count != target->adj_count)
1037 return 0;
3f9c7369 1038
d62a17ae 1039 return update_subgroup_ready_for_merge(target);
3f9c7369
DS
1040}
1041
1042/*
1043 * update_subgroup_merge
1044 *
1045 * Merge the first subgroup into the second one.
1046 */
d62a17ae 1047static void update_subgroup_merge(struct update_subgroup *subgrp,
1048 struct update_subgroup *target,
1049 const char *reason)
3f9c7369 1050{
d62a17ae 1051 struct peer_af *paf;
1052 int result;
1053 int peer_count;
3f9c7369 1054
d62a17ae 1055 assert(subgrp->adj_count == target->adj_count);
3f9c7369 1056
d62a17ae 1057 peer_count = subgrp->peer_count;
3f9c7369 1058
d62a17ae 1059 while (1) {
1060 paf = LIST_FIRST(&subgrp->peers);
1061 if (!paf)
1062 break;
3f9c7369 1063
d62a17ae 1064 update_subgroup_remove_peer_internal(subgrp, paf);
3f9c7369 1065
d62a17ae 1066 /*
1067 * Add the peer to the target subgroup, while making sure that
1068 * any currently enqueued packets won't be sent to it. Enqueued
1069 * packets could, for example, result in an unnecessary withdraw
1070 * followed by an advertise.
1071 */
1072 update_subgroup_add_peer(target, paf, 0);
1073 }
3f9c7369 1074
d62a17ae 1075 SUBGRP_INCR_STAT(target, merge_events);
3f9c7369 1076
d62a17ae 1077 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
6cde4b45 1078 zlog_debug("u%" PRIu64 ":s%" PRIu64" (%d peers) merged into u%" PRIu64 ":s%" PRIu64", trigger: %s",
d62a17ae 1079 subgrp->update_group->id, subgrp->id, peer_count,
1080 target->update_group->id, target->id,
1081 reason ? reason : "unknown");
3f9c7369 1082
d62a17ae 1083 result = update_subgroup_check_delete(subgrp);
1084 assert(result);
3f9c7369
DS
1085}
1086
1087/*
1088 * update_subgroup_check_merge
1089 *
1090 * Merge this subgroup into another subgroup if possible.
1091 *
2951a7a4 1092 * Returns true if the subgroup has been merged. The subgroup pointer
3f9c7369
DS
1093 * should not be accessed in this case.
1094 */
3dc339cd
DA
1095bool update_subgroup_check_merge(struct update_subgroup *subgrp,
1096 const char *reason)
3f9c7369 1097{
d62a17ae 1098 struct update_subgroup *target;
3f9c7369 1099
d62a17ae 1100 if (!update_subgroup_ready_for_merge(subgrp))
3dc339cd 1101 return false;
3f9c7369 1102
d62a17ae 1103 /*
1104 * Look for a subgroup to merge into.
1105 */
a2addae8 1106 UPDGRP_FOREACH_SUBGRP (subgrp->update_group, target) {
d62a17ae 1107 if (update_subgroup_can_merge_into(subgrp, target))
1108 break;
1109 }
3f9c7369 1110
d62a17ae 1111 if (!target)
3dc339cd 1112 return false;
3f9c7369 1113
d62a17ae 1114 update_subgroup_merge(subgrp, target, reason);
3dc339cd 1115 return true;
3f9c7369
DS
1116}
1117
d62a17ae 1118/*
9d303b37
DL
1119* update_subgroup_merge_check_thread_cb
1120*/
d62a17ae 1121static int update_subgroup_merge_check_thread_cb(struct thread *thread)
3f9c7369 1122{
d62a17ae 1123 struct update_subgroup *subgrp;
3f9c7369 1124
d62a17ae 1125 subgrp = THREAD_ARG(thread);
3f9c7369 1126
d62a17ae 1127 subgrp->t_merge_check = NULL;
3f9c7369 1128
d62a17ae 1129 update_subgroup_check_merge(subgrp, "triggered merge check");
1130 return 0;
3f9c7369
DS
1131}
1132
1133/*
1134 * update_subgroup_trigger_merge_check
1135 *
1136 * Triggers a call to update_subgroup_check_merge() on a clean context.
1137 *
1138 * @param force If true, the merge check will be triggered even if the
1139 * subgroup doesn't currently look ready for a merge.
1140 *
2951a7a4 1141 * Returns true if a merge check will be performed shortly.
3f9c7369 1142 */
3dc339cd
DA
1143bool update_subgroup_trigger_merge_check(struct update_subgroup *subgrp,
1144 int force)
3f9c7369 1145{
d62a17ae 1146 if (subgrp->t_merge_check)
3dc339cd 1147 return true;
3f9c7369 1148
d62a17ae 1149 if (!force && !update_subgroup_ready_for_merge(subgrp))
3dc339cd 1150 return false;
3f9c7369 1151
d62a17ae 1152 subgrp->t_merge_check = NULL;
1153 thread_add_timer_msec(bm->master, update_subgroup_merge_check_thread_cb,
1154 subgrp, 0, &subgrp->t_merge_check);
3f9c7369 1155
d62a17ae 1156 SUBGRP_INCR_STAT(subgrp, merge_checks_triggered);
3f9c7369 1157
3dc339cd 1158 return true;
3f9c7369
DS
1159}
1160
1161/*
1162 * update_subgroup_copy_adj_out
1163 *
1164 * Helper function that clones the adj out (state about advertised
1165 * routes) from one subgroup to another. It assumes that the adj out
1166 * of the target subgroup is empty.
1167 */
d62a17ae 1168static void update_subgroup_copy_adj_out(struct update_subgroup *source,
1169 struct update_subgroup *dest)
3f9c7369 1170{
d62a17ae 1171 struct bgp_adj_out *aout, *aout_copy;
1172
a2addae8 1173 SUBGRP_FOREACH_ADJ (source, aout) {
d62a17ae 1174 /*
1175 * Copy the adj out.
1176 */
9bcb3eef
DS
1177 aout_copy = bgp_adj_out_alloc(dest, aout->dest,
1178 aout->addpath_tx_id);
d62a17ae 1179 aout_copy->attr =
7c87afac 1180 aout->attr ? bgp_attr_intern(aout->attr) : NULL;
d62a17ae 1181 }
0ab7b206
AD
1182
1183 dest->scount = source->scount;
3f9c7369
DS
1184}
1185
1186/*
1187 * update_subgroup_copy_packets
1188 *
1189 * Copy packets after and including the given packet to the subgroup
1190 * 'dest'.
1191 *
1192 * Returns the number of packets copied.
1193 */
d62a17ae 1194static int update_subgroup_copy_packets(struct update_subgroup *dest,
1195 struct bpacket *pkt)
3f9c7369 1196{
d62a17ae 1197 int count;
1198
1199 count = 0;
1200 while (pkt && pkt->buffer) {
1201 bpacket_queue_add(SUBGRP_PKTQ(dest), stream_dup(pkt->buffer),
1202 &pkt->arr);
1203 count++;
1204 pkt = bpacket_next(pkt);
1205 }
3f9c7369 1206
d62a17ae 1207 bpacket_queue_sanity_check(SUBGRP_PKTQ(dest));
3f9c7369 1208
d62a17ae 1209 return count;
3f9c7369
DS
1210}
1211
3dc339cd
DA
1212static bool updgrp_prefix_list_update(struct update_group *updgrp,
1213 const char *name)
3f9c7369 1214{
d62a17ae 1215 struct peer *peer;
1216 struct bgp_filter *filter;
1217
1218 peer = UPDGRP_PEER(updgrp);
1219 filter = &peer->filter[UPDGRP_AFI(updgrp)][UPDGRP_SAFI(updgrp)];
1220
1221 if (PREFIX_LIST_OUT_NAME(filter)
1222 && (strcmp(name, PREFIX_LIST_OUT_NAME(filter)) == 0)) {
1223 PREFIX_LIST_OUT(filter) = prefix_list_lookup(
1224 UPDGRP_AFI(updgrp), PREFIX_LIST_OUT_NAME(filter));
3dc339cd 1225 return true;
d62a17ae 1226 }
3dc339cd 1227 return false;
3f9c7369
DS
1228}
1229
3dc339cd
DA
1230static bool updgrp_filter_list_update(struct update_group *updgrp,
1231 const char *name)
3f9c7369 1232{
d62a17ae 1233 struct peer *peer;
1234 struct bgp_filter *filter;
1235
1236 peer = UPDGRP_PEER(updgrp);
1237 filter = &peer->filter[UPDGRP_AFI(updgrp)][UPDGRP_SAFI(updgrp)];
1238
1239 if (FILTER_LIST_OUT_NAME(filter)
1240 && (strcmp(name, FILTER_LIST_OUT_NAME(filter)) == 0)) {
1241 FILTER_LIST_OUT(filter) =
1242 as_list_lookup(FILTER_LIST_OUT_NAME(filter));
3dc339cd 1243 return true;
d62a17ae 1244 }
3dc339cd 1245 return false;
3f9c7369
DS
1246}
1247
3dc339cd
DA
1248static bool updgrp_distribute_list_update(struct update_group *updgrp,
1249 const char *name)
3f9c7369 1250{
d62a17ae 1251 struct peer *peer;
1252 struct bgp_filter *filter;
1253
1254 peer = UPDGRP_PEER(updgrp);
1255 filter = &peer->filter[UPDGRP_AFI(updgrp)][UPDGRP_SAFI(updgrp)];
1256
1257 if (DISTRIBUTE_OUT_NAME(filter)
1258 && (strcmp(name, DISTRIBUTE_OUT_NAME(filter)) == 0)) {
1259 DISTRIBUTE_OUT(filter) = access_list_lookup(
1260 UPDGRP_AFI(updgrp), DISTRIBUTE_OUT_NAME(filter));
3dc339cd 1261 return true;
d62a17ae 1262 }
3dc339cd 1263 return false;
3f9c7369
DS
1264}
1265
d62a17ae 1266static int updgrp_route_map_update(struct update_group *updgrp,
1267 const char *name, int *def_rmap_changed)
3f9c7369 1268{
d62a17ae 1269 struct peer *peer;
1270 struct bgp_filter *filter;
1271 int changed = 0;
1272 afi_t afi;
1273 safi_t safi;
1274
1275 peer = UPDGRP_PEER(updgrp);
1276 afi = UPDGRP_AFI(updgrp);
1277 safi = UPDGRP_SAFI(updgrp);
1278 filter = &peer->filter[afi][safi];
1279
1280 if (ROUTE_MAP_OUT_NAME(filter)
1281 && (strcmp(name, ROUTE_MAP_OUT_NAME(filter)) == 0)) {
1282 ROUTE_MAP_OUT(filter) = route_map_lookup_by_name(name);
1283
1284 changed = 1;
1285 }
1286
1287 if (UNSUPPRESS_MAP_NAME(filter)
1288 && (strcmp(name, UNSUPPRESS_MAP_NAME(filter)) == 0)) {
1289 UNSUPPRESS_MAP(filter) = route_map_lookup_by_name(name);
1290 changed = 1;
1291 }
1292
1293 /* process default-originate route-map */
1294 if (peer->default_rmap[afi][safi].name
1295 && (strcmp(name, peer->default_rmap[afi][safi].name) == 0)) {
1296 peer->default_rmap[afi][safi].map =
1297 route_map_lookup_by_name(name);
1298 if (def_rmap_changed)
1299 *def_rmap_changed = 1;
1300 }
1301 return changed;
3f9c7369
DS
1302}
1303
1304/*
1305 * hash iteration callback function to process a policy change for an
1306 * update group. Check if the changed policy matches the updgrp's
1307 * outbound route-map or unsuppress-map or default-originate map or
1308 * filter-list or prefix-list or distribute-list.
1309 * Trigger update generation accordingly.
1310 */
d62a17ae 1311static int updgrp_policy_update_walkcb(struct update_group *updgrp, void *arg)
3f9c7369 1312{
d62a17ae 1313 struct updwalk_context *ctx = arg;
1314 struct update_subgroup *subgrp;
1315 int changed = 0;
1316 int def_changed = 0;
1317
1318 if (!updgrp || !ctx || !ctx->policy_name)
1319 return UPDWALK_CONTINUE;
1320
1321 switch (ctx->policy_type) {
1322 case BGP_POLICY_ROUTE_MAP:
1323 changed = updgrp_route_map_update(updgrp, ctx->policy_name,
1324 &def_changed);
1325 break;
1326 case BGP_POLICY_FILTER_LIST:
1327 changed = updgrp_filter_list_update(updgrp, ctx->policy_name);
1328 break;
1329 case BGP_POLICY_PREFIX_LIST:
1330 changed = updgrp_prefix_list_update(updgrp, ctx->policy_name);
1331 break;
1332 case BGP_POLICY_DISTRIBUTE_LIST:
1333 changed =
1334 updgrp_distribute_list_update(updgrp, ctx->policy_name);
1335 break;
1336 default:
1337 break;
1338 }
1339
1340 /* If not doing route update, return after updating "config" */
1341 if (!ctx->policy_route_update)
1342 return UPDWALK_CONTINUE;
1343
1344 /* If nothing has changed, return after updating "config" */
1345 if (!changed && !def_changed)
1346 return UPDWALK_CONTINUE;
1347
1348 /*
1349 * If something has changed, at the beginning of a route-map
1350 * modification
1351 * event, mark each subgroup's needs-refresh bit. For one, it signals to
1352 * whoever that the subgroup needs a refresh. Second, it prevents
1353 * premature
1354 * merge of this subgroup with another before a complete (outbound)
1355 * refresh.
1356 */
1357 if (ctx->policy_event_start_flag) {
a2addae8 1358 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 1359 update_subgroup_set_needs_refresh(subgrp, 1);
1360 }
1361 return UPDWALK_CONTINUE;
1362 }
1363
a2addae8 1364 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 1365 if (changed) {
1366 if (bgp_debug_update(NULL, NULL, updgrp, 0))
1367 zlog_debug(
6cde4b45 1368 "u%" PRIu64 ":s%" PRIu64" announcing routes upon policy %s (type %d) change",
d62a17ae 1369 updgrp->id, subgrp->id,
1370 ctx->policy_name, ctx->policy_type);
1371 subgroup_announce_route(subgrp);
1372 }
1373 if (def_changed) {
1374 if (bgp_debug_update(NULL, NULL, updgrp, 0))
1375 zlog_debug(
6cde4b45 1376 "u%" PRIu64 ":s%" PRIu64" announcing default upon default routemap %s change",
d62a17ae 1377 updgrp->id, subgrp->id,
1378 ctx->policy_name);
1379 subgroup_default_originate(subgrp, 0);
1380 }
1381 update_subgroup_set_needs_refresh(subgrp, 0);
1382 }
1383 return UPDWALK_CONTINUE;
3f9c7369
DS
1384}
1385
e3b78da8 1386static int update_group_walkcb(struct hash_bucket *bucket, void *arg)
3f9c7369 1387{
e3b78da8 1388 struct update_group *updgrp = bucket->data;
d62a17ae 1389 struct updwalk_context *wctx = arg;
1390 int ret = (*wctx->cb)(updgrp, wctx->context);
1391 return ret;
3f9c7369
DS
1392}
1393
d62a17ae 1394static int update_group_periodic_merge_walkcb(struct update_group *updgrp,
1395 void *arg)
3f9c7369 1396{
d62a17ae 1397 struct update_subgroup *subgrp;
1398 struct update_subgroup *tmp_subgrp;
1399 const char *reason = arg;
3f9c7369 1400
a2addae8
RW
1401 UPDGRP_FOREACH_SUBGRP_SAFE (updgrp, subgrp, tmp_subgrp)
1402 update_subgroup_check_merge(subgrp, reason);
d62a17ae 1403 return UPDWALK_CONTINUE;
3f9c7369
DS
1404}
1405
1406/********************
1407 * PUBLIC FUNCTIONS
1408 ********************/
1409
1410/*
1411 * trigger function when a policy (route-map/filter-list/prefix-list/
1412 * distribute-list etc.) content changes. Go through all the
1413 * update groups and process the change.
1414 *
1415 * bgp: the bgp instance
1416 * ptype: the type of policy that got modified, see bgpd.h
1417 * pname: name of the policy
1418 * route_update: flag to control if an automatic update generation should
1419 * occur
1420 * start_event: flag that indicates if it's the beginning of the change.
1421 * Esp. when the user is changing the content interactively
1422 * over multiple statements. Useful to set dirty flag on
1423 * update groups.
1424 */
d62a17ae 1425void update_group_policy_update(struct bgp *bgp, bgp_policy_type_e ptype,
1426 const char *pname, int route_update,
1427 int start_event)
3f9c7369 1428{
d62a17ae 1429 struct updwalk_context ctx;
3f9c7369 1430
d62a17ae 1431 memset(&ctx, 0, sizeof(ctx));
1432 ctx.policy_type = ptype;
1433 ctx.policy_name = pname;
1434 ctx.policy_route_update = route_update;
1435 ctx.policy_event_start_flag = start_event;
1436 ctx.flags = 0;
3f9c7369 1437
d62a17ae 1438 update_group_walk(bgp, updgrp_policy_update_walkcb, &ctx);
3f9c7369
DS
1439}
1440
1441/*
1442 * update_subgroup_split_peer
1443 *
1444 * Ensure that the given peer is in a subgroup of its own in the
1445 * specified update group.
1446 */
d62a17ae 1447void update_subgroup_split_peer(struct peer_af *paf,
1448 struct update_group *updgrp)
3f9c7369 1449{
d62a17ae 1450 struct update_subgroup *old_subgrp, *subgrp;
1451 uint64_t old_id;
1452
1453
1454 old_subgrp = paf->subgroup;
1455
1456 if (!updgrp)
1457 updgrp = old_subgrp->update_group;
1458
1459 /*
1460 * If the peer is alone in its subgroup, reuse the existing
1461 * subgroup.
1462 */
1463 if (old_subgrp->peer_count == 1) {
1464 if (updgrp == old_subgrp->update_group)
1465 return;
1466
1467 subgrp = old_subgrp;
1468 old_id = old_subgrp->update_group->id;
1469
1470 if (bgp_debug_peer_updout_enabled(paf->peer->host)) {
1471 UPDGRP_PEER_DBG_DIS(old_subgrp->update_group);
1472 }
1473
1474 update_group_remove_subgroup(old_subgrp->update_group,
1475 old_subgrp);
1476 update_group_add_subgroup(updgrp, subgrp);
1477
1478 if (bgp_debug_peer_updout_enabled(paf->peer->host)) {
1479 UPDGRP_PEER_DBG_EN(updgrp);
1480 }
1481 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
6cde4b45 1482 zlog_debug("u%" PRIu64 ":s%" PRIu64" peer %s moved to u%" PRIu64 ":s%" PRIu64,
d62a17ae 1483 old_id, subgrp->id, paf->peer->host,
1484 updgrp->id, subgrp->id);
1485
1486 /*
1487 * The state of the subgroup (adj_out, advs, packet queue etc)
1488 * is consistent internally, but may not be identical to other
1489 * subgroups in the new update group even if the version number
1490 * matches up. Make sure a full refresh is done before the
1491 * subgroup is merged with another.
1492 */
1493 update_subgroup_set_needs_refresh(subgrp, 1);
1494
1495 SUBGRP_INCR_STAT(subgrp, updgrp_switch_events);
1496 return;
1497 }
3f9c7369 1498
d62a17ae 1499 /*
1500 * Create a new subgroup under the specified update group, and copy
1501 * over relevant state to it.
1502 */
1503 subgrp = update_subgroup_create(updgrp);
1504 update_subgroup_inherit_info(subgrp, old_subgrp);
1505
1506 subgrp->split_from.update_group_id = old_subgrp->update_group->id;
1507 subgrp->split_from.subgroup_id = old_subgrp->id;
1508
1509 /*
1510 * Copy out relevant state from the old subgroup.
1511 */
1512 update_subgroup_copy_adj_out(paf->subgroup, subgrp);
1513 update_subgroup_copy_packets(subgrp, paf->next_pkt_to_send);
1514
1515 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
6cde4b45 1516 zlog_debug("u%" PRIu64 ":s%" PRIu64" peer %s split and moved into u%" PRIu64":s%" PRIu64,
d62a17ae 1517 paf->subgroup->update_group->id, paf->subgroup->id,
1518 paf->peer->host, updgrp->id, subgrp->id);
1519
1520 SUBGRP_INCR_STAT(paf->subgroup, split_events);
1521
1522 /*
1523 * Since queued advs were left behind, this new subgroup needs a
1524 * refresh.
1525 */
1526 update_subgroup_set_needs_refresh(subgrp, 1);
1527
1528 /*
1529 * Remove peer from old subgroup, and add it to the new one.
1530 */
1531 update_subgroup_remove_peer(paf->subgroup, paf);
1532
1533 update_subgroup_add_peer(subgrp, paf, 1);
3f9c7369
DS
1534}
1535
d62a17ae 1536void update_bgp_group_init(struct bgp *bgp)
3f9c7369 1537{
d62a17ae 1538 int afid;
3f9c7369 1539
a2addae8 1540 AF_FOREACH (afid)
3f65c5b1 1541 bgp->update_groups[afid] =
996c9314 1542 hash_create(updgrp_hash_key_make, updgrp_hash_cmp,
3f65c5b1 1543 "BGP Update Group Hash");
3f9c7369
DS
1544}
1545
d62a17ae 1546void update_bgp_group_free(struct bgp *bgp)
3d68677e 1547{
d62a17ae 1548 int afid;
1549
a2addae8 1550 AF_FOREACH (afid) {
d62a17ae 1551 if (bgp->update_groups[afid]) {
1552 hash_free(bgp->update_groups[afid]);
1553 bgp->update_groups[afid] = NULL;
1554 }
1555 }
3d68677e
DS
1556}
1557
d62a17ae 1558void update_group_show(struct bgp *bgp, afi_t afi, safi_t safi, struct vty *vty,
1559 uint64_t subgrp_id)
3f9c7369 1560{
d62a17ae 1561 struct updwalk_context ctx;
1562 memset(&ctx, 0, sizeof(ctx));
1563 ctx.vty = vty;
1564 ctx.subgrp_id = subgrp_id;
8fe8a7f6 1565
d62a17ae 1566 update_group_af_walk(bgp, afi, safi, update_group_show_walkcb, &ctx);
3f9c7369
DS
1567}
1568
1569/*
1570 * update_group_show_stats
1571 *
1572 * Show global statistics about update groups.
1573 */
d62a17ae 1574void update_group_show_stats(struct bgp *bgp, struct vty *vty)
3f9c7369 1575{
d62a17ae 1576 vty_out(vty, "Update groups created: %u\n",
1577 bgp->update_group_stats.updgrps_created);
1578 vty_out(vty, "Update groups deleted: %u\n",
1579 bgp->update_group_stats.updgrps_deleted);
1580 vty_out(vty, "Update subgroups created: %u\n",
1581 bgp->update_group_stats.subgrps_created);
1582 vty_out(vty, "Update subgroups deleted: %u\n",
1583 bgp->update_group_stats.subgrps_deleted);
1584 vty_out(vty, "Join events: %u\n", bgp->update_group_stats.join_events);
1585 vty_out(vty, "Prune events: %u\n",
1586 bgp->update_group_stats.prune_events);
1587 vty_out(vty, "Merge events: %u\n",
1588 bgp->update_group_stats.merge_events);
1589 vty_out(vty, "Split events: %u\n",
1590 bgp->update_group_stats.split_events);
1591 vty_out(vty, "Update group switch events: %u\n",
1592 bgp->update_group_stats.updgrp_switch_events);
1593 vty_out(vty, "Peer route refreshes combined: %u\n",
1594 bgp->update_group_stats.peer_refreshes_combined);
1595 vty_out(vty, "Merge checks triggered: %u\n",
1596 bgp->update_group_stats.merge_checks_triggered);
3f9c7369
DS
1597}
1598
1599/*
1600 * update_group_adjust_peer
1601 */
d62a17ae 1602void update_group_adjust_peer(struct peer_af *paf)
3f9c7369 1603{
d62a17ae 1604 struct update_group *updgrp;
1605 struct update_subgroup *subgrp, *old_subgrp;
1606 struct peer *peer;
1607
1608 if (!paf)
1609 return;
1610
1611 peer = PAF_PEER(paf);
1612 if (!peer_established(peer)) {
1613 return;
1614 }
1615
1616 if (!CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE)) {
1617 return;
3f9c7369 1618 }
3f9c7369 1619
d62a17ae 1620 if (!peer->afc_nego[paf->afi][paf->safi]) {
1621 return;
1622 }
3f9c7369 1623
d62a17ae 1624 updgrp = update_group_find(paf);
1625 if (!updgrp) {
1626 updgrp = update_group_create(paf);
1627 if (!updgrp) {
e50f7cfd 1628 flog_err(EC_BGP_UPDGRP_CREATE,
1c50c1c0
QY
1629 "couldn't create update group for peer %s",
1630 paf->peer->host);
d62a17ae 1631 return;
1632 }
1633 }
3f9c7369 1634
d62a17ae 1635 old_subgrp = paf->subgroup;
3f9c7369 1636
d62a17ae 1637 if (old_subgrp) {
3f9c7369 1638
d62a17ae 1639 /*
1640 * If the update group of the peer is unchanged, the peer can
1641 * stay
1642 * in its existing subgroup and we're done.
1643 */
1644 if (old_subgrp->update_group == updgrp)
1645 return;
1646
1647 /*
1648 * The peer is switching between update groups. Put it in its
1649 * own subgroup under the new update group.
1650 */
1651 update_subgroup_split_peer(paf, updgrp);
1652 return;
1653 }
1654
1655 subgrp = update_subgroup_find(updgrp, paf);
1656 if (!subgrp) {
1657 subgrp = update_subgroup_create(updgrp);
1658 if (!subgrp)
1659 return;
1660 }
3f9c7369 1661
d62a17ae 1662 update_subgroup_add_peer(subgrp, paf, 1);
1663 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
1664 zlog_debug("u%" PRIu64 ":s%" PRIu64 " add peer %s", updgrp->id,
1665 subgrp->id, paf->peer->host);
1666
1667 return;
3f9c7369
DS
1668}
1669
d62a17ae 1670int update_group_adjust_soloness(struct peer *peer, int set)
3f9c7369 1671{
d62a17ae 1672 struct peer_group *group;
1673 struct listnode *node, *nnode;
1674
1675 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP)) {
1676 peer_lonesoul_or_not(peer, set);
1677 if (peer->status == Established)
1678 bgp_announce_route_all(peer);
1679 } else {
1680 group = peer->group;
1681 for (ALL_LIST_ELEMENTS(group->peer, node, nnode, peer)) {
1682 peer_lonesoul_or_not(peer, set);
1683 if (peer->status == Established)
1684 bgp_announce_route_all(peer);
1685 }
1686 }
1687 return 0;
3f9c7369
DS
1688}
1689
1690/*
1691 * update_subgroup_rib
1692 */
d62a17ae 1693struct bgp_table *update_subgroup_rib(struct update_subgroup *subgrp)
3f9c7369 1694{
d62a17ae 1695 struct bgp *bgp;
3f9c7369 1696
d62a17ae 1697 bgp = SUBGRP_INST(subgrp);
1698 if (!bgp)
1699 return NULL;
3f9c7369 1700
d62a17ae 1701 return bgp->rib[SUBGRP_AFI(subgrp)][SUBGRP_SAFI(subgrp)];
3f9c7369
DS
1702}
1703
d62a17ae 1704void update_group_af_walk(struct bgp *bgp, afi_t afi, safi_t safi,
1705 updgrp_walkcb cb, void *ctx)
3f9c7369 1706{
d62a17ae 1707 struct updwalk_context wctx;
1708 int afid;
3f9c7369 1709
d62a17ae 1710 if (!bgp)
1711 return;
1712 afid = afindex(afi, safi);
1713 if (afid >= BGP_AF_MAX)
1714 return;
3f9c7369 1715
d62a17ae 1716 memset(&wctx, 0, sizeof(wctx));
1717 wctx.cb = cb;
1718 wctx.context = ctx;
0de4848d 1719
d62a17ae 1720 if (bgp->update_groups[afid])
1721 hash_walk(bgp->update_groups[afid], update_group_walkcb, &wctx);
3f9c7369
DS
1722}
1723
d62a17ae 1724void update_group_walk(struct bgp *bgp, updgrp_walkcb cb, void *ctx)
3f9c7369 1725{
d62a17ae 1726 afi_t afi;
1727 safi_t safi;
3f9c7369 1728
a2addae8 1729 FOREACH_AFI_SAFI (afi, safi) {
d62a17ae 1730 update_group_af_walk(bgp, afi, safi, cb, ctx);
1731 }
3f9c7369
DS
1732}
1733
d62a17ae 1734void update_group_periodic_merge(struct bgp *bgp)
3f9c7369 1735{
d62a17ae 1736 char reason[] = "periodic merge check";
3f9c7369 1737
d62a17ae 1738 update_group_walk(bgp, update_group_periodic_merge_walkcb,
1739 (void *)reason);
3f9c7369
DS
1740}
1741
0de4848d
DS
1742static int
1743update_group_default_originate_route_map_walkcb(struct update_group *updgrp,
d62a17ae 1744 void *arg)
0de4848d 1745{
d62a17ae 1746 struct update_subgroup *subgrp;
1747 struct peer *peer;
1748 afi_t afi;
1749 safi_t safi;
1750
a2addae8 1751 UPDGRP_FOREACH_SUBGRP (updgrp, subgrp) {
d62a17ae 1752 peer = SUBGRP_PEER(subgrp);
1753 afi = SUBGRP_AFI(subgrp);
1754 safi = SUBGRP_SAFI(subgrp);
1755
1756 if (peer->default_rmap[afi][safi].name) {
1757 subgroup_default_originate(subgrp, 0);
1758 }
1759 }
1760
1761 return UPDWALK_CONTINUE;
0de4848d
DS
1762}
1763
d62a17ae 1764int update_group_refresh_default_originate_route_map(struct thread *thread)
0de4848d 1765{
d62a17ae 1766 struct bgp *bgp;
1767 char reason[] = "refresh default-originate route-map";
0de4848d 1768
d62a17ae 1769 bgp = THREAD_ARG(thread);
1770 update_group_walk(bgp, update_group_default_originate_route_map_walkcb,
1771 reason);
1772 THREAD_TIMER_OFF(bgp->t_rmap_def_originate_eval);
1773 bgp_unlock(bgp);
ffd0c037 1774
95f7965d 1775 return 0;
0de4848d
DS
1776}
1777
3f9c7369
DS
1778/*
1779 * peer_af_announce_route
1780 *
1781 * Refreshes routes out to a peer_af immediately.
1782 *
2951a7a4 1783 * If the combine parameter is true, then this function will try to
3f9c7369
DS
1784 * gather other peers in the subgroup for which a route announcement
1785 * is pending and efficently announce routes to all of them.
1786 *
1787 * For now, the 'combine' option has an effect only if all peers in
1788 * the subgroup have a route announcement pending.
1789 */
d62a17ae 1790void peer_af_announce_route(struct peer_af *paf, int combine)
3f9c7369 1791{
d62a17ae 1792 struct update_subgroup *subgrp;
1793 struct peer_af *cur_paf;
1794 int all_pending;
1795
1796 subgrp = paf->subgroup;
1797 all_pending = 0;
1798
1799 if (combine) {
1800 /*
1801 * If there are other peers in the old subgroup that also need
1802 * routes to be announced, pull them into the peer's new
1803 * subgroup.
1804 * Combine route announcement with other peers if possible.
1805 *
1806 * For now, we combine only if all peers in the subgroup have an
1807 * announcement pending.
1808 */
1809 all_pending = 1;
1810
a2addae8 1811 SUBGRP_FOREACH_PEER (subgrp, cur_paf) {
d62a17ae 1812 if (cur_paf == paf)
1813 continue;
1814
1815 if (cur_paf->t_announce_route)
1816 continue;
1817
1818 all_pending = 0;
1819 break;
1820 }
1821 }
1822 /*
1823 * Announce to the peer alone if we were not asked to combine peers,
1824 * or if some peers don't have a route annoucement pending.
1825 */
1826 if (!combine || !all_pending) {
1827 update_subgroup_split_peer(paf, NULL);
7bfdba54 1828 subgrp = paf->subgroup;
d62a17ae 1829
7bfdba54 1830 assert(subgrp && subgrp->update_group);
d62a17ae 1831 if (bgp_debug_update(paf->peer, NULL, subgrp->update_group, 0))
6cde4b45 1832 zlog_debug("u%" PRIu64 ":s%" PRIu64" %s announcing routes",
d62a17ae 1833 subgrp->update_group->id, subgrp->id,
1834 paf->peer->host);
1835
1836 subgroup_announce_route(paf->subgroup);
1837 return;
3f9c7369 1838 }
3f9c7369 1839
d62a17ae 1840 /*
1841 * We will announce routes the entire subgroup.
1842 *
1843 * First stop refresh timers on all the other peers.
1844 */
a2addae8 1845 SUBGRP_FOREACH_PEER (subgrp, cur_paf) {
d62a17ae 1846 if (cur_paf == paf)
1847 continue;
3f9c7369 1848
d62a17ae 1849 bgp_stop_announce_route_timer(cur_paf);
1850 }
3f9c7369 1851
d62a17ae 1852 if (bgp_debug_update(paf->peer, NULL, subgrp->update_group, 0))
6cde4b45 1853 zlog_debug("u%" PRIu64 ":s%" PRIu64" announcing routes to %s, combined into %d peers",
d62a17ae 1854 subgrp->update_group->id, subgrp->id,
1855 paf->peer->host, subgrp->peer_count);
3f9c7369 1856
d62a17ae 1857 subgroup_announce_route(subgrp);
3f9c7369 1858
d62a17ae 1859 SUBGRP_INCR_STAT_BY(subgrp, peer_refreshes_combined,
1860 subgrp->peer_count - 1);
3f9c7369
DS
1861}
1862
2fc102e1
QY
1863void subgroup_trigger_write(struct update_subgroup *subgrp)
1864{
1865 struct peer_af *paf;
1866
becedef6
QY
1867 /*
1868 * For each peer in the subgroup, schedule a job to pull packets from
1869 * the subgroup output queue into their own output queue. This action
1870 * will trigger a write job on the I/O thread.
1871 */
996c9314
LB
1872 SUBGRP_FOREACH_PEER (subgrp, paf)
1873 if (paf->peer->status == Established)
1874 thread_add_timer_msec(
1875 bm->master, bgp_generate_updgrp_packets,
1876 paf->peer, 0,
1877 &paf->peer->t_generate_updgrp_packets);
2fc102e1
QY
1878}
1879
d62a17ae 1880int update_group_clear_update_dbg(struct update_group *updgrp, void *arg)
3f9c7369 1881{
d62a17ae 1882 UPDGRP_PEER_DBG_OFF(updgrp);
1883 return UPDWALK_CONTINUE;
3f9c7369 1884}
adbac85e 1885
06370dac 1886/* Return true if we should addpath encode NLRI to this peer */
d62a17ae 1887int bgp_addpath_encode_tx(struct peer *peer, afi_t afi, safi_t safi)
adbac85e 1888{
d62a17ae 1889 return (CHECK_FLAG(peer->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_TX_ADV)
1890 && CHECK_FLAG(peer->af_cap[afi][safi],
1891 PEER_CAP_ADDPATH_AF_RX_RCV));
adbac85e 1892}