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
// SPDX-License-Identifier: Apache-2.0

use std::collections::{hash_map::Entry, BTreeMap, HashMap};

use ethtool::{
    EthtoolAttr, EthtoolCoalesceAttr, EthtoolFeatureAttr, EthtoolFeatureBit,
    EthtoolHandle, EthtoolHeader, EthtoolLinkModeAttr, EthtoolPauseAttr,
    EthtoolRingAttr,
};
use futures::stream::TryStreamExt;
use serde::{Deserialize, Serialize, Serializer};

use crate::NisporError;

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone, Default)]
#[non_exhaustive]
pub struct EthtoolPauseInfo {
    pub rx: bool,
    pub tx: bool,
    pub auto_negotiate: bool,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub struct EthtoolFeatureInfo {
    #[serde(serialize_with = "ordered_map")]
    pub fixed: HashMap<String, bool>,
    #[serde(serialize_with = "ordered_map")]
    pub changeable: HashMap<String, bool>,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone, Default)]
#[non_exhaustive]
pub struct EthtoolCoalesceInfo {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pkt_rate_high: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pkt_rate_low: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rate_sample_interval: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rx_max_frames: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rx_max_frames_high: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rx_max_frames_irq: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rx_max_frames_low: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rx_usecs: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rx_usecs_high: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rx_usecs_irq: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rx_usecs_low: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stats_block_usecs: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tx_max_frames: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tx_max_frames_high: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tx_max_frames_irq: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tx_max_frames_low: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tx_usecs: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tx_usecs_high: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tx_usecs_irq: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tx_usecs_low: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub use_adaptive_rx: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub use_adaptive_tx: Option<bool>,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone, Default)]
#[non_exhaustive]
pub struct EthtoolRingInfo {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rx: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rx_max: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rx_jumbo: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rx_jumbo_max: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rx_mini: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rx_mini_max: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tx: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tx_max: Option<u32>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum EthtoolLinkModeDuplex {
    Half,
    Full,
    Unknown,
    Other(u8),
}

impl From<&ethtool::EthtoolLinkModeDuplex> for EthtoolLinkModeDuplex {
    fn from(v: &ethtool::EthtoolLinkModeDuplex) -> Self {
        match v {
            ethtool::EthtoolLinkModeDuplex::Half => Self::Half,
            ethtool::EthtoolLinkModeDuplex::Full => Self::Full,
            ethtool::EthtoolLinkModeDuplex::Unknown => Self::Unknown,
            ethtool::EthtoolLinkModeDuplex::Other(d) => Self::Other(*d),
        }
    }
}

impl Default for EthtoolLinkModeDuplex {
    fn default() -> Self {
        EthtoolLinkModeDuplex::Unknown
    }
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone, Default)]
#[non_exhaustive]
pub struct EthtoolLinkModeInfo {
    pub auto_negotiate: bool,
    pub ours: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub peer: Option<Vec<String>>,
    pub speed: u32,
    pub duplex: EthtoolLinkModeDuplex,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub controller_subordinate_cfg: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub controller_subordinate_state: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lanes: Option<u32>,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone, Default)]
#[non_exhaustive]
pub struct EthtoolInfo {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pause: Option<EthtoolPauseInfo>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub features: Option<EthtoolFeatureInfo>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coalesce: Option<EthtoolCoalesceInfo>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ring: Option<EthtoolRingInfo>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub link_mode: Option<EthtoolLinkModeInfo>,
}

fn ordered_map<S>(
    value: &HashMap<String, bool>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    let ordered: BTreeMap<_, _> = value.iter().collect();
    ordered.serialize(serializer)
}

