rtnetlink/link/
vxlan.rs

1// SPDX-License-Identifier: MIT
2
3use crate::{
4    packet_route::link::{InfoData, InfoKind, InfoVxlan},
5    LinkMessageBuilder,
6};
7
8/// Represent VxLAN interface.
9/// Example code on creating a VxLAN interface
10/// ```no_run
11/// use rtnetlink::{new_connection, LinkVxlan};
12/// #[tokio::main]
13/// async fn main() -> Result<(), String> {
14///     let (connection, handle, _) = new_connection().unwrap();
15///     tokio::spawn(connection);
16///
17///     handle
18///         .link()
19///         .add(LinkVxlan::new("vxlan100", 100)
20///         .dev(10)
21///         .port(4789)
22///         .up()
23///         .build())
24///         .execute()
25///         .await
26///         .map_err(|e| format!("{e}"))
27/// }
28/// ```
29///
30/// Please check LinkMessageBuilder::<LinkVlan> for more detail.
31#[derive(Debug)]
32pub struct LinkVxlan;
33
34impl LinkVxlan {
35    /// Wrapper of `LinkMessageBuilder::<LinkVxlan>::new().id()`
36    pub fn new(name: &str, vni: u32) -> LinkMessageBuilder<Self> {
37        LinkMessageBuilder::<LinkVxlan>::new(name).id(vni)
38    }
39}
40
41impl LinkMessageBuilder<LinkVxlan> {
42    /// Create [LinkMessageBuilder] for VLAN
43    pub fn new(name: &str) -> Self {
44        LinkMessageBuilder::<LinkVxlan>::new_with_info_kind(InfoKind::Vxlan)
45            .name(name.to_string())
46    }
47
48    pub fn append_info_data(self, info: InfoVxlan) -> Self {
49        let mut ret = self;
50        if let InfoData::Vxlan(infos) = ret
51            .info_data
52            .get_or_insert_with(|| InfoData::Vxlan(Vec::new()))
53        {
54            infos.push(info);
55        }
56        ret
57    }
58
59    /// VNI
60    pub fn id(self, id: u32) -> Self {
61        self.append_info_data(InfoVxlan::Id(id))
62    }
63
64    /// This is equivalent to `devPHYS_DEV` for ip link vxlan.
65    /// Adds the `dev` attribute to the VXLAN
66    /// This is equivalent to `ip link add name NAME type vxlan id VNI dev
67    /// LINK`,  dev LINK - specifies the physical device to use
68    ///  for tunnel endpoint communication.
69    /// But instead of specifing a link name (`LINK`), we specify a link index.
70    /// Please be aware the `LinkMessageBuilder::link()` will not works for
71    /// VxLAN.
72    pub fn dev(self, index: u32) -> Self {
73        self.append_info_data(InfoVxlan::Link(index))
74    }
75
76    /// Adds the `dstport` attribute to the VXLAN
77    /// This is equivalent to `ip link add name NAME type vxlan id VNI dstport
78    /// PORT`. dstport PORT - specifies the UDP destination port to
79    /// communicate to the remote VXLAN tunnel endpoint.
80    pub fn port(self, port: u16) -> Self {
81        self.append_info_data(InfoVxlan::Port(port))
82    }
83
84    /// Adds the `group` attribute to the VXLAN
85    /// This is equivalent to `ip link add name NAME type vxlan id VNI group
86    /// IPADDR`, group IPADDR - specifies the multicast IP address to join.
87    /// This function takes an IPv4 address
88    /// WARNING: only one between `remote` and `group` can be present.
89    pub fn group(self, addr: std::net::Ipv4Addr) -> Self {
90        self.append_info_data(InfoVxlan::Group(addr))
91    }
92
93    /// Adds the `group` attribute to the VXLAN
94    /// This is equivalent to `ip link add name NAME type vxlan id VNI group
95    /// IPADDR`, group IPADDR - specifies the multicast IP address to join.
96    /// This function takes an IPv6 address
97    /// WARNING: only one between `remote` and `group` can be present.
98    pub fn group6(self, addr: std::net::Ipv6Addr) -> Self {
99        self.append_info_data(InfoVxlan::Group6(addr))
100    }
101
102    /// Adds the `remote` attribute to the VXLAN
103    /// This is equivalent to `ip link add name NAME type vxlan id VNI remote
104    /// IPADDR`, remote IPADDR - specifies the unicast destination IP
105    /// address to use in outgoing packets when the
106    /// destination link layer address is not known in the
107    /// VXLAN device forwarding database.
108    /// This function takes an IPv4 address.
109    /// WARNING: only one between `remote` and `group` can be present.
110    pub fn remote(self, addr: std::net::Ipv4Addr) -> Self {
111        self.group(addr)
112    }
113
114    /// Adds the `remote` attribute to the VXLAN
115    /// This is equivalent to `ip link add name NAME type vxlan id VNI remote
116    /// IPADDR`, remote IPADDR - specifies the unicast destination IP
117    /// address to use in outgoing packets when the
118    /// destination link layer address is not known in the
119    /// VXLAN device forwarding database.
120    /// This function takes an IPv6 address.
121    /// WARNING: only one between `remote` and `group` can be present.
122    pub fn remote6(self, addr: std::net::Ipv6Addr) -> Self {
123        self.group6(addr)
124    }
125
126    /// Adds the `local` attribute to the VXLAN
127    /// This is equivalent to `ip link add name NAME type vxlan id VNI local
128    /// IPADDR`, local IPADDR - specifies the source IP address to use in
129    /// outgoing packets. This function takes an IPv4 address.
130    pub fn local(self, addr: std::net::Ipv4Addr) -> Self {
131        self.append_info_data(InfoVxlan::Local(addr))
132    }
133
134    /// Adds the `local` attribute to the VXLAN
135    /// This is equivalent to `ip link add name NAME type vxlan id VNI local
136    /// IPADDR`, local IPADDR - specifies the source IP address to use in
137    /// outgoing packets. This function takes an IPv6 address.
138    pub fn local6(self, addr: std::net::Ipv6Addr) -> Self {
139        self.append_info_data(InfoVxlan::Local6(addr))
140    }
141
142    /// Adds the `tos` attribute to the VXLAN
143    /// This is equivalent to `ip link add name NAME type vxlan id VNI tos TOS`.
144    /// tos TOS - specifies the TOS value to use in outgoing packets.
145    pub fn tos(self, tos: u8) -> Self {
146        self.append_info_data(InfoVxlan::Tos(tos))
147    }
148
149    /// Adds the `ttl` attribute to the VXLAN
150    /// This is equivalent to `ip link add name NAME type vxlan id VNI ttl TTL`.
151    /// ttl TTL - specifies the TTL value to use in outgoing packets.
152    pub fn ttl(self, ttl: u8) -> Self {
153        self.append_info_data(InfoVxlan::Ttl(ttl))
154    }
155
156    /// Adds the `flowlabel` attribute to the VXLAN
157    /// This is equivalent to `ip link add name NAME type vxlan id VNI flowlabel
158    /// LABEL`. flowlabel LABEL - specifies the flow label to use in
159    /// outgoing packets.
160    pub fn label(self, label: u32) -> Self {
161        self.append_info_data(InfoVxlan::Label(label))
162    }
163
164    /// Adds the `learning` attribute to the VXLAN
165    /// This is equivalent to `ip link add name NAME type vxlan id VNI
166    /// \[no\]learning`. \[no\]learning - specifies if unknown source link layer
167    /// addresses and IP addresses are entered into the VXLAN
168    /// device forwarding database.
169    pub fn learning(self, learning: bool) -> Self {
170        self.append_info_data(InfoVxlan::Learning(learning))
171    }
172
173    /// Adds the `ageing` attribute to the VXLAN
174    /// This is equivalent to `ip link add name NAME type vxlan id VNI ageing
175    /// SECONDS`. ageing SECONDS - specifies the lifetime in seconds of
176    /// FDB entries learnt by the kernel.
177    pub fn ageing(self, seconds: u32) -> Self {
178        self.append_info_data(InfoVxlan::Ageing(seconds))
179    }
180
181    /// Adds the `maxaddress` attribute to the VXLAN
182    /// This is equivalent to `ip link add name NAME type vxlan id VNI
183    /// maxaddress LIMIT`. maxaddress LIMIT - specifies the maximum number
184    /// of FDB entries.
185    pub fn limit(self, limit: u32) -> Self {
186        self.append_info_data(InfoVxlan::Limit(limit))
187    }
188
189    /// Adds the `srcport` attribute to the VXLAN
190    /// This is equivalent to `ip link add name NAME type vxlan id VNI srcport
191    /// MIN MAX`. srcport MIN MAX - specifies the range of port numbers
192    /// to use as UDP source ports to communicate to the
193    /// remote VXLAN tunnel endpoint.
194    pub fn port_range(self, min: u16, max: u16) -> Self {
195        self.append_info_data(InfoVxlan::PortRange((min, max)))
196    }
197
198    /// Adds the `proxy` attribute to the VXLAN
199    /// This is equivalent to `ip link add name NAME type vxlan id VNI
200    /// [no]proxy`. \[no\]proxy - specifies ARP proxy is turned on.
201    pub fn proxy(self, proxy: bool) -> Self {
202        self.append_info_data(InfoVxlan::Proxy(proxy))
203    }
204
205    /// Adds the `rsc` attribute to the VXLAN This is equivalent to
206    /// `ip link add name NAME type vxlan id VNI [no]rsc`.
207    /// \[no\]rsc - specifies if route short circuit is turned on.
208    pub fn rsc(self, rsc: bool) -> Self {
209        self.append_info_data(InfoVxlan::Rsc(rsc))
210    }
211
212    // Adds the `l2miss` attribute to the VXLAN
213    /// This is equivalent to `ip link add name NAME type vxlan id VNI
214    /// [no]l2miss`. \[no\]l2miss - specifies if netlink LLADDR miss
215    /// notifications are generated.
216    pub fn l2miss(self, l2miss: bool) -> Self {
217        self.append_info_data(InfoVxlan::L2Miss(l2miss))
218    }
219
220    // Adds the `l3miss` attribute to the VXLAN
221    /// This is equivalent to `ip link add name NAME type vxlan id VNI
222    /// [no]l3miss`. \[no\]l3miss - specifies if netlink IP ADDR
223    /// miss notifications are generated.
224    pub fn l3miss(self, l3miss: bool) -> Self {
225        self.append_info_data(InfoVxlan::L3Miss(l3miss))
226    }
227
228    pub fn collect_metadata(self, collect_metadata: bool) -> Self {
229        self.append_info_data(InfoVxlan::CollectMetadata(collect_metadata))
230    }
231
232    // Adds the `udp_csum` attribute to the VXLAN
233    /// This is equivalent to `ip link add name NAME type vxlan id VNI
234    /// [no]udp_csum`. \[no\]udpcsum - specifies if UDP checksum is
235    /// calculated for transmitted packets over IPv4.
236    pub fn udp_csum(self, udp_csum: bool) -> Self {
237        self.append_info_data(InfoVxlan::UDPCsum(udp_csum))
238    }
239}