summaryrefslogtreecommitdiff
path: root/lib/bfd.c
blob: 9698576d071bc182efeb17cec468e3adb1163340 (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
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
/* Copyright (c) 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. */

#include <config.h>
#include "bfd.h"

#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <sys/socket.h>

#include "byte-order.h"
#include "connectivity.h"
#include "csum.h"
#include "dp-packet.h"
#include "dpif.h"
#include "openvswitch/dynamic-string.h"
#include "flow.h"
#include "hash.h"
#include "openvswitch/hmap.h"
#include "openvswitch/list.h"
#include "netdev.h"
#include "odp-util.h"
#include "openvswitch/ofpbuf.h"
#include "ovs-thread.h"
#include "openvswitch/types.h"
#include "packets.h"
#include "openvswitch/poll-loop.h"
#include "random.h"
#include "seq.h"
#include "smap.h"
#include "timeval.h"
#include "unaligned.h"
#include "unixctl.h"
#include "util.h"
#include "openvswitch/vlog.h"

VLOG_DEFINE_THIS_MODULE(bfd);

/* XXX Finish BFD.
 *
 * The goal of this module is to replace CFM with something both more flexible
 * and standards compliant.  In service of this goal, the following needs to be
 * done.
 *
 * - Compliance
 *   * Implement Demand mode.
 *   * Go through the RFC line by line and verify we comply.
 *   * Test against a hardware implementation.  Preferably a popular one.
 *   * Delete BFD packets with nw_ttl != 255 in the datapath to prevent DOS
 *     attacks.
 *
 * - Unit tests.
 *
 * - Set TOS/PCP on the outer tunnel header when encapped.
 *
 * - Sending BFD messages should be in its own thread/process.
 *
 * - Scale testing.  How does it operate when there are large number of bfd
 *   sessions?  Do we ever have random flaps?  What's the CPU utilization?
 *
 * - Rely on data traffic for liveness by using BFD demand mode.
 *   If we're receiving traffic on a port, we can safely assume it's up (modulo
 *   unidrectional failures).  BFD has a demand mode in which it can stay quiet
 *   unless it feels the need to check the status of the port.  Using this, we
 *   can implement a strategy in which BFD only sends control messages on dark
 *   interfaces.
 *
 * - Depending on how one interprets the spec, it appears that a BFD session
 *   can never change bfd.LocalDiag to "No Diagnostic".  We should verify that
 *   this is what hardware implementations actually do.  Seems like "No
 *   Diagnostic" should be set once a BFD session state goes UP. */

#define BFD_VERSION 1

enum flags {
    FLAG_MULTIPOINT = 1 << 0,
    FLAG_DEMAND = 1 << 1,
    FLAG_AUTH = 1 << 2,
    FLAG_CTL = 1 << 3,
    FLAG_FINAL = 1 << 4,
    FLAG_POLL = 1 << 5
};

enum state {
    STATE_ADMIN_DOWN = 0 << 6,
    STATE_DOWN = 1 << 6,
    STATE_INIT = 2 << 6,
    STATE_UP = 3 << 6
};

enum diag {
    DIAG_NONE = 0,                /* No Diagnostic. */
    DIAG_EXPIRED = 1,             /* Control Detection Time Expired. */
    DIAG_ECHO_FAILED = 2,         /* Echo Function Failed. */
    DIAG_RMT_DOWN = 3,            /* Neighbor Signaled Session Down. */
    DIAG_FWD_RESET = 4,           /* Forwarding Plane Reset. */
    DIAG_PATH_DOWN = 5,           /* Path Down. */
    DIAG_CPATH_DOWN = 6,          /* Concatenated Path Down. */
    DIAG_ADMIN_DOWN = 7,          /* Administratively Down. */
    DIAG_RCPATH_DOWN = 8          /* Reverse Concatenated Path Down. */
};

/* RFC 5880 Section 4.1
 *  0                   1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |Vers |  Diag   |Sta|P|F|C|A|D|M|  Detect Mult  |    Length     |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                       My Discriminator                        |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                      Your Discriminator                       |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                    Desired Min TX Interval                    |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                   Required Min RX Interval                    |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                 Required Min Echo RX Interval                 |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */
struct msg {
    uint8_t vers_diag;              /* Version and diagnostic. */
    uint8_t flags;                  /* 2bit State field followed by flags. */
    uint8_t mult;                   /* Fault detection multiplier. */
    uint8_t length;                 /* Length of this BFD message. */
    ovs_16aligned_be32 my_disc;     /* My discriminator. */
    ovs_16aligned_be32 your_disc;   /* Your discriminator. */
    ovs_16aligned_be32 min_tx;      /* Desired minimum tx interval. */
    ovs_16aligned_be32 min_rx;      /* Required minimum rx interval. */
    ovs_16aligned_be32 min_rx_echo; /* Required minimum echo rx interval. */
};

BUILD_ASSERT_DECL(BFD_PACKET_LEN == sizeof(struct msg));

#define DIAG_MASK 0x1f
#define VERS_SHIFT 5
#define STATE_MASK 0xC0
#define FLAGS_MASK 0x3f
#define DEFAULT_MULT 3

#define BFD_DEFAULT_SRC_IP 0xA9FE0101 /* 169.254.1.1 */
#define BFD_DEFAULT_DST_IP 0xA9FE0100 /* 169.254.1.0 */

struct bfd {
    struct hmap_node node;        /* In 'all_bfds'. */
    uint32_t disc;                /* bfd.LocalDiscr. Key in 'all_bfds' hmap. */

    char *name;                   /* Name used for logging. */

    bool cpath_down;              /* Concatenated Path Down. */
    uint8_t mult;                 /* bfd.DetectMult. */
    uint8_t rmt_mult;             /* Remote bfd.DetectMult. */

    struct netdev *netdev;
    uint64_t rx_packets;          /* Packets received by 'netdev'. */

    enum state state;             /* bfd.SessionState. */
    enum state rmt_state;         /* bfd.RemoteSessionState. */

    enum diag diag;               /* bfd.LocalDiag. */
    enum diag rmt_diag;           /* Remote diagnostic. */

    enum flags flags;             /* Flags sent on messages. */
    enum flags rmt_flags;         /* Flags last received. */

    bool oam;                     /* Set tunnel OAM flag if applicable. */

    uint32_t rmt_disc;            /* bfd.RemoteDiscr. */

    struct eth_addr local_eth_src; /* Local eth src address. */
    struct eth_addr local_eth_dst; /* Local eth dst address. */

    struct eth_addr rmt_eth_dst;   /* Remote eth dst address. */

    ovs_be32 ip_src;              /* IPv4 source address. */
    ovs_be32 ip_dst;              /* IPv4 destination address. */

    uint16_t udp_src;             /* UDP source port. */

    /* All timers in milliseconds. */
    long long int rmt_min_rx;     /* bfd.RemoteMinRxInterval. */
    long long int rmt_min_tx;     /* Remote minimum TX interval. */

    long long int cfg_min_tx;     /* Configured minimum TX rate. */
    long long int cfg_min_rx;     /* Configured required minimum RX rate. */
    long long int poll_min_tx;    /* Min TX negotating in a poll sequence. */
    long long int poll_min_rx;    /* Min RX negotating in a poll sequence. */
    long long int min_tx;         /* bfd.DesiredMinTxInterval. */
    long long int min_rx;         /* bfd.RequiredMinRxInterval. */

    long long int last_tx;        /* Last TX time. */
    long long int next_tx;        /* Next TX time. */
    long long int detect_time;    /* RFC 5880 6.8.4 Detection time. */

    bool last_forwarding;         /* Last calculation of forwarding flag. */
    int forwarding_override;      /* Manual override of 'forwarding' status. */

    atomic_bool check_tnl_key;    /* Verify tunnel key of inbound packets? */
    struct ovs_refcount ref_cnt;

    /* When forward_if_rx is true, bfd_forwarding() will return
     * true as long as there are incoming packets received.
     * Note, forwarding_override still has higher priority. */
    bool forwarding_if_rx;
    long long int forwarding_if_rx_detect_time;

    /* When 'bfd->forwarding_if_rx' is set, at least one bfd control packet
     * is required to be received every 100 * bfd->cfg_min_rx.  If bfd
     * control packet is not received within this interval, even if data
     * packets are received, the bfd->forwarding will still be false. */
    long long int demand_rx_bfd_time;

    /* BFD decay related variables. */
    bool in_decay;                /* True when bfd is in decay. */
    int decay_min_rx;             /* min_rx is set to decay_min_rx when */
                                  /* in decay. */
    int decay_rx_ctl;             /* Count bfd packets received within decay */
                                  /* detect interval. */
    uint64_t decay_rx_packets;    /* Packets received by 'netdev'. */
    long long int decay_detect_time; /* Decay detection time. */

    uint64_t flap_count;          /* Counts bfd forwarding flaps. */

    /* True when the variables returned by bfd_get_status() are changed
     * since last check. */
    bool status_changed;
};

static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
static struct hmap all_bfds__ = HMAP_INITIALIZER(&all_bfds__);
static struct hmap *const all_bfds OVS_GUARDED_BY(mutex) = &all_bfds__;

static void bfd_lookup_ip(const char *host_name, ovs_be32 def, ovs_be32 *ip)
    OVS_REQUIRES(mutex);
static bool bfd_forwarding__(struct bfd *) OVS_REQUIRES(mutex);
static bool bfd_in_poll(const struct bfd *) OVS_REQUIRES(mutex);
static void bfd_poll(struct bfd *bfd) OVS_REQUIRES(mutex);
static const char *bfd_diag_str(enum diag) OVS_REQUIRES(mutex);
static const char *bfd_state_str(enum state) OVS_REQUIRES(mutex);
static long long int bfd_min_tx(const struct bfd *) OVS_REQUIRES(mutex);
static long long int bfd_tx_interval(const struct bfd *)
    OVS_REQUIRES(mutex);
static long long int bfd_rx_interval(const struct bfd *)
    OVS_REQUIRES(mutex);
static void bfd_set_next_tx(struct bfd *) OVS_REQUIRES(mutex);
static void bfd_set_state(struct bfd *, enum state, enum diag)
    OVS_REQUIRES(mutex);
static uint32_t generate_discriminator(void) OVS_REQUIRES(mutex);
static void bfd_put_details(struct ds *, const struct bfd *)
    OVS_REQUIRES(mutex);
static uint64_t bfd_rx_packets(const struct bfd *) OVS_REQUIRES(mutex);
static void bfd_try_decay(struct bfd *) OVS_REQUIRES(mutex);
static void bfd_decay_update(struct bfd *) OVS_REQUIRES(mutex);
static void bfd_status_changed(struct bfd *) OVS_REQUIRES(mutex);

static void bfd_forwarding_if_rx_update(struct bfd *) OVS_REQUIRES(mutex);
static void bfd_unixctl_show(struct unixctl_conn *, int argc,
                             const char *argv[], void *aux OVS_UNUSED);
static void bfd_unixctl_set_forwarding_override(struct unixctl_conn *,
                                                int argc, const char *argv[],
                                                void *aux OVS_UNUSED);
static void log_msg(enum vlog_level, const struct msg *, const char *message,
                    const struct bfd *) OVS_REQUIRES(mutex);

static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(20, 20);

/* Returns true if the interface on which 'bfd' is running may be used to
 * forward traffic according to the BFD session state. */
bool
bfd_forwarding(struct bfd *bfd) OVS_EXCLUDED(mutex)
{
    bool ret;

    ovs_mutex_lock(&mutex);
    ret = bfd_forwarding__(bfd);
    ovs_mutex_unlock(&mutex);
    return ret;
}

/* When forwarding_if_rx is enabled, if there are packets received,
 * updates forwarding_if_rx_detect_time. */
void
bfd_account_rx(struct bfd *bfd, const struct dpif_flow_stats *stats)
{
    if (stats->n_packets && bfd->forwarding_if_rx) {
        ovs_mutex_lock(&mutex);
        bfd_forwarding__(bfd);
        bfd_forwarding_if_rx_update(bfd);
        bfd_forwarding__(bfd);
        ovs_mutex_unlock(&mutex);
    }
}

/* Returns and resets the 'bfd->status_changed'. */
bool
bfd_check_status_change(struct bfd *bfd) OVS_EXCLUDED(mutex)
{
    bool ret;

    ovs_mutex_lock(&mutex);
    ret = bfd->status_changed;
    bfd->status_changed = false;
    ovs_mutex_unlock(&mutex);

    return ret;
}

/* Returns a 'smap' of key value pairs representing the status of 'bfd'
 * intended for the OVS database. */
void
bfd_get_status(const struct bfd *bfd, struct smap *smap)
    OVS_EXCLUDED(mutex)
{
    ovs_mutex_lock(&mutex);
    smap_add(smap, "forwarding",
             bfd_forwarding__(CONST_CAST(struct bfd *, bfd))
             ? "true" : "false");
    smap_add(smap, "state", bfd_state_str(bfd->state));
    smap_add(smap, "diagnostic", bfd_diag_str(bfd->diag));
    smap_add_format(smap, "flap_count", "%"PRIu64, bfd->flap_count);
    smap_add(smap, "remote_state", bfd_state_str(bfd->rmt_state));
    smap_add(smap, "remote_diagnostic", bfd_diag_str(bfd->rmt_diag));
    ovs_mutex_unlock(&mutex);
}

void
bfd_init(void)
{
    unixctl_command_register("bfd/show", "[interface]", 0, 1,
                             bfd_unixctl_show, NULL);
    unixctl_command_register("bfd/set-forwarding",
                             "[interface] normal|false|true", 1, 2,
                             bfd_unixctl_set_forwarding_override, NULL);
}

/* Initializes, destroys, or reconfigures the BFD session 'bfd' (named 'name'),
 * according to the database configuration contained in 'cfg'.  Takes ownership
 * of 'bfd', which may be NULL.  Returns a BFD object which may be used as a
 * handle for the session, or NULL if BFD is not enabled according to 'cfg'.
 * Also returns NULL if cfg is NULL. */
struct bfd *
bfd_configure(struct bfd *bfd, const char *name, const struct smap *cfg,
              struct netdev *netdev) OVS_EXCLUDED(mutex)
{
    static atomic_count udp_src = ATOMIC_COUNT_INIT(0);

    int decay_min_rx;
    long long int min_tx, min_rx;
    bool need_poll = false;
    bool cfg_min_rx_changed = false;
    bool cpath_down, forwarding_if_rx;

    if (!cfg || !smap_get_bool(cfg, "enable", false)) {
        bfd_unref(bfd);
        return NULL;
    }

    ovs_mutex_lock(&mutex);
    if (!bfd) {
        bfd = xzalloc(sizeof *bfd);
        bfd->name = xstrdup(name);
        bfd->forwarding_override = -1;
        bfd->disc = generate_discriminator();
        hmap_insert(all_bfds, &bfd->node, bfd->disc);

        bfd->diag = DIAG_NONE;
        bfd->min_tx = 1000;
        bfd->rmt_mult = 0;
        bfd->mult = DEFAULT_MULT;
        ovs_refcount_init(&bfd->ref_cnt);
        bfd->netdev = netdev_ref(netdev);
        bfd->rx_packets = bfd_rx_packets(bfd);
        bfd->in_decay = false;
        bfd->flap_count = 0;

        /* RFC 5881 section 4
         * The source port MUST be in the range 49152 through 65535.  The same
         * UDP source port number MUST be used for all BFD Control packets
         * associated with a particular session.  The source port number SHOULD
         * be unique among all BFD sessions on the system. */
        bfd->udp_src = (atomic_count_inc(&udp_src) % 16384) + 49152;

        bfd_set_state(bfd, STATE_DOWN, DIAG_NONE);

        bfd_status_changed(bfd);
    }

    int old_mult = bfd->mult;
    int new_mult = smap_get_int(cfg, "mult", DEFAULT_MULT);
    if (new_mult < 1 || new_mult > 255) {
        new_mult = DEFAULT_MULT;
    }
    bfd->mult = new_mult;

    bfd->oam = smap_get_bool(cfg, "oam", false);

    atomic_store_relaxed(&bfd->check_tnl_key,
                         smap_get_bool(cfg, "check_tnl_key", false));
    min_tx = smap_get_int(cfg, "min_tx", 100);
    min_tx = MAX(min_tx, 1);
    if (bfd->cfg_min_tx != min_tx) {
        bfd->cfg_min_tx = min_tx;
        if (bfd->state != STATE_UP
            || (!bfd_in_poll(bfd) && bfd->cfg_min_tx < bfd->min_tx)) {
            bfd->min_tx = bfd->cfg_min_tx;
        }
        need_poll = true;
    }

    min_rx = smap_get_int(cfg, "min_rx", 1000);
    min_rx = MAX(min_rx, 1);
    if (bfd->cfg_min_rx != min_rx) {
        bfd->cfg_min_rx = min_rx;
        if (bfd->state != STATE_UP
            || (!bfd_in_poll(bfd) && bfd->cfg_min_rx > bfd->min_rx)) {
            bfd->min_rx = bfd->cfg_min_rx;
        }
        cfg_min_rx_changed = true;
        need_poll = true;
    }

    decay_min_rx = smap_get_int(cfg, "decay_min_rx", 0);
    if (bfd->decay_min_rx != decay_min_rx || cfg_min_rx_changed) {
        if (decay_min_rx > 0 && decay_min_rx < bfd->cfg_min_rx) {
            VLOG_WARN("%s: decay_min_rx cannot be less than %lld ms",
                      bfd->name, bfd->cfg_min_rx);
            bfd->decay_min_rx = 0;
        } else {
            bfd->decay_min_rx = decay_min_rx;
        }
        /* Resets decay. */
        bfd->in_decay = false;
        bfd_decay_update(bfd);
        need_poll = true;
    }

    cpath_down = smap_get_bool(cfg, "cpath_down", false);
    if (bfd->cpath_down != cpath_down) {
        bfd->cpath_down = cpath_down;
        bfd_set_state(bfd, bfd->state, DIAG_NONE);
        need_poll = true;
    }

    eth_addr_from_string(smap_get_def(cfg, "bfd_local_src_mac", ""),
                         &bfd->local_eth_src);
    eth_addr_from_string(smap_get_def(cfg, "bfd_local_dst_mac", ""),
                         &bfd->local_eth_dst);
    eth_addr_from_string(smap_get_def(cfg, "bfd_remote_dst_mac", ""),
                         &bfd->rmt_eth_dst);

    bfd_lookup_ip(smap_get_def(cfg, "bfd_src_ip", ""),
                  htonl(BFD_DEFAULT_SRC_IP), &bfd->ip_src);
    bfd_lookup_ip(smap_get_def(cfg, "bfd_dst_ip", ""),
                  htonl(BFD_DEFAULT_DST_IP), &bfd->ip_dst);

    forwarding_if_rx = smap_get_bool(cfg, "forwarding_if_rx", false);
    if (bfd->forwarding_if_rx != forwarding_if_rx) {
        bfd->forwarding_if_rx = forwarding_if_rx;
        if (bfd->state == STATE_UP && bfd->forwarding_if_rx) {
            bfd_forwarding_if_rx_update(bfd);
        } else {
            bfd->forwarding_if_rx_detect_time = 0;
        }
    } else if (bfd->state == STATE_UP && bfd->forwarding_if_rx
               && old_mult != new_mult) {
        bfd_forwarding_if_rx_update(bfd);
    }

    if (need_poll) {
        bfd_poll(bfd);
    }
    ovs_mutex_unlock(&mutex);
    return bfd;
}

struct bfd *
bfd_ref(const struct bfd *bfd_)
{
    struct bfd *bfd = CONST_CAST(struct bfd *, bfd_);
    if (bfd) {
        ovs_refcount_ref(&bfd->ref_cnt);
    }
    return bfd;
}

void
bfd_unref(struct bfd *bfd) OVS_EXCLUDED(mutex)
{
    if (bfd && ovs_refcount_unref_relaxed(&bfd->ref_cnt) == 1) {
        ovs_mutex_lock(&mutex);
        bfd_status_changed(bfd);
        hmap_remove(all_bfds, &bfd->node);
        netdev_close(bfd->netdev);
        free(bfd->name);
        free(bfd);
        ovs_mutex_unlock(&mutex);
    }
}

long long int
bfd_wait(const struct bfd *bfd) OVS_EXCLUDED(mutex)
{
    long long int wake_time = bfd_wake_time(bfd);
    poll_timer_wait_until(wake_time);
    return wake_time;
}

/* Returns the next wake up time. */
long long int
bfd_wake_time(const struct bfd *bfd) OVS_EXCLUDED(mutex)
{
    long long int retval;

    if (!bfd) {
        return LLONG_MAX;
    }

    ovs_mutex_lock(&mutex);
    if (bfd->flags & FLAG_FINAL) {
        retval = 0;
    } else {
        retval = bfd->next_tx;
        if (bfd->state > STATE_DOWN) {
            retval = MIN(bfd->detect_time, retval);
        }
    }
    ovs_mutex_unlock(&mutex);
    return retval;
}

void
bfd_run(struct bfd *bfd) OVS_EXCLUDED(mutex)
{
    long long int now;
    bool old_in_decay;

    ovs_mutex_lock(&mutex);
    now = time_msec();
    old_in_decay = bfd->in_decay;

    if (bfd->state > STATE_DOWN && now >= bfd->detect_time) {
        bfd_set_state(bfd, STATE_DOWN, DIAG_EXPIRED);
    }
    bfd_forwarding__(bfd);

    /* Decay may only happen when state is STATE_UP, bfd->decay_min_rx is
     * configured, and decay_detect_time is reached. */
    if (bfd->state == STATE_UP && bfd->decay_min_rx > 0
        && now >= bfd->decay_detect_time) {
        bfd_try_decay(bfd);
    }

    if (bfd->min_tx != bfd->cfg_min_tx
        || (bfd->min_rx != bfd->cfg_min_rx && bfd->min_rx != bfd->decay_min_rx)
        || bfd->in_decay != old_in_decay) {
        bfd_poll(bfd);
    }
    ovs_mutex_unlock(&mutex);
}

bool
bfd_should_send_packet(const struct bfd *bfd) OVS_EXCLUDED(mutex)
{
    bool ret;
    ovs_mutex_lock(&mutex);
    ret = bfd->flags & FLAG_FINAL || time_msec() >= bfd->next_tx;
    ovs_mutex_unlock(&mutex);
    return ret;
}

void
bfd_put_packet(struct bfd *bfd, struct dp_packet *p,
               const struct eth_addr eth_src, bool *oam) OVS_EXCLUDED(mutex)
{
    long long int min_tx, min_rx;
    struct udp_header *udp;
    struct eth_header *eth;
    struct ip_header *ip;
    struct msg *msg;

    ovs_mutex_lock(&mutex);
    if (bfd->next_tx) {
        long long int delay = time_msec() - bfd->next_tx;
        long long int interval = bfd_tx_interval(bfd);
        if (delay > interval * 3 / 2) {
            VLOG_INFO("%s: long delay of %lldms (expected %lldms) sending BFD"
                      " control message", bfd->name, delay, interval);
        }
    }

    /* RFC 5880 Section 6.5
     * A BFD Control packet MUST NOT have both the Poll (P) and Final (F) bits
     * set. */
    ovs_assert(!(bfd->flags & FLAG_POLL) || !(bfd->flags & FLAG_FINAL));

    dp_packet_reserve(p, 2); /* Properly align after the ethernet header. */
    eth = dp_packet_put_uninit(p, sizeof *eth);
    eth->eth_src = eth_addr_is_zero(bfd->local_eth_src)
        ? eth_src : bfd->local_eth_src;
    eth->eth_dst = eth_addr_is_zero(bfd->local_eth_dst)
        ? eth_addr_bfd : bfd->local_eth_dst;
    eth->eth_type = htons(ETH_TYPE_IP);

    ip = dp_packet_put_zeros(p, sizeof *ip);
    ip->ip_ihl_ver = IP_IHL_VER(5, 4);
    ip->ip_tot_len = htons(sizeof *ip + sizeof *udp + sizeof *msg);
    ip->ip_ttl = MAXTTL;
    ip->ip_tos = IPTOS_PREC_INTERNETCONTROL;
    ip->ip_proto = IPPROTO_UDP;
    put_16aligned_be32(&ip->ip_src, bfd->ip_src);
    put_16aligned_be32(&ip->ip_dst, bfd->ip_dst);
    /* Checksum has already been zeroed by put_zeros call. */
    ip->ip_csum = csum(ip, sizeof *ip);

    udp = dp_packet_put_zeros(p, sizeof *udp);
    udp->udp_src = htons(bfd->udp_src);
    udp->udp_dst = htons(BFD_DEST_PORT);
    udp->udp_len = htons(sizeof *udp + sizeof *msg);

    msg = dp_packet_put_uninit(p, sizeof *msg);
    msg->vers_diag = (BFD_VERSION << 5) | bfd->diag;
    msg->flags = (bfd->state & STATE_MASK) | bfd->flags;

    msg->mult = bfd->mult;
    msg->length = BFD_PACKET_LEN;
    put_16aligned_be32(&msg->my_disc, htonl(bfd->disc));
    put_16aligned_be32(&msg->your_disc, htonl(bfd->rmt_disc));
    put_16aligned_be32(&msg->min_rx_echo, htonl(0));

    if (bfd_in_poll(bfd)) {
        min_tx = bfd->poll_min_tx;
        min_rx = bfd->poll_min_rx;
    } else {
        min_tx = bfd_min_tx(bfd);
        min_rx = bfd->min_rx;
    }

    put_16aligned_be32(&msg->min_tx, htonl(min_tx * 1000));
    put_16aligned_be32(&msg->min_rx, htonl(min_rx * 1000));

    bfd->flags &= ~FLAG_FINAL;
    *oam = bfd->oam;

    log_msg(VLL_DBG, msg, "Sending BFD Message", bfd);

    bfd->last_tx = time_msec();
    bfd_set_next_tx(bfd);
    ovs_mutex_unlock(&mutex);
}

bool
bfd_should_process_flow(const struct bfd *bfd_, const struct flow *flow,
                        struct flow_wildcards *wc)
{
    struct bfd *bfd = CONST_CAST(struct bfd *, bfd_);

    if (!eth_addr_is_zero(bfd->rmt_eth_dst)) {
        memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);

        if (!eth_addr_equals(bfd->rmt_eth_dst, flow->dl_dst)) {
            return false;
        }
    }

    if (flow->dl_type == htons(ETH_TYPE_IP)) {
        memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
        if (flow->nw_proto == IPPROTO_UDP
            && !(flow->nw_frag & FLOW_NW_FRAG_LATER)
            && tp_dst_equals(flow, BFD_DEST_PORT, wc)
            && (bfd->ip_src == htonl(BFD_DEFAULT_SRC_IP)
                || bfd->ip_src == flow->nw_dst)) {

            if (bfd->ip_src == flow->nw_dst) {
                memset(&wc->masks.nw_dst, 0xffffffff, sizeof wc->masks.nw_dst);
            }

            bool check_tnl_key;

            atomic_read_relaxed(&bfd->check_tnl_key, &check_tnl_key);
            if (check_tnl_key) {
                memset(&wc->masks.tunnel.tun_id, 0xff,
                       sizeof wc->masks.tunnel.tun_id);
                return flow->tunnel.tun_id == htonll(0);
            }
            return true;
        }
    }
    return false;
}

