forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbgp_fsm.c
2961 lines (2557 loc) · 83.4 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
/* BGP-4 Finite State Machine
* From RFC1771 [A Border Gateway Protocol 4 (BGP-4)]
* Copyright (C) 1996, 97, 98 Kunihiro Ishiguro
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include "linklist.h"
#include "prefix.h"
#include "sockunion.h"
#include "thread.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_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. */
int bgp_event(struct thread *);
/* BGP thread functions. */
static int bgp_start_timer(struct thread *);
static int bgp_connect_timer(struct thread *);
static int bgp_holdtime_timer(struct thread *);
static int bgp_delayopen_timer(struct thread *);
/* BGP FSM functions. */
static int bgp_start(struct peer *);
/* Register peer with NHT */
static 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->su.sa.sa_family),
SAFI_UNICAST, NULL, peer, connected);
}
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;
int fd;
enum bgp_fsm_status status, pstatus;
enum bgp_fsm_events last_evt, last_maj_evt;
assert(from_peer != NULL);
peer = from_peer->doppelganger;
if (!peer || !CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
return from_peer;
/*
* 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->fd, peer,
peer->fd);
bgp_writes_off(peer);
bgp_reads_off(peer);
bgp_writes_off(from_peer);
bgp_reads_off(from_peer);
/*
* 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(from_peer);
BGP_TIMER_OFF(peer->t_routeadv);
BGP_TIMER_OFF(peer->t_connect);
BGP_TIMER_OFF(peer->t_delayopen);
BGP_TIMER_OFF(peer->t_connect_check_r);
BGP_TIMER_OFF(peer->t_connect_check_w);
BGP_TIMER_OFF(from_peer->t_routeadv);
BGP_TIMER_OFF(from_peer->t_connect);
BGP_TIMER_OFF(from_peer->t_delayopen);
BGP_TIMER_OFF(from_peer->t_connect_check_r);
BGP_TIMER_OFF(from_peer->t_connect_check_w);
BGP_TIMER_OFF(from_peer->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.
*/
frr_with_mutex(&peer->io_mtx, &from_peer->io_mtx) {
fd = peer->fd;
peer->fd = from_peer->fd;
from_peer->fd = fd;
stream_fifo_clean(peer->ibuf);
stream_fifo_clean(peer->obuf);
/*
* this should never happen, since bgp_process_packet() is the
* only task that sets and unsets the current packet and it
* runs in our pthread.
*/
if (peer->curr) {
flog_err(
EC_BGP_PKT_PROCESS,
"[%s] Dropping pending packet on connection transfer:",
peer->host);
/* there used to be a bgp_packet_dump call here, but
* that's extremely confusing since there's no way to
* identify the packet in MRT dumps or BMP as dropped
* due to connection transfer.
*/
stream_free(peer->curr);
peer->curr = NULL;
}
// copy each packet from old peer's output queue to new peer
while (from_peer->obuf->head)
stream_fifo_push(peer->obuf,
stream_fifo_pop(from_peer->obuf));
// copy each packet from old peer's input queue to new peer
while (from_peer->ibuf->head)
stream_fifo_push(peer->ibuf,
stream_fifo_pop(from_peer->ibuf));
ringbuf_wipe(peer->ibuf_work);
ringbuf_copy(peer->ibuf_work, from_peer->ibuf_work,
ringbuf_remain(from_peer->ibuf_work));
}
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;
status = peer->status;
pstatus = peer->ostatus;
last_evt = peer->last_event;
last_maj_evt = peer->last_major_event;
peer->status = from_peer->status;
peer->ostatus = from_peer->ostatus;
peer->last_event = from_peer->last_event;
peer->last_major_event = from_peer->last_major_event;
from_peer->status = status;
from_peer->ostatus = pstatus;
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->peer_gr_present_state = from_peer->peer_gr_present_state;
peer->peer_gr_new_status_flag = from_peer->peer_gr_new_status_flag;
bgp_peer_gr_flags_update(peer);
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 (from_peer->hostname != NULL) {
if (peer->hostname) {
XFREE(MTYPE_BGP_PEER_HOST, peer->hostname);
peer->hostname = NULL;
}
peer->hostname = from_peer->hostname;
from_peer->hostname = NULL;
}
if (from_peer->domainname != NULL) {
if (peer->domainname) {
XFREE(MTYPE_BGP_PEER_HOST, peer->domainname);
peer->domainname = NULL;
}
peer->domainname = from_peer->domainname;
from_peer->domainname = NULL;
}
FOREACH_AFI_SAFI (afi, safi) {
peer->af_flags[afi][safi] = from_peer->af_flags[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];
}
if (bgp_getsockname(peer) < 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, peer->fd, from_peer->fd);
BGP_EVENT_ADD(peer, BGP_Stop);
BGP_EVENT_ADD(from_peer, BGP_Stop);
return NULL;
}
if (from_peer->status > Active) {
if (bgp_getsockname(from_peer) < 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, from_peer->fd, peer->fd);
bgp_stop(from_peer);
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);
bgp_reads_on(peer);
bgp_writes_on(peer);
thread_add_timer_msec(bm->master, bgp_process_packet, peer, 0,
&peer->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 *peer)
{
switch (peer->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(peer)
|| (peer->bgp->inst_type != BGP_INSTANCE_TYPE_VIEW &&
peer->bgp->vrf_id == VRF_UNKNOWN)) {
BGP_TIMER_OFF(peer->t_start);
} else {
BGP_TIMER_ON(peer->t_start, bgp_start_timer,
peer->v_start);
}
BGP_TIMER_OFF(peer->t_connect);
BGP_TIMER_OFF(peer->t_holdtime);
bgp_keepalives_off(peer);
BGP_TIMER_OFF(peer->t_routeadv);
BGP_TIMER_OFF(peer->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. */
BGP_TIMER_OFF(peer->t_start);
if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER_DELAYOPEN))
BGP_TIMER_ON(peer->t_connect, bgp_connect_timer,
(peer->v_delayopen + peer->v_connect));
else
BGP_TIMER_ON(peer->t_connect, bgp_connect_timer,
peer->v_connect);
BGP_TIMER_OFF(peer->t_holdtime);
bgp_keepalives_off(peer);
BGP_TIMER_OFF(peer->t_routeadv);
break;
case Active:
/* Active is waiting connection from remote peer. And if
connect timer is expired, change status to Connect. */
BGP_TIMER_OFF(peer->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)) {
BGP_TIMER_OFF(peer->t_connect);
} else {
if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER_DELAYOPEN))
BGP_TIMER_ON(
peer->t_connect, bgp_connect_timer,
(peer->v_delayopen + peer->v_connect));
else
BGP_TIMER_ON(peer->t_connect, bgp_connect_timer,
peer->v_connect);
}
BGP_TIMER_OFF(peer->t_holdtime);
bgp_keepalives_off(peer);
BGP_TIMER_OFF(peer->t_routeadv);
break;
case OpenSent:
/* OpenSent status. */
BGP_TIMER_OFF(peer->t_start);
BGP_TIMER_OFF(peer->t_connect);
if (peer->v_holdtime != 0) {
BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer,
peer->v_holdtime);
} else {
BGP_TIMER_OFF(peer->t_holdtime);
}
bgp_keepalives_off(peer);
BGP_TIMER_OFF(peer->t_routeadv);
BGP_TIMER_OFF(peer->t_delayopen);
break;
case OpenConfirm:
/* OpenConfirm status. */
BGP_TIMER_OFF(peer->t_start);
BGP_TIMER_OFF(peer->t_connect);
/* If the negotiated Hold Time value is zero, then the Hold Time
timer and KeepAlive timers are not started. */
if (peer->v_holdtime == 0) {
BGP_TIMER_OFF(peer->t_holdtime);
bgp_keepalives_off(peer);
} else {
BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer,
peer->v_holdtime);
bgp_keepalives_on(peer);
}
BGP_TIMER_OFF(peer->t_routeadv);
BGP_TIMER_OFF(peer->t_delayopen);
break;
case Established:
/* In Established status start and connect timer is turned
off. */
BGP_TIMER_OFF(peer->t_start);
BGP_TIMER_OFF(peer->t_connect);
BGP_TIMER_OFF(peer->t_delayopen);
/* Same as OpenConfirm, if holdtime is zero then both holdtime
and keepalive must be turned off. */
if (peer->v_holdtime == 0) {
BGP_TIMER_OFF(peer->t_holdtime);
bgp_keepalives_off(peer);
} else {
BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer,
peer->v_holdtime);
bgp_keepalives_on(peer);
}
break;
case Deleted:
BGP_TIMER_OFF(peer->t_gr_restart);
BGP_TIMER_OFF(peer->t_gr_stale);
BGP_TIMER_OFF(peer->t_pmax_restart);
BGP_TIMER_OFF(peer->t_refresh_stalepath);
/* fallthru */
case Clearing:
BGP_TIMER_OFF(peer->t_start);
BGP_TIMER_OFF(peer->t_connect);
BGP_TIMER_OFF(peer->t_holdtime);
bgp_keepalives_off(peer);
BGP_TIMER_OFF(peer->t_routeadv);
BGP_TIMER_OFF(peer->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 int bgp_start_timer(struct thread *thread)
{
struct peer *peer;
peer = THREAD_ARG(thread);
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s [FSM] Timer (start timer expire).", peer->host);
THREAD_VAL(thread) = BGP_Start;
bgp_event(thread); /* bgp_event unlocks peer */
return 0;
}
/* BGP connect retry timer. */
static int bgp_connect_timer(struct thread *thread)
{
struct peer *peer;
int ret;
peer = THREAD_ARG(thread);
/* stop the DelayOpenTimer if it is running */
if (peer->t_delayopen)
BGP_TIMER_OFF(peer->t_delayopen);
assert(!peer->t_write);
assert(!peer->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(peer);
ret = -1;
} else {
THREAD_VAL(thread) = ConnectRetry_timer_expired;
bgp_event(thread); /* bgp_event unlocks peer */
ret = 0;
}
return ret;
}
/* BGP holdtime timer. */
static int bgp_holdtime_timer(struct thread *thread)
{
atomic_size_t inq_count;
struct peer *peer;
peer = THREAD_ARG(thread);
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(&peer->ibuf->count,
memory_order_relaxed);
if (inq_count) {
BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer,
peer->v_holdtime);
return 0;
}
THREAD_VAL(thread) = Hold_Timer_expired;
bgp_event(thread); /* bgp_event unlocks peer */
return 0;
}
int bgp_routeadv_timer(struct thread *thread)
{
struct peer *peer;
peer = THREAD_ARG(thread);
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s [FSM] Timer (routeadv timer expire)",
peer->host);
peer->synctime = bgp_clock();
thread_add_timer_msec(bm->master, bgp_generate_updgrp_packets, peer, 0,
&peer->t_generate_updgrp_packets);
/* MRAI timer will be started again when FIFO is built, no need to
* do it here.
*/
return 0;
}
/* RFC 4271 DelayOpenTimer */
int bgp_delayopen_timer(struct thread *thread)
{
struct peer *peer;
peer = THREAD_ARG(thread);
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s [FSM] Timer (DelayOpentimer expire)",
peer->host);
THREAD_VAL(thread) = DelayOpen_timer_expired;
bgp_event(thread); /* bgp_event unlocks peer */
return 0;
}
/* 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",
"Waiting for NHT",
"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"};
static int bgp_graceful_restart_timer_expire(struct thread *thread)
{
struct peer *peer;
afi_t afi;
safi_t safi;
peer = THREAD_ARG(thread);
/* NSF delete stale route */
for (afi = AFI_IP; afi < AFI_MAX; afi++)
for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; safi++)
if (peer->nsf[afi][safi])
bgp_clear_stale_route(peer, afi, safi);
UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
BGP_TIMER_OFF(peer->t_gr_stale);
if (bgp_debug_neighbor_events(peer)) {
zlog_debug("%s graceful restart timer expired", peer->host);
zlog_debug("%s graceful restart stalepath timer stopped",
peer->host);
}
bgp_timer_set(peer);
return 0;
}
static int bgp_graceful_stale_timer_expire(struct thread *thread)
{
struct peer *peer;
afi_t afi;
safi_t safi;
peer = THREAD_ARG(thread);
if (bgp_debug_neighbor_events(peer))
zlog_debug("%s graceful restart stalepath timer expired",
peer->host);
/* NSF delete stale route */
for (afi = AFI_IP; afi < AFI_MAX; afi++)
for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; safi++)
if (peer->nsf[afi][safi])
bgp_clear_stale_route(peer, afi, safi);
return 0;
}
/* Selection deferral timer processing function */
static int bgp_graceful_deferral_timer_expire(struct thread *thread)
{
struct afi_safi_info *info;
afi_t afi;
safi_t safi;
struct bgp *bgp;
info = THREAD_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 */
return 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)
{
THREAD_OFF(bgp->t_update_delay);
THREAD_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;
quagga_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("bgp_start_routeadv(), update hold status %d",
bgp->main_peers_update_hold);
if (bgp->main_peers_update_hold)
return;
quagga_timestamp(3, bgp->update_delay_peers_resume_time,
sizeof(bgp->update_delay_peers_resume_time));
for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
if (peer->status != Established)
continue;
BGP_TIMER_OFF(peer->t_routeadv);
BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0);
}
}
/**
* see bgp_fsm.h
*/
void bgp_adjust_routeadv(struct peer *peer)
{
time_t nowtime = bgp_clock();
double diff;
unsigned long remain;
/* 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.
*/
if (peer->t_routeadv)
BGP_TIMER_OFF(peer->t_routeadv);
peer->synctime = bgp_clock();
/* 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(&peer->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) {
BGP_TIMER_OFF(peer->t_routeadv);
BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0);
return;
}
/*
* CASE II:
* - Find when to expire the MRAI timer.
* If MRAI timer is not active, assume we can start it now.
*
* <------- MRAI --------->
* |------------|-----------------------|
* <-------- m ----------><----- r ----->
* ^ ^ ^
* | | |
* | | current time
* | timer start
* last write
*
* (MRAI - m) < r
*/
if (peer->t_routeadv)
remain = thread_timer_remain_second(peer->t_routeadv);
else
remain = peer->v_routeadv;
diff = peer->v_routeadv - diff;
if (diff <= (double)remain) {
BGP_TIMER_OFF(peer->t_routeadv);
BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, diff);
}
}
static bool bgp_maxmed_onstartup_applicable(struct bgp *bgp)
{
if (!bgp->maxmed_onstartup_over)
return true;
return false;
}
bool bgp_maxmed_onstartup_configured(struct bgp *bgp)
{
if (bgp->v_maxmed_onstartup != BGP_MAXMED_ONSTARTUP_UNCONFIGURED)
return true;
return false;
}
bool bgp_maxmed_onstartup_active(struct bgp *bgp)
{
if (bgp->t_maxmed_onstartup)
return true;
return false;
}
void bgp_maxmed_update(struct bgp *bgp)
{
uint8_t maxmed_active;
uint32_t maxmed_value;
if (bgp->v_maxmed_admin) {
maxmed_active = 1;
maxmed_value = bgp->maxmed_admin_value;
} else if (bgp->t_maxmed_onstartup) {
maxmed_active = 1;
maxmed_value = bgp->maxmed_onstartup_value;
} else {
maxmed_active = 0;
maxmed_value = BGP_MAXMED_VALUE_DEFAULT;
}
if (bgp->maxmed_active != maxmed_active
|| bgp->maxmed_value != maxmed_value) {
bgp->maxmed_active = maxmed_active;
bgp->maxmed_value = maxmed_value;
update_group_announce(bgp);
}
}
int bgp_fsm_error_subcode(int status)
{
int fsm_err_subcode = BGP_NOTIFY_FSM_ERR_SUBCODE_UNSPECIFIC;
switch (status) {
case OpenSent:
fsm_err_subcode = BGP_NOTIFY_FSM_ERR_SUBCODE_OPENSENT;
break;
case OpenConfirm:
fsm_err_subcode = BGP_NOTIFY_FSM_ERR_SUBCODE_OPENCONFIRM;
break;
case Established:
fsm_err_subcode = BGP_NOTIFY_FSM_ERR_SUBCODE_ESTABLISHED;
break;
default:
break;
}
return fsm_err_subcode;
}
/* The maxmed onstartup timer expiry callback. */
static int bgp_maxmed_onstartup_timer(struct thread *thread)
{
struct bgp *bgp;
zlog_info("Max med on startup ended - timer expired.");
bgp = THREAD_ARG(thread);
THREAD_OFF(bgp->t_maxmed_onstartup);
bgp->maxmed_onstartup_over = 1;
bgp_maxmed_update(bgp);
return 0;
}
static void bgp_maxmed_onstartup_begin(struct bgp *bgp)
{
/* Applicable only once in the process lifetime on the startup */
if (bgp->maxmed_onstartup_over)
return;
zlog_info("Begin maxmed onstartup mode - timer %d seconds",
bgp->v_maxmed_onstartup);
thread_add_timer(bm->master, bgp_maxmed_onstartup_timer, bgp,
bgp->v_maxmed_onstartup, &bgp->t_maxmed_onstartup);
if (!bgp->v_maxmed_admin) {
bgp->maxmed_active = 1;
bgp->maxmed_value = bgp->maxmed_onstartup_value;
}
/* Route announce to all peers should happen after this in