Skip to content
Merged
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
26 changes: 16 additions & 10 deletions Documentation/core-api/execution.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ EEVDF 公平任务使用增强红黑树组织选择状态;CPU 所有权、任
源不可用时回退旧 uptime,源切换不使公平时钟倒退。这不是用户时钟源;
正常调度采样须在一次硬件回绕内,不对长时间暂停后的绝对时间作保证。

发布可运行任务时,调度器设置各在线 CPU 的重新调度标志,并通过独立的
LAPIC 向量唤醒远端 CPU;同一未处理标志只发送一次 IPI。自 CPU、离线 CPU
及 SMP 启动交接完成前不发送。IPI 只确认本地 APIC,用户帧进入正常的
调度检查;内核帧继续执行,到原有安全检查点再切换,不抢占持锁内核代码。
任务的 CPU 预留、亲和性、停止状态和 EEVDF 权重保持原有规则。
唤醒一个阻塞任务时,调度器优先通知持有其用户帧的 CPU,或亲和性允许的
上次运行且目前空闲的 CPU。两者均不可用时,向各在线 CPU 发布重新调度
请求。远端 CPU 通过独立的 LAPIC 向量接收通知。同一未处理标志只发送
一次 IPI。自 CPU、离线 CPU 及 SMP 启动交接完成前不发送。
IPI 确认本地 APIC 后,用户帧进入正常调度检查,内核帧在安全检查点切换。

用户帧离开直接返回路径后,必须在释放 CPU 预留之前保存 FPU/SSE 状态。
空闲等待会释放执行事务,原任务可能在另一 CPU 运行并更新快照;因此即使
Expand Down Expand Up @@ -80,11 +80,17 @@ OFD、管道和地址空间的生命周期,各管道自己的数据锁保护
该路径只接纳没有共享 MM、没有在途续调且缓冲区已驻留的用户任务。
复制前检查全部页面;输出还要求各级页表及叶项具有真实硬件写权限。
缺页、COW、共享文件首次写入通知和设备映射不在数据锁内处理,统一回到
完整路径。命名管道及带路径的描述符保留原来的 inode 通知,跟踪中的任务
也走完整分发。每次复制仍最多 32 KiB,不改变 ``PIPE_BUF``、容量或
大请求短读写语义。

数据锁在唤醒等待队列、通知 poll 或检查调度之前释放。阻塞等待和
完整路径。匿名管道可以持有 pipefs inode 并走并行路径。命名管道、可能有
inotify 读写观察者的匿名管道和跟踪中的任务仍走完整分发。每次复制仍最多
32 KiB,``PIPE_BUF``、容量和大请求的短读写规则保持一致。内置
``/dev/null`` 的标量写入也可用读事务完成。它检查描述符权限和用户地址
范围,按 Linux 的单次传输上限返回已丢弃的字节数。
有已驻留输出缓冲区的 ``clock_gettime(CLOCK_MONOTONIC)`` 也可以在读事务
内取得原时间源的快照,避免启用进度显示时的连续时钟查询阻塞并行 I/O。

数据锁在唤醒等待队列、通知 poll 或检查调度之前释放。首次 poll 或 epoll
扫描会读取当时的管道状态并启用后续数据通知。在首次扫描之前,管道数据
变动只唤醒该管道自身的读写等待者。阻塞等待和
``SIGPIPE`` 继续由独占路径处理;检查条件与登记睡眠之间不会插入并行
数据修改。已完成的调用更新原有进程 I/O 统计,并用同一用户返回检查处理
待处理信号、CPU 亲和性和重新调度请求。
Expand Down
15 changes: 15 additions & 0 deletions Documentation/core-api/network-devices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ Device identity
e1000 和 e1000e 的链路状态查询在每个设备、每个时钟 tick 内复用一次硬件采样,避免短时间内反复读取同一个 MMIO
寄存器。e1000e 发送前另行检查当前链路。收包轮询和协议定时器照常运行。

e1000 把帧提交到 DMA 环后立即返回,仅在环满时等待可重用的描述符。
e1000 和 e1000e 每回收 8 个接收描述符更新一次硬件队列尾指针,队列排空时立即归还余下的描述符。
e1000e 优先使用 MSI 收包。IRQ 只应答硬件并安排任务上下文收包。任务发现
队列已空时重新打开通知并复查描述符,以处理这段时间到达的数据。MSI
分配失败时继续轮询,卸载时先关闭 IRQ 并等待回调退出,再停止 DMA。

