nexthop.h 9.9 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/notifier.h>
14
#include <linux/route.h>
D
David Ahern 已提交
15 16
#include <linux/types.h>
#include <net/ip_fib.h>
17
#include <net/ip6_fib.h>
D
David Ahern 已提交
18 19 20 21 22 23 24 25 26 27 28 29
#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;
30
	u8		nh_fdb;
D
David Ahern 已提交
31 32 33 34 35
	u32		nh_flags;

	int		nh_ifindex;
	struct net_device *dev;

36 37
	union {
		__be32		ipv4;
38
		struct in6_addr	ipv6;
39 40
	} gw;

41 42 43
	struct nlattr	*nh_grp;
	u16		nh_grp_type;

44 45 46
	struct nlattr	*nh_encap;
	u16		nh_encap_type;

D
David Ahern 已提交
47 48 49 50 51 52 53 54 55 56
	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;
57
	bool			fdb_nh;
D
David Ahern 已提交
58 59 60

	union {
		struct fib_nh_common	fib_nhc;
61
		struct fib_nh		fib_nh;
62
		struct fib6_nh		fib6_nh;
D
David Ahern 已提交
63 64 65
	};
};

66 67 68
struct nh_grp_entry {
	struct nexthop	*nh;
	u8		weight;
69 70 71 72 73 74

	union {
		struct {
			atomic_t	upper_bound;
		} mpath;
	};
75 76 77 78 79 80

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

struct nh_group {
81
	struct nh_group		*spare; /* spare group for removals */
82 83
	u16			num_nh;
	bool			mpath;
84
	bool			fdb_nh;
85
	bool			has_v4;
86
	struct nh_grp_entry	nh_entries[];
87 88
};

D
David Ahern 已提交
89 90
struct nexthop {
	struct rb_node		rb_node;    /* entry on netns rbtree */
91
	struct list_head	fi_list;    /* v4 entries using nh */
92
	struct list_head	f6i_list;   /* v6 entries using nh */
93
	struct list_head        fdb_list;   /* fdb entries using this nh */
94
	struct list_head	grp_list;   /* nh group entries using this nh */
D
David Ahern 已提交
95 96 97 98 99 100
	struct net		*net;

	u32			id;

	u8			protocol;   /* app managing this nh */
	u8			nh_flags;
101
	bool			is_group;
D
David Ahern 已提交
102 103 104 105 106 107

	refcount_t		refcnt;
	struct rcu_head		rcu;

	union {
		struct nh_info	__rcu *nh_info;
108
		struct nh_group __rcu *nh_grp;
D
David Ahern 已提交
109 110 111
	};
};

112
enum nexthop_event_type {
113 114
	NEXTHOP_EVENT_DEL,
	NEXTHOP_EVENT_REPLACE,
115 116
};

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
struct nh_notifier_single_info {
	struct net_device *dev;
	u8 gw_family;
	union {
		__be32 ipv4;
		struct in6_addr ipv6;
	};
	u8 is_reject:1,
	   is_fdb:1,
	   has_encap:1;
};

struct nh_notifier_grp_entry_info {
	u8 weight;
	u32 id;
	struct nh_notifier_single_info nh;
};

struct nh_notifier_grp_info {
	u16 num_nh;
	bool is_fdb;
	struct nh_notifier_grp_entry_info nh_entries[];
};

struct nh_notifier_info {
	struct net *net;
	struct netlink_ext_ack *extack;
	u32 id;
	bool is_grp;
	union {
		struct nh_notifier_single_info *nh;
		struct nh_notifier_grp_info *nh_grp;
	};
};

152 153
int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
			      struct netlink_ext_ack *extack);
154
int unregister_nexthop_notifier(struct net *net, struct notifier_block *nb);
155
void nexthop_set_hw_flags(struct net *net, u32 id, bool offload, bool trap);
156

D
David Ahern 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
/* 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);
}

172 173 174 175
static inline bool nexthop_cmp(const struct nexthop *nh1,
			       const struct nexthop *nh2)
{
	return nh1 == nh2;
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
}

static inline bool nexthop_is_fdb(const struct nexthop *nh)
{
	if (nh->is_group) {
		const struct nh_group *nh_grp;

		nh_grp = rcu_dereference_rtnl(nh->nh_grp);
		return nh_grp->fdb_nh;
	} else {
		const struct nh_info *nhi;

		nhi = rcu_dereference_rtnl(nh->nh_info);
		return nhi->fdb_nh;
	}
191 192 193 194 195 196 197 198 199 200 201
}

static inline bool nexthop_has_v4(const struct nexthop *nh)
{
	if (nh->is_group) {
		struct nh_group *nh_grp;

		nh_grp = rcu_dereference_rtnl(nh->nh_grp);
		return nh_grp->has_v4;
	}
	return false;
202 203
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
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;

221
	if (nh->is_group) {
222 223 224
		struct nh_group *nh_grp;

		nh_grp = rcu_dereference_rtnl(nh->nh_grp);
225 226
		if (nh_grp->mpath)
			rc = nh_grp->num_nh;
227 228 229 230 231 232
	}

	return rc;
}

static inline
233
struct nexthop *nexthop_mpath_select(const struct nh_group *nhg, int nhsel)
234 235 236 237
{
	/* for_nexthops macros in fib_semantics.c grabs a pointer to
	 * the nexthop before checking nhsel
	 */
