PKTQUEUE(9)
okay so bestie, welcome to pktqueue(9) β the Kernel Developerβs Manual page for lockless network protocol input queues with integrated ISR scheduling. This one is lowkey iconic, and weβre gonna get through every detail together. π
β¨ NAME β¨
okay so the whole squad is pktqueue, pktq_create, pktq_destroy, pktq_enqueue, pktq_dequeue, pktq_barrier, pktq_ifdetach, pktq_flush, pktq_set_maxlen, pktq_rps_hash, pktq_sysctl_setup, and sysctl_pktq_rps_hash_handler β together theyβre the lockless network protocol input queues with integrated ISR scheduling. Obsessed. π
πΈ SYNOPSIS πΈ
okay bestie, hereβs the exact include and signatures, no cap:
#include <net/pktqueue.h>
pktqueue_t *
pktq_create(size_t maxlen, void (*intrh)(void *), void *arg);
void
pktq_destroy(pktqueue_t *pq);
bool
pktq_enqueue(pktqueue_t *pq, struct mbuf *m, u_int hash);
struct mbuf *
pktq_dequeue(pktqueue_t *pq);
void
pktq_barrier(pktqueue_t *pq);
void
pktq_ifdetach(void);
void
pktq_flush(pktqueue_t *pq);
int
pktq_set_maxlen(pktqueue_t *pq, size_t maxlen);
uint32_t
pktq_rps_hash(const pktq_rps_hash_func_t *funcp, const struct mbuf *m);
int
pktq_sysctl_setup(pktqueue_t *pq, struct sysctllog **clog,
const struct sysctlnode *parent_node, int qid);
int
sysctl_pktq_rps_hash_handler(SYSCTLFN_ARGS);
π¦ DESCRIPTION π¦
The pktqueue functions give you a lockless network protocol input queue interface with integrated software interrupt scheduling and support for receiver-side packet steering (RPS). The implementation is based around per-CPU producer-consumer queues; multiple CPUs may enqueue packets into a CPU's queue, but only the owning CPU will dequeue packets from any given queue. Periodt. π«Ά
π FUNCTIONS π
pktq_create(maxlen, intrh, arg)β Creates a packet queue that can store at mostmaxlenpackets at one time.maxlenmust not exceedPCQ_MAXLEN. The software interrupt handlerintrhwith argumentargwill be established atSOFTINT_NET. πpktq_destroy(pq)β Destroys the specified packet queue. The caller is responsible for ensuring that no packets remain in the queue prior to calling this function. π¨pktq_enqueue(pq, m, hash)β Enqueues the packetmin the specified packet queue. The modulus ofhashand the number of CPUs will be computed and used to select the per-CPU queue where the packet will be stored, and thus upon which CPU the packet will be processed. Once the CPU selection has been made and the packet placed in the queue, the software interrupt associated with packet queue will be scheduled. β¨pktq_dequeue(pq)β Dequeues a packet from the current CPU's queue. If no packets remain,NULLis returned. This function must be called with kernel preemption disabled, which is always the case when running in a software interrupt handler. π₯pktq_barrier(pq)β Waits for a grace period when all packets enqueued at the moment of calling this function will have been dequeued. πpktq_ifdetach()β This function is called when a network interface is detached from the system. It performs apktq_barrier()operation on all packet queues. π¦pktq_flush(pq)β This function removes and frees all packets in the specified queue. The caller is responsible for ensuring that no calls topktq_enqueue()orpktq_dequeue()run concurrently withpktq_flush(). πpktq_set_maxlen(pq, maxlen)β Sets the maximum queue length to the valuemaxlen. If the new value ofmaxlenis smaller than the previous value, then this routine may block until all packets that were previously in the packet queue can be re-enqueued. βpktq_rps_hash(funcp, m)β Calculates the RPS hash for the packetmusing the hash function referenced byfuncp. The available hash functions arezero(always returns 0),curcpu(returns the index of the current CPU),toeplitz(Toeplitz hash of an IPv4 or IPv6 packet), andtoeplitz-othercpus(same astoeplitzbut always hashes to a CPU other than the current CPU). A default hash routine is provided by the global variablepktq_rps_hash_default. The default routine is guaranteed to be safe to use for any network protocol. The behavior oftoeplitzandtoeplitz-othercpusis undefined if used with protocols other than IPv4 or IPv6. π¨pktq_sysctl_setup(pq, clog, parent_node, qid)β This function registers standard sysctl handlers forpqat the parent sysctl nodeparent_node.qidallows the caller to specify the node ID at which to attach toparent_node; useCTL_CREATEto dynamically assign a node ID. Theclogargument is passed directly tosysctl_createv(). πsysctl_pktq_rps_hash_handler()β This function provides a way for the user to select the preferred RPS hash function to be used by a caller ofpktq_rps_hash()viasysctl(8). When callingsysctl_createv()to create the sysctl node, passsysctl_pktq_rps_hash_handler()as thefuncargument and the pointer to the RPS hash function reference variable as thenewpargument (cast tovoid *). π
π CODE REFERENCES π
- The
pktqueueinterface is implemented within the filenet/pktqueue.c. π§ - An example of how to use
pktq_rps_hash()can be found in theether_input()function. An example of how to usesysctl_pktq_rps_hash_handler()can be found in theether_sysctl_setup()function. Both reside within the filenet/if_ethersubr.c. π - An example of how to use
pktq_sysctl_setup()can be found in thesysctl_net_inet_ip_setup()function within the filenetinet/ip_input.c. π
π NOTES π
- The
maxlenargument provided topktq_create()specifies the maximum number of packets that can be stored in each per-CPU queue. This means that, in theory, the maximum number of packets that can be enqueued ismaxlen * ncpu. However, as a practical matter, the number will be smaller because the distribution of packets across the per-CPU queues is not perfectly uniform. π - Calls to
pktq_set_maxlen()may result in re-ordering the delivery of packets currently in the queue. π«Ά
π SEE ALSO π
sysctl(8), kpreempt(9), pcq(9), softintr(9), sysctl(9)
π°οΈ HISTORY π°οΈ
The pktqueue interface first appeared in NetBSD 7.0. π
kisses bestie, this page comes from Debian and is dated September 3, 2022 β now go slay those kernel queues π β¨
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2022 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms