summaryrefslogtreecommitdiff
path: root/tests/bfd.at
blob: f5c6409f6c656ad6e807c85056ff0c51ca4a645a (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
AT_BANNER([bfd])

m4_define([BFD_CHECK], [
AT_CHECK([ovs-appctl bfd/show $1 | sed -e '/Time:/d' | sed -e '/Discriminator/d' | sed -e '/Interval:/d'| sed -e '/Multiplier/d'],[0],
[dnl
  Forwarding: $2
  Concatenated Path Down: $3

  Local Flags: $4
  Local Session State: $5
  Local Diagnostic: $6

  Remote Flags: $7
  Remote Session State: $8
  Remote Diagnostic: $9
])
])

m4_define([BFD_CHECK_TX], [
AT_CHECK([ovs-appctl bfd/show $1 | sed -n '/TX Interval/p'],[0],
[dnl
  TX Interval: Approx $2
  Local Minimum TX Interval: $3
  Remote Minimum TX Interval: $4
])
])

m4_define([BFD_CHECK_RX], [
AT_CHECK([ovs-appctl bfd/show $1 | sed -n '/RX Interval/p'],[0],
[dnl
  RX Interval: Approx $2
  Local Minimum RX Interval: $3
  Remote Minimum RX Interval: $4
])
])

m4_define([BFD_VSCTL_LIST_IFACE], [
AT_CHECK([ovs-vsctl list interface $1 | sed -n $2],[0],
[dnl
$3
])
])

m4_define([BFD_CHECK_MULT], [
AT_CHECK([ovs-appctl bfd/show $1 | sed -n '/Detect Multiplier/p'],[0],
[dnl
  Detect Multiplier: $2
  Remote Detect Multiplier: $3
])
])

AT_SETUP([bfd - basic config on different bridges])
#Create 2 bridges connected by patch ports and enable BFD
OVS_VSWITCHD_START(
   [add-br br1 -- \
       set bridge br1 datapath-type=dummy \
       other-config:hwaddr=aa:55:aa:56:00:00 -- \
    add-port br1 p1 -- set Interface p1 type=patch \
       options:peer=p0 -- \
    add-port br0 p0 -- set Interface p0 type=patch \
       options:peer=p1 -- \
    set Interface p0 bfd:enable=true -- \
    set Interface p1 bfd:enable=true ])
ovs-appctl time/stop
ovs-appctl time/warp 4100 100

#Verify that BFD has been enabled on both interfaces.
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])

AT_CHECK([ ovs-vsctl set interface p0 bfd:enable=false])
ovs-appctl time/warp 4100 100
BFD_CHECK([p1], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])

AT_CHECK([ ovs-vsctl set interface p0 bfd:enable=true])
ovs-appctl time/warp 4100 100
BFD_CHECK([p1], [true], [false], [none], [up], [Control Detection Time Expired], [none], [up], [No Diagnostic])
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [Control Detection Time Expired])

ovs-vsctl del-br br0
AT_CHECK([ovs-appctl bfd/show p0], [2],[ignore], [no such bfd object
ovs-appctl: ovs-vswitchd: server returned an error
])
ovs-vsctl del-br br1
#Check that the entries are gone.
AT_CHECK([ovs-appctl bfd/show p1], [2],[ignore], [no such bfd object
ovs-appctl: ovs-vswitchd: server returned an error
])

OVS_VSWITCHD_STOP
AT_CLEANUP


AT_SETUP([bfd - Verify tunnel down detection])
#Create 3 bridges - br-bfd0, br-bfd1 and br-sw which is midway between the two. br-sw is
#connected to br-bfd0 and br-bfd1 through patch ports p0-sw and p1-sw. Enable BFD on
#interfaces in br-bfd0 and br-bfd1. When br-sw is dropping all packets, BFD should detect
# that the tunnel is down, and come back up when br-sw is working fine.

OVS_VSWITCHD_START(
   [add-br br-bfd0 -- \
       set bridge br-bfd0 datapath-type=dummy \
       other-config:hwaddr=aa:55:aa:56:00:00 -- \
    add-br br-bfd1 -- \
       set bridge br-bfd1 datapath-type=dummy \
       other-config:hwaddr=aa:55:aa:57:00:00 -- \
    add-br br-sw -- \
       set bridge br-sw datapath-type=dummy \
       other-config:hwaddr=aa:55:aa:58:00:00 -- \
    add-port br-sw p1-sw -- set Interface p1-sw type=patch \
       options:peer=p1 ofport_request=2 -- \
    add-port br-sw p0-sw -- set Interface p0-sw type=patch \
       options:peer=p0 ofport_request=1 -- \
    add-port br-bfd1 p1 -- set Interface p1 type=patch \
       options:peer=p1-sw bfd:enable=true -- \
    add-port br-bfd0 p0 -- set Interface p0 type=patch \
       options:peer=p0-sw bfd:enable=true --])

ovs-appctl time/stop

#Create 2 bridges connected by patch ports and enable BFD

AT_CHECK([ovs-ofctl add-flow br-sw 'priority=0,actions=NORMAL'])
#Verify that BFD is enabled.
ovs-appctl time/warp 4100 100
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])

#Drop all packets in the br-sw bridge so that the tunnel is down.
AT_CHECK([ ovs-ofctl add-flow br-sw 'priority=5,actions=drop' ])
ovs-appctl time/warp 4100 100
BFD_CHECK([p1], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])
BFD_CHECK([p0], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])

#Delete the added flow
AT_CHECK([ovs-ofctl del-flows br-sw], [0])
AT_CHECK([ovs-ofctl add-flow br-sw 'priority=0,actions=NORMAL'])
#Verify that BFD is back up again.
ovs-appctl time/warp 4100 100

BFD_CHECK([p1], [true], [false], [none], [up], [Control Detection Time Expired], [none], [up], [Control Detection Time Expired])
BFD_CHECK([p0], [true], [false], [none], [up], [Control Detection Time Expired], [none], [up], [Control Detection Time Expired])

#Now, Verify one-side tunnel down detection
#When br-sw is dropping packets from one end, BFD should detect
# that the tunnel is down, and come back up when br-sw is working fine.

#Bring down the br-bfd1 - br-sw link. So BFD packets will be sent from p0,
# but not received by p1. p0 will receive all BFD packets from p1.

AT_CHECK([ ovs-ofctl add-flow br-sw 'in_port=1,priority=5,actions=drop'])
ovs-appctl time/warp 4100 100
# Make sure p1 BFD state is down since it received no BFD packets.
BFD_CHECK([p1], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])
ovs-appctl time/warp 4100 100

# p0 will be in init state once it receives "down" BFD message from p1.
BFD_CHECK([p0], [false], [false], [none], [init], [Neighbor Signaled Session Down], [none], [down], [Control Detection Time Expired])

AT_CHECK([ovs-ofctl del-flows br-sw])
AT_CHECK([ovs-ofctl add-flow br-sw 'priority=0,actions=NORMAL'])
#Ensure that BFD is back up again.

