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
// SPDX-License-Identifier: MIT
use crate::link::{BridgeId, BridgeIdBuffer};
use anyhow::Context;
use byteorder::{ByteOrder, NativeEndian};
use netlink_packet_utils::{
    nla::{DefaultNla, Nla, NlaBuffer},
    parsers::{parse_u16, parse_u32, parse_u64, parse_u8},
    traits::{Emitable, Parseable},
    DecodeError,
};

const IFLA_BRPORT_STATE: u16 = 1;
const IFLA_BRPORT_PRIORITY: u16 = 2;
const IFLA_BRPORT_COST: u16 = 3;
const IFLA_BRPORT_MODE: u16 = 4;
const IFLA_BRPORT_GUARD: u16 = 5;
const IFLA_BRPORT_PROTECT: u16 = 6;
const IFLA_BRPORT_FAST_LEAVE: u16 = 7;
const IFLA_BRPORT_LEARNING: u16 = 8;
const IFLA_BRPORT_UNICAST_FLOOD: u16 = 9;
const IFLA_BRPORT_PROXYARP: u16 = 10;
// only used in one driver, we don't know if its type is stable:
// const IFLA_BRPORT_LEARNING_SYNC: u16 = 11;
const IFLA_BRPORT_PROXYARP_WIFI: u16 = 12;
const IFLA_BRPORT_ROOT_ID: u16 = 13;
const IFLA_BRPORT_BRIDGE_ID: u16 = 14;
const IFLA_BRPORT_DESIGNATED_PORT: u16 = 15;
const IFLA_BRPORT_DESIGNATED_COST: u16 = 16;
const IFLA_BRPORT_ID: u16 = 17;
const IFLA_BRPORT_NO: u16 = 18;
const IFLA_BRPORT_TOPOLOGY_CHANGE_ACK: u16 = 19;
const IFLA_BRPORT_CONFIG_PENDING: u16 = 20;
const IFLA_BRPORT_MESSAGE_AGE_TIMER: u16 = 21;
const IFLA_BRPORT_FORWARD_DELAY_TIMER: u16 = 22;
const IFLA_BRPORT_HOLD_TIMER: u16 = 23;
const IFLA_BRPORT_FLUSH: u16 = 24;
const IFLA_BRPORT_MULTICAST_ROUTER: u16 = 25;
// const IFLA_BRPORT_PAD: u16 = 26;
const IFLA_BRPORT_MCAST_FLOOD: u16 = 27;
const IFLA_BRPORT_MCAST_TO_UCAST: u16 = 28;
const IFLA_BRPORT_VLAN_TUNNEL: u16 = 29;
const IFLA_BRPORT_BCAST_FLOOD: u16 = 30;
const IFLA_BRPORT_GROUP_FWD_MASK: u16 = 31;
const IFLA_BRPORT_NEIGH_SUPPRESS: u16 = 32;
const IFLA_BRPORT_ISOLATED: u16 = 33;
const IFLA_BRPORT_BACKUP_PORT: u16 = 34;
const IFLA_BRPORT_MRP_RING_OPEN: u16 = 35;
const IFLA_BRPORT_MRP_IN_OPEN: u16 = 36;
const IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT: u16 = 37;
const IFLA_BRPORT_MCAST_EHT_HOSTS_CNT: u16 = 38;
const IFLA_BRPORT_LOCKED: u16 = 39;
const IFLA_BRPORT_MAB: u16 = 40;
const IFLA_BRPORT_MCAST_N_GROUPS: u16 = 41;
const IFLA_BRPORT_MCAST_MAX_GROUPS: u16 = 42;
const IFLA_BRPORT_NEIGH_VLAN_SUPPRESS: u16 = 43;
const IFLA_BRPORT_BACKUP_NHID: u16 = 44;

