inetpeer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, 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. __u32 tcp_ts;
  50. __u32 tcp_ts_stamp;
  51. };
  52. struct rcu_head rcu;
  53. struct inet_peer *gc_next;
  54. };
  55. /* following fields might be frequently dirtied */
  56. __u32 dtime; /* the time of last use of not referenced entries */
  57. atomic_t refcnt;
  58. };
  59. void inet_initpeers(void) __init;
  60. #define INETPEER_METRICS_NEW (~(u32) 0)
  61. static inline bool inet_metrics_new(const struct inet_peer *p)
  62. {
  63. return p->metrics[RTAX_LOCK-1] == INETPEER_METRICS_NEW;
  64. }
  65. /* can be called with or without local BH being disabled */
  66. struct inet_peer *inet_getpeer(const struct inetpeer_addr *daddr, int create);
  67. static inline struct inet_peer *inet_getpeer_v4(__be32 v4daddr, int create)
  68. {
  69. struct inetpeer_addr daddr;
  70. daddr.addr.a4 = v4daddr;
  71. daddr.family = AF_INET;
  72. return inet_getpeer(&daddr, create);
  73. }
  74. static inline struct inet_peer *inet_getpeer_v6(const struct in6_addr *v6daddr, int create)
  75. {
  76. struct inetpeer_addr daddr;
  77. *(struct in6_addr *)daddr.addr.a6 = *v6daddr;
  78. daddr.family = AF_INET6;
  79. return inet_getpeer(&daddr, create);
  80. }
  81. /* can be called from BH context or outside */
  82. extern void inet_putpeer(struct inet_peer *p);
  83. extern bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
  84. extern void inetpeer_invalidate_tree(int family);
  85. /*
  86. * temporary check to make sure we dont access rid, tcp_ts,
  87. * tcp_ts_stamp if no refcount is taken on inet_peer
  88. */
  89. static inline void inet_peer_refcheck(const struct inet_peer *p)
  90. {
  91. WARN_ON_ONCE(atomic_read(&p->refcnt) <= 0);
  92. }
  93. #endif /* _NET_INETPEER_H */