pub(crate) async fn get_ethtool_infos(
) -> Result<HashMap<String, EthtoolInfo>, NisporError> {
    let mut infos: HashMap<String, EthtoolInfo> = HashMap::new();

    let (connection, mut handle, _) = ethtool::new_connection()?;

    tokio::spawn(connection);

    let mut pause_infos = dump_pause_infos(&mut handle).await?;
    let mut feature_infos = dump_feature_infos(&mut handle).await?;
    let mut coalesce_infos = dump_coalesce_infos(&mut handle).await?;
    let mut ring_infos = dump_ring_infos(&mut handle).await?;
    let mut link_mode_infos = dump_link_mode_infos(&mut handle).await?;

    for (iface_name, pause_info) in pause_infos.drain() {
        infos.insert(
            iface_name,
            EthtoolInfo {
                pause: Some(pause_info),
                ..Default::default()
            },
        );
    }

    for (iface_name, feature_info) in feature_infos.drain() {
        match infos.get_mut(&iface_name) {
            Some(ref mut info) => {
                info.features = Some(feature_info);
            }
            None => {
                infos.insert(
                    iface_name,
                    EthtoolInfo {
                        features: Some(feature_info),
                        ..Default::default()
                    },
                );
            }
        };
    }

    for (iface_name, coalesce_info) in coalesce_infos.drain() {
        match infos.get_mut(&iface_name) {
            Some(ref mut info) => {
                info.coalesce = Some(coalesce_info);
            }
            None => {
                infos.insert(
                    iface_name,
                    EthtoolInfo {
                        coalesce: Some(coalesce_info),
                        ..Default::default()
                    },
                );
            }
        };
    }

    for (iface_name, ring_info) in ring_infos.drain() {
        match infos.get_mut(&iface_name) {
            Some(ref mut info) => {
                info.ring = Some(ring_info);
            }
            None => {
                infos.insert(
                    iface_name,
                    EthtoolInfo {
                        ring: Some(ring_info),
                        ..Default::default()
                    },
                );
            }
        };
    }

    for (iface_name, link_mode_info) in link_mode_infos.drain() {
        match infos.get_mut(&iface_name) {
            Some(ref mut info) => {
                info.link_mode = Some(link_mode_info);
            }
            None => {
                infos.insert(
                    iface_name,
                    EthtoolInfo {
                        link_mode: Some(link_mode_info),
                        ..Default::default()
                    },
                );
            }
        };
    }

    Ok(infos)
}

async fn dump_pause_infos(
    handle: &mut EthtoolHandle,
) -> Result<HashMap<String, EthtoolPauseInfo>, NisporError> {
    let mut infos = HashMap::new();
    let mut pause_handle = handle.pause().get(None).execute().await;
    while let Some(genl_msg) = pause_handle.try_next().await? {
        let ethtool_msg = genl_msg.payload;
        let mut iface_name = None;
        let mut pause_info = EthtoolPauseInfo::default();

        for nla in ethtool_msg.nlas.as_slice() {
            if let EthtoolAttr::Pause(nla) = nla {
                if let EthtoolPauseAttr::Header(hdrs) = nla {
                    iface_name = get_iface_name_from_header(hdrs);
                } else if let EthtoolPauseAttr::AutoNeg(v) = nla {
                    pause_info.auto_negotiate = *v
                } else if let EthtoolPauseAttr::Rx(v) = nla {
                    pause_info.rx = *v
                } else if let EthtoolPauseAttr::Tx(v) = nla {
                    pause_info.tx = *v
                }
            }
        }
        if let Some(i) = iface_name {
            infos.insert(i, pause_info);
        }
    }
    Ok(infos)
}

async fn dump_feature_infos(
    handle: &mut EthtoolHandle,
) -> Result<HashMap<String, EthtoolFeatureInfo>, NisporError> {
    let mut infos = HashMap::new();
    let mut feature_handle = handle.feature().get(None).execute().await;
    while let Some(genl_msg) = feature_handle.try_next().await? {
        let ethtool_msg = genl_msg.payload;
        let mut iface_name = None;
        let mut fixed_features: HashMap<String, bool> = HashMap::new();
        let mut changeable_features: HashMap<String, bool> = HashMap::new();

        for nla in ethtool_msg.nlas.as_slice() {
            if let EthtoolAttr::Feature(EthtoolFeatureAttr::NoChange(
                feature_bits,
            )) = nla
            {
                for EthtoolFeatureBit { name, .. } in feature_bits {
                    fixed_features.insert(name.to_string(), false);
                }
            }
        }

        for nla in ethtool_msg.nlas.as_slice() {
            if let EthtoolAttr::Feature(EthtoolFeatureAttr::Header(hdrs)) = nla
            {
                iface_name = get_iface_name_from_header(hdrs);
                break;
            }
        }

        for nla in ethtool_msg.nlas.as_slice() {
            if let EthtoolAttr::Feature(EthtoolFeatureAttr::Hw(feature_bits)) =
                nla
            {
                for feature_bit in feature_bits {
                    match feature_bit {
                        EthtoolFeatureBit {
                            index: _,
                            name,
                            value: true,
                        } => {
                            // Dummy interface show `tx-lockless` is
                            // changeable, but EthtoolFeatureAttr::NoChange() says
                            // otherwise. The kernel code
                            // `NETIF_F_NEVER_CHANGE` shows `tx-lockless`
                            // should never been changeable.
                            if let Entry::Occupied(mut e) =
                                fixed_features.entry(name.clone())
                            {
                                e.insert(false);
                            } else {
                                changeable_features
                                    .insert(name.to_string(), false);
                            }
                        }
                        EthtoolFeatureBit {
                            index: _,
                            name,
                            value: false,
                        } => {
                            fixed_features.insert(name.to_string(), false);
                        }
                    }
                }
            }
        }

        for nla in ethtool_msg.nlas.as_slice() {
            if let EthtoolAttr::Feature(EthtoolFeatureAttr::Active(
                feature_bits,
            )) = nla
            {
                for feature_bit in feature_bits {
                    if let Entry::Occupied(mut e) =
                        fixed_features.entry(feature_bit.name.clone())
                    {
                        e.insert(true);
                    } else if changeable_features
                        .contains_key(&feature_bit.name)
                    {
                        changeable_features
                            .insert(feature_bit.name.clone(), true);
                    }
                }
            }
        }

        if let Some(i) = iface_name {
            infos.insert(
                i.to_string(),
                EthtoolFeatureInfo {
                    fixed: fixed_features,
                    changeable: changeable_features,
                },
            );
        }
    }

    Ok(infos)
}

