AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
netif.c File Reference

Network interfaces (see net/netif.h). More...

#include <core/system.h>
#include <core/arch.h>
#include <core/kargs.h>
#include <core/module.h>
#include <core/panic.h>
#include <core/printk.h>
#include <core/string.h>
#include <net/byteorder.h>
#include <net/netif.h>
#include <net/inet.h>
#include <sys/callout.h>
#include <sys/kthread.h>
#include <sys/sched.h>

Go to the source code of this file.

Macros

#define NET_BUDGET   32
#define NET_BACKLOG   256

Functions

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[:gateway].
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.
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.
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, then sleeps until the next frame or callout.
struct netif * netif_find (const char *name)
 Finds an interface by its name.
static void loopback_init (void)
 Sets up lo, the loopback interface, once.
int netif_register (struct netif *netif)
 Registers a network interface.
static int loopback_transmit (struct netif *netif, struct netbuf *nb)
 Sends a frame on lo: a copy of it comes back in.
static int loopback_module_init (void)
 Sets up lo at boot, so that it is there even without a network card.
static void rx_queue (struct netif *netif, struct netbuf *nb)
 Queues a received frame for the network's thread, and wakes it up.
void netif_input (struct netif *netif, struct netbuf *nb)
 Hands the stack a frame that a driver received, possibly from an interrupt.
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 interface.
int netif_output (struct netif *netif, struct netbuf *nb)
 Sends a frame through an interface's driver.

Variables

struct netif * netifs
static int ethers_nr
static struct netif loopback
static struct thread * net_thread
static struct waitq net_wait
static struct netbuf * rx_head
static struct netbuf * rx_tail
static size_t rx_count
static struct callout_list net_callouts

Detailed Description

Network interfaces (see net/netif.h).

A network driver registers its interface, hands it the frames it receives (netif_input()), and sends the frames the interface gives it (its transmit operation). lo, the loopback interface (127.0.0.1/8), is always there and comes first; the others are eth0, eth1, ..., in the order they are registered.

Received frames wait for the network's thread ([net], a kernel thread), which takes them up the stack, NET_BUDGET at a time before the other threads run again, and runs the network's callouts (net_callout_set()). It is an urgent thread: when a driver's interrupt wakes it, it runs as soon as the interrupt returns.

Definition in file netif.c.

Macro Definition Documentation

◆ NET_BACKLOG

#define NET_BACKLOG   256

Definition at line 32 of file netif.c.

Referenced by rx_queue().

◆ NET_BUDGET

#define NET_BUDGET   32

Definition at line 31 of file netif.c.

Referenced by net_main().

Function Documentation

◆ loopback_init()

void loopback_init ( void )
static

◆ loopback_module_init()

int loopback_module_init ( void )
static

Sets up lo at boot, so that it is there even without a network card.

Definition at line 284 of file netif.c.

References loopback_init(), and loopback_module_init().

Referenced by loopback_module_init().

◆ loopback_transmit()

int loopback_transmit ( struct netif * netif,
struct netbuf * nb )
static

Sends a frame on lo: a copy of it comes back in.

Only frames sent through lo explicitly get here (a broadcast, SO_BINDTODEVICE): packets to 127.0.0.0/8 are turned around earlier, by ipv4_output().

Definition at line 255 of file netif.c.

References netbuf::data, netbuf::len, loopback_transmit(), memcpy(), netbuf_alloc(), and netif_input().

Referenced by loopback_init(), and loopback_transmit().

◆ mac_string()

char * mac_string ( const uint8_t mac[6],
char buf[18] )
static

Writes a MAC address into buf as "xx:xx:xx:xx:xx:xx" (printk() has no field widths) and returns buf.

Definition at line 115 of file netif.c.

References mac_string().

Referenced by mac_string(), and netif_register().

◆ net_callout_set()

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.

TCP's timers use it.

Parameters
cthe callout
nsthe delay, in ns
fnthe function to call
argits argument

Definition at line 137 of file netif.c.

References callout_set_on(), net_callout_set(), net_callouts, net_thread, and thread_wakeup().

Referenced by arp_resolve(), arp_retry(), keepalive_timeout(), net_callout_set(), rexmt_timeout(), tcp_dack(), tcp_fin_wait_2(), tcp_keepalive_reset(), tcp_output(), tcp_rexmt_start(), and tcp_time_wait().

◆ net_main()

void net_main ( void * arg)
static

Runs the network's thread: it takes the received frames up the stack and runs the network's callouts, then sleeps until the next frame or callout.

Definition at line 149 of file netif.c.

References arch_rtime_ns(), arch_yield(), callout_list_next(), callout_list_run(), ether_input(), ipv4_input(), netbuf::loopback, netif::mac, NET_BUDGET, net_callouts, net_main(), net_wait, netbuf::netif, netbuf::next, NULL, rx_count, rx_head, rx_tail, and sched_wait().

Referenced by net_main(), and netif_register().

◆ netif_config()

