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
;
125 if (p
& DST_METRICS_READ_ONLY
)
126 return dst
->ops
->cow_metrics(dst
, p
);
127 return __DST_METRICS_PTR(p
);
130 /* This may only be invoked before the entry has reached global
133 static inline void dst_init_metrics(struct dst_entry
*dst
,
134 const u32
*src_metrics
,
137 dst
->_metrics
= ((unsigned long) src_metrics
) |
138 (read_only
? DST_METRICS_READ_ONLY
: 0);
141 static inline void dst_copy_metrics(struct dst_entry
*dest
, const struct dst_entry
*src
)
143 u32
*dst_metrics
= dst_metrics_write_ptr(dest
);
146 u32
*src_metrics
= DST_METRICS_PTR(src
);
148 memcpy(dst_metrics
, src_metrics
, RTAX_MAX
* sizeof(u32
));
152 static inline u32
*dst_metrics_ptr(struct dst_entry
*dst
)
154 return DST_METRICS_PTR(dst
);
158 dst_metric_raw(const struct dst_entry
*dst
, const int metric
)
160 u32
*p
= DST_METRICS_PTR(dst
);
166 dst_metric(const struct dst_entry
*dst
, const int metric
)
168 WARN_ON_ONCE(metric
== RTAX_HOPLIMIT
||
169 metric
== RTAX_ADVMSS
||
171 return dst_metric_raw(dst
, metric
);
175 dst_metric_advmss(const struct dst_entry
*dst
)
177 u32 advmss
= dst_metric_raw(dst
, RTAX_ADVMSS
);
180 advmss
= dst
->ops
->default_advmss(dst
);
185 static inline void dst_metric_set(struct dst_entry
*dst
, int metric
, u32 val
)
187 u32
*p
= dst_metrics_write_ptr(dst
);
194 dst_feature(const struct dst_entry
*dst
, u32 feature
)
196 return dst_metric(dst
, RTAX_FEATURES
) & feature
;
199 static inline u32
dst_mtu(const struct dst_entry
*dst
)
201 u32 mtu
= dst_metric_raw(dst
, RTAX_MTU
);
204 mtu
= dst
->ops
->default_mtu(dst
);
209 /* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
210 static inline unsigned long dst_metric_rtt(const struct dst_entry
*dst
, int metric
)
212 return msecs_to_jiffies(dst_metric(dst
, metric
));
215 static inline void set_dst_metric_rtt(struct dst_entry
*dst
, int metric
,
218 dst_metric_set(dst
, metric
, jiffies_to_msecs(rtt
));
222 dst_allfrag(const struct dst_entry
*dst
)
224 int ret
= dst_feature(dst
, RTAX_FEATURE_ALLFRAG
);
229 dst_metric_locked(const struct dst_entry
*dst
, int metric
)
231 return dst_metric(dst
, RTAX_LOCK
) & (1<<metric
);
234 static inline void dst_hold(struct dst_entry
* dst
)
237 * If your kernel compilation stops here, please check
238 * __pad_to_align_refcnt declaration in struct dst_entry
240 BUILD_BUG_ON(offsetof(struct dst_entry
, __refcnt
) & 63);
241 atomic_inc(&dst
->__refcnt
);
244 static inline void dst_use(struct dst_entry
*dst
, unsigned long time
)
251 static inline void dst_use_noref(struct dst_entry
*dst
, unsigned long time
)
258 struct dst_entry
* dst_clone(struct dst_entry
* dst
)
261 atomic_inc(&dst
->__refcnt
);
265 extern void dst_release(struct dst_entry
*dst
);
267 static inline void refdst_drop(unsigned long refdst
)
269 if (!(refdst
& SKB_DST_NOREF
))
270 dst_release((struct dst_entry
*)(refdst
& SKB_DST_PTRMASK
));
274 * skb_dst_drop - drops skb dst
277 * Drops dst reference count if a reference was taken.
279 static inline void skb_dst_drop(struct sk_buff
*skb
)
281 if (skb
->_skb_refdst
) {
282 refdst_drop(skb
->_skb_refdst
);
283 skb
->_skb_refdst
= 0UL;
287 static inline void skb_dst_copy(struct sk_buff
*nskb
, const struct sk_buff
*oskb
)
289 nskb
->_skb_refdst
= oskb
->_skb_refdst
;
290 if (!(nskb
->_skb_refdst
& SKB_DST_NOREF
))
291 dst_clone(skb_dst(nskb
));
295 * skb_dst_force - makes sure skb dst is refcounted
298 * If dst is not yet refcounted, let's do it
300 static inline void skb_dst_force(struct sk_buff
*skb
)
302 if (skb_dst_is_noref(skb
)) {
303 WARN_ON(!rcu_read_lock_held());
304 skb
->_skb_refdst
&= ~SKB_DST_NOREF
;
305 dst_clone(skb_dst(skb
));
311 * __skb_tunnel_rx - prepare skb for rx reinsert
313 * @dev: tunnel device
315 * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
316 * so make some cleanups. (no accounting done)
318 static inline void __skb_tunnel_rx(struct sk_buff
*skb
, struct net_device
*dev
)
322 skb_set_queue_mapping(skb
, 0);
328 * skb_tunnel_rx - prepare skb for rx reinsert
330 * @dev: tunnel device
332 * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
333 * so make some cleanups, and perform accounting.
334 * Note: this accounting is not SMP safe.
336 static inline void skb_tunnel_rx(struct sk_buff
*skb
, struct net_device
*dev
)
338 /* TODO : stats should be SMP safe */
339 dev
->stats
.rx_packets
++;
340 dev
->stats
.rx_bytes
+= skb
->len
;
341 __skb_tunnel_rx(skb
, dev
);
344 /* Children define the path of the packet through the
345 * Linux networking. Thus, destinations are stackable.
348 static inline struct dst_entry
*skb_dst_pop(struct sk_buff
*skb
)
350 struct dst_entry
*child
= dst_clone(skb_dst(skb
)->child
);
356 extern int dst_discard(struct sk_buff
*skb
);
357 extern void *dst_alloc(struct dst_ops
* ops
, int initial_ref
);
358 extern void __dst_free(struct dst_entry
* dst
);
359 extern struct dst_entry
*dst_destroy(struct dst_entry
* dst
);
361 static inline void dst_free(struct dst_entry
* dst
)
363 if (dst
->obsolete
> 1)
365 if (!atomic_read(&dst
->__refcnt
)) {
366 dst
= dst_destroy(dst
);
373 static inline void dst_rcu_free(struct rcu_head
*head
)
375 struct dst_entry
*dst
= container_of(head
, struct dst_entry
, rcu_head
);
379 static inline void dst_confirm(struct dst_entry
*dst
)
382 neigh_confirm(dst
->neighbour
);
385 static inline void dst_link_failure(struct sk_buff
*skb
)
387 struct dst_entry
*dst
= skb_dst(skb
);
388 if (dst
&& dst
->ops
&& dst
->ops
->link_failure
)
389 dst
->ops
->link_failure(skb
);
392 static inline void dst_set_expires(struct dst_entry
*dst
, int timeout
)
394 unsigned long expires
= jiffies
+ timeout
;
399 if (dst
->expires
== 0 || time_before(expires
, dst
->expires
))
400 dst
->expires
= expires
;
403 /* Output packet to network from transport. */
404 static inline int dst_output(struct sk_buff
*skb
)
406 return skb_dst(skb
)->output(skb
);
409 /* Input packet from network to transport. */
410 static inline int dst_input(struct sk_buff
*skb
)
412 return skb_dst(skb
)->input(skb
);
415 static inline struct dst_entry
*dst_check(struct dst_entry
*dst
, u32 cookie
)
418 dst
= dst
->ops
->check(dst
, cookie
);
422 extern void dst_init(void);
424 /* Flags for xfrm_lookup flags argument. */
426 XFRM_LOOKUP_ICMP
= 1 << 0,
431 static inline struct dst_entry
*xfrm_lookup(struct net
*net
,
432 struct dst_entry
*dst_orig
,
433 const struct flowi
*fl
, struct sock
*sk
,
439 extern struct dst_entry
*xfrm_lookup(struct net
*net
, struct dst_entry
*dst_orig
,
440 const struct flowi
*fl
, struct sock
*sk
,
445 #endif /* _NET_DST_H */