forked from M-Labs/zynq-rs
1
0
Fork 0

eth: increase desc list safety

This commit is contained in:
Björn Stein 2019-10-18 00:03:17 +02:00 committed by Astro
parent 4e9c38527e
commit 9053166acc
2 changed files with 12 additions and 4 deletions

View File

@ -69,7 +69,8 @@ impl<'a> DescList<'a> {
}
DescList {
list,
// Shorten the list of descriptors to the required number.
list: &mut list[0..=last],
buffers,
next: 0,
}
@ -85,7 +86,7 @@ impl<'a> DescList<'a> {
if entry.word0.read().used() {
let word1 = entry.word1.read();
let len = word1.frame_length_lsbs().into();
let buffer = &self.buffers[self.next][0..len];
let buffer = &mut self.buffers[self.next][0..len];
self.next += 1;
if self.next >= list_len {
@ -107,7 +108,7 @@ impl<'a> DescList<'a> {
/// Releases a buffer back to the HW upon Drop
pub struct PktRef<'a> {
entry: &'a mut DescEntry,
buffer: &'a [u8],
buffer: &'a mut [u8],
}
impl<'a> Drop for PktRef<'a> {

View File

@ -40,6 +40,12 @@ pub struct DescList<'a> {
impl<'a> DescList<'a> {
pub fn new(list: &'a mut [DescEntry], buffers: &'a mut [[u8; MTU]]) -> Self {
let last = list.len().min(buffers.len()) - 1;
// Sending seems to not work properly with only one packet
// buffer (two duplicates get send with every packet), so
// check that at least 2 are allocated, i.e. that the index of
// the last one is at least one.
assert!(last > 0);
for (i, (entry, buffer)) in list.iter_mut().zip(buffers.iter_mut()).enumerate() {
let is_last = i == last;
let buffer_addr = &mut buffer[0] as *mut _ as u32;
@ -58,7 +64,8 @@ impl<'a> DescList<'a> {
}
DescList {
list,
// Shorten the list of descriptors to the required number.
list: &mut list[0..=last],
buffers,
next: 0,
}