netlink-packet-route 0.32.1

netlink packet types
Documentation
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
// SPDX-License-Identifier: MIT

use netlink_packet_core::{Emitable, Parseable};

use crate::{
    link::{
        BondAdSelect, BondAllPortActive, BondArpAllTargets, BondArpValidate,
        BondFailOverMac, BondLacpRate, BondMode, BondPortState,
        BondPrimaryReselect, BondXmitHashPolicy, ChurnState, InfoBond,
        InfoBondPort, InfoData, InfoKind, InfoPortData, InfoPortKind,
        LacpState, LinkAttribute, LinkFlags, LinkHeader, LinkInfo,
        LinkLayerType, LinkMessage, LinkMessageBuffer, LinkMode, Map,
        MiiStatus, State,
    },
    AddressFamily, RouteNetlinkMessage,
};

#[test]
fn test_bond_link_info() {
    let raw: Vec<u8> = vec![
        0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x43, 0x14, 0x01, 0x00,
        0x00, 0x00, 0x00, 0x00, 0xdc, 0x00, // length 220
        0x12, 0x00, // IFLA_LINKINFO 18
        0x09, 0x00, // length 9
        0x01, 0x00, // IFLA_INFO_KIND 1
        0x62, 0x6f, 0x6e, 0x64, 0x00, // 'bond\0'
        0x00, 0x00, 0x00, // padding
        0xcc, 0x00, // length 204
        0x02, 0x00, // IFLA_INFO_DATA
        0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x08, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x1c, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00,
        0x08, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x09, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x05, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x0d, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x08, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x10, 0x00,
        0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x08, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x13, 0x00,
        0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00,
        0x05, 0x00, 0x1d, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x15, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x05, 0x00, 0x1b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x1e, 0x00,
        0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00,
        0x05, 0x00, 0x21, 0x00, 0x01, 0x00, 0x00, 0x00,
    ];

    let expected = LinkMessage {
        header: LinkHeader {
            interface_family: AddressFamily::Unspec,
            index: 24,
            link_layer_type: LinkLayerType::Ether,
            flags: LinkFlags::Broadcast
                | LinkFlags::Controller
                | LinkFlags::LowerUp
                | LinkFlags::Multicast
                | LinkFlags::Running
                | LinkFlags::Up,
            change_mask: LinkFlags::empty(),
        },
        attributes: vec![LinkAttribute::LinkInfo(vec![
            LinkInfo::Kind(InfoKind::Bond),
            LinkInfo::Data(InfoData::Bond(vec![
                InfoBond::Mode(BondMode::BalanceRr),
                InfoBond::MiiMon(0),
                InfoBond::UpDelay(0),
                InfoBond::DownDelay(0),
                InfoBond::PeerNotifDelay(0),
                InfoBond::UseCarrier(true),
                InfoBond::ArpInterval(0),
                InfoBond::ArpValidate(BondArpValidate::None),
                InfoBond::ArpAllTargets(BondArpAllTargets::Any),
                InfoBond::PrimaryReselect(BondPrimaryReselect::Always),
                InfoBond::FailOverMac(BondFailOverMac::None),
                InfoBond::XmitHashPolicy(BondXmitHashPolicy::Layer2),
                InfoBond::ResendIgmp(1),
                InfoBond::NumPeerNotif(1),
                InfoBond::AllPortsActive(BondAllPortActive::Dropped),
                InfoBond::MinLinks(0),
                InfoBond::LpInterval(1),
                InfoBond::PacketsPerPort(1),
                InfoBond::AdLacpActive(true),
                InfoBond::AdLacpRate(BondLacpRate::Slow),
                InfoBond::AdSelect(BondAdSelect::Stable),
                InfoBond::TlbDynamicLb(true),
                InfoBond::MissedMax(2),
                InfoBond::CoupledControl(true),
                InfoBond::BroadcastNeigh(true),
            ])),
        ])],
    };

    assert_eq!(
        expected,
        LinkMessage::parse(&LinkMessageBuffer::new(&raw)).unwrap()
    );

    let mut buf = vec![0; expected.buffer_len()];

    expected.emit(&mut buf);

    assert_eq!(buf, raw);
}

