dst.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. struct xfrm_state *xfrm;
  52. int (*input)(struct sk_buff*);
  53. int (*output)(struct sk_buff*);
  54. struct dst_ops *ops;
  55. u32 metrics[RTAX_MAX];
  56. #ifdef CONFIG_NET_CLS_ROUTE
  57. __u32 tclassid;
  58. #endif
  59. /*
  60. * __refcnt wants to be on a different cache line from
  61. * input/output/ops or performance tanks badly
  62. */
  63. atomic_t __refcnt; /* client references */
  64. int __use;
  65. unsigned long lastuse;
  66. union {
  67. struct dst_entry *next;
  68. struct rtable *rt_next;
  69. struct rt6_info *rt6_next;
  70. struct dn_route *dn_next;
  71. };
  72. };
  73. struct dst_ops
  74. {
  75. unsigned short family;
  76. __be16 protocol;
  77. unsigned gc_thresh;
  78. int (*gc)(struct dst_ops *ops);
  79. struct dst_entry * (*check)(struct dst_entry *, __u32 cookie);
  80. void (*destroy)(struct dst_entry *);
  81. void (*ifdown)(struct dst_entry *,
  82. struct net_device *dev, int how);
  83. struct dst_entry * (*negative_advice)(struct dst_entry *);
  84. void (*link_failure)(struct sk_buff *);
  85. void (*update_pmtu)(struct dst_entry *dst, u32 mtu);
  86. int (*local_out)(struct sk_buff *skb);
  87. int entry_size;
  88. atomic_t entries;
  89. struct kmem_cache *kmem_cachep;
  90. struct net *dst_net;
  91. };
  92. #ifdef __KERNEL__
  93. static inline u32
  94. dst_metric(const struct dst_entry *dst, int metric)
  95. {
  96. return dst->metrics[metric-1];
  97. }
  98. static inline u32 dst_mtu(const struct dst_entry *dst)
  99. {
  100. u32 mtu = dst_metric(dst, RTAX_MTU);
  101. /*
  102. * Alexey put it here, so ask him about it :)
  103. */
  104. barrier();
  105. return mtu;
  106. }
  107. static inline u32
  108. dst_allfrag(const struct dst_entry *dst)
  109. {
  110. int ret = dst_metric(dst, RTAX_FEATURES) & RTAX_FEATURE_ALLFRAG;
  111. /* Yes, _exactly_. This is paranoia. */
  112. barrier();
  113. return ret;
  114. }
  115. static inline int
  116. dst_metric_locked(struct dst_entry *dst, int metric)
  117. {
  118. return dst_metric(dst, RTAX_LOCK) & (1<<metric);
  119. }
  120. static inline void dst_hold(struct dst_entry * dst)
  121. {
  122. atomic_inc(&dst->__refcnt);
  123. }
  124. static inline void dst_use(struct dst_entry *dst, unsigned long time)
  125. {
  126. dst_hold(dst);
  127. dst->__use++;
  128. dst->lastuse = time;
  129. }
  130. static inline
  131. struct dst_entry * dst_clone(struct dst_entry * dst)
  132. {
  133. if (dst)
  134. atomic_inc(&dst->__refcnt);
  135. return dst;
  136. }
  137. static inline
  138. void dst_release(struct dst_entry * dst)
  139. {
  140. if (dst) {
  141. WARN_ON(atomic_read(&dst->__refcnt) < 1);
  142. smp_mb__before_atomic_dec();
  143. atomic_dec(&dst->__refcnt);
  144. }
  145. }
  146. /* Children define the path of the packet through the
  147. * Linux networking. Thus, destinations are stackable.
  148. */
  149. static inline struct dst_entry *dst_pop(struct dst_entry *dst)
  150. {
  151. struct dst_entry *child = dst_clone(dst->child);
  152. dst_release(dst);
  153. return child;
  154. }
  155. extern int dst_discard(struct sk_buff *skb);
  156. extern void * dst_alloc(struct dst_ops * ops);
  157. extern void __dst_free(struct dst_entry * dst);
  158. extern struct dst_entry *dst_destroy(struct dst_entry * dst);
  159. static inline void dst_free(struct dst_entry * dst)
  160. {
  161. if (dst->obsolete > 1)
  162. return;
  163. if (!atomic_read(&dst->__refcnt)) {
  164. dst = dst_destroy(dst);
  165. if (!dst)
  166. return;
  167. }
  168. __dst_free(dst);
  169. }
  170. static inline void dst_rcu_free(struct rcu_head *head)
  171. {
  172. struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
  173. dst_free(dst);
  174. }
  175. static inline void dst_confirm(struct dst_entry *dst)
  176. {
  177. if (dst)
  178. neigh_confirm(dst->neighbour);
  179. }
  180. static inline void dst_negative_advice(struct dst_entry **dst_p)
  181. {
  182. struct dst_entry * dst = *dst_p;
  183. if (dst && dst->ops->negative_advice)
  184. *dst_p = dst->ops->negative_advice(dst);
  185. }
  186. static inline void dst_link_failure(struct sk_buff *skb)
  187. {
  188. struct dst_entry * dst = skb->dst;
  189. if (dst && dst->ops && dst->ops->link_failure)
  190. dst->ops->link_failure(skb);
  191. }
  192. static inline void dst_set_expires(struct dst_entry *dst, int timeout)
  193. {
  194. unsigned long expires = jiffies + timeout;
  195. if (expires == 0)
  196. expires = 1;
  197. if (dst->expires == 0 || time_before(expires, dst->expires))
  198. dst->expires = expires;
  199. }
  200. /* Output packet to network from transport. */
  201. static inline int dst_output(struct sk_buff *skb)
  202. {
  203. return skb->dst->output(skb);
  204. }
  205. /* Input packet from network to transport. */
  206. static inline int dst_input(struct sk_buff *skb)
  207. {
  208. int err;
  209. for (;;) {
  210. err = skb->dst->input(skb);
  211. if (likely(err == 0))
  212. return err;
  213. /* Oh, Jamal... Seems, I will not forgive you this mess. :-) */
  214. if (unlikely(err != NET_XMIT_BYPASS))
  215. return err;
  216. }
  217. }
  218. static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
  219. {
  220. if (dst->obsolete)
  221. dst = dst->ops->check(dst, cookie);
  222. return dst;
  223. }
  224. extern void dst_init(void);
  225. /* Flags for xfrm_lookup flags argument. */
  226. enum {
  227. XFRM_LOOKUP_WAIT = 1 << 0,
  228. XFRM_LOOKUP_ICMP = 1 << 1,
  229. };
  230. struct flowi;
  231. #ifndef CONFIG_XFRM
  232. static inline int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  233. struct sock *sk, int flags)
  234. {
  235. return 0;
  236. }
  237. static inline int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  238. struct sock *sk, int flags)
  239. {
  240. return 0;
  241. }
  242. #else
  243. extern int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  244. struct sock *sk, int flags);
  245. extern int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  246. struct sock *sk, int flags);
  247. #endif
  248. #endif
  249. #endif /* _NET_DST_H */