AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
netbuf.c
Go to the documentation of this file.
1
9
10#include <core/system.h>
11#include <core/arch.h>
12#include <mm/mm.h>
13#include <net/netbuf.h>
14
15/* The free netbufs, and how many there are. Once more than NETBUF_KEEP
16 * are free, a page whose netbufs are all free goes back to the page
17 * allocator (keeping fewer would only make the next packets take a page
18 * again at once). */
19#define NETBUF_KEEP 64
20#define NETBUF_PER_PAGE (PAGE_SIZE / NETBUF_SIZE)
21
22static struct netbuf *free_netbufs;
23static size_t free_count;
24
26static void free_push(struct netbuf *nb)
27{
28 nb->free_prev = NULL;
29 nb->next = free_netbufs;
30
31 if (free_netbufs)
33
34 free_netbufs = nb;
35 free_count++;
36}
37
39static void free_remove(struct netbuf *nb)
40{
41 if (nb->free_prev)
42 nb->free_prev->next = nb->next;
43 else
44 free_netbufs = nb->next;
45
46 if (nb->next)
47 nb->next->free_prev = nb->free_prev;
48
49 free_count--;
50}
51
58struct netbuf *netbuf_alloc(void)
59{
60 if (!free_netbufs) {
61 struct vm_page *vm_page = mm_page_alloc();
62
63 if (!vm_page)
64 return NULL;
65
67
68 if (!page) {
70 return NULL;
71 }
72
73 for (size_t off = 0; off < PAGE_SIZE; off += NETBUF_SIZE) {
74 struct netbuf *nb = (struct netbuf *) (page + off);
75 nb->page = vm_page->paddr;
76 free_push(nb);
77 }
78 }
79
80 struct netbuf *nb = free_netbufs;
81 free_remove(nb);
82
83 /* the page's reference count is how many of its netbufs are in use */
85
86 nb->next = NULL;
87 nb->netif = NULL;
88 nb->data = (uint8_t *) (nb + 1) + NETBUF_HEADROOM;
89 nb->len = 0;
90
91 return nb;
92}
93
99void netbuf_free(struct netbuf *nb)
100{
101 free_push(nb);
102 mm_page_decref(nb->page);
103
104 if (mm_page_ref(nb->page) || free_count <= NETBUF_KEEP)
105 return;
106
107 /* all the page's netbufs are free: take them off the list, and give
108 * the page back */
109 uint8_t *page = (uint8_t *) ((uintptr_t) nb & ~(uintptr_t) PAGE_MASK);
110
111 for (size_t i = 0; i < NETBUF_PER_PAGE; ++i)
112 free_remove((struct netbuf *) (page + i * NETBUF_SIZE));
113
115}
#define PAGE_SIZE
Definition system.h:7
#define PAGE_MASK
Definition system.h:8
void mm_page_decref(paddr_t paddr)
Drops a reference to a page of RAM.
Definition mm.c:50
struct vm_page * mm_page_alloc(void)
Allocates a free page of physical memory.
Definition mm.c:78
size_t mm_page_ref(paddr_t paddr)
Returns how many references a page of RAM has.
Definition mm.c:62
void mm_page_dealloc(paddr_t paddr)
Frees a page of physical memory, back to the zone it came from.
Definition mm.c:103
void mm_page_incref(paddr_t paddr)
Adds a reference to a page of RAM.
Definition mm.c:39
static struct netbuf * free_netbufs
Definition netbuf.c:22
static size_t free_count
Definition netbuf.c:23
#define NETBUF_KEEP
Definition netbuf.c:19
#define NETBUF_PER_PAGE
Definition netbuf.c:20
static void free_remove(struct netbuf *nb)
Takes a netbuf off the free list.
Definition netbuf.c:39
static void free_push(struct netbuf *nb)
Puts a netbuf on the free list.
Definition netbuf.c:26
#define NETBUF_SIZE
Definition netbuf.h:40
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_HEADROOM
Definition netbuf.h:41
void * arch_phys_map(uintptr_t paddr, size_t size)
Returns a kernel address for a range of physical memory: its address in the direct map if it's there,...
Definition pmap.c:497
#define NULL
Definition stddef.h:27
__SIZE_TYPE__ uintptr_t
Definition stddef.h:12
unsigned char uint8_t
Definition stddef.h:20
A packet (net/netbuf.c).
Definition netbuf.h:16
struct netif * netif
Definition netbuf.h:18
struct netbuf * next
Definition netbuf.h:17
uint8_t * data
Definition netbuf.h:19
paddr_t page
Definition netbuf.h:34
struct netbuf * free_prev
Definition netbuf.h:35
size_t len
Definition netbuf.h:20
A page of physical memory (pages[] has one for every page of RAM).
Definition vm.h:150
size_t off
its offset in its object or anon
Definition vm.h:155
paddr_t paddr
its physical address
Definition vm.h:151