Skip to content

<net/packetring.h>

Single-producer, single-consumer ring of Ethernet frames between a NIC driver and netd.

#include <net/packetring.h>

Constants

Name Value Description
NIC_FRAME_SIZE 1536 Bytes per frame slot.
NIC_RING_DEPTH 16 Slots per ring; one stays empty, so a ring holds NIC_RING_DEPTH - 1 frames.
NIC_MTU 1500 Largest IP packet in a frame.
NIC_RING_BYTES sizeof(NicRing) Size of one ring (24648 bytes).
NIC_RING_STRIDE Ring size rounded up to whole pages (28672 bytes).
NIC_RX_OFFSET 0u Offset of the rx ring in the shared memory.
NIC_TX_OFFSET NIC_RING_STRIDE Offset of the tx ring in the shared memory.
NIC_SHM_BYTES (2u * NIC_RING_STRIDE) Size of the shared memory: 14 pages.

Structures

NicFrame

One frame slot.

Field Type Description
len uint32_t Bytes of data in use.
data uint8_t Frame bytes.

NicRing

A ring of frames in shared memory; empty when head == tail.

Field Type Description
slots NicFrame Frame slots.
head volatile uint32_t Next slot the producer writes.
tail volatile uint32_t Next slot the consumer reads.

Functions

PacketRingPush

int PacketRingPush(NicRing *r, void *src, uint16_t len);

Producer: copies a frame into the ring.

Parameter Direction Description
r inout Ring.
src in Frame bytes.
len in Frame length, at most NIC_FRAME_SIZE.

Returns: 0, or an error code.

Error Condition
ERR_OVERFLOW r or src is NULL, or len is too large.
ERR_BUFFULL The ring is full.

PacketRingPop

int PacketRingPop(NicFrame *dst, NicRing *r);

Consumer: copies the oldest frame out of the ring.

Parameter Direction Description
dst out Where to copy the frame.
r inout Ring.

Returns: 0, or an error code.

Error Condition
ERR_BADARG dst or r is NULL.
ERR_BUFEMPTY The ring is empty.
ERR_OVERFLOW The slot claimed an impossible length; it has been skipped.

PacketRingReserve

NicFrame * PacketRingReserve(NicRing *r);

Producer: returns the next free slot to fill in place, without copying.

Fill in its data and len, then call PacketRingCommit().

Parameter Direction Description
r in Ring.

Returns: The slot, or NULL if the ring is full or r is NULL.

PacketRingCommit

void PacketRingCommit(NicRing *r);

Producer: publishes the slot from PacketRingReserve().

A barrier orders the slot's contents before the new head.

Parameter Direction Description
r inout Ring.

PacketRingPeek

NicFrame * PacketRingPeek(NicRing *r);

Consumer: returns the oldest frame to read in place, without copying.

Call PacketRingConsume() when done with it.

Parameter Direction Description
r in Ring.

Returns: The slot, or NULL if the ring is empty or r is NULL.

PacketRingConsume

void PacketRingConsume(NicRing *r);

Consumer: releases the slot from PacketRingPeek() back to the producer.

Parameter Direction Description
r inout Ring.