async fn dump_coalesce_infos(
    handle: &mut EthtoolHandle,
) -> Result<HashMap<String, EthtoolCoalesceInfo>, NisporError> {
    let mut infos = HashMap::new();
    let mut coalesce_handle = handle.coalesce().get(None).execute().await;
    while let Some(genl_msg) = coalesce_handle.try_next().await? {
        let ethtool_msg = genl_msg.payload;
        let mut iface_name = None;
        let mut coalesce_info = EthtoolCoalesceInfo::default();
        for nla in ethtool_msg.nlas.as_slice() {
            if let EthtoolAttr::Coalesce(nla) = nla {
                match nla {
                    EthtoolCoalesceAttr::Header(hdrs) => {
                        iface_name = get_iface_name_from_header(hdrs)
                    }
                    EthtoolCoalesceAttr::RxUsecs(d) => {
                        coalesce_info.rx_usecs = Some(*d)
                    }
                    EthtoolCoalesceAttr::RxMaxFrames(d) => {
                        coalesce_info.rx_max_frames = Some(*d)
                    }
                    EthtoolCoalesceAttr::RxUsecsIrq(d) => {
                        coalesce_info.rx_usecs_irq = Some(*d)
                    }
                    EthtoolCoalesceAttr::RxMaxFramesIrq(d) => {
                        coalesce_info.rx_max_frames_irq = Some(*d)
                    }
                    EthtoolCoalesceAttr::TxUsecs(d) => {
                        coalesce_info.tx_usecs = Some(*d)
                    }
                    EthtoolCoalesceAttr::TxMaxFrames(d) => {
                        coalesce_info.tx_max_frames = Some(*d)
                    }
                    EthtoolCoalesceAttr::TxUsecsIrq(d) => {
                        coalesce_info.tx_usecs_irq = Some(*d)
                    }
                    EthtoolCoalesceAttr::TxMaxFramesIrq(d) => {
                        coalesce_info.tx_max_frames_irq = Some(*d)
                    }
                    EthtoolCoalesceAttr::StatsBlockUsecs(d) => {
                        coalesce_info.stats_block_usecs = Some(*d)
                    }
                    EthtoolCoalesceAttr::UseAdaptiveRx(d) => {
                        coalesce_info.use_adaptive_rx = Some(*d)
                    }
                    EthtoolCoalesceAttr::UseAdaptiveTx(d) => {
                        coalesce_info.use_adaptive_tx = Some(*d)
                    }
                    EthtoolCoalesceAttr::PktRateLow(d) => {
                        coalesce_info.pkt_rate_low = Some(*d)
                    }
                    EthtoolCoalesceAttr::RxUsecsLow(d) => {
                        coalesce_info.rx_usecs_low = Some(*d)
                    }
                    EthtoolCoalesceAttr::RxMaxFramesLow(d) => {
                        coalesce_info.rx_max_frames_low = Some(*d)
                    }
                    EthtoolCoalesceAttr::TxUsecsLow(d) => {
                        coalesce_info.tx_usecs_low = Some(*d)
                    }
                    EthtoolCoalesceAttr::TxMaxFramesLow(d) => {
                        coalesce_info.tx_max_frames_low = Some(*d)
                    }
                    EthtoolCoalesceAttr::PktRateHigh(d) => {
                        coalesce_info.pkt_rate_high = Some(*d)
                    }
                    EthtoolCoalesceAttr::RxUsecsHigh(d) => {
                        coalesce_info.rx_usecs_high = Some(*d)
                    }
                    EthtoolCoalesceAttr::RxMaxFramesHigh(d) => {
                        coalesce_info.rx_max_frames_high = Some(*d)
                    }
                    EthtoolCoalesceAttr::TxUsecsHigh(d) => {
                        coalesce_info.tx_usecs_high = Some(*d)
                    }
                    EthtoolCoalesceAttr::TxMaxFramesHigh(d) => {
                        coalesce_info.tx_max_frames_high = Some(*d)
                    }
                    EthtoolCoalesceAttr::RateSampleInterval(d) => {
                        coalesce_info.rate_sample_interval = Some(*d)
                    }
                    _ => log::warn!(
                        "WARN: Unsupported EthtoolCoalesceAttr {:?}",
                        nla
                    ),
                }
            }
        }
        if let Some(i) = iface_name {
            infos.insert(i, coalesce_info);
        }
    }
    Ok(infos)
}

