dst.h 5.9 KB

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