dst.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 <linux/refcount.h>
  16. #include <net/neighbour.h>
  17. #include <asm/processor.h>
  18. #define DST_GC_MIN (HZ/10)
  19. #define DST_GC_INC (HZ/2)
  20. #define DST_GC_MAX (120*HZ)
  21. /* Each dst_entry has reference count and sits in some parent list(s).
  22. * When it is removed from parent list, it is "freed" (dst_free).
  23. * After this it enters dead state (dst->obsolete > 0) and if its refcnt
  24. * is zero, it can be destroyed immediately, otherwise it is added
  25. * to gc list and garbage collector periodically checks the refcnt.
  26. */
  27. struct sk_buff;
  28. struct dst_entry {
  29. struct net_device *dev;
  30. struct rcu_head rcu_head;
  31. struct dst_entry *child;
  32. struct dst_ops *ops;
  33. unsigned long _metrics;
  34. unsigned long expires;
  35. struct dst_entry *path;
  36. struct dst_entry *from;
  37. #ifdef CONFIG_XFRM
  38. struct xfrm_state *xfrm;
  39. #else
  40. void *__pad1;
  41. #endif
  42. int (*input)(struct sk_buff *);
  43. int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
  44. unsigned short flags;
  45. #define DST_HOST 0x0001
  46. #define DST_NOXFRM 0x0002
  47. #define DST_NOPOLICY 0x0004
  48. #define DST_NOCOUNT 0x0008
  49. #define DST_FAKE_RTABLE 0x0010
  50. #define DST_XFRM_TUNNEL 0x0020
  51. #define DST_XFRM_QUEUE 0x0040
  52. #define DST_METADATA 0x0080
  53. short error;
  54. /* A non-zero value of dst->obsolete forces by-hand validation
  55. * of the route entry. Positive values are set by the generic
  56. * dst layer to indicate that the entry has been forcefully
  57. * destroyed.
  58. *
  59. * Negative values are used by the implementation layer code to
  60. * force invocation of the dst_ops->check() method.
  61. */
  62. short obsolete;
  63. #define DST_OBSOLETE_NONE 0
  64. #define DST_OBSOLETE_DEAD 2
  65. #define DST_OBSOLETE_FORCE_CHK -1
  66. #define DST_OBSOLETE_KILL -2
  67. unsigned short header_len; /* more space at head required */
  68. unsigned short trailer_len; /* space to reserve at tail */
  69. unsigned short __pad3;
  70. #ifdef CONFIG_IP_ROUTE_CLASSID
  71. __u32 tclassid;
  72. #else
  73. __u32 __pad2;
  74. #endif
  75. #ifdef CONFIG_64BIT
  76. /*
  77. * Align __refcnt to a 64 bytes alignment
  78. * (L1_CACHE_SIZE would be too much)
  79. */
  80. long __pad_to_align_refcnt[2];
  81. #endif
  82. /*
  83. * __refcnt wants to be on a different cache line from
  84. * input/output/ops or performance tanks badly
  85. */
  86. atomic_t __refcnt; /* client references */
  87. int __use;
  88. unsigned long lastuse;
  89. struct lwtunnel_state *lwtstate;
  90. union {
  91. struct dst_entry *next;
  92. struct rtable __rcu *rt_next;
  93. struct rt6_info *rt6_next;
  94. struct dn_route __rcu *dn_next;
  95. };
  96. };
  97. struct dst_metrics {
  98. u32 metrics[RTAX_MAX];
  99. refcount_t refcnt;
  100. };
  101. extern const struct dst_metrics dst_default_metrics;
  102. u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
  103. #define DST_METRICS_READ_ONLY 0x1UL
  104. #define DST_METRICS_REFCOUNTED 0x2UL
  105. #define DST_METRICS_FLAGS 0x3UL
  106. #define __DST_METRICS_PTR(Y) \
  107. ((u32 *)((Y) & ~DST_METRICS_FLAGS))
  108. #define DST_METRICS_PTR(X) __DST_METRICS_PTR((X)->_metrics)
  109. static inline bool dst_metrics_read_only(const struct dst_entry *dst)
  110. {
  111. return dst->_metrics & DST_METRICS_READ_ONLY;
  112. }
  113. void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
  114. static inline void dst_destroy_metrics_generic(struct dst_entry *dst)
  115. {
  116. unsigned long val = dst->_metrics;
  117. if (!(val & DST_METRICS_READ_ONLY))
  118. __dst_destroy_metrics_generic(dst, val);
  119. }
  120. static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
  121. {
  122. unsigned long p = dst->_metrics;
  123. BUG_ON(!p);
  124. if (p & DST_METRICS_READ_ONLY)
  125. return dst->ops->cow_metrics(dst, p);
  126. return __DST_METRICS_PTR(p);
  127. }
  128. /* This may only be invoked before the entry has reached global
  129. * visibility.
  130. */
  131. static inline void dst_init_metrics(struct dst_entry *dst,
  132. const u32 *src_metrics,
  133. bool read_only)
  134. {
  135. dst->_metrics = ((unsigned long) src_metrics) |
  136. (read_only ? DST_METRICS_READ_ONLY : 0);
  137. }
  138. static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
  139. {
  140. u32 *dst_metrics = dst_metrics_write_ptr(dest);
  141. if (dst_metrics) {
  142. u32 *src_metrics = DST_METRICS_PTR(src);
  143. memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
  144. }
  145. }
  146. static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
  147. {
  148. return DST_METRICS_PTR(dst);
  149. }
  150. static inline u32
  151. dst_metric_raw(const struct dst_entry *dst, const int metric)
  152. {
  153. u32 *p = DST_METRICS_PTR(dst);
  154. return p[metric-1];
  155. }
  156. static inline u32
  157. dst_metric(const struct dst_entry *dst, const int metric)
  158. {
  159. WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
  160. metric == RTAX_ADVMSS ||
  161. metric == RTAX_MTU);
  162. return dst_metric_raw(dst, metric);
  163. }
  164. static inline u32
  165. dst_metric_advmss(const struct dst_entry *dst)
  166. {
  167. u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS);
  168. if (!advmss)
  169. advmss = dst->ops->default_advmss(dst);
  170. return advmss;
  171. }
  172. static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
  173. {
  174. u32 *p = dst_metrics_write_ptr(dst);
  175. if (p)
  176. p[metric-1] = val;
  177. }
  178. /* Kernel-internal feature bits that are unallocated in user space. */
  179. #define DST_FEATURE_ECN_CA (1 << 31)
  180. #define DST_FEATURE_MASK (DST_FEATURE_ECN_CA)
  181. #define DST_FEATURE_ECN_MASK (DST_FEATURE_ECN_CA | RTAX_FEATURE_ECN)
  182. static inline u32
  183. dst_feature(const struct dst_entry *dst, u32 feature)
  184. {
  185. return dst_metric(dst, RTAX_FEATURES) & feature;
  186. }
  187. static inline u32 dst_mtu(const struct dst_entry *dst)
  188. {
  189. return dst->ops->mtu(dst);
  190. }
  191. /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
  192. static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
  193. {
  194. return msecs_to_jiffies(dst_metric(dst, metric));
  195. }
  196. static inline u32
  197. dst_allfrag(const struct dst_entry *dst)
  198. {
  199. int ret = dst_feature(dst, RTAX_FEATURE_ALLFRAG);
  200. return ret;
  201. }
  202. static inline int
  203. dst_metric_locked(const struct dst_entry *dst, int metric)
  204. {
  205. return dst_metric(dst, RTAX_LOCK) & (1<<metric);
  206. }
  207. static inline void dst_hold(struct dst_entry *dst)
  208. {
  209. /*
  210. * If your kernel compilation stops here, please check
  211. * __pad_to_align_refcnt declaration in struct dst_entry
  212. */
  213. BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
  214. WARN_ON(atomic_inc_not_zero(&dst->__refcnt) == 0);
  215. }
  216. static inline void dst_use(struct dst_entry *dst, unsigned long time)
  217. {
  218. dst_hold(dst);
  219. dst->__use++;
  220. dst->lastuse = time;
  221. }
  222. static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
  223. {
  224. dst->__use++;
  225. dst->lastuse = time;
  226. }
  227. static inline struct dst_entry *dst_clone(struct dst_entry *dst)
  228. {
  229. if (dst)
  230. atomic_inc(&dst->__refcnt);
  231. return dst;
  232. }
  233. void dst_release(struct dst_entry *dst);
  234. void dst_release_immediate(struct dst_entry *dst);
  235. static inline void refdst_drop(unsigned long refdst)
  236. {
  237. if (!(refdst & SKB_DST_NOREF))
  238. dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
  239. }
  240. /**
  241. * skb_dst_drop - drops skb dst
  242. * @skb: buffer
  243. *
  244. * Drops dst reference count if a reference was taken.
  245. */
  246. static inline void skb_dst_drop(struct sk_buff *skb)
  247. {
  248. if (skb->_skb_refdst) {
  249. refdst_drop(skb->_skb_refdst);
  250. skb->_skb_refdst = 0UL;
  251. }
  252. }
  253. static inline void __skb_dst_copy(struct sk_buff *nskb, unsigned long refdst)
  254. {
  255. nskb->_skb_refdst = refdst;
  256. if (!(nskb->_skb_refdst & SKB_DST_NOREF))
  257. dst_clone(skb_dst(nskb));
  258. }
  259. static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
  260. {
  261. __skb_dst_copy(nskb, oskb->_skb_refdst);
  262. }
  263. /**
  264. * skb_dst_force - makes sure skb dst is refcounted
  265. * @skb: buffer
  266. *
  267. * If dst is not yet refcounted, let's do it
  268. */
  269. static inline void skb_dst_force(struct sk_buff *skb)
  270. {
  271. if (skb_dst_is_noref(skb)) {
  272. WARN_ON(!rcu_read_lock_held());
  273. skb->_skb_refdst &= ~SKB_DST_NOREF;
  274. dst_clone(skb_dst(skb));
  275. }
  276. }
  277. /**
  278. * dst_hold_safe - Take a reference on a dst if possible
  279. * @dst: pointer to dst entry
  280. *
  281. * This helper returns false if it could not safely
  282. * take a reference on a dst.
  283. */
  284. static inline bool dst_hold_safe(struct dst_entry *dst)
  285. {
  286. return atomic_inc_not_zero(&dst->__refcnt);
  287. }
  288. /**
  289. * skb_dst_force_safe - makes sure skb dst is refcounted
  290. * @skb: buffer
  291. *
  292. * If dst is not yet refcounted and not destroyed, grab a ref on it.
  293. */
  294. static inline void skb_dst_force_safe(struct sk_buff *skb)
  295. {
  296. if (skb_dst_is_noref(skb)) {
  297. struct dst_entry *dst = skb_dst(skb);
  298. if (!dst_hold_safe(dst))
  299. dst = NULL;
  300. skb->_skb_refdst = (unsigned long)dst;
  301. }
  302. }
  303. /**
  304. * __skb_tunnel_rx - prepare skb for rx reinsert
  305. * @skb: buffer
  306. * @dev: tunnel device
  307. * @net: netns for packet i/o
  308. *
  309. * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
  310. * so make some cleanups. (no accounting done)
  311. */
  312. static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
  313. struct net *net)
  314. {
  315. skb->dev = dev;
  316. /*
  317. * Clear hash so that we can recalulate the hash for the
  318. * encapsulated packet, unless we have already determine the hash
  319. * over the L4 4-tuple.
  320. */
  321. skb_clear_hash_if_not_l4(skb);
  322. skb_set_queue_mapping(skb, 0);
  323. skb_scrub_packet(skb, !net_eq(net, dev_net(dev)));
  324. }
  325. /**
  326. * skb_tunnel_rx - prepare skb for rx reinsert
  327. * @skb: buffer
  328. * @dev: tunnel device
  329. *
  330. * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
  331. * so make some cleanups, and perform accounting.
  332. * Note: this accounting is not SMP safe.
  333. */
  334. static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
  335. struct net *net)
  336. {
  337. /* TODO : stats should be SMP safe */
  338. dev->stats.rx_packets++;
  339. dev->stats.rx_bytes += skb->len;
  340. __skb_tunnel_rx(skb, dev, net);
  341. }
  342. static inline u32 dst_tclassid(const struct sk_buff *skb)
  343. {
  344. #ifdef CONFIG_IP_ROUTE_CLASSID
  345. const struct dst_entry *dst;
  346. dst = skb_dst(skb);
  347. if (dst)
  348. return dst->tclassid;
  349. #endif
  350. return 0;
  351. }
  352. int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  353. static inline int dst_discard(struct sk_buff *skb)
  354. {
  355. return dst_discard_out(&init_net, skb->sk, skb);
  356. }
  357. void *dst_alloc(struct dst_ops *ops, struct net_device *dev, int initial_ref,
  358. int initial_obsolete, unsigned short flags);
  359. void dst_init(struct dst_entry *dst, struct dst_ops *ops,
  360. struct net_device *dev, int initial_ref, int initial_obsolete,
  361. unsigned short flags);
  362. struct dst_entry *dst_destroy(struct dst_entry *dst);
  363. void dst_dev_put(struct dst_entry *dst);
  364. static inline void dst_confirm(struct dst_entry *dst)
  365. {
  366. }
  367. static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
  368. {
  369. struct neighbour *n = dst->ops->neigh_lookup(dst, NULL, daddr);
  370. return IS_ERR(n) ? NULL : n;
  371. }
  372. static inline struct neighbour *dst_neigh_lookup_skb(const struct dst_entry *dst,
  373. struct sk_buff *skb)
  374. {
  375. struct neighbour *n = dst->ops->neigh_lookup(dst, skb, NULL);
  376. return IS_ERR(n) ? NULL : n;
  377. }
  378. static inline void dst_confirm_neigh(const struct dst_entry *dst,
  379. const void *daddr)
  380. {
  381. if (dst->ops->confirm_neigh)
  382. dst->ops->confirm_neigh(dst, daddr);
  383. }
  384. static inline void dst_link_failure(struct sk_buff *skb)
  385. {
  386. struct dst_entry *dst = skb_dst(skb);
  387. if (dst && dst->ops && dst->ops->link_failure)
  388. dst->ops->link_failure(skb);
  389. }
  390. static inline void dst_set_expires(struct dst_entry *dst, int timeout)
  391. {
  392. unsigned long expires = jiffies + timeout;
  393. if (expires == 0)
  394. expires = 1;
  395. if (dst->expires == 0 || time_before(expires, dst->expires))
  396. dst->expires = expires;
  397. }
  398. /* Output packet to network from transport. */
  399. static inline int dst_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  400. {
  401. return skb_dst(skb)->output(net, sk, skb);
  402. }
  403. /* Input packet from network to transport. */
  404. static inline int dst_input(struct sk_buff *skb)
  405. {
  406. return skb_dst(skb)->input(skb);
  407. }
  408. static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
  409. {
  410. if (dst->obsolete)
  411. dst = dst->ops->check(dst, cookie);
  412. return dst;
  413. }
  414. /* Flags for xfrm_lookup flags argument. */
  415. enum {
  416. XFRM_LOOKUP_ICMP = 1 << 0,
  417. XFRM_LOOKUP_QUEUE = 1 << 1,
  418. XFRM_LOOKUP_KEEP_DST_REF = 1 << 2,
  419. };
  420. struct flowi;
  421. #ifndef CONFIG_XFRM
  422. static inline struct dst_entry *xfrm_lookup(struct net *net,
  423. struct dst_entry *dst_orig,
  424. const struct flowi *fl,
  425. const struct sock *sk,
  426. int flags)
  427. {
  428. return dst_orig;
  429. }
  430. static inline struct dst_entry *xfrm_lookup_route(struct net *net,
  431. struct dst_entry *dst_orig,
  432. const struct flowi *fl,
  433. const struct sock *sk,
  434. int flags)
  435. {
  436. return dst_orig;
  437. }
  438. static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
  439. {
  440. return NULL;
  441. }
  442. #else
  443. struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
  444. const struct flowi *fl, const struct sock *sk,
  445. int flags);
  446. struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
  447. const struct flowi *fl, const struct sock *sk,
  448. int flags);
  449. /* skb attached with this dst needs transformation if dst->xfrm is valid */
  450. static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
  451. {
  452. return dst->xfrm;
  453. }
  454. #endif
  455. #endif /* _NET_DST_H */