From e49d2a9fa5a485c33a250ce843d44fc6dedea8b5 Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Mon, 8 Jul 2024 21:37:15 +0200 Subject: Kernel/Net: Don't use kmalloc to create send buffers Current method is also really bad since it uses multiple copies when it should instead just copy to the send buffer of the network card directly. But I have other things that I want to prioritize first. --- kernel/network/tcp.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'kernel/network/tcp.c') diff --git a/kernel/network/tcp.c b/kernel/network/tcp.c index 8a56dd1..8717250 100644 --- a/kernel/network/tcp.c +++ b/kernel/network/tcp.c @@ -87,6 +87,7 @@ static void tcp_send(struct TcpConnection *con, u8 *buffer, u16 length, send_ipv4_packet((ipv4_t){.d = con->outgoing_ip}, 6, buffer, length); } +u8 tcp_buffer[0x1000]; void tcp_send_empty_payload(struct TcpConnection *con, u8 flags) { struct TcpHeader header; memset(&header, 0, sizeof(header)); @@ -110,8 +111,10 @@ void tcp_send_empty_payload(struct TcpConnection *con, u8 flags) { header.checksum = tcp_calculate_checksum( ip_address, con->outgoing_ip, (const u8 *)payload, payload_length, &header, sizeof(struct TcpHeader) + payload_length); - int send_len = sizeof(header) + payload_length; - u8 *send_buffer = kmalloc(send_len); + u32 send_len = sizeof(header) + payload_length; + + assert(send_len < sizeof(tcp_buffer)); + u8 *send_buffer = tcp_buffer; memcpy(send_buffer, &header, sizeof(header)); memcpy(send_buffer + sizeof(header), payload, payload_length); @@ -133,13 +136,15 @@ void tcp_close_connection(struct TcpConnection *con) { if (TCP_STATE_CLOSE_WAIT == con->state) { tcp_send_empty_payload(con, FIN); con->state = TCP_STATE_LAST_ACK; + tcp_destroy_connection(con); // Client does not appear to respond + // with last ack? return; } if (TCP_STATE_ESTABLISHED == con->state) { // FIXME: // Book says it should be FIN but observed network traffic says it // should be FIN|ACK? - tcp_send_empty_payload(con, FIN | ACK); + tcp_send_empty_payload(con, FIN); con->state = TCP_STATE_FIN_WAIT1; return; } @@ -189,9 +194,9 @@ int send_tcp_packet(struct TcpConnection *con, const u8 *payload, header.checksum = tcp_calculate_checksum( ip_address, con->outgoing_ip, (const u8 *)payload, payload_length, &header, sizeof(struct TcpHeader) + payload_length); - int send_len = sizeof(header) + payload_length; - u8 *send_buffer = kmalloc(send_len); - assert(send_buffer); + u32 send_len = sizeof(header) + payload_length; + assert(send_len < sizeof(tcp_buffer)); + u8 *send_buffer = tcp_buffer; memcpy(send_buffer, &header, sizeof(header)); memcpy(send_buffer + sizeof(header), payload, payload_length); -- cgit v1.2.3