dst.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 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. unsigned short pending_confirm;
  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. #ifdef CONFIG_IP_ROUTE_CLASSID
  72. __u32 tclassid;
  73. #else
  74. __u32 __pad2;
  75. #endif
  76. #ifdef CONFIG_64BIT
  77. /*
  78. * Align __refcnt to a 64 bytes alignment
  79. * (L1_CACHE_SIZE would be too much)
  80. */
  81. long __pad_to_align_refcnt[2];
  82. #endif
  83. /*
  84. * __refcnt wants to be on a different cache line from
  85. * input/output/ops or performance tanks badly
  86. */
  87. atomic_t __refcnt; /* client references */
  88. int __use;
  89. unsigned long lastuse;
  90. struct lwtunnel_state *lwtstate;
  91. union {
  92. struct dst_entry *next;
  93. struct rtable __rcu *rt_next;
  94. struct rt6_info *rt6_next;
  95. struct dn_route __rcu *dn_next;
  96. };
  97. };
  98. u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
  99. extern const u32 dst_default_metrics[];
  100. #define DST_METRICS_READ_ONLY 0x1UL
  101. #define DST_METRICS_FLAGS 0x3UL
  102. #define __DST_METRICS_PTR(Y) \
  103. ((u32 *)((Y) & ~DST_METRICS_FLAGS))
  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. 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. /* Kernel-internal feature bits that are unallocated in user space. */
  175. #define DST_FEATURE_ECN_CA (1 << 31)
  176. #define DST_FEATURE_MASK (DST_FEATURE_ECN_CA)
  177. #define DST_FEATURE_ECN_MASK (DST_FEATURE_ECN_CA | RTAX_FEATURE_ECN)
  178. static inline u32
  179. dst_feature(const struct dst_entry *dst, u32 feature)
  180. {
  181. return dst_metric(dst, RTAX_FEATURES) & feature;
  182. }
  183. static inline u32 dst_mtu(const struct dst_entry *dst)
  184. {
  185. return dst->ops->mtu(dst);
  186. }
  187. /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
  188. static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
  189. {
  190. return msecs_to_jiffies(dst_metric(dst, metric));
  191. }
  192. static inline u32
  193. dst_allfrag(const struct dst_entry *dst)
  194. {
  195. int ret = dst_feature(dst, RTAX_FEATURE_ALLFRAG);
  196. return ret;
  197. }
  198. static inline int
  199. dst_metric_locked(const struct dst_entry *dst, int metric)
  200. {
  201. return dst_metric(dst, RTAX_LOCK) & (1<<metric);
  202. }
  203. static inline void dst_hold(struct dst_entry *dst)
  204. {
  205. /*
  206. * If your kernel compilation stops here, please check
  207. * __pad_to_align_refcnt declaration in struct dst_entry
  208. */
  209. BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
  210. atomic_inc(&dst->__refcnt);
  211. }
  212. static inline void dst_use(struct dst_entry *dst, unsigned long time)
  213. {
  214. dst_hold(dst);
  215. dst->__use++;
  216. dst->lastuse = time;
  217. }
  218. static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
  219. {
  220. dst->__use++;
  221. dst->lastuse = time;
  222. }
  223. static inline struct dst_entry *dst_clone(struct dst_entry *dst)
  224. {
  225. if (dst)
  226. atomic_inc(&dst->__refcnt);
  227. return dst;
  228. }
  229. void dst_release(struct dst_entry *dst);
  230. static inline void refdst_drop(unsigned long refdst)
  231. {
  232. if (!(refdst & SKB_DST_NOREF))
  233. dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
  234. }
  235. /**
  236. * skb_dst_drop - drops skb dst
  237. * @skb: buffer
  238. *
  239. * Drops dst reference count if a reference was taken.
  240. */
  241. static inline void skb_dst_drop(struct sk_buff *skb)
  242. {
  243. if (skb->_skb_refdst) {
  244. refdst_drop(skb->_skb_refdst);
  245. skb->_skb_refdst = 0UL;
  246. }
  247. }
  248. static inline void __skb_dst_copy(struct sk_buff *nskb, unsigned long refdst)
  249. {
  250. nskb->_skb_refdst = refdst;
  251. if (!(nskb->_skb_refdst & SKB_DST_NOREF))
  252. dst_clone(skb_dst(nskb));
  253. }
  254. static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
  255. {
  256. __skb_dst_copy(nskb, oskb->_skb_refdst);
  257. }
  258. /**
  259. * skb_dst_force - makes sure skb dst is refcounted
  260. * @skb: buffer
  261. *
  262. * If dst is not yet refcounted, let's do it
  263. */
  264. static inline void skb_dst_force(struct sk_buff *skb)
  265. {
  266. if (skb_dst_is_noref(skb)) {
  267. WARN_ON(!rcu_read_lock_held());
  268. skb->_skb_refdst &= ~SKB_DST_NOREF;
  269. dst_clone(skb_dst(skb));
  270. }
  271. }
  272. /**
  273. * dst_hold_safe - Take a reference on a dst if possible
  274. * @dst: pointer to dst entry
  275. *
  276. * This helper returns false if it could not safely
  277. * take a reference on a dst.
  278. */
  279. static inline bool dst_hold_safe(struct dst_entry *dst)
  280. {
  281. if (dst->flags & DST_NOCACHE)
  282. return atomic_inc_not_zero(&dst->__refcnt);
  283. dst_hold(dst);
  284. return true;
  285. }
  286. /**
  287. * skb_dst_force_safe - makes sure skb dst is refcounted
  288. * @skb: buffer
  289. *
  290. * If dst is not yet refcounted and not destroyed, grab a ref on it.
  291. */
  292. static inline void skb_dst_force_safe(struct sk_buff *skb)
  293. {
  294. if (skb_dst_is_noref(skb)) {
  295. struct dst_entry *dst = skb_dst(skb);
  296. if (!dst_hold_safe(dst))
  297. dst = NULL;
  298. skb->_skb_refdst = (unsigned long)dst;
  299. }
  300. }
  301. /**
  302. * __skb_tunnel_rx - prepare skb for rx reinsert
  303. * @skb: buffer
  304. * @dev: tunnel device
  305. * @net: netns for packet i/o
  306. *
  307. * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
  308. * so make some cleanups. (no accounting done)
  309. */
  310. static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
  311. struct net *net)
  312. {
  313. skb->dev = dev;
  314. /*
  315. * Clear hash so that we can recalulate the hash for the
  316. * encapsulated packet, unless we have already determine the hash
  317. * over the L4 4-tuple.
  318. */
  319. skb_clear_hash_if_not_l4(skb);
  320. skb_set_queue_mapping(skb, 0);
  321. skb_scrub_packet(skb, !net_eq(net, dev_net(dev)));
  322. }
  323. /**
  324. * skb_tunnel_rx - prepare skb for rx reinsert
  325. * @skb: buffer
  326. * @dev: tunnel device
  327. *
  328. * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
  329. * so make some cleanups, and perform accounting.
  330. * Note: this accounting is not SMP safe.
  331. */
  332. static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
  333. struct net *net)
  334. {
  335. /* TODO : stats should be SMP safe */
  336. dev->stats.rx_packets++;
  337. dev->stats.rx_bytes += skb->len;
  338. __skb_tunnel_rx(skb, dev, net);
  339. }
  340. static inline u32 dst_tclassid(const struct sk_buff *skb)
  341. {
  342. #ifdef CONFIG_IP_ROUTE_CLASSID
  343. const struct dst_entry *dst;
  344. dst = skb_dst(skb);
  345. if (dst)
  346. return dst->tclassid;
  347. #endif
  348. return 0;
  349. }
  350. int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  351. static inline int dst_discard(struct sk_buff *skb)
  352. {
  353. return dst_discard_out(&init_net, skb->sk, skb);
  354. }
  355. void *dst_alloc(struct dst_ops *ops, struct net_device *dev, int initial_ref,
  356. int initial_obsolete, unsigned short flags);
  357. void dst_init(struct dst_entry *dst, struct dst_ops *ops,
  358. struct net_device *dev, int initial_ref, int initial_obsolete,
  359. unsigned short flags);
  360. void __dst_free(struct dst_entry *dst);
  361. struct dst_entry *dst_destroy(struct dst_entry *dst);
  362. static inline void dst_free(struct dst_entry *dst)
  363. {
  364. if (dst->obsolete > 0)
  365. return;
  366. if (!atomic_read(&dst->__refcnt)) {
  367. dst = dst_destroy(dst);
  368. if (!dst)
  369. return;
  370. }
  371. __dst_free(dst);
  372. }
  373. static inline void dst_rcu_free(struct rcu_head *head)
  374. {
  375. struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
  376. dst_free(dst);
  377. }
  378. static inline void dst_confirm(struct dst_entry *dst)
  379. {
  380. dst->pending_confirm = 1;
  381. }
  382. static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
  383. struct sk_buff *skb)
  384. {
  385. const struct hh_cache *hh;
  386. if (dst->pending_confirm) {
  387. unsigned long now = jiffies;
  388. dst->pending_confirm = 0;
  389. /* avoid dirtying neighbour */
  390. if (n->confirmed != now)
  391. n->confirmed = now;
  392. }
  393. hh = &n->hh;
  394. if ((n->nud_state & NUD_CONNECTED) && hh->hh_len)
  395. return neigh_hh_output(hh, skb);
  396. else
  397. return n->output(n, skb);
  398. }
  399. static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
  400. {
  401. struct neighbour *n = dst->ops->neigh_lookup(dst, NULL, daddr);
  402. return IS_ERR(n) ? NULL : n;
  403. }
  404. static inline struct neighbour *dst_neigh_lookup_skb(const struct dst_entry *dst,
  405. struct sk_buff *skb)
  406. {
  407. struct neighbour *n = dst->ops->neigh_lookup(dst, skb, NULL);
  408. return IS_ERR(n) ? NULL : n;
  409. }
  410. static inline void dst_link_failure(struct sk_buff *skb)
  411. {
  412. struct dst_entry *dst = skb_dst(skb);
  413. if (dst && dst->ops && dst->ops->link_failure)
  414. dst->ops->link_failure(skb);
  415. }
  416. static inline void dst_set_expires(struct dst_entry *dst, int timeout)
  417. {
  418. unsigned long expires = jiffies + timeout;
  419. if (expires == 0)
  420. expires = 1;
  421. if (dst->expires == 0 || time_before(expires, dst->expires))
  422. dst->expires = expires;
  423. }
  424. /* Output packet to network from transport. */
  425. static inline int dst_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  426. {
  427. return skb_dst(skb)->output(net, sk, skb);
  428. }
  429. /* Input packet from network to transport. */
  430. static inline int dst_input(struct sk_buff *skb)
  431. {
  432. return skb_dst(skb)->input(skb);
  433. }
  434. static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
  435. {
  436. if (dst->obsolete)
  437. dst = dst->ops->check(dst, cookie);
  438. return dst;
  439. }
  440. void dst_subsys_init(void);
  441. /* Flags for xfrm_lookup flags argument. */
  442. enum {
  443. XFRM_LOOKUP_ICMP = 1 << 0,
  444. XFRM_LOOKUP_QUEUE = 1 << 1,
  445. XFRM_LOOKUP_KEEP_DST_REF = 1 << 2,
  446. };
  447. struct flowi;
  448. #ifndef CONFIG_XFRM
  449. static inline struct dst_entry *xfrm_lookup(struct net *net,
  450. struct dst_entry *dst_orig,
  451. const struct flowi *fl,
  452. const struct sock *sk,
  453. int flags)
  454. {
  455. return dst_orig;
  456. }
  457. static inline struct dst_entry *xfrm_lookup_route(struct net *net,
  458. struct dst_entry *dst_orig,
  459. const struct flowi *fl,
  460. const struct sock *sk,
  461. int flags)
  462. {
  463. return dst_orig;
  464. }
  465. static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
  466. {
  467. return NULL;
  468. }
  469. #else
  470. struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
  471. const struct flowi *fl, const struct sock *sk,
  472. int flags);
  473. struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
  474. const struct flowi *fl, const struct sock *sk,
  475. int flags);
  476. /* skb attached with this dst needs transformation if dst->xfrm is valid */
  477. static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
  478. {
  479. return dst->xfrm;
  480. }
  481. #endif
  482. #endif /* _NET_DST_H */