|
| 1 | +/* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | +/* |
| 3 | + * Generic nexthop implementation |
| 4 | + * |
| 5 | + * Copyright (c) 2017-19 Cumulus Networks |
| 6 | + * Copyright (c) 2017-19 David Ahern <[email protected]> |
| 7 | + */ |
| 8 | + |
| 9 | +#ifndef __LINUX_NEXTHOP_H |
| 10 | +#define __LINUX_NEXTHOP_H |
| 11 | + |
| 12 | +#include <linux/netdevice.h> |
| 13 | +#include <linux/types.h> |
| 14 | +#include <net/ip_fib.h> |
| 15 | +#include <net/netlink.h> |
| 16 | + |
| 17 | +#define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK |
| 18 | + |
| 19 | +struct nexthop; |
| 20 | + |
| 21 | +struct nh_config { |
| 22 | + u32 nh_id; |
| 23 | + |
| 24 | + u8 nh_family; |
| 25 | + u8 nh_protocol; |
| 26 | + u8 nh_blackhole; |
| 27 | + u32 nh_flags; |
| 28 | + |
| 29 | + int nh_ifindex; |
| 30 | + struct net_device *dev; |
| 31 | + |
| 32 | + u32 nlflags; |
| 33 | + struct nl_info nlinfo; |
| 34 | +}; |
| 35 | + |
| 36 | +struct nh_info { |
| 37 | + struct hlist_node dev_hash; /* entry on netns devhash */ |
| 38 | + struct nexthop *nh_parent; |
| 39 | + |
| 40 | + u8 family; |
| 41 | + bool reject_nh; |
| 42 | + |
| 43 | + union { |
| 44 | + struct fib_nh_common fib_nhc; |
| 45 | + }; |
| 46 | +}; |
| 47 | + |
| 48 | +struct nexthop { |
| 49 | + struct rb_node rb_node; /* entry on netns rbtree */ |
| 50 | + struct net *net; |
| 51 | + |
| 52 | + u32 id; |
| 53 | + |
| 54 | + u8 protocol; /* app managing this nh */ |
| 55 | + u8 nh_flags; |
| 56 | + |
| 57 | + refcount_t refcnt; |
| 58 | + struct rcu_head rcu; |
| 59 | + |
| 60 | + union { |
| 61 | + struct nh_info __rcu *nh_info; |
| 62 | + }; |
| 63 | +}; |
| 64 | + |
| 65 | +/* caller is holding rcu or rtnl; no reference taken to nexthop */ |
| 66 | +struct nexthop *nexthop_find_by_id(struct net *net, u32 id); |
| 67 | +void nexthop_free_rcu(struct rcu_head *head); |
| 68 | + |
| 69 | +static inline bool nexthop_get(struct nexthop *nh) |
| 70 | +{ |
| 71 | + return refcount_inc_not_zero(&nh->refcnt); |
| 72 | +} |
| 73 | + |
| 74 | +static inline void nexthop_put(struct nexthop *nh) |
| 75 | +{ |
| 76 | + if (refcount_dec_and_test(&nh->refcnt)) |
| 77 | + call_rcu(&nh->rcu, nexthop_free_rcu); |
| 78 | +} |
| 79 | + |
| 80 | +/* called with rcu lock */ |
| 81 | +static inline bool nexthop_is_blackhole(const struct nexthop *nh) |
| 82 | +{ |
| 83 | + const struct nh_info *nhi; |
| 84 | + |
| 85 | + nhi = rcu_dereference(nh->nh_info); |
| 86 | + return nhi->reject_nh; |
| 87 | +} |
| 88 | +#endif |
0 commit comments