netcfg/
interface.rs

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
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use either::Either;
use serde::{Deserialize, Deserializer};
use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicU32, Ordering};

use crate::DeviceClass;

const INTERFACE_PREFIX_WLAN: &str = "wlan";
const INTERFACE_PREFIX_ETHERNET: &str = "eth";
const INTERFACE_PREFIX_AP: &str = "ap";

#[derive(PartialEq, Eq, Debug, Clone, Hash)]
pub(crate) struct InterfaceNamingIdentifier {
    pub(crate) mac: fidl_fuchsia_net_ext::MacAddress,
}

pub(crate) fn generate_identifier(
    mac_address: &fidl_fuchsia_net_ext::MacAddress,
) -> InterfaceNamingIdentifier {
    InterfaceNamingIdentifier { mac: *mac_address }
}

// Get the NormalizedMac using the last octet of the MAC address. The offset
// modifies the last_byte in an attempt to avoid naming conflicts.
// For example, a MAC of `[0x1, 0x1, 0x1, 0x1, 0x1, 0x9]` with offset 0
// becomes `9`.
fn get_mac_identifier_from_octets(
    octets: &[u8; 6],
    interface_type: crate::InterfaceType,
    offset: u8,
) -> Result<u8, anyhow::Error> {
    if offset == u8::MAX {
        return Err(anyhow::format_err!(
            "could not find unique identifier for mac={:?}, interface_type={:?}",
            octets,
            interface_type
        ));
    }

    let last_byte = octets[octets.len() - 1];
    let (identifier, _) = last_byte.overflowing_add(offset);
    Ok(identifier)
}

// Get the normalized bus path for a topological path.
// For example, a PCI device at `02:00.1` becomes `02001`.
// At the time of writing, typical topological paths appear similar to:
//
// PCI:
// "/dev/sys/platform/pt/PCI0/bus/02:00.0/02:00.0/e1000/ethernet"
//
// USB over PCI:
// "/dev/sys/platform/pt/PCI0/bus/00:14.0/00:14.0/xhci/usb/007/ifc-000/<snip>/wlan/wlan-ethernet/ethernet"
// 00:14:0 following "/PCI0/bus/" represents BDF (Bus Device Function)
//
// USB over DWC:
// "/dev/sys/platform/05:00:18/usb-phy-composite/aml_usb_phy/dwc2/dwc2_phy/dwc2/usb-peripheral/function-000/cdc-eth-function/netdevice-migration/network-device"
// 05:00:18 following "platform" represents
// vid(vendor id):pid(product id):did(device id) and are defined in each board file
//
// SDIO
// "/dev/sys/platform/05:00:6/aml-sd-emmc/sdio/broadcom-wlanphy/wlanphy"
// 05:00:6 following "platform" represents
// vid(vendor id):pid(product id):did(device id) and are defined in each board file
//
// Ethernet Jack for VIM2
// "/dev/sys/platform/04:02:7/aml-ethernet/Designware-MAC/ethernet"
//
// VirtIo
// "/dev/sys/platform/pt/PC00/bus/00:1e.0/00_1e_0/virtio-net/network-device"
//
// Since there is no real standard for topological paths, when no bus path can be found,
// the function attempts to return one that is unlikely to conflict with any existing path
// by assuming a bus path of ff:ff:ff, and decrementing from there. This permits
// generating unique, well-formed names in cases where a matching path component can't be
// found, while also being relatively recognizable as exceptional.
fn get_normalized_bus_path_for_topo_path(topological_path: &str) -> String {
    static PATH_UNIQ_MARKER: AtomicU32 = AtomicU32::new(0xffffff);
    topological_path
        .split("/")
        .find(|pc| {
            pc.len() >= 7 && pc.chars().all(|c| c.is_digit(16) || c == ':' || c == '.' || c == '_')
        })
        .and_then(|s| {
            Some(s.replace(&[':', '.', '_'], "").trim_end_matches(|c| c == '0').to_string())
        })
        .unwrap_or_else(|| format!("{:01$x}", PATH_UNIQ_MARKER.fetch_sub(1, Ordering::SeqCst), 6))
}

#[derive(Debug)]
pub struct InterfaceNamingConfig {
    naming_rules: Vec<NamingRule>,
    interfaces: HashMap<InterfaceNamingIdentifier, String>,
}

impl InterfaceNamingConfig {
    pub(crate) fn from_naming_rules(naming_rules: Vec<NamingRule>) -> InterfaceNamingConfig {
        InterfaceNamingConfig { naming_rules, interfaces: HashMap::new() }
    }

    /// Returns a stable interface name for the specified interface.
    pub(crate) fn generate_stable_name(
        &mut self,
        topological_path: &str,
        mac: &fidl_fuchsia_net_ext::MacAddress,
        device_class: DeviceClass,
    ) -> Result<(&str, InterfaceNamingIdentifier), NameGenerationError> {
        let interface_naming_id = generate_identifier(mac);
        let info = DeviceInfoRef { topological_path, mac, device_class };

        // Interfaces that are named using the NormalizedMac naming rule are
        // named to avoid MAC address final octet collisions. When a device
        // with the same identifier is re-installed, re-attempt name generation
        // since the MAC identifiers used may have changed.
        match self.interfaces.remove(&interface_naming_id) {
            Some(name) => tracing::info!(
                "{name} already existed for this identifier\
            {interface_naming_id:?}. inserting a new one."
            ),
            None => {
                // This interface naming id will have a new entry
            }
        }

        let generated_name = self.generate_name(&info)?;
        if let Some(name) =
            self.interfaces.insert(interface_naming_id.clone(), generated_name.clone())
        {
            tracing::error!(
                "{name} was unexpectedly found for {interface_naming_id:?} \
            when inserting a new name"
            );
        }

        // Need to grab a reference to appease the borrow checker.
        let generated_name = match self.interfaces.get(&interface_naming_id) {
            Some(name) => Ok(name),
            None => Err(NameGenerationError::GenerationError(anyhow::format_err!(
                "expected to see name {generated_name} present since it was just added"
            ))),
        }?;

        Ok((generated_name, interface_naming_id))
    }

    fn generate_name(&self, info: &DeviceInfoRef<'_>) -> Result<String, NameGenerationError> {
        generate_name_from_naming_rules(&self.naming_rules, &self.interfaces, &info)
    }
}

/// An error observed when generating a new name.
#[derive(Debug)]
pub enum NameGenerationError {
    GenerationError(anyhow::Error),
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "lowercase")]
pub enum BusType {
    PCI,
    SDIO,
    USB,
    Unknown,
    VirtIo,
}

impl BusType {
    // Retrieve the list of composition rules that comprise the default name
    // for the interface based on BusType.
    // Example names for the following default rules:
    // * USB device: "ethx5"
    // * PCI/SDIO device: "wlans5009"
    fn get_default_name_composition_rules(&self) -> Vec<NameCompositionRule> {
        match *self {
            BusType::USB | BusType::Unknown => vec![
                NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::DeviceClass },
                NameCompositionRule::Static { value: String::from("x") },
                NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::NormalizedMac },
            ],
            BusType::PCI | BusType::SDIO | BusType::VirtIo => vec![
                NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::DeviceClass },
                NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::BusType },
                NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::BusPath },
            ],
        }
    }
}

