Skip to content

Commit b86e8d9

Browse files
committed
Merge remote-tracking branch 'upstream/main' into refactored_net
2 parents 2e2191a + 0469a3d commit b86e8d9

7 files changed

Lines changed: 76 additions & 76 deletions

File tree

kernel/audio/virtio_audio_pci.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ bool VirtioAudioDriver::init(){
9696
kprintf("[VIRTIO_AUDIO] Failed to setup interrupts");
9797
return false;
9898
case 1:
99-
kprintf("[VIRTIO_AUDIO] Interrupts setup with MSI-X %i,%i",AUDIO_IRQ);
99+
kprintf("[VIRTIO_AUDIO] Interrupts setup with MSI-X %i",AUDIO_IRQ);
100100
break;
101101
default:
102-
kprintf("[VIRTIO_AUDIO] Interrupts setup with MSI %i,%i",AUDIO_IRQ);
102+
kprintf("[VIRTIO_AUDIO] Interrupts setup with MSI %i",AUDIO_IRQ);
103103
break;
104104
}
105105
pci_enable_device(addr);
@@ -111,17 +111,17 @@ bool VirtioAudioDriver::init(){
111111

112112
select_queue(&audio_dev, EVENT_QUEUE);
113113

114+
audio_dev.common_cfg->queue_msix_vector = 0;
115+
if (audio_dev.common_cfg->queue_msix_vector != 0){
116+
kprintf("[VIRTIO_AUDIO error] failed to setup interrupts for event queue");
117+
return false;
118+
}
114119
for (uint16_t i = 0; i < 128; i++){
115120
void* buf = kalloc(audio_dev.memory_page, sizeof(virtio_snd_event), ALIGN_64B, true, true);
116121
virtio_add_buffer(&audio_dev, i, (uintptr_t)buf, sizeof(virtio_snd_event));
117122
}
118123

119124
select_queue(&audio_dev, CONTROL_QUEUE);
120-
audio_dev.common_cfg->queue_msix_vector = 0;
121-
if (audio_dev.common_cfg->queue_msix_vector != 0){
122-
kprintf("[VIRTIO_AUDIO error] failed to setup interrupts for event queue");
123-
return false;
124-
}
125125

126126
return get_config();
127127
}
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
#pragma once
22

33
#include "types.h"
4-
#include "std/string.h"
5-
#include "ui/graphic_types.h"
6-
#include "net/network_types.h"
4+
#include "net/network_types.h"
75

86
class NetDriver {
97
public:
10-
NetDriver(){}
8+
NetDriver() = default;
119
virtual bool init() = 0;
1210

1311
virtual sizedptr allocate_packet(size_t size) = 0;
14-
15-
virtual sizedptr handle_receive_packet() = 0;
16-
12+
virtual sizedptr handle_receive_packet(void* buffer) = 0;
1713
virtual void handle_sent_packet() = 0;
18-
1914
virtual void enable_verbose() = 0;
20-
2115
virtual void send_packet(sizedptr packet) = 0;
22-
2316
virtual void get_mac(net_l2l3_endpoint *context) = 0;
2417

2518
virtual ~NetDriver() = default;
2619

2720
uint16_t header_size;
28-
};
21+
};

kernel/networking/drivers/virtio_net_pci/virtio_net_pci.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
#define RECEIVE_QUEUE 0
1010
#define TRANSMIT_QUEUE 1
11-
//TODO: review this number
12-
#define MAX_size 0x1000
1311