void
bfd_process_packet(struct bfd *bfd, const struct flow *flow,
                   const struct dp_packet *p) OVS_EXCLUDED(mutex)
{
    uint32_t rmt_min_rx, pkt_your_disc;
    enum state rmt_state;
    enum flags flags;
    uint8_t version;
    struct msg *msg;
    const uint8_t *l7 = dp_packet_get_udp_payload(p);

    if (!l7) {
        return; /* No UDP payload. */
    }

    /* This function is designed to follow section RFC 5880 6.8.6 closely. */

    ovs_mutex_lock(&mutex);
    /* Increments the decay rx counter. */
    bfd->decay_rx_ctl++;

    bfd_forwarding__(bfd);

    if (flow->nw_ttl != 255) {
        /* XXX Should drop in the kernel to prevent DOS. */
        goto out;
    }

    msg = dp_packet_at(p, l7 - (uint8_t *)dp_packet_data(p), BFD_PACKET_LEN);
    if (!msg) {
        VLOG_INFO_RL(&rl, "%s: Received too-short BFD control message (only "
                     "%"PRIdPTR" bytes long, at least %d required).",
                     bfd->name, (uint8_t *) dp_packet_tail(p) - l7,
                     BFD_PACKET_LEN);
        goto out;
    }

    /* RFC 5880 Section 6.8.6
     * If the Length field is greater than the payload of the encapsulating
     * protocol, the packet MUST be discarded.
     *
     * Note that we make this check implicitly.  Above we use dp_packet_at() to
     * ensure that there are at least BFD_PACKET_LEN bytes in the payload of
     * the encapsulating protocol.  Below we require msg->length to be exactly
     * BFD_PACKET_LEN bytes. */

    flags = msg->flags & FLAGS_MASK;
    rmt_state = msg->flags & STATE_MASK;
    version = msg->vers_diag >> VERS_SHIFT;

    log_msg(VLL_DBG, msg, "Received BFD control message", bfd);

    if (version != BFD_VERSION) {
        log_msg(VLL_WARN, msg, "Incorrect version", bfd);
        goto out;
    }

    /* Technically this should happen after the length check. We don't support
     * authentication however, so it's simpler to do the check first. */
    if (flags & FLAG_AUTH) {
        log_msg(VLL_WARN, msg, "Authenticated control message with"
                   " authentication disabled", bfd);
        goto out;
    }

    if (msg->length != BFD_PACKET_LEN) {
        log_msg(VLL_WARN, msg, "Unexpected length", bfd);
        if (msg->length < BFD_PACKET_LEN) {
            goto out;
        }
    }

    if (!msg->mult) {
        log_msg(VLL_WARN, msg, "Zero multiplier", bfd);
        goto out;
    }

    if (flags & FLAG_MULTIPOINT) {
        log_msg(VLL_WARN, msg, "Unsupported multipoint flag", bfd);
        goto out;
    }

    if (!get_16aligned_be32(&msg->my_disc)) {
        log_msg(VLL_WARN, msg, "NULL my_disc", bfd);
        goto out;
    }

    pkt_your_disc = ntohl(get_16aligned_be32(&msg->your_disc));
    if (pkt_your_disc) {
        /* Technically, we should use the your discriminator field to figure
         * out which 'struct bfd' this packet is destined towards.  That way a
         * bfd session could migrate from one interface to another
         * transparently.  This doesn't fit in with the OVS structure very
         * well, so in this respect, we are not compliant. */
       if (pkt_your_disc != bfd->disc) {
           log_msg(VLL_WARN, msg, "Incorrect your_disc", bfd);
           goto out;
       }
    } else if (rmt_state > STATE_DOWN) {
        log_msg(VLL_WARN, msg, "Null your_disc", bfd);
        goto out;
    }

    if (bfd->rmt_state != rmt_state) {
        bfd_status_changed(bfd);
    }

    bfd->rmt_disc = ntohl(get_16aligned_be32(&msg->my_disc));
    bfd->rmt_state = rmt_state;
    bfd->rmt_flags = flags;
    bfd->rmt_diag = msg->vers_diag & DIAG_MASK;

    if (flags & FLAG_FINAL && bfd_in_poll(bfd)) {
        bfd->min_tx = bfd->poll_min_tx;
        bfd->min_rx = bfd->poll_min_rx;
        bfd->flags &= ~FLAG_POLL;
        log_msg(VLL_INFO, msg, "Poll sequence terminated", bfd);
    }

    if (flags & FLAG_POLL) {
        /* RFC 5880 Section 6.5
         * When the other system receives a Poll, it immediately transmits a
         * BFD Control packet with the Final (F) bit set, independent of any
         * periodic BFD Control packets it may be sending
         * (see section 6.8.7). */
        bfd->flags &= ~FLAG_POLL;
        bfd->flags |= FLAG_FINAL;
    }

    if (bfd->rmt_mult != msg->mult) {
        VLOG_INFO("Interface %s remote mult value %d changed to %d",
                   bfd->name, bfd->rmt_mult, msg->mult);
        bfd->rmt_mult = msg->mult;
    }

    rmt_min_rx = MAX(ntohl(get_16aligned_be32(&msg->min_rx)) / 1000, 1);
    if (bfd->rmt_min_rx != rmt_min_rx) {
        bfd->rmt_min_rx = rmt_min_rx;
        if (bfd->next_tx) {
            bfd_set_next_tx(bfd);
        }
        log_msg(VLL_INFO, msg, "New remote min_rx", bfd);
    }

    bfd->rmt_min_tx = MAX(ntohl(get_16aligned_be32(&msg->min_tx)) / 1000, 1);
    bfd->detect_time = bfd_rx_interval(bfd) * bfd->rmt_mult + time_msec();

    if (bfd->state == STATE_ADMIN_DOWN) {
        VLOG_DBG_RL(&rl, "Administratively down, dropping control message.");
        goto out;
    }

    if (rmt_state == STATE_ADMIN_DOWN) {
        if (bfd->state != STATE_DOWN) {
            bfd_set_state(bfd, STATE_DOWN, DIAG_RMT_DOWN);
        }
    } else {
        switch (bfd->state) {
        case STATE_DOWN:
            if (rmt_state == STATE_DOWN) {
                bfd_set_state(bfd, STATE_INIT, bfd->diag);
            } else if (rmt_state == STATE_INIT) {
                bfd_set_state(bfd, STATE_UP, bfd->diag);
            }
            break;
        case STATE_INIT:
            if (rmt_state > STATE_DOWN) {
                bfd_set_state(bfd, STATE_UP, bfd->diag);
            }
            break;
        case STATE_UP:
            if (rmt_state <= STATE_DOWN) {
                bfd_set_state(bfd, STATE_DOWN, DIAG_RMT_DOWN);
                log_msg(VLL_INFO, msg, "Remote signaled STATE_DOWN", bfd);
            }
            break;
        case STATE_ADMIN_DOWN:
        default:
            OVS_NOT_REACHED();
        }
    }
    /* XXX: RFC 5880 Section 6.8.6 Demand mode related calculations here. */

    if (bfd->forwarding_if_rx) {
        bfd->demand_rx_bfd_time = time_msec() + 100 * bfd->cfg_min_rx;
    }

out:
    bfd_forwarding__(bfd);
    ovs_mutex_unlock(&mutex);
}

