rtnetlink/link/
set_bond_port.rs

1// SPDX-License-Identifier: MIT
2
3use futures::stream::StreamExt;
4use netlink_packet_core::{NetlinkMessage, NLM_F_ACK, NLM_F_REQUEST};
5use netlink_packet_route::{
6    link::nlas::{Info, InfoBondPort, InfoPortData, InfoPortKind, Nla},
7    LinkMessage, RtnlMessage,
8};
9
10use crate::{try_nl, Error, Handle};
11
12pub struct BondPortSetRequest {
13    handle: Handle,
14    index: u32,
15    port_nlas: Vec<InfoBondPort>,
16}
17
18impl BondPortSetRequest {
19    pub(crate) fn new(handle: Handle, index: u32) -> Self {
20        BondPortSetRequest {
21            handle,
22            index,
23            port_nlas: Vec::new(),
24        }
25    }
26
27    /// Execute the request.
28    pub async fn execute(self) -> Result<(), Error> {
29        let BondPortSetRequest {
30            mut handle,
31            index,
32            port_nlas,
33        } = self;
34
35        let mut message = LinkMessage::default();
36        message.header.index = index;
37        message.nlas.push(Nla::Info(vec![
38            Info::PortKind(InfoPortKind::Bond),
39            Info::PortData(InfoPortData::BondPort(port_nlas)),
40        ]));
41
42        let mut req = NetlinkMessage::from(RtnlMessage::NewLink(message));
43        req.header.flags = NLM_F_REQUEST | NLM_F_ACK;
44
45        let mut response = handle.request(req)?;
46        while let Some(message) = response.next().await {
47            try_nl!(message);
48        }
49        Ok(())
50    }
51
52    /// Return a mutable reference to the Vec<InfoBondPort>
53    pub fn info_port_nlas_mut(&mut self) -> &mut Vec<InfoBondPort> {
54        &mut self.port_nlas
55    }
56
57    /// Adds the `queue_id` attribute to the bond port
58    /// This is equivalent to
59    /// `ip link set name NAME type bond_slave queue_id QUEUE_ID`.
60    pub fn queue_id(mut self, queue_id: u16) -> Self {
61        self.port_nlas.push(InfoBondPort::QueueId(queue_id));
62        self
63    }
64
65    /// Adds the `prio` attribute to the bond port
66    /// This is equivalent to `ip link set name NAME type bond_slave prio PRIO`.
67    pub fn prio(mut self, prio: i32) -> Self {
68        self.port_nlas.push(InfoBondPort::Prio(prio));
69        self
70    }
71}