dst.h 13 KB

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