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
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashMap;

use serde::{
    de::IntoDeserializer, Deserialize, Deserializer, Serialize, Serializer,
};

use crate::{
    deserializer::NumberAsString, BaseInterface, ErrorKind, Interface,
    InterfaceState, InterfaceType, MergedInterface, NmstateError,
};

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
/// Bond interface. When serializing or deserializing, the [BaseInterface] will
/// be flatted and [BondConfig] stored as `link-aggregation` section. The yaml
/// output [crate::NetworkState] containing an example bond interface:
/// ```yml
/// interfaces:
/// - name: bond99
///   type: bond
///   state: up
///   mac-address: 1A:24:D5:CA:76:54
///   mtu: 1500
///   min-mtu: 68
///   max-mtu: 65535
///   wait-ip: any
///   ipv4:
///     enabled: false
///   ipv6:
///     enabled: false
///   accept-all-mac-addresses: false
///   link-aggregation:
///     mode: balance-rr
///     options:
///       all_slaves_active: dropped
///       arp_all_targets: any
///       arp_interval: 0
///       arp_validate: none
///       downdelay: 0
///       lp_interval: 1
///       miimon: 100
///       min_links: 0
///       packets_per_slave: 1
///       primary_reselect: always
///       resend_igmp: 1
///       updelay: 0
///       use_carrier: true
///     port:
///     - eth1
///     - eth2
/// ```
pub struct BondInterface {
    #[serde(flatten)]
    /// Base interface. Flat during serializing.
    pub base: BaseInterface,
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "link-aggregation"
    )]
    /// Bond specific settings.
    pub bond: Option<BondConfig>,
}

impl Default for BondInterface {
    fn default() -> Self {
        let mut base = BaseInterface::new();
        base.iface_type = InterfaceType::Bond;
        Self { base, bond: None }
    }
}

impl BondInterface {
    // * Do not merge bond options from current when bond mode is changing
    pub(crate) fn special_merge(&mut self, desired: &Self, current: &Self) {
        if let Some(bond_conf) = self.bond.as_mut() {
            if let (Some(des_bond_conf), Some(cur_bond_conf)) =
                (desired.bond.as_ref(), current.bond.as_ref())
            {
                if des_bond_conf.mode != cur_bond_conf.mode {
                    bond_conf.options.clone_from(&des_bond_conf.options);
                }
            }
        }
    }

    fn drop_empty_arp_ip_target(&mut self) {
        if let Some(ref mut bond_conf) = self.bond {
            if let Some(ref mut bond_opts) = &mut bond_conf.options {
                if let Some(ref mut arp_ip_target) = bond_opts.arp_ip_target {
                    if arp_ip_target.is_empty() {
                        bond_opts.arp_ip_target = None;
                    }
                }
            }
        }
    }

    fn sort_ports(&mut self) {
        if let Some(ref mut bond_conf) = self.bond {
            if let Some(ref mut port_conf) = &mut bond_conf.port {
                port_conf.sort_unstable_by_key(|p| p.clone())
            }
        }
    }

    fn sort_ports_config(&mut self) {
        if let Some(ref mut bond_conf) = self.bond {
            if let Some(ref mut port_conf) = &mut bond_conf.ports_config {
                port_conf.sort_unstable_by_key(|p| p.name.clone())
            }
        }
    }

    pub(crate) fn sanitize(&mut self) -> Result<(), NmstateError> {
        self.sort_ports();
        self.sort_ports_config();
        self.drop_empty_arp_ip_target();
        self.make_ad_actor_system_mac_upper_case();
        self.check_overlap_queue_id()?;
        Ok(())
    }

    // In kernel code drivers/net/bonding/bond_options.c
    // bond_option_queue_id_set(), kernel is not allowing multiple bond port
    // holding the same queue ID, hence we raise error when queue id overlapped.
    fn check_overlap_queue_id(&self) -> Result<(), NmstateError> {
        let mut existing_qids: HashMap<u16, &str> = HashMap::new();
        if let Some(ports_conf) =
            self.bond.as_ref().and_then(|b| b.ports_config.as_deref())
        {
            for port_conf in ports_conf
                .iter()
                .filter(|p| p.queue_id.is_some() && p.queue_id != Some(0))
            {
                if let Some(queue_id) = port_conf.queue_id {
                    if let Some(exist_port_name) = existing_qids.get(&queue_id)
                    {
                        return Err(NmstateError::new(
                            ErrorKind::InvalidArgument,
                            format!(
                                "Port {} and {} of Bond {} are sharing the \
                                same queue-id which is not supported by \
                                linux kernel yet",
                                exist_port_name,
                                port_conf.name.as_str(),
                                self.base.name.as_str()
                            ),
                        ));
                    } else {
                        existing_qids.insert(queue_id, port_conf.name.as_str());
                    }
                }
            }
        }
        Ok(())
    }

    // Return None when desire state does not mention ports
    pub(crate) fn ports(&self) -> Option<Vec<&str>> {
        let config = self.bond.clone().unwrap_or_default();
        if config.port.is_some() {
            self.bond
                .as_ref()
                .and_then(|bond_conf| bond_conf.port.as_ref())
                .map(|ports| {
                    ports.as_slice().iter().map(|p| p.as_str()).collect()
                })
        } else {
            self.bond
                .as_ref()
                .and_then(|bond_conf| bond_conf.ports_config.as_ref())
                .map(|ports| {
                    ports.as_slice().iter().map(|p| p.name.as_str()).collect()
                })
        }
    }

