linux/include/net/dst.h
<<
>>
Prefs
   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#include <linux/rh_kabi.h>
  21
  22#define DST_GC_MIN      (HZ/10)
  23#define DST_GC_INC      (HZ/2)
  24#define DST_GC_MAX      (120*HZ)
  25
  26/* Each dst_entry has reference count and sits in some parent list(s).
  27 * When it is removed from parent list, it is "freed" (dst_free).
  28 * After this it enters dead state (dst->obsolete > 0) and if its refcnt
  29 * is zero, it can be destroyed immediately, otherwise it is added
  30 * to gc list and garbage collector periodically checks the refcnt.
  31 */
  32
  33struct sk_buff;
  34
  35struct dst_entry {
  36        struct rcu_head         rcu_head;
  37        struct dst_entry        *child;
  38        struct net_device       *dev;
  39        struct  dst_ops         *ops;
  40        unsigned long           _metrics;
  41        unsigned long           expires;
  42        struct dst_entry        *path;
  43        struct dst_entry        *from;
  44#ifdef CONFIG_XFRM
  45        struct xfrm_state       *xfrm;
  46#else
  47        void                    *__pad1;
  48#endif
  49        int                     (*input)(struct sk_buff *);
  50        RH_KABI_REPLACE(int                     (*output)(struct sk_buff *),
  51                        int                     (*output)(struct sock *sk, struct sk_buff *skb))
  52
  53        unsigned short          flags;
  54#define DST_HOST                0x0001
  55#define DST_NOXFRM              0x0002
  56#define DST_NOPOLICY            0x0004
  57#define DST_NOHASH              0x0008
  58#define DST_NOCACHE             0x0010
  59#define DST_NOCOUNT             0x0020
  60#define DST_FAKE_RTABLE         0x0040
  61#define DST_XFRM_TUNNEL         0x0080
  62#define DST_XFRM_QUEUE          0x0100
  63#define DST_METADATA            0x0200
  64
  65        RH_KABI_DEPRECATE(unsigned short, pending_confirm)
  66        short                   error;
  67
  68        /* A non-zero value of dst->obsolete forces by-hand validation
  69         * of the route entry.  Positive values are set by the generic
  70         * dst layer to indicate that the entry has been forcefully
  71         * destroyed.
  72         *
  73         * Negative values are used by the implementation layer code to
  74         * force invocation of the dst_ops->check() method.
  75         */
  76        short                   obsolete;
  77#define DST_OBSOLETE_NONE       0
  78#define DST_OBSOLETE_DEAD       2
  79#define DST_OBSOLETE_FORCE_CHK  -1
  80#define DST_OBSOLETE_KILL       -2
  81        unsigned short          header_len;     /* more space at head required */
  82        unsigned short          trailer_len;    /* space to reserve at tail */
  83#ifdef CONFIG_IP_ROUTE_CLASSID
  84        __u32                   tclassid;
  85#else
  86        __u32                   __pad2;
  87#endif
  88
  89        /*
  90         * Align __refcnt to a 64 bytes alignment
  91         * (L1_CACHE_SIZE would be too much)
  92         */
  93#ifdef CONFIG_64BIT
  94        long                    __pad_to_align_refcnt[2];
  95#endif
  96        /*
  97         * __refcnt wants to be on a different cache line from
  98         * input/output/ops or performance tanks badly
  99         */
 100        atomic_t                __refcnt;       /* client references    */
 101        int                     __use;
 102        unsigned long           lastuse;
 103        union {
 104                struct dst_entry        *next;
 105                struct rtable __rcu     *rt_next;
 106                struct rt6_info         *rt6_next;
 107                struct dn_route __rcu   *dn_next;
 108        };
 109
 110        /* RHEL SPECIFIC
 111         *
 112         * The following padding has been inserted before ABI freeze to
 113         * allow extending the structure while preserve ABI. Feel free
 114         * to replace reserved slots with required structure field
 115         * additions of your backport.
 116         */
 117#ifdef __GENKSYMS__
 118        u32                     rh_reserved1;
 119        u32                     rh_reserved2;
 120#else
 121        struct lwtunnel_state   *lwtstate;
 122#endif
 123        u32                     rh_reserved3;
 124        u32                     rh_reserved4;
 125};
 126
 127extern u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old);
 128extern const u32 dst_default_metrics[];
 129
 130#define DST_METRICS_READ_ONLY           0x1UL
 131#define DST_METRICS_FLAGS               0x3UL
 132#define __DST_METRICS_PTR(Y)    \
 133        ((u32 *)((Y) & ~DST_METRICS_FLAGS))
 134#define DST_METRICS_PTR(X)      __DST_METRICS_PTR((X)->_metrics)
 135
 136static inline bool dst_metrics_read_only(const struct dst_entry *dst)
 137{
 138        return dst->_metrics & DST_METRICS_READ_ONLY;
 139}
 140
 141void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old);
 142
 143static inline void dst_destroy_metrics_generic(struct dst_entry *dst)
 144{
 145        unsigned long val = dst->_metrics;
 146        if (!(val & DST_METRICS_READ_ONLY))
 147                __dst_destroy_metrics_generic(dst, val);
 148}
 149
 150static inline u32 *dst_metrics_write_ptr(struct dst_entry *dst)
 151{
 152        unsigned long p = dst->_metrics;
 153
 154        BUG_ON(!p);
 155
 156        if (p & DST_METRICS_READ_ONLY)
 157                return dst->ops->cow_metrics(dst, p);
 158        return __DST_METRICS_PTR(p);
 159}
 160
 161/* This may only be invoked before the entry has reached global
 162 * visibility.
 163 */
 164static inline void dst_init_metrics(struct dst_entry *dst,
 165                                    const u32 *src_metrics,
 166                                    bool read_only)
 167{
 168        dst->_metrics = ((unsigned long) src_metrics) |
 169                (read_only ? DST_METRICS_READ_ONLY : 0);
 170}
 171
 172static inline void dst_copy_metrics(struct dst_entry *dest, const struct dst_entry *src)
 173{
 174        u32 *dst_metrics = dst_metrics_write_ptr(dest);
 175
 176        if (dst_metrics) {
 177                u32 *src_metrics = DST_METRICS_PTR(src);
 178
 179                memcpy(dst_metrics, src_metrics, RTAX_MAX * sizeof(u32));
 180        }
 181}
 182
 183static inline u32 *dst_metrics_ptr(struct dst_entry *dst)
 184{
 185        return DST_METRICS_PTR(dst);
 186}
 187
 188static inline u32
 189dst_metric_raw(const struct dst_entry *dst, const int metric)
 190{
 191        u32 *p = DST_METRICS_PTR(dst);
 192
 193        return p[metric-1];
 194}
 195
 196static inline u32
 197dst_metric(const struct dst_entry *dst, const int metric)
 198{
 199        WARN_ON_ONCE(metric == RTAX_HOPLIMIT ||
 200                     metric == RTAX_ADVMSS ||
 201                     metric == RTAX_MTU);
 202        return dst_metric_raw(dst, metric);
 203}
 204
 205static inline u32
 206dst_metric_advmss(const struct dst_entry *dst)
 207{
 208        u32 advmss = dst_metric_raw(dst, RTAX_ADVMSS);
 209
 210        if (!advmss)
 211                advmss = dst->ops->default_advmss(dst);
 212
 213        return advmss;
 214}
 215
 216static inline void dst_metric_set(struct dst_entry *dst, int metric, u32 val)
 217{
 218        u32 *p = dst_metrics_write_ptr(dst);
 219
 220        if (p)
 221                p[metric-1] = val;
 222}
 223
 224/* Kernel-internal feature bits that are unallocated in user space. */
 225#define DST_FEATURE_ECN_CA      (1 << 31)
 226
 227#define DST_FEATURE_MASK        (DST_FEATURE_ECN_CA)
 228#define DST_FEATURE_ECN_MASK    (DST_FEATURE_ECN_CA | RTAX_FEATURE_ECN)
 229
 230static inline u32
 231dst_feature(const struct dst_entry *dst, u32 feature)
 232{
 233        return dst_metric(dst, RTAX_FEATURES) & feature;
 234}
 235
 236static inline u32 dst_mtu(const struct dst_entry *dst)
 237{
 238        return dst->ops->mtu(dst);
 239}
 240
 241/* RTT metrics are stored in milliseconds for user ABI, but used as jiffies */
 242static inline unsigned long dst_metric_rtt(const struct dst_entry *dst, int metric)
 243{
 244        return msecs_to_jiffies(dst_metric(dst, metric));
 245}
 246
 247static inline u32
 248dst_allfrag(const struct dst_entry *dst)
 249{
 250        int ret = dst_feature(dst,  RTAX_FEATURE_ALLFRAG);
 251        return ret;
 252}
 253
 254static inline int
 255dst_metric_locked(const struct dst_entry *dst, int metric)
 256{
 257        return dst_metric(dst, RTAX_LOCK) & (1<<metric);
 258}
 259
 260static inline void dst_hold(struct dst_entry *dst)
 261{
 262        /*
 263         * If your kernel compilation stops here, please check
 264         * __pad_to_align_refcnt declaration in struct dst_entry
 265         */
 266        BUILD_BUG_ON(offsetof(struct dst_entry, __refcnt) & 63);
 267        atomic_inc(&dst->__refcnt);
 268}
 269
 270static inline void dst_use(struct dst_entry *dst, unsigned long time)
 271{
 272        dst_hold(dst);
 273        dst->__use++;
 274        dst->lastuse = time;
 275}
 276
 277static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
 278{
 279        dst->__use++;
 280        dst->lastuse = time;
 281}
 282
 283static inline struct dst_entry *dst_clone(struct dst_entry *dst)
 284{
 285        if (dst)
 286                atomic_inc(&dst->__refcnt);
 287        return dst;
 288}
 289
 290extern void dst_release(struct dst_entry *dst);
 291
 292static inline void refdst_drop(unsigned long refdst)
 293{
 294        if (!(refdst & SKB_DST_NOREF))
 295                dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
 296}
 297
 298/**
 299 * skb_dst_drop - drops skb dst
 300 * @skb: buffer
 301 *
 302 * Drops dst reference count if a reference was taken.
 303 */
 304static inline void skb_dst_drop(struct sk_buff *skb)
 305{
 306        if (skb->_skb_refdst) {
 307                refdst_drop(skb->_skb_refdst);
 308                skb->_skb_refdst = 0UL;
 309        }
 310}
 311
 312static inline void __skb_dst_copy(struct sk_buff *nskb, unsigned long refdst)
 313{
 314        nskb->_skb_refdst = refdst;
 315        if (!(nskb->_skb_refdst & SKB_DST_NOREF))
 316                dst_clone(skb_dst(nskb));
 317}
 318
 319static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
 320{
 321        __skb_dst_copy(nskb, oskb->_skb_refdst);
 322}
 323
 324/**
 325 * skb_dst_force - makes sure skb dst is refcounted
 326 * @skb: buffer
 327 *
 328 * If dst is not yet refcounted, let's do it
 329 */
 330static inline void skb_dst_force(struct sk_buff *skb)
 331{
 332        if (skb_dst_is_noref(skb)) {
 333                WARN_ON(!rcu_read_lock_held());
 334                skb->_skb_refdst &= ~SKB_DST_NOREF;
 335                dst_clone(skb_dst(skb));
 336        }
 337}
 338
 339/**
 340 * dst_hold_safe - Take a reference on a dst if possible
 341 * @dst: pointer to dst entry
 342 *
 343 * This helper returns false if it could not safely
 344 * take a reference on a dst.
 345 */
 346static inline bool dst_hold_safe(struct dst_entry *dst)
 347{
 348        if (dst->flags & DST_NOCACHE)
 349                return atomic_inc_not_zero(&dst->__refcnt);
 350        dst_hold(dst);
 351        return true;
 352}
 353
 354/**
 355 * skb_dst_force_safe - makes sure skb dst is refcounted
 356 * @skb: buffer
 357 *
 358 * If dst is not yet refcounted and not destroyed, grab a ref on it.
 359 */
 360static inline void skb_dst_force_safe(struct sk_buff *skb)
 361{
 362        if (skb_dst_is_noref(skb)) {
 363                struct dst_entry *dst = skb_dst(skb);
 364
 365                if (!dst_hold_safe(dst))
 366                        dst = NULL;
 367
 368                skb->_skb_refdst = (unsigned long)dst;
 369        }
 370}
 371
 372
 373/**
 374 *      __skb_tunnel_rx - prepare skb for rx reinsert
 375 *      @skb: buffer
 376 *      @dev: tunnel device
 377 *      @net: netns for packet i/o
 378 *
 379 *      After decapsulation, packet is going to re-enter (netif_rx()) our stack,
 380 *      so make some cleanups. (no accounting done)
 381 */
 382static inline void __skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
 383                                   struct net *net)
 384{
 385        skb->dev = dev;
 386
 387        /*
 388         * Clear hash so that we can recalulate the hash for the
 389         * encapsulated packet, unless we have already determine the hash
 390         * over the L4 4-tuple.
 391         */
 392        skb_clear_hash_if_not_l4(skb);
 393        skb_set_queue_mapping(skb, 0);
 394        skb_scrub_packet(skb, !net_eq(net, dev_net(dev)));
 395}
 396
 397/**
 398 *      skb_tunnel_rx - prepare skb for rx reinsert
 399 *      @skb: buffer
 400 *      @dev: tunnel device
 401 *
 402 *      After decapsulation, packet is going to re-enter (netif_rx()) our stack,
 403 *      so make some cleanups, and perform accounting.
 404 *      Note: this accounting is not SMP safe.
 405 */
 406static inline void skb_tunnel_rx(struct sk_buff *skb, struct net_device *dev,
 407                                 struct net *net)
 408{
 409        /* TODO : stats should be SMP safe */
 410        dev->stats.rx_packets++;
 411        dev->stats.rx_bytes += skb->len;
 412        __skb_tunnel_rx(skb, dev, net);
 413}
 414
 415/* Children define the path of the packet through the
 416 * Linux networking.  Thus, destinations are stackable.
 417 */
 418
 419static inline struct dst_entry *skb_dst_pop(struct sk_buff *skb)
 420{
 421        struct dst_entry *child = dst_clone(skb_dst(skb)->child);
 422
 423        skb_dst_drop(skb);
 424        return child;
 425}
 426
 427int dst_discard_sk(struct sock *sk, struct sk_buff *skb);
 428static inline int dst_discard(struct sk_buff *skb)
 429{
 430        return dst_discard_sk(skb->sk, skb);
 431}
 432extern void *dst_alloc(struct dst_ops *ops, struct net_device *dev,
 433                       int initial_ref, int initial_obsolete,
 434                       unsigned short flags);
 435void dst_init(struct dst_entry *dst, struct dst_ops *ops,
 436              struct net_device *dev, int initial_ref, int initial_obsolete,
 437              unsigned short flags);
 438extern void __dst_free(struct dst_entry *dst);
 439extern struct dst_entry *dst_destroy(struct dst_entry *dst);
 440
 441static inline void dst_free(struct dst_entry *dst)
 442{
 443        if (dst->obsolete > 0)
 444                return;
 445        if (!atomic_read(&dst->__refcnt)) {
 446                dst = dst_destroy(dst);
 447                if (!dst)
 448                        return;
 449        }
 450        __dst_free(dst);
 451}
 452
 453static inline void dst_rcu_free(struct rcu_head *head)
 454{
 455        struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
 456        dst_free(dst);
 457}
 458
 459static inline void dst_confirm(struct dst_entry *dst)
 460{
 461}
 462
 463static inline int dst_neigh_output(struct dst_entry *dst, struct neighbour *n,
 464                                   struct sk_buff *skb)
 465{
 466        const struct hh_cache *hh;
 467
 468        hh = &n->hh;
 469        if ((n->nud_state & NUD_CONNECTED) && hh->hh_len)
 470                return neigh_hh_output(hh, skb);
 471        else
 472                return n->output(n, skb);
 473}
 474
 475static inline struct neighbour *dst_neigh_lookup(const struct dst_entry *dst, const void *daddr)
 476{
 477        struct neighbour *n = dst->ops->neigh_lookup(dst, NULL, daddr);
 478        return IS_ERR(n) ? NULL : n;
 479}
 480
 481static inline struct neighbour *dst_neigh_lookup_skb(const struct dst_entry *dst,
 482                                                     struct sk_buff *skb)
 483{
 484        struct neighbour *n =  dst->ops->neigh_lookup(dst, skb, NULL);
 485        return IS_ERR(n) ? NULL : n;
 486}
 487
 488static inline void dst_confirm_neigh(const struct dst_entry *dst,
 489                                     const void *daddr)
 490{
 491        if (dst->ops->confirm_neigh)
 492                dst->ops->confirm_neigh(dst, daddr);
 493}
 494
 495static inline void dst_link_failure(struct sk_buff *skb)
 496{
 497        struct dst_entry *dst = skb_dst(skb);
 498        if (dst && dst->ops && dst->ops->link_failure)
 499                dst->ops->link_failure(skb);
 500}
 501
 502static inline void dst_set_expires(struct dst_entry *dst, int timeout)
 503{
 504        unsigned long expires = jiffies + timeout;
 505
 506        if (expires == 0)
 507                expires = 1;
 508
 509        if (dst->expires == 0 || time_before(expires, dst->expires))
 510                dst->expires = expires;
 511}
 512
 513/* Output packet to network from transport.  */
 514static inline int dst_output_sk(struct sock *sk, struct sk_buff *skb)
 515{
 516        return skb_dst(skb)->output(sk, skb);
 517}
 518static inline int dst_output(struct sk_buff *skb)
 519{
 520        return dst_output_sk(skb->sk, skb);
 521}
 522
 523/* Input packet from network to transport.  */
 524static inline int dst_input(struct sk_buff *skb)
 525{
 526        return skb_dst(skb)->input(skb);
 527}
 528
 529static inline struct dst_entry *dst_check(struct dst_entry *dst, u32 cookie)
 530{
 531        if (dst->obsolete)
 532                dst = dst->ops->check(dst, cookie);
 533        return dst;
 534}
 535
 536void dst_subsys_init(void);
 537
 538/* Flags for xfrm_lookup flags argument. */
 539enum {
 540        XFRM_LOOKUP_ICMP = 1 << 0,
 541        XFRM_LOOKUP_QUEUE = 1 << 1,
 542        XFRM_LOOKUP_KEEP_DST_REF = 1 << 2,
 543};
 544
 545struct flowi;
 546#ifndef CONFIG_XFRM
 547static inline struct dst_entry *xfrm_lookup(struct net *net,
 548                                            struct dst_entry *dst_orig,
 549                                            const struct flowi *fl, struct sock *sk,
 550                                            int flags)
 551{
 552        return dst_orig;
 553}
 554
 555static inline struct dst_entry *xfrm_lookup_route(struct net *net,
 556                                                  struct dst_entry *dst_orig,
 557                                                  const struct flowi *fl,
 558                                                  struct sock *sk,
 559                                                  int flags)
 560{
 561        return dst_orig;
 562}
 563
 564static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
 565{
 566        return NULL;
 567}
 568
 569#else
 570extern struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
 571                                     const struct flowi *fl, struct sock *sk,
 572                                     int flags);
 573
 574struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
 575                                    const struct flowi *fl, struct sock *sk,
 576                                    int flags);
 577
 578/* skb attached with this dst needs transformation if dst->xfrm is valid */
 579static inline struct xfrm_state *dst_xfrm(const struct dst_entry *dst)
 580{
 581        return dst->xfrm;
 582}
 583#endif
 584
 585#endif /* _NET_DST_H */
 586