dst.h 12 KB

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