]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_updgrp.h
Added json formating support to show-...-neighbors-... bgp commands.
[mirror_frr.git] / bgpd / bgp_updgrp.h
1 /**
2 * bgp_updgrp.c: BGP update group structures
3 *
4 * @copyright Copyright (C) 2014 Cumulus Networks, Inc.
5 *
6 * @author Avneesh Sachdev <avneesh@sproute.net>
7 * @author Rajesh Varadarajan <rajesh@sproute.net>
8 * @author Pradosh Mohapatra <pradosh@sproute.net>
9 *
10 * This file is part of GNU Zebra.
11 *
12 * GNU Zebra is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2, or (at your option) any
15 * later version.
16 *
17 * GNU Zebra is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
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
25 * 02111-1307, USA.
26 */
27
28 #ifndef _QUAGGA_BGP_UPDGRP_H
29 #define _QUAGGA_BGP_UPDGRP_H
30
31 #include "bgp_advertise.h"
32
33 #define BGP_DEFAULT_SUBGROUP_COALESCE_TIME 200
34
35 #define PEER_UPDGRP_FLAGS (PEER_FLAG_LOCAL_AS_NO_PREPEND | \
36 PEER_FLAG_LOCAL_AS_REPLACE_AS)
37
38 #define PEER_UPDGRP_AF_FLAGS (PEER_FLAG_SEND_COMMUNITY | \
39 PEER_FLAG_SEND_EXT_COMMUNITY | \
40 PEER_FLAG_DEFAULT_ORIGINATE | \
41 PEER_FLAG_REFLECTOR_CLIENT | \
42 PEER_FLAG_NEXTHOP_SELF | \
43 PEER_FLAG_NEXTHOP_UNCHANGED | \
44 PEER_FLAG_FORCE_NEXTHOP_SELF | \
45 PEER_FLAG_AS_PATH_UNCHANGED | \
46 PEER_FLAG_MED_UNCHANGED | \
47 PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED | \
48 PEER_FLAG_REMOVE_PRIVATE_AS | \
49 PEER_FLAG_REMOVE_PRIVATE_AS_ALL | \
50 PEER_FLAG_REMOVE_PRIVATE_AS_REPLACE | \
51 PEER_FLAG_AS_OVERRIDE)
52
53 #define PEER_UPDGRP_CAP_FLAGS (PEER_CAP_AS4_RCV)
54
55 #define PEER_UPDGRP_AF_CAP_FLAGS (PEER_CAP_ORF_PREFIX_SM_RCV | \
56 PEER_CAP_ORF_PREFIX_SM_OLD_RCV |\
57 PEER_CAP_ENHE_AF_NEGO)
58
59 typedef enum
60 {
61 BGP_ATTR_VEC_NH = 0,
62 BGP_ATTR_VEC_MAX
63 } bpacket_attr_vec_type;
64
65 typedef struct
66 {
67 u_int32_t flags;
68 unsigned long offset;
69 } bpacket_attr_vec;
70
71 #define BPKT_ATTRVEC_FLAGS_UPDATED (1 << 0)
72 #define BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS (1 << 1)
73 #define BPKT_ATTRVEC_FLAGS_REFLECTED (1 << 2)
74 #define BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED (1 << 3)
75 #define BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED (1 << 4)
76 #define BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED (1 << 5)
77 #define BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED (1 << 6)
78
79 typedef struct bpacket_attr_vec_arr
80 {
81 bpacket_attr_vec entries[BGP_ATTR_VEC_MAX];
82 } bpacket_attr_vec_arr;
83
84 struct bpacket
85 {
86 /* for being part of an update subgroup's message list */
87 TAILQ_ENTRY (bpacket) pkt_train;
88
89 /* list of peers (well, peer_afs) that the packet needs to be sent to */
90 LIST_HEAD (pkt_peer_list, peer_af) peers;
91
92 struct stream *buffer;
93 bpacket_attr_vec_arr arr;
94
95 unsigned int ver;
96 };
97
98 struct bpacket_queue
99 {
100 TAILQ_HEAD (pkt_queue, bpacket) pkts;
101
102 #if 0
103 /* A dummy packet that is used to thread all peers that have
104 completed their work */
105 struct bpacket sentinel;
106 #endif
107
108 unsigned int conf_max_count;
109 unsigned int curr_count;
110 unsigned int hwm_count;
111 unsigned int max_count_reached_count;
112 };
113
114 struct update_group
115 {
116 /* back pointer to the BGP instance */
117 struct bgp *bgp;
118
119 /* list of subgroups that belong to the update group */
120 LIST_HEAD (subgrp_list, update_subgroup) subgrps;
121
122 /* lazy way to store configuration common to all peers
123 hash function will compute from this data */
124 struct peer *conf;
125
126 afi_t afi;
127 safi_t safi;
128 int afid;
129
130 u_int64_t id;
131 time_t uptime;
132
133 u_int32_t join_events;
134 u_int32_t prune_events;
135 u_int32_t merge_events;
136 u_int32_t updgrp_switch_events;
137 u_int32_t peer_refreshes_combined;
138 u_int32_t adj_count;
139 u_int32_t split_events;
140 u_int32_t merge_checks_triggered;
141
142 u_int32_t subgrps_created;
143 u_int32_t subgrps_deleted;
144
145 u_int32_t num_dbg_en_peers;
146 };
147
148 /*
149 * Shorthand for a global statistics counter.
150 */
151 #define UPDGRP_GLOBAL_STAT(updgrp, stat) \
152 ((updgrp)->bgp->update_group_stats.stat)
153
154 /*
155 * Add the given value to a counter on an update group and the bgp
156 * instance.
157 */
158 #define UPDGRP_INCR_STAT_BY(updgrp, stat, value) \
159 do { \
160 (updgrp)->stat += (value); \
161 UPDGRP_GLOBAL_STAT(updgrp, stat) += (value); \
162 } while (0)
163
164 /*
165 * Increment a counter on a update group and its parent structures.
166 */
167 #define UPDGRP_INCR_STAT(subgrp, stat) \
168 UPDGRP_INCR_STAT_BY(subgrp, stat, 1)
169
170 struct update_subgroup
171 {
172 /* back pointer to the parent update group */
173 struct update_group *update_group;
174
175 /* list of peers that belong to the subgroup */
176 LIST_HEAD (peer_list, peer_af) peers;
177 int peer_count;
178
179 /* for being part of an update group's subgroup list */
180 LIST_ENTRY (update_subgroup) updgrp_train;
181
182 struct bpacket_queue pkt_queue;
183
184 /*
185 * List of adj-out structures for this subgroup.
186 * It essentially represents the snapshot of every prefix that
187 * has been advertised to the members of the subgroup
188 */
189 TAILQ_HEAD (adjout_queue, bgp_adj_out) adjq;
190
191 /* packet buffer for update generation */
192 struct stream *work;
193
194 /* We use a separate stream to encode MP_REACH_NLRI for efficient
195 * NLRI packing. peer->work stores all the other attributes. The
196 * actual packet is then constructed by concatenating the two.
197 */
198 struct stream *scratch;
199
200 /* synchronization list and time */
201 struct bgp_synchronize *sync;
202
203 /* send prefix count */
204 unsigned long scount;
205
206 /* announcement attribute hash */
207 struct hash *hash;
208
209 struct thread *t_coalesce;
210 u_int32_t v_coalesce;
211
212 struct thread *t_merge_check;
213
214 /* table version that the subgroup has caught up to. */
215 uint64_t version;
216
217 /* version maintained to record adj changes */
218 uint64_t adj_version;
219
220 time_t uptime;
221
222 /*
223 * Identifying information about the subgroup that this subgroup was split
224 * from, if any.
225 */
226 struct
227 {
228 u_int64_t update_group_id;
229 u_int64_t subgroup_id;
230 } split_from;
231
232 u_int32_t join_events;
233 u_int32_t prune_events;
234
235 /*
236 * This is bumped up when another subgroup merges into this one.
237 */
238 u_int32_t merge_events;
239 u_int32_t updgrp_switch_events;
240 u_int32_t peer_refreshes_combined;
241 u_int32_t adj_count;
242 u_int32_t split_events;
243 u_int32_t merge_checks_triggered;
244
245 u_int64_t id;
246 struct zlog *log;
247
248 u_int16_t sflags;
249
250 /* Subgroup flags, see below */
251 u_int16_t flags;
252 };
253
254 /*
255 * We need to do an outbound refresh to get this subgroup into a
256 * consistent state.
257 */
258 #define SUBGRP_FLAG_NEEDS_REFRESH (1 << 0)
259
260 #define SUBGRP_STATUS_DEFAULT_ORIGINATE (1 << 0)
261
262 /*
263 * Add the given value to the specified counter on a subgroup and its
264 * parent structures.
265 */
266 #define SUBGRP_INCR_STAT_BY(subgrp, stat, value) \
267 do { \
268 (subgrp)->stat += (value); \
269 if ((subgrp)->update_group) \
270 UPDGRP_INCR_STAT_BY((subgrp)->update_group, stat, value); \
271 } while (0)
272
273 /*
274 * Increment a counter on a subgroup and its parent structures.
275 */
276 #define SUBGRP_INCR_STAT(subgrp, stat) \
277 SUBGRP_INCR_STAT_BY(subgrp, stat, 1)
278
279 /*
280 * Decrement a counter on a subgroup and its parent structures.
281 */
282 #define SUBGRP_DECR_STAT(subgrp, stat) \
283 SUBGRP_INCR_STAT_BY(subgrp, stat, -1)
284
285
286 typedef int (*updgrp_walkcb) (struct update_group * updgrp, void *ctx);
287
288 /* really a private structure */
289 struct updwalk_context
290 {
291 struct vty *vty;
292 struct bgp_node *rn;
293 struct bgp_info *ri;
294 u_int64_t updgrp_id;
295 u_int64_t subgrp_id;
296 bgp_policy_type_e policy_type;
297 const char *policy_name;
298 int policy_event_start_flag;
299 int policy_route_update;
300 updgrp_walkcb cb;
301 void *context;
302 u_int8_t flags;
303
304 #define UPDWALK_FLAGS_ADVQUEUE (1 << 0)
305 #define UPDWALK_FLAGS_ADVERTISED (1 << 1)
306 };
307
308 #define UPDWALK_CONTINUE HASHWALK_CONTINUE
309 #define UPDWALK_ABORT HASHWALK_ABORT
310
311 #define PAF_PEER(p) ((p)->peer)
312 #define PAF_SUBGRP(p) ((p)->subgroup)
313 #define PAF_UPDGRP(p) ((p)->subgroup->update_group)
314 #define PAF_PKTQ(f) SUBGRP_PKTQ((f)->subgroup)
315
316 #define UPDGRP_PEER(u) ((u)->conf)
317 #define UPDGRP_AFI(u) ((u)->afi)
318 #define UPDGRP_SAFI(u) ((u)->safi)
319 #define UPDGRP_INST(u) ((u)->bgp)
320 #define UPDGRP_AFFLAGS(u) \
321 ((u)->conf->af_flags[UPDGRP_AFI(u)][UPDGRP_SAFI(u)])
322 #define UPDGRP_DBG_ON(u) ((u)->num_dbg_en_peers)
323 #define UPDGRP_PEER_DBG_EN(u) (((u)->num_dbg_en_peers)++)
324 #define UPDGRP_PEER_DBG_DIS(u) (((u)->num_dbg_en_peers)--)
325 #define UPDGRP_PEER_DBG_OFF(u) (u)->num_dbg_en_peers = 0
326
327 #define SUBGRP_AFI(s) UPDGRP_AFI((s)->update_group)
328 #define SUBGRP_SAFI(s) UPDGRP_SAFI((s)->update_group)
329 #define SUBGRP_PEER(s) UPDGRP_PEER((s)->update_group)
330 #define SUBGRP_PCOUNT(s) ((s)->peer_count)
331 #define SUBGRP_PFIRST(s) LIST_FIRST(&((s)->peers))
332 #define SUBGRP_PKTQ(s) &((s)->pkt_queue)
333 #define SUBGRP_INST(s) UPDGRP_INST((s)->update_group)
334 #define SUBGRP_AFFLAGS(s) UPDGRP_AFFLAGS((s)->update_group)
335 #define SUBGRP_UPDGRP(s) ((s)->update_group)
336
337 /*
338 * Walk all subgroups in an update group.
339 */
340 #define UPDGRP_FOREACH_SUBGRP(updgrp, subgrp) \
341 LIST_FOREACH(subgrp, &((updgrp)->subgrps), updgrp_train)
342
343 #define UPDGRP_FOREACH_SUBGRP_SAFE(updgrp, subgrp, tmp_subgrp) \
344 LIST_FOREACH_SAFE(subgrp, &((updgrp)->subgrps), updgrp_train, tmp_subgrp)
345
346 #define SUBGRP_FOREACH_PEER(subgrp, paf) \
347 LIST_FOREACH(paf, &(subgrp->peers), subgrp_train)
348
349 #define SUBGRP_FOREACH_PEER_SAFE(subgrp, paf, temp_paf) \
350 LIST_FOREACH_SAFE(paf, &(subgrp->peers), subgrp_train, temp_paf)
351
352 #define SUBGRP_FOREACH_ADJ(subgrp, adj) \
353 TAILQ_FOREACH(adj, &(subgrp->adjq), subgrp_adj_train)
354
355 #define SUBGRP_FOREACH_ADJ_SAFE(subgrp, adj, adj_temp) \
356 TAILQ_FOREACH_SAFE(adj, &(subgrp->adjq), subgrp_adj_train, adj_temp)
357
358 /* Prototypes. */
359 /* bgp_updgrp.c */
360 extern void update_bgp_group_init (struct bgp *);
361 extern void udpate_bgp_group_free (struct bgp *);
362
363 extern void
364 update_group_show (struct bgp *bgp, afi_t afi, safi_t safi, struct vty *vty, u_int64_t subgrp_id);
365 extern void update_group_show_stats (struct bgp *bgp, struct vty *vty);
366 extern void update_group_adjust_peer (struct peer_af *paf);
367 extern int update_group_adjust_soloness (struct peer *peer, int set);
368
369 extern void
370 update_subgroup_remove_peer (struct update_subgroup *, struct peer_af *);
371 extern struct bgp_table *update_subgroup_rib (struct update_subgroup *);
372 extern void
373 update_subgroup_split_peer (struct peer_af *, struct update_group *);
374 extern int
375 update_subgroup_check_merge (struct update_subgroup *, const char *);
376 extern int
377 update_subgroup_trigger_merge_check (struct update_subgroup *,
378 int force);
379 extern void update_group_policy_update (struct bgp *bgp,
380 bgp_policy_type_e ptype, const char *pname,
381 int route_update, int start_event);
382 extern void update_group_af_walk (struct bgp *bgp, afi_t afi, safi_t safi,
383 updgrp_walkcb cb, void *ctx);
384 extern void update_group_walk (struct bgp *bgp, updgrp_walkcb cb, void *ctx);
385 extern void update_group_periodic_merge (struct bgp *bgp);
386 extern int update_group_refresh_default_originate_route_map (struct thread *thread);
387 extern void update_group_start_advtimer (struct bgp *bgp);
388
389 extern void update_subgroup_inherit_info (struct update_subgroup *to,
390 struct update_subgroup *from);
391
392 /* bgp_updgrp_packet.c */
393 extern struct bpacket *bpacket_alloc (void);
394 extern void bpacket_free (struct bpacket *pkt);
395 extern void bpacket_queue_init (struct bpacket_queue *q);
396 extern void bpacket_queue_cleanup (struct bpacket_queue *q);
397 extern void bpacket_queue_sanity_check (struct bpacket_queue *q);
398 extern struct bpacket *bpacket_queue_add (struct bpacket_queue *q,
399 struct stream *s,
400 struct bpacket_attr_vec_arr
401 *vecarr);
402 struct bpacket *bpacket_queue_remove (struct bpacket_queue *q);
403 extern struct bpacket *bpacket_queue_first (struct bpacket_queue *q);
404 struct bpacket *bpacket_queue_last (struct bpacket_queue *q);
405 unsigned int bpacket_queue_length (struct bpacket_queue *q);
406 unsigned int bpacket_queue_hwm_length (struct bpacket_queue *q);
407 int bpacket_queue_is_full (struct bgp *bgp, struct bpacket_queue *q);
408 extern void bpacket_queue_advance_peer (struct peer_af *paf);
409 extern void bpacket_queue_remove_peer (struct peer_af *paf);
410 extern void bpacket_add_peer (struct bpacket *pkt, struct peer_af *paf);
411 unsigned int bpacket_queue_virtual_length (struct peer_af *paf);
412 extern void bpacket_queue_show_vty (struct bpacket_queue *q, struct vty *vty);
413 int subgroup_packets_to_build (struct update_subgroup *subgrp);
414 extern struct bpacket *subgroup_update_packet (struct update_subgroup *s);
415 extern struct bpacket *subgroup_withdraw_packet (struct update_subgroup *s);
416 extern struct stream *bpacket_reformat_for_peer (struct bpacket *pkt,
417 struct peer_af *paf);
418 extern void bpacket_attr_vec_arr_reset (struct bpacket_attr_vec_arr *vecarr);
419 extern void bpacket_attr_vec_arr_set_vec (struct bpacket_attr_vec_arr *vecarr,
420 bpacket_attr_vec_type type,
421 struct stream *s,
422 struct attr *attr);
423 extern void
424 subgroup_default_update_packet (struct update_subgroup *subgrp,
425 struct attr *attr, struct peer *from);
426 extern void subgroup_default_withdraw_packet (struct update_subgroup *subgrp);
427
428 /* bgp_updgrp_adv.c */
429 extern struct bgp_advertise *bgp_advertise_clean_subgroup (struct
430 update_subgroup
431 *subgrp,
432 struct bgp_adj_out
433 *adj);
434 extern void update_group_show_adj_queue (struct bgp *bgp, afi_t afi,
435 safi_t safi, struct vty *vty,
436 u_int64_t id);
437 extern void update_group_show_advertised (struct bgp *bgp, afi_t afi,
438 safi_t safi, struct vty *vty,
439 u_int64_t id);
440 extern void update_group_show_packet_queue (struct bgp *bgp, afi_t afi,
441 safi_t safi, struct vty *vty,
442 u_int64_t id);
443 extern void subgroup_announce_route (struct update_subgroup *subgrp);
444 extern void subgroup_announce_all (struct update_subgroup *subgrp);
445
446 extern void
447 subgroup_default_originate (struct update_subgroup *subgrp, int withdraw);
448 extern void
449 group_announce_route (struct bgp *bgp, afi_t afi, safi_t safi,
450 struct bgp_node *rn, struct bgp_info *ri);
451 extern void subgroup_clear_table (struct update_subgroup *subgrp);
452 extern void update_group_announce (struct bgp *bgp);
453 extern void update_group_announce_rrclients (struct bgp *bgp);
454 extern void peer_af_announce_route (struct peer_af *paf, int combine);
455 extern struct bgp_adj_out *bgp_adj_out_alloc (struct update_subgroup *subgrp,
456 struct bgp_node *rn);
457 extern void bgp_adj_out_remove_subgroup (struct bgp_node *rn,
458 struct bgp_adj_out *adj,
459 struct update_subgroup *subgrp);
460 extern void
461 bgp_adj_out_set_subgroup (struct bgp_node *rn,
462 struct update_subgroup *subgrp,
463 struct attr *attr, struct bgp_info *binfo);
464 extern void
465 bgp_adj_out_unset_subgroup (struct bgp_node *rn,
466 struct update_subgroup *subgrp,
467 char withdraw);
468 void
469 subgroup_announce_table (struct update_subgroup *subgrp,
470 struct bgp_table *table, int rsclient);
471 extern void
472 subgroup_trigger_write (struct update_subgroup *subgrp);
473
474 extern int
475 update_group_clear_update_dbg (struct update_group *updgrp, void *arg);
476
477 extern void update_bgp_group_free(struct bgp *bgp);
478
479 /*
480 * Inline functions
481 */
482
483 /*
484 * bpacket_queue_is_empty
485 */
486 static inline int
487 bpacket_queue_is_empty (struct bpacket_queue *queue)
488 {
489
490 /*
491 * The packet queue is empty if it only contains a sentinel.
492 */
493 if (queue->curr_count != 1)
494 return 0;
495
496 assert (bpacket_queue_first (queue)->buffer == NULL);
497 return 1;
498 }
499
500 /*
501 * bpacket_next
502 *
503 * Returns the packet after the given packet in a bpacket queue.
504 */
505 static inline struct bpacket *
506 bpacket_next (struct bpacket *pkt)
507 {
508 return TAILQ_NEXT (pkt, pkt_train);
509 }
510
511 /*
512 * update_group_adjust_peer_afs
513 *
514 * Adjust all peer_af structures for the given peer.
515 */
516 static inline void
517 update_group_adjust_peer_afs (struct peer *peer)
518 {
519 struct peer_af *paf;
520 afi_t afi;
521
522 PEERAF_FOREACH (peer, paf, afi) update_group_adjust_peer (paf);
523 }
524
525 /*
526 * update_group_remove_peer_afs
527 *
528 * Remove all peer_af structures for the given peer from their subgroups.
529 */
530 static inline void
531 update_group_remove_peer_afs (struct peer *peer)
532 {
533 struct peer_af *paf;
534 afi_t afi;
535
536 PEERAF_FOREACH (peer, paf, afi)
537 update_subgroup_remove_peer (PAF_SUBGRP (paf), paf);
538 }
539
540 /*
541 * update_subgroup_needs_refresh
542 */
543 static inline int
544 update_subgroup_needs_refresh (const struct update_subgroup *subgrp)
545 {
546 if (CHECK_FLAG (subgrp->flags, SUBGRP_FLAG_NEEDS_REFRESH))
547 return 1;
548 else
549 return 0;
550 }
551
552 /*
553 * update_subgroup_set_needs_refresh
554 */
555 static inline void
556 update_subgroup_set_needs_refresh (struct update_subgroup *subgrp, int value)
557 {
558 if (value)
559 SET_FLAG (subgrp->flags, SUBGRP_FLAG_NEEDS_REFRESH);
560 else
561 UNSET_FLAG (subgrp->flags, SUBGRP_FLAG_NEEDS_REFRESH);
562 }
563
564 static inline struct update_subgroup *
565 peer_subgroup (struct peer *peer, afi_t afi, safi_t safi)
566 {
567 struct peer_af *paf;
568
569 paf = peer_af_find (peer, afi, safi);
570 if (paf)
571 return PAF_SUBGRP (paf);
572 return NULL;
573 }
574
575 /*
576 * update_group_adjust_peer_afs
577 *
578 * Adjust all peer_af structures for the given peer.
579 */
580 static inline void
581 bgp_announce_peer (struct peer *peer)
582 {
583 struct peer_af *paf;
584 int af;
585
586 PEERAF_FOREACH (peer, paf, af) subgroup_announce_all (PAF_SUBGRP (paf));
587 }
588
589 /**
590 * advertise_list_is_empty
591 */
592 static inline int
593 advertise_list_is_empty (struct update_subgroup *subgrp)
594 {
595 if (!BGP_ADV_FIFO_EMPTY (&subgrp->sync->update) ||
596 !BGP_ADV_FIFO_EMPTY (&subgrp->sync->withdraw) ||
597 !BGP_ADV_FIFO_EMPTY (&subgrp->sync->withdraw_low))
598 {
599 return 0;
600 }
601
602 return 1;
603 }
604
605 #endif /* _QUAGGA_BGP_UPDGRP_H */