ovs-appctl time/warp 1100 100
#Bring down the br-bfd0 - br-sw link
AT_CHECK([ ovs-ofctl add-flow br-sw 'in_port=2,priority=5,actions=drop'])
ovs-appctl time/warp 4100 100
BFD_CHECK([p0], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])
ovs-appctl time/warp 4100 100
BFD_CHECK([p1], [false], [false], [none], [init], [Neighbor Signaled Session Down], [none], [down], [Control Detection Time Expired])
OVS_VSWITCHD_STOP
AT_CLEANUP


AT_SETUP([bfd - concatenated path down])
#Create 2 bridges connected by patch ports and enable BFD
OVS_VSWITCHD_START()
ovs-appctl time/stop
AT_CHECK([ ovs-vsctl -- add-br br1 -- \
           set bridge br1 datapath-type=dummy \
           other-config:hwaddr=aa:55:aa:56:00:00 ])
AT_CHECK([ ovs-vsctl -- add-port br1 p1 -- set Interface p1 type=patch \
           options:peer=p0 ])
AT_CHECK([ ovs-vsctl -- add-port br0 p0 -- set Interface p0 type=patch \
           options:peer=p1 ])
AT_CHECK([ ovs-vsctl -- set interface p0 bfd:enable=true ])
AT_CHECK([ ovs-vsctl -- set interface p1 bfd:enable=true ])
ovs-appctl time/warp 4100 100

#Verify that BFD has been enabled on both interfaces.
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])

#Set cpath_down to true on one interface, make sure the remote interface updates its values.
AT_CHECK([ovs-vsctl set interface p0 bfd:cpath_down=true])
ovs-appctl time/warp 4100 100
BFD_CHECK([p1], [false], [false], [none], [up], [No Diagnostic], [none], [up], [Concatenated Path Down])
OVS_VSWITCHD_STOP
AT_CLEANUP


AT_SETUP([bfd - Edit the Min Tx/Rx values])
#Create 2 bridges connected by patch ports and enable BFD
OVS_VSWITCHD_START()
ovs-appctl time/stop
AT_CHECK([ ovs-vsctl -- add-br br1 -- \
           set bridge br1 datapath-type=dummy ])
AT_CHECK([ ovs-vsctl -- add-port br1 p1 -- set Interface p1 type=patch \
           options:peer=p0 ])
AT_CHECK([ ovs-vsctl -- add-port br0 p0 -- set Interface p0 type=patch \
           options:peer=p1 ])
AT_CHECK([ ovs-vsctl -- set interface p0 bfd:enable=true ])
AT_CHECK([ ovs-vsctl -- set interface p1 bfd:enable=true ])
ovs-appctl time/warp 3100 100
#Verify that BFD has been enabled on both interfaces.
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
#Edit the min Tx value.
AT_CHECK([ovs-vsctl set interface p0 bfd:min_tx=200])
ovs-appctl time/warp 2100 100
BFD_CHECK_TX([p0], [1000ms], [200ms], [100ms])
BFD_CHECK_TX([p1], [1000ms], [100ms], [200ms])

#Edit the min Rx value.
AT_CHECK([ovs-vsctl set interface p1 bfd:min_rx=300])
ovs-appctl time/warp 2100 100
BFD_CHECK_RX([p1], [300ms], [300ms], [1000ms])
BFD_CHECK_RX([p0], [1000ms], [1000ms], [300ms])

OVS_VSWITCHD_STOP
AT_CLEANUP

AT_SETUP([bfd - check_tnl_key])
OVS_VSWITCHD_START([add-port br0 p1 -- set Interface p1 type=gre \
                    options:remote_ip=2.2.2.2 options:key=1 ofport_request=1 -- \
                    set interface p1 bfd:enable=true -- \
                    set bridge br0 fail-mode=standalone])

# by default check_tnl_key is false. so we should process a bfd packet with tun_id=1.
AT_CHECK([ovs-appctl ofproto/trace --l7-len 0 ovs-dummy 'tunnel(tun_id=0x1,src=2.2.2.2,dst=2.2.2.1,tos=0x0,ttl=64,tp_src=0,tp_dst=0,flags(key)),in_port(1),skb_mark(0/0),eth(src=00:11:22:33:44:55,dst=00:23:20:00:00:01),eth_type(0x0800),ipv4(src=169.254.1.0/0.0.0.0,dst=169.254.1.1/0.0.0.0,proto=17/0xff,tos=0/0,ttl=255/0,frag=no),udp(src=49152/0,dst=3784/0xffff)' -generate], [0], [stdout])
# check that the packet should be handled as BFD packet.
AT_CHECK([tail -2 stdout], [0], [dnl
This flow is handled by the userspace slow path because it:
  - Consists of BFD packets.
], [])

# turn on the check_tnl_key.
AT_CHECK([ovs-vsctl set interface p1 bfd:check_tnl_key=true])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'tunnel(tun_id=0x1,src=2.2.2.2,dst=2.2.2.1,tos=0x0,ttl=64,tp_src=0,tp_dst=0,flags(key)),in_port(1),skb_mark(0/0),eth(src=00:11:22:33:44:55,dst=00:23:20:00:00:01),eth_type(0x0800),ipv4(src=169.254.1.0/0.0.0.0,dst=169.254.1.1/0.0.0.0,proto=17/0xff,tos=0/0,ttl=255/0,frag=no),udp(src=49152/0,dst=3784/0xffff)' -generate], [0], [stdout])
# check that the packet should be handled as normal packet.
AT_CHECK([tail -1 stdout], [0],[dnl
Datapath actions: 100
], [])

# set the tunnel key to 0.
AT_CHECK([ovs-vsctl set interface p1 options:key=0])
AT_CHECK([ovs-appctl ofproto/trace --l7-len 0 ovs-dummy 'tunnel(tun_id=0x0,src=2.2.2.2,dst=2.2.2.1,tos=0x0,ttl=64,tp_src=0,tp_dst=0,flags(key)),in_port(1),skb_mark(0/0),eth(src=00:11:22:33:44:55,dst=00:23:20:00:00:01),eth_type(0x0800),ipv4(src=169.254.1.0/0.0.0.0,dst=169.254.1.1/0.0.0.0,proto=17/0xff,tos=0/0,ttl=255/0,frag=no),udp(src=49152/0,dst=3784/0xffff)' -generate], [0], [stdout])
# check that the packet should be handled as BFD packet.
AT_CHECK([tail -2 stdout], [0], [dnl
This flow is handled by the userspace slow path because it:
  - Consists of BFD packets.
], [])

OVS_VSWITCHD_STOP
AT_CLEANUP

