inetpeer.h 3.44 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *		INETPEER - A storage for permanent information about peers
 *
 *  Authors:	Andrey V. Savochkin <saw@msu.ru>
 */

#ifndef _NET_INETPEER_H
#define _NET_INETPEER_H

#include <linux/types.h>
#include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/spinlock.h>
14
#include <linux/rtnetlink.h>
15
#include <net/ipv6.h>
16
#include <linux/atomic.h>
Linus Torvalds's avatar
Linus Torvalds committed
17

18 19 20 21 22 23
/* IPv4 address key for cache lookups */
struct ipv4_addr_key {
	__be32	addr;
	int	vif;
};

24 25 26
#define INETPEER_MAXKEYSZ   (sizeof(struct in6_addr) / sizeof(u32))

struct inetpeer_addr {
27
	union {
28
		struct ipv4_addr_key	a4;
29 30
		struct in6_addr		a6;
		u32			key[INETPEER_MAXKEYSZ];
31
	};
32
	__u16				family;
33
};
34

Eric Dumazet's avatar
Eric Dumazet committed
35
struct inet_peer {
36
	/* group together avl_left,avl_right,v4daddr to speedup lookups */
37
	struct inet_peer __rcu	*avl_left, *avl_right;
38
	struct inetpeer_addr	daddr;
39
	__u32			avl_height;
40 41 42 43

	u32			metrics[RTAX_MAX];
	u32			rate_tokens;	/* rate limiting for ICMP */
	unsigned long		rate_last;
44 45 46 47
	union {
		struct list_head	gc_list;
		struct rcu_head     gc_rcu;
	};
48
	/*
49 50
	 * Once inet_peer is queued for deletion (refcnt == -1), following field
	 * is not available: rid
51
	 * We can share memory with rcu_head to help keep inet_peer small.
52 53 54
	 */
	union {
		struct {
55
			atomic_t			rid;		/* Frag reception counter */
56 57
		};
		struct rcu_head         rcu;
58
		struct inet_peer	*gc_next;
59
	};
60 61 62 63

	/* following fields might be frequently dirtied */
	__u32			dtime;	/* the time of last use of not referenced entries */
	atomic_t		refcnt;
Linus Torvalds's avatar
Linus Torvalds committed
64 65
};

66 67 68 69 70 71
struct inet_peer_base {
	struct inet_peer __rcu	*root;
	seqlock_t		lock;
	int			total;
};

72
void inet_peer_base_init(struct inet_peer_base *);
73

74
void inet_initpeers(void) __init;
Linus Torvalds's avatar
Linus Torvalds committed
75

76 77
#define INETPEER_METRICS_NEW	(~(u32) 0)

78 79
static inline void inetpeer_set_addr_v4(struct inetpeer_addr *iaddr, __be32 ip)
{
80
	iaddr->a4.addr = ip;
81
	iaddr->a4.vif = 0;
82 83 84 85 86
	iaddr->family = AF_INET;
}

static inline __be32 inetpeer_get_addr_v4(struct inetpeer_addr *iaddr)
{
87
	return iaddr->a4.addr;
88 89 90 91 92
}

static inline void inetpeer_set_addr_v6(struct inetpeer_addr *iaddr,
					struct in6_addr *in6)
{
93
	iaddr->a6 = *in6;
94 95 96 97 98
	iaddr->family = AF_INET6;
}

static inline struct in6_addr *inetpeer_get_addr_v6(struct inetpeer_addr *iaddr)
{
99
	return &iaddr->a6;
100 101
}

Linus Torvalds's avatar
Linus Torvalds committed
102
/* can be called with or without local BH being disabled */
103
struct inet_peer *inet_getpeer(struct inet_peer_base *base,
104 105
			       const struct inetpeer_addr *daddr,
			       int create);
106

107
static inline struct inet_peer *inet_getpeer_v4(struct inet_peer_base *base,
108
						__be32 v4daddr,
109
						int vif, int create)
110
{
111
	struct inetpeer_addr daddr;
112

113 114
	daddr.a4.addr = v4daddr;
	daddr.a4.vif = vif;
115
	daddr.family = AF_INET;
116
	return inet_getpeer(base, &daddr, create);
117
}
Linus Torvalds's avatar
Linus Torvalds committed
118

119
static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
120 121
						const struct in6_addr *v6daddr,
						int create)
122
{
123
	struct inetpeer_addr daddr;
124

125
	daddr.a6 = *v6daddr;
126
	daddr.family = AF_INET6;
127
	return inet_getpeer(base, &daddr, create);
128 129
}

130 131 132
static inline int inetpeer_addr_cmp(const struct inetpeer_addr *a,
				    const struct inetpeer_addr *b)
{
133 134 135 136 137 138
	int i, n;

	if (a->family == AF_INET)
		n = sizeof(a->a4) / sizeof(u32);
	else
		n = sizeof(a->a6) / sizeof(u32);
139 140

	for (i = 0; i < n; i++) {
141
		if (a->key[i] == b->key[i])
142
			continue;
143
		if (a->key[i] < b->key[i])
144 145 146 147 148 149 150
			return -1;
		return 1;
	}

	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
151
/* can be called from BH context or outside */
152 153
void inet_putpeer(struct inet_peer *p);
bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
Linus Torvalds's avatar
Linus Torvalds committed
154

155
void inetpeer_invalidate_tree(struct inet_peer_base *);
156

Linus Torvalds's avatar
Linus Torvalds committed
157
#endif /* _NET_INETPEER_H */