net: API and initial implementation for nexthop objects
Subsystems (4)
David Ahern
maintainercover net: API and initial implementation for nexthop objects
From: David Ahern <[email protected]>
This set contains the API and initial implementation for nexthops as standalone objects.
Patch 1 contains the UAPI and updates to selinux struct.
Patch 2 contains the barebones code for nexthop commands, rbtree maintenance and notifications.
Patch 3 then adds support for IPv4 gateways along with handling of netdev events.
Patch 4 adds support for IPv6 gateways.
Patch 5 has the implementation of the encap attributes.
Patch 6 adds support for nexthop groups.
At the end of this set, nexthop objects can be created and deleted and userspace can monitor nexthop events, but ipv4 and ipv6 routes can not use them yet. Once the nexthop struct is defined, follow on sets add it to fib{6}_info and handle it within the respective code before routes can be inserted using them.
David Ahern (6):
net: nexthop uapi
net: Initial nexthop code
nexthop: Add support for IPv4 nexthops
nexthop: Add support for IPv6 gateways
nexthop: Add support for lwt encaps
nexthop: Add support for nexthop groups
include/net/net_namespace.h | 2 + include/net/netns/nexthop.h | 18 + include/net/nexthop.h | 195 ++++++ include/uapi/linux/nexthop.h | 56 ++ include/uapi/linux/rtnetlink.h | 10 + net/ipv4/Makefile | 2 +- net/ipv4/nexthop.c | 1479 ++++++++++++++++++++++++++++++++++++++++ security/selinux/nlmsgtab.c | 5 +- 8 files changed, 1765 insertions(+), 2 deletions(-) create mode 100644 include/net/netns/nexthop.h create mode 100644 include/net/nexthop.h create mode 100644 include/uapi/linux/nexthop.h create mode 100644 net/ipv4/nexthop.c
Signature · 1 line
2.11.0
David Ahern
maintainerPATCH 1/6 net: nexthop uapi
From: David Ahern <[email protected]>
New UAPI for nexthops as standalone objects:
- defines netlink ancillary header, struct nhmsg
- RTM commands for nexthop objects, RTM_*NEXTHOP,
- RTNLGRP for nexthop notifications, RTNLGRP_NEXTHOP,
- Attributes for creating nexthops, NHA_*
- Attribute for route specs to specify a nexthop by id, RTA_NH_ID.
The nexthop attributes and semantics follow the route and RTA ones for device, gateway and lwt encap. Unique to nexthop objects are a blackhole and a group which contains references to other nexthop objects. With the exception of blackhole and group, nexthop objects MUST contain a device. Gateway and encap are optional. Nexthop groups can only reference other pre-existing nexthops by id. If the NHA_ID attribute is present that id is used for the nexthop. If not specified, one is auto assigned.
Dump requests can include attributes:
- NHA_GROUPS to return only nexthop groups,
- NHA_MASTER to limit dumps to nexthops with devices enslaved to the given master (e.g., VRF)
- NHA_OIF to limit dumps to nexthops using given device
nlmsg_route_perms in selinux code is updated for the new RTM comands.
--- include/uapi/linux/nexthop.h | 56 ++++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/rtnetlink.h | 10 ++++++++ security/selinux/nlmsgtab.c | 5 +++- 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 include/uapi/linux/nexthop.h
include/uapi/linux/nexthop.h new file +56 -0
@@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_LINUX_NEXTHOP_H +#define _UAPI_LINUX_NEXTHOP_H + +#include <linux/types.h> + +struct nhmsg { +unsigned char nh_family; +unsigned char nh_scope;/* return only */ +unsigned char nh_protocol;/* Routing protocol that installed nh */ +unsigned char resvd; +unsigned int nh_flags;/* RTNH_F flags */ +}; + +/* entry in a nexthop group */ +struct nexthop_grp { +__u32 id;/* nexthop id - must exist */ +__u8 weight;/* weight of this nexthop */ +__u8 resvd1; +__u16 resvd2; +}; + +enum { +NEXTHOP_GRP_TYPE_MPATH ,/* default type if not specified */ + __NEXTHOP_GRP_TYPE_MAX, +}; + +#define NEXTHOP_GRP_TYPE_MAX (__NEXTHOP_GRP_TYPE_MAX - 1) + +enum { +NHA_UNSPEC , +NHA_ID ,/* u32; id for nexthop. id == 0 means auto-assign */ + +NHA_GROUP ,/* array of nexthop_grp */ +NHA_GROUP_TYPE ,/* u16 one of NEXTHOP_GRP_TYPE */ +/* if NHA_GROUP attribute is added, no other attributes can be set */ + +NHA_BLACKHOLE ,/* flag; nexthop used to blackhole packets */ +/* if NHA_BLACKHOLE is added, OIF, GATEWAY, ENCAP can not be set */ + +NHA_OIF ,/* u32; nexthop device */ +NHA_GATEWAY ,/* be32 (IPv4) or in6_addr (IPv6) gw address */ +NHA_ENCAP_TYPE ,/* u16; lwt encap type */ +NHA_ENCAP ,/* lwt encap data */ + +/* NHA_OIF can be appended to dump request to return only +* nexthops using given device +*/ +NHA_GROUPS ,/* flag; only return nexthop groups in dump */ +NHA_MASTER ,/* u32; only return nexthops with given master dev */ + + __NHA_MAX, +}; + +#define NHA_MAX (__NHA_MAX - 1) +#endif
include/uapi/linux/rtnetlink.h +10 -0
@@ -157,6 +157,13 @@ enum { RTM_GETCHAIN,#define RTM_GETCHAIN RTM_GETCHAIN + RTM_NEWNEXTHOP = 104, +#define RTM_NEWNEXTHOP RTM_NEWNEXTHOP + RTM_DELNEXTHOP, +#define RTM_DELNEXTHOP RTM_DELNEXTHOP + RTM_GETNEXTHOP, +#define RTM_GETNEXTHOP RTM_GETNEXTHOP + __RTM_MAX,#define RTM_MAX (((__RTM_MAX + 3) & ~3) - 1) };
@@ -342,6 +349,7 @@ enum rtattr_type_t {RTA_IP_PROTO ,RTA_SPORT ,RTA_DPORT , +RTA_NH_ID , __RTA_MAX };
@@ -704,6 +712,8 @@ enum rtnetlink_groups {#define RTNLGRP_IPV4_MROUTE_R RTNLGRP_IPV4_MROUTE_R RTNLGRP_IPV6_MROUTE_R,#define RTNLGRP_IPV6_MROUTE_R RTNLGRP_IPV6_MROUTE_R + RTNLGRP_NEXTHOP, +#define RTNLGRP_NEXTHOP RTNLGRP_NEXTHOP __RTNLGRP_MAX };#define RTNLGRP_MAX (__RTNLGRP_MAX - 1)
security/selinux/nlmsgtab.c +4 -1
@@ -83,6 +83,9 @@ static const struct nlmsg_perm nlmsg_route_perms[] = { RTM_NEWCHAIN, NETLINK_ROUTE_SOCKET__NLMSG_WRITE }, { RTM_DELCHAIN, NETLINK_ROUTE_SOCKET__NLMSG_WRITE }, { RTM_GETCHAIN, NETLINK_ROUTE_SOCKET__NLMSG_READ }, + { RTM_NEWNEXTHOP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE }, + { RTM_DELNEXTHOP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE }, + { RTM_GETNEXTHOP, NETLINK_ROUTE_SOCKET__NLMSG_READ }, }; static const struct nlmsg_perm nlmsg_tcpdiag_perms[] =
@@ -166,7 +169,7 @@ int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm)* structures at the top of this file with the new mappings * before updating the BUILD_BUG_ON() macro! */ -BUILD_BUG_ON (RTM_MAX != (RTM_NEWCHAIN +3 )); +BUILD_BUG_ON (RTM_MAX != (RTM_NEWNEXTHOP +3 )); err =nlmsg_perm (nlmsg_type, perm, nlmsg_route_perms,sizeof (nlmsg_route_perms));break ;
Signature · 1 line
2.11.0
David Ahern
maintainerPATCH 2/6 net: Initial nexthop code
In reply to David AhernFrom: David Ahern <[email protected]>
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.
--- include/net/net_namespace.h | 2 + include/net/netns/nexthop.h | 18 ++ include/net/nexthop.h | 88 ++++++ net/ipv4/Makefile | 2 +- net/ipv4/nexthop.c | 722 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 831 insertions(+), 1 deletion(-) create mode 100644 include/net/netns/nexthop.h create mode 100644 include/net/nexthop.h create mode 100644 net/ipv4/nexthop.c
include/net/net_namespace.h +2 -0
@@ -19,6 +19,7 @@#include <net/netns/packet.h> #include <net/netns/ipv4.h> #include <net/netns/ipv6.h> +#include <net/netns/nexthop.h> #include <net/netns/ieee802154_6lowpan.h> #include <net/netns/sctp.h> #include <net/netns/dccp.h>
@@ -108,6 +109,7 @@ struct net {struct netns_mib mib;struct netns_packet packet;struct netns_unix unx; +struct netns_nexthop nexthop;struct netns_ipv4 ipv4;#if IS_ENABLED (CONFIG_IPV6 )struct netns_ipv6 ipv6;
include/net/netns/nexthop.h new file +18 -0
@@ -0,0 +1,18 @@ +/* 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
include/net/nexthop.h new file +88 -0
88 lines not on this page · raw message
net/ipv4/Makefile +1 -1
@@ -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 \ fib_frontend.o fib_semantics.o fib_trie.o fib_notifier.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/
net/ipv4/nexthop.c new file +722 -0
722 lines not on this page · raw message
Signature · 1 line
2.11.0
David Ahern
maintainerPATCH 3/6 nexthop: Add support for IPv4 nexthops
In reply to David AhernFrom: David Ahern <[email protected]>
Add support for IPv4 nexthops. If nh_family is set to AF_INET, then NHA_GATEWAY is expected to be an IPv4 address.
Register for netdev events to be notified of admin up/down changes as well as deletes. A hash table is used to track nexthop per devices to quickly convert device events to the affected nexthops.
--- include/net/nexthop.h | 5 ++ net/ipv4/nexthop.c | 208 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 213 insertions(+)
include/net/nexthop.h +5 -0
@@ -29,6 +29,10 @@ struct nh_config {int nh_ifindex;struct net_device *dev; +union { +__be32 ipv4; + } gw; +u32 nlflags;struct nl_info nlinfo; };
@@ -42,6 +46,7 @@ struct nh_info {union {struct fib_nh_common fib_nhc; +struct fib_nh fib_nh; }; };
net/ipv4/nexthop.c +208 -0
328 lines not on this page · raw message
Signature · 1 line
2.11.0
David Ahern
maintainerPATCH 4/6 nexthop: Add support for IPv6 gateways
In reply to David AhernFrom: David Ahern <[email protected]>
Handle IPv6 gateway in a nexthop spec. If nh_family is set to AF_INET6, NHA_GATEWAY is expected to be an IPv6 address. Add ipv6 option to gw in nh_config to hold the address, add fib6_nh to nh_info to leverage the ipv6 initialization and cleanup code. Update nh_fill_node to dump the v6 address.
--- include/net/nexthop.h | 3 +++ net/ipv4/nexthop.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+)
include/net/nexthop.h +3 -0
@@ -12,6 +12,7 @@#include <linux/netdevice.h> #include <linux/types.h> #include <net/ip_fib.h> +#include <net/ip6_fib.h> #include <net/netlink.h> #define NEXTHOP_VALID_USER_FLAGS RTNH_F_ONLINK
@@ -31,6 +32,7 @@ struct nh_config {union {__be32 ipv4; +struct in6_addr ipv6; } gw;u32 nlflags;
@@ -47,6 +49,7 @@ struct nh_info {union {struct fib_nh_common fib_nhc;struct fib_nh fib_nh; +struct fib6_nh fib6_nh; }; };
net/ipv4/nexthop.c +56 -0
110 lines not on this page · raw message
Signature · 1 line
2.11.0
David Ahern
maintainerPATCH 5/6 nexthop: Add support for lwt encaps
In reply to David AhernFrom: David Ahern <[email protected]>
Add support for NHA_ENCAP and NHA_ENCAP_TYPE. Leverages the existing code for lwtunnel within fib_nh_common, so the only change needed is handling the attributes in the nexthop code.
--- include/net/nexthop.h | 3 +++ net/ipv4/nexthop.c | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-)
include/net/nexthop.h +3 -0
@@ -35,6 +35,9 @@ struct nh_config {struct in6_addr ipv6; } gw; +struct nlattr *nh_encap; +u16 nh_encap_type; +u32 nlflags;struct nl_info nlinfo; };
net/ipv4/nexthop.c +36 -1
@@ -9,6 +9,7 @@#include <linux/rtnetlink.h> #include <linux/slab.h> #include <net/ipv6_stubs.h> +#include <net/lwtunnel.h> #include <net/nexthop.h> #include <net/route.h> #include <net/sock.h>
@@ -182,6 +183,11 @@ static int nh_fill_node(struct sk_buff *skb, struct nexthop *nh,break ; } +if (nhi->fib_nhc.nhc_lwtstate && +lwtunnel_fill_encap (skb, nhi->fib_nhc.nhc_lwtstate, +NHA_ENCAP ,NHA_ENCAP_TYPE ) <0 ) + goto nla_put_failure; + out:nlmsg_end (skb, nlh);return 0 ;
@@ -213,6 +219,11 @@ static size_t nh_nlmsg_size(struct nexthop *nh)break ; } +if (nhi->fib_nhc.nhc_lwtstate) { + sz +=lwtunnel_get_encap_size (nhi->fib_nhc.nhc_lwtstate); + sz +=nla_total_size (2 );/* NHA_ENCAP_TYPE */ + } +return sz; }
@@ -370,6 +381,8 @@ static int nh_create_ipv4(struct net *net, struct nexthop *nh, .fc_gw4 = cfg->gw.ipv4, .fc_gw_family = cfg->gw.ipv4 ? AF_INET : 0, .fc_flags = cfg->nh_flags, + .fc_encap = cfg->nh_encap, + .fc_encap_type = cfg->nh_encap_type, };u32 tb_id =l3mdev_fib_table (cfg->dev);int err = -EINVAL ;
@@ -402,6 +415,8 @@ static int nh_create_ipv6(struct net *net, struct nexthop *nh, .fc_ifindex = cfg->nh_ifindex, .fc_gateway = cfg->gw.ipv6, .fc_flags = cfg->nh_flags, + .fc_encap = cfg->nh_encap, + .fc_encap_type = cfg->nh_encap_type, };int err = -EINVAL ;
@@ -561,7 +576,8 @@ static int rtm_to_nh_config(struct net *net, struct sk_buff *skb, cfg->nh_id =nla_get_u32 (tb[NHA_ID ]);if (tb[NHA_BLACKHOLE ]) { -if (tb[NHA_GATEWAY ] || tb[NHA_OIF ]) { +if (tb[NHA_GATEWAY ] || tb[NHA_OIF ] || + tb[NHA_ENCAP ] || tb[NHA_ENCAP_TYPE ]) {NL_SET_ERR_MSG (extack,"Blackhole attribute can not be used with gateway or oif" ); goto out; }
@@ -626,6 +642,25 @@ static int rtm_to_nh_config(struct net *net, struct sk_buff *skb, } } +if (tb[NHA_ENCAP ]) { + cfg->nh_encap = tb[NHA_ENCAP ]; + +if (!tb[NHA_ENCAP_TYPE ]) { +NL_SET_ERR_MSG (extack,"LWT encapsulation type is missing" ); + goto out; + } + + cfg->nh_encap_type =nla_get_u16 (tb[NHA_ENCAP_TYPE ]); + err =lwtunnel_valid_encap_type (cfg->nh_encap_type, extack); +if (err <0 ) + goto out; + + }else if (tb[NHA_ENCAP_TYPE ]) { +NL_SET_ERR_MSG (extack,"LWT encapsulation attribute is missing" ); + goto out; + } + + err =0 ; out:return err;
Signature · 1 line
2.11.0
David Ahern
maintainerPATCH 6/6 nexthop: Add support for nexthop groups
In reply to David AhernFrom: David Ahern <[email protected]>
Allow the creation of nexthop groups which reference other nexthop objects to create multipath routes:
+--------------+
+------------+ +--------------+ |
| nh nh_grp --->| nh_grp_entry |-+
+------------+ +---------|----+
^ | | +------------+
+----------------+ +--->| nh, weight |
nh_parent +------------+A group entry points to a nexthop with a weight for that hop within the group. The nexthop has a list_head, grp_list, for tracking which groups it is a member of and the group entry has a reference back to the parent. The grp_list is used when a nexthop is deleted - to efficiently remove it from groups using it.
If a nexthop group spec is given, no other attributes can be set. Each nexthop id in a group spec must already exist.
Similar to single nexthops, the specification of a nexthop group can be updated so that data is managed with rcu locking.
Add path selection function to account for multiple paths and add ipv{4,6}_good_nh helpers to know that if a neighbor entry exists it is in a good state.
Update NETDEV event handling to rebalance multipath nexthop groups if a nexthop is deleted due to a link event (down or unregister).
When a nexthop is removed any groups using it are updated. Groups using a nexthop a tracked via a grp_list.
Nexthop dumps can be limited to groups only by adding NHA_GROUPS to the request.
--- include/net/nexthop.h | 98 +++++++++- net/ipv4/nexthop.c | 504 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 578 insertions(+), 24 deletions(-)
include/net/nexthop.h +97 -1
@@ -35,6 +35,9 @@ struct nh_config {struct in6_addr ipv6; } gw; +struct nlattr *nh_grp; +u16 nh_grp_type; +struct nlattr *nh_encap;u16 nh_encap_type;
@@ -56,20 +59,39 @@ struct nh_info { }; }; +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 { +u16 num_nh; +bool mpath; +bool has_v4; +struct nh_grp_entry nh_entries[0 ]; +}; +struct nexthop {struct rb_node rb_node;/* entry on netns rbtree */ +struct list_head grp_list;/* nh group entries using this nh */ struct net *net;u32 id;u8 protocol;/* app managing this nh */ u8 nh_flags; +bool is_group;refcount_t refcnt;struct rcu_head rcu;union {struct nh_info __rcu *nh_info; +struct nh_group __rcu *nh_grp; }; };
@@ -88,12 +110,86 @@ static inline void nexthop_put(struct nexthop *nh)call_rcu (&nh->rcu, nexthop_free_rcu); } +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 ; + +if (nexthop_is_multipath (nh)) { +struct nh_group *nh_grp; + + nh_grp =rcu_dereference_rtnl (nh->nh_grp); + rc = nh_grp->num_nh; + }else { +const struct nh_info *nhi; + + nhi =rcu_dereference_rtnl (nh->nh_info); +if (nhi->reject_nh) + rc =0 ; + } + +return rc; +} + +static inline +struct nexthop *nexthop_mpath_select (const struct nexthop *nh,int nhsel) +{ +const struct nh_group *nhg =rcu_dereference_rtnl (nh->nh_grp); + +/* for_nexthops macros in fib_semantics.c grabs a pointer to +* the nexthop before checking nhsel +*/ +if (nhsel > nhg->num_nh) +return NULL ; + +return nhg->nh_entries[nhsel].nh; +} + +static inline +int nexthop_mpath_fill_node (struct sk_buff *skb,struct nexthop *nh) +{ +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; + +if (fib_add_nexthop (skb, nhc, weight) <0 ) +return -EMSGSIZE ; + } + +return 0 ; +} +/* 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); +if (nexthop_is_multipath (nh)) { +if (nexthop_num_path (nh) >1 ) +return false; + nh =nexthop_mpath_select (nh,0 ); +if (!nh) +return false; + } + + nhi =rcu_dereference_rtnl (nh->nh_info);return nhi->reject_nh; }#endif
net/ipv4/nexthop.c +481 -23
672 lines not on this page · raw message
Signature · 1 line
2.11.0
David Miller
maintainerFrom: David Ahern <[email protected]>
Date: Fri, 24 May 2019 14:43:02 -0700
Quoted 17 lines
From: David Ahern <[email protected]>
This set contains the API and initial implementation for nexthops as standalone objects.
Patch 1 contains the UAPI and updates to selinux struct.
Patch 2 contains the barebones code for nexthop commands, rbtree maintenance and notifications.
Patch 3 then adds support for IPv4 gateways along with handling of netdev events.
Patch 4 adds support for IPv6 gateways.
Patch 5 has the implementation of the encap attributes.
Quoted 7 lines
Patch 6 adds support for nexthop groups.
At the end of this set, nexthop objects can be created and deleted and userspace can monitor nexthop events, but ipv4 and ipv6 routes can not use them yet. Once the nexthop struct is defined, follow on sets add it to fib{6}_info and handle it within the respective code before routes can be inserted using them.
Series applied, I'll push this out after compile testing.
Thanks David.