inet_fragment.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * inet fragments management
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Pavel Emelyanov <xemul@openvz.org>
  10. * Started as consolidation of ipv4/ip_fragment.c,
  11. * ipv6/reassembly. and ipv6 nf conntrack reassembly
  12. */
  13. #include <linux/list.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/module.h>
  16. #include <linux/timer.h>
  17. #include <linux/mm.h>
  18. #include <linux/random.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/rtnetlink.h>
  21. #include <net/inet_frag.h>
  22. static void inet_frag_secret_rebuild(unsigned long dummy)
  23. {
  24. struct inet_frags *f = (struct inet_frags *)dummy;
  25. unsigned long now = jiffies;
  26. int i;
  27. write_lock(&f->lock);
  28. get_random_bytes(&f->rnd, sizeof(u32));
  29. for (i = 0; i < INETFRAGS_HASHSZ; i++) {
  30. struct inet_frag_queue *q;
  31. struct hlist_node *p, *n;
  32. hlist_for_each_entry_safe(q, p, n, &f->hash[i], list) {
  33. unsigned int hval = f->hashfn(q);
  34. if (hval != i) {
  35. hlist_del(&q->list);
  36. /* Relink to new hash chain. */
  37. hlist_add_head(&q->list, &f->hash[hval]);
  38. }
  39. }
  40. }
  41. write_unlock(&f->lock);
  42. mod_timer(&f->secret_timer, now + f->ctl->secret_interval);
  43. }
  44. void inet_frags_init(struct inet_frags *f)
  45. {
  46. int i;
  47. for (i = 0; i < INETFRAGS_HASHSZ; i++)
  48. INIT_HLIST_HEAD(&f->hash[i]);
  49. INIT_LIST_HEAD(&f->lru_list);
  50. rwlock_init(&f->lock);
  51. f->rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
  52. (jiffies ^ (jiffies >> 6)));
  53. f->nqueues = 0;
  54. atomic_set(&f->mem, 0);
  55. init_timer(&f->secret_timer);
  56. f->secret_timer.function = inet_frag_secret_rebuild;
  57. f->secret_timer.data = (unsigned long)f;
  58. f->secret_timer.expires = jiffies + f->ctl->secret_interval;
  59. add_timer(&f->secret_timer);
  60. }
  61. EXPORT_SYMBOL(inet_frags_init);
  62. void inet_frags_fini(struct inet_frags *f)
  63. {
  64. del_timer(&f->secret_timer);
  65. }
  66. EXPORT_SYMBOL(inet_frags_fini);
  67. static inline void fq_unlink(struct inet_frag_queue *fq, struct inet_frags *f)
  68. {
  69. write_lock(&f->lock);
  70. hlist_del(&fq->list);
  71. list_del(&fq->lru_list);
  72. f->nqueues--;
  73. write_unlock(&f->lock);
  74. }
  75. void inet_frag_kill(struct inet_frag_queue *fq, struct inet_frags *f)
  76. {
  77. if (del_timer(&fq->timer))
  78. atomic_dec(&fq->refcnt);
  79. if (!(fq->last_in & COMPLETE)) {
  80. fq_unlink(fq, f);
  81. atomic_dec(&fq->refcnt);
  82. fq->last_in |= COMPLETE;
  83. }
  84. }
  85. EXPORT_SYMBOL(inet_frag_kill);
  86. static inline void frag_kfree_skb(struct inet_frags *f, struct sk_buff *skb,
  87. int *work)
  88. {
  89. if (work)
  90. *work -= skb->truesize;
  91. atomic_sub(skb->truesize, &f->mem);
  92. if (f->skb_free)
  93. f->skb_free(skb);
  94. kfree_skb(skb);
  95. }
  96. void inet_frag_destroy(struct inet_frag_queue *q, struct inet_frags *f,
  97. int *work)
  98. {
  99. struct sk_buff *fp;
  100. BUG_TRAP(q->last_in & COMPLETE);
  101. BUG_TRAP(del_timer(&q->timer) == 0);
  102. /* Release all fragment data. */
  103. fp = q->fragments;
  104. while (fp) {
  105. struct sk_buff *xp = fp->next;
  106. frag_kfree_skb(f, fp, work);
  107. fp = xp;
  108. }
  109. if (work)
  110. *work -= f->qsize;
  111. atomic_sub(f->qsize, &f->mem);
  112. f->destructor(q);
  113. }
  114. EXPORT_SYMBOL(inet_frag_destroy);
  115. int inet_frag_evictor(struct inet_frags *f)
  116. {
  117. struct inet_frag_queue *q;
  118. int work, evicted = 0;
  119. work = atomic_read(&f->mem) - f->ctl->low_thresh;
  120. while (work > 0) {
  121. read_lock(&f->lock);
  122. if (list_empty(&f->lru_list)) {
  123. read_unlock(&f->lock);
  124. break;
  125. }
  126. q = list_first_entry(&f->lru_list,
  127. struct inet_frag_queue, lru_list);
  128. atomic_inc(&q->refcnt);
  129. read_unlock(&f->lock);
  130. spin_lock(&q->lock);
  131. if (!(q->last_in & COMPLETE))
  132. inet_frag_kill(q, f);
  133. spin_unlock(&q->lock);
  134. if (atomic_dec_and_test(&q->refcnt))
  135. inet_frag_destroy(q, f, &work);
  136. evicted++;
  137. }
  138. return evicted;
  139. }
  140. EXPORT_SYMBOL(inet_frag_evictor);