blob: 2588a9a88cc6dde8c1ea77981461723b1651b521 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Alexey Dobriyan86393e52009-08-29 01:34:49 +000011#include <net/dst_ops.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020012#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/rtnetlink.h>
14#include <linux/rcupdate.h>
15#include <linux/jiffies.h>
16#include <net/neighbour.h>
17#include <asm/processor.h>
18
19/*
20 * 0 - no debugging messages
21 * 1 - rare events and bugs (default)
22 * 2 - trace mode.
23 */
24#define RT_CACHE_DEBUG 0
25
26#define DST_GC_MIN (HZ/10)
27#define DST_GC_INC (HZ/2)
28#define DST_GC_MAX (120*HZ)
29
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.
35 */
36
37struct sk_buff;
38
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000039struct dst_entry {
Eric Dumazet1e19e022007-02-09 16:26:55 -080040 struct rcu_head rcu_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 struct dst_entry *child;
42 struct net_device *dev;
David S. Miller62fa8a82011-01-26 20:51:05 -080043 struct dst_ops *ops;
44 unsigned long _metrics;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 unsigned long expires;
Zhang Yanminf1dd9c32008-03-12 22:52:37 -070046 struct dst_entry *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct neighbour *neighbour;
48 struct hh_cache *hh;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -070049#ifdef CONFIG_XFRM
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct xfrm_state *xfrm;
Eric Dumazet5635c102008-11-16 19:46:36 -080051#else
52 void *__pad1;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -070053#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 int (*input)(struct sk_buff*);
55 int (*output)(struct sk_buff*);
56
David S. Miller62fa8a82011-01-26 20:51:05 -080057 short error;
58 short obsolete;
59 unsigned short header_len; /* more space at head required */
60 unsigned short trailer_len; /* space to reserve at tail */
Patrick McHardyc7066f72011-01-14 13:36:42 +010061#ifdef CONFIG_IP_ROUTE_CLASSID
Zhang Yanminf1dd9c32008-03-12 22:52:37 -070062 __u32 tclassid;
Eric Dumazet5635c102008-11-16 19:46:36 -080063#else
64 __u32 __pad2;
Zhang Yanminf1dd9c32008-03-12 22:52:37 -070065#endif
66
Eric Dumazet5635c102008-11-16 19:46:36 -080067 /*
68 * Align __refcnt to a 64 bytes alignment
69 * (L1_CACHE_SIZE would be too much)
70 */
71#ifdef CONFIG_64BIT
Eric Dumazet5635c102008-11-16 19:46:36 -080072 long __pad_to_align_refcnt[1];
73#endif
Zhang Yanminf1dd9c32008-03-12 22:52:37 -070074 /*
75 * __refcnt wants to be on a different cache line from
76 * input/output/ops or performance tanks badly
77 */
Eric Dumazet1e19e022007-02-09 16:26:55 -080078 atomic_t __refcnt; /* client references */
79 int __use;
Zhang Yanminf1dd9c32008-03-12 22:52:37 -070080 unsigned long lastuse;
David S. Miller62fa8a82011-01-26 20:51:05 -080081 int flags;
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
Eric Dumazet1e19e022007-02-09 16:26:55 -080087 union {
Eric Dumazetfc766e4c2010-10-29 03:09:24 +000088 struct dst_entry *next;
89 struct rtable __rcu *rt_next;
90 struct rt6_info *rt6_next;
91 struct dn_route __rcu *dn_next;
Eric Dumazet1e19e022007-02-09 16:26:55 -080092 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
David S. Miller62fa8a82011-01-26 20:51:05 -080095extern u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
David S. Miller725d1e12011-01-28 14:05:05 -080096extern const u32 dst_default_metrics[RTAX_MAX];
David S. Miller62fa8a82011-01-26 20:51:05 -080097
98#define DST_METRICS_READ_ONLY 0x1UL
99#define __DST_METRICS_PTR(Y) \
100 ((u32 *)((Y) & ~DST_METRICS_READ_ONLY))
101#define DST_METRICS_PTR(X) __DST_METRICS_PTR((X)->_metrics)
102
103static inline bool dst_metrics_read_only(const struct dst_entry *dst)
104{
105 return dst->_metrics & DST_METRICS_READ_ONLY;
106}
107
108extern void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
109
110static inline void dst_destroy_metrics_generic(struct dst_entry *dst)
111{
112 unsigned long val = dst->_metrics;
113 if (!(val & DST_METRICS_READ_ONLY))
114 __dst_destroy_metrics_generic(dst, val);
115}
116
117static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
118{
119 unsigned long p = dst->_metrics;
120
121 if (p & DST_METRICS_READ_ONLY)
122 return dst->ops->cow_metrics(dst, p);
123 return __DST_METRICS_PTR(p);
124}
125
126/* This may only be invoked before the entry has reached global
127 * visibility.
128 */
129static inline void dst_init_metrics(struct dst_entry *dst,
130 const u32 *src_metrics,
131 bool read_only)
132{
133 dst->_metrics = ((unsigned long) src_metrics) |
134 (read_only ? DST_METRICS_READ_ONLY : 0);
135}
136
137static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
138{
139 u32 *dst_metrics = dst_metrics_write_ptr(dest);
140
141 if (dst_metrics) {
142 u32 *src_metrics = DST_METRICS_PTR(src);
143
144 memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
145 }
146}
147
148static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
149{
150 return DST_METRICS_PTR(dst);
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static inline u32
David S. Miller5170ae82010-12-12 21:35:57 -0800154dst_metric_raw(const struct dst_entry *dst, const int metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
David S. Miller62fa8a82011-01-26 20:51:05 -0800156 u32 *p = DST_METRICS_PTR(dst);
157
158 return p[metric-1];
David S. Millerdefb3512010-12-08 21:16:57 -0800159}
160
David S. Miller5170ae82010-12-12 21:35:57 -0800161static inline u32
162dst_metric(const struct dst_entry *dst, const int metric)
163{
David S. Miller0dbaee32010-12-13 12:52:14 -0800164 WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
David S. Millerd33e4552010-12-14 13:01:14 -0800165 metric == RTAX_ADVMSS ||
166 metric == RTAX_MTU);
David S. Miller5170ae82010-12-12 21:35:57 -0800167 return dst_metric_raw(dst, metric);
168}
169
David S. Miller0dbaee32010-12-13 12:52:14 -0800170static inline u32
171dst_metric_advmss(const struct dst_entry *dst)
172{
173 u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS);
174
175 if (!advmss)
176 advmss = dst->ops->default_advmss(dst);
177
178 return advmss;
179}
180
David S. Millerdefb3512010-12-08 21:16:57 -0800181static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
182{
David S. Miller62fa8a82011-01-26 20:51:05 -0800183 u32 *p = dst_metrics_write_ptr(dst);
David S. Millerdefb3512010-12-08 21:16:57 -0800184
David S. Miller62fa8a82011-01-26 20:51:05 -0800185 if (p)
186 p[metric-1] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Gilad Ben-Yossef0c3adfb2009-10-28 04:15:23 +0000189static inline u32
190dst_feature(const struct dst_entry *dst, u32 feature)
191{
David S. Millerbb5b7c12009-12-15 20:56:42 -0800192 return dst_metric(dst, RTAX_FEATURES) & feature;
Gilad Ben-Yossef0c3adfb2009-10-28 04:15:23 +0000193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static inline u32 dst_mtu(const struct dst_entry *dst)
196{
David S. Millerd33e4552010-12-14 13:01:14 -0800197 u32 mtu = dst_metric_raw(dst, RTAX_MTU);
198
199 if (!mtu)
200 mtu = dst->ops->default_mtu(dst);
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return mtu;
203}
204
Stephen Hemmingerc1e20f72008-07-18 23:02:15 -0700205/* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
206static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
207{
208 return msecs_to_jiffies(dst_metric(dst, metric));
209}
210
211static inline void set_dst_metric_rtt(struct dst_entry *dst, int metric,
212 unsigned long rtt)
213{
David S. Millerdefb3512010-12-08 21:16:57 -0800214 dst_metric_set(dst, metric, jiffies_to_msecs(rtt));
Stephen Hemmingerc1e20f72008-07-18 23:02:15 -0700215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static inline u32
218dst_allfrag(const struct dst_entry *dst)
219{
Gilad Ben-Yossef0c3adfb2009-10-28 04:15:23 +0000220 int ret = dst_feature(dst, RTAX_FEATURE_ALLFRAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return ret;
222}
223
224static inline int
David S. Millerd33e4552010-12-14 13:01:14 -0800225dst_metric_locked(const struct dst_entry *dst, int metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 return dst_metric(dst, RTAX_LOCK) & (1<<metric);
228}
229
230static inline void dst_hold(struct dst_entry * dst)
231{
Eric Dumazet5635c102008-11-16 19:46:36 -0800232 /*
233 * If your kernel compilation stops here, please check
234 * __pad_to_align_refcnt declaration in struct dst_entry
235 */
236 BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 atomic_inc(&dst->__refcnt);
238}
239
Pavel Emelyanov03f49f32007-11-10 21:28:34 -0800240static inline void dst_use(struct dst_entry *dst, unsigned long time)
241{
242 dst_hold(dst);
243 dst->__use++;
244 dst->lastuse = time;
245}
246
Eric Dumazet7fee2262010-05-11 23:19:48 +0000247static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
248{
249 dst->__use++;
250 dst->lastuse = time;
251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static inline
254struct dst_entry * dst_clone(struct dst_entry * dst)
255{
256 if (dst)
257 atomic_inc(&dst->__refcnt);
258 return dst;
259}
260
Ilpo Järvinen8d330862008-03-27 17:53:31 -0700261extern void dst_release(struct dst_entry *dst);
Eric Dumazet7fee2262010-05-11 23:19:48 +0000262
263static inline void refdst_drop(unsigned long refdst)
264{
265 if (!(refdst & SKB_DST_NOREF))
266 dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
267}
268
269/**
270 * skb_dst_drop - drops skb dst
271 * @skb: buffer
272 *
273 * Drops dst reference count if a reference was taken.
274 */
Eric Dumazetadf30902009-06-02 05:19:30 +0000275static inline void skb_dst_drop(struct sk_buff *skb)
276{
Eric Dumazet7fee2262010-05-11 23:19:48 +0000277 if (skb->_skb_refdst) {
278 refdst_drop(skb->_skb_refdst);
279 skb->_skb_refdst = 0UL;
280 }
281}
282
283static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
284{
285 nskb->_skb_refdst = oskb->_skb_refdst;
286 if (!(nskb->_skb_refdst & SKB_DST_NOREF))
287 dst_clone(skb_dst(nskb));
288}
289
290/**
291 * skb_dst_force - makes sure skb dst is refcounted
292 * @skb: buffer
293 *
294 * If dst is not yet refcounted, let's do it
295 */
296static inline void skb_dst_force(struct sk_buff *skb)
297{
298 if (skb_dst_is_noref(skb)) {
299 WARN_ON(!rcu_read_lock_held());
300 skb->_skb_refdst &= ~SKB_DST_NOREF;
301 dst_clone(skb_dst(skb));
302 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000303}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700305
306/**
Eric Dumazet290b8952010-09-27 00:33:35 +0000307 * __skb_tunnel_rx - prepare skb for rx reinsert
308 * @skb: buffer
309 * @dev: tunnel device
310 *
311 * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
312 * so make some cleanups. (no accounting done)
313 */
314static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev)
315{
316 skb->dev = dev;
317 skb->rxhash = 0;
318 skb_set_queue_mapping(skb, 0);
319 skb_dst_drop(skb);
320 nf_reset(skb);
321}
322
323/**
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700324 * skb_tunnel_rx - prepare skb for rx reinsert
325 * @skb: buffer
326 * @dev: tunnel device
327 *
328 * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
329 * so make some cleanups, and perform accounting.
Eric Dumazet290b8952010-09-27 00:33:35 +0000330 * Note: this accounting is not SMP safe.
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700331 */
332static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev)
333{
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700334 /* TODO : stats should be SMP safe */
335 dev->stats.rx_packets++;
336 dev->stats.rx_bytes += skb->len;
Eric Dumazet290b8952010-09-27 00:33:35 +0000337 __skb_tunnel_rx(skb, dev);
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/* Children define the path of the packet through the
341 * Linux networking. Thus, destinations are stackable.
342 */
343
Steffen Klassert8764ab22010-06-04 01:57:38 +0000344static inline struct dst_entry *skb_dst_pop(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Steffen Klasserte4334302011-03-15 21:09:32 +0000346 struct dst_entry *child = dst_clone(skb_dst(skb)->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Steffen Klassert8764ab22010-06-04 01:57:38 +0000348 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return child;
350}
351
Herbert Xu352e5122007-11-13 21:34:06 -0800352extern int dst_discard(struct sk_buff *skb);
David S. Miller5c1e6aa2011-04-28 14:13:38 -0700353extern void *dst_alloc(struct dst_ops * ops, struct net_device *dev,
354 int initial_ref, int initial_obsolete, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355extern void __dst_free(struct dst_entry * dst);
356extern struct dst_entry *dst_destroy(struct dst_entry * dst);
357
358static inline void dst_free(struct dst_entry * dst)
359{
360 if (dst->obsolete > 1)
361 return;
362 if (!atomic_read(&dst->__refcnt)) {
363 dst = dst_destroy(dst);
364 if (!dst)
365 return;
366 }
367 __dst_free(dst);
368}
369
370static inline void dst_rcu_free(struct rcu_head *head)
371{
372 struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
373 dst_free(dst);
374}
375
376static inline void dst_confirm(struct dst_entry *dst)
377{
378 if (dst)
379 neigh_confirm(dst->neighbour);
380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382static inline void dst_link_failure(struct sk_buff *skb)
383{
Eric Dumazetadf30902009-06-02 05:19:30 +0000384 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (dst && dst->ops && dst->ops->link_failure)
386 dst->ops->link_failure(skb);
387}
388
389static inline void dst_set_expires(struct dst_entry *dst, int timeout)
390{
391 unsigned long expires = jiffies + timeout;
392
393 if (expires == 0)
394 expires = 1;
395
396 if (dst->expires == 0 || time_before(expires, dst->expires))
397 dst->expires = expires;
398}
399
400/* Output packet to network from transport. */
401static inline int dst_output(struct sk_buff *skb)
402{
Eric Dumazetadf30902009-06-02 05:19:30 +0000403 return skb_dst(skb)->output(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406/* Input packet from network to transport. */
407static inline int dst_input(struct sk_buff *skb)
408{
Eric Dumazetadf30902009-06-02 05:19:30 +0000409 return skb_dst(skb)->input(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
413{
414 if (dst->obsolete)
415 dst = dst->ops->check(dst, cookie);
416 return dst;
417}
418
419extern void dst_init(void);
420
Herbert Xu815f4e52007-12-12 10:36:59 -0800421/* Flags for xfrm_lookup flags argument. */
422enum {
David S. Miller80c0bc92011-03-01 14:36:37 -0800423 XFRM_LOOKUP_ICMP = 1 << 0,
Herbert Xu815f4e52007-12-12 10:36:59 -0800424};
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426struct flowi;
427#ifndef CONFIG_XFRM
David S. Miller452edd52011-03-02 13:27:41 -0800428static inline struct dst_entry *xfrm_lookup(struct net *net,
429 struct dst_entry *dst_orig,
430 const struct flowi *fl, struct sock *sk,
431 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
David S. Miller452edd52011-03-02 13:27:41 -0800433 return dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435#else
David S. Miller452edd52011-03-02 13:27:41 -0800436extern struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
437 const struct flowi *fl, struct sock *sk,
438 int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441#endif /* _NET_DST_H */