]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/net/inetpeer.h
net: Refactor inetpeer address struct
[karo-tx-linux.git] / include / net / inetpeer.h
1 /*
2  *              INETPEER - A storage for permanent information about peers
3  *
4  *  Authors:    Andrey V. Savochkin <saw@msu.ru>
5  */
6
7 #ifndef _NET_INETPEER_H
8 #define _NET_INETPEER_H
9
10 #include <linux/types.h>
11 #include <linux/init.h>
12 #include <linux/jiffies.h>
13 #include <linux/spinlock.h>
14 #include <linux/rtnetlink.h>
15 #include <net/ipv6.h>
16 #include <linux/atomic.h>
17
18 #define INETPEER_MAXKEYSZ   (sizeof(struct in6_addr) / sizeof(u32))
19
20 struct inetpeer_addr {
21         union {
22                 __be32                  a4;
23                 struct in6_addr         a6;
24                 u32                     key[INETPEER_MAXKEYSZ];
25         };
26         __u16                           family;
27 };
28
29 struct inet_peer {
30         /* group together avl_left,avl_right,v4daddr to speedup lookups */
31         struct inet_peer __rcu  *avl_left, *avl_right;
32         struct inetpeer_addr    daddr;
33         __u32                   avl_height;
34
35         u32                     metrics[RTAX_MAX];
36         u32                     rate_tokens;    /* rate limiting for ICMP */
37         unsigned long           rate_last;
38         union {
39                 struct list_head        gc_list;
40                 struct rcu_head     gc_rcu;
41         };
42         /*
43          * Once inet_peer is queued for deletion (refcnt == -1), following field
44          * is not available: rid
45          * We can share memory with rcu_head to help keep inet_peer small.
46          */
47         union {
48                 struct {
49                         atomic_t                        rid;            /* Frag reception counter */
50                 };
51                 struct rcu_head         rcu;
52                 struct inet_peer        *gc_next;
53         };
54
55         /* following fields might be frequently dirtied */
56         __u32                   dtime;  /* the time of last use of not referenced entries */
57         atomic_t                refcnt;
58 };
59
60 struct inet_peer_base {
61         struct inet_peer __rcu  *root;
62         seqlock_t               lock;
63         int                     total;
64 };
65
66 void inet_peer_base_init(struct inet_peer_base *);
67
68 void inet_initpeers(void) __init;
69
70 #define INETPEER_METRICS_NEW    (~(u32) 0)
71
72 static inline void inetpeer_set_addr_v4(struct inetpeer_addr *iaddr, __be32 ip)
73 {
74         iaddr->a4 = ip;
75         iaddr->family = AF_INET;
76 }
77
78 static inline __be32 inetpeer_get_addr_v4(struct inetpeer_addr *iaddr)
79 {
80         return iaddr->a4;
81 }
82
83 static inline void inetpeer_set_addr_v6(struct inetpeer_addr *iaddr,
84                                         struct in6_addr *in6)
85 {
86         iaddr->a6 = *in6;
87         iaddr->family = AF_INET6;
88 }
89
90 static inline struct in6_addr *inetpeer_get_addr_v6(struct inetpeer_addr *iaddr)
91 {
92         return &iaddr->a6;
93 }
94
95 /* can be called with or without local BH being disabled */
96 struct inet_peer *inet_getpeer(struct inet_peer_base *base,
97                                const struct inetpeer_addr *daddr,
98                                int create);
99
100 static inline struct inet_peer *inet_getpeer_v4(struct inet_peer_base *base,
101                                                 __be32 v4daddr,
102                                                 int create)
103 {
104         struct inetpeer_addr daddr;
105
106         daddr.a4 = v4daddr;
107         daddr.family = AF_INET;
108         return inet_getpeer(base, &daddr, create);
109 }
110
111 static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
112                                                 const struct in6_addr *v6daddr,
113                                                 int create)
114 {
115         struct inetpeer_addr daddr;
116
117         daddr.a6 = *v6daddr;
118         daddr.family = AF_INET6;
119         return inet_getpeer(base, &daddr, create);
120 }
121
122 static inline int inetpeer_addr_cmp(const struct inetpeer_addr *a,
123                                     const struct inetpeer_addr *b)
124 {
125         int i, n;
126
127         if (a->family == AF_INET)
128                 n = sizeof(a->a4) / sizeof(u32);
129         else
130                 n = sizeof(a->a6) / sizeof(u32);
131
132         for (i = 0; i < n; i++) {
133                 if (a->key[i] == b->key[i])
134                         continue;
135                 if (a->key[i] < b->key[i])
136                         return -1;
137                 return 1;
138         }
139
140         return 0;
141 }
142
143 /* can be called from BH context or outside */
144 void inet_putpeer(struct inet_peer *p);
145 bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
146
147 void inetpeer_invalidate_tree(struct inet_peer_base *);
148
149 #endif /* _NET_INETPEER_H */