]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - include/net/dst.h
Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux
[mirror_ubuntu-zesty-kernel.git] / include / net / dst.h
1 /*
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
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>
19
20 #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
31 struct sk_buff;
32
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*);
49
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
58
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
68
69 /*
70 * Align __refcnt to a 64 bytes alignment
71 * (L1_CACHE_SIZE would be too much)
72 */
73 #ifdef CONFIG_64BIT
74 long __pad_to_align_refcnt[2];
75 #endif
76 /*
77 * __refcnt wants to be on a different cache line from
78 * input/output/ops or performance tanks badly
79 */
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;
88 };
89 };
90
91 static inline struct neighbour *dst_get_neighbour_noref(struct dst_entry *dst)
92 {
93 return rcu_dereference(dst->_neighbour);
94 }
95
96 static inline struct neighbour *dst_get_neighbour_noref_raw(struct dst_entry *dst)
97 {
98 return rcu_dereference_raw(dst->_neighbour);
99 }
100
101 static inline void dst_set_neighbour(struct dst_entry *dst, struct neighbour *neigh)
102 {
103 rcu_assign_pointer(dst->_neighbour, neigh);
104 }
105
106 extern u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
107 extern const u32 dst_default_metrics[RTAX_MAX];
108
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
114 static inline bool dst_metrics_read_only(const struct dst_entry *dst)
115 {
116 return dst->_metrics & DST_METRICS_READ_ONLY;
117 }
118
119 extern void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
120
121 static 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
128 static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
129 {
130 unsigned long p = dst->_metrics;
131
132 BUG_ON(!p);
133
134 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 */
142 static 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
150 static 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
161 static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
162 {
163 return DST_METRICS_PTR(dst);
164 }
165
166 static inline u32
167 dst_metric_raw(const struct dst_entry *dst, const int metric)
168 {
169 u32 *p = DST_METRICS_PTR(dst);
170
171 return p[metric-1];
172 }
173
174 static inline u32
175 dst_metric(const struct dst_entry *dst, const int metric)
176 {
177 WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
178 metric == RTAX_ADVMSS ||
179 metric == RTAX_MTU);
180 return dst_metric_raw(dst, metric);
181 }
182
183 static inline u32
184 dst_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
194 static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
195 {
196 u32 *p = dst_metrics_write_ptr(dst);
197
198 if (p)
199 p[metric-1] = val;
200 }
201
202 static inline u32
203 dst_feature(const struct dst_entry *dst, u32 feature)
204 {
205 return dst_metric(dst, RTAX_FEATURES) & feature;
206 }
207
208 static inline u32 dst_mtu(const struct dst_entry *dst)
209 {
210 return dst->ops->mtu(dst);
211 }
212
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)
215 {
216 return msecs_to_jiffies(dst_metric(dst, metric));
217 }
218
219 static inline void set_dst_metric_rtt(struct dst_entry *dst, int metric,
220 unsigned long rtt)
221 {
222 dst_metric_set(dst, metric, jiffies_to_msecs(rtt));
223 }
224
225 static inline u32
226 dst_allfrag(const struct dst_entry *dst)
227 {
228 int ret = dst_feature(dst, RTAX_FEATURE_ALLFRAG);
229 return ret;
230 }
231
232 static inline int
233 dst_metric_locked(const struct dst_entry *dst, int metric)
234 {
235 return dst_metric(dst, RTAX_LOCK) & (1<<metric);
236 }
237
238 static inline void dst_hold(struct dst_entry * dst)
239 {
240 /*
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);
245 atomic_inc(&dst->__refcnt);
246 }
247
248 static 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
255 static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
256 {
257 dst->__use++;
258 dst->lastuse = time;
259 }
260
261 static inline
262 struct dst_entry * dst_clone(struct dst_entry * dst)
263 {
264 if (dst)
265 atomic_inc(&dst->__refcnt);
266 return dst;
267 }
268
269 extern void dst_release(struct dst_entry *dst);
270
271 static 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 */
283 static inline void skb_dst_drop(struct sk_buff *skb)
284 {
285 if (skb->_skb_refdst) {
286 refdst_drop(skb->_skb_refdst);
287 skb->_skb_refdst = 0UL;
288 }
289 }
290
291 static 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 */
304 static 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 }
311 }
312
313
314 /**
315 * __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 */
322 static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev)
323 {
324 skb->dev = dev;
325
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;
333 skb_set_queue_mapping(skb, 0);
334 skb_dst_drop(skb);
335 nf_reset(skb);
336 }
337
338 /**
339 * 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.
345 * Note: this accounting is not SMP safe.
346 */
347 static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev)
348 {
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);
353 }
354
355 /* Children define the path of the packet through the
356 * Linux networking. Thus, destinations are stackable.
357 */
358
359 static inline struct dst_entry *skb_dst_pop(struct sk_buff *skb)
360 {
361 struct dst_entry *child = dst_clone(skb_dst(skb)->child);
362
363 skb_dst_drop(skb);
364 return child;
365 }
366
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);
372
373 static 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
385 static 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
391 static inline void dst_confirm(struct dst_entry *dst)
392 {
393 if (dst) {
394 struct neighbour *n;
395
396 rcu_read_lock();
397 n = dst_get_neighbour_noref(dst);
398 neigh_confirm(n);
399 rcu_read_unlock();
400 }
401 }
402
403 static 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
408 static inline void dst_link_failure(struct sk_buff *skb)
409 {
410 struct dst_entry *dst = skb_dst(skb);
411 if (dst && dst->ops && dst->ops->link_failure)
412 dst->ops->link_failure(skb);
413 }
414
415 static 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. */
427 static inline int dst_output(struct sk_buff *skb)
428 {
429 return skb_dst(skb)->output(skb);
430 }
431
432 /* Input packet from network to transport. */
433 static inline int dst_input(struct sk_buff *skb)
434 {
435 return skb_dst(skb)->input(skb);
436 }
437
438 static 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
445 extern void dst_init(void);
446
447 /* Flags for xfrm_lookup flags argument. */
448 enum {
449 XFRM_LOOKUP_ICMP = 1 << 0,
450 };
451
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)
458 {
459 return dst_orig;
460 }
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
466
467 #endif /* _NET_DST_H */