# Tests below are for bfd decay features.
AT_SETUP([bfd - bfd decay])
AT_SKIP_IF([test "$IS_ARM64" = "yes"])
OVS_VSWITCHD_START([add-br br1 -- set bridge br1 datapath-type=dummy -- \
                    add-port br1 p1 -- set Interface p1 type=patch \
                    options:peer=p0 ofport_request=2 -- \
                    add-port br0 p0 -- set Interface p0 type=patch \
                    options:peer=p1 ofport_request=1 -- \
                    set Interface p0 bfd:enable=true bfd:min_tx=300 bfd:min_rx=300 -- \
                    set Interface p1 bfd:enable=true bfd:min_tx=500 bfd:min_rx=500])

ovs-appctl time/stop

# wait for a while to stablize everything.
ovs-appctl time/warp 10000 500
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK_TX([p0], [500ms], [300ms], [500ms])
BFD_CHECK_RX([p0], [500ms], [300ms], [500ms])

# Test-1 BFD decay: decay to decay_min_rx
AT_CHECK([ovs-vsctl set interface p0 bfd:decay_min_rx=3000])
# bfd:decay_min_rx is set to 3000ms after the local state of p0 goes up,
# so for the first 2000ms, there should be no change.
ovs-appctl time/warp 2000 500
BFD_CHECK_TX([p0], [500ms], [300ms], [500ms])
BFD_CHECK_RX([p0], [500ms], [300ms], [500ms])

# advance the clock by 5000ms.
ovs-appctl time/warp 5000 500
# now, min_rx should decay to 3000ms.
BFD_CHECK_TX([p0], [500ms], [300ms], [500ms])
BFD_CHECK_RX([p0], [3000ms], [3000ms], [500ms])

# advance clock by 5000ms and check the flags are all 'none'.
ovs-appctl time/warp 5000 500
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK_TX([p0], [500ms], [300ms], [500ms])
BFD_CHECK_RX([p0], [3000ms], [3000ms], [500ms])
# End of Test-1 ###############################################################


# Test-2 BFD decay: go back to min_rx when there is traffic
# receive packet at 1/100ms rate for 5000ms.
for i in `seq 0 49`
do
    ovs-appctl time/warp 100
    AT_CHECK([ovs-ofctl packet-out br1 "in_port=3 packet=90e2ba01475000101856b2e80806000108000604000100101856b2e80202020300000000000002020202 actions=2"],
             [0], [stdout], [])
done
# after a decay interval (3000ms), the p0 min_rx will go back to
# min_rx.
BFD_CHECK_TX([p0], [500ms], [300ms], [500ms])
BFD_CHECK_RX([p0], [500ms], [300ms], [500ms])
# End of Test-2 ###############################################################


# Test-3 BFD decay: set decay_min_rx to 1000ms.
# this should firstly reset the min_rx and then re-decay to 1000ms.
AT_CHECK([ovs-vsctl set Interface p0 bfd:decay_min_rx=1000])
# advance the clock by 10000ms, decay should have happened.
ovs-appctl time/warp 10000 500
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK_TX([p0], [500ms], [300ms], [500ms])
BFD_CHECK_RX([p0], [1000ms], [1000ms], [500ms])
# End of Test-3 ###############################################################


# Test-4 BFD decay: set decay_min_rx to 0 to disable bfd decay.
AT_CHECK([ovs-vsctl set Interface p0 bfd:decay_min_rx=0])
# advance the clock by 5000ms.
ovs-appctl time/warp 10000 500
# min_rx is reset.
BFD_CHECK_TX([p0], [500ms], [300ms], [500ms])
BFD_CHECK_RX([p0], [500ms], [300ms], [500ms])

for i in `seq 0 20`
do
    ovs-appctl time/warp 500
    BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
    BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
    BFD_CHECK_TX([p0], [500ms], [300ms], [500ms])
    BFD_CHECK_RX([p0], [500ms], [300ms], [500ms])
done
# End of Test-4 ################################################################


# Test-5 BFD decay: rmt_min_tx is greater than decay_min_rx
AT_CHECK([ovs-vsctl set Interface p0 bfd:decay_min_rx=3000 -- set interface p1 bfd:min_tx=5000])
# advance the clock by 10000ms to stable everything.
ovs-appctl time/warp 10000 500
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
# p0 rx should show 5000ms even if it is in decay.
BFD_CHECK_TX([p0], [500ms], [300ms], [5000ms])
BFD_CHECK_RX([p0], [5000ms], [3000ms], [500ms])
# then, there should be no change of status,
for i in `seq 0 19`
do
    ovs-appctl time/warp 500
    BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
    BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
    BFD_CHECK_TX([p0], [500ms], [300ms], [5000ms])
    BFD_CHECK_RX([p0], [5000ms], [3000ms], [500ms])
done
# reset the p1's min_tx to 500ms.
AT_CHECK([ovs-vsctl set Interface p1 bfd:min_tx=500])
# advance the clock by 20000ms to stable everything.
# since p0 has been in decay, now the RX will show 3000ms.
ovs-appctl time/warp 20000 500
BFD_CHECK_TX([p0], [500ms], [300ms], [500ms])
BFD_CHECK_RX([p0], [3000ms], [3000ms], [500ms])
# End of Test-5 ###############################################################


# Test-6 BFD decay: state up->down->up.
# turn bfd off on p1
AT_CHECK([ovs-vsctl set Interface p1 bfd:enable=false])

# advance the clock by 15000ms to stable everything.
ovs-appctl time/warp 15000 1000
BFD_CHECK([p0], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])
BFD_CHECK_TX([p0], [1000ms], [1000ms], [0ms])
BFD_CHECK_RX([p0], [300ms], [300ms], [1ms])

# resume the bfd on p1. the bfd should not go to decay mode direclty.
AT_CHECK([ovs-vsctl set Interface p1 bfd:enable=true])
ovs-appctl time/warp 1500 500
BFD_CHECK_TX([p0], [500ms], [300ms], [500ms])
BFD_CHECK_RX([p0], [500ms], [300ms], [500ms])

# since the decay_min_rx is still 3000ms, so after 5000ms, p0 should have decayed.
ovs-appctl time/warp 5000 500
BFD_CHECK_TX([p0], [500ms], [300ms], [500ms])
BFD_CHECK_RX([p0], [3000ms], [3000ms], [500ms])
# End of Test-6 ################################################################

OVS_VSWITCHD_STOP
AT_CLEANUP

# Tests below are for bfd forwarding_if_rx feature.

# forwarding_if_rx Test1
# Test1 tests the case when bfd is only enabled on one end of the link.
# Under this situation, the forwarding flag should always be false, even
# though there is data packet received, since there is no bfd control
# packet received during the demand_rx_bfd interval.
AT_SETUP([bfd - bfd forwarding_if_rx - bfd on one side])
OVS_VSWITCHD_START([add-br br1 -- set bridge br1 datapath-type=dummy -- \
                    add-port br1 p1 -- set Interface p1 type=patch \
                    options:peer=p0 ofport_request=2 -- \
                    add-port br0 p0 -- set Interface p0 type=patch \
                    options:peer=p1 ofport_request=1 -- \
                    set Interface p0 bfd:enable=true bfd:min_tx=500 bfd:min_rx=500 -- \
                    add-port br1 p2 -- set Interface p2 type=internal ofport_request=3])

