blob: aaff1a0881a353ab92d517656b1e884b6770dcd8 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -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
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
31struct sk_buff;
32
33struct 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 union {
40 unsigned long expires;
41 /* point to where the dst_entry copied from */
42 struct dst_entry *from;
43 };
44 struct dst_entry *path;
45 struct neighbour __rcu *_neighbour;
46#ifdef CONFIG_XFRM
47 struct xfrm_state *xfrm;
48#else
49 void *__pad1;
50#endif
51 int (*input)(struct sk_buff*);
52 int (*output)(struct sk_buff*);
53
54 int flags;
55#define DST_HOST 0x0001
56#define DST_NOXFRM 0x0002
57#define DST_NOPOLICY 0x0004
58#define DST_NOHASH 0x0008
59#define DST_NOCACHE 0x0010
60#define DST_NOCOUNT 0x0020
61#define DST_NOPEER 0x0040
62#define DST_FAKE_RTABLE 0x0080
63#define DST_XFRM_TUNNEL 0x0100
64
65 short error;
66 short obsolete;
67 unsigned short header_len; /* more space at head required */
68 unsigned short trailer_len; /* space to reserve at tail */
69#ifdef CONFIG_IP_ROUTE_CLASSID
70 __u32 tclassid;
71#else
72 __u32 __pad2;
73#endif
74
75 /*
76 * Align __refcnt to a 64 bytes alignment
77 * (L1_CACHE_SIZE would be too much)
78 */
79#ifdef CONFIG_64BIT
80 long __pad_to_align_refcnt[2];
81#endif
82 /*
83 * __refcnt wants to be on a different cache line from
84 * input/output/ops or performance tanks badly
85 */
86 atomic_t __refcnt; /* client references */
87 int __use;
88 unsigned long lastuse;
89 union {
90 struct dst_entry *next;
91 struct rtable __rcu *rt_next;
92 struct rt6_info *rt6_next;
93 struct dn_route __rcu *dn_next;
94 };
95 struct list_head conn_head;
96};
97
98static inline struct neighbour *dst_get_neighbour_noref(struct dst_entry *dst)
99{
100 return rcu_dereference(dst->_neighbour);
101}
102
103static inline struct neighbour *dst_get_neighbour_noref_raw(struct dst_entry *dst)
104{
105 return rcu_dereference_raw(dst->_neighbour);
106}
107
108static inline void dst_set_neighbour(struct dst_entry *dst, struct neighbour *neigh)
109{
110 rcu_assign_pointer(dst->_neighbour, neigh);
111}
112
113extern u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
114extern const u32 dst_default_metrics[RTAX_MAX];
115
116#define DST_METRICS_READ_ONLY 0x1UL
117#define __DST_METRICS_PTR(Y) \
118 ((u32 *)((Y) & ~DST_METRICS_READ_ONLY))
119#define DST_METRICS_PTR(X) __DST_METRICS_PTR((X)->_metrics)
120
121static inline bool dst_metrics_read_only(const struct dst_entry *dst)
122{
123 return dst->_metrics & DST_METRICS_READ_ONLY;
124}
125
126extern void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
127
128static inline void dst_destroy_metrics_generic(struct dst_entry *dst)
129{
130 unsigned long val = dst->_metrics;
131 if (!(val & DST_METRICS_READ_ONLY))
132 __dst_destroy_metrics_generic(dst, val);
133}
134
135static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
136{
137 unsigned long p = dst->_metrics;
138
139 BUG_ON(!p);
140
141 if (p & DST_METRICS_READ_ONLY)
142 return dst->ops->cow_metrics(dst, p);
143 return __DST_METRICS_PTR(p);
144}
145
146/* This may only be invoked before the entry has reached global
147 * visibility.
148 */
149static inline void dst_init_metrics(struct dst_entry *dst,
150 const u32 *src_metrics,
151 bool read_only)
152{
153 dst->_metrics = ((unsigned long) src_metrics) |
154 (read_only ? DST_METRICS_READ_ONLY : 0);
155}
156
157static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
158{
159 u32 *dst_metrics = dst_metrics_write_ptr(dest);
160
161 if (dst_metrics) {
162 u32 *src_metrics = DST_METRICS_PTR(src);
163
164 memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
165 }
166}
167
168static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
169{
170 return DST_METRICS_PTR(dst);
171}
172
173static inline u32
174dst_metric_raw(const struct dst_entry *dst, const int metric)
175{
176 u32 *p = DST_METRICS_PTR(dst);
177
178 return p[metric-1];
179}
180
181static inline u32
182dst_metric(const struct dst_entry *dst, const int metric)
183{
184 WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
185 metric == RTAX_ADVMSS ||
186 metric == RTAX_MTU);
187 return dst_metric_raw(dst, metric);
188}
189
190static inline u32
191dst_metric_advmss(const struct dst_entry *dst)
192{
193 u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS);
194
195 if (!advmss)
196 advmss = dst->ops->default_advmss(dst);
197
198 return advmss;
199}
200
201static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
202{
203 u32 *p = dst_metrics_write_ptr(dst);
204
205 if (p)
206 p[metric-1] = val;
207}
208
209static inline u32
210dst_feature(const struct dst_entry *dst, u32 feature)
211{
212 return dst_metric(dst, RTAX_FEATURES) & feature;
213}
214
215static inline u32 dst_mtu(const struct dst_entry *dst)
216{
217 return dst->ops->mtu(dst);
218}
219
220/* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
221static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
222{
223 return msecs_to_jiffies(dst_metric(dst, metric));
224}
225
226static inline void set_dst_metric_rtt(struct dst_entry *dst, int metric,
227 unsigned long rtt)
228{
229 dst_metric_set(dst, metric, jiffies_to_msecs(rtt));
230}
231
232static inline u32
233dst_allfrag(const struct dst_entry *dst)
234{
235 int ret = dst_feature(dst, RTAX_FEATURE_ALLFRAG);
236 return ret;
237}
238
239static inline int
240dst_metric_locked(const struct dst_entry *dst, int metric)
241{
242 return dst_metric(dst, RTAX_LOCK) & (1<<metric);
243}
244
245static inline void dst_hold(struct dst_entry * dst)
246{
247 /*
248 * If your kernel compilation stops here, please check
249 * __pad_to_align_refcnt declaration in struct dst_entry
250 */
251 BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
252 atomic_inc(&dst->__refcnt);
253}
254
255static inline void dst_use(struct dst_entry *dst, unsigned long time)
256{
257 dst_hold(dst);
258 dst->__use++;
259 dst->lastuse = time;
260}
261
262static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
263{
264 dst->__use++;
265 dst->lastuse = time;
266}
267
268static inline
269struct dst_entry * dst_clone(struct dst_entry * dst)
270{
271 if (dst)
272 atomic_inc(&dst->__refcnt);
273 return dst;
274}
275
276extern void dst_release(struct dst_entry *dst);
277
278static inline void refdst_drop(unsigned long refdst)
279{
280 if (!(refdst & SKB_DST_NOREF))
281 dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
282}
283
284/**
285 * skb_dst_drop - drops skb dst
286 * @skb: buffer
287 *
288 * Drops dst reference count if a reference was taken.
289 */
290static inline void skb_dst_drop(struct sk_buff *skb)
291{
292 if (skb->_skb_refdst) {
293 refdst_drop(skb->_skb_refdst);
294 skb->_skb_refdst = 0UL;
295 }
296}
297
298static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
299{
300 nskb->_skb_refdst = oskb->_skb_refdst;
301 if (!(nskb->_skb_refdst & SKB_DST_NOREF))
302 dst_clone(skb_dst(nskb));
303}
304
305/**
306 * skb_dst_force - makes sure skb dst is refcounted
307 * @skb: buffer
308 *
309 * If dst is not yet refcounted, let's do it
310 */
311static inline void skb_dst_force(struct sk_buff *skb)
312{
313 if (skb_dst_is_noref(skb)) {
314 WARN_ON(!rcu_read_lock_held());
315 skb->_skb_refdst &= ~SKB_DST_NOREF;
316 dst_clone(skb_dst(skb));
317 }
318}
319
320
321/**
322 * __skb_tunnel_rx - prepare skb for rx reinsert
323 * @skb: buffer
324 * @dev: tunnel device
325 *
326 * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
327 * so make some cleanups. (no accounting done)
328 */
329static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev)
330{
331 skb->dev = dev;
332
333 /*
334 * Clear rxhash so that we can recalulate the hash for the
335 * encapsulated packet, unless we have already determine the hash
336 * over the L4 4-tuple.
337 */
338 if (!skb->l4_rxhash)
339 skb->rxhash = 0;
340 skb_set_queue_mapping(skb, 0);
341 skb_dst_drop(skb);
342 nf_reset(skb);
343}
344
345/**
346 * skb_tunnel_rx - prepare skb for rx reinsert
347 * @skb: buffer
348 * @dev: tunnel device
349 *
350 * After decapsulation, packet is going to re-enter (netif_rx()) our stack,
351 * so make some cleanups, and perform accounting.
352 * Note: this accounting is not SMP safe.
353 */
354static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev)
355{
356 /* TODO : stats should be SMP safe */
357 dev->stats.rx_packets++;
358 dev->stats.rx_bytes += skb->len;
359 __skb_tunnel_rx(skb, dev);
360}
361
362/* Children define the path of the packet through the
363 * Linux networking. Thus, destinations are stackable.
364 */
365
366static inline struct dst_entry *skb_dst_pop(struct sk_buff *skb)
367{
368 struct dst_entry *child = dst_clone(skb_dst(skb)->child);
369
370 skb_dst_drop(skb);
371 return child;
372}
373
374extern int dst_discard(struct sk_buff *skb);
375extern void *dst_alloc(struct dst_ops * ops, struct net_device *dev,
376 int initial_ref, int initial_obsolete, int flags);
377extern void __dst_free(struct dst_entry * dst);
378extern struct dst_entry *dst_destroy(struct dst_entry * dst);
379
380static inline void dst_free(struct dst_entry * dst)
381{
382 if (dst->obsolete > 1)
383 return;
384 if (!atomic_read(&dst->__refcnt)) {
385 dst = dst_destroy(dst);
386 if (!dst)
387 return;
388 }
389 net_run_track(PRT_DST," free");
390 netruninfo_add(NULL, DST_FREE);
391 __dst_free(dst);
392}
393
394static inline void dst_rcu_free(struct rcu_head *head)
395{
396 struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
397 dst_free(dst);
398}
399
400static inline void dst_confirm(struct dst_entry *dst)
401{
402 if (dst) {
403 struct neighbour *n;
404
405 rcu_read_lock();
406 n = dst_get_neighbour_noref(dst);
407 neigh_confirm(n);
408 rcu_read_unlock();
409 }
410}
411
412static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
413{
414 return dst->ops->neigh_lookup(dst, daddr);
415}
416
417static inline void dst_link_failure(struct sk_buff *skb)
418{
419 struct dst_entry *dst = skb_dst(skb);
420 if (dst && dst->ops && dst->ops->link_failure)
421 dst->ops->link_failure(skb);
422}
423
424static inline void dst_set_expires(struct dst_entry *dst, int timeout)
425{
426 unsigned long expires = jiffies + timeout;
427
428 if (expires == 0)
429 expires = 1;
430
431 if (dst->expires == 0 || time_before(expires, dst->expires))
432 dst->expires = expires;
433}
434
435/* Output packet to network from transport. */
436static inline int dst_output(struct sk_buff *skb)
437{
438 return skb_dst(skb)->output(skb);
439}
440
441/* Input packet from network to transport. */
442static inline int dst_input(struct sk_buff *skb)
443{
444 return skb_dst(skb)->input(skb);
445}
446
447static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
448{
449 if (dst->obsolete)
450 dst = dst->ops->check(dst, cookie);
451 return dst;
452}
453
454extern void dst_init(void);
455
456/* Flags for xfrm_lookup flags argument. */
457enum {
458 XFRM_LOOKUP_ICMP = 1 << 0,
459};
460
461struct flowi;
462#ifndef CONFIG_XFRM
463static inline struct dst_entry *xfrm_lookup(struct net *net,
464 struct dst_entry *dst_orig,
465 const struct flowi *fl, struct sock *sk,
466 int flags)
467{
468 return dst_orig;
469}
470
471static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
472{
473 return NULL;
474}
475
476#else
477extern struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
478 const struct flowi *fl, struct sock *sk,
479 int flags);
480
481/* skb attached with this dst needs transformation if dst->xfrm is valid */
482static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
483{
484 return dst->xfrm;
485}
486#endif
487
488#endif /* _NET_DST_H */