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
// SPDX-License-Identifier: MIT
use crate::{
packet_route::link::{GtpRole, InfoData, InfoGtp, InfoKind},
LinkMessageBuilder,
};
/// Represent GTP interface.
/// Example code on creating a GTP interface
/// ```no_run
/// use rtnetlink::{new_connection, LinkGtp};
/// #[tokio::main]
/// async fn main() -> Result<(), String> {
/// let (connection, handle, _) = new_connection().unwrap();
/// tokio::spawn(connection);
///
/// handle
/// .link()
/// .add(
/// LinkGtp::new("gtp0")
/// .role(rtnetlink::packet_route::link::GtpRole::Sgsn)
/// .build(),
/// )
/// .execute()
/// .await
/// .map_err(|e| format!("{e}"))
/// }
/// ```
///
/// Please check LinkMessageBuilder::<LinkGtp> for more detail.
#[derive(Default, Debug)]
pub struct LinkGtp;
impl LinkGtp {
/// Equal to `LinkMessageBuilder::<LinkGtp>::new()`
pub fn new(name: &str) -> LinkMessageBuilder<Self> {
LinkMessageBuilder::<LinkGtp>::new(name)
}
}
impl LinkMessageBuilder<LinkGtp> {
/// Create [LinkMessageBuilder] for GTP interface type
pub fn new(name: &str) -> Self {
let mut builder =
LinkMessageBuilder::<LinkGtp>::new_with_info_kind(InfoKind::Gtp)
.name(name.to_string());
// iproute2 always sets create_sockets to true
builder = builder.create_sockets(true);
builder
}
fn append_info_data(self, info: InfoGtp) -> Self {
let mut ret = self;
if let InfoData::Gtp(infos) = ret
.info_data
.get_or_insert_with(|| InfoData::Gtp(Vec::new()))
{
infos.push(info);
}
ret
}
/// This is equivalent to `role ROLE` in command
/// `ip link add name NAME type gtp role ROLE`.
/// ROLE is either "sgsn" or "ggsn".
pub fn role(self, role: GtpRole) -> Self {
self.append_info_data(InfoGtp::Role(role))
}
/// This is equivalent to `hsize HSIZE` in command
/// `ip link add name NAME type gtp hsize HSIZE`.
pub fn pdp_hashsize(self, hsize: u32) -> Self {
self.append_info_data(InfoGtp::PdpHashsize(hsize))
}
/// This is equivalent to `restart_count N` in command
/// `ip link add name NAME type gtp restart_count N`.
pub fn restart_count(self, count: u8) -> Self {
self.append_info_data(InfoGtp::RestartCount(count))
}
/// Internal: set create_sockets flag.
/// This is always set to true by iproute2.
fn create_sockets(self, enabled: bool) -> Self {
self.append_info_data(InfoGtp::CreateSockets(enabled))
}
}