ovs-appctl time/stop
# check the inital status.
BFD_CHECK([p0], [false], [false], [none], [down], [No Diagnostic], [none], [down], [No Diagnostic])
BFD_CHECK_TX([p0], [1000ms], [1000ms], [0ms])
BFD_CHECK_RX([p0], [500ms], [500ms], [1ms])

# enable forwarding_if_rx.
AT_CHECK([ovs-vsctl set Interface p0 bfd:forwarding_if_rx=true], [0])

# there should be no change of forwarding flag, since
# there is no traffic.
for i in `seq 0 3`
do
    ovs-appctl time/warp 500
    BFD_CHECK([p0], [false], [false], [none], [down], [No Diagnostic], [none], [down], [No Diagnostic])
done

# receive packet at 1/100ms rate for 2000ms.
for i in `seq 0 19`
do
    ovs-appctl time/warp 100
    AT_CHECK([ovs-ofctl packet-out br1 "in_port=3 packet=90e2ba01475000101856b2e80806000108000604000100101856b2e80202020300000000000002020202 actions=2"],
             [0], [stdout], [])
done
# the forwarding flag should be false, due to the demand_rx_bfd.
BFD_CHECK([p0], [false], [false], [none], [down], [No Diagnostic], [none], [down], [No Diagnostic])

AT_CHECK([ovs-vsctl del-br br1], [0], [ignore])
OVS_VSWITCHD_STOP
AT_CLEANUP


# forwarding_if_rx Test2
# Test2 is for testing that the enable of forwarding_if_rx will not
# affect the normal bfd communication.  bfd is enabled on both ends of
# the link.
AT_SETUP([bfd - bfd forwarding_if_rx - bfd on both sides])
OVS_VSWITCHD_START([add-br br1 -- set bridge br1 datapath-type=dummy -- \
                    add-port br1 p1 -- set Interface p1 type=patch \
                    options:peer=p0 ofport_request=2 -- \
                    add-port br0 p0 -- set Interface p0 type=patch \
                    options:peer=p1 ofport_request=1 -- \
                    set Interface p0 bfd:enable=true bfd:min_tx=500 bfd:min_rx=500 -- \
                    set Interface p1 bfd:enable=true bfd:min_tx=300 bfd:min_rx=300 -- \
                    add-port br1 p2 -- set Interface p2 type=internal ofport_request=3])

ovs-appctl time/stop
# advance the clock, to stablize the states.
ovs-appctl time/warp 5000 500

# enable forwarding_if_rx.
AT_CHECK([ovs-vsctl set Interface p0 bfd:forwarding_if_rx=true], [0])

# there should be no change of the forwarding flag, since
# the bfd on both ends is already up.
for i in `seq 0 5`
do
    ovs-appctl time/warp 500
    BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
done

# stop the bfd on one side.
AT_CHECK([ovs-vsctl set Interface p1 bfd:enable=false], [0])
# for within 1500ms, the detection timer is not out.
# there is no change to status.
for i in `seq 0 1`
do
    ovs-appctl time/warp 500
    BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
    for i in `seq 0 4`
    do
        AT_CHECK([ovs-ofctl packet-out br1 "in_port=3 packet=90e2ba01475000101856b2e80806000108000604000100101856b2e80202020300000000000002020202 actions=2"],
                 [0], [stdout], [])
    done
done

# at 1500ms, the STATE should go DOWN, due to Control Detection Time Expired.
# but forwarding flag should be still true.
ovs-appctl time/warp 500
BFD_CHECK([p0], [true], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])

# reset bfd forwarding_if_rx.
AT_CHECK([ovs-vsctl set Interface p0 bfd:forwarding_if_rx=false], [0])
# forwarding flag should turn to false since the STATE is DOWN.
BFD_CHECK([p0], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])

# re-enable bfd on the other end. the states should be up.
AT_CHECK([ovs-vsctl set Interface p1 bfd:enable=true bfd:min_tx=300 bfd:min_rx=300])
# advance the clock, to stablize the states.
ovs-appctl time/warp 5000 500
BFD_CHECK([p0], [true], [false], [none], [up], [Control Detection Time Expired], [none], [up], [No Diagnostic])
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [Control Detection Time Expired])
BFD_CHECK_TX([p0], [500ms], [500ms], [300ms])
BFD_CHECK_RX([p0], [500ms], [500ms], [300ms])

AT_CHECK([ovs-vsctl del-br br1], [0], [ignore])
OVS_VSWITCHD_STOP
AT_CLEANUP

# forwarding_if_rx Test3
# Test3 is for testing that the enable of forwarding_if_rx will not
# affect the bfd decay feature.  bfd is enabled on both ends of the link.
AT_SETUP([bfd - bfd forwarding_if_rx - with bfd decay])
OVS_VSWITCHD_START([add-br br1 -- set bridge br1 datapath-type=dummy -- \
                    add-port br1 p1 -- set Interface p1 type=patch \
                    options:peer=p0 ofport_request=2 -- \
                    add-port br0 p0 -- set Interface p0 type=patch \
                    options:peer=p1 ofport_request=1 -- \
                    set Interface p0 bfd:enable=true bfd:min_tx=300 bfd:min_rx=300 bfd:decay_min_rx=3000 -- \
                    set Interface p1 bfd:enable=true bfd:min_tx=500 bfd:min_rx=500])

ovs-appctl time/stop
# advance the clock, to stablize the states.
ovs-appctl time/warp 10000 500
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK_TX([p0], [500ms], [300ms], [500ms])
BFD_CHECK_RX([p0], [3000ms], [3000ms], [500ms])

# enable forwarding_if_rx.
AT_CHECK([ovs-vsctl set Interface p0 bfd:forwarding_if_rx=true], [0])

# there should be no change of the forwarding flag, since
# the bfd on both ends is already up.
for i in `seq 0 9`
do
    ovs-appctl time/warp 500
    BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
done

# reconfigure the decay_min_rx to 1000ms.
AT_CHECK([ovs-vsctl set interface p0 bfd:decay_min_rx=1000])

# wait for 5000ms to decay.
ovs-appctl time/warp 5000 500
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK_TX([p0], [500ms], [300ms], [500ms])
BFD_CHECK_RX([p0], [1000ms], [1000ms], [500ms])

# stop the bfd on one side.
AT_CHECK([ovs-vsctl set Interface p1 bfd:enable=false], [0])

# advance clock by 4000ms, while receiving packets.
# the STATE should go DOWN, due to Control Detection Time Expired.
# but forwarding flag should be still true.
for i in `seq 0 7`
do
    ovs-appctl time/warp 500
    AT_CHECK([ovs-ofctl packet-out br1 "in_port=3 packet=90e2ba01475000101856b2e80806000108000604000100101856b2e80202020300000000000002020202 actions=2"],
             [0], [stdout], [])
done
BFD_CHECK([p0], [true], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])