/* Must be called when the netdev owned by 'bfd' should change. */
void
bfd_set_netdev(struct bfd *bfd, const struct netdev *netdev)
    OVS_EXCLUDED(mutex)
{
    ovs_mutex_lock(&mutex);
    if (bfd->netdev != netdev) {
        netdev_close(bfd->netdev);
        bfd->netdev = netdev_ref(netdev);
        if (bfd->decay_min_rx && bfd->state == STATE_UP) {
            bfd_decay_update(bfd);
        }
        if (bfd->forwarding_if_rx && bfd->state == STATE_UP) {
            bfd_forwarding_if_rx_update(bfd);
        }
        bfd->rx_packets = bfd_rx_packets(bfd);
    }
    ovs_mutex_unlock(&mutex);
}


/* Updates the forwarding flag.  If override is not configured and
 * the forwarding flag value changes, increments the flap count.
 *
 * Note this function may be called multiple times in a function
 * (e.g. bfd_account_rx) before and after the bfd state or status
 * change.  This is to capture any forwarding flag flap. */
static bool
bfd_forwarding__(struct bfd *bfd) OVS_REQUIRES(mutex)
{
    long long int now = time_msec();
    bool forwarding_if_rx;
    bool last_forwarding = bfd->last_forwarding;

    if (bfd->forwarding_override != -1) {
        return bfd->forwarding_override == 1;
    }

    forwarding_if_rx = bfd->forwarding_if_rx
                       && bfd->forwarding_if_rx_detect_time > now
                       && bfd->demand_rx_bfd_time > now;

    bfd->last_forwarding = (bfd->state == STATE_UP || forwarding_if_rx)
                           && bfd->rmt_diag != DIAG_PATH_DOWN
                           && bfd->rmt_diag != DIAG_CPATH_DOWN
                           && bfd->rmt_diag != DIAG_RCPATH_DOWN;
    if (bfd->last_forwarding != last_forwarding) {
        bfd->flap_count++;
        bfd_status_changed(bfd);
    }
    return bfd->last_forwarding;
}

