11#include " network_dispatch.hpp"
2+ #include " network.h"
23#include " drivers/virtio_net_pci/virtio_net_pci.hpp"
34#include " memory/page_allocator.h"
45#include " net/link_layer/eth.h"
5- #include " port_manager.h"
66#include " net/network_types.h"
7- #include " net/link_layer/arp.h"
8- #include " net/application_layer/dhcp_daemon.h"
7+ #include " port_manager.h"
98#include " std/memfunctions.h"
10- #include " console/kio.h"
11- #include " types.h"
12- #include " process/scheduler.h"
13- #include " std/string.h"
14-
15- #define QUEUE_CAPACITY 1024
16-
179
18- // TODO: takes a moment to responde to every request. smt is creating a variable delay in receiving and trasmission somewhere
1910extern void sleep (uint64_t ms);
2011extern uintptr_t malloc (uint64_t size);
21- extern void free (void *ptr, uint64_t size);
12+ extern void free (void *ptr, uint64_t size);
2213
2314static uint16_t g_net_pid = 0xFFFF ;
15+ static uint8_t recv_buffer[MAX_PACKET_SIZE ];
2416
25- NetworkDispatch::NetworkDispatch () : ports(UINT16_MAX + 1 ),
17+ NetworkDispatch::NetworkDispatch ()
18+ : ports(UINT16_MAX + 1 ),
2619 driver(nullptr ),
2720 tx_queue(QUEUE_CAPACITY ),
2821 rx_queue(QUEUE_CAPACITY )
@@ -44,38 +37,40 @@ bool NetworkDispatch::init()
4437void NetworkDispatch::handle_download_interrupt ()
4538{
4639 if (!driver) return ;
40+
41+ sizedptr raw = driver->handle_receive_packet (recv_buffer);
42+ if (raw.size < sizeof (eth_hdr_t )) {
43+ return ;
44+ }
4745
48- sizedptr raw = driver->handle_receive_packet ();
49- if (!raw.ptr ) return ;
46+ sizedptr frame{0 , raw.size };
47+ frame.ptr = reinterpret_cast <uintptr_t >(
48+ kalloc (reinterpret_cast <void *>(get_current_heap ()),
49+ raw.size , ALIGN_16B ,
50+ get_current_privilege (), false ));
51+ if (!frame.ptr ) return ;
5052
51- sizedptr frame = make_copy (raw);
52- free_frame (raw);
53+ memcpy (reinterpret_cast <void *>(frame.ptr ), recv_buffer, raw.size );
5354
54- if (frame.ptr && frame.size >= sizeof (eth_hdr_t )) {
55- if (!rx_queue.enqueue (frame))
56- free_frame (frame);
57- } else {
55+ if (!rx_queue.enqueue (frame))
5856 free_frame (frame);
59- }
6057}
6158
6259void NetworkDispatch::handle_upload_interrupt ()
6360{
64- if (driver) driver->handle_sent_packet ();
61+ if (driver)
62+ driver->handle_sent_packet ();
6563}
6664
6765bool NetworkDispatch::enqueue_frame (const sizedptr &frame)
6866{
69- if (frame.size == 0 ) {
70- kprintf (" [NetworkDispatch] drop zero‑length frame\n " );
71- return false ;
72- }
67+ if (frame.size == 0 ) return false ;
7368
7469 sizedptr pkt = driver->allocate_packet (frame.size );
7570 if (!pkt.ptr ) return false ;
7671
77- void * dst = reinterpret_cast <void *>(pkt.ptr + driver->header_size );
78- memcpy (dst, reinterpret_cast <const void *>(frame.ptr ), frame.size );
72+ void * dst = reinterpret_cast <void *>(pkt.ptr + driver->header_size );
73+ memcpy (dst, reinterpret_cast <const void *>(frame.ptr ), frame.size );
7974
8075 if (!tx_queue.enqueue (pkt)) {
8176 free_frame (pkt);
@@ -103,7 +98,8 @@ void NetworkDispatch::net_task()
10398 driver->send_packet (pkt);
10499 }
105100
106- if (!did_work) sleep (10 );
101+ if (!did_work)
102+ sleep (10 );
107103 }
108104}
109105
@@ -118,17 +114,16 @@ bool NetworkDispatch::dequeue_packet_for(uint16_t pid, sizedptr *out)
118114 sizedptr stored = buf.entries [buf.read_index ];
119115 buf.read_index = (buf.read_index + 1 ) % PACKET_BUFFER_CAPACITY ;
120116
121- void *dst = kalloc (reinterpret_cast <void *>(get_current_heap ()),
122- stored.size , ALIGN_16B ,
123- get_current_privilege (), false );
117+ void *dst = kalloc (reinterpret_cast <void *>(get_current_heap ()),
118+ stored.size , ALIGN_16B ,
119+ get_current_privilege (), false );
124120 if (!dst) return false ;
125121
126- memcpy (dst, reinterpret_cast <void *>(stored.ptr ), stored.size );
127- out->ptr = reinterpret_cast <uintptr_t >(dst);
122+ memcpy (dst, reinterpret_cast <void *>(stored.ptr ), stored.size );
123+ out->ptr = reinterpret_cast <uintptr_t >(dst);
128124 out->size = stored.size ;
129125
130- free (reinterpret_cast <void *>(stored.ptr ), stored.size );
131- // free(reinterpret_cast<void*>(out->ptr), out->size);
126+ free (reinterpret_cast <void *>(stored.ptr ), stored.size );
132127 return true ;
133128}
134129
@@ -138,25 +133,25 @@ static sizedptr make_user_copy(const sizedptr &src)
138133 uintptr_t mem = malloc (src.size );
139134 if (!mem) return out;
140135
141- memcpy (reinterpret_cast <void *>(mem),
142- reinterpret_cast <const void *>(src.ptr ),
136+ memcpy (reinterpret_cast <void *>(mem),
137+ reinterpret_cast <const void *>(src.ptr ),
143138 src.size );
144139
145- out.ptr = mem;
140+ out.ptr = mem;
146141 out.size = src.size ;
147142 return out;
148143}
149144
150145sizedptr NetworkDispatch::make_copy (const sizedptr &in)
151146{
152147 sizedptr out{0 , 0 };
153- void *dst = kalloc (reinterpret_cast <void *>(get_current_heap ()),
154- in.size , ALIGN_16B ,
155- get_current_privilege (), false );
148+ void *dst = kalloc (reinterpret_cast <void *>(get_current_heap ()),
149+ in.size , ALIGN_16B ,
150+ get_current_privilege (), false );
156151 if (!dst) return out;
157152
158- memcpy (dst, reinterpret_cast <const void *>(in.ptr ), in.size );
159- out.ptr = reinterpret_cast <uintptr_t >(dst);
153+ memcpy (dst, reinterpret_cast <const void *>(in.ptr ), in.size );
154+ out.ptr = reinterpret_cast <uintptr_t >(dst);
160155 out.size = in.size ;
161156 return out;
162157}
@@ -167,4 +162,4 @@ void NetworkDispatch::free_frame(const sizedptr &f)
167162}
168163
169164void NetworkDispatch::set_net_pid (uint16_t pid) { g_net_pid = pid; }
170- uint16_t NetworkDispatch::get_net_pid () const { return g_net_pid; }
165+ uint16_t NetworkDispatch::get_net_pid () const { return g_net_pid; }
0 commit comments