#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum InfoBridgePort {
    State(BridgePortState),
    Priority(u16),
    Cost(u32),
    HairpinMode(bool),
    Guard(bool),
    Protect(bool),
    FastLeave(bool),
    Learning(bool),
    UnicastFlood(bool),
    ProxyARP(bool),
    ProxyARPWifi(bool),
    RootId(BridgeId),
    BridgeId(BridgeId),
    DesignatedPort(u16),
    DesignatedCost(u16),
    PortId(u16),
    PortNumber(u16),
    TopologyChangeAck(bool),
    ConfigPending(bool),
    MessageAgeTimer(u64),
    ForwardDelayTimer(u64),
    HoldTimer(u64),
    Flush,
    MulticastRouter(BridgePortMulticastRouter),
    MulticastFlood(bool),
    MulticastToUnicast(bool),
    VlanTunnel(bool),
    BroadcastFlood(bool),
    GroupFwdMask(u16),
    NeighSupress(bool),
    Isolated(bool),
    BackupPort(u32),
    MrpRingOpen(bool),
    MrpInOpen(bool),
    MulticastEhtHostsLimit(u32),
    MulticastEhtHostsCnt(u32),
    Locked(bool),
    Mab(bool),
    MulticastNGroups(u32),
    MulticastMaxGroups(u32),
    NeighVlanSupress(bool),
    BackupNextHopId(u32),
    Other(DefaultNla),
}

impl Nla for InfoBridgePort {
    fn value_len(&self) -> usize {
        match self {
            InfoBridgePort::Flush => 0,
            InfoBridgePort::State(_)
            | InfoBridgePort::HairpinMode(_)
            | InfoBridgePort::Guard(_)
            | InfoBridgePort::Protect(_)
            | InfoBridgePort::FastLeave(_)
            | InfoBridgePort::Learning(_)
            | InfoBridgePort::UnicastFlood(_)
            | InfoBridgePort::ProxyARP(_)
            | InfoBridgePort::ProxyARPWifi(_)
            | InfoBridgePort::TopologyChangeAck(_)
            | InfoBridgePort::ConfigPending(_)
            | InfoBridgePort::MulticastRouter(_)
            | InfoBridgePort::MulticastFlood(_)
            | InfoBridgePort::MulticastToUnicast(_)
            | InfoBridgePort::VlanTunnel(_)
            | InfoBridgePort::BroadcastFlood(_)
            | InfoBridgePort::NeighSupress(_)
            | InfoBridgePort::Isolated(_)
            | InfoBridgePort::MrpRingOpen(_)
            | InfoBridgePort::MrpInOpen(_)
            | InfoBridgePort::Locked(_)
            | InfoBridgePort::Mab(_)
            | InfoBridgePort::NeighVlanSupress(_) => 1,
            InfoBridgePort::Priority(_)
            | InfoBridgePort::DesignatedPort(_)
            | InfoBridgePort::DesignatedCost(_)
            | InfoBridgePort::PortId(_)
            | InfoBridgePort::PortNumber(_)
            | InfoBridgePort::GroupFwdMask(_) => 2,
            InfoBridgePort::Cost(_)
            | InfoBridgePort::BackupPort(_)
            | InfoBridgePort::MulticastEhtHostsLimit(_)
            | InfoBridgePort::MulticastEhtHostsCnt(_)
            | InfoBridgePort::MulticastNGroups(_)
            | InfoBridgePort::MulticastMaxGroups(_)
            | InfoBridgePort::BackupNextHopId(_) => 4,
            InfoBridgePort::RootId(_)
            | InfoBridgePort::BridgeId(_)
            | InfoBridgePort::MessageAgeTimer(_)
            | InfoBridgePort::ForwardDelayTimer(_)
            | InfoBridgePort::HoldTimer(_) => 8,
            InfoBridgePort::Other(nla) => nla.value_len(),
        }
    }

