rtnetlink/link/
veth.rs

1// SPDX-License-Identifier: MIT
2
3use crate::{
4    packet_route::link::{InfoData, InfoKind, InfoVeth},
5    LinkMessageBuilder, LinkUnspec,
6};
7
8#[derive(Debug)]
9/// Represent virtual ethernet interface.
10/// Example code on creating a veth pair
11/// ```no_run
12/// use rtnetlink::{new_connection, LinkVeth};
13/// #[tokio::main]
14/// async fn main() -> Result<(), String> {
15///     let (connection, handle, _) = new_connection().unwrap();
16///     tokio::spawn(connection);
17///
18///     handle
19///         .link()
20///         .add(LinkVeth::new("veth1", "veth1-peer").build())
21///         .execute()
22///         .await
23///         .map_err(|e| format!("{e}"))
24/// }
25/// ```
26///
27/// Please check LinkMessageBuilder::<LinkVeth> for more detail.
28pub struct LinkVeth;
29
30impl LinkVeth {
31    /// Equal to `LinkMessageBuilder::<LinkVeth>::new(name, peer)`
32    pub fn new(name: &str, peer: &str) -> LinkMessageBuilder<Self> {
33        LinkMessageBuilder::<LinkVeth>::new(name, peer)
34    }
35}
36
37impl LinkMessageBuilder<LinkVeth> {
38    /// Create [LinkMessageBuilder] for VETH
39    pub fn new(name: &str, peer: &str) -> Self {
40        LinkMessageBuilder::<LinkVeth>::new_with_info_kind(InfoKind::Veth)
41            .name(name.to_string())
42            .peer(peer)
43    }
44
45    pub fn peer(mut self, peer: &str) -> Self {
46        let peer_msg = LinkMessageBuilder::<LinkUnspec>::new()
47            .name(peer.to_string())
48            .build();
49
50        self.info_data = Some(InfoData::Veth(InfoVeth::Peer(peer_msg)));
51        self
52    }
53}