dst.h 11 KB

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