同步收包在完整一轮网卡都没有新数据或协议工作时结束,每次最多处理 32 轮。
收到丢弃的描述符、推进 TCP 发送或投递 loopback 数据时继续处理。
UDP 单播通过轮询网卡发出后,如果随即检查收包且队列为空,内核会再检查
一次出站网卡。每次最多使用约 180 微秒的检查预算,所有接口每个时钟
tick 合计最多 2 毫秒,并有尝试次数上限。IRQ 网卡通过现有的就绪通知唤醒
等待 ``poll()`` 的任务。

旧 ``e1000_*`` 服务入口和 ReliefOS 旧网络管理请求查询首个存活接口。
旧驱动 API 表和公开用户结构继续使用原布局。新设备接口属于 ReliefNT 私有
内核接口,原生模块通过精确构建 ABI 关联。
Expand Down Expand Up @@ -86,6 +99,8 @@ Validation
==========

``tools/test_net_device.py`` 验证多个 owner、独立回调、计数和索引生命周期。
``tools/test_net_rx_budget.py`` 验证中断收包的公平调度、同步空轮询退出和 UDP
收包复查预算。e1000 与 e1000e 的宿主测试检查 DMA 环、描述符发布及 MSI 回退。
``tools/test_net_multi_interface.py`` 验证 ioctl、rtnetlink 和逐接口选路。
``tools/test_net_multinic_data.py`` 验证 ARP、UDP 输出和 AF_PACKET 隔离。
既有 TCP、UDP 和 IPv6 定向套件覆盖对应传输的设备绑定、作用域与资源清理。
Expand Down
64 changes: 39 additions & 25 deletions drivers/net/e1000/e1000_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ static void e1000_log(enum console_log_level level, const char *message)

/* Linux e1000's default accommodates receive bursts while a task is running. */
#define E1000_RX_COUNT 256u
#define E1000_RX_REFILL_BATCH 8u
#define E1000_TX_COUNT 16u
#define E1000_BUFFER_SIZE 2048u
#define E1000_TX_WAIT_SPINS 250000u
Expand Down Expand Up @@ -134,6 +135,7 @@ struct e1000_state {
uint8_t *rx_buf[E1000_RX_COUNT];
uint8_t *tx_buf[E1000_TX_COUNT];
uint32_t rx_index;
uint32_t rx_refill_count;
uint32_t tx_tail;
struct kernel_spinlock carrier_lock;
uint64_t carrier_tick;
Expand All @@ -157,15 +159,6 @@ static void e1000_memzero(void *dst, uint32_t len)
}
}

static void e1000_memcpy(void *dst, const void *src, uint32_t len)
{
uint8_t *d = (uint8_t *)dst;
const uint8_t *s = (const uint8_t *)src;
while (len--) {
*d++ = *s++;
}
}

static uint32_t e1000_reg_read(struct e1000_state *d, uint32_t offset)
{
return *(volatile uint32_t *)(d->mmio_base + offset);
Expand Down Expand Up @@ -309,6 +302,7 @@ static void e1000_configure_rx_tx(struct e1000_state *d)
e1000_reg_write(d, E1000_REG_RDH, 0);
e1000_reg_write(d, E1000_REG_RDT, E1000_RX_COUNT - 1u);
d->rx_index = 0;
d->rx_refill_count = 0;

e1000_reg_write(d, E1000_REG_TDBAL, (uint32_t)d->tx_phys);
e1000_reg_write(d, E1000_REG_TDBAH, (uint32_t)(d->tx_phys >> 32));
Expand Down Expand Up @@ -429,6 +423,11 @@ static const uint8_t *e1000_device_mac(void *context)
return d->mac;
}

