aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-drbd.c
blob: eaff1e14066c908cb5269909307ccbdac120f886 (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
/* packet-drbd.c
 * Routines for DRBD dissection
 * By Joel Colledge <joel.colledge@linbit.com>
 * Copyright 2019, LINBIT Information Technologies GmbH
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/*
 * Wireshark dissector for DRBD - Distributed Replicated Block Device.
 * The DRBD Linux kernel module sources can be found at https://github.com/LINBIT/drbd-9.0
 * More information about Linbit and DRBD can be found at https://www.linbit.com/
 */

#include <config.h>

#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/dissectors/packet-tcp.h>

#include <wsutil/str_util.h>

typedef struct value_payload_decoder {
    int value;
    void (*decoder_fn)(tvbuff_t *, proto_tree*);
} value_payload_decoder;

/* Known as SHARED_SECRET_MAX in the DRBD sources */
#define DRBD_STRING_MAX 64

enum drbd_packet {
    P_DATA                = 0x00,
    P_DATA_REPLY          = 0x01,
    P_RS_DATA_REPLY       = 0x02,
    P_BARRIER             = 0x03,
    P_BITMAP              = 0x04,
    P_BECOME_SYNC_TARGET  = 0x05,
    P_BECOME_SYNC_SOURCE  = 0x06,
    P_UNPLUG_REMOTE       = 0x07,
    P_DATA_REQUEST        = 0x08,
    P_RS_DATA_REQUEST     = 0x09,
    P_SYNC_PARAM          = 0x0a,
    P_PROTOCOL            = 0x0b,
    P_UUIDS               = 0x0c,
    P_SIZES               = 0x0d,
    P_STATE               = 0x0e,
    P_SYNC_UUID           = 0x0f,
    P_AUTH_CHALLENGE      = 0x10,
    P_AUTH_RESPONSE       = 0x11,
    P_STATE_CHG_REQ       = 0x12,

    P_PING                = 0x13,
    P_PING_ACK            = 0x14,
    P_RECV_ACK            = 0x15,
    P_WRITE_ACK           = 0x16,
    P_RS_WRITE_ACK        = 0x17,
    P_SUPERSEDED          = 0x18,
    P_NEG_ACK             = 0x19,
    P_NEG_DREPLY          = 0x1a,
    P_NEG_RS_DREPLY       = 0x1b,
    P_BARRIER_ACK         = 0x1c,
    P_STATE_CHG_REPLY     = 0x1d,

    P_OV_REQUEST          = 0x1e,
    P_OV_REPLY            = 0x1f,
    P_OV_RESULT           = 0x20,
    P_CSUM_RS_REQUEST     = 0x21,
    P_RS_IS_IN_SYNC       = 0x22,
    P_SYNC_PARAM89        = 0x23,
    P_COMPRESSED_BITMAP   = 0x24,

    P_DELAY_PROBE         = 0x27,
    P_OUT_OF_SYNC         = 0x28,
    P_RS_CANCEL           = 0x29,
    P_CONN_ST_CHG_REQ     = 0x2a,
    P_CONN_ST_CHG_REPLY   = 0x2b,
    P_RETRY_WRITE         = 0x2c,
    P_PROTOCOL_UPDATE     = 0x2d,
    P_TWOPC_PREPARE       = 0x2e,
    P_TWOPC_ABORT         = 0x2f,

    P_DAGTAG              = 0x30,

    P_TRIM                = 0x31,

    P_RS_THIN_REQ         = 0x32,
    P_RS_DEALLOCATED      = 0x33,

    P_WSAME               = 0x34,
    P_TWOPC_PREP_RSZ      = 0x35,
    P_ZEROES              = 0x36,

    P_PEER_ACK            = 0x40,
    P_PEERS_IN_SYNC       = 0x41,

    P_UUIDS110            = 0x42,
    P_PEER_DAGTAG         = 0x43,
    P_CURRENT_UUID        = 0x44,

    P_TWOPC_YES           = 0x45,
    P_TWOPC_NO            = 0x46,
    P_TWOPC_COMMIT        = 0x47,
    P_TWOPC_RETRY         = 0x48,

    P_CONFIRM_STABLE      = 0x49,

    P_INITIAL_META        = 0xfff1,
    P_INITIAL_DATA        = 0xfff2,

    P_CONNECTION_FEATURES = 0xfffe
};

static const value_string packet_names[] = {
    { P_DATA, "P_DATA" },
    { P_DATA_REPLY, "P_DATA_REPLY" },
    { P_RS_DATA_REPLY, "P_RS_DATA_REPLY" },
    { P_BARRIER, "P_BARRIER" },
    { P_BITMAP, "P_BITMAP" },
    { P_BECOME_SYNC_TARGET, "P_BECOME_SYNC_TARGET" },
    { P_BECOME_SYNC_SOURCE, "P_BECOME_SYNC_SOURCE" },
    { P_UNPLUG_REMOTE, "P_UNPLUG_REMOTE" },
    { P_DATA_REQUEST, "P_DATA_REQUEST" },
    { P_RS_DATA_REQUEST, "P_RS_DATA_REQUEST" },
    { P_SYNC_PARAM, "P_SYNC_PARAM" },
    { P_PROTOCOL, "P_PROTOCOL" },
    { P_UUIDS, "P_UUIDS" },
    { P_SIZES, "P_SIZES" },
    { P_STATE, "P_STATE" },
    { P_SYNC_UUID, "P_SYNC_UUID" },
    { P_AUTH_CHALLENGE, "P_AUTH_CHALLENGE" },
    { P_AUTH_RESPONSE, "P_AUTH_RESPONSE" },
    { P_STATE_CHG_REQ, "P_STATE_CHG_REQ" },

    { P_PING, "P_PING" },
    { P_PING_ACK, "P_PING_ACK" },
    { P_RECV_ACK, "P_RECV_ACK" },
    { P_WRITE_ACK, "P_WRITE_ACK" },
    { P_RS_WRITE_ACK, "P_RS_WRITE_ACK" },
    { P_SUPERSEDED, "P_SUPERSEDED" },
    { P_NEG_ACK, "P_NEG_ACK" },
    { P_NEG_DREPLY, "P_NEG_DREPLY" },
    { P_NEG_RS_DREPLY, "P_NEG_RS_DREPLY" },
    { P_BARRIER_ACK, "P_BARRIER_ACK" },
    { P_STATE_CHG_REPLY, "P_STATE_CHG_REPLY" },

    { P_OV_REQUEST, "P_OV_REQUEST" },
    { P_OV_REPLY, "P_OV_REPLY" },
    { P_OV_RESULT, "P_OV_RESULT" },
    { P_CSUM_RS_REQUEST, "P_CSUM_RS_REQUEST" },
    { P_RS_IS_IN_SYNC, "P_RS_IS_IN_SYNC" },
    { P_SYNC_PARAM89, "P_SYNC_PARAM89" },
    { P_COMPRESSED_BITMAP, "P_COMPRESSED_BITMAP" },

    { P_DELAY_PROBE, "P_DELAY_PROBE" },
    { P_OUT_OF_SYNC, "P_OUT_OF_SYNC" },
    { P_RS_CANCEL, "P_RS_CANCEL" },
    { P_CONN_ST_CHG_REQ, "P_CONN_ST_CHG_REQ" },
    { P_CONN_ST_CHG_REPLY, "P_CONN_ST_CHG_REPLY" },
    { P_RETRY_WRITE, "P_RETRY_WRITE" },
    { P_PROTOCOL_UPDATE, "P_PROTOCOL_UPDATE" },
    { P_TWOPC_PREPARE, "P_TWOPC_PREPARE" },
    { P_TWOPC_ABORT, "P_TWOPC_ABORT" },

    { P_DAGTAG, "P_DAGTAG" },

    { P_TRIM, "P_TRIM" },

    { P_RS_THIN_REQ, "P_RS_THIN_REQ" },
    { P_RS_DEALLOCATED, "P_RS_DEALLOCATED" },

    { P_WSAME, "P_WSAME" },
    { P_TWOPC_PREP_RSZ, "P_TWOPC_PREP_RSZ" },
    { P_ZEROES, "P_ZEROES" },

    { P_PEER_ACK, "P_PEER_ACK" },
    { P_PEERS_IN_SYNC, "P_PEERS_IN_SYNC" },

    { P_UUIDS110, "P_UUIDS110" },
    { P_PEER_DAGTAG, "P_PEER_DAGTAG" },
    { P_CURRENT_UUID, "P_CURRENT_UUID" },

    { P_TWOPC_YES, "P_TWOPC_YES" },
    { P_TWOPC_NO, "P_TWOPC_NO" },
    { P_TWOPC_COMMIT, "P_TWOPC_COMMIT" },
    { P_TWOPC_RETRY, "P_TWOPC_RETRY" },

    { P_CONFIRM_STABLE, "P_CONFIRM_STABLE" },

    { P_INITIAL_META, "P_INITIAL_META" },
    { P_INITIAL_DATA, "P_INITIAL_DATA" },

    { P_CONNECTION_FEATURES, "P_CONNECTION_FEATURES" },
    { 0, NULL }
};

#define DRBD_PROT_A   1
#define DRBD_PROT_B   2
#define DRBD_PROT_C   3

static const value_string protocol_names[] = {
    { DRBD_PROT_A, "A" },
    { DRBD_PROT_B, "B" },
    { DRBD_PROT_C, "C" },
    { 0, NULL }
};

#define DRBD_ROLE_UNKNOWN   0
#define DRBD_ROLE_PRIMARY   1
#define DRBD_ROLE_SECONDARY 2

static const value_string role_names[] = {
    { DRBD_ROLE_UNKNOWN, "UNKNOWN" },
    { DRBD_ROLE_PRIMARY, "PRIMARY" },
    { DRBD_ROLE_SECONDARY, "SECONDARY" },
    { 0, NULL }
};

#define DRBD_CONNECTION_STATE_C_STANDALONE 0
#define DRBD_CONNECTION_STATE_C_DISCONNECTING 1
#define DRBD_CONNECTION_STATE_C_UNCONNECTED 2
#define DRBD_CONNECTION_STATE_C_TIMEOUT 3
#define DRBD_CONNECTION_STATE_C_BROKEN_PIPE 4
#define DRBD_CONNECTION_STATE_C_NETWORK_FAILURE 5
#define DRBD_CONNECTION_STATE_C_PROTOCOL_ERROR 6
#define DRBD_CONNECTION_STATE_C_TEAR_DOWN 7
#define DRBD_CONNECTION_STATE_C_CONNECTING 8
#define DRBD_CONNECTION_STATE_C_CONNECTED 9
#define DRBD_CONNECTION_STATE_L_ESTABLISHED 10
#define DRBD_CONNECTION_STATE_L_STARTING_SYNC_S 11
#define DRBD_CONNECTION_STATE_L_STARTING_SYNC_T 12
#define DRBD_CONNECTION_STATE_L_WF_BITMAP_S 13
#define DRBD_CONNECTION_STATE_L_WF_BITMAP_T 14
#define DRBD_CONNECTION_STATE_L_WF_SYNC_UUID 15
#define DRBD_CONNECTION_STATE_L_SYNC_SOURCE 16
#define DRBD_CONNECTION_STATE_L_SYNC_TARGET 17
#define DRBD_CONNECTION_STATE_L_VERIFY_S 18
#define DRBD_CONNECTION_STATE_L_VERIFY_T 19
#define DRBD_CONNECTION_STATE_L_PAUSED_SYNC_S 20
#define DRBD_CONNECTION_STATE_L_PAUSED_SYNC_T 21
#define DRBD_CONNECTION_STATE_L_AHEAD 22
#define DRBD_CONNECTION_STATE_L_BEHIND 23

static const value_string connection_state_names[] = {
    { DRBD_CONNECTION_STATE_C_STANDALONE, "C_STANDALONE" },
    { DRBD_CONNECTION_STATE_C_DISCONNECTING, "C_DISCONNECTING" },
    { DRBD_CONNECTION_STATE_C_UNCONNECTED, "C_UNCONNECTED" },
    { DRBD_CONNECTION_STATE_C_TIMEOUT, "C_TIMEOUT" },
    { DRBD_CONNECTION_STATE_C_BROKEN_PIPE, "C_BROKEN_PIPE" },
    { DRBD_CONNECTION_STATE_C_NETWORK_FAILURE, "C_NETWORK_FAILURE" },
    { DRBD_CONNECTION_STATE_C_PROTOCOL_ERROR, "C_PROTOCOL_ERROR" },
    { DRBD_CONNECTION_STATE_C_TEAR_DOWN, "C_TEAR_DOWN" },
    { DRBD_CONNECTION_STATE_C_CONNECTING, "C_CONNECTING" },
    { DRBD_CONNECTION_STATE_C_CONNECTED, "C_CONNECTED" },
    { DRBD_CONNECTION_STATE_L_ESTABLISHED, "L_ESTABLISHED" },
    { DRBD_CONNECTION_STATE_L_STARTING_SYNC_S, "L_STARTING_SYNC_S" },
    { DRBD_CONNECTION_STATE_L_STARTING_SYNC_T, "L_STARTING_SYNC_T" },
    { DRBD_CONNECTION_STATE_L_WF_BITMAP_S, "L_WF_BITMAP_S" },
    { DRBD_CONNECTION_STATE_L_WF_BITMAP_T, "L_WF_BITMAP_T" },
    { DRBD_CONNECTION_STATE_L_WF_SYNC_UUID, "L_WF_SYNC_UUID" },
    { DRBD_CONNECTION_STATE_L_SYNC_SOURCE, "L_SYNC_SOURCE" },
    { DRBD_CONNECTION_STATE_L_SYNC_TARGET, "L_SYNC_TARGET" },
    { DRBD_CONNECTION_STATE_L_VERIFY_S, "L_VERIFY_S" },
    { DRBD_CONNECTION_STATE_L_VERIFY_T, "L_VERIFY_T" },
    { DRBD_CONNECTION_STATE_L_PAUSED_SYNC_S, "L_PAUSED_SYNC_S" },
    { DRBD_CONNECTION_STATE_L_PAUSED_SYNC_T, "L_PAUSED_SYNC_T" },
    { DRBD_CONNECTION_STATE_L_AHEAD, "L_AHEAD" },
    { DRBD_CONNECTION_STATE_L_BEHIND, "L_BEHIND" },
    { 0, NULL }
};

#define DRBD_DISK_STATE_DISKLESS 0
#define DRBD_DISK_STATE_ATTACHING 1
#define DRBD_DISK_STATE_DETACHING 2
#define DRBD_DISK_STATE_FAILED 3
#define DRBD_DISK_STATE_NEGOTIATING 4
#define DRBD_DISK_STATE_INCONSISTENT 5
#define DRBD_DISK_STATE_OUTDATED 6
#define DRBD_DISK_STATE_UNKNOWN 7
#define DRBD_DISK_STATE_CONSISTENT 8
#define DRBD_DISK_STATE_UP_TO_DATE 9

static const value_string disk_state_names[] = {
    { DRBD_DISK_STATE_DISKLESS, "D_DISKLESS" },
    { DRBD_DISK_STATE_ATTACHING, "D_ATTACHING" },
    { DRBD_DISK_STATE_DETACHING, "D_DETACHING" },
    { DRBD_DISK_STATE_FAILED, "D_FAILED" },
    { DRBD_DISK_STATE_NEGOTIATING, "D_NEGOTIATING" },
    { DRBD_DISK_STATE_INCONSISTENT, "D_INCONSISTENT" },
    { DRBD_DISK_STATE_OUTDATED, "D_OUTDATED" },
    { DRBD_DISK_STATE_UNKNOWN, "D_UNKNOWN" },
    { DRBD_DISK_STATE_CONSISTENT, "D_CONSISTENT" },
    { DRBD_DISK_STATE_UP_TO_DATE, "D_UP_TO_DATE" },
    { 0, NULL }
};

#define STATE_ROLE (0x3 << 0)    /* 3/4	 primary/secondary/unknown */
#define STATE_PEER (0x3 << 2)    /* 3/4	 primary/secondary/unknown */
#define STATE_CONN (0x1f << 4)    /* 17/32	 cstates */
#define STATE_DISK (0xf << 9)    /* 8/16	 from D_DISKLESS to D_UP_TO_DATE */
#define STATE_PDSK (0xf << 13)    /* 8/16	 from D_DISKLESS to D_UP_TO_DATE */
#define STATE_SUSP (0x1 << 17)    /* 2/2	 IO suspended no/yes (by user) */
#define STATE_AFTR_ISP (0x1 << 18)  /* isp .. imposed sync pause */
#define STATE_PEER_ISP (0x1 << 19)
#define STATE_USER_ISP (0x1 << 20)
#define STATE_SUSP_NOD (0x1 << 21)  /* IO suspended because no data */
#define STATE_SUSP_FEN (0x1 << 22)  /* IO suspended because fence peer handler runs*/
#define STATE_QUORUM (0x1 << 23)

#define UUID_FLAG_DISCARD_MY_DATA 1
#define UUID_FLAG_CRASHED_PRIMARY 2
#define UUID_FLAG_INCONSISTENT 4
#define UUID_FLAG_SKIP_INITIAL_SYNC 8
#define UUID_FLAG_NEW_DATAGEN 16
#define UUID_FLAG_STABLE 32
#define UUID_FLAG_GOT_STABLE 64
#define UUID_FLAG_RESYNC 128
#define UUID_FLAG_RECONNECT 256
#define UUID_FLAG_DISKLESS_PRIMARY 512
#define UUID_FLAG_PRIMARY_LOST_QUORUM 1024

#define DP_HARDBARRIER        1
#define DP_RW_SYNC            2
#define DP_MAY_SET_IN_SYNC    4
#define DP_UNPLUG             8
#define DP_FUA               16
#define DP_FLUSH             32
#define DP_DISCARD           64
#define DP_SEND_RECEIVE_ACK 128
#define DP_SEND_WRITE_ACK   256
#define DP_WSAME            512
#define DP_ZEROES          1024

static void dissect_drbd_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);

