net: mctp: Add support for gateway routing
Jeremy Kerr
maintainerv5 cover net: mctp: Add support for gateway routing
This series adds a gateway route type for the MCTP core, allowing non-local EIDs as the match for a route.
Example setup using the mctp tools:
mctp route add 9 via mctpi2c0
mctp neigh add 9 dev mctpi2c0 lladdr 0x1d
mctp route add 10 gw 9
- will route packets to eid 10 through mctpi2c0, using a dest lladdr of 0x1d (ie, that of the directly-attached eid 9).
The core change to support this is the introduction of a struct mctp_dst, which represents the result of a route lookup. Since this involves a bit of surgery through the routing code, we add a few tests along the way.
We're introducing an ABI change in the new RTM_{NEW,GET,DEL}ROUTE netlink formats, with the support for a RTA_GATEWAY attribute. Because we need a network ID specified to fully-qualify a gateway EID, the RTA_GATEWAY attribute carries the (net, eid) tuple in full:
struct mctp_fq_addr {
unsigned int net;
mctp_eid_t eid;
}Of course, any questions, comments etc are most welcome.
---
Changes in v5:
- reinstate IFF_LOOPBACK check for new routes. Reported by Paolo Abeni
- mctp_sendmsg: return immediately on errors before we have allocated
skb, preventing a mctp_dst_release() on uninit dst. Reported by Paolo
Abeni.
- move mctp_dev_hold() to nlparse_populate, so we're consistent in the
cleanup semantics. defer all stores to @rt to the success case.
- keep existing semantics of extaddr->halen checking
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- avoid vla detection from const array-size initialiser, reported by ktr
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- 02/14 (new): Reduce frame size for route_input_cloned_frag kunit test,
preventing -Wframe-size-larger-than warnings
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- 12/13: prevent uninitialsed gateway variable in nlparse_common
- 13/13: make test data static
- commit message spelling fixes
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Jeremy Kerr (14):
net: mctp: don't use source cb data when forwarding, ensure pkt_type is set
net: mctp: test: make cloned_frag buffers more appropriately-sized
net: mctp: separate routing database from routing operations
net: mctp: separate cb from direct-addressing routing
net: mctp: test: Add an addressed device constructor
net: mctp: test: Add extaddr routing output test
net: mctp: test: move functions into utils.[ch]
net: mctp: test: add sock test infrastructure
net: mctp: test: Add initial socket tests
net: mctp: pass net into route creation
net: mctp: remove routes by netid, not by device
net: mctp: allow NL parsing directly into a struct mctp_route
net: mctp: add gateway routing support
net: mctp: test: Add tests for gateway routes
include/net/mctp.h | 52 +++- include/uapi/linux/mctp.h | 8 + net/mctp/af_mctp.c | 66 ++--- net/mctp/route.c | 574 ++++++++++++++++++++++++++++-------------- net/mctp/test/route-test.c | 612 +++++++++++++++++++++++++++++---------------- net/mctp/test/sock-test.c | 229 +++++++++++++++++ net/mctp/test/utils.c | 196 ++++++++++++++- net/mctp/test/utils.h | 44 ++++ 8 files changed, 1324 insertions(+), 457 deletions(-)
---
base-commit: 0097c4195b1d0ca57d15979626c769c74747b5a0
change-id: 20250520-dev-forwarding-0711973470bf
Best regards,
Signature · 1 line
Jeremy Kerr <[email protected]>
Jeremy Kerr
maintainerPATCH v5 1/14 net: mctp: don't use source cb data when forwarding, ensure pkt_type is set
In the output path, only check the skb->cb data when we know it's from a local socket; input packets will have source address information there instead.
In order to detect when we're forwarding, set skb->pkt_type on input/output.
--- net/mctp/route.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
net/mctp/route.c +12 -1
@@ -392,6 +392,9 @@ static int mctp_route_input(struct mctp_route *route, struct sk_buff *skb)*/ skb_orphan (skb); +if (skb->pkt_type ==PACKET_OUTGOING ) + skb->pkt_type =PACKET_LOOPBACK ; +/* ensure we have enough data for a header and a type */ if (skb->len <sizeof (struct mctp_hdr ) +1 ) goto out;
@@ -578,7 +581,13 @@ static int mctp_route_output(struct mctp_route *route, struct sk_buff *skb)return -EMSGSIZE ; } -if (cb->ifindex) { +/* If we're forwarding, we don't want to use the input path's cb, +* as it holds the *source* hardware addressing information. +* +* We will have a PACKET_HOST skb from the dev, or PACKET_OUTGOING +* from a socket; only use cb in the latter case. +*/ +if (skb->pkt_type ==PACKET_OUTGOING && cb->ifindex) {/* direct route; use the hwaddr we stashed in sendmsg */ if (cb->halen != skb->dev->addr_len) {/* sanity check, sendmsg should have already caught this */
@@ -587,6 +596,7 @@ static int mctp_route_output(struct mctp_route *route, struct sk_buff *skb) } daddr = cb->haddr; }else { + skb->pkt_type =PACKET_OUTGOING ;/* If lookup fails let the device handle daddr==NULL */ if (mctp_neigh_lookup (route->dev, hdr->dest, daddr_buf) ==0 ) daddr = daddr_buf;
@@ -1032,6 +1042,7 @@ int mctp_local_output(struct sock *sk, struct mctp_route *rt, tag = req_tag &MCTP_TAG_MASK ; } + skb->pkt_type =PACKET_OUTGOING ; skb->protocol =htons (ETH_P_MCTP ); skb->priority =0 ;skb_reset_transport_header (skb);
Signature · 1 line
2.39.5
Jeremy Kerr
maintainerPATCH v5 2/14 net: mctp: test: make cloned_frag buffers more appropriately-sized
In reply to Jeremy KerrIn our input_cloned_frag test, we currently allocate our test buffers arbitrarily-sized at 100 bytes.
We only expect to receive a max of 15 bytes from the socket, so reduce to a more appropriate size. There are some upcoming changes to the routing code which hit a frame-size limit on s390, so reduce the usage before that lands.
---
v4:
- don't use const size_t as array size const, it isn't const enough, so triggers -Wvla-larger-than warnings. Reported by ktr.
v3:
- new commit, preventing -Wframe-larger-than issues for the upcoming change, reported by ktr.
--- net/mctp/test/route-test.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
net/mctp/test/route-test.c +3 -5
@@ -933,20 +933,18 @@ static void mctp_test_route_input_cloned_frag(struct kunit *test)RX_FRAG (FL_S ,0 ), RX_FRAG(FL_E, 1), }; +const size_t data_len =3 ;/* arbitrary */ +u8 compare[3 *ARRAY_SIZE (hdrs)]; +u8 flat[3 *ARRAY_SIZE (hdrs)];struct mctp_test_route *rt;struct mctp_test_dev *dev;struct sk_buff *skb[5 ];struct sk_buff *rx_skb;struct socket *sock; -size_t data_len; -u8 compare[100 ]; -u8 flat[100 ];size_t total;void *p;int rc; -/* Arbitrary length */ - data_len =3 ; total = data_len +sizeof (struct mctp_hdr );__mctp_route_test_init (test, &dev, &rt, &sock,MCTP_NET_ANY );
Signature · 1 line
2.39.5
Jeremy Kerr
maintainerPATCH v5 3/14 net: mctp: separate routing database from routing operations
In reply to Jeremy KerrThis change adds a struct mctp_dst, representing the result of a routing lookup. This decouples the struct mctp_route from the actual implementation of a routing operation.
This will allow for future routing changes which may require more involved lookup logic, such as gateway routing - which may require multiple traversals of the routing table.
Since we only use the struct mctp_route at lookup time, we no longer hold routes over a routing operation, as we only need it to populate the dst. However, we do hold the dev while the dst is active.
This requires some changes to the route test infrastructure, as we no longer have a mock route to handle the route output operation, and transient dsts are created by the routing code, so we can't override them as easily.
Instead, we use kunit->priv to stash a packet queue, and a custom dst_output function queues into that packet queue, which we can use for later expectations.
--
v5:
- mctp_sendmsg: return immediately on errors before we have allocated skb, preventing a mctp_dst_release() on uninit dst. Reported by Paolo Abeni.
- keep existing semantics of extaddr->halen checking
--- include/net/mctp.h | 35 +++++-- net/mctp/af_mctp.c | 62 ++++-------- net/mctp/route.c | 210 ++++++++++++++++++++------------------ net/mctp/test/route-test.c | 245 ++++++++++++++++++++++++++++----------------- 4 files changed, 313 insertions(+), 239 deletions(-)
include/net/mctp.h +29 -6
@@ -222,6 +222,8 @@ struct mctp_flow {struct mctp_sk_key *key; }; +struct mctp_dst ; +/* Route definition. * * These are held in the pernet->mctp.routes list, with RCU protection for
@@ -229,8 +231,7 @@ struct mctp_flow {* dropped on NETDEV_UNREGISTER events. * * Updates to the route table are performed under rtnl; all reads under RCU, -* so routes cannot be referenced over a RCU grace period. Specifically: A -* caller cannot block between mctp_route_lookup and mctp_route_release() +* so routes cannot be referenced over a RCU grace period. */ struct mctp_route {mctp_eid_t min, max;
@@ -238,7 +239,7 @@ struct mctp_route {unsigned char type;unsigned int mtu;struct mctp_dev *dev; -int (*output)(struct mctp_route *route, +int (*output)(struct mctp_dst *dst,struct sk_buff *skb);struct list_head list;
@@ -246,12 +247,34 @@ struct mctp_route {struct rcu_head rcu; }; +/* Route lookup result: dst. Represents the results of a routing decision, +* but is only held over the individual routing operation. +* +* Will typically be stored on the caller stack, and must be released after +* usage. +*/ +struct mctp_dst { +struct mctp_dev *dev; +unsigned int mtu; + +/* set for direct addressing */ +unsigned char halen; +unsigned char haddr[MAX_ADDR_LEN ]; + +int (*output)(struct mctp_dst *dst,struct sk_buff *skb); +}; + +int mctp_dst_from_extaddr (struct mctp_dst *dst,struct net *net,int ifindex, +unsigned char halen,const unsigned char *haddr); +/* route interfaces */ -struct mctp_route *mctp_route_lookup (struct net *net,unsigned int dnet, -mctp_eid_t daddr); +int mctp_route_lookup (struct net *net,unsigned int dnet, +mctp_eid_t daddr,struct mctp_dst *dst); + +void mctp_dst_release (struct mctp_dst *dst);/* always takes ownership of skb */ -int mctp_local_output (struct sock *sk,struct mctp_route *rt, +int mctp_local_output (struct sock *sk,struct mctp_dst *dst,struct sk_buff *skb,mctp_eid_t daddr,u8 req_tag);void mctp_key_unref (struct mctp_sk_key *key);
net/mctp/af_mctp.c +22 -40
94 lines not on this page · raw message
net/mctp/route.c +110 -100
380 lines not on this page · raw message
net/mctp/test/route-test.c +152 -93
689 lines not on this page · raw message
Signature · 1 line
2.39.5
Jeremy Kerr
maintainerPATCH v5 4/14 net: mctp: separate cb from direct-addressing routing
In reply to Jeremy KerrNow that we have the dst->haddr populated by sendmsg (when extended addressing is in use), we no longer need to stash the link-layer address in the skb->cb.
Instead, only use skb->cb for incoming lladdr data.
While we're at it: remove cb->src, as was never used.
--- include/net/mctp.h | 4 ++-- net/mctp/route.c | 21 +++++---------------- 2 files changed, 7 insertions(+), 18 deletions(-)
include/net/mctp.h +2 -2
@@ -183,8 +183,8 @@ struct mctp_sk_key {struct mctp_skb_cb {unsigned int magic;unsigned int net; -int ifindex;/* extended/direct addressing if set */ -mctp_eid_t src; +/* fields below provide extended addressing for ingress to recvmsg() */ +int ifindex;unsigned char halen;unsigned char haddr[MAX_ADDR_LEN ]; };
net/mctp/route.c +5 -16
@@ -561,35 +561,28 @@ static int mctp_dst_input(struct mctp_dst *dst, struct sk_buff *skb)static int mctp_dst_output (struct mctp_dst *dst,struct sk_buff *skb) { -struct mctp_skb_cb *cb =mctp_cb (skb);struct mctp_hdr *hdr =mctp_hdr (skb);char daddr_buf[MAX_ADDR_LEN ];char *daddr =NULL ;int rc; skb->protocol =htons (ETH_P_MCTP ); + skb->pkt_type =PACKET_OUTGOING ;if (skb->len > dst->mtu) {kfree_skb (skb);return -EMSGSIZE ; } -/* If we're forwarding, we don't want to use the input path's cb, -* as it holds the *source* hardware addressing information. -* -* We will have a PACKET_HOST skb from the dev, or PACKET_OUTGOING -* from a socket; only use cb in the latter case. -*/ -if (skb->pkt_type ==PACKET_OUTGOING && cb->ifindex) { -/* direct route; use the hwaddr we stashed in sendmsg */ -if (cb->halen != skb->dev->addr_len) { +/* direct route; use the hwaddr we stashed in sendmsg */ +if (dst->halen) { +if (dst->halen != skb->dev->addr_len) {/* sanity check, sendmsg should have already caught this */ kfree_skb (skb);return -EMSGSIZE ; } - daddr = cb->haddr; + daddr = dst->haddr; }else { - skb->pkt_type =PACKET_OUTGOING ;/* If lookup fails let the device handle daddr==NULL */ if (mctp_neigh_lookup (dst->dev, hdr->dest, daddr_buf) ==0 ) daddr = daddr_buf;
@@ -1009,7 +1002,6 @@ int mctp_local_output(struct sock *sk, struct mctp_dst *dst, struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag) { struct mctp_sock *msk = container_of(sk, struct mctp_sock, sk); -struct mctp_skb_cb *cb =mctp_cb (skb);struct mctp_sk_key *key;struct mctp_hdr *hdr;unsigned long flags;
@@ -1064,9 +1056,6 @@ int mctp_local_output(struct sock *sk, struct mctp_dst *dst,skb_reset_network_header (skb); skb->dev = dst->dev->dev; -/* cb->net will have been set on initial ingress */ - cb->src = saddr; -/* set up common header fields */ hdr =mctp_hdr (skb); hdr->ver =1 ;
Signature · 1 line
2.39.5
Jeremy Kerr
maintainerPATCH v5 5/14 net: mctp: test: Add an addressed device constructor
In reply to Jeremy KerrUpcoming tests will check semantics of hardware addressing, which require a dev with ->addr_len != 0. Add a constructor to create a MCTP interface using a physically-addressed bus type.
--- net/mctp/test/utils.c | 20 ++++++++++++++++++-- net/mctp/test/utils.h | 7 +++++++ 2 files changed, 25 insertions(+), 2 deletions(-)
net/mctp/test/utils.c +18 -2
@@ -26,19 +26,22 @@ static void mctp_test_dev_setup(struct net_device *ndev) ndev->type =ARPHRD_MCTP ; ndev->mtu =MCTP_DEV_TEST_MTU ; ndev->hard_header_len =0 ; - ndev->addr_len =0 ; ndev->tx_queue_len =DEFAULT_TX_QUEUE_LEN ; ndev->flags =IFF_NOARP ; ndev->netdev_ops = &mctp_test_netdev_ops; ndev->needs_free_netdev = true; } -struct mctp_test_dev *mctp_test_create_dev (void ) +static struct mctp_test_dev *__mctp_test_create_dev (unsigned short lladdr_len, +const unsigned char *lladdr) {struct mctp_test_dev *dev;struct net_device *ndev;int rc; +if (WARN_ON (lladdr_len >MAX_ADDR_LEN )) +return NULL ; + ndev =alloc_netdev (sizeof (*dev),"mctptest%d" ,NET_NAME_ENUM , mctp_test_dev_setup);if (!ndev)
@@ -46,6 +49,8 @@ struct mctp_test_dev *mctp_test_create_dev(void) dev =netdev_priv (ndev); dev->ndev = ndev; + ndev->addr_len = lladdr_len; +dev_addr_set (ndev, lladdr); rc =register_netdev (ndev);if (rc) {
@@ -61,6 +66,17 @@ struct mctp_test_dev *mctp_test_create_dev(void)return dev; } +struct mctp_test_dev *mctp_test_create_dev (void ) +{ +return __mctp_test_create_dev (0 ,NULL ); +} + +struct mctp_test_dev *mctp_test_create_dev_lladdr (unsigned short lladdr_len, +const unsigned char *lladdr) +{ +return __mctp_test_create_dev (lladdr_len, lladdr); +} +void mctp_test_destroy_dev (struct mctp_test_dev *dev) {mctp_dev_put (dev->mdev);
net/mctp/test/utils.h +7 -0
@@ -3,6 +3,8 @@#ifndef __NET_MCTP_TEST_UTILS_H#define __NET_MCTP_TEST_UTILS_H +#include <uapi/linux/netdevice.h> +#include <kunit/test.h> #define MCTP_DEV_TEST_MTU 68
@@ -10,11 +12,16 @@struct mctp_test_dev {struct net_device *ndev;struct mctp_dev *mdev; + +unsigned short lladdr_len; +unsigned char lladdr[MAX_ADDR_LEN ]; };struct mctp_test_dev ;struct mctp_test_dev *mctp_test_create_dev (void ); +struct mctp_test_dev *mctp_test_create_dev_lladdr (unsigned short lladdr_len, +const unsigned char *lladdr);void mctp_test_destroy_dev (struct mctp_test_dev *dev);#endif /* __NET_MCTP_TEST_UTILS_H */
Signature · 1 line
2.39.5
Jeremy Kerr
maintainerPATCH v5 6/14 net: mctp: test: Add extaddr routing output test
In reply to Jeremy KerrTest that the routing code preserves the haddr data in a skb through an input route operation.
--- net/mctp/test/route-test.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
net/mctp/test/route-test.c +53 -0
@@ -1294,6 +1294,58 @@ static void mctp_test_route_output_key_create(struct kunit *test)mctp_test_destroy_dev (dev); } +static void mctp_test_route_extaddr_input (struct kunit *test) +{ +static const unsigned char haddr[] = {0xaa ,0x55 }; +struct mctp_test_pktqueue tpq; +struct mctp_skb_cb *cb, *cb2; +const unsigned int len =40 ; +struct mctp_test_dev *dev; +struct sk_buff *skb, *skb2; +struct mctp_dst dst; +struct mctp_hdr hdr; +struct socket *sock; +int rc; + + hdr.ver =1 ; + hdr.src =10 ; + hdr.dest =8 ; + hdr.flags_seq_tag =FL_S |FL_E |FL_TO ; + +__mctp_route_test_init (test, &dev, &dst, &tpq, &sock,MCTP_NET_ANY ); + + skb =mctp_test_create_skb (&hdr, len); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, skb); + +/* set our hardware addressing data */ + cb =mctp_cb (skb); +memcpy (cb->haddr, haddr,sizeof (haddr)); + cb->halen =sizeof (haddr); + +mctp_test_skb_set_dev (skb, dev); + + rc =mctp_dst_input (&dst, skb); +KUNIT_ASSERT_EQ (test, rc,0 ); + +mctp_test_dst_release (&dst, &tpq); + + skb2 =skb_recv_datagram (sock->sk,MSG_DONTWAIT , &rc); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, skb2); +KUNIT_ASSERT_EQ (test, skb2->len, len); + + cb2 =mctp_cb (skb2); + +/* Received SKB should have the hardware addressing as set above. +* We're likely to have the same actual cb here (ie., cb == cb2), +* but it's the comparison that we care about +*/ +KUNIT_EXPECT_EQ (test, cb2->halen,sizeof (haddr)); +KUNIT_EXPECT_MEMEQ (test, cb2->haddr, haddr,sizeof (haddr)); + +skb_free_datagram (sock->sk, skb2); +mctp_test_destroy_dev (dev); +} +static struct kunit_case mctp_test_cases[] = {KUNIT_CASE_PARAM (mctp_test_fragment, mctp_frag_gen_params),KUNIT_CASE_PARAM (mctp_test_rx_input, mctp_rx_input_gen_params),
@@ -1310,6 +1362,7 @@ static struct kunit_case mctp_test_cases[] = {KUNIT_CASE (mctp_test_fragment_flow),KUNIT_CASE (mctp_test_route_output_key_create),KUNIT_CASE (mctp_test_route_input_cloned_frag), + KUNIT_CASE(mctp_test_route_extaddr_input), {} };
Signature · 1 line
2.39.5
Jeremy Kerr
maintainerPATCH v5 7/14 net: mctp: test: move functions into utils.[ch]
In reply to Jeremy KerrA future change will add another mctp test .c file, so move some of the common test setup from route.c into the utils object.
--- net/mctp/test/route-test.c | 163 --------------------------------------------- net/mctp/test/utils.c | 150 +++++++++++++++++++++++++++++++++++++++++ net/mctp/test/utils.h | 32 +++++++++ 3 files changed, 182 insertions(+), 163 deletions(-)
net/mctp/test/route-test.c +0 -163
@@ -8,169 +8,6 @@#include "utils.h" -struct mctp_test_route { -struct mctp_route rt; -}; - -static const unsigned int test_pktqueue_magic =0x5f713aef ; - -struct mctp_test_pktqueue { -unsigned int magic; -struct sk_buff_head pkts; -}; - -static void mctp_test_pktqueue_init (struct mctp_test_pktqueue *tpq) -{ - tpq->magic = test_pktqueue_magic; -skb_queue_head_init (&tpq->pkts); -} - -static int mctp_test_dst_output (struct mctp_dst *dst,struct sk_buff *skb) -{ -struct kunit *test = current->kunit_test; -struct mctp_test_pktqueue *tpq = test->priv; - -KUNIT_ASSERT_EQ (test, tpq->magic, test_pktqueue_magic); - -skb_queue_tail (&tpq->pkts, skb); - -return 0 ; -} - -/* local version of mctp_route_alloc() */ -static struct mctp_test_route *mctp_route_test_alloc (void ) -{ -struct mctp_test_route *rt; - - rt =kzalloc (sizeof (*rt),GFP_KERNEL ); -if (!rt) -return NULL ; - -INIT_LIST_HEAD (&rt->rt.list); -refcount_set (&rt->rt.refs,1 ); - rt->rt.output = mctp_test_dst_output; - -return rt; -} - -static struct mctp_test_route *mctp_test_create_route (struct net *net, -struct mctp_dev *dev, -mctp_eid_t eid, -unsigned int mtu) -{ -struct mctp_test_route *rt; - - rt =mctp_route_test_alloc (); -if (!rt) -return NULL ; - - rt->rt.min = eid; - rt->rt.max = eid; - rt->rt.mtu = mtu; - rt->rt.type =RTN_UNSPEC ; -if (dev) -mctp_dev_hold (dev); - rt->rt.dev = dev; - -list_add_rcu (&rt->rt.list, &net->mctp.routes); - -return rt; -} - -/* Convenience function for our test dst; release with mctp_test_dst_release() -*/ -static void mctp_test_dst_setup (struct kunit *test,struct mctp_dst *dst, -struct mctp_test_dev *dev, -struct mctp_test_pktqueue *tpq, -unsigned int mtu) -{ -KUNIT_EXPECT_NOT_ERR_OR_NULL (test, dev); - -memset (dst,0 ,sizeof (*dst)); - - dst->dev = dev->mdev; -__mctp_dev_get (dst->dev->dev); - dst->mtu = mtu; - dst->output = mctp_test_dst_output; -mctp_test_pktqueue_init (tpq); - test->priv = tpq; -} - -static void mctp_test_dst_release (struct mctp_dst *dst, -struct mctp_test_pktqueue *tpq) -{ -mctp_dst_release (dst); -skb_queue_purge (&tpq->pkts); -} - -static void mctp_test_route_destroy (struct kunit *test, -struct mctp_test_route *rt) -{ -unsigned int refs; - -rtnl_lock (); -list_del_rcu (&rt->rt.list); -rtnl_unlock (); - -if (rt->rt.dev) -mctp_dev_put (rt->rt.dev); - - refs =refcount_read (&rt->rt.refs); -KUNIT_ASSERT_EQ_MSG (test, refs,1 ,"route ref imbalance" ); - -kfree_rcu (&rt->rt, rcu); -} - -static void mctp_test_skb_set_dev (struct sk_buff *skb, -struct mctp_test_dev *dev) -{ -struct mctp_skb_cb *cb; - - cb =mctp_cb (skb); - cb->net =READ_ONCE (dev->mdev->net); - skb->dev = dev->ndev; -} - -static struct sk_buff *mctp_test_create_skb (const struct mctp_hdr *hdr, -unsigned int data_len) -{ -size_t hdr_len =sizeof (*hdr); -struct sk_buff *skb; -unsigned int i; -u8 *buf; - - skb =alloc_skb (hdr_len + data_len,GFP_KERNEL ); -if (!skb) -return NULL ; - -__mctp_cb (skb); -memcpy (skb_put (skb, hdr_len), hdr, hdr_len); - - buf =skb_put (skb, data_len); -for (i =0 ; i < data_len; i++) - buf[i] = i &0xff ; - -return skb; -} - -static struct sk_buff *__mctp_test_create_skb_data (const struct mctp_hdr *hdr, -const void *data, -size_t data_len) -{ -size_t hdr_len =sizeof (*hdr); -struct sk_buff *skb; - - skb =alloc_skb (hdr_len + data_len,GFP_KERNEL ); -if (!skb) -return NULL ; - -__mctp_cb (skb); -memcpy (skb_put (skb, hdr_len), hdr, hdr_len); -memcpy (skb_put (skb, data_len), data, data_len); - -return skb; -} -#define mctp_test_create_skb_data (h, d) \ __mctp_test_create_skb_data(h, d, sizeof(*d))
net/mctp/test/utils.c +150 -0
153 lines not on this page · raw message
net/mctp/test/utils.h +32 -0
@@ -5,6 +5,9 @@#include <uapi/linux/netdevice.h> +#include <net/mctp.h> +#include <net/mctpdevice.h> +#include <kunit/test.h> #define MCTP_DEV_TEST_MTU 68
@@ -19,9 +22,38 @@ struct mctp_test_dev {struct mctp_test_dev ; +struct mctp_test_route { +struct mctp_route rt; +}; + +struct mctp_test_pktqueue { +unsigned int magic; +struct sk_buff_head pkts; +}; +struct mctp_test_dev *mctp_test_create_dev (void );struct mctp_test_dev *mctp_test_create_dev_lladdr (unsigned short lladdr_len,const unsigned char *lladdr);void mctp_test_destroy_dev (struct mctp_test_dev *dev); +struct mctp_test_route *mctp_test_create_route (struct net *net, +struct mctp_dev *dev, +mctp_eid_t eid, +unsigned int mtu); +void mctp_test_dst_setup (struct kunit *test,struct mctp_dst *dst, +struct mctp_test_dev *dev, +struct mctp_test_pktqueue *tpq,unsigned int mtu); +void mctp_test_dst_release (struct mctp_dst *dst, +struct mctp_test_pktqueue *tpq); +void mctp_test_pktqueue_init (struct mctp_test_pktqueue *tpq); +void mctp_test_route_destroy (struct kunit *test,struct mctp_test_route *rt); +void mctp_test_skb_set_dev (struct sk_buff *skb,struct mctp_test_dev *dev); +struct sk_buff *mctp_test_create_skb (const struct mctp_hdr *hdr, +unsigned int data_len); +struct sk_buff *__mctp_test_create_skb_data (const struct mctp_hdr *hdr, +const void *data,size_t data_len); + +#define mctp_test_create_skb_data (h, d) \ + __mctp_test_create_skb_data(h, d, sizeof(*d)) +#endif /* __NET_MCTP_TEST_UTILS_H */
Signature · 1 line
2.39.5
Jeremy Kerr
maintainerPATCH v5 8/14 net: mctp: test: add sock test infrastructure
In reply to Jeremy KerrAdd a new test object, for use with the af_mctp socket code. This is intially empty, but we'll start populating actual tests in an upcoming change.
--- net/mctp/af_mctp.c | 4 ++++ net/mctp/test/route-test.c | 2 +- net/mctp/test/sock-test.c | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-)
net/mctp/af_mctp.c +4 -0
@@ -775,3 +775,7 @@ MODULE_DESCRIPTION("MCTP core");MODULE_AUTHOR ("Jeremy Kerr <[email protected]>" );MODULE_ALIAS_NETPROTO (PF_MCTP ); + +#if IS_ENABLED (CONFIG_MCTP_TEST ) +#include "test/sock-test.c" +#endif
net/mctp/test/route-test.c +1 -1
@@ -1204,7 +1204,7 @@ static struct kunit_case mctp_test_cases[] = { };static struct kunit_suite mctp_test_suite = { - .name ="mctp" , + .name ="mctp-route" , .test_cases = mctp_test_cases, };
net/mctp/test/sock-test.c new file +16 -0
@@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <kunit/test.h> + +#include "utils.h" + +static struct kunit_case mctp_test_cases[] = { + {} +}; + +static struct kunit_suite mctp_test_suite = { + .name ="mctp-sock" , + .test_cases = mctp_test_cases, +}; + +kunit_test_suite (mctp_test_suite);
Signature · 1 line
2.39.5
Jeremy Kerr
maintainerPATCH v5 9/14 net: mctp: test: Add initial socket tests
In reply to Jeremy KerrRecent changes have modified the extaddr path a little, so add a couple of kunit tests to af-mctp.c. These check that we're correctly passing lladdr data between sendmsg/recvmsg and the routing layer.
--- net/mctp/route.c | 5 ++ net/mctp/test/sock-test.c | 213 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 218 insertions(+)
net/mctp/route.c +5 -0
@@ -17,6 +17,8 @@#include <linux/rtnetlink.h> #include <linux/skbuff.h> +#include <kunit/static_stub.h> +#include <uapi/linux/if_arp.h> #include <net/mctp.h>
@@ -1011,6 +1013,9 @@ int mctp_local_output(struct sock *sk, struct mctp_dst *dst,int rc;u8 tag; +KUNIT_STATIC_STUB_REDIRECT (mctp_local_output, sk, dst, skb, daddr, + req_tag); + rc = -ENODEV ;spin_lock_irqsave (&dst->dev->addrs_lock, flags);
net/mctp/test/sock-test.c +213 -0
223 lines not on this page · raw message
Signature · 1 line
2.39.5
Jeremy Kerr
maintainerPATCH v5 10/14 net: mctp: pass net into route creation
In reply to Jeremy KerrWe may not have a mdev pointer, from which we currently extract the net.
Instead, pass the net directly.
--- net/mctp/route.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)
net/mctp/route.c +16 -15
@@ -1086,12 +1086,11 @@ int mctp_local_output(struct sock *sk, struct mctp_dst *dst, }/* route management */ -static int mctp_route_add (struct mctp_dev *mdev,mctp_eid_t daddr_start, -unsigned int daddr_extent,unsigned int mtu, -unsigned char type) +static int mctp_route_add (struct net *net,struct mctp_dev *mdev, +mctp_eid_t daddr_start,unsigned int daddr_extent, +unsigned int mtu,unsigned char type) {int (*rtfn)(struct mctp_dst *dst,struct sk_buff *skb); -struct net *net =dev_net (mdev->dev);struct mctp_route *rt, *ert;if (!mctp_address_unicast (daddr_start))
@@ -1138,10 +1137,10 @@ static int mctp_route_add(struct mctp_dev *mdev, mctp_eid_t daddr_start,return 0 ; } -static int mctp_route_remove (struct mctp_dev *mdev,mctp_eid_t daddr_start, -unsigned int daddr_extent,unsigned char type) +static int mctp_route_remove (struct net *net,struct mctp_dev *mdev, +mctp_eid_t daddr_start,unsigned int daddr_extent, +unsigned char type) { -struct net *net =dev_net (mdev->dev);struct mctp_route *rt, *tmp;mctp_eid_t daddr_end;bool dropped;
@@ -1170,12 +1169,12 @@ static int mctp_route_remove(struct mctp_dev *mdev, mctp_eid_t daddr_start,int mctp_route_add_local (struct mctp_dev *mdev,mctp_eid_t addr) { -return mctp_route_add (mdev, addr,0 ,0 ,RTN_LOCAL ); +return mctp_route_add (dev_net (mdev->dev), mdev, addr,0 ,0 ,RTN_LOCAL ); }int mctp_route_remove_local (struct mctp_dev *mdev,mctp_eid_t addr) { -return mctp_route_remove (mdev, addr,0 ,RTN_LOCAL ); +return mctp_route_remove (dev_net (mdev->dev), mdev, addr,0 ,RTN_LOCAL ); }/* removes all entries for a given device */
@@ -1284,12 +1283,11 @@ static const struct nla_policy rta_mctp_policy[RTA_MAX + 1] = {/* Common part for RTM_NEWROUTE and RTM_DELROUTE parsing. * tb must hold RTA_MAX+1 elements. */ -static int mctp_route_nlparse (struct sk_buff *skb,struct nlmsghdr *nlh, +static int mctp_route_nlparse (struct net *net,struct nlmsghdr *nlh,struct netlink_ext_ack *extack,struct nlattr **tb,struct rtmsg **rtm,struct mctp_dev **mdev,mctp_eid_t *daddr_start) { -struct net *net =sock_net (skb->sk);struct net_device *dev;unsigned int ifindex;int rc;
@@ -1343,6 +1341,7 @@ static const struct nla_policy rta_metrics_policy[RTAX_MAX + 1] = {static int mctp_newroute (struct sk_buff *skb,struct nlmsghdr *nlh,struct netlink_ext_ack *extack) { +struct net *net =sock_net (skb->sk);struct nlattr *tb[RTA_MAX +1 ];struct nlattr *tbx[RTAX_MAX +1 ];mctp_eid_t daddr_start;
@@ -1351,7 +1350,7 @@ static int mctp_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,unsigned int mtu;int rc; - rc =mctp_route_nlparse (skb, nlh, extack, tb, + rc =mctp_route_nlparse (net, nlh, extack, tb, &rtm, &mdev, &daddr_start);if (rc <0 )return rc;
@@ -1371,7 +1370,7 @@ static int mctp_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, mtu =nla_get_u32 (tbx[RTAX_MTU ]); } - rc =mctp_route_add (mdev, daddr_start, rtm->rtm_dst_len, mtu, + rc =mctp_route_add (net, mdev, daddr_start, rtm->rtm_dst_len, mtu, rtm->rtm_type);return rc; }
@@ -1379,13 +1378,14 @@ static int mctp_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,static int mctp_delroute (struct sk_buff *skb,struct nlmsghdr *nlh,struct netlink_ext_ack *extack) { +struct net *net =sock_net (skb->sk);struct nlattr *tb[RTA_MAX +1 ];mctp_eid_t daddr_start;struct mctp_dev *mdev;struct rtmsg *rtm;int rc; - rc =mctp_route_nlparse (skb, nlh, extack, tb, + rc =mctp_route_nlparse (net, nlh, extack, tb, &rtm, &mdev, &daddr_start);if (rc <0 )return rc;
@@ -1394,7 +1394,8 @@ static int mctp_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,if (rtm->rtm_type !=RTN_UNICAST )return -EINVAL ; - rc =mctp_route_remove (mdev, daddr_start, rtm->rtm_dst_len,RTN_UNICAST ); + rc =mctp_route_remove (net, mdev, daddr_start, rtm->rtm_dst_len, +RTN_UNICAST );return rc; }
Signature · 1 line
2.39.5
Jeremy Kerr
maintainerPATCH v5 11/14 net: mctp: remove routes by netid, not by device
In reply to Jeremy KerrIn upcoming changes, a route may not have a device associated. Since the route is matched on the (network, eid) tuple, pass the netid itself into mctp_route_remove.
--- net/mctp/route.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
net/mctp/route.c +10 -4
@@ -1085,6 +1085,11 @@ int mctp_local_output(struct sock *sk, struct mctp_dst *dst,return rc; } +static unsigned int mctp_route_netid (struct mctp_route *rt) +{ +return rt->dev->net; +} +/* route management */ static int mctp_route_add(struct net *net, struct mctp_dev *mdev, mctp_eid_t daddr_start, unsigned int daddr_extent,
@@ -1137,7 +1142,7 @@ static int mctp_route_add(struct net *net, struct mctp_dev *mdev,return 0 ; } -static int mctp_route_remove (struct net *net,struct mctp_dev *mdev, +static int mctp_route_remove (struct net *net,unsigned int netid,mctp_eid_t daddr_start,unsigned int daddr_extent,unsigned char type) {
@@ -1154,7 +1159,7 @@ static int mctp_route_remove(struct net *net, struct mctp_dev *mdev,ASSERT_RTNL ();list_for_each_entry_safe (rt, tmp, &net->mctp.routes, list) { -if (rt->dev == mdev && +if (mctp_route_netid (rt) == netid && rt->min == daddr_start && rt->max == daddr_end && rt->type == type) {list_del_rcu (&rt->list);
@@ -1174,7 +1179,8 @@ int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr)int mctp_route_remove_local (struct mctp_dev *mdev,mctp_eid_t addr) { -return mctp_route_remove (dev_net (mdev->dev), mdev, addr,0 ,RTN_LOCAL ); +return mctp_route_remove (dev_net (mdev->dev), mdev->net, + addr,0 ,RTN_LOCAL ); }/* removes all entries for a given device */
@@ -1394,7 +1400,7 @@ static int mctp_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,if (rtm->rtm_type !=RTN_UNICAST )return -EINVAL ; - rc =mctp_route_remove (net, mdev, daddr_start, rtm->rtm_dst_len, + rc =mctp_route_remove (net, mdev->net, daddr_start, rtm->rtm_dst_len,RTN_UNICAST );return rc; }
Signature · 1 line
2.39.5
Jeremy Kerr
maintainerPATCH v5 12/14 net: mctp: allow NL parsing directly into a struct mctp_route
In reply to Jeremy KerrThe netlink route parsing functions end up setting a bunch of output variables from the rt attributes. This will get messy when the routes become more complex.
So, split the rt parsing into two types: a lookup (returning route target data suitable for a route lookup, like when deleting a route) and a populate (setting fields of a struct mctp_route).
In doing this, we need to separate the route allocation from mctp_route_add, so add some comments on the lifetime semantics for the latter.
--
v5:
- reinstate IFF_LOOPBACK check for new routes. Reported by Paolo Abeni
- move mctp_dev_hold() to nlparse_populate, so we're consistent in the cleanup semantics. defer all stores to @rt to the success case.
--- net/mctp/route.c | 202 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 140 insertions(+), 62 deletions(-)
net/mctp/route.c +140 -62
@@ -1091,25 +1091,32 @@ static unsigned int mctp_route_netid(struct mctp_route *rt) }/* route management */ -static int mctp_route_add (struct net *net,struct mctp_dev *mdev, -mctp_eid_t daddr_start,unsigned int daddr_extent, -unsigned int mtu,unsigned char type) + +/* mctp_route_add(): Add the provided route, previously allocated via +* mctp_route_alloc(). On success, takes ownership of @rt, which includes a +* hold on rt->dev for usage in the route table. On failure a caller will want +* to mctp_route_release(). +* +* We expect that the caller has set rt->type, rt->min, rt->max, rt->dev and +* rt->mtu, and that the route holds a reference to rt->dev (via mctp_dev_hold). +* Other fields will be populated. +*/ +static int mctp_route_add (struct net *net,struct mctp_route *rt) { -int (*rtfn)(struct mctp_dst *dst,struct sk_buff *skb); -struct mctp_route *rt, *ert; +struct mctp_route *ert; -if (!mctp_address_unicast (daddr_start)) +if (!mctp_address_unicast (rt->min) || !mctp_address_unicast (rt->max))return -EINVAL ; -if (daddr_extent >0xff || daddr_start + daddr_extent >=255 ) +if (!rt->dev)return -EINVAL ; -switch (type) { +switch (rt->type) {case RTN_LOCAL : - rtfn = mctp_dst_input; + rt->output = mctp_dst_input;break ;case RTN_UNICAST : - rtfn = mctp_dst_output; + rt->output = mctp_dst_output;break ;default :return -EINVAL ;
@@ -1117,22 +1124,9 @@ static int mctp_route_add(struct net *net, struct mctp_dev *mdev,ASSERT_RTNL (); - rt =mctp_route_alloc (); -if (!rt) -return -ENOMEM ; - - rt->min = daddr_start; - rt->max = daddr_start + daddr_extent; - rt->mtu = mtu; - rt->dev = mdev; -mctp_dev_hold (rt->dev); - rt->type = type; - rt->output = rtfn; -/* Prevent duplicate identical routes. */ list_for_each_entry (ert, &net->mctp.routes, list) {if (mctp_rt_compare_exact (rt, ert)) { -mctp_route_release (rt);return -EEXIST ; } }
@@ -1174,7 +1168,25 @@ static int mctp_route_remove(struct net *net, unsigned int netid,int mctp_route_add_local (struct mctp_dev *mdev,mctp_eid_t addr) { -return mctp_route_add (dev_net (mdev->dev), mdev, addr,0 ,0 ,RTN_LOCAL ); +struct mctp_route *rt; +int rc; + + rt =mctp_route_alloc (); +if (!rt) +return -ENOMEM ; + + rt->min = addr; + rt->max = addr; + rt->dev = mdev; + rt->type =RTN_LOCAL ; + +mctp_dev_hold (rt->dev); + + rc =mctp_route_add (dev_net (mdev->dev), rt); +if (rc) +mctp_route_release (rt); + +return rc; }int mctp_route_remove_local (struct mctp_dev *mdev,mctp_eid_t addr)
@@ -1286,13 +1298,16 @@ static const struct nla_policy rta_mctp_policy[RTA_MAX + 1] = { [RTA_OIF] = { .type = NLA_U32 }, }; -/* Common part for RTM_NEWROUTE and RTM_DELROUTE parsing. -* tb must hold RTA_MAX+1 elements. -*/ -static int mctp_route_nlparse (struct net *net,struct nlmsghdr *nlh, -struct netlink_ext_ack *extack, -struct nlattr **tb,struct rtmsg **rtm, -struct mctp_dev **mdev,mctp_eid_t *daddr_start) +static const struct nla_policy rta_metrics_policy[RTAX_MAX +1 ] = { + [RTAX_MTU ] = { .type =NLA_U32 }, +}; + +/* base parsing; common to both _lookup and _populate variants */ +static int mctp_route_nlparse_common (struct net *net,struct nlmsghdr *nlh, +struct netlink_ext_ack *extack, +struct nlattr **tb,struct rtmsg **rtm, +struct mctp_dev **mdev, +mctp_eid_t *daddr_start) {struct net_device *dev;unsigned int ifindex;
@@ -1323,61 +1338,126 @@ static int mctp_route_nlparse(struct net *net, struct nlmsghdr *nlh,return -EINVAL ; } +if ((*rtm)->rtm_type !=RTN_UNICAST ) { +NL_SET_ERR_MSG (extack,"rtm_type must be RTN_UNICAST" ); +return -EINVAL ; + } + dev =__dev_get_by_index (net, ifindex);if (!dev) {NL_SET_ERR_MSG (extack,"bad ifindex" );return -ENODEV ; } + *mdev =mctp_dev_get_rtnl (dev);if (!*mdev)return -ENODEV ; -if (dev->flags &IFF_LOOPBACK ) { -NL_SET_ERR_MSG (extack,"no routes to loopback" ); -return -EINVAL ; - } -return 0 ; } -static const struct nla_policy rta_metrics_policy[RTAX_MAX +1 ] = { - [RTAX_MTU ] = { .type =NLA_U32 }, -}; - -static int mctp_newroute (struct sk_buff *skb,struct nlmsghdr *nlh, -struct netlink_ext_ack *extack) +/* Route parsing for lookup operations; we only need the "route target" +* components (ie., network and dest-EID range). +*/ +static int mctp_route_nlparse_lookup (struct net *net,struct nlmsghdr *nlh, +struct netlink_ext_ack *extack, +unsigned char *type,unsigned int *netid, +mctp_eid_t *daddr_start, +unsigned int *daddr_extent) { -struct net *net =sock_net (skb->sk);struct nlattr *tb[RTA_MAX +1 ]; +struct mctp_dev *mdev; +struct rtmsg *rtm; +int rc; + + rc =mctp_route_nlparse_common (net, nlh, extack, tb, &rtm, + &mdev, daddr_start); +if (rc) +return rc; + + *netid = mdev->net; + *type = rtm->rtm_type; + *daddr_extent = rtm->rtm_dst_len; + +return 0 ; +} + +/* Full route parse for RTM_NEWROUTE: populate @rt. On success, the route will +* hold a reference to the dev. +*/ +static int mctp_route_nlparse_populate (struct net *net,struct nlmsghdr *nlh, +struct netlink_ext_ack *extack, +struct mctp_route *rt) +{struct nlattr *tbx[RTAX_MAX +1 ]; +struct nlattr *tb[RTA_MAX +1 ]; +unsigned int daddr_extent;mctp_eid_t daddr_start; -struct mctp_dev *mdev; +struct mctp_dev *dev;struct rtmsg *rtm; -unsigned int mtu; +u32 mtu =0 ;int rc; - rc =mctp_route_nlparse (net, nlh, extack, tb, - &rtm, &mdev, &daddr_start); -if (rc <0 ) + rc =mctp_route_nlparse_common (net, nlh, extack, tb, &rtm, + &dev, &daddr_start); +if (rc)return rc; -if (rtm->rtm_type !=RTN_UNICAST ) { -NL_SET_ERR_MSG (extack,"rtm_type must be RTN_UNICAST" ); + daddr_extent = rtm->rtm_dst_len; + +if (daddr_extent >0xff || daddr_extent + daddr_start >=255 ) { +NL_SET_ERR_MSG (extack,"invalid eid range" );return -EINVAL ; } - mtu =0 ;if (tb[RTA_METRICS ]) { rc =nla_parse_nested (tbx,RTAX_MAX , tb[RTA_METRICS ], rta_metrics_policy,NULL ); -if (rc <0 ) +if (rc <0 ) { +NL_SET_ERR_MSG (extack,"incorrect RTA_METRICS format" );return rc; + }if (tbx[RTAX_MTU ]) mtu =nla_get_u32 (tbx[RTAX_MTU ]); } - rc =mctp_route_add (net, mdev, daddr_start, rtm->rtm_dst_len, mtu, - rtm->rtm_type); + rt->type = rtm->rtm_type; + rt->min = daddr_start; + rt->max = daddr_start + daddr_extent; + rt->mtu = mtu; + rt->dev = dev; +mctp_dev_hold (rt->dev); + +return 0 ; +} + +static int mctp_newroute (struct sk_buff *skb,struct nlmsghdr *nlh, +struct netlink_ext_ack *extack) +{ +struct net *net =sock_net (skb->sk); +struct mctp_route *rt; +int rc; + + rt =mctp_route_alloc (); +if (!rt) +return -ENOMEM ; + + rc =mctp_route_nlparse_populate (net, nlh, extack, rt); +if (rc <0 ) + goto err_free; + +if (rt->dev->dev->flags &IFF_LOOPBACK ) { +NL_SET_ERR_MSG (extack,"no routes to loopback" ); + rc = -EINVAL ; + goto err_free; + } + + rc =mctp_route_add (net, rt); +if (!rc) +return 0 ; + +err_free: +mctp_route_release (rt);return rc; }
@@ -1385,23 +1465,21 @@ static int mctp_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, struct netlink_ext_ack *extack) {struct net *net =sock_net (skb->sk); -struct nlattr *tb[RTA_MAX +1 ]; +unsigned int netid, daddr_extent; +unsigned char type =RTN_UNSPEC ;mctp_eid_t daddr_start; -struct mctp_dev *mdev; -struct rtmsg *rtm;int rc; - rc =mctp_route_nlparse (net, nlh, extack, tb, - &rtm, &mdev, &daddr_start); + rc =mctp_route_nlparse_lookup (net, nlh, extack, &type, &netid, + &daddr_start, &daddr_extent);if (rc <0 )return rc;/* we only have unicast routes */ -if (rtm->rtm_type !=RTN_UNICAST ) +if (type !=RTN_UNICAST )return -EINVAL ; - rc =mctp_route_remove (net, mdev->net, daddr_start, rtm->rtm_dst_len, -RTN_UNICAST ); + rc =mctp_route_remove (net, netid, daddr_start, daddr_extent, type);return rc; }
Signature · 1 line
2.39.5
Jeremy Kerr
maintainerPATCH v5 13/14 net: mctp: add gateway routing support
In reply to Jeremy KerrThis change allows for gateway routing, where a route table entry may reference a routable endpoint (by network and EID), instead of routing directly to a netdevice.
We add support for a RTM_GATEWAY attribute for netlink route updates, with an attribute format of:
struct mctp_fq_addr {
unsigned int net;
mctp_eid_t eid;
}- we need the net here to uniquely identify the target EID, as we no longer have the device reference directly (which would provide the net id in the case of direct routes).
This makes route lookups recursive, as a route lookup that returns a gateway route must be resolved into a direct route (ie, to a device) eventually. We provide a limit to the route lookups, to prevent infinite loop routing.
The route lookup populates a new 'nexthop' field in the dst structure, which now specifies the key for the neighbour table lookup on device output, rather than using the packet destination address directly.
--
v2:
- prevent uninitialsed gateway variable in nlparse_common, as reported by Simon Horman via smatch
--- include/net/mctp.h | 13 ++- include/uapi/linux/mctp.h | 8 ++ net/mctp/route.c | 206 +++++++++++++++++++++++++++++++++------------- net/mctp/test/utils.c | 3 +- 4 files changed, 173 insertions(+), 57 deletions(-)
include/net/mctp.h +12 -1
@@ -237,8 +237,18 @@ struct mctp_route {mctp_eid_t min, max;unsigned char type; +unsigned int mtu; -struct mctp_dev *dev; + +enum { +MCTP_ROUTE_DIRECT , +MCTP_ROUTE_GATEWAY , + } dst_type; +union { +struct mctp_dev *dev; +struct mctp_fq_addr gateway; + }; +int (*output)(struct mctp_dst *dst,struct sk_buff *skb);
@@ -256,6 +266,7 @@ struct mctp_route {struct mctp_dst {struct mctp_dev *dev;unsigned int mtu; +mctp_eid_t nexthop;/* set for direct addressing */ unsigned char halen;
include/uapi/linux/mctp.h +8 -0
@@ -37,6 +37,14 @@ struct sockaddr_mctp_ext {__u8 smctp_haddr[MAX_ADDR_LEN ]; }; +/* A "fully qualified" MCTP address, which includes the system-local network ID, +* required to uniquely resolve a routable EID. +*/ +struct mctp_fq_addr { +unsigned int net; +mctp_eid_t eid; +}; +#define MCTP_NET_ANY 0x0#define MCTP_ADDR_NULL 0x00
net/mctp/route.c +151 -55
398 lines not on this page · raw message
net/mctp/test/utils.c +2 -1
@@ -134,6 +134,7 @@ struct mctp_test_route *mctp_test_create_route(struct net *net, rt->rt.max = eid; rt->rt.mtu = mtu; rt->rt.type =RTN_UNSPEC ; + rt->rt.dst_type =MCTP_ROUTE_DIRECT ;if (dev)mctp_dev_hold (dev); rt->rt.dev = dev;
@@ -176,7 +177,7 @@ void mctp_test_route_destroy(struct kunit *test, struct mctp_test_route *rt)list_del_rcu (&rt->rt.list);rtnl_unlock (); -if (rt->rt.dev) +if (rt->rt.dst_type ==MCTP_ROUTE_DIRECT && rt->rt.dev)mctp_dev_put (rt->rt.dev); refs =refcount_read (&rt->rt.refs);
Signature · 1 line
2.39.5
Jeremy Kerr
maintainerPATCH v5 14/14 net: mctp: test: Add tests for gateway routes
In reply to Jeremy KerrAdd a few kunit tests for the gateway routing. Because we have multiple route types now (direct and gateway), rename mctp_test_create_route to mctp_test_create_route_direct, and add a _gateway variant too.
--
v2:
- static-ify mctp_route_gw_mtu_tests, reported by kernel test robot
--- net/mctp/test/route-test.c | 233 ++++++++++++++++++++++++++++++++++++++++++++- net/mctp/test/sock-test.c | 2 +- net/mctp/test/utils.c | 33 ++++++- net/mctp/test/utils.h | 13 ++- 4 files changed, 271 insertions(+), 10 deletions(-)
net/mctp/test/route-test.c +232 -1
@@ -141,7 +141,7 @@ static void mctp_test_rx_input(struct kunit *test) dev =mctp_test_create_dev ();KUNIT_ASSERT_NOT_ERR_OR_NULL (test, dev); - rt =mctp_test_create_route (&init_net, dev->mdev,8 ,68 ); + rt =mctp_test_create_route_direct (&init_net, dev->mdev,8 ,68 );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, rt); skb =mctp_test_create_skb (¶ms->hdr,1 );
@@ -1183,6 +1183,233 @@ static void mctp_test_route_extaddr_input(struct kunit *test)mctp_test_destroy_dev (dev); } +static void mctp_test_route_gw_lookup (struct kunit *test) +{ +struct mctp_test_route *rt1, *rt2; +struct mctp_dst dst = {0 }; +struct mctp_test_dev *dev; +int rc; + + dev =mctp_test_create_dev (); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, dev); + +/* 8 (local) -> 10 (gateway) via 9 (direct) */ + rt1 =mctp_test_create_route_direct (&init_net, dev->mdev,9 ,0 ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, rt1); + rt2 =mctp_test_create_route_gw (&init_net, dev->mdev->net,10 ,9 ,0 ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, rt2); + + rc =mctp_route_lookup (&init_net, dev->mdev->net,10 , &dst); +KUNIT_EXPECT_EQ (test, rc,0 ); +KUNIT_EXPECT_PTR_EQ (test, dst.dev, dev->mdev); +KUNIT_EXPECT_EQ (test, dst.mtu, dev->ndev->mtu); +KUNIT_EXPECT_EQ (test, dst.nexthop,9 ); +KUNIT_EXPECT_EQ (test, dst.halen,0 ); + +mctp_dst_release (&dst); + +mctp_test_route_destroy (test, rt2); +mctp_test_route_destroy (test, rt1); +mctp_test_destroy_dev (dev); +} + +static void mctp_test_route_gw_loop (struct kunit *test) +{ +struct mctp_test_route *rt1, *rt2; +struct mctp_dst dst = {0 }; +struct mctp_test_dev *dev; +int rc; + + dev =mctp_test_create_dev (); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, dev); + +/* two routes using each other as the gw */ + rt1 =mctp_test_create_route_gw (&init_net, dev->mdev->net,9 ,10 ,0 ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, rt1); + rt2 =mctp_test_create_route_gw (&init_net, dev->mdev->net,10 ,9 ,0 ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, rt2); + +/* this should fail, rather than infinite-loop */ + rc =mctp_route_lookup (&init_net, dev->mdev->net,10 , &dst); +KUNIT_EXPECT_NE (test, rc,0 ); + +mctp_test_route_destroy (test, rt2); +mctp_test_route_destroy (test, rt1); +mctp_test_destroy_dev (dev); +} + +struct mctp_route_gw_mtu_test { +/* working away from the local stack */ +unsigned int dev, neigh, gw, dst; +unsigned int exp; +}; + +static void mctp_route_gw_mtu_to_desc (const struct mctp_route_gw_mtu_test *t, +char *desc) +{ +sprintf (desc,"dev %d, neigh %d, gw %d, dst %d -> %d" , + t->dev, t->neigh, t->gw, t->dst, t->exp); +} + +static const struct mctp_route_gw_mtu_test mctp_route_gw_mtu_tests[] = { +/* no route-specific MTUs */ + {68 ,0 ,0 ,0 ,68 }, + {100 ,0 ,0 ,0 ,100 }, +/* one route MTU (smaller than dev mtu), others unrestricted */ + {100 ,68 ,0 ,0 ,68 }, + {100 ,0 ,68 ,0 ,68 }, + {100 ,0 ,0 ,68 ,68 }, +/* smallest applied, regardless of order */ + {100 ,99 ,98 ,68 ,68 }, + {99 ,100 ,98 ,68 ,68 }, + {98 ,99 ,100 ,68 ,68 }, + {68 ,98 ,99 ,100 ,68 }, +}; + +KUNIT_ARRAY_PARAM (mctp_route_gw_mtu, mctp_route_gw_mtu_tests, + mctp_route_gw_mtu_to_desc); + +static void mctp_test_route_gw_mtu (struct kunit *test) +{ +const struct mctp_route_gw_mtu_test *mtus = test->param_value; +struct mctp_test_route *rt1, *rt2, *rt3; +struct mctp_dst dst = {0 }; +struct mctp_test_dev *dev; +struct mctp_dev *mdev; +unsigned int netid; +int rc; + + dev =mctp_test_create_dev (); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, dev); + dev->ndev->mtu = mtus->dev; + mdev = dev->mdev; + netid = mdev->net; + +/* 8 (local) -> 11 (dst) via 10 (gw) via 9 (neigh) */ + rt1 =mctp_test_create_route_direct (&init_net, mdev,9 , mtus->neigh); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, rt1); + + rt2 =mctp_test_create_route_gw (&init_net, netid,10 ,9 , mtus->gw); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, rt2); + + rt3 =mctp_test_create_route_gw (&init_net, netid,11 ,10 , mtus->dst); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, rt3); + + rc =mctp_route_lookup (&init_net, dev->mdev->net,11 , &dst); +KUNIT_EXPECT_EQ (test, rc,0 ); +KUNIT_EXPECT_EQ (test, dst.mtu, mtus->exp); + +mctp_dst_release (&dst); + +mctp_test_route_destroy (test, rt3); +mctp_test_route_destroy (test, rt2); +mctp_test_route_destroy (test, rt1); +mctp_test_destroy_dev (dev); +} + +#define MCTP_TEST_LLADDR_LEN 2 +struct mctp_test_llhdr { +unsigned int magic; +unsigned char src[MCTP_TEST_LLADDR_LEN ]; +unsigned char dst[MCTP_TEST_LLADDR_LEN ]; +}; + +static const unsigned int mctp_test_llhdr_magic =0x5c78339c ; + +static int test_dev_header_create (struct sk_buff *skb,struct net_device *dev, +unsigned short type,const void *daddr, +const void *saddr,unsigned int len) +{ +struct kunit *test = current->kunit_test; +struct mctp_test_llhdr *hdr; + + hdr =skb_push (skb,sizeof (*hdr)); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, hdr); +skb_reset_mac_header (skb); + + hdr->magic = mctp_test_llhdr_magic; +memcpy (&hdr->src, saddr,sizeof (hdr->src)); +memcpy (&hdr->dst, daddr,sizeof (hdr->dst)); + +return 0 ; +} + +/* Test the dst_output path for a gateway-routed skb: we should have it +* lookup the nexthop EID in the neighbour table, and call into +* header_ops->create to resolve that to a lladdr. Our mock header_ops->create +* will just set a synthetic link-layer header, which we check after transmit. +*/ +static void mctp_test_route_gw_output (struct kunit *test) +{ +const unsigned char haddr_self[MCTP_TEST_LLADDR_LEN ] = {0xaa ,0x03 }; +const unsigned char haddr_peer[MCTP_TEST_LLADDR_LEN ] = {0xaa ,0x02 }; +const struct header_ops ops = { + .create = test_dev_header_create, + }; +struct mctp_neigh neigh = {0 }; +struct mctp_test_llhdr *ll_hdr; +struct mctp_dst dst = {0 }; +struct mctp_hdr hdr = {0 }; +struct mctp_test_dev *dev; +struct sk_buff *skb; +unsigned char *buf; +int i, rc; + + dev =mctp_test_create_dev_lladdr (sizeof (haddr_self), haddr_self); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, dev); + dev->ndev->header_ops = &ops; + + dst.dev = dev->mdev; +__mctp_dev_get (dst.dev->dev); + dst.mtu =68 ; + dst.nexthop =9 ; + +/* simple mctp_neigh_add for the gateway (not dest!) endpoint */ +INIT_LIST_HEAD (&neigh.list); + neigh.dev = dev->mdev; +mctp_dev_hold (dev->mdev); + neigh.eid =9 ; + neigh.source =MCTP_NEIGH_STATIC ; +memcpy (neigh.ha, haddr_peer,sizeof (haddr_peer)); +list_add_rcu (&neigh.list, &init_net.mctp.neighbours); + + hdr.ver =1 ; + hdr.src =8 ; + hdr.dest =10 ; + hdr.flags_seq_tag =FL_S |FL_E |FL_TO ; + +/* construct enough for a future link-layer header, the provided +* mctp header, and 4 bytes of data +*/ + skb =alloc_skb (sizeof (*ll_hdr) +sizeof (hdr) +4 ,GFP_KERNEL ); + skb->dev = dev->ndev; +__mctp_cb (skb); + +skb_reserve (skb,sizeof (*ll_hdr)); + +memcpy (skb_put (skb,sizeof (hdr)), &hdr,sizeof (hdr)); + buf =skb_put (skb,4 ); +for (i =0 ; i <4 ; i++) + buf[i] = i; + +/* extra ref over the dev_xmit */ +skb_get (skb); + + rc =mctp_dst_output (&dst, skb); +KUNIT_EXPECT_EQ (test, rc,0 ); + +mctp_dst_release (&dst); +list_del_rcu (&neigh.list); +mctp_dev_put (dev->mdev); + +/* check that we have our header created with the correct neighbour */ + ll_hdr = (void *)skb_mac_header (skb); +KUNIT_EXPECT_EQ (test, ll_hdr->magic, mctp_test_llhdr_magic); +KUNIT_EXPECT_MEMEQ (test, ll_hdr->src, haddr_self,sizeof (haddr_self)); +KUNIT_EXPECT_MEMEQ (test, ll_hdr->dst, haddr_peer,sizeof (haddr_peer)); +kfree_skb (skb); +} +static struct kunit_case mctp_test_cases[] = {KUNIT_CASE_PARAM (mctp_test_fragment, mctp_frag_gen_params),KUNIT_CASE_PARAM (mctp_test_rx_input, mctp_rx_input_gen_params),
@@ -1200,6 +1427,10 @@ static struct kunit_case mctp_test_cases[] = {KUNIT_CASE (mctp_test_route_output_key_create),KUNIT_CASE (mctp_test_route_input_cloned_frag),KUNIT_CASE (mctp_test_route_extaddr_input), +KUNIT_CASE (mctp_test_route_gw_lookup), +KUNIT_CASE (mctp_test_route_gw_loop), +KUNIT_CASE_PARAM (mctp_test_route_gw_mtu, mctp_route_gw_mtu_gen_params), + KUNIT_CASE(mctp_test_route_gw_output), {} };
net/mctp/test/sock-test.c +1 -1
@@ -40,7 +40,7 @@ static void __mctp_sock_test_init(struct kunit *test,kfree (addrs); - rt =mctp_test_create_route (dev_net (dev->ndev), dev->mdev,9 ,0 ); + rt =mctp_test_create_route_direct (dev_net (dev->ndev), dev->mdev,9 ,0 );KUNIT_ASSERT_NOT_ERR_OR_NULL (test, rt); rc =sock_create_kern (&init_net,AF_MCTP ,SOCK_DGRAM ,0 , &sock);
net/mctp/test/utils.c +29 -4
@@ -119,10 +119,10 @@ static struct mctp_test_route *mctp_route_test_alloc(void)return rt; } -struct mctp_test_route *mctp_test_create_route (struct net *net, -struct mctp_dev *dev, -mctp_eid_t eid, -unsigned int mtu) +struct mctp_test_route *mctp_test_create_route_direct (struct net *net, +struct mctp_dev *dev, +mctp_eid_t eid, +unsigned int mtu) {struct mctp_test_route *rt;
@@ -144,6 +144,31 @@ struct mctp_test_route *mctp_test_create_route(struct net *net,return rt; } +struct mctp_test_route *mctp_test_create_route_gw (struct net *net, +unsigned int netid, +mctp_eid_t eid, +mctp_eid_t gw, +unsigned int mtu) +{ +struct mctp_test_route *rt; + + rt =mctp_route_test_alloc (); +if (!rt) +return NULL ; + + rt->rt.min = eid; + rt->rt.max = eid; + rt->rt.mtu = mtu; + rt->rt.type =RTN_UNSPEC ; + rt->rt.dst_type =MCTP_ROUTE_GATEWAY ; + rt->rt.gateway.eid = gw; + rt->rt.gateway.net = netid; + +list_add_rcu (&rt->rt.list, &net->mctp.routes); + +return rt; +} +/* Convenience function for our test dst; release with mctp_test_dst_release() */ void mctp_test_dst_setup(struct kunit *test, struct mctp_dst *dst,
net/mctp/test/utils.h +9 -4
@@ -36,10 +36,15 @@ struct mctp_test_dev *mctp_test_create_dev_lladdr(unsigned short lladdr_len, const unsigned char *lladdr);void mctp_test_destroy_dev (struct mctp_test_dev *dev); -struct mctp_test_route *mctp_test_create_route (struct net *net, -struct mctp_dev *dev, -mctp_eid_t eid, -unsigned int mtu); +struct mctp_test_route *mctp_test_create_route_direct (struct net *net, +struct mctp_dev *dev, +mctp_eid_t eid, +unsigned int mtu); +struct mctp_test_route *mctp_test_create_route_gw (struct net *net, +unsigned int netid, +mctp_eid_t eid, +mctp_eid_t gw, +unsigned int mtu);void mctp_test_dst_setup (struct kunit *test,struct mctp_dst *dst,struct mctp_test_dev *dev,struct mctp_test_pktqueue *tpq,unsigned int mtu);
Signature · 1 line
2.39.5
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Quoted 10 lines · On Wed, 02 Jul 2025 14:20:00 +0800 you wrote:
This series adds a gateway route type for the MCTP core, allowing non-local EIDs as the match for a route.
Example setup using the mctp tools:
mctp route add 9 via mctpi2c0
mctp neigh add 9 dev mctpi2c0 lladdr0x1d
mctp route add 10 gw 9[...]
Here is the summary with links:
- [net-next,v5,01/14] net: mctp: don't use source cb data when forwarding, ensure pkt_type is set
https://git.kernel.org/netdev/net-next/c/e0f3c79cc0bb - [net-next,v5,02/14] net: mctp: test: make cloned_frag buffers more appropriately-sized https://git.kernel.org/netdev/net-next/c/fc2b87d036e2 - [net-next,v5,03/14] net: mctp: separate routing database from routing operations https://git.kernel.org/netdev/net-next/c/269936db5eb3 - [net-next,v5,04/14] net: mctp: separate cb from direct-addressing routing https://git.kernel.org/netdev/net-next/c/3007f90ec038 - [net-next,v5,05/14] net: mctp: test: Add an addressed device constructor https://git.kernel.org/netdev/net-next/c/96b341a8e782 - [net-next,v5,06/14] net: mctp: test: Add extaddr routing output test https://git.kernel.org/netdev/net-next/c/46ee16462fed - [net-next,v5,07/14] net: mctp: test: move functions into utils.[ch] https://git.kernel.org/netdev/net-next/c/80bcf05e54e0 - [net-next,v5,08/14] net: mctp: test: add sock test infrastructure https://git.kernel.org/netdev/net-next/c/19396179a0f1 - [net-next,v5,09/14] net: mctp: test: Add initial socket tests https://git.kernel.org/netdev/net-next/c/9b4a8c38f4fe - [net-next,v5,10/14] net: mctp: pass net into route creation https://git.kernel.org/netdev/net-next/c/48e6aa60bf28 - [net-next,v5,11/14] net: mctp: remove routes by netid, not by device https://git.kernel.org/netdev/net-next/c/4a1de053d7f0 - [net-next,v5,12/14] net: mctp: allow NL parsing directly into a struct mctp_route https://git.kernel.org/netdev/net-next/c/28ddbb2abe13 - [net-next,v5,13/14] net: mctp: add gateway routing support https://git.kernel.org/netdev/net-next/c/ad39c12fcee3 - [net-next,v5,14/14] net: mctp: test: Add tests for gateway routes https://git.kernel.org/netdev/net-next/c/48e1736e5dc1
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html