AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
netbuf.h
Go to the documentation of this file.
1#ifndef _NET_NETBUF_H
2#define _NET_NETBUF_H
3
4#include <core/system.h>
5#include <core/panic.h>
6
7struct netif;
8
16struct netbuf {
17 struct netbuf *next; /* the next one in a queue, or on the free list */
18 struct netif *netif; /* the interface it came in on */
20 size_t len;
21
22 int loopback; /* whether it's an IPv4 packet sent to ourselves */
23
24 /* where a datagram came from (in a socket's queue) */
27
28 /* for a TCP segment that came before its turn: where its data goes,
29 * and whether a FIN follows it */
31 int fin;
32
33 /* its page of memory, and the netbuf before it on the free list */
36};
37
38/* A netbuf's size, and the room kept before the data for the headers
39 * that the layers below add. */
40#define NETBUF_SIZE 2048
41#define NETBUF_HEADROOM 128
42
43/* The end of a netbuf's room. */
44#define NETBUF_END(nb) ((uint8_t *) (nb) + NETBUF_SIZE)
45
46/* Allocates a netbuf, or frees one. */
47struct netbuf *netbuf_alloc(void);
48void netbuf_free(struct netbuf *nb);
49
52static inline void *netbuf_push(struct netbuf *nb, size_t size)
53{
54 if (nb->data - size < (uint8_t *) (nb + 1))
55 panic("netbuf: no room for a header");
56
57 nb->data -= size;
58 nb->len += size;
59 return nb->data;
60}
61
64static inline void *netbuf_pull(struct netbuf *nb, size_t size)
65{
66 if (nb->len < size)
67 return NULL;
68
69 void *header = nb->data;
70 nb->data += size;
71 nb->len -= size;
72 return header;
73}
74
77static inline void *netbuf_put(struct netbuf *nb, size_t size)
78{
79 if (nb->data + nb->len + size > NETBUF_END(nb))
80 return NULL;
81
82 void *tail = nb->data + nb->len;
83 nb->len += size;
84 return tail;
85}
86
87#endif /* ! _NET_NETBUF_H */
uint64_t paddr_t
Definition mm.h:39
#define panic(s)
Definition panic.h:15
static void * netbuf_push(struct netbuf *nb, size_t size)
Makes room for a header of size bytes before the data, and returns it.
Definition netbuf.h:52
static void * netbuf_put(struct netbuf *nb, size_t size)
Adds size bytes at the end of the packet, and returns them, or NULL if there's no room.
Definition netbuf.h:77
static void * netbuf_pull(struct netbuf *nb, size_t size)
Takes the header of size bytes off the front of the packet, and returns it, or NULL if the packet is ...
Definition netbuf.h:64
void netbuf_free(struct netbuf *nb)
Frees a netbuf.
Definition netbuf.c:99
struct netbuf * netbuf_alloc(void)
Allocates a netbuf, empty, with room for the headers that will be put in front of its data.
Definition netbuf.c:58
#define NETBUF_END(nb)
Definition netbuf.h:44
#define NULL
Definition stddef.h:27
unsigned int uint32_t
Definition stddef.h:22
unsigned char uint8_t
Definition stddef.h:20
unsigned short int uint16_t
Definition stddef.h:21
A packet (net/netbuf.c).
Definition netbuf.h:16
int loopback
Definition netbuf.h:22
int fin
Definition netbuf.h:31
uint32_t src_ip
Definition netbuf.h:25
uint32_t seq
Definition netbuf.h:30
struct netif * netif
Definition netbuf.h:18
struct netbuf * next
Definition netbuf.h:17
uint8_t * data
Definition netbuf.h:19
uint16_t src_port
Definition netbuf.h:26
paddr_t page
Definition netbuf.h:34
struct netbuf * free_prev
Definition netbuf.h:35
size_t len
Definition netbuf.h:20
A network interface (net/netif.c): a device that sends and receives Ethernet frames,...
Definition netif.h:16