    pub(crate) fn get_port_conf(
        &self,
        port_name: &str,
    ) -> Option<&BondPortConfig> {
        self.bond
            .as_ref()
            .and_then(|bond_conf| bond_conf.ports_config.as_ref())
            .and_then(|port_confs| {
                port_confs
                    .iter()
                    .find(|port_conf| port_conf.name == port_name)
            })
    }

    pub(crate) fn mode(&self) -> Option<BondMode> {
        self.bond.as_ref().and_then(|bond_conf| bond_conf.mode)
    }

    pub fn new() -> Self {
        Self::default()
    }

    fn is_mac_restricted_mode(&self) -> bool {
        self.bond
            .as_ref()
            .and_then(|bond_conf| {
                if self.mode() == Some(BondMode::ActiveBackup) {
                    bond_conf.options.as_ref()
                } else {
                    None
                }
            })
            .and_then(|bond_opts| bond_opts.fail_over_mac)
            == Some(BondFailOverMac::Active)
    }

    fn is_not_mac_restricted_mode_explicitly(&self) -> bool {
        (self.mode().is_some() && self.mode() != Some(BondMode::ActiveBackup))
            || ![None, Some(BondFailOverMac::Active)].contains(
                &self
                    .bond
                    .as_ref()
                    .and_then(|bond_conf| bond_conf.options.as_ref())
                    .and_then(|bond_opts| bond_opts.fail_over_mac),
            )
    }

    fn validate_new_iface_with_no_mode(
        &self,
        current: Option<&Interface>,
    ) -> Result<(), NmstateError> {
        if self.base.state == InterfaceState::Up
            && current.is_none()
            && self.mode().is_none()
        {
            let e = NmstateError::new(
                ErrorKind::InvalidArgument,
                format!(
                    "Bond mode is mandatory for new bond interface: {}",
                    &self.base.name
                ),
            );
            log::error!("{}", e);
            return Err(e);
        }
        Ok(())
    }

    // Fail on
    // * Desire mac restricted mode with mac defined
    // * Desire mac address with current interface in mac restricted mode with
    //   desired not changing mac restricted mode
    fn validate_mac_restricted_mode(
        &self,
        current: Option<&Interface>,
    ) -> Result<(), NmstateError> {
        let e = NmstateError::new(
            ErrorKind::InvalidArgument,
            "MAC address cannot be specified in bond interface along with \
            fail_over_mac active on active backup mode"
                .to_string(),
        );
        if self.is_mac_restricted_mode() && self.base.mac_address.is_some() {
            log::error!("{}", e);
            return Err(e);
        }

        if let Some(Interface::Bond(current)) = current {
            if current.is_mac_restricted_mode()
                && self.base.mac_address.is_some()
                && !self.is_not_mac_restricted_mode_explicitly()
            {
                log::error!("{}", e);
                return Err(e);
            }
        }
        Ok(())
    }

    fn validate_conflict_in_port_and_port_configs(
        &self,
    ) -> Result<(), NmstateError> {
        let bond_config = self.bond.clone().unwrap_or_default();
        if bond_config.port.is_some() && bond_config.ports_config.is_some() {
            let mut port_list = bond_config.port.unwrap_or_default();
            let mut port_config_list: Vec<String> = bond_config
                .ports_config
                .unwrap_or_default()
                .into_iter()
                .map(|p| p.name)
                .collect();
            port_list.sort_unstable();
            port_config_list.sort_unstable();
            let matching = port_list
                .iter()
                .zip(port_config_list.iter())
                .filter(|&(port_name, port_config_name)| {
                    port_name == port_config_name
                })
                .count();
            if matching != port_list.len() || matching != port_config_list.len()
            {
                let e = NmstateError::new(
                    ErrorKind::InvalidArgument,
                    format!(
                        "The port names specified in `port` conflict with \
                        the port names specified in `ports-config` for \
                        bond interface: {}",
                        &self.base.name
                    ),
                );
                log::error!("{}", e);
                return Err(e);
            }
        }
        Ok(())
    }

    pub(crate) fn is_options_reset(&self) -> bool {
        if let Some(bond_opts) = self
            .bond
            .as_ref()
            .and_then(|bond_conf| bond_conf.options.as_ref())
        {
            bond_opts == &BondOptions::default()
        } else {
            false
        }
    }

    fn make_ad_actor_system_mac_upper_case(&mut self) {
        if let Some(mac) = self
            .bond
            .as_mut()
            .and_then(|c| c.options.as_mut())
            .and_then(|o| o.ad_actor_system.as_mut())
        {
            mac.make_ascii_uppercase();
        }
    }

    pub(crate) fn remove_port(&mut self, port_to_remove: &str) {
        if let Some(index) = self.bond.as_ref().and_then(|bond_conf| {
            bond_conf.port.as_ref().and_then(|ports| {
                ports
                    .iter()
                    .position(|port_name| port_name == port_to_remove)
            })
        }) {
            self.bond
                .as_mut()
                .and_then(|bond_conf| bond_conf.port.as_mut())
                .map(|ports| ports.remove(index));
        }
    }

    pub(crate) fn change_port_name(
        &mut self,
        origin_name: &str,
        new_name: String,
    ) {
        if let Some(index) = self
            .bond
            .as_ref()
            .and_then(|bond_conf| bond_conf.port.as_ref())
            .and_then(|ports| {
                ports.iter().position(|port_name| port_name == origin_name)
            })
        {
            if let Some(ports) = self
                .bond
                .as_mut()
                .and_then(|bond_conf| bond_conf.port.as_mut())
            {
                ports[index] = new_name;
            }
        }
    }

