dst.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/config.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 dst_entry *next;
  34. atomic_t __refcnt; /* client references */
  35. int __use;
  36. struct dst_entry *child;
  37. struct net_device *dev;
  38. short error;
  39. short obsolete;
  40. int flags;
  41. #define DST_HOST 1
  42. #define DST_NOXFRM 2
  43. #define DST_NOPOLICY 4
  44. #define DST_NOHASH 8
  45. #define DST_BALANCED 0x10
  46. unsigned long lastuse;
  47. unsigned long expires;
  48. unsigned short header_len; /* more space at head required */
  49. unsigned short trailer_len; /* space to reserve at tail */
  50. u32 metrics[RTAX_MAX];
  51. struct dst_entry *path;
  52. unsigned long rate_last; /* rate limiting for ICMP */
  53. unsigned long rate_tokens;
  54. struct neighbour *neighbour;
  55. struct hh_cache *hh;
  56. struct xfrm_state *xfrm;
  57. int (*input)(struct sk_buff*);
  58. int (*output)(struct sk_buff*);
  59. #ifdef CONFIG_NET_CLS_ROUTE
  60. __u32 tclassid;
  61. #endif
  62. struct dst_ops *ops;
  63. struct rcu_head rcu_head;
  64. char info[0];
  65. };
  66. struct dst_ops
  67. {
  68. unsigned short family;
  69. unsigned short protocol;
  70. unsigned gc_thresh;
  71. int (*gc)(void);
  72. struct dst_entry * (*check)(struct dst_entry *, __u32 cookie);
  73. void (*destroy)(struct dst_entry *);
  74. void (*ifdown)(struct dst_entry *,
  75. struct net_device *dev, int how);
  76. struct dst_entry * (*negative_advice)(struct dst_entry *);
  77. void (*link_failure)(struct sk_buff *);
  78. void (*update_pmtu)(struct dst_entry *dst, u32 mtu);
  79. int entry_size;
  80. atomic_t entries;
  81. kmem_cache_t *kmem_cachep;
  82. };
  83. #ifdef __KERNEL__
  84. static inline u32
  85. dst_metric(const struct dst_entry *dst, int metric)
  86. {
  87. return dst->metrics[metric-1];
  88. }
  89. static inline u32 dst_mtu(const struct dst_entry *dst)
  90. {
  91. u32 mtu = dst_metric(dst, RTAX_MTU);
  92. /*
  93. * Alexey put it here, so ask him about it :)
  94. */
  95. barrier();
  96. return mtu;
  97. }
  98. static inline u32
  99. dst_allfrag(const struct dst_entry *dst)
  100. {
  101. int ret = dst_metric(dst, RTAX_FEATURES) & RTAX_FEATURE_ALLFRAG;
  102. /* Yes, _exactly_. This is paranoia. */
  103. barrier();
  104. return ret;
  105. }
  106. static inline int
  107. dst_metric_locked(struct dst_entry *dst, int metric)
  108. {
  109. return dst_metric(dst, RTAX_LOCK) & (1<<metric);
  110. }
  111. static inline void dst_hold(struct dst_entry * dst)
  112. {
  113. atomic_inc(&dst->__refcnt);
  114. }
  115. static inline
  116. struct dst_entry * dst_clone(struct dst_entry * dst)
  117. {
  118. if (dst)
  119. atomic_inc(&dst->__refcnt);
  120. return dst;
  121. }
  122. static inline
  123. void dst_release(struct dst_entry * dst)
  124. {
  125. if (dst) {
  126. WARN_ON(atomic_read(&dst->__refcnt) < 1);
  127. smp_mb__before_atomic_dec();
  128. atomic_dec(&dst->__refcnt);
  129. }
  130. }
  131. /* Children define the path of the packet through the
  132. * Linux networking. Thus, destinations are stackable.
  133. */
  134. static inline struct dst_entry *dst_pop(struct dst_entry *dst)
  135. {
  136. struct dst_entry *child = dst_clone(dst->child);
  137. dst_release(dst);
  138. return child;
  139. }
  140. extern void * dst_alloc(struct dst_ops * ops);
  141. extern void __dst_free(struct dst_entry * dst);
  142. extern struct dst_entry *dst_destroy(struct dst_entry * dst);
  143. static inline void dst_free(struct dst_entry * dst)
  144. {
  145. if (dst->obsolete > 1)
  146. return;
  147. if (!atomic_read(&dst->__refcnt)) {
  148. dst = dst_destroy(dst);
  149. if (!dst)
  150. return;
  151. }
  152. __dst_free(dst);
  153. }
  154. static inline void dst_rcu_free(struct rcu_head *head)
  155. {
  156. struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
  157. dst_free(dst);
  158. }
  159. static inline void dst_confirm(struct dst_entry *dst)
  160. {
  161. if (dst)
  162. neigh_confirm(dst->neighbour);
  163. }
  164. static inline void dst_negative_advice(struct dst_entry **dst_p)
  165. {
  166. struct dst_entry * dst = *dst_p;
  167. if (dst && dst->ops->negative_advice)
  168. *dst_p = dst->ops->negative_advice(dst);
  169. }
  170. static inline void dst_link_failure(struct sk_buff *skb)
  171. {
  172. struct dst_entry * dst = skb->dst;
  173. if (dst && dst->ops && dst->ops->link_failure)
  174. dst->ops->link_failure(skb);
  175. }
  176. static inline void dst_set_expires(struct dst_entry *dst, int timeout)
  177. {
  178. unsigned long expires = jiffies + timeout;
  179. if (expires == 0)
  180. expires = 1;
  181. if (dst->expires == 0 || time_before(expires, dst->expires))
  182. dst->expires = expires;
  183. }
  184. /* Output packet to network from transport. */
  185. static inline int dst_output(struct sk_buff *skb)
  186. {
  187. int err;
  188. for (;;) {
  189. err = skb->dst->output(skb);
  190. if (likely(err == 0))
  191. return err;
  192. if (unlikely(err != NET_XMIT_BYPASS))
  193. return err;
  194. }
  195. }
  196. /* Input packet from network to transport. */
  197. static inline int dst_input(struct sk_buff *skb)
  198. {
  199. int err;
  200. for (;;) {
  201. err = skb->dst->input(skb);
  202. if (likely(err == 0))
  203. return err;
  204. /* Oh, Jamal... Seems, I will not forgive you this mess. :-) */
  205. if (unlikely(err != NET_XMIT_BYPASS))
  206. return err;
  207. }
  208. }
  209. static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
  210. {
  211. if (dst->obsolete)
  212. dst = dst->ops->check(dst, cookie);
  213. return dst;
  214. }
  215. extern void dst_init(void);
  216. struct flowi;
  217. #ifndef CONFIG_XFRM
  218. static inline int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  219. struct sock *sk, int flags)
  220. {
  221. return 0;
  222. }
  223. #else
  224. extern int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  225. struct sock *sk, int flags);
  226. #endif
  227. #endif
  228. #endif /* _NET_DST_H */