forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbfd.c
2062 lines (1703 loc) · 49.1 KB
/
bfd.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
/*********************************************************************
* Copyright 2013 Cumulus Networks, LLC. All rights reserved.
* Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved.
*
* bfd.c: implements the BFD protocol.
*
* Authors
* -------
* Shrijeet Mukherjee [shm@cumulusnetworks.com]
* Kanna Rajagopal [kanna@cumulusnetworks.com]
* Radhika Mahankali [Radhika@cumulusnetworks.com]
*/
#include <zebra.h>
#include "lib/jhash.h"
#include "lib/network.h"
#include "bfd.h"
DEFINE_MTYPE_STATIC(BFDD, BFDD_CONFIG, "long-lived configuration memory");
DEFINE_MTYPE_STATIC(BFDD, BFDD_PROFILE, "long-lived profile memory");
DEFINE_MTYPE_STATIC(BFDD, BFDD_SESSION_OBSERVER, "Session observer");
DEFINE_MTYPE_STATIC(BFDD, BFDD_VRF, "BFD VRF");
/*
* Prototypes
*/
static uint32_t ptm_bfd_gen_ID(void);
static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd);
static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa,
uint32_t ldisc);
static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc);
static const char *get_diag_str(int diag);
static void bs_admin_down_handler(struct bfd_session *bs, int nstate);
static void bs_down_handler(struct bfd_session *bs, int nstate);
static void bs_init_handler(struct bfd_session *bs, int nstate);
static void bs_up_handler(struct bfd_session *bs, int nstate);
/**
* Remove BFD profile from all BFD sessions so we don't leave dangling
* pointers.
*/
static void bfd_profile_detach(struct bfd_profile *bp);
/* Zeroed array with the size of an IPv6 address. */
struct in6_addr zero_addr;
/** BFD profiles list. */
struct bfdproflist bplist;
/*
* Functions
*/
struct bfd_profile *bfd_profile_lookup(const char *name)
{
struct bfd_profile *bp;
TAILQ_FOREACH (bp, &bplist, entry) {
if (strcmp(name, bp->name))
continue;
return bp;
}
return NULL;
}
static void bfd_profile_set_default(struct bfd_profile *bp)
{
bp->admin_shutdown = false;
bp->detection_multiplier = BFD_DEFDETECTMULT;
bp->echo_mode = false;
bp->passive = false;
bp->minimum_ttl = BFD_DEF_MHOP_TTL;
bp->min_echo_rx = BFD_DEF_REQ_MIN_ECHO_RX;
bp->min_echo_tx = BFD_DEF_DES_MIN_ECHO_TX;
bp->min_rx = BFD_DEFREQUIREDMINRX;
bp->min_tx = BFD_DEFDESIREDMINTX;
}
struct bfd_profile *bfd_profile_new(const char *name)
{
struct bfd_profile *bp;
/* Search for duplicates. */
if (bfd_profile_lookup(name) != NULL)
return NULL;
/* Allocate, name it and put into list. */
bp = XCALLOC(MTYPE_BFDD_PROFILE, sizeof(*bp));
strlcpy(bp->name, name, sizeof(bp->name));
TAILQ_INSERT_TAIL(&bplist, bp, entry);
/* Set default values. */
bfd_profile_set_default(bp);
return bp;
}
void bfd_profile_free(struct bfd_profile *bp)
{
/* Detach from any session. */
if (bglobal.bg_shutdown == false)
bfd_profile_detach(bp);
/* Remove from global list. */
TAILQ_REMOVE(&bplist, bp, entry);
XFREE(MTYPE_BFDD_PROFILE, bp);
}
void bfd_profile_apply(const char *profname, struct bfd_session *bs)
{
struct bfd_profile *bp;
/* Remove previous profile if any. */
if (bs->profile_name) {
/* We are changing profiles. */
if (strcmp(bs->profile_name, profname)) {
XFREE(MTYPE_BFDD_PROFILE, bs->profile_name);
bs->profile_name =
XSTRDUP(MTYPE_BFDD_PROFILE, profname);
}
} else /* Save the current profile name (in case it doesn't exist). */
bs->profile_name = XSTRDUP(MTYPE_BFDD_PROFILE, profname);
/* Look up new profile to apply. */
bp = bfd_profile_lookup(profname);
/* Point to profile if it exists. */
bs->profile = bp;
/* Apply configuration. */
bfd_session_apply(bs);
}
void bfd_session_apply(struct bfd_session *bs)
{
struct bfd_profile *bp;
uint32_t min_tx = bs->timers.desired_min_tx;
uint32_t min_rx = bs->timers.required_min_rx;
/* Pick the source of configuration. */
bp = bs->profile ? bs->profile : &bs->peer_profile;
/* Set multiplier if not the default. */
if (bs->peer_profile.detection_multiplier == BFD_DEFDETECTMULT)
bs->detect_mult = bp->detection_multiplier;
else
bs->detect_mult = bs->peer_profile.detection_multiplier;
/* Set timers if not the default. */
if (bs->peer_profile.min_tx == BFD_DEFDESIREDMINTX)
bs->timers.desired_min_tx = bp->min_tx;
else
bs->timers.desired_min_tx = bs->peer_profile.min_tx;
if (bs->peer_profile.min_rx == BFD_DEFREQUIREDMINRX)
bs->timers.required_min_rx = bp->min_rx;
else
bs->timers.required_min_rx = bs->peer_profile.min_rx;
/* We can only apply echo options on single hop sessions. */
if (!CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)) {
/* Configure echo timers if they were default. */
if (bs->peer_profile.min_echo_rx == BFD_DEF_REQ_MIN_ECHO_RX)
bs->timers.required_min_echo_rx = bp->min_echo_rx;
else
bs->timers.required_min_echo_rx =
bs->peer_profile.min_echo_rx;
if (bs->peer_profile.min_echo_tx == BFD_DEF_DES_MIN_ECHO_TX)
bs->timers.desired_min_echo_tx = bp->min_echo_tx;
else
bs->timers.desired_min_echo_tx =
bs->peer_profile.min_echo_tx;
/* Toggle echo if default value. */
if (bs->peer_profile.echo_mode == false)
bfd_set_echo(bs, bp->echo_mode);
else
bfd_set_echo(bs, bs->peer_profile.echo_mode);
} else {
/* Configure the TTL packet filter. */
if (bs->peer_profile.minimum_ttl == BFD_DEF_MHOP_TTL)
bs->mh_ttl = bp->minimum_ttl;
else
bs->mh_ttl = bs->peer_profile.minimum_ttl;
}
/* Toggle 'passive-mode' if default value. */
if (bs->peer_profile.passive == false)
bfd_set_passive_mode(bs, bp->passive);
else
bfd_set_passive_mode(bs, bs->peer_profile.passive);
/* Toggle 'no shutdown' if default value. */
if (bs->peer_profile.admin_shutdown == false)
bfd_set_shutdown(bs, bp->admin_shutdown);
else
bfd_set_shutdown(bs, bs->peer_profile.admin_shutdown);
/* If session interval changed negotiate new timers. */
if (bs->ses_state == PTM_BFD_UP
&& (bs->timers.desired_min_tx != min_tx
|| bs->timers.required_min_rx != min_rx))
bfd_set_polling(bs);
/* Send updated information to data plane. */
bfd_dplane_update_session(bs);
}
void bfd_profile_remove(struct bfd_session *bs)
{
/* Remove any previous set profile name. */
XFREE(MTYPE_BFDD_PROFILE, bs->profile_name);
bs->profile = NULL;
bfd_session_apply(bs);
}
void gen_bfd_key(struct bfd_key *key, struct sockaddr_any *peer,
struct sockaddr_any *local, bool mhop, const char *ifname,
const char *vrfname)
{
memset(key, 0, sizeof(*key));
switch (peer->sa_sin.sin_family) {
case AF_INET:
key->family = AF_INET;
memcpy(&key->peer, &peer->sa_sin.sin_addr,
sizeof(peer->sa_sin.sin_addr));
memcpy(&key->local, &local->sa_sin.sin_addr,
sizeof(local->sa_sin.sin_addr));
break;
case AF_INET6:
key->family = AF_INET6;
memcpy(&key->peer, &peer->sa_sin6.sin6_addr,
sizeof(peer->sa_sin6.sin6_addr));
memcpy(&key->local, &local->sa_sin6.sin6_addr,
sizeof(local->sa_sin6.sin6_addr));
break;
}
key->mhop = mhop;
if (ifname && ifname[0])
strlcpy(key->ifname, ifname, sizeof(key->ifname));
if (vrfname && vrfname[0])
strlcpy(key->vrfname, vrfname, sizeof(key->vrfname));
else
strlcpy(key->vrfname, VRF_DEFAULT_NAME, sizeof(key->vrfname));
}
struct bfd_session *bs_peer_find(struct bfd_peer_cfg *bpc)
{
struct bfd_key key;
/* Otherwise fallback to peer/local hash lookup. */
gen_bfd_key(&key, &bpc->bpc_peer, &bpc->bpc_local, bpc->bpc_mhop,
bpc->bpc_localif, bpc->bpc_vrfname);
return bfd_key_lookup(key);
}
/*
* Starts a disabled BFD session.
*
* A session is disabled when the specified interface/VRF doesn't exist
* yet. It might happen on FRR boot or with virtual interfaces.
*/
int bfd_session_enable(struct bfd_session *bs)
{
struct interface *ifp = NULL;
struct vrf *vrf = NULL;
int psock;
/* We are using data plane, we don't need software. */
if (bs->bdc)
return 0;
/*
* If the interface or VRF doesn't exist, then we must register
* the session but delay its start.
*/
if (bs->key.vrfname[0]) {
vrf = vrf_lookup_by_name(bs->key.vrfname);
if (vrf == NULL) {
zlog_err(
"session-enable: specified VRF %s doesn't exists.",
bs->key.vrfname);
return 0;
}
} else {
vrf = vrf_lookup_by_id(VRF_DEFAULT);
}
assert(vrf);
if (bs->key.ifname[0]) {
ifp = if_lookup_by_name(bs->key.ifname, vrf->vrf_id);
if (ifp == NULL) {
zlog_err(
"session-enable: specified interface %s (VRF %s) doesn't exist.",
bs->key.ifname, vrf->name);
return 0;
}
}
/* Assign interface/VRF pointers. */
bs->vrf = vrf;
/* Assign interface pointer (if any). */
bs->ifp = ifp;
/* Attempt to use data plane. */
if (bglobal.bg_use_dplane && bfd_dplane_add_session(bs) == 0)
return 0;
/* Sanity check: don't leak open sockets. */
if (bs->sock != -1) {
if (bglobal.debug_peer_event)
zlog_debug("%s: previous socket open", __func__);
close(bs->sock);
bs->sock = -1;
}
/*
* Get socket for transmitting control packets. Note that if we
* could use the destination port (3784) for the source
* port we wouldn't need a socket per session.
*/
if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_IPV6) == 0) {
psock = bp_peer_socket(bs);
if (psock == -1)
return 0;
} else {
psock = bp_peer_socketv6(bs);
if (psock == -1)
return 0;
}
/*
* We've got a valid socket, lets start the timers and the
* protocol.
*/
bs->sock = psock;
/* Only start timers if we are using active mode. */
if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE) == 0) {
bfd_recvtimer_update(bs);
ptm_bfd_start_xmt_timer(bs, false);
}
/* initialize RTT */
bfd_rtt_init(bs);
return 0;
}
/*
* Disabled a running BFD session.
*
* A session is disabled when the specified interface/VRF gets removed
* (e.g. virtual interfaces).
*/
void bfd_session_disable(struct bfd_session *bs)
{
/* We are using data plane, we don't need software. */
if (bs->bdc)
return;
/* Free up socket resources. */
if (bs->sock != -1) {
close(bs->sock);
bs->sock = -1;
}
/* Disable all timers. */
bfd_recvtimer_delete(bs);
bfd_xmttimer_delete(bs);
ptm_bfd_echo_stop(bs);
/* Set session down so it doesn't report UP and disabled. */
ptm_bfd_sess_dn(bs, BD_PATH_DOWN);
}
static uint32_t ptm_bfd_gen_ID(void)
{
uint32_t session_id;
/*
* RFC 5880, Section 6.8.1. recommends that we should generate
* random session identification numbers.
*/
do {
session_id = CHECK_FLAG((frr_weak_random() << 16), 0xFFFF0000) |
CHECK_FLAG(frr_weak_random(), 0x0000FFFF);
} while (session_id == 0 || bfd_id_lookup(session_id) != NULL);
return session_id;
}
void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo)
{
uint64_t jitter, xmt_TO;
int maxpercent;
xmt_TO = is_echo ? bfd->echo_xmt_TO : bfd->xmt_TO;
/*
* From section 6.5.2: trasmit interval should be randomly jittered
* between
* 75% and 100% of nominal value, unless detect_mult is 1, then should
* be
* between 75% and 90%.
*/
maxpercent = (bfd->detect_mult == 1) ? 16 : 26;
jitter = (xmt_TO * (75 + (frr_weak_random() % maxpercent))) / 100;
/* XXX remove that division above */
if (is_echo)
bfd_echo_xmttimer_update(bfd, jitter);
else
bfd_xmttimer_update(bfd, jitter);
}
static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd)
{
/* Send the scheduled echo packet */
/* if ipv4 use the new echo implementation that causes
* the packet to be looped in forwarding plane of peer
*/
if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6) == 0)
#ifdef BFD_LINUX
ptm_bfd_echo_fp_snd(bfd);
#else
ptm_bfd_echo_snd(bfd);
#endif
else
ptm_bfd_echo_snd(bfd);
/* Restart the timer for next time */
ptm_bfd_start_xmt_timer(bfd, true);
}
void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit)
{
/* Send the scheduled control packet */
ptm_bfd_snd(bfd, fbit);
/* Restart the timer for next time */
ptm_bfd_start_xmt_timer(bfd, false);
}
void ptm_bfd_echo_stop(struct bfd_session *bfd)
{
bfd->echo_xmt_TO = 0;
bfd->echo_detect_TO = 0;
UNSET_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE);
bfd_echo_xmttimer_delete(bfd);
bfd_echo_recvtimer_delete(bfd);
}
void ptm_bfd_echo_start(struct bfd_session *bfd)
{
bfd->echo_detect_TO = (bfd->remote_detect_mult * bfd->echo_xmt_TO);
if (bfd->echo_detect_TO > 0) {
bfd_echo_recvtimer_update(bfd);
ptm_bfd_echo_xmt_TO(bfd);
}
}
void ptm_bfd_sess_up(struct bfd_session *bfd)
{
int old_state = bfd->ses_state;
bfd->local_diag = 0;
bfd->ses_state = PTM_BFD_UP;
monotime(&bfd->uptime);
/* Connection is up, lets negotiate timers. */
bfd_set_polling(bfd);
/* Start sending control packets with poll bit immediately. */
ptm_bfd_snd(bfd, 0);
ptm_bfd_notify(bfd, bfd->ses_state);
if (old_state != bfd->ses_state) {
bfd->stats.session_up++;
if (bglobal.debug_peer_event)
zlog_debug("state-change: [%s] %s -> %s",
bs_to_string(bfd), state_list[old_state].str,
state_list[bfd->ses_state].str);
}
}
void ptm_bfd_sess_dn(struct bfd_session *bfd, uint8_t diag)
{
int old_state = bfd->ses_state;
bfd->local_diag = diag;
bfd->discrs.remote_discr = 0;
bfd->ses_state = PTM_BFD_DOWN;
bfd->polling = 0;
bfd->demand_mode = 0;
monotime(&bfd->downtime);
/*
* Only attempt to send if we have a valid socket:
* this function might be called by session disablers and in
* this case we won't have a valid socket (i.e. interface was
* removed or VRF doesn't exist anymore).
*/
if (bfd->sock != -1)
ptm_bfd_snd(bfd, 0);
/* Slow down the control packets, the connection is down. */
bs_set_slow_timers(bfd);
/* only signal clients when going from up->down state */
if (old_state == PTM_BFD_UP)
ptm_bfd_notify(bfd, PTM_BFD_DOWN);
/* Stop echo packet transmission if they are active */
if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE))
ptm_bfd_echo_stop(bfd);
/* Stop attempting to transmit or expect control packets if passive. */
if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_PASSIVE)) {
bfd_recvtimer_delete(bfd);
bfd_xmttimer_delete(bfd);
}
if (old_state != bfd->ses_state) {
bfd->stats.session_down++;
if (bglobal.debug_peer_event)
zlog_debug("state-change: [%s] %s -> %s reason:%s",
bs_to_string(bfd), state_list[old_state].str,
state_list[bfd->ses_state].str,
get_diag_str(bfd->local_diag));
}
/* clear peer's mac address */
UNSET_FLAG(bfd->flags, BFD_SESS_FLAG_MAC_SET);
memset(bfd->peer_hw_addr, 0, sizeof(bfd->peer_hw_addr));
/* reset local address ,it might has been be changed after bfd is up*/
memset(&bfd->local_address, 0, sizeof(bfd->local_address));
/* reset RTT */
bfd_rtt_init(bfd);
}
static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa,
uint32_t ldisc)
{
struct bfd_session *bs;
bs = bfd_id_lookup(ldisc);
if (bs == NULL)
return NULL;
switch (bs->key.family) {
case AF_INET:
if (memcmp(&sa->sa_sin.sin_addr, &bs->key.peer,
sizeof(sa->sa_sin.sin_addr)))
return NULL;
break;
case AF_INET6:
if (memcmp(&sa->sa_sin6.sin6_addr, &bs->key.peer,
sizeof(sa->sa_sin6.sin6_addr)))
return NULL;
break;
}
return bs;
}
struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *cp,
struct sockaddr_any *peer,
struct sockaddr_any *local,
struct interface *ifp,
vrf_id_t vrfid,
bool is_mhop)
{
struct vrf *vrf;
struct bfd_key key;
/* Find our session using the ID signaled by the remote end. */
if (cp->discrs.remote_discr)
return bfd_find_disc(peer, ntohl(cp->discrs.remote_discr));
/* Search for session without using discriminator. */
vrf = vrf_lookup_by_id(vrfid);
gen_bfd_key(&key, peer, local, is_mhop, ifp ? ifp->name : NULL,
vrf ? vrf->name : VRF_DEFAULT_NAME);
/* XXX maybe remoteDiscr should be checked for remoteHeard cases. */
return bfd_key_lookup(key);
}
void bfd_xmt_cb(struct event *t)
{
struct bfd_session *bs = EVENT_ARG(t);
ptm_bfd_xmt_TO(bs, 0);
}
void bfd_echo_xmt_cb(struct event *t)
{
struct bfd_session *bs = EVENT_ARG(t);
if (bs->echo_xmt_TO > 0)
ptm_bfd_echo_xmt_TO(bs);
}
/* Was ptm_bfd_detect_TO() */
void bfd_recvtimer_cb(struct event *t)
{
struct bfd_session *bs = EVENT_ARG(t);
switch (bs->ses_state) {
case PTM_BFD_INIT:
case PTM_BFD_UP:
ptm_bfd_sess_dn(bs, BD_CONTROL_EXPIRED);
break;
}
}
/* Was ptm_bfd_echo_detect_TO() */
void bfd_echo_recvtimer_cb(struct event *t)
{
struct bfd_session *bs = EVENT_ARG(t);
switch (bs->ses_state) {
case PTM_BFD_INIT:
case PTM_BFD_UP:
ptm_bfd_sess_dn(bs, BD_ECHO_FAILED);
break;
}
}
struct bfd_session *bfd_session_new(void)
{
struct bfd_session *bs;
bs = XCALLOC(MTYPE_BFDD_CONFIG, sizeof(*bs));
/* Set peer session defaults. */
bfd_profile_set_default(&bs->peer_profile);
bs->timers.desired_min_tx = BFD_DEFDESIREDMINTX;
bs->timers.required_min_rx = BFD_DEFREQUIREDMINRX;
bs->timers.required_min_echo_rx = BFD_DEF_REQ_MIN_ECHO_RX;
bs->timers.desired_min_echo_tx = BFD_DEF_DES_MIN_ECHO_TX;
bs->detect_mult = BFD_DEFDETECTMULT;
bs->mh_ttl = BFD_DEF_MHOP_TTL;
bs->ses_state = PTM_BFD_DOWN;
/* Initiate connection with slow timers. */
bs_set_slow_timers(bs);
/* Initiate remote settings as well. */
bs->remote_timers = bs->cur_timers;
bs->remote_detect_mult = BFD_DEFDETECTMULT;
bs->sock = -1;
monotime(&bs->uptime);
bs->downtime = bs->uptime;
return bs;
}
static void _bfd_session_update(struct bfd_session *bs,
struct bfd_peer_cfg *bpc)
{
if (bpc->bpc_has_txinterval) {
bs->timers.desired_min_tx = bpc->bpc_txinterval * 1000;
bs->peer_profile.min_tx = bs->timers.desired_min_tx;
}
if (bpc->bpc_has_recvinterval) {
bs->timers.required_min_rx = bpc->bpc_recvinterval * 1000;
bs->peer_profile.min_rx = bs->timers.required_min_rx;
}
if (bpc->bpc_has_detectmultiplier) {
bs->detect_mult = bpc->bpc_detectmultiplier;
bs->peer_profile.detection_multiplier = bs->detect_mult;
}
if (bpc->bpc_has_echorecvinterval) {
bs->timers.required_min_echo_rx = bpc->bpc_echorecvinterval * 1000;
bs->peer_profile.min_echo_rx = bs->timers.required_min_echo_rx;
}
if (bpc->bpc_has_echotxinterval) {
bs->timers.desired_min_echo_tx = bpc->bpc_echotxinterval * 1000;
bs->peer_profile.min_echo_tx = bs->timers.desired_min_echo_tx;
}
if (bpc->bpc_cbit)
SET_FLAG(bs->flags, BFD_SESS_FLAG_CBIT);
else
UNSET_FLAG(bs->flags, BFD_SESS_FLAG_CBIT);
if (bpc->bpc_has_minimum_ttl) {
bs->mh_ttl = bpc->bpc_minimum_ttl;
bs->peer_profile.minimum_ttl = bpc->bpc_minimum_ttl;
}
bs->peer_profile.echo_mode = bpc->bpc_echo;
bfd_set_echo(bs, bpc->bpc_echo);
/*
* Shutdown needs to be the last in order to avoid timers enable when
* the session is disabled.
*/
bs->peer_profile.admin_shutdown = bpc->bpc_shutdown;
bfd_set_passive_mode(bs, bpc->bpc_passive);
bfd_set_shutdown(bs, bpc->bpc_shutdown);
/*
* Apply profile last: it also calls `bfd_set_shutdown`.
*
* There is no problem calling `shutdown` twice if the value doesn't
* change or if it is overridden by peer specific configuration.
*/
if (bpc->bpc_has_profile)
bfd_profile_apply(bpc->bpc_profile, bs);
}
static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc)
{
/* User didn't want to update, return failure. */
if (bpc->bpc_createonly)
return -1;
_bfd_session_update(bs, bpc);
return 0;
}
void bfd_session_free(struct bfd_session *bs)
{
struct bfd_session_observer *bso;
bfd_session_disable(bs);
/* Remove session from data plane if any. */
bfd_dplane_delete_session(bs);
bfd_key_delete(bs->key);
bfd_id_delete(bs->discrs.my_discr);
/* Remove observer if any. */
TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) {
if (bso->bso_bs != bs)
continue;
break;
}
if (bso != NULL)
bs_observer_del(bso);
XFREE(MTYPE_BFDD_PROFILE, bs->profile_name);
XFREE(MTYPE_BFDD_CONFIG, bs);
}
struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc)
{
struct bfd_session *bfd, *l_bfd;
/* check to see if this needs a new session */
l_bfd = bs_peer_find(bpc);
if (l_bfd) {
/* Requesting a duplicated peer means update configuration. */
if (bfd_session_update(l_bfd, bpc) == 0)
return l_bfd;
else
return NULL;
}
/* Get BFD session storage with its defaults. */
bfd = bfd_session_new();
/*
* Store interface/VRF name in case we need to delay session
* start. See `bfd_session_enable` for more information.
*/
if (bpc->bpc_has_localif)
strlcpy(bfd->key.ifname, bpc->bpc_localif,
sizeof(bfd->key.ifname));
if (bpc->bpc_has_vrfname)
strlcpy(bfd->key.vrfname, bpc->bpc_vrfname,
sizeof(bfd->key.vrfname));
else
strlcpy(bfd->key.vrfname, VRF_DEFAULT_NAME,
sizeof(bfd->key.vrfname));
/* Copy remaining data. */
if (bpc->bpc_ipv4 == false)
SET_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6);
bfd->key.family = (bpc->bpc_ipv4) ? AF_INET : AF_INET6;
switch (bfd->key.family) {
case AF_INET:
memcpy(&bfd->key.peer, &bpc->bpc_peer.sa_sin.sin_addr,
sizeof(bpc->bpc_peer.sa_sin.sin_addr));
memcpy(&bfd->key.local, &bpc->bpc_local.sa_sin.sin_addr,
sizeof(bpc->bpc_local.sa_sin.sin_addr));
break;
case AF_INET6:
memcpy(&bfd->key.peer, &bpc->bpc_peer.sa_sin6.sin6_addr,
sizeof(bpc->bpc_peer.sa_sin6.sin6_addr));
memcpy(&bfd->key.local, &bpc->bpc_local.sa_sin6.sin6_addr,
sizeof(bpc->bpc_local.sa_sin6.sin6_addr));
break;
default:
assert(1);
break;
}
if (bpc->bpc_mhop)
SET_FLAG(bfd->flags, BFD_SESS_FLAG_MH);
bfd->key.mhop = bpc->bpc_mhop;
if (bs_registrate(bfd) == NULL)
return NULL;
/* Apply other configurations. */
_bfd_session_update(bfd, bpc);
return bfd;
}
struct bfd_session *bs_registrate(struct bfd_session *bfd)
{
/* Registrate session into data structures. */
bfd_key_insert(bfd);
bfd->discrs.my_discr = ptm_bfd_gen_ID();
bfd_id_insert(bfd);
/* Try to enable session and schedule for packet receive/send. */
if (bfd_session_enable(bfd) == -1) {
/* Unrecoverable failure, remove the session/peer. */
bfd_session_free(bfd);
return NULL;
}
/* Add observer if we have moving parts. */
if (bfd->key.ifname[0] || bfd->key.vrfname[0] || bfd->sock == -1)
bs_observer_add(bfd);
if (bglobal.debug_peer_event)
zlog_debug("session-new: %s", bs_to_string(bfd));
return bfd;
}
int ptm_bfd_sess_del(struct bfd_peer_cfg *bpc)
{
struct bfd_session *bs;
/* Find session and call free(). */
bs = bs_peer_find(bpc);
if (bs == NULL)
return -1;
/* This pointer is being referenced, don't let it be deleted. */
if (bs->refcount > 0) {
zlog_err("session-delete: refcount failure: %" PRIu64" references",
bs->refcount);
return -1;
}
if (bglobal.debug_peer_event)
zlog_debug("%s: %s", __func__, bs_to_string(bs));
bfd_session_free(bs);
return 0;
}
void bfd_set_polling(struct bfd_session *bs)
{
/*
* Start polling procedure: the only timers that require polling
* to change value without losing connection are:
*
* - Desired minimum transmission interval;
* - Required minimum receive interval;
*
* RFC 5880, Section 6.8.3.
*/
bs->polling = 1;
}
/*
* bs_<state>_handler() functions implement the BFD state machine
* transition mechanism. `<state>` is the current session state and
* the parameter `nstate` is the peer new state.
*/
static void bs_admin_down_handler(struct bfd_session *bs
__attribute__((__unused__)),
int nstate __attribute__((__unused__)))
{
/*
* We are administratively down, there is no state machine
* handling.
*/
}
static void bs_down_handler(struct bfd_session *bs, int nstate)
{
switch (nstate) {
case PTM_BFD_ADM_DOWN:
/*
* Remote peer doesn't want to talk, so lets keep the
* connection down.
*/
case PTM_BFD_UP:
/* Peer can't be up yet, wait it go to 'init' or 'down'. */
break;
case PTM_BFD_DOWN:
/*
* Remote peer agreed that the path is down, lets try to
* bring it up.
*/
bs->ses_state = PTM_BFD_INIT;
/*
* RFC 5880, Section 6.1.
* A system taking the Passive role MUST NOT begin
* sending BFD packets for a particular session until
* it has received a BFD packet for that session, and thus
* has learned the remote system's discriminator value.
*
* Now we can start transmission timer in passive mode.
*/
if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE))
ptm_bfd_xmt_TO(bs, 0);
break;
case PTM_BFD_INIT:
/*
* Remote peer told us his path is up, lets turn
* activate the session.
*/
ptm_bfd_sess_up(bs);
break;
default:
if (bglobal.debug_peer_event)
zlog_debug("state-change: unhandled neighbor state: %d",
nstate);
break;
}
}
static void bs_init_handler(struct bfd_session *bs, int nstate)
{
switch (nstate) {
case PTM_BFD_ADM_DOWN:
/*
* Remote peer doesn't want to talk, so lets make the
* connection down.
*/
ptm_bfd_sess_dn(bs, BD_NEIGHBOR_DOWN);
break;
case PTM_BFD_DOWN:
/* Remote peer hasn't moved to first stage yet. */
break;
case PTM_BFD_INIT:
case PTM_BFD_UP:
/* We agreed on the settings and the path is up. */
ptm_bfd_sess_up(bs);
break;
default:
if (bglobal.debug_peer_event)
zlog_debug("state-change: unhandled neighbor state: %d",
nstate);
break;
}
}