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, tcp_ts, tcp_ts_stamp
  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. __u32 tcp_ts;
  51. __u32 tcp_ts_stamp;
  52. };
  53. struct rcu_head rcu;
  54. struct inet_peer *gc_next;
  55. };
  56. /* following fields might be frequently dirtied */
  57. __u32 dtime; /* the time of last use of not referenced entries */
  58. atomic_t refcnt;
  59. };
  60. struct inet_peer_base {
  61. struct inet_peer __rcu *root;
  62. seqlock_t lock;
  63. int total;
  64. };
  65. #define INETPEER_BASE_BIT 0x1UL
  66. static inline struct inet_peer *inetpeer_ptr(unsigned long val)
  67. {
  68. BUG_ON(val & INETPEER_BASE_BIT);
  69. return (struct inet_peer *) val;
  70. }
  71. static inline struct inet_peer_base *inetpeer_base_ptr(unsigned long val)
  72. {
  73. if (!(val & INETPEER_BASE_BIT))
  74. return NULL;
  75. val &= ~INETPEER_BASE_BIT;
  76. return (struct inet_peer_base *) val;
  77. }
  78. static inline bool inetpeer_ptr_is_peer(unsigned long val)
  79. {
  80. return !(val & INETPEER_BASE_BIT);
  81. }
  82. static inline void __inetpeer_ptr_set_peer(unsigned long *val, struct inet_peer *peer)
  83. {
  84. /* This implicitly clears INETPEER_BASE_BIT */
  85. *val = (unsigned long) peer;
  86. }
  87. static inline bool inetpeer_ptr_set_peer(unsigned long *ptr, struct inet_peer *peer)
  88. {
  89. unsigned long val = (unsigned long) peer;
  90. unsigned long orig = *ptr;
  91. if (!(orig & INETPEER_BASE_BIT) || !val ||
  92. cmpxchg(ptr, orig, val) != orig)
  93. return false;
  94. return true;
  95. }
  96. static inline void inetpeer_init_ptr(unsigned long *ptr, struct inet_peer_base *base)
  97. {
  98. *ptr = (unsigned long) base | INETPEER_BASE_BIT;
  99. }
  100. static inline void inetpeer_transfer_peer(unsigned long *to, unsigned long *from)
  101. {
  102. unsigned long val = *from;
  103. *to = val;
  104. if (inetpeer_ptr_is_peer(val)) {
  105. struct inet_peer *peer = inetpeer_ptr(val);
  106. atomic_inc(&peer->refcnt);
  107. }
  108. }
  109. extern void inet_peer_base_init(struct inet_peer_base *);
  110. void inet_initpeers(void) __init;
  111. #define INETPEER_METRICS_NEW (~(u32) 0)
  112. static inline bool inet_metrics_new(const struct inet_peer *p)
  113. {
  114. return p->metrics[RTAX_LOCK-1] == INETPEER_METRICS_NEW;
  115. }
  116. /* can be called with or without local BH being disabled */
  117. struct inet_peer *inet_getpeer(struct inet_peer_base *base,
  118. const struct inetpeer_addr *daddr,
  119. int create);
  120. static inline struct inet_peer *inet_getpeer_v4(struct inet_peer_base *base,
  121. __be32 v4daddr,
  122. int create)
  123. {
  124. struct inetpeer_addr daddr;
  125. daddr.addr.a4 = v4daddr;
  126. daddr.family = AF_INET;
  127. return inet_getpeer(base, &daddr, create);
  128. }
  129. static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
  130. const struct in6_addr *v6daddr,
  131. int create)
  132. {
  133. struct inetpeer_addr daddr;
  134. *(struct in6_addr *)daddr.addr.a6 = *v6daddr;
  135. daddr.family = AF_INET6;
  136. return inet_getpeer(base, &daddr, create);
  137. }
  138. /* can be called from BH context or outside */
  139. extern void inet_putpeer(struct inet_peer *p);
  140. extern bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
  141. extern void inetpeer_invalidate_tree(struct inet_peer_base *);
  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 */