forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbgp_fsm.c
1879 lines (1615 loc) · 53.3 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 GNU Zebra; see the file COPYING. If not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA. */
#include <zebra.h>
#include "linklist.h"
#include "prefix.h"
#include "vty.h"
#include "sockunion.h"
#include "thread.h"
#include "log.h"
#include "stream.h"
#include "memory.h"
#include "plist.h"
#include "workqueue.h"
#include "queue.h"
#include "filter.h"
#include "command.h"
#include "lib/json.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_debug.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"
#ifdef HAVE_SNMP
#include "bgpd/bgp_snmp.h"
#endif /* HAVE_SNMP */
#include "bgpd/bgp_updgrp.h"
#include "bgpd/bgp_nht.h"
#include "bgpd/bgp_bfd.h"
#include "bgpd/bgp_memory.h"
/* Definition of display strings corresponding to FSM events. This should be
* kept consistent with the events defined in bgpd.h
*/
static const char *bgp_event_str[] =
{
NULL,
"BGP_Start",
"BGP_Stop",
"TCP_connection_open",
"TCP_connection_closed",
"TCP_connection_open_failed",
"TCP_fatal_error",
"ConnectRetry_timer_expired",
"Hold_Timer_expired",
"KeepAlive_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_keepalive_timer (struct thread *);
/* BGP FSM functions. */
static int bgp_start (struct peer *);
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;
int status, pstatus;
unsigned char 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;
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_WRITE_OFF(peer->t_write);
BGP_READ_OFF(peer->t_read);
BGP_WRITE_OFF(from_peer->t_write);
BGP_READ_OFF(from_peer->t_read);
BGP_TIMER_OFF(peer->t_routeadv);
BGP_TIMER_OFF(from_peer->t_routeadv);
fd = peer->fd;
peer->fd = from_peer->fd;
from_peer->fd = fd;
stream_reset(peer->ibuf);
stream_fifo_clean(peer->obuf);
stream_fifo_clean(from_peer->obuf);
peer->as = from_peer->as;
peer->v_holdtime = from_peer->v_holdtime;
peer->v_keepalive = from_peer->v_keepalive;
peer->routeadv = from_peer->routeadv;
peer->v_routeadv = from_peer->v_routeadv;
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;
if (from_peer->hostname != NULL)
{
if (peer->hostname)
{
XFREE(MTYPE_BGP_PEER_HOST, peer->hostname);
peer->hostname = NULL;
}
peer->hostname = XSTRDUP(MTYPE_BGP_PEER_HOST, from_peer->hostname);
XFREE(MTYPE_BGP_PEER_HOST, 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 = XSTRDUP(MTYPE_BGP_PEER_HOST, from_peer->domainname);
XFREE(MTYPE_BGP_PEER_HOST, from_peer->domainname);
from_peer->domainname = NULL;
}
for (afi = AFI_IP; afi < AFI_MAX; afi++)
for (safi = SAFI_UNICAST; safi < SAFI_MAX; 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)
{
zlog_err ("%%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_stop(peer);
bgp_stop(from_peer);
return NULL;
}
if (from_peer->status > Active)
{
if (bgp_getsockname(from_peer) < 0)
{
zlog_err ("%%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;
}
}
BGP_READ_ON(peer->t_read, bgp_read, peer->fd);
BGP_WRITE_ON(peer->t_write, bgp_write, peer->fd);
if (from_peer)
peer_xfer_stats(peer, from_peer);
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))
{
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_TIMER_OFF (peer->t_keepalive);
BGP_TIMER_OFF (peer->t_routeadv);
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);
BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
BGP_TIMER_OFF (peer->t_holdtime);
BGP_TIMER_OFF (peer->t_keepalive);
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
{
BGP_TIMER_ON (peer->t_connect, bgp_connect_timer, peer->v_connect);
}
BGP_TIMER_OFF (peer->t_holdtime);
BGP_TIMER_OFF (peer->t_keepalive);
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_TIMER_OFF (peer->t_keepalive);
BGP_TIMER_OFF (peer->t_routeadv);
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_TIMER_OFF (peer->t_keepalive);
}
else
{
BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
peer->v_holdtime);
BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
peer->v_keepalive);
}
BGP_TIMER_OFF (peer->t_routeadv);
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);
/* 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_TIMER_OFF (peer->t_keepalive);
}
else
{
BGP_TIMER_ON (peer->t_holdtime, bgp_holdtime_timer,
peer->v_holdtime);
BGP_TIMER_ON (peer->t_keepalive, bgp_keepalive_timer,
peer->v_keepalive);
}
break;
case Deleted:
BGP_TIMER_OFF (peer->t_gr_restart);
BGP_TIMER_OFF (peer->t_gr_stale);
BGP_TIMER_OFF (peer->t_pmax_restart);
case Clearing:
BGP_TIMER_OFF (peer->t_start);
BGP_TIMER_OFF (peer->t_connect);
BGP_TIMER_OFF (peer->t_holdtime);
BGP_TIMER_OFF (peer->t_keepalive);
BGP_TIMER_OFF (peer->t_routeadv);
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);
peer->t_start = NULL;
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);
peer->t_connect = NULL;
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)
{
struct peer *peer;
peer = THREAD_ARG (thread);
peer->t_holdtime = NULL;
if (bgp_debug_neighbor_events(peer))
zlog_debug ("%s [FSM] Timer (holdtime timer expire)", peer->host);
THREAD_VAL (thread) = Hold_Timer_expired;
bgp_event (thread); /* bgp_event unlocks peer */
return 0;
}
/* BGP keepalive fire ! */
static int
bgp_keepalive_timer (struct thread *thread)
{
struct peer *peer;
peer = THREAD_ARG (thread);
peer->t_keepalive = NULL;
if (bgp_debug_neighbor_events(peer))
zlog_debug ("%s [FSM] Timer (keepalive timer expire)", peer->host);
THREAD_VAL (thread) = KeepAlive_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);
peer->t_routeadv = NULL;
if (bgp_debug_neighbor_events(peer))
zlog_debug ("%s [FSM] Timer (routeadv timer expire)", peer->host);
peer->synctime = bgp_clock ();
BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
/* MRAI timer will be started again when FIFO is built, no need to
* do it here.
*/
return 0;
}
/* BGP Peer Down Cause */
const char *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"
};
static int
bgp_graceful_restart_timer_expire (struct thread *thread)
{
struct peer *peer;
afi_t afi;
safi_t safi;
peer = THREAD_ARG (thread);
peer->t_gr_restart = NULL;
/* NSF delete stale route */
for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
for (safi = SAFI_UNICAST ; safi < SAFI_RESERVED_3 ; 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);
peer->t_gr_stale = NULL;
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_RESERVED_3 ; safi++)
if (peer->nsf[afi][safi])
bgp_clear_stale_route (peer, afi, safi);
return 0;
}
static int
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 1;
return 0;
}
int
bgp_update_delay_active (struct bgp *bgp)
{
if (bgp->t_update_delay)
return 1;
return 0;
}
int
bgp_update_delay_configured (struct bgp *bgp)
{
if (bgp->v_update_delay)
return 1;
return 0;
}
/* 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_TIMER_OFF (bgp->t_update_delay);
THREAD_TIMER_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(bm->process_main_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 ();
BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
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_write);
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 int
bgp_maxmed_onstartup_applicable (struct bgp *bgp)
{
if (!bgp->maxmed_onstartup_over)
return 1;
return 0;
}
int
bgp_maxmed_onstartup_configured (struct bgp *bgp)
{
if (bgp->v_maxmed_onstartup != BGP_MAXMED_ONSTARTUP_UNCONFIGURED)
return 1;
return 0;
}
int
bgp_maxmed_onstartup_active (struct bgp *bgp)
{
if (bgp->t_maxmed_onstartup)
return 1;
return 0;
}
void
bgp_maxmed_update (struct bgp *bgp)
{
u_char maxmed_active;
u_int32_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);
}
}
/* 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_TIMER_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_TIMER_ON (bm->master, bgp->t_maxmed_onstartup,
bgp_maxmed_onstartup_timer,
bgp, bgp->v_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 bgp_establish() */
}
static void
bgp_maxmed_onstartup_process_status_change(struct peer *peer)
{
if (peer->status == Established && !peer->bgp->established)
{
bgp_maxmed_onstartup_begin(peer->bgp);
}
}
/* The update delay timer expiry callback. */
static int
bgp_update_delay_timer (struct thread *thread)
{
struct bgp *bgp;
zlog_info ("Update delay ended - timer expired.");
bgp = THREAD_ARG (thread);
THREAD_TIMER_OFF (bgp->t_update_delay);
bgp_update_delay_end(bgp);
return 0;
}
/* The establish wait timer expiry callback. */
static int
bgp_establish_wait_timer (struct thread *thread)
{
struct bgp *bgp;
zlog_info ("Establish wait - timer expired.");
bgp = THREAD_ARG (thread);
THREAD_TIMER_OFF (bgp->t_establish_wait);
bgp_check_update_delay(bgp);
return 0;
}
/* Steps to begin the update delay:
- initialize queues if needed
- stop the queue processing
- start the timer */
static void
bgp_update_delay_begin (struct bgp *bgp)
{
struct listnode *node, *nnode;
struct peer *peer;
/* Stop the processing of queued work. Enqueue shall continue */
work_queue_plug(bm->process_main_queue);
for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
peer->update_delay_over = 0;
/* Start the update-delay timer */
THREAD_TIMER_ON (bm->master, bgp->t_update_delay, bgp_update_delay_timer,
bgp, bgp->v_update_delay);
if (bgp->v_establish_wait != bgp->v_update_delay)
THREAD_TIMER_ON (bm->master, bgp->t_establish_wait, bgp_establish_wait_timer,
bgp, bgp->v_establish_wait);
quagga_timestamp(3, bgp->update_delay_begin_time,
sizeof(bgp->update_delay_begin_time));
}
static void
bgp_update_delay_process_status_change(struct peer *peer)
{
if (peer->status == Established)
{
if (!peer->bgp->established++)
{
bgp_update_delay_begin(peer->bgp);
zlog_info ("Begin read-only mode - update-delay timer %d seconds",
peer->bgp->v_update_delay);
}
if (CHECK_FLAG (peer->cap, PEER_CAP_RESTART_BIT_RCV))
bgp_update_restarted_peers(peer);
}
if (peer->ostatus == Established && bgp_update_delay_active(peer->bgp))
{
/* Adjust the update-delay state to account for this flap.
NOTE: Intentionally skipping adjusting implicit_eors or explicit_eors
counters. Extra sanity check in bgp_check_update_delay() should
be enough to take care of any additive discrepancy in bgp eor
counters */
peer->bgp->established--;
peer->update_delay_over = 0;
}
}
/* Called after event occured, this function change status and reset
read/write and timer thread. */
void
bgp_fsm_change_status (struct peer *peer, int status)
{
bgp_dump_state (peer, peer->status, status);
/* Transition into Clearing or Deleted must /always/ clear all routes..
* (and must do so before actually changing into Deleted..
*/
if (status >= Clearing)
{
bgp_clear_route_all (peer);
/* If no route was queued for the clear-node processing, generate the
* completion event here. This is needed because if there are no routes
* to trigger the background clear-node thread, the event won't get
* generated and the peer would be stuck in Clearing. Note that this
* event is for the peer and helps the peer transition out of Clearing
* state; it should not be generated per (AFI,SAFI). The event is
* directly posted here without calling clear_node_complete() as we
* shouldn't do an extra unlock. This event will get processed after
* the state change that happens below, so peer will be in Clearing
* (or Deleted).
*/
if (!work_queue_is_scheduled (peer->clear_node_queue))
BGP_EVENT_ADD (peer, Clearing_Completed);
}
/* Preserve old status and change into new status. */
peer->ostatus = peer->status;
peer->status = status;
/* Save event that caused status change. */
peer->last_major_event = peer->cur_event;
if (status == Established)
UNSET_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER);
/* If max-med processing is applicable, do the necessary. */
if (status == Established)
{
if (bgp_maxmed_onstartup_configured(peer->bgp) &&
bgp_maxmed_onstartup_applicable(peer->bgp))
bgp_maxmed_onstartup_process_status_change(peer);
else
peer->bgp->maxmed_onstartup_over = 1;
}
/* If update-delay processing is applicable, do the necessary. */
if (bgp_update_delay_configured(peer->bgp) &&
bgp_update_delay_applicable(peer->bgp))
bgp_update_delay_process_status_change(peer);
if (bgp_debug_neighbor_events(peer))
zlog_debug ("%s went from %s to %s",
peer->host,
LOOKUP (bgp_status_msg, peer->ostatus),
LOOKUP (bgp_status_msg, peer->status));
}
/* Flush the event queue and ensure the peer is shut down */
static int
bgp_clearing_completed (struct peer *peer)
{
int rc = bgp_stop(peer);
if (rc >= 0)
BGP_EVENT_FLUSH (peer);
return rc;
}
/* Administrative BGP peer stop event. */
/* May be called multiple times for the same peer */
int
bgp_stop (struct peer *peer)
{
afi_t afi;
safi_t safi;