Commit ab84be7e authored by David Ahern's avatar David Ahern Committed by David S. Miller

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: default avatarDavid Ahern <dsahern@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 65ee00a9
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <net/netns/packet.h> #include <net/netns/packet.h>
#include <net/netns/ipv4.h> #include <net/netns/ipv4.h>
#include <net/netns/ipv6.h> #include <net/netns/ipv6.h>
#include <net/netns/nexthop.h>
#include <net/netns/ieee802154_6lowpan.h> #include <net/netns/ieee802154_6lowpan.h>
#include <net/netns/sctp.h> #include <net/netns/sctp.h>
#include <net/netns/dccp.h> #include <net/netns/dccp.h>
...@@ -108,6 +109,7 @@ struct net { ...@@ -108,6 +109,7 @@ struct net {
struct netns_mib mib; struct netns_mib mib;
struct netns_packet packet; struct netns_packet packet;
struct netns_unix unx; struct netns_unix unx;
struct netns_nexthop nexthop;
struct netns_ipv4 ipv4; struct netns_ipv4 ipv4;
#if IS_ENABLED(CONFIG_IPV6) #if IS_ENABLED(CONFIG_IPV6)
struct netns_ipv6 ipv6; struct netns_ipv6 ipv6;
......
/* SPDX-License-Identifier: GPL-2.0 */
/*
* nexthops in net namespaces
*/
#ifndef __NETNS_NEXTHOP_H__
#define __NETNS_NEXTHOP_H__
#include <linux/rbtree.h>
struct netns_nexthop {
struct rb_root rb_root; /* tree of nexthops by id */
struct hlist_head *devhash; /* nexthops by device */
unsigned int seq; /* protected by rtnl_mutex */
u32 last_id_allocated;
};
#endif
/* 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>
#include <linux/types.h>
#include <net/ip_fib.h>
#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;
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;
};
};
struct nexthop {
struct rb_node rb_node; /* entry on netns rbtree */
struct net *net;
u32 id;
u8 protocol; /* app managing this nh */
u8 nh_flags;
refcount_t refcnt;
struct rcu_head rcu;
union {
struct nh_info __rcu *nh_info;
};
};
/* 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);
}
/* called with rcu lock */
static inline bool nexthop_is_blackhole(const struct nexthop *nh)
{
const struct nh_info *nhi;
nhi = rcu_dereference(nh->nh_info);
return nhi->reject_nh;
}
#endif
...@@ -14,7 +14,7 @@ obj-y := route.o inetpeer.o protocol.o \ ...@@ -14,7 +14,7 @@ obj-y := route.o inetpeer.o protocol.o \
udp_offload.o arp.o icmp.o devinet.o af_inet.o igmp.o \ udp_offload.o arp.o icmp.o devinet.o af_inet.o igmp.o \
fib_frontend.o fib_semantics.o fib_trie.o fib_notifier.o \ fib_frontend.o fib_semantics.o fib_trie.o fib_notifier.o \
inet_fragment.o ping.o ip_tunnel_core.o gre_offload.o \ inet_fragment.o ping.o ip_tunnel_core.o gre_offload.o \
metrics.o netlink.o metrics.o netlink.o nexthop.o
obj-$(CONFIG_BPFILTER) += bpfilter/ obj-$(CONFIG_BPFILTER) += bpfilter/
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment