dst.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 <net/dst_ops.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. 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. #ifdef CONFIG_XFRM
  52. struct xfrm_state *xfrm;
  53. #else
  54. void *__pad1;
  55. #endif
  56. int (*input)(struct sk_buff*);
  57. int (*output)(struct sk_buff*);
  58. struct dst_ops *ops;
  59. u32 metrics[RTAX_MAX];
  60. #ifdef CONFIG_NET_CLS_ROUTE
  61. __u32 tclassid;
  62. #else
  63. __u32 __pad2;
  64. #endif
  65. /*
  66. * Align __refcnt to a 64 bytes alignment
  67. * (L1_CACHE_SIZE would be too much)
  68. */
  69. #ifdef CONFIG_64BIT
  70. long __pad_to_align_refcnt[1];
  71. #endif
  72. /*
  73. * __refcnt wants to be on a different cache line from
  74. * input/output/ops or performance tanks badly
  75. */
  76. atomic_t __refcnt; /* client references */
  77. int __use;
  78. unsigned long lastuse;
  79. union {
  80. struct dst_entry *next;
  81. struct rtable *rt_next;
  82. struct rt6_info *rt6_next;
  83. struct dn_route *dn_next;
  84. };
  85. };
  86. #ifdef __KERNEL__
  87. static inline u32
  88. dst_metric(const struct dst_entry *dst, int metric)
  89. {
  90. return dst->metrics[metric-1];
  91. }
  92. static inline u32
  93. dst_feature(const struct dst_entry *dst, u32 feature)
  94. {
  95. return dst_metric(dst, RTAX_FEATURES) & feature;
  96. }
  97. static inline u32 dst_mtu(const struct dst_entry *dst)
  98. {
  99. u32 mtu = dst_metric(dst, RTAX_MTU);
  100. /*
  101. * Alexey put it here, so ask him about it :)
  102. */
  103. barrier();
  104. return mtu;
  105. }
  106. /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
  107. static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
  108. {
  109. return msecs_to_jiffies(dst_metric(dst, metric));
  110. }
  111. static inline void set_dst_metric_rtt(struct dst_entry *dst, int metric,
  112. unsigned long rtt)
  113. {
  114. dst->metrics[metric-1] = jiffies_to_msecs(rtt);
  115. }
  116. static inline u32
  117. dst_allfrag(const struct dst_entry *dst)
  118. {
  119. int ret = dst_feature(dst, RTAX_FEATURE_ALLFRAG);
  120. /* Yes, _exactly_. This is paranoia. */
  121. barrier();
  122. return ret;
  123. }
  124. static inline int
  125. dst_metric_locked(struct dst_entry *dst, int metric)
  126. {
  127. return dst_metric(dst, RTAX_LOCK) & (1<<metric);
  128. }
  129. static inline void dst_hold(struct dst_entry * dst)
  130. {
  131. /*
  132. * If your kernel compilation stops here, please check
  133. * __pad_to_align_refcnt declaration in struct dst_entry
  134. */
  135. BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
  136. atomic_inc(&dst->__refcnt);
  137. }
  138. static inline void dst_use(struct dst_entry *dst, unsigned long time)
  139. {
  140. dst_hold(dst);
  141. dst->__use++;
  142. dst->lastuse = time;
  143. }
  144. static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
  145. {
  146. dst->__use++;
  147. dst->lastuse = time;
  148. }
  149. static inline
  150. struct dst_entry * dst_clone(struct dst_entry * dst)
  151. {
  152. if (dst)
  153. atomic_inc(&dst->__refcnt);
  154. return dst;
  155. }
  156. extern void dst_release(struct dst_entry *dst);
  157. static inline void refdst_drop(unsigned long refdst)
  158. {
  159. if (!(refdst & SKB_DST_NOREF))
  160. dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
  161. }
  162. /**
  163. * skb_dst_drop - drops skb dst
  164. * @skb: buffer
  165. *
  166. * Drops dst reference count if a reference was taken.
  167. */
  168. static inline void skb_dst_drop(struct sk_buff *skb)
  169. {
  170. if (skb->_skb_refdst) {
  171. refdst_drop(skb->_skb_refdst);
  172. skb->_skb_refdst = 0UL;
  173. }
  174. }
  175. static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
  176. {
  177. nskb->_skb_refdst = oskb->_skb_refdst;
  178. if (!(nskb->_skb_refdst & SKB_DST_NOREF))
  179. dst_clone(skb_dst(nskb));
  180. }
  181. /**
  182. * skb_dst_force - makes sure skb dst is refcounted
  183. * @skb: buffer
  184. *
  185. * If dst is not yet refcounted, let's do it
  186. */
  187. static inline void skb_dst_force(struct sk_buff *skb)
  188. {
  189. if (skb_dst_is_noref(skb)) {
  190. WARN_ON(!rcu_read_lock_held());
  191. skb->_skb_refdst &= ~SKB_DST_NOREF;
  192. dst_clone(skb_dst(skb));
  193. }
  194. }
  195. /**
  196. * skb_tunnel_rx - prepare skb for rx reinsert
  197. * @skb: buffer
  198. * @dev: tunnel device
  199. *
  200. * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
  201. * so make some cleanups, and perform accounting.
  202. */
  203. static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev)
  204. {
  205. skb->dev = dev;
  206. /* TODO : stats should be SMP safe */
  207. dev->stats.rx_packets++;
  208. dev->stats.rx_bytes += skb->len;
  209. skb->rxhash = 0;
  210. skb_dst_drop(skb);
  211. nf_reset(skb);
  212. }
  213. /* Children define the path of the packet through the
  214. * Linux networking. Thus, destinations are stackable.
  215. */
  216. static inline struct dst_entry *skb_dst_pop(struct sk_buff *skb)
  217. {
  218. struct dst_entry *child = skb_dst(skb)->child;
  219. skb_dst_drop(skb);
  220. return child;
  221. }
  222. extern int dst_discard(struct sk_buff *skb);
  223. extern void * dst_alloc(struct dst_ops * ops);
  224. extern void __dst_free(struct dst_entry * dst);
  225. extern struct dst_entry *dst_destroy(struct dst_entry * dst);
  226. static inline void dst_free(struct dst_entry * dst)
  227. {
  228. if (dst->obsolete > 1)
  229. return;
  230. if (!atomic_read(&dst->__refcnt)) {
  231. dst = dst_destroy(dst);
  232. if (!dst)
  233. return;
  234. }
  235. __dst_free(dst);
  236. }
  237. static inline void dst_rcu_free(struct rcu_head *head)
  238. {
  239. struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
  240. dst_free(dst);
  241. }
  242. static inline void dst_confirm(struct dst_entry *dst)
  243. {
  244. if (dst)
  245. neigh_confirm(dst->neighbour);
  246. }
  247. static inline void dst_link_failure(struct sk_buff *skb)
  248. {
  249. struct dst_entry *dst = skb_dst(skb);
  250. if (dst && dst->ops && dst->ops->link_failure)
  251. dst->ops->link_failure(skb);
  252. }
  253. static inline void dst_set_expires(struct dst_entry *dst, int timeout)
  254. {
  255. unsigned long expires = jiffies + timeout;
  256. if (expires == 0)
  257. expires = 1;
  258. if (dst->expires == 0 || time_before(expires, dst->expires))
  259. dst->expires = expires;
  260. }
  261. /* Output packet to network from transport. */
  262. static inline int dst_output(struct sk_buff *skb)
  263. {
  264. return skb_dst(skb)->output(skb);
  265. }
  266. /* Input packet from network to transport. */
  267. static inline int dst_input(struct sk_buff *skb)
  268. {
  269. return skb_dst(skb)->input(skb);
  270. }
  271. static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
  272. {
  273. if (dst->obsolete)
  274. dst = dst->ops->check(dst, cookie);
  275. return dst;
  276. }
  277. extern void dst_init(void);
  278. /* Flags for xfrm_lookup flags argument. */
  279. enum {
  280. XFRM_LOOKUP_WAIT = 1 << 0,
  281. XFRM_LOOKUP_ICMP = 1 << 1,
  282. };
  283. struct flowi;
  284. #ifndef CONFIG_XFRM
  285. static inline int xfrm_lookup(struct net *net, struct dst_entry **dst_p,
  286. struct flowi *fl, struct sock *sk, int flags)
  287. {
  288. return 0;
  289. }
  290. static inline int __xfrm_lookup(struct net *net, struct dst_entry **dst_p,
  291. struct flowi *fl, struct sock *sk, int flags)
  292. {
  293. return 0;
  294. }
  295. #else
  296. extern int xfrm_lookup(struct net *net, struct dst_entry **dst_p,
  297. struct flowi *fl, struct sock *sk, int flags);
  298. extern int __xfrm_lookup(struct net *net, struct dst_entry **dst_p,
  299. struct flowi *fl, struct sock *sk, int flags);
  300. #endif
  301. #endif
  302. #endif /* _NET_DST_H */