Skip to content

Commit 53ddd16

Browse files
modularise network drivers for e1000 and rtl8139
1 parent 8cc26be commit 53ddd16

16 files changed

Lines changed: 83 additions & 47 deletions

File tree

include/kernel.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@
5959
#include "taskswitch.h"
6060
#include "acpi.h"
6161
#include "net.h"
62-
#include "rtl8139.h"
63-
#include "e1000.h"
6462
#include "arp.h"
6563
#include "ip.h"
6664
#include "ethernet.h"

src/e1000.c renamed to modules/e1000/e1000.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#include <kernel.h>
2+
#include "e1000.h"
23
#include <mmio.h>
34

4-
uint8_t bar_type = 0; // Type of BAR0
5-
uint16_t io_base = 0; // IO Base Address
6-
uint64_t mem_base = 0; // MMIO Base Address
7-
bool eerprom_exists = false; // A flag indicating if eeprom exists
8-
uint8_t mac[6] = {0}; // A buffer for storing the mack address
9-
e1000_rx_desc_t *rx_descs[E1000_NUM_RX_DESC] = {NULL}; // Receive Descriptor Buffers
10-
e1000_tx_desc_t *tx_descs[E1000_NUM_TX_DESC] = {NULL}; // Transmit Descriptor Buffers
11-
uint16_t rx_cur = 0; // Current Receive Descriptor Buffer
12-
uint16_t tx_cur = 0; // Current Transmit Descriptor Buffer
5+
static uint8_t bar_type = 0; // Type of BAR0
6+
static uint16_t io_base = 0; // IO Base Address
7+
static uint64_t mem_base = 0; // MMIO Base Address
8+
static bool eerprom_exists = false; // A flag indicating if eeprom exists
9+
static uint8_t mac[6] = {0}; // A buffer for storing the mack address
10+
static e1000_rx_desc_t *rx_descs[E1000_NUM_RX_DESC]; // Receive Descriptor Buffers
11+
static e1000_tx_desc_t *tx_descs[E1000_NUM_TX_DESC]; // Transmit Descriptor Buffers
12+
static uint16_t rx_cur = 0; // Current Receive Descriptor Buffer
13+
static uint16_t tx_cur = 0; // Current Transmit Descriptor Buffer
1314

1415
static void *tx_buffers[E1000_NUM_TX_DESC];
1516

@@ -181,6 +182,7 @@ void e1000_handle_receive() {
181182
}
182183
netdev_t* dev = get_active_network_device();
183184
if (!dev || dev->deviceid != ((INTEL_VEND << 16) | e1000_device_id)) {
185+
dprintf("invalid dev in handle receive\n");
184186
return;
185187
}
186188

@@ -213,6 +215,7 @@ bool e1000_send_packet(void *p_data, uint16_t p_len) {
213215
}
214216

215217
if (!tx_descs[tx_cur]) {
218+
dprintf("e1000: Bad send buffer\n");
216219
return false;
217220
}
218221