/* Helpers. */
static void
bfd_lookup_ip(const char *host_name, ovs_be32 def, ovs_be32 *addr)
{
    if (host_name[0]) {
        if (ip_parse(host_name, addr)) {
            return;
        }
        VLOG_ERR_RL(&rl, "\"%s\" is not a valid IP address", host_name);
    }
    *addr = def;
}

static bool
bfd_in_poll(const struct bfd *bfd) OVS_REQUIRES(mutex)
{
    return (bfd->flags & FLAG_POLL) != 0;
}

static void
bfd_poll(struct bfd *bfd) OVS_REQUIRES(mutex)
{
    if (bfd->state > STATE_DOWN && !bfd_in_poll(bfd)
        && !(bfd->flags & FLAG_FINAL)) {
        bfd->poll_min_tx = bfd->cfg_min_tx;
        bfd->poll_min_rx = bfd->in_decay ? bfd->decay_min_rx : bfd->cfg_min_rx;
        bfd->flags |= FLAG_POLL;
        bfd->next_tx = 0;
        VLOG_INFO_RL(&rl, "%s: Initiating poll sequence", bfd->name);
    }
}

static long long int
bfd_min_tx(const struct bfd *bfd) OVS_REQUIRES(mutex)
{
    /* RFC 5880 Section 6.8.3
     * When bfd.SessionState is not Up, the system MUST set
     * bfd.DesiredMinTxInterval to a value of not less than one second
     * (1,000,000 microseconds).  This is intended to ensure that the
     * bandwidth consumed by BFD sessions that are not Up is negligible,
     * particularly in the case where a neighbor may not be running BFD. */
    return (bfd->state == STATE_UP ? bfd->min_tx : MAX(bfd->min_tx, 1000));
}

