The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
netif.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file netif.h
5 * @brief network interfaces: the devices that send and receive frames.
6 *
7 * a driver registers an interface with a send function, and gives each
8 * frame it receives to netif_receive, which gives it to Ethernet (or, on
9 * the loopback interface, which has no Ethernet header, to IPv4).
10 *
11 * locking: all network state is under one spinlock (net_lock, irqsave).
12 * frames are received in the device's interrupt handler, and the stack
13 * handles them there, with the lock held. on one CPU that is simple and
14 * safe: a syscall that sends holds the lock with interrupts off, so the
15 * receive path cannot run at the same time.
16 */
17
18#include "common/stdint.h"
20
21#define NETIF_MAX 4
22#define NETIF_NAME_MAX 8
23#define ETH_ADDR_LEN 6
24#define ETH_MTU 1500
25
26struct netif;
27
28/**
29 * send one frame (for Ethernet: from the destination address to the end
30 * of the payload, no checksum; for loopback: an IPv4 packet). called with
31 * net_lock held.
32 * @return false if the device has no room (the frame is dropped).
33 */
34typedef bool (*netif_send_fn)(struct netif *netif, const void *frame, uint32_t length);
35
36typedef struct netif
37{
39 uint8_t mac[ETH_ADDR_LEN]; // all zero for the loopback interface
43 void *driver; // the driver's own data
44
45 // IPv4, in network byte order; 0 when not set
49
57
58/**
59 * set up the network layer and the loopback interface (lo).
60 */
61void net_init(void);
62
63/**
64 * take and release the network lock.
65 */
66uint32_t net_lock(void);
68
69/**
70 * sleep on a wait queue with net_lock held (interruptibly): the lock is
71 * let go while the process sleeps and held again on return. the caller
72 * checks its condition and signal_pending in a loop.
73 */
74void net_wait(wait_queue_t *wq);
75
76/**
77 * add an interface. the name is made from prefix and a number (eth0).
78 * @return the interface, or NULL if there are already NETIF_MAX.
79 */
80netif_t *netif_register(const char *prefix, const uint8_t mac[ETH_ADDR_LEN], uint32_t mtu,
81 netif_send_fn send, void *driver);
82
83/**
84 * a frame from the device (called with net_lock held, often from an
85 * interrupt). the data is only valid during the call.
86 */
87void netif_receive(netif_t *netif, const void *frame, uint32_t length);
88
89/**
90 * send a frame on an interface and count it (net_lock held).
91 */
92bool netif_send(netif_t *netif, const void *frame, uint32_t length);
93
94/**
95 * the interfaces, for /proc/net and the configuration calls.
96 * @return NULL after the last one.
97 */
99
100/**
101 * the interface with this name, or NULL.
102 */
103netif_t *netif_find(const char *name);
104
105// ioctls on any IPv4 socket: read and set an interface (struct ifreq).
106// the S calls need root. the numbers are Linux's, but for the gateway,
107// which Linux sets through a routing table; ginnOS keeps one gateway per
108// interface and uses two private numbers for it.
109#define SIOCGIFFLAGS 0x8913
110#define SIOCSIFFLAGS 0x8914
111#define SIOCGIFADDR 0x8915
112#define SIOCSIFADDR 0x8916
113#define SIOCGIFBRDADDR 0x8919
114#define SIOCGIFNETMASK 0x891B
115#define SIOCSIFNETMASK 0x891C
116#define SIOCGIFMTU 0x8921
117#define SIOCGIFHWADDR 0x8927
118#define SIOCGIFGATEWAY 0x89F0 // SIOCDEVPRIVATE: ifr_addr is the gateway
119#define SIOCSIFGATEWAY 0x89F1
120
121// ifr_flags
122#define IFF_UP 0x0001
123#define IFF_BROADCAST 0x0002
124#define IFF_LOOPBACK 0x0008
125#define IFF_RUNNING 0x0040
126
127#define IFNAMSIZ 16
128
129// struct ifreq: the name, then a union of 16 bytes on i686, 24 on x86_64
130// (the size of Linux's struct ifmap there)
131typedef struct
132{
134 union
135 {
136 struct
137 {
139 uint8_t data[14];
140 } addr; // a sockaddr: an address, or the MAC with family 1 (ARPHRD_ETHER)
142 int32_t value; // the MTU
143 unsigned long pad[3];
144 };
145} kifreq_t;
146
147/**
148 * an interface ioctl (from a socket).
149 * @param arg a struct ifreq in user memory.
150 * @return 0, or -ENODEV (no interface with the name), -EPERM, -EINVAL,
151 * -EFAULT, or -ENOTTY for another request.
152 */
153int32_t netif_ioctl(uint32_t request, void *arg);
uint8_t flags
Definition idt.c:3
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
unsigned short uint16_t
Definition stdint.h:13
int int32_t
Definition stdint.h:14
short int16_t
Definition stdint.h:12
netif_t * netif_register(const char *prefix, const uint8_t mac[ETH_ADDR_LEN], uint32_t mtu, netif_send_fn send, void *driver)
add an interface.
Definition netif.c:105
netif_t * netif_find(const char *name)
the interface with this name, or NULL.
Definition netif.c:171
void net_init(void)
set up the network layer and the loopback interface (lo).
Definition netif.c:91
bool netif_send(netif_t *netif, const void *frame, uint32_t length)
send a frame on an interface and count it (net_lock held).
Definition netif.c:154
uint32_t net_lock(void)
take and release the network lock.
Definition netif.c:36
void netif_receive(netif_t *netif, const void *frame, uint32_t length)
a frame from the device (called with net_lock held, often from an interrupt).
Definition netif.c:140
void net_wait(wait_queue_t *wq)
sleep on a wait queue with net_lock held (interruptibly): the lock is let go while the process sleeps...
Definition netif.c:46
netif_t * netif_at(uint32_t index)
the interfaces, for /proc/net and the configuration calls.
Definition netif.c:166
int32_t netif_ioctl(uint32_t request, void *arg)
an interface ioctl (from a socket).
Definition netif.c:216
void net_unlock(uint32_t flags)
Definition netif.c:41
#define ETH_ADDR_LEN
Definition netif.h:23
#define NETIF_NAME_MAX
Definition netif.h:22
bool(*) netif_send_fn(struct netif *netif, const void *frame, uint32_t length)
send one frame (for Ethernet: from the destination address to the end of the payload,...
Definition netif.h:34
#define IFNAMSIZ
Definition netif.h:127
struct netif netif_t
ssize_t send(int fd, const void *buf, size_t len, int flags)
#define bool
Definition stdbool.h:8
Definition netif.h:132
char name[IFNAMSIZ]
Definition netif.h:133
Definition netif.h:37
void * driver
Definition netif.h:43
bool loopback
Definition netif.h:41
uint32_t netmask
Definition netif.h:47
uint32_t mtu
Definition netif.h:40
uint32_t address
Definition netif.h:46
uint32_t tx_dropped
Definition netif.h:55
uint8_t mac[ETH_ADDR_LEN]
Definition netif.h:39
uint64_t rx_bytes
Definition netif.h:51
netif_send_fn send
Definition netif.h:42
char name[NETIF_NAME_MAX]
Definition netif.h:38
uint64_t rx_packets
Definition netif.h:50
uint32_t gateway
Definition netif.h:48
uint64_t tx_packets
Definition netif.h:53
uint64_t tx_bytes
Definition netif.h:54
uint32_t rx_dropped
Definition netif.h:52
a queue of blocked processes, linked through process_t::wait_next.
Definition wait_queue.h:35
struct kifreq_t::@013275246343256132214203350006272232306350211233::@036171060105106122243032352020117223361004264361 addr