2 * bgp_updgrp.c: BGP update group structures
4 * @copyright Copyright (C) 2014 Cumulus Networks, Inc.
6 * @author Avneesh Sachdev <avneesh@sproute.net>
7 * @author Rajesh Varadarajan <rajesh@sproute.net>
8 * @author Pradosh Mohapatra <pradosh@sproute.net>
10 * This file is part of GNU Zebra.
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
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.
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
34 #include "sockunion.h"
42 #include "workqueue.h"
47 #include "bgpd/bgpd.h"
48 #include "bgpd/bgp_table.h"
49 #include "bgpd/bgp_debug.h"
50 #include "bgpd/bgp_errors.h"
51 #include "bgpd/bgp_fsm.h"
52 #include "bgpd/bgp_addpath.h"
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"
58 #include "bgpd/bgp_io.h"
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
70 static void update_group_checkin(struct update_group
*updgrp
)
72 updgrp
->id
= ++bm
->updgrp_idspace
;
73 updgrp
->uptime
= monotime(NULL
);
76 static void update_subgroup_checkin(struct update_subgroup
*subgrp
,
77 struct update_group
*updgrp
)
79 subgrp
->id
= ++bm
->subgrp_idspace
;
80 subgrp
->uptime
= monotime(NULL
);
83 static void sync_init(struct update_subgroup
*subgrp
,
84 struct update_group
*updgrp
)
86 struct peer
*peer
= UPDGRP_PEER(updgrp
);
89 XCALLOC(MTYPE_BGP_SYNCHRONISE
, sizeof(struct bgp_synchronize
));
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
);
94 hash_create(bgp_advertise_attr_hash_key
,
95 bgp_advertise_attr_hash_cmp
, "BGP SubGroup Hash");
97 /* We use a larger buffer for subgrp->work in the event that:
98 * - We RX a BGP_UPDATE where the attributes alone are just
99 * under 4096 or 65535 (if Extended Message capability negotiated).
100 * - The user configures an outbound route-map that does many as-path
101 * prepends or adds many communities. At most they can have
103 * args in a route-map so there is a finite limit on how large they
105 * make the attributes.
107 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid
109 * checking for every single attribute as we construct an UPDATE.
111 subgrp
->work
= stream_new(peer
->max_packet_size
112 + BGP_MAX_PACKET_SIZE_OVERFLOW
);
113 subgrp
->scratch
= stream_new(peer
->max_packet_size
);
116 static void sync_delete(struct update_subgroup
*subgrp
)
118 XFREE(MTYPE_BGP_SYNCHRONISE
, subgrp
->sync
);
120 hash_clean(subgrp
->hash
,
121 (void (*)(void *))bgp_advertise_attr_free
);
122 hash_free(subgrp
->hash
);
126 stream_free(subgrp
->work
);
129 stream_free(subgrp
->scratch
);
130 subgrp
->scratch
= NULL
;
136 * copy only those fields that are relevant to update group match
138 static void conf_copy(struct peer
*dst
, struct peer
*src
, afi_t afi
,
141 struct bgp_filter
*srcfilter
;
142 struct bgp_filter
*dstfilter
;
144 srcfilter
= &src
->filter
[afi
][safi
];
145 dstfilter
= &dst
->filter
[afi
][safi
];
148 dst
->sort
= src
->sort
;
150 dst
->v_routeadv
= src
->v_routeadv
;
151 dst
->flags
= src
->flags
;
152 dst
->af_flags
[afi
][safi
] = src
->af_flags
[afi
][safi
];
153 dst
->pmax_out
[afi
][safi
] = src
->pmax_out
[afi
][safi
];
154 dst
->max_packet_size
= src
->max_packet_size
;
155 XFREE(MTYPE_BGP_PEER_HOST
, dst
->host
);
157 dst
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, src
->host
);
159 dst
->af_cap
[afi
][safi
] = src
->af_cap
[afi
][safi
];
160 dst
->afc_nego
[afi
][safi
] = src
->afc_nego
[afi
][safi
];
161 dst
->orf_plist
[afi
][safi
] = src
->orf_plist
[afi
][safi
];
162 dst
->addpath_type
[afi
][safi
] = src
->addpath_type
[afi
][safi
];
163 dst
->local_as
= src
->local_as
;
164 dst
->change_local_as
= src
->change_local_as
;
165 dst
->shared_network
= src
->shared_network
;
166 dst
->local_role
= src
->local_role
;
168 if (src
->soo
[afi
][safi
]) {
169 ecommunity_free(&dst
->soo
[afi
][safi
]);
170 dst
->soo
[afi
][safi
] = ecommunity_dup(src
->soo
[afi
][safi
]);
173 memcpy(&(dst
->nexthop
), &(src
->nexthop
), sizeof(struct bgp_nexthop
));
175 dst
->group
= src
->group
;
177 if (src
->default_rmap
[afi
][safi
].name
) {
178 dst
->default_rmap
[afi
][safi
].name
=
179 XSTRDUP(MTYPE_ROUTE_MAP_NAME
,
180 src
->default_rmap
[afi
][safi
].name
);
181 dst
->default_rmap
[afi
][safi
].map
=
182 src
->default_rmap
[afi
][safi
].map
;
185 if (DISTRIBUTE_OUT_NAME(srcfilter
)) {
186 DISTRIBUTE_OUT_NAME(dstfilter
) = XSTRDUP(
187 MTYPE_BGP_FILTER_NAME
, DISTRIBUTE_OUT_NAME(srcfilter
));
188 DISTRIBUTE_OUT(dstfilter
) = DISTRIBUTE_OUT(srcfilter
);
191 if (PREFIX_LIST_OUT_NAME(srcfilter
)) {
192 PREFIX_LIST_OUT_NAME(dstfilter
) = XSTRDUP(
193 MTYPE_BGP_FILTER_NAME
, PREFIX_LIST_OUT_NAME(srcfilter
));
194 PREFIX_LIST_OUT(dstfilter
) = PREFIX_LIST_OUT(srcfilter
);
197 if (FILTER_LIST_OUT_NAME(srcfilter
)) {
198 FILTER_LIST_OUT_NAME(dstfilter
) = XSTRDUP(
199 MTYPE_BGP_FILTER_NAME
, FILTER_LIST_OUT_NAME(srcfilter
));
200 FILTER_LIST_OUT(dstfilter
) = FILTER_LIST_OUT(srcfilter
);
203 if (ROUTE_MAP_OUT_NAME(srcfilter
)) {
204 ROUTE_MAP_OUT_NAME(dstfilter
) = XSTRDUP(
205 MTYPE_BGP_FILTER_NAME
, ROUTE_MAP_OUT_NAME(srcfilter
));
206 ROUTE_MAP_OUT(dstfilter
) = ROUTE_MAP_OUT(srcfilter
);
209 if (UNSUPPRESS_MAP_NAME(srcfilter
)) {
210 UNSUPPRESS_MAP_NAME(dstfilter
) = XSTRDUP(
211 MTYPE_BGP_FILTER_NAME
, UNSUPPRESS_MAP_NAME(srcfilter
));
212 UNSUPPRESS_MAP(dstfilter
) = UNSUPPRESS_MAP(srcfilter
);
215 if (ADVERTISE_MAP_NAME(srcfilter
)) {
216 ADVERTISE_MAP_NAME(dstfilter
) = XSTRDUP(
217 MTYPE_BGP_FILTER_NAME
, ADVERTISE_MAP_NAME(srcfilter
));
218 ADVERTISE_MAP(dstfilter
) = ADVERTISE_MAP(srcfilter
);
219 ADVERTISE_CONDITION(dstfilter
) = ADVERTISE_CONDITION(srcfilter
);
222 if (CONDITION_MAP_NAME(srcfilter
)) {
223 CONDITION_MAP_NAME(dstfilter
) = XSTRDUP(
224 MTYPE_BGP_FILTER_NAME
, CONDITION_MAP_NAME(srcfilter
));
225 CONDITION_MAP(dstfilter
) = CONDITION_MAP(srcfilter
);
228 dstfilter
->advmap
.update_type
= srcfilter
->advmap
.update_type
;
232 * since we did a bunch of XSTRDUP's in conf_copy, time to free them up
234 static void conf_release(struct peer
*src
, afi_t afi
, safi_t safi
)
236 struct bgp_filter
*srcfilter
;
238 srcfilter
= &src
->filter
[afi
][safi
];
240 XFREE(MTYPE_ROUTE_MAP_NAME
, src
->default_rmap
[afi
][safi
].name
);
242 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->dlist
[FILTER_OUT
].name
);
244 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->plist
[FILTER_OUT
].name
);
246 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->aslist
[FILTER_OUT
].name
);
248 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->map
[RMAP_OUT
].name
);
250 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->usmap
.name
);
252 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->advmap
.aname
);
254 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->advmap
.cname
);
256 XFREE(MTYPE_BGP_PEER_HOST
, src
->host
);
258 ecommunity_free(&src
->soo
[afi
][safi
]);
261 static void peer2_updgrp_copy(struct update_group
*updgrp
, struct peer_af
*paf
)
274 updgrp
->afi
= paf
->afi
;
275 updgrp
->safi
= paf
->safi
;
276 updgrp
->afid
= paf
->afid
;
277 updgrp
->bgp
= src
->bgp
;
279 conf_copy(dst
, src
, paf
->afi
, paf
->safi
);
283 * auxiliary functions to maintain the hash table.
284 * - updgrp_hash_alloc - to create a new entry, passed to hash_get
285 * - updgrp_hash_key_make - makes the key for update group search
286 * - updgrp_hash_cmp - compare two update groups.
288 static void *updgrp_hash_alloc(void *p
)
290 struct update_group
*updgrp
;
291 const struct update_group
*in
;
293 in
= (const struct update_group
*)p
;
294 updgrp
= XCALLOC(MTYPE_BGP_UPDGRP
, sizeof(struct update_group
));
295 memcpy(updgrp
, in
, sizeof(struct update_group
));
296 updgrp
->conf
= XCALLOC(MTYPE_BGP_PEER
, sizeof(struct peer
));
297 conf_copy(updgrp
->conf
, in
->conf
, in
->afi
, in
->safi
);
302 * The hash value for a peer is computed from the following variables:
304 * 1. IBGP (1) or EBGP (2)
305 * 2. FLAGS based on configuration:
306 * LOCAL_AS_NO_PREPEND
307 * LOCAL_AS_REPLACE_AS
308 * 3. AF_FLAGS based on configuration:
309 * Refer to definition in bgp_updgrp.h
310 * 4. (AF-independent) Capability flags:
312 * 5. (AF-dependent) Capability flags:
313 * ORF_PREFIX_SM_RCV (peer can send prefix ORF)
316 * 8. Outbound route-map name (neighbor route-map <> out)
317 * 9. Outbound distribute-list name (neighbor distribute-list <> out)
318 * 10. Outbound prefix-list name (neighbor prefix-list <> out)
319 * 11. Outbound as-list name (neighbor filter-list <> out)
320 * 12. Unsuppress map name (neighbor unsuppress-map <>)
321 * 13. default rmap name (neighbor default-originate route-map <>)
322 * 14. encoding both global and link-local nexthop?
323 * 15. If peer is configured to be a lonesoul, peer ip address
324 * 16. Local-as should match, if configured.
325 * 17. maximum-prefix-out
326 * 18. Local-role should also match, if configured.
329 static unsigned int updgrp_hash_key_make(const void *p
)
331 const struct update_group
*updgrp
;
332 const struct peer
*peer
;
333 const struct bgp_filter
*filter
;
340 #define SEED2 2147483647
346 flags
= peer
->af_flags
[afi
][safi
];
347 filter
= &peer
->filter
[afi
][safi
];
351 key
= jhash_1word(peer
->sort
, key
); /* EBGP or IBGP */
352 key
= jhash_1word((peer
->flags
& PEER_UPDGRP_FLAGS
), key
);
353 key
= jhash_1word((flags
& PEER_UPDGRP_AF_FLAGS
), key
);
354 key
= jhash_1word((uint32_t)peer
->addpath_type
[afi
][safi
], key
);
355 key
= jhash_1word((peer
->cap
& PEER_UPDGRP_CAP_FLAGS
), key
);
356 key
= jhash_1word((peer
->af_cap
[afi
][safi
] & PEER_UPDGRP_AF_CAP_FLAGS
),
358 key
= jhash_1word(peer
->v_routeadv
, key
);
359 key
= jhash_1word(peer
->change_local_as
, key
);
360 key
= jhash_1word(peer
->max_packet_size
, key
);
361 key
= jhash_1word(peer
->pmax_out
[afi
][safi
], key
);
364 key
= jhash_1word(jhash(peer
->group
->name
,
365 strlen(peer
->group
->name
), SEED1
),
368 if (filter
->map
[RMAP_OUT
].name
)
369 key
= jhash_1word(jhash(filter
->map
[RMAP_OUT
].name
,
370 strlen(filter
->map
[RMAP_OUT
].name
),
374 if (filter
->dlist
[FILTER_OUT
].name
)
375 key
= jhash_1word(jhash(filter
->dlist
[FILTER_OUT
].name
,
376 strlen(filter
->dlist
[FILTER_OUT
].name
),
380 if (filter
->plist
[FILTER_OUT
].name
)
381 key
= jhash_1word(jhash(filter
->plist
[FILTER_OUT
].name
,
382 strlen(filter
->plist
[FILTER_OUT
].name
),
386 if (filter
->aslist
[FILTER_OUT
].name
)
387 key
= jhash_1word(jhash(filter
->aslist
[FILTER_OUT
].name
,
388 strlen(filter
->aslist
[FILTER_OUT
].name
),
392 if (filter
->usmap
.name
)
393 key
= jhash_1word(jhash(filter
->usmap
.name
,
394 strlen(filter
->usmap
.name
), SEED1
),
397 if (filter
->advmap
.aname
)
398 key
= jhash_1word(jhash(filter
->advmap
.aname
,
399 strlen(filter
->advmap
.aname
), SEED1
),
402 if (filter
->advmap
.update_type
)
403 key
= jhash_1word(filter
->advmap
.update_type
, key
);
405 if (peer
->default_rmap
[afi
][safi
].name
)
407 jhash(peer
->default_rmap
[afi
][safi
].name
,
408 strlen(peer
->default_rmap
[afi
][safi
].name
),
412 /* If peer is on a shared network and is exchanging IPv6 prefixes,
413 * it needs to include link-local address. That's different from
414 * non-shared-network peers (nexthop encoded with 32 bytes vs 16
415 * bytes). We create different update groups to take care of that.
418 (peer
->shared_network
&& peer_afi_active_nego(peer
, AFI_IP6
)),
421 * There are certain peers that must get their own update-group:
423 * - peers that negotiated ORF
424 * - maximum-prefix-out is set
426 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_LONESOUL
)
427 || CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_RCV
)
428 || CHECK_FLAG(peer
->af_cap
[afi
][safi
],
429 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
)
430 || CHECK_FLAG(peer
->af_flags
[afi
][safi
], PEER_FLAG_MAX_PREFIX_OUT
))
431 key
= jhash_1word(jhash(peer
->host
, strlen(peer
->host
), SEED2
),
434 * Multiple sessions with the same neighbor should get their own
435 * update-group if they have different roles.
437 key
= jhash_1word(peer
->local_role
, key
);
439 if (peer
->soo
[afi
][safi
]) {
440 char *soo_str
= ecommunity_str(peer
->soo
[afi
][safi
]);
442 key
= jhash_1word(jhash(soo_str
, strlen(soo_str
), SEED1
), key
);
445 if (bgp_debug_neighbor_events(peer
)) {
447 "%pBP Update Group Hash: sort: %d UpdGrpFlags: %ju UpdGrpAFFlags: %ju",
449 (intmax_t)CHECK_FLAG(peer
->flags
, PEER_UPDGRP_FLAGS
),
450 (intmax_t)CHECK_FLAG(flags
, PEER_UPDGRP_AF_FLAGS
));
452 "%pBP Update Group Hash: addpath: %u UpdGrpCapFlag: %u UpdGrpCapAFFlag: %u route_adv: %u change local as: %u",
453 peer
, (uint32_t)peer
->addpath_type
[afi
][safi
],
454 CHECK_FLAG(peer
->cap
, PEER_UPDGRP_CAP_FLAGS
),
455 CHECK_FLAG(peer
->af_cap
[afi
][safi
],
456 PEER_UPDGRP_AF_CAP_FLAGS
),
457 peer
->v_routeadv
, peer
->change_local_as
);
459 "%pBP Update Group Hash: max packet size: %u pmax_out: %u Peer Group: %s rmap out: %s",
460 peer
, peer
->max_packet_size
, peer
->pmax_out
[afi
][safi
],
461 peer
->group
? peer
->group
->name
: "(NONE)",
462 ROUTE_MAP_OUT_NAME(filter
) ? ROUTE_MAP_OUT_NAME(filter
)
465 "%pBP Update Group Hash: dlist out: %s plist out: %s aslist out: %s usmap out: %s advmap: %s",
467 DISTRIBUTE_OUT_NAME(filter
)
468 ? DISTRIBUTE_OUT_NAME(filter
)
470 PREFIX_LIST_OUT_NAME(filter
)
471 ? PREFIX_LIST_OUT_NAME(filter
)
473 FILTER_LIST_OUT_NAME(filter
)
474 ? FILTER_LIST_OUT_NAME(filter
)
476 UNSUPPRESS_MAP_NAME(filter
)
477 ? UNSUPPRESS_MAP_NAME(filter
)
479 ADVERTISE_MAP_NAME(filter
) ? ADVERTISE_MAP_NAME(filter
)
482 "%pBP Update Group Hash: default rmap: %s shared network and afi active network: %d",
484 peer
->default_rmap
[afi
][safi
].name
485 ? peer
->default_rmap
[afi
][safi
].name
487 peer
->shared_network
&&
488 peer_afi_active_nego(peer
, AFI_IP6
));
490 "%pBP Update Group Hash: Lonesoul: %d ORF prefix: %u ORF old: %u max prefix out: %ju",
491 peer
, !!CHECK_FLAG(peer
->flags
, PEER_FLAG_LONESOUL
),
492 CHECK_FLAG(peer
->af_cap
[afi
][safi
],
493 PEER_CAP_ORF_PREFIX_SM_RCV
),
494 CHECK_FLAG(peer
->af_cap
[afi
][safi
],
495 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
),
496 (intmax_t)CHECK_FLAG(peer
->af_flags
[afi
][safi
],
497 PEER_FLAG_MAX_PREFIX_OUT
));
498 zlog_debug("%pBP Update Group Hash key: %u", peer
, key
);
503 static bool updgrp_hash_cmp(const void *p1
, const void *p2
)
505 const struct update_group
*grp1
;
506 const struct update_group
*grp2
;
507 const struct peer
*pe1
;
508 const struct peer
*pe2
;
511 const struct bgp_filter
*fl1
;
512 const struct bgp_filter
*fl2
;
525 flags1
= pe1
->af_flags
[afi
][safi
];
526 flags2
= pe2
->af_flags
[afi
][safi
];
527 fl1
= &pe1
->filter
[afi
][safi
];
528 fl2
= &pe2
->filter
[afi
][safi
];
530 /* put EBGP and IBGP peers in different update groups */
531 if (pe1
->sort
!= pe2
->sort
)
534 /* check peer flags */
535 if ((pe1
->flags
& PEER_UPDGRP_FLAGS
)
536 != (pe2
->flags
& PEER_UPDGRP_FLAGS
))
539 /* If there is 'local-as' configured, it should match. */
540 if (pe1
->change_local_as
!= pe2
->change_local_as
)
543 if (pe1
->pmax_out
[afi
][safi
] != pe2
->pmax_out
[afi
][safi
])
546 /* flags like route reflector client */
547 if ((flags1
& PEER_UPDGRP_AF_FLAGS
) != (flags2
& PEER_UPDGRP_AF_FLAGS
))
550 if (pe1
->addpath_type
[afi
][safi
] != pe2
->addpath_type
[afi
][safi
])
553 if ((pe1
->cap
& PEER_UPDGRP_CAP_FLAGS
)
554 != (pe2
->cap
& PEER_UPDGRP_CAP_FLAGS
))
557 if ((pe1
->af_cap
[afi
][safi
] & PEER_UPDGRP_AF_CAP_FLAGS
)
558 != (pe2
->af_cap
[afi
][safi
] & PEER_UPDGRP_AF_CAP_FLAGS
))
561 if (pe1
->v_routeadv
!= pe2
->v_routeadv
)
564 if (pe1
->group
!= pe2
->group
)
567 /* Roles can affect filtering */
568 if (pe1
->local_role
!= pe2
->local_role
)
571 /* route-map names should be the same */
572 if ((fl1
->map
[RMAP_OUT
].name
&& !fl2
->map
[RMAP_OUT
].name
)
573 || (!fl1
->map
[RMAP_OUT
].name
&& fl2
->map
[RMAP_OUT
].name
)
574 || (fl1
->map
[RMAP_OUT
].name
&& fl2
->map
[RMAP_OUT
].name
575 && strcmp(fl1
->map
[RMAP_OUT
].name
, fl2
->map
[RMAP_OUT
].name
)))
578 if ((fl1
->dlist
[FILTER_OUT
].name
&& !fl2
->dlist
[FILTER_OUT
].name
)
579 || (!fl1
->dlist
[FILTER_OUT
].name
&& fl2
->dlist
[FILTER_OUT
].name
)
580 || (fl1
->dlist
[FILTER_OUT
].name
&& fl2
->dlist
[FILTER_OUT
].name
581 && strcmp(fl1
->dlist
[FILTER_OUT
].name
,
582 fl2
->dlist
[FILTER_OUT
].name
)))
585 if ((fl1
->plist
[FILTER_OUT
].name
&& !fl2
->plist
[FILTER_OUT
].name
)
586 || (!fl1
->plist
[FILTER_OUT
].name
&& fl2
->plist
[FILTER_OUT
].name
)
587 || (fl1
->plist
[FILTER_OUT
].name
&& fl2
->plist
[FILTER_OUT
].name
588 && strcmp(fl1
->plist
[FILTER_OUT
].name
,
589 fl2
->plist
[FILTER_OUT
].name
)))
592 if ((fl1
->aslist
[FILTER_OUT
].name
&& !fl2
->aslist
[FILTER_OUT
].name
)
593 || (!fl1
->aslist
[FILTER_OUT
].name
&& fl2
->aslist
[FILTER_OUT
].name
)
594 || (fl1
->aslist
[FILTER_OUT
].name
&& fl2
->aslist
[FILTER_OUT
].name
595 && strcmp(fl1
->aslist
[FILTER_OUT
].name
,
596 fl2
->aslist
[FILTER_OUT
].name
)))
599 if ((fl1
->usmap
.name
&& !fl2
->usmap
.name
)
600 || (!fl1
->usmap
.name
&& fl2
->usmap
.name
)
601 || (fl1
->usmap
.name
&& fl2
->usmap
.name
602 && strcmp(fl1
->usmap
.name
, fl2
->usmap
.name
)))
605 if ((fl1
->advmap
.aname
&& !fl2
->advmap
.aname
)
606 || (!fl1
->advmap
.aname
&& fl2
->advmap
.aname
)
607 || (fl1
->advmap
.aname
&& fl2
->advmap
.aname
608 && strcmp(fl1
->advmap
.aname
, fl2
->advmap
.aname
)))
611 if (fl1
->advmap
.update_type
!= fl2
->advmap
.update_type
)
614 if ((pe1
->default_rmap
[afi
][safi
].name
615 && !pe2
->default_rmap
[afi
][safi
].name
)
616 || (!pe1
->default_rmap
[afi
][safi
].name
617 && pe2
->default_rmap
[afi
][safi
].name
)
618 || (pe1
->default_rmap
[afi
][safi
].name
619 && pe2
->default_rmap
[afi
][safi
].name
620 && strcmp(pe1
->default_rmap
[afi
][safi
].name
,
621 pe2
->default_rmap
[afi
][safi
].name
)))
624 if ((afi
== AFI_IP6
) && (pe1
->shared_network
!= pe2
->shared_network
))
627 if ((CHECK_FLAG(pe1
->flags
, PEER_FLAG_LONESOUL
)
628 || CHECK_FLAG(pe1
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_RCV
)
629 || CHECK_FLAG(pe1
->af_cap
[afi
][safi
],
630 PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
631 && !sockunion_same(&pe1
->su
, &pe2
->su
))
637 static void peer_lonesoul_or_not(struct peer
*peer
, int set
)
639 /* no change in status? */
640 if (set
== (CHECK_FLAG(peer
->flags
, PEER_FLAG_LONESOUL
) > 0))
644 SET_FLAG(peer
->flags
, PEER_FLAG_LONESOUL
);
646 UNSET_FLAG(peer
->flags
, PEER_FLAG_LONESOUL
);
648 update_group_adjust_peer_afs(peer
);
652 * subgroup_total_packets_enqueued
654 * Returns the total number of packets enqueued to a subgroup.
657 subgroup_total_packets_enqueued(struct update_subgroup
*subgrp
)
661 pkt
= bpacket_queue_last(SUBGRP_PKTQ(subgrp
));
666 static int update_group_show_walkcb(struct update_group
*updgrp
, void *arg
)
668 struct updwalk_context
*ctx
= arg
;
670 struct update_subgroup
*subgrp
;
672 struct bgp_filter
*filter
;
673 struct peer
*peer
= UPDGRP_PEER(updgrp
);
679 if (ctx
->subgrp_id
) {
680 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
681 if (ctx
->subgrp_id
&& (ctx
->subgrp_id
!= subgrp
->id
))
693 /* Since this routine is invoked from a walk, we cannot signal
695 /* error here, can only return. */
701 vty_out(vty
, "Update-group %" PRIu64
":\n", updgrp
->id
);
702 vty_out(vty
, " Created: %s", timestamp_string(updgrp
->uptime
));
703 filter
= &updgrp
->conf
->filter
[updgrp
->afi
][updgrp
->safi
];
704 if (filter
->map
[RMAP_OUT
].name
)
705 vty_out(vty
, " Outgoing route map: %s\n",
706 filter
->map
[RMAP_OUT
].name
);
707 vty_out(vty
, " MRAI value (seconds): %d\n", updgrp
->conf
->v_routeadv
);
708 if (updgrp
->conf
->change_local_as
)
709 vty_out(vty
, " Local AS %u%s%s\n",
710 updgrp
->conf
->change_local_as
,
711 CHECK_FLAG(updgrp
->conf
->flags
,
712 PEER_FLAG_LOCAL_AS_NO_PREPEND
)
715 CHECK_FLAG(updgrp
->conf
->flags
,
716 PEER_FLAG_LOCAL_AS_REPLACE_AS
)
720 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
721 if (ctx
->subgrp_id
&& (ctx
->subgrp_id
!= subgrp
->id
))
724 vty_out(vty
, " Update-subgroup %" PRIu64
":\n", subgrp
->id
);
725 vty_out(vty
, " Created: %s",
726 timestamp_string(subgrp
->uptime
));
728 if (subgrp
->split_from
.update_group_id
729 || subgrp
->split_from
.subgroup_id
) {
730 vty_out(vty
, " Split from group id: %" PRIu64
"\n",
731 subgrp
->split_from
.update_group_id
);
733 " Split from subgroup id: %" PRIu64
"\n",
734 subgrp
->split_from
.subgroup_id
);
737 vty_out(vty
, " Join events: %u\n", subgrp
->join_events
);
738 vty_out(vty
, " Prune events: %u\n", subgrp
->prune_events
);
739 vty_out(vty
, " Merge events: %u\n", subgrp
->merge_events
);
740 vty_out(vty
, " Split events: %u\n", subgrp
->split_events
);
741 vty_out(vty
, " Update group switch events: %u\n",
742 subgrp
->updgrp_switch_events
);
743 vty_out(vty
, " Peer refreshes combined: %u\n",
744 subgrp
->peer_refreshes_combined
);
745 vty_out(vty
, " Merge checks triggered: %u\n",
746 subgrp
->merge_checks_triggered
);
747 vty_out(vty
, " Coalesce Time: %u%s\n",
748 (UPDGRP_INST(subgrp
->update_group
))->coalesce_time
,
749 subgrp
->t_coalesce
? "(Running)" : "");
750 vty_out(vty
, " Version: %" PRIu64
"\n", subgrp
->version
);
751 vty_out(vty
, " Packet queue length: %d\n",
752 bpacket_queue_length(SUBGRP_PKTQ(subgrp
)));
753 vty_out(vty
, " Total packets enqueued: %u\n",
754 subgroup_total_packets_enqueued(subgrp
));
755 vty_out(vty
, " Packet queue high watermark: %d\n",
756 bpacket_queue_hwm_length(SUBGRP_PKTQ(subgrp
)));
757 vty_out(vty
, " Adj-out list count: %u\n", subgrp
->adj_count
);
758 vty_out(vty
, " Advertise list: %s\n",
759 advertise_list_is_empty(subgrp
) ? "empty"
761 vty_out(vty
, " Flags: %s\n",
762 CHECK_FLAG(subgrp
->flags
, SUBGRP_FLAG_NEEDS_REFRESH
)
766 vty_out(vty
, " Max packet size: %d\n",
767 peer
->max_packet_size
);
768 if (subgrp
->peer_count
> 0) {
769 vty_out(vty
, " Peers:\n");
770 SUBGRP_FOREACH_PEER (subgrp
, paf
)
771 vty_out(vty
, " - %s\n", paf
->peer
->host
);
774 return UPDWALK_CONTINUE
;
778 * Helper function to show the packet queue for each subgroup of update group.
779 * Will be constrained to a particular subgroup id if id !=0
781 static int updgrp_show_packet_queue_walkcb(struct update_group
*updgrp
,
784 struct updwalk_context
*ctx
= arg
;
785 struct update_subgroup
*subgrp
;
789 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
790 if (ctx
->subgrp_id
&& (ctx
->subgrp_id
!= subgrp
->id
))
792 vty_out(vty
, "update group %" PRIu64
", subgroup %" PRIu64
"\n",
793 updgrp
->id
, subgrp
->id
);
794 bpacket_queue_show_vty(SUBGRP_PKTQ(subgrp
), vty
);
796 return UPDWALK_CONTINUE
;
800 * Show the packet queue for each subgroup of update group. Will be
801 * constrained to a particular subgroup id if id !=0
803 void update_group_show_packet_queue(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
804 struct vty
*vty
, uint64_t id
)
806 struct updwalk_context ctx
;
808 memset(&ctx
, 0, sizeof(ctx
));
812 update_group_af_walk(bgp
, afi
, safi
, updgrp_show_packet_queue_walkcb
,
816 static struct update_group
*update_group_find(struct peer_af
*paf
)
818 struct update_group
*updgrp
;
819 struct update_group tmp
;
820 struct peer tmp_conf
;
822 if (!peer_established(PAF_PEER(paf
)))
825 memset(&tmp
, 0, sizeof(tmp
));
826 memset(&tmp_conf
, 0, sizeof(tmp_conf
));
827 tmp
.conf
= &tmp_conf
;
828 peer2_updgrp_copy(&tmp
, paf
);
830 updgrp
= hash_lookup(paf
->peer
->bgp
->update_groups
[paf
->afid
], &tmp
);
831 conf_release(&tmp_conf
, paf
->afi
, paf
->safi
);
835 static struct update_group
*update_group_create(struct peer_af
*paf
)
837 struct update_group
*updgrp
;
838 struct update_group tmp
;
839 struct peer tmp_conf
;
841 memset(&tmp
, 0, sizeof(tmp
));
842 memset(&tmp_conf
, 0, sizeof(tmp_conf
));
843 tmp
.conf
= &tmp_conf
;
844 peer2_updgrp_copy(&tmp
, paf
);
846 updgrp
= hash_get(paf
->peer
->bgp
->update_groups
[paf
->afid
], &tmp
,
848 update_group_checkin(updgrp
);
850 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
851 zlog_debug("create update group %" PRIu64
, updgrp
->id
);
853 UPDGRP_GLOBAL_STAT(updgrp
, updgrps_created
) += 1;
855 conf_release(&tmp_conf
, paf
->afi
, paf
->safi
);
859 static void update_group_delete(struct update_group
*updgrp
)
861 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
862 zlog_debug("delete update group %" PRIu64
, updgrp
->id
);
864 UPDGRP_GLOBAL_STAT(updgrp
, updgrps_deleted
) += 1;
866 hash_release(updgrp
->bgp
->update_groups
[updgrp
->afid
], updgrp
);
867 conf_release(updgrp
->conf
, updgrp
->afi
, updgrp
->safi
);
869 XFREE(MTYPE_BGP_PEER_HOST
, updgrp
->conf
->host
);
871 XFREE(MTYPE_BGP_PEER_IFNAME
, updgrp
->conf
->ifname
);
873 XFREE(MTYPE_BGP_PEER
, updgrp
->conf
);
874 XFREE(MTYPE_BGP_UPDGRP
, updgrp
);
877 static void update_group_add_subgroup(struct update_group
*updgrp
,
878 struct update_subgroup
*subgrp
)
880 if (!updgrp
|| !subgrp
)
883 LIST_INSERT_HEAD(&(updgrp
->subgrps
), subgrp
, updgrp_train
);
884 subgrp
->update_group
= updgrp
;
887 static void update_group_remove_subgroup(struct update_group
*updgrp
,
888 struct update_subgroup
*subgrp
)
890 if (!updgrp
|| !subgrp
)
893 LIST_REMOVE(subgrp
, updgrp_train
);
894 subgrp
->update_group
= NULL
;
895 if (LIST_EMPTY(&(updgrp
->subgrps
)))
896 update_group_delete(updgrp
);
899 static struct update_subgroup
*
900 update_subgroup_create(struct update_group
*updgrp
)
902 struct update_subgroup
*subgrp
;
904 subgrp
= XCALLOC(MTYPE_BGP_UPD_SUBGRP
, sizeof(struct update_subgroup
));
905 update_subgroup_checkin(subgrp
, updgrp
);
906 subgrp
->v_coalesce
= (UPDGRP_INST(updgrp
))->coalesce_time
;
907 sync_init(subgrp
, updgrp
);
908 bpacket_queue_init(SUBGRP_PKTQ(subgrp
));
909 bpacket_queue_add(SUBGRP_PKTQ(subgrp
), NULL
, NULL
);
910 TAILQ_INIT(&(subgrp
->adjq
));
911 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
912 zlog_debug("create subgroup u%" PRIu64
":s%" PRIu64
, updgrp
->id
,
915 update_group_add_subgroup(updgrp
, subgrp
);
917 UPDGRP_INCR_STAT(updgrp
, subgrps_created
);
922 static void update_subgroup_delete(struct update_subgroup
*subgrp
)
927 if (subgrp
->update_group
)
928 UPDGRP_INCR_STAT(subgrp
->update_group
, subgrps_deleted
);
930 THREAD_OFF(subgrp
->t_merge_check
);
931 THREAD_OFF(subgrp
->t_coalesce
);
933 bpacket_queue_cleanup(SUBGRP_PKTQ(subgrp
));
934 subgroup_clear_table(subgrp
);
938 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
) && subgrp
->update_group
)
939 zlog_debug("delete subgroup u%" PRIu64
":s%" PRIu64
,
940 subgrp
->update_group
->id
, subgrp
->id
);
942 update_group_remove_subgroup(subgrp
->update_group
, subgrp
);
944 XFREE(MTYPE_BGP_UPD_SUBGRP
, subgrp
);
947 void update_subgroup_inherit_info(struct update_subgroup
*to
,
948 struct update_subgroup
*from
)
953 to
->sflags
= from
->sflags
;
957 * update_subgroup_check_delete
959 * Delete a subgroup if it is ready to be deleted.
961 * Returns true if the subgroup was deleted.
963 static bool update_subgroup_check_delete(struct update_subgroup
*subgrp
)
968 if (!LIST_EMPTY(&(subgrp
->peers
)))
971 update_subgroup_delete(subgrp
);
977 * update_subgroup_add_peer
979 * @param send_enqueued_packets If true all currently enqueued packets will
980 * also be sent to the peer.
982 static void update_subgroup_add_peer(struct update_subgroup
*subgrp
,
984 int send_enqueued_pkts
)
991 LIST_INSERT_HEAD(&(subgrp
->peers
), paf
, subgrp_train
);
992 paf
->subgroup
= subgrp
;
993 subgrp
->peer_count
++;
995 if (bgp_debug_peer_updout_enabled(paf
->peer
->host
)) {
996 UPDGRP_PEER_DBG_EN(subgrp
->update_group
);
999 SUBGRP_INCR_STAT(subgrp
, join_events
);
1001 if (send_enqueued_pkts
) {
1002 pkt
= bpacket_queue_first(SUBGRP_PKTQ(subgrp
));
1006 * Hang the peer off of the last, placeholder, packet in the
1007 * queue. This means it won't see any of the packets that are
1008 * currently the queue.
1010 pkt
= bpacket_queue_last(SUBGRP_PKTQ(subgrp
));
1011 assert(pkt
->buffer
== NULL
);
1014 bpacket_add_peer(pkt
, paf
);
1016 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1017 zlog_debug("peer %s added to subgroup s%" PRIu64
,
1018 paf
->peer
->host
, subgrp
->id
);
1022 * update_subgroup_remove_peer_internal
1024 * Internal function that removes a peer from a subgroup, but does not
1025 * delete the subgroup. A call to this function must almost always be
1026 * followed by a call to update_subgroup_check_delete().
1028 * @see update_subgroup_remove_peer
1030 static void update_subgroup_remove_peer_internal(struct update_subgroup
*subgrp
,
1031 struct peer_af
*paf
)
1033 assert(subgrp
&& paf
&& subgrp
->update_group
);
1035 if (bgp_debug_peer_updout_enabled(paf
->peer
->host
)) {
1036 UPDGRP_PEER_DBG_DIS(subgrp
->update_group
);
1039 bpacket_queue_remove_peer(paf
);
1040 LIST_REMOVE(paf
, subgrp_train
);
1041 paf
->subgroup
= NULL
;
1042 subgrp
->peer_count
--;
1044 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1045 zlog_debug("peer %s deleted from subgroup s%"
1046 PRIu64
" peer cnt %d",
1047 paf
->peer
->host
, subgrp
->id
, subgrp
->peer_count
);
1048 SUBGRP_INCR_STAT(subgrp
, prune_events
);
1052 * update_subgroup_remove_peer
1054 void update_subgroup_remove_peer(struct update_subgroup
*subgrp
,
1055 struct peer_af
*paf
)
1057 if (!subgrp
|| !paf
)
1060 update_subgroup_remove_peer_internal(subgrp
, paf
);
1062 if (update_subgroup_check_delete(subgrp
))
1066 * The deletion of the peer may have caused some packets to be
1067 * deleted from the subgroup packet queue. Check if the subgroup can
1070 update_subgroup_check_merge(subgrp
, "removed peer from subgroup");
1073 static struct update_subgroup
*update_subgroup_find(struct update_group
*updgrp
,
1074 struct peer_af
*paf
)
1076 struct update_subgroup
*subgrp
= NULL
;
1079 if (paf
->subgroup
) {
1085 if (!peer_established(PAF_PEER(paf
)))
1088 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
1089 if (subgrp
->version
!= version
1090 || CHECK_FLAG(subgrp
->sflags
,
1091 SUBGRP_STATUS_DEFAULT_ORIGINATE
))
1095 * The version number is not meaningful on a subgroup that needs
1098 if (update_subgroup_needs_refresh(subgrp
))
1108 * update_subgroup_ready_for_merge
1110 * Returns true if this subgroup is in a state that allows it to be
1111 * merged into another subgroup.
1113 static bool update_subgroup_ready_for_merge(struct update_subgroup
*subgrp
)
1117 * Not ready if there are any encoded packets waiting to be written
1120 if (!bpacket_queue_is_empty(SUBGRP_PKTQ(subgrp
)))
1124 * Not ready if there enqueued updates waiting to be encoded.
1126 if (!advertise_list_is_empty(subgrp
))
1130 * Don't attempt to merge a subgroup that needs a refresh. For one,
1131 * we can't determine if the adj_out of such a group matches that of
1134 if (update_subgroup_needs_refresh(subgrp
))
1141 * update_subgrp_can_merge_into
1143 * Returns true if the first subgroup can merge into the second
1146 static int update_subgroup_can_merge_into(struct update_subgroup
*subgrp
,
1147 struct update_subgroup
*target
)
1150 if (subgrp
== target
)
1154 * Both must have processed the BRIB to the same point in order to
1157 if (subgrp
->version
!= target
->version
)
1160 if (CHECK_FLAG(subgrp
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
)
1161 != CHECK_FLAG(target
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
))
1164 if (subgrp
->adj_count
!= target
->adj_count
)
1167 return update_subgroup_ready_for_merge(target
);
1171 * update_subgroup_merge
1173 * Merge the first subgroup into the second one.
1175 static void update_subgroup_merge(struct update_subgroup
*subgrp
,
1176 struct update_subgroup
*target
,
1179 struct peer_af
*paf
;
1183 assert(subgrp
->adj_count
== target
->adj_count
);
1185 peer_count
= subgrp
->peer_count
;
1188 paf
= LIST_FIRST(&subgrp
->peers
);
1192 update_subgroup_remove_peer_internal(subgrp
, paf
);
1195 * Add the peer to the target subgroup, while making sure that
1196 * any currently enqueued packets won't be sent to it. Enqueued
1197 * packets could, for example, result in an unnecessary withdraw
1198 * followed by an advertise.
1200 update_subgroup_add_peer(target
, paf
, 0);
1203 SUBGRP_INCR_STAT(target
, merge_events
);
1205 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1206 zlog_debug("u%" PRIu64
":s%" PRIu64
" (%d peers) merged into u%" PRIu64
":s%" PRIu64
", trigger: %s",
1207 subgrp
->update_group
->id
, subgrp
->id
, peer_count
,
1208 target
->update_group
->id
, target
->id
,
1209 reason
? reason
: "unknown");
1211 result
= update_subgroup_check_delete(subgrp
);
1216 * update_subgroup_check_merge
1218 * Merge this subgroup into another subgroup if possible.
1220 * Returns true if the subgroup has been merged. The subgroup pointer
1221 * should not be accessed in this case.
1223 bool update_subgroup_check_merge(struct update_subgroup
*subgrp
,
1226 struct update_subgroup
*target
;
1228 if (!update_subgroup_ready_for_merge(subgrp
))
1232 * Look for a subgroup to merge into.
1234 UPDGRP_FOREACH_SUBGRP (subgrp
->update_group
, target
) {
1235 if (update_subgroup_can_merge_into(subgrp
, target
))
1242 update_subgroup_merge(subgrp
, target
, reason
);
1247 * update_subgroup_merge_check_thread_cb
1249 static void update_subgroup_merge_check_thread_cb(struct thread
*thread
)
1251 struct update_subgroup
*subgrp
;
1253 subgrp
= THREAD_ARG(thread
);
1255 subgrp
->t_merge_check
= NULL
;
1257 update_subgroup_check_merge(subgrp
, "triggered merge check");
1261 * update_subgroup_trigger_merge_check
1263 * Triggers a call to update_subgroup_check_merge() on a clean context.
1265 * @param force If true, the merge check will be triggered even if the
1266 * subgroup doesn't currently look ready for a merge.
1268 * Returns true if a merge check will be performed shortly.
1270 bool update_subgroup_trigger_merge_check(struct update_subgroup
*subgrp
,
1273 if (subgrp
->t_merge_check
)
1276 if (!force
&& !update_subgroup_ready_for_merge(subgrp
))
1279 subgrp
->t_merge_check
= NULL
;
1280 thread_add_timer_msec(bm
->master
, update_subgroup_merge_check_thread_cb
,
1281 subgrp
, 0, &subgrp
->t_merge_check
);
1283 SUBGRP_INCR_STAT(subgrp
, merge_checks_triggered
);
1289 * update_subgroup_copy_adj_out
1291 * Helper function that clones the adj out (state about advertised
1292 * routes) from one subgroup to another. It assumes that the adj out
1293 * of the target subgroup is empty.
1295 static void update_subgroup_copy_adj_out(struct update_subgroup
*source
,
1296 struct update_subgroup
*dest
)
1298 struct bgp_adj_out
*aout
, *aout_copy
;
1300 SUBGRP_FOREACH_ADJ (source
, aout
) {
1304 aout_copy
= bgp_adj_out_alloc(dest
, aout
->dest
,
1305 aout
->addpath_tx_id
);
1307 aout
->attr
? bgp_attr_intern(aout
->attr
) : NULL
;
1310 dest
->scount
= source
->scount
;
1314 * update_subgroup_copy_packets
1316 * Copy packets after and including the given packet to the subgroup
1319 * Returns the number of packets copied.
1321 static int update_subgroup_copy_packets(struct update_subgroup
*dest
,
1322 struct bpacket
*pkt
)
1327 while (pkt
&& pkt
->buffer
) {
1328 bpacket_queue_add(SUBGRP_PKTQ(dest
), stream_dup(pkt
->buffer
),
1331 pkt
= bpacket_next(pkt
);
1337 static bool updgrp_prefix_list_update(struct update_group
*updgrp
,
1341 struct bgp_filter
*filter
;
1343 peer
= UPDGRP_PEER(updgrp
);
1344 filter
= &peer
->filter
[UPDGRP_AFI(updgrp
)][UPDGRP_SAFI(updgrp
)];
1346 if (PREFIX_LIST_OUT_NAME(filter
)
1347 && (strcmp(name
, PREFIX_LIST_OUT_NAME(filter
)) == 0)) {
1348 PREFIX_LIST_OUT(filter
) = prefix_list_lookup(
1349 UPDGRP_AFI(updgrp
), PREFIX_LIST_OUT_NAME(filter
));
1355 static bool updgrp_filter_list_update(struct update_group
*updgrp
,
1359 struct bgp_filter
*filter
;
1361 peer
= UPDGRP_PEER(updgrp
);
1362 filter
= &peer
->filter
[UPDGRP_AFI(updgrp
)][UPDGRP_SAFI(updgrp
)];
1364 if (FILTER_LIST_OUT_NAME(filter
)
1365 && (strcmp(name
, FILTER_LIST_OUT_NAME(filter
)) == 0)) {
1366 FILTER_LIST_OUT(filter
) =
1367 as_list_lookup(FILTER_LIST_OUT_NAME(filter
));
1373 static bool updgrp_distribute_list_update(struct update_group
*updgrp
,
1377 struct bgp_filter
*filter
;
1379 peer
= UPDGRP_PEER(updgrp
);
1380 filter
= &peer
->filter
[UPDGRP_AFI(updgrp
)][UPDGRP_SAFI(updgrp
)];
1382 if (DISTRIBUTE_OUT_NAME(filter
)
1383 && (strcmp(name
, DISTRIBUTE_OUT_NAME(filter
)) == 0)) {
1384 DISTRIBUTE_OUT(filter
) = access_list_lookup(
1385 UPDGRP_AFI(updgrp
), DISTRIBUTE_OUT_NAME(filter
));
1391 static int updgrp_route_map_update(struct update_group
*updgrp
,
1392 const char *name
, int *def_rmap_changed
)
1395 struct bgp_filter
*filter
;
1400 peer
= UPDGRP_PEER(updgrp
);
1401 afi
= UPDGRP_AFI(updgrp
);
1402 safi
= UPDGRP_SAFI(updgrp
);
1403 filter
= &peer
->filter
[afi
][safi
];
1405 if (ROUTE_MAP_OUT_NAME(filter
)
1406 && (strcmp(name
, ROUTE_MAP_OUT_NAME(filter
)) == 0)) {
1407 ROUTE_MAP_OUT(filter
) = route_map_lookup_by_name(name
);
1412 if (UNSUPPRESS_MAP_NAME(filter
)
1413 && (strcmp(name
, UNSUPPRESS_MAP_NAME(filter
)) == 0)) {
1414 UNSUPPRESS_MAP(filter
) = route_map_lookup_by_name(name
);
1418 /* process default-originate route-map */
1419 if (peer
->default_rmap
[afi
][safi
].name
1420 && (strcmp(name
, peer
->default_rmap
[afi
][safi
].name
) == 0)) {
1421 peer
->default_rmap
[afi
][safi
].map
=
1422 route_map_lookup_by_name(name
);
1423 if (def_rmap_changed
)
1424 *def_rmap_changed
= 1;
1430 * hash iteration callback function to process a policy change for an
1431 * update group. Check if the changed policy matches the updgrp's
1432 * outbound route-map or unsuppress-map or default-originate map or
1433 * filter-list or prefix-list or distribute-list.
1434 * Trigger update generation accordingly.
1436 static int updgrp_policy_update_walkcb(struct update_group
*updgrp
, void *arg
)
1438 struct updwalk_context
*ctx
= arg
;
1439 struct update_subgroup
*subgrp
;
1441 int def_changed
= 0;
1443 if (!updgrp
|| !ctx
|| !ctx
->policy_name
)
1444 return UPDWALK_CONTINUE
;
1446 switch (ctx
->policy_type
) {
1447 case BGP_POLICY_ROUTE_MAP
:
1448 changed
= updgrp_route_map_update(updgrp
, ctx
->policy_name
,
1451 case BGP_POLICY_FILTER_LIST
:
1452 changed
= updgrp_filter_list_update(updgrp
, ctx
->policy_name
);
1454 case BGP_POLICY_PREFIX_LIST
:
1455 changed
= updgrp_prefix_list_update(updgrp
, ctx
->policy_name
);
1457 case BGP_POLICY_DISTRIBUTE_LIST
:
1459 updgrp_distribute_list_update(updgrp
, ctx
->policy_name
);
1465 /* If not doing route update, return after updating "config" */
1466 if (!ctx
->policy_route_update
)
1467 return UPDWALK_CONTINUE
;
1469 /* If nothing has changed, return after updating "config" */
1470 if (!changed
&& !def_changed
)
1471 return UPDWALK_CONTINUE
;
1474 * If something has changed, at the beginning of a route-map
1476 * event, mark each subgroup's needs-refresh bit. For one, it signals to
1477 * whoever that the subgroup needs a refresh. Second, it prevents
1479 * merge of this subgroup with another before a complete (outbound)
1482 if (ctx
->policy_event_start_flag
) {
1483 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
1484 update_subgroup_set_needs_refresh(subgrp
, 1);
1486 return UPDWALK_CONTINUE
;
1489 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
1490 /* Avoid supressing duplicate routes later
1491 * when processing in subgroup_announce_table().
1493 SET_FLAG(subgrp
->sflags
, SUBGRP_STATUS_FORCE_UPDATES
);
1496 if (bgp_debug_update(NULL
, NULL
, updgrp
, 0))
1498 "u%" PRIu64
":s%" PRIu64
" announcing routes upon policy %s (type %d) change",
1499 updgrp
->id
, subgrp
->id
,
1500 ctx
->policy_name
, ctx
->policy_type
);
1501 subgroup_announce_route(subgrp
);
1504 if (bgp_debug_update(NULL
, NULL
, updgrp
, 0))
1506 "u%" PRIu64
":s%" PRIu64
" announcing default upon default routemap %s change",
1507 updgrp
->id
, subgrp
->id
,
1509 if (route_map_lookup_by_name(ctx
->policy_name
)) {
1511 * When there is change in routemap, this flow
1512 * is triggered. the routemap is still present
1513 * in lib, hence its a update flow. The flag
1514 * needs to be unset.
1516 UNSET_FLAG(subgrp
->sflags
,
1517 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
1518 subgroup_default_originate(subgrp
, 0);
1521 * This is a explicit withdraw, since the
1522 * routemap is not present in routemap lib. need
1523 * to pass 1 for withdraw arg.
1525 subgroup_default_originate(subgrp
, 1);
1528 update_subgroup_set_needs_refresh(subgrp
, 0);
1530 return UPDWALK_CONTINUE
;
1533 static int update_group_walkcb(struct hash_bucket
*bucket
, void *arg
)
1535 struct update_group
*updgrp
= bucket
->data
;
1536 struct updwalk_context
*wctx
= arg
;
1537 int ret
= (*wctx
->cb
)(updgrp
, wctx
->context
);
1541 static int update_group_periodic_merge_walkcb(struct update_group
*updgrp
,
1544 struct update_subgroup
*subgrp
;
1545 struct update_subgroup
*tmp_subgrp
;
1546 const char *reason
= arg
;
1548 UPDGRP_FOREACH_SUBGRP_SAFE (updgrp
, subgrp
, tmp_subgrp
)
1549 update_subgroup_check_merge(subgrp
, reason
);
1550 return UPDWALK_CONTINUE
;
1553 /********************
1555 ********************/
1558 * trigger function when a policy (route-map/filter-list/prefix-list/
1559 * distribute-list etc.) content changes. Go through all the
1560 * update groups and process the change.
1562 * bgp: the bgp instance
1563 * ptype: the type of policy that got modified, see bgpd.h
1564 * pname: name of the policy
1565 * route_update: flag to control if an automatic update generation should
1567 * start_event: flag that indicates if it's the beginning of the change.
1568 * Esp. when the user is changing the content interactively
1569 * over multiple statements. Useful to set dirty flag on
1572 void update_group_policy_update(struct bgp
*bgp
, enum bgp_policy_type ptype
,
1573 const char *pname
, bool route_update
,
1576 struct updwalk_context ctx
;
1578 memset(&ctx
, 0, sizeof(ctx
));
1579 ctx
.policy_type
= ptype
;
1580 ctx
.policy_name
= pname
;
1581 ctx
.policy_route_update
= route_update
;
1582 ctx
.policy_event_start_flag
= start_event
;
1585 update_group_walk(bgp
, updgrp_policy_update_walkcb
, &ctx
);
1589 * update_subgroup_split_peer
1591 * Ensure that the given peer is in a subgroup of its own in the
1592 * specified update group.
1594 void update_subgroup_split_peer(struct peer_af
*paf
,
1595 struct update_group
*updgrp
)
1597 struct update_subgroup
*old_subgrp
, *subgrp
;
1601 old_subgrp
= paf
->subgroup
;
1604 updgrp
= old_subgrp
->update_group
;
1607 * If the peer is alone in its subgroup, reuse the existing
1610 if (old_subgrp
->peer_count
== 1) {
1611 if (updgrp
== old_subgrp
->update_group
)
1614 subgrp
= old_subgrp
;
1615 old_id
= old_subgrp
->update_group
->id
;
1617 if (bgp_debug_peer_updout_enabled(paf
->peer
->host
)) {
1618 UPDGRP_PEER_DBG_DIS(old_subgrp
->update_group
);
1621 update_group_remove_subgroup(old_subgrp
->update_group
,
1623 update_group_add_subgroup(updgrp
, subgrp
);
1625 if (bgp_debug_peer_updout_enabled(paf
->peer
->host
)) {
1626 UPDGRP_PEER_DBG_EN(updgrp
);
1628 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1629 zlog_debug("u%" PRIu64
":s%" PRIu64
" peer %s moved to u%" PRIu64
":s%" PRIu64
,
1630 old_id
, subgrp
->id
, paf
->peer
->host
,
1631 updgrp
->id
, subgrp
->id
);
1634 * The state of the subgroup (adj_out, advs, packet queue etc)
1635 * is consistent internally, but may not be identical to other
1636 * subgroups in the new update group even if the version number
1637 * matches up. Make sure a full refresh is done before the
1638 * subgroup is merged with another.
1640 update_subgroup_set_needs_refresh(subgrp
, 1);
1642 SUBGRP_INCR_STAT(subgrp
, updgrp_switch_events
);
1647 * Create a new subgroup under the specified update group, and copy
1648 * over relevant state to it.
1650 subgrp
= update_subgroup_create(updgrp
);
1651 update_subgroup_inherit_info(subgrp
, old_subgrp
);
1653 subgrp
->split_from
.update_group_id
= old_subgrp
->update_group
->id
;
1654 subgrp
->split_from
.subgroup_id
= old_subgrp
->id
;
1657 * Copy out relevant state from the old subgroup.
1659 update_subgroup_copy_adj_out(paf
->subgroup
, subgrp
);
1660 update_subgroup_copy_packets(subgrp
, paf
->next_pkt_to_send
);
1662 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1663 zlog_debug("u%" PRIu64
":s%" PRIu64
" peer %s split and moved into u%" PRIu64
":s%" PRIu64
,
1664 paf
->subgroup
->update_group
->id
, paf
->subgroup
->id
,
1665 paf
->peer
->host
, updgrp
->id
, subgrp
->id
);
1667 SUBGRP_INCR_STAT(paf
->subgroup
, split_events
);
1670 * Since queued advs were left behind, this new subgroup needs a
1673 update_subgroup_set_needs_refresh(subgrp
, 1);
1676 * Remove peer from old subgroup, and add it to the new one.
1678 update_subgroup_remove_peer(paf
->subgroup
, paf
);
1680 update_subgroup_add_peer(subgrp
, paf
, 1);
1683 void update_bgp_group_init(struct bgp
*bgp
)
1688 bgp
->update_groups
[afid
] =
1689 hash_create(updgrp_hash_key_make
, updgrp_hash_cmp
,
1690 "BGP Update Group Hash");
1693 void update_bgp_group_free(struct bgp
*bgp
)
1698 if (bgp
->update_groups
[afid
]) {
1699 hash_free(bgp
->update_groups
[afid
]);
1700 bgp
->update_groups
[afid
] = NULL
;
1705 void update_group_show(struct bgp
*bgp
, afi_t afi
, safi_t safi
, struct vty
*vty
,
1708 struct updwalk_context ctx
;
1709 memset(&ctx
, 0, sizeof(ctx
));
1711 ctx
.subgrp_id
= subgrp_id
;
1713 update_group_af_walk(bgp
, afi
, safi
, update_group_show_walkcb
, &ctx
);
1717 * update_group_show_stats
1719 * Show global statistics about update groups.
1721 void update_group_show_stats(struct bgp
*bgp
, struct vty
*vty
)
1723 vty_out(vty
, "Update groups created: %u\n",
1724 bgp
->update_group_stats
.updgrps_created
);
1725 vty_out(vty
, "Update groups deleted: %u\n",
1726 bgp
->update_group_stats
.updgrps_deleted
);
1727 vty_out(vty
, "Update subgroups created: %u\n",
1728 bgp
->update_group_stats
.subgrps_created
);
1729 vty_out(vty
, "Update subgroups deleted: %u\n",
1730 bgp
->update_group_stats
.subgrps_deleted
);
1731 vty_out(vty
, "Join events: %u\n", bgp
->update_group_stats
.join_events
);
1732 vty_out(vty
, "Prune events: %u\n",
1733 bgp
->update_group_stats
.prune_events
);
1734 vty_out(vty
, "Merge events: %u\n",
1735 bgp
->update_group_stats
.merge_events
);
1736 vty_out(vty
, "Split events: %u\n",
1737 bgp
->update_group_stats
.split_events
);
1738 vty_out(vty
, "Update group switch events: %u\n",
1739 bgp
->update_group_stats
.updgrp_switch_events
);
1740 vty_out(vty
, "Peer route refreshes combined: %u\n",
1741 bgp
->update_group_stats
.peer_refreshes_combined
);
1742 vty_out(vty
, "Merge checks triggered: %u\n",
1743 bgp
->update_group_stats
.merge_checks_triggered
);
1747 * update_group_adjust_peer
1749 void update_group_adjust_peer(struct peer_af
*paf
)
1751 struct update_group
*updgrp
;
1752 struct update_subgroup
*subgrp
, *old_subgrp
;
1758 peer
= PAF_PEER(paf
);
1759 if (!peer_established(peer
)) {
1763 if (!CHECK_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
)) {
1767 if (!peer
->afc_nego
[paf
->afi
][paf
->safi
]) {
1771 updgrp
= update_group_find(paf
);
1773 updgrp
= update_group_create(paf
);
1775 flog_err(EC_BGP_UPDGRP_CREATE
,
1776 "couldn't create update group for peer %s",
1782 old_subgrp
= paf
->subgroup
;
1787 * If the update group of the peer is unchanged, the peer can
1789 * in its existing subgroup and we're done.
1791 if (old_subgrp
->update_group
== updgrp
)
1795 * The peer is switching between update groups. Put it in its
1796 * own subgroup under the new update group.
1798 update_subgroup_split_peer(paf
, updgrp
);
1802 subgrp
= update_subgroup_find(updgrp
, paf
);
1804 subgrp
= update_subgroup_create(updgrp
);
1809 update_subgroup_add_peer(subgrp
, paf
, 1);
1810 if (BGP_DEBUG(update_groups
, UPDATE_GROUPS
))
1811 zlog_debug("u%" PRIu64
":s%" PRIu64
" add peer %s", updgrp
->id
,
1812 subgrp
->id
, paf
->peer
->host
);
1817 int update_group_adjust_soloness(struct peer
*peer
, int set
)
1819 struct peer_group
*group
;
1820 struct listnode
*node
, *nnode
;
1822 if (!CHECK_FLAG(peer
->sflags
, PEER_STATUS_GROUP
)) {
1823 peer_lonesoul_or_not(peer
, set
);
1824 if (peer_established(peer
))
1825 bgp_announce_route_all(peer
);
1827 group
= peer
->group
;
1828 for (ALL_LIST_ELEMENTS(group
->peer
, node
, nnode
, peer
)) {
1829 peer_lonesoul_or_not(peer
, set
);
1830 if (peer_established(peer
))
1831 bgp_announce_route_all(peer
);
1838 * update_subgroup_rib
1840 struct bgp_table
*update_subgroup_rib(struct update_subgroup
*subgrp
)
1844 bgp
= SUBGRP_INST(subgrp
);
1848 return bgp
->rib
[SUBGRP_AFI(subgrp
)][SUBGRP_SAFI(subgrp
)];
1851 void update_group_af_walk(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1852 updgrp_walkcb cb
, void *ctx
)
1854 struct updwalk_context wctx
;
1859 afid
= afindex(afi
, safi
);
1860 if (afid
>= BGP_AF_MAX
)
1863 memset(&wctx
, 0, sizeof(wctx
));
1867 if (bgp
->update_groups
[afid
])
1868 hash_walk(bgp
->update_groups
[afid
], update_group_walkcb
, &wctx
);
1871 void update_group_walk(struct bgp
*bgp
, updgrp_walkcb cb
, void *ctx
)
1876 FOREACH_AFI_SAFI (afi
, safi
) {
1877 update_group_af_walk(bgp
, afi
, safi
, cb
, ctx
);
1881 void update_group_periodic_merge(struct bgp
*bgp
)
1883 char reason
[] = "periodic merge check";
1885 update_group_walk(bgp
, update_group_periodic_merge_walkcb
,
1890 update_group_default_originate_route_map_walkcb(struct update_group
*updgrp
,
1893 struct update_subgroup
*subgrp
;
1898 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
) {
1899 peer
= SUBGRP_PEER(subgrp
);
1900 afi
= SUBGRP_AFI(subgrp
);
1901 safi
= SUBGRP_SAFI(subgrp
);
1903 if (peer
->default_rmap
[afi
][safi
].name
) {
1905 * When there is change in routemap this flow will
1906 * be triggered. We need to unset the Flag to ensure
1907 * the update flow gets triggered.
1909 UNSET_FLAG(subgrp
->sflags
,
1910 SUBGRP_STATUS_DEFAULT_ORIGINATE
);
1911 subgroup_default_originate(subgrp
, 0);
1915 return UPDWALK_CONTINUE
;
1918 void update_group_refresh_default_originate_route_map(struct thread
*thread
)
1921 char reason
[] = "refresh default-originate route-map";
1923 bgp
= THREAD_ARG(thread
);
1924 update_group_walk(bgp
, update_group_default_originate_route_map_walkcb
,
1926 THREAD_OFF(bgp
->t_rmap_def_originate_eval
);
1931 * peer_af_announce_route
1933 * Refreshes routes out to a peer_af immediately.
1935 * If the combine parameter is true, then this function will try to
1936 * gather other peers in the subgroup for which a route announcement
1937 * is pending and efficently announce routes to all of them.
1939 * For now, the 'combine' option has an effect only if all peers in
1940 * the subgroup have a route announcement pending.
1942 void peer_af_announce_route(struct peer_af
*paf
, int combine
)
1944 struct update_subgroup
*subgrp
;
1945 struct peer_af
*cur_paf
;
1948 subgrp
= paf
->subgroup
;
1953 * If there are other peers in the old subgroup that also need
1954 * routes to be announced, pull them into the peer's new
1956 * Combine route announcement with other peers if possible.
1958 * For now, we combine only if all peers in the subgroup have an
1959 * announcement pending.
1963 SUBGRP_FOREACH_PEER (subgrp
, cur_paf
) {
1967 if (cur_paf
->t_announce_route
)
1975 * Announce to the peer alone if we were not asked to combine peers,
1976 * or if some peers don't have a route annoucement pending.
1978 if (!combine
|| !all_pending
) {
1979 update_subgroup_split_peer(paf
, NULL
);
1980 subgrp
= paf
->subgroup
;
1982 assert(subgrp
&& subgrp
->update_group
);
1983 if (bgp_debug_update(paf
->peer
, NULL
, subgrp
->update_group
, 0))
1984 zlog_debug("u%" PRIu64
":s%" PRIu64
" %s announcing routes",
1985 subgrp
->update_group
->id
, subgrp
->id
,
1988 subgroup_announce_route(paf
->subgroup
);
1993 * We will announce routes the entire subgroup.
1995 * First stop refresh timers on all the other peers.
1997 SUBGRP_FOREACH_PEER (subgrp
, cur_paf
) {
2001 bgp_stop_announce_route_timer(cur_paf
);
2004 if (bgp_debug_update(paf
->peer
, NULL
, subgrp
->update_group
, 0))
2005 zlog_debug("u%" PRIu64
":s%" PRIu64
" announcing routes to %s, combined into %d peers",
2006 subgrp
->update_group
->id
, subgrp
->id
,
2007 paf
->peer
->host
, subgrp
->peer_count
);
2009 subgroup_announce_route(subgrp
);
2011 SUBGRP_INCR_STAT_BY(subgrp
, peer_refreshes_combined
,
2012 subgrp
->peer_count
- 1);
2015 void subgroup_trigger_write(struct update_subgroup
*subgrp
)
2017 struct peer_af
*paf
;
2020 * For each peer in the subgroup, schedule a job to pull packets from
2021 * the subgroup output queue into their own output queue. This action
2022 * will trigger a write job on the I/O thread.
2024 SUBGRP_FOREACH_PEER (subgrp
, paf
)
2025 if (peer_established(paf
->peer
))
2026 thread_add_timer_msec(
2027 bm
->master
, bgp_generate_updgrp_packets
,
2029 &paf
->peer
->t_generate_updgrp_packets
);
2032 int update_group_clear_update_dbg(struct update_group
*updgrp
, void *arg
)
2034 UPDGRP_PEER_DBG_OFF(updgrp
);
2035 return UPDWALK_CONTINUE
;
2038 /* Return true if we should addpath encode NLRI to this peer */
2039 bool bgp_addpath_encode_tx(struct peer
*peer
, afi_t afi
, safi_t safi
)
2041 return (CHECK_FLAG(peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_TX_ADV
)
2042 && CHECK_FLAG(peer
->af_cap
[afi
][safi
],
2043 PEER_CAP_ADDPATH_AF_RX_RCV
));
2046 bool bgp_check_selected(struct bgp_path_info
*bpi
, struct peer
*peer
,
2047 bool addpath_capable
, afi_t afi
, safi_t safi
)
2049 return (CHECK_FLAG(bpi
->flags
, BGP_PATH_SELECTED
) ||
2051 bgp_addpath_tx_path(peer
->addpath_type
[afi
][safi
], bpi
)));