inetpeer.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * INETPEER - A storage for permanent information about peers
  3. *
  4. * Authors: Andrey V. Savochkin <saw@msu.ru>
  5. */
  6. #ifndef _NET_INETPEER_H
  7. #define _NET_INETPEER_H
  8. #include <linux/types.h>
  9. #include <linux/init.h>
  10. #include <linux/jiffies.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/rtnetlink.h>
  13. #include <net/ipv6.h>
  14. #include <linux/atomic.h>
  15. struct inetpeer_addr_base {
  16. union {
  17. __be32 a4;
  18. __be32 a6[4];
  19. };
  20. };
  21. struct inetpeer_addr {
  22. struct inetpeer_addr_base addr;
  23. __u16 family;
  24. };
  25. struct inet_peer {
  26. /* group together avl_left,avl_right,v4daddr to speedup lookups */
  27. struct inet_peer __rcu *avl_left, *avl_right;
  28. struct inetpeer_addr daddr;
  29. __u32 avl_height;
  30. u32 metrics[RTAX_MAX];
  31. u32 rate_tokens; /* rate limiting for ICMP */
  32. unsigned long rate_last;
  33. unsigned long pmtu_expires;
  34. u32 pmtu_orig;
  35. u32 pmtu_learned;
  36. struct inetpeer_addr_base redirect_learned;
  37. union {
  38. struct list_head gc_list;
  39. struct rcu_head gc_rcu;
  40. };
  41. /*
  42. * Once inet_peer is queued for deletion (refcnt == -1), following fields
  43. * are not available: rid, ip_id_count
  44. * We can share memory with rcu_head to help keep inet_peer small.
  45. */
  46. union {
  47. struct {
  48. atomic_t rid; /* Frag reception counter */
  49. atomic_t ip_id_count; /* IP ID for the next packet */
  50. };
  51. struct rcu_head rcu;
  52. struct inet_peer *gc_next;
  53. };
  54. /* following fields might be frequently dirtied */
  55. __u32 dtime; /* the time of last use of not referenced entries */
  56. atomic_t refcnt;
  57. };
  58. struct inet_peer_base {
  59. struct inet_peer __rcu *root;
  60. seqlock_t lock;
  61. u32 flush_seq;
  62. int total;
  63. };
  64. #define INETPEER_BASE_BIT 0x1UL
  65. static inline struct inet_peer *inetpeer_ptr(unsigned long val)
  66. {
  67. BUG_ON(val & INETPEER_BASE_BIT);
  68. return (struct inet_peer *) val;
  69. }
  70. static inline struct inet_peer_base *inetpeer_base_ptr(unsigned long val)
  71. {
  72. if (!(val & INETPEER_BASE_BIT))
  73. return NULL;
  74. val &= ~INETPEER_BASE_BIT;
  75. return (struct inet_peer_base *) val;
  76. }
  77. static inline bool inetpeer_ptr_is_peer(unsigned long val)
  78. {
  79. return !(val & INETPEER_BASE_BIT);
  80. }
  81. static inline void __inetpeer_ptr_set_peer(unsigned long *val, struct inet_peer *peer)
  82. {
  83. /* This implicitly clears INETPEER_BASE_BIT */
  84. *val = (unsigned long) peer;
  85. }
  86. static inline bool inetpeer_ptr_set_peer(unsigned long *ptr, struct inet_peer *peer)
  87. {
  88. unsigned long val = (unsigned long) peer;
  89. unsigned long orig = *ptr;
  90. if (!(orig & INETPEER_BASE_BIT) ||
  91. cmpxchg(ptr, orig, val) != orig)
  92. return false;
  93. return true;
  94. }
  95. static inline void inetpeer_init_ptr(unsigned long *ptr, struct inet_peer_base *base)
  96. {
  97. *ptr = (unsigned long) base | INETPEER_BASE_BIT;
  98. }
  99. static inline void inetpeer_transfer_peer(unsigned long *to, unsigned long *from)
  100. {
  101. unsigned long val = *from;
  102. *to = val;
  103. if (inetpeer_ptr_is_peer(val)) {
  104. struct inet_peer *peer = inetpeer_ptr(val);
  105. atomic_inc(&peer->refcnt);
  106. }
  107. }
  108. extern void inet_peer_base_init(struct inet_peer_base *);
  109. void inet_initpeers(void) __init;
  110. #define INETPEER_METRICS_NEW (~(u32) 0)
  111. static inline bool inet_metrics_new(const struct inet_peer *p)
  112. {
  113. return p->metrics[RTAX_LOCK-1] == INETPEER_METRICS_NEW;
  114. }
  115. /* can be called with or without local BH being disabled */
  116. struct inet_peer *inet_getpeer(struct inet_peer_base *base,
  117. const struct inetpeer_addr *daddr,
  118. int create);
  119. static inline struct inet_peer *inet_getpeer_v4(struct inet_peer_base *base,
  120. __be32 v4daddr,
  121. int create)
  122. {
  123. struct inetpeer_addr daddr;
  124. daddr.addr.a4 = v4daddr;
  125. daddr.family = AF_INET;
  126. return inet_getpeer(base, &daddr, create);
  127. }
  128. static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
  129. const struct in6_addr *v6daddr,
  130. int create)
  131. {
  132. struct inetpeer_addr daddr;
  133. *(struct in6_addr *)daddr.addr.a6 = *v6daddr;
  134. daddr.family = AF_INET6;
  135. return inet_getpeer(base, &daddr, create);
  136. }
  137. /* can be called from BH context or outside */
  138. extern void inet_putpeer(struct inet_peer *p);
  139. extern bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
  140. extern void inetpeer_invalidate_tree(struct inet_peer_base *);
  141. extern void inetpeer_invalidate_family(int family);
  142. /*
  143. * temporary check to make sure we dont access rid, ip_id_count, tcp_ts,
  144. * tcp_ts_stamp if no refcount is taken on inet_peer
  145. */
  146. static inline void inet_peer_refcheck(const struct inet_peer *p)
  147. {
  148. WARN_ON_ONCE(atomic_read(&p->refcnt) <= 0);
  149. }
  150. /* can be called with or without local BH being disabled */
  151. static inline int inet_getid(struct inet_peer *p, int more)
  152. {
  153. int old, new;
  154. more++;
  155. inet_peer_refcheck(p);
  156. do {
  157. old = atomic_read(&p->ip_id_count);
  158. new = old + more;
  159. if (!new)
  160. new = 1;
  161. } while (atomic_cmpxchg(&p->ip_id_count, old, new) != old);
  162. return new;
  163. }
  164. #endif /* _NET_INETPEER_H */