inet_frag.h 3.3 KB
Newer Older
1 2 3
#ifndef __NET_FRAG_H__
#define __NET_FRAG_H__

4 5
#include <linux/percpu_counter.h>

6
struct netns_frags {
7
	int			nqueues;
8
	struct list_head	lru_list;
9

10 11
	/* The percpu_counter "mem" need to be cacheline aligned.
	 *  mem.count must not share cacheline with other writers
12
	 */
13 14
	struct percpu_counter   mem ____cacheline_aligned_in_smp;

15 16
	/* sysctls */
	int			timeout;
17 18
	int			high_thresh;
	int			low_thresh;
19 20
};

21 22 23
struct inet_frag_queue {
	spinlock_t		lock;
	struct timer_list	timer;      /* when will this queue expire? */
24 25 26
	struct list_head	lru_list;   /* lru list member */
	struct hlist_node	list;
	atomic_t		refcnt;
27
	struct sk_buff		*fragments; /* list of received fragments */
28
	struct sk_buff		*fragments_tail;
29 30 31 32 33
	ktime_t			stamp;
	int			len;        /* total length of orig datagram */
	int			meat;
	__u8			last_in;    /* first/last segment arrived? */

34 35 36
#define INET_FRAG_COMPLETE	4
#define INET_FRAG_FIRST_IN	2
#define INET_FRAG_LAST_IN	1
37 38

	u16			max_size;
39 40

	struct netns_frags	*net;
41 42
};

43 44 45 46
#define INETFRAGS_HASHSZ		64

struct inet_frags {
	struct hlist_head	hash[INETFRAGS_HASHSZ];
47 48 49 50
	/* This rwlock is a global lock (seperate per IPv4, IPv6 and
	 * netfilter). Important to keep this on a seperate cacheline.
	 */
	rwlock_t		lock ____cacheline_aligned_in_smp;
51
	int			secret_interval;
52
	struct timer_list	secret_timer;
53 54
	u32			rnd;
	int			qsize;
55 56

	unsigned int		(*hashfn)(struct inet_frag_queue *);
57
	bool			(*match)(struct inet_frag_queue *q, void *arg);
58 59
	void			(*constructor)(struct inet_frag_queue *q,
						void *arg);
60 61
	void			(*destructor)(struct inet_frag_queue *);
	void			(*skb_free)(struct sk_buff *);
62
	void			(*frag_expire)(unsigned long data);
63 64 65 66 67
};

void inet_frags_init(struct inet_frags *);
void inet_frags_fini(struct inet_frags *);

68
void inet_frags_init_net(struct netns_frags *nf);
69
void inet_frags_exit_net(struct netns_frags *nf, struct inet_frags *f);
70

71
void inet_frag_kill(struct inet_frag_queue *q, struct inet_frags *f);
72 73
void inet_frag_destroy(struct inet_frag_queue *q,
				struct inet_frags *f, int *work);
74
int inet_frag_evictor(struct netns_frags *nf, struct inet_frags *f, bool force);
75
struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
76 77
		struct inet_frags *f, void *key, unsigned int hash)
	__releases(&f->lock);
78

P
Pavel Emelyanov 已提交
79 80 81 82 83 84
static inline void inet_frag_put(struct inet_frag_queue *q, struct inet_frags *f)
{
	if (atomic_dec_and_test(&q->refcnt))
		inet_frag_destroy(q, f, NULL);
}

85 86
/* Memory Tracking Functions. */

87 88 89 90 91 92 93
/* The default percpu_counter batch size is not big enough to scale to
 * fragmentation mem acct sizes.
 * The mem size of a 64K fragment is approx:
 *  (44 fragments * 2944 truesize) + frag_queue struct(200) = 129736 bytes
 */
static unsigned int frag_percpu_counter_batch = 130000;

94 95
static inline int frag_mem_limit(struct netns_frags *nf)
{
96
	return percpu_counter_read(&nf->mem);
97 98 99 100
}

static inline void sub_frag_mem_limit(struct inet_frag_queue *q, int i)
{
101
	__percpu_counter_add(&q->net->mem, -i, frag_percpu_counter_batch);
102 103 104 105
}

static inline void add_frag_mem_limit(struct inet_frag_queue *q, int i)
{
106
	__percpu_counter_add(&q->net->mem, i, frag_percpu_counter_batch);
107 108 109 110
}

static inline void init_frag_mem_limit(struct netns_frags *nf)
{
111
	percpu_counter_init(&nf->mem, 0);
112 113 114 115
}

static inline int sum_frag_mem_limit(struct netns_frags *nf)
{
116
	return percpu_counter_sum_positive(&nf->mem);
117 118
}

119
#endif