    fn emit_value(&self, buffer: &mut [u8]) {
        match self {
            InfoBridgePort::Flush => (),
            InfoBridgePort::HairpinMode(value)
            | InfoBridgePort::Guard(value)
            | InfoBridgePort::Protect(value)
            | InfoBridgePort::FastLeave(value)
            | InfoBridgePort::Learning(value)
            | InfoBridgePort::UnicastFlood(value)
            | InfoBridgePort::ProxyARP(value)
            | InfoBridgePort::TopologyChangeAck(value)
            | InfoBridgePort::ConfigPending(value)
            | InfoBridgePort::ProxyARPWifi(value)
            | InfoBridgePort::MulticastFlood(value)
            | InfoBridgePort::MulticastToUnicast(value)
            | InfoBridgePort::VlanTunnel(value)
            | InfoBridgePort::BroadcastFlood(value)
            | InfoBridgePort::NeighSupress(value)
            | InfoBridgePort::Isolated(value)
            | InfoBridgePort::MrpRingOpen(value)
            | InfoBridgePort::MrpInOpen(value)
            | InfoBridgePort::Locked(value)
            | InfoBridgePort::Mab(value)
            | InfoBridgePort::NeighVlanSupress(value) => {
                buffer[0] = if *value { 1 } else { 0 }
            }
            InfoBridgePort::Priority(value)
            | InfoBridgePort::DesignatedPort(value)
            | InfoBridgePort::DesignatedCost(value)
            | InfoBridgePort::PortId(value)
            | InfoBridgePort::PortNumber(value)
            | InfoBridgePort::GroupFwdMask(value) => {
                NativeEndian::write_u16(buffer, *value)
            }
            InfoBridgePort::Cost(value)
            | InfoBridgePort::BackupPort(value)
            | InfoBridgePort::MulticastEhtHostsLimit(value)
            | InfoBridgePort::MulticastEhtHostsCnt(value)
            | InfoBridgePort::MulticastNGroups(value)
            | InfoBridgePort::MulticastMaxGroups(value)
            | InfoBridgePort::BackupNextHopId(value) => {
                NativeEndian::write_u32(buffer, *value)
            }
            InfoBridgePort::MessageAgeTimer(value)
            | InfoBridgePort::ForwardDelayTimer(value)
            | InfoBridgePort::HoldTimer(value) => {
                NativeEndian::write_u64(buffer, *value)
            }
            InfoBridgePort::RootId(bridge_id)
            | InfoBridgePort::BridgeId(bridge_id) => bridge_id.emit(buffer),
            InfoBridgePort::State(state) => buffer[0] = (*state).into(),
            InfoBridgePort::MulticastRouter(mcast_router) => {
                buffer[0] = (*mcast_router).into()
            }
            InfoBridgePort::Other(nla) => nla.emit_value(buffer),
        }
    }

