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