async fn dump_ring_infos(
    handle: &mut EthtoolHandle,
) -> Result<HashMap<String, EthtoolRingInfo>, NisporError> {
    let mut infos = HashMap::new();
    let mut ring_handle = handle.ring().get(None).execute().await;
    while let Some(genl_msg) = ring_handle.try_next().await? {
        let ethtool_msg = genl_msg.payload;
        let mut iface_name = None;
        let mut ring_info = EthtoolRingInfo::default();
        for nla in ethtool_msg.nlas.as_slice() {
            if let EthtoolAttr::Ring(nla) = nla {
                match nla {
                    EthtoolRingAttr::Header(hdrs) => {
                        iface_name = get_iface_name_from_header(hdrs)
                    }
                    EthtoolRingAttr::RxMax(d) => ring_info.rx_max = Some(*d),
                    EthtoolRingAttr::RxMiniMax(d) => {
                        ring_info.rx_mini_max = Some(*d)
                    }
                    EthtoolRingAttr::RxJumboMax(d) => {
                        ring_info.rx_jumbo_max = Some(*d)
                    }
                    EthtoolRingAttr::TxMax(d) => ring_info.tx_max = Some(*d),
                    EthtoolRingAttr::Rx(d) => ring_info.rx = Some(*d),
                    EthtoolRingAttr::RxMini(d) => ring_info.rx_mini = Some(*d),
                    EthtoolRingAttr::RxJumbo(d) => {
                        ring_info.rx_jumbo = Some(*d)
                    }
                    EthtoolRingAttr::Tx(d) => ring_info.tx = Some(*d),
                    _ => log::warn!(
                        "WARN: Unsupported EthtoolRingAttr {:?}",
                        nla
                    ),
                }
            }
        }
        if let Some(i) = iface_name {
            infos.insert(i, ring_info);
        }
    }
    Ok(infos)
}

async fn dump_link_mode_infos(
    handle: &mut EthtoolHandle,
) -> Result<HashMap<String, EthtoolLinkModeInfo>, NisporError> {
    let mut infos = HashMap::new();
    let mut link_mode_handle = handle.link_mode().get(None).execute().await;
    while let Some(genl_msg) = link_mode_handle.try_next().await? {
        let ethtool_msg = genl_msg.payload;
        let mut iface_name = None;
        let mut link_mode_info = EthtoolLinkModeInfo::default();
        for nla in ethtool_msg.nlas.as_slice() {
            if let EthtoolAttr::LinkMode(nla) = nla {
                match nla {
                    EthtoolLinkModeAttr::Header(hdrs) => {
                        iface_name = get_iface_name_from_header(hdrs)
                    }
                    EthtoolLinkModeAttr::Autoneg(d) => {
                        link_mode_info.auto_negotiate = *d
                    }
                    EthtoolLinkModeAttr::Ours(d) => {
                        link_mode_info.ours.clone_from(d)
                    }
                    EthtoolLinkModeAttr::Peer(d) => {
                        link_mode_info.peer = Some(d.clone())
                    }
                    EthtoolLinkModeAttr::Speed(d) => {
                        link_mode_info.speed =
                            if *d == std::u32::MAX { 0 } else { *d }
                    }
                    EthtoolLinkModeAttr::Duplex(d) => {
                        link_mode_info.duplex = d.into()
                    }
                    EthtoolLinkModeAttr::ControllerSubordinateCfg(d) => {
                        link_mode_info.controller_subordinate_cfg = Some(*d)
                    }
                    EthtoolLinkModeAttr::ControllerSubordinateState(d) => {
                        link_mode_info.controller_subordinate_state = Some(*d)
                    }
                    EthtoolLinkModeAttr::Lanes(d) => {
                        link_mode_info.lanes = Some(*d)
                    }

                    _ => log::warn!(
                        "WARN: Unsupported EthtoolLinkModeAttr {:?}",
                        nla
                    ),
                }
            }
        }
        if let Some(i) = iface_name {
            infos.insert(i, link_mode_info);
        }
    }
    Ok(infos)
}

fn get_iface_name_from_header(hdrs: &[EthtoolHeader]) -> Option<String> {
    for hdr in hdrs {
        if let EthtoolHeader::DevName(iface_name) = hdr {
            return Some(iface_name.to_string());
        }
    }
    None
}