My Kernel v0.1.0
packet.h
1
21#ifndef KERNEL_NET_PACKET_H
22#define KERNEL_NET_PACKET_H
23
24#include <kernel/error.h>
25#include <kernel/types.h>
26
27#include <libalgo/queue.h>
28#include <utils/math.h>
29
30struct ethernet_device;
31/* L2 headers */
32struct ethernet_header;
33/* L3 headers */
34struct ipv4_header;
35struct arp_header;
36
51struct packet {
53 size_t packet_size;
54 size_t popped;
55
58
60 union {
61 void *raw;
62 struct ethernet_header *ethernet;
63 } l2;
64
66 union {
67 void *raw;
68 struct ipv4_header *ipv4;
69 struct arp_header *arp;
70 } l3;
71
73 void *payload;
74
76};
77
78#define PACKET_ALIGN (sizeof(uint64_t))
79
81struct packet *packet_new(size_t packet_size);
82
88struct packet *packet_clone(const struct packet *packet);
89
91void packet_free(struct packet *packet);
92
94error_t packet_send(struct packet *packet);
95
97static inline void *packet_start(const struct packet *packet)
98{
99 return ((void *)packet) + align_up(sizeof(struct packet), PACKET_ALIGN);
100}
101
103static inline size_t packet_size(const struct packet *packet)
104{
105 return packet->packet_size;
106}
107
109static inline size_t packet_read_size(const struct packet *packet)
110{
111 return packet->packet_size - packet->popped;
112}
113
115static inline void *packet_end(const struct packet *packet)
116{
118}
119
121error_t packet_put(struct packet *packet, const void *data, size_t size);
122
126size_t packet_peek(struct packet *packet, void *data, size_t size);
127
132size_t packet_pop(struct packet *packet, void *data, size_t size);
133
135#define packet_put_literal(packet, data) packet_put(packet, &data, sizeof(data))
136
138#define packet_peek_literal(packet, data) \
139 packet_peek(packet, &data, sizeof(data))
140
142#define packet_pop_literal(packet, data) packet_pop(packet, &data, sizeof(data))
143
145static inline void packet_mark_l2_start(struct packet *packet)
146{
147 packet->l2.raw = packet_end(packet);
148}
149
151static inline void packet_set_l2_size(struct packet *packet, size_t size)
152{
153 packet->l3.raw = packet->l2.raw + size;
154}
155
157static inline void packet_mark_l3_start(struct packet *packet)
158{
159 packet->l3.raw = packet_end(packet);
160}
161
163static inline void packet_set_l3_size(struct packet *packet, size_t size)
164{
165 packet->payload = packet->l3.raw + size;
166}
167
169static inline void *packet_payload(const struct packet *packet)
170{
171 return packet->payload;
172}
173
175static inline size_t packet_header_size(const struct packet *packet)
176{
177 return packet->payload -
178 (((void *)packet) + align_up(sizeof(struct packet), PACKET_ALIGN));
179}
180
182static inline size_t packet_payload_size(const struct packet *packet)
183{
184 return packet_end(packet) - packet->payload;
185}
186
187#endif /* KERNEL_NET_PACKET_H */
188
static size_t packet_read_size(const struct packet *packet)
Definition: packet.h:109
static size_t packet_size(const struct packet *packet)
Definition: packet.h:103
static void packet_mark_l2_start(struct packet *packet)
Set the current end of the packet as the start of the link layer.
Definition: packet.h:145
struct packet * packet_new(size_t packet_size)
Create a new packet.
Definition: packet.c:8
static void * packet_payload(const struct packet *packet)
Definition: packet.h:169
static size_t packet_header_size(const struct packet *packet)
Definition: packet.h:175
size_t packet_pop(struct packet *packet, void *data, size_t size)
Read data from the packet.
Definition: packet.c:83
size_t packet_peek(struct packet *packet, void *data, size_t size)
Read data from the packet.
Definition: packet.c:73
static void packet_mark_l3_start(struct packet *packet)
Set the current end of the packet as the start of the network layer.
Definition: packet.h:157
struct packet * packet_clone(const struct packet *packet)
Clone a packet.
Definition: packet.c:29
static void * packet_end(const struct packet *packet)
Find the end of the actual packet's content.
Definition: packet.h:115
static void packet_set_l3_size(struct packet *packet, size_t size)
Set the start of the packet payload N bytes after the network layer.
Definition: packet.h:163
error_t packet_put(struct packet *packet, const void *data, size_t size)
Append new data to the packet.
Definition: packet.c:62
static size_t packet_payload_size(const struct packet *packet)
Definition: packet.h:182
void packet_free(struct packet *packet)
Free a packet.
Definition: packet.c:51
static void * packet_start(const struct packet *packet)
Find the start of the actual packet's content.
Definition: packet.h:97
error_t packet_send(struct packet *packet)
Send the packet.
Definition: packet.c:56
static void packet_set_l2_size(struct packet *packet, size_t size)
Set the start of the network layer N bytes after the link layer.
Definition: packet.h:151
#define align_up(_value, _power)
Align _value to the next multiple of _power.
Definition: math.h:47
An ARP header.
Definition: arp.h:28
An ethernet device.
Definition: ethernet.h:50
An ethernet frame.
Definition: ethernet.h:34
Intrusive doubly-linked list node.
Definition: linked_list.h:27
A network packet.
Definition: packet.h:51
node_t rx_this
Definition: packet.h:75
union packet::@2 l2
The packet's link layer header.
union packet::@3 l3
The packet's network layer header.
void * payload
Start of the packet's content (L4 and beyond)
Definition: packet.h:73
size_t allocated_size
Size allocated for the packet.
Definition: packet.h:52
size_t popped
Number of popped bytes.
Definition: packet.h:54
struct ethernet_device * netdev
The internet device this packet went/is going through.
Definition: packet.h:57
size_t packet_size
Size of the contained packet.
Definition: packet.h:53