dst.h 12 KB

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