/** @brief Publish one frame to the TX ring, waiting only for reusable space.
* @param context Active controller under network execution ownership.
* @param frame Borrowed Ethernet frame. @param len Frame bytes excluding hardware FCS.
* @return Zero when DMA owns the frame, or a negative input or ring-full error.
*/
static int e1000_device_send(void *context, const void *frame, uint32_t len)
{
struct e1000_state *d=context;
Expand All @@ -438,35 +437,51 @@ static int e1000_device_send(void *context, const void *frame, uint32_t len)
return -1;
}
index = d->tx_tail;
next = (index + 1u) % E1000_TX_COUNT;
/* The tail must never catch an outstanding head. Reserve one slot to
* distinguish a full ring from an empty one. */
for (uint32_t spin = 0; spin < E1000_TX_WAIT_SPINS; ++spin) {
if (d->tx[index].status & E1000_TX_STATUS_DD) {
if ((d->tx[index].status & E1000_TX_STATUS_DD) &&
(d->tx[next].status & E1000_TX_STATUS_DD)) {
break;
}
if (spin + 1u == E1000_TX_WAIT_SPINS) {
return -1;
}
e1000_cpu_relax();
}
e1000_memcpy(d->tx_buf[index], frame, len);
__atomic_thread_fence(__ATOMIC_ACQUIRE);
__builtin_memcpy(d->tx_buf[index], frame, len);
d->tx[index].length = (uint16_t)len;
d->tx[index].cso = 0;
d->tx[index].cmd = E1000_TX_CMD_EOP | E1000_TX_CMD_IFCS | E1000_TX_CMD_RS;
d->tx[index].status = 0;
d->tx[index].css = 0;
d->tx[index].special = 0;
__atomic_thread_fence(__ATOMIC_RELEASE);
next = (index + 1u) % E1000_TX_COUNT;
d->tx_tail = next;
e1000_reg_write(d, E1000_REG_TDT, next);
for (uint32_t spin = 0; spin < E1000_TX_WAIT_SPINS; ++spin) {
if (d->tx[index].status & E1000_TX_STATUS_DD) {
return 0;
}
e1000_cpu_relax();
}
return -1;
return 0;
}

/** @brief Return every recycled RX descriptor to hardware in one tail update.
* @param d Active controller under network execution ownership.
*/
static void e1000_rx_refill(struct e1000_state *d)
{
if (!d->rx_refill_count) return;
__atomic_thread_fence(__ATOMIC_RELEASE);
uint32_t tail = (d->rx_index + E1000_RX_COUNT - 1u) % E1000_RX_COUNT;
e1000_reg_write(d, E1000_REG_RDT, tail);
d->rx_refill_count = 0;
}

