|
miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
|
#include <miniOS/drivers/virtio_net.h>#include <miniOS/drivers/pci.h>#include <miniOS/arch/x86_64/port.h>#include <miniOS/mm/pmm.h>#include <miniOS/mm/vmm.h>#include <miniOS/net/netdev.h>#include <miniOS/net/ethernet.h>#include <miniOS/io.h>#include <string.h>
Classes | |
| struct | virtq_desc |
| struct | virtq_avail |
| struct | virtq_used_elem |
| struct | virtq_used |
| struct | virtio_net_hdr |
| struct | vnet_queue_t |
| struct | vnet_state_t |
Macros | |
| #define | VIRTIO_VENDOR_ID 0x1AF4u |
| #define | VIRTIO_NET_DEVICE_ID 0x1000u |
| #define | VREG_DEVICE_FEATURES 0x00u /* 32-bit RO */ |
| #define | VREG_GUEST_FEATURES 0x04u /* 32-bit WO */ |
| #define | VREG_QUEUE_PFN 0x08u /* 32-bit RW: physical frame number of queue */ |
| #define | VREG_QUEUE_SIZE 0x0Cu /* 16-bit RO: number of descriptors */ |
| #define | VREG_QUEUE_SELECT 0x0Eu /* 16-bit WO: select queue index */ |
| #define | VREG_QUEUE_NOTIFY 0x10u /* 16-bit WO: kick queue */ |
| #define | VREG_DEVICE_STATUS 0x12u /* 8-bit RW: device status byte */ |
| #define | VREG_ISR_STATUS 0x13u /* 8-bit RO, clears on read */ |
| #define | VREG_NET_MAC 0x14u /* 6 bytes: MAC address */ |
| #define | VREG_NET_STATUS 0x1Au /* 16-bit RO: link status */ |
| #define | VSTAT_ACKNOWLEDGE 0x01u |
| #define | VSTAT_DRIVER 0x02u |
| #define | VSTAT_DRIVER_OK 0x04u |
| #define | VIRTIO_NET_F_MAC (1u << 5) |
| #define | VIRTQ_DESC_F_NEXT 0x1u /* descriptor chains to .next */ |
| #define | VIRTQ_DESC_F_WRITE 0x2u /* device writes into buffer (RX) */ |
| #define | VNET_QUEUE_SIZE 256u /* must match device-reported QueueSize */ |
| #define | VNET_QUEUE_MASK (VNET_QUEUE_SIZE - 1u) |
| #define | VNET_QUEUE_PAGES 3u /* 3 pages for N=256 layout */ |
| #define | VNET_RX_SLOTS 64u /* pre-filled RX descriptors (≤ VNET_QUEUE_SIZE) */ |
| #define | VIRTIO_NET_HDR_LEN 10u |
| #define | VNET_RXBUF_SIZE (VIRTIO_NET_HDR_LEN + 1518u) |
| #define | VNET_DESC_OFFSET 0u |
| #define | VNET_AVAIL_OFFSET (sizeof(struct virtq_desc) * VNET_QUEUE_SIZE) /* 4096 */ |
| #define | VNET_USED_OFFSET 8192u /* page-aligned: align(4096+520,4096)=8192 */ |
Functions | |
| struct virtq_desc | __attribute__ ((packed)) |
| static void | vnet_out8 (uint16_t reg, uint8_t v) |
| static void | vnet_out16 (uint16_t reg, uint16_t v) |
| static void | vnet_out32 (uint16_t reg, uint32_t v) |
| static uint8_t | vnet_in8 (uint16_t reg) |
| static uint16_t | vnet_in16 (uint16_t reg) |
| static int | vnet_setup_queue (vnet_queue_t *q, uint16_t queue_idx) |
| static void | vnet_rxq_fill (void) |
| static int | vnet_open (netdev_t *dev) |
| static int | vnet_get_mac (netdev_t *dev, uint8_t mac_out[6]) |
| static bool | vnet_get_link (netdev_t *dev) |
| static int | vnet_xmit (netdev_t *dev, const void *frame, size_t frame_len) |
| static int | vnet_poll (netdev_t *dev) |
| int | virtio_net_probe_and_init (void) |
Variables | |
| typedef | __attribute__ |
| uint64_t | addr |
| uint32_t | len |
| uint16_t | flags |
| uint16_t | next |
| uint16_t | idx |
| uint16_t | ring [VNET_QUEUE_SIZE] |
| uint16_t | used_event |
| uint32_t | id |
| uint16_t | avail_event |
| uint8_t | gso_type |
| uint16_t | hdr_len |
| uint16_t | gso_size |
| uint16_t | csum_start |
| uint16_t | csum_offset |
| static uint8_t | g_rx_bufs [VNET_RX_SLOTS][VNET_RXBUF_SIZE] |
| static uint8_t | g_tx_buf [VIRTIO_NET_HDR_LEN+NET_MAX_FRAME_SIZE] |
| static vnet_state_t | g_vnet_state |
| static netdev_t | g_vnet_netdev |
| static const netdev_ops_t | g_vnet_ops |
| #define VIRTIO_NET_DEVICE_ID 0x1000u |
| #define VIRTIO_NET_F_MAC (1u << 5) |
| #define VIRTIO_NET_HDR_LEN 10u |
| #define VIRTIO_VENDOR_ID 0x1AF4u |
| #define VIRTQ_DESC_F_NEXT 0x1u /* descriptor chains to .next */ |
| #define VIRTQ_DESC_F_WRITE 0x2u /* device writes into buffer (RX) */ |
| #define VNET_AVAIL_OFFSET (sizeof(struct virtq_desc) * VNET_QUEUE_SIZE) /* 4096 */ |
| #define VNET_DESC_OFFSET 0u |
| #define VNET_QUEUE_MASK (VNET_QUEUE_SIZE - 1u) |
| #define VNET_QUEUE_PAGES 3u /* 3 pages for N=256 layout */ |
| #define VNET_QUEUE_SIZE 256u /* must match device-reported QueueSize */ |
| #define VNET_RX_SLOTS 64u /* pre-filled RX descriptors (≤ VNET_QUEUE_SIZE) */ |
| #define VNET_RXBUF_SIZE (VIRTIO_NET_HDR_LEN + 1518u) |
| #define VNET_USED_OFFSET 8192u /* page-aligned: align(4096+520,4096)=8192 */ |
| #define VREG_DEVICE_FEATURES 0x00u /* 32-bit RO */ |
| #define VREG_DEVICE_STATUS 0x12u /* 8-bit RW: device status byte */ |
| #define VREG_GUEST_FEATURES 0x04u /* 32-bit WO */ |
| #define VREG_ISR_STATUS 0x13u /* 8-bit RO, clears on read */ |
| #define VREG_NET_MAC 0x14u /* 6 bytes: MAC address */ |
| #define VREG_NET_STATUS 0x1Au /* 16-bit RO: link status */ |
| #define VREG_QUEUE_NOTIFY 0x10u /* 16-bit WO: kick queue */ |
| #define VREG_QUEUE_PFN 0x08u /* 32-bit RW: physical frame number of queue */ |
| #define VREG_QUEUE_SELECT 0x0Eu /* 16-bit WO: select queue index */ |
| #define VREG_QUEUE_SIZE 0x0Cu /* 16-bit RO: number of descriptors */ |
| #define VSTAT_ACKNOWLEDGE 0x01u |
| #define VSTAT_DRIVER 0x02u |
| #define VSTAT_DRIVER_OK 0x04u |
| struct virtq_desc __attribute__ | ( | (packed) | ) |
| int virtio_net_probe_and_init | ( | void | ) |
virtio_net_probe_and_init() - Probe PCI bus for a virtio-net device and initialise it.
Scans pci_devices[] for vendor=0x1AF4, device=0x1000. Negotiates legacy virtio features, allocates virtqueues from physically contiguous PMM frames, and registers a netdev_t via netdev_register().


|
static |





|
static |






|
static |

|
static |


|
static |



| struct madt_lapic_entry __attribute__ |
| uint64_t addr |
| uint16_t avail_event |
| uint16_t csum_offset |
| uint16_t csum_start |
| uint32_t flags |
|
static |
|
static |
|
static |
|
static |
|
static |
| uint16_t gso_size |
| uint8_t gso_type |
| uint16_t hdr_len |
| uint32_t id |
| uint16_t idx |
| uint32_t len |
| uint16_t next |
| struct virtq_used_elem ring |
| uint16_t used_event |