static long long int
bfd_tx_interval(const struct bfd *bfd) OVS_REQUIRES(mutex)
{
    long long int interval = bfd_min_tx(bfd);
    return MAX(interval, bfd->rmt_min_rx);
}

static long long int
bfd_rx_interval(const struct bfd *bfd) OVS_REQUIRES(mutex)
{
    return MAX(bfd->min_rx, bfd->rmt_min_tx);
}

static void
bfd_set_next_tx(struct bfd *bfd) OVS_REQUIRES(mutex)
{
    long long int interval = bfd_tx_interval(bfd);
    if (bfd->mult == 1) {
        interval -= interval * (10 + random_range(16)) / 100;
    } else {
        interval -= interval * random_range(26) / 100;
    }
    bfd->next_tx = bfd->last_tx + interval;
}

static const char *
bfd_flag_str(enum flags flags)
{
    struct ds ds = DS_EMPTY_INITIALIZER;
    static char flag_str[128];

    if (!flags) {
        return "none";
    }

    if (flags & FLAG_MULTIPOINT) {
        ds_put_cstr(&ds, "multipoint ");
    }

    if (flags & FLAG_DEMAND) {
        ds_put_cstr(&ds, "demand ");
    }

    if (flags & FLAG_AUTH) {
        ds_put_cstr(&ds, "auth ");
    }

    if (flags & FLAG_CTL) {
        ds_put_cstr(&ds, "ctl ");
    }

    if (flags & FLAG_FINAL) {
        ds_put_cstr(&ds, "final ");
    }

    if (flags & FLAG_POLL) {
        ds_put_cstr(&ds, "poll ");
    }

    /* Do not copy the trailing whitespace. */
    ds_chomp(&ds, ' ');
    ovs_strlcpy(flag_str, ds_cstr(&ds), sizeof flag_str);
    ds_destroy(&ds);
    return flag_str;
}

