Skip to content

Commit 04548f7

Browse files
committed
Implement vm_enable_net API
The vm_enable_net function to facilitate the integration of additional I/O devices in the future. The function initializes the virtio-net device by setting up the tap device using virtio_net_init and subsequently registers the virtio-net device with the PCI bus. This function is invoked during the VM initialization session.
1 parent d87e2dc commit 04548f7

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ OBJS := \
3030
virtio-pci.o \
3131
virtq.o \
3232
virtio-blk.o \
33+
virtio-net.o \
3334
diskimg.o \
3435
main.o
3536

src/main.c

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ int main(int argc, char *argv[])
9393
return throw_err("Failed to load initrd");
9494
if (diskimg_file && vm_load_diskimg(&vm, diskimg_file) < 0)
9595
return throw_err("Failed to load disk image");
96+
if (vm_enable_net(&vm) < 0)
97+
fprintf(stderr, "Failed to enable virtio-net device\n");
9698

9799
if (vm_late_init(&vm) < 0)
98100
return -1;

src/vm.c

+8
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ int vm_load_diskimg(vm_t *v, const char *diskimg_file)
9797
return 0;
9898
}
9999

100+
int vm_enable_net(vm_t *v)
101+
{
102+
if (!virtio_net_init(&v->virtio_net_dev))
103+
return -1;
104+
virtio_net_init_pci(&v->virtio_net_dev, &v->pci, &v->io_bus, &v->mmio_bus);
105+
return 0;
106+
}
107+
100108
void vm_handle_io(vm_t *v, struct kvm_run *run)
101109
{
102110
uint64_t addr = run->io.port;

src/vm.h

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ int vm_load_image(vm_t *v, const char *image_path);
3131
int vm_load_initrd(vm_t *v, const char *initrd_path);
3232
int vm_load_diskimg(vm_t *v, const char *diskimg_file);
3333
int vm_late_init(vm_t *v);
34+
int vm_enable_net(vm_t *v);
3435
int vm_run(vm_t *v);
3536
int vm_irq_line(vm_t *v, int irq, int level);
3637
void *vm_guest_to_host(vm_t *v, uint64_t guest);

0 commit comments

Comments
 (0)