forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbgp_fsm.c
3077 lines (2658 loc) · 90.1 KB
/
bgp_fsm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/* BGP-4 Finite State Machine
* From RFC1771 [A Border Gateway Protocol 4 (BGP-4)]
* Copyright (C) 1996, 97, 98 Kunihiro Ishiguro
*/
#include <zebra.h>
#include "linklist.h"
#include "prefix.h"
#include "sockunion.h"
#include "frrevent.h"
#include "log.h"
#include "stream.h"
#include "ringbuf.h"
#include "memory.h"
#include "plist.h"
#include "workqueue.h"
#include "queue.h"
#include "filter.h"
#include "command.h"
#include "lib_errors.h"
#include "zclient.h"
#include "lib/json.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_errors.h"
#include "bgpd/bgp_fsm.h"
#include "bgpd/bgp_packet.h"
#include "bgpd/bgp_network.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_dump.h"
#include "bgpd/bgp_open.h"
#include "bgpd/bgp_advertise.h"
#include "bgpd/bgp_community.h"
#include "bgpd/bgp_updgrp.h"
#include "bgpd/bgp_nht.h"
#include "bgpd/bgp_bfd.h"
#include "bgpd/bgp_memory.h"
#include "bgpd/bgp_keepalives.h"
#include "bgpd/bgp_io.h"
#include "bgpd/bgp_zebra.h"
#include "bgpd/bgp_vty.h"
DEFINE_HOOK(peer_backward_transition, (struct peer * peer), (peer));
DEFINE_HOOK(peer_status_changed, (struct peer * peer), (peer));
/* Definition of display strings corresponding to FSM events. This should be
* kept consistent with the events defined in bgpd.h
*/
static const char *const bgp_event_str[] = {
NULL,
"BGP_Start",
"BGP_Stop",
"TCP_connection_open",
"TCP_connection_open_w_delay",
"TCP_connection_closed",
"TCP_connection_open_failed",
"TCP_fatal_error",
"ConnectRetry_timer_expired",
"Hold_Timer_expired",
"KeepAlive_timer_expired",
"DelayOpen_timer_expired",
"Receive_OPEN_message",
"Receive_KEEPALIVE_message",
"Receive_UPDATE_message",
"Receive_NOTIFICATION_message",
"Clearing_Completed",
};
/* BGP FSM (finite state machine) has three types of functions. Type
one is thread functions. Type two is event functions. Type three
is FSM functions. Timer functions are set by bgp_timer_set
function. */
/* BGP event function. */
void bgp_event(struct event *event);
/* BGP thread functions. */
static void bgp_start_timer(struct event *event);
static void bgp_connect_timer(struct event *event);
static void bgp_holdtime_timer(struct event *event);
static void bgp_delayopen_timer(struct event *event);
/* Register peer with NHT */
int bgp_peer_reg_with_nht(struct peer *peer)
{
int connected = 0;
if (peer->sort == BGP_PEER_EBGP && peer->ttl == BGP_DEFAULT_TTL
&& !CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)
&& !CHECK_FLAG(peer->bgp->flags, BGP_FLAG_DISABLE_NH_CONNECTED_CHK))
connected = 1;
return bgp_find_or_add_nexthop(peer->bgp, peer->bgp,
family2afi(
peer->connection->su.sa.sa_family),
SAFI_UNICAST, NULL, peer, connected,
NULL);
}
static void peer_xfer_stats(struct peer *peer_dst, struct peer *peer_src)
{
/* Copy stats over. These are only the pre-established state stats */
peer_dst->open_in += peer_src->open_in;
peer_dst->open_out += peer_src->open_out;
peer_dst->keepalive_in += peer_src->keepalive_in;
peer_dst->keepalive_out += peer_src->keepalive_out;
peer_dst->notify_in += peer_src->notify_in;
peer_dst->notify_out += peer_src->notify_out;
peer_dst->dynamic_cap_in += peer_src->dynamic_cap_in;
peer_dst->dynamic_cap_out += peer_src->dynamic_cap_out;
}
static struct peer *peer_xfer_conn(struct peer *from_peer)
{
struct peer *peer;
afi_t afi;
safi_t safi;
enum bgp_fsm_events last_evt, last_maj_evt;
struct peer_connection *keeper, *going_away;
assert(from_peer != NULL);
/*
* Keeper is the connection that is staying around
*/
keeper = from_peer->connection;
peer = from_peer->doppelganger;
if (!peer || !CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
return from_peer;
/*
* from_peer is pointing at the non config node and
* at this point peer is pointing at the CONFIG node
* peer ( non incoming connection ). The going_away pointer
* is the connection that is being placed on to
* the non Config node for deletion.
*/
going_away = peer->connection;
/*
* Let's check that we are not going to loose known configuration
* state based upon doppelganger rules.
*/
FOREACH_AFI_SAFI (afi, safi) {
if (from_peer->afc[afi][safi] != peer->afc[afi][safi]) {
flog_err(
EC_BGP_DOPPELGANGER_CONFIG,
"from_peer->afc[%d][%d] is not the same as what we are overwriting",
afi, safi);
return NULL;
}
}
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s: peer transfer %p fd %d -> %p fd %d)",
from_peer->host, from_peer, from_peer->connection->fd,
peer, peer->connection->fd);
bgp_writes_off(going_away);
bgp_reads_off(going_away);
bgp_writes_off(keeper);
bgp_reads_off(keeper);
/*
* Before exchanging FD remove doppelganger from
* keepalive peer hash. It could be possible conf peer
* fd is set to -1. If blocked on lock then keepalive
* thread can access peer pointer with fd -1.
*/
bgp_keepalives_off(keeper);
EVENT_OFF(going_away->t_routeadv);
EVENT_OFF(going_away->t_connect);
EVENT_OFF(going_away->t_delayopen);
EVENT_OFF(going_away->t_connect_check_r);
EVENT_OFF(going_away->t_connect_check_w);
EVENT_OFF(going_away->t_stop_with_notify);
EVENT_OFF(keeper->t_routeadv);
EVENT_OFF(keeper->t_connect);
EVENT_OFF(keeper->t_delayopen);
EVENT_OFF(keeper->t_connect_check_r);
EVENT_OFF(keeper->t_connect_check_w);
EVENT_OFF(keeper->t_process_packet);
/*
* At this point in time, it is possible that there are packets pending
* on various buffers. Those need to be transferred or dropped,
* otherwise we'll get spurious failures during session establishment.
*/
peer->connection = keeper;
keeper->peer = peer;
from_peer->connection = going_away;
going_away->peer = from_peer;
peer->as = from_peer->as;
peer->v_holdtime = from_peer->v_holdtime;
peer->v_keepalive = from_peer->v_keepalive;
peer->v_routeadv = from_peer->v_routeadv;
peer->v_delayopen = from_peer->v_delayopen;
peer->v_gr_restart = from_peer->v_gr_restart;
peer->cap = from_peer->cap;
peer->remote_role = from_peer->remote_role;
last_evt = peer->last_event;
last_maj_evt = peer->last_major_event;
peer->last_event = from_peer->last_event;
peer->last_major_event = from_peer->last_major_event;
from_peer->last_event = last_evt;
from_peer->last_major_event = last_maj_evt;
peer->remote_id = from_peer->remote_id;
peer->last_reset = from_peer->last_reset;
peer->max_packet_size = from_peer->max_packet_size;
BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp,
peer->bgp->peer);
if (bgp_peer_gr_mode_get(peer) == PEER_DISABLE) {
UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE);
if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) {
peer_nsf_stop(peer);
}
}
if (peer->hostname) {
XFREE(MTYPE_BGP_PEER_HOST, peer->hostname);
peer->hostname = NULL;
}
if (from_peer->hostname != NULL) {
peer->hostname = from_peer->hostname;
from_peer->hostname = NULL;
}
if (peer->domainname) {
XFREE(MTYPE_BGP_PEER_HOST, peer->domainname);
peer->domainname = NULL;
}
if (from_peer->domainname != NULL) {
peer->domainname = from_peer->domainname;
from_peer->domainname = NULL;
}
if (peer->soft_version) {
XFREE(MTYPE_BGP_SOFT_VERSION, peer->soft_version);
peer->soft_version = NULL;
}
if (from_peer->soft_version) {
peer->soft_version = from_peer->soft_version;
from_peer->soft_version = NULL;
}
FOREACH_AFI_SAFI (afi, safi) {
peer->af_sflags[afi][safi] = from_peer->af_sflags[afi][safi];
peer->af_cap[afi][safi] = from_peer->af_cap[afi][safi];
peer->afc_nego[afi][safi] = from_peer->afc_nego[afi][safi];
peer->afc_adv[afi][safi] = from_peer->afc_adv[afi][safi];
peer->afc_recv[afi][safi] = from_peer->afc_recv[afi][safi];
peer->orf_plist[afi][safi] = from_peer->orf_plist[afi][safi];
peer->llgr[afi][safi] = from_peer->llgr[afi][safi];
peer->addpath_paths_limit[afi][safi] =
from_peer->addpath_paths_limit[afi][safi];
}
if (bgp_getsockname(keeper) < 0) {
flog_err(EC_LIB_SOCKET,
"%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)",
(CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)
? "accept"
: ""),
peer->host, going_away->fd, keeper->fd);
BGP_EVENT_ADD(going_away, BGP_Stop);
BGP_EVENT_ADD(keeper, BGP_Stop);
return NULL;
}
if (going_away->status > Active) {
if (bgp_getsockname(going_away) < 0) {
flog_err(EC_LIB_SOCKET,
"%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)",
(CHECK_FLAG(from_peer->sflags,
PEER_STATUS_ACCEPT_PEER)
? "accept"
: ""),
from_peer->host, going_away->fd, keeper->fd);
bgp_stop(going_away);
from_peer = NULL;
}
}
// Note: peer_xfer_stats() must be called with I/O turned OFF
if (from_peer)
peer_xfer_stats(peer, from_peer);
/* Register peer for NHT. This is to allow RAs to be enabled when
* needed, even on a passive connection.
*/
bgp_peer_reg_with_nht(peer);
if (from_peer)
bgp_replace_nexthop_by_peer(from_peer, peer);
bgp_reads_on(keeper);
bgp_writes_on(keeper);
event_add_event(bm->master, bgp_process_packet, keeper, 0,
&keeper->t_process_packet);
return (peer);
}
/* Hook function called after bgp event is occered. And vty's
neighbor command invoke this function after making neighbor
structure. */
void bgp_timer_set(struct peer_connection *connection)
{
afi_t afi;
safi_t safi;
struct peer *peer = connection->peer;
switch (connection->status) {
case Idle:
/* First entry point of peer's finite state machine. In Idle
status start timer is on unless peer is shutdown or peer is
inactive. All other timer must be turned off */
if (BGP_PEER_START_SUPPRESSED(peer) || !peer_active(connection) ||
peer->bgp->vrf_id == VRF_UNKNOWN) {
EVENT_OFF(connection->t_start);
} else {
BGP_TIMER_ON(connection->t_start, bgp_start_timer,
peer->v_start);
}
EVENT_OFF(connection->t_connect);
EVENT_OFF(connection->t_holdtime);
bgp_keepalives_off(connection);
EVENT_OFF(connection->t_routeadv);
EVENT_OFF(connection->t_delayopen);
break;
case Connect:
/* After start timer is expired, the peer moves to Connect
status. Make sure start timer is off and connect timer is
on. */
EVENT_OFF(connection->t_start);
if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER_DELAYOPEN))
BGP_TIMER_ON(connection->t_connect, bgp_connect_timer,
(peer->v_delayopen + peer->v_connect));
else
BGP_TIMER_ON(connection->t_connect, bgp_connect_timer,
peer->v_connect);
EVENT_OFF(connection->t_holdtime);
bgp_keepalives_off(connection);
EVENT_OFF(connection->t_routeadv);
break;
case Active:
/* Active is waiting connection from remote peer. And if
connect timer is expired, change status to Connect. */
EVENT_OFF(connection->t_start);
/* If peer is passive mode, do not set connect timer. */
if (CHECK_FLAG(peer->flags, PEER_FLAG_PASSIVE)
|| CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) {
EVENT_OFF(connection->t_connect);
} else {
if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER_DELAYOPEN))
BGP_TIMER_ON(connection->t_connect,
bgp_connect_timer,
(peer->v_delayopen +
peer->v_connect));
else
BGP_TIMER_ON(connection->t_connect,
bgp_connect_timer, peer->v_connect);
}
EVENT_OFF(connection->t_holdtime);
bgp_keepalives_off(connection);
EVENT_OFF(connection->t_routeadv);
break;
case OpenSent:
/* OpenSent status. */
EVENT_OFF(connection->t_start);
EVENT_OFF(connection->t_connect);
if (peer->v_holdtime != 0) {
BGP_TIMER_ON(connection->t_holdtime, bgp_holdtime_timer,
peer->v_holdtime);
} else {
EVENT_OFF(connection->t_holdtime);
}
bgp_keepalives_off(connection);
EVENT_OFF(connection->t_routeadv);
EVENT_OFF(connection->t_delayopen);
break;
case OpenConfirm:
/* OpenConfirm status. */
EVENT_OFF(connection->t_start);
EVENT_OFF(connection->t_connect);
/*
* If the negotiated Hold Time value is zero, then the Hold Time
* timer and KeepAlive timers are not started.
* Additionally if a different hold timer has been negotiated
* than we must stop then start the timer again
*/
EVENT_OFF(connection->t_holdtime);
if (peer->v_holdtime == 0)
bgp_keepalives_off(connection);
else {
BGP_TIMER_ON(connection->t_holdtime, bgp_holdtime_timer,
peer->v_holdtime);
bgp_keepalives_on(connection);
}
EVENT_OFF(connection->t_routeadv);
EVENT_OFF(connection->t_delayopen);
break;
case Established:
/* In Established status start and connect timer is turned
off. */
EVENT_OFF(connection->t_start);
EVENT_OFF(connection->t_connect);
EVENT_OFF(connection->t_delayopen);
/*
* Same as OpenConfirm, if holdtime is zero then both holdtime
* and keepalive must be turned off.
* Additionally if a different hold timer has been negotiated
* then we must stop then start the timer again
*/
EVENT_OFF(connection->t_holdtime);
if (peer->v_holdtime == 0)
bgp_keepalives_off(connection);
else {
BGP_TIMER_ON(connection->t_holdtime, bgp_holdtime_timer,
peer->v_holdtime);
bgp_keepalives_on(connection);
}
break;
case Deleted:
EVENT_OFF(peer->connection->t_gr_restart);
EVENT_OFF(peer->connection->t_gr_stale);
FOREACH_AFI_SAFI (afi, safi)
EVENT_OFF(peer->t_llgr_stale[afi][safi]);
EVENT_OFF(peer->connection->t_pmax_restart);
EVENT_OFF(peer->t_refresh_stalepath);
fallthrough;
case Clearing:
EVENT_OFF(connection->t_start);
EVENT_OFF(connection->t_connect);
EVENT_OFF(connection->t_holdtime);
bgp_keepalives_off(connection);
EVENT_OFF(connection->t_routeadv);
EVENT_OFF(connection->t_delayopen);
break;
case BGP_STATUS_MAX:
flog_err(EC_LIB_DEVELOPMENT,
"BGP_STATUS_MAX while a legal state is not valid state for the FSM");
break;
}
}
/* BGP start timer. This function set BGP_Start event to thread value
and process event. */
static void bgp_start_timer(struct event *thread)
{
struct peer_connection *connection = EVENT_ARG(thread);
struct peer *peer = connection->peer;
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s [FSM] Timer (start timer expire).", peer->host);
EVENT_VAL(thread) = BGP_Start;
bgp_event(thread); /* bgp_event unlocks peer */
}
/* BGP connect retry timer. */
static void bgp_connect_timer(struct event *thread)
{
struct peer_connection *connection = EVENT_ARG(thread);
struct peer *peer = connection->peer;
/* stop the DelayOpenTimer if it is running */
EVENT_OFF(connection->t_delayopen);
assert(!connection->t_write);
assert(!connection->t_read);
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s [FSM] Timer (connect timer expire)", peer->host);
if (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER))
bgp_stop(connection);
else {
EVENT_VAL(thread) = ConnectRetry_timer_expired;
bgp_event(thread); /* bgp_event unlocks peer */
}
}
/* BGP holdtime timer. */
static void bgp_holdtime_timer(struct event *thread)
{
atomic_size_t inq_count;
struct peer_connection *connection = EVENT_ARG(thread);
struct peer *peer = connection->peer;
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s [FSM] Timer (holdtime timer expire)",
peer->host);
/*
* Given that we do not have any expectation of ordering
* for handling packets from a peer -vs- handling
* the hold timer for a peer as that they are both
* events on the peer. If we have incoming
* data on the peers inq, let's give the system a chance
* to handle that data. This can be especially true
* for systems where we are heavily loaded for one
* reason or another.
*/
inq_count = atomic_load_explicit(&connection->ibuf->count,
memory_order_relaxed);
if (inq_count)
BGP_TIMER_ON(connection->t_holdtime, bgp_holdtime_timer,
peer->v_holdtime);
EVENT_VAL(thread) = Hold_Timer_expired;
bgp_event(thread); /* bgp_event unlocks peer */
}
void bgp_routeadv_timer(struct event *thread)
{
struct peer_connection *connection = EVENT_ARG(thread);
struct peer *peer = connection->peer;
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s [FSM] Timer (routeadv timer expire)", peer->host);
peer->synctime = monotime(NULL);
event_add_timer_msec(bm->master, bgp_generate_updgrp_packets, connection,
0, &connection->t_generate_updgrp_packets);
/* MRAI timer will be started again when FIFO is built, no need to
* do it here.
*/
}
/* RFC 4271 DelayOpenTimer */
void bgp_delayopen_timer(struct event *thread)
{
struct peer_connection *connection = EVENT_ARG(thread);
struct peer *peer = connection->peer;
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s [FSM] Timer (DelayOpentimer expire)",
peer->host);
EVENT_VAL(thread) = DelayOpen_timer_expired;
bgp_event(thread); /* bgp_event unlocks peer */
}
/* BGP Peer Down Cause */
const char *const peer_down_str[] = {
"",
"Router ID changed",
"Remote AS changed",
"Local AS change",
"Cluster ID changed",
"Confederation identifier changed",
"Confederation peer changed",
"RR client config change",
"RS client config change",
"Update source change",
"Address family activated",
"Admin. shutdown",
"User reset",
"BGP Notification received",
"BGP Notification send",
"Peer closed the session",
"Neighbor deleted",
"Peer-group add member",
"Peer-group delete member",
"Capability changed",
"Passive config change",
"Multihop config change",
"NSF peer closed the session",
"Intf peering v6only config change",
"BFD down received",
"Interface down",
"Neighbor address lost",
"No path to specified Neighbor",
"Waiting for Peer IPv6 LLA",
"Waiting for VRF to be initialized",
"No AFI/SAFI activated for peer",
"AS Set config change",
"Waiting for peer OPEN",
"Reached received prefix count",
"Socket Error",
"Admin. shutdown (RTT)",
"Suppress Fib Turned On or Off",
"Password config change",
};
static void bgp_graceful_restart_timer_off(struct peer_connection *connection,
struct peer *peer)
{
afi_t afi;
safi_t safi;
FOREACH_AFI_SAFI (afi, safi)
if (CHECK_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_LLGR_WAIT))
return;
UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
EVENT_OFF(connection->t_gr_stale);
if (peer_dynamic_neighbor(peer) &&
!(CHECK_FLAG(peer->flags, PEER_FLAG_DELETE))) {
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s (dynamic neighbor) deleted (%s)",
peer->host, __func__);
peer_delete(peer);
}
bgp_timer_set(connection);
}
static void bgp_llgr_stale_timer_expire(struct event *thread)
{
struct peer_af *paf;
struct peer *peer;
afi_t afi;
safi_t safi;
paf = EVENT_ARG(thread);
peer = paf->peer;
afi = paf->afi;
safi = paf->safi;
/* If the timer for the "Long-lived Stale Time" expires before the
* session is re-established, the helper MUST delete all the
* stale routes from the neighbor that it is retaining.
*/
if (bgp_debug_neighbor_events(peer))
zlog_debug("%pBP Long-lived stale timer (%s) expired", peer,
get_afi_safi_str(afi, safi, false));
UNSET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_LLGR_WAIT);
bgp_clear_stale_route(peer, afi, safi);
bgp_graceful_restart_timer_off(peer->connection, peer);
}
static void bgp_set_llgr_stale(struct peer *peer, afi_t afi, safi_t safi)
{
struct bgp_dest *dest;
struct bgp_path_info *pi;
struct bgp_table *table;
struct attr attr;
if (safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP || safi == SAFI_EVPN) {
for (dest = bgp_table_top(peer->bgp->rib[afi][safi]); dest;
dest = bgp_route_next(dest)) {
struct bgp_dest *rm;
table = bgp_dest_get_bgp_table_info(dest);
if (!table)
continue;
for (rm = bgp_table_top(table); rm;
rm = bgp_route_next(rm))
for (pi = bgp_dest_get_bgp_path_info(rm); pi;
pi = pi->next) {
if (pi->peer != peer)
continue;
if (bgp_attr_get_community(pi->attr) &&
community_include(
bgp_attr_get_community(
pi->attr),
COMMUNITY_NO_LLGR))
continue;
if (bgp_attr_get_community(pi->attr) &&
community_include(bgp_attr_get_community(pi->attr),
COMMUNITY_LLGR_STALE))
continue;
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP Long-lived set stale community (LLGR_STALE) for: %pFX",
peer, &dest->rn->p);
attr = *pi->attr;
bgp_attr_add_llgr_community(&attr);
pi->attr = bgp_attr_intern(&attr);
bgp_process(peer->bgp, rm, pi, afi,
safi);
}
}
} else {
for (dest = bgp_table_top(peer->bgp->rib[afi][safi]); dest;
dest = bgp_route_next(dest))
for (pi = bgp_dest_get_bgp_path_info(dest); pi;
pi = pi->next) {
if (pi->peer != peer)
continue;
if (bgp_attr_get_community(pi->attr) &&
community_include(
bgp_attr_get_community(pi->attr),
COMMUNITY_NO_LLGR))
continue;
if (bgp_attr_get_community(pi->attr) &&
community_include(bgp_attr_get_community(pi->attr),
COMMUNITY_LLGR_STALE))
continue;
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP Long-lived set stale community (LLGR_STALE) for: %pFX",
peer, &dest->rn->p);
attr = *pi->attr;
bgp_attr_add_llgr_community(&attr);
pi->attr = bgp_attr_intern(&attr);
bgp_process(peer->bgp, dest, pi, afi, safi);
}
}
}
static void bgp_graceful_restart_timer_expire(struct event *thread)
{
struct peer_connection *connection = EVENT_ARG(thread);
struct peer *peer = connection->peer;
struct peer *tmp_peer;
struct listnode *node, *nnode;
struct peer_af *paf;
afi_t afi;
safi_t safi;
if (bgp_debug_neighbor_events(peer)) {
zlog_debug("%pBP graceful restart timer expired", peer);
zlog_debug("%pBP graceful restart stalepath timer stopped",
peer);
}
FOREACH_AFI_SAFI (afi, safi) {
if (!peer->nsf[afi][safi])
continue;
/* Once the "Restart Time" period ends, the LLGR period is
* said to have begun and the following procedures MUST be
* performed:
*
* The helper router MUST start a timer for the
* "Long-lived Stale Time".
*
* The helper router MUST attach the LLGR_STALE community
* for the stale routes being retained. Note that this
* requirement implies that the routes would need to be
* readvertised, to disseminate the modified community.
*/
if (peer->llgr[afi][safi].stale_time) {
paf = peer_af_find(peer, afi, safi);
if (!paf)
continue;
if (bgp_debug_neighbor_events(peer))
zlog_debug(
"%pBP Long-lived stale timer (%s) started for %d sec",
peer,
get_afi_safi_str(afi, safi, false),
peer->llgr[afi][safi].stale_time);
SET_FLAG(peer->af_sflags[afi][safi],
PEER_STATUS_LLGR_WAIT);
bgp_set_llgr_stale(peer, afi, safi);
bgp_clear_stale_route(peer, afi, safi);
event_add_timer(bm->master, bgp_llgr_stale_timer_expire,
paf, peer->llgr[afi][safi].stale_time,
&peer->t_llgr_stale[afi][safi]);
for (ALL_LIST_ELEMENTS(peer->bgp->peer, node, nnode,
tmp_peer))
bgp_announce_route(tmp_peer, afi, safi, false);
} else {
bgp_clear_stale_route(peer, afi, safi);
}
}
bgp_graceful_restart_timer_off(connection, peer);
}
static void bgp_graceful_stale_timer_expire(struct event *thread)
{
struct peer_connection *connection = EVENT_ARG(thread);
struct peer *peer = connection->peer;
afi_t afi;
safi_t safi;
if (bgp_debug_neighbor_events(peer))
zlog_debug("%pBP graceful restart stalepath timer expired",
peer);
/* NSF delete stale route */
FOREACH_AFI_SAFI_NSF (afi, safi)
if (peer->nsf[afi][safi])
bgp_clear_stale_route(peer, afi, safi);
}
/* Selection deferral timer processing function */
static void bgp_graceful_deferral_timer_expire(struct event *thread)
{
struct afi_safi_info *info;
afi_t afi;
safi_t safi;
struct bgp *bgp;
info = EVENT_ARG(thread);
afi = info->afi;
safi = info->safi;
bgp = info->bgp;
if (BGP_DEBUG(update, UPDATE_OUT))
zlog_debug(
"afi %d, safi %d : graceful restart deferral timer expired",
afi, safi);
bgp->gr_info[afi][safi].eor_required = 0;
bgp->gr_info[afi][safi].eor_received = 0;
XFREE(MTYPE_TMP, info);
/* Best path selection */
bgp_best_path_select_defer(bgp, afi, safi);
}
static bool bgp_update_delay_applicable(struct bgp *bgp)
{
/* update_delay_over flag should be reset (set to 0) for any new
applicability of the update-delay during BGP process lifetime.
And it should be set after an occurence of the update-delay is
over)*/
if (!bgp->update_delay_over)
return true;
return false;
}
bool bgp_update_delay_active(struct bgp *bgp)
{
if (bgp->t_update_delay)
return true;
return false;
}
bool bgp_update_delay_configured(struct bgp *bgp)
{
if (bgp->v_update_delay)
return true;
return false;
}
/* Do the post-processing needed when bgp comes out of the read-only mode
on ending the update delay. */
void bgp_update_delay_end(struct bgp *bgp)
{
EVENT_OFF(bgp->t_update_delay);
EVENT_OFF(bgp->t_establish_wait);
/* Reset update-delay related state */
bgp->update_delay_over = 1;
bgp->established = 0;
bgp->restarted_peers = 0;
bgp->implicit_eors = 0;
bgp->explicit_eors = 0;
frr_timestamp(3, bgp->update_delay_end_time,
sizeof(bgp->update_delay_end_time));
/*
* Add an end-of-initial-update marker to the main process queues so
* that
* the route advertisement timer for the peers can be started. Also set
* the zebra and peer update hold flags. These flags are used to achieve
* three stages in the update-delay post processing:
* 1. Finish best-path selection for all the prefixes held on the
* queues.
* (routes in BGP are updated, and peers sync queues are populated
* too)
* 2. As the eoiu mark is reached in the bgp process routine, ship all
* the
* routes to zebra. With that zebra should see updates from BGP
* close
* to each other.
* 3. Unblock the peer update writes. With that peer update packing
* with
* the prefixes should be at its maximum.
*/
bgp_add_eoiu_mark(bgp);
bgp->main_zebra_update_hold = 1;
bgp->main_peers_update_hold = 1;
/*
* Resume the queue processing. This should trigger the event that would
* take care of processing any work that was queued during the read-only
* mode.
*/
work_queue_unplug(bgp->process_queue);
}
/**
* see bgp_fsm.h
*/
void bgp_start_routeadv(struct bgp *bgp)
{
struct listnode *node, *nnode;
struct peer *peer;
zlog_info("%s, update hold status %d", __func__,
bgp->main_peers_update_hold);
if (bgp->main_peers_update_hold)
return;
frr_timestamp(3, bgp->update_delay_peers_resume_time,
sizeof(bgp->update_delay_peers_resume_time));
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
struct peer_connection *connection = peer->connection;
if (!peer_established(connection))
continue;
EVENT_OFF(connection->t_routeadv);
BGP_TIMER_ON(connection->t_routeadv, bgp_routeadv_timer, 0);
}
}
/**
* see bgp_fsm.h
*/
void bgp_adjust_routeadv(struct peer *peer)
{
time_t nowtime = monotime(NULL);
double diff;
unsigned long remain;
struct peer_connection *connection = peer->connection;
/* Bypass checks for special case of MRAI being 0 */
if (peer->v_routeadv == 0) {
/* Stop existing timer, just in case it is running for a
* different
* duration and schedule write thread immediately.
*/
EVENT_OFF(connection->t_routeadv);
peer->synctime = monotime(NULL);
/* If suppress fib pending is enabled, route is advertised to
* peers when the status is received from the FIB. The delay
* is added to update group packet generate which will allow
* more routes to be sent in the update message
*/
BGP_UPDATE_GROUP_TIMER_ON(&connection->t_generate_updgrp_packets,
bgp_generate_updgrp_packets);
return;
}
/*
* CASE I:
* If the last update was written more than MRAI back, expire the timer
* instantly so that we can send the update out sooner.
*
* <------- MRAI --------->
* |-----------------|-----------------------|
* <------------- m ------------>
* ^ ^ ^
* | | |
* | | current time
* | timer start
* last write
*
* m > MRAI
*/
diff = difftime(nowtime, peer->last_update);
if (diff > (double)peer->v_routeadv) {
EVENT_OFF(connection->t_routeadv);
BGP_TIMER_ON(connection->t_routeadv, bgp_routeadv_timer, 0);
return;