inetpeer.h 4.0 KB

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