# receive packet at 1/100ms rate for 1000ms.
for i in `seq 0 9`
do
    AT_CHECK([ovs-ofctl packet-out br1 "in_port=3 packet=90e2ba01475000101856b2e80806000108000604000100101856b2e80202020300000000000002020202 actions=2"],
             [0], [stdout], [])
    ovs-appctl time/warp 100
    # the forwarding flag should always be true during this time.
    BFD_CHECK([p0], [true], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])
done

# stop receiving for 5000ms.
ovs-appctl time/warp 5000 100
BFD_CHECK([p0], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])

# reset bfd forwarding_if_rx.
AT_CHECK([ovs-vsctl set Interface p0 bfd:forwarding_if_rx=false])
BFD_CHECK([p0], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])
# re-enable bfd forwarding_if_rx.
AT_CHECK([ovs-vsctl set Interface p0 bfd:forwarding_if_rx=true])
BFD_CHECK([p0], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])

# re-enable bfd on the other end. the states should be up.
AT_CHECK([ovs-vsctl set Interface p1 bfd:enable=true bfd:min_tx=300 bfd:min_rx=300])
# advance the clock, to stablize the states.
ovs-appctl time/warp 10000 500
BFD_CHECK([p0], [true], [false], [none], [up], [Control Detection Time Expired], [none], [up], [No Diagnostic])
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [Control Detection Time Expired])
BFD_CHECK_TX([p0], [300ms], [300ms], [300ms])
BFD_CHECK_RX([p0], [1000ms], [1000ms], [300ms])

AT_CHECK([ovs-vsctl del-br br1], [0], [ignore])
OVS_VSWITCHD_STOP
AT_CLEANUP

# forwarding_if_rx Test4
# Test4 is for testing the demand_rx_bfd feature.
# bfd is enabled on both ends of the link.
AT_SETUP([bfd - bfd forwarding_if_rx - demand_rx_bfd])
OVS_VSWITCHD_START([add-br br1 -- set bridge br1 datapath-type=dummy -- \
                    add-port br1 p1 -- set Interface p1 type=patch \
                    options:peer=p0 ofport_request=2 -- \
                    add-port br0 p0 -- set Interface p0 type=patch \
                    options:peer=p1 ofport_request=1 -- \
                    set Interface p0 bfd:enable=true bfd:min_tx=300 bfd:min_rx=300 bfd:forwarding_if_rx=true -- \
                    set Interface p1 bfd:enable=true bfd:min_tx=500 bfd:min_rx=500])

ovs-appctl time/stop
# advance the clock, to stablize the states.
ovs-appctl time/warp 10000 500
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK_TX([p0], [500ms], [300ms], [500ms])

# disable the bfd on p1.
AT_CHECK([ovs-vsctl set Interface p1 bfd:enable=false], [0])

# advance clock by 4000ms, while receiving packets.
# the STATE should go DOWN, due to Control Detection Time Expired.
# but forwarding flag should be still true.
for i in `seq 0 7`
do
    ovs-appctl time/warp 500
    AT_CHECK([ovs-ofctl packet-out br1 "in_port=3 packet=90e2ba01475000101856b2e80806000108000604000100101856b2e80202020300000000000002020202 actions=2"],
             [0], [stdout], [])
done
BFD_CHECK([p0], [true], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])

# advance clock long enough to trigger the demand_bfd_rx interval
# (100 * bfd->cfm_min_rx), forwarding flag should go down since there
# is no bfd control packet received during the demand_rx_bfd.
for i in `seq 0 120`
do
    ovs-appctl time/warp 300
    AT_CHECK([ovs-ofctl packet-out br1 "in_port=3 packet=90e2ba01475000101856b2e80806000108000604000100101856b2e80202020300000000000002020202 actions=2"],
             [0], [stdout], [])
done
BFD_CHECK([p0], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])

# now enable the bfd on p1 again.
AT_CHECK([ovs-vsctl set Interface p1 bfd:enable=true], [0])
# advance clock by 5000ms.  and p1 and p0 should be all up.
ovs-appctl time/warp 5000 500
BFD_CHECK([p0], [true], [false], [none], [up], [Control Detection Time Expired], [none], [up], [No Diagnostic])
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [Control Detection Time Expired])
BFD_CHECK_TX([p0], [500ms], [300ms], [500ms])

# disable the bfd on p1 again.
AT_CHECK([ovs-vsctl set Interface p1 bfd:enable=false], [0])
# advance clock long enough to trigger the demand_rx_bfd,
# forwarding flag should go down since there is no bfd control packet
# received during the demand_rx_bfd.
for i in `seq 0 120`
do
    ovs-appctl time/warp 300
    AT_CHECK([ovs-ofctl packet-out br1 "in_port=3 packet=90e2ba01475000101856b2e80806000108000604000100101856b2e80202020300000000000002020202 actions=2"],
             [0], [stdout], [])
done
BFD_CHECK([p0], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])

AT_CHECK([ovs-vsctl del-br br1], [0], [ignore])
OVS_VSWITCHD_STOP
AT_CLEANUP

# test bfd:flap_count.
# This test contains three part:
# part 1. tests the flap_count on normal bfd monitored link.
# part 2. tests the flap_count when forwarding override is used.
# part 3. tests the flap_count when forwarding_if_rx is enabled.
AT_SETUP([bfd - flap_count])
#Create 2 bridges connected by patch ports and enable bfd
OVS_VSWITCHD_START([add-br br1 -- \
                    set bridge br1 datapath-type=dummy \
                    other-config:hwaddr=aa:55:aa:56:00:00 -- \
                    add-port br1 p1 -- set Interface p1 type=patch \
                    options:peer=p0 ofport_request=2 -- \
                    add-port br0 p0 -- set Interface p0 type=patch \
                    options:peer=p1 ofport_request=1 -- \
                    set Interface p0 bfd:enable=true bfd:min_tx=100 bfd:min_rx=100 -- \
                    set Interface p1 bfd:enable=true bfd:min_tx=100 bfd:min_rx=100])

ovs-appctl time/stop
# Disable the stats update to prevent the race between ovsdb updating
# stats and ovs-vsctl cmd closing the jsonrpc session.
AT_CHECK([ovs-vsctl set Open_vSwitch . other_config:stats-update-interval=50000000])

# Part-1 wait for a while to stablize bfd.
ovs-appctl time/warp 10100 100
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK_TX([p0], [100ms], [100ms], [100ms])
BFD_CHECK_RX([p0], [100ms], [100ms], [100ms])
# both p0 and p1 should have flap_count = "1". since down->up.
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["1"])
BFD_VSCTL_LIST_IFACE([p1], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["1"])

# turn bfd on p1 off, should increment the bfd:flap_count on p0.
AT_CHECK([ovs-vsctl set interface p1 bfd:enable=false])
ovs-appctl time/warp 5000 100
BFD_CHECK([p0], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["2"])
AT_CHECK([ovs-vsctl list interface p1 | sed -n "s/^.*flap_count=\(.*\), forwarding.*$/\1/p"])