    pub(crate) fn get_config_changed_ports(&self, current: &Self) -> Vec<&str> {
        let mut ret: Vec<&str> = Vec::new();
        let mut des_ports_index: HashMap<&str, &BondPortConfig> =
            HashMap::new();
        let mut cur_ports_index: HashMap<&str, &BondPortConfig> =
            HashMap::new();
        if let Some(port_confs) = self
            .bond
            .as_ref()
            .and_then(|bond_conf| bond_conf.ports_config.as_ref())
        {
            for port_conf in port_confs {
                des_ports_index.insert(port_conf.name.as_str(), port_conf);
            }
        }

        if let Some(port_confs) = current
            .bond
            .as_ref()
            .and_then(|bond_conf| bond_conf.ports_config.as_ref())
        {
            for port_conf in port_confs {
                cur_ports_index.insert(port_conf.name.as_str(), port_conf);
            }
        }

        for (port_name, port_conf) in des_ports_index.iter() {
            if let Some(cur_port_conf) = cur_ports_index.get(port_name) {
                if port_conf.is_changed(cur_port_conf) {
                    ret.push(port_name);
                }
            }
        }
        ret
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy)]
#[non_exhaustive]
#[serde(remote = "BondMode")]
/// Bond mode
pub enum BondMode {
    #[serde(rename = "balance-rr", alias = "0")]
    /// Deserialize and serialize from/to `balance-rr`.
    /// You can use integer 0 for deserializing to this mode.
    RoundRobin,
    #[serde(rename = "active-backup", alias = "1")]
    /// Deserialize and serialize from/to `active-backup`.
    /// You can use integer 1 for deserializing to this mode.
    ActiveBackup,
    #[serde(rename = "balance-xor", alias = "2")]
    /// Deserialize and serialize from/to `balance-xor`.
    /// You can use integer 2 for deserializing to this mode.
    XOR,
    #[serde(rename = "broadcast", alias = "3")]
    /// Deserialize and serialize from/to `broadcast`.
    /// You can use integer 3 for deserializing to this mode.
    Broadcast,
    #[serde(rename = "802.3ad", alias = "4")]
    /// Deserialize and serialize from/to `802.3ad`.
    /// You can use integer 4 for deserializing to this mode.
    LACP,
    #[serde(rename = "balance-tlb", alias = "5")]
    /// Deserialize and serialize from/to `balance-tlb`.
    /// You can use integer 5 for deserializing to this mode.
    TLB,
    /// Deserialize and serialize from/to `balance-alb`.
    /// You can use integer 6 for deserializing to this mode.
    #[serde(rename = "balance-alb", alias = "6")]
    ALB,
    #[serde(rename = "unknown")]
    Unknown,
}

impl<'de> Deserialize<'de> for BondMode {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        BondMode::deserialize(
            NumberAsString::deserialize(deserializer)?
                .as_str()
                .into_deserializer(),
        )
    }
}

impl Serialize for BondMode {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        BondMode::serialize(self, serializer)
    }
}

impl Default for BondMode {
    fn default() -> Self {
        Self::RoundRobin
    }
}

impl std::fmt::Display for BondMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                BondMode::RoundRobin => "balance-rr",
                BondMode::ActiveBackup => "active-backup",
                BondMode::XOR => "balance-xor",
                BondMode::Broadcast => "broadcast",
                BondMode::LACP => "802.3ad",
                BondMode::TLB => "balance-tlb",
                BondMode::ALB => "balance-alb",
                BondMode::Unknown => "unknown",
            }
        )
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
#[non_exhaustive]
pub struct BondConfig {
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Mode is mandatory when create new bond interface.
    pub mode: Option<BondMode>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// When applying, if defined, it will override current port list.
    /// The verification will not fail on bond options miss-match but an
    /// warning message.
    /// Please refer to [kernel documentation](https://www.kernel.org/doc/Documentation/networking/bonding.txt) for detail
    pub options: Option<BondOptions>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "ports")]
    /// Deserialize and serialize from/to `port`.
    /// You can also use `ports` for deserializing.
    /// When applying, if defined, it will override current port list.
    pub port: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Deserialize and serialize from/to `ports-config`.
    /// When applying, if defined, it will override current ports
    /// configuration. Note that `port` is not required to set with
    /// `ports-config`. An error will be raised during apply when the port
    /// names specified in `port` and `ports-config` conflict with each
    /// other.
    pub ports_config: Option<Vec<BondPortConfig>>,
}

impl BondConfig {
    pub fn new() -> Self {
        Self::default()
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy)]
#[non_exhaustive]
#[serde(remote = "BondAdSelect", rename_all = "kebab-case")]
/// Specifies the 802.3ad aggregation selection logic to use.
pub enum BondAdSelect {
    /// Deserialize and serialize from/to `stable`.
    #[serde(alias = "0")]
    Stable,
    /// Deserialize and serialize from/to `bandwidth`.
    #[serde(alias = "1")]
    Bandwidth,
    /// Deserialize and serialize from/to `count`.
    #[serde(alias = "2")]
    Count,
}

impl<'de> Deserialize<'de> for BondAdSelect {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        BondAdSelect::deserialize(
            NumberAsString::deserialize(deserializer)?
                .as_str()
                .into_deserializer(),
        )
    }
}

impl Serialize for BondAdSelect {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        BondAdSelect::serialize(self, serializer)
    }
}