static const char *
bfd_state_str(enum state state)
{
    switch (state) {
    case STATE_ADMIN_DOWN: return "admin_down";
    case STATE_DOWN: return "down";
    case STATE_INIT: return "init";
    case STATE_UP: return "up";
    default: return "invalid";
    }
}

static const char *
bfd_diag_str(enum diag diag) {
    switch (diag) {
    case DIAG_NONE: return "No Diagnostic";
    case DIAG_EXPIRED: return "Control Detection Time Expired";
    case DIAG_ECHO_FAILED: return "Echo Function Failed";
    case DIAG_RMT_DOWN: return "Neighbor Signaled Session Down";
    case DIAG_FWD_RESET: return "Forwarding Plane Reset";
    case DIAG_PATH_DOWN: return "Path Down";
    case DIAG_CPATH_DOWN: return "Concatenated Path Down";
    case DIAG_ADMIN_DOWN: return "Administratively Down";
    case DIAG_RCPATH_DOWN: return "Reverse Concatenated Path Down";
    default: return "Invalid Diagnostic";
    }
};

static void
log_msg(enum vlog_level level, const struct msg *p, const char *message,
        const struct bfd *bfd) OVS_REQUIRES(mutex)
{
    struct ds ds = DS_EMPTY_INITIALIZER;

    if (vlog_should_drop(&this_module, level, &rl)) {
        return;
    }

    ds_put_format(&ds,
                  "%s: %s."
                  "\n  vers:%d diag:\"%s\" state:%s mult:%"PRIu8
                  " length:%"PRIu8
                  "\n  flags: %s"
                  "\n  my_disc:0x%"PRIx32" your_disc:0x%"PRIx32
                  "\n  min_tx:%"PRIu32"us (%"PRIu32"ms)"
                  "\n  min_rx:%"PRIu32"us (%"PRIu32"ms)"
                  "\n  min_rx_echo:%"PRIu32"us (%"PRIu32"ms)",
                  bfd->name, message, p->vers_diag >> VERS_SHIFT,
                  bfd_diag_str(p->vers_diag & DIAG_MASK),
                  bfd_state_str(p->flags & STATE_MASK),
                  p->mult, p->length, bfd_flag_str(p->flags & FLAGS_MASK),
                  ntohl(get_16aligned_be32(&p->my_disc)),
                  ntohl(get_16aligned_be32(&p->your_disc)),
                  ntohl(get_16aligned_be32(&p->min_tx)),
                  ntohl(get_16aligned_be32(&p->min_tx)) / 1000,
                  ntohl(get_16aligned_be32(&p->min_rx)),
                  ntohl(get_16aligned_be32(&p->min_rx)) / 1000,
                  ntohl(get_16aligned_be32(&p->min_rx_echo)),
                  ntohl(get_16aligned_be32(&p->min_rx_echo)) / 1000);
    bfd_put_details(&ds, bfd);
    VLOG(level, "%s", ds_cstr(&ds));
    ds_destroy(&ds);
}

