2 * net/dst.h Protocol independent destination cache definitions.
4 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 #include <net/dst_ops.h>
12 #include <linux/netdevice.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/rcupdate.h>
15 #include <linux/jiffies.h>
16 #include <net/neighbour.h>
17 #include <asm/processor.h>
20 * 0 - no debugging messages
21 * 1 - rare events and bugs (default)
24 #define RT_CACHE_DEBUG 0
26 #define DST_GC_MIN (HZ/10)
27 #define DST_GC_INC (HZ/2)
28 #define DST_GC_MAX (120*HZ)
30 /* Each dst_entry has reference count and sits in some parent list(s).
31 * When it is removed from parent list, it is "freed" (dst_free).
32 * After this it enters dead state (dst->obsolete > 0) and if its refcnt
33 * is zero, it can be destroyed immediately, otherwise it is added
34 * to gc list and garbage collector periodically checks the refcnt.
40 struct rcu_head rcu_head
;
41 struct dst_entry
*child
;
42 struct net_device
*dev
;
44 unsigned long _metrics
;
45 unsigned long expires
;
46 struct dst_entry
*path
;
47 struct neighbour
*neighbour
;
50 struct xfrm_state
*xfrm
;
54 int (*input
)(struct sk_buff
*);
55 int (*output
)(struct sk_buff
*);
59 unsigned short header_len
; /* more space at head required */
60 unsigned short trailer_len
; /* space to reserve at tail */
61 #ifdef CONFIG_IP_ROUTE_CLASSID
68 * Align __refcnt to a 64 bytes alignment
69 * (L1_CACHE_SIZE would be too much)
72 long __pad_to_align_refcnt
[1];
75 * __refcnt wants to be on a different cache line from
76 * input/output/ops or performance tanks badly
78 atomic_t __refcnt
; /* client references */
80 unsigned long lastuse
;
82 #define DST_HOST 0x0001
83 #define DST_NOXFRM 0x0002
84 #define DST_NOPOLICY 0x0004
85 #define DST_NOHASH 0x0008
86 #define DST_NOCACHE 0x0010
88 struct dst_entry
*next
;
89 struct rtable __rcu
*rt_next
;
90 struct rt6_info
*rt6_next
;
91 struct dn_route __rcu
*dn_next
;
97 extern u32
*dst_cow_metrics_generic(struct dst_entry
*dst
, unsigned long old
);
98 extern const u32 dst_default_metrics
[RTAX_MAX
];
100 #define DST_METRICS_READ_ONLY 0x1UL
101 #define __DST_METRICS_PTR(Y) \
102 ((u32 *)((Y) & ~DST_METRICS_READ_ONLY))
103 #define DST_METRICS_PTR(X) __DST_METRICS_PTR((X)->_metrics)
105 static inline bool dst_metrics_read_only(const struct dst_entry
*dst
)
107 return dst
->_metrics
& DST_METRICS_READ_ONLY
;
110 extern void __dst_destroy_metrics_generic(struct dst_entry
*dst
, unsigned long old
);
112 static inline void dst_destroy_metrics_generic(struct dst_entry
*dst
)
114 unsigned long val
= dst
->_metrics
;
115 if (!(val
& DST_METRICS_READ_ONLY
))
116 __dst_destroy_metrics_generic(dst
, val
);
119 static inline u32
*dst_metrics_write_ptr(struct dst_entry
*dst
)
121 unsigned long p
= dst
->_metrics
;
123 if (p
& DST_METRICS_READ_ONLY
)
124 return dst
->ops
->cow_metrics(dst
, p
);
125 return __DST_METRICS_PTR(p
);
128 /* This may only be invoked before the entry has reached global
131 static inline void dst_init_metrics(struct dst_entry
*dst
,
132 const u32
*src_metrics
,
135 dst
->_metrics
= ((unsigned long) src_metrics
) |
136 (read_only
? DST_METRICS_READ_ONLY
: 0);
139 static inline void dst_copy_metrics(struct dst_entry
*dest
, const struct dst_entry
*src
)
141 u32
*dst_metrics
= dst_metrics_write_ptr(dest
);
144 u32
*src_metrics
= DST_METRICS_PTR(src
);
146 memcpy(dst_metrics
, src_metrics
, RTAX_MAX
* sizeof(u32
));
150 static inline u32
*dst_metrics_ptr(struct dst_entry
*dst
)
152 return DST_METRICS_PTR(dst
);
156 dst_metric_raw(const struct dst_entry
*dst
, const int metric
)
158 u32
*p
= DST_METRICS_PTR(dst
);
164 dst_metric(const struct dst_entry
*dst
, const int metric
)
166 WARN_ON_ONCE(metric
== RTAX_HOPLIMIT
||
167 metric
== RTAX_ADVMSS
||
169 return dst_metric_raw(dst
, metric
);
173 dst_metric_advmss(const struct dst_entry
*dst
)
175 u32 advmss
= dst_metric_raw(dst
, RTAX_ADVMSS
);
178 advmss
= dst
->ops
->default_advmss(dst
);
183 static inline void dst_metric_set(struct dst_entry
*dst
, int metric
, u32 val
)
185 u32
*p
= dst_metrics_write_ptr(dst
);
192 dst_feature(const struct dst_entry
*dst
, u32 feature
)
194 return dst_metric(dst
, RTAX_FEATURES
) & feature
;
197 static inline u32
dst_mtu(const struct dst_entry
*dst
)
199 u32 mtu
= dst_metric_raw(dst
, RTAX_MTU
);
202 mtu
= dst
->ops
->default_mtu(dst
);
207 /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
208 static inline unsigned long dst_metric_rtt(const struct dst_entry
*dst
, int metric
)
210 return msecs_to_jiffies(dst_metric(dst
, metric
));
213 static inline void set_dst_metric_rtt(struct dst_entry
*dst
, int metric
,
216 dst_metric_set(dst
, metric
, jiffies_to_msecs(rtt
));
220 dst_allfrag(const struct dst_entry
*dst
)
222 int ret
= dst_feature(dst
, RTAX_FEATURE_ALLFRAG
);
227 dst_metric_locked(const struct dst_entry
*dst
, int metric
)
229 return dst_metric(dst
, RTAX_LOCK
) & (1<<metric
);
232 static inline void dst_hold(struct dst_entry
* dst
)
235 * If your kernel compilation stops here, please check
236 * __pad_to_align_refcnt declaration in struct dst_entry
238 BUILD_BUG_ON(offsetof(struct dst_entry
, __refcnt
) & 63);
239 atomic_inc(&dst
->__refcnt
);
242 static inline void dst_use(struct dst_entry
*dst
, unsigned long time
)
249 static inline void dst_use_noref(struct dst_entry
*dst
, unsigned long time
)
256 struct dst_entry
* dst_clone(struct dst_entry
* dst
)
259 atomic_inc(&dst
->__refcnt
);
263 extern void dst_release(struct dst_entry
*dst
);
265 static inline void refdst_drop(unsigned long refdst
)
267 if (!(refdst
& SKB_DST_NOREF
))
268 dst_release((struct dst_entry
*)(refdst
& SKB_DST_PTRMASK
));
272 * skb_dst_drop - drops skb dst
275 * Drops dst reference count if a reference was taken.
277 static inline void skb_dst_drop(struct sk_buff
*skb
)
279 if (skb
->_skb_refdst
) {
280 refdst_drop(skb
->_skb_refdst
);
281 skb
->_skb_refdst
= 0UL;
285 static inline void skb_dst_copy(struct sk_buff
*nskb
, const struct sk_buff
*oskb
)
287 nskb
->_skb_refdst
= oskb
->_skb_refdst
;
288 if (!(nskb
->_skb_refdst
& SKB_DST_NOREF
))
289 dst_clone(skb_dst(nskb
));
293 * skb_dst_force - makes sure skb dst is refcounted
296 * If dst is not yet refcounted, let's do it
298 static inline void skb_dst_force(struct sk_buff
*skb
)
300 if (skb_dst_is_noref(skb
)) {
301 WARN_ON(!rcu_read_lock_held());
302 skb
->_skb_refdst
&= ~SKB_DST_NOREF
;
303 dst_clone(skb_dst(skb
));
309 * __skb_tunnel_rx - prepare skb for rx reinsert
311 * @dev: tunnel device
313 * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
314 * so make some cleanups. (no accounting done)
316 static inline void __skb_tunnel_rx(struct sk_buff
*skb
, struct net_device
*dev
)
320 skb_set_queue_mapping(skb
, 0);
326 * skb_tunnel_rx - prepare skb for rx reinsert
328 * @dev: tunnel device
330 * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
331 * so make some cleanups, and perform accounting.
332 * Note: this accounting is not SMP safe.
334 static inline void skb_tunnel_rx(struct sk_buff
*skb
, struct net_device
*dev
)
336 /* TODO : stats should be SMP safe */
337 dev
->stats
.rx_packets
++;
338 dev
->stats
.rx_bytes
+= skb
->len
;
339 __skb_tunnel_rx(skb
, dev
);
342 /* Children define the path of the packet through the
343 * Linux networking. Thus, destinations are stackable.
346 static inline struct dst_entry
*skb_dst_pop(struct sk_buff
*skb
)
348 struct dst_entry
*child
= skb_dst(skb
)->child
;
354 extern int dst_discard(struct sk_buff
*skb
);
355 extern void *dst_alloc(struct dst_ops
* ops
, int initial_ref
);
356 extern void __dst_free(struct dst_entry
* dst
);
357 extern struct dst_entry
*dst_destroy(struct dst_entry
* dst
);
359 static inline void dst_free(struct dst_entry
* dst
)
361 if (dst
->obsolete
> 1)
363 if (!atomic_read(&dst
->__refcnt
)) {
364 dst
= dst_destroy(dst
);
371 static inline void dst_rcu_free(struct rcu_head
*head
)
373 struct dst_entry
*dst
= container_of(head
, struct dst_entry
, rcu_head
);
377 static inline void dst_confirm(struct dst_entry
*dst
)
380 neigh_confirm(dst
->neighbour
);
383 static inline void dst_link_failure(struct sk_buff
*skb
)
385 struct dst_entry
*dst
= skb_dst(skb
);
386 if (dst
&& dst
->ops
&& dst
->ops
->link_failure
)
387 dst
->ops
->link_failure(skb
);
390 static inline void dst_set_expires(struct dst_entry
*dst
, int timeout
)
392 unsigned long expires
= jiffies
+ timeout
;
397 if (dst
->expires
== 0 || time_before(expires
, dst
->expires
))
398 dst
->expires
= expires
;
401 /* Output packet to network from transport. */
402 static inline int dst_output(struct sk_buff
*skb
)
404 return skb_dst(skb
)->output(skb
);
407 /* Input packet from network to transport. */
408 static inline int dst_input(struct sk_buff
*skb
)
410 return skb_dst(skb
)->input(skb
);
413 static inline struct dst_entry
*dst_check(struct dst_entry
*dst
, u32 cookie
)
416 dst
= dst
->ops
->check(dst
, cookie
);
420 extern void dst_init(void);
422 /* Flags for xfrm_lookup flags argument. */
424 XFRM_LOOKUP_ICMP
= 1 << 0,
429 static inline struct dst_entry
*xfrm_lookup(struct net
*net
,
430 struct dst_entry
*dst_orig
,
431 const struct flowi
*fl
, struct sock
*sk
,
437 extern struct dst_entry
*xfrm_lookup(struct net
*net
, struct dst_entry
*dst_orig
,
438 const struct flowi
*fl
, struct sock
*sk
,
443 #endif /* _NET_DST_H */