impl std::fmt::Display for BondAdSelect {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Stable => "stable",
                Self::Bandwidth => "bandwidth",
                Self::Count => "count",
            }
        )
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(rename_all = "kebab-case", remote = "BondLacpRate")]
#[non_exhaustive]
/// Option specifying the rate in which we'll ask our link partner to transmit
/// LACPDU packets in 802.3ad mode
pub enum BondLacpRate {
    /// Request partner to transmit LACPDUs every 30 seconds.
    /// Serialize to `slow`.
    /// Deserialize from 0 or `slow`.
    #[serde(alias = "0")]
    Slow,
    /// Request partner to transmit LACPDUs every 1 second
    /// Serialize to `fast`.
    /// Deserialize from 1 or `fast`.
    #[serde(alias = "1")]
    Fast,
}

impl<'de> Deserialize<'de> for BondLacpRate {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        BondLacpRate::deserialize(
            NumberAsString::deserialize(deserializer)?
                .as_str()
                .into_deserializer(),
        )
    }
}

impl Serialize for BondLacpRate {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        BondLacpRate::serialize(self, serializer)
    }
}

impl std::fmt::Display for BondLacpRate {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Slow => "slow",
                Self::Fast => "fast",
            }
        )
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy)]
#[serde(rename_all = "kebab-case", remote = "BondAllPortsActive")]
#[non_exhaustive]
/// Equal to kernel `all_slaves_active` option.
/// Specifies that duplicate frames (received on inactive ports) should be
/// dropped (0) or delivered (1).
pub enum BondAllPortsActive {
    /// Drop the duplicate frames
    /// Serialize to `dropped`.
    /// Deserialize from 0 or `dropped`.
    #[serde(alias = "0")]
    Dropped,
    /// Deliver the duplicate frames
    /// Serialize to `delivered`.
    /// Deserialize from 1 or `delivered`.
    #[serde(alias = "1")]
    Delivered,
}

impl<'de> Deserialize<'de> for BondAllPortsActive {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        BondAllPortsActive::deserialize(
            NumberAsString::deserialize(deserializer)?
                .as_str()
                .into_deserializer(),
        )
    }
}

impl Serialize for BondAllPortsActive {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        BondAllPortsActive::serialize(self, serializer)
    }
}

impl std::fmt::Display for BondAllPortsActive {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Dropped => "dropped",
                Self::Delivered => "delivered",
            }
        )
    }
}

impl From<BondAllPortsActive> for u8 {
    fn from(v: BondAllPortsActive) -> u8 {
        match v {
            BondAllPortsActive::Dropped => 0,
            BondAllPortsActive::Delivered => 1,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(rename_all = "kebab-case", remote = "BondArpAllTargets")]
#[non_exhaustive]
/// The `arp_all_targets` kernel bond option: Specifies the quantity of
/// arp_ip_targets that must be reachable in order for the ARP monitor to
/// consider a port as being up. This option affects only active-backup mode
/// for ports with arp_validation enabled.
pub enum BondArpAllTargets {
    /// consider the port up only when any of the `arp_ip_targets` is reachable
    #[serde(alias = "0")]
    Any,
    /// consider the port up only when all of the `arp_ip_targets` are
    /// reachable
    #[serde(alias = "1")]
    All,
}

impl<'de> Deserialize<'de> for BondArpAllTargets {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        BondArpAllTargets::deserialize(
            NumberAsString::deserialize(deserializer)?
                .as_str()
                .into_deserializer(),
        )
    }
}

impl Serialize for BondArpAllTargets {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        BondArpAllTargets::serialize(self, serializer)
    }
}

impl std::fmt::Display for BondArpAllTargets {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Any => "any",
                Self::All => "all",
            }
        )
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(rename_all = "snake_case", remote = "BondArpValidate")]
#[non_exhaustive]
/// The `arp_validate` kernel bond option: Specifies whether or not ARP probes
/// and replies should be validated in any mode that supports arp monitoring, or
/// whether non-ARP traffic should be filtered (disregarded) for link monitoring
/// purposes.
pub enum BondArpValidate {
    /// No validation or filtering is performed.
    /// Serialize to `none`.
    /// Deserialize from 0 or `none`.
    #[serde(alias = "0")]
    None,
    /// Validation is performed only for the active port.
    /// Serialize to `active`.
    /// Deserialize from 1 or `active`.
    #[serde(alias = "1")]
    Active,
    /// Validation is performed only for backup ports.
    /// Serialize to `backup`.
    /// Deserialize from 2 or `backup`.
    #[serde(alias = "2")]
    Backup,
    /// Validation is performed for all ports.
    /// Serialize to `all`.
    /// Deserialize from 3 or `all`.
    #[serde(alias = "3")]
    All,
    /// Filtering is applied to all ports. No validation is performed.
    /// Serialize to `filter`.
    /// Deserialize from 4 or `filter`.
    #[serde(alias = "4")]
    Filter,
    /// Filtering is applied to all ports, validation is performed only for
    /// the active port.
    /// Serialize to `filter_active`.
    /// Deserialize from 5 or `filter-active`.
    #[serde(alias = "5")]
    FilterActive,
    /// Filtering is applied to all ports, validation is performed only for
    /// backup port.
    /// Serialize to `filter_backup`.
    /// Deserialize from 6 or `filter_backup`.
    #[serde(alias = "6")]
    FilterBackup,
}

impl<'de> Deserialize<'de> for BondArpValidate {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        BondArpValidate::deserialize(
            NumberAsString::deserialize(deserializer)?
                .as_str()
                .into_deserializer(),
        )
    }
}

