dst.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 (*get_mss)(struct dst_entry *dst, u32 mtu);
  80. int entry_size;
  81. atomic_t entries;
  82. kmem_cache_t *kmem_cachep;
  83. };
  84. #ifdef __KERNEL__
  85. static inline u32
  86. dst_metric(const struct dst_entry *dst, int metric)
  87. {
  88. return dst->metrics[metric-1];
  89. }
  90. static inline u32 dst_mtu(const struct dst_entry *dst)
  91. {
  92. u32 mtu = dst_metric(dst, RTAX_MTU);
  93. /*
  94. * Alexey put it here, so ask him about it :)
  95. */
  96. barrier();
  97. return mtu;
  98. }
  99. static inline u32
  100. dst_allfrag(const struct dst_entry *dst)
  101. {
  102. int ret = dst_metric(dst, RTAX_FEATURES) & RTAX_FEATURE_ALLFRAG;
  103. /* Yes, _exactly_. This is paranoia. */
  104. barrier();
  105. return ret;
  106. }
  107. static inline int
  108. dst_metric_locked(struct dst_entry *dst, int metric)
  109. {
  110. return dst_metric(dst, RTAX_LOCK) & (1<<metric);
  111. }
  112. static inline void dst_hold(struct dst_entry * dst)
  113. {
  114. atomic_inc(&dst->__refcnt);
  115. }
  116. static inline
  117. struct dst_entry * dst_clone(struct dst_entry * dst)
  118. {
  119. if (dst)
  120. atomic_inc(&dst->__refcnt);
  121. return dst;
  122. }
  123. static inline
  124. void dst_release(struct dst_entry * dst)
  125. {
  126. if (dst) {
  127. WARN_ON(atomic_read(&dst->__refcnt) < 1);
  128. smp_mb__before_atomic_dec();
  129. atomic_dec(&dst->__refcnt);
  130. }
  131. }
  132. /* Children define the path of the packet through the
  133. * Linux networking. Thus, destinations are stackable.
  134. */
  135. static inline struct dst_entry *dst_pop(struct dst_entry *dst)
  136. {
  137. struct dst_entry *child = dst_clone(dst->child);
  138. dst_release(dst);
  139. return child;
  140. }
  141. extern void * dst_alloc(struct dst_ops * ops);
  142. extern void __dst_free(struct dst_entry * dst);
  143. extern struct dst_entry *dst_destroy(struct dst_entry * dst);
  144. static inline void dst_free(struct dst_entry * dst)
  145. {
  146. if (dst->obsolete > 1)
  147. return;
  148. if (!atomic_read(&dst->__refcnt)) {
  149. dst = dst_destroy(dst);
  150. if (!dst)
  151. return;
  152. }
  153. __dst_free(dst);
  154. }
  155. static inline void dst_rcu_free(struct rcu_head *head)
  156. {
  157. struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
  158. dst_free(dst);
  159. }
  160. static inline void dst_confirm(struct dst_entry *dst)
  161. {
  162. if (dst)
  163. neigh_confirm(dst->neighbour);
  164. }
  165. static inline void dst_negative_advice(struct dst_entry **dst_p)
  166. {
  167. struct dst_entry * dst = *dst_p;
  168. if (dst && dst->ops->negative_advice)
  169. *dst_p = dst->ops->negative_advice(dst);
  170. }
  171. static inline void dst_link_failure(struct sk_buff *skb)
  172. {
  173. struct dst_entry * dst = skb->dst;
  174. if (dst && dst->ops && dst->ops->link_failure)
  175. dst->ops->link_failure(skb);
  176. }
  177. static inline void dst_set_expires(struct dst_entry *dst, int timeout)
  178. {
  179. unsigned long expires = jiffies + timeout;
  180. if (expires == 0)
  181. expires = 1;
  182. if (dst->expires == 0 || time_before(expires, dst->expires))
  183. dst->expires = expires;
  184. }
  185. /* Output packet to network from transport. */
  186. static inline int dst_output(struct sk_buff *skb)
  187. {
  188. int err;
  189. for (;;) {
  190. err = skb->dst->output(skb);
  191. if (likely(err == 0))
  192. return err;
  193. if (unlikely(err != NET_XMIT_BYPASS))
  194. return err;
  195. }
  196. }
  197. /* Input packet from network to transport. */
  198. static inline int dst_input(struct sk_buff *skb)
  199. {
  200. int err;
  201. for (;;) {
  202. err = skb->dst->input(skb);
  203. if (likely(err == 0))
  204. return err;
  205. /* Oh, Jamal... Seems, I will not forgive you this mess. :-) */
  206. if (unlikely(err != NET_XMIT_BYPASS))
  207. return err;
  208. }
  209. }
  210. static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
  211. {
  212. if (dst->obsolete)
  213. dst = dst->ops->check(dst, cookie);
  214. return dst;
  215. }
  216. extern void dst_init(void);
  217. struct flowi;
  218. #ifndef CONFIG_XFRM
  219. static inline int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  220. struct sock *sk, int flags)
  221. {
  222. return 0;
  223. }
  224. #else
  225. extern int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  226. struct sock *sk, int flags);
  227. #endif
  228. #endif
  229. #endif /* _NET_DST_H */