# turn bfd on p1 on again, should increment the bfd:flap_count on p0.
# p1 should still have flap_count = "1", since it is reset.
AT_CHECK([ovs-vsctl set interface p1 bfd:enable=true])
ovs-appctl time/warp 5000 100
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["3"])
BFD_VSCTL_LIST_IFACE([p1], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["1"])


# Part-2 now turn on the forwarding_override.
AT_CHECK([ovs-appctl bfd/set-forwarding p0 true], [0], [dnl
OK
])

# turn bfd on p1 off, should not increment the bfd:flap_count on p0, since forwarding_override is on.
AT_CHECK([ovs-vsctl set interface p1 bfd:enable=false])
ovs-appctl time/warp 5000 100
BFD_CHECK([p0], [true], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["3"])
AT_CHECK([ovs-vsctl list interface p1 | sed -n "s/^.*flap_count=\(.*\), forwarding.*$/\1/p"])

# turn bfd on p1 on again, should not increment the bfd:flap_count on p0, since forwarding override is on.
# p1 should still have flap_count = "1", since it is reset.
AT_CHECK([ovs-vsctl set interface p1 bfd:enable=true])
ovs-appctl time/warp 5000 100
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["3"])
BFD_VSCTL_LIST_IFACE([p1], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["1"])

# turn the forwarding_override back to normal.
AT_CHECK([ovs-appctl bfd/set-forwarding p0 normal], [0], [dnl
OK
])

# turn bfd on p1 off and on, should increment the bfd:flap_count on p0.
AT_CHECK([ovs-vsctl set interface p1 bfd:enable=false])
ovs-appctl time/warp 5000 100
AT_CHECK([ovs-vsctl set interface p1 bfd:enable=true])
ovs-appctl time/warp 5000 100
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["5"])
BFD_VSCTL_LIST_IFACE([p1], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["1"])

# Part-3 now turn on forwarding_if_rx.
AT_CHECK([ovs-vsctl set Interface p0 bfd:forwarding_if_rx=true], [0])
ovs-appctl time/warp 1100 100
# disable the bfd on p1.
AT_CHECK([ovs-vsctl set Interface p1 bfd:enable=false], [0])

# advance clock by 4000ms, while receiving packets.
# the STATE should go DOWN, due to Control Detection Time Expired.
# but forwarding flag should be true.
for i in `seq 0 39`
do
    ovs-appctl time/warp 100
    AT_CHECK([ovs-ofctl packet-out br1 "in_port=3 packet=90e2ba01475000101856b2e80806000108000604000100101856b2e80202020300000000000002020202 actions=2"],
             [0], [stdout], [])
done
BFD_CHECK([p0], [true], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])
# flap_count should remain unchanged.
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["5"])

# stop the traffic for more than 100 * bfd->cfm_min_rx ms, the forwarding flag of p0 should turn false.
# and there should be the increment of flap_count.
ovs-appctl time/warp 12100 100
BFD_CHECK([p0], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["6"])

# advance clock by 4000ms, and resume the traffic.
for i in `seq 0 39`
do
    ovs-appctl time/warp 100
    AT_CHECK([ovs-ofctl packet-out br1 "in_port=3 packet=90e2ba01475000101856b2e80806000108000604000100101856b2e80202020300000000000002020202 actions=2"],
             [0], [stdout], [])
done
# forwarding should be false, since there is still no bfd control packet received.
BFD_CHECK([p0], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["6"])

# turn on the bfd on p1.
AT_CHECK([ovs-vsctl set interface p1 bfd:enable=true])
ovs-appctl time/warp 5000 100
# even though there is no data traffic, since p1 bfd is on again, should increment the flap_count.
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["7"])
BFD_VSCTL_LIST_IFACE([p1], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["1"])

OVS_VSWITCHD_STOP
AT_CLEANUP

AT_SETUP([bfd - check that BFD works together with RSTP])
# Create br0 with interfaces p1
#    and br1 with interfaces p2
# with p1 and p2 connected via unix domain socket
OVS_VSWITCHD_START(
  [set bridge br0 rstp_enable=true -- \
   add-br br1 -- \
   set bridge br1 other-config:hwaddr=aa:66:aa:66:00:00 -- \
   set bridge br1 datapath-type=dummy -- \
   set bridge br1 rstp_enable=true -- \
])

AT_CHECK([ovs-vsctl add-port br0 p1 -- \
   set interface p1 type=dummy options:pstream=punix:$OVS_RUNDIR/p0.sock bfd:enable=true -- \
])

AT_CHECK([ovs-vsctl add-port br1 p2 -- \
   set interface p2 type=dummy options:stream=unix:$OVS_RUNDIR/p0.sock bfd:enable=true -- \
])

ovs-appctl time/stop
ovs-appctl time/warp 4100 100

# Forwarding should be true
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p2], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])

# Disable bfd on p2, forwarding on p1 should go to false
AT_CHECK([ovs-vsctl set interface p2 bfd:enable=false])

ovs-appctl time/warp 5000 100
BFD_CHECK([p1], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])

OVS_VSWITCHD_STOP
AT_CLEANUP

# test bfd: liveness propagation - OF1.3.
AT_SETUP([bfd - liveness propagation - OF1.3])
OVS_VSWITCHD_START
AT_CHECK([ovs-ofctl -O OpenFlow13 -P standard monitor br0 --detach --no-chdir --pidfile])
check_liveness () {
    printf '\n\n--- check_liveness %d ---\n\n\n' $1
    shift

        echo >>expout "OFPT_PORT_STATUS (OF1.3): MOD: 1(p0): addr:
     config:     0
     state:      $1
     speed: 0 Mbps now, 0 Mbps max"

        AT_CHECK(
      [[sed '
s/ (xid=0x[0-9a-fA-F]*)//
s/ *duration.*//
s/addr:[0-9a-fA-F:]*/addr:/' < monitor.log|grep -A3 "MOD: 1(p0)"|grep -ve --]],
      [0], [expout])
}
: > expout
ovs-appctl -t ovs-ofctl ofctl/barrier
ovs-appctl -t ovs-ofctl ofctl/set-output-file monitor.log
# Set miss_send_len to 128, enabling port_status messages to our service connection.
ovs-appctl -t ovs-ofctl ofctl/send 0409000c0123456700000080
#Create 2 bridges connected by patch ports and enable bfd
AT_CHECK([ovs-vsctl add-br br1 -- \
          set bridge br1 datapath-type=dummy \
          other-config:hwaddr=aa:55:aa:56:00:00 -- \
          add-port br1 p1 -- set Interface p1 type=patch \
          options:peer=p0 ofport_request=2 -- \
          add-port br0 p0 -- set Interface p0 type=patch \
          options:peer=p1 ofport_request=1 -- \
          set Interface p0 bfd:enable=true bfd:min_tx=100 bfd:min_rx=100 -- \
          set Interface p1 bfd:enable=true bfd:min_tx=100 bfd:min_rx=100])

ovs-appctl time/stop
# Disable the stats update to prevent the race between ovsdb updating
# stats and ovs-vsctl cmd closing the jsonrpc session.
AT_CHECK([ovs-vsctl set Open_vSwitch . other_config:stats-update-interval=50000000])

# wait for a while to stablize bfd.
ovs-appctl time/warp 10100 100
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK_TX([p0], [100ms], [100ms], [100ms])
BFD_CHECK_RX([p0], [100ms], [100ms], [100ms])
# both p0 and p1 should have flap_count = "1". since down->up.
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["1"])
BFD_VSCTL_LIST_IFACE([p1], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["1"])
check_liveness 1 LIVE

# turn bfd on p1 off, should increment the bfd:flap_count on p0.
AT_CHECK([ovs-vsctl set interface p1 bfd:enable=false])
ovs-appctl time/warp 5000 100
BFD_CHECK([p0], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["2"])
AT_CHECK([ovs-vsctl list interface p1 | sed -n "s/^.*flap_count=\(.*\), forwarding.*$/\1/p"])
check_liveness 2 0

# turn bfd on p1 on again, should increment the bfd:flap_count on p0.
# p1 should still have flap_count = "1", since it is reset.
AT_CHECK([ovs-vsctl set interface p1 bfd:enable=true])
ovs-appctl time/warp 5000 100
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["3"])
BFD_VSCTL_LIST_IFACE([p1], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["1"])
check_liveness 3 LIVE