impl Serialize for BondArpValidate {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        BondArpValidate::serialize(self, serializer)
    }
}

impl std::fmt::Display for BondArpValidate {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::None => "none",
                Self::Active => "active",
                Self::Backup => "backup",
                Self::All => "all",
                Self::Filter => "filter",
                Self::FilterActive => "filter_active",
                Self::FilterBackup => "filter_backup",
            }
        )
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy)]
#[serde(rename_all = "kebab-case", remote = "BondFailOverMac")]
#[non_exhaustive]
/// The `fail_over_mac` kernel bond option: Specifies whether active-backup mode
/// should set all ports to the same MAC address at port attachment (the
/// traditional behavior), or, when enabled, perform special handling of the
/// bond's MAC address in accordance with the selected policy.
pub enum BondFailOverMac {
    /// This setting disables fail_over_mac, and causes bonding to set all
    /// ports of an active-backup bond to the same MAC address at attachment
    /// time.
    /// Serialize to `none`.
    /// Deserialize from 0 or `none`.
    #[serde(alias = "0")]
    None,
    /// The "active" fail_over_mac policy indicates that the MAC address of the
    /// bond should always be the MAC address of the currently active port.
    /// The MAC address of the ports is not changed; instead, the MAC address
    /// of the bond changes during a failover.
    /// Serialize to `active`.
    /// Deserialize from 1 or `active`.
    #[serde(alias = "1")]
    Active,
    /// The "follow" fail_over_mac policy causes the MAC address of the bond to
    /// be selected normally (normally the MAC address of the first port added
    /// to the bond). However, the second and subsequent ports are not set to
    /// this MAC address while they are in a backup role; a port is programmed
    /// with the bond's MAC address at failover time (and the formerly active
    /// port receives the newly active port's MAC address).
    /// Serialize to `follow`.
    /// Deserialize from 2 or `follow`.
    #[serde(alias = "2")]
    Follow,
}

impl<'de> Deserialize<'de> for BondFailOverMac {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        BondFailOverMac::deserialize(
            NumberAsString::deserialize(deserializer)?
                .as_str()
                .into_deserializer(),
        )
    }
}

impl Serialize for BondFailOverMac {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        BondFailOverMac::serialize(self, serializer)
    }
}

impl std::fmt::Display for BondFailOverMac {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::None => "none",
                Self::Active => "active",
                Self::Follow => "follow",
            }
        )
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(rename_all = "kebab-case", remote = "BondPrimaryReselect")]
#[non_exhaustive]
/// The `primary_reselect` kernel bond option: Specifies the reselection policy
/// for the primary port. This affects how the primary port is chosen to
/// become the active port when failure of the active port or recovery of the
/// primary port occurs. This option is designed to prevent flip-flopping
/// between the primary port and other ports.
pub enum BondPrimaryReselect {
    ///The primary port becomes the active port whenever it comes back up.
    /// Serialize to `always`.
    /// Deserialize from 0 or `always`.
    #[serde(alias = "0")]
    Always,
    /// The primary port becomes the active port when it comes back up, if the
    /// speed and duplex of the primary port is better than the speed and
    /// duplex of the current active port.
    /// Serialize to `better`.
    /// Deserialize from 1 or `better`.
    #[serde(alias = "1")]
    Better,
    /// The primary port becomes the active port only if the current active
    /// port fails and the primary port is up.
    /// Serialize to `failure`.
    /// Deserialize from 2 or `failure`.
    #[serde(alias = "2")]
    Failure,
}

impl<'de> Deserialize<'de> for BondPrimaryReselect {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        BondPrimaryReselect::deserialize(
            NumberAsString::deserialize(deserializer)?
                .as_str()
                .into_deserializer(),
        )
    }
}

impl Serialize for BondPrimaryReselect {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        BondPrimaryReselect::serialize(self, serializer)
    }
}
impl std::fmt::Display for BondPrimaryReselect {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Always => "always",
                Self::Better => "better",
                Self::Failure => "failure",
            }
        )
    }
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone, Copy)]
#[non_exhaustive]
#[serde(remote = "BondXmitHashPolicy")]
/// The `xmit_hash_policy` kernel bond option: Selects the transmit hash policy
/// to use for port selection in balance-xor, 802.3ad, and tlb modes.
pub enum BondXmitHashPolicy {
    #[serde(rename = "layer2", alias = "0")]
    /// Serialize to `layer2`.
    /// Deserialize from 0 or `layer2`.
    Layer2,
    #[serde(rename = "layer3+4", alias = "1")]
    /// Serialize to `layer3+4`.
    /// Deserialize from 1 or `layer3+4`.
    Layer34,
    #[serde(rename = "layer2+3", alias = "2")]
    /// Serialize to `layer2+3`.
    /// Deserialize from 2 or `layer2+3`.
    Layer23,
    #[serde(rename = "encap2+3", alias = "3")]
    /// Serialize to `encap2+3`.
    /// Deserialize from 3 or `encap2+3`.
    Encap23,
    #[serde(rename = "encap3+4", alias = "4")]
    /// Serialize to `encap3+4`.
    /// Deserialize from 4 or `encap3+4`.
    Encap34,
    #[serde(rename = "vlan+srcmac", alias = "5")]
    /// Serialize to `vlan+srcmac`.
    /// Deserialize from 5 or `vlan+srcmac`.
    VlanSrcMac,
}