#[test]
fn test_bond_port_link_info() {
    let raw: Vec<u8> = vec![
        0x00, 0x00, 0x01, 0x00, 0x15, 0x00, 0x00, 0x00, 0x43, 0x18, 0x01, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x12, 0x00, 0x09, 0x00, 0x01, 0x00,
        0x76, 0x65, 0x74, 0x68, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x04, 0x00,
        0x62, 0x6f, 0x6e, 0x64, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x05, 0x00,
        0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x02, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x0a, 0x00, 0x04, 0x00, 0x00, 0x23, 0x45, 0x67, 0x89, 0x1a, 0x00, 0x00,
        0x06, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x09, 0x00,
        0x00, 0x00, 0x00, 0x00,
    ];

    let expected = LinkMessage {
        header: LinkHeader {
            interface_family: AddressFamily::Unspec,
            index: 21,
            link_layer_type: LinkLayerType::Ether,
            flags: LinkFlags::Broadcast
                | LinkFlags::LowerUp
                | LinkFlags::Multicast
                | LinkFlags::Port
                | LinkFlags::Running
                | LinkFlags::Up,
            change_mask: LinkFlags::empty(),
        },
        attributes: vec![LinkAttribute::LinkInfo(vec![
            LinkInfo::Kind(InfoKind::Veth),
            LinkInfo::PortKind(InfoPortKind::Bond),
            LinkInfo::PortData(InfoPortData::BondPort(vec![
                InfoBondPort::BondPortState(BondPortState::Active),
                InfoBondPort::MiiStatus(MiiStatus::Up),
                InfoBondPort::LinkFailureCount(0),
                InfoBondPort::PermHwaddr(vec![
                    0x00, 0x23, 0x45, 0x67, 0x89, 0x1a,
                ]),
                InfoBondPort::QueueId(0),
                InfoBondPort::Prio(0),
            ])),
        ])],
    };

    assert_eq!(
        expected,
        LinkMessage::parse(&LinkMessageBuffer::new(&raw)).unwrap()
    );

    let mut buf = vec![0; expected.buffer_len()];

    expected.emit(&mut buf);

    assert_eq!(buf, raw);
}

