Support SO_PRIORITY

This commit is contained in:
jiangjianfeng 2025-06-11 07:03:05 +00:00 committed by Ruihan Li
parent 8f6bc07b23
commit ec8beab540
3 changed files with 45 additions and 15 deletions

View File

@ -18,6 +18,7 @@ impl_socket_options!(
pub struct SendBuf(u32);
pub struct RecvBuf(u32);
pub struct Error(Option<crate::error::Error>);
pub struct Priority(i32);
pub struct Linger(LingerOption);
pub struct KeepAlive(bool);
pub struct SendBufForce(u32);

View File

@ -1,5 +1,7 @@
// SPDX-License-Identifier: MPL-2.0
use core::ops::RangeInclusive;
use aster_bigtcp::socket::{
NeedIfacePoll, TCP_RECV_BUF_LEN, TCP_SEND_BUF_LEN, UDP_RECV_PAYLOAD_LEN, UDP_SEND_PAYLOAD_LEN,
};
@ -9,8 +11,8 @@ use crate::{
match_sock_option_mut, match_sock_option_ref,
net::socket::{
options::{
KeepAlive, Linger, RecvBuf, RecvBufForce, ReuseAddr, ReusePort, SendBuf, SendBufForce,
SocketOption,
KeepAlive, Linger, Priority, RecvBuf, RecvBufForce, ReuseAddr, ReusePort, SendBuf,
SendBufForce, SocketOption,
},
unix::UNIX_STREAM_DEFAULT_BUF_SIZE,
},
@ -28,42 +30,48 @@ pub struct SocketOptionSet {
recv_buf: u32,
linger: LingerOption,
keep_alive: bool,
priority: i32,
}
impl Default for SocketOptionSet {
fn default() -> Self {
Self {
reuse_addr: false,
reuse_port: false,
send_buf: MIN_SENDBUF,
recv_buf: MIN_RECVBUF,
linger: LingerOption::default(),
keep_alive: false,
priority: 0,
}
}
}
impl SocketOptionSet {
/// Return the default socket level options for tcp socket.
pub fn new_tcp() -> Self {
Self {
reuse_addr: false,
reuse_port: false,
send_buf: TCP_SEND_BUF_LEN as u32,
recv_buf: TCP_RECV_BUF_LEN as u32,
linger: LingerOption::default(),
keep_alive: false,
..Default::default()
}
}
/// Return the default socket level options for udp socket.
pub fn new_udp() -> Self {
Self {
reuse_addr: false,
reuse_port: false,
send_buf: UDP_SEND_PAYLOAD_LEN as u32,
recv_buf: UDP_RECV_PAYLOAD_LEN as u32,
linger: LingerOption::default(),
keep_alive: false,
..Default::default()
}
}
/// Returns the default socket level options for unix stream socket.
pub(in crate::net) fn new_unix_stream() -> Self {
Self {
reuse_addr: false,
reuse_port: false,
send_buf: UNIX_STREAM_DEFAULT_BUF_SIZE as u32,
recv_buf: UNIX_STREAM_DEFAULT_BUF_SIZE as u32,
linger: LingerOption::default(),
keep_alive: false,
..Default::default()
}
}
@ -94,6 +102,10 @@ impl SocketOptionSet {
let linger = self.linger();
socket_linger.set(linger);
},
socket_priority: Priority => {
let priority = self.priority();
socket_priority.set(priority);
},
socket_keepalive: KeepAlive => {
let keep_alive = self.keep_alive();
socket_keepalive.set(keep_alive);
@ -144,6 +156,11 @@ impl SocketOptionSet {
let reuse_port = socket_reuse_port.get().unwrap();
self.set_reuse_port(*reuse_port);
},
socket_priority: Priority => {
let priority = socket_priority.get().unwrap();
check_priority(*priority)?;
self.set_priority(*priority);
},
socket_linger: Linger => {
let linger = socket_linger.get().unwrap();
self.set_linger(*linger);
@ -192,6 +209,16 @@ fn check_current_privileged() -> Result<()> {
return_errno_with_message!(Errno::EPERM, "the process does not have permissions")
}
fn check_priority(priority: i32) -> Result<()> {
const NORMAL_PRIORITY_RANGE: RangeInclusive<i32> = 0..=6;
if NORMAL_PRIORITY_RANGE.contains(&priority) {
return Ok(());
}
check_current_privileged()
}
pub const MIN_SENDBUF: u32 = 2304;
pub const MIN_RECVBUF: u32 = 2304;

View File

@ -4,7 +4,7 @@ use super::RawSocketOption;
use crate::{
impl_raw_sock_option_get_only, impl_raw_socket_option,
net::socket::options::{
Error, KeepAlive, Linger, RecvBuf, RecvBufForce, ReuseAddr, ReusePort, SendBuf,
Error, KeepAlive, Linger, Priority, RecvBuf, RecvBufForce, ReuseAddr, ReusePort, SendBuf,
SendBufForce, SocketOption,
},
prelude::*,
@ -47,6 +47,7 @@ pub fn new_socket_option(name: i32) -> Result<Box<dyn RawSocketOption>> {
CSocketOptionName::REUSEADDR => Ok(Box::new(ReuseAddr::new())),
CSocketOptionName::ERROR => Ok(Box::new(Error::new())),
CSocketOptionName::REUSEPORT => Ok(Box::new(ReusePort::new())),
CSocketOptionName::PRIORITY => Ok(Box::new(Priority::new())),
CSocketOptionName::LINGER => Ok(Box::new(Linger::new())),
CSocketOptionName::KEEPALIVE => Ok(Box::new(KeepAlive::new())),
CSocketOptionName::SNDBUFFORCE => Ok(Box::new(SendBufForce::new())),
@ -60,6 +61,7 @@ impl_raw_socket_option!(RecvBuf);
impl_raw_socket_option!(ReuseAddr);
impl_raw_sock_option_get_only!(Error);
impl_raw_socket_option!(ReusePort);
impl_raw_socket_option!(Priority);
impl_raw_socket_option!(Linger);
impl_raw_socket_option!(KeepAlive);
impl_raw_socket_option!(SendBufForce);