static void
bfd_set_state(struct bfd *bfd, enum state state, enum diag diag)
    OVS_REQUIRES(mutex)
{
    if (bfd->cpath_down) {
        diag = DIAG_CPATH_DOWN;
    }

    if (bfd->state != state || bfd->diag != diag) {
        if (!VLOG_DROP_INFO(&rl)) {
            struct ds ds = DS_EMPTY_INITIALIZER;

            ds_put_format(&ds, "%s: BFD state change: %s->%s"
                          " \"%s\"->\"%s\".\n",
                          bfd->name, bfd_state_str(bfd->state),
                          bfd_state_str(state), bfd_diag_str(bfd->diag),
                          bfd_diag_str(diag));
            bfd_put_details(&ds, bfd);
            VLOG_INFO("%s", ds_cstr(&ds));
            ds_destroy(&ds);
        }

        bfd->state = state;
        bfd->diag = diag;

        if (bfd->state <= STATE_DOWN) {
            bfd->rmt_state = STATE_DOWN;
            bfd->rmt_diag = DIAG_NONE;
            bfd->rmt_min_rx = 1;
            bfd->rmt_flags = 0;
            bfd->rmt_disc = 0;
            bfd->rmt_min_tx = 0;
            /* Resets the min_rx if in_decay. */
            if (bfd->in_decay) {
                bfd->min_rx = bfd->cfg_min_rx;
                bfd->in_decay = false;
            }
        }
        /* Resets the decay when state changes to STATE_UP
         * and decay_min_rx is configured. */
        if (bfd->state == STATE_UP && bfd->decay_min_rx) {
            bfd_decay_update(bfd);
        }

        bfd_status_changed(bfd);
    }
}

static uint64_t
bfd_rx_packets(const struct bfd *bfd) OVS_REQUIRES(mutex)
{
    struct netdev_stats stats;

    if (!netdev_get_stats(bfd->netdev, &stats)) {
        return stats.rx_packets;
    } else {
        return 0;
    }
}

/* Decays the bfd->min_rx to bfd->decay_min_rx when 'diff' is less than
 * the 'expect' value. */
static void
bfd_try_decay(struct bfd *bfd) OVS_REQUIRES(mutex)
{
    int64_t diff, expect;

    /* The 'diff' is the difference between current interface rx_packets
     * stats and last-time check.  The 'expect' is the recorded number of
     * bfd control packets received within an approximately decay_min_rx
     * (2000 ms if decay_min_rx is less than 2000 ms) interval.
     *
     * Since the update of rx_packets stats at interface happens
     * asynchronously to the bfd_rx_packets() function, the 'diff' value
     * can be jittered.  Thusly, we double the decay_rx_ctl to provide
     * more wiggle room. */
    diff = bfd_rx_packets(bfd) - bfd->decay_rx_packets;
    expect = 2 * MAX(bfd->decay_rx_ctl, 1);
    bfd->in_decay = diff <= expect ? true : false;
    bfd_decay_update(bfd);
}