#[test]
fn test_parsing_link_bond_port_ad() {
    // Kernel RTM_NEWLINK message of an 802.3ad bond port, captured via
    // nlmon when the port became active. Captured from kernel
    // 7.1.5-arch1-2
    // ```sh
    // sudo modprobe nlmon bonding dummy
    // sudo ip link add nl0 type nlmon
    // sudo ip link set nl0 up
    // sudo tcpdump -i nl0 -w bond_port.pcap &
    // sudo ip link add bond0 type bond mode 802.3ad
    // sudo ip link add bond-d0 type dummy
    // sudo ip link set bond-d0 master bond0
    // sudo ip link set bond0 up
    // sudo pkill tcpdump
    // ```
    let raw: Vec<u8> = vec![
        0x00, 0x00, // family AF_UNSPEC
        0x01, 0x00, // link layer type ARPHRD_ETHER
        0x1a, 0x00, 0x00, 0x00, // interface index 26
        0xc3, 0x08, 0x01, 0x00, // flags
        0x00, 0x00, 0x00, 0x00, // change mask 0
        0x84, 0x00, // length 132
        0x12, 0x00, // IFLA_LINKINFO 18
        0x0a, 0x00, // length 10
        0x01, 0x00, // IFLA_INFO_KIND 1
        0x64, 0x75, 0x6d, 0x6d, 0x79, 0x00, // 'dummy\0'
        0x00, 0x00, // padding
        0x09, 0x00, // length 9
        0x04, 0x00, // IFLA_INFO_PORT_KIND 4
        0x62, 0x6f, 0x6e, 0x64, 0x00, // 'bond\0'
        0x00, 0x00, 0x00, // padding
        0x68, 0x00, // length 104
        0x05, 0x00, // IFLA_INFO_PORT_DATA 5
        // IFLA_BOND_PORT_STATE active(0)
        0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
        // IFLA_BOND_PORT_MII_STATUS up(0)
        0x05, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
        // IFLA_BOND_PORT_LINK_FAILURE_COUNT 0
        0x08, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
        // IFLA_BOND_PORT_PERM_HWADDR
        0x0a, 0x00, 0x04, 0x00, 0xf6, 0xbe, 0x32, 0x7c, 0x99, 0x6b, 0x00, 0x00,
        // IFLA_BOND_PORT_QUEUE_ID 0
        0x06, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
        // IFLA_BOND_PORT_PRIO 0
        0x08, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00,
        // IFLA_BOND_PORT_AD_AGGREGATOR_ID 1
        0x06, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00,
        // IFLA_BOND_PORT_AD_ACTOR_OPER_PORT_STATE 0x45
        // (active, aggregating, defaulted)
        0x05, 0x00, 0x07, 0x00, 0x45, 0x00, 0x00, 0x00,
        // IFLA_BOND_PORT_AD_PARTNER_OPER_PORT_STATE 0
        0x06, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
        // IFLA_BOND_PORT_AD_CHURN_ACTOR_STATE monitor(0)
        0x05, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00,
        // IFLA_BOND_PORT_AD_CHURN_PARTNER_STATE monitor(0)
        0x05, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
        // IFLA_BOND_PORT_ACTOR_PORT_PRIO 255
        0x06, 0x00, 0x0a, 0x00, 0xff, 0x00, 0x00, 0x00,
    ];

    let expected = LinkMessage {
        header: LinkHeader {
            interface_family: AddressFamily::Unspec,
            index: 26,
            link_layer_type: LinkLayerType::Ether,
            flags: LinkFlags::Broadcast
                | LinkFlags::LowerUp
                | LinkFlags::Noarp
                | LinkFlags::Port
                | LinkFlags::Running
                | LinkFlags::Up,
            change_mask: LinkFlags::empty(),
        },
        attributes: vec![LinkAttribute::LinkInfo(vec![
            LinkInfo::Kind(InfoKind::Dummy),
            LinkInfo::PortKind(InfoPortKind::Bond),
            LinkInfo::PortData(InfoPortData::BondPort(vec![
                InfoBondPort::BondPortState(BondPortState::Active),
                InfoBondPort::MiiStatus(MiiStatus::Up),
                InfoBondPort::LinkFailureCount(0),
                InfoBondPort::PermHwaddr(vec![
                    0xf6, 0xbe, 0x32, 0x7c, 0x99, 0x6b,
                ]),
                InfoBondPort::QueueId(0),
                InfoBondPort::Prio(0),
                InfoBondPort::AdAggregatorId(1),
                InfoBondPort::AdActorOperPortState(
                    LacpState::LACP_ACTIVITY
                        | LacpState::AGGREGATION
                        | LacpState::DEFAULTED,
                ),
                InfoBondPort::AdPartnerOperPortState(LacpState::empty()),
                InfoBondPort::AdChurnActorState(ChurnState::Monitor),
                InfoBondPort::AdChurnPartnerState(ChurnState::Monitor),
                InfoBondPort::ActorPortPrio(255),
            ])),
        ])],
    };

    assert_eq!(
        expected,
        LinkMessage::parse(&LinkMessageBuffer::new(&raw)).unwrap()
    );

    let mut buf = vec![0; expected.buffer_len()];

    expected.emit(&mut buf);

    assert_eq!(buf, raw);
}