238
	if (nhsel >= nhg->num_nh)
239 240 241 242 243 244
		return NULL;

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

static inline
245 246
int nexthop_mpath_fill_node(struct sk_buff *skb, struct nexthop *nh,
			    u8 rt_family)
247 248 249 250 251 252 253 254 255 256
{
	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;

257
		if (fib_add_nexthop(skb, nhc, weight, rt_family) < 0)
258 259 260 261 262 263
			return -EMSGSIZE;
	}

	return 0;
}

D
David Ahern 已提交
264 265 266 267 268
/* called with rcu lock */
static inline bool nexthop_is_blackhole(const struct nexthop *nh)
{
	const struct nh_info *nhi;

269 270 271 272 273
	if (nh->is_group) {
		struct nh_group *nh_grp;

		nh_grp = rcu_dereference_rtnl(nh->nh_grp);
		if (nh_grp->num_nh > 1)
274
			return false;
275 276

		nh = nh_grp->nh_entries[0].nh;
277 278 279
	}

	nhi = rcu_dereference_rtnl(nh->nh_info);
D
David Ahern 已提交
280 281
	return nhi->reject_nh;
}
282

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
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);

302 303 304 305 306 307 308 309 310
	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;
		}
311 312 313 314 315 316
	}

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

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
/* called from fib_table_lookup with rcu_lock */
static inline
struct fib_nh_common *nexthop_get_nhc_lookup(const struct nexthop *nh,
					     int fib_flags,
					     const struct flowi4 *flp,
					     int *nhsel)
{
	struct nh_info *nhi;

	if (nh->is_group) {
		struct nh_group *nhg = rcu_dereference(nh->nh_grp);
		int i;

		for (i = 0; i < nhg->num_nh; i++) {
			struct nexthop *nhe = nhg->nh_entries[i].nh;

			nhi = rcu_dereference(nhe->nh_info);
			if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
				*nhsel = i;
				return &nhi->fib_nhc;
			}
		}
	} else {
		nhi = rcu_dereference(nh->nh_info);
		if (fib_lookup_good_nhc(&nhi->fib_nhc, fib_flags, flp)) {
			*nhsel = 0;
			return &nhi->fib_nhc;
		}
	}

	return NULL;
}

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
static inline bool nexthop_uses_dev(const struct nexthop *nh,
				    const struct net_device *dev)
{
	struct nh_info *nhi;

	if (nh->is_group) {
		struct nh_group *nhg = rcu_dereference(nh->nh_grp);
		int i;

		for (i = 0; i < nhg->num_nh; i++) {
			struct nexthop *nhe = nhg->nh_entries[i].nh;

			nhi = rcu_dereference(nhe->nh_info);
			if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
				return true;
		}
	} else {
		nhi = rcu_dereference(nh->nh_info);
		if (nhc_l3mdev_matches_dev(&nhi->fib_nhc, dev))
			return true;
	}

	return false;
}

375 376
static inline unsigned int fib_info_num_path(const struct fib_info *fi)
{
377 378 379
	if (unlikely(fi->nh))
		return nexthop_num_path(fi->nh);

380 381 382
	return fi->fib_nhs;
}

383 384 385
int fib_check_nexthop(struct nexthop *nh, u8 scope,
		      struct netlink_ext_ack *extack);

386 387
static inline struct fib_nh_common *fib_info_nhc(struct fib_info *fi, int nhsel)
{
388 389 390
	if (unlikely(fi->nh))
		return nexthop_fib_nhc(fi->nh, nhsel);

391 392 393
	return &fi->fib_nh[nhsel].nh_common;
}

394
/* only used when fib_nh is built into fib_info */
395 396
static inline struct fib_nh *fib_info_nh(struct fib_info *fi, int nhsel)
{
397 398
	WARN_ON(fi->nh);

399 400
	return &fi->fib_nh[nhsel];
}
401 402 403 404 405 406 407 408 409 410 411

/*
 * 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;

412 413 414 415 416
	if (nh->is_group) {
		struct nh_group *nh_grp;

		nh_grp = rcu_dereference_rtnl(nh->nh_grp);
		nh = nexthop_mpath_select(nh_grp, 0);
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
		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;
	}
}
452 453 454 455

int nexthop_for_each_fib6_nh(struct nexthop *nh,
			     int (*cb)(struct fib6_nh *nh, void *arg),
			     void *arg);
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

static inline int nexthop_get_family(struct nexthop *nh)
{
	struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);

	return nhi->family;
}

static inline
struct fib_nh_common *nexthop_fdb_nhc(struct nexthop *nh)
{
	struct nh_info *nhi = rcu_dereference_rtnl(nh->nh_info);

	return &nhi->fib_nhc;
}

static inline struct fib_nh_common *nexthop_path_fdb_result(struct nexthop *nh,
							    int hash)
{
	struct nh_info *nhi;
	struct nexthop *nhp;

	nhp = nexthop_select_path(nh, hash);
	if (unlikely(!nhp))
		return NULL;
	nhi = rcu_dereference(nhp->nh_info);
	return &nhi->fib_nhc;
}
D
David Ahern 已提交
484
#endif