skb_array.h 4.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*
 *	Definitions for the 'struct skb_array' datastructure.
 *
 *	Author:
 *		Michael S. Tsirkin <mst@redhat.com>
 *
 *	Copyright (C) 2016 Red Hat, Inc.
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation; either version 2 of the License, or (at your
 *	option) any later version.
 *
 *	Limited-size FIFO of skbs. Can be used more or less whenever
 *	sk_buff_head can be used, except you need to know the queue size in
 *	advance.
 *	Implemented as a type-safe wrapper around ptr_ring.
 */

#ifndef _LINUX_SKB_ARRAY_H
#define _LINUX_SKB_ARRAY_H 1

#ifdef __KERNEL__
#include <linux/ptr_ring.h>
#include <linux/skbuff.h>
#include <linux/if_vlan.h>
#endif

struct skb_array {
	struct ptr_ring ring;
};

/* Might be slightly faster than skb_array_full below, but callers invoking
 * this in a loop must use a compiler barrier, for example cpu_relax().
 */
static inline bool __skb_array_full(struct skb_array *a)
{
	return __ptr_ring_full(&a->ring);
}

static inline bool skb_array_full(struct skb_array *a)
{
	return ptr_ring_full(&a->ring);
}

static inline int skb_array_produce(struct skb_array *a, struct sk_buff *skb)
{
	return ptr_ring_produce(&a->ring, skb);
}

static inline int skb_array_produce_irq(struct skb_array *a, struct sk_buff *skb)
{
	return ptr_ring_produce_irq(&a->ring, skb);
}

static inline int skb_array_produce_bh(struct skb_array *a, struct sk_buff *skb)
{
	return ptr_ring_produce_bh(&a->ring, skb);
}

static inline int skb_array_produce_any(struct skb_array *a, struct sk_buff *skb)
{
	return ptr_ring_produce_any(&a->ring, skb);
}

66 67 68
/* Might be slightly faster than skb_array_empty below, but only safe if the
 * array is never resized. Also, callers invoking this in a loop must take care
 * to use a compiler barrier, for example cpu_relax().
69 70 71 72 73 74 75 76 77 78 79
 */
static inline bool __skb_array_empty(struct skb_array *a)
{
	return !__ptr_ring_peek(&a->ring);
}

static inline bool skb_array_empty(struct skb_array *a)
{
	return ptr_ring_empty(&a->ring);
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
static inline bool skb_array_empty_bh(struct skb_array *a)
{
	return ptr_ring_empty_bh(&a->ring);
}

static inline bool skb_array_empty_irq(struct skb_array *a)
{
	return ptr_ring_empty_irq(&a->ring);
}

static inline bool skb_array_empty_any(struct skb_array *a)
{
	return ptr_ring_empty_any(&a->ring);
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
static inline struct sk_buff *skb_array_consume(struct skb_array *a)
{
	return ptr_ring_consume(&a->ring);
}

static inline struct sk_buff *skb_array_consume_irq(struct skb_array *a)
{
	return ptr_ring_consume_irq(&a->ring);
}

static inline struct sk_buff *skb_array_consume_any(struct skb_array *a)
{
	return ptr_ring_consume_any(&a->ring);
}

static inline struct sk_buff *skb_array_consume_bh(struct skb_array *a)
{
	return ptr_ring_consume_bh(&a->ring);
}

static inline int __skb_array_len_with_tag(struct sk_buff *skb)
{
	if (likely(skb)) {
		int len = skb->len;

		if (skb_vlan_tag_present(skb))
			len += VLAN_HLEN;

		return len;
	} else {
		return 0;
	}
}

static inline int skb_array_peek_len(struct skb_array *a)
{
	return PTR_RING_PEEK_CALL(&a->ring, __skb_array_len_with_tag);
}

static inline int skb_array_peek_len_irq(struct skb_array *a)
{
	return PTR_RING_PEEK_CALL_IRQ(&a->ring, __skb_array_len_with_tag);
}

static inline int skb_array_peek_len_bh(struct skb_array *a)
{
	return PTR_RING_PEEK_CALL_BH(&a->ring, __skb_array_len_with_tag);
}

static inline int skb_array_peek_len_any(struct skb_array *a)
{
	return PTR_RING_PEEK_CALL_ANY(&a->ring, __skb_array_len_with_tag);
}

static inline int skb_array_init(struct skb_array *a, int size, gfp_t gfp)
{
	return ptr_ring_init(&a->ring, size, gfp);
}

Jason Wang's avatar
Jason Wang committed
154
static void __skb_array_destroy_skb(void *ptr)
155 156 157 158
{
	kfree_skb(ptr);
}

Jason Wang's avatar
Jason Wang committed
159
static inline int skb_array_resize(struct skb_array *a, int size, gfp_t gfp)
160 161 162 163
{
	return ptr_ring_resize(&a->ring, size, gfp, __skb_array_destroy_skb);
}

164 165 166 167 168 169 170 171 172
static inline int skb_array_resize_multiple(struct skb_array **rings,
					    int nrings, int size, gfp_t gfp)
{
	BUILD_BUG_ON(offsetof(struct skb_array, ring));
	return ptr_ring_resize_multiple((struct ptr_ring **)rings,
					nrings, size, gfp,
					__skb_array_destroy_skb);
}

173 174
static inline void skb_array_cleanup(struct skb_array *a)
{
175
	ptr_ring_cleanup(&a->ring, __skb_array_destroy_skb);
176 177 178
}

#endif /* _LINUX_SKB_ARRAY_H  */