blob: 59c5d18cc3857da85690627b085e1065eeeb78ad [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>
Paul Gortmaker187f1882011-11-23 20:12:59 -050015#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/jiffies.h>
17#include <net/neighbour.h>
18#include <asm/processor.h>
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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
31struct sk_buff;
32
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000033struct dst_entry {
Eric Dumazet1e19e022007-02-09 16:26:55 -080034 struct rcu_head rcu_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 struct dst_entry *child;
36 struct net_device *dev;
David S. Miller62fa8a82011-01-26 20:51:05 -080037 struct dst_ops *ops;
38 unsigned long _metrics;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 unsigned long expires;
Zhang Yanminf1dd9c32008-03-12 22:52:37 -070040 struct dst_entry *path;
Eric Dumazetf2c31e32011-07-29 19:00:53 +000041 struct neighbour __rcu *_neighbour;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -070042#ifdef CONFIG_XFRM
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct xfrm_state *xfrm;
Eric Dumazet5635c102008-11-16 19:46:36 -080044#else
45 void *__pad1;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -070046#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int (*input)(struct sk_buff*);
48 int (*output)(struct sk_buff*);
49
David S. Millerf6b72b62011-07-14 07:53:20 -070050 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
Eric Dumazete688a602011-12-22 04:15:53 +000057#define DST_NOPEER 0x0040
David S. Millerf6b72b62011-07-14 07:53:20 -070058
David S. Miller62fa8a82011-01-26 20:51:05 -080059 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 */
Patrick McHardyc7066f72011-01-14 13:36:42 +010063#ifdef CONFIG_IP_ROUTE_CLASSID
Zhang Yanminf1dd9c32008-03-12 22:52:37 -070064 __u32 tclassid;
Eric Dumazet5635c102008-11-16 19:46:36 -080065#else
66 __u32 __pad2;
Zhang Yanminf1dd9c32008-03-12 22:52:37 -070067#endif
68
Eric Dumazet5635c102008-11-16 19:46:36 -080069 /*
70 * Align __refcnt to a 64 bytes alignment
71 * (L1_CACHE_SIZE would be too much)
72 */
73#ifdef CONFIG_64BIT
David S. Millerf6b72b62011-07-14 07:53:20 -070074 long __pad_to_align_refcnt[2];
Eric Dumazet5635c102008-11-16 19:46:36 -080075#endif
Zhang Yanminf1dd9c32008-03-12 22:52:37 -070076 /*
77 * __refcnt wants to be on a different cache line from
78 * input/output/ops or performance tanks badly
79 */
Eric Dumazet1e19e022007-02-09 16:26:55 -080080 atomic_t __refcnt; /* client references */
81 int __use;
Zhang Yanminf1dd9c32008-03-12 22:52:37 -070082 unsigned long lastuse;
Eric Dumazet1e19e022007-02-09 16:26:55 -080083 union {
Eric Dumazetfc766e4c2010-10-29 03:09:24 +000084 struct dst_entry *next;
85 struct rtable __rcu *rt_next;
86 struct rt6_info *rt6_next;
87 struct dn_route __rcu *dn_next;
Eric Dumazet1e19e022007-02-09 16:26:55 -080088 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
David Miller27217452011-12-02 16:52:08 +000091static inline struct neighbour *dst_get_neighbour_noref(struct dst_entry *dst)
David S. Miller69cce1d2011-07-17 23:09:49 -070092{
Eric Dumazetf2c31e32011-07-29 19:00:53 +000093 return rcu_dereference(dst->_neighbour);
94}
95
David Miller27217452011-12-02 16:52:08 +000096static inline struct neighbour *dst_get_neighbour_noref_raw(struct dst_entry *dst)
Eric Dumazetf2c31e32011-07-29 19:00:53 +000097{
98 return rcu_dereference_raw(dst->_neighbour);
David S. Miller69cce1d2011-07-17 23:09:49 -070099}
100
101static inline void dst_set_neighbour(struct dst_entry *dst, struct neighbour *neigh)
102{
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000103 rcu_assign_pointer(dst->_neighbour, neigh);
David S. Miller69cce1d2011-07-17 23:09:49 -0700104}
105
David S. Miller62fa8a82011-01-26 20:51:05 -0800106extern u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
David S. Miller725d1e12011-01-28 14:05:05 -0800107extern const u32 dst_default_metrics[RTAX_MAX];
David S. Miller62fa8a82011-01-26 20:51:05 -0800108
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
114static inline bool dst_metrics_read_only(const struct dst_entry *dst)
115{
116 return dst->_metrics & DST_METRICS_READ_ONLY;
117}
118
119extern void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
120
121static 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
128static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
129{
130 unsigned long p = dst->_metrics;
131
Stephen Hemminger1f370702011-05-24 13:50:52 -0400132 BUG_ON(!p);
133
David S. Miller62fa8a82011-01-26 20:51:05 -0800134 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 */
142static 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
150static 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
161static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
162{
163 return DST_METRICS_PTR(dst);
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static inline u32
David S. Miller5170ae82010-12-12 21:35:57 -0800167dst_metric_raw(const struct dst_entry *dst, const int metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
David S. Miller62fa8a82011-01-26 20:51:05 -0800169 u32 *p = DST_METRICS_PTR(dst);
170
171 return p[metric-1];
David S. Millerdefb3512010-12-08 21:16:57 -0800172}
173
David S. Miller5170ae82010-12-12 21:35:57 -0800174static inline u32
175dst_metric(const struct dst_entry *dst, const int metric)
176{
David S. Miller0dbaee32010-12-13 12:52:14 -0800177 WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
David S. Millerd33e4552010-12-14 13:01:14 -0800178 metric == RTAX_ADVMSS ||
179 metric == RTAX_MTU);
David S. Miller5170ae82010-12-12 21:35:57 -0800180 return dst_metric_raw(dst, metric);
181}
182
David S. Miller0dbaee32010-12-13 12:52:14 -0800183static inline u32
184dst_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
David S. Millerdefb3512010-12-08 21:16:57 -0800194static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
195{
David S. Miller62fa8a82011-01-26 20:51:05 -0800196 u32 *p = dst_metrics_write_ptr(dst);
David S. Millerdefb3512010-12-08 21:16:57 -0800197
David S. Miller62fa8a82011-01-26 20:51:05 -0800198 if (p)
199 p[metric-1] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Gilad Ben-Yossef0c3adfb2009-10-28 04:15:23 +0000202static inline u32
203dst_feature(const struct dst_entry *dst, u32 feature)
204{
David S. Millerbb5b7c12009-12-15 20:56:42 -0800205 return dst_metric(dst, RTAX_FEATURES) & feature;
Gilad Ben-Yossef0c3adfb2009-10-28 04:15:23 +0000206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208static inline u32 dst_mtu(const struct dst_entry *dst)
209{
Steffen Klassert618f9bc2011-11-23 02:13:31 +0000210 return dst->ops->mtu(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Stephen Hemmingerc1e20f72008-07-18 23:02:15 -0700213/* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
214static 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
219static inline void set_dst_metric_rtt(struct dst_entry *dst, int metric,
220 unsigned long rtt)
221{
David S. Millerdefb3512010-12-08 21:16:57 -0800222 dst_metric_set(dst, metric, jiffies_to_msecs(rtt));
Stephen Hemmingerc1e20f72008-07-18 23:02:15 -0700223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static inline u32
226dst_allfrag(const struct dst_entry *dst)
227{
Gilad Ben-Yossef0c3adfb2009-10-28 04:15:23 +0000228 int ret = dst_feature(dst, RTAX_FEATURE_ALLFRAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return ret;
230}
231
232static inline int
David S. Millerd33e4552010-12-14 13:01:14 -0800233dst_metric_locked(const struct dst_entry *dst, int metric)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 return dst_metric(dst, RTAX_LOCK) & (1<<metric);
236}
237
238static inline void dst_hold(struct dst_entry * dst)
239{
Eric Dumazet5635c102008-11-16 19:46:36 -0800240 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 atomic_inc(&dst->__refcnt);
246}
247
Pavel Emelyanov03f49f32007-11-10 21:28:34 -0800248static 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
Eric Dumazet7fee2262010-05-11 23:19:48 +0000255static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
256{
257 dst->__use++;
258 dst->lastuse = time;
259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static inline
262struct dst_entry * dst_clone(struct dst_entry * dst)
263{
264 if (dst)
265 atomic_inc(&dst->__refcnt);
266 return dst;
267}
268
Ilpo Järvinen8d330862008-03-27 17:53:31 -0700269extern void dst_release(struct dst_entry *dst);
Eric Dumazet7fee2262010-05-11 23:19:48 +0000270
271static 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 */
Eric Dumazetadf30902009-06-02 05:19:30 +0000283static inline void skb_dst_drop(struct sk_buff *skb)
284{
Eric Dumazet7fee2262010-05-11 23:19:48 +0000285 if (skb->_skb_refdst) {
286 refdst_drop(skb->_skb_refdst);
287 skb->_skb_refdst = 0UL;
288 }
289}
290
291static 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 */
304static 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 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000311}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700313
314/**
Eric Dumazet290b8952010-09-27 00:33:35 +0000315 * __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 */
322static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev)
323{
324 skb->dev = dev;
Tom Herbertbdeab992011-08-14 19:45:55 +0000325
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;
Eric Dumazet290b8952010-09-27 00:33:35 +0000333 skb_set_queue_mapping(skb, 0);
334 skb_dst_drop(skb);
335 nf_reset(skb);
336}
337
338/**
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700339 * 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.
Eric Dumazet290b8952010-09-27 00:33:35 +0000345 * Note: this accounting is not SMP safe.
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700346 */
347static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev)
348{
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700349 /* TODO : stats should be SMP safe */
350 dev->stats.rx_packets++;
351 dev->stats.rx_bytes += skb->len;
Eric Dumazet290b8952010-09-27 00:33:35 +0000352 __skb_tunnel_rx(skb, dev);
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/* Children define the path of the packet through the
356 * Linux networking. Thus, destinations are stackable.
357 */
358
Steffen Klassert8764ab22010-06-04 01:57:38 +0000359static inline struct dst_entry *skb_dst_pop(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Steffen Klasserte4334302011-03-15 21:09:32 +0000361 struct dst_entry *child = dst_clone(skb_dst(skb)->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Steffen Klassert8764ab22010-06-04 01:57:38 +0000363 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return child;
365}
366
Herbert Xu352e5122007-11-13 21:34:06 -0800367extern int dst_discard(struct sk_buff *skb);
David S. Miller5c1e6aa2011-04-28 14:13:38 -0700368extern void *dst_alloc(struct dst_ops * ops, struct net_device *dev,
369 int initial_ref, int initial_obsolete, int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370extern void __dst_free(struct dst_entry * dst);
371extern struct dst_entry *dst_destroy(struct dst_entry * dst);
372
373static 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
385static 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
391static inline void dst_confirm(struct dst_entry *dst)
392{
David S. Miller69cce1d2011-07-17 23:09:49 -0700393 if (dst) {
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000394 struct neighbour *n;
395
396 rcu_read_lock();
David Miller27217452011-12-02 16:52:08 +0000397 n = dst_get_neighbour_noref(dst);
David S. Miller69cce1d2011-07-17 23:09:49 -0700398 neigh_confirm(n);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000399 rcu_read_unlock();
David S. Miller69cce1d2011-07-17 23:09:49 -0700400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
David S. Millerd3aaeb32011-07-18 00:40:17 -0700403static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408static inline void dst_link_failure(struct sk_buff *skb)
409{
Eric Dumazetadf30902009-06-02 05:19:30 +0000410 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (dst && dst->ops && dst->ops->link_failure)
412 dst->ops->link_failure(skb);
413}
414
415static 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. */
427static inline int dst_output(struct sk_buff *skb)
428{
Eric Dumazetadf30902009-06-02 05:19:30 +0000429 return skb_dst(skb)->output(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432/* Input packet from network to transport. */
433static inline int dst_input(struct sk_buff *skb)
434{
Eric Dumazetadf30902009-06-02 05:19:30 +0000435 return skb_dst(skb)->input(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
438static 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
445extern void dst_init(void);
446
Herbert Xu815f4e52007-12-12 10:36:59 -0800447/* Flags for xfrm_lookup flags argument. */
448enum {
David S. Miller80c0bc92011-03-01 14:36:37 -0800449 XFRM_LOOKUP_ICMP = 1 << 0,
Herbert Xu815f4e52007-12-12 10:36:59 -0800450};
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452struct flowi;
453#ifndef CONFIG_XFRM
David S. Miller452edd52011-03-02 13:27:41 -0800454static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
David S. Miller452edd52011-03-02 13:27:41 -0800459 return dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461#else
David S. Miller452edd52011-03-02 13:27:41 -0800462extern struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
463 const struct flowi *fl, struct sock *sk,
464 int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467#endif /* _NET_DST_H */