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