dst.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * net/dst.h Protocol independent destination cache definitions.
  3. *
  4. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  5. *
  6. */
  7. #ifndef _NET_DST_H
  8. #define _NET_DST_H
  9. #include <net/dst_ops.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/rtnetlink.h>
  12. #include <linux/rcupdate.h>
  13. #include <linux/jiffies.h>
  14. #include <net/neighbour.h>
  15. #include <asm/processor.h>
  16. /*
  17. * 0 - no debugging messages
  18. * 1 - rare events and bugs (default)
  19. * 2 - trace mode.
  20. */
  21. #define RT_CACHE_DEBUG 0
  22. #define DST_GC_MIN (HZ/10)
  23. #define DST_GC_INC (HZ/2)
  24. #define DST_GC_MAX (120*HZ)
  25. /* Each dst_entry has reference count and sits in some parent list(s).
  26. * When it is removed from parent list, it is "freed" (dst_free).
  27. * After this it enters dead state (dst->obsolete > 0) and if its refcnt
  28. * is zero, it can be destroyed immediately, otherwise it is added
  29. * to gc list and garbage collector periodically checks the refcnt.
  30. */
  31. struct sk_buff;
  32. struct dst_entry
  33. {
  34. struct rcu_head rcu_head;
  35. struct dst_entry *child;
  36. struct net_device *dev;
  37. short error;
  38. short obsolete;
  39. int flags;
  40. #define DST_HOST 1
  41. #define DST_NOXFRM 2
  42. #define DST_NOPOLICY 4
  43. #define DST_NOHASH 8
  44. unsigned long expires;
  45. unsigned short header_len; /* more space at head required */
  46. unsigned short trailer_len; /* space to reserve at tail */
  47. unsigned int rate_tokens;
  48. unsigned long rate_last; /* rate limiting for ICMP */
  49. struct dst_entry *path;
  50. struct neighbour *neighbour;
  51. struct hh_cache *hh;
  52. #ifdef CONFIG_XFRM
  53. struct xfrm_state *xfrm;
  54. #else
  55. void *__pad1;
  56. #endif
  57. int (*input)(struct sk_buff*);
  58. int (*output)(struct sk_buff*);
  59. struct dst_ops *ops;
  60. u32 metrics[RTAX_MAX];
  61. #ifdef CONFIG_NET_CLS_ROUTE
  62. __u32 tclassid;
  63. #else
  64. __u32 __pad2;
  65. #endif
  66. /*
  67. * Align __refcnt to a 64 bytes alignment
  68. * (L1_CACHE_SIZE would be too much)
  69. */
  70. #ifdef CONFIG_64BIT
  71. long __pad_to_align_refcnt[2];
  72. #else
  73. long __pad_to_align_refcnt[1];
  74. #endif
  75. /*
  76. * __refcnt wants to be on a different cache line from
  77. * input/output/ops or performance tanks badly
  78. */
  79. atomic_t __refcnt; /* client references */
  80. int __use;
  81. unsigned long lastuse;
  82. union {
  83. struct dst_entry *next;
  84. struct rtable *rt_next;
  85. struct rt6_info *rt6_next;
  86. struct dn_route *dn_next;
  87. };
  88. };
  89. #ifdef __KERNEL__
  90. static inline u32
  91. dst_metric(const struct dst_entry *dst, int metric)
  92. {
  93. return dst->metrics[metric-1];
  94. }
  95. static inline u32 dst_mtu(const struct dst_entry *dst)
  96. {
  97. u32 mtu = dst_metric(dst, RTAX_MTU);
  98. /*
  99. * Alexey put it here, so ask him about it :)
  100. */
  101. barrier();
  102. return mtu;
  103. }
  104. /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
  105. static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
  106. {
  107. return msecs_to_jiffies(dst_metric(dst, metric));
  108. }
  109. static inline void set_dst_metric_rtt(struct dst_entry *dst, int metric,
  110. unsigned long rtt)
  111. {
  112. dst->metrics[metric-1] = jiffies_to_msecs(rtt);
  113. }
  114. static inline u32
  115. dst_allfrag(const struct dst_entry *dst)
  116. {
  117. int ret = dst_metric(dst, RTAX_FEATURES) & RTAX_FEATURE_ALLFRAG;
  118. /* Yes, _exactly_. This is paranoia. */
  119. barrier();
  120. return ret;
  121. }
  122. static inline int
  123. dst_metric_locked(struct dst_entry *dst, int metric)
  124. {
  125. return dst_metric(dst, RTAX_LOCK) & (1<<metric);
  126. }
  127. static inline void dst_hold(struct dst_entry * dst)
  128. {
  129. /*
  130. * If your kernel compilation stops here, please check
  131. * __pad_to_align_refcnt declaration in struct dst_entry
  132. */
  133. BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
  134. atomic_inc(&dst->__refcnt);
  135. }
  136. static inline void dst_use(struct dst_entry *dst, unsigned long time)
  137. {
  138. dst_hold(dst);
  139. dst->__use++;
  140. dst->lastuse = time;
  141. }
  142. static inline
  143. struct dst_entry * dst_clone(struct dst_entry * dst)
  144. {
  145. if (dst)
  146. atomic_inc(&dst->__refcnt);
  147. return dst;
  148. }
  149. extern void dst_release(struct dst_entry *dst);
  150. static inline void skb_dst_drop(struct sk_buff *skb)
  151. {
  152. if (skb->_skb_dst)
  153. dst_release(skb_dst(skb));
  154. skb->_skb_dst = 0UL;
  155. }
  156. /* Children define the path of the packet through the
  157. * Linux networking. Thus, destinations are stackable.
  158. */
  159. static inline struct dst_entry *dst_pop(struct dst_entry *dst)
  160. {
  161. struct dst_entry *child = dst_clone(dst->child);
  162. dst_release(dst);
  163. return child;
  164. }
  165. extern int dst_discard(struct sk_buff *skb);
  166. extern void * dst_alloc(struct dst_ops * ops);
  167. extern void __dst_free(struct dst_entry * dst);
  168. extern struct dst_entry *dst_destroy(struct dst_entry * dst);
  169. static inline void dst_free(struct dst_entry * dst)
  170. {
  171. if (dst->obsolete > 1)
  172. return;
  173. if (!atomic_read(&dst->__refcnt)) {
  174. dst = dst_destroy(dst);
  175. if (!dst)
  176. return;
  177. }
  178. __dst_free(dst);
  179. }
  180. static inline void dst_rcu_free(struct rcu_head *head)
  181. {
  182. struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
  183. dst_free(dst);
  184. }
  185. static inline void dst_confirm(struct dst_entry *dst)
  186. {
  187. if (dst)
  188. neigh_confirm(dst->neighbour);
  189. }
  190. static inline void dst_negative_advice(struct dst_entry **dst_p)
  191. {
  192. struct dst_entry * dst = *dst_p;
  193. if (dst && dst->ops->negative_advice)
  194. *dst_p = dst->ops->negative_advice(dst);
  195. }
  196. static inline void dst_link_failure(struct sk_buff *skb)
  197. {
  198. struct dst_entry *dst = skb_dst(skb);
  199. if (dst && dst->ops && dst->ops->link_failure)
  200. dst->ops->link_failure(skb);
  201. }
  202. static inline void dst_set_expires(struct dst_entry *dst, int timeout)
  203. {
  204. unsigned long expires = jiffies + timeout;
  205. if (expires == 0)
  206. expires = 1;
  207. if (dst->expires == 0 || time_before(expires, dst->expires))
  208. dst->expires = expires;
  209. }
  210. /* Output packet to network from transport. */
  211. static inline int dst_output(struct sk_buff *skb)
  212. {
  213. return skb_dst(skb)->output(skb);
  214. }
  215. /* Input packet from network to transport. */
  216. static inline int dst_input(struct sk_buff *skb)
  217. {
  218. return skb_dst(skb)->input(skb);
  219. }
  220. static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
  221. {
  222. if (dst->obsolete)
  223. dst = dst->ops->check(dst, cookie);
  224. return dst;
  225. }
  226. extern void dst_init(void);
  227. /* Flags for xfrm_lookup flags argument. */
  228. enum {
  229. XFRM_LOOKUP_WAIT = 1 << 0,
  230. XFRM_LOOKUP_ICMP = 1 << 1,
  231. };
  232. struct flowi;
  233. #ifndef CONFIG_XFRM
  234. static inline int xfrm_lookup(struct net *net, struct dst_entry **dst_p,
  235. struct flowi *fl, struct sock *sk, int flags)
  236. {
  237. return 0;
  238. }
  239. static inline int __xfrm_lookup(struct net *net, struct dst_entry **dst_p,
  240. struct flowi *fl, struct sock *sk, int flags)
  241. {
  242. return 0;
  243. }
  244. #else
  245. extern int xfrm_lookup(struct net *net, struct dst_entry **dst_p,
  246. struct flowi *fl, struct sock *sk, int flags);
  247. extern int __xfrm_lookup(struct net *net, struct dst_entry **dst_p,
  248. struct flowi *fl, struct sock *sk, int flags);
  249. #endif
  250. #endif
  251. #endif /* _NET_DST_H */