/* Shared kernel receive pump: see netcore.h. */ #include "netcore.h" #include "dns.h" #include "net.h" #include "netdev.h" #include "netkexec.h" #include "process.h" #include "spinlock.h" #include "tcp.h" /* Reply scratch for ARP/ICMP answered by the pump. The received frame is * still in netdev_rx_scratch(), so the reply cannot reuse it. */ static unsigned char reply_buf[NETDEV_FRAME_MAX]; /* Stack operations that may sleep between pumps, tracked per process so a * killed waiter cannot leave the netkexec/netlog gates stuck. */ #define NET_STACK_MAX 16 static struct { unsigned int pid; unsigned int depth; } stack_ops[NET_STACK_MAX]; static int stack_slot(unsigned int pid) { unsigned int i; for (i = 0; i < NET_STACK_MAX; i++) if (stack_ops[i].depth && stack_ops[i].pid == pid) return (int)i; for (i = 0; i < NET_STACK_MAX; i++) if (!stack_ops[i].depth) { stack_ops[i].pid = pid; return (int)i; } return -1; } void net_stack_enter(void) { struct process *p = current_proc(); int slot; if (!p) return; slot = stack_slot(p->pid); if (slot >= 0) stack_ops[slot].depth++; } void net_stack_leave(void) { struct process *p = current_proc(); int slot; if (!p) return; slot = stack_slot(p->pid); if (slot >= 0 && stack_ops[slot].depth) stack_ops[slot].depth--; } int net_stack_busy(void) { unsigned int i; for (i = 0; i < NET_STACK_MAX; i++) if (stack_ops[i].depth) return 1; return 0; } void net_stack_reap(unsigned int pid) { unsigned int i; for (i = 0; i < NET_STACK_MAX; i++) if (stack_ops[i].pid == pid) stack_ops[i].depth = 0; } unsigned int net_irq_enter(void) { unsigned int flags = spin_irq_save_disable(); __asm__ volatile ("sti"); return flags; } void net_irq_exit(unsigned int flags) { spin_irq_restore(flags); } int net_pump(unsigned int budget) { const struct netdev *nd = netdev_active(); unsigned char *rx; unsigned int n, flags; if (!nd) return 0; if (netdev_in_use()) return 0; rx = netdev_rx_scratch(); flags = net_irq_enter(); for (n = 0; n < budget; n++) { int len = netdev_rx_frame(nd, rx, NETDEV_FRAME_MAX); if (len < 0) break; if (tcp_client_input(rx, (unsigned int)len)) continue; if (tcp_srv_input(rx, (unsigned int)len)) continue; if (dns_input(rx, (unsigned int)len)) continue; if (netkexec_capture_frame(rx, (unsigned int)len)) continue; net_arp_input(rx, (unsigned int)len); { int rlen = net_build_reply(rx, (unsigned int)len, reply_buf, sizeof(reply_buf)); if (rlen > 0) netdev_tx_frame(nd, reply_buf, (unsigned int)rlen); } } net_irq_exit(flags); return (int)n; }