dst.h 7.1 KB

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