/** @brief Consume one frame and amortize RX tail updates across a ready burst.
* @param context Active controller under network execution ownership.
* @param frame Writable receive buffer. @param capacity Buffer bytes.
* @param out_len Optional frame length, cleared when no data is returned.
* @return One for a frame, zero for no frame, or a negative descriptor error.
*/
static int e1000_device_poll(void *context, void *frame, uint32_t capacity, uint32_t *out_len)
{
struct e1000_state *d=context;
Expand All @@ -483,6 +498,7 @@ static int e1000_device_poll(void *context, void *frame, uint32_t capacity, uint
index = d->rx_index;
status = d->rx[index].status;
if ((status & E1000_RX_STATUS_DD) == 0) {
e1000_rx_refill(d);
return 0;
}
__atomic_thread_fence(__ATOMIC_ACQUIRE);
Expand All @@ -492,20 +508,18 @@ static int e1000_device_poll(void *context, void *frame, uint32_t capacity, uint
receive_error = len > capacity ? -90 : -74;
len = 0;
}
if (len > capacity) {
len = capacity;
}
if (len) {
e1000_memcpy(frame, d->rx_buf[index], len);
__builtin_memcpy(frame, d->rx_buf[index], len);
}
d->rx[index].length = 0;
d->rx[index].checksum = 0;
d->rx[index].status = 0;
d->rx[index].errors = 0;
d->rx[index].special = 0;
__atomic_thread_fence(__ATOMIC_RELEASE);
e1000_reg_write(d, E1000_REG_RDT, index);
d->rx_index = (index + 1u) % E1000_RX_COUNT;
if (++d->rx_refill_count == E1000_RX_REFILL_BATCH ||
!(d->rx[d->rx_index].status & E1000_RX_STATUS_DD))
e1000_rx_refill(d);
if (out_len) {
*out_len = len;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/net/e1000e/Kbuild
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
obj-$(CONFIG_E1000E) += e1000e.o
e1000e-y := main.o hardware.o link.o rings.o module.o
e1000e-y := main.o hardware.o link.o rings.o interrupt.o module.o

include $(srctree)/scripts/Makefile.module
22 changes: 20 additions & 2 deletions drivers/net/e1000e/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#define E1K_FCAH 0x002cu
#define E1K_FCT 0x0030u
#define E1K_ICR 0x00c0u
#define E1K_ITR 0x00c4u
#define E1K_IMS 0x00d0u
#define E1K_IMC 0x00d8u
#define E1K_RCTL 0x0100u
#define E1K_TCTL 0x0400u
Expand All @@ -37,6 +39,7 @@
#define E1K_DRV_LOAD (1u << 28)
#define E1K_SW_OWN (1u << 5)
#define E1K_RX_COUNT 256u
#define E1K_RX_REFILL_BATCH 8u
#define E1K_TX_COUNT 64u
#define E1K_BUFFER 2048u
#define E1K_MAX_FRAME 1518u
Expand Down Expand Up @@ -72,10 +75,12 @@ struct e1k_device {
volatile struct e1k_rx_descriptor *rx;
volatile struct e1k_tx_descriptor *tx;
uint8_t *rx_data, *tx_data;
unsigned rx_next, tx_next, tx_clean;
unsigned rx_next, rx_refill_count, tx_next, tx_clean;
uint64_t carrier_tick;
bool carrier_valid, carrier_up;
struct kernel_spinlock rx_lock, tx_lock, carrier_lock;
uint32_t irq_handle, irq_ifindex;
bool irq_masked;
struct kernel_spinlock rx_lock, tx_lock, carrier_lock, irq_lock;
};

/** @brief Read one aligned device register through the owned UC mapping.
Expand Down Expand Up @@ -135,4 +140,17 @@ int e1k_send(struct e1k_device *d, const void *frame, uint32_t length);
* @return One for a frame, zero for an empty ring, or negative malformed/size error.
*/
int e1k_receive(struct e1k_device *d, void *frame, uint32_t capacity, uint32_t *length);
/** @brief Enable receive MSI on a registered controller if a vector is available.
* @param d Active device with all interrupt sources masked.
* @param ifindex Registered interface, which may still await publication.
*/
void e1k_interrupt_start(struct e1k_device *d, uint32_t ifindex);
/** @brief Rearm MSI after the receive queue drains and check for a raced frame.
* @param d Pinned controller with rx_lock held.
*/
void e1k_interrupt_complete(struct e1k_device *d);
/** @brief Mask the receive source and drain callbacks before teardown.
* @param d Owned controller with normal data callbacks already drained.
*/
void e1k_interrupt_stop(struct e1k_device *d);
#endif
79 changes: 79 additions & 0 deletions drivers/net/e1000e/interrupt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* SPDX-License-Identifier: Apache-2.0 */
#include "internal.h"
#include <reliefnt/net_device.h>

/* Descriptor threshold, receive overrun and receive timer. */
#define E1K_RX_INTERRUPTS ((1u << 4) | (1u << 6) | (1u << 7))
#define E1K_ITR_64_US 250u

/** @brief Mask and acknowledge a receive event before queuing task-context work.
* @param context Controller pinned by the MSI lease.
*/
static void e1k_rx_irq(void *context)
{
struct e1k_device *d = context;
uint64_t flags;
kernel_spin_lock_irqsave(&d->irq_lock, &flags);
e1k_write(d, E1K_IMC, E1K_RX_INTERRUPTS);
(void)e1k_read(d, E1K_ICR);
d->irq_masked = true;
uint32_t id = d->irq_ifindex;
kernel_spin_unlock_irqrestore(&d->irq_lock, flags);
if (id) netdev_rx_notify(id);
}

/** @copydoc e1k_interrupt_start */
void e1k_interrupt_start(struct e1k_device *d, uint32_t ifindex)
{
if (!d->api->request_pci_irq || !d->api->free_pci_irq ||
d->api->request_pci_irq(&d->pci, e1k_rx_irq, d, &d->irq_handle)) return;

uint64_t flags;
kernel_spin_lock_irqsave(&d->irq_lock, &flags);
d->irq_ifindex = ifindex;
e1k_write(d, E1K_ITR, E1K_ITR_64_US);
(void)e1k_read(d, E1K_ICR);
e1k_write(d, E1K_IMS, E1K_RX_INTERRUPTS);
e1k_flush(d);
d->irq_masked = false;
kernel_spin_unlock_irqrestore(&d->irq_lock, flags);
netdev_rx_notify(ifindex);
}

/** @copydoc e1k_interrupt_complete */
void e1k_interrupt_complete(struct e1k_device *d)
{
if (!d->irq_handle) return;
uint64_t flags;
uint32_t notify = 0;
kernel_spin_lock_irqsave(&d->irq_lock, &flags);
if (d->irq_masked && d->irq_ifindex) {
e1k_write(d, E1K_IMS, E1K_RX_INTERRUPTS);
e1k_flush(d);
d->irq_masked = false;
if (d->rx[d->rx_next].status & 1u) {
e1k_write(d, E1K_IMC, E1K_RX_INTERRUPTS);
(void)e1k_read(d, E1K_ICR);
d->irq_masked = true;
notify = d->irq_ifindex;
}
}
kernel_spin_unlock_irqrestore(&d->irq_lock, flags);
if (notify) netdev_rx_notify(notify);
}

/** @copydoc e1k_interrupt_stop */
void e1k_interrupt_stop(struct e1k_device *d)
{
if (!d->irq_handle) return;
uint64_t flags;
kernel_spin_lock_irqsave(&d->irq_lock, &flags);
e1k_write(d, E1K_IMC, E1K_RX_INTERRUPTS);
(void)e1k_read(d, E1K_ICR);
uint32_t handle = d->irq_handle;
d->irq_handle = 0;
d->irq_ifindex = 0;
d->irq_masked = true;
kernel_spin_unlock_irqrestore(&d->irq_lock, flags);
d->api->free_pci_irq(handle);
}
8 changes: 5 additions & 3 deletions drivers/net/e1000e/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static const struct reliefnt_netdev_ops ops={
*/
static int stop_device(struct e1k_device *d)
{
e1k_interrupt_stop(d);
int rc=e1k_hardware_stop(d);
if (rc) return rc;
e1k_free_rings(d);
Expand Down Expand Up @@ -107,11 +108,11 @@ static int start(const struct reliefos_driver_kernel_api *api)
*d=(struct e1k_device){.api=api,.pci=pci,.next=devices}; devices=d;
rc=e1k_hardware_start(d);
if (!rc) rc=e1k_allocate_rings(d);
uint32_t ifindex;
if (!rc) {
uint16_t command=api->pci_read16(pci.bus,pci.slot,pci.function,4);
api->pci_write16(pci.bus,pci.slot,pci.function,4,command|6u);
e1k_enable_rings(d); d->active=true;
uint32_t ifindex;
rc=driver_register_netdev(&ops,d,&ifindex);
}
if (rc) {
Expand All @@ -123,11 +124,12 @@ static int start(const struct reliefos_driver_kernel_api *api)
}
e1k_write(d,E1K_CTRL_EXT,e1k_read(d,E1K_CTRL_EXT)|E1K_DRV_LOAD);
e1k_flush(d); ++registered;
e1k_interrupt_start(d, ifindex);
api->console_log(CONSOLE_LOG_INFO,
"[e1000e] %02x:%02x.%u Intel %04x:%04x MAC=%02x:%02x:%02x:%02x:%02x:%02x; link=%s\n",
"[e1000e] %02x:%02x.%u Intel %04x:%04x MAC=%02x:%02x:%02x:%02x:%02x:%02x; link=%s irq=%s\n",
pci.bus,pci.slot,pci.function,pci.vendor_id,pci.device_id,
d->mac[0],d->mac[1],d->mac[2],d->mac[3],d->mac[4],d->mac[5],
ready(d)?"up":"negotiating/down");
ready(d)?"up":"negotiating/down", d->irq_handle?"msi":"poll");
}
return registered ? 0 : first_error ? first_error : -19;
}
Expand Down
Loading
Loading