void netif_config ( struct netif * netif)
static

Configures the first Ethernet interface from the kernel's command line: ip=address/prefix-length[:gateway].

Definition at line 73 of file netif.c.

References netif::flags, netif::gateway, htonl, netif::ip, kargs_get(), netif::name, netif_config(), NETIF_UP, netif::netmask, ntohl, NULL, parse_ipv4(), and printk().

Referenced by netif_config(), and netif_register().

◆ netif_find()

struct netif * netif_find ( const char * name)

Finds an interface by its name.

Parameters
namethe name (at most 16 characters, IFNAMSIZ)
Returns
the interface, or NULL

Definition at line 186 of file netif.c.

References netif::name, netif_find(), netifs, netif::next, and NULL.

Referenced by ifreq_request(), netif_find(), and socket_inet_setsockopt().

◆ netif_input()

void netif_input ( struct netif * netif,
struct netbuf * nb )

Hands the stack a frame that a driver received, possibly from an interrupt.

The netbuf is the stack's from now on.

Parameters
netifthe interface it came in on
nbthe frame

Definition at line 323 of file netif.c.

References netbuf::len, netbuf::loopback, netbuf::netif, netif_input(), rx_queue(), and netif::stats.

Referenced by loopback_transmit(), netif_input(), and rx_poll().

◆ netif_loopback()

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 interface.

Parameters
netifthe interface it is received on
nbthe packet, from its IPv4 header

Definition at line 340 of file netif.c.

References netbuf::len, netbuf::loopback, netbuf::netif, netif_loopback(), rx_queue(), and netif::stats.

Referenced by ipv4_output(), and netif_loopback().

◆ netif_output()

int netif_output ( struct netif * netif,
struct netbuf * nb )

Sends a frame through an interface's driver.

The netbuf stays the caller's.

Parameters
netifthe interface
nbthe frame
Returns
0, or the driver's negative errno

Definition at line 358 of file netif.c.

References netbuf::len, netif_output(), netif::stats, and netif::transmit.

Referenced by ether_output(), and netif_output().

◆ netif_register()

int netif_register ( struct netif * netif)

Registers a network interface.

An interface without a name gets the next "ethN". The first Ethernet interface is configured from the kernel's ip= argument. The network's thread is started with the first interface.

Parameters
netifthe interface, with its MAC address and transmit set
Returns
0

Definition at line 206 of file netif.c.

References ethers_nr, netif::flags, kthread_create(), loopback, loopback_init(), netif::mac, mac_string(), netif::mtu, netif::name, net_main(), net_thread, netif_config(), NETIF_LOOPBACK, netif_register(), netifs, netif::next, NULL, panic, printk(), snprintf(), netif::transmit, and thread::urgent.

Referenced by e1000_probe(), loopback_init(), and netif_register().

◆ parse_ipv4()

int parse_ipv4 ( const char ** s,
uint32_t * ip )
static

Parses an address "a.b.c.d" at *s into *ip, in network byte order, and moves *s past it.

Returns 0, or -1 if it isn't an address.

Definition at line 49 of file netif.c.

References htonl, and parse_ipv4().

Referenced by netif_config(), and parse_ipv4().

◆ rx_queue()

void rx_queue ( struct netif * netif,
struct netbuf * nb )
static

Queues a received frame for the network's thread, and wakes it up.

The frame is dropped if the interface is down or too many frames wait.

Definition at line 294 of file netif.c.

References netif::flags, NET_BACKLOG, net_thread, NETIF_UP, netbuf::next, NULL, rx_count, rx_head, rx_queue(), rx_tail, and thread_wakeup().

Referenced by netif_input(), netif_loopback(), and rx_queue().

Variable Documentation

◆ ethers_nr

int ethers_nr
static

Definition at line 35 of file netif.c.

Referenced by netif_register().

◆ loopback

struct netif loopback
static

Definition at line 37 of file netif.c.

Referenced by loopback_init(), and netif_register().

◆ net_callouts

struct callout_list net_callouts
static

Definition at line 45 of file netif.c.

Referenced by net_callout_set(), and net_main().

◆ net_thread

struct thread* net_thread
static

Definition at line 39 of file netif.c.

Referenced by net_callout_set(), netif_register(), and rx_queue().

◆ net_wait

struct waitq net_wait
static

Definition at line 40 of file netif.c.

Referenced by net_main().

◆ netifs

struct netif* netifs

Definition at line 34 of file netif.c.

Referenced by ifconf(), ipv4_is_local(), ipv4_route(), netif_find(), netif_index(), netif_register(), and route().

◆ rx_count

size_t rx_count
static

Definition at line 43 of file netif.c.

Referenced by net_main(), and rx_queue().

◆ rx_head

struct netbuf* rx_head
static

Definition at line 42 of file netif.c.

Referenced by net_main(), and rx_queue().

◆ rx_tail

struct netbuf * rx_tail
static

Definition at line 42 of file netif.c.

Referenced by net_main(), and rx_queue().