/* Updates the rx_packets, decay_rx_ctl and decay_detect_time. */
static void
bfd_decay_update(struct bfd * bfd) OVS_REQUIRES(mutex)
{
    bfd->decay_rx_packets = bfd_rx_packets(bfd);
    bfd->decay_rx_ctl = 0;
    bfd->decay_detect_time = MAX(bfd->decay_min_rx, 2000) + time_msec();
}

/* Records the status change and changes the global connectivity seq. */
static void
bfd_status_changed(struct bfd *bfd) OVS_REQUIRES(mutex)
{
    seq_change(connectivity_seq_get());
    bfd->status_changed = true;
}

static void
bfd_forwarding_if_rx_update(struct bfd *bfd) OVS_REQUIRES(mutex)
{
    int64_t incr = bfd_rx_interval(bfd) * bfd->mult;
    bfd->forwarding_if_rx_detect_time = MAX(incr, 2000) + time_msec();
}

static uint32_t
generate_discriminator(void)
{
    uint32_t disc = 0;

    /* RFC 5880 Section 6.8.1
     * It SHOULD be set to a random (but still unique) value to improve
     * security.  The value is otherwise outside the scope of this
     * specification. */

    while (!disc) {
        struct bfd *bfd;

        /* 'disc' is by definition random, so there's no reason to waste time
         * hashing it. */
        disc = random_uint32();
        HMAP_FOR_EACH_IN_BUCKET (bfd, node, disc, all_bfds) {
            if (bfd->disc == disc) {
                disc = 0;
                break;
            }
        }
    }

    return disc;
}

static struct bfd *
bfd_find_by_name(const char *name) OVS_REQUIRES(mutex)
{
    struct bfd *bfd;

    HMAP_FOR_EACH (bfd, node, all_bfds) {
        if (!strcmp(bfd->name, name)) {
            return bfd;
        }
    }
    return NULL;
}

static void
bfd_put_details(struct ds *ds, const struct bfd *bfd) OVS_REQUIRES(mutex)
{
    ds_put_format(ds, "  Forwarding: %s\n",
                  bfd_forwarding__(CONST_CAST(struct bfd *, bfd))
                  ? "true" : "false");
    ds_put_format(ds, "  Detect Multiplier: %d\n", bfd->mult);
    ds_put_format(ds, "  Concatenated Path Down: %s\n",
                  bfd->cpath_down ? "true" : "false");
    ds_put_format(ds, "  TX Interval: Approx %lldms\n", bfd_tx_interval(bfd));
    ds_put_format(ds, "  RX Interval: Approx %lldms\n", bfd_rx_interval(bfd));
    ds_put_format(ds, "  Detect Time: now %+lldms\n",
                  bfd->detect_time - time_msec());
    ds_put_format(ds, "  Next TX Time: now %+lldms\n",
                  bfd->next_tx -time_msec());
    ds_put_format(ds, "  Last TX Time: now %+lldms\n",
                  bfd->last_tx - time_msec());

    ds_put_cstr(ds, "\n");

    ds_put_format(ds, "  Local Flags: %s\n", bfd_flag_str(bfd->flags));
    ds_put_format(ds, "  Local Session State: %s\n",
                  bfd_state_str(bfd->state));
    ds_put_format(ds, "  Local Diagnostic: %s\n", bfd_diag_str(bfd->diag));
    ds_put_format(ds, "  Local Discriminator: 0x%"PRIx32"\n", bfd->disc);
    ds_put_format(ds, "  Local Minimum TX Interval: %lldms\n",
                  bfd_min_tx(bfd));
    ds_put_format(ds, "  Local Minimum RX Interval: %lldms\n", bfd->min_rx);

    ds_put_cstr(ds, "\n");

    ds_put_format(ds, "  Remote Flags: %s\n", bfd_flag_str(bfd->rmt_flags));
    ds_put_format(ds, "  Remote Session State: %s\n",
                  bfd_state_str(bfd->rmt_state));
    ds_put_format(ds, "  Remote Diagnostic: %s\n",
                  bfd_diag_str(bfd->rmt_diag));
    ds_put_format(ds, "  Remote Discriminator: 0x%"PRIx32"\n", bfd->rmt_disc);
    ds_put_format(ds, "  Remote Minimum TX Interval: %lldms\n",
                  bfd->rmt_min_tx);
    ds_put_format(ds, "  Remote Minimum RX Interval: %lldms\n",
                  bfd->rmt_min_rx);
    ds_put_format(ds, "  Remote Detect Multiplier: %d\n", bfd->rmt_mult);
}

static void
bfd_unixctl_show(struct unixctl_conn *conn, int argc, const char *argv[],
                 void *aux OVS_UNUSED) OVS_EXCLUDED(mutex)
{
    struct ds ds = DS_EMPTY_INITIALIZER;
    struct bfd *bfd;

    ovs_mutex_lock(&mutex);
    if (argc > 1) {
        bfd = bfd_find_by_name(argv[1]);
        if (!bfd) {
            unixctl_command_reply_error(conn, "no such bfd object");
            goto out;
        }
        bfd_put_details(&ds, bfd);
    } else {
        HMAP_FOR_EACH (bfd, node, all_bfds) {
            ds_put_format(&ds, "---- %s ----\n", bfd->name);
            bfd_put_details(&ds, bfd);
        }
    }
    unixctl_command_reply(conn, ds_cstr(&ds));
    ds_destroy(&ds);

out:
    ovs_mutex_unlock(&mutex);
}


static void
bfd_unixctl_set_forwarding_override(struct unixctl_conn *conn, int argc,
                                    const char *argv[], void *aux OVS_UNUSED)
    OVS_EXCLUDED(mutex)
{
    const char *forward_str = argv[argc - 1];
    int forwarding_override;
    struct bfd *bfd;

    ovs_mutex_lock(&mutex);
    if (!strcasecmp("true", forward_str)) {
        forwarding_override = 1;
    } else if (!strcasecmp("false", forward_str)) {
        forwarding_override = 0;
    } else if (!strcasecmp("normal", forward_str)) {
        forwarding_override = -1;
    } else {
        unixctl_command_reply_error(conn, "unknown fault string");
        goto out;
    }

    if (argc > 2) {
        bfd = bfd_find_by_name(argv[1]);
        if (!bfd) {
            unixctl_command_reply_error(conn, "no such BFD object");
            goto out;
        }
        bfd->forwarding_override = forwarding_override;
        bfd_status_changed(bfd);
    } else {
        HMAP_FOR_EACH (bfd, node, all_bfds) {
            bfd->forwarding_override = forwarding_override;
            bfd_status_changed(bfd);
        }
    }

    unixctl_command_reply(conn, "OK");

out:
    ovs_mutex_unlock(&mutex);
}