dst.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 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. struct lwtunnel_state *lwtstate;
  78. /*
  79. * Align __refcnt to a 64 bytes alignment
  80. * (L1_CACHE_SIZE would be too much)
  81. */
  82. long __pad_to_align_refcnt[1];
  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. #ifndef CONFIG_64BIT
  92. struct lwtunnel_state *lwtstate;
  93. #endif
  94. union {
  95. struct dst_entry *next;
  96. struct rtable __rcu *rt_next;
  97. struct rt6_info *rt6_next;
  98. struct dn_route __rcu *dn_next;
  99. };
  100. };
  101. u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
  102. extern const u32 dst_default_metrics[];
  103. #define DST_METRICS_READ_ONLY 0x1UL
  104. #define DST_METRICS_FLAGS 0x3UL
  105. #define __DST_METRICS_PTR(Y) \
  106. ((u32 *)((Y) & ~DST_METRICS_FLAGS))
  107. #define DST_METRICS_PTR(X) __DST_METRICS_PTR((X)->_metrics)
  108. static inline bool dst_metrics_read_only(const struct dst_entry *dst)
  109. {
  110. return dst->_metrics & DST_METRICS_READ_ONLY;
  111. }
  112. void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
  113. static inline void dst_destroy_metrics_generic(struct dst_entry *dst)
  114. {
  115. unsigned long val = dst->_metrics;
  116. if (!(val & DST_METRICS_READ_ONLY))
  117. __dst_destroy_metrics_generic(dst, val);
  118. }
  119. static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
  120. {
  121. unsigned long p = dst->_metrics;
  122. BUG_ON(!p);
  123. if (p & DST_METRICS_READ_ONLY)
  124. return dst->ops->cow_metrics(dst, p);
  125. return __DST_METRICS_PTR(p);
  126. }
  127. /* This may only be invoked before the entry has reached global
  128. * visibility.
  129. */
  130. static inline void dst_init_metrics(struct dst_entry *dst,
  131. const u32 *src_metrics,
  132. bool read_only)
  133. {
  134. dst->_metrics = ((unsigned long) src_metrics) |
  135. (read_only ? DST_METRICS_READ_ONLY : 0);
  136. }
  137. static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
  138. {
  139. u32 *dst_metrics = dst_metrics_write_ptr(dest);
  140. if (dst_metrics) {
  141. u32 *src_metrics = DST_METRICS_PTR(src);
  142. memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
  143. }
  144. }
  145. static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
  146. {
  147. return DST_METRICS_PTR(dst);
  148. }
  149. static inline u32
  150. dst_metric_raw(const struct dst_entry *dst, const int metric)
  151. {
  152. u32 *p = DST_METRICS_PTR(dst);
  153. return p[metric-1];
  154. }
  155. static inline u32
  156. dst_metric(const struct dst_entry *dst, const int metric)
  157. {
  158. WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
  159. metric == RTAX_ADVMSS ||
  160. metric == RTAX_MTU);
  161. return dst_metric_raw(dst, metric);
  162. }
  163. static inline u32
  164. dst_metric_advmss(const struct dst_entry *dst)
  165. {
  166. u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS);
  167. if (!advmss)
  168. advmss = dst->ops->default_advmss(dst);
  169. return advmss;
  170. }
  171. static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
  172. {
  173. u32 *p = dst_metrics_write_ptr(dst);
  174. if (p)
  175. p[metric-1] = val;
  176. }
  177. /* Kernel-internal feature bits that are unallocated in user space. */
  178. #define DST_FEATURE_ECN_CA (1 << 31)
  179. #define DST_FEATURE_MASK (DST_FEATURE_ECN_CA)
  180. #define DST_FEATURE_ECN_MASK (DST_FEATURE_ECN_CA | RTAX_FEATURE_ECN)
  181. static inline u32
  182. dst_feature(const struct dst_entry *dst, u32 feature)
  183. {
  184. return dst_metric(dst, RTAX_FEATURES) & feature;
  185. }
  186. static inline u32 dst_mtu(const struct dst_entry *dst)
  187. {
  188. return dst->ops->mtu(dst);
  189. }
  190. /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
  191. static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
  192. {
  193. return msecs_to_jiffies(dst_metric(dst, metric));
  194. }
  195. static inline u32
  196. dst_allfrag(const struct dst_entry *dst)
  197. {
  198. int ret = dst_feature(dst, RTAX_FEATURE_ALLFRAG);
  199. return ret;
  200. }
  201. static inline int
  202. dst_metric_locked(const struct dst_entry *dst, int metric)
  203. {
  204. return dst_metric(dst, RTAX_LOCK) & (1<<metric);
  205. }
  206. static inline void dst_hold(struct dst_entry *dst)
  207. {
  208. /*
  209. * If your kernel compilation stops here, please check
  210. * __pad_to_align_refcnt declaration in struct dst_entry
  211. */
  212. BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
  213. atomic_inc(&dst->__refcnt);
  214. }
  215. static inline void dst_use(struct dst_entry *dst, unsigned long time)
  216. {
  217. dst_hold(dst);
  218. dst->__use++;
  219. dst->lastuse = time;
  220. }
  221. static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
  222. {
  223. dst->__use++;
  224. dst->lastuse = time;
  225. }
  226. static inline struct dst_entry *dst_clone(struct dst_entry *dst)
  227. {
  228. if (dst)
  229. atomic_inc(&dst->__refcnt);
  230. return dst;
  231. }
  232. void dst_release(struct dst_entry *dst);
  233. static inline void refdst_drop(unsigned long refdst)
  234. {
  235. if (!(refdst & SKB_DST_NOREF))
  236. dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
  237. }
  238. /**
  239. * skb_dst_drop - drops skb dst
  240. * @skb: buffer
  241. *
  242. * Drops dst reference count if a reference was taken.
  243. */
  244. static inline void skb_dst_drop(struct sk_buff *skb)
  245. {
  246. if (skb->_skb_refdst) {
  247. refdst_drop(skb->_skb_refdst);
  248. skb->_skb_refdst = 0UL;
  249. }
  250. }
  251. static inline void __skb_dst_copy(struct sk_buff *nskb, unsigned long refdst)
  252. {
  253. nskb->_skb_refdst = refdst;
  254. if (!(nskb->_skb_refdst & SKB_DST_NOREF))
  255. dst_clone(skb_dst(nskb));
  256. }
  257. static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
  258. {
  259. __skb_dst_copy(nskb, oskb->_skb_refdst);
  260. }
  261. /**
  262. * skb_dst_force - makes sure skb dst is refcounted
  263. * @skb: buffer
  264. *
  265. * If dst is not yet refcounted, let's do it
  266. */
  267. static inline void skb_dst_force(struct sk_buff *skb)
  268. {
  269. if (skb_dst_is_noref(skb)) {
  270. WARN_ON(!rcu_read_lock_held());
  271. skb->_skb_refdst &= ~SKB_DST_NOREF;
  272. dst_clone(skb_dst(skb));
  273. }
  274. }
  275. /**
  276. * __skb_tunnel_rx - prepare skb for rx reinsert
  277. * @skb: buffer
  278. * @dev: tunnel device
  279. * @net: netns for packet i/o
  280. *
  281. * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
  282. * so make some cleanups. (no accounting done)
  283. */
  284. static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
  285. struct net *net)
  286. {
  287. skb->dev = dev;
  288. /*
  289. * Clear hash so that we can recalulate the hash for the
  290. * encapsulated packet, unless we have already determine the hash
  291. * over the L4 4-tuple.
  292. */
  293. skb_clear_hash_if_not_l4(skb);
  294. skb_set_queue_mapping(skb, 0);
  295. skb_scrub_packet(skb, !net_eq(net, dev_net(dev)));
  296. }
  297. /**
  298. * skb_tunnel_rx - prepare skb for rx reinsert
  299. * @skb: buffer
  300. * @dev: tunnel device
  301. *
  302. * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
  303. * so make some cleanups, and perform accounting.
  304. * Note: this accounting is not SMP safe.
  305. */
  306. static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
  307. struct net *net)
  308. {
  309. /* TODO : stats should be SMP safe */
  310. dev->stats.rx_packets++;
  311. dev->stats.rx_bytes += skb->len;
  312. __skb_tunnel_rx(skb, dev, net);
  313. }
  314. int dst_discard_sk(struct sock *sk, struct sk_buff *skb);
  315. static inline int dst_discard(struct sk_buff *skb)
  316. {
  317. return dst_discard_sk(skb->sk, skb);
  318. }
  319. void *dst_alloc(struct dst_ops *ops, struct net_device *dev, int initial_ref,
  320. int initial_obsolete, unsigned short flags);
  321. void dst_init(struct dst_entry *dst, struct dst_ops *ops,
  322. struct net_device *dev, int initial_ref, int initial_obsolete,
  323. unsigned short flags);
  324. void __dst_free(struct dst_entry *dst);
  325. struct dst_entry *dst_destroy(struct dst_entry *dst);
  326. static inline void dst_free(struct dst_entry *dst)
  327. {
  328. if (dst->obsolete > 0)
  329. return;
  330. if (!atomic_read(&dst->__refcnt)) {
  331. dst = dst_destroy(dst);
  332. if (!dst)
  333. return;
  334. }
  335. __dst_free(dst);
  336. }
  337. static inline void dst_rcu_free(struct rcu_head *head)
  338. {
  339. struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
  340. dst_free(dst);
  341. }
  342. static inline void dst_confirm(struct dst_entry *dst)
  343. {
  344. dst->pending_confirm = 1;
  345. }
  346. static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
  347. struct sk_buff *skb)
  348. {
  349. const struct hh_cache *hh;
  350. if (dst->pending_confirm) {
  351. unsigned long now = jiffies;
  352. dst->pending_confirm = 0;
  353. /* avoid dirtying neighbour */
  354. if (n->confirmed != now)
  355. n->confirmed = now;
  356. }
  357. hh = &n->hh;
  358. if ((n->nud_state & NUD_CONNECTED) && hh->hh_len)
  359. return neigh_hh_output(hh, skb);
  360. else
  361. return n->output(n, skb);
  362. }
  363. static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
  364. {
  365. struct neighbour *n = dst->ops->neigh_lookup(dst, NULL, daddr);
  366. return IS_ERR(n) ? NULL : n;
  367. }
  368. static inline struct neighbour *dst_neigh_lookup_skb(const struct dst_entry *dst,
  369. struct sk_buff *skb)
  370. {
  371. struct neighbour *n = dst->ops->neigh_lookup(dst, skb, NULL);
  372. return IS_ERR(n) ? NULL : n;
  373. }
  374. static inline void dst_link_failure(struct sk_buff *skb)
  375. {
  376. struct dst_entry *dst = skb_dst(skb);
  377. if (dst && dst->ops && dst->ops->link_failure)
  378. dst->ops->link_failure(skb);
  379. }
  380. static inline void dst_set_expires(struct dst_entry *dst, int timeout)
  381. {
  382. unsigned long expires = jiffies + timeout;
  383. if (expires == 0)
  384. expires = 1;
  385. if (dst->expires == 0 || time_before(expires, dst->expires))
  386. dst->expires = expires;
  387. }
  388. /* Output packet to network from transport. */
  389. static inline int dst_output(struct sock *sk, struct sk_buff *skb)
  390. {
  391. return skb_dst(skb)->output(sk, skb);
  392. }
  393. static inline int dst_output_okfn(struct net *net, struct sock *sk, struct sk_buff *skb)
  394. {
  395. return dst_output(sk, skb);
  396. }
  397. /* Input packet from network to transport. */
  398. static inline int dst_input(struct sk_buff *skb)
  399. {
  400. return skb_dst(skb)->input(skb);
  401. }
  402. static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
  403. {
  404. if (dst->obsolete)
  405. dst = dst->ops->check(dst, cookie);
  406. return dst;
  407. }
  408. void dst_subsys_init(void);
  409. /* Flags for xfrm_lookup flags argument. */
  410. enum {
  411. XFRM_LOOKUP_ICMP = 1 << 0,
  412. XFRM_LOOKUP_QUEUE = 1 << 1,
  413. XFRM_LOOKUP_KEEP_DST_REF = 1 << 2,
  414. };
  415. struct flowi;
  416. #ifndef CONFIG_XFRM
  417. static inline struct dst_entry *xfrm_lookup(struct net *net,
  418. struct dst_entry *dst_orig,
  419. const struct flowi *fl, struct sock *sk,
  420. int flags)
  421. {
  422. return dst_orig;
  423. }
  424. static inline struct dst_entry *xfrm_lookup_route(struct net *net,
  425. struct dst_entry *dst_orig,
  426. const struct flowi *fl,
  427. struct sock *sk,
  428. int flags)
  429. {
  430. return dst_orig;
  431. }
  432. static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
  433. {
  434. return NULL;
  435. }
  436. #else
  437. struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
  438. const struct flowi *fl, struct sock *sk,
  439. int flags);
  440. struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
  441. const struct flowi *fl, struct sock *sk,
  442. int flags);
  443. /* skb attached with this dst needs transformation if dst->xfrm is valid */
  444. static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
  445. {
  446. return dst->xfrm;
  447. }
  448. #endif
  449. #endif /* _NET_DST_H */