OVS_VSWITCHD_STOP
AT_CLEANUP

# test bfd: liveness propagation - OF1.4.
AT_SETUP([bfd - liveness propagation - OF1.4])
OVS_VSWITCHD_START
AT_CHECK([ovs-ofctl -O OpenFlow14 -P standard monitor br0 --detach --no-chdir --pidfile])
check_liveness () {
    printf '\n\n--- check_liveness %d ---\n\n\n' $1
    shift

        echo >>expout "OFPT_PORT_STATUS (OF1.4): MOD: 1(p0): addr:
     config:     0
     state:      $1
     speed: 0 Mbps now, 0 Mbps max"

        AT_CHECK(
      [[sed '
s/ (xid=0x[0-9a-fA-F]*)//
s/ *duration.*//
s/addr:[0-9a-fA-F:]*/addr:/' < monitor.log|grep -A3 "MOD: 1(p0)"|grep -ve --]],
      [0], [expout])
}
: > expout
ovs-appctl -t ovs-ofctl ofctl/barrier
ovs-appctl -t ovs-ofctl ofctl/set-output-file monitor.log
# Set miss_send_len to 128, enabling port_status messages to our service connection.
ovs-appctl -t ovs-ofctl ofctl/send 0509000c0123456700000080
#Create 2 bridges connected by patch ports and enable bfd
AT_CHECK([ovs-vsctl add-br br1 -- \
          set bridge br1 datapath-type=dummy \
          other-config:hwaddr=aa:55:aa:56:00:00 -- \
          add-port br1 p1 -- set Interface p1 type=patch \
          options:peer=p0 ofport_request=2 -- \
          add-port br0 p0 -- set Interface p0 type=patch \
          options:peer=p1 ofport_request=1 -- \
          set Interface p0 bfd:enable=true bfd:min_tx=100 bfd:min_rx=100 -- \
          set Interface p1 bfd:enable=true bfd:min_tx=100 bfd:min_rx=100])

ovs-appctl time/stop
# Disable the stats update to prevent the race between ovsdb updating
# stats and ovs-vsctl cmd closing the jsonrpc session.
AT_CHECK([ovs-vsctl set Open_vSwitch . other_config:stats-update-interval=50000000])

# wait for a while to stablize bfd.
ovs-appctl time/warp 10100 100
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK_TX([p0], [100ms], [100ms], [100ms])
BFD_CHECK_RX([p0], [100ms], [100ms], [100ms])
# both p0 and p1 should have flap_count = "1". since down->up.
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["1"])
BFD_VSCTL_LIST_IFACE([p1], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["1"])
check_liveness 1 LIVE

# turn bfd on p1 off, should increment the bfd:flap_count on p0.
AT_CHECK([ovs-vsctl set interface p1 bfd:enable=false])
ovs-appctl time/warp 5000 100
BFD_CHECK([p0], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["2"])
AT_CHECK([ovs-vsctl list interface p1 | sed -n "s/^.*flap_count=\(.*\), forwarding.*$/\1/p"])
check_liveness 2 0

# turn bfd on p1 on again, should increment the bfd:flap_count on p0.
# p1 should still have flap_count = "1", since it is reset.
AT_CHECK([ovs-vsctl set interface p1 bfd:enable=true])
ovs-appctl time/warp 5000 100
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["3"])
BFD_VSCTL_LIST_IFACE([p1], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["1"])
check_liveness 3 LIVE

OVS_VSWITCHD_STOP
AT_CLEANUP

# test bfd: liveness propagation - OF1.5.
AT_SETUP([bfd - liveness propagation - OF1.5])
OVS_VSWITCHD_START
AT_CHECK([ovs-ofctl -O OpenFlow15 -P standard monitor br0 --detach --no-chdir --pidfile])
check_liveness () {
    printf '\n\n--- check_liveness %d ---\n\n\n' $1
    shift

        echo >>expout "OFPT_PORT_STATUS (OF1.5): MOD: 1(p0): addr:
     config:     0
     state:      $1
     speed: 0 Mbps now, 0 Mbps max"

        AT_CHECK(
      [[sed '
s/ (xid=0x[0-9a-fA-F]*)//
s/ *duration.*//
s/addr:[0-9a-fA-F:]*/addr:/' < monitor.log|grep -A3 "MOD: 1(p0)"|grep -ve --]],
      [0], [expout])
}
: > expout
ovs-appctl -t ovs-ofctl ofctl/barrier
ovs-appctl -t ovs-ofctl ofctl/set-output-file monitor.log
# Set miss_send_len to 128, enabling port_status messages to our service connection.
ovs-appctl -t ovs-ofctl ofctl/send 0609000c0123456700000080
#Create 2 bridges connected by patch ports and enable bfd
AT_CHECK([ovs-vsctl add-br br1 -- \
          set bridge br1 datapath-type=dummy \
          other-config:hwaddr=aa:55:aa:56:00:00 -- \
          add-port br1 p1 -- set Interface p1 type=patch \
          options:peer=p0 ofport_request=2 -- \
          add-port br0 p0 -- set Interface p0 type=patch \
          options:peer=p1 ofport_request=1 -- \
          set Interface p0 bfd:enable=true bfd:min_tx=100 bfd:min_rx=100 -- \
          set Interface p1 bfd:enable=true bfd:min_tx=100 bfd:min_rx=100])

ovs-appctl time/stop
# Disable the stats update to prevent the race between ovsdb updating
# stats and ovs-vsctl cmd closing the jsonrpc session.
AT_CHECK([ovs-vsctl set Open_vSwitch . other_config:stats-update-interval=50000000])

# wait for a while to stablize bfd.
ovs-appctl time/warp 10100 100
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK_TX([p0], [100ms], [100ms], [100ms])
BFD_CHECK_RX([p0], [100ms], [100ms], [100ms])
# both p0 and p1 should have flap_count = "1". since down->up.
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["1"])
BFD_VSCTL_LIST_IFACE([p1], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["1"])
check_liveness 1 LIVE

# turn bfd on p1 off, should increment the bfd:flap_count on p0.
AT_CHECK([ovs-vsctl set interface p1 bfd:enable=false])
ovs-appctl time/warp 5000 100
BFD_CHECK([p0], [false], [false], [none], [down], [Control Detection Time Expired], [none], [down], [No Diagnostic])
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["2"])
AT_CHECK([ovs-vsctl list interface p1 | sed -n "s/^.*flap_count=\(.*\), forwarding.*$/\1/p"])
check_liveness 2 0