impl std::fmt::Display for BusType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let name = match *self {
            Self::PCI => "p",
            Self::SDIO => "s",
            Self::USB => "u",
            Self::Unknown => "unk",
            Self::VirtIo => "v",
        };
        write!(f, "{}", name)
    }
}

// Extract the `BusType` for a device given the topological path.
fn get_bus_type_for_topological_path(topological_path: &str) -> BusType {
    let p = topological_path;

    if p.contains("/PCI0") {
        // A USB bus will require a bridge over a PCI controller, so a
        // topological path for a USB bus should contain strings to represent
        // PCI and USB.
        if p.contains("/usb/") {
            return BusType::USB;
        }
        return BusType::PCI;
    } else if p.contains("/usb-peripheral/") {
        // On VIM3 targets, the USB bus does not require a bridge over a PCI
        // controller, so the bus path represents the USB type with a
        // different string.
        return BusType::USB;
    } else if p.contains("/sdio/") {
        return BusType::SDIO;
    } else if p.contains("/virtio-net/") {
        return BusType::VirtIo;
    }

    BusType::Unknown
}

fn deserialize_glob_pattern<'de, D>(deserializer: D) -> Result<glob::Pattern, D::Error>
where
    D: Deserializer<'de>,
{
    let buf = String::deserialize(deserializer)?;
    glob::Pattern::new(&buf).map_err(serde::de::Error::custom)
}

/// The matching rules available for a `NamingRule`.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub enum MatchingRule {
    BusTypes(Vec<BusType>),
    // TODO(https://fxbug.dev/42085144): Use a lightweight regex crate with the basic
    // regex features to allow for more configurations than glob.
    #[serde(deserialize_with = "deserialize_glob_pattern")]
    TopologicalPath(glob::Pattern),
    DeviceClasses(Vec<DeviceClass>),
    // Signals whether this rule should match any interface.
    Any(bool),
}

/// The matching rules available for a `ProvisoningRule`.
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
#[serde(untagged)]
pub enum ProvisioningMatchingRule {
    // TODO(github.com/serde-rs/serde/issues/912): Use `other` once it supports
    // deserializing into non-unit variants. `untagged` can only be applied
    // to the entire enum, so `interface_name` is used as a field to ensure
    // stability across configuration matching rules.
    InterfaceName {
        #[serde(rename = "interface_name", deserialize_with = "deserialize_glob_pattern")]
        pattern: glob::Pattern,
    },
    Common(MatchingRule),
}

impl MatchingRule {
    fn does_interface_match(&self, info: &DeviceInfoRef<'_>) -> Result<bool, anyhow::Error> {
        match &self {
            MatchingRule::BusTypes(type_list) => {
                // Match the interface if the interface under comparison
                // matches any of the types included in the list.
                let bus_type = get_bus_type_for_topological_path(info.topological_path);
                Ok(type_list.contains(&bus_type))
            }
            MatchingRule::TopologicalPath(pattern) => {
                // Match the interface if the provided pattern finds any
                // matches in the interface under comparison's
                // topological path.
                Ok(pattern.matches(info.topological_path))
            }
            MatchingRule::DeviceClasses(class_list) => {
                // Match the interface if the interface under comparison
                // matches any of the types included in the list.
                Ok(class_list.contains(&info.device_class))
            }
            MatchingRule::Any(matches_any_interface) => Ok(*matches_any_interface),
        }
    }
}

impl ProvisioningMatchingRule {
    fn does_interface_match(
        &self,
        info: &DeviceInfoRef<'_>,
        interface_name: &str,
    ) -> Result<bool, anyhow::Error> {
        match &self {
            ProvisioningMatchingRule::InterfaceName { pattern } => {
                // Match the interface if the provided pattern finds any
                // matches in the interface under comparison's name.
                Ok(pattern.matches(interface_name))
            }
            ProvisioningMatchingRule::Common(matching_rule) => {
                // Handle the other `MatchingRule`s the same as the naming
                // policy matchers.
                matching_rule.does_interface_match(info)
            }
        }
    }
}

// TODO(https://fxbug.dev/42084785): Create dynamic naming rules
// A naming rule that uses device information to produce a component of
// the interface's name.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub enum DynamicNameCompositionRule {
    BusPath,
    BusType,
    DeviceClass,
    // A unique value seeded by the final octet of the interface's MAC address.
    NormalizedMac,
}

impl DynamicNameCompositionRule {
    // `true` when a rule can be re-tried to produce a different name.
    fn supports_retry(&self) -> bool {
        match *self {
            DynamicNameCompositionRule::BusPath
            | DynamicNameCompositionRule::BusType
            | DynamicNameCompositionRule::DeviceClass => false,
            DynamicNameCompositionRule::NormalizedMac => true,
        }
    }

    fn get_name(&self, info: &DeviceInfoRef<'_>, attempt_num: u8) -> Result<String, anyhow::Error> {
        Ok(match *self {
            DynamicNameCompositionRule::BusPath => {
                get_normalized_bus_path_for_topo_path(info.topological_path)
            }
            DynamicNameCompositionRule::BusType => {
                get_bus_type_for_topological_path(info.topological_path).to_string()
            }
            DynamicNameCompositionRule::DeviceClass => match info.device_class.into() {
                crate::InterfaceType::WlanClient => INTERFACE_PREFIX_WLAN,
                crate::InterfaceType::Ethernet => INTERFACE_PREFIX_ETHERNET,
                crate::InterfaceType::WlanAp => INTERFACE_PREFIX_AP,
            }
            .to_string(),
            DynamicNameCompositionRule::NormalizedMac => {
                let fidl_fuchsia_net_ext::MacAddress { octets } = info.mac;
                let mac_identifier =
                    get_mac_identifier_from_octets(octets, info.device_class.into(), attempt_num)?;
                format!("{mac_identifier:x}")
            }
        })
    }
}

// A rule that dictates a component of an interface's name. An interface's name
// is determined by extracting the name of each rule, in order, and
// concatenating the results.
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(deny_unknown_fields, rename_all = "lowercase", tag = "type")]
pub enum NameCompositionRule {
    Static { value: String },
    Dynamic { rule: DynamicNameCompositionRule },
    // The default name composition rules based on the device's BusType.
    // Defined in `BusType::get_default_name_composition_rules`.
    Default,
}

/// A rule that dictates how interfaces that align with the property matching
/// rules should be named.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(deny_unknown_fields, rename_all = "lowercase")]
pub struct NamingRule {
    /// A set of rules to check against an interface's properties. All rules
    /// must apply for the naming scheme to take effect.
    pub matchers: HashSet<MatchingRule>,
    /// The rules to apply to the interface to produce the interface's name.
    pub naming_scheme: Vec<NameCompositionRule>,
}

