PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/kernel/include/net/inetpeer.h

https://gitlab.com/abhishekr700/ASUS_ZenFone_ZE550KL
C Header | 186 lines | 133 code | 33 blank | 20 comment | 5 complexity | c95b15c2d5d016594ba52aa6d36333c8 MD5 | raw file
  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 fields
  39. * are not available: rid, ip_id_count
  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. atomic_t ip_id_count; /* IP ID for the next packet */
  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. u32 flush_seq;
  58. int total;
  59. };
  60. #define INETPEER_BASE_BIT 0x1UL
  61. static inline struct inet_peer *inetpeer_ptr(unsigned long val)
  62. {
  63. BUG_ON(val & INETPEER_BASE_BIT);
  64. return (struct inet_peer *) val;
  65. }
  66. static inline struct inet_peer_base *inetpeer_base_ptr(unsigned long val)
  67. {
  68. if (!(val & INETPEER_BASE_BIT))
  69. return NULL;
  70. val &= ~INETPEER_BASE_BIT;
  71. return (struct inet_peer_base *) val;
  72. }
  73. static inline bool inetpeer_ptr_is_peer(unsigned long val)
  74. {
  75. return !(val & INETPEER_BASE_BIT);
  76. }
  77. static inline void __inetpeer_ptr_set_peer(unsigned long *val, struct inet_peer *peer)
  78. {
  79. /* This implicitly clears INETPEER_BASE_BIT */
  80. *val = (unsigned long) peer;
  81. }
  82. static inline bool inetpeer_ptr_set_peer(unsigned long *ptr, struct inet_peer *peer)
  83. {
  84. unsigned long val = (unsigned long) peer;
  85. unsigned long orig = *ptr;
  86. if (!(orig & INETPEER_BASE_BIT) ||
  87. cmpxchg(ptr, orig, val) != orig)
  88. return false;
  89. return true;
  90. }
  91. static inline void inetpeer_init_ptr(unsigned long *ptr, struct inet_peer_base *base)
  92. {
  93. *ptr = (unsigned long) base | INETPEER_BASE_BIT;
  94. }
  95. static inline void inetpeer_transfer_peer(unsigned long *to, unsigned long *from)
  96. {
  97. unsigned long val = *from;
  98. *to = val;
  99. if (inetpeer_ptr_is_peer(val)) {
  100. struct inet_peer *peer = inetpeer_ptr(val);
  101. atomic_inc(&peer->refcnt);
  102. }
  103. }
  104. extern void inet_peer_base_init(struct inet_peer_base *);
  105. void inet_initpeers(void) __init;
  106. #define INETPEER_METRICS_NEW (~(u32) 0)
  107. static inline bool inet_metrics_new(const struct inet_peer *p)
  108. {
  109. return p->metrics[RTAX_LOCK-1] == INETPEER_METRICS_NEW;
  110. }
  111. /* can be called with or without local BH being disabled */
  112. struct inet_peer *inet_getpeer(struct inet_peer_base *base,
  113. const struct inetpeer_addr *daddr,
  114. int create);
  115. static inline struct inet_peer *inet_getpeer_v4(struct inet_peer_base *base,
  116. __be32 v4daddr,
  117. int create)
  118. {
  119. struct inetpeer_addr daddr;
  120. daddr.addr.a4 = v4daddr;
  121. daddr.family = AF_INET;
  122. return inet_getpeer(base, &daddr, create);
  123. }
  124. static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
  125. const struct in6_addr *v6daddr,
  126. int create)
  127. {
  128. struct inetpeer_addr daddr;
  129. *(struct in6_addr *)daddr.addr.a6 = *v6daddr;
  130. daddr.family = AF_INET6;
  131. return inet_getpeer(base, &daddr, create);
  132. }
  133. /* can be called from BH context or outside */
  134. extern void inet_putpeer(struct inet_peer *p);
  135. extern bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
  136. extern void inetpeer_invalidate_tree(struct inet_peer_base *);
  137. extern void inetpeer_invalidate_family(int family);
  138. /*
  139. * temporary check to make sure we dont access rid, ip_id_count, tcp_ts,
  140. * tcp_ts_stamp if no refcount is taken on inet_peer
  141. */
  142. static inline void inet_peer_refcheck(const struct inet_peer *p)
  143. {
  144. WARN_ON_ONCE(atomic_read(&p->refcnt) <= 0);
  145. }
  146. /* can be called with or without local BH being disabled */
  147. static inline int inet_getid(struct inet_peer *p, int more)
  148. {
  149. more++;
  150. inet_peer_refcheck(p);
  151. return atomic_add_return(more, &p->ip_id_count) - more;
  152. }
  153. #endif /* _NET_INETPEER_H */