32#define NET_BACKLOG 256
53 for (
int i = 0; i < 4; ++i) {
54 if (**s <
'0' || **s >
'9')
58 while (**s >=
'0' && **s <=
'9')
59 byte =
byte * 10 + (*(*s)++ -
'0');
61 if (
byte > 255 || (i < 3 && *(*s)++ !=
'.'))
64 addr = addr << 8 | byte;
80 uint32_t ip, gateway = 0, prefix = 24;
88 while (*s >=
'0' && *s <=
'9')
89 prefix = prefix * 10 + (*s++ -
'0');
95 if (*s ==
':' && (++s,
parse_ipv4(&s, &gateway)))
105 a >> 24, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff, prefix,
106 g >> 24, (g >> 16) & 0xff, (g >> 8) & 0xff, g & 0xff);
110 printk(
"net: ip=: not <address>/<prefix length>[:<gateway>]\n");
117 static const char digits[] =
"0123456789abcdef";
119 for (
int i = 0; i < 6; ++i) {
120 buf[i * 3] = digits[mac[i] >> 4];
121 buf[i * 3 + 1] = digits[mac[i] & 0xf];
122 buf[i * 3 + 2] = i < 5 ?
':' :
'\0';
214 panic(
"net: can't make the network's thread");
297 netif->stats.rx_dropped++;
void arch_yield(void)
Puts the current thread at the end of the ready list, and runs the next ready thread.
void callout_list_run(struct callout_list *list, uint64_t now)
Calls the callouts of a list that are due, taking each off the list first.
uint64_t callout_list_next(struct callout_list *list)
Finds when the first callout of a list is due.
void callout_set_on(struct callout_list *list, struct callout *c, uint64_t ns, void(*fn)(void *), void *arg)
Sets a callout on a list: fn(arg) will be called once ns ns have passed, by whoever runs the list.
void ether_input(struct netif *netif, struct netbuf *nb)
Takes in a frame that an interface received, and passes it to ARP or IPv4.
void ipv4_input(struct netif *netif, struct netbuf *nb, const uint8_t src_mac[6])
Takes in an IPv4 packet that an interface received, and passes it to its protocol.
int kargs_get(const char *key, const char **value)
Looks up an argument of the kernel's command line.
int kthread_create(const char *name, void(*fn)(void *), void *arg, struct thread **ref)
Makes a kernel thread, and makes it ready to run.
void * memcpy(void *dst, const void *src, size_t n)
Copies n bytes; dst and src must not overlap.
#define MODULE_INIT(name, i, f)
struct netbuf * netbuf_alloc(void)
Allocates a netbuf, empty, with room for the headers that will be put in front of its data.
static struct thread * net_thread
static int loopback_transmit(struct netif *netif, struct netbuf *nb)
Sends a frame on lo: a copy of it comes back in.
static struct netif loopback
static void net_main(void *arg)
Runs the network's thread: it takes the received frames up the stack and runs the network's callouts,...
static struct netbuf * rx_head
static int parse_ipv4(const char **s, uint32_t *ip)
Parses an address "a.b.c.d" at *s into *ip, in network byte order, and moves *s past it.
static void netif_config(struct netif *netif)
Configures the first Ethernet interface from the kernel's command line: ip=address/prefix-length[:gat...
static void loopback_init(void)
Sets up lo, the loopback interface, once.
static struct netbuf * rx_tail
static void rx_queue(struct netif *netif, struct netbuf *nb)
Queues a received frame for the network's thread, and wakes it up.
static struct waitq net_wait
static char * mac_string(const uint8_t mac[6], char buf[18])
Writes a MAC address into buf as "xx:xx:xx:xx:xx:xx" (printk() has no field widths) and returns buf.
static struct callout_list net_callouts
static int loopback_module_init(void)
Sets up lo at boot, so that it is there even without a network card.
int netif_register(struct netif *netif)
Registers a network interface.
struct netif * netif_find(const char *name)
Finds an interface by its name.
int netif_output(struct netif *netif, struct netbuf *nb)
Sends a frame through an interface's driver.
void net_callout_set(struct callout *c, uint64_t ns, void(*fn)(void *), void *arg)
Sets a callout that the network's thread runs: fn(arg) is called once ns ns have passed.
void netif_loopback(struct netif *netif, struct netbuf *nb)
Hands the stack an IPv4 packet sent to one of our own addresses, as if it had come in through an inte...
void netif_input(struct netif *netif, struct netbuf *nb)
Hands the stack a frame that a driver received, possibly from an interrupt.
int printk(const char *fmt,...)
Writes a kernel message, to the message buffer and the consoles.
int sched_wait(struct waitq *waitq, uint64_t until)
Puts the current thread to sleep on a wait queue until it is woken up, a signal ends the sleep,...
int snprintf(char *s, size_t n, const char *fmt,...)
Formats a string into a buffer.
unsigned long long int uint64_t
A list of callouts called from one context: the timer's interrupt (callout_set()),...
A function to call later, with its argument.
A network interface (net/netif.c): a device that sends and receives Ethernet frames,...
int(*) transmit(struct netif *netif, struct netbuf *nb)
struct netif::@020302303320120142126121344055047003105154122231 stats
int urgent
it runs before the others once woken, as soon as the interrupt that woke it returns (sched_thread_wak...
uint64_t arch_rtime_ns(void)
Reads the monotonic clock, which starts at boot.
void thread_wakeup(struct thread *thread)
Wakes up one thread, if it is sleeping on a wait queue.