x86/pci-calgary_64.c: Remove obsoleted simple_strtoul() usage
[linux-2.6/btrfs-unstable.git] / include / net / dst.h
blob59c5d18cc3857da85690627b085e1065eeeb78ad
1 /*
2 * net/dst.h Protocol independent destination cache definitions.
4 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 */
8 #ifndef _NET_DST_H
9 #define _NET_DST_H
11 #include <net/dst_ops.h>
12 #include <linux/netdevice.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/rcupdate.h>
15 #include <linux/bug.h>
16 #include <linux/jiffies.h>
17 #include <net/neighbour.h>
18 #include <asm/processor.h>
20 #define DST_GC_MIN (HZ/10)
21 #define DST_GC_INC (HZ/2)
22 #define DST_GC_MAX (120*HZ)
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.
31 struct sk_buff;
33 struct dst_entry {
34 struct rcu_head rcu_head;
35 struct dst_entry *child;
36 struct net_device *dev;
37 struct dst_ops *ops;
38 unsigned long _metrics;
39 unsigned long expires;
40 struct dst_entry *path;
41 struct neighbour __rcu *_neighbour;
42 #ifdef CONFIG_XFRM
43 struct xfrm_state *xfrm;
44 #else
45 void *__pad1;
46 #endif
47 int (*input)(struct sk_buff*);
48 int (*output)(struct sk_buff*);
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
57 #define DST_NOPEER 0x0040
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 */
63 #ifdef CONFIG_IP_ROUTE_CLASSID
64 __u32 tclassid;
65 #else
66 __u32 __pad2;
67 #endif
70 * Align __refcnt to a 64 bytes alignment
71 * (L1_CACHE_SIZE would be too much)
73 #ifdef CONFIG_64BIT
74 long __pad_to_align_refcnt[2];
75 #endif
77 * __refcnt wants to be on a different cache line from
78 * input/output/ops or performance tanks badly
80 atomic_t __refcnt; /* client references */
81 int __use;
82 unsigned long lastuse;
83 union {
84 struct dst_entry *next;
85 struct rtable __rcu *rt_next;
86 struct rt6_info *rt6_next;
87 struct dn_route __rcu *dn_next;
91 static inline struct neighbour *dst_get_neighbour_noref(struct dst_entry *dst)
93 return rcu_dereference(dst->_neighbour);
96 static inline struct neighbour *dst_get_neighbour_noref_raw(struct dst_entry *dst)
98 return rcu_dereference_raw(dst->_neighbour);
101 static inline void dst_set_neighbour(struct dst_entry *dst, struct neighbour *neigh)
103 rcu_assign_pointer(dst->_neighbour, neigh);
106 extern u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
107 extern const u32 dst_default_metrics[RTAX_MAX];
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)
114 static inline bool dst_metrics_read_only(const struct dst_entry *dst)
116 return dst->_metrics & DST_METRICS_READ_ONLY;
119 extern void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
121 static inline void dst_destroy_metrics_generic(struct dst_entry *dst)
123 unsigned long val = dst->_metrics;
124 if (!(val & DST_METRICS_READ_ONLY))
125 __dst_destroy_metrics_generic(dst, val);
128 static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
130 unsigned long p = dst->_metrics;
132 BUG_ON(!p);
134 if (p & DST_METRICS_READ_ONLY)
135 return dst->ops->cow_metrics(dst, p);
136 return __DST_METRICS_PTR(p);
139 /* This may only be invoked before the entry has reached global
140 * visibility.
142 static inline void dst_init_metrics(struct dst_entry *dst,
143 const u32 *src_metrics,
144 bool read_only)
146 dst->_metrics = ((unsigned long) src_metrics) |
147 (read_only ? DST_METRICS_READ_ONLY : 0);
150 static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
152 u32 *dst_metrics = dst_metrics_write_ptr(dest);
154 if (dst_metrics) {
155 u32 *src_metrics = DST_METRICS_PTR(src);
157 memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
161 static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
163 return DST_METRICS_PTR(dst);
166 static inline u32
167 dst_metric_raw(const struct dst_entry *dst, const int metric)
169 u32 *p = DST_METRICS_PTR(dst);
171 return p[metric-1];
174 static inline u32
175 dst_metric(const struct dst_entry *dst, const int metric)
177 WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
178 metric == RTAX_ADVMSS ||
179 metric == RTAX_MTU);
180 return dst_metric_raw(dst, metric);
183 static inline u32
184 dst_metric_advmss(const struct dst_entry *dst)
186 u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS);
188 if (!advmss)
189 advmss = dst->ops->default_advmss(dst);
191 return advmss;
194 static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
196 u32 *p = dst_metrics_write_ptr(dst);
198 if (p)
199 p[metric-1] = val;
202 static inline u32
203 dst_feature(const struct dst_entry *dst, u32 feature)
205 return dst_metric(dst, RTAX_FEATURES) & feature;
208 static inline u32 dst_mtu(const struct dst_entry *dst)
210 return dst->ops->mtu(dst);
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)
216 return msecs_to_jiffies(dst_metric(dst, metric));
219 static inline void set_dst_metric_rtt(struct dst_entry *dst, int metric,
220 unsigned long rtt)
222 dst_metric_set(dst, metric, jiffies_to_msecs(rtt));
225 static inline u32
226 dst_allfrag(const struct dst_entry *dst)
228 int ret = dst_feature(dst, RTAX_FEATURE_ALLFRAG);
229 return ret;
232 static inline int
233 dst_metric_locked(const struct dst_entry *dst, int metric)
235 return dst_metric(dst, RTAX_LOCK) & (1<<metric);
238 static inline void dst_hold(struct dst_entry * dst)
241 * If your kernel compilation stops here, please check
242 * __pad_to_align_refcnt declaration in struct dst_entry
244 BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
245 atomic_inc(&dst->__refcnt);
248 static inline void dst_use(struct dst_entry *dst, unsigned long time)
250 dst_hold(dst);
251 dst->__use++;
252 dst->lastuse = time;
255 static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
257 dst->__use++;
258 dst->lastuse = time;
261 static inline
262 struct dst_entry * dst_clone(struct dst_entry * dst)
264 if (dst)
265 atomic_inc(&dst->__refcnt);
266 return dst;
269 extern void dst_release(struct dst_entry *dst);
271 static inline void refdst_drop(unsigned long refdst)
273 if (!(refdst & SKB_DST_NOREF))
274 dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
278 * skb_dst_drop - drops skb dst
279 * @skb: buffer
281 * Drops dst reference count if a reference was taken.
283 static inline void skb_dst_drop(struct sk_buff *skb)
285 if (skb->_skb_refdst) {
286 refdst_drop(skb->_skb_refdst);
287 skb->_skb_refdst = 0UL;
291 static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
293 nskb->_skb_refdst = oskb->_skb_refdst;
294 if (!(nskb->_skb_refdst & SKB_DST_NOREF))
295 dst_clone(skb_dst(nskb));
299 * skb_dst_force - makes sure skb dst is refcounted
300 * @skb: buffer
302 * If dst is not yet refcounted, let's do it
304 static inline void skb_dst_force(struct sk_buff *skb)
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));
315 * __skb_tunnel_rx - prepare skb for rx reinsert
316 * @skb: buffer
317 * @dev: tunnel device
319 * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
320 * so make some cleanups. (no accounting done)
322 static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev)
324 skb->dev = dev;
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.
331 if (!skb->l4_rxhash)
332 skb->rxhash = 0;
333 skb_set_queue_mapping(skb, 0);
334 skb_dst_drop(skb);
335 nf_reset(skb);
339 * skb_tunnel_rx - prepare skb for rx reinsert
340 * @skb: buffer
341 * @dev: tunnel device
343 * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
344 * so make some cleanups, and perform accounting.
345 * Note: this accounting is not SMP safe.
347 static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev)
349 /* TODO : stats should be SMP safe */
350 dev->stats.rx_packets++;
351 dev->stats.rx_bytes += skb->len;
352 __skb_tunnel_rx(skb, dev);
355 /* Children define the path of the packet through the
356 * Linux networking. Thus, destinations are stackable.
359 static inline struct dst_entry *skb_dst_pop(struct sk_buff *skb)
361 struct dst_entry *child = dst_clone(skb_dst(skb)->child);
363 skb_dst_drop(skb);
364 return child;
367 extern int dst_discard(struct sk_buff *skb);
368 extern void *dst_alloc(struct dst_ops * ops, struct net_device *dev,
369 int initial_ref, int initial_obsolete, int flags);
370 extern void __dst_free(struct dst_entry * dst);
371 extern struct dst_entry *dst_destroy(struct dst_entry * dst);
373 static inline void dst_free(struct dst_entry * dst)
375 if (dst->obsolete > 1)
376 return;
377 if (!atomic_read(&dst->__refcnt)) {
378 dst = dst_destroy(dst);
379 if (!dst)
380 return;
382 __dst_free(dst);
385 static inline void dst_rcu_free(struct rcu_head *head)
387 struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
388 dst_free(dst);
391 static inline void dst_confirm(struct dst_entry *dst)
393 if (dst) {
394 struct neighbour *n;
396 rcu_read_lock();
397 n = dst_get_neighbour_noref(dst);
398 neigh_confirm(n);
399 rcu_read_unlock();
403 static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
405 return dst->ops->neigh_lookup(dst, daddr);
408 static inline void dst_link_failure(struct sk_buff *skb)
410 struct dst_entry *dst = skb_dst(skb);
411 if (dst && dst->ops && dst->ops->link_failure)
412 dst->ops->link_failure(skb);
415 static inline void dst_set_expires(struct dst_entry *dst, int timeout)
417 unsigned long expires = jiffies + timeout;
419 if (expires == 0)
420 expires = 1;
422 if (dst->expires == 0 || time_before(expires, dst->expires))
423 dst->expires = expires;
426 /* Output packet to network from transport. */
427 static inline int dst_output(struct sk_buff *skb)
429 return skb_dst(skb)->output(skb);
432 /* Input packet from network to transport. */
433 static inline int dst_input(struct sk_buff *skb)
435 return skb_dst(skb)->input(skb);
438 static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
440 if (dst->obsolete)
441 dst = dst->ops->check(dst, cookie);
442 return dst;
445 extern void dst_init(void);
447 /* Flags for xfrm_lookup flags argument. */
448 enum {
449 XFRM_LOOKUP_ICMP = 1 << 0,
452 struct flowi;
453 #ifndef CONFIG_XFRM
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)
459 return dst_orig;
461 #else
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);
465 #endif
467 #endif /* _NET_DST_H */