Skip to content

Commit bc437e0

Browse files
committed
dhcp works in qemu with dedicated dhcp server on the subnetwork
1 parent b07a35a commit bc437e0

File tree

21 files changed

+160
-71
lines changed

21 files changed

+160
-71
lines changed

Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM debian:bookworm-slim
2+
3+
RUN apt-get update && apt-get install -y \
4+
build-essential \
5+
nasm \
6+
qemu-system-x86 \
7+
gdb \
8+
clang-format \
9+
tcpdump \
10+
dnsmasq \
11+
bridge-utils \
12+
iproute2 \
13+
net-tools \
14+
procps \
15+
&& rm -rf /var/lib/apt/lists/*
16+
17+
WORKDIR /workspace

Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
C_SOURCES = $(wildcard kernel/*.c drivers/*.c cpu/*.c libc/*.c apps/*.c net/*.c)
2-
HEADERS = $(wildcard kernel/*.h drivers/*.h cpu/*.h libc/*.h apps/*.h net/*.h)
1+
C_SOURCES = $(wildcard kernel/*.c drivers/*.c cpu/*.c libc/*.c apps/*.c net/*.c fs/*.c)
2+
HEADERS = $(wildcard kernel/*.h drivers/*.h cpu/*.h libc/*.h apps/*.h net/*.h fs/*.h)
33

44
BUILD_DIR = _build
55
OBJ = $(patsubst %.c, $(BUILD_DIR)/%.o, $(C_SOURCES)) $(BUILD_DIR)/cpu/interrupt.o $(BUILD_DIR)/cpu/task_switch.o
@@ -10,11 +10,11 @@ LD = ld -m elf_i386
1010
CFLAGSNO = -fno-pie -nostdlib -fno-builtin -nodefaultlibs -nostartfiles -Wno-error=comment -Wno-error=unused-variable -Wno-error=unused-parameter #-Wno-error=pointer-arith
1111
USAN = -fsanitize=undefined -fno-sanitize=shift
1212
CFLAGS = -g -O0 -m32 -fno-pie -ffreestanding -nostdlib -fno-builtin -nodefaultlibs -nostartfiles -Werror -Wpedantic -Wall -Wextra -I$(shell pwd)
13-
NETWORKING = -netdev tap,id=my_tap0,ifname=tap0 -device rtl8139,netdev=my_tap0
13+
NETWORKING = -netdev tap,id=net0,ifname=tap0,script=no,downscript=no -device rtl8139,netdev=net0
1414
QEMU_FLAGS=-no-shutdown -no-reboot
1515
RUN = qemu-system-i386 --enable-kvm $(QEMU_FLAGS) -m 4 -serial stdio -kernel $(BUILD_DIR)/kernel.elf -drive file=disk/fat16.img,format=raw -d int,cpu_reset,guest_errors -D qemu.log -trace kvm* -D kvm_trace.log
1616

17-
$(shell mkdir -p $(BUILD_DIR)/boot $(BUILD_DIR)/kernel $(BUILD_DIR)/drivers $(BUILD_DIR)/cpu $(BUILD_DIR)/libc $(BUILD_DIR)/apps $(BUILD_DIR)/net)
17+
$(shell mkdir -p $(BUILD_DIR)/boot $(BUILD_DIR)/kernel $(BUILD_DIR)/drivers $(BUILD_DIR)/cpu $(BUILD_DIR)/libc $(BUILD_DIR)/apps $(BUILD_DIR)/net $(BUILD_DIR)/fs)
1818

1919
all: format bits $(BUILD_DIR)/kernel.elf disk/fat16.img
2020

@@ -34,6 +34,8 @@ net: all
3434
$(RUN) $(NETWORKING)
3535
run: all
3636
$(RUN)
37+
vnc : all
38+
$(RUN) $(NETWORKING) -vnc :1
3739

3840
#################
3941
disk/fat16.img:

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
```bash
2+
docker build -t kernel-dev .
3+
4+
sudo docker run -it --privileged --cap-add=NET_ADMIN \
5+
-v $(pwd):/workspace \
6+
-p 5901:5901 \
7+
kernel-dev bash
8+
9+
vncviewer localhost:5901$
10+
```

TODO

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,12 @@ TODO:
7272
[ ] thread the ehternet packets
7373
[ ] add some log groups for debugging
7474
[ ] write a readme and collect main features and quirks
75-
[ ] ip packets are inefficient, it works but...
75+
[ ] ip packets are inefficient, it works but...
76+
[ ] vfs based on 9p
77+
[ ] sockets, for udp
78+
79+
80+
`
81+
brctl show
82+
sudo brctl addif virbr0 tap0
83+
`

apps/vga_demo_terminal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

3-
#include "drivers/fat16.h"
43
#include "drivers/keyboard.h"
54
#include "drivers/screen.h"
5+
#include "fs/fat16.h"
66
#include "libc/bdf.h"
77

88
typedef struct {

dhcp.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ip tuntap add dev tap0 mode tap
2+
ip addr add 192.168.100.1/24 dev tap0
3+
ip link set tap0 up
4+
5+
dnsmasq --interface=tap0 --dhcp-range=192.168.100.10,192.168.100.50,12h --log-dhcp --no-daemon &
File renamed without changes.
File renamed without changes.

kernel/kernel.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,27 @@ void kernel_main(void) {
8484

8585
uint8_t mac_addr[] = {0};
8686
get_mac_addr(mac_addr);
87-
/*
88-
*/
89-
90-
/*
91-
ethernet_send_packet(mac_addr, (void *)str, strlen(str), 0x0021);
92-
ip_send_packet(ip_addr, (void *)str, strlen(str));
93-
*/
9487

9588
// this does not work i cant seem to set up the network on wsl2 qemu
9689
// the packet looks okay, but i cant reach the dhcp server
97-
/*
90+
91+
// maybe put this in a state machine?
9892
dhcp_discover();
99-
while (gethostaddr((char *)mac_addr) == 0){};
100-
*/
93+
extern int is_ip_allocated;
94+
extern int is_ip_offered;
95+
96+
while (!is_ip_offered) {
97+
}
98+
99+
while (!is_ip_allocated) {
100+
sleep(1000);
101+
extern uint32_t prev_requested_ip;
102+
dhcp_request((uint8_t *)&prev_requested_ip);
103+
}
104+
extern uint8_t my_ip[4];
105+
serial_debug("we got a dynamic ip! %d.%d.%d.%d", my_ip[0], my_ip[1],
106+
my_ip[2], my_ip[3]);
101107

102-
// udp_send_packet(ip_addr, 1234, 1153, str, strlen(str));
103108
while (1) {
104109
;
105110
}

libc/bdf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
2-
#include "drivers/fat16.h"
2+
#include "fs/fat16.h"
33

44
#define MAX_GLYPHS 256
55
#define GLYPH_HEIGHT 16

0 commit comments

Comments
 (0)