little-dude / netlink

netlink libraries for rust
Other
329 stars 89 forks source link

Support for link slave info #227

Open lights0123 opened 2 years ago

lights0123 commented 2 years ago

Currently, slave links are just returned as raw bytes:

https://github.com/little-dude/netlink/blob/5c1efa5758fe1e562a9317b22c22419ff6461f66/netlink-sys/src/packet/rtnl/link/nlas/link_infos.rs#L35-L36

These should be decoded just like master links. The bridge slave info can be printed currently with:

if let Nla::Info(info) = nla {
    if let Some(Info::SlaveKind(name)) = info.get(0) {
        if name == b"bridge\0" {
            if let Some(Info::SlaveData(data)) = info.get(1) {
                let iter = NlasIterator::new(data);
                for i in iter {
                    let i = i.unwrap();
                    match i.kind() {
                        0 => println!("IFLA_BRPORT_UNSPEC: {:?}", i.value()),
                        1 => println!("IFLA_BRPORT_STATE: {:?}", i.value()),
                        2 => println!("IFLA_BRPORT_PRIORITY: {:?}", i.value()),
                        3 => println!("IFLA_BRPORT_COST: {:?}", i.value()),
                        4 => println!("IFLA_BRPORT_MODE: {:?}", i.value()),
                        5 => println!("IFLA_BRPORT_GUARD: {:?}", i.value()),
                        6 => println!("IFLA_BRPORT_PROTECT: {:?}", i.value()),
                        7 => println!("IFLA_BRPORT_FAST_LEAVE: {:?}", i.value()),
                        8 => println!("IFLA_BRPORT_LEARNING: {:?}", i.value()),
                        9 => println!("IFLA_BRPORT_UNICAST_FLOOD: {:?}", i.value()),
                        10 => println!("IFLA_BRPORT_PROXYARP: {:?}", i.value()),
                        11 => println!("IFLA_BRPORT_LEARNING_SYNC: {:?}", i.value()),
                        12 => println!("IFLA_BRPORT_PROXYARP_WIFI: {:?}", i.value()),
                        13 => println!("IFLA_BRPORT_ROOT_ID: {:?}", i.value()),
                        14 => println!("IFLA_BRPORT_BRIDGE_ID: {:?}", i.value()),
                        15 => println!("IFLA_BRPORT_DESIGNATED_PORT: {:?}", i.value()),
                        16 => println!("IFLA_BRPORT_DESIGNATED_COST: {:?}", i.value()),
                        17 => println!("IFLA_BRPORT_ID: {:?}", i.value()),
                        18 => println!("IFLA_BRPORT_NO: {:?}", i.value()),
                        19 => {
                            println!("IFLA_BRPORT_TOPOLOGY_CHANGE_ACK: {:?}", i.value())
                        }
                        20 => println!("IFLA_BRPORT_CONFIG_PENDING: {:?}", i.value()),
                        21 => {
                            println!("IFLA_BRPORT_MESSAGE_AGE_TIMER: {:?}", i.value())
                        }
                        22 => {
                            println!("IFLA_BRPORT_FORWARD_DELAY_TIMER: {:?}", i.value())
                        }
                        23 => println!("IFLA_BRPORT_HOLD_TIMER: {:?}", i.value()),
                        24 => println!("IFLA_BRPORT_FLUSH: {:?}", i.value()),
                        25 => println!("IFLA_BRPORT_MULTICAST_ROUTER: {:?}", i.value()),
                        26 => println!("IFLA_BRPORT_PAD: {:?}", i.value()),
                        27 => println!("IFLA_BRPORT_MCAST_FLOOD: {:?}", i.value()),
                        28 => println!("IFLA_BRPORT_MCAST_TO_UCAST: {:?}", i.value()),
                        29 => println!("IFLA_BRPORT_VLAN_TUNNEL: {:?}", i.value()),
                        30 => println!("IFLA_BRPORT_BCAST_FLOOD: {:?}", i.value()),
                        31 => println!("IFLA_BRPORT_GROUP_FWD_MASK: {:?}", i.value()),
                        32 => println!("IFLA_BRPORT_NEIGH_SUPPRESS: {:?}", i.value()),
                        _ => {}
                    }
                }
            }
        }
    }
}

An example of the data it produces:

IFLA_BRPORT_STATE: [3]
IFLA_BRPORT_PRIORITY: [32, 0]
IFLA_BRPORT_COST: [4, 0, 0, 0]
IFLA_BRPORT_MODE: [0]
IFLA_BRPORT_GUARD: [0]
IFLA_BRPORT_PROTECT: [0]
IFLA_BRPORT_FAST_LEAVE: [0]
IFLA_BRPORT_MCAST_TO_UCAST: [0]
IFLA_BRPORT_LEARNING: [1]
IFLA_BRPORT_UNICAST_FLOOD: [1]
IFLA_BRPORT_MCAST_FLOOD: [1]
IFLA_BRPORT_BCAST_FLOOD: [1]
IFLA_BRPORT_PROXYARP: [0]
IFLA_BRPORT_PROXYARP_WIFI: [0]
IFLA_BRPORT_ROOT_ID: [128, 0, 120, 208, 4, 44, 61, 251]
IFLA_BRPORT_BRIDGE_ID: [128, 0, 120, 208, 4, 44, 61, 251]
IFLA_BRPORT_DESIGNATED_PORT: [1, 128]
IFLA_BRPORT_DESIGNATED_COST: [0, 0]
IFLA_BRPORT_ID: [1, 128]
IFLA_BRPORT_NO: [1, 0]
IFLA_BRPORT_TOPOLOGY_CHANGE_ACK: [0]
IFLA_BRPORT_CONFIG_PENDING: [0]
IFLA_BRPORT_VLAN_TUNNEL: [0]
IFLA_BRPORT_GROUP_FWD_MASK: [0, 0]
IFLA_BRPORT_NEIGH_SUPPRESS: [0]
IFLA_BRPORT_MESSAGE_AGE_TIMER: [0, 0, 0, 0, 0, 0, 0, 0]
IFLA_BRPORT_FORWARD_DELAY_TIMER: [0, 0, 0, 0, 0, 0, 0, 0]
IFLA_BRPORT_HOLD_TIMER: [0, 0, 0, 0, 0, 0, 0, 0]
IFLA_BRPORT_MULTICAST_ROUTER: [1]

These constants can be uncommented here: https://github.com/little-dude/netlink/blob/8d3db04ed05044343d68298d08f2d7c11b73f1f0/netlink-packet-route/src/rtnl/constants.rs#L832-L864

It appears from the iproute2 source code that only bonds and bridges have slave devices.