impl NamingRule {
    // An interface's name is determined by extracting the name of each rule,
    // in order, and concatenating the results. Returns an error if the
    // interface name cannot be generated.
    fn generate_name(
        &self,
        interfaces: &HashMap<InterfaceNamingIdentifier, String>,
        info: &DeviceInfoRef<'_>,
    ) -> Result<String, NameGenerationError> {
        // When a bus type cannot be found for a path, use the USB
        // default naming policy which uses a MAC address.
        let bus_type = get_bus_type_for_topological_path(&info.topological_path);

        // Expand any `Default` rules into the `Static` and `Dynamic` rules in a single vector.
        // If this was being consumed once, we could avoid the call to `collect`. However, since we
        // want to use it twice, we need to convert it to a form where the items can be itererated
        // over without consuming them.
        let expanded_rules = self
            .naming_scheme
            .iter()
            .map(|rule| {
                if let NameCompositionRule::Default = rule {
                    Either::Right(bus_type.get_default_name_composition_rules().into_iter())
                } else {
                    Either::Left(std::iter::once(rule.clone()))
                }
            })
            .flatten()
            .collect::<Vec<_>>();

        // Determine whether any rules present support retrying for a unique name.
        let should_reattempt_on_conflict = expanded_rules.iter().any(|rule| {
            if let NameCompositionRule::Dynamic { rule } = rule {
                rule.supports_retry()
            } else {
                false
            }
        });

        let mut attempt_num = 0u8;
        loop {
            let name = expanded_rules
                .iter()
                .map(|rule| match rule {
                    NameCompositionRule::Static { value } => Ok(value.clone()),
                    // Dynamic rules require the knowledge of `DeviceInfo` properties.
                    NameCompositionRule::Dynamic { rule } => rule
                        .get_name(info, attempt_num)
                        .map_err(NameGenerationError::GenerationError),
                    NameCompositionRule::Default => {
                        unreachable!(
                            "Default naming rules should have been pre-expanded. \
                             Nested default rules are not supported."
                        );
                    }
                })
                .collect::<Result<String, NameGenerationError>>()?;

            if interfaces.values().any(|existing_name| existing_name == &name) {
                if should_reattempt_on_conflict {
                    attempt_num += 1;
                    // Try to generate another name with the modified attempt number.
                    continue;
                }

                tracing::warn!(
                    "name ({name}) already used for an interface installed by netcfg. \
                 using name since it is possible that the interface using this name is no \
                 longer active"
                );
            }
            return Ok(name);
        }
    }

    // An interface must align with all specified `MatchingRule`s.
    fn does_interface_match(&self, info: &DeviceInfoRef<'_>) -> bool {
        self.matchers.iter().all(|rule| rule.does_interface_match(info).unwrap_or_default())
    }
}

// Find the first `NamingRule` that matches the device and attempt to
// construct a name from the provided `NameCompositionRule`s.
fn generate_name_from_naming_rules(
    naming_rules: &[NamingRule],
    interfaces: &HashMap<InterfaceNamingIdentifier, String>,
    info: &DeviceInfoRef<'_>,
) -> Result<String, NameGenerationError> {
    // TODO(https://fxbug.dev/42086002): Consider adding an option to the rules to allow
    // fallback rules when name generation fails.
    // Use the first naming rule that matches the interface to enforce consistent
    // interface names, even if there are other matching rules.
    let fallback_rule = fallback_naming_rule();
    let first_matching_rule =
        naming_rules.iter().find(|rule| rule.does_interface_match(&info)).unwrap_or(
            // When there are no `NamingRule`s that match the device,
            // use a fallback rule that has the Default naming scheme.
            &fallback_rule,
        );

    first_matching_rule.generate_name(interfaces, &info)
}

// Matches any device and uses the default naming rule.
fn fallback_naming_rule() -> NamingRule {
    NamingRule {
        matchers: HashSet::from([MatchingRule::Any(true)]),
        naming_scheme: vec![NameCompositionRule::Default],
    }
}

/// Whether the interface should be provisioned locally by netcfg, or
/// delegated. Provisioning is the set of events that occurs after
/// interface enumeration, such as starting a DHCP client and assigning
/// an IP to the interface. Provisioning actions work to support
/// Internet connectivity.
#[derive(Copy, Clone, Debug, Deserialize, PartialEq)]
#[serde(deny_unknown_fields, rename_all = "lowercase")]
pub enum ProvisioningAction {
    /// Netcfg will provision the interface
    Local,
    /// Netcfg will not provision the interface. The provisioning
    /// of the interface will occur elsewhere
    Delegated,
}

/// A rule that dictates how interfaces that align with the property matching
/// rules should be provisioned.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(deny_unknown_fields, rename_all = "lowercase")]
pub struct ProvisioningRule {
    /// A set of rules to check against an interface's properties. All rules
    /// must apply for the provisioning action to take effect.
    #[allow(unused)]
    pub matchers: HashSet<ProvisioningMatchingRule>,
    /// The provisioning policy that netcfg applies to a matching
    /// interface.
    #[allow(unused)]
    pub provisioning: ProvisioningAction,
}

// A ref version of `devices::DeviceInfo` to avoid the need to clone data
// unnecessarily. Devices without MAC are not supported yet, see
// `add_new_device` in `lib.rs`. This makes mac into a required field for
// ease of use.
pub(super) struct DeviceInfoRef<'a> {
    pub(super) device_class: DeviceClass,
    pub(super) mac: &'a fidl_fuchsia_net_ext::MacAddress,
    pub(super) topological_path: &'a str,
}

impl<'a> DeviceInfoRef<'a> {
    pub(super) fn interface_type(&self) -> crate::InterfaceType {
        let DeviceInfoRef { device_class, mac: _, topological_path: _ } = self;
        (*device_class).into()
    }

    pub(super) fn is_wlan_ap(&self) -> bool {
        let DeviceInfoRef { device_class, mac: _, topological_path: _ } = self;
        match device_class {
            DeviceClass::WlanAp => true,
            DeviceClass::WlanClient
            | DeviceClass::Virtual
            | DeviceClass::Ethernet
            | DeviceClass::Bridge
            | DeviceClass::Ppp
            | DeviceClass::Lowpan => false,
        }
    }
}

impl ProvisioningRule {
    // An interface must align with all specified `MatchingRule`s.
    fn does_interface_match(&self, info: &DeviceInfoRef<'_>, interface_name: &str) -> bool {
        self.matchers
            .iter()
            .all(|rule| rule.does_interface_match(info, interface_name).unwrap_or_default())
    }
}

// Find the first `ProvisioningRule` that matches the device and get
// the associated `ProvisioningAction`. By default, use Local provisioning
// so that Netcfg will provision interfaces unless configuration
// indicates otherwise.
pub(crate) fn find_provisioning_action_from_provisioning_rules(
    provisioning_rules: &[ProvisioningRule],
    info: &DeviceInfoRef<'_>,
    interface_name: &str,
) -> ProvisioningAction {
    provisioning_rules
        .iter()
        .find_map(|rule| {
            if rule.does_interface_match(&info, &interface_name) {
                Some(rule.provisioning)
            } else {
                None
            }
        })
        .unwrap_or(
            // When there are no `ProvisioningRule`s that match the device,
            // use Local provisioning.
            ProvisioningAction::Local,
        )
}

