]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - include/net/dst.h
Merge branch 'turbostat' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux
[mirror_ubuntu-eoan-kernel.git] / include / net / dst.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2/*
3 * net/dst.h Protocol independent destination cache definitions.
4 *
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 *
7 */
8
9#ifndef _NET_DST_H
10#define _NET_DST_H
11
86393e52 12#include <net/dst_ops.h>
14c85021 13#include <linux/netdevice.h>
1da177e4
LT
14#include <linux/rtnetlink.h>
15#include <linux/rcupdate.h>
187f1882 16#include <linux/bug.h>
1da177e4 17#include <linux/jiffies.h>
9620fef2 18#include <linux/refcount.h>
1da177e4
LT
19#include <net/neighbour.h>
20#include <asm/processor.h>
21
1da177e4
LT
22struct sk_buff;
23
fd2c3ef7 24struct dst_entry {
66727145 25 struct net_device *dev;
62fa8a84
DM
26 struct dst_ops *ops;
27 unsigned long _metrics;
ecd98837 28 unsigned long expires;
def8b4fa 29#ifdef CONFIG_XFRM
1da177e4 30 struct xfrm_state *xfrm;
5635c10d
ED
31#else
32 void *__pad1;
def8b4fa 33#endif
7f95e188 34 int (*input)(struct sk_buff *);
ede2059d 35 int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
1da177e4 36
5110effe 37 unsigned short flags;
f6b72b62
DM
38#define DST_HOST 0x0001
39#define DST_NOXFRM 0x0002
40#define DST_NOPOLICY 0x0004
1eb04e7c
WW
41#define DST_NOCOUNT 0x0008
42#define DST_FAKE_RTABLE 0x0010
43#define DST_XFRM_TUNNEL 0x0020
44#define DST_XFRM_QUEUE 0x0040
45#define DST_METADATA 0x0080
f6b72b62 46
f5b0a874
DM
47 /* A non-zero value of dst->obsolete forces by-hand validation
48 * of the route entry. Positive values are set by the generic
49 * dst layer to indicate that the entry has been forcefully
50 * destroyed.
51 *
52 * Negative values are used by the implementation layer code to
53 * force invocation of the dst_ops->check() method.
54 */
62fa8a84 55 short obsolete;
f5b0a874
DM
56#define DST_OBSOLETE_NONE 0
57#define DST_OBSOLETE_DEAD 2
58#define DST_OBSOLETE_FORCE_CHK -1
ceb33206 59#define DST_OBSOLETE_KILL -2
62fa8a84
DM
60 unsigned short header_len; /* more space at head required */
61 unsigned short trailer_len; /* space to reserve at tail */
51ce8bd4 62
f1dd9c37
ZY
63 /*
64 * __refcnt wants to be on a different cache line from
65 * input/output/ops or performance tanks badly
66 */
8b207e73
DM
67#ifdef CONFIG_64BIT
68 atomic_t __refcnt; /* 64-bit offset 64 */
69#endif
1e19e02c 70 int __use;
f1dd9c37 71 unsigned long lastuse;
751a587a 72 struct lwtunnel_state *lwtstate;
8b207e73
DM
73 struct rcu_head rcu_head;
74 short error;
75 short __pad;
76 __u32 tclassid;
77#ifndef CONFIG_64BIT
78 atomic_t __refcnt; /* 32-bit offset 64 */
79#endif
1da177e4
LT
80};
81
3fb07daf
ED
82struct dst_metrics {
83 u32 metrics[RTAX_MAX];
9620fef2 84 refcount_t refcnt;
3fb07daf
ED
85};
86extern const struct dst_metrics dst_default_metrics;
87
a4023dd0 88u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
62fa8a84 89
e5fd387a 90#define DST_METRICS_READ_ONLY 0x1UL
3fb07daf 91#define DST_METRICS_REFCOUNTED 0x2UL
e5fd387a 92#define DST_METRICS_FLAGS 0x3UL
62fa8a84 93#define __DST_METRICS_PTR(Y) \
e5fd387a 94 ((u32 *)((Y) & ~DST_METRICS_FLAGS))
62fa8a84
DM
95#define DST_METRICS_PTR(X) __DST_METRICS_PTR((X)->_metrics)
96
97static inline bool dst_metrics_read_only(const struct dst_entry *dst)
98{
99 return dst->_metrics & DST_METRICS_READ_ONLY;
100}
101
a4023dd0 102void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
62fa8a84
DM
103
104static inline void dst_destroy_metrics_generic(struct dst_entry *dst)
105{
106 unsigned long val = dst->_metrics;
107 if (!(val & DST_METRICS_READ_ONLY))
108 __dst_destroy_metrics_generic(dst, val);
109}
110
111static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
112{
113 unsigned long p = dst->_metrics;
114
1f37070d
SH
115 BUG_ON(!p);
116
62fa8a84
DM
117 if (p & DST_METRICS_READ_ONLY)
118 return dst->ops->cow_metrics(dst, p);
119 return __DST_METRICS_PTR(p);
120}
121
122/* This may only be invoked before the entry has reached global
123 * visibility.
124 */
125static inline void dst_init_metrics(struct dst_entry *dst,
126 const u32 *src_metrics,
127 bool read_only)
128{
129 dst->_metrics = ((unsigned long) src_metrics) |
130 (read_only ? DST_METRICS_READ_ONLY : 0);
131}
132
133static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
134{
135 u32 *dst_metrics = dst_metrics_write_ptr(dest);
136
137 if (dst_metrics) {
138 u32 *src_metrics = DST_METRICS_PTR(src);
139
140 memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
141 }
142}
143
144static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
145{
146 return DST_METRICS_PTR(dst);
147}
148
1da177e4 149static inline u32
5170ae82 150dst_metric_raw(const struct dst_entry *dst, const int metric)
1da177e4 151{
62fa8a84
DM
152 u32 *p = DST_METRICS_PTR(dst);
153
154 return p[metric-1];
defb3519
DM
155}
156
5170ae82
DM
157static inline u32
158dst_metric(const struct dst_entry *dst, const int metric)
159{
0dbaee3b 160 WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
d33e4553
DM
161 metric == RTAX_ADVMSS ||
162 metric == RTAX_MTU);
5170ae82
DM
163 return dst_metric_raw(dst, metric);
164}
165
0dbaee3b
DM
166static inline u32
167dst_metric_advmss(const struct dst_entry *dst)
168{
169 u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS);
170
171 if (!advmss)
172 advmss = dst->ops->default_advmss(dst);
173
174 return advmss;
175}
176
defb3519
DM
177static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
178{
62fa8a84 179 u32 *p = dst_metrics_write_ptr(dst);
defb3519 180
62fa8a84
DM
181 if (p)
182 p[metric-1] = val;
1da177e4
LT
183}
184
c3a8d947 185/* Kernel-internal feature bits that are unallocated in user space. */
40f6a2cb 186#define DST_FEATURE_ECN_CA (1U << 31)
c3a8d947
DB
187
188#define DST_FEATURE_MASK (DST_FEATURE_ECN_CA)
189#define DST_FEATURE_ECN_MASK (DST_FEATURE_ECN_CA | RTAX_FEATURE_ECN)
190
0c3adfb8
GBY
191static inline u32
192dst_feature(const struct dst_entry *dst, u32 feature)
193{
bb5b7c11 194 return dst_metric(dst, RTAX_FEATURES) & feature;
0c3adfb8
GBY
195}
196
1da177e4
LT
197static inline u32 dst_mtu(const struct dst_entry *dst)
198{
618f9bc7 199 return dst->ops->mtu(dst);
1da177e4
LT
200}
201
c1e20f7c
SH
202/* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
203static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
204{
205 return msecs_to_jiffies(dst_metric(dst, metric));
206}
207
1da177e4
LT
208static inline u32
209dst_allfrag(const struct dst_entry *dst)
210{
0c3adfb8 211 int ret = dst_feature(dst, RTAX_FEATURE_ALLFRAG);
1da177e4
LT
212 return ret;
213}
214
215static inline int
d33e4553 216dst_metric_locked(const struct dst_entry *dst, int metric)
1da177e4
LT
217{
218 return dst_metric(dst, RTAX_LOCK) & (1<<metric);
219}
220
7f95e188 221static inline void dst_hold(struct dst_entry *dst)
1da177e4 222{
5635c10d
ED
223 /*
224 * If your kernel compilation stops here, please check
8b207e73 225 * the placement of __refcnt in struct dst_entry
5635c10d
ED
226 */
227 BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
44ebe791 228 WARN_ON(atomic_inc_not_zero(&dst->__refcnt) == 0);
1da177e4
LT
229}
230
0da4af00 231static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
03f49f34 232{
32d18ab1 233 if (unlikely(time != dst->lastuse)) {
0da4af00
WW
234 dst->__use++;
235 dst->lastuse = time;
236 }
03f49f34
PE
237}
238
0da4af00 239static inline void dst_hold_and_use(struct dst_entry *dst, unsigned long time)
7fee226a 240{
0da4af00
WW
241 dst_hold(dst);
242 dst_use_noref(dst, time);
7fee226a
ED
243}
244
7f95e188 245static inline struct dst_entry *dst_clone(struct dst_entry *dst)
1da177e4
LT
246{
247 if (dst)
222d7dbd 248 dst_hold(dst);
1da177e4
LT
249 return dst;
250}
251
a4023dd0 252void dst_release(struct dst_entry *dst);
7fee226a 253
5f56f409
WW
254void dst_release_immediate(struct dst_entry *dst);
255
7fee226a
ED
256static inline void refdst_drop(unsigned long refdst)
257{
258 if (!(refdst & SKB_DST_NOREF))
259 dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
260}
261
262/**
263 * skb_dst_drop - drops skb dst
264 * @skb: buffer
265 *
266 * Drops dst reference count if a reference was taken.
267 */
adf30907
ED
268static inline void skb_dst_drop(struct sk_buff *skb)
269{
7fee226a
ED
270 if (skb->_skb_refdst) {
271 refdst_drop(skb->_skb_refdst);
272 skb->_skb_refdst = 0UL;
273 }
274}
275
e79e2595 276static inline void __skb_dst_copy(struct sk_buff *nskb, unsigned long refdst)
7fee226a 277{
e79e2595 278 nskb->_skb_refdst = refdst;
7fee226a
ED
279 if (!(nskb->_skb_refdst & SKB_DST_NOREF))
280 dst_clone(skb_dst(nskb));
281}
282
e79e2595
JS
283static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
284{
285 __skb_dst_copy(nskb, oskb->_skb_refdst);
286}
287
5037e9ef
ED
288/**
289 * dst_hold_safe - Take a reference on a dst if possible
290 * @dst: pointer to dst entry
291 *
292 * This helper returns false if it could not safely
293 * take a reference on a dst.
294 */
295static inline bool dst_hold_safe(struct dst_entry *dst)
296{
b2a9c0ed 297 return atomic_inc_not_zero(&dst->__refcnt);
5037e9ef
ED
298}
299
300/**
222d7dbd 301 * skb_dst_force - makes sure skb dst is refcounted
5037e9ef
ED
302 * @skb: buffer
303 *
304 * If dst is not yet refcounted and not destroyed, grab a ref on it.
b60a7738 305 * Returns true if dst is refcounted.
5037e9ef 306 */
b60a7738 307static inline bool skb_dst_force(struct sk_buff *skb)
5037e9ef
ED
308{
309 if (skb_dst_is_noref(skb)) {
310 struct dst_entry *dst = skb_dst(skb);
311
222d7dbd 312 WARN_ON(!rcu_read_lock_held());
5037e9ef
ED
313 if (!dst_hold_safe(dst))
314 dst = NULL;
315
316 skb->_skb_refdst = (unsigned long)dst;
317 }
b60a7738
FW
318
319 return skb->_skb_refdst != 0UL;
5037e9ef
ED
320}
321
d19d56dd 322
290b895e
ED
323/**
324 * __skb_tunnel_rx - prepare skb for rx reinsert
325 * @skb: buffer
326 * @dev: tunnel device
ea23192e 327 * @net: netns for packet i/o
290b895e
ED
328 *
329 * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
330 * so make some cleanups. (no accounting done)
331 */
ea23192e
ND
332static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
333 struct net *net)
290b895e
ED
334{
335 skb->dev = dev;
bdeab991
TH
336
337 /*
7539fadc 338 * Clear hash so that we can recalulate the hash for the
bdeab991
TH
339 * encapsulated packet, unless we have already determine the hash
340 * over the L4 4-tuple.
341 */
7539fadc 342 skb_clear_hash_if_not_l4(skb);
290b895e 343 skb_set_queue_mapping(skb, 0);
ea23192e 344 skb_scrub_packet(skb, !net_eq(net, dev_net(dev)));
290b895e
ED
345}
346
d19d56dd
ED
347/**
348 * skb_tunnel_rx - prepare skb for rx reinsert
349 * @skb: buffer
350 * @dev: tunnel device
8eb1a859 351 * @net: netns for packet i/o
d19d56dd
ED
352 *
353 * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
354 * so make some cleanups, and perform accounting.
290b895e 355 * Note: this accounting is not SMP safe.
d19d56dd 356 */
ea23192e
ND
357static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
358 struct net *net)
d19d56dd 359{
d19d56dd
ED
360 /* TODO : stats should be SMP safe */
361 dev->stats.rx_packets++;
362 dev->stats.rx_bytes += skb->len;
ea23192e 363 __skb_tunnel_rx(skb, dev, net);
d19d56dd
ED
364}
365
808c1b69
DB
366static inline u32 dst_tclassid(const struct sk_buff *skb)
367{
368#ifdef CONFIG_IP_ROUTE_CLASSID
369 const struct dst_entry *dst;
370
371 dst = skb_dst(skb);
372 if (dst)
373 return dst->tclassid;
374#endif
375 return 0;
376}
377
ede2059d 378int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
aad88724
ED
379static inline int dst_discard(struct sk_buff *skb)
380{
ede2059d 381 return dst_discard_out(&init_net, skb->sk, skb);
aad88724 382}
a4023dd0
JP
383void *dst_alloc(struct dst_ops *ops, struct net_device *dev, int initial_ref,
384 int initial_obsolete, unsigned short flags);
f38a9eb1
TG
385void dst_init(struct dst_entry *dst, struct dst_ops *ops,
386 struct net_device *dev, int initial_ref, int initial_obsolete,
387 unsigned short flags);
a4023dd0 388struct dst_entry *dst_destroy(struct dst_entry *dst);
4a6ce2b6 389void dst_dev_put(struct dst_entry *dst);
1da177e4 390
1da177e4
LT
391static inline void dst_confirm(struct dst_entry *dst)
392{
5110effe 393}
f2c31e32 394
d3aaeb38
DM
395static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
396{
aaa0c23c
ZZ
397 struct neighbour *n = dst->ops->neigh_lookup(dst, NULL, daddr);
398 return IS_ERR(n) ? NULL : n;
f894cbf8
DM
399}
400
401static inline struct neighbour *dst_neigh_lookup_skb(const struct dst_entry *dst,
402 struct sk_buff *skb)
403{
aaa0c23c
ZZ
404 struct neighbour *n = dst->ops->neigh_lookup(dst, skb, NULL);
405 return IS_ERR(n) ? NULL : n;
d3aaeb38
DM
406}
407
63fca65d
JA
408static inline void dst_confirm_neigh(const struct dst_entry *dst,
409 const void *daddr)
410{
411 if (dst->ops->confirm_neigh)
412 dst->ops->confirm_neigh(dst, daddr);
413}
414
1da177e4
LT
415static inline void dst_link_failure(struct sk_buff *skb)
416{
adf30907 417 struct dst_entry *dst = skb_dst(skb);
1da177e4
LT
418 if (dst && dst->ops && dst->ops->link_failure)
419 dst->ops->link_failure(skb);
420}
421
422static inline void dst_set_expires(struct dst_entry *dst, int timeout)
423{
424 unsigned long expires = jiffies + timeout;
425
426 if (expires == 0)
427 expires = 1;
428
429 if (dst->expires == 0 || time_before(expires, dst->expires))
430 dst->expires = expires;
431}
432
433/* Output packet to network from transport. */
13206b6b 434static inline int dst_output(struct net *net, struct sock *sk, struct sk_buff *skb)
aad88724 435{
ede2059d 436 return skb_dst(skb)->output(net, sk, skb);
aad88724 437}
1da177e4
LT
438
439/* Input packet from network to transport. */
440static inline int dst_input(struct sk_buff *skb)
441{
adf30907 442 return skb_dst(skb)->input(skb);
1da177e4
LT
443}
444
445static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
446{
447 if (dst->obsolete)
448 dst = dst->ops->check(dst, cookie);
449 return dst;
450}
451
815f4e57
HX
452/* Flags for xfrm_lookup flags argument. */
453enum {
80c0bc9e 454 XFRM_LOOKUP_ICMP = 1 << 0,
b8c203b2 455 XFRM_LOOKUP_QUEUE = 1 << 1,
ac37e251 456 XFRM_LOOKUP_KEEP_DST_REF = 1 << 2,
815f4e57
HX
457};
458
1da177e4
LT
459struct flowi;
460#ifndef CONFIG_XFRM
452edd59
DM
461static inline struct dst_entry *xfrm_lookup(struct net *net,
462 struct dst_entry *dst_orig,
6f9c9615
ED
463 const struct flowi *fl,
464 const struct sock *sk,
452edd59 465 int flags)
1da177e4 466{
452edd59 467 return dst_orig;
f92ee619
SK
468}
469
bc56b334
BW
470static inline struct dst_entry *
471xfrm_lookup_with_ifid(struct net *net, struct dst_entry *dst_orig,
472 const struct flowi *fl, const struct sock *sk,
473 int flags, u32 if_id)
474{
475 return dst_orig;
476}
477
f92ee619
SK
478static inline struct dst_entry *xfrm_lookup_route(struct net *net,
479 struct dst_entry *dst_orig,
480 const struct flowi *fl,
6f9c9615 481 const struct sock *sk,
f92ee619
SK
482 int flags)
483{
484 return dst_orig;
485}
e87b3998
VY
486
487static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
488{
489 return NULL;
490}
491
1da177e4 492#else
a4023dd0 493struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
6f9c9615 494 const struct flowi *fl, const struct sock *sk,
a4023dd0 495 int flags);
e87b3998 496
bc56b334
BW
497struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
498 struct dst_entry *dst_orig,
499 const struct flowi *fl,
500 const struct sock *sk, int flags,
501 u32 if_id);
502
f92ee619 503struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
6f9c9615 504 const struct flowi *fl, const struct sock *sk,
f92ee619
SK
505 int flags);
506
e87b3998
VY
507/* skb attached with this dst needs transformation if dst->xfrm is valid */
508static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
509{
510 return dst->xfrm;
511}
1da177e4 512#endif
1da177e4 513
f15ca723
ND
514static inline void skb_dst_update_pmtu(struct sk_buff *skb, u32 mtu)
515{
516 struct dst_entry *dst = skb_dst(skb);
517
518 if (dst && dst->ops->update_pmtu)
519 dst->ops->update_pmtu(dst, NULL, skb, mtu);
520}
521
6b4f92af
SB
522static inline void skb_tunnel_check_pmtu(struct sk_buff *skb,
523 struct dst_entry *encap_dst,
524 int headroom)
525{
526 u32 encap_mtu = dst_mtu(encap_dst);
527
528 if (skb->len > encap_mtu - headroom)
529 skb_dst_update_pmtu(skb, encap_mtu - headroom);
530}
531
1da177e4 532#endif /* _NET_DST_H */