twizzler_net/
buffer.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
use std::{
    mem::{size_of, MaybeUninit},
    sync::Mutex,
};

use twizzler_object::Object;

use crate::req::PacketData;

#[repr(C)]
pub struct BufferBase {
    counter: u32,
    pos: usize,
    reuse: [u32; 4096],
}

impl twizzler_abi::marker::BaseType for BufferBase {
    fn init<T>(_t: T) -> Self {
        todo!()
    }

    fn tags() -> &'static [(
        twizzler_abi::marker::BaseVersion,
        twizzler_abi::marker::BaseTag,
    )] {
        todo!()
    }
}

pub struct BufferController {
    #[allow(dead_code)]
    mgr: bool,
    #[allow(dead_code)]
    tx: bool,
    #[allow(dead_code)]
    obj: Mutex<Object<BufferBase>>,
}

const BUFFER_SIZE: usize = 8 * 1024; //TODO

pub struct ManagedBuffer<'a> {
    controller: &'a BufferController,
    idx: u32,
    len: usize,
    owned: bool,
}

impl<'a> ManagedBuffer<'a> {
    fn new(controller: &'a BufferController, idx: u32, len: usize) -> Self {
        Self {
            controller,
            idx,
            owned: true,
            len,
        }
    }

    pub fn new_unowned(controller: &'a BufferController, idx: u32, len: usize) -> Self {
        Self {
            controller,
            idx,
            owned: false,
            len,
        }
    }

    pub fn buffer_len(&self) -> usize {
        self.len
    }

    pub fn set_len(&mut self, len: usize) {
        if len > BUFFER_SIZE {
            panic!("cannot set buffer size above maximum value");
        }
        self.len = len;
    }

    pub fn as_bytes(&self) -> &[u8] {
        self.controller.get_slice(self.idx, self.len)
    }

    #[allow(clippy::mut_from_ref)]
    pub fn as_bytes_mut(&self) -> &mut [u8] {
        self.controller.get_slice_mut(self.idx, self.len)
    }

    pub fn copy_in(&mut self, data: &[u8]) {
        self.len = data.len();
        self.as_bytes_mut().copy_from_slice(data);
    }

    pub fn as_packet_data(&self) -> PacketData {
        PacketData {
            buffer_idx: self.idx,
            buffer_len: self.len as u32,
        }
    }

    pub fn get_data_mut<T>(&mut self, offset: usize) -> &mut MaybeUninit<T> {
        if offset + size_of::<T>() >= BUFFER_SIZE {
            panic!("tried to access buffer data out of bounds");
        }
        self.len = std::cmp::max(self.len, offset + size_of::<T>());
        let bytes = self.as_bytes_mut();
        unsafe {
            ((&mut bytes[offset..(offset + size_of::<T>())]).as_mut_ptr() as *mut MaybeUninit<T>)
                .as_mut()
                .unwrap()
        }
    }
}

impl BufferController {
    pub(crate) fn new(mgr: bool, tx: bool, obj: Object<BufferBase>) -> Self {
        Self {
            mgr,
            tx,
            obj: Mutex::new(obj),
        }
    }

    pub async fn allocate(&self) -> ManagedBuffer<'_> {
        let obj = self.obj.lock().unwrap();
        // TODO: unsafe
        let base = unsafe { obj.base_mut_unchecked() };
        let b = if base.pos == 0 {
            let b = ManagedBuffer::new(self, base.counter, 0);
            base.counter += 1;
            b
        } else {
            base.pos -= 1;
            ManagedBuffer::new(self, base.reuse[base.pos], 0)
        };
        println!(
            "allocated buffer {} from {} {}",
            b.idx,
            if self.mgr { "mgr" } else { "client" },
            if self.tx { "tx" } else { "rx" }
        );
        b
    }

    pub fn release(&self, idx: u32) {
        println!(
            "releasing buffer {} to {} {}",
            idx,
            if self.mgr { "mgr" } else { "client" },
            if self.tx { "tx" } else { "rx" }
        );
        let obj = self.obj.lock().unwrap();
        // TODO: unsafe
        let base = unsafe { obj.base_mut_unchecked() };
        if base.counter == idx + 1 {
            base.counter -= 1;
        } else {
            base.reuse[base.pos] = idx;
            base.pos += 1;
        }
    }

    pub fn get_slice(&self, idx: u32, len: usize) -> &[u8] {
        let obj = self.obj.lock().unwrap();
        let ptr = obj.raw_lea(idx as usize * BUFFER_SIZE + 0x2000);
        unsafe { core::slice::from_raw_parts(ptr, len) }
    }

    #[allow(clippy::mut_from_ref)]
    pub fn get_slice_mut(&self, idx: u32, len: usize) -> &mut [u8] {
        let obj = self.obj.lock().unwrap();
        let ptr = obj.raw_lea_mut(idx as usize * BUFFER_SIZE + 0x2000);
        unsafe { core::slice::from_raw_parts_mut(ptr, len) }
    }
}

impl<'a> Drop for ManagedBuffer<'a> {
    fn drop(&mut self) {
        if self.owned {
            self.controller.release(self.idx);
        }
    }
}