Skip to content

Commit 2a512ab

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 2a512ab

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

src/main.c

Lines changed: 2 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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)