dst.h 10 KB

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