#[test]
fn test_bond_arp_validate() {
    let raw: Vec<u8> = vec![
        0x00, 0x00, 0x01, 0x00, 0x05, 0x00, 0x00, 0x00, 0x02, 0x14, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x03, 0x00, 0x62, 0x6f, 0x6e, 0x64,
        0x30, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0d, 0x00, 0xe8, 0x03, 0x00, 0x00,
        0x05, 0x00, 0x10, 0x00, 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x11, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, 0xdc, 0x05, 0x00, 0x00,
        0x08, 0x00, 0x32, 0x00, 0x44, 0x00, 0x00, 0x00, 0x08, 0x00, 0x33, 0x00,
        0xff, 0xff, 0x00, 0x00, 0x08, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x08, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x1f, 0x00,
        0x10, 0x00, 0x00, 0x00, 0x08, 0x00, 0x28, 0x00, 0xff, 0xff, 0x00, 0x00,
        0x08, 0x00, 0x29, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x20, 0x00,
        0x10, 0x00, 0x00, 0x00, 0x05, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x09, 0x00, 0x06, 0x00, 0x6e, 0x6f, 0x6f, 0x70, 0x00, 0x00, 0x00, 0x00,
        0x08, 0x00, 0x23, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x2f, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x30, 0x00, 0x01, 0x00, 0x00, 0x00,
        0x05, 0x00, 0x27, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x0e, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x01, 0x00,
        0x92, 0xb3, 0xba, 0x20, 0xd7, 0xa0, 0x00, 0x00, 0x0a, 0x00, 0x02, 0x00,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
    ];
    let expected = LinkMessage {
        header: LinkHeader {
            interface_family: AddressFamily::Unspec,
            index: 5,
            link_layer_type: LinkLayerType::Ether,
            flags: LinkFlags::Broadcast
                | LinkFlags::Controller
                | LinkFlags::Multicast,
            change_mask: LinkFlags::empty(),
        },
        attributes: vec![
            LinkAttribute::IfName("bond0".into()),
            LinkAttribute::TxQueueLen(1000),
            LinkAttribute::OperState(State::Down),
            LinkAttribute::Mode(LinkMode::Default),
            LinkAttribute::Mtu(1500),
            LinkAttribute::MinMtu(68),
            LinkAttribute::MaxMtu(65535),
            LinkAttribute::Group(0),
            LinkAttribute::Promiscuity(0),
            LinkAttribute::NumTxQueues(16),
            LinkAttribute::GsoMaxSegs(65535),
            LinkAttribute::GsoMaxSize(65536),
            LinkAttribute::NumRxQueues(16),
            LinkAttribute::Carrier(0),
            LinkAttribute::Qdisc("noop".into()),
            LinkAttribute::CarrierChanges(1),
            LinkAttribute::CarrierUpCount(0),
            LinkAttribute::CarrierDownCount(1),
            LinkAttribute::ProtoDown(0),
            LinkAttribute::Map(Map {
                memory_start: 0,
                memory_end: 0,
                base_address: 0,
                irq: 0,
                dma: 0,
                port: 0,
            }),
            LinkAttribute::Address(vec![0x92, 0xb3, 0xba, 0x20, 0xd7, 0xa0]),
            LinkAttribute::Broadcast(vec![0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
        ],
    };

    let packet = RouteNetlinkMessage::NewLink(expected);
    let mut buf = vec![0u8; packet.buffer_len()];
    packet.emit(&mut buf);

    assert_eq!(raw, buf);
}

#[test]
fn test_bond_mode_from_str() {
    use std::str::FromStr;
    assert_eq!(BondMode::BalanceRr, "balance-rr".parse().unwrap());
    assert_eq!(BondMode::ActiveBackup, "active-backup".parse().unwrap());
    assert_eq!(BondMode::BalanceXor, "balance-xor".parse().unwrap());
    assert_eq!(BondMode::Broadcast, "broadcast".parse().unwrap());
    assert_eq!(BondMode::Ieee8023Ad, "802.3ad".parse().unwrap());
    assert_eq!(BondMode::BalanceTlb, "balance-tlb".parse().unwrap());
    assert_eq!(BondMode::BalanceAlb, "balance-alb".parse().unwrap());
    assert_eq!(BondMode::BalanceRr, "0".parse().unwrap());
    assert_eq!(BondMode::ActiveBackup, "1".parse().unwrap());
    assert_eq!(BondMode::BalanceXor, "2".parse().unwrap());
    assert_eq!(BondMode::Broadcast, "3".parse().unwrap());
    assert_eq!(BondMode::Ieee8023Ad, "4".parse().unwrap());
    assert_eq!(BondMode::BalanceTlb, "5".parse().unwrap());
    assert_eq!(BondMode::BalanceAlb, "6".parse().unwrap());
    assert!(BondMode::from_str("bogus").is_err());
}

#[test]
fn test_bond_arp_validate_from_str() {
    use std::str::FromStr;
    assert_eq!(BondArpValidate::None, "none".parse().unwrap());
    assert_eq!(BondArpValidate::Active, "active".parse().unwrap());
    assert_eq!(BondArpValidate::Backup, "backup".parse().unwrap());
    assert_eq!(BondArpValidate::All, "all".parse().unwrap());
    assert_eq!(BondArpValidate::Filter, "filter".parse().unwrap());
    assert_eq!(
        BondArpValidate::FilterActive,
        "filter_active".parse().unwrap()
    );
    assert_eq!(
        BondArpValidate::FilterBackup,
        "filter_backup".parse().unwrap()
    );
    assert_eq!(BondArpValidate::None, "0".parse().unwrap());
    assert_eq!(BondArpValidate::Active, "1".parse().unwrap());
    assert_eq!(BondArpValidate::Backup, "2".parse().unwrap());
    assert_eq!(BondArpValidate::All, "3".parse().unwrap());
    assert_eq!(BondArpValidate::Filter, "4".parse().unwrap());
    assert_eq!(BondArpValidate::FilterActive, "5".parse().unwrap());
    assert_eq!(BondArpValidate::FilterBackup, "6".parse().unwrap());
    assert!(BondArpValidate::from_str("bogus").is_err());
}

#[test]
fn test_bond_primary_reselect_from_str() {
    use std::str::FromStr;
    assert_eq!(BondPrimaryReselect::Always, "always".parse().unwrap());
    assert_eq!(BondPrimaryReselect::Better, "better".parse().unwrap());
    assert_eq!(BondPrimaryReselect::Failure, "failure".parse().unwrap());
    assert_eq!(BondPrimaryReselect::Always, "0".parse().unwrap());
    assert_eq!(BondPrimaryReselect::Better, "1".parse().unwrap());
    assert_eq!(BondPrimaryReselect::Failure, "2".parse().unwrap());
    assert!(BondPrimaryReselect::from_str("bogus").is_err());
}

#[test]
fn test_bond_xmit_hash_policy_from_str() {
    use std::str::FromStr;
    assert_eq!(BondXmitHashPolicy::Layer2, "layer2".parse().unwrap());
    assert_eq!(BondXmitHashPolicy::Layer34, "layer3+4".parse().unwrap());
    assert_eq!(BondXmitHashPolicy::Layer23, "layer2+3".parse().unwrap());
    assert_eq!(BondXmitHashPolicy::Encap23, "encap2+3".parse().unwrap());
    assert_eq!(BondXmitHashPolicy::Encap34, "encap3+4".parse().unwrap());
    assert_eq!(
        BondXmitHashPolicy::VlanSrcMac,
        "vlan+srcmac".parse().unwrap()
    );
    assert_eq!(BondXmitHashPolicy::Layer2, "0".parse().unwrap());
    assert_eq!(BondXmitHashPolicy::Layer34, "1".parse().unwrap());
    assert_eq!(BondXmitHashPolicy::Layer23, "2".parse().unwrap());
    assert_eq!(BondXmitHashPolicy::Encap23, "3".parse().unwrap());
    assert_eq!(BondXmitHashPolicy::Encap34, "4".parse().unwrap());
    assert_eq!(BondXmitHashPolicy::VlanSrcMac, "5".parse().unwrap());
    assert!(BondXmitHashPolicy::from_str("bogus").is_err());
}

#[test]
fn test_bond_arp_all_targets_from_str() {
    use std::str::FromStr;
    assert_eq!(BondArpAllTargets::Any, "any".parse().unwrap());
    assert_eq!(BondArpAllTargets::All, "all".parse().unwrap());
    assert_eq!(BondArpAllTargets::Any, "0".parse().unwrap());
    assert_eq!(BondArpAllTargets::All, "1".parse().unwrap());
    assert!(BondArpAllTargets::from_str("bogus").is_err());
}

#[test]
fn test_bond_fail_over_mac_from_str() {
    use std::str::FromStr;
    assert_eq!(BondFailOverMac::None, "none".parse().unwrap());
    assert_eq!(BondFailOverMac::Active, "active".parse().unwrap());
    assert_eq!(BondFailOverMac::Follow, "follow".parse().unwrap());
    assert_eq!(BondFailOverMac::None, "0".parse().unwrap());
    assert_eq!(BondFailOverMac::Active, "1".parse().unwrap());
    assert_eq!(BondFailOverMac::Follow, "2".parse().unwrap());
    assert!(BondFailOverMac::from_str("bogus").is_err());
}

#[test]
fn test_bond_ad_select_from_str() {
    use std::str::FromStr;
    assert_eq!(BondAdSelect::Stable, "stable".parse().unwrap());
    assert_eq!(BondAdSelect::Bandwidth, "bandwidth".parse().unwrap());
    assert_eq!(BondAdSelect::Count, "count".parse().unwrap());
    assert_eq!(
        BondAdSelect::ActorPortPrio,
        "actor_port_prio".parse().unwrap()
    );
    assert_eq!(BondAdSelect::Stable, "0".parse().unwrap());
    assert_eq!(BondAdSelect::Bandwidth, "1".parse().unwrap());
    assert_eq!(BondAdSelect::Count, "2".parse().unwrap());
    assert_eq!(BondAdSelect::ActorPortPrio, "3".parse().unwrap());
    assert!(BondAdSelect::from_str("bogus").is_err());
}

#[test]
fn test_bond_lacp_rate_from_str() {
    use std::str::FromStr;
    assert_eq!(BondLacpRate::Slow, "slow".parse().unwrap());
    assert_eq!(BondLacpRate::Fast, "fast".parse().unwrap());
    assert_eq!(BondLacpRate::Slow, "0".parse().unwrap());
    assert_eq!(BondLacpRate::Fast, "1".parse().unwrap());
    assert!(BondLacpRate::from_str("bogus").is_err());
}

#[test]
fn test_bond_all_ports_active_from_str() {
    use std::str::FromStr;
    assert_eq!(BondAllPortActive::Dropped, "dropped".parse().unwrap());
    assert_eq!(BondAllPortActive::Delivered, "delivered".parse().unwrap());
    assert_eq!(BondAllPortActive::Dropped, "0".parse().unwrap());
    assert_eq!(BondAllPortActive::Delivered, "1".parse().unwrap());
    assert!(BondAllPortActive::from_str("bogus").is_err());
}

#[test]
fn test_bond_all_ports_active_display() {
    assert_eq!("dropped", BondAllPortActive::Dropped.to_string());
    assert_eq!("delivered", BondAllPortActive::Delivered.to_string());
    assert_eq!("255", BondAllPortActive::Other(255).to_string());
}