net_consomme/
resolver.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::ConsommeEndpoint;
use consomme::ConsommeState;
use net_backend::resolve::ResolveEndpointParams;
use net_backend::resolve::ResolvedEndpoint;
use net_backend_resources::consomme::ConsommeHandle;
use thiserror::Error;
use vm_resource::ResolveResource;
use vm_resource::declare_static_resolver;
use vm_resource::kind::NetEndpointHandleKind;

pub struct ConsommeResolver;

declare_static_resolver! {
    ConsommeResolver,
    (NetEndpointHandleKind, ConsommeHandle),
}

#[derive(Debug, Error)]
pub enum ResolveConsommeError {
    #[error(transparent)]
    Consomme(consomme::Error),
    #[error(transparent)]
    InvalidCidr(consomme::InvalidCidr),
}

impl ResolveResource<NetEndpointHandleKind, ConsommeHandle> for ConsommeResolver {
    type Output = ResolvedEndpoint;
    type Error = ResolveConsommeError;

    fn resolve(
        &self,
        resource: ConsommeHandle,
        input: ResolveEndpointParams,
    ) -> Result<Self::Output, Self::Error> {
        let mut state = ConsommeState::new().map_err(ResolveConsommeError::Consomme)?;
        state.client_mac.0 = input.mac_address.to_bytes();
        if let Some(cidr) = &resource.cidr {
            state
                .set_cidr(cidr)
                .map_err(ResolveConsommeError::InvalidCidr)?;
        }
        let endpoint = ConsommeEndpoint::new_with_state(state);
        Ok(endpoint.into())
    }
}