dst.h 11 KB

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