emu_cli/network/
mod.rs

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// all of this code and everything underneath it is dead currently. I am building it slowly as I
// solve several problems towards making it reality.

mod address;
mod netlink;
mod subnet;

use self::address::Address;
use crate::vm::VM;

use anyhow::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::PathBuf};

pub trait Network<'a>: Send + Clone + Default + Serialize + Deserialize<'a> {
    fn name(&self) -> String;
    fn set_name(&self, name: String) -> Self;

    // optional
    fn index(&self) -> Option<u32>;
    fn set_index(&self, index: u32) -> Self;
}

pub trait NetworkInterface: Send + Default {
    fn name(&self) -> String;
    fn addresses(&self) -> Vec<Address>;

    // optional stuff
    fn peer_name(&self) -> Option<String>;
    fn index(&self) -> Option<u32>;
    fn id(&self) -> Option<u32>;
}

#[async_trait]
pub trait NetworkManager<'a, I, N>
where
    Self: Clone,
    I: NetworkInterface,
    N: Network<'a>,
{
    async fn create_network(&self, name: &str) -> Result<N>;
    async fn delete_network(&self, network: N) -> Result<()>;
    async fn exists_network(&self, network: N) -> Result<bool>;
    async fn create_interface(&self, network: N, id: u32) -> Result<I>;
    async fn delete_interface(&self, interface: I) -> Result<()>;
    async fn exists_interface(&self, interface: I) -> Result<bool>;
    async fn bind(&self, network: N, interface: I) -> Result<()>;
    async fn unbind(&self, interface: I) -> Result<()>;
    async fn add_address(&self, interface: I, address: &Address) -> Result<()>;
}

pub trait VMInterface<N>
where
    Self: Default,
    N: Network<'static>,
{
    fn add_to_network(&self, network: N) -> Result<()>;
    fn remove_from_network(&self, network: N) -> Result<()>;
    fn configure_addresses(&self, addresses: Vec<Address>) -> Result<()>;
}

#[derive(Debug, Clone, Default)]
pub struct VMNetwork<V, I, N>
where
    N: Network<'static>,
    V: VMInterface<N>,
    I: NetworkInterface,
{
    network: N,
    vms: HashMap<String, VM>,
    vm: std::marker::PhantomData<V>,
    interface: std::marker::PhantomData<I>,
}

impl<V, T, N> VMNetwork<V, T, N>
where
    N: Network<'static>,
    V: VMInterface<N>,
    T: NetworkInterface,
{
    pub fn add_vm(&mut self, vm: &VM) -> Result<()> {
        self.vms.insert(vm.name(), vm.clone());
        Ok(())
    }

    pub fn remove_vm(&mut self, vm: &VM) -> Result<()> {
        self.vms.remove(&vm.name());
        Ok(())
    }

    pub fn list(&self) -> Result<Vec<VM>> {
        Ok(self.vms.values().map(Clone::clone).collect::<Vec<VM>>())
    }

    pub fn network(&self) -> N {
        self.network.clone()
    }
}

pub type NetworkMap = HashMap<String, Vec<String>>;
pub type NetworkIndexMap = HashMap<String, u32>;

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct NetworkConfig {
    networks: NetworkMap,
    indexes: NetworkIndexMap,
}

pub type VMNetworkMap<V, T, N> = HashMap<String, VMNetwork<V, T, N>>;

pub struct NetworkList<V, M, T, N>
where
    N: Network<'static>,
    V: VMInterface<N>,
    M: NetworkManager<'static, T, N>,
    T: NetworkInterface,
{
    networks: VMNetworkMap<V, T, N>,
    manager: M,
}

impl<V, M, T, N> NetworkList<V, M, T, N>
where
    N: Network<'static>,
    V: VMInterface<N>,
    M: NetworkManager<'static, T, N>,
    T: NetworkInterface,
{
    pub fn create(&self, _: String) -> Result<()> {
        Ok(())
    }

    pub fn teardown(&self, _: String) -> Result<()> {
        Ok(())
    }

    pub fn save(&self, filename: PathBuf) -> Result<()> {
        let mut config = NetworkConfig::default();

        for (name, network) in &self.networks {
            config.networks.insert(
                name.to_string(),
                network
                    .list()?
                    .iter()
                    .map(|n| n.name())
                    .collect::<Vec<String>>(),
            );

            config.indexes.insert(
                network.network().name(),
                network.network().index().unwrap_or_default(),
            );
        }

        Ok(std::fs::write(filename, toml::to_string(&config)?)?)
    }

    pub fn load(manager: M, filename: PathBuf) -> Result<Self> {
        let map: NetworkConfig = toml::from_str(&std::fs::read_to_string(filename)?)?;
        let mut networks = VMNetworkMap::default();

        for (key, vms) in map.networks {
            let mut tmp: HashMap<String, VM> = HashMap::default();
            for vm in vms {
                tmp.insert(vm.clone(), vm.clone().into());
            }

            networks.insert(
                key.to_string(),
                VMNetwork {
                    network: N::default()
                        .set_name(key.to_string())
                        .set_index(map.indexes.get(&key).map_or_else(|| 0, |x| *x)),
                    vms: tmp,
                    ..Default::default()
                },
            );
        }

        Ok(Self { networks, manager })
    }

    pub fn manager(&self) -> M {
        self.manager.clone()
    }
}

impl<V, M, T, N> std::ops::Deref for NetworkList<V, M, T, N>
where
    N: Network<'static>,
    V: VMInterface<N>,
    M: NetworkManager<'static, T, N>,
    T: NetworkInterface,
{
    type Target = HashMap<String, VMNetwork<V, T, N>>;

    fn deref(&self) -> &Self::Target {
        &self.networks
    }
}

impl<V, M, T, N> std::ops::DerefMut for NetworkList<V, M, T, N>
where
    N: Network<'static>,
    V: VMInterface<N>,
    M: NetworkManager<'static, T, N>,
    T: NetworkInterface,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.networks
    }
}