summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-04-28 21:39:53 +0200
committerAnton Kling <anton@kling.gg>2024-04-28 21:40:39 +0200
commit507062d51b3e018fdfac838e7982ffe62ff423c5 (patch)
tree9ed18a03fd2c35329525872f2aabe2455a7a6236 /kernel
parent7695cb9ca77a32aae75cf0d2bcea9769cd938615 (diff)
Kernel: Use ringbuffer for handling incoming socket data
Diffstat (limited to 'kernel')
-rw-r--r--kernel/lib/ringbuffer.c4
-rw-r--r--kernel/lib/ringbuffer.h4
-rw-r--r--kernel/network/tcp.c8
-rw-r--r--kernel/queue.c3
-rw-r--r--kernel/socket.c13
-rw-r--r--kernel/socket.h4
6 files changed, 18 insertions, 18 deletions
diff --git a/kernel/lib/ringbuffer.c b/kernel/lib/ringbuffer.c
index 425971d..66dd17e 100644
--- a/kernel/lib/ringbuffer.c
+++ b/kernel/lib/ringbuffer.c
@@ -65,6 +65,10 @@ u32 ringbuffer_read(struct ringbuffer *rb, u8 *buffer, u32 len) {
return orig_len - len;
}
+int ringbuffer_isempty(const struct ringbuffer *rb) {
+ return (rb->write_ptr == rb->read_ptr);
+}
+
void ringbuffer_free(struct ringbuffer *rb) {
kfree(rb->buffer);
rb->buffer = NULL;
diff --git a/kernel/lib/ringbuffer.h b/kernel/lib/ringbuffer.h
index c329af3..3ebc507 100644
--- a/kernel/lib/ringbuffer.h
+++ b/kernel/lib/ringbuffer.h
@@ -1,3 +1,5 @@
+#ifndef RINGBUFFER_H
+#define RINGBUFFER_H
#include <typedefs.h>
struct ringbuffer {
@@ -10,7 +12,9 @@ struct ringbuffer {
int ringbuffer_init(struct ringbuffer *rb, u32 buffer_size);
u32 ringbuffer_write(struct ringbuffer *rb, const u8 *buffer, u32 len);
u32 ringbuffer_read(struct ringbuffer *rb, u8 *buffer, u32 len);
+int ringbuffer_isempty(const struct ringbuffer *rb);
void ringbuffer_free(struct ringbuffer *rb);
#ifdef KERNEL_TEST
void ringbuffer_test(void);
#endif // KERNEL_TEST
+#endif // RINGBUFFER_H
diff --git a/kernel/network/tcp.c b/kernel/network/tcp.c
index 997b699..8740a63 100644
--- a/kernel/network/tcp.c
+++ b/kernel/network/tcp.c
@@ -186,7 +186,6 @@ void handle_tcp(ipv4_t src_ip, const u8 *payload, u32 payload_length) {
}
struct TcpConnection *incoming_connection = tcp_find_connection(dst_port);
- kprintf("dst_port: %d\n", dst_port);
if (!incoming_connection) {
kprintf("unable to find open port for incoming connection\n");
}
@@ -214,10 +213,9 @@ void handle_tcp(ipv4_t src_ip, const u8 *payload, u32 payload_length) {
}
u16 tcp_payload_length = payload_length - header->data_offset * sizeof(u32);
if (tcp_payload_length > 0) {
- int len = fifo_object_write(
- (u8 *)(payload + header->data_offset * sizeof(u32)), 0,
- tcp_payload_length, incoming_connection->data_file);
- assert(len >= 0);
+ const u8 *tcp_payload = payload + header->data_offset * sizeof(u32);
+ u32 len = ringbuffer_write(&incoming_connection->buffer, tcp_payload,
+ tcp_payload_length);
assert(len == tcp_payload_length);
incoming_connection->ack += len;
tcp_send_ack(incoming_connection);
diff --git a/kernel/queue.c b/kernel/queue.c
index 0bef21b..43b2400 100644
--- a/kernel/queue.c
+++ b/kernel/queue.c
@@ -65,8 +65,7 @@ int queue_should_block(struct event_queue *q, int *is_empty) {
} else if (EVENT_TYPE_TCP_SOCKET == ev->type) {
struct TcpConnection *con = tcp_get_connection(ev->internal_id, q->p);
assert(con);
- assert(con->data_file);
- if (con->data_file->has_data) {
+ if (!ringbuffer_isempty(&con->buffer)) {
return 0;
}
}
diff --git a/kernel/socket.c b/kernel/socket.c
index d3dfd1a..e0c3f35 100644
--- a/kernel/socket.c
+++ b/kernel/socket.c
@@ -80,7 +80,7 @@ struct TcpConnection *internal_tcp_incoming(u32 src_ip, u16 src_port,
con->incoming_port = dst_port; // FIXME: Should be different for each
// connection
- con->data_file = create_fifo_object();
+ ringbuffer_init(&con->buffer, 8192);
stack_push(&listen->incoming_connections, (void *)connection_id);
return con;
}
@@ -141,7 +141,7 @@ u32 tcp_connect_ipv4(u32 ip, u16 port, int *error) {
con->outgoing_ip = ip;
con->outgoing_port = port;
- con->data_file = create_fifo_object();
+ ringbuffer_init(&con->buffer, 8192);
tcp_send_syn(con);
@@ -190,14 +190,9 @@ int tcp_read(u32 socket, u8 *buffer, u64 buffer_size, u64 *out) {
return 0;
}
- int rc = fifo_object_read(buffer, 0, buffer_size, con->data_file);
- if (rc <= 0) {
- enable_interrupts();
- rc = 0;
- return 0;
- }
+ u32 len = ringbuffer_read(&con->buffer, buffer, buffer_size);
if (out) {
- *out = rc;
+ *out = len;
}
return 1;
}
diff --git a/kernel/socket.h b/kernel/socket.h
index 550b62b..ad505b8 100644
--- a/kernel/socket.h
+++ b/kernel/socket.h
@@ -3,6 +3,7 @@
#include <fs/fifo.h>
#include <fs/vfs.h>
#include <lib/buffered_write.h>
+#include <lib/ringbuffer.h>
#include <lib/stack.h>
#include <stddef.h>
#include <typedefs.h>
@@ -40,8 +41,7 @@ struct TcpConnection {
u16 outgoing_port;
int unhandled_packet;
-
- FIFO_FILE *data_file;
+ struct ringbuffer buffer;
u32 seq;
u32 ack;