dst.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/netdevice.h>
  11. #include <linux/rtnetlink.h>
  12. #include <linux/rcupdate.h>
  13. #include <linux/jiffies.h>
  14. #include <net/neighbour.h>
  15. #include <asm/processor.h>
  16. /*
  17. * 0 - no debugging messages
  18. * 1 - rare events and bugs (default)
  19. * 2 - trace mode.
  20. */
  21. #define RT_CACHE_DEBUG 0
  22. #define DST_GC_MIN (HZ/10)
  23. #define DST_GC_INC (HZ/2)
  24. #define DST_GC_MAX (120*HZ)
  25. /* Each dst_entry has reference count and sits in some parent list(s).
  26. * When it is removed from parent list, it is "freed" (dst_free).
  27. * After this it enters dead state (dst->obsolete > 0) and if its refcnt
  28. * is zero, it can be destroyed immediately, otherwise it is added
  29. * to gc list and garbage collector periodically checks the refcnt.
  30. */
  31. struct sk_buff;
  32. struct dst_entry
  33. {
  34. struct dst_entry *next;
  35. atomic_t __refcnt; /* client references */
  36. int __use;
  37. struct dst_entry *child;
  38. struct net_device *dev;
  39. short error;
  40. short obsolete;
  41. int flags;
  42. #define DST_HOST 1
  43. #define DST_NOXFRM 2
  44. #define DST_NOPOLICY 4
  45. #define DST_NOHASH 8
  46. #define DST_BALANCED 0x10
  47. unsigned long lastuse;
  48. unsigned long expires;
  49. unsigned short header_len; /* more space at head required */
  50. unsigned short trailer_len; /* space to reserve at tail */
  51. u32 metrics[RTAX_MAX];
  52. struct dst_entry *path;
  53. unsigned long rate_last; /* rate limiting for ICMP */
  54. unsigned long rate_tokens;
  55. struct neighbour *neighbour;
  56. struct hh_cache *hh;
  57. struct xfrm_state *xfrm;
  58. int (*input)(struct sk_buff*);
  59. int (*output)(struct sk_buff*);
  60. #ifdef CONFIG_NET_CLS_ROUTE
  61. __u32 tclassid;
  62. #endif
  63. struct dst_ops *ops;
  64. struct rcu_head rcu_head;
  65. char info[0];
  66. };
  67. struct dst_ops
  68. {
  69. unsigned short family;
  70. unsigned short protocol;
  71. unsigned gc_thresh;
  72. int (*gc)(void);
  73. struct dst_entry * (*check)(struct dst_entry *, __u32 cookie);
  74. void (*destroy)(struct dst_entry *);
  75. void (*ifdown)(struct dst_entry *,
  76. struct net_device *dev, int how);
  77. struct dst_entry * (*negative_advice)(struct dst_entry *);
  78. void (*link_failure)(struct sk_buff *);
  79. void (*update_pmtu)(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. return skb->dst->output(skb);
  189. }
  190. /* Input packet from network to transport. */
  191. static inline int dst_input(struct sk_buff *skb)
  192. {
  193. int err;
  194. for (;;) {
  195. err = skb->dst->input(skb);
  196. if (likely(err == 0))
  197. return err;
  198. /* Oh, Jamal... Seems, I will not forgive you this mess. :-) */
  199. if (unlikely(err != NET_XMIT_BYPASS))
  200. return err;
  201. }
  202. }
  203. static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
  204. {
  205. if (dst->obsolete)
  206. dst = dst->ops->check(dst, cookie);
  207. return dst;
  208. }
  209. extern void dst_init(void);
  210. struct flowi;
  211. #ifndef CONFIG_XFRM
  212. static inline int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  213. struct sock *sk, int flags)
  214. {
  215. return 0;
  216. }
  217. #else
  218. extern int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  219. struct sock *sk, int flags);
  220. #endif
  221. #endif
  222. #endif /* _NET_DST_H */