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