components/
udp_driver.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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Component to initialize the userland UDP driver.
//!
//! This provides one Component, UDPDriverComponent. This component initializes
//! a userspace UDP driver that allows apps to use the UDP stack.
//!
//! Usage
//! -----
//! ```rust
//!    let udp_driver = UDPDriverComponent::new(
//!        board_kernel,
//!        udp_send_mux,
//!        udp_recv_mux,
//!        udp_port_table,
//!        local_ip_ifaces,
//!        PAYLOAD_LEN,
//!     )
//!     .finalize(components::udp_driver_component_static!());
//! ```

use capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm;
use capsules_extra::net::ipv6::ip_utils::IPAddr;
use capsules_extra::net::ipv6::ipv6_send::IP6SendStruct;
use capsules_extra::net::network_capabilities::{
    AddrRange, NetworkCapability, PortRange, UdpVisibilityCapability,
};
use capsules_extra::net::udp::udp_port_table::UdpPortManager;
use capsules_extra::net::udp::udp_recv::MuxUdpReceiver;
use capsules_extra::net::udp::udp_recv::UDPReceiver;
use capsules_extra::net::udp::udp_send::{MuxUdpSender, UDPSendStruct, UDPSender};
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::capabilities::NetworkCapabilityCreationCapability;
use kernel::component::Component;
use kernel::create_capability;
use kernel::hil::time::Alarm;

const MAX_PAYLOAD_LEN: usize = super::udp_mux::MAX_PAYLOAD_LEN;

// Setup static space for the objects.
#[macro_export]
macro_rules! udp_driver_component_static {
    ($A:ty $(,)?) => {{
        use components::udp_mux::MAX_PAYLOAD_LEN;

        let udp_send = kernel::static_buf!(
            capsules_extra::net::udp::udp_send::UDPSendStruct<
                'static,
                capsules_extra::net::ipv6::ipv6_send::IP6SendStruct<
                    'static,
                    capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, $A>,
                >,
            >
        );
        let udp_vis_cap =
            kernel::static_buf!(capsules_extra::net::network_capabilities::UdpVisibilityCapability);
        let net_cap =
            kernel::static_buf!(capsules_extra::net::network_capabilities::NetworkCapability);
        let udp_driver = kernel::static_buf!(capsules_extra::net::udp::UDPDriver<'static>);
        let buffer = kernel::static_buf!([u8; MAX_PAYLOAD_LEN]);
        let udp_recv =
            kernel::static_buf!(capsules_extra::net::udp::udp_recv::UDPReceiver<'static>);

        (udp_send, udp_vis_cap, net_cap, udp_driver, buffer, udp_recv)
    };};
}

pub struct UDPDriverComponent<A: Alarm<'static> + 'static> {
    board_kernel: &'static kernel::Kernel,
    driver_num: usize,
    udp_send_mux:
        &'static MuxUdpSender<'static, IP6SendStruct<'static, VirtualMuxAlarm<'static, A>>>,
    udp_recv_mux: &'static MuxUdpReceiver<'static>,
    port_table: &'static UdpPortManager,
    interface_list: &'static [IPAddr],
}

impl<A: Alarm<'static>> UDPDriverComponent<A> {
    pub fn new(
        board_kernel: &'static kernel::Kernel,
        driver_num: usize,
        udp_send_mux: &'static MuxUdpSender<
            'static,
            IP6SendStruct<'static, VirtualMuxAlarm<'static, A>>,
        >,
        udp_recv_mux: &'static MuxUdpReceiver<'static>,
        port_table: &'static UdpPortManager,
        interface_list: &'static [IPAddr],
    ) -> Self {
        Self {
            board_kernel,
            driver_num,
            udp_send_mux,
            udp_recv_mux,
            port_table,
            interface_list,
        }
    }
}

impl<A: Alarm<'static>> Component for UDPDriverComponent<A> {
    type StaticInput = (
        &'static mut MaybeUninit<
            UDPSendStruct<
                'static,
                capsules_extra::net::ipv6::ipv6_send::IP6SendStruct<
                    'static,
                    VirtualMuxAlarm<'static, A>,
                >,
            >,
        >,
        &'static mut MaybeUninit<
            capsules_extra::net::network_capabilities::UdpVisibilityCapability,
        >,
        &'static mut MaybeUninit<capsules_extra::net::network_capabilities::NetworkCapability>,
        &'static mut MaybeUninit<capsules_extra::net::udp::UDPDriver<'static>>,
        &'static mut MaybeUninit<[u8; MAX_PAYLOAD_LEN]>,
        &'static mut MaybeUninit<UDPReceiver<'static>>,
    );
    type Output = &'static capsules_extra::net::udp::UDPDriver<'static>;

    fn finalize(self, s: Self::StaticInput) -> Self::Output {
        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
        // TODO: change initialization below
        let create_cap = create_capability!(NetworkCapabilityCreationCapability);
        let udp_vis = s.1.write(UdpVisibilityCapability::new(&create_cap));
        let udp_send = s.0.write(UDPSendStruct::new(self.udp_send_mux, udp_vis));

        // Can't use create_capability bc need capability to have a static lifetime
        // so that UDP driver can use it as needed
        struct DriverCap;
        unsafe impl capabilities::UdpDriverCapability for DriverCap {}
        static DRIVER_CAP: DriverCap = DriverCap;

        let net_cap = s.2.write(NetworkCapability::new(
            AddrRange::Any,
            PortRange::Any,
            PortRange::Any,
            &create_cap,
        ));

        let buffer = s.4.write([0; MAX_PAYLOAD_LEN]);

        let udp_driver = s.3.write(capsules_extra::net::udp::UDPDriver::new(
            udp_send,
            self.board_kernel.create_grant(self.driver_num, &grant_cap),
            self.interface_list,
            MAX_PAYLOAD_LEN,
            self.port_table,
            kernel::utilities::leasable_buffer::SubSliceMut::new(buffer),
            &DRIVER_CAP,
            net_cap,
        ));
        udp_send.set_client(udp_driver);
        self.port_table.set_user_ports(udp_driver, &DRIVER_CAP);

        let udp_driver_rcvr = s.5.write(UDPReceiver::new());
        self.udp_recv_mux.set_driver(udp_driver);
        self.udp_recv_mux.add_client(udp_driver_rcvr);
        udp_driver
    }
}