# turn bfd on p1 on again, should increment the bfd:flap_count on p0.
# p1 should still have flap_count = "1", since it is reset.
AT_CHECK([ovs-vsctl set interface p1 bfd:enable=true])
ovs-appctl time/warp 5000 100
BFD_VSCTL_LIST_IFACE([p0], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["3"])
BFD_VSCTL_LIST_IFACE([p1], ["s/^.*flap_count=\(.*\), forwarding.*$/\1/p"], ["1"])
check_liveness 3 LIVE

OVS_VSWITCHD_STOP
AT_CLEANUP

AT_SETUP([bfd - Edit the Detect Mult values])
#Create 2 bridges connected by patch ports and enable BFD
OVS_VSWITCHD_START()
ovs-appctl time/stop
AT_CHECK([ ovs-vsctl -- add-br br1 -- \
           set bridge br1 datapath-type=dummy ])
AT_CHECK([ ovs-vsctl -- add-port br1 p1 -- set Interface p1 type=patch\
           options:peer=p0 ])
AT_CHECK([ ovs-vsctl -- add-port br0 p0 -- set Interface p0 type=patch\
           options:peer=p1 ])
AT_CHECK([ ovs-vsctl -- set interface p0 bfd:enable=true ])
AT_CHECK([ ovs-vsctl -- set interface p1 bfd:enable=true ])
ovs-appctl time/warp 3100 100
#Verify that BFD has been enabled on both interfaces.
BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic])
#Verify that default mult values are 3.
BFD_CHECK_MULT([p0], [3], [3])
BFD_CHECK_MULT([p1], [3], [3])
#Set the mult values to valid range border mult(p0)=1 mult(p1)=255.
AT_CHECK([ovs-vsctl set interface p0 bfd:mult=1])
AT_CHECK([ovs-vsctl set interface p1 bfd:mult=255])
ovs-appctl time/warp 3100 100
BFD_CHECK_MULT([p0], [1], [255])
BFD_CHECK_MULT([p1], [255], [1])

#Set the mult values out valid range border mult(p0)=0 mult(p1)=256.
AT_CHECK([ovs-vsctl set interface p0 bfd:mult=0])
AT_CHECK([ovs-vsctl set interface p1 bfd:mult=256])
ovs-appctl time/warp 3100 100
BFD_CHECK_MULT([p0], [3], [3])
BFD_CHECK_MULT([p1], [3], [3])

#Set valid non default mult values mult(p0)=8 mult(p1)=125.
AT_CHECK([ovs-vsctl set interface p0 bfd:mult=8])
AT_CHECK([ovs-vsctl set interface p1 bfd:mult=125])
ovs-appctl time/warp 3100 100
BFD_CHECK_MULT([p0], [8], [125])
BFD_CHECK_MULT([p1], [125], [8])

#Clear mult values. Detect mult values shall be default 3 again.
AT_CHECK([ovs-vsctl remove interface p0 bfd mult])
AT_CHECK([ovs-vsctl remove interface p1 bfd mult])
ovs-appctl time/warp 3100 100
BFD_CHECK_MULT([p0], [3], [3])
BFD_CHECK_MULT([p1], [3], [3])

OVS_VSWITCHD_STOP
AT_CLEANUP

AT_SETUP([bfd - overlay])
OVS_VSWITCHD_START([add-port br0 p1 -- set Interface p1 type=gre \
                    options:remote_ip=2.2.2.2 ofport_request=1 -- \
                    set interface p1 bfd:enable=true bfd:bfd_src_ip=2.2.2.1 -- \
                    set bridge br0 fail-mode=standalone])

# Userspace slow path handles normal BFD packets.
AT_CHECK([ovs-appctl ofproto/trace --l7-len 0 ovs-dummy 'tunnel(tun_id=0x0,src=2.2.2.2,dst=2.2.2.1,tos=0x0,ttl=64,tp_src=0,tp_dst=0,flags()),in_port(1),skb_mark(0/0),eth(src=00:11:22:33:44:55,dst=00:23:20:00:00:01),eth_type(0x0800),ipv4(src=2.2.2.2/0.0.0.0,dst=2.2.2.1/0.0.0.0,proto=17/0xff,tos=0/0,ttl=255/0,frag=no),udp(src=49152/0,dst=3784/0xffff)' -generate], [0], [stdout])
# check that the packet should be handled as BFD packet.
AT_CHECK([tail -2 stdout], [0], [dnl
This flow is handled by the userspace slow path because it:
  - Consists of BFD packets.
], [])

# Userspace slow path won't handle overlay BFD packets. Instead, other OVS flows, if configured, will handle them.
AT_CHECK([ovs-appctl ofproto/trace --l7-len 0 ovs-dummy 'tunnel(tun_id=0x0,src=2.2.2.2,dst=2.2.2.1,tos=0x0,ttl=64,tp_src=0,tp_dst=0,flags()),in_port(1),skb_mark(0/0),eth(src=00:11:22:33:44:66,dst=00:23:20:00:00:77),eth_type(0x0800),ipv4(src=192.168.2.2/0.0.0.0,dst=192.168.2.1/0.0.0.0,proto=17/0xff,tos=0/0,ttl=255/0,frag=no),udp(src=49152/0,dst=3784/0xffff)' -generate], [0], [stdout])
AT_CHECK([tail -10 stdout], [0], [dnl
bridge("br0")
-------------
 0. priority 0
    NORMAL
     -> learned that 00:11:22:33:44:66 is on port p1 in VLAN 0
     -> no learned MAC for destination, flooding

Final flow: unchanged
Megaflow: recirc_id=0,eth,udp,tun_id=0,tun_src=2.2.2.2,tun_dst=2.2.2.1,tun_tos=0,tun_flags=-df-csum+key,in_port=1,dl_src=00:11:22:33:44:66,dl_dst=00:23:20:00:00:77,nw_frag=no,tp_dst=3784
Datapath actions: 100
], [])

OVS_VSWITCHD_STOP
AT_CLEANUP