miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
Loading...
Searching...
No Matches
virtio_net.c File Reference
#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>
Include dependency graph for virtio_net.c:

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

Macro Definition Documentation

◆ VIRTIO_NET_DEVICE_ID

#define VIRTIO_NET_DEVICE_ID   0x1000u

◆ VIRTIO_NET_F_MAC

#define VIRTIO_NET_F_MAC   (1u << 5)

◆ VIRTIO_NET_HDR_LEN

#define VIRTIO_NET_HDR_LEN   10u

◆ VIRTIO_VENDOR_ID

#define VIRTIO_VENDOR_ID   0x1AF4u

◆ VIRTQ_DESC_F_NEXT

#define VIRTQ_DESC_F_NEXT   0x1u /* descriptor chains to .next */

◆ VIRTQ_DESC_F_WRITE

#define VIRTQ_DESC_F_WRITE   0x2u /* device writes into buffer (RX) */

◆ VNET_AVAIL_OFFSET

#define VNET_AVAIL_OFFSET   (sizeof(struct virtq_desc) * VNET_QUEUE_SIZE) /* 4096 */

◆ VNET_DESC_OFFSET

#define VNET_DESC_OFFSET   0u

◆ VNET_QUEUE_MASK

#define VNET_QUEUE_MASK   (VNET_QUEUE_SIZE - 1u)

◆ VNET_QUEUE_PAGES

#define VNET_QUEUE_PAGES   3u /* 3 pages for N=256 layout */

◆ VNET_QUEUE_SIZE

#define VNET_QUEUE_SIZE   256u /* must match device-reported QueueSize */

◆ VNET_RX_SLOTS

#define VNET_RX_SLOTS   64u /* pre-filled RX descriptors (≤ VNET_QUEUE_SIZE) */

◆ VNET_RXBUF_SIZE

#define VNET_RXBUF_SIZE   (VIRTIO_NET_HDR_LEN + 1518u)

◆ VNET_USED_OFFSET

#define VNET_USED_OFFSET   8192u /* page-aligned: align(4096+520,4096)=8192 */

◆ VREG_DEVICE_FEATURES

#define VREG_DEVICE_FEATURES   0x00u /* 32-bit RO */

◆ VREG_DEVICE_STATUS

#define VREG_DEVICE_STATUS   0x12u /* 8-bit RW: device status byte */

◆ VREG_GUEST_FEATURES

#define VREG_GUEST_FEATURES   0x04u /* 32-bit WO */

◆ VREG_ISR_STATUS

#define VREG_ISR_STATUS   0x13u /* 8-bit RO, clears on read */

◆ VREG_NET_MAC

#define VREG_NET_MAC   0x14u /* 6 bytes: MAC address */

◆ VREG_NET_STATUS

#define VREG_NET_STATUS   0x1Au /* 16-bit RO: link status */

◆ VREG_QUEUE_NOTIFY

#define VREG_QUEUE_NOTIFY   0x10u /* 16-bit WO: kick queue */

◆ VREG_QUEUE_PFN

#define VREG_QUEUE_PFN   0x08u /* 32-bit RW: physical frame number of queue */

◆ VREG_QUEUE_SELECT

#define VREG_QUEUE_SELECT   0x0Eu /* 16-bit WO: select queue index */

◆ VREG_QUEUE_SIZE

#define VREG_QUEUE_SIZE   0x0Cu /* 16-bit RO: number of descriptors */

◆ VSTAT_ACKNOWLEDGE

#define VSTAT_ACKNOWLEDGE   0x01u

◆ VSTAT_DRIVER

#define VSTAT_DRIVER   0x02u

◆ VSTAT_DRIVER_OK

#define VSTAT_DRIVER_OK   0x04u

Function Documentation

◆ __attribute__()

struct virtq_desc __attribute__ ( (packed) )

◆ virtio_net_probe_and_init()

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().

Returns
: 0 on success, -1 if no device found or initialisation failed.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vnet_get_link()

bool vnet_get_link ( netdev_t * dev)
static

◆ vnet_get_mac()

int vnet_get_mac ( netdev_t * dev,
uint8_t mac_out[6] )
static
Here is the call graph for this function:

◆ vnet_in16()

uint16_t vnet_in16 ( uint16_t reg)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vnet_in8()

uint8_t vnet_in8 ( uint16_t reg)
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vnet_open()

int vnet_open ( netdev_t * dev)
static

◆ vnet_out16()

void vnet_out16 ( uint16_t reg,
uint16_t v )
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vnet_out32()

void vnet_out32 ( uint16_t reg,
uint32_t v )
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vnet_out8()

void vnet_out8 ( uint16_t reg,
uint8_t v )
inlinestatic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vnet_poll()

int vnet_poll ( netdev_t * dev)
static
Here is the call graph for this function:

◆ vnet_rxq_fill()

void vnet_rxq_fill ( void )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vnet_setup_queue()

int vnet_setup_queue ( vnet_queue_t * q,
uint16_t queue_idx )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vnet_xmit()

int vnet_xmit ( netdev_t * dev,
const void * frame,
size_t frame_len )
static
Here is the call graph for this function:

Variable Documentation

◆ __attribute__

struct madt_lapic_entry __attribute__

◆ addr

uint64_t addr

◆ avail_event

uint16_t avail_event

◆ csum_offset

uint16_t csum_offset

◆ csum_start

uint16_t csum_start

◆ flags

uint32_t flags

◆ g_rx_bufs

◆ g_tx_buf

◆ g_vnet_netdev

netdev_t g_vnet_netdev
static

◆ g_vnet_ops

const netdev_ops_t g_vnet_ops
static
Initial value:
= {
.open = vnet_open,
.poll = vnet_poll,
.xmit = vnet_xmit,
.get_mac = vnet_get_mac,
.get_link = vnet_get_link,
}
static int vnet_poll(netdev_t *dev)
Definition virtio_net.c:271
static int vnet_xmit(netdev_t *dev, const void *frame, size_t frame_len)
Definition virtio_net.c:229
static bool vnet_get_link(netdev_t *dev)
Definition virtio_net.c:223
static int vnet_open(netdev_t *dev)
Definition virtio_net.c:210
static int vnet_get_mac(netdev_t *dev, uint8_t mac_out[6])
Definition virtio_net.c:216

◆ g_vnet_state

vnet_state_t g_vnet_state
static

◆ gso_size

uint16_t gso_size

◆ gso_type

uint8_t gso_type

◆ hdr_len

uint16_t hdr_len

◆ id

◆ idx

uint16_t idx

◆ len

uint32_t len

◆ next

uint16_t next

◆ ring

struct virtq_used_elem ring

◆ used_event

uint16_t used_event