Skip to content

Commit

Permalink
Implement vm_enable_net API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jimmylu890303 committed Jul 21, 2024
1 parent 580014e commit d7f9a3c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ int main(int argc, char *argv[])
return throw_err("Failed to load initrd");
if (diskimg_file && vm_load_diskimg(&vm, diskimg_file) < 0)
return throw_err("Failed to load disk image");
if (vm_enable_net(&vm) < 0)
return throw_err("Failed to Enable Virtio-Net Device");

if (vm_late_init(&vm) < 0)
return -1;
Expand Down
8 changes: 8 additions & 0 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ int vm_load_diskimg(vm_t *v, const char *diskimg_file)
return 0;
}

int vm_enable_net(vm_t *v)
{
if (!virtio_net_init(&v->virtio_net_dev))
return -1;
virtio_net_init_pci(&v->virtio_net_dev, &v->pci, &v->io_bus, &v->mmio_bus);
return 0;
}

void vm_handle_io(vm_t *v, struct kvm_run *run)
{
uint64_t addr = run->io.port;
Expand Down
1 change: 1 addition & 0 deletions src/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ int vm_load_image(vm_t *v, const char *image_path);
int vm_load_initrd(vm_t *v, const char *initrd_path);
int vm_load_diskimg(vm_t *v, const char *diskimg_file);
int vm_late_init(vm_t *v);
int vm_enable_net(vm_t *v);
int vm_run(vm_t *v);
int vm_irq_line(vm_t *v, int irq, int level);
void *vm_guest_to_host(vm_t *v, uint64_t guest);
Expand Down

0 comments on commit d7f9a3c

Please sign in to comment.