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
23 * along with GNU Zebra; see the file COPYING. If not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
35 #include "sockunion.h"
43 #include "workqueue.h"
48 #include "bgpd/bgpd.h"
49 #include "bgpd/bgp_table.h"
50 #include "bgpd/bgp_debug.h"
51 #include "bgpd/bgp_fsm.h"
52 #include "bgpd/bgp_advertise.h"
53 #include "bgpd/bgp_packet.h"
54 #include "bgpd/bgp_updgrp.h"
55 #include "bgpd/bgp_route.h"
56 #include "bgpd/bgp_filter.h"
63 * assign a unique ID to update group and subgroup. Mostly for display/
64 * debugging purposes. It's a 64-bit space - used leisurely without a
65 * worry about its wrapping and about filling gaps. While at it, timestamp
69 update_group_checkin (struct update_group
*updgrp
)
71 updgrp
->id
= ++bm
->updgrp_idspace
;
72 updgrp
->uptime
= bgp_clock ();
76 update_subgroup_checkin (struct update_subgroup
*subgrp
,
77 struct update_group
*updgrp
)
79 subgrp
->id
= ++bm
->subgrp_idspace
;
80 subgrp
->uptime
= bgp_clock ();
84 sync_init (struct update_subgroup
*subgrp
)
86 subgrp
->sync
= XCALLOC (MTYPE_BGP_SYNCHRONISE
,
87 sizeof (struct bgp_synchronize
));
88 BGP_ADV_FIFO_INIT (&subgrp
->sync
->update
);
89 BGP_ADV_FIFO_INIT (&subgrp
->sync
->withdraw
);
90 BGP_ADV_FIFO_INIT (&subgrp
->sync
->withdraw_low
);
91 subgrp
->hash
= hash_create (baa_hash_key
, baa_hash_cmp
);
93 /* We use a larger buffer for subgrp->work in the event that:
94 * - We RX a BGP_UPDATE where the attributes alone are just
95 * under BGP_MAX_PACKET_SIZE
96 * - The user configures an outbound route-map that does many as-path
97 * prepends or adds many communities. At most they can have CMD_ARGC_MAX
98 * args in a route-map so there is a finite limit on how large they can
99 * make the attributes.
101 * Having a buffer with BGP_MAX_PACKET_SIZE_OVERFLOW allows us to avoid bounds
102 * checking for every single attribute as we construct an UPDATE.
104 subgrp
->work
= stream_new (BGP_MAX_PACKET_SIZE
+ BGP_MAX_PACKET_SIZE_OVERFLOW
);
105 subgrp
->scratch
= stream_new (BGP_MAX_PACKET_SIZE
);
109 sync_delete (struct update_subgroup
*subgrp
)
112 XFREE (MTYPE_BGP_SYNCHRONISE
, subgrp
->sync
);
115 hash_free (subgrp
->hash
);
118 stream_free (subgrp
->work
);
121 stream_free (subgrp
->scratch
);
122 subgrp
->scratch
= NULL
;
128 * copy only those fields that are relevant to update group match
131 conf_copy (struct peer
*dst
, struct peer
*src
, afi_t afi
, safi_t safi
)
133 struct bgp_filter
*srcfilter
;
134 struct bgp_filter
*dstfilter
;
136 srcfilter
= &src
->filter
[afi
][safi
];
137 dstfilter
= &dst
->filter
[afi
][safi
];
140 dst
->sort
= src
->sort
;
142 dst
->v_routeadv
= src
->v_routeadv
;
143 dst
->flags
= src
->flags
;
144 dst
->af_flags
[afi
][safi
] = src
->af_flags
[afi
][safi
];
146 XFREE(MTYPE_BGP_PEER_HOST
, dst
->host
);
148 dst
->host
= XSTRDUP(MTYPE_BGP_PEER_HOST
, src
->host
);
150 dst
->af_cap
[afi
][safi
] = src
->af_cap
[afi
][safi
];
151 dst
->afc_nego
[afi
][safi
] = src
->afc_nego
[afi
][safi
];
152 dst
->orf_plist
[afi
][safi
] = src
->orf_plist
[afi
][safi
];
153 dst
->local_as
= src
->local_as
;
154 dst
->change_local_as
= src
->change_local_as
;
155 dst
->shared_network
= src
->shared_network
;
156 memcpy (&(dst
->nexthop
), &(src
->nexthop
), sizeof (struct bgp_nexthop
));
158 dst
->group
= src
->group
;
160 if (src
->default_rmap
[afi
][safi
].name
)
162 dst
->default_rmap
[afi
][safi
].name
=
163 XSTRDUP(MTYPE_ROUTE_MAP_NAME
, src
->default_rmap
[afi
][safi
].name
);
164 dst
->default_rmap
[afi
][safi
].map
= src
->default_rmap
[afi
][safi
].map
;
167 if (DISTRIBUTE_OUT_NAME(srcfilter
))
169 DISTRIBUTE_OUT_NAME(dstfilter
) = XSTRDUP(MTYPE_BGP_FILTER_NAME
, DISTRIBUTE_OUT_NAME(srcfilter
));
170 DISTRIBUTE_OUT(dstfilter
) = DISTRIBUTE_OUT(srcfilter
);
173 if (PREFIX_LIST_OUT_NAME(srcfilter
))
175 PREFIX_LIST_OUT_NAME(dstfilter
) = XSTRDUP(MTYPE_BGP_FILTER_NAME
, PREFIX_LIST_OUT_NAME(srcfilter
));
176 PREFIX_LIST_OUT(dstfilter
) = PREFIX_LIST_OUT(srcfilter
);
179 if (FILTER_LIST_OUT_NAME(srcfilter
))
181 FILTER_LIST_OUT_NAME(dstfilter
) = XSTRDUP(MTYPE_BGP_FILTER_NAME
, FILTER_LIST_OUT_NAME(srcfilter
));
182 FILTER_LIST_OUT(dstfilter
) = FILTER_LIST_OUT(srcfilter
);
185 if (ROUTE_MAP_OUT_NAME(srcfilter
))
187 ROUTE_MAP_OUT_NAME(dstfilter
) = XSTRDUP(MTYPE_BGP_FILTER_NAME
, ROUTE_MAP_OUT_NAME(srcfilter
));
188 ROUTE_MAP_OUT(dstfilter
) = ROUTE_MAP_OUT(srcfilter
);
191 if (UNSUPPRESS_MAP_NAME(srcfilter
))
193 UNSUPPRESS_MAP_NAME(dstfilter
) = XSTRDUP(MTYPE_BGP_FILTER_NAME
, UNSUPPRESS_MAP_NAME(srcfilter
));
194 UNSUPPRESS_MAP(dstfilter
) = UNSUPPRESS_MAP(srcfilter
);
199 * since we did a bunch of XSTRDUP's in conf_copy, time to free them up
202 conf_release (struct peer
*src
, afi_t afi
, safi_t safi
)
204 struct bgp_filter
*srcfilter
;
206 srcfilter
= &src
->filter
[afi
][safi
];
208 if (src
->default_rmap
[afi
][safi
].name
)
209 XFREE(MTYPE_ROUTE_MAP_NAME
, src
->default_rmap
[afi
][safi
].name
);
211 if (srcfilter
->dlist
[FILTER_OUT
].name
)
212 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->dlist
[FILTER_OUT
].name
);
214 if (srcfilter
->plist
[FILTER_OUT
].name
)
215 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->plist
[FILTER_OUT
].name
);
217 if (srcfilter
->aslist
[FILTER_OUT
].name
)
218 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->aslist
[FILTER_OUT
].name
);
220 if (srcfilter
->map
[RMAP_OUT
].name
)
221 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->map
[RMAP_OUT
].name
);
223 if (srcfilter
->usmap
.name
)
224 XFREE(MTYPE_BGP_FILTER_NAME
, srcfilter
->usmap
.name
);
227 XFREE(MTYPE_BGP_PEER_HOST
, src
->host
);
232 peer2_updgrp_copy (struct update_group
*updgrp
, struct peer_af
*paf
)
245 updgrp
->afi
= paf
->afi
;
246 updgrp
->safi
= paf
->safi
;
247 updgrp
->afid
= paf
->afid
;
248 updgrp
->bgp
= src
->bgp
;
250 conf_copy (dst
, src
, paf
->afi
, paf
->safi
);
254 * auxiliary functions to maintain the hash table.
255 * - updgrp_hash_alloc - to create a new entry, passed to hash_get
256 * - updgrp_hash_key_make - makes the key for update group search
257 * - updgrp_hash_cmp - compare two update groups.
260 updgrp_hash_alloc (void *p
)
262 struct update_group
*updgrp
;
263 const struct update_group
*in
;
265 in
= (const struct update_group
*)p
;
266 updgrp
= XCALLOC (MTYPE_BGP_UPDGRP
, sizeof (struct update_group
));
267 memcpy (updgrp
, in
, sizeof (struct update_group
));
268 updgrp
->conf
= XCALLOC (MTYPE_BGP_PEER
, sizeof (struct peer
));
269 conf_copy (updgrp
->conf
, in
->conf
, in
->afi
, in
->safi
);
274 * The hash value for a peer is computed from the following variables:
276 * 1. IBGP (1) or EBGP (2)
277 * 2. FLAGS based on configuration:
278 * LOCAL_AS_NO_PREPEND
279 * LOCAL_AS_REPLACE_AS
280 * 3. AF_FLAGS based on configuration:
281 * Refer to definition in bgp_updgrp.h
282 * 4. (AF-independent) Capability flags:
284 * 5. (AF-dependent) Capability flags:
285 * ORF_PREFIX_SM_RCV (peer can send prefix ORF)
288 * 8. Outbound route-map name (neighbor route-map <> out)
289 * 9. Outbound distribute-list name (neighbor distribute-list <> out)
290 * 10. Outbound prefix-list name (neighbor prefix-list <> out)
291 * 11. Outbound as-list name (neighbor filter-list <> out)
292 * 12. Unsuppress map name (neighbor unsuppress-map <>)
293 * 13. default rmap name (neighbor default-originate route-map <>)
294 * 14. encoding both global and link-local nexthop?
295 * 15. If peer is configured to be a lonesoul, peer ip address
296 * 16. Local-as should match, if configured.
300 updgrp_hash_key_make (void *p
)
302 const struct update_group
*updgrp
;
303 const struct peer
*peer
;
304 const struct bgp_filter
*filter
;
311 #define SEED2 2147483647
317 flags
= peer
->af_flags
[afi
][safi
];
318 filter
= &peer
->filter
[afi
][safi
];
322 key
= jhash_1word (peer
->sort
, key
); /* EBGP or IBGP */
323 key
= jhash_1word ((peer
->flags
& PEER_UPDGRP_FLAGS
), key
);
324 key
= jhash_1word ((flags
& PEER_UPDGRP_AF_FLAGS
), key
);
325 key
= jhash_1word ((peer
->cap
& PEER_UPDGRP_CAP_FLAGS
), key
);
326 key
= jhash_1word ((peer
->af_cap
[afi
][safi
] &
327 PEER_UPDGRP_AF_CAP_FLAGS
), key
);
328 key
= jhash_1word (peer
->v_routeadv
, key
);
329 key
= jhash_1word (peer
->change_local_as
, key
);
332 key
= jhash_1word (jhash (peer
->group
->name
,
333 strlen (peer
->group
->name
), SEED1
), key
);
335 if (filter
->map
[RMAP_OUT
].name
)
336 key
= jhash_1word (jhash (filter
->map
[RMAP_OUT
].name
,
337 strlen (filter
->map
[RMAP_OUT
].name
), SEED1
),
340 if (filter
->dlist
[FILTER_OUT
].name
)
341 key
= jhash_1word (jhash (filter
->dlist
[FILTER_OUT
].name
,
342 strlen (filter
->dlist
[FILTER_OUT
].name
), SEED1
),
345 if (filter
->plist
[FILTER_OUT
].name
)
346 key
= jhash_1word (jhash (filter
->plist
[FILTER_OUT
].name
,
347 strlen (filter
->plist
[FILTER_OUT
].name
), SEED1
),
350 if (filter
->aslist
[FILTER_OUT
].name
)
351 key
= jhash_1word (jhash (filter
->aslist
[FILTER_OUT
].name
,
352 strlen (filter
->aslist
[FILTER_OUT
].name
),
355 if (filter
->usmap
.name
)
356 key
= jhash_1word (jhash (filter
->usmap
.name
,
357 strlen (filter
->usmap
.name
), SEED1
), key
);
359 if (peer
->default_rmap
[afi
][safi
].name
)
360 key
= jhash_1word (jhash (peer
->default_rmap
[afi
][safi
].name
,
361 strlen (peer
->default_rmap
[afi
][safi
].name
),
364 /* If peer is on a shared network and is exchanging IPv6 prefixes,
365 * it needs to include link-local address. That's different from
366 * non-shared-network peers (nexthop encoded with 32 bytes vs 16
367 * bytes). We create different update groups to take care of that.
369 key
= jhash_1word ((peer
->shared_network
&&
370 peer_afi_active_nego (peer
, AFI_IP6
)),
374 * There are certain peers that must get their own update-group:
376 * - peers that negotiated ORF
378 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_LONESOUL
) ||
379 CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_RCV
) ||
380 CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_OLD_RCV
))
381 key
= jhash_1word (jhash (peer
->host
, strlen (peer
->host
), SEED2
), key
);
387 updgrp_hash_cmp (const void *p1
, const void *p2
)
389 const struct update_group
*grp1
;
390 const struct update_group
*grp2
;
391 const struct peer
*pe1
;
392 const struct peer
*pe2
;
395 const struct bgp_filter
*fl1
;
396 const struct bgp_filter
*fl2
;
409 flags1
= pe1
->af_flags
[afi
][safi
];
410 flags2
= pe2
->af_flags
[afi
][safi
];
411 fl1
= &pe1
->filter
[afi
][safi
];
412 fl2
= &pe2
->filter
[afi
][safi
];
414 /* put EBGP and IBGP peers in different update groups */
415 if (pe1
->sort
!= pe2
->sort
)
418 /* check peer flags */
419 if ((pe1
->flags
& PEER_UPDGRP_FLAGS
) !=
420 (pe2
->flags
& PEER_UPDGRP_FLAGS
))
423 /* If there is 'local-as' configured, it should match. */
424 if (pe1
->change_local_as
!= pe2
->change_local_as
)
427 /* flags like route reflector client */
428 if ((flags1
& PEER_UPDGRP_AF_FLAGS
) != (flags2
& PEER_UPDGRP_AF_FLAGS
))
431 if ((pe1
->cap
& PEER_UPDGRP_CAP_FLAGS
) !=
432 (pe2
->cap
& PEER_UPDGRP_CAP_FLAGS
))
435 if ((pe1
->af_cap
[afi
][safi
] & PEER_UPDGRP_AF_CAP_FLAGS
) !=
436 (pe2
->af_cap
[afi
][safi
] & PEER_UPDGRP_AF_CAP_FLAGS
))
439 if (pe1
->v_routeadv
!= pe2
->v_routeadv
)
442 if (pe1
->group
!= pe2
->group
)
445 /* route-map names should be the same */
446 if ((fl1
->map
[RMAP_OUT
].name
&& !fl2
->map
[RMAP_OUT
].name
) ||
447 (!fl1
->map
[RMAP_OUT
].name
&& fl2
->map
[RMAP_OUT
].name
) ||
448 (fl1
->map
[RMAP_OUT
].name
&& fl2
->map
[RMAP_OUT
].name
&&
449 strcmp (fl1
->map
[RMAP_OUT
].name
, fl2
->map
[RMAP_OUT
].name
)))
452 if ((fl1
->dlist
[FILTER_OUT
].name
&& !fl2
->dlist
[FILTER_OUT
].name
) ||
453 (!fl1
->dlist
[FILTER_OUT
].name
&& fl2
->dlist
[FILTER_OUT
].name
) ||
454 (fl1
->dlist
[FILTER_OUT
].name
&& fl2
->dlist
[FILTER_OUT
].name
&&
455 strcmp (fl1
->dlist
[FILTER_OUT
].name
, fl2
->dlist
[FILTER_OUT
].name
)))
458 if ((fl1
->plist
[FILTER_OUT
].name
&& !fl2
->plist
[FILTER_OUT
].name
) ||
459 (!fl1
->plist
[FILTER_OUT
].name
&& fl2
->plist
[FILTER_OUT
].name
) ||
460 (fl1
->plist
[FILTER_OUT
].name
&& fl2
->plist
[FILTER_OUT
].name
&&
461 strcmp (fl1
->plist
[FILTER_OUT
].name
, fl2
->plist
[FILTER_OUT
].name
)))
464 if ((fl1
->aslist
[FILTER_OUT
].name
&& !fl2
->aslist
[FILTER_OUT
].name
) ||
465 (!fl1
->aslist
[FILTER_OUT
].name
&& fl2
->aslist
[FILTER_OUT
].name
) ||
466 (fl1
->aslist
[FILTER_OUT
].name
&& fl2
->aslist
[FILTER_OUT
].name
&&
467 strcmp (fl1
->aslist
[FILTER_OUT
].name
, fl2
->aslist
[FILTER_OUT
].name
)))
470 if ((fl1
->usmap
.name
&& !fl2
->usmap
.name
) ||
471 (!fl1
->usmap
.name
&& fl2
->usmap
.name
) ||
472 (fl1
->usmap
.name
&& fl2
->usmap
.name
&&
473 strcmp (fl1
->usmap
.name
, fl2
->usmap
.name
)))
476 if ((pe1
->default_rmap
[afi
][safi
].name
&&
477 !pe2
->default_rmap
[afi
][safi
].name
) ||
478 (!pe1
->default_rmap
[afi
][safi
].name
&&
479 pe2
->default_rmap
[afi
][safi
].name
) ||
480 (pe1
->default_rmap
[afi
][safi
].name
&&
481 pe2
->default_rmap
[afi
][safi
].name
&&
482 strcmp (pe1
->default_rmap
[afi
][safi
].name
,
483 pe2
->default_rmap
[afi
][safi
].name
)))
486 if ((afi
== AFI_IP6
) && (pe1
->shared_network
!= pe2
->shared_network
))
489 if ((CHECK_FLAG (pe1
->flags
, PEER_FLAG_LONESOUL
) ||
490 CHECK_FLAG (pe1
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_RCV
) ||
491 CHECK_FLAG (pe1
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_OLD_RCV
)) &&
492 !sockunion_same (&pe1
->su
, &pe2
->su
))
499 peer_lonesoul_or_not (struct peer
*peer
, int set
)
501 /* no change in status? */
502 if (set
== (CHECK_FLAG (peer
->flags
, PEER_FLAG_LONESOUL
) > 0))
506 SET_FLAG (peer
->flags
, PEER_FLAG_LONESOUL
);
508 UNSET_FLAG (peer
->flags
, PEER_FLAG_LONESOUL
);
510 update_group_adjust_peer_afs (peer
);
514 * subgroup_total_packets_enqueued
516 * Returns the total number of packets enqueued to a subgroup.
519 subgroup_total_packets_enqueued (struct update_subgroup
*subgrp
)
523 pkt
= bpacket_queue_last (SUBGRP_PKTQ (subgrp
));
529 update_group_show_walkcb (struct update_group
*updgrp
, void *arg
)
531 struct updwalk_context
*ctx
= arg
;
533 struct update_subgroup
*subgrp
;
535 struct bgp_filter
*filter
;
543 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
)
545 if (ctx
->subgrp_id
&& (ctx
->subgrp_id
!= subgrp
->id
))
561 /* Since this routine is invoked from a walk, we cannot signal any */
562 /* error here, can only return. */
568 vty_out (vty
, "Update-group %" PRIu64
":%s", updgrp
->id
, VTY_NEWLINE
);
569 vty_out (vty
, " Created: %s", timestamp_string (updgrp
->uptime
));
570 filter
= &updgrp
->conf
->filter
[updgrp
->afi
][updgrp
->safi
];
571 if (filter
->map
[RMAP_OUT
].name
)
572 vty_out (vty
, " Outgoing route map: %s%s%s",
573 filter
->map
[RMAP_OUT
].map
? "X" : "",
574 filter
->map
[RMAP_OUT
].name
, VTY_NEWLINE
);
575 vty_out (vty
, " MRAI value (seconds): %d%s",
576 updgrp
->conf
->v_routeadv
, VTY_NEWLINE
);
577 if (updgrp
->conf
->change_local_as
)
578 vty_out (vty
, " Local AS %u%s%s%s",
579 updgrp
->conf
->change_local_as
,
580 CHECK_FLAG (updgrp
->conf
->flags
,
581 PEER_FLAG_LOCAL_AS_NO_PREPEND
) ? " no-prepend" : "",
582 CHECK_FLAG (updgrp
->conf
->flags
,
583 PEER_FLAG_LOCAL_AS_REPLACE_AS
) ? " replace-as" : "",
586 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
)
588 if (ctx
->subgrp_id
&& (ctx
->subgrp_id
!= subgrp
->id
))
590 vty_out (vty
, "%s", VTY_NEWLINE
);
591 vty_out (vty
, " Update-subgroup %" PRIu64
":%s", subgrp
->id
, VTY_NEWLINE
);
592 vty_out (vty
, " Created: %s", timestamp_string (subgrp
->uptime
));
594 if (subgrp
->split_from
.update_group_id
|| subgrp
->split_from
.subgroup_id
)
596 vty_out (vty
, " Split from group id: %" PRIu64
"%s",
597 subgrp
->split_from
.update_group_id
, VTY_NEWLINE
);
598 vty_out (vty
, " Split from subgroup id: %" PRIu64
"%s",
599 subgrp
->split_from
.subgroup_id
, VTY_NEWLINE
);
602 vty_out (vty
, " Join events: %u%s", subgrp
->join_events
, VTY_NEWLINE
);
603 vty_out (vty
, " Prune events: %u%s",
604 subgrp
->prune_events
, VTY_NEWLINE
);
605 vty_out (vty
, " Merge events: %u%s",
606 subgrp
->merge_events
, VTY_NEWLINE
);
607 vty_out (vty
, " Split events: %u%s",
608 subgrp
->split_events
, VTY_NEWLINE
);
609 vty_out (vty
, " Update group switch events: %u%s",
610 subgrp
->updgrp_switch_events
, VTY_NEWLINE
);
611 vty_out (vty
, " Peer refreshes combined: %u%s",
612 subgrp
->peer_refreshes_combined
, VTY_NEWLINE
);
613 vty_out (vty
, " Merge checks triggered: %u%s",
614 subgrp
->merge_checks_triggered
, VTY_NEWLINE
);
615 vty_out (vty
, " Version: %" PRIu64
"%s", subgrp
->version
, VTY_NEWLINE
);
616 vty_out (vty
, " Packet queue length: %d%s",
617 bpacket_queue_length (SUBGRP_PKTQ (subgrp
)), VTY_NEWLINE
);
618 vty_out (vty
, " Total packets enqueued: %u%s",
619 subgroup_total_packets_enqueued (subgrp
), VTY_NEWLINE
);
620 vty_out (vty
, " Packet queue high watermark: %d%s",
621 bpacket_queue_hwm_length (SUBGRP_PKTQ (subgrp
)), VTY_NEWLINE
);
622 vty_out (vty
, " Adj-out list count: %u%s",
623 subgrp
->adj_count
, VTY_NEWLINE
);
624 vty_out (vty
, " Advertise list: %s%s",
625 advertise_list_is_empty (subgrp
) ? "empty" : "not empty",
627 vty_out (vty
, " Flags: %s%s",
628 CHECK_FLAG (subgrp
->flags
,
629 SUBGRP_FLAG_NEEDS_REFRESH
) ? "R" : "", VTY_NEWLINE
);
630 if (subgrp
->peer_count
> 0)
632 vty_out (vty
, " Peers:%s", VTY_NEWLINE
);
633 SUBGRP_FOREACH_PEER (subgrp
, paf
)
634 vty_out (vty
, " - %s%s", paf
->peer
->host
, VTY_NEWLINE
);
637 return UPDWALK_CONTINUE
;
641 * Helper function to show the packet queue for each subgroup of update group.
642 * Will be constrained to a particular subgroup id if id !=0
645 updgrp_show_packet_queue_walkcb (struct update_group
*updgrp
, void *arg
)
647 struct updwalk_context
*ctx
= arg
;
648 struct update_subgroup
*subgrp
;
652 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
)
654 if (ctx
->subgrp_id
&& (ctx
->subgrp_id
!= subgrp
->id
))
656 vty_out (vty
, "update group %" PRIu64
", subgroup %" PRIu64
"%s", updgrp
->id
,
657 subgrp
->id
, VTY_NEWLINE
);
658 bpacket_queue_show_vty (SUBGRP_PKTQ (subgrp
), vty
);
660 return UPDWALK_CONTINUE
;
664 * Show the packet queue for each subgroup of update group. Will be
665 * constrained to a particular subgroup id if id !=0
668 update_group_show_packet_queue (struct bgp
*bgp
, afi_t afi
, safi_t safi
,
669 struct vty
*vty
, uint64_t id
)
671 struct updwalk_context ctx
;
673 memset (&ctx
, 0, sizeof (ctx
));
677 update_group_af_walk (bgp
, afi
, safi
, updgrp_show_packet_queue_walkcb
,
681 static struct update_group
*
682 update_group_find (struct peer_af
*paf
)
684 struct update_group
*updgrp
;
685 struct update_group tmp
;
686 struct peer tmp_conf
;
688 if (!peer_established (PAF_PEER (paf
)))
691 memset (&tmp
, 0, sizeof (tmp
));
692 memset (&tmp_conf
, 0, sizeof (tmp_conf
));
693 tmp
.conf
= &tmp_conf
;
694 peer2_updgrp_copy (&tmp
, paf
);
696 updgrp
= hash_lookup (paf
->peer
->bgp
->update_groups
[paf
->afid
], &tmp
);
697 conf_release (&tmp_conf
, paf
->afi
, paf
->safi
);
701 static struct update_group
*
702 update_group_create (struct peer_af
*paf
)
704 struct update_group
*updgrp
;
705 struct update_group tmp
;
706 struct peer tmp_conf
;
708 memset (&tmp
, 0, sizeof (tmp
));
709 memset (&tmp_conf
, 0, sizeof (tmp_conf
));
710 tmp
.conf
= &tmp_conf
;
711 peer2_updgrp_copy (&tmp
, paf
);
713 updgrp
= hash_get (paf
->peer
->bgp
->update_groups
[paf
->afid
], &tmp
,
717 update_group_checkin (updgrp
);
719 if (BGP_DEBUG (update_groups
, UPDATE_GROUPS
))
720 zlog_debug ("create update group %" PRIu64
, updgrp
->id
);
722 UPDGRP_GLOBAL_STAT (updgrp
, updgrps_created
) += 1;
724 conf_release(&tmp_conf
, paf
->afi
, paf
->safi
);
729 update_group_delete (struct update_group
*updgrp
)
731 if (BGP_DEBUG (update_groups
, UPDATE_GROUPS
))
732 zlog_debug ("delete update group %" PRIu64
, updgrp
->id
);
734 UPDGRP_GLOBAL_STAT (updgrp
, updgrps_deleted
) += 1;
736 hash_release (updgrp
->bgp
->update_groups
[updgrp
->afid
], updgrp
);
737 conf_release (updgrp
->conf
, updgrp
->afi
, updgrp
->safi
);
739 if (updgrp
->conf
->host
)
740 XFREE(MTYPE_BGP_PEER_HOST
, updgrp
->conf
->host
);
741 updgrp
->conf
->host
= NULL
;
743 if (updgrp
->conf
->ifname
)
744 XFREE(MTYPE_BGP_PEER_IFNAME
, updgrp
->conf
->ifname
);
746 XFREE (MTYPE_BGP_PEER
, updgrp
->conf
);
747 XFREE (MTYPE_BGP_UPDGRP
, updgrp
);
751 update_group_add_subgroup (struct update_group
*updgrp
,
752 struct update_subgroup
*subgrp
)
754 if (!updgrp
|| !subgrp
)
757 LIST_INSERT_HEAD (&(updgrp
->subgrps
), subgrp
, updgrp_train
);
758 subgrp
->update_group
= updgrp
;
762 update_group_remove_subgroup (struct update_group
*updgrp
,
763 struct update_subgroup
*subgrp
)
765 if (!updgrp
|| !subgrp
)
768 LIST_REMOVE (subgrp
, updgrp_train
);
769 subgrp
->update_group
= NULL
;
770 if (LIST_EMPTY (&(updgrp
->subgrps
)))
771 update_group_delete (updgrp
);
774 static struct update_subgroup
*
775 update_subgroup_create (struct update_group
*updgrp
)
777 struct update_subgroup
*subgrp
;
779 subgrp
= XCALLOC (MTYPE_BGP_UPD_SUBGRP
, sizeof (struct update_subgroup
));
780 update_subgroup_checkin (subgrp
, updgrp
);
781 subgrp
->v_coalesce
= (UPDGRP_INST (updgrp
))->coalesce_time
;
783 bpacket_queue_init (SUBGRP_PKTQ (subgrp
));
784 bpacket_queue_add (SUBGRP_PKTQ (subgrp
), NULL
, NULL
);
785 TAILQ_INIT (&(subgrp
->adjq
));
786 if (BGP_DEBUG (update_groups
, UPDATE_GROUPS
))
787 zlog_debug ("create subgroup u%" PRIu64
":s%" PRIu64
,
788 updgrp
->id
, subgrp
->id
);
790 update_group_add_subgroup (updgrp
, subgrp
);
792 UPDGRP_INCR_STAT (updgrp
, subgrps_created
);
798 update_subgroup_delete (struct update_subgroup
*subgrp
)
803 if (subgrp
->update_group
)
804 UPDGRP_INCR_STAT (subgrp
->update_group
, subgrps_deleted
);
806 if (subgrp
->t_merge_check
)
807 THREAD_OFF (subgrp
->t_merge_check
);
809 if (subgrp
->t_coalesce
)
810 THREAD_TIMER_OFF (subgrp
->t_coalesce
);
812 bpacket_queue_cleanup (SUBGRP_PKTQ (subgrp
));
813 subgroup_clear_table (subgrp
);
815 if (subgrp
->t_coalesce
)
816 THREAD_TIMER_OFF (subgrp
->t_coalesce
);
817 sync_delete (subgrp
);
819 if (BGP_DEBUG (update_groups
, UPDATE_GROUPS
))
820 zlog_debug ("delete subgroup u%" PRIu64
":s%" PRIu64
,
821 subgrp
->update_group
->id
, subgrp
->id
);
823 update_group_remove_subgroup (subgrp
->update_group
, subgrp
);
825 XFREE (MTYPE_BGP_UPD_SUBGRP
, subgrp
);
829 update_subgroup_inherit_info (struct update_subgroup
*to
,
830 struct update_subgroup
*from
)
835 to
->sflags
= from
->sflags
;
839 * update_subgroup_check_delete
841 * Delete a subgroup if it is ready to be deleted.
843 * Returns TRUE if the subgroup was deleted.
846 update_subgroup_check_delete (struct update_subgroup
*subgrp
)
851 if (!LIST_EMPTY (&(subgrp
->peers
)))
854 update_subgroup_delete (subgrp
);
860 * update_subgroup_add_peer
862 * @param send_enqueued_packets If true all currently enqueued packets will
863 * also be sent to the peer.
866 update_subgroup_add_peer (struct update_subgroup
*subgrp
, struct peer_af
*paf
,
867 int send_enqueued_pkts
)
874 LIST_INSERT_HEAD (&(subgrp
->peers
), paf
, subgrp_train
);
875 paf
->subgroup
= subgrp
;
876 subgrp
->peer_count
++;
878 if (bgp_debug_peer_updout_enabled(paf
->peer
->host
))
880 UPDGRP_PEER_DBG_EN(subgrp
->update_group
);
883 SUBGRP_INCR_STAT (subgrp
, join_events
);
885 if (send_enqueued_pkts
)
887 pkt
= bpacket_queue_first (SUBGRP_PKTQ (subgrp
));
893 * Hang the peer off of the last, placeholder, packet in the
894 * queue. This means it won't see any of the packets that are
895 * currently the queue.
897 pkt
= bpacket_queue_last (SUBGRP_PKTQ (subgrp
));
898 assert (pkt
->buffer
== NULL
);
901 bpacket_add_peer (pkt
, paf
);
903 bpacket_queue_sanity_check (SUBGRP_PKTQ (subgrp
));
907 * update_subgroup_remove_peer_internal
909 * Internal function that removes a peer from a subgroup, but does not
910 * delete the subgroup. A call to this function must almost always be
911 * followed by a call to update_subgroup_check_delete().
913 * @see update_subgroup_remove_peer
916 update_subgroup_remove_peer_internal (struct update_subgroup
*subgrp
,
919 assert (subgrp
&& paf
);
921 if (bgp_debug_peer_updout_enabled(paf
->peer
->host
))
923 UPDGRP_PEER_DBG_DIS(subgrp
->update_group
);
926 bpacket_queue_remove_peer (paf
);
927 LIST_REMOVE (paf
, subgrp_train
);
928 paf
->subgroup
= NULL
;
929 subgrp
->peer_count
--;
931 SUBGRP_INCR_STAT (subgrp
, prune_events
);
935 * update_subgroup_remove_peer
938 update_subgroup_remove_peer (struct update_subgroup
*subgrp
,
944 update_subgroup_remove_peer_internal (subgrp
, paf
);
946 if (update_subgroup_check_delete (subgrp
))
950 * The deletion of the peer may have caused some packets to be
951 * deleted from the subgroup packet queue. Check if the subgroup can
954 update_subgroup_check_merge (subgrp
, "removed peer from subgroup");
957 static struct update_subgroup
*
958 update_subgroup_find (struct update_group
*updgrp
, struct peer_af
*paf
)
960 struct update_subgroup
*subgrp
= NULL
;
971 if (!peer_established (PAF_PEER (paf
)))
974 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
)
976 if (subgrp
->version
!= version
||
977 CHECK_FLAG(subgrp
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
))
981 * The version number is not meaningful on a subgroup that needs
984 if (update_subgroup_needs_refresh (subgrp
))
994 * update_subgroup_ready_for_merge
996 * Returns TRUE if this subgroup is in a state that allows it to be
997 * merged into another subgroup.
1000 update_subgroup_ready_for_merge (struct update_subgroup
*subgrp
)
1004 * Not ready if there are any encoded packets waiting to be written
1007 if (!bpacket_queue_is_empty (SUBGRP_PKTQ (subgrp
)))
1011 * Not ready if there enqueued updates waiting to be encoded.
1013 if (!advertise_list_is_empty (subgrp
))
1017 * Don't attempt to merge a subgroup that needs a refresh. For one,
1018 * we can't determine if the adj_out of such a group matches that of
1021 if (update_subgroup_needs_refresh (subgrp
))
1028 * update_subgrp_can_merge_into
1030 * Returns TRUE if the first subgroup can merge into the second
1034 update_subgroup_can_merge_into (struct update_subgroup
*subgrp
,
1035 struct update_subgroup
*target
)
1038 if (subgrp
== target
)
1042 * Both must have processed the BRIB to the same point in order to
1045 if (subgrp
->version
!= target
->version
)
1048 if (CHECK_FLAG(subgrp
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
) !=
1049 CHECK_FLAG(target
->sflags
, SUBGRP_STATUS_DEFAULT_ORIGINATE
))
1052 if (subgrp
->adj_count
!= target
->adj_count
)
1055 return update_subgroup_ready_for_merge (target
);
1059 * update_subgroup_merge
1061 * Merge the first subgroup into the second one.
1064 update_subgroup_merge (struct update_subgroup
*subgrp
,
1065 struct update_subgroup
*target
, const char *reason
)
1067 struct peer_af
*paf
;
1071 assert (subgrp
->adj_count
== target
->adj_count
);
1073 peer_count
= subgrp
->peer_count
;
1077 paf
= LIST_FIRST (&subgrp
->peers
);
1081 update_subgroup_remove_peer_internal (subgrp
, paf
);
1084 * Add the peer to the target subgroup, while making sure that
1085 * any currently enqueued packets won't be sent to it. Enqueued
1086 * packets could, for example, result in an unnecessary withdraw
1087 * followed by an advertise.
1089 update_subgroup_add_peer (target
, paf
, 0);
1092 SUBGRP_INCR_STAT (target
, merge_events
);
1094 if (BGP_DEBUG (update_groups
, UPDATE_GROUPS
))
1095 zlog_debug ("u%" PRIu64
":s%" PRIu64
" (%d peers) merged into u%" PRIu64
":s%" PRIu64
", "
1096 "trigger: %s", subgrp
->update_group
->id
, subgrp
->id
, peer_count
,
1097 target
->update_group
->id
, target
->id
, reason
? reason
: "unknown");
1099 result
= update_subgroup_check_delete (subgrp
);
1104 * update_subgroup_check_merge
1106 * Merge this subgroup into another subgroup if possible.
1108 * Returns TRUE if the subgroup has been merged. The subgroup pointer
1109 * should not be accessed in this case.
1112 update_subgroup_check_merge (struct update_subgroup
*subgrp
,
1115 struct update_subgroup
*target
;
1117 if (!update_subgroup_ready_for_merge (subgrp
))
1121 * Look for a subgroup to merge into.
1123 UPDGRP_FOREACH_SUBGRP (subgrp
->update_group
, target
)
1125 if (update_subgroup_can_merge_into (subgrp
, target
))
1132 update_subgroup_merge (subgrp
, target
, reason
);
1137 * update_subgroup_merge_check_thread_cb
1140 update_subgroup_merge_check_thread_cb (struct thread
*thread
)
1142 struct update_subgroup
*subgrp
;
1144 subgrp
= THREAD_ARG (thread
);
1146 subgrp
->t_merge_check
= NULL
;
1148 update_subgroup_check_merge (subgrp
, "triggered merge check");
1153 * update_subgroup_trigger_merge_check
1155 * Triggers a call to update_subgroup_check_merge() on a clean context.
1157 * @param force If true, the merge check will be triggered even if the
1158 * subgroup doesn't currently look ready for a merge.
1160 * Returns TRUE if a merge check will be performed shortly.
1163 update_subgroup_trigger_merge_check (struct update_subgroup
*subgrp
,
1166 if (subgrp
->t_merge_check
)
1169 if (!force
&& !update_subgroup_ready_for_merge (subgrp
))
1172 subgrp
->t_merge_check
=
1173 thread_add_background(bm
->master
, update_subgroup_merge_check_thread_cb
,
1176 SUBGRP_INCR_STAT (subgrp
, merge_checks_triggered
);
1182 * update_subgroup_copy_adj_out
1184 * Helper function that clones the adj out (state about advertised
1185 * routes) from one subgroup to another. It assumes that the adj out
1186 * of the target subgroup is empty.
1189 update_subgroup_copy_adj_out (struct update_subgroup
*source
,
1190 struct update_subgroup
*dest
)
1192 struct bgp_adj_out
*aout
, *aout_copy
;
1194 SUBGRP_FOREACH_ADJ (source
, aout
)
1199 aout_copy
= bgp_adj_out_alloc (dest
, aout
->rn
, aout
->addpath_tx_id
);
1200 aout_copy
->attr
= aout
->attr
? bgp_attr_refcount (aout
->attr
) : NULL
;
1205 * update_subgroup_copy_packets
1207 * Copy packets after and including the given packet to the subgroup
1210 * Returns the number of packets copied.
1213 update_subgroup_copy_packets (struct update_subgroup
*dest
,
1214 struct bpacket
*pkt
)
1219 while (pkt
&& pkt
->buffer
)
1221 bpacket_queue_add (SUBGRP_PKTQ (dest
), stream_dup (pkt
->buffer
),
1224 pkt
= bpacket_next (pkt
);
1227 bpacket_queue_sanity_check (SUBGRP_PKTQ (dest
));
1233 updgrp_prefix_list_update (struct update_group
*updgrp
, const char *name
)
1236 struct bgp_filter
*filter
;
1238 peer
= UPDGRP_PEER (updgrp
);
1239 filter
= &peer
->filter
[UPDGRP_AFI(updgrp
)][UPDGRP_SAFI(updgrp
)];
1241 if (PREFIX_LIST_OUT_NAME(filter
) &&
1242 (strcmp (name
, PREFIX_LIST_OUT_NAME(filter
)) == 0))
1244 PREFIX_LIST_OUT(filter
) =
1245 prefix_list_lookup (UPDGRP_AFI(updgrp
), PREFIX_LIST_OUT_NAME(filter
));
1252 updgrp_filter_list_update (struct update_group
*updgrp
, const char *name
)
1255 struct bgp_filter
*filter
;
1257 peer
= UPDGRP_PEER (updgrp
);
1258 filter
= &peer
->filter
[UPDGRP_AFI(updgrp
)][UPDGRP_SAFI(updgrp
)];
1260 if (FILTER_LIST_OUT_NAME(filter
) &&
1261 (strcmp (name
, FILTER_LIST_OUT_NAME(filter
)) == 0))
1263 FILTER_LIST_OUT(filter
) = as_list_lookup (FILTER_LIST_OUT_NAME(filter
));
1270 updgrp_distribute_list_update (struct update_group
*updgrp
, const char *name
)
1273 struct bgp_filter
*filter
;
1275 peer
= UPDGRP_PEER(updgrp
);
1276 filter
= &peer
->filter
[UPDGRP_AFI(updgrp
)][UPDGRP_SAFI(updgrp
)];
1278 if (DISTRIBUTE_OUT_NAME(filter
) &&
1279 (strcmp (name
, DISTRIBUTE_OUT_NAME(filter
)) == 0))
1281 DISTRIBUTE_OUT(filter
) = access_list_lookup(UPDGRP_AFI(updgrp
),
1282 DISTRIBUTE_OUT_NAME(filter
));
1289 updgrp_route_map_update (struct update_group
*updgrp
, const char *name
,
1290 int *def_rmap_changed
)
1293 struct bgp_filter
*filter
;
1298 peer
= UPDGRP_PEER (updgrp
);
1299 afi
= UPDGRP_AFI (updgrp
);
1300 safi
= UPDGRP_SAFI (updgrp
);
1301 filter
= &peer
->filter
[afi
][safi
];
1303 if (ROUTE_MAP_OUT_NAME(filter
) &&
1304 (strcmp (name
, ROUTE_MAP_OUT_NAME(filter
)) == 0))
1306 ROUTE_MAP_OUT(filter
) = route_map_lookup_by_name (name
);
1311 if (UNSUPPRESS_MAP_NAME(filter
) &&
1312 (strcmp (name
, UNSUPPRESS_MAP_NAME(filter
)) == 0))
1314 UNSUPPRESS_MAP(filter
) = route_map_lookup_by_name (name
);
1318 /* process default-originate route-map */
1319 if (peer
->default_rmap
[afi
][safi
].name
&&
1320 (strcmp (name
, peer
->default_rmap
[afi
][safi
].name
) == 0))
1322 peer
->default_rmap
[afi
][safi
].map
= route_map_lookup_by_name (name
);
1323 if (def_rmap_changed
)
1324 *def_rmap_changed
= 1;
1330 * hash iteration callback function to process a policy change for an
1331 * update group. Check if the changed policy matches the updgrp's
1332 * outbound route-map or unsuppress-map or default-originate map or
1333 * filter-list or prefix-list or distribute-list.
1334 * Trigger update generation accordingly.
1337 updgrp_policy_update_walkcb (struct update_group
*updgrp
, void *arg
)
1339 struct updwalk_context
*ctx
= arg
;
1340 struct update_subgroup
*subgrp
;
1342 int def_changed
= 0;
1344 if (!updgrp
|| !ctx
|| !ctx
->policy_name
)
1345 return UPDWALK_CONTINUE
;
1347 switch (ctx
->policy_type
) {
1348 case BGP_POLICY_ROUTE_MAP
:
1349 changed
= updgrp_route_map_update(updgrp
, ctx
->policy_name
, &def_changed
);
1351 case BGP_POLICY_FILTER_LIST
:
1352 changed
= updgrp_filter_list_update(updgrp
, ctx
->policy_name
);
1354 case BGP_POLICY_PREFIX_LIST
:
1355 changed
= updgrp_prefix_list_update(updgrp
, ctx
->policy_name
);
1357 case BGP_POLICY_DISTRIBUTE_LIST
:
1358 changed
= updgrp_distribute_list_update(updgrp
, ctx
->policy_name
);
1364 /* If not doing route update, return after updating "config" */
1365 if (!ctx
->policy_route_update
)
1366 return UPDWALK_CONTINUE
;
1368 /* If nothing has changed, return after updating "config" */
1369 if (!changed
&& !def_changed
)
1370 return UPDWALK_CONTINUE
;
1373 * If something has changed, at the beginning of a route-map modification
1374 * event, mark each subgroup's needs-refresh bit. For one, it signals to
1375 * whoever that the subgroup needs a refresh. Second, it prevents premature
1376 * merge of this subgroup with another before a complete (outbound) refresh.
1378 if (ctx
->policy_event_start_flag
)
1380 UPDGRP_FOREACH_SUBGRP(updgrp
, subgrp
)
1382 update_subgroup_set_needs_refresh(subgrp
, 1);
1384 return UPDWALK_CONTINUE
;
1387 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
)
1391 if (bgp_debug_update(NULL
, NULL
, updgrp
, 0))
1392 zlog_debug ("u%" PRIu64
":s%" PRIu64
" announcing routes upon policy %s (type %d) change",
1393 updgrp
->id
, subgrp
->id
, ctx
->policy_name
, ctx
->policy_type
);
1394 subgroup_announce_route (subgrp
);
1398 if (bgp_debug_update(NULL
, NULL
, updgrp
, 0))
1399 zlog_debug ("u%" PRIu64
":s%" PRIu64
" announcing default upon default routemap %s change",
1400 updgrp
->id
, subgrp
->id
, ctx
->policy_name
);
1401 subgroup_default_originate (subgrp
, 0);
1403 update_subgroup_set_needs_refresh(subgrp
, 0);
1405 return UPDWALK_CONTINUE
;
1409 update_group_walkcb (struct hash_backet
*backet
, void *arg
)
1411 struct update_group
*updgrp
= backet
->data
;
1412 struct updwalk_context
*wctx
= arg
;
1413 int ret
= (*wctx
->cb
) (updgrp
, wctx
->context
);
1418 update_group_periodic_merge_walkcb (struct update_group
*updgrp
, void *arg
)
1420 struct update_subgroup
*subgrp
;
1421 struct update_subgroup
*tmp_subgrp
;
1422 const char *reason
= arg
;
1424 UPDGRP_FOREACH_SUBGRP_SAFE (updgrp
, subgrp
, tmp_subgrp
)
1425 update_subgroup_check_merge (subgrp
, reason
);
1426 return UPDWALK_CONTINUE
;
1429 /********************
1431 ********************/
1434 * trigger function when a policy (route-map/filter-list/prefix-list/
1435 * distribute-list etc.) content changes. Go through all the
1436 * update groups and process the change.
1438 * bgp: the bgp instance
1439 * ptype: the type of policy that got modified, see bgpd.h
1440 * pname: name of the policy
1441 * route_update: flag to control if an automatic update generation should
1443 * start_event: flag that indicates if it's the beginning of the change.
1444 * Esp. when the user is changing the content interactively
1445 * over multiple statements. Useful to set dirty flag on
1449 update_group_policy_update (struct bgp
*bgp
, bgp_policy_type_e ptype
,
1450 const char *pname
, int route_update
, int start_event
)
1452 struct updwalk_context ctx
;
1454 memset (&ctx
, 0, sizeof (ctx
));
1455 ctx
.policy_type
= ptype
;
1456 ctx
.policy_name
= pname
;
1457 ctx
.policy_route_update
= route_update
;
1458 ctx
.policy_event_start_flag
= start_event
;
1461 update_group_walk (bgp
, updgrp_policy_update_walkcb
, &ctx
);
1465 * update_subgroup_split_peer
1467 * Ensure that the given peer is in a subgroup of its own in the
1468 * specified update group.
1471 update_subgroup_split_peer (struct peer_af
*paf
, struct update_group
*updgrp
)
1473 struct update_subgroup
*old_subgrp
, *subgrp
;
1477 old_subgrp
= paf
->subgroup
;
1480 updgrp
= old_subgrp
->update_group
;
1483 * If the peer is alone in its subgroup, reuse the existing
1486 if (old_subgrp
->peer_count
== 1)
1488 if (updgrp
== old_subgrp
->update_group
)
1491 subgrp
= old_subgrp
;
1492 old_id
= old_subgrp
->update_group
->id
;
1494 if (bgp_debug_peer_updout_enabled(paf
->peer
->host
))
1496 UPDGRP_PEER_DBG_DIS(old_subgrp
->update_group
);
1499 update_group_remove_subgroup (old_subgrp
->update_group
, old_subgrp
);
1500 update_group_add_subgroup (updgrp
, subgrp
);
1502 if (bgp_debug_peer_updout_enabled(paf
->peer
->host
))
1504 UPDGRP_PEER_DBG_EN(updgrp
);
1506 if (BGP_DEBUG (update_groups
, UPDATE_GROUPS
))
1507 zlog_debug ("u%" PRIu64
":s%" PRIu64
" peer %s moved to u%" PRIu64
":s%" PRIu64
,
1508 old_id
, subgrp
->id
, paf
->peer
->host
, updgrp
->id
, subgrp
->id
);
1511 * The state of the subgroup (adj_out, advs, packet queue etc)
1512 * is consistent internally, but may not be identical to other
1513 * subgroups in the new update group even if the version number
1514 * matches up. Make sure a full refresh is done before the
1515 * subgroup is merged with another.
1517 update_subgroup_set_needs_refresh (subgrp
, 1);
1519 SUBGRP_INCR_STAT (subgrp
, updgrp_switch_events
);
1524 * Create a new subgroup under the specified update group, and copy
1525 * over relevant state to it.
1527 subgrp
= update_subgroup_create (updgrp
);
1528 update_subgroup_inherit_info (subgrp
, old_subgrp
);
1530 subgrp
->split_from
.update_group_id
= old_subgrp
->update_group
->id
;
1531 subgrp
->split_from
.subgroup_id
= old_subgrp
->id
;
1534 * Copy out relevant state from the old subgroup.
1536 update_subgroup_copy_adj_out (paf
->subgroup
, subgrp
);
1537 update_subgroup_copy_packets (subgrp
, paf
->next_pkt_to_send
);
1539 if (BGP_DEBUG (update_groups
, UPDATE_GROUPS
))
1540 zlog_debug ("u%" PRIu64
":s%" PRIu64
" peer %s split and moved into u%" PRIu64
":s%" PRIu64
,
1541 paf
->subgroup
->update_group
->id
, paf
->subgroup
->id
,
1542 paf
->peer
->host
, updgrp
->id
, subgrp
->id
);
1544 SUBGRP_INCR_STAT (paf
->subgroup
, split_events
);
1547 * Since queued advs were left behind, this new subgroup needs a
1550 update_subgroup_set_needs_refresh (subgrp
, 1);
1553 * Remove peer from old subgroup, and add it to the new one.
1555 update_subgroup_remove_peer (paf
->subgroup
, paf
);
1557 update_subgroup_add_peer (subgrp
, paf
, 1);
1561 update_bgp_group_init (struct bgp
*bgp
)
1566 bgp
->update_groups
[afid
] = hash_create (updgrp_hash_key_make
,
1571 update_bgp_group_free (struct bgp
*bgp
)
1577 if (bgp
->update_groups
[afid
])
1579 hash_free(bgp
->update_groups
[afid
]);
1580 bgp
->update_groups
[afid
] = NULL
;
1586 update_group_show (struct bgp
*bgp
, afi_t afi
, safi_t safi
, struct vty
*vty
,
1589 struct updwalk_context ctx
;
1590 memset (&ctx
, 0, sizeof (ctx
));
1592 ctx
.subgrp_id
= subgrp_id
;
1594 update_group_af_walk (bgp
, afi
, safi
, update_group_show_walkcb
, &ctx
);
1598 * update_group_show_stats
1600 * Show global statistics about update groups.
1603 update_group_show_stats (struct bgp
*bgp
, struct vty
*vty
)
1605 vty_out (vty
, "Update groups created: %u%s",
1606 bgp
->update_group_stats
.updgrps_created
, VTY_NEWLINE
);
1607 vty_out (vty
, "Update groups deleted: %u%s",
1608 bgp
->update_group_stats
.updgrps_deleted
, VTY_NEWLINE
);
1609 vty_out (vty
, "Update subgroups created: %u%s",
1610 bgp
->update_group_stats
.subgrps_created
, VTY_NEWLINE
);
1611 vty_out (vty
, "Update subgroups deleted: %u%s",
1612 bgp
->update_group_stats
.subgrps_deleted
, VTY_NEWLINE
);
1613 vty_out (vty
, "Join events: %u%s",
1614 bgp
->update_group_stats
.join_events
, VTY_NEWLINE
);
1615 vty_out (vty
, "Prune events: %u%s",
1616 bgp
->update_group_stats
.prune_events
, VTY_NEWLINE
);
1617 vty_out (vty
, "Merge events: %u%s",
1618 bgp
->update_group_stats
.merge_events
, VTY_NEWLINE
);
1619 vty_out (vty
, "Split events: %u%s",
1620 bgp
->update_group_stats
.split_events
, VTY_NEWLINE
);
1621 vty_out (vty
, "Update group switch events: %u%s",
1622 bgp
->update_group_stats
.updgrp_switch_events
, VTY_NEWLINE
);
1623 vty_out (vty
, "Peer route refreshes combined: %u%s",
1624 bgp
->update_group_stats
.peer_refreshes_combined
, VTY_NEWLINE
);
1625 vty_out (vty
, "Merge checks triggered: %u%s",
1626 bgp
->update_group_stats
.merge_checks_triggered
, VTY_NEWLINE
);
1630 * update_group_adjust_peer
1633 update_group_adjust_peer (struct peer_af
*paf
)
1635 struct update_group
*updgrp
;
1636 struct update_subgroup
*subgrp
, *old_subgrp
;
1642 peer
= PAF_PEER (paf
);
1643 if (!peer_established (peer
))
1648 if (!CHECK_FLAG (peer
->flags
, PEER_FLAG_CONFIG_NODE
))
1653 if (!peer
->afc_nego
[paf
->afi
][paf
->safi
])
1658 updgrp
= update_group_find (paf
);
1661 updgrp
= update_group_create (paf
);
1664 zlog_err ("couldn't create update group for peer %s",
1670 old_subgrp
= paf
->subgroup
;
1676 * If the update group of the peer is unchanged, the peer can stay
1677 * in its existing subgroup and we're done.
1679 if (old_subgrp
->update_group
== updgrp
)
1683 * The peer is switching between update groups. Put it in its
1684 * own subgroup under the new update group.
1686 update_subgroup_split_peer (paf
, updgrp
);
1690 subgrp
= update_subgroup_find (updgrp
, paf
);
1693 subgrp
= update_subgroup_create (updgrp
);
1698 update_subgroup_add_peer (subgrp
, paf
, 1);
1699 if (BGP_DEBUG (update_groups
, UPDATE_GROUPS
))
1700 zlog_debug ("u%" PRIu64
":s%" PRIu64
" add peer %s",
1701 updgrp
->id
, subgrp
->id
, paf
->peer
->host
);
1707 update_group_adjust_soloness (struct peer
*peer
, int set
)
1709 struct peer_group
*group
;
1710 struct listnode
*node
, *nnode
;
1712 if (!CHECK_FLAG (peer
->sflags
, PEER_STATUS_GROUP
))
1714 peer_lonesoul_or_not (peer
, set
);
1715 if (peer
->status
== Established
)
1716 bgp_announce_route_all (peer
);
1720 group
= peer
->group
;
1721 for (ALL_LIST_ELEMENTS (group
->peer
, node
, nnode
, peer
))
1723 peer_lonesoul_or_not (peer
, set
);
1724 if (peer
->status
== Established
)
1725 bgp_announce_route_all (peer
);
1732 * update_subgroup_rib
1735 update_subgroup_rib (struct update_subgroup
*subgrp
)
1739 bgp
= SUBGRP_INST (subgrp
);
1743 return bgp
->rib
[SUBGRP_AFI (subgrp
)][SUBGRP_SAFI (subgrp
)];
1747 update_group_af_walk (struct bgp
*bgp
, afi_t afi
, safi_t safi
,
1748 updgrp_walkcb cb
, void *ctx
)
1750 struct updwalk_context wctx
;
1755 afid
= afindex (afi
, safi
);
1756 if (afid
>= BGP_AF_MAX
)
1759 memset (&wctx
, 0, sizeof (wctx
));
1763 if (bgp
->update_groups
[afid
])
1764 hash_walk (bgp
->update_groups
[afid
], update_group_walkcb
, &wctx
);
1768 update_group_walk (struct bgp
*bgp
, updgrp_walkcb cb
, void *ctx
)
1773 FOREACH_AFI_SAFI (afi
, safi
)
1775 update_group_af_walk (bgp
, afi
, safi
, cb
, ctx
);
1780 update_group_periodic_merge (struct bgp
*bgp
)
1782 char reason
[] = "periodic merge check";
1784 update_group_walk (bgp
, update_group_periodic_merge_walkcb
,
1789 update_group_default_originate_route_map_walkcb(struct update_group
*updgrp
,
1792 struct update_subgroup
*subgrp
;
1797 UPDGRP_FOREACH_SUBGRP (updgrp
, subgrp
)
1799 peer
= SUBGRP_PEER (subgrp
);
1800 afi
= SUBGRP_AFI (subgrp
);
1801 safi
= SUBGRP_SAFI (subgrp
);
1803 if (peer
->default_rmap
[afi
][safi
].name
)
1805 subgroup_default_originate (subgrp
, 0);
1809 return UPDWALK_CONTINUE
;
1813 update_group_refresh_default_originate_route_map (struct thread
*thread
)
1816 char reason
[] = "refresh default-originate route-map";
1818 bgp
= THREAD_ARG(thread
);
1819 update_group_walk (bgp
, update_group_default_originate_route_map_walkcb
,
1821 THREAD_TIMER_OFF (bgp
->t_rmap_def_originate_eval
);
1828 * peer_af_announce_route
1830 * Refreshes routes out to a peer_af immediately.
1832 * If the combine parameter is TRUE, then this function will try to
1833 * gather other peers in the subgroup for which a route announcement
1834 * is pending and efficently announce routes to all of them.
1836 * For now, the 'combine' option has an effect only if all peers in
1837 * the subgroup have a route announcement pending.
1840 peer_af_announce_route (struct peer_af
*paf
, int combine
)
1842 struct update_subgroup
*subgrp
;
1843 struct peer_af
*cur_paf
;
1846 subgrp
= paf
->subgroup
;
1852 * If there are other peers in the old subgroup that also need
1853 * routes to be announced, pull them into the peer's new
1855 * Combine route announcement with other peers if possible.
1857 * For now, we combine only if all peers in the subgroup have an
1858 * announcement pending.
1862 SUBGRP_FOREACH_PEER (subgrp
, cur_paf
)
1867 if (cur_paf
->t_announce_route
)
1875 * Announce to the peer alone if we were not asked to combine peers,
1876 * or if some peers don't have a route annoucement pending.
1878 if (!combine
|| !all_pending
)
1880 update_subgroup_split_peer (paf
, NULL
);
1884 if (bgp_debug_update(paf
->peer
, NULL
, subgrp
->update_group
, 0))
1885 zlog_debug ("u%" PRIu64
":s%" PRIu64
" %s announcing routes",
1886 subgrp
->update_group
->id
, subgrp
->id
, paf
->peer
->host
);
1888 subgroup_announce_route (paf
->subgroup
);
1893 * We will announce routes the entire subgroup.
1895 * First stop refresh timers on all the other peers.
1897 SUBGRP_FOREACH_PEER (subgrp
, cur_paf
)
1902 bgp_stop_announce_route_timer (cur_paf
);
1905 if (bgp_debug_update(paf
->peer
, NULL
, subgrp
->update_group
, 0))
1906 zlog_debug ("u%" PRIu64
":s%" PRIu64
" announcing routes to %s, combined into %d peers",
1907 subgrp
->update_group
->id
, subgrp
->id
,
1908 paf
->peer
->host
, subgrp
->peer_count
);
1910 subgroup_announce_route (subgrp
);
1912 SUBGRP_INCR_STAT_BY (subgrp
, peer_refreshes_combined
,
1913 subgrp
->peer_count
- 1);
1917 subgroup_trigger_write (struct update_subgroup
*subgrp
)
1919 struct peer_af
*paf
;
1922 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
1923 zlog_debug("u%llu:s%llu scheduling write thread for peers",
1924 subgrp
->update_group
->id
, subgrp
->id
);
1926 SUBGRP_FOREACH_PEER (subgrp
, paf
)
1928 if (paf
->peer
->status
== Established
)
1930 BGP_PEER_WRITE_ON (paf
->peer
->t_write
, bgp_write
, paf
->peer
->fd
,
1937 update_group_clear_update_dbg (struct update_group
*updgrp
, void *arg
)
1939 UPDGRP_PEER_DBG_OFF(updgrp
);
1940 return UPDWALK_CONTINUE
;
1943 /* Return true if we should addpath encode NLRI to this peer */
1945 bgp_addpath_encode_tx (struct peer
*peer
, afi_t afi
, safi_t safi
)
1947 return (CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_TX_ADV
) &&
1948 CHECK_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ADDPATH_AF_RX_RCV
));
1952 * Return true if this is a path we should advertise due to a
1953 * configured addpath-tx knob
1956 bgp_addpath_tx_path (struct peer
*peer
, afi_t afi
, safi_t safi
,
1957 struct bgp_info
*ri
)
1959 if (CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_ADDPATH_TX_ALL_PATHS
))
1962 if (CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_ADDPATH_TX_BESTPATH_PER_AS
) &&
1963 CHECK_FLAG (ri
->flags
, BGP_INFO_DMED_SELECTED
))