#[cfg(test)]
mod tests {
    use super::*;
    use assert_matches::assert_matches;
    use test_case::test_case;

    // This is a lossy conversion between `InterfaceType` and `DeviceClass`
    // that allows tests to use a `devices::DeviceInfo` struct instead of
    // handling the fields individually.
    fn device_class_from_interface_type(ty: crate::InterfaceType) -> DeviceClass {
        match ty {
            crate::InterfaceType::Ethernet => DeviceClass::Ethernet,
            crate::InterfaceType::WlanClient => DeviceClass::WlanClient,
            crate::InterfaceType::WlanAp => DeviceClass::WlanAp,
        }
    }

    // usb interfaces
    #[test_case(
        "/dev/sys/platform/pt/PCI0/bus/00:14.0/00:14.0/xhci/usb/004/004/ifc-000/ax88179/ethernet",
        [0x01, 0x01, 0x01, 0x01, 0x01, 0x01],
        crate::InterfaceType::WlanClient,
        "wlanx1";
        "usb_wlan"
    )]
    #[test_case(
        "/dev/sys/platform/pt/PCI0/bus/00:15.0/00:15.0/xhci/usb/004/004/ifc-000/ax88179/ethernet",
        [0x02, 0x02, 0x02, 0x02, 0x02, 0x02],
        crate::InterfaceType::Ethernet,
        "ethx2";
        "usb_eth"
    )]
    // pci interfaces
    #[test_case(
        "/dev/sys/platform/pt/PCI0/bus/00:14.0/00:14.0/ethernet",
        [0x03, 0x03, 0x03, 0x03, 0x03, 0x03],
        crate::InterfaceType::WlanClient,
        "wlanp0014";
        "pci_wlan"
    )]
    #[test_case(
        "/dev/sys/platform/pt/PCI0/bus/00:15.0/00:14.0/ethernet",
        [0x04, 0x04, 0x04, 0x04, 0x04, 0x04],
        crate::InterfaceType::Ethernet,
        "ethp0015";
        "pci_eth"
    )]
    // platform interfaces (ethernet jack and sdio devices)
    #[test_case(
        "/dev/sys/platform/05:00:6/aml-sd-emmc/sdio/broadcom-wlanphy/wlanphy",
        [0x05, 0x05, 0x05, 0x05, 0x05, 0x05],
        crate::InterfaceType::WlanClient,
        "wlans05006";
        "platform_wlan"
    )]
    #[test_case(
        "/dev/sys/platform/04:02:7/aml-ethernet/Designware-MAC/ethernet",
        [0x07, 0x07, 0x07, 0x07, 0x07, 0x07],
        crate::InterfaceType::Ethernet,
        "ethx7";
        "platform_eth"
    )]
    // unknown interfaces
    #[test_case(
        "/dev/sys/unknown",
        [0x08, 0x08, 0x08, 0x08, 0x08, 0x08],
        crate::InterfaceType::WlanClient,
        "wlanx8";
        "unknown_wlan1"
    )]
    #[test_case(
        "unknown",
        [0x09, 0x09, 0x09, 0x09, 0x09, 0x09],
        crate::InterfaceType::WlanClient,
        "wlanx9";
        "unknown_wlan2"
    )]
    #[test_case(
        "unknown",
        [0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a],
        crate::InterfaceType::WlanAp,
        "apxa";
        "unknown_ap"
    )]
    #[test_case(
        "/dev/sys/platform/pt/PC00/bus/00:1e.0/00_1e_0/virtio-net/network-device",
        [0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b],
        crate::InterfaceType::Ethernet,
        "ethv001e";
        "virtio_attached_ethernet"
    )]

    fn test_generate_name(
        topological_path: &'static str,
        mac: [u8; 6],
        interface_type: crate::InterfaceType,
        want_name: &'static str,
    ) {
        let interface_naming_config = InterfaceNamingConfig::from_naming_rules(vec![]);
        let name = interface_naming_config
            .generate_name(&DeviceInfoRef {
                device_class: device_class_from_interface_type(interface_type),
                mac: &fidl_fuchsia_net_ext::MacAddress { octets: mac },
                topological_path,
            })
            .expect("failed to generate the name");
        assert_eq!(name, want_name);
    }

    struct StableNameTestCase {
        topological_path: &'static str,
        mac: [u8; 6],
        interface_type: crate::InterfaceType,
        want_name: &'static str,
        expected_size: usize,
    }

    // Base case. Interface should be added to config.
    #[test_case([StableNameTestCase {
        topological_path: "/dev/sys/platform/pt/PCI0/bus/00:14.0_/00:14.0/ethernet",
        mac: [0x01, 0x01, 0x01, 0x01, 0x01, 0x01],
        interface_type: crate::InterfaceType::WlanClient,
        want_name: "wlanp0014",
        expected_size: 1 }];
        "single_interface"
    )]
    // Test case that shares the same topo path and different MAC, but same
    // last octet. Expect to see second interface added with different name.
    #[test_case([StableNameTestCase {
        topological_path: "/dev/sys/platform/pt/PCI0/bus/00:14.0_/00:14.0/ethernet",
        mac: [0x01, 0x01, 0x01, 0x01, 0x01, 0x01],
        interface_type: crate::InterfaceType::WlanClient,
        want_name: "wlanp0014",
        expected_size: 1}, StableNameTestCase {
        topological_path: "/dev/sys/platform/pt/PCI0/bus/00:14.0_/00:14.0/ethernet",
        mac: [0xFE, 0x01, 0x01, 0x01, 0x01, 0x01],
        interface_type: crate::InterfaceType::WlanAp,
        want_name: "app0014",
        expected_size: 2 }];
        "two_interfaces_same_topo_path_different_mac"
    )]
    #[test_case([StableNameTestCase {
        topological_path: "/dev/sys/platform/pt/PCI0/bus/00:14.0_/00:14.0/ethernet",
        mac: [0x01, 0x01, 0x01, 0x01, 0x01, 0x01],
        interface_type: crate::InterfaceType::WlanClient,
        want_name: "wlanp0014",
        expected_size: 1}, StableNameTestCase {
        topological_path: "/dev/sys/platform/pt/PCI0/bus/01:00.0/01:00.0/iwlwifi-wlan-softmac/wlan-ethernet/ethernet",
        mac: [0xFE, 0x01, 0x01, 0x01, 0x01, 0x01],
        interface_type: crate::InterfaceType::Ethernet,
        want_name: "ethp01",
        expected_size: 2 }];
        "two_distinct_interfaces"
    )]
    // Test case that labels iwilwifi as ethernet, then changes the device
    // class to wlan. The test should detect that the device class doesn't
    // match the interface name, and overwrite with the new interface name
    // that does match.
    #[test_case([StableNameTestCase {
        topological_path: "/dev/sys/platform/pt/PCI0/bus/01:00.0/01:00.0/iwlwifi-wlan-softmac/wlan-ethernet/ethernet",
        mac: [0x01, 0x01, 0x01, 0x01, 0x01, 0x01],
        interface_type: crate::InterfaceType::Ethernet,
        want_name: "ethp01",
        expected_size: 1 }, StableNameTestCase {
        topological_path: "/dev/sys/platform/pt/PCI0/bus/01:00.0/01:00.0/iwlwifi-wlan-softmac/wlan-ethernet/ethernet",
        mac: [0x01, 0x01, 0x01, 0x01, 0x01, 0x01],
        interface_type: crate::InterfaceType::WlanClient,
        want_name: "wlanp01",
        expected_size: 1 }];
        "two_interfaces_different_device_class"
    )]
    fn test_generate_stable_name(test_cases: impl IntoIterator<Item = StableNameTestCase>) {
        let mut interface_naming_config = InterfaceNamingConfig::from_naming_rules(vec![]);

        // query an existing interface with the same topo path and a different mac address
        for (
            _i,
            StableNameTestCase { topological_path, mac, interface_type, want_name, expected_size },
        ) in test_cases.into_iter().enumerate()
        {
            let (name, _identifier) = interface_naming_config
                .generate_stable_name(
                    topological_path,
                    &fidl_fuchsia_net_ext::MacAddress { octets: mac },
                    device_class_from_interface_type(interface_type),
                )
                .expect("failed to get the interface name");
            assert_eq!(name, want_name);
            // Ensure the number of interfaces we expect are present.
            assert_eq!(interface_naming_config.interfaces.len(), expected_size);
        }
    }

    #[test]
    fn test_get_usb_255() {
        let topo_usb = "/dev/pci-00:14.0-fidl/xhci/usb/004/004/ifc-000/ax88179/ethernet";

        // test cases for 256 usb interfaces
        let mut config = InterfaceNamingConfig::from_naming_rules(vec![]);
        for n in 0u8..255u8 {
            let octets = [n, 0x01, 0x01, 0x01, 0x01, 00];

            let interface_naming_id =
                generate_identifier(&fidl_fuchsia_net_ext::MacAddress { octets });

            let name = config
                .generate_name(&DeviceInfoRef {
                    device_class: device_class_from_interface_type(
                        crate::InterfaceType::WlanClient,
                    ),
                    mac: &fidl_fuchsia_net_ext::MacAddress { octets },
                    topological_path: topo_usb,
                })
                .expect("failed to generate the name");
            assert_eq!(name, format!("{}{:x}", "wlanx", n));
            assert_matches!(config.interfaces.insert(interface_naming_id, name), None);
        }

        let octets = [0x00, 0x00, 0x01, 0x01, 0x01, 00];
        assert!(config
            .generate_name(&DeviceInfoRef {
                device_class: device_class_from_interface_type(crate::InterfaceType::WlanClient),
                mac: &fidl_fuchsia_net_ext::MacAddress { octets },
                topological_path: topo_usb
            },)
            .is_err());
    }

    #[test]
    fn test_get_usb_255_with_naming_rule() {
        let topo_usb = "/dev/pci-00:14.0-fidl/xhci/usb/004/004/ifc-000/ax88179/ethernet";

        let naming_rule = NamingRule {
            matchers: HashSet::new(),
            naming_scheme: vec![
                NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::NormalizedMac },
                NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::NormalizedMac },
            ],
        };

        // test cases for 256 usb interfaces
        let mut config = InterfaceNamingConfig::from_naming_rules(vec![naming_rule]);
        for n in 0u8..255u8 {
            let octets = [n, 0x01, 0x01, 0x01, 0x01, 00];
            let interface_naming_id =
                generate_identifier(&fidl_fuchsia_net_ext::MacAddress { octets });

            let info = DeviceInfoRef {
                device_class: DeviceClass::Ethernet,
                mac: &fidl_fuchsia_net_ext::MacAddress { octets },
                topological_path: topo_usb,
            };

            let name = config.generate_name(&info).expect("failed to generate the name");
            // With only NormalizedMac as a NameCompositionRule, the name
            // should simply be the NormalizedMac itself.
            assert_eq!(name, format!("{n:x}{n:x}"));

            assert_matches!(config.interfaces.insert(interface_naming_id, name), None);
        }

        let octets = [0x00, 0x00, 0x01, 0x01, 0x01, 00];
        assert!(config
            .generate_name(&DeviceInfoRef {
                device_class: DeviceClass::Ethernet,
                mac: &fidl_fuchsia_net_ext::MacAddress { octets },
                topological_path: topo_usb
            })
            .is_err());
    }

    // Arbitrary values for devices::DeviceInfo for cases where DeviceInfo has
    // no impact on the test.
    fn default_device_info() -> DeviceInfoRef<'static> {
        DeviceInfoRef {
            device_class: DeviceClass::Ethernet,
            mac: &fidl_fuchsia_net_ext::MacAddress { octets: [0x1, 0x1, 0x1, 0x1, 0x1, 0x1] },
            topological_path: "",
        }
    }

    #[test_case(
        "/dev/sys/platform/pt/PCI0/bus/00:14.0_/00:14.0/ethernet",
        vec![BusType::PCI],
        BusType::PCI,
        true,
        "0014";
        "pci_match"
    )]
    #[test_case(
        "/dev/sys/platform/pt/PCI0/bus/00:14.0_/00:14.0/ethernet",
        vec![BusType::USB, BusType::SDIO],
        BusType::PCI,
        false,
        "0014";
        "pci_no_match"
    )]
    #[test_case(
        "/dev/sys/platform/pt/PCI0/bus/00:14.0/00:14.0/xhci/usb/004/004/ifc-000/ax88179/ethernet",
        vec![BusType::USB],
        BusType::USB,
        true,
        "0014";
        "pci_usb_match"
    )]
    #[test_case(
        "/dev/sys/platform/05:00:18/usb-phy-composite/aml_usb_phy/dwc2/dwc2_phy/dwc2/usb-peripheral/function-000/cdc-eth-function/netdevice-migration/network-device",
        vec![BusType::USB],
        BusType::USB,
        true,
        "050018";
        "dwc_usb_match"
    )]
    // Same topological path as the case for USB, but with
    // non-matching bus types. Ensure that even though PCI is
    // present in the topological path, it does not match a PCI
    // controller.
    #[test_case(
        "/dev/sys/platform/pt/PCI0/bus/00:14.0/00:14.0/xhci/usb/004/004/ifc-000/ax88179/ethernet",
        vec![BusType::PCI, BusType::SDIO],
        BusType::USB,
        false,
        "0014";
        "usb_no_match"
    )]
    #[test_case(
        "/dev/sys/platform/05:00:6/aml-sd-emmc/sdio/broadcom-wlanphy/wlanphy",
        vec![BusType::SDIO],
        BusType::SDIO,
        true,
        "05006";
        "sdio_match"
    )]
    #[test_case(
        "/dev/sys/platform/pt/PC00/bus/00:1e.0/00_1e_0/virtio-net/network-device",
        vec![BusType::VirtIo],
        BusType::VirtIo,
        true,
        "001e";
        "virtio_match_alternate_location"
    )]
    #[test_case(
        "/dev/sys/platform/pt/PC00/bus/<malformed>/00_1e_0/virtio-net/network-device",
        vec![BusType::VirtIo],
        BusType::VirtIo,
        true,
        "001e";
        "virtio_matches_underscore_path"
    )]
    #[test_case(
        "/dev/sys/platform/pt/PC00/bus/00:1e.1/00_1e_1/virtio-net/network-device",
        vec![BusType::VirtIo],
        BusType::VirtIo,
        true,
        "001e1";
        "virtio_match_alternate_no_trim"
    )]
    #[test_case(
        "/dev/sys/platform/pt/PC00/bus/<unrecognized_bus_path>/network-device",
        vec![BusType::Unknown],
        BusType::Unknown,
        true,
        "ffffff";
        "unknown_bus_match_unrecognized"
    )]
    fn test_interface_matching_and_naming_by_bus_properties(
        topological_path: &'static str,
        bus_types: Vec<BusType>,
        expected_bus_type: BusType,
        want_match: bool,
        want_name: &'static str,
    ) {
        let device_info = DeviceInfoRef {
            topological_path: topological_path,
            // `device_class` and `mac` have no effect on `BusType`
            // matching, so we use arbitrary values.
            ..default_device_info()
        };

        // Verify the `BusType` determined from the device's
        // topological path.
        let bus_type = get_bus_type_for_topological_path(&device_info.topological_path);
        assert_eq!(bus_type, expected_bus_type);

        // Create a matching rule for the provided `BusType` list.
        let matching_rule = MatchingRule::BusTypes(bus_types);
        let does_interface_match = matching_rule.does_interface_match(&device_info).unwrap();
        assert_eq!(does_interface_match, want_match);

        let name = get_normalized_bus_path_for_topo_path(&device_info.topological_path);
        assert_eq!(name, want_name);

        // Ensure that calling again will decrement this. It's unfortunate to need to encode this
        // in the test itself, but each test runs separately, so we can't rely on static storage
        // between test invocations.
        if want_name == "ffffff" {
            let name = get_normalized_bus_path_for_topo_path(&device_info.topological_path);
            assert_eq!(name, "fffffe");
        }
    }

    // Glob matches the number pattern of XX:XX in the path.
    #[test_case(
        "/dev/sys/platform/pt/PCI0/bus/00:14.0_/00:14.0/ethernet",
        r"*[0-9][0-9]:[0-9][0-9]*",
        true;
        "pattern_matches"
    )]
    #[test_case("pattern/will/match/anything", r"*", true; "pattern_matches_any")]
    // Glob checks for '00' after the colon but it will not find it.
    #[test_case(
        "/dev/sys/platform/pt/PCI0/bus/00:14.0_/00:14.0/ethernet",
        r"*[0-9][0-9]:00*",
        false;
        "no_matches"
    )]
    fn test_interface_matching_by_topological_path(
        topological_path: &'static str,
        glob_str: &'static str,
        want_match: bool,
    ) {
        let device_info = DeviceInfoRef {
            topological_path,
            // `device_class` and `mac` have no effect on `TopologicalPath`
            // matching, so we use arbitrary values.
            ..default_device_info()
        };

        // Create a matching rule for the provided glob expression.
        let matching_rule = MatchingRule::TopologicalPath(glob::Pattern::new(glob_str).unwrap());
        let does_interface_match = matching_rule.does_interface_match(&device_info).unwrap();
        assert_eq!(does_interface_match, want_match);
    }

    // Glob matches the default naming by MAC address.
    #[test_case(
        "ethx5",
        r"ethx[0-9]*",
        true;
        "pattern_matches"
    )]
    #[test_case("arbitraryname", r"*", true; "pattern_matches_any")]
    // Glob matches default naming by SDIO + bus path.
    #[test_case(
        "wlans1002",
        r"eths[0-9][0-9][0-9][0-9]*",
        false;
        "no_matches"
    )]
    fn test_interface_matching_by_interface_name(
        interface_name: &'static str,
        glob_str: &'static str,
        want_match: bool,
    ) {
        // Create a matching rule for the provided glob expression.
        let provisioning_matching_rule = ProvisioningMatchingRule::InterfaceName {
            pattern: glob::Pattern::new(glob_str).unwrap(),
        };
        let does_interface_match = provisioning_matching_rule
            .does_interface_match(&default_device_info(), interface_name)
            .unwrap();
        assert_eq!(does_interface_match, want_match);
    }

    #[test_case(
        DeviceClass::Ethernet,
        vec![DeviceClass::Ethernet],
        true;
        "eth_match"
    )]
    #[test_case(
        DeviceClass::Ethernet,
        vec![DeviceClass::WlanClient, DeviceClass::WlanAp],
        false;
        "eth_no_match"
    )]
    #[test_case(
        DeviceClass::WlanClient,
        vec![DeviceClass::WlanClient],
        true;
        "wlan_match"
    )]
    #[test_case(
        DeviceClass::WlanClient,
        vec![DeviceClass::Ethernet, DeviceClass::WlanAp],
        false;
        "wlan_no_match"
    )]
    #[test_case(
        DeviceClass::WlanAp,
        vec![DeviceClass::WlanAp],
        true;
        "ap_match"
    )]
    #[test_case(
        DeviceClass::WlanAp,
        vec![DeviceClass::Ethernet, DeviceClass::WlanClient],
        false;
        "ap_no_match"
    )]
    fn test_interface_matching_by_device_class(
        device_class: DeviceClass,
        device_classes: Vec<DeviceClass>,
        want_match: bool,
    ) {
        let device_info = DeviceInfoRef { device_class, ..default_device_info() };

        // Create a matching rule for the provided `DeviceClass` list.
        let matching_rule = MatchingRule::DeviceClasses(device_classes);
        let does_interface_match = matching_rule.does_interface_match(&device_info).unwrap();
        assert_eq!(does_interface_match, want_match);
    }

    // The device information should not have any impact on whether the
    // interface matches, but we use Ethernet and Wlan as base cases
    // to ensure that all interfaces are accepted or all interfaces
    // are rejected.
    #[test_case(
        DeviceClass::Ethernet,
        "/dev/pci-00:15.0-fidl/xhci/usb/004/004/ifc-000/ax88179/ethernet"
    )]
    #[test_case(DeviceClass::WlanClient, "/dev/pci-00:14.0/ethernet")]
    fn test_interface_matching_by_any_matching_rule(
        device_class: DeviceClass,
        topological_path: &'static str,
    ) {
        let device_info = DeviceInfoRef {
            device_class,
            mac: &fidl_fuchsia_net_ext::MacAddress { octets: [0x1, 0x1, 0x1, 0x1, 0x1, 0x1] },
            topological_path,
        };

        // Create a matching rule that should match any interface.
        let matching_rule = MatchingRule::Any(true);
        let does_interface_match = matching_rule.does_interface_match(&device_info).unwrap();
        assert!(does_interface_match);

        // Create a matching rule that should reject any interface.
        let matching_rule = MatchingRule::Any(false);
        let does_interface_match = matching_rule.does_interface_match(&device_info).unwrap();
        assert!(!does_interface_match);
    }

    #[test_case(
        DeviceInfoRef { device_class: DeviceClass::Ethernet, ..default_device_info() },
        vec![MatchingRule::DeviceClasses(vec![DeviceClass::WlanClient])],
        false;
        "false_single_rule"
    )]
    #[test_case(
        DeviceInfoRef { device_class: DeviceClass::Ethernet, ..default_device_info() },
        vec![MatchingRule::DeviceClasses(vec![DeviceClass::WlanClient]), MatchingRule::Any(true)],
        false;
        "false_one_rule_of_multiple"
    )]
    #[test_case(
        DeviceInfoRef { device_class: DeviceClass::Ethernet, ..default_device_info() },
        vec![MatchingRule::Any(true)],
        true;
        "true_single_rule"
    )]
    #[test_case(
        DeviceInfoRef { device_class: DeviceClass::Ethernet, ..default_device_info() },
        vec![MatchingRule::DeviceClasses(vec![DeviceClass::Ethernet]), MatchingRule::Any(true)],
        true;
        "true_multiple_rules"
    )]
    fn test_does_interface_match(
        info: DeviceInfoRef<'_>,
        matching_rules: Vec<MatchingRule>,
        want_match: bool,
    ) {
        let naming_rule =
            NamingRule { matchers: HashSet::from_iter(matching_rules), naming_scheme: Vec::new() };
        assert_eq!(naming_rule.does_interface_match(&info), want_match);
    }

    #[test_case(
        DeviceInfoRef { device_class: DeviceClass::Ethernet, ..default_device_info() },
        "",
        vec![
            ProvisioningMatchingRule::Common(
                MatchingRule::DeviceClasses(vec![DeviceClass::WlanClient])
            )
        ],
        false;
        "false_single_rule"
    )]
    #[test_case(
        DeviceInfoRef { device_class: DeviceClass::WlanClient, ..default_device_info() },
        "wlanx5009",
        vec![
            ProvisioningMatchingRule::InterfaceName {
                pattern: glob::Pattern::new("ethx*").unwrap()
            },
            ProvisioningMatchingRule::Common(MatchingRule::Any(true))
        ],
        false;
        "false_one_rule_of_multiple"
    )]
    #[test_case(
        DeviceInfoRef { device_class: DeviceClass::Ethernet, ..default_device_info() },
        "",
        vec![ProvisioningMatchingRule::Common(MatchingRule::Any(true))],
        true;
        "true_single_rule"
    )]
    #[test_case(
        DeviceInfoRef { device_class: DeviceClass::Ethernet, ..default_device_info() },
        "wlanx5009",
        vec![
            ProvisioningMatchingRule::Common(
                MatchingRule::DeviceClasses(vec![DeviceClass::Ethernet])
            ),
            ProvisioningMatchingRule::InterfaceName {
                pattern: glob::Pattern::new("wlanx*").unwrap()
            }
        ],
        true;
        "true_multiple_rules"
    )]
    fn test_does_interface_match_provisioning_rule(
        info: DeviceInfoRef<'_>,
        interface_name: &str,
        matching_rules: Vec<ProvisioningMatchingRule>,
        want_match: bool,
    ) {
        let provisioning_rule = ProvisioningRule {
            matchers: HashSet::from_iter(matching_rules),
            provisioning: ProvisioningAction::Local,
        };
        assert_eq!(provisioning_rule.does_interface_match(&info, interface_name), want_match);
    }

    #[test_case(
        vec![NameCompositionRule::Static { value: String::from("x") }],
        default_device_info(),
        "x";
        "single_static"
    )]
    #[test_case(
        vec![
            NameCompositionRule::Static { value: String::from("eth") },
            NameCompositionRule::Static { value: String::from("x") },
            NameCompositionRule::Static { value: String::from("100") },
        ],
        default_device_info(),
        "ethx100";
        "multiple_static"
    )]
    #[test_case(
        vec![NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::NormalizedMac }],
        DeviceInfoRef {
            mac: &fidl_fuchsia_net_ext::MacAddress { octets: [0x1, 0x1, 0x1, 0x1, 0x1, 0x1] },
            ..default_device_info()
        },
        "1";
        "normalized_mac"
    )]
    #[test_case(
        vec![
            NameCompositionRule::Static { value: String::from("eth") },
            NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::NormalizedMac },
        ],
        DeviceInfoRef {
            mac: &fidl_fuchsia_net_ext::MacAddress { octets: [0x1, 0x1, 0x1, 0x1, 0x1, 0x9] },
            ..default_device_info()
        },
        "eth9";
        "normalized_mac_with_static"
    )]
    #[test_case(
        vec![NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::DeviceClass }],
        DeviceInfoRef { device_class: DeviceClass::Ethernet, ..default_device_info() },
        "eth";
        "eth_device_class"
    )]
    #[test_case(
        vec![NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::DeviceClass }],
        DeviceInfoRef { device_class: DeviceClass::WlanClient, ..default_device_info() },
        "wlan";
        "wlan_device_class"
    )]
    #[test_case(
        vec![
            NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::DeviceClass },
            NameCompositionRule::Static { value: String::from("x") },
        ],
        DeviceInfoRef { device_class: DeviceClass::Ethernet, ..default_device_info() },
        "ethx";
        "device_class_with_static"
    )]
    #[test_case(
        vec![
            NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::DeviceClass },
            NameCompositionRule::Static { value: String::from("x") },
            NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::NormalizedMac },
        ],
        DeviceInfoRef {
            device_class: DeviceClass::WlanClient,
            mac: &fidl_fuchsia_net_ext::MacAddress { octets: [0x1, 0x1, 0x1, 0x1, 0x1, 0x8] },
            ..default_device_info()
        },
        "wlanx8";
        "device_class_with_static_with_normalized_mac"
    )]
    #[test_case(
        vec![
            NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::DeviceClass },
            NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::BusType },
            NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::BusPath },
        ],
        DeviceInfoRef {
            device_class: DeviceClass::Ethernet,
            topological_path: "/dev/sys/platform/pt/PCI0/bus/00:14.0_/00:14.0/ethernet",
            ..default_device_info()
        },
        "ethp0014";
        "device_class_with_pci_bus_type_with_bus_path"
    )]
    #[test_case(
        vec![
            NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::DeviceClass },
            NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::BusType },
            NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::BusPath },
        ],
        DeviceInfoRef {
            device_class: DeviceClass::Ethernet,
            topological_path: "/dev/sys/platform/pt/PCI0/bus/00:14.0/00:14.0/xhci/usb/004/004/ifc-000/ax88179/ethernet",
            ..default_device_info()
        },
        "ethu0014";
        "device_class_with_pci_usb_bus_type_with_bus_path"
    )]
    #[test_case(
        vec![
            NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::DeviceClass },
            NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::BusType },
            NameCompositionRule::Dynamic { rule: DynamicNameCompositionRule::BusPath },
        ],
        DeviceInfoRef {
            device_class: DeviceClass::Ethernet,
            topological_path: "/dev/sys/platform/05:00:18/usb-phy-composite/aml_usb_phy/dwc2/dwc2_phy/dwc2/usb-peripheral/function-000/cdc-eth-function/netdevice-migration/network-device",
            ..default_device_info()
        },
        "ethu050018";
        "device_class_with_dwc_usb_bus_type_with_bus_path"
    )]
    #[test_case(
        vec![NameCompositionRule::Default],
        DeviceInfoRef {
            device_class: DeviceClass::Ethernet,
            topological_path: "/dev/sys/platform/pt/PCI0/bus/00:14.0/00:14.0/xhci/usb/004/004/ifc-000/ax88179/ethernet",
            mac: &fidl_fuchsia_net_ext::MacAddress { octets: [0x1, 0x1, 0x1, 0x1, 0x1, 0x2] },
        },
        "ethx2";
        "default_usb_pci"
    )]
    #[test_case(
        vec![NameCompositionRule::Default],
        DeviceInfoRef {
            device_class: DeviceClass::Ethernet,
            topological_path: "/dev/sys/platform/05:00:18/usb-phy-composite/aml_usb_phy/dwc2/dwc2_phy/dwc2/usb-peripheral/function-000/cdc-eth-function/netdevice-migration/network-device",
            mac: &fidl_fuchsia_net_ext::MacAddress { octets: [0x1, 0x1, 0x1, 0x1, 0x1, 0x3] },
        },
        "ethx3";
        "default_usb_dwc"
    )]
    #[test_case(
        vec![NameCompositionRule::Default],
        DeviceInfoRef {
            device_class: DeviceClass::Ethernet,
            topological_path: "/dev/sys/platform/05:00:6/aml-sd-emmc/sdio/broadcom-wlanphy/wlanphy",
            ..default_device_info()
        },
        "eths05006";
        "default_sdio"
    )]
    fn test_naming_rules(
        composition_rules: Vec<NameCompositionRule>,
        info: DeviceInfoRef<'_>,
        expected_name: &'static str,
    ) {
        let naming_rule = NamingRule { matchers: HashSet::new(), naming_scheme: composition_rules };

        let name = naming_rule.generate_name(&HashMap::new(), &info);
        assert_eq!(name.unwrap(), expected_name.to_owned());
    }

    #[test]
    fn test_generate_name_from_naming_rule_interface_name_exists_no_reattempt() {
        let shared_interface_name = "x".to_owned();
        let mut interfaces = HashMap::new();
        assert_matches!(
            interfaces.insert(
                InterfaceNamingIdentifier {
                    mac: fidl_fuchsia_net_ext::MacAddress {
                        octets: [0x1, 0x1, 0x1, 0x1, 0x1, 0x1]
                    },
                },
                shared_interface_name.clone(),
            ),
            None
        );

        let naming_rule = NamingRule {
            matchers: HashSet::new(),
            naming_scheme: vec![NameCompositionRule::Static {
                value: shared_interface_name.clone(),
            }],
        };

        let name = naming_rule.generate_name(&interfaces, &default_device_info()).unwrap();
        assert_eq!(name, shared_interface_name);
    }

    // This test is different from `test_get_usb_255_with_naming_rule` as this
    // test increments the last byte, ensuring that the offset is reset prior
    // to each name being generated.
    #[test]
    fn test_generate_name_from_naming_rule_many_unique_macs() {
        let topo_usb = "/dev/pci-00:14.0-fidl/xhci/usb/004/004/ifc-000/ax88179/ethernet";

        let naming_rule = NamingRule {
            matchers: HashSet::new(),
            naming_scheme: vec![NameCompositionRule::Dynamic {
                rule: DynamicNameCompositionRule::NormalizedMac,
            }],
        };

        // test cases for 256 usb interfaces
        let mut interfaces = HashMap::new();

        for n in 0u8..255u8 {
            let octets = [0x01, 0x01, 0x01, 0x01, 0x01, n];
            let interface_naming_id =
                generate_identifier(&fidl_fuchsia_net_ext::MacAddress { octets });
            let info = DeviceInfoRef {
                device_class: DeviceClass::Ethernet,
                mac: &fidl_fuchsia_net_ext::MacAddress { octets },
                topological_path: topo_usb,
            };

            let name =
                naming_rule.generate_name(&interfaces, &info).expect("failed to generate the name");
            assert_eq!(name, format!("{n:x}"));

            assert_matches!(interfaces.insert(interface_naming_id, name.clone()), None);
        }
    }

    #[test_case(true, "x"; "matches_first_rule")]
    #[test_case(false, "ethx1"; "fallback_default")]
    fn test_generate_name_from_naming_rules(match_first_rule: bool, expected_name: &'static str) {
        // Use an Ethernet device that is determined to have a USB bus type
        // from the topological path.
        let info = DeviceInfoRef {
            device_class: DeviceClass::Ethernet,
            mac: &fidl_fuchsia_net_ext::MacAddress { octets: [0x1, 0x1, 0x1, 0x1, 0x1, 0x1] },
            topological_path: "/dev/sys/platform/pt/PCI0/bus/00:14.0/00:14.0/xhci/usb/004/004/ifc-000/ax88179/ethernet"
        };
        let name = generate_name_from_naming_rules(
            &[
                NamingRule {
                    matchers: HashSet::from([MatchingRule::Any(match_first_rule)]),
                    naming_scheme: vec![NameCompositionRule::Static { value: String::from("x") }],
                },
                // Include an arbitrary rule that matches no interface
                // to ensure that it has no impact on the test.
                NamingRule {
                    matchers: HashSet::from([MatchingRule::Any(false)]),
                    naming_scheme: vec![NameCompositionRule::Static { value: String::from("y") }],
                },
            ],
            &HashMap::new(),
            &info,
        )
        .unwrap();
        assert_eq!(name, expected_name.to_owned());
    }

    #[test_case(true, ProvisioningAction::Delegated; "matches_first_rule")]
    #[test_case(false, ProvisioningAction::Local; "fallback_default")]
    fn test_find_provisioning_action_from_provisioning_rules(
        match_first_rule: bool,
        expected: ProvisioningAction,
    ) {
        let provisioning_action = find_provisioning_action_from_provisioning_rules(
            &[ProvisioningRule {
                matchers: HashSet::from([ProvisioningMatchingRule::Common(MatchingRule::Any(
                    match_first_rule,
                ))]),
                provisioning: ProvisioningAction::Delegated,
            }],
            &DeviceInfoRef {
                device_class: DeviceClass::WlanClient,
                mac: &fidl_fuchsia_net_ext::MacAddress { octets: [0x1, 0x1, 0x1, 0x1, 0x1, 0x1] },
                topological_path: "",
            },
            "wlans5009",
        );
        assert_eq!(provisioning_action, expected);
    }
}