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