Skip to content

Commit

Permalink
Rewrite vnet_preprocess as an inline function
Browse files Browse the repository at this point in the history
  • Loading branch information
jserv committed Jul 27, 2023
1 parent 64d74c0 commit 3a8055b
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions virtio-net.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
#define VNET_QUEUE_NUM_MAX 1024
#define VNET_QUEUE (vnet->queues[vnet->QueueSel])

#define VNET_PREPROCESS_ADDR(addr) \
((addr) < RAM_SIZE && !((addr) &0b11) ? ((addr) >> 2) \
: (virtio_net_set_fail(vnet), 0))

#define PRIV(x) ((struct virtio_net_config *) x->priv)

enum { VNET_QUEUE_RX = 0, VNET_QUEUE_TX = 1 };
Expand All @@ -51,6 +47,14 @@ static void virtio_net_set_fail(virtio_net_state_t *vnet)
vnet->InterruptStatus |= VIRTIO_INT__CONF_CHANGE;
}

static inline uint32_t vnet_preprocess(virtio_net_state_t *vnet, uint32_t addr)
{
if ((addr >= RAM_SIZE) || (addr & 0b11))
return virtio_net_set_fail(vnet), 0;

return addr >> 2;
}

static void virtio_net_update_status(virtio_net_state_t *vnet, uint32_t status)
{
vnet->Status |= status;
Expand Down Expand Up @@ -327,21 +331,21 @@ static bool virtio_net_reg_write(virtio_net_state_t *vnet,
1; /* set VIRTQ_AVAIL_F_NO_INTERRUPT */
return true;
case _(QueueDescLow):
VNET_QUEUE.QueueDesc = VNET_PREPROCESS_ADDR(value);
VNET_QUEUE.QueueDesc = vnet_preprocess(vnet, value);
return true;
case _(QueueDescHigh):
if (value)
virtio_net_set_fail(vnet);
return true;
case _(QueueDriverLow):
VNET_QUEUE.QueueAvail = VNET_PREPROCESS_ADDR(value);
VNET_QUEUE.QueueAvail = vnet_preprocess(vnet, value);
return true;
case _(QueueDriverHigh):
if (value)
virtio_net_set_fail(vnet);
return true;
case _(QueueDeviceLow):
VNET_QUEUE.QueueUsed = VNET_PREPROCESS_ADDR(value);
VNET_QUEUE.QueueUsed = vnet_preprocess(vnet, value);
return true;
case _(QueueDeviceHigh):
if (value)
Expand Down

0 comments on commit 3a8055b

Please sign in to comment.