1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * bgp_updgrp.c: BGP update group structures
5 * @copyright Copyright (C) 2014 Cumulus Networks, Inc.
7 * @author Avneesh Sachdev <avneesh@sproute.net>
8 * @author Rajesh Varadarajan <rajesh@sproute.net>
9 * @author Pradosh Mohapatra <pradosh@sproute.net>
12 #ifndef _QUAGGA_BGP_UPDGRP_H
13 #define _QUAGGA_BGP_UPDGRP_H
15 #include "bgp_advertise.h"
18 * The following three heuristic constants determine how long advertisement to
19 * a subgroup will be delayed after it is created. The intent is to allow
20 * transient changes in peer state (primarily session establishment) to settle,
21 * so that more peers can be grouped together and benefit from sharing
22 * advertisement computations with the subgroup.
24 * These values have a very large impact on initial convergence time; any
25 * changes should be accompanied by careful performance testing at all scales.
27 * The coalesce time 'C' for a new subgroup within a particular BGP instance
28 * 'B' with total number of known peers 'P', established or not, is computed as
31 * C = MIN(BGP_MAX_SUBGROUP_COALESCE_TIME,
32 * BGP_DEFAULT_SUBGROUP_COALESCE_TIME +
33 * (P*BGP_PEER_ADJUST_SUBGROUP_COALESCE_TIME))
35 #define BGP_DEFAULT_SUBGROUP_COALESCE_TIME 1000
36 #define BGP_MAX_SUBGROUP_COALESCE_TIME 10000
37 #define BGP_PEER_ADJUST_SUBGROUP_COALESCE_TIME 50
39 #define PEER_UPDGRP_FLAGS \
40 (PEER_FLAG_LOCAL_AS_NO_PREPEND | PEER_FLAG_LOCAL_AS_REPLACE_AS)
42 #define PEER_UPDGRP_AF_FLAGS \
43 (PEER_FLAG_SEND_COMMUNITY | PEER_FLAG_SEND_EXT_COMMUNITY \
44 | PEER_FLAG_SEND_LARGE_COMMUNITY \
45 | PEER_FLAG_DEFAULT_ORIGINATE | PEER_FLAG_REFLECTOR_CLIENT \
46 | PEER_FLAG_RSERVER_CLIENT | PEER_FLAG_NEXTHOP_SELF \
47 | PEER_FLAG_NEXTHOP_UNCHANGED | PEER_FLAG_FORCE_NEXTHOP_SELF \
48 | PEER_FLAG_AS_PATH_UNCHANGED | PEER_FLAG_MED_UNCHANGED \
49 | PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED | PEER_FLAG_REMOVE_PRIVATE_AS \
50 | PEER_FLAG_REMOVE_PRIVATE_AS_ALL \
51 | PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE \
52 | PEER_FLAG_REMOVE_PRIVATE_AS_ALL_REPLACE \
53 | PEER_FLAG_AS_OVERRIDE)
55 #define PEER_UPDGRP_CAP_FLAGS (PEER_CAP_AS4_RCV)
57 #define PEER_UPDGRP_AF_CAP_FLAGS \
58 (PEER_CAP_ORF_PREFIX_SM_RCV | PEER_CAP_ORF_PREFIX_SM_OLD_RCV \
59 | PEER_CAP_ADDPATH_AF_TX_ADV | PEER_CAP_ADDPATH_AF_RX_RCV \
60 | PEER_CAP_ENHE_AF_NEGO)
62 enum bpacket_attr_vec_type
{ BGP_ATTR_VEC_NH
= 0, BGP_ATTR_VEC_MAX
};
69 #define BPKT_ATTRVEC_FLAGS_UPDATED (1 << 0)
70 #define BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS (1 << 1)
71 #define BPKT_ATTRVEC_FLAGS_REFLECTED (1 << 2)
72 #define BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED (1 << 3)
73 #define BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED (1 << 4)
74 #define BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED (1 << 5)
75 #define BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED (1 << 6)
76 #define BPKT_ATTRVEC_FLAGS_RMAP_VPNV4_NH_CHANGED (1 << 7)
77 #define BPKT_ATTRVEC_FLAGS_RMAP_VPNV6_GNH_CHANGED (1 << 8)
79 typedef struct bpacket_attr_vec_arr
{
80 bpacket_attr_vec entries
[BGP_ATTR_VEC_MAX
];
81 } bpacket_attr_vec_arr
;
84 /* for being part of an update subgroup's message list */
85 TAILQ_ENTRY(bpacket
) pkt_train
;
87 /* list of peers (well, peer_afs) that the packet needs to be sent to */
88 LIST_HEAD(pkt_peer_list
, peer_af
) peers
;
90 struct stream
*buffer
;
91 bpacket_attr_vec_arr arr
;
96 struct bpacket_queue
{
97 TAILQ_HEAD(pkt_queue
, bpacket
) pkts
;
99 unsigned int conf_max_count
;
100 unsigned int curr_count
;
101 unsigned int hwm_count
;
102 unsigned int max_count_reached_count
;
105 struct update_group
{
106 /* back pointer to the BGP instance */
109 /* list of subgroups that belong to the update group */
110 LIST_HEAD(subgrp_list
, update_subgroup
) subgrps
;
112 /* lazy way to store configuration common to all peers
113 hash function will compute from this data */
123 uint32_t join_events
;
124 uint32_t prune_events
;
125 uint32_t merge_events
;
126 uint32_t updgrp_switch_events
;
127 uint32_t peer_refreshes_combined
;
129 uint32_t split_events
;
130 uint32_t merge_checks_triggered
;
132 uint32_t subgrps_created
;
133 uint32_t subgrps_deleted
;
135 uint32_t num_dbg_en_peers
;
139 * Shorthand for a global statistics counter.
141 #define UPDGRP_GLOBAL_STAT(updgrp, stat) \
142 ((updgrp)->bgp->update_group_stats.stat)
145 * Add the given value to a counter on an update group and the bgp
148 #define UPDGRP_INCR_STAT_BY(updgrp, stat, value) \
150 (updgrp)->stat += (value); \
151 UPDGRP_GLOBAL_STAT(updgrp, stat) += (value); \
155 * Increment a counter on a update group and its parent structures.
157 #define UPDGRP_INCR_STAT(subgrp, stat) UPDGRP_INCR_STAT_BY(subgrp, stat, 1)
159 struct update_subgroup
{
160 /* back pointer to the parent update group */
161 struct update_group
*update_group
;
163 /* list of peers that belong to the subgroup */
164 LIST_HEAD(peer_list
, peer_af
) peers
;
167 /* for being part of an update group's subgroup list */
168 LIST_ENTRY(update_subgroup
) updgrp_train
;
170 struct bpacket_queue pkt_queue
;
173 * List of adj-out structures for this subgroup.
174 * It essentially represents the snapshot of every prefix that
175 * has been advertised to the members of the subgroup
177 TAILQ_HEAD(adjout_queue
, bgp_adj_out
) adjq
;
179 /* packet buffer for update generation */
182 /* We use a separate stream to encode MP_REACH_NLRI for efficient
183 * NLRI packing. peer->obuf_work stores all the other attributes. The
184 * actual packet is then constructed by concatenating the two.
186 struct stream
*scratch
;
188 /* synchronization list and time */
189 struct bgp_synchronize
*sync
;
191 /* send prefix count */
194 /* send prefix count prior to packet update */
197 /* announcement attribute hash */
200 struct thread
*t_coalesce
;
203 struct thread
*t_merge_check
;
205 /* table version that the subgroup has caught up to. */
208 /* version maintained to record adj changes */
209 uint64_t adj_version
;
214 * Identifying information about the subgroup that this subgroup was
219 uint64_t update_group_id
;
220 uint64_t subgroup_id
;
223 uint32_t join_events
;
224 uint32_t prune_events
;
227 * This is bumped up when another subgroup merges into this one.
229 uint32_t merge_events
;
230 uint32_t updgrp_switch_events
;
231 uint32_t peer_refreshes_combined
;
233 uint32_t split_events
;
234 uint32_t merge_checks_triggered
;
239 #define SUBGRP_STATUS_DEFAULT_ORIGINATE (1 << 0)
240 #define SUBGRP_STATUS_FORCE_UPDATES (1 << 1)
241 #define SUBGRP_STATUS_TABLE_REPARSING (1 << 2)
243 * This flag has been added to ensure that the SNT counters
244 * gets incremented and decremented only during the creation
245 * and deletion workflows of default originate,
246 * not during the update workflow.
248 #define SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED (1 << 3)
251 #define SUBGRP_FLAG_NEEDS_REFRESH (1 << 0)
255 * Add the given value to the specified counter on a subgroup and its
258 #define SUBGRP_INCR_STAT_BY(subgrp, stat, value) \
260 (subgrp)->stat += (value); \
261 if ((subgrp)->update_group) \
262 UPDGRP_INCR_STAT_BY((subgrp)->update_group, stat, \
267 * Increment a counter on a subgroup and its parent structures.
269 #define SUBGRP_INCR_STAT(subgrp, stat) SUBGRP_INCR_STAT_BY(subgrp, stat, 1)
272 * Decrement a counter on a subgroup and its parent structures.
274 #define SUBGRP_DECR_STAT(subgrp, stat) SUBGRP_INCR_STAT_BY(subgrp, stat, -1)
276 typedef int (*updgrp_walkcb
)(struct update_group
*updgrp
, void *ctx
);
278 /* really a private structure */
279 struct updwalk_context
{
281 struct bgp_dest
*dest
;
282 struct bgp_path_info
*pi
;
285 enum bgp_policy_type policy_type
;
286 const char *policy_name
;
287 int policy_event_start_flag
;
288 bool policy_route_update
;
293 json_object
*json_updategrps
;
295 #define UPDWALK_FLAGS_ADVQUEUE (1 << 0)
296 #define UPDWALK_FLAGS_ADVERTISED (1 << 1)
299 #define UPDWALK_CONTINUE HASHWALK_CONTINUE
300 #define UPDWALK_ABORT HASHWALK_ABORT
302 #define PAF_PEER(p) ((p)->peer)
303 #define PAF_SUBGRP(p) ((p)->subgroup)
304 #define PAF_UPDGRP(p) ((p)->subgroup->update_group)
305 #define PAF_PKTQ(f) SUBGRP_PKTQ((f)->subgroup)
307 #define UPDGRP_PEER(u) ((u)->conf)
308 #define UPDGRP_AFI(u) ((u)->afi)
309 #define UPDGRP_SAFI(u) ((u)->safi)
310 #define UPDGRP_INST(u) ((u)->bgp)
311 #define UPDGRP_AFFLAGS(u) ((u)->conf->af_flags[UPDGRP_AFI(u)][UPDGRP_SAFI(u)])
312 #define UPDGRP_DBG_ON(u) ((u)->num_dbg_en_peers)
313 #define UPDGRP_PEER_DBG_EN(u) (((u)->num_dbg_en_peers)++)
314 #define UPDGRP_PEER_DBG_DIS(u) (((u)->num_dbg_en_peers)--)
315 #define UPDGRP_PEER_DBG_OFF(u) (u)->num_dbg_en_peers = 0
317 #define SUBGRP_AFI(s) UPDGRP_AFI((s)->update_group)
318 #define SUBGRP_SAFI(s) UPDGRP_SAFI((s)->update_group)
319 #define SUBGRP_PEER(s) UPDGRP_PEER((s)->update_group)
320 #define SUBGRP_PCOUNT(s) ((s)->peer_count)
321 #define SUBGRP_PFIRST(s) LIST_FIRST(&((s)->peers))
322 #define SUBGRP_PKTQ(s) &((s)->pkt_queue)
323 #define SUBGRP_INST(s) UPDGRP_INST((s)->update_group)
324 #define SUBGRP_AFFLAGS(s) UPDGRP_AFFLAGS((s)->update_group)
325 #define SUBGRP_UPDGRP(s) ((s)->update_group)
328 * Walk all subgroups in an update group.
330 #define UPDGRP_FOREACH_SUBGRP(updgrp, subgrp) \
331 LIST_FOREACH (subgrp, &((updgrp)->subgrps), updgrp_train)
333 #define UPDGRP_FOREACH_SUBGRP_SAFE(updgrp, subgrp, tmp_subgrp) \
334 LIST_FOREACH_SAFE (subgrp, &((updgrp)->subgrps), updgrp_train, \
337 #define SUBGRP_FOREACH_PEER(subgrp, paf) \
338 LIST_FOREACH (paf, &(subgrp->peers), subgrp_train)
340 #define SUBGRP_FOREACH_PEER_SAFE(subgrp, paf, temp_paf) \
341 LIST_FOREACH_SAFE (paf, &(subgrp->peers), subgrp_train, temp_paf)
343 #define SUBGRP_FOREACH_ADJ(subgrp, adj) \
344 TAILQ_FOREACH (adj, &(subgrp->adjq), subgrp_adj_train)
346 #define SUBGRP_FOREACH_ADJ_SAFE(subgrp, adj, adj_temp) \
347 TAILQ_FOREACH_SAFE (adj, &(subgrp->adjq), subgrp_adj_train, adj_temp)
351 extern void update_bgp_group_init(struct bgp
*);
352 extern void udpate_bgp_group_free(struct bgp
*);
354 extern void update_group_show(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
355 struct vty
*vty
, uint64_t subgrp_id
, bool uj
);
356 extern void update_group_show_stats(struct bgp
*bgp
, struct vty
*vty
);
357 extern void update_group_adjust_peer(struct peer_af
*paf
);
358 extern int update_group_adjust_soloness(struct peer
*peer
, int set
);
360 extern void update_subgroup_remove_peer(struct update_subgroup
*,
362 extern struct bgp_table
*update_subgroup_rib(struct update_subgroup
*);
363 extern void update_subgroup_split_peer(struct peer_af
*, struct update_group
*);
364 extern bool update_subgroup_check_merge(struct update_subgroup
*, const char *);
365 extern bool update_subgroup_trigger_merge_check(struct update_subgroup
*,
367 extern void update_group_policy_update(struct bgp
*bgp
,
368 enum bgp_policy_type ptype
,
369 const char *pname
, bool route_update
,
371 extern void update_group_af_walk(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
372 updgrp_walkcb cb
, void *ctx
);
373 extern void update_group_walk(struct bgp
*bgp
, updgrp_walkcb cb
, void *ctx
);
374 extern void update_group_periodic_merge(struct bgp
*bgp
);
376 update_group_refresh_default_originate_route_map(struct thread
*thread
);
377 extern void update_group_start_advtimer(struct bgp
*bgp
);
379 extern void update_subgroup_inherit_info(struct update_subgroup
*to
,
380 struct update_subgroup
*from
);
382 /* bgp_updgrp_packet.c */
383 extern struct bpacket
*bpacket_alloc(void);
384 extern void bpacket_free(struct bpacket
*pkt
);
385 extern void bpacket_queue_init(struct bpacket_queue
*q
);
386 extern void bpacket_queue_cleanup(struct bpacket_queue
*q
);
387 extern void bpacket_queue_sanity_check(struct bpacket_queue
*q
);
388 extern struct bpacket
*bpacket_queue_add(struct bpacket_queue
*q
,
390 struct bpacket_attr_vec_arr
*vecarr
);
391 struct bpacket
*bpacket_queue_remove(struct bpacket_queue
*q
);
392 extern struct bpacket
*bpacket_queue_first(struct bpacket_queue
*q
);
393 struct bpacket
*bpacket_queue_last(struct bpacket_queue
*q
);
394 unsigned int bpacket_queue_length(struct bpacket_queue
*q
);
395 unsigned int bpacket_queue_hwm_length(struct bpacket_queue
*q
);
396 bool bpacket_queue_is_full(struct bgp
*bgp
, struct bpacket_queue
*q
);
397 extern void bpacket_queue_advance_peer(struct peer_af
*paf
);
398 extern void bpacket_queue_remove_peer(struct peer_af
*paf
);
399 extern void bpacket_add_peer(struct bpacket
*pkt
, struct peer_af
*paf
);
400 unsigned int bpacket_queue_virtual_length(struct peer_af
*paf
);
401 extern void bpacket_queue_show_vty(struct bpacket_queue
*q
, struct vty
*vty
);
402 bool subgroup_packets_to_build(struct update_subgroup
*subgrp
);
403 extern struct bpacket
*subgroup_update_packet(struct update_subgroup
*s
);
404 extern struct bpacket
*subgroup_withdraw_packet(struct update_subgroup
*s
);
405 extern struct stream
*bpacket_reformat_for_peer(struct bpacket
*pkt
,
406 struct peer_af
*paf
);
407 extern void bpacket_attr_vec_arr_reset(struct bpacket_attr_vec_arr
*vecarr
);
408 extern void bpacket_attr_vec_arr_set_vec(struct bpacket_attr_vec_arr
*vecarr
,
409 enum bpacket_attr_vec_type type
,
410 struct stream
*s
, struct attr
*attr
);
411 extern void subgroup_default_update_packet(struct update_subgroup
*subgrp
,
414 extern void subgroup_default_withdraw_packet(struct update_subgroup
*subgrp
);
416 /* bgp_updgrp_adv.c */
417 extern struct bgp_advertise
*
418 bgp_advertise_clean_subgroup(struct update_subgroup
*subgrp
,
419 struct bgp_adj_out
*adj
);
420 extern void update_group_show_adj_queue(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
421 struct vty
*vty
, uint64_t id
);
422 extern void update_group_show_advertised(struct bgp
*bgp
, afi_t afi
,
423 safi_t safi
, struct vty
*vty
,
425 extern void update_group_show_packet_queue(struct bgp
*bgp
, afi_t afi
,
426 safi_t safi
, struct vty
*vty
,
428 extern void subgroup_announce_route(struct update_subgroup
*subgrp
);
429 extern void subgroup_announce_all(struct update_subgroup
*subgrp
);
431 extern void subgroup_default_originate(struct update_subgroup
*subgrp
,
433 extern void group_announce_route(struct bgp
*bgp
, afi_t afi
, safi_t safi
,
434 struct bgp_dest
*dest
,
435 struct bgp_path_info
*pi
);
436 extern void subgroup_clear_table(struct update_subgroup
*subgrp
);
437 extern void update_group_announce(struct bgp
*bgp
);
438 extern void update_group_announce_rrclients(struct bgp
*bgp
);
439 extern void peer_af_announce_route(struct peer_af
*paf
, int combine
);
440 extern struct bgp_adj_out
*bgp_adj_out_alloc(struct update_subgroup
*subgrp
,
441 struct bgp_dest
*dest
,
442 uint32_t addpath_tx_id
);
443 extern void bgp_adj_out_remove_subgroup(struct bgp_dest
*dest
,
444 struct bgp_adj_out
*adj
,
445 struct update_subgroup
*subgrp
);
446 extern void bgp_adj_out_set_subgroup(struct bgp_dest
*dest
,
447 struct update_subgroup
*subgrp
,
449 struct bgp_path_info
*path
);
450 extern void bgp_adj_out_unset_subgroup(struct bgp_dest
*dest
,
451 struct update_subgroup
*subgrp
,
452 char withdraw
, uint32_t addpath_tx_id
);
453 void subgroup_announce_table(struct update_subgroup
*subgrp
,
454 struct bgp_table
*table
);
455 extern void subgroup_trigger_write(struct update_subgroup
*subgrp
);
457 extern int update_group_clear_update_dbg(struct update_group
*updgrp
,
460 extern void update_bgp_group_free(struct bgp
*bgp
);
461 extern bool bgp_addpath_encode_tx(struct peer
*peer
, afi_t afi
, safi_t safi
);
462 extern bool bgp_check_selected(struct bgp_path_info
*bpi
, struct peer
*peer
,
463 bool addpath_capable
, afi_t afi
, safi_t safi
);
464 extern bool bgp_addpath_capable(struct bgp_path_info
*bpi
, struct peer
*peer
,
465 afi_t afi
, safi_t safi
);
472 * bpacket_queue_is_empty
474 static inline int bpacket_queue_is_empty(struct bpacket_queue
*queue
)
478 * The packet queue is empty if it only contains a sentinel.
480 if (queue
->curr_count
!= 1)
483 assert(bpacket_queue_first(queue
)->buffer
== NULL
);
490 * Returns the packet after the given packet in a bpacket queue.
492 static inline struct bpacket
*bpacket_next(struct bpacket
*pkt
)
494 return TAILQ_NEXT(pkt
, pkt_train
);
498 * update_group_adjust_peer_afs
500 * Adjust all peer_af structures for the given peer.
502 static inline void update_group_adjust_peer_afs(struct peer
*peer
)
507 for (afidx
= BGP_AF_START
; afidx
< BGP_AF_MAX
; afidx
++) {
508 paf
= peer
->peer_af_array
[afidx
];
510 update_group_adjust_peer(paf
);
515 * update_group_remove_peer_afs
517 * Remove all peer_af structures for the given peer from their subgroups.
519 static inline void update_group_remove_peer_afs(struct peer
*peer
)
524 for (afidx
= BGP_AF_START
; afidx
< BGP_AF_MAX
; afidx
++) {
525 paf
= peer
->peer_af_array
[afidx
];
527 update_subgroup_remove_peer(PAF_SUBGRP(paf
), paf
);
532 * update_subgroup_needs_refresh
535 update_subgroup_needs_refresh(const struct update_subgroup
*subgrp
)
537 if (CHECK_FLAG(subgrp
->flags
, SUBGRP_FLAG_NEEDS_REFRESH
))
544 * update_subgroup_set_needs_refresh
547 update_subgroup_set_needs_refresh(struct update_subgroup
*subgrp
, int value
)
550 SET_FLAG(subgrp
->flags
, SUBGRP_FLAG_NEEDS_REFRESH
);
552 UNSET_FLAG(subgrp
->flags
, SUBGRP_FLAG_NEEDS_REFRESH
);
555 static inline struct update_subgroup
*peer_subgroup(struct peer
*peer
,
556 afi_t afi
, safi_t safi
)
560 paf
= peer_af_find(peer
, afi
, safi
);
562 return PAF_SUBGRP(paf
);
567 * update_group_adjust_peer_afs
569 * Adjust all peer_af structures for the given peer.
571 static inline void bgp_announce_peer(struct peer
*peer
)
576 for (afidx
= BGP_AF_START
; afidx
< BGP_AF_MAX
; afidx
++) {
577 paf
= peer
->peer_af_array
[afidx
];
579 subgroup_announce_all(PAF_SUBGRP(paf
));
584 * advertise_list_is_empty
586 static inline int advertise_list_is_empty(struct update_subgroup
*subgrp
)
588 if (bgp_adv_fifo_count(&subgrp
->sync
->update
)
589 || bgp_adv_fifo_count(&subgrp
->sync
->withdraw
)
590 || bgp_adv_fifo_count(&subgrp
->sync
->withdraw_low
)) {
597 #endif /* _QUAGGA_BGP_UPDGRP_H */