inetpeer.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:14k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * INETPEER - A storage for permanent information about peers
  3.  *
  4.  *  This source is covered by the GNU GPL, the same as all kernel sources.
  5.  *
  6.  *  Version: $Id: inetpeer.c,v 1.7 2001/09/20 21:22:50 davem Exp $
  7.  *
  8.  *  Authors: Andrey V. Savochkin <saw@msu.ru>
  9.  */
  10. #include <linux/types.h>
  11. #include <linux/slab.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/random.h>
  15. #include <linux/sched.h>
  16. #include <linux/timer.h>
  17. #include <linux/time.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <net/inetpeer.h>
  21. /*
  22.  *  Theory of operations.
  23.  *  We keep one entry for each peer IP address.  The nodes contains long-living
  24.  *  information about the peer which doesn't depend on routes.
  25.  *  At this moment this information consists only of ID field for the next
  26.  *  outgoing IP packet.  This field is incremented with each packet as encoded
  27.  *  in inet_getid() function (include/net/inetpeer.h).
  28.  *  At the moment of writing this notes identifier of IP packets is generated
  29.  *  to be unpredictable using this code only for packets subjected
  30.  *  (actually or potentially) to defragmentation.  I.e. DF packets less than
  31.  *  PMTU in size uses a constant ID and do not use this code (see
  32.  *  ip_select_ident() in include/net/ip.h).
  33.  *
  34.  *  Route cache entries hold references to our nodes.
  35.  *  New cache entries get references via lookup by destination IP address in
  36.  *  the avl tree.  The reference is grabbed only when it's needed i.e. only
  37.  *  when we try to output IP packet which needs an unpredictable ID (see
  38.  *  __ip_select_ident() in net/ipv4/route.c).
  39.  *  Nodes are removed only when reference counter goes to 0.
  40.  *  When it's happened the node may be removed when a sufficient amount of
  41.  *  time has been passed since its last use.  The less-recently-used entry can
  42.  *  also be removed if the pool is overloaded i.e. if the total amount of
  43.  *  entries is greater-or-equal than the threshold.
  44.  *
  45.  *  Node pool is organised as an AVL tree.
  46.  *  Such an implementation has been chosen not just for fun.  It's a way to
  47.  *  prevent easy and efficient DoS attacks by creating hash collisions.  A huge
  48.  *  amount of long living nodes in a single hash slot would significantly delay
  49.  *  lookups performed with disabled BHs.
  50.  *
  51.  *  Serialisation issues.
  52.  *  1.  Nodes may appear in the tree only with the pool write lock held.
  53.  *  2.  Nodes may disappear from the tree only with the pool write lock held
  54.  *      AND reference count being 0.
  55.  *  3.  Nodes appears and disappears from unused node list only under
  56.  *      "inet_peer_unused_lock".
  57.  *  4.  Global variable peer_total is modified under the pool lock.
  58.  *  5.  struct inet_peer fields modification:
  59.  * avl_left, avl_right, avl_parent, avl_height: pool lock
  60.  * unused_next, unused_prevp: unused node list lock
  61.  * refcnt: atomically against modifications on other CPU;
  62.  *    usually under some other lock to prevent node disappearing
  63.  * dtime: unused node list lock
  64.  * v4daddr: unchangeable
  65.  * ip_id_count: idlock
  66.  */
  67. /* Exported for inet_getid inline function.  */
  68. spinlock_t inet_peer_idlock = SPIN_LOCK_UNLOCKED;
  69. static kmem_cache_t *peer_cachep;
  70. #define node_height(x) x->avl_height
  71. static struct inet_peer peer_fake_node = {
  72. avl_left : &peer_fake_node,
  73. avl_right : &peer_fake_node,
  74. avl_height : 0
  75. };
  76. #define peer_avl_empty (&peer_fake_node)
  77. static struct inet_peer *peer_root = peer_avl_empty;
  78. static rwlock_t peer_pool_lock = RW_LOCK_UNLOCKED;
  79. #define PEER_MAXDEPTH 40 /* sufficient for about 2^27 nodes */
  80. static volatile int peer_total;
  81. /* Exported for sysctl_net_ipv4.  */
  82. int inet_peer_threshold = 65536 + 128; /* start to throw entries more
  83.  * aggressively at this stage */
  84. int inet_peer_minttl = 120 * HZ; /* TTL under high load: 120 sec */
  85. int inet_peer_maxttl = 10 * 60 * HZ; /* usual time to live: 10 min */
  86. /* Exported for inet_putpeer inline function.  */
  87. struct inet_peer *inet_peer_unused_head,
  88. **inet_peer_unused_tailp = &inet_peer_unused_head;
  89. spinlock_t inet_peer_unused_lock = SPIN_LOCK_UNLOCKED;
  90. #define PEER_MAX_CLEANUP_WORK 30
  91. static void peer_check_expire(unsigned long dummy);
  92. static struct timer_list peer_periodic_timer =
  93. { { NULL, NULL }, 0, 0, &peer_check_expire };
  94. /* Exported for sysctl_net_ipv4.  */
  95. int inet_peer_gc_mintime = 10 * HZ,
  96.     inet_peer_gc_maxtime = 120 * HZ;
  97. /* Called from ip_output.c:ip_init  */
  98. void __init inet_initpeers(void)
  99. {
  100. struct sysinfo si;
  101. /* Use the straight interface to information about memory. */
  102. si_meminfo(&si);
  103. /* The values below were suggested by Alexey Kuznetsov
  104.  * <kuznet@ms2.inr.ac.ru>.  I don't have any opinion about the values
  105.  * myself.  --SAW
  106.  */
  107. if (si.totalram <= (32768*1024)/PAGE_SIZE)
  108. inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
  109. if (si.totalram <= (16384*1024)/PAGE_SIZE)
  110. inet_peer_threshold >>= 1; /* about 512KB */
  111. if (si.totalram <= (8192*1024)/PAGE_SIZE)
  112. inet_peer_threshold >>= 2; /* about 128KB */
  113. peer_cachep = kmem_cache_create("inet_peer_cache",
  114. sizeof(struct inet_peer),
  115. 0, SLAB_HWCACHE_ALIGN,
  116. NULL, NULL);
  117. /* All the timers, started at system startup tend
  118.    to synchronize. Perturb it a bit.
  119.  */
  120. peer_periodic_timer.expires = jiffies
  121. + net_random() % inet_peer_gc_maxtime
  122. + inet_peer_gc_maxtime;
  123. add_timer(&peer_periodic_timer);
  124. }
  125. /* Called with or without local BH being disabled. */
  126. static void unlink_from_unused(struct inet_peer *p)
  127. {
  128. spin_lock_bh(&inet_peer_unused_lock);
  129. if (p->unused_prevp != NULL) {
  130. /* On unused list. */
  131. *p->unused_prevp = p->unused_next;
  132. if (p->unused_next != NULL)
  133. p->unused_next->unused_prevp = p->unused_prevp;
  134. else
  135. inet_peer_unused_tailp = p->unused_prevp;
  136. p->unused_prevp = NULL; /* mark it as removed */
  137. }
  138. spin_unlock_bh(&inet_peer_unused_lock);
  139. }
  140. /* Called with local BH disabled and the pool lock held. */
  141. #define lookup(daddr) 
  142. ({
  143. struct inet_peer *u, **v;
  144. stackptr = stack;
  145. *stackptr++ = &peer_root;
  146. for (u = peer_root; u != peer_avl_empty; ) {
  147. if (daddr == u->v4daddr)
  148. break;
  149. if (daddr < u->v4daddr)
  150. v = &u->avl_left;
  151. else
  152. v = &u->avl_right;
  153. *stackptr++ = v;
  154. u = *v;
  155. }
  156. u;
  157. })
  158. /* Called with local BH disabled and the pool write lock held. */
  159. #define lookup_rightempty(start)
  160. ({
  161. struct inet_peer *u, **v;
  162. *stackptr++ = &start->avl_left;
  163. v = &start->avl_left;
  164. for (u = *v; u->avl_right != peer_avl_empty; ) {
  165. v = &u->avl_right;
  166. *stackptr++ = v;
  167. u = *v;
  168. }
  169. u;
  170. })
  171. /* Called with local BH disabled and the pool write lock held.
  172.  * Variable names are the proof of operation correctness.
  173.  * Look into mm/map_avl.c for more detail description of the ideas.  */
  174. static void peer_avl_rebalance(struct inet_peer **stack[],
  175. struct inet_peer ***stackend)
  176. {
  177. struct inet_peer **nodep, *node, *l, *r;
  178. int lh, rh;
  179. while (stackend > stack) {
  180. nodep = *--stackend;
  181. node = *nodep;
  182. l = node->avl_left;
  183. r = node->avl_right;
  184. lh = node_height(l);
  185. rh = node_height(r);
  186. if (lh > rh + 1) { /* l: RH+2 */
  187. struct inet_peer *ll, *lr, *lrl, *lrr;
  188. int lrh;
  189. ll = l->avl_left;
  190. lr = l->avl_right;
  191. lrh = node_height(lr);
  192. if (lrh <= node_height(ll)) { /* ll: RH+1 */
  193. node->avl_left = lr; /* lr: RH or RH+1 */
  194. node->avl_right = r; /* r: RH */
  195. node->avl_height = lrh + 1; /* RH+1 or RH+2 */
  196. l->avl_left = ll; /* ll: RH+1 */
  197. l->avl_right = node; /* node: RH+1 or RH+2 */
  198. l->avl_height = node->avl_height + 1;
  199. *nodep = l;
  200. } else { /* ll: RH, lr: RH+1 */
  201. lrl = lr->avl_left; /* lrl: RH or RH-1 */
  202. lrr = lr->avl_right; /* lrr: RH or RH-1 */
  203. node->avl_left = lrr; /* lrr: RH or RH-1 */
  204. node->avl_right = r; /* r: RH */
  205. node->avl_height = rh + 1; /* node: RH+1 */
  206. l->avl_left = ll; /* ll: RH */
  207. l->avl_right = lrl; /* lrl: RH or RH-1 */
  208. l->avl_height = rh + 1; /* l: RH+1 */
  209. lr->avl_left = l; /* l: RH+1 */
  210. lr->avl_right = node; /* node: RH+1 */
  211. lr->avl_height = rh + 2;
  212. *nodep = lr;
  213. }
  214. } else if (rh > lh + 1) { /* r: LH+2 */
  215. struct inet_peer *rr, *rl, *rlr, *rll;
  216. int rlh;
  217. rr = r->avl_right;
  218. rl = r->avl_left;
  219. rlh = node_height(rl);
  220. if (rlh <= node_height(rr)) { /* rr: LH+1 */
  221. node->avl_right = rl; /* rl: LH or LH+1 */
  222. node->avl_left = l; /* l: LH */
  223. node->avl_height = rlh + 1; /* LH+1 or LH+2 */
  224. r->avl_right = rr; /* rr: LH+1 */
  225. r->avl_left = node; /* node: LH+1 or LH+2 */
  226. r->avl_height = node->avl_height + 1;
  227. *nodep = r;
  228. } else { /* rr: RH, rl: RH+1 */
  229. rlr = rl->avl_right; /* rlr: LH or LH-1 */
  230. rll = rl->avl_left; /* rll: LH or LH-1 */
  231. node->avl_right = rll; /* rll: LH or LH-1 */
  232. node->avl_left = l; /* l: LH */
  233. node->avl_height = lh + 1; /* node: LH+1 */
  234. r->avl_right = rr; /* rr: LH */
  235. r->avl_left = rlr; /* rlr: LH or LH-1 */
  236. r->avl_height = lh + 1; /* r: LH+1 */
  237. rl->avl_right = r; /* r: LH+1 */
  238. rl->avl_left = node; /* node: LH+1 */
  239. rl->avl_height = lh + 2;
  240. *nodep = rl;
  241. }
  242. } else {
  243. node->avl_height = (lh > rh ? lh : rh) + 1;
  244. }
  245. }
  246. }
  247. /* Called with local BH disabled and the pool write lock held. */
  248. #define link_to_pool(n)
  249. do {
  250. n->avl_height = 1;
  251. n->avl_left = peer_avl_empty;
  252. n->avl_right = peer_avl_empty;
  253. **--stackptr = n;
  254. peer_avl_rebalance(stack, stackptr);
  255. } while(0)
  256. /* May be called with local BH enabled. */
  257. static void unlink_from_pool(struct inet_peer *p)
  258. {
  259. int do_free;
  260. do_free = 0;
  261. write_lock_bh(&peer_pool_lock);
  262. /* Check the reference counter.  It was artificially incremented by 1
  263.  * in cleanup() function to prevent sudden disappearing.  If the
  264.  * reference count is still 1 then the node is referenced only as `p'
  265.  * here and from the pool.  So under the exclusive pool lock it's safe
  266.  * to remove the node and free it later. */
  267. if (atomic_read(&p->refcnt) == 1) {
  268. struct inet_peer **stack[PEER_MAXDEPTH];
  269. struct inet_peer ***stackptr, ***delp;
  270. if (lookup(p->v4daddr) != p)
  271. BUG();
  272. delp = stackptr - 1; /* *delp[0] == p */
  273. if (p->avl_left == peer_avl_empty) {
  274. *delp[0] = p->avl_right;
  275. --stackptr;
  276. } else {
  277. /* look for a node to insert instead of p */
  278. struct inet_peer *t;
  279. t = lookup_rightempty(p);
  280. if (*stackptr[-1] != t)
  281. BUG();
  282. **--stackptr = t->avl_left;
  283. /* t is removed, t->v4daddr > x->v4daddr for any
  284.  * x in p->avl_left subtree.
  285.  * Put t in the old place of p. */
  286. *delp[0] = t;
  287. t->avl_left = p->avl_left;
  288. t->avl_right = p->avl_right;
  289. t->avl_height = p->avl_height;
  290. if (delp[1] != &p->avl_left)
  291. BUG();
  292. delp[1] = &t->avl_left; /* was &p->avl_left */
  293. }
  294. peer_avl_rebalance(stack, stackptr);
  295. peer_total--;
  296. do_free = 1;
  297. }
  298. write_unlock_bh(&peer_pool_lock);
  299. if (do_free)
  300. kmem_cache_free(peer_cachep, p);
  301. else
  302. /* The node is used again.  Decrease the reference counter
  303.  * back.  The loop "cleanup -> unlink_from_unused
  304.  *   -> unlink_from_pool -> putpeer -> link_to_unused
  305.  *   -> cleanup (for the same node)"
  306.  * doesn't really exist because the entry will have a
  307.  * recent deletion time and will not be cleaned again soon. */
  308. inet_putpeer(p);
  309. }
  310. /* May be called with local BH enabled. */
  311. static int cleanup_once(unsigned long ttl)
  312. {
  313. struct inet_peer *p;
  314. /* Remove the first entry from the list of unused nodes. */
  315. spin_lock_bh(&inet_peer_unused_lock);
  316. p = inet_peer_unused_head;
  317. if (p != NULL) {
  318. if (time_after(p->dtime + ttl, jiffies)) {
  319. /* Do not prune fresh entries. */
  320. spin_unlock_bh(&inet_peer_unused_lock);
  321. return -1;
  322. }
  323. inet_peer_unused_head = p->unused_next;
  324. if (p->unused_next != NULL)
  325. p->unused_next->unused_prevp = p->unused_prevp;
  326. else
  327. inet_peer_unused_tailp = p->unused_prevp;
  328. p->unused_prevp = NULL; /* mark as not on the list */
  329. /* Grab an extra reference to prevent node disappearing
  330.  * before unlink_from_pool() call. */
  331. atomic_inc(&p->refcnt);
  332. }
  333. spin_unlock_bh(&inet_peer_unused_lock);
  334. if (p == NULL)
  335. /* It means that the total number of USED entries has
  336.  * grown over inet_peer_threshold.  It shouldn't really
  337.  * happen because of entry limits in route cache. */
  338. return -1;
  339. unlink_from_pool(p);
  340. return 0;
  341. }
  342. /* Called with or without local BH being disabled. */
  343. struct inet_peer *inet_getpeer(__u32 daddr, int create)
  344. {
  345. struct inet_peer *p, *n;
  346. struct inet_peer **stack[PEER_MAXDEPTH], ***stackptr;
  347. /* Look up for the address quickly. */
  348. read_lock_bh(&peer_pool_lock);
  349. p = lookup(daddr);
  350. if (p != peer_avl_empty)
  351. atomic_inc(&p->refcnt);
  352. read_unlock_bh(&peer_pool_lock);
  353. if (p != peer_avl_empty) {
  354. /* The existing node has been found. */
  355. /* Remove the entry from unused list if it was there. */
  356. unlink_from_unused(p);
  357. return p;
  358. }
  359. if (!create)
  360. return NULL;
  361. /* Allocate the space outside the locked region. */
  362. n = kmem_cache_alloc(peer_cachep, GFP_ATOMIC);
  363. if (n == NULL)
  364. return NULL;
  365. n->v4daddr = daddr;
  366. atomic_set(&n->refcnt, 1);
  367. n->ip_id_count = secure_ip_id(daddr);
  368. n->tcp_ts_stamp = 0;
  369. write_lock_bh(&peer_pool_lock);
  370. /* Check if an entry has suddenly appeared. */
  371. p = lookup(daddr);
  372. if (p != peer_avl_empty)
  373. goto out_free;
  374. /* Link the node. */
  375. link_to_pool(n);
  376. n->unused_prevp = NULL; /* not on the list */
  377. peer_total++;
  378. write_unlock_bh(&peer_pool_lock);
  379. if (peer_total >= inet_peer_threshold)
  380. /* Remove one less-recently-used entry. */
  381. cleanup_once(0);
  382. return n;
  383. out_free:
  384. /* The appropriate node is already in the pool. */
  385. atomic_inc(&p->refcnt);
  386. write_unlock_bh(&peer_pool_lock);
  387. /* Remove the entry from unused list if it was there. */
  388. unlink_from_unused(p);
  389. /* Free preallocated the preallocated node. */
  390. kmem_cache_free(peer_cachep, n);
  391. return p;
  392. }
  393. /* Called with local BH disabled. */
  394. static void peer_check_expire(unsigned long dummy)
  395. {
  396. int i;
  397. int ttl;
  398. if (peer_total >= inet_peer_threshold)
  399. ttl = inet_peer_minttl;
  400. else
  401. ttl = inet_peer_maxttl
  402. - (inet_peer_maxttl - inet_peer_minttl) / HZ *
  403. peer_total / inet_peer_threshold * HZ;
  404. for (i = 0; i < PEER_MAX_CLEANUP_WORK && !cleanup_once(ttl); i++);
  405. /* Trigger the timer after inet_peer_gc_mintime .. inet_peer_gc_maxtime
  406.  * interval depending on the total number of entries (more entries,
  407.  * less interval). */
  408. peer_periodic_timer.expires = jiffies
  409. + inet_peer_gc_maxtime
  410. - (inet_peer_gc_maxtime - inet_peer_gc_mintime) / HZ *
  411. peer_total / inet_peer_threshold * HZ;
  412. add_timer(&peer_periodic_timer);
  413. }