static void decode_payload_connection_features(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_auth_challenge(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_auth_response(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_data(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_data_reply(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_rs_data_reply(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_barrier(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_data_request(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_sync_param(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_protocol(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_uuids(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_sizes(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_state(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_req_state(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_sync_uuid(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_skip(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_out_of_sync(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_twopc(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_dagtag(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_uuids110(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_peer_dagtag(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_current_uuid(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_data_size(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_data_wsame(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_rs_deallocated(tvbuff_t *tvb, proto_tree *tree);

static void decode_payload_block_ack(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_barrier_ack(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_confirm_stable(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_rq_s_reply(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_peer_ack(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_peers_in_sync(tvbuff_t *tvb, proto_tree *tree);
static void decode_payload_twopc_reply(tvbuff_t *tvb, proto_tree *tree);

static const value_payload_decoder payload_decoders[] = {
    { P_CONNECTION_FEATURES, decode_payload_connection_features },
    { P_AUTH_CHALLENGE, decode_payload_auth_challenge },
    { P_AUTH_RESPONSE, decode_payload_auth_response },
    { P_DATA, decode_payload_data },
    { P_DATA_REPLY, decode_payload_data_reply },
    { P_RS_DATA_REPLY, decode_payload_rs_data_reply },
    { P_BARRIER, decode_payload_barrier },
    { P_BITMAP, NULL }, /* TODO: decode additional data */
    { P_COMPRESSED_BITMAP, NULL }, /* TODO: decode additional data */
    { P_UNPLUG_REMOTE, NULL },
    { P_DATA_REQUEST, decode_payload_data_request },
    { P_RS_DATA_REQUEST, decode_payload_data_request },
    { P_SYNC_PARAM, decode_payload_sync_param },
    { P_SYNC_PARAM89, decode_payload_sync_param },
    { P_PROTOCOL, decode_payload_protocol },
    { P_UUIDS, decode_payload_uuids },
    { P_SIZES, decode_payload_sizes },
    { P_STATE, decode_payload_state },
    { P_STATE_CHG_REQ, decode_payload_req_state },
    { P_SYNC_UUID, decode_payload_sync_uuid },
    { P_OV_REQUEST, decode_payload_data_request },
    { P_OV_REPLY, decode_payload_data_request }, /* TODO: decode additional data */
    { P_CSUM_RS_REQUEST, decode_payload_data_request }, /* TODO: decode additional data */
    { P_RS_THIN_REQ, decode_payload_data_request },
    { P_DELAY_PROBE, decode_payload_skip },
    { P_OUT_OF_SYNC, decode_payload_out_of_sync },
    { P_CONN_ST_CHG_REQ, decode_payload_req_state },
    { P_PROTOCOL_UPDATE, decode_payload_protocol }, /* TODO: decode additional data */
    { P_TWOPC_PREPARE, decode_payload_twopc },
    { P_TWOPC_PREP_RSZ, decode_payload_twopc },
    { P_TWOPC_ABORT, decode_payload_twopc },
    { P_DAGTAG, decode_payload_dagtag },
    { P_UUIDS110, decode_payload_uuids110 },
    { P_PEER_DAGTAG, decode_payload_peer_dagtag },
    { P_CURRENT_UUID, decode_payload_current_uuid },
    { P_TWOPC_COMMIT, decode_payload_twopc },
    { P_TRIM, decode_payload_data_size },
    { P_ZEROES, decode_payload_data_size },
    { P_RS_DEALLOCATED, decode_payload_rs_deallocated },
    { P_WSAME, decode_payload_data_wsame },

    { P_PING, NULL },
    { P_PING_ACK, NULL },
    { P_RECV_ACK, decode_payload_block_ack },
    { P_WRITE_ACK, decode_payload_block_ack },
    { P_RS_WRITE_ACK, decode_payload_block_ack },
    { P_SUPERSEDED, decode_payload_block_ack },
    { P_NEG_ACK, decode_payload_block_ack },
    { P_NEG_DREPLY, decode_payload_block_ack },
    { P_NEG_RS_DREPLY, decode_payload_block_ack },
    { P_OV_RESULT, decode_payload_block_ack },
    { P_BARRIER_ACK, decode_payload_barrier_ack },
    { P_CONFIRM_STABLE, decode_payload_confirm_stable },
    { P_STATE_CHG_REPLY, decode_payload_rq_s_reply },
    { P_RS_IS_IN_SYNC, decode_payload_block_ack },
    { P_DELAY_PROBE, decode_payload_skip },
    { P_RS_CANCEL, decode_payload_block_ack },
    { P_CONN_ST_CHG_REPLY, decode_payload_rq_s_reply },
    { P_RETRY_WRITE, decode_payload_block_ack },
    { P_PEER_ACK, decode_payload_peer_ack },
    { P_PEERS_IN_SYNC, decode_payload_peers_in_sync },
    { P_TWOPC_YES, decode_payload_twopc_reply },
    { P_TWOPC_NO, decode_payload_twopc_reply },
    { P_TWOPC_RETRY, decode_payload_twopc_reply },
};


void proto_register_drbd(void);
void proto_reg_handoff_drbd(void);

static dissector_handle_t drbd_handle;

static int proto_drbd = -1;

static int hf_drbd_command = -1;
static int hf_drbd_length = -1;
static int hf_drbd_volume = -1;
static int hf_drbd_auth_challenge_nonce = -1;
static int hf_drbd_auth_response_hash = -1;
static int hf_drbd_sector = -1;
static int hf_drbd_block_id = -1;
static int hf_drbd_seq_num = -1;
static int hf_drbd_dp_flags = -1;
static int hf_drbd_data = -1;
static int hf_drbd_size = -1;
static int hf_drbd_blksize = -1;
static int hf_drbd_protocol_min = -1;
static int hf_drbd_feature_flags = -1;
static int hf_drbd_protocol_max = -1;
static int hf_drbd_sender_node_id = -1;
static int hf_drbd_receiver_node_id = -1;
static int hf_drbd_barrier = -1;
static int hf_drbd_set_size = -1;
static int hf_drbd_oldest_block_id = -1;
static int hf_drbd_youngest_block_id = -1;
static int hf_drbd_resync_rate = -1;
static int hf_drbd_verify_alg = -1;
static int hf_drbd_csums_alg = -1;
static int hf_drbd_c_plan_ahead = -1;
static int hf_drbd_c_delay_target = -1;
static int hf_drbd_c_fill_target = -1;
static int hf_drbd_c_max_rate = -1;
static int hf_drbd_protocol = -1;
static int hf_drbd_after_sb_0p = -1;
static int hf_drbd_after_sb_1p = -1;
static int hf_drbd_after_sb_2p = -1;
static int hf_drbd_conn_flags = -1;
static int hf_drbd_two_primaries = -1;
static int hf_drbd_integrity_alg = -1;
static int hf_drbd_current_uuid = -1;
static int hf_drbd_bitmap_uuid = -1;
static int hf_drbd_history_uuid_list = -1;
static int hf_drbd_history_uuid = -1;
static int hf_drbd_dirty_bits = -1;
static int hf_drbd_uuid_flags = -1;
static int hf_drbd_node_mask = -1;
static int hf_drbd_bitmap_uuids_mask = -1;
static int hf_drbd_uuid = -1;
static int hf_drbd_weak_nodes = -1;
static int hf_drbd_physical_block_size = -1;
static int hf_drbd_logical_block_size = -1;
static int hf_drbd_alignment_offset = -1;
static int hf_drbd_io_min = -1;
static int hf_drbd_io_opt = -1;
static int hf_drbd_discard_enabled = -1;
static int hf_drbd_discard_zeroes_data = -1;
static int hf_drbd_write_same_capable = -1;
static int hf_drbd_d_size = -1;
static int hf_drbd_u_size = -1;
static int hf_drbd_c_size = -1;
static int hf_drbd_max_bio_size = -1;
static int hf_drbd_queue_order_type = -1;
static int hf_drbd_dds_flags = -1;
static int hf_drbd_state = -1;
static int hf_drbd_mask = -1;
static int hf_drbd_val = -1;
static int hf_drbd_retcode = -1;
static int hf_drbd_tid = -1;
static int hf_drbd_initiator_node_id = -1;
static int hf_drbd_target_node_id = -1;
static int hf_drbd_nodes_to_reach = -1;
static int hf_drbd_reachable_nodes = -1;
static int hf_drbd_offset = -1;
static int hf_drbd_dagtag = -1;
static int hf_drbd_node_id = -1;

static int hf_drbd_state_role = -1;
static int hf_drbd_state_peer = -1;
static int hf_drbd_state_conn = -1;
static int hf_drbd_state_disk = -1;
static int hf_drbd_state_pdsk = -1;
static int hf_drbd_state_susp = -1;
static int hf_drbd_state_aftr_isp = -1;
static int hf_drbd_state_peer_isp = -1;
static int hf_drbd_state_user_isp = -1;
static int hf_drbd_state_susp_nod = -1;
static int hf_drbd_state_susp_fen = -1;
static int hf_drbd_state_quorum = -1;

static int hf_drbd_uuid_flag_discard_my_data = -1;
static int hf_drbd_uuid_flag_crashed_primary = -1;
static int hf_drbd_uuid_flag_inconsistent = -1;
static int hf_drbd_uuid_flag_skip_initial_sync = -1;
static int hf_drbd_uuid_flag_new_datagen = -1;
static int hf_drbd_uuid_flag_stable = -1;
static int hf_drbd_uuid_flag_got_stable = -1;
static int hf_drbd_uuid_flag_resync = -1;
static int hf_drbd_uuid_flag_reconnect = -1;
static int hf_drbd_uuid_flag_diskless_primary = -1;
static int hf_drbd_uuid_flag_primary_lost_quorum = -1;

static int hf_drbd_dp_hardbarrier = -1;
static int hf_drbd_dp_rw_sync = -1;
static int hf_drbd_dp_may_set_in_sync = -1;
static int hf_drbd_dp_unplug = -1;
static int hf_drbd_dp_fua = -1;
static int hf_drbd_dp_flush = -1;
static int hf_drbd_dp_discard = -1;
static int hf_drbd_dp_send_receive_ack = -1;
static int hf_drbd_dp_send_write_ack = -1;
static int hf_drbd_dp_wsame = -1;
static int hf_drbd_dp_zeroes = -1;

static gint ett_drbd = -1;
static gint ett_drbd_state = -1;
static gint ett_drbd_uuid_flags = -1;
static gint ett_drbd_history_uuids = -1;
static gint ett_drbd_data_flags = -1;

static const int *state_fields[] = {
    &hf_drbd_state_role,
    &hf_drbd_state_peer,
    &hf_drbd_state_conn,
    &hf_drbd_state_disk,
    &hf_drbd_state_pdsk,
    &hf_drbd_state_susp,
    &hf_drbd_state_aftr_isp,
    &hf_drbd_state_peer_isp,
    &hf_drbd_state_user_isp,
    &hf_drbd_state_susp_nod,
    &hf_drbd_state_susp_fen,
    &hf_drbd_state_quorum,
    NULL
};

static const int *uuid_flag_fields[] = {
    &hf_drbd_uuid_flag_discard_my_data,
    &hf_drbd_uuid_flag_crashed_primary,
    &hf_drbd_uuid_flag_inconsistent,
    &hf_drbd_uuid_flag_skip_initial_sync,
    &hf_drbd_uuid_flag_new_datagen,
    &hf_drbd_uuid_flag_stable,
    &hf_drbd_uuid_flag_got_stable,
    &hf_drbd_uuid_flag_resync,
    &hf_drbd_uuid_flag_reconnect,
    &hf_drbd_uuid_flag_diskless_primary,
    &hf_drbd_uuid_flag_primary_lost_quorum,
    NULL
};

static const int *data_flag_fields[] = {
    &hf_drbd_dp_hardbarrier,
    &hf_drbd_dp_rw_sync,
    &hf_drbd_dp_may_set_in_sync,
    &hf_drbd_dp_unplug,
    &hf_drbd_dp_fua,
    &hf_drbd_dp_flush,
    &hf_drbd_dp_discard,
    &hf_drbd_dp_send_receive_ack,
    &hf_drbd_dp_send_write_ack,
    &hf_drbd_dp_wsame,
    &hf_drbd_dp_zeroes,
    NULL
};

#define CHALLENGE_LEN 64

static gboolean is_bit_set_64(guint64 value, int bit) {
    return !!(value & (G_GUINT64_CONSTANT(1) << bit));
}

/*
 * Length of the frame header.
 */
#define DRBD_FRAME_HEADER_80_LEN 8
#define DRBD_FRAME_HEADER_95_LEN 8
#define DRBD_FRAME_HEADER_100_LEN 16

#define DRBD_MAGIC 0x83740267
#define DRBD_MAGIC_BIG 0x835a
#define DRBD_MAGIC_100 0x8620ec20

static guint get_drbd_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset, void *data _U_)
{
    guint32 magic32;
    guint16 magic16;

    magic32 = tvb_get_ntohl(tvb, offset);

    if (magic32 == DRBD_MAGIC)
        return DRBD_FRAME_HEADER_80_LEN + tvb_get_ntohs(tvb, offset + 6);

    if (tvb_reported_length_remaining(tvb, offset) >= DRBD_FRAME_HEADER_100_LEN && magic32 == DRBD_MAGIC_100)
        return DRBD_FRAME_HEADER_100_LEN + tvb_get_ntohl(tvb, offset + 8);

    magic16 = tvb_get_ntohs(tvb, offset);

    if (magic16 == DRBD_MAGIC_BIG)
        return DRBD_FRAME_HEADER_95_LEN + tvb_get_ntohl(tvb, offset + 4);

    return 0;
}

static int dissect_drbd_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree _U_, void* data _U_)
{
    dissect_drbd_message(tvb, pinfo, tree);
    return tvb_reported_length(tvb);
}

static int dissect_drbd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
{
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "DRBD");
    tcp_dissect_pdus(tvb, pinfo, tree, TRUE, DRBD_FRAME_HEADER_80_LEN,
            get_drbd_pdu_len, dissect_drbd_pdu, data);
    return tvb_reported_length(tvb);
}

static gboolean test_drbd_protocol(tvbuff_t *tvb, packet_info *pinfo,
        proto_tree *tree, void *data _U_)
{
    guint reported_length = tvb_reported_length(tvb);
    if (reported_length < DRBD_FRAME_HEADER_80_LEN) {
        return FALSE;
    }

    gboolean match = FALSE;
    guint32 magic32 = tvb_get_ntohl(tvb, 0);

    if (magic32 == DRBD_MAGIC)
        match = TRUE;
    else if (reported_length >= DRBD_FRAME_HEADER_100_LEN && magic32 == DRBD_MAGIC_100)
        match = TRUE;
    else {
        guint16 magic16 = tvb_get_ntohs(tvb, 0);
        if (magic16 == DRBD_MAGIC_BIG)
            match = TRUE;
    }

    if (match) {
        conversation_t *conversation = find_or_create_conversation(pinfo);
        conversation_set_dissector(conversation, drbd_handle);
        dissect_drbd(tvb, pinfo, tree, data);
    }

    return match;
}

/**
 * Returns buffer containing the payload.
 */
static tvbuff_t *decode_header(tvbuff_t *tvb, proto_tree *pt, guint16 *command)
{
    guint32 magic32;
    guint16 magic16;

    magic32 = tvb_get_ntohl(tvb, 0);

    if (magic32 == DRBD_MAGIC) {
        *command = tvb_get_ntohs(tvb, 4);

        proto_tree_add_item(pt, hf_drbd_command, tvb, 4, 2, ENC_BIG_ENDIAN);
        proto_tree_add_item(pt, hf_drbd_length, tvb, 6, 2, ENC_BIG_ENDIAN);

        return tvb_new_subset_remaining(tvb, DRBD_FRAME_HEADER_80_LEN);
    }

    if (tvb_reported_length(tvb) >= DRBD_FRAME_HEADER_100_LEN && magic32 == DRBD_MAGIC_100) {
        *command = tvb_get_ntohs(tvb, 6);

        proto_tree_add_item(pt, hf_drbd_volume, tvb, 4, 2, ENC_BIG_ENDIAN);
        proto_tree_add_item(pt, hf_drbd_command, tvb, 6, 2, ENC_BIG_ENDIAN);
        proto_tree_add_item(pt, hf_drbd_length, tvb, 8, 4, ENC_BIG_ENDIAN);

        return tvb_new_subset_remaining(tvb, DRBD_FRAME_HEADER_100_LEN);
    }

    magic16 = tvb_get_ntohs(tvb, 0);

    if (magic16 == DRBD_MAGIC_BIG) {
        *command = tvb_get_ntohs(tvb, 2);

        proto_tree_add_item(pt, hf_drbd_command, tvb, 2, 2, ENC_BIG_ENDIAN);
        proto_tree_add_item(pt, hf_drbd_length, tvb, 4, 4, ENC_BIG_ENDIAN);

        return tvb_new_subset_remaining(tvb, DRBD_FRAME_HEADER_95_LEN);
    }

    return NULL;
}

static void dissect_drbd_message(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    proto_tree      *drbd_tree;
    proto_item      *ti;
    guint16         command = -1;

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "DRBD");
    col_clear(pinfo->cinfo, COL_INFO);

    ti = proto_tree_add_item(tree, proto_drbd, tvb, 0, -1, ENC_NA);
    drbd_tree = proto_item_add_subtree(ti, ett_drbd);

    tvbuff_t *payload_tvb = decode_header(tvb, drbd_tree, &command);

    if (!payload_tvb)
        return;

    /*
     * Indicate what kind of message this is.
     */
    const gchar *packet_name = val_to_str(command, packet_names, "Unknown (0x%02x)");
    const gchar *info_text = col_get_text(pinfo->cinfo, COL_INFO);
    if (!info_text || !info_text[0]) {
        col_append_ports(pinfo->cinfo, COL_INFO, PT_TCP, pinfo->srcport, pinfo->destport);
        col_append_fstr(pinfo->cinfo, COL_INFO, " [%s]", packet_name);
    } else {
        col_append_sep_fstr(pinfo->cinfo, COL_INFO, " ", "[%s]", packet_name);
    }
    col_set_fence(pinfo->cinfo, COL_INFO);

    if (tree == NULL)
        return;

    proto_item_set_text(ti, "DRBD [%s]", packet_name);

    const value_payload_decoder *payload_decoder = NULL;
    for (unsigned int i = 0; i < array_length(payload_decoders); i++) {
        if (payload_decoders[i].value == command) {
            payload_decoder = &payload_decoders[i];
            break;
        }
    }

    if (payload_decoder && payload_decoder->decoder_fn)
        (*payload_decoder->decoder_fn) (payload_tvb, drbd_tree);
}

static void decode_payload_connection_features(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_protocol_min, tvb, 0, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_feature_flags, tvb, 4, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_protocol_max, tvb, 8, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_sender_node_id, tvb, 12, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_receiver_node_id, tvb, 16, 4, ENC_BIG_ENDIAN);
}

static void decode_payload_auth_challenge(tvbuff_t *tvb _U_, proto_tree *tree _U_)
{
    proto_tree_add_bytes_format(tree, hf_drbd_auth_challenge_nonce, tvb, 0, CHALLENGE_LEN, NULL, "Nonce");
}

static void decode_payload_auth_response(tvbuff_t *tvb _U_, proto_tree *tree _U_)
{
    proto_tree_add_bytes_format(tree, hf_drbd_auth_response_hash, tvb, 0, -1, NULL, "Hash");
}

static void decode_data_common(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_sector, tvb, 0, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_block_id, tvb, 8, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_seq_num, tvb, 16, 4, ENC_BIG_ENDIAN);
    proto_tree_add_bitmask(tree, tvb, 20, hf_drbd_dp_flags, ett_drbd_data_flags, data_flag_fields, ENC_BIG_ENDIAN);
}

static void decode_data_remaining(tvbuff_t *tvb, proto_tree *tree, guint offset)
{
    guint nbytes = tvb_reported_length_remaining(tvb, offset);
    proto_tree_add_bytes_format(tree, hf_drbd_data, tvb, offset,
            -1, NULL, "Data (%u byte%s)", nbytes, plurality(nbytes, "", "s"));
}

static void decode_payload_data(tvbuff_t *tvb, proto_tree *tree)
{
    decode_data_common(tvb, tree);
    decode_data_remaining(tvb, tree, 24);
}

static void decode_payload_data_reply(tvbuff_t *tvb, proto_tree *tree)
{
    decode_data_common(tvb, tree);
    decode_data_remaining(tvb, tree, 24);
}

static void decode_payload_rs_data_reply(tvbuff_t *tvb, proto_tree *tree)
{
    decode_data_common(tvb, tree);
    decode_data_remaining(tvb, tree, 24);
}

static void decode_payload_barrier(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_barrier, tvb, 0, 4, ENC_BIG_ENDIAN);
}

static void decode_payload_data_request(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_sector, tvb, 0, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_block_id, tvb, 8, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_blksize, tvb, 16, 4, ENC_BIG_ENDIAN);
}

static void decode_payload_sync_param(tvbuff_t *tvb, proto_tree *tree)
{
    guint length = tvb_reported_length(tvb);
    guint offset = 0;

    proto_tree_add_item(tree, hf_drbd_resync_rate, tvb, offset, 4, ENC_BIG_ENDIAN);
    offset += 4;
    proto_tree_add_item(tree, hf_drbd_verify_alg, tvb, offset, DRBD_STRING_MAX, ENC_ASCII | ENC_NA);
    offset += DRBD_STRING_MAX;

    if (length >= offset + DRBD_STRING_MAX) {
        proto_tree_add_item(tree, hf_drbd_csums_alg, tvb, offset, DRBD_STRING_MAX, ENC_ASCII | ENC_NA);
        offset += DRBD_STRING_MAX;
    }

    if (length >= offset + 16) {
        proto_tree_add_item(tree, hf_drbd_c_plan_ahead, tvb, offset, 4, ENC_BIG_ENDIAN);
        proto_tree_add_item(tree, hf_drbd_c_delay_target, tvb, offset + 4, 4, ENC_BIG_ENDIAN);
        proto_tree_add_item(tree, hf_drbd_c_fill_target, tvb, offset + 8, 4, ENC_BIG_ENDIAN);
        proto_tree_add_item(tree, hf_drbd_c_max_rate, tvb, offset + 12, 4, ENC_BIG_ENDIAN);
    }
}

static void decode_payload_protocol(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_protocol, tvb, 0, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_after_sb_0p, tvb, 4, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_after_sb_1p, tvb, 8, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_after_sb_2p, tvb, 12, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_conn_flags, tvb, 16, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_two_primaries, tvb, 20, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_integrity_alg, tvb, 24, -1, ENC_ASCII | ENC_NA);
}

static void decode_payload_uuids(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_current_uuid, tvb, 0, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_bitmap_uuid, tvb, 8, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_history_uuid, tvb, 16, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_history_uuid, tvb, 24, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_dirty_bits, tvb, 32, 8, ENC_BIG_ENDIAN);
    proto_tree_add_bitmask(tree, tvb, 40, hf_drbd_uuid_flags, ett_drbd_uuid_flags, uuid_flag_fields, ENC_BIG_ENDIAN);
}

static void decode_payload_sizes(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_d_size, tvb, 0, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_u_size, tvb, 8, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_c_size, tvb, 16, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_max_bio_size, tvb, 24, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_queue_order_type, tvb, 28, 2, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_dds_flags, tvb, 30, 2, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_physical_block_size, tvb, 32, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_logical_block_size, tvb, 36, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_alignment_offset, tvb, 40, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_io_min, tvb, 44, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_io_opt, tvb, 48, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_discard_enabled, tvb, 52, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_discard_zeroes_data, tvb, 53, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_write_same_capable, tvb, 54, 1, ENC_BIG_ENDIAN);
}

static void decode_payload_state(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_bitmask(tree, tvb, 0, hf_drbd_state, ett_drbd_state, state_fields, ENC_BIG_ENDIAN);
}

static void decode_payload_req_state(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_mask, tvb, 0, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_val, tvb, 4, 4, ENC_BIG_ENDIAN);
}

static void decode_payload_sync_uuid(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_uuid, tvb, 0, 8, ENC_BIG_ENDIAN);
}

static void decode_payload_skip(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_seq_num, tvb, 0, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_offset, tvb, 4, 4, ENC_BIG_ENDIAN);
}

static void decode_payload_out_of_sync(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_sector, tvb, 0, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_blksize, tvb, 8, 4, ENC_BIG_ENDIAN);
}

static void decode_payload_twopc(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_tid, tvb, 0, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_initiator_node_id, tvb, 4, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_target_node_id, tvb, 8, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_nodes_to_reach, tvb, 12, 8, ENC_BIG_ENDIAN);
    /* TODO: Decode further fields based on type */
}

static void decode_payload_dagtag(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_dagtag, tvb, 0, 8, ENC_BIG_ENDIAN);
}

static void decode_payload_uuids110(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_current_uuid, tvb, 0, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_dirty_bits, tvb, 8, 8, ENC_BIG_ENDIAN);
    proto_tree_add_bitmask(tree, tvb, 16, hf_drbd_uuid_flags, ett_drbd_uuid_flags, uuid_flag_fields, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_node_mask, tvb, 24, 8, ENC_BIG_ENDIAN);

    guint64 bitmap_uuids_mask;
    proto_tree_add_item_ret_uint64(tree, hf_drbd_bitmap_uuids_mask, tvb, 32, 8, ENC_BIG_ENDIAN, &bitmap_uuids_mask);

    guint offset = 40;
    for (int i = 0; i < 64; i++) {
        if (is_bit_set_64(bitmap_uuids_mask, i)) {
            guint64 bitmap_uuid = tvb_get_ntoh64(tvb, offset);
            proto_tree_add_uint64_format(tree, hf_drbd_bitmap_uuid, tvb, offset, 8, bitmap_uuid,
                    "Bitmap UUID for node %d: 0x%016" G_GINT64_MODIFIER "x", i, bitmap_uuid);
            offset += 8;
        }
    }

    proto_item *history_uuids = proto_tree_add_item(tree, hf_drbd_history_uuid_list, tvb, offset, -1, ENC_NA);
    proto_tree *history_tree = proto_item_add_subtree(history_uuids, ett_drbd_history_uuids);
    guint total_length = tvb_reported_length(tvb);
    while (offset < total_length) {
        proto_tree_add_item(history_tree, hf_drbd_history_uuid, tvb, offset, 8, ENC_BIG_ENDIAN);
        offset += 8;
    }
}

static void decode_payload_peer_dagtag(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_dagtag, tvb, 0, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_node_id, tvb, 8, 4, ENC_BIG_ENDIAN);
}

static void decode_payload_current_uuid(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_uuid, tvb, 0, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_weak_nodes, tvb, 8, 8, ENC_BIG_ENDIAN);
}

static void decode_payload_data_size(tvbuff_t *tvb, proto_tree *tree)
{
    decode_data_common(tvb, tree);
    proto_tree_add_item(tree, hf_drbd_size, tvb, 24, 4, ENC_BIG_ENDIAN);
}

static void decode_payload_data_wsame(tvbuff_t *tvb, proto_tree *tree)
{
    decode_data_common(tvb, tree);
    proto_tree_add_item(tree, hf_drbd_size, tvb, 24, 4, ENC_BIG_ENDIAN);
    decode_data_remaining(tvb, tree, 28);
}

static void decode_payload_rs_deallocated(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_sector, tvb, 0, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_blksize, tvb, 8, 4, ENC_BIG_ENDIAN);
}

static void decode_payload_block_ack(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_sector, tvb, 0, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_block_id, tvb, 8, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_blksize, tvb, 16, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_seq_num, tvb, 20, 4, ENC_BIG_ENDIAN);
}

static void decode_payload_barrier_ack(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_barrier, tvb, 0, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_set_size, tvb, 4, 4, ENC_BIG_ENDIAN);
}

static void decode_payload_confirm_stable(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_oldest_block_id, tvb, 0, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_youngest_block_id, tvb, 8, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_set_size, tvb, 16, 4, ENC_BIG_ENDIAN);
}

static void decode_payload_rq_s_reply(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_retcode, tvb, 0, 4, ENC_BIG_ENDIAN);
}

static void decode_payload_peer_ack(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_node_mask, tvb, 0, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_dagtag, tvb, 8, 8, ENC_BIG_ENDIAN);
}

static void decode_payload_peers_in_sync(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_sector, tvb, 0, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_node_mask, tvb, 8, 8, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_size, tvb, 16, 4, ENC_BIG_ENDIAN);
}

static void decode_payload_twopc_reply(tvbuff_t *tvb, proto_tree *tree)
{
    proto_tree_add_item(tree, hf_drbd_tid, tvb, 0, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_initiator_node_id, tvb, 4, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(tree, hf_drbd_reachable_nodes, tvb, 8, 8, ENC_BIG_ENDIAN);
    /* TODO: Decode further fields based on type */
}

static void format_node_mask(gchar *s, guint64 value)
{
    if (!value) {
        g_strlcpy(s, "<none>", ITEM_LABEL_LENGTH);
        return;
    }

    int written = 0;
    int run_start = -1;
    for (int i = 0; i < 64 && written < ITEM_LABEL_LENGTH; i++) {
        gboolean is_set = is_bit_set_64(value, i);

        int run_end;
        if (!is_set) {
            run_end = i;
        } else if (i == 63) {
            if (run_start == -1)
                run_start = i;
            run_end = 64;
        } else {
            run_end = -1;
        }

        if (run_start != -1 && run_end != -1) {
            int run_length = run_end - run_start;
            const char *sep = written ? ", " : "";

            if (run_length == 1)
                written += g_snprintf(s + written, ITEM_LABEL_LENGTH - written, "%s%d", sep, run_start);
            else if (run_length == 2)
                written += g_snprintf(s + written, ITEM_LABEL_LENGTH - written, "%s%d, %d", sep, run_start, run_start + 1);
            else
                written += g_snprintf(s + written, ITEM_LABEL_LENGTH - written, "%s%d - %d", sep, run_start, run_end - 1);
        }

        if (!is_set)
            run_start = -1;
        else if (run_start == -1)
            run_start = i;
    }
}

void proto_register_drbd(void)
{
    static hf_register_info hf[] = {
        { &hf_drbd_command, { "Command", "drbd.command", FT_UINT16, BASE_HEX, VALS(packet_names), 0x0, NULL, HFILL }},
        { &hf_drbd_length, { "Payload length", "drbd.length", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_volume, { "Volume", "drbd.volume", FT_INT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_auth_challenge_nonce, { "Nonce", "drbd.auth_nonce", FT_BYTES, BASE_NO_DISPLAY_VALUE, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_auth_response_hash, { "Hash", "drbd.auth_hash", FT_BYTES, BASE_NO_DISPLAY_VALUE, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_sector, { "Sector", "drbd.sector", FT_UINT64, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_block_id, { "Block ID", "drbd.block_id", FT_UINT64, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_seq_num, { "Sequence number", "drbd.seq_num", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_dp_flags, { "Data flags", "drbd.dp_flags", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_data, { "Data", "drbd.data", FT_BYTES, BASE_NO_DISPLAY_VALUE, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_size, { "size", "drbd.size", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_blksize, { "blksize", "drbd.blksize", FT_UINT32, BASE_DEC_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_protocol_min, { "protocol_min", "drbd.protocol_min", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_feature_flags, { "feature_flags", "drbd.feature_flags", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_protocol_max, { "protocol_max", "drbd.protocol_max", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_sender_node_id, { "sender_node_id", "drbd.sender_node_id", FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_receiver_node_id, { "receiver_node_id", "drbd.receiver_node_id", FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_barrier, { "barrier", "drbd.barrier", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_set_size, { "set_size", "drbd.set_size", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_oldest_block_id, { "oldest_block_id", "drbd.oldest_block_id", FT_UINT64, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_youngest_block_id, { "youngest_block_id", "drbd.youngest_block_id", FT_UINT64, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_resync_rate, { "resync_rate", "drbd.resync_rate", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_verify_alg, { "verify_alg", "drbd.verify_alg", FT_STRINGZ, STR_ASCII, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_csums_alg, { "csums_alg", "drbd.csums_alg", FT_STRINGZ, STR_ASCII, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_c_plan_ahead, { "c_plan_ahead", "drbd.c_plan_ahead", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_c_delay_target, { "c_delay_target", "drbd.c_delay_target", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_c_fill_target, { "c_fill_target", "drbd.c_fill_target", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_c_max_rate, { "c_max_rate", "drbd.c_max_rate", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_protocol, { "protocol", "drbd.protocol", FT_UINT32, BASE_HEX, VALS(protocol_names), 0x0, NULL, HFILL }},
        { &hf_drbd_after_sb_0p, { "after_sb_0p", "drbd.after_sb_0p", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_after_sb_1p, { "after_sb_1p", "drbd.after_sb_1p", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_after_sb_2p, { "after_sb_2p", "drbd.after_sb_2p", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_conn_flags, { "conn_flags", "drbd.conn_flags", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_two_primaries, { "two_primaries", "drbd.two_primaries", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_integrity_alg, { "integrity_alg", "drbd.integrity_alg", FT_STRINGZ, STR_ASCII, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_current_uuid, { "Current UUID", "drbd.current_uuid", FT_UINT64, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_bitmap_uuid, { "Bitmap UUID", "drbd.bitmap_uuid", FT_UINT64, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_history_uuid_list, { "History UUIDs", "drbd.history_uuids", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_history_uuid, { "History UUID", "drbd.history_uuids", FT_UINT64, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_dirty_bits, { "Dirty bits", "drbd.dirty_bits", FT_UINT64, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_uuid_flags, { "UUID flags", "drbd.uuid_flags", FT_UINT64, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_node_mask, { "Nodes", "drbd.node_mask", FT_UINT64, BASE_CUSTOM, format_node_mask, 0x0, NULL, HFILL }},
        { &hf_drbd_bitmap_uuids_mask, { "Bitmap UUID nodes", "drbd.bitmap_uuids_mask", FT_UINT64, BASE_CUSTOM, format_node_mask, 0x0, NULL, HFILL }},
        { &hf_drbd_uuid, { "uuid", "drbd.uuid", FT_UINT64, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_weak_nodes, { "weak_nodes", "drbd.weak_nodes", FT_UINT64, BASE_CUSTOM, format_node_mask, 0x0, NULL, HFILL }},
        { &hf_drbd_physical_block_size, { "physical_block_size", "drbd.physical_block_size", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_logical_block_size, { "logical_block_size", "drbd.logical_block_size", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_alignment_offset, { "alignment_offset", "drbd.alignment_offset", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_io_min, { "io_min", "drbd.io_min", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_io_opt, { "io_opt", "drbd.io_opt", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_discard_enabled, { "discard_enabled", "drbd.discard_enabled", FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_discard_zeroes_data, { "discard_zeroes_data", "drbd.discard_zeroes_data", FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_write_same_capable, { "write_same_capable", "drbd.write_same_capable", FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_d_size, { "d_size", "drbd.d_size", FT_UINT64, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_u_size, { "u_size", "drbd.u_size", FT_UINT64, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_c_size, { "c_size", "drbd.c_size", FT_UINT64, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_max_bio_size, { "max_bio_size", "drbd.max_bio_size", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_queue_order_type, { "queue_order_type", "drbd.queue_order_type", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_dds_flags, { "dds_flags", "drbd.dds_flags", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_state, { "state", "drbd.state", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_mask, { "mask", "drbd.mask", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_val, { "val", "drbd.val", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_retcode, { "retcode", "drbd.retcode", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_tid, { "tid", "drbd.tid", FT_UINT32, BASE_DEC_HEX, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_initiator_node_id, { "initiator_node_id", "drbd.initiator_node_id", FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_target_node_id, { "target_node_id", "drbd.target_node_id", FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_nodes_to_reach, { "nodes_to_reach", "drbd.nodes_to_reach", FT_UINT64, BASE_CUSTOM, format_node_mask, 0x0, NULL, HFILL }},
        { &hf_drbd_reachable_nodes, { "reachable_nodes", "drbd.reachable_nodes", FT_UINT64, BASE_CUSTOM, format_node_mask, 0x0, NULL, HFILL }},
        { &hf_drbd_offset, { "offset", "drbd.offset", FT_UINT32, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_dagtag, { "dagtag", "drbd.dagtag", FT_UINT64, BASE_HEX_DEC, NULL, 0x0, NULL, HFILL }},
        { &hf_drbd_node_id, { "node_id", "drbd.node_id", FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},

        { &hf_drbd_state_role, { "role", "drbd.state.role", FT_UINT32, BASE_DEC, VALS(role_names), STATE_ROLE, NULL, HFILL }},
        { &hf_drbd_state_peer, { "peer", "drbd.state.peer", FT_UINT32, BASE_DEC, VALS(role_names), STATE_PEER, NULL, HFILL }},
        { &hf_drbd_state_conn, { "conn", "drbd.state.conn", FT_UINT32, BASE_DEC, VALS(connection_state_names), STATE_CONN, NULL, HFILL }},
        { &hf_drbd_state_disk, { "disk", "drbd.state.disk", FT_UINT32, BASE_DEC, VALS(disk_state_names), STATE_DISK, NULL, HFILL }},
        { &hf_drbd_state_pdsk, { "pdsk", "drbd.state.pdsk", FT_UINT32, BASE_DEC, VALS(disk_state_names), STATE_PDSK, NULL, HFILL }},
        { &hf_drbd_state_susp, { "susp", "drbd.state.susp", FT_BOOLEAN, 32, NULL, STATE_SUSP, NULL, HFILL }},
        { &hf_drbd_state_aftr_isp, { "aftr_isp", "drbd.state.aftr_isp", FT_BOOLEAN, 32, NULL, STATE_AFTR_ISP, NULL, HFILL }},
        { &hf_drbd_state_peer_isp, { "peer_isp", "drbd.state.peer_isp", FT_BOOLEAN, 32, NULL, STATE_PEER_ISP, NULL, HFILL }},
        { &hf_drbd_state_user_isp, { "user_isp", "drbd.state.user_isp", FT_BOOLEAN, 32, NULL, STATE_USER_ISP, NULL, HFILL }},
        { &hf_drbd_state_susp_nod, { "susp_nod", "drbd.state.susp_nod", FT_BOOLEAN, 32, NULL, STATE_SUSP_NOD, NULL, HFILL }},
        { &hf_drbd_state_susp_fen, { "susp_fen", "drbd.state.susp_fen", FT_BOOLEAN, 32, NULL, STATE_SUSP_FEN, NULL, HFILL }},
        { &hf_drbd_state_quorum, { "quorum", "drbd.state.quorum", FT_BOOLEAN, 32, NULL, STATE_QUORUM, NULL, HFILL }},

        { &hf_drbd_uuid_flag_discard_my_data, { "discard_my_data", "drbd.uuid_flag.discard_my_data", FT_BOOLEAN, 64, NULL, UUID_FLAG_DISCARD_MY_DATA, NULL, HFILL }},
        { &hf_drbd_uuid_flag_crashed_primary, { "crashed_primary", "drbd.uuid_flag.crashed_primary", FT_BOOLEAN, 64, NULL, UUID_FLAG_CRASHED_PRIMARY, NULL, HFILL }},
        { &hf_drbd_uuid_flag_inconsistent, { "inconsistent", "drbd.uuid_flag.inconsistent", FT_BOOLEAN, 64, NULL, UUID_FLAG_INCONSISTENT, NULL, HFILL }},
        { &hf_drbd_uuid_flag_skip_initial_sync, { "skip_initial_sync", "drbd.uuid_flag.skip_initial_sync", FT_BOOLEAN, 64, NULL, UUID_FLAG_SKIP_INITIAL_SYNC, NULL, HFILL }},
        { &hf_drbd_uuid_flag_new_datagen, { "new_datagen", "drbd.uuid_flag.new_datagen", FT_BOOLEAN, 64, NULL, UUID_FLAG_NEW_DATAGEN, NULL, HFILL }},
        { &hf_drbd_uuid_flag_stable, { "stable", "drbd.uuid_flag.stable", FT_BOOLEAN, 64, NULL, UUID_FLAG_STABLE, NULL, HFILL }},
        { &hf_drbd_uuid_flag_got_stable, { "got_stable", "drbd.uuid_flag.got_stable", FT_BOOLEAN, 64, NULL, UUID_FLAG_GOT_STABLE, NULL, HFILL }},
        { &hf_drbd_uuid_flag_resync, { "resync", "drbd.uuid_flag.resync", FT_BOOLEAN, 64, NULL, UUID_FLAG_RESYNC, NULL, HFILL }},
        { &hf_drbd_uuid_flag_reconnect, { "reconnect", "drbd.uuid_flag.reconnect", FT_BOOLEAN, 64, NULL, UUID_FLAG_RECONNECT, NULL, HFILL }},
        { &hf_drbd_uuid_flag_diskless_primary, { "diskless_primary", "drbd.uuid_flag.diskless_primary", FT_BOOLEAN, 64, NULL, UUID_FLAG_DISKLESS_PRIMARY, NULL, HFILL }},
        { &hf_drbd_uuid_flag_primary_lost_quorum, { "primary_lost_quorum", "drbd.uuid_flag.primary_lost_quorum", FT_BOOLEAN, 64, NULL, UUID_FLAG_PRIMARY_LOST_QUORUM, NULL, HFILL }},

        { &hf_drbd_dp_hardbarrier, { "hardbarrier", "drbd.dp_flag.hardbarrier", FT_BOOLEAN, 32, NULL, DP_HARDBARRIER, NULL, HFILL }},
        { &hf_drbd_dp_rw_sync, { "rw_sync", "drbd.dp_flag.rw_sync", FT_BOOLEAN, 32, NULL, DP_RW_SYNC, NULL, HFILL }},
        { &hf_drbd_dp_may_set_in_sync, { "may_set_in_sync", "drbd.dp_flag.may_set_in_sync", FT_BOOLEAN, 32, NULL, DP_MAY_SET_IN_SYNC, NULL, HFILL }},
        { &hf_drbd_dp_unplug, { "unplug", "drbd.dp_flag.unplug", FT_BOOLEAN, 32, NULL, DP_UNPLUG, NULL, HFILL }},
        { &hf_drbd_dp_fua, { "fua", "drbd.dp_flag.fua", FT_BOOLEAN, 32, NULL, DP_FUA, NULL, HFILL }},
        { &hf_drbd_dp_flush, { "flush", "drbd.dp_flag.flush", FT_BOOLEAN, 32, NULL, DP_FLUSH, NULL, HFILL }},
        { &hf_drbd_dp_discard, { "discard", "drbd.dp_flag.discard", FT_BOOLEAN, 32, NULL, DP_DISCARD, NULL, HFILL }},
        { &hf_drbd_dp_send_receive_ack, { "send_receive_ack", "drbd.dp_flag.send_receive_ack", FT_BOOLEAN, 32, NULL, DP_SEND_RECEIVE_ACK, NULL, HFILL }},
        { &hf_drbd_dp_send_write_ack, { "send_write_ack", "drbd.dp_flag.send_write_ack", FT_BOOLEAN, 32, NULL, DP_SEND_WRITE_ACK, NULL, HFILL }},
        { &hf_drbd_dp_wsame, { "wsame", "drbd.dp_flag.wsame", FT_BOOLEAN, 32, NULL, DP_WSAME, NULL, HFILL }},
        { &hf_drbd_dp_zeroes, { "zeroes", "drbd.dp_flag.zeroes", FT_BOOLEAN, 32, NULL, DP_ZEROES, NULL, HFILL }},
    };

    static gint *ett[] = {
        &ett_drbd,
        &ett_drbd_state,
        &ett_drbd_uuid_flags,
        &ett_drbd_history_uuids,
        &ett_drbd_data_flags,
    };

    proto_drbd = proto_register_protocol("DRBD Protocol", "DRBD", "drbd");
    proto_register_field_array(proto_drbd, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
}

void proto_reg_handoff_drbd(void)
{
    drbd_handle = create_dissector_handle(dissect_drbd, proto_drbd);
    heur_dissector_add("tcp", test_drbd_protocol, "DRBD over TCP", "drbd_tcp", proto_drbd, HEURISTIC_ENABLE);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */