nexthop.h 6.5 KB
Newer Older
D
David Ahern 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Generic nexthop implementation
 *
 * Copyright (c) 2017-19 Cumulus Networks
 * Copyright (c) 2017-19 David Ahern <dsa@cumulusnetworks.com>
 */

#ifndef __LINUX_NEXTHOP_H
#define __LINUX_NEXTHOP_H

#include <linux/netdevice.h>
13
#include <linux/route.h>
D
David Ahern 已提交
14 15
#include <linux/types.h>
#include <net/ip_fib.h>
16
#include <net/ip6_fib.h>
D
David Ahern 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <net/netlink.h>

#define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK

struct nexthop;

struct nh_config {
	u32		nh_id;

	u8		nh_family;
	u8		nh_protocol;
	u8		nh_blackhole;
	u32		nh_flags;

	int		nh_ifindex;
	struct net_device *dev;

34 35
	union {
		__be32		ipv4;
36
		struct in6_addr	ipv6;
37 38
	} gw;

39 40 41
	struct nlattr	*nh_grp;
	u16		nh_grp_type;

42 43 44
	struct nlattr	*nh_encap;
	u16		nh_encap_type;

D
David Ahern 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57
	u32		nlflags;
	struct nl_info	nlinfo;
};

struct nh_info {
	struct hlist_node	dev_hash;    /* entry on netns devhash */
	struct nexthop		*nh_parent;

	u8			family;
	bool			reject_nh;

	union {
		struct fib_nh_common	fib_nhc;
58
		struct fib_nh		fib_nh;
59
		struct fib6_nh		fib6_nh;
D
David Ahern 已提交
60 61 62
	};
};

63 64 65 66 67 68 69 70 71 72
struct nh_grp_entry {
	struct nexthop	*nh;
	u8		weight;
	atomic_t	upper_bound;

	struct list_head nh_list;
	struct nexthop	*nh_parent;  /* nexthop of group with this entry */
};

struct nh_group {
73
	struct nh_group		*spare; /* spare group for removals */
74 75 76
	u16			num_nh;
	bool			mpath;
	bool			has_v4;
77
	struct nh_grp_entry	nh_entries[];
78 79
};

D
David Ahern 已提交
80 81
struct nexthop {
	struct rb_node		rb_node;    /* entry on netns rbtree */
82
	struct list_head	fi_list;    /* v4 entries using nh */
83
	struct list_head	f6i_list;   /* v6 entries using nh */
84
	struct list_head	grp_list;   /* nh group entries using this nh */
D
David Ahern 已提交
85 86 87 88 89 90
	struct net		*net;

	u32			id;

	u8			protocol;   /* app managing this nh */
	u8			nh_flags;
91
	bool			is_group;
D
David Ahern 已提交
92 93 94 95 96 97

	refcount_t		refcnt;
	struct rcu_head		rcu;

	union {
		struct nh_info	__rcu *nh_info;
98
		struct nh_group __rcu *nh_grp;
D
David Ahern 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	};
};

/* caller is holding rcu or rtnl; no reference taken to nexthop */
struct nexthop *nexthop_find_by_id(struct net *net, u32 id);
void nexthop_free_rcu(struct rcu_head *head);

static inline bool nexthop_get(struct nexthop *nh)
{
	return refcount_inc_not_zero(&nh->refcnt);
}

static inline void nexthop_put(struct nexthop *nh)
{
	if (refcount_dec_and_test(&nh->refcnt))
		call_rcu(&nh->rcu, nexthop_free_rcu);
}

117 118 119 120 121 122
static inline bool nexthop_cmp(const struct nexthop *nh1,
			       const struct nexthop *nh2)
{
	return nh1 == nh2;
}

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
static inline bool nexthop_is_multipath(const struct nexthop *nh)
{
	if (nh->is_group) {
		struct nh_group *nh_grp;

		nh_grp = rcu_dereference_rtnl(nh->nh_grp);
		return nh_grp->mpath;
	}
	return false;
}

struct nexthop *nexthop_select_path(struct nexthop *nh, int hash);

static inline unsigned int nexthop_num_path(const struct nexthop *nh)
{
	unsigned int rc = 1;

140
	if (nh->is_group) {
141 142 143
		struct nh_group *nh_grp;

		nh_grp = rcu_dereference_rtnl(nh->nh_grp);
144 145
		if (nh_grp->mpath)
			rc = nh_grp->num_nh;
146 147 148 149 150 151
	}

	return rc;
}

static inline
152
struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
153 154 155 156
{
	/* for_nexthops macros in fib_semantics.c grabs a pointer to
	 * the nexthop before checking nhsel
	 */
157
	if (nhsel >= nhg->num_nh)
158 159 160 161 162 163
		return NULL;

	return nhg->nh_entries[nhsel].nh;
}

static inline
164 165
int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
			    u8 rt_family)
166 167 168 169 170 171 172 173 174 175
{
	struct nh_group *nhg = rtnl_dereference(nh->nh_grp);
	int i;

	for (i = 0; i < nhg->num_nh; i++) {
		struct nexthop *nhe = nhg->nh_entries[i].nh;
		struct nh_info *nhi = rcu_dereference_rtnl(nhe->nh_info);
		struct fib_nh_common *nhc = &nhi->fib_nhc;
		int weight = nhg->nh_entries[i].weight;

176
		if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0)
177 178 179 180 181 182
			return -EMSGSIZE;
	}

	return 0;
}

D
David Ahern 已提交
183 184 185 186 187
/* called with rcu lock */
static inline bool nexthop_is_blackhole(const struct nexthop *nh)
{
	const struct nh_info *nhi;

188 189 190 191 192
	if (nh->is_group) {
		struct nh_group *nh_grp;

		nh_grp = rcu_dereference_rtnl(nh->nh_grp);
		if (nh_grp->num_nh > 1)
193
			return false;
194 195

		nh = nh_grp->nh_entries[0].nh;
196 197 198
	}

	nhi = rcu_dereference_rtnl(nh->nh_info);
D
David Ahern 已提交
199 200
	return nhi->reject_nh;
}
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
static inline void nexthop_path_fib_result(struct fib_result *res, int hash)
{
	struct nh_info *nhi;
	struct nexthop *nh;

	nh = nexthop_select_path(res->fi->nh, hash);
	nhi = rcu_dereference(nh->nh_info);
	res->nhc = &nhi->fib_nhc;
}

/* called with rcu read lock or rtnl held */
static inline
struct fib_nh_common *nexthop_fib_nhc(struct nexthop *nh, int nhsel)
{
	struct nh_info *nhi;

	BUILD_BUG_ON(offsetof(struct fib_nh, nh_common) != 0);
	BUILD_BUG_ON(offsetof(struct fib6_nh, nh_common) != 0);

221 222 223 224 225 226 227 228 229
	if (nh->is_group) {
		struct nh_group *nh_grp;

		nh_grp = rcu_dereference_rtnl(nh->nh_grp);
		if (nh_grp->mpath) {
			nh = nexthop_mpath_select(nh_grp, nhsel);
			if (!nh)
				return NULL;
		}
230 231 232 233 234 235
	}

	nhi = rcu_dereference_rtnl(nh->nh_info);
	return &nhi->fib_nhc;
}

236 237
static inline unsigned int fib_info_num_path(const struct fib_info *fi)
{
238 239 240
	if (unlikely(fi->nh))
		return nexthop_num_path(fi->nh);

241 242 243
	return fi->fib_nhs;
}

244 245 246
int fib_check_nexthop(struct nexthop *nh, u8 scope,
		      struct netlink_ext_ack *extack);

247 248
static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
{
249 250 251
	if (unlikely(fi->nh))
		return nexthop_fib_nhc(fi->nh, nhsel);

252 253 254
	return &fi->fib_nh[nhsel].nh_common;
}

255
/* only used when fib_nh is built into fib_info */
256 257
static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
{
258 259
	WARN_ON(fi->nh);

260 261
	return &fi->fib_nh[nhsel];
}
262 263 264 265 266 267 268 269 270 271 272

/*
 * IPv6 variants
 */
int fib6_check_nexthop(struct nexthop *nh, struct fib6_config *cfg,
		       struct netlink_ext_ack *extack);

static inline struct fib6_nh *nexthop_fib6_nh(struct nexthop *nh)
{
	struct nh_info *nhi;

273 274 275 276 277
	if (nh->is_group) {
		struct nh_group *nh_grp;

		nh_grp = rcu_dereference_rtnl(nh->nh_grp);
		nh = nexthop_mpath_select(nh_grp, 0);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
		if (!nh)
			return NULL;
	}

	nhi = rcu_dereference_rtnl(nh->nh_info);
	if (nhi->family == AF_INET6)
		return &nhi->fib6_nh;

	return NULL;
}

static inline struct net_device *fib6_info_nh_dev(struct fib6_info *f6i)
{
	struct fib6_nh *fib6_nh;

	fib6_nh = f6i->nh ? nexthop_fib6_nh(f6i->nh) : f6i->fib6_nh;
	return fib6_nh->fib_nh_dev;
}

static inline void nexthop_path_fib6_result(struct fib6_result *res, int hash)
{
	struct nexthop *nh = res->f6i->nh;
	struct nh_info *nhi;

	nh = nexthop_select_path(nh, hash);

	nhi = rcu_dereference_rtnl(nh->nh_info);
	if (nhi->reject_nh) {
		res->fib6_type = RTN_BLACKHOLE;
		res->fib6_flags |= RTF_REJECT;
		res->nh = nexthop_fib6_nh(nh);
	} else {
		res->nh = &nhi->fib6_nh;
	}
}
313 314 315 316

int nexthop_for_each_fib6_nh(struct nexthop *nh,
			     int (*cb)(struct fib6_nh *nh, void *arg),
			     void *arg);
D
David Ahern 已提交
317
#endif