dst.h 10.0 KB

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