Skip to content

Rewrite poll_syscall #327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/interface/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,3 +639,64 @@ pub fn kernel_select(

return result;
}


// Implementations of poll related pollfd structure
pub struct PollFd(pub libc::pollfd);

impl PollFd {
pub fn new_with_fd(fd: i32) -> PollFd {
let raw_pollfd = libc::pollfd {
fd,
events: 0,
revents: 0,
};
PollFd(raw_pollfd)
}

// set the event in pollfd
pub fn set_event(&mut self, event: i16) {
self.0.events |= event;
}

pub fn get_revent(&self) -> i16 {
self.0.revents
}

pub fn get_fd(&self) -> i32 {
self.0.fd
}
}

pub fn kernel_poll(
fds: &mut [PollFd],
nfds: u64,
) -> i32 {
// convert [PollFd] to Vec<libc::pollfd>
let mut poll_fds: Vec<libc::pollfd> = fds.iter_mut()
.map(|poll_fd| poll_fd.0) // Directly use the value
.collect();

// Call libc::poll and store the result
let result = unsafe {
// Create a timeval struct with zero timeout
let kpoll_timeout: i32 = 0;

// do the libc poll
let ret = libc::poll(
poll_fds.as_mut_ptr(),
nfds as u64,
kpoll_timeout
);

// convert the result back to [PollFd]
for (index, pollfd) in poll_fds.iter().enumerate() {
let item = fds.get_mut(index).unwrap();
item.0.revents = pollfd.revents;
}

ret
};

return result;
}
38 changes: 38 additions & 0 deletions src/safeposix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,41 @@ pub fn update_readfds_from_kernel_select(
}
return kernel_ret;
}

pub struct PollInetInfo {
pub rawfd_lindfd_index_tuples: interface::RustHashMap<i32, i32>,
pub kernel_pollfd: Vec<interface::PollFd>,
}

impl PollInetInfo {
pub fn new() -> Self {
PollInetInfo {
rawfd_lindfd_index_tuples: interface::RustHashMap::new(),
kernel_pollfd: Vec::new(),
}
}
}

pub fn update_pollstruct_from_kernel_poll(
pollfds: &mut [interface::PollStruct],
inet_info: &mut PollInetInfo,
) -> i32 {
let kernel_ret;
// note that this poll call always have timeout = 0, so it doesn't block
let nfds = inet_info.kernel_pollfd.len() as u64;
// do the kernel poll
kernel_ret = interface::kernel_poll(
inet_info.kernel_pollfd.as_mut_slice(),
nfds
);
if kernel_ret > 0 {
// fill the pollfds with kernel poll result
for pollfd in &inet_info.kernel_pollfd {
let index = (*inet_info.rawfd_lindfd_index_tuples.get(&pollfd.get_fd()).unwrap()) as usize;
let pollstruct = pollfds.get_mut(index).unwrap();
// set revents
pollstruct.revents = pollfd.get_revent();
}
}
return kernel_ret;
}
Loading