dst.h 9.0 KB

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