@@ -258,7 +261,6 @@ void e1000_up() {
258261
*/
259262
void e1000_handler([[maybe_unused]] uint8_t isr, [[maybe_unused]] uint64_t error, [[maybe_unused]] uint64_t irq, void* opaque) {
260263
uint32_t status = e1000_read_command(REG_ICR);
261-
262264
if (status & ICR_RXT0) {
263265
e1000_handle_receive();
264266
}
@@ -420,4 +422,16 @@ void init_e1000() {
420422
eerprom_exists = false;
421423

422424
e1000_start(&pci_device);
425+
426+
dhcp_discover();
427+
}
428+
429+
bool EXPORTED MOD_INIT_SYM(KMOD_ABI)(void) {
430+
dprintf("e1000: loaded\n");
431+
init_e1000();
432+
return true;
433+
}
434+
435+
void EXPORTED MOD_EXIT_SYM(KMOD_ABI)(void) {
436+
dprintf("e1000: unloaded\n");
423437
}
File renamed without changes.

src/rtl8139.c renamed to modules/rtl8139/rtl8139.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <kernel.h>
2+
#include "rtl8139.h"
23

34
#define TX_BUF_COUNT 4
45
#define TX_BUF_SIZE 2048 // More than enough for Ethernet frame
@@ -338,4 +339,17 @@ void init_rtl8139() {
338339
register_network_device(net);
339340

340341
interrupts_on();
342+
343+
dhcp_discover();
344+
}
345+
346+
347+
bool EXPORTED MOD_INIT_SYM(KMOD_ABI)(void) {
348+
dprintf("rtl8139: loaded\n");
349+
init_rtl8139();
350+
return true;
351+
}
352+
353+
void EXPORTED MOD_EXIT_SYM(KMOD_ABI)(void) {
354+
dprintf("rtl8139: unloaded\n");
341355
}

src/basic/sockets.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ void sockread_statement(struct basic_ctx* ctx)
5353

5454
process_t* proc = proc_cur(logical_cpu_id());
5555

56-
int rv = recv((int)fd, input, MAX_STRINGLEN, false, 10);
56+
int rv = recv((int)fd, input, MAX_STRINGLEN, false, 100);
57+
58+
dprintf("sockread recv=%d\n", rv);
5759

5860
if (rv == 0) {
5961
// Not ready yet, yield and retry later

src/ethernet.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ int ethernet_send_packet(uint8_t* dst_mac_addr, uint8_t* data, uint32_t len, uin
1313
netdev_t* dev = get_active_network_device();
1414
uint64_t flags;
1515
if (!dev) {
16+
dprintf("no active network device, not sending packet\n");
1617
return 0;
1718
}
1819
lock_spinlock_irq(&ethernet_lock, &flags);
@@ -32,7 +33,9 @@ int ethernet_send_packet(uint8_t* dst_mac_addr, uint8_t* data, uint32_t len, uin
3233
memcpy(frame->dst_mac_addr, dst_mac_addr, 6);
3334
memcpy(frame_data, data, len);
3435
frame->type = htons(protocol);
35-
dprintf("ethernet_send_packet frame=%08lx\n", (uint64_t)frame);
36+
dprintf("ethernet_send_packet dest=%02x:%02x:%02x:%02x:%02x:%02x src=%02x:%02x:%02x:%02x:%02x:%02x len=%u\n",
37+
dst_mac_addr[0], dst_mac_addr[1], dst_mac_addr[2], dst_mac_addr[3], dst_mac_addr[4], dst_mac_addr[5],
38+
src_mac_addr[0], src_mac_addr[1], src_mac_addr[2], src_mac_addr[3], src_mac_addr[4], src_mac_addr[5], len);
3639
dev->send_packet(frame, sizeof(ethernet_frame_t) + len);
3740
unlock_spinlock_irq(&ethernet_lock, flags);
3841
return len;

src/init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ init_func_t init_funcs[] = {
1010
init_acpi, init_idt, boot_aps, init_pci, init_realtime_clock,
1111
init_devicenames, init_keyboard, init_ide, init_ahci,
1212
init_filesystem, init_devfs, init_iso9660, init_fat32,
13-
init_rfs, init_modules, init_rtl8139, init_e1000,
13+
init_rfs, init_modules, network_up,
1414
NULL,
1515
};
1616

@@ -19,7 +19,7 @@ char* init_funcs_names[] = {
1919
"idt", "cpus", "pci", "clock",
2020
"devicenames", "keyboard", "ide", "ahci",
2121
"filesystem", "devfs", "iso9660", "fat32",
22-
"rfs", "modules", "rtl8139", "e1000",
22+
"rfs", "modules", "network",
2323
NULL,
2424
};
2525

src/kernel.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ void kmain()
77
{
88
init();
99

10-
if (get_active_network_device()) {
11-
kprintf("Bringing up network...");
12-
network_up();
13-
}
14-
1510
bool live_cd = false, root_fs_mounted = false;
1611
if (rr_kfile_req.response) {
1712
struct limine_kernel_file_response* kernel_info = rr_kfile_req.response;
@@ -28,7 +23,8 @@ void kmain()
2823
preboot_fail("Failed to mount boot drive to VFS!");
2924
}
3025

31-
//load_module("test");
26+
// TODO: Don't hard code this, give BASIC facility to load it via /programs/init
27+
load_module("e1000");
3228

3329
init_process();
3430
}

src/net/arp.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include <kernel.h>
22

3-
arp_table_entry_t arp_table[512];
3+
static arp_table_entry_t arp_table[512] = {};
44
static uint8_t zero_hardware_addr[6] = {0, 0, 0, 0, 0, 0};
5-
int arp_table_size = 0;
6-
int arp_table_curr = 0;
5+
static int arp_table_size = 0;
6+
static int arp_table_curr = 0;
77

88
arp_table_entry_t* get_arp_entry(size_t index) {
99
if (index > 512) {
@@ -30,6 +30,7 @@ void arp_handle_packet(arp_packet_t* arp_packet, [[maybe_unused]] int len) {
3030
memcpy(dst_hardware_addr, arp_packet->src_hardware_addr, 6);
3131
memcpy(dst_protocol_addr, arp_packet->src_protocol_addr, 4);
3232
if (ntohs(arp_packet->opcode) == ARP_REQUEST) {
33+
dprintf("arp request\n");
3334
unsigned char addr[4];
3435
uint32_t my_ip = 0;
3536
if (gethostaddr(addr)) {
@@ -39,6 +40,7 @@ void arp_handle_packet(arp_packet_t* arp_packet, [[maybe_unused]] int len) {
3940

4041
netdev_t* dev = get_active_network_device();
4142
if (!dev) {
43+
dprintf("No active net dev (arp handle packet)\n");
4244
return;
4345
}
4446

@@ -60,12 +62,16 @@ void arp_handle_packet(arp_packet_t* arp_packet, [[maybe_unused]] int len) {
6062
arp_packet->protocol = htons(ETHERNET_TYPE_IP);
6163

6264
ethernet_send_packet(dst_hardware_addr, (uint8_t*)arp_packet, sizeof(arp_packet_t), ETHERNET_TYPE_ARP);
65+
} else {
66+
dprintf("Invalid addr %08x\n", my_ip);
6367
}
6468
} else if(ntohs(arp_packet->opcode) == ARP_REPLY) {
69+
dprintf("ARP_REPLY from: %08x hw %02x:%02x:%02x:%02x:%02x:%02x\n", *(uint32_t*)&dst_protocol_addr, dst_hardware_addr[0], dst_hardware_addr[1], dst_hardware_addr[2], dst_hardware_addr[3], dst_hardware_addr[4], dst_hardware_addr[5]);
6570
}
6671

6772
uint8_t dummy[6];
6873
if (!arp_lookup(dummy, dst_protocol_addr)) {
74+
dprintf("not in arp table, storing at %d\n", arp_table_size);
6975
memcpy(&arp_table[arp_table_curr].ip_addr, dst_protocol_addr, 4);
7076
memcpy(&arp_table[arp_table_curr++].mac_addr, dst_hardware_addr, 6);
7177
if(arp_table_size < 512) {
@@ -81,6 +87,7 @@ void arp_handle_packet(arp_packet_t* arp_packet, [[maybe_unused]] int len) {
8187
void arp_send_packet(uint8_t* dst_hardware_addr, uint8_t* dst_protocol_addr) {
8288
netdev_t* dev = get_active_network_device();
8389
if (!dev) {
90+
dprintf("arp send packet: no active net dev\n");
8491
return;
8592
}
8693
static arp_packet_t * arp_packet = NULL;
@@ -103,6 +110,7 @@ void arp_send_packet(uint8_t* dst_hardware_addr, uint8_t* dst_protocol_addr) {
103110
arp_packet->hardware_type = htons(HARDWARE_TYPE_ETHERNET);
104111
arp_packet->protocol = htons(ETHERNET_TYPE_IP);
105112

113+
dprintf("arp send (broadcast) for %08x\n", *(uint32_t*)&dst_protocol_addr);
106114
ethernet_send_packet(broadcast_mac_address, (uint8_t*)arp_packet, sizeof(arp_packet_t), ETHERNET_TYPE_ARP);
107115
}
108116

0 commit comments

Comments
 (0)