inetpeer.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <net/ipv6.h>
  13. #include <asm/atomic.h>
  14. struct inetpeer_addr {
  15. union {
  16. __be32 a4;
  17. __be32 a6[4];
  18. };
  19. __u16 family;
  20. };
  21. struct inet_peer {
  22. /* group together avl_left,avl_right,v4daddr to speedup lookups */
  23. struct inet_peer __rcu *avl_left, *avl_right;
  24. struct inetpeer_addr daddr;
  25. __u32 avl_height;
  26. struct list_head unused;
  27. __u32 dtime; /* the time of last use of not
  28. * referenced entries */
  29. atomic_t refcnt;
  30. /*
  31. * Once inet_peer is queued for deletion (refcnt == -1), following fields
  32. * are not available: rid, ip_id_count, tcp_ts, tcp_ts_stamp
  33. * We can share memory with rcu_head to keep inet_peer small
  34. * (less then 64 bytes)
  35. */
  36. union {
  37. struct {
  38. atomic_t rid; /* Frag reception counter */
  39. atomic_t ip_id_count; /* IP ID for the next packet */
  40. __u32 tcp_ts;
  41. __u32 tcp_ts_stamp;
  42. };
  43. struct rcu_head rcu;
  44. };
  45. };
  46. void inet_initpeers(void) __init;
  47. /* can be called with or without local BH being disabled */
  48. struct inet_peer *inet_getpeer(struct inetpeer_addr *daddr, int create);
  49. static inline struct inet_peer *inet_getpeer_v4(__be32 v4daddr, int create)
  50. {
  51. struct inetpeer_addr daddr;
  52. daddr.a4 = v4daddr;
  53. daddr.family = AF_INET;
  54. return inet_getpeer(&daddr, create);
  55. }
  56. static inline struct inet_peer *inet_getpeer_v6(struct in6_addr *v6daddr, int create)
  57. {
  58. struct inetpeer_addr daddr;
  59. ipv6_addr_copy((struct in6_addr *)daddr.a6, v6daddr);
  60. daddr.family = AF_INET6;
  61. return inet_getpeer(&daddr, create);
  62. }
  63. /* can be called from BH context or outside */
  64. extern void inet_putpeer(struct inet_peer *p);
  65. /*
  66. * temporary check to make sure we dont access rid, ip_id_count, tcp_ts,
  67. * tcp_ts_stamp if no refcount is taken on inet_peer
  68. */
  69. static inline void inet_peer_refcheck(const struct inet_peer *p)
  70. {
  71. WARN_ON_ONCE(atomic_read(&p->refcnt) <= 0);
  72. }
  73. /* can be called with or without local BH being disabled */
  74. static inline __u16 inet_getid(struct inet_peer *p, int more)
  75. {
  76. more++;
  77. inet_peer_refcheck(p);
  78. return atomic_add_return(more, &p->ip_id_count) - more;
  79. }
  80. #endif /* _NET_INETPEER_H */