1412
#define kprintfv(fmt, ...) \
1513
({ \
@@ -84,8 +82,8 @@ bool VirtioNetDriver::init(){
8482
select_queue(&vnp_net_dev, RECEIVE_QUEUE);
8583

8684
for (uint16_t i = 0; i < 128; i++){
87-
void* buf = kalloc(vnp_net_dev.memory_page, MAX_size, ALIGN_64B, true, true);
88-
virtio_add_buffer(&vnp_net_dev, i, (uintptr_t)buf, MAX_size);
85+
void* buf = kalloc(vnp_net_dev.memory_page, MAX_PACKET_SIZE, ALIGN_64B, true, true);
86+
virtio_add_buffer(&vnp_net_dev, i, (uintptr_t)buf, MAX_PACKET_SIZE);
8987
}
9088

9189
vnp_net_dev.common_cfg->queue_msix_vector = 0;
@@ -123,7 +121,7 @@ sizedptr VirtioNetDriver::allocate_packet(size_t size){
123121
return (sizedptr){(uintptr_t)kalloc(vnp_net_dev.memory_page, size + header_size, ALIGN_64B, true, true),size + header_size};
124122
}
125123

126-
sizedptr VirtioNetDriver::handle_receive_packet(){
124+
sizedptr VirtioNetDriver::handle_receive_packet(void* buffer){
127125
select_queue(&vnp_net_dev, RECEIVE_QUEUE);
128126
struct virtq_used* used = (struct virtq_used*)(uintptr_t)vnp_net_dev.common_cfg->queue_device;
129127
struct virtq_desc* desc = (struct virtq_desc*)(uintptr_t)vnp_net_dev.common_cfg->queue_desc;
@@ -141,11 +139,15 @@ sizedptr VirtioNetDriver::handle_receive_packet(){
141139
uintptr_t packet = desc[desc_index].addr;
142140
packet += sizeof(virtio_net_hdr_t);
143141

142+
memcpy(buffer, (void*)packet, len);
143+
144144
avail->ring[avail->idx % 128] = desc_index;
145145
avail->idx++;
146146

147147
*(volatile uint16_t*)(uintptr_t)(vnp_net_dev.notify_cfg + vnp_net_dev.notify_off_multiplier * RECEIVE_QUEUE) = 0;
148148

149+
kfree((void*)desc[desc_index].addr, MAX_PACKET_SIZE);
150+
149151
return (sizedptr){packet,len};
150152
}
151153

kernel/networking/drivers/virtio_net_pci/virtio_net_pci.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class VirtioNetDriver : public NetDriver {
1414

1515
sizedptr allocate_packet(size_t size) override;
1616

17-
sizedptr handle_receive_packet() override;
18-
17+
sizedptr handle_receive_packet(void* buffer) override;
18+
1919
void handle_sent_packet() override;
2020

2121
void enable_verbose() override;

kernel/networking/network.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ extern "C" {
88
#include "net/network_types.h"
99

1010
#define NET_IRQ 32
11+
//TODO: consider using the system MTU here
12+
#define MAX_PACKET_SIZE 0x1000
1113

1214
void network_net_set_pid(uint16_t pid);
1315
uint16_t network_net_get_pid(void);
@@ -17,7 +19,6 @@ void network_handle_download_interrupt(void);
1719
void network_handle_upload_interrupt(void);
1820
void network_net_task_entry(void);
1921

20-
2122
int net_tx_frame(uintptr_t frame_ptr, uint32_t frame_len);
2223
int net_rx_frame(sizedptr *out_frame);
2324

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
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
1910
extern void sleep(uint64_t ms);
2011
extern 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

2314
static 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()
4437
void 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

6259
void NetworkDispatch::handle_upload_interrupt()
6360
{
64-
if (driver) driver->handle_sent_packet();
61+
if (driver)
62+
driver->handle_sent_packet();
6563
}
6664

6765
bool 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

150145
sizedptr 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

169164
void 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; }

shared/std/memfunctions.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,17 @@ void* memset(void* dest, uint32_t val, size_t count) {
3333
}
3434

3535
void* memcpy(void *dest, const void *src, uint64_t count) {
36-
uint64_t *d64 = (uint64_t *)dest;
37-
const uint64_t *s64 = (const uint64_t *)src;
36+
uint8_t *d8 = (uint8_t *)dest;
37+
const uint8_t *s8 = (const uint8_t *)src;
38+
39+
while (count > 0 &&
40+
(((uintptr_t)d8 & 7) != 0 || ((uintptr_t)s8 & 7) != 0)) {
41+
*d8++ = *s8++;
42+
count--;
43+
}
44+
45+
uint64_t *d64 = (uint64_t *)d8;
46+
const uint64_t *s64 = (const uint64_t *)s8;
3847

3948
uint64_t blocks = count / 32;
4049
for (uint64_t i = 0; i < blocks; i++) {
@@ -49,8 +58,8 @@ void* memcpy(void *dest, const void *src, uint64_t count) {
4958
uint64_t remaining = (count % 32) / 8;
5059
for (uint64_t i = 0; i < remaining; i++) d64[i] = s64[i];
5160

52-
uint8_t *d8 = (uint8_t *)(d64 + remaining);
53-
const uint8_t *s8 = (const uint8_t *)(s64 + remaining);
61+
d8 = (uint8_t *)(d64 + remaining);
62+
s8 = (const uint8_t *)(s64 + remaining);
5463
for (uint64_t i = 0; i < count % 8; i++) d8[i] = s8[i];
5564

5665
return dest;

0 commit comments

Comments
 (0)