Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * net/dst.h Protocol independent destination cache definitions. | |
3 | * | |
4 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
5 | * | |
6 | */ | |
7 | ||
8 | #ifndef _NET_DST_H | |
9 | #define _NET_DST_H | |
10 | ||
86393e52 | 11 | #include <net/dst_ops.h> |
14c85021 | 12 | #include <linux/netdevice.h> |
1da177e4 LT |
13 | #include <linux/rtnetlink.h> |
14 | #include <linux/rcupdate.h> | |
187f1882 | 15 | #include <linux/bug.h> |
1da177e4 LT |
16 | #include <linux/jiffies.h> |
17 | #include <net/neighbour.h> | |
18 | #include <asm/processor.h> | |
19 | ||
1da177e4 LT |
20 | #define DST_GC_MIN (HZ/10) |
21 | #define DST_GC_INC (HZ/2) | |
22 | #define DST_GC_MAX (120*HZ) | |
23 | ||
24 | /* Each dst_entry has reference count and sits in some parent list(s). | |
25 | * When it is removed from parent list, it is "freed" (dst_free). | |
26 | * After this it enters dead state (dst->obsolete > 0) and if its refcnt | |
27 | * is zero, it can be destroyed immediately, otherwise it is added | |
28 | * to gc list and garbage collector periodically checks the refcnt. | |
29 | */ | |
30 | ||
31 | struct sk_buff; | |
32 | ||
fd2c3ef7 | 33 | struct dst_entry { |
1e19e02c | 34 | struct rcu_head rcu_head; |
1da177e4 LT |
35 | struct dst_entry *child; |
36 | struct net_device *dev; | |
62fa8a84 DM |
37 | struct dst_ops *ops; |
38 | unsigned long _metrics; | |
ecd98837 | 39 | unsigned long expires; |
f1dd9c37 | 40 | struct dst_entry *path; |
ecd98837 | 41 | struct dst_entry *from; |
def8b4fa | 42 | #ifdef CONFIG_XFRM |
1da177e4 | 43 | struct xfrm_state *xfrm; |
5635c10d ED |
44 | #else |
45 | void *__pad1; | |
def8b4fa | 46 | #endif |
7f95e188 EZ |
47 | int (*input)(struct sk_buff *); |
48 | int (*output)(struct sk_buff *); | |
1da177e4 | 49 | |
5110effe | 50 | unsigned short flags; |
f6b72b62 DM |
51 | #define DST_HOST 0x0001 |
52 | #define DST_NOXFRM 0x0002 | |
53 | #define DST_NOPOLICY 0x0004 | |
54 | #define DST_NOHASH 0x0008 | |
55 | #define DST_NOCACHE 0x0010 | |
56 | #define DST_NOCOUNT 0x0020 | |
e90c1483 HFS |
57 | #define DST_FAKE_RTABLE 0x0040 |
58 | #define DST_XFRM_TUNNEL 0x0080 | |
59 | #define DST_XFRM_QUEUE 0x0100 | |
f6b72b62 | 60 | |
5110effe DM |
61 | unsigned short pending_confirm; |
62 | ||
62fa8a84 | 63 | short error; |
f5b0a874 DM |
64 | |
65 | /* A non-zero value of dst->obsolete forces by-hand validation | |
66 | * of the route entry. Positive values are set by the generic | |
67 | * dst layer to indicate that the entry has been forcefully | |
68 | * destroyed. | |
69 | * | |
70 | * Negative values are used by the implementation layer code to | |
71 | * force invocation of the dst_ops->check() method. | |
72 | */ | |
62fa8a84 | 73 | short obsolete; |
f5b0a874 DM |
74 | #define DST_OBSOLETE_NONE 0 |
75 | #define DST_OBSOLETE_DEAD 2 | |
76 | #define DST_OBSOLETE_FORCE_CHK -1 | |
ceb33206 | 77 | #define DST_OBSOLETE_KILL -2 |
62fa8a84 DM |
78 | unsigned short header_len; /* more space at head required */ |
79 | unsigned short trailer_len; /* space to reserve at tail */ | |
c7066f70 | 80 | #ifdef CONFIG_IP_ROUTE_CLASSID |
f1dd9c37 | 81 | __u32 tclassid; |
5635c10d ED |
82 | #else |
83 | __u32 __pad2; | |
f1dd9c37 ZY |
84 | #endif |
85 | ||
5635c10d ED |
86 | /* |
87 | * Align __refcnt to a 64 bytes alignment | |
88 | * (L1_CACHE_SIZE would be too much) | |
89 | */ | |
90 | #ifdef CONFIG_64BIT | |
f6b72b62 | 91 | long __pad_to_align_refcnt[2]; |
5635c10d | 92 | #endif |
f1dd9c37 ZY |
93 | /* |
94 | * __refcnt wants to be on a different cache line from | |
95 | * input/output/ops or performance tanks badly | |
96 | */ | |
1e19e02c ED |
97 | atomic_t __refcnt; /* client references */ |
98 | int __use; | |
f1dd9c37 | 99 | unsigned long lastuse; |
1e19e02c | 100 | union { |
fc766e4c ED |
101 | struct dst_entry *next; |
102 | struct rtable __rcu *rt_next; | |
103 | struct rt6_info *rt6_next; | |
104 | struct dn_route __rcu *dn_next; | |
1e19e02c | 105 | }; |
1da177e4 LT |
106 | }; |
107 | ||
a4023dd0 | 108 | u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old); |
a37e6e34 | 109 | extern const u32 dst_default_metrics[]; |
62fa8a84 DM |
110 | |
111 | #define DST_METRICS_READ_ONLY 0x1UL | |
112 | #define __DST_METRICS_PTR(Y) \ | |
113 | ((u32 *)((Y) & ~DST_METRICS_READ_ONLY)) | |
114 | #define DST_METRICS_PTR(X) __DST_METRICS_PTR((X)->_metrics) | |
115 | ||
116 | static inline bool dst_metrics_read_only(const struct dst_entry *dst) | |
117 | { | |
118 | return dst->_metrics & DST_METRICS_READ_ONLY; | |
119 | } | |
120 | ||
a4023dd0 | 121 | void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old); |
62fa8a84 DM |
122 | |
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 | ||
130 | static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst) | |
131 | { | |
132 | unsigned long p = dst->_metrics; | |
133 | ||
1f37070d SH |
134 | BUG_ON(!p); |
135 | ||
62fa8a84 DM |
136 | if (p & DST_METRICS_READ_ONLY) |
137 | return dst->ops->cow_metrics(dst, p); | |
138 | return __DST_METRICS_PTR(p); | |
139 | } | |
140 | ||
141 | /* This may only be invoked before the entry has reached global | |
142 | * visibility. | |
143 | */ | |
144 | static inline void dst_init_metrics(struct dst_entry *dst, | |
145 | const u32 *src_metrics, | |
146 | bool read_only) | |
147 | { | |
148 | dst->_metrics = ((unsigned long) src_metrics) | | |
149 | (read_only ? DST_METRICS_READ_ONLY : 0); | |
150 | } | |
151 | ||
152 | static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src) | |
153 | { | |
154 | u32 *dst_metrics = dst_metrics_write_ptr(dest); | |
155 | ||
156 | if (dst_metrics) { | |
157 | u32 *src_metrics = DST_METRICS_PTR(src); | |
158 | ||
159 | memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32)); | |
160 | } | |
161 | } | |
162 | ||
163 | static inline u32 *dst_metrics_ptr(struct dst_entry *dst) | |
164 | { | |
165 | return DST_METRICS_PTR(dst); | |
166 | } | |
167 | ||
1da177e4 | 168 | static inline u32 |
5170ae82 | 169 | dst_metric_raw(const struct dst_entry *dst, const int metric) |
1da177e4 | 170 | { |
62fa8a84 DM |
171 | u32 *p = DST_METRICS_PTR(dst); |
172 | ||
173 | return p[metric-1]; | |
defb3519 DM |
174 | } |
175 | ||
5170ae82 DM |
176 | static inline u32 |
177 | dst_metric(const struct dst_entry *dst, const int metric) | |
178 | { | |
0dbaee3b | 179 | WARN_ON_ONCE(metric == RTAX_HOPLIMIT || |
d33e4553 DM |
180 | metric == RTAX_ADVMSS || |
181 | metric == RTAX_MTU); | |
5170ae82 DM |
182 | return dst_metric_raw(dst, metric); |
183 | } | |
184 | ||
0dbaee3b DM |
185 | static inline u32 |
186 | dst_metric_advmss(const struct dst_entry *dst) | |
187 | { | |
188 | u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS); | |
189 | ||
190 | if (!advmss) | |
191 | advmss = dst->ops->default_advmss(dst); | |
192 | ||
193 | return advmss; | |
194 | } | |
195 | ||
defb3519 DM |
196 | static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val) |
197 | { | |
62fa8a84 | 198 | u32 *p = dst_metrics_write_ptr(dst); |
defb3519 | 199 | |
62fa8a84 DM |
200 | if (p) |
201 | p[metric-1] = val; | |
1da177e4 LT |
202 | } |
203 | ||
0c3adfb8 GBY |
204 | static inline u32 |
205 | dst_feature(const struct dst_entry *dst, u32 feature) | |
206 | { | |
bb5b7c11 | 207 | return dst_metric(dst, RTAX_FEATURES) & feature; |
0c3adfb8 GBY |
208 | } |
209 | ||
1da177e4 LT |
210 | static inline u32 dst_mtu(const struct dst_entry *dst) |
211 | { | |
618f9bc7 | 212 | return dst->ops->mtu(dst); |
1da177e4 LT |
213 | } |
214 | ||
c1e20f7c SH |
215 | /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */ |
216 | static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric) | |
217 | { | |
218 | return msecs_to_jiffies(dst_metric(dst, metric)); | |
219 | } | |
220 | ||
1da177e4 LT |
221 | static inline u32 |
222 | dst_allfrag(const struct dst_entry *dst) | |
223 | { | |
0c3adfb8 | 224 | int ret = dst_feature(dst, RTAX_FEATURE_ALLFRAG); |
1da177e4 LT |
225 | return ret; |
226 | } | |
227 | ||
228 | static inline int | |
d33e4553 | 229 | dst_metric_locked(const struct dst_entry *dst, int metric) |
1da177e4 LT |
230 | { |
231 | return dst_metric(dst, RTAX_LOCK) & (1<<metric); | |
232 | } | |
233 | ||
7f95e188 | 234 | static inline void dst_hold(struct dst_entry *dst) |
1da177e4 | 235 | { |
5635c10d ED |
236 | /* |
237 | * If your kernel compilation stops here, please check | |
238 | * __pad_to_align_refcnt declaration in struct dst_entry | |
239 | */ | |
240 | BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63); | |
1da177e4 LT |
241 | atomic_inc(&dst->__refcnt); |
242 | } | |
243 | ||
03f49f34 PE |
244 | static inline void dst_use(struct dst_entry *dst, unsigned long time) |
245 | { | |
246 | dst_hold(dst); | |
247 | dst->__use++; | |
248 | dst->lastuse = time; | |
249 | } | |
250 | ||
7fee226a ED |
251 | static inline void dst_use_noref(struct dst_entry *dst, unsigned long time) |
252 | { | |
253 | dst->__use++; | |
254 | dst->lastuse = time; | |
255 | } | |
256 | ||
7f95e188 | 257 | static inline struct dst_entry *dst_clone(struct dst_entry *dst) |
1da177e4 LT |
258 | { |
259 | if (dst) | |
260 | atomic_inc(&dst->__refcnt); | |
261 | return dst; | |
262 | } | |
263 | ||
a4023dd0 | 264 | void dst_release(struct dst_entry *dst); |
7fee226a ED |
265 | |
266 | static inline void refdst_drop(unsigned long refdst) | |
267 | { | |
268 | if (!(refdst & SKB_DST_NOREF)) | |
269 | dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK)); | |
270 | } | |
271 | ||
272 | /** | |
273 | * skb_dst_drop - drops skb dst | |
274 | * @skb: buffer | |
275 | * | |
276 | * Drops dst reference count if a reference was taken. | |
277 | */ | |
adf30907 ED |
278 | static inline void skb_dst_drop(struct sk_buff *skb) |
279 | { | |
7fee226a ED |
280 | if (skb->_skb_refdst) { |
281 | refdst_drop(skb->_skb_refdst); | |
282 | skb->_skb_refdst = 0UL; | |
283 | } | |
284 | } | |
285 | ||
286 | static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb) | |
287 | { | |
288 | nskb->_skb_refdst = oskb->_skb_refdst; | |
289 | if (!(nskb->_skb_refdst & SKB_DST_NOREF)) | |
290 | dst_clone(skb_dst(nskb)); | |
291 | } | |
292 | ||
293 | /** | |
294 | * skb_dst_force - makes sure skb dst is refcounted | |
295 | * @skb: buffer | |
296 | * | |
297 | * If dst is not yet refcounted, let's do it | |
298 | */ | |
299 | static inline void skb_dst_force(struct sk_buff *skb) | |
300 | { | |
301 | if (skb_dst_is_noref(skb)) { | |
302 | WARN_ON(!rcu_read_lock_held()); | |
303 | skb->_skb_refdst &= ~SKB_DST_NOREF; | |
304 | dst_clone(skb_dst(skb)); | |
305 | } | |
adf30907 | 306 | } |
1da177e4 | 307 | |
d19d56dd | 308 | |
290b895e ED |
309 | /** |
310 | * __skb_tunnel_rx - prepare skb for rx reinsert | |
311 | * @skb: buffer | |
312 | * @dev: tunnel device | |
ea23192e | 313 | * @net: netns for packet i/o |
290b895e ED |
314 | * |
315 | * After decapsulation, packet is going to re-enter (netif_rx()) our stack, | |
316 | * so make some cleanups. (no accounting done) | |
317 | */ | |
ea23192e ND |
318 | static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev, |
319 | struct net *net) | |
290b895e ED |
320 | { |
321 | skb->dev = dev; | |
bdeab991 TH |
322 | |
323 | /* | |
7539fadc | 324 | * Clear hash so that we can recalulate the hash for the |
bdeab991 TH |
325 | * encapsulated packet, unless we have already determine the hash |
326 | * over the L4 4-tuple. | |
327 | */ | |
7539fadc | 328 | skb_clear_hash_if_not_l4(skb); |
290b895e | 329 | skb_set_queue_mapping(skb, 0); |
ea23192e | 330 | skb_scrub_packet(skb, !net_eq(net, dev_net(dev))); |
290b895e ED |
331 | } |
332 | ||
d19d56dd ED |
333 | /** |
334 | * skb_tunnel_rx - prepare skb for rx reinsert | |
335 | * @skb: buffer | |
336 | * @dev: tunnel device | |
337 | * | |
338 | * After decapsulation, packet is going to re-enter (netif_rx()) our stack, | |
339 | * so make some cleanups, and perform accounting. | |
290b895e | 340 | * Note: this accounting is not SMP safe. |
d19d56dd | 341 | */ |
ea23192e ND |
342 | static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev, |
343 | struct net *net) | |
d19d56dd | 344 | { |
d19d56dd ED |
345 | /* TODO : stats should be SMP safe */ |
346 | dev->stats.rx_packets++; | |
347 | dev->stats.rx_bytes += skb->len; | |
ea23192e | 348 | __skb_tunnel_rx(skb, dev, net); |
d19d56dd ED |
349 | } |
350 | ||
1da177e4 LT |
351 | /* Children define the path of the packet through the |
352 | * Linux networking. Thus, destinations are stackable. | |
353 | */ | |
354 | ||
8764ab2c | 355 | static inline struct dst_entry *skb_dst_pop(struct sk_buff *skb) |
1da177e4 | 356 | { |
e433430a | 357 | struct dst_entry *child = dst_clone(skb_dst(skb)->child); |
1da177e4 | 358 | |
8764ab2c | 359 | skb_dst_drop(skb); |
1da177e4 LT |
360 | return child; |
361 | } | |
362 | ||
a4023dd0 JP |
363 | int dst_discard(struct sk_buff *skb); |
364 | void *dst_alloc(struct dst_ops *ops, struct net_device *dev, int initial_ref, | |
365 | int initial_obsolete, unsigned short flags); | |
366 | void __dst_free(struct dst_entry *dst); | |
367 | struct dst_entry *dst_destroy(struct dst_entry *dst); | |
1da177e4 | 368 | |
7f95e188 | 369 | static inline void dst_free(struct dst_entry *dst) |
1da177e4 | 370 | { |
f5b0a874 | 371 | if (dst->obsolete > 0) |
1da177e4 LT |
372 | return; |
373 | if (!atomic_read(&dst->__refcnt)) { | |
374 | dst = dst_destroy(dst); | |
375 | if (!dst) | |
376 | return; | |
377 | } | |
378 | __dst_free(dst); | |
379 | } | |
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 | ||
387 | static inline void dst_confirm(struct dst_entry *dst) | |
388 | { | |
5110effe DM |
389 | dst->pending_confirm = 1; |
390 | } | |
f2c31e32 | 391 | |
5110effe DM |
392 | static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n, |
393 | struct sk_buff *skb) | |
394 | { | |
425f09ab ED |
395 | const struct hh_cache *hh; |
396 | ||
397 | if (dst->pending_confirm) { | |
398 | unsigned long now = jiffies; | |
5110effe | 399 | |
5110effe | 400 | dst->pending_confirm = 0; |
425f09ab ED |
401 | /* avoid dirtying neighbour */ |
402 | if (n->confirmed != now) | |
403 | n->confirmed = now; | |
69cce1d1 | 404 | } |
5110effe DM |
405 | |
406 | hh = &n->hh; | |
407 | if ((n->nud_state & NUD_CONNECTED) && hh->hh_len) | |
408 | return neigh_hh_output(hh, skb); | |
409 | else | |
410 | return n->output(n, skb); | |
1da177e4 LT |
411 | } |
412 | ||
d3aaeb38 DM |
413 | static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr) |
414 | { | |
aaa0c23c ZZ |
415 | struct neighbour *n = dst->ops->neigh_lookup(dst, NULL, daddr); |
416 | return IS_ERR(n) ? NULL : n; | |
f894cbf8 DM |
417 | } |
418 | ||
419 | static inline struct neighbour *dst_neigh_lookup_skb(const struct dst_entry *dst, | |
420 | struct sk_buff *skb) | |
421 | { | |
aaa0c23c ZZ |
422 | struct neighbour *n = dst->ops->neigh_lookup(dst, skb, NULL); |
423 | return IS_ERR(n) ? NULL : n; | |
d3aaeb38 DM |
424 | } |
425 | ||
1da177e4 LT |
426 | static inline void dst_link_failure(struct sk_buff *skb) |
427 | { | |
adf30907 | 428 | struct dst_entry *dst = skb_dst(skb); |
1da177e4 LT |
429 | if (dst && dst->ops && dst->ops->link_failure) |
430 | dst->ops->link_failure(skb); | |
431 | } | |
432 | ||
433 | static inline void dst_set_expires(struct dst_entry *dst, int timeout) | |
434 | { | |
435 | unsigned long expires = jiffies + timeout; | |
436 | ||
437 | if (expires == 0) | |
438 | expires = 1; | |
439 | ||
440 | if (dst->expires == 0 || time_before(expires, dst->expires)) | |
441 | dst->expires = expires; | |
442 | } | |
443 | ||
444 | /* Output packet to network from transport. */ | |
445 | static inline int dst_output(struct sk_buff *skb) | |
446 | { | |
adf30907 | 447 | return skb_dst(skb)->output(skb); |
1da177e4 LT |
448 | } |
449 | ||
450 | /* Input packet from network to transport. */ | |
451 | static inline int dst_input(struct sk_buff *skb) | |
452 | { | |
adf30907 | 453 | return skb_dst(skb)->input(skb); |
1da177e4 LT |
454 | } |
455 | ||
456 | static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie) | |
457 | { | |
458 | if (dst->obsolete) | |
459 | dst = dst->ops->check(dst, cookie); | |
460 | return dst; | |
461 | } | |
462 | ||
a4023dd0 | 463 | void dst_init(void); |
1da177e4 | 464 | |
815f4e57 HX |
465 | /* Flags for xfrm_lookup flags argument. */ |
466 | enum { | |
80c0bc9e | 467 | XFRM_LOOKUP_ICMP = 1 << 0, |
815f4e57 HX |
468 | }; |
469 | ||
1da177e4 LT |
470 | struct flowi; |
471 | #ifndef CONFIG_XFRM | |
452edd59 DM |
472 | static inline struct dst_entry *xfrm_lookup(struct net *net, |
473 | struct dst_entry *dst_orig, | |
474 | const struct flowi *fl, struct sock *sk, | |
475 | int flags) | |
1da177e4 | 476 | { |
452edd59 | 477 | return dst_orig; |
1da177e4 | 478 | } |
e87b3998 VY |
479 | |
480 | static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst) | |
481 | { | |
482 | return NULL; | |
483 | } | |
484 | ||
1da177e4 | 485 | #else |
a4023dd0 JP |
486 | struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig, |
487 | const struct flowi *fl, struct sock *sk, | |
488 | int flags); | |
e87b3998 VY |
489 | |
490 | /* skb attached with this dst needs transformation if dst->xfrm is valid */ | |
491 | static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst) | |
492 | { | |
493 | return dst->xfrm; | |
494 | } | |
1da177e4 | 495 | #endif |
1da177e4 LT |
496 | |
497 | #endif /* _NET_DST_H */ |