dst.h 10 KB

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