inetpeer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. struct in6_addr in6;
  20. };
  21. };
  22. struct inetpeer_addr {
  23. struct inetpeer_addr_base addr;
  24. __u16 family;
  25. };
  26. struct inet_peer {
  27. /* group together avl_left,avl_right,v4daddr to speedup lookups */
  28. struct inet_peer __rcu *avl_left, *avl_right;
  29. struct inetpeer_addr daddr;
  30. __u32 avl_height;
  31. u32 metrics[RTAX_MAX];
  32. u32 rate_tokens; /* rate limiting for ICMP */
  33. unsigned long rate_last;
  34. union {
  35. struct list_head gc_list;
  36. struct rcu_head gc_rcu;
  37. };
  38. /*
  39. * Once inet_peer is queued for deletion (refcnt == -1), following field
  40. * is not available: rid
  41. * We can share memory with rcu_head to help keep inet_peer small.
  42. */
  43. union {
  44. struct {
  45. atomic_t rid; /* Frag reception counter */
  46. };
  47. struct rcu_head rcu;
  48. struct inet_peer *gc_next;
  49. };
  50. /* following fields might be frequently dirtied */
  51. __u32 dtime; /* the time of last use of not referenced entries */
  52. atomic_t refcnt;
  53. };
  54. struct inet_peer_base {
  55. struct inet_peer __rcu *root;
  56. seqlock_t lock;
  57. int total;
  58. };
  59. #define INETPEER_BASE_BIT 0x1UL
  60. static inline struct inet_peer *inetpeer_ptr(unsigned long val)
  61. {
  62. BUG_ON(val & INETPEER_BASE_BIT);
  63. return (struct inet_peer *) val;
  64. }
  65. static inline struct inet_peer_base *inetpeer_base_ptr(unsigned long val)
  66. {
  67. if (!(val & INETPEER_BASE_BIT))
  68. return NULL;
  69. val &= ~INETPEER_BASE_BIT;
  70. return (struct inet_peer_base *) val;
  71. }
  72. static inline bool inetpeer_ptr_is_peer(unsigned long val)
  73. {
  74. return !(val & INETPEER_BASE_BIT);
  75. }
  76. static inline void __inetpeer_ptr_set_peer(unsigned long *val, struct inet_peer *peer)
  77. {
  78. /* This implicitly clears INETPEER_BASE_BIT */
  79. *val = (unsigned long) peer;
  80. }
  81. static inline bool inetpeer_ptr_set_peer(unsigned long *ptr, struct inet_peer *peer)
  82. {
  83. unsigned long val = (unsigned long) peer;
  84. unsigned long orig = *ptr;
  85. if (!(orig & INETPEER_BASE_BIT) ||
  86. cmpxchg(ptr, orig, val) != orig)
  87. return false;
  88. return true;
  89. }
  90. static inline void inetpeer_init_ptr(unsigned long *ptr, struct inet_peer_base *base)
  91. {
  92. *ptr = (unsigned long) base | INETPEER_BASE_BIT;
  93. }
  94. static inline void inetpeer_transfer_peer(unsigned long *to, unsigned long *from)
  95. {
  96. unsigned long val = *from;
  97. *to = val;
  98. if (inetpeer_ptr_is_peer(val)) {
  99. struct inet_peer *peer = inetpeer_ptr(val);
  100. atomic_inc(&peer->refcnt);
  101. }
  102. }
  103. void inet_peer_base_init(struct inet_peer_base *);
  104. void inet_initpeers(void) __init;
  105. #define INETPEER_METRICS_NEW (~(u32) 0)
  106. static inline bool inet_metrics_new(const struct inet_peer *p)
  107. {
  108. return p->metrics[RTAX_LOCK-1] == INETPEER_METRICS_NEW;
  109. }
  110. /* can be called with or without local BH being disabled */
  111. struct inet_peer *inet_getpeer(struct inet_peer_base *base,
  112. const struct inetpeer_addr *daddr,
  113. int create);
  114. static inline struct inet_peer *inet_getpeer_v4(struct inet_peer_base *base,
  115. __be32 v4daddr,
  116. int create)
  117. {
  118. struct inetpeer_addr daddr;
  119. daddr.addr.a4 = v4daddr;
  120. daddr.family = AF_INET;
  121. return inet_getpeer(base, &daddr, create);
  122. }
  123. static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
  124. const struct in6_addr *v6daddr,
  125. int create)
  126. {
  127. struct inetpeer_addr daddr;
  128. daddr.addr.in6 = *v6daddr;
  129. daddr.family = AF_INET6;
  130. return inet_getpeer(base, &daddr, create);
  131. }
  132. /* can be called from BH context or outside */
  133. void inet_putpeer(struct inet_peer *p);
  134. bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
  135. void inetpeer_invalidate_tree(struct inet_peer_base *);
  136. /*
  137. * temporary check to make sure we dont access rid, tcp_ts,
  138. * tcp_ts_stamp if no refcount is taken on inet_peer
  139. */
  140. static inline void inet_peer_refcheck(const struct inet_peer *p)
  141. {
  142. WARN_ON_ONCE(atomic_read(&p->refcnt) <= 0);
  143. }
  144. #endif /* _NET_INETPEER_H */