2026-07-02 15:40:39 +08:00
|
|
|
|
#include <utsm/net.h>
|
|
|
|
|
|
#include <utsm/log.h>
|
2026-10-03 12:44:57 +08:00
|
|
|
|
#include <utsm/panic.h>
|
2026-07-02 15:40:39 +08:00
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* 最小 netdev 注册表:
|
|
|
|
|
|
* - 早期 DKM 驱动加载路径是单线程顺序执行,当前无需引入锁,避免在网络底座阶段扩大复杂度。
|
|
|
|
|
|
* - 后续进入多核/热插拔后,应在 register/device_info 外围加自旋锁或 RCU 风格只读快照。
|
2026-07-03 12:59:59 +08:00
|
|
|
|
*
|
|
|
|
|
|
* 无线扫描接口(scan_start/scan_count/scan_result/is_wireless)通过 dispatch 转发到驱动
|
|
|
|
|
|
* 注册的回调。有线驱动(e1000/virtio_net)不填这些回调,dispatch 时返回 -3 表示不支持。
|
2026-07-02 15:40:39 +08:00
|
|
|
|
*/
|
|
|
|
|
|
static dkm_net_device_desc g_net_devices[DKM_NET_MAX_DEVICES];
|
|
|
|
|
|
static u32 g_net_device_count;
|
|
|
|
|
|
|
|
|
|
|
|
void net_init(void) {
|
|
|
|
|
|
g_net_device_count = 0;
|
|
|
|
|
|
log_info("[NET] registry init");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int net_register_device_impl(const dkm_net_device_desc *desc) {
|
|
|
|
|
|
if (!desc || !desc->name) return -1;
|
|
|
|
|
|
if (g_net_device_count >= DKM_NET_MAX_DEVICES) return -2;
|
|
|
|
|
|
|
|
|
|
|
|
u32 index = g_net_device_count++;
|
|
|
|
|
|
g_net_devices[index] = *desc;
|
|
|
|
|
|
|
|
|
|
|
|
log_info("[NET] device registered");
|
|
|
|
|
|
log_info(desc->name);
|
|
|
|
|
|
log_hex64("[NET] index=", index);
|
|
|
|
|
|
log_hex64("[NET] flags=", desc->flags);
|
|
|
|
|
|
return (int)index;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static u32 net_device_count_impl(void) {
|
|
|
|
|
|
return g_net_device_count;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-04 21:34:50 +08:00
|
|
|
|
/* attest: 有线/无线设备计数(is_wireless 回调非空即视为无线) */
|
|
|
|
|
|
void net_attest_counts(u32 *out_total, u32 *out_wireless) {
|
|
|
|
|
|
u32 wireless = 0;
|
|
|
|
|
|
for (u32 i = 0; i < g_net_device_count; i++) {
|
|
|
|
|
|
if (g_net_devices[i].is_wireless) wireless++;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (out_total) *out_total = g_net_device_count;
|
|
|
|
|
|
if (out_wireless) *out_wireless = wireless;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-02 15:40:39 +08:00
|
|
|
|
static int net_device_info_impl(u32 index, dkm_net_device_info *out) {
|
|
|
|
|
|
if (!out) return -1;
|
|
|
|
|
|
if (index >= g_net_device_count) return -2;
|
|
|
|
|
|
|
|
|
|
|
|
const dkm_net_device_desc *dev = &g_net_devices[index];
|
|
|
|
|
|
out->name = dev->name;
|
|
|
|
|
|
for (u32 i = 0; i < 6; i++) {
|
|
|
|
|
|
out->mac[i] = dev->mac[i];
|
|
|
|
|
|
}
|
|
|
|
|
|
out->flags = dev->flags;
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int net_tx_impl(u32 index, const void *packet, u32 length) {
|
|
|
|
|
|
if (index >= g_net_device_count) return -2;
|
|
|
|
|
|
const dkm_net_device_desc *dev = &g_net_devices[index];
|
|
|
|
|
|
if (!dev->tx) return -3;
|
|
|
|
|
|
return dev->tx(dev->ctx, packet, length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int net_rx_poll_impl(u32 index, void *buffer, u32 capacity, u32 *out_length) {
|
|
|
|
|
|
if (index >= g_net_device_count) return -2;
|
|
|
|
|
|
const dkm_net_device_desc *dev = &g_net_devices[index];
|
|
|
|
|
|
if (!dev->rx_poll) return -3;
|
|
|
|
|
|
return dev->rx_poll(dev->ctx, buffer, capacity, out_length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-03 12:59:59 +08:00
|
|
|
|
static int net_scan_start_impl(u32 index) {
|
|
|
|
|
|
if (index >= g_net_device_count) return -2;
|
|
|
|
|
|
const dkm_net_device_desc *dev = &g_net_devices[index];
|
|
|
|
|
|
if (!dev->scan_start) return -3;
|
|
|
|
|
|
return dev->scan_start(dev->ctx);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int net_scan_count_impl(u32 index) {
|
|
|
|
|
|
if (index >= g_net_device_count) return -2;
|
|
|
|
|
|
const dkm_net_device_desc *dev = &g_net_devices[index];
|
|
|
|
|
|
if (!dev->scan_count) return -3;
|
|
|
|
|
|
return dev->scan_count(dev->ctx);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int net_scan_result_impl(u32 index, u32 n, dkm_net_scan_result *out) {
|
|
|
|
|
|
if (!out) return -1;
|
|
|
|
|
|
if (index >= g_net_device_count) return -2;
|
|
|
|
|
|
const dkm_net_device_desc *dev = &g_net_devices[index];
|
|
|
|
|
|
if (!dev->scan_result) return -3;
|
|
|
|
|
|
return dev->scan_result(dev->ctx, n, out);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int net_is_wireless_impl(u32 index) {
|
|
|
|
|
|
if (index >= g_net_device_count) return -2;
|
|
|
|
|
|
const dkm_net_device_desc *dev = &g_net_devices[index];
|
|
|
|
|
|
if (dev->is_wireless) return dev->is_wireless(dev->ctx);
|
|
|
|
|
|
return (dev->flags & DKM_NET_F_WIRELESS) ? 1 : 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-31 13:53:49 +08:00
|
|
|
|
/* 统计查询:返回驱动层累计的收发统计。
|
2026-10-03 12:44:57 +08:00
|
|
|
|
* 严格错误策略插桩:全零统计占位 = 向上层谎报"网卡零流量",
|
|
|
|
|
|
* 零降级 panic(NETST-E01)。驱动层统计区接入(driver_ctx 共享统计)
|
|
|
|
|
|
* 后才能提供真实语义。 */
|
2026-07-31 13:53:49 +08:00
|
|
|
|
static int net_device_stats_impl(u32 index, dkm_net_stats *out) {
|
|
|
|
|
|
if (!out) return -1;
|
|
|
|
|
|
if (index >= g_net_device_count) return -2;
|
2026-10-03 12:44:57 +08:00
|
|
|
|
(void)index;
|
|
|
|
|
|
panic_full("NETST-E01 NET STATS NOT IMPLEMENTED",
|
|
|
|
|
|
"netdev stats placeholder returns all-zero: driver stats wiring not implemented", 0);
|
2026-07-31 13:53:49 +08:00
|
|
|
|
for (u32 i = 0; i < sizeof(dkm_net_stats); i++)
|
|
|
|
|
|
((u8 *)out)[i] = 0;
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-02 15:40:39 +08:00
|
|
|
|
static const dkm_net_api g_net_api = {
|
|
|
|
|
|
.register_device = net_register_device_impl,
|
|
|
|
|
|
.device_count = net_device_count_impl,
|
|
|
|
|
|
.device_info = net_device_info_impl,
|
|
|
|
|
|
.tx = net_tx_impl,
|
2026-07-03 12:59:59 +08:00
|
|
|
|
.rx_poll = net_rx_poll_impl,
|
|
|
|
|
|
.scan_start = net_scan_start_impl,
|
|
|
|
|
|
.scan_count = net_scan_count_impl,
|
|
|
|
|
|
.scan_result = net_scan_result_impl,
|
2026-07-31 13:53:49 +08:00
|
|
|
|
.is_wireless = net_is_wireless_impl,
|
|
|
|
|
|
.device_stats = net_device_stats_impl
|
2026-07-02 15:40:39 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const dkm_net_api *net_get_api(void) {
|
|
|
|
|
|
return &g_net_api;
|
|
|
|
|
|
}
|