    fn kind(&self) -> u16 {
        match self {
            InfoBridgePort::State(_) => IFLA_BRPORT_STATE,
            InfoBridgePort::Priority(_) => IFLA_BRPORT_PRIORITY,
            InfoBridgePort::Cost(_) => IFLA_BRPORT_COST,
            InfoBridgePort::HairpinMode(_) => IFLA_BRPORT_MODE,
            InfoBridgePort::Guard(_) => IFLA_BRPORT_GUARD,
            InfoBridgePort::Protect(_) => IFLA_BRPORT_PROTECT,
            InfoBridgePort::FastLeave(_) => IFLA_BRPORT_FAST_LEAVE,
            InfoBridgePort::Learning(_) => IFLA_BRPORT_LEARNING,
            InfoBridgePort::UnicastFlood(_) => IFLA_BRPORT_UNICAST_FLOOD,
            InfoBridgePort::ProxyARP(_) => IFLA_BRPORT_PROXYARP,
            InfoBridgePort::ProxyARPWifi(_) => IFLA_BRPORT_PROXYARP_WIFI,
            InfoBridgePort::RootId(_) => IFLA_BRPORT_ROOT_ID,
            InfoBridgePort::BridgeId(_) => IFLA_BRPORT_BRIDGE_ID,
            InfoBridgePort::DesignatedPort(_) => IFLA_BRPORT_DESIGNATED_PORT,
            InfoBridgePort::DesignatedCost(_) => IFLA_BRPORT_DESIGNATED_COST,
            InfoBridgePort::PortId(_) => IFLA_BRPORT_ID,
            InfoBridgePort::PortNumber(_) => IFLA_BRPORT_NO,
            InfoBridgePort::TopologyChangeAck(_) => {
                IFLA_BRPORT_TOPOLOGY_CHANGE_ACK
            }
            InfoBridgePort::ConfigPending(_) => IFLA_BRPORT_CONFIG_PENDING,
            InfoBridgePort::MessageAgeTimer(_) => IFLA_BRPORT_MESSAGE_AGE_TIMER,
            InfoBridgePort::ForwardDelayTimer(_) => {
                IFLA_BRPORT_FORWARD_DELAY_TIMER
            }
            InfoBridgePort::HoldTimer(_) => IFLA_BRPORT_HOLD_TIMER,
            InfoBridgePort::Flush => IFLA_BRPORT_FLUSH,
            InfoBridgePort::MulticastRouter(_) => IFLA_BRPORT_MULTICAST_ROUTER,
            InfoBridgePort::MulticastFlood(_) => IFLA_BRPORT_MCAST_FLOOD,
            InfoBridgePort::MulticastToUnicast(_) => IFLA_BRPORT_MCAST_TO_UCAST,
            InfoBridgePort::VlanTunnel(_) => IFLA_BRPORT_VLAN_TUNNEL,
            InfoBridgePort::BroadcastFlood(_) => IFLA_BRPORT_BCAST_FLOOD,
            InfoBridgePort::GroupFwdMask(_) => IFLA_BRPORT_GROUP_FWD_MASK,
            InfoBridgePort::NeighSupress(_) => IFLA_BRPORT_NEIGH_SUPPRESS,
            InfoBridgePort::Isolated(_) => IFLA_BRPORT_ISOLATED,
            InfoBridgePort::BackupPort(_) => IFLA_BRPORT_BACKUP_PORT,
            InfoBridgePort::MrpRingOpen(_) => IFLA_BRPORT_MRP_RING_OPEN,
            InfoBridgePort::MrpInOpen(_) => IFLA_BRPORT_MRP_IN_OPEN,
            InfoBridgePort::MulticastEhtHostsLimit(_) => {
                IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT
            }
            InfoBridgePort::MulticastEhtHostsCnt(_) => {
                IFLA_BRPORT_MCAST_EHT_HOSTS_CNT
            }
            InfoBridgePort::Locked(_) => IFLA_BRPORT_LOCKED,
            InfoBridgePort::Mab(_) => IFLA_BRPORT_MAB,
            InfoBridgePort::MulticastNGroups(_) => IFLA_BRPORT_MCAST_N_GROUPS,
            InfoBridgePort::MulticastMaxGroups(_) => {
                IFLA_BRPORT_MCAST_MAX_GROUPS
            }
            InfoBridgePort::NeighVlanSupress(_) => {
                IFLA_BRPORT_NEIGH_VLAN_SUPPRESS
            }
            InfoBridgePort::BackupNextHopId(_) => IFLA_BRPORT_BACKUP_NHID,
            InfoBridgePort::Other(nla) => nla.kind(),
        }
    }
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
    for InfoBridgePort
{
    fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
        let payload = buf.value();

        Ok(match buf.kind() {
            IFLA_BRPORT_STATE => InfoBridgePort::State(
                parse_u8(payload)
                    .with_context(|| {
                        format!("invalid IFLA_BRPORT_STATE {payload:?}")
                    })?
                    .into(),
            ),
            IFLA_BRPORT_PRIORITY => {
                InfoBridgePort::Priority(parse_u16(payload).with_context(
                    || format!("invalid IFLA_BRPORT_PRIORITY {payload:?}"),
                )?)
            }
            IFLA_BRPORT_COST => {
                InfoBridgePort::Cost(parse_u32(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_COST {payload:?}")
                })?)
            }
            IFLA_BRPORT_MODE => InfoBridgePort::HairpinMode(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_MODE {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_GUARD => InfoBridgePort::Guard(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_GUARD {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_PROTECT => InfoBridgePort::Protect(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_PROTECT {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_FAST_LEAVE => InfoBridgePort::FastLeave(
                parse_u8(payload).with_context(|| {
                    format!(
                        "invalid IFLA_BRPORT_FAST_LEAVE {payload:?}"
                    )
                })? > 0,
            ),
            IFLA_BRPORT_LEARNING => InfoBridgePort::Learning(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_LEARNING {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_UNICAST_FLOOD => InfoBridgePort::UnicastFlood(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_UNICAST_FLOOD {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_PROXYARP => InfoBridgePort::ProxyARP(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_PROXYARP {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_PROXYARP_WIFI => InfoBridgePort::ProxyARPWifi(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_PROXYARP_WIFI {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_ROOT_ID => Self::RootId(
                BridgeId::parse(&BridgeIdBuffer::new(payload)).with_context(|| {
                    format!("invalid IFLA_BRPORT_ROOT_ID {payload:?}")
                })?,
            ),
            IFLA_BRPORT_BRIDGE_ID => Self::BridgeId(
                BridgeId::parse(&BridgeIdBuffer::new(payload)).with_context(|| {
                    format!("invalid IFLA_BRPORT_BRIDGE_ID {payload:?}")
                })?,
            ),
            IFLA_BRPORT_DESIGNATED_PORT => InfoBridgePort::DesignatedPort(
                parse_u16(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_DESIGNATED_PORT {payload:?}")
                })?,
            ),
            IFLA_BRPORT_DESIGNATED_COST => InfoBridgePort::DesignatedCost(
                parse_u16(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_DESIGNATED_COST {payload:?}")
                })?,
            ),
            IFLA_BRPORT_ID => {
                InfoBridgePort::PortId(parse_u16(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_ID {payload:?}")
                })?)
            }
            IFLA_BRPORT_NO => {
                InfoBridgePort::PortNumber(parse_u16(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_NO {payload:?}")
                })?)
            }
            IFLA_BRPORT_TOPOLOGY_CHANGE_ACK => {
                InfoBridgePort::TopologyChangeAck(
                    parse_u8(payload).with_context(|| {
                        format!(
                            "invalid IFLA_BRPORT_TOPOLOGY_CHANGE_ACK {payload:?}"
                        )
                    })? > 0,
                )
            }
            IFLA_BRPORT_CONFIG_PENDING => InfoBridgePort::ConfigPending(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_CONFIG_PENDING {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_MESSAGE_AGE_TIMER => InfoBridgePort::MessageAgeTimer(
                parse_u64(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_MESSAGE_AGE_TIMER {payload:?}")
                })?,
            ),
            IFLA_BRPORT_FORWARD_DELAY_TIMER => {
                InfoBridgePort::ForwardDelayTimer(
                    parse_u64(payload).with_context(|| {
                        format!(
                            "invalid IFLA_BRPORT_FORWARD_DELAY_TIMER {payload:?}"
                        )
                    })?,
                )
            }
            IFLA_BRPORT_HOLD_TIMER => {
                InfoBridgePort::HoldTimer(parse_u64(payload).with_context(
                    || format!("invalid IFLA_BRPORT_HOLD_TIMER {payload:?}"),
                )?)
            }
            IFLA_BRPORT_FLUSH => InfoBridgePort::Flush,
            IFLA_BRPORT_MULTICAST_ROUTER => InfoBridgePort::MulticastRouter(
                parse_u8(payload)
                    .with_context(|| {
                        format!(
                            "invalid IFLA_BRPORT_MULTICAST_ROUTER {payload:?}"
                        )
                    })?
                    .into(),
            ),
            IFLA_BRPORT_MCAST_FLOOD => InfoBridgePort::MulticastFlood(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_MCAST_FLOOD {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_MCAST_TO_UCAST => InfoBridgePort::MulticastToUnicast(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_MCAST_TO_UCAST {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_VLAN_TUNNEL => InfoBridgePort::VlanTunnel(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_VLAN_TUNNEL {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_BCAST_FLOOD => InfoBridgePort::BroadcastFlood(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_BCAST_FLOOD {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_GROUP_FWD_MASK => InfoBridgePort::GroupFwdMask(
                parse_u16(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_GROUP_FWD_MASK {payload:?}")
                })?,
            ),
            IFLA_BRPORT_NEIGH_SUPPRESS => InfoBridgePort::NeighSupress(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_NEIGH_SUPPRESS {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_ISOLATED => InfoBridgePort::Isolated(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_ISOLATED {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_BACKUP_PORT => {
                InfoBridgePort::BackupPort(parse_u32(payload).with_context(
                    || format!("invalid IFLA_BRPORT_BACKUP_PORT {payload:?}"),
                )?)
            }
            IFLA_BRPORT_MRP_RING_OPEN => InfoBridgePort::MrpRingOpen(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_MRP_RING_OPEN {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_MRP_IN_OPEN => InfoBridgePort::MrpInOpen(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_MRP_IN_OPEN {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT => {
                InfoBridgePort::MulticastEhtHostsLimit(
                    parse_u32(payload).with_context(|| {
                        format!(
                            "invalid IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT {payload:?}"
                        )
                    })?,
                )
            }
            IFLA_BRPORT_MCAST_EHT_HOSTS_CNT => {
                InfoBridgePort::MulticastEhtHostsCnt(
                    parse_u32(payload).with_context(|| {
                        format!(
                            "invalid IFLA_BRPORT_MCAST_EHT_HOSTS_CNT {payload:?}"
                        )
                    })?
                )
            }
            IFLA_BRPORT_LOCKED => InfoBridgePort::Locked(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_LOCKED {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_MAB => InfoBridgePort::Mab(
                parse_u8(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_MAB {payload:?}")
                })? > 0,
            ),
            IFLA_BRPORT_MCAST_N_GROUPS => InfoBridgePort::MulticastNGroups(
                parse_u32(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_MCAST_N_GROUPS {payload:?}")
                })?,
            ),
            IFLA_BRPORT_MCAST_MAX_GROUPS => InfoBridgePort::MulticastMaxGroups(
                parse_u32(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_MCAST_MAX_GROUPS {payload:?}")
                })?,
            ),
            IFLA_BRPORT_NEIGH_VLAN_SUPPRESS => InfoBridgePort::NeighVlanSupress(
                parse_u8(payload).with_context(|| {
                    format!(
                        "invalid IFLA_BRPORT_NEIGH_VLAN_SUPPRESS {payload:?}"
                    )
                })? > 0,
            ),
            IFLA_BRPORT_BACKUP_NHID => InfoBridgePort::BackupNextHopId(
                parse_u32(payload).with_context(|| {
                    format!("invalid IFLA_BRPORT_BACKUP_NHID {payload:?}")
                })?,
            ),
            kind => InfoBridgePort::Other(
                DefaultNla::parse(buf).with_context(|| {
                    format!(
                        "failed to parse bridge port NLA of type '{kind}' into DefaultNla"
                    )
                })?,
            ),
        })
    }
}

const BR_STATE_DISABLED: u8 = 0;
const BR_STATE_LISTENING: u8 = 1;
const BR_STATE_LEARNING: u8 = 2;
const BR_STATE_FORWARDING: u8 = 3;
const BR_STATE_BLOCKING: u8 = 4;

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[non_exhaustive]
pub enum BridgePortState {
    Disabled,
    Listening,
    Learning,
    Forwarding,
    Blocking,
    Other(u8),
}

impl From<u8> for BridgePortState {
    fn from(value: u8) -> Self {
        match value {
            BR_STATE_DISABLED => BridgePortState::Disabled,
            BR_STATE_LISTENING => BridgePortState::Listening,
            BR_STATE_LEARNING => BridgePortState::Learning,
            BR_STATE_FORWARDING => BridgePortState::Forwarding,
            BR_STATE_BLOCKING => BridgePortState::Blocking,
            _ => BridgePortState::Other(value),
        }
    }
}

impl From<BridgePortState> for u8 {
    fn from(value: BridgePortState) -> Self {
        match value {
            BridgePortState::Disabled => BR_STATE_DISABLED,
            BridgePortState::Listening => BR_STATE_LISTENING,
            BridgePortState::Learning => BR_STATE_LEARNING,
            BridgePortState::Forwarding => BR_STATE_FORWARDING,
            BridgePortState::Blocking => BR_STATE_BLOCKING,
            BridgePortState::Other(v) => v,
        }
    }
}

const MDB_RTR_TYPE_DISABLED: u8 = 0;
const MDB_RTR_TYPE_TEMP_QUERY: u8 = 1;
const MDB_RTR_TYPE_PERM: u8 = 2;
const MDB_RTR_TYPE_TEMP: u8 = 3;

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[non_exhaustive]
pub enum BridgePortMulticastRouter {
    Disabled,
    TempQuery,
    Perm,
    Temp,
    Other(u8),
}

impl From<u8> for BridgePortMulticastRouter {
    fn from(value: u8) -> Self {
        match value {
            MDB_RTR_TYPE_DISABLED => BridgePortMulticastRouter::Disabled,
            MDB_RTR_TYPE_TEMP_QUERY => BridgePortMulticastRouter::TempQuery,
            MDB_RTR_TYPE_PERM => BridgePortMulticastRouter::Perm,
            MDB_RTR_TYPE_TEMP => BridgePortMulticastRouter::Temp,
            _ => BridgePortMulticastRouter::Other(value),
        }
    }
}

impl From<BridgePortMulticastRouter> for u8 {
    fn from(value: BridgePortMulticastRouter) -> Self {
        match value {
            BridgePortMulticastRouter::Disabled => MDB_RTR_TYPE_DISABLED,
            BridgePortMulticastRouter::TempQuery => MDB_RTR_TYPE_TEMP_QUERY,
            BridgePortMulticastRouter::Perm => MDB_RTR_TYPE_PERM,
            BridgePortMulticastRouter::Temp => MDB_RTR_TYPE_TEMP,
            BridgePortMulticastRouter::Other(v) => v,
        }
    }
}