impl<'de> Deserialize<'de> for BondXmitHashPolicy {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        BondXmitHashPolicy::deserialize(
            NumberAsString::deserialize(deserializer)?
                .as_str()
                .into_deserializer(),
        )
    }
}

impl Serialize for BondXmitHashPolicy {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        BondXmitHashPolicy::serialize(self, serializer)
    }
}

impl std::fmt::Display for BondXmitHashPolicy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Layer2 => "layer2",
                Self::Layer34 => "layer3+4",
                Self::Layer23 => "layer2+3",
                Self::Encap23 => "encap2+3",
                Self::Encap34 => "encap3+4",
                Self::VlanSrcMac => "vlan+srcmac",
            }
        )
    }
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq, Eq)]
#[non_exhaustive]
#[serde(deny_unknown_fields)]
pub struct BondOptions {
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_u16_or_string"
    )]
    /// In an AD system, this specifies the system priority. The allowed range
    /// is 1 - 65535.
    pub ad_actor_sys_prio: Option<u16>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// In an AD system, this specifies the mac-address for the actor in
    /// protocol packet exchanges (LACPDUs). The value cannot be NULL or
    /// multicast. It is preferred to have the local-admin bit set for this mac
    /// but driver does not enforce it. If the value is not given then system
    /// defaults to using the controller's mac address as actors' system
    /// address.
    pub ad_actor_system: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Specifies the 802.3ad aggregation selection logic to use. The
    /// possible values and their effects are:
    pub ad_select: Option<BondAdSelect>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_u16_or_string"
    )]
    /// In an AD system, the port-key has three parts as shown below -
    ///
    /// ```text
    /// Bits   Use
    /// 00     Duplex
    /// 01-05  Speed
    /// 06-15  User-defined
    /// ```
    ///
    /// This defines the upper 10 bits of the port key. The values can be from
    /// 0
    /// - 1023. If not given, the system defaults to 0.
    ///
    /// This parameter has effect only in 802.3ad mode.
    pub ad_user_port_key: Option<u16>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Specifies that duplicate frames (received on inactive ports) should be
    /// dropped (0) or delivered (1).
    ///
    /// Normally, bonding will drop duplicate frames (received on inactive
    /// ports), which is desirable for most users. But there are some times it
    /// is nice to allow duplicate frames to be delivered.
    pub all_slaves_active: Option<BondAllPortsActive>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Specifies the quantity of arp_ip_targets that must be reachable in
    /// order for the ARP monitor to consider a port as being up. This
    /// option affects only active-backup mode for ports with
    /// arp_validation enabled.
    pub arp_all_targets: Option<BondArpAllTargets>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_u32_or_string"
    )]
    /// Specifies the ARP link monitoring frequency in milliseconds.
    ///
    /// The ARP monitor works by periodically checking the port devices to
    /// determine whether they have sent or received traffic recently (the
    /// precise criteria depends upon the bonding mode, and the state of the
    /// port). Regular traffic is generated via ARP probes issued for the
    /// addresses specified by the arp_ip_target option.
    ///
    /// This behavior can be modified by the arp_validate option,
    /// below.
    ///
    /// If ARP monitoring is used in an etherchannel compatible mode (modes 0
    /// and 2), the switch should be configured in a mode that evenly
    /// distributes packets across all links. If the switch is configured to
    /// distribute the packets in an XOR fashion, all replies from the ARP
    /// targets will be received on the same link which could cause the other
    /// team members to fail. ARP monitoring should not be used in conjunction
    /// with miimon. A value of 0 disables ARP monitoring. The default value
    /// is 0.
    pub arp_interval: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]

    /// Specifies the IP addresses to use as ARP monitoring peers when
    /// arp_interval is > 0. These are the targets of the ARP request sent to
    /// determine the health of the link to the targets. Specify these values
    /// in ddd.ddd.ddd.ddd format. Multiple IP addresses must be separated by a
    /// comma. At least one IP address must be given for ARP monitoring to
    /// function. The maximum number of targets that can be specified is 16.
    /// The default value is no IP addresses.
    pub arp_ip_target: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Specifies whether or not ARP probes and replies should be validated in
    /// any mode that supports arp monitoring, or whether non-ARP traffic
    /// should be filtered (disregarded) for link monitoring purposes.
    pub arp_validate: Option<BondArpValidate>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_u32_or_string"
    )]
    /// Specifies the time, in milliseconds, to wait before disabling a port
    /// after a link failure has been detected. This option is only valid for
    /// the miimon link monitor. The downdelay value should be a multiple of
    /// the miimon value; if not, it will be rounded down to the nearest
    /// multiple. The default value is 0.
    pub downdelay: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Specifies whether active-backup mode should set all ports to the same
    /// MAC address at enportment (the traditional behavior), or, when enabled,
    /// perform special handling of the bond's MAC address in accordance with
    /// the selected policy.
    pub fail_over_mac: Option<BondFailOverMac>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Option specifying the rate in which we'll ask our link partner to
    /// transmit LACPDU packets in 802.3ad mode.
    pub lacp_rate: Option<BondLacpRate>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_u32_or_string"
    )]
    /// Specifies the number of seconds between instances where the bonding
    /// driver sends learning packets to each slaves peer switch.
    ///
    /// The valid range is 1 - 0x7fffffff; the default value is 1. This Option
    /// has effect only in balance-tlb and balance-alb modes.
    pub lp_interval: Option<u32>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_u32_or_string"
    )]
    /// Specifies the MII link monitoring frequency in milliseconds.
    /// This determines how often the link state of each port is
    /// inspected for link failures. A value of zero disables MII
    /// link monitoring. A value of 100 is a good starting point.
    /// The use_carrier option, below, affects how the link state is
    /// determined. See the High Availability section for additional
    /// information. The default value is 0.
    pub miimon: Option<u32>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_u32_or_string"
    )]
    /// Specifies the minimum number of links that must be active before
    /// asserting carrier. It is similar to the Cisco EtherChannel min-links
    /// feature. This allows setting the minimum number of member ports that
    /// must be up (link-up state) before marking the bond device as up
    /// (carrier on). This is useful for situations where higher level services
    /// such as clustering want to ensure a minimum number of low bandwidth
    /// links are active before switchover. This option only affect 802.3ad
    /// mode.
    ///
    /// The default value is 0. This will cause carrier to be asserted (for
    /// 802.3ad mode) whenever there is an active aggregator, regardless of the
    /// number of available links in that aggregator. Note that, because an
    /// aggregator cannot be active without at least one available link,
    /// setting this option to 0 or to 1 has the exact same effect.
    pub min_links: Option<u32>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_u8_or_string"
    )]
    /// Specify the number of peer notifications (gratuitous ARPs and
    /// unsolicited IPv6 Neighbor Advertisements) to be issued after a
    /// failover event. As soon as the link is up on the new port
    /// (possibly immediately) a peer notification is sent on the
    /// bonding device and each VLAN sub-device. This is repeated at
    /// the rate specified by peer_notif_delay if the number is
    /// greater than 1.
    ///
    /// The valid range is 0 - 255; the default value is 1. These options
    /// affect only the active-backup mode. These options were added for
    /// bonding versions 3.3.0 and 3.4.0 respectively.
    ///
    /// From Linux 3.0 and bonding version 3.7.1, these notifications are
    /// generated by the ipv4 and ipv6 code and the numbers of repetitions
    /// cannot be set independently.
    pub num_grat_arp: Option<u8>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_u8_or_string"
    )]
    /// Identical to [BondOptions.num_grat_arp]
    pub num_unsol_na: Option<u8>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_u32_or_string"
    )]
    /// Specify the number of packets to transmit through a port before moving
    /// to the next one. When set to 0 then a port is chosen at random.
    ///
    /// The valid range is 0 - 65535; the default value is 1. This option has
    /// effect only in balance-rr mode.
    pub packets_per_slave: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// A string (eth0, eth2, etc) specifying which slave is the primary
    /// device. The specified device will always be the active slave while
    /// it is available. Only when the primary is off-line will alternate
    /// devices be used. This is useful when one slave is preferred over
    /// another, e.g., when one slave has higher throughput than another.
    ///
    /// The primary option is only valid for active-backup(1), balance-tlb (5)
    /// and balance-alb (6) mode.
    pub primary: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Specifies the reselection policy for the primary port. This affects
    /// how the primary port is chosen to become the active port when failure
    /// of the active port or recovery of the primary port occurs. This
    /// option is designed to prevent flip-flopping between the primary port
    /// and other ports.
    pub primary_reselect: Option<BondPrimaryReselect>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_u32_or_string"
    )]
    /// Specifies the number of IGMP membership reports to be issued after
    /// a failover event. One membership report is issued immediately after
    /// the failover, subsequent packets are sent in each 200ms interval.
    ///
    /// The valid range is 0 - 255; the default value is 1. A value of 0
    /// prevents the IGMP membership report from being issued in response
    /// to the failover event.
    ///
    /// This option is useful for bonding modes balance-rr (0), active-backup
    /// (1), balance-tlb (5) and balance-alb (6), in which a failover can
    /// switch the IGMP traffic from one port to another. Therefore a
    /// fresh IGMP report must be issued to cause the switch to forward the
    /// incoming IGMP traffic over the newly selected port.
    pub resend_igmp: Option<u32>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_bool_or_string"
    )]
    /// Specifies if dynamic shuffling of flows is enabled in tlb mode. The
    /// value has no effect on any other modes.
    ///
    /// The default behavior of tlb mode is to shuffle active flows across
    /// ports based on the load in that interval. This gives nice lb
    /// characteristics but can cause packet reordering. If re-ordering is a
    /// concern use this variable to disable flow shuffling and rely on load
    /// balancing provided solely by the hash distribution. xmit-hash-policy
    /// can be used to select the appropriate hashing for the setup.
    ///
    /// The sysfs entry can be used to change the setting per bond device and
    /// the initial value is derived from the module parameter. The sysfs entry
    /// is allowed to be changed only if the bond device is down.
    ///
    /// The default value is "1" that enables flow shuffling while value "0"
    /// disables it. This option was added in bonding driver 3.7.1
    pub tlb_dynamic_lb: Option<bool>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_u32_or_string"
    )]
    /// Specifies the time, in milliseconds, to wait before enabling a port
    /// after a link recovery has been detected. This option is only valid for
    /// the miimon link monitor. The updelay value should be a multiple of the
    /// miimon value; if not, it will be rounded down to the nearest multiple.
    /// The default value is 0.
    pub updelay: Option<u32>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_bool_or_string"
    )]
    /// Specifies whether or not miimon should use MII or ETHTOOL
    /// ioctls vs. netif_carrier_ok() to determine the link
    /// status. The MII or ETHTOOL ioctls are less efficient and
    /// utilize a deprecated calling sequence within the kernel.  The
    /// netif_carrier_ok() relies on the device driver to maintain its
    /// state with netif_carrier_on/off; at this writing, most, but
    /// not all, device drivers support this facility.
    ///
    /// If bonding insists that the link is up when it should not be,
    /// it may be that your network device driver does not support
    /// netif_carrier_on/off.  The default state for netif_carrier is
    /// "carrier on," so if a driver does not support netif_carrier,
    /// it will appear as if the link is always up.  In this case,
    /// setting use_carrier to 0 will cause bonding to revert to the
    /// MII / ETHTOOL ioctl method to determine the link state.
    ///
    /// A value of 1 enables the use of netif_carrier_ok(), a value of
    /// 0 will use the deprecated MII / ETHTOOL ioctls.  The default
    /// value is 1.
    pub use_carrier: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    /// Selects the transmit hash policy to use for slave selection in
    /// balance-xor, 802.3ad, and tlb modes.
    pub xmit_hash_policy: Option<BondXmitHashPolicy>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_bool_or_string",
        alias = "balance-slb"
    )]
    pub balance_slb: Option<bool>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_u8_or_string"
    )]
    pub arp_missed_max: Option<u8>,
}

