mirror of
https://github.com/subhobhai943/SUB-OS.git
synced 2026-10-25 16:52:29 +00:00
The visible goal was a Web app: type a URL, see the page, fetched over the TCP stack SUB-OS built. Getting there surfaced -- and forced fixes to -- two deeper problems. Async HTTP client (net/http_client.c) tcp_connect/recv all block, so fetching from the GUI would freeze the compositor. Fetches now run on one long-lived kernel worker thread that services a single-slot mailbox; the requester posts a job and polls its state. Ownership follows the state, and the two hand-off points (worker completion, requester release) run under interrupts-off, so a window can close mid-fetch with neither leak nor use-after-free. do_fetch returns its terminal state rather than publishing it, closing a window where the requester could free the job before finalize read it. Cooperative scheduling for the idle thread (the core fix) The login prompt, the shell and the GUI compositor all run on the pid-0 idle thread, which the scheduler by design never preempts (sched_tick skips pid 0) and never requeues on a yield (weave_on clear). So it could neither be preempted to let the worker run, nor cleanly yield -- the worker was simply starved, and an early attempt to yield from it froze the shell outright. The idle thread now joins the weave rotation once hardware interrupts are live, and the network wait loops (tcp, dns, ping, the worker's idle) yield through a new net_wait() instead of halting the CPU. The compositor spends its frame-budget slack yielding too. Result: a background fetch progresses while the desktop holds 54 FPS. Web app (gui/gui_app_web.c) A text browser: URL bar, Go, and a Raw/Reader toggle. Raw shows the response verbatim; Reader strips markup (dropping <script>/<style> bodies), decodes the common entities, and word-wraps the body. The line index is cached against content/view/width so scrolling and idle repaints are free. It only ever polls the worker, so it never stalls. TCP throughput (the limit flagged last time) The 32-slot table with a 2 s TIME_WAIT linger capped sustained serving at ~16 req/s. The table is now 64 slots, a connection this end closed after serving a request lingers 250 ms rather than 2 s, and expired TIME_WAIT entries are reaped from the count path so a quiet table still drains. netstat/netmon gained served-since-boot and peak-slots counters. Also: a `webget` CLI (the async client from a shell), listen/accept introspection already present, and a globe icon for the app. Verified on x86_64: `webget` fetched an 812-byte page (HTTP 200, 10 ms) on the worker thread; the GUI Web app rendered the same page in Reader mode while the desktop held 54 FPS; an 80-request burst through the 64-slot table returned 80/80 (was 39/40 before) and netstat reported "80 served, peak 33 of 64 slots" -- a peak that would have overflowed the old table. Builds clean on x86_64, aarch64 and armv8i. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01Fh5z6V7nGPbHvqVG5p4CXX
126 lines
3.5 KiB
C
126 lines
3.5 KiB
C
#ifndef _NET_NET_H
|
|
#define _NET_NET_H
|
|
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <stdbool.h>
|
|
|
|
#define ETHERTYPE_IPV4 0x0800
|
|
#define ETHERTYPE_ARP 0x0806
|
|
|
|
#define IP_PROTO_ICMP 1
|
|
#define IP_PROTO_TCP 6
|
|
#define IP_PROTO_UDP 17
|
|
|
|
static inline uint16_t htons(uint16_t host) {
|
|
return (uint16_t)((host << 8) | (host >> 8));
|
|
}
|
|
static inline uint16_t ntohs(uint16_t net) {
|
|
return htons(net);
|
|
}
|
|
static inline uint32_t htonl(uint32_t host) {
|
|
return (uint32_t)(((host & 0x000000FF) << 24) |
|
|
((host & 0x0000FF00) << 8) |
|
|
((host & 0x00FF0000) >> 8) |
|
|
((host & 0xFF000000) >> 24));
|
|
}
|
|
static inline uint32_t ntohl(uint32_t net) {
|
|
return htonl(net);
|
|
}
|
|
|
|
typedef struct {
|
|
uint8_t dst_mac[6];
|
|
uint8_t src_mac[6];
|
|
uint16_t ethertype;
|
|
} __attribute__((packed)) eth_header_t;
|
|
|
|
typedef struct {
|
|
uint16_t htype;
|
|
uint16_t ptype;
|
|
uint8_t hlen;
|
|
uint8_t plen;
|
|
uint16_t opcode;
|
|
uint8_t sender_mac[6];
|
|
uint32_t sender_ip;
|
|
uint8_t target_mac[6];
|
|
uint32_t target_ip;
|
|
} __attribute__((packed)) arp_header_t;
|
|
|
|
typedef struct {
|
|
uint8_t ihl_version;
|
|
uint8_t tos;
|
|
uint16_t total_length;
|
|
uint16_t id;
|
|
uint16_t flags_fragment;
|
|
uint8_t ttl;
|
|
uint8_t protocol;
|
|
uint16_t checksum;
|
|
uint32_t src_ip;
|
|
uint32_t dst_ip;
|
|
} __attribute__((packed)) ip_header_t;
|
|
|
|
typedef struct {
|
|
uint8_t type;
|
|
uint8_t code;
|
|
uint16_t checksum;
|
|
uint16_t id;
|
|
uint16_t sequence;
|
|
} __attribute__((packed)) icmp_header_t;
|
|
|
|
typedef struct {
|
|
uint16_t src_port;
|
|
uint16_t dst_port;
|
|
uint16_t length;
|
|
uint16_t checksum;
|
|
} __attribute__((packed)) udp_header_t;
|
|
|
|
typedef struct {
|
|
char name[16];
|
|
uint8_t mac[6];
|
|
uint32_t ip;
|
|
uint32_t subnet;
|
|
uint32_t gateway;
|
|
uint32_t dns;
|
|
bool is_up;
|
|
} net_if_t;
|
|
|
|
typedef struct {
|
|
uint32_t ip;
|
|
uint8_t mac[6];
|
|
bool valid;
|
|
} arp_entry_t;
|
|
|
|
void net_init(void);
|
|
void net_receive(const uint8_t* packet, uint16_t length);
|
|
void net_send_eth(const uint8_t* dst_mac, uint16_t ethertype, const void* payload, uint16_t len);
|
|
void net_send_ip(uint32_t dst_ip, uint8_t protocol, const void* payload, uint16_t len);
|
|
void net_send_arp_req(uint32_t target_ip);
|
|
int net_ping(uint32_t target_ip, uint32_t count, uint32_t timeout_ms);
|
|
|
|
net_if_t* net_get_primary_if(void);
|
|
arp_entry_t* net_get_arp_table(int* count_out);
|
|
|
|
uint32_t ip_parse(const char* ip_str);
|
|
void ip_to_str(uint32_t ip, char* buf);
|
|
|
|
// Yield the CPU once while blocked in a network wait loop (a handshake, a
|
|
// receive, a ping reply). Inbound packets are delivered from the NIC interrupt
|
|
// regardless of which task runs, so cooperatively handing the CPU to other
|
|
// tasks -- the GUI compositor above all -- keeps the desktop live during a
|
|
// fetch instead of freezing it for the duration. When the caller is the only
|
|
// runnable task it returns at once, so a shell-context wait still just spins to
|
|
// its timeout.
|
|
void net_wait(void);
|
|
|
|
// One's-complement checksum, summed a byte at a time so the loads cannot be
|
|
// reordered around a caller's store into the buffer being summed, and so no
|
|
// alignment is assumed of a packed header. Words are read big-endian;
|
|
// net_csum_finish returns the result already in network byte order, ready to
|
|
// be assigned straight to a header's checksum field.
|
|
uint32_t net_csum_add(uint32_t sum, const void* data, size_t len);
|
|
uint16_t net_csum_finish(uint32_t sum);
|
|
uint16_t net_checksum(const void* data, size_t len);
|
|
void mac_to_str(const uint8_t* mac, char* buf);
|
|
|
|
#endif // _NET_NET_H
|