Skip to content

Commit ab84be7

Browse files
dsaherndavem330
authored andcommitted
net: Initial nexthop code
Barebones start point for nexthops. Implementation for RTM commands, notifications, management of rbtree for holding nexthops by id, and kernel side data structures for nexthops and nexthop config. Nexthops are maintained in an rbtree sorted by id. Similar to routes, nexthops are configured per namespace using netns_nexthop struct added to struct net. Nexthop notifications are sent when a nexthop is added or deleted, but NOT if the delete is due to a device event or network namespace teardown (which also involves device events). Applications are expected to use the device down event to flush nexthops and any routes used by the nexthops. Signed-off-by: David Ahern <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 65ee00a commit ab84be7

File tree

5 files changed

+831
-1
lines changed

5 files changed

+831
-1
lines changed

include/net/net_namespace.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <net/netns/packet.h>
2020
#include <net/netns/ipv4.h>
2121
#include <net/netns/ipv6.h>
22+
#include <net/netns/nexthop.h>
2223
#include <net/netns/ieee802154_6lowpan.h>
2324
#include <net/netns/sctp.h>
2425
#include <net/netns/dccp.h>
@@ -108,6 +109,7 @@ struct net {
108109
struct netns_mib mib;
109110
struct netns_packet packet;
110111
struct netns_unix unx;
112+
struct netns_nexthop nexthop;
111113
struct netns_ipv4 ipv4;
112114
#if IS_ENABLED(CONFIG_IPV6)
113115
struct netns_ipv6 ipv6;

include/net/netns/nexthop.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* nexthops in net namespaces
4+
*/
5+
6+
#ifndef __NETNS_NEXTHOP_H__
7+
#define __NETNS_NEXTHOP_H__
8+
9+
#include <linux/rbtree.h>
10+
11+
struct netns_nexthop {
12+
struct rb_root rb_root; /* tree of nexthops by id */
13+
struct hlist_head *devhash; /* nexthops by device */
14+
15+
unsigned int seq; /* protected by rtnl_mutex */
16+
u32 last_id_allocated;
17+
};
18+
#endif

include/net/nexthop.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

net/ipv4/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ obj-y := route.o inetpeer.o protocol.o \
1414
udp_offload.o arp.o icmp.o devinet.o af_inet.o igmp.o \
1515
fib_frontend.o fib_semantics.o fib_trie.o fib_notifier.o \
1616
inet_fragment.o ping.o ip_tunnel_core.o gre_offload.o \
17-
metrics.o netlink.o
17+
metrics.o netlink.o nexthop.o
1818

1919
obj-$(CONFIG_BPFILTER) += bpfilter/
2020

0 commit comments

Comments
 (0)