Home | History | Annotate | Line # | Download | only in net
pktqueue.c revision 1.11
      1  1.11  thorpej /*	$NetBSD: pktqueue.c,v 1.11 2020/02/07 12:35:33 thorpej Exp $	*/
      2   1.1    rmind 
      3   1.1    rmind /*-
      4   1.1    rmind  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5   1.1    rmind  * All rights reserved.
      6   1.1    rmind  *
      7   1.1    rmind  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1    rmind  * by Mindaugas Rasiukevicius.
      9   1.1    rmind  *
     10   1.1    rmind  * Redistribution and use in source and binary forms, with or without
     11   1.1    rmind  * modification, are permitted provided that the following conditions
     12   1.1    rmind  * are met:
     13   1.1    rmind  * 1. Redistributions of source code must retain the above copyright
     14   1.1    rmind  *    notice, this list of conditions and the following disclaimer.
     15   1.1    rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1    rmind  *    notice, this list of conditions and the following disclaimer in the
     17   1.1    rmind  *    documentation and/or other materials provided with the distribution.
     18   1.1    rmind  *
     19   1.1    rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1    rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1    rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1    rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1    rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1    rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1    rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1    rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1    rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1    rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1    rmind  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1    rmind  */
     31   1.1    rmind 
     32   1.4    rmind /*
     33   1.4    rmind  * The packet queue (pktqueue) interface is a lockless IP input queue
     34   1.4    rmind  * which also abstracts and handles network ISR scheduling.  It provides
     35   1.4    rmind  * a mechanism to enable receiver-side packet steering (RPS).
     36   1.4    rmind  */
     37   1.4    rmind 
     38   1.1    rmind #include <sys/cdefs.h>
     39  1.11  thorpej __KERNEL_RCSID(0, "$NetBSD: pktqueue.c,v 1.11 2020/02/07 12:35:33 thorpej Exp $");
     40   1.1    rmind 
     41   1.1    rmind #include <sys/param.h>
     42   1.1    rmind #include <sys/types.h>
     43   1.1    rmind 
     44   1.1    rmind #include <sys/atomic.h>
     45   1.1    rmind #include <sys/cpu.h>
     46   1.1    rmind #include <sys/pcq.h>
     47   1.1    rmind #include <sys/intr.h>
     48   1.1    rmind #include <sys/mbuf.h>
     49   1.1    rmind #include <sys/proc.h>
     50   1.1    rmind #include <sys/percpu.h>
     51  1.11  thorpej #include <sys/xcall.h>
     52   1.1    rmind 
     53   1.1    rmind #include <net/pktqueue.h>
     54   1.1    rmind 
     55   1.1    rmind /*
     56   1.1    rmind  * WARNING: update this if struct pktqueue changes.
     57   1.1    rmind  */
     58   1.1    rmind #define	PKTQ_CLPAD	\
     59   1.1    rmind     MAX(COHERENCY_UNIT, COHERENCY_UNIT - sizeof(kmutex_t) - sizeof(u_int))
     60   1.1    rmind 
     61   1.1    rmind struct pktqueue {
     62   1.1    rmind 	/*
     63   1.1    rmind 	 * The lock used for a barrier mechanism.  The barrier counter,
     64   1.1    rmind 	 * as well as the drop counter, are managed atomically though.
     65   1.1    rmind 	 * Ensure this group is in a separate cache line.
     66   1.1    rmind 	 */
     67   1.1    rmind 	kmutex_t	pq_lock;
     68   1.1    rmind 	volatile u_int	pq_barrier;
     69   1.1    rmind 	uint8_t		_pad[PKTQ_CLPAD];
     70   1.1    rmind 
     71   1.1    rmind 	/* The size of the queue, counters and the interrupt handler. */
     72   1.1    rmind 	u_int		pq_maxlen;
     73   1.1    rmind 	percpu_t *	pq_counters;
     74   1.1    rmind 	void *		pq_sih;
     75   1.1    rmind 
     76   1.1    rmind 	/* Finally, per-CPU queues. */
     77   1.1    rmind 	pcq_t *		pq_queue[];
     78   1.1    rmind };
     79   1.1    rmind 
     80   1.1    rmind /* The counters of the packet queue. */
     81   1.1    rmind #define	PQCNT_ENQUEUE	0
     82   1.1    rmind #define	PQCNT_DEQUEUE	1
     83   1.1    rmind #define	PQCNT_DROP	2
     84   1.1    rmind #define	PQCNT_NCOUNTERS	3
     85   1.1    rmind 
     86   1.1    rmind typedef struct {
     87   1.1    rmind 	uint64_t	count[PQCNT_NCOUNTERS];
     88   1.1    rmind } pktq_counters_t;
     89   1.1    rmind 
     90   1.1    rmind /* Special marker value used by pktq_barrier() mechanism. */
     91   1.1    rmind #define	PKTQ_MARKER	((void *)(~0ULL))
     92   1.1    rmind 
     93   1.1    rmind /*
     94   1.1    rmind  * The total size of pktqueue_t which depends on the number of CPUs.
     95   1.1    rmind  */
     96   1.1    rmind #define	PKTQUEUE_STRUCT_LEN(ncpu)	\
     97   1.1    rmind     roundup2(offsetof(pktqueue_t, pq_queue[ncpu]), coherency_unit)
     98   1.1    rmind 
     99   1.1    rmind pktqueue_t *
    100   1.5    ozaki pktq_create(size_t maxlen, void (*intrh)(void *), void *sc)
    101   1.1    rmind {
    102   1.1    rmind 	const u_int sflags = SOFTINT_NET | SOFTINT_MPSAFE | SOFTINT_RCPU;
    103   1.1    rmind 	const size_t len = PKTQUEUE_STRUCT_LEN(ncpu);
    104   1.1    rmind 	pktqueue_t *pq;
    105   1.1    rmind 	percpu_t *pc;
    106   1.1    rmind 	void *sih;
    107   1.1    rmind 
    108   1.9      chs 	pc = percpu_alloc(sizeof(pktq_counters_t));
    109   1.5    ozaki 	if ((sih = softint_establish(sflags, intrh, sc)) == NULL) {
    110   1.1    rmind 		percpu_free(pc, sizeof(pktq_counters_t));
    111   1.1    rmind 		return NULL;
    112   1.1    rmind 	}
    113   1.1    rmind 
    114   1.1    rmind 	pq = kmem_zalloc(len, KM_SLEEP);
    115   1.1    rmind 	for (u_int i = 0; i < ncpu; i++) {
    116   1.1    rmind 		pq->pq_queue[i] = pcq_create(maxlen, KM_SLEEP);
    117   1.1    rmind 	}
    118   1.1    rmind 	mutex_init(&pq->pq_lock, MUTEX_DEFAULT, IPL_NONE);
    119   1.1    rmind 	pq->pq_maxlen = maxlen;
    120   1.1    rmind 	pq->pq_counters = pc;
    121   1.1    rmind 	pq->pq_sih = sih;
    122   1.1    rmind 
    123   1.1    rmind 	return pq;
    124   1.1    rmind }
    125   1.1    rmind 
    126   1.1    rmind void
    127   1.1    rmind pktq_destroy(pktqueue_t *pq)
    128   1.1    rmind {
    129   1.1    rmind 	const size_t len = PKTQUEUE_STRUCT_LEN(ncpu);
    130   1.1    rmind 
    131   1.1    rmind 	for (u_int i = 0; i < ncpu; i++) {
    132   1.1    rmind 		pcq_t *q = pq->pq_queue[i];
    133   1.1    rmind 		KASSERT(pcq_peek(q) == NULL);
    134   1.1    rmind 		pcq_destroy(q);
    135   1.1    rmind 	}
    136   1.1    rmind 	percpu_free(pq->pq_counters, sizeof(pktq_counters_t));
    137   1.1    rmind 	softint_disestablish(pq->pq_sih);
    138   1.1    rmind 	mutex_destroy(&pq->pq_lock);
    139   1.1    rmind 	kmem_free(pq, len);
    140   1.1    rmind }
    141   1.1    rmind 
    142   1.1    rmind /*
    143   1.1    rmind  * - pktq_inc_counter: increment the counter given an ID.
    144   1.1    rmind  * - pktq_collect_counts: handler to sum up the counts from each CPU.
    145   1.1    rmind  * - pktq_getcount: return the effective count given an ID.
    146   1.1    rmind  */
    147   1.1    rmind 
    148   1.1    rmind static inline void
    149   1.1    rmind pktq_inc_count(pktqueue_t *pq, u_int i)
    150   1.1    rmind {
    151   1.1    rmind 	percpu_t *pc = pq->pq_counters;
    152   1.1    rmind 	pktq_counters_t *c;
    153   1.1    rmind 
    154   1.1    rmind 	c = percpu_getref(pc);
    155   1.1    rmind 	c->count[i]++;
    156   1.1    rmind 	percpu_putref(pc);
    157   1.1    rmind }
    158   1.1    rmind 
    159   1.1    rmind static void
    160   1.1    rmind pktq_collect_counts(void *mem, void *arg, struct cpu_info *ci)
    161   1.1    rmind {
    162   1.1    rmind 	const pktq_counters_t *c = mem;
    163   1.1    rmind 	pktq_counters_t *sum = arg;
    164   1.1    rmind 
    165  1.11  thorpej 	int s = splnet();
    166  1.11  thorpej 
    167   1.1    rmind 	for (u_int i = 0; i < PQCNT_NCOUNTERS; i++) {
    168   1.1    rmind 		sum->count[i] += c->count[i];
    169   1.1    rmind 	}
    170  1.11  thorpej 
    171  1.11  thorpej 	splx(s);
    172   1.1    rmind }
    173   1.1    rmind 
    174   1.1    rmind uint64_t
    175   1.1    rmind pktq_get_count(pktqueue_t *pq, pktq_count_t c)
    176   1.1    rmind {
    177   1.1    rmind 	pktq_counters_t sum;
    178   1.1    rmind 
    179   1.1    rmind 	if (c != PKTQ_MAXLEN) {
    180   1.1    rmind 		memset(&sum, 0, sizeof(sum));
    181  1.11  thorpej 		percpu_foreach_xcall(pq->pq_counters,
    182  1.11  thorpej 		    XC_HIGHPRI_IPL(IPL_SOFTNET), pktq_collect_counts, &sum);
    183   1.1    rmind 	}
    184   1.1    rmind 	switch (c) {
    185   1.1    rmind 	case PKTQ_NITEMS:
    186   1.1    rmind 		return sum.count[PQCNT_ENQUEUE] - sum.count[PQCNT_DEQUEUE];
    187   1.1    rmind 	case PKTQ_DROPS:
    188   1.1    rmind 		return sum.count[PQCNT_DROP];
    189   1.1    rmind 	case PKTQ_MAXLEN:
    190   1.1    rmind 		return pq->pq_maxlen;
    191   1.1    rmind 	}
    192   1.1    rmind 	return 0;
    193   1.1    rmind }
    194   1.1    rmind 
    195   1.1    rmind uint32_t
    196   1.1    rmind pktq_rps_hash(const struct mbuf *m __unused)
    197   1.1    rmind {
    198   1.1    rmind 	/*
    199   1.1    rmind 	 * XXX: No distribution yet; the softnet_lock contention
    200   1.1    rmind 	 * XXX: must be eliminated first.
    201   1.1    rmind 	 */
    202   1.1    rmind 	return 0;
    203   1.1    rmind }
    204   1.1    rmind 
    205   1.1    rmind /*
    206   1.1    rmind  * pktq_enqueue: inject the packet into the end of the queue.
    207   1.1    rmind  *
    208   1.1    rmind  * => Must be called from the interrupt or with the preemption disabled.
    209   1.1    rmind  * => Consumes the packet and returns true on success.
    210   1.1    rmind  * => Returns false on failure; caller is responsible to free the packet.
    211   1.1    rmind  */
    212   1.1    rmind bool
    213   1.3    rmind pktq_enqueue(pktqueue_t *pq, struct mbuf *m, const u_int hash __unused)
    214   1.1    rmind {
    215   1.8    ozaki #if defined(_RUMPKERNEL) || defined(_RUMP_NATIVE_ABI)
    216   1.7    ozaki 	const unsigned cpuid = curcpu()->ci_index;
    217   1.7    ozaki #else
    218   1.7    ozaki 	const unsigned cpuid = hash % ncpu;
    219   1.7    ozaki #endif
    220   1.1    rmind 
    221   1.1    rmind 	KASSERT(kpreempt_disabled());
    222   1.1    rmind 
    223   1.1    rmind 	if (__predict_false(!pcq_put(pq->pq_queue[cpuid], m))) {
    224   1.1    rmind 		pktq_inc_count(pq, PQCNT_DROP);
    225   1.1    rmind 		return false;
    226   1.1    rmind 	}
    227   1.1    rmind 	softint_schedule_cpu(pq->pq_sih, cpu_lookup(cpuid));
    228   1.1    rmind 	pktq_inc_count(pq, PQCNT_ENQUEUE);
    229   1.1    rmind 	return true;
    230   1.1    rmind }
    231   1.1    rmind 
    232   1.1    rmind /*
    233   1.1    rmind  * pktq_dequeue: take a packet from the queue.
    234   1.1    rmind  *
    235   1.1    rmind  * => Must be called with preemption disabled.
    236   1.1    rmind  * => Must ensure there are not concurrent dequeue calls.
    237   1.1    rmind  */
    238   1.1    rmind struct mbuf *
    239   1.1    rmind pktq_dequeue(pktqueue_t *pq)
    240   1.1    rmind {
    241   1.1    rmind 	const struct cpu_info *ci = curcpu();
    242   1.1    rmind 	const unsigned cpuid = cpu_index(ci);
    243   1.1    rmind 	struct mbuf *m;
    244   1.1    rmind 
    245   1.1    rmind 	m = pcq_get(pq->pq_queue[cpuid]);
    246   1.1    rmind 	if (__predict_false(m == PKTQ_MARKER)) {
    247   1.1    rmind 		/* Note the marker entry. */
    248   1.1    rmind 		atomic_inc_uint(&pq->pq_barrier);
    249   1.1    rmind 		return NULL;
    250   1.1    rmind 	}
    251   1.1    rmind 	if (__predict_true(m != NULL)) {
    252   1.1    rmind 		pktq_inc_count(pq, PQCNT_DEQUEUE);
    253   1.1    rmind 	}
    254   1.1    rmind 	return m;
    255   1.1    rmind }
    256   1.1    rmind 
    257   1.1    rmind /*
    258   1.1    rmind  * pktq_barrier: waits for a grace period when all packets enqueued at
    259   1.1    rmind  * the moment of calling this routine will be processed.  This is used
    260   1.1    rmind  * to ensure that e.g. packets referencing some interface were drained.
    261   1.1    rmind  */
    262   1.1    rmind void
    263   1.1    rmind pktq_barrier(pktqueue_t *pq)
    264   1.1    rmind {
    265   1.1    rmind 	u_int pending = 0;
    266   1.1    rmind 
    267   1.1    rmind 	mutex_enter(&pq->pq_lock);
    268   1.1    rmind 	KASSERT(pq->pq_barrier == 0);
    269   1.1    rmind 
    270   1.1    rmind 	for (u_int i = 0; i < ncpu; i++) {
    271   1.1    rmind 		pcq_t *q = pq->pq_queue[i];
    272   1.1    rmind 
    273   1.1    rmind 		/* If the queue is empty - nothing to do. */
    274   1.1    rmind 		if (pcq_peek(q) == NULL) {
    275   1.1    rmind 			continue;
    276   1.1    rmind 		}
    277   1.1    rmind 		/* Otherwise, put the marker and entry. */
    278   1.1    rmind 		while (!pcq_put(q, PKTQ_MARKER)) {
    279   1.1    rmind 			kpause("pktqsync", false, 1, NULL);
    280   1.1    rmind 		}
    281   1.1    rmind 		kpreempt_disable();
    282   1.1    rmind 		softint_schedule_cpu(pq->pq_sih, cpu_lookup(i));
    283   1.1    rmind 		kpreempt_enable();
    284   1.1    rmind 		pending++;
    285   1.1    rmind 	}
    286   1.1    rmind 
    287   1.1    rmind 	/* Wait for each queue to process the markers. */
    288   1.1    rmind 	while (pq->pq_barrier != pending) {
    289   1.1    rmind 		kpause("pktqsync", false, 1, NULL);
    290   1.1    rmind 	}
    291   1.1    rmind 	pq->pq_barrier = 0;
    292   1.1    rmind 	mutex_exit(&pq->pq_lock);
    293   1.1    rmind }
    294   1.1    rmind 
    295   1.1    rmind /*
    296   1.1    rmind  * pktq_flush: free mbufs in all queues.
    297   1.1    rmind  *
    298   1.4    rmind  * => The caller must ensure there are no concurrent writers or flush calls.
    299   1.1    rmind  */
    300   1.1    rmind void
    301   1.1    rmind pktq_flush(pktqueue_t *pq)
    302   1.1    rmind {
    303   1.1    rmind 	struct mbuf *m;
    304   1.1    rmind 
    305   1.1    rmind 	for (u_int i = 0; i < ncpu; i++) {
    306   1.1    rmind 		while ((m = pcq_get(pq->pq_queue[i])) != NULL) {
    307   1.1    rmind 			pktq_inc_count(pq, PQCNT_DEQUEUE);
    308   1.1    rmind 			m_freem(m);
    309   1.1    rmind 		}
    310   1.1    rmind 	}
    311   1.1    rmind }
    312   1.2    rmind 
    313   1.2    rmind /*
    314   1.2    rmind  * pktq_set_maxlen: create per-CPU queues using a new size and replace
    315   1.2    rmind  * the existing queues without losing any packets.
    316   1.2    rmind  */
    317   1.2    rmind int
    318   1.2    rmind pktq_set_maxlen(pktqueue_t *pq, size_t maxlen)
    319   1.2    rmind {
    320   1.2    rmind 	const u_int slotbytes = ncpu * sizeof(pcq_t *);
    321   1.2    rmind 	pcq_t **qs;
    322   1.2    rmind 
    323   1.2    rmind 	if (!maxlen || maxlen > PCQ_MAXLEN)
    324   1.2    rmind 		return EINVAL;
    325   1.2    rmind 	if (pq->pq_maxlen == maxlen)
    326   1.2    rmind 		return 0;
    327   1.2    rmind 
    328   1.2    rmind 	/* First, allocate the new queues and replace them. */
    329   1.2    rmind 	qs = kmem_zalloc(slotbytes, KM_SLEEP);
    330   1.2    rmind 	for (u_int i = 0; i < ncpu; i++) {
    331   1.2    rmind 		qs[i] = pcq_create(maxlen, KM_SLEEP);
    332   1.2    rmind 	}
    333   1.2    rmind 	mutex_enter(&pq->pq_lock);
    334   1.2    rmind 	for (u_int i = 0; i < ncpu; i++) {
    335   1.2    rmind 		/* Swap: store of a word is atomic. */
    336   1.2    rmind 		pcq_t *q = pq->pq_queue[i];
    337   1.2    rmind 		pq->pq_queue[i] = qs[i];
    338   1.2    rmind 		qs[i] = q;
    339   1.2    rmind 	}
    340   1.2    rmind 	pq->pq_maxlen = maxlen;
    341   1.2    rmind 	mutex_exit(&pq->pq_lock);
    342   1.2    rmind 
    343   1.2    rmind 	/*
    344   1.2    rmind 	 * At this point, the new packets are flowing into the new
    345   1.4    rmind 	 * queues.  However, the old queues may have some packets
    346   1.4    rmind 	 * present which are no longer being processed.  We are going
    347   1.2    rmind 	 * to re-enqueue them.  This may change the order of packet
    348   1.2    rmind 	 * arrival, but it is not considered an issue.
    349   1.2    rmind 	 *
    350   1.4    rmind 	 * There may be in-flight interrupts calling pktq_dequeue()
    351   1.2    rmind 	 * which reference the old queues.  Issue a barrier to ensure
    352   1.2    rmind 	 * that we are going to be the only pcq_get() callers on the
    353   1.2    rmind 	 * old queues.
    354   1.2    rmind 	 */
    355   1.2    rmind 	pktq_barrier(pq);
    356   1.2    rmind 
    357   1.2    rmind 	for (u_int i = 0; i < ncpu; i++) {
    358   1.2    rmind 		struct mbuf *m;
    359   1.2    rmind 
    360   1.2    rmind 		while ((m = pcq_get(qs[i])) != NULL) {
    361   1.2    rmind 			while (!pcq_put(pq->pq_queue[i], m)) {
    362   1.2    rmind 				kpause("pktqrenq", false, 1, NULL);
    363   1.2    rmind 			}
    364   1.2    rmind 		}
    365   1.2    rmind 		pcq_destroy(qs[i]);
    366   1.2    rmind 	}
    367   1.2    rmind 
    368   1.2    rmind 	/* Well, that was fun. */
    369   1.2    rmind 	kmem_free(qs, slotbytes);
    370   1.2    rmind 	return 0;
    371   1.2    rmind }
    372   1.6    ozaki 
    373   1.6    ozaki int
    374   1.6    ozaki sysctl_pktq_maxlen(SYSCTLFN_ARGS, pktqueue_t *pq)
    375   1.6    ozaki {
    376   1.6    ozaki 	u_int nmaxlen = pktq_get_count(pq, PKTQ_MAXLEN);
    377   1.6    ozaki 	struct sysctlnode node = *rnode;
    378   1.6    ozaki 	int error;
    379   1.6    ozaki 
    380   1.6    ozaki 	node.sysctl_data = &nmaxlen;
    381   1.6    ozaki 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    382   1.6    ozaki 	if (error || newp == NULL)
    383   1.6    ozaki 		return error;
    384   1.6    ozaki 	return pktq_set_maxlen(pq, nmaxlen);
    385   1.6    ozaki }
    386   1.6    ozaki 
    387   1.6    ozaki int
    388   1.6    ozaki sysctl_pktq_count(SYSCTLFN_ARGS, pktqueue_t *pq, u_int count_id)
    389   1.6    ozaki {
    390  1.10  msaitoh 	uint64_t count = pktq_get_count(pq, count_id);
    391   1.6    ozaki 	struct sysctlnode node = *rnode;
    392  1.10  msaitoh 
    393   1.6    ozaki 	node.sysctl_data = &count;
    394   1.6    ozaki 	return sysctl_lookup(SYSCTLFN_CALL(&node));
    395   1.6    ozaki }
    396