impl BondOptions {
    pub fn new() -> Self {
        Self::default()
    }

    fn validate_ad_actor_system_mac_address(&self) -> Result<(), NmstateError> {
        if let Some(ad_actor_system) = &self.ad_actor_system {
            if ad_actor_system.to_uppercase().starts_with("01:00:5E") {
                let e = NmstateError::new(
                    ErrorKind::InvalidArgument,
                    "The ad_actor_system bond option cannot be an IANA \
                    multicast address(prefix with 01:00:5E)"
                        .to_string(),
                );
                log::error!("{}", e);
                return Err(e);
            }
        }
        Ok(())
    }

    fn validate_miimon_and_arp_interval(&self) -> Result<(), NmstateError> {
        if let (Some(miimon), Some(arp_interval)) =
            (self.miimon, self.arp_interval)
        {
            if miimon > 0 && arp_interval > 0 {
                let e = NmstateError::new(
                    ErrorKind::InvalidArgument,
                    "Bond miimon and arp interval are not compatible options."
                        .to_string(),
                );
                log::error!("{}", e);
                return Err(e);
            }
        }
        Ok(())
    }

    fn validate_balance_slb(
        &self,
        current: Option<&Self>,
        mode: BondMode,
    ) -> Result<(), NmstateError> {
        if self
            .balance_slb
            .or_else(|| current.and_then(|c| c.balance_slb))
            == Some(true)
        {
            let xmit_hash_policy = self
                .xmit_hash_policy
                .or_else(|| current.and_then(|c| c.xmit_hash_policy));
            if mode != BondMode::XOR
                || xmit_hash_policy != Some(BondXmitHashPolicy::VlanSrcMac)
            {
                return Err(NmstateError::new(
                    ErrorKind::InvalidArgument,
                    "To enable balance-slb, bond mode should be \
                    balance-xor and xmit_hash_policy: 'vlan+srcmac'"
                        .to_string(),
                ));
            }
        }
        Ok(())
    }
}

impl MergedInterface {
    pub(crate) fn post_inter_ifaces_process_bond(
        &mut self,
    ) -> Result<(), NmstateError> {
        if let Some(Interface::Bond(apply_iface)) = self.for_apply.as_ref() {
            apply_iface
                .validate_new_iface_with_no_mode(self.current.as_ref())?;
            apply_iface.validate_mac_restricted_mode(self.current.as_ref())?;
            apply_iface.validate_conflict_in_port_and_port_configs()?;

            if let Some(bond_opts) =
                apply_iface.bond.as_ref().and_then(|b| b.options.as_ref())
            {
                bond_opts.validate_ad_actor_system_mac_address()?;
                bond_opts.validate_miimon_and_arp_interval()?;

                if let Interface::Bond(merged_iface) = &self.merged {
                    if let Some(mode) =
                        merged_iface.bond.as_ref().and_then(|b| b.mode)
                    {
                        let cur_bond_opts =
                            if let Some(Interface::Bond(cur_iface)) =
                                self.current.as_ref()
                            {
                                cur_iface
                                    .bond
                                    .as_ref()
                                    .and_then(|b| b.options.as_ref())
                            } else {
                                None
                            };
                        bond_opts.validate_balance_slb(cur_bond_opts, mode)?
                    }
                }
            }
        }
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct BondPortConfig {
    /// name is mandatory when specifying the ports configuration.
    pub name: String,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_i32_or_string"
    )]
    /// Deserialize and serialize from/to `priority`.
    /// When applying, if defined, it will override the current bond port
    /// priority. The verification will fail if bonding mode is not
    /// active-backup(1) or balance-tlb (5) or balance-alb (6).
    pub priority: Option<i32>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        deserialize_with = "crate::deserializer::option_u16_or_string"
    )]
    /// Deserialize and serialize from/to `queue-id`.
    pub queue_id: Option<u16>,
}

impl std::fmt::Display for BondPortConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "BondPortConfig {{ name: {}, priority: {}, queue_id: {} }}",
            self.name,
            self.priority.unwrap_or_default(),
            self.queue_id.unwrap_or_default()
        )
    }
}

impl BondPortConfig {
    pub fn new() -> Self {
        Self::default()
    }

    fn is_changed(&self, current: &Self) -> bool {
        (self.priority.is_some() && self.priority != current.priority)
            || (self.queue_id.is_some() && self.queue_id != current.queue_id)
    }
}