Skip to content

Commit e6d8e7d

Browse files
mkjPaolo Abeni
authored andcommitted
net: mctp: Add bind lookup test
Test the preference order of bound socket matches with a series of test packets. Signed-off-by: Matt Johnston <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
1 parent b7e2812 commit e6d8e7d

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed

net/mctp/test/route-test.c

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,193 @@ static void mctp_test_route_gw_output(struct kunit *test)
14081408
kfree_skb(skb);
14091409
}
14101410

1411+
struct mctp_bind_lookup_test {
1412+
/* header of incoming message */
1413+
struct mctp_hdr hdr;
1414+
u8 ty;
1415+
/* mctp network of incoming interface (smctp_network) */
1416+
unsigned int net;
1417+
1418+
/* expected socket, matches .name in lookup_binds, NULL for dropped */
1419+
const char *expect;
1420+
};
1421+
1422+
/* Single-packet TO-set message */
1423+
#define LK(src, dst) RX_HDR(1, (src), (dst), FL_S | FL_E | FL_TO)
1424+
1425+
/* Input message test cases for bind lookup tests.
1426+
*
1427+
* 10 and 11 are local EIDs.
1428+
* 20 and 21 are remote EIDs.
1429+
*/
1430+
static const struct mctp_bind_lookup_test mctp_bind_lookup_tests[] = {
1431+
/* both local-eid and remote-eid binds, remote eid is preferenced */
1432+
{ .hdr = LK(20, 10), .ty = 1, .net = 1, .expect = "remote20" },
1433+
1434+
{ .hdr = LK(20, 255), .ty = 1, .net = 1, .expect = "remote20" },
1435+
{ .hdr = LK(20, 0), .ty = 1, .net = 1, .expect = "remote20" },
1436+
{ .hdr = LK(0, 255), .ty = 1, .net = 1, .expect = "any" },
1437+
{ .hdr = LK(0, 11), .ty = 1, .net = 1, .expect = "any" },
1438+
{ .hdr = LK(0, 0), .ty = 1, .net = 1, .expect = "any" },
1439+
{ .hdr = LK(0, 10), .ty = 1, .net = 1, .expect = "local10" },
1440+
{ .hdr = LK(21, 10), .ty = 1, .net = 1, .expect = "local10" },
1441+
{ .hdr = LK(21, 11), .ty = 1, .net = 1, .expect = "remote21local11" },
1442+
1443+
/* both src and dest set to eid=99. unusual, but accepted
1444+
* by MCTP stack currently.
1445+
*/
1446+
{ .hdr = LK(99, 99), .ty = 1, .net = 1, .expect = "any" },
1447+
1448+
/* unbound smctp_type */
1449+
{ .hdr = LK(20, 10), .ty = 3, .net = 1, .expect = NULL },
1450+
1451+
/* smctp_network tests */
1452+
1453+
{ .hdr = LK(0, 0), .ty = 1, .net = 7, .expect = "any" },
1454+
{ .hdr = LK(21, 10), .ty = 1, .net = 2, .expect = "any" },
1455+
1456+
/* remote EID 20 matches, but MCTP_NET_ANY in "remote20" resolved
1457+
* to net=1, so lookup doesn't match "remote20"
1458+
*/
1459+
{ .hdr = LK(20, 10), .ty = 1, .net = 3, .expect = "any" },
1460+
1461+
{ .hdr = LK(21, 10), .ty = 1, .net = 3, .expect = "remote21net3" },
1462+
{ .hdr = LK(21, 10), .ty = 1, .net = 4, .expect = "remote21net4" },
1463+
{ .hdr = LK(21, 10), .ty = 1, .net = 5, .expect = "remote21net5" },
1464+
1465+
{ .hdr = LK(21, 10), .ty = 1, .net = 5, .expect = "remote21net5" },
1466+
1467+
{ .hdr = LK(99, 10), .ty = 1, .net = 8, .expect = "local10net8" },
1468+
1469+
{ .hdr = LK(99, 10), .ty = 1, .net = 9, .expect = "anynet9" },
1470+
{ .hdr = LK(0, 0), .ty = 1, .net = 9, .expect = "anynet9" },
1471+
{ .hdr = LK(99, 99), .ty = 1, .net = 9, .expect = "anynet9" },
1472+
{ .hdr = LK(20, 10), .ty = 1, .net = 9, .expect = "anynet9" },
1473+
};
1474+
1475+
/* Binds to create during the lookup tests */
1476+
static const struct mctp_test_bind_setup lookup_binds[] = {
1477+
/* any address and net, type 1 */
1478+
{ .name = "any", .bind_addr = MCTP_ADDR_ANY,
1479+
.bind_net = MCTP_NET_ANY, .bind_type = 1, },
1480+
/* local eid 10, net 1 (resolved from MCTP_NET_ANY) */
1481+
{ .name = "local10", .bind_addr = 10,
1482+
.bind_net = MCTP_NET_ANY, .bind_type = 1, },
1483+
/* local eid 10, net 8 */
1484+
{ .name = "local10net8", .bind_addr = 10,
1485+
.bind_net = 8, .bind_type = 1, },
1486+
/* any EID, net 9 */
1487+
{ .name = "anynet9", .bind_addr = MCTP_ADDR_ANY,
1488+
.bind_net = 9, .bind_type = 1, },
1489+
1490+
/* remote eid 20, net 1, any local eid */
1491+
{ .name = "remote20", .bind_addr = MCTP_ADDR_ANY,
1492+
.bind_net = MCTP_NET_ANY, .bind_type = 1,
1493+
.have_peer = true, .peer_addr = 20, .peer_net = MCTP_NET_ANY, },
1494+
1495+
/* remote eid 20, net 1, local eid 11 */
1496+
{ .name = "remote21local11", .bind_addr = 11,
1497+
.bind_net = MCTP_NET_ANY, .bind_type = 1,
1498+
.have_peer = true, .peer_addr = 21, .peer_net = MCTP_NET_ANY, },
1499+
1500+
/* remote eid 21, specific net=3 for connect() */
1501+
{ .name = "remote21net3", .bind_addr = MCTP_ADDR_ANY,
1502+
.bind_net = MCTP_NET_ANY, .bind_type = 1,
1503+
.have_peer = true, .peer_addr = 21, .peer_net = 3, },
1504+
1505+
/* remote eid 21, net 4 for bind, specific net=4 for connect() */
1506+
{ .name = "remote21net4", .bind_addr = MCTP_ADDR_ANY,
1507+
.bind_net = 4, .bind_type = 1,
1508+
.have_peer = true, .peer_addr = 21, .peer_net = 4, },
1509+
1510+
/* remote eid 21, net 5 for bind, specific net=5 for connect() */
1511+
{ .name = "remote21net5", .bind_addr = MCTP_ADDR_ANY,
1512+
.bind_net = 5, .bind_type = 1,
1513+
.have_peer = true, .peer_addr = 21, .peer_net = 5, },
1514+
};
1515+
1516+
static void mctp_bind_lookup_desc(const struct mctp_bind_lookup_test *t,
1517+
char *desc)
1518+
{
1519+
snprintf(desc, KUNIT_PARAM_DESC_SIZE,
1520+
"{src %d dst %d ty %d net %d expect %s}",
1521+
t->hdr.src, t->hdr.dest, t->ty, t->net, t->expect);
1522+
}
1523+
1524+
KUNIT_ARRAY_PARAM(mctp_bind_lookup, mctp_bind_lookup_tests,
1525+
mctp_bind_lookup_desc);
1526+
1527+
static void mctp_test_bind_lookup(struct kunit *test)
1528+
{
1529+
const struct mctp_bind_lookup_test *rx;
1530+
struct socket *socks[ARRAY_SIZE(lookup_binds)];
1531+
struct sk_buff *skb_pkt = NULL, *skb_sock = NULL;
1532+
struct socket *sock_ty0, *sock_expect = NULL;
1533+
struct mctp_test_pktqueue tpq;
1534+
struct mctp_test_dev *dev;
1535+
struct mctp_dst dst;
1536+
int rc;
1537+
1538+
rx = test->param_value;
1539+
1540+
__mctp_route_test_init(test, &dev, &dst, &tpq, &sock_ty0, rx->net);
1541+
/* Create all binds */
1542+
for (size_t i = 0; i < ARRAY_SIZE(lookup_binds); i++) {
1543+
mctp_test_bind_run(test, &lookup_binds[i],
1544+
&rc, &socks[i]);
1545+
KUNIT_ASSERT_EQ(test, rc, 0);
1546+
1547+
/* Record the expected receive socket */
1548+
if (rx->expect &&
1549+
strcmp(rx->expect, lookup_binds[i].name) == 0) {
1550+
KUNIT_ASSERT_NULL(test, sock_expect);
1551+
sock_expect = socks[i];
1552+
}
1553+
}
1554+
KUNIT_ASSERT_EQ(test, !!sock_expect, !!rx->expect);
1555+
1556+
/* Create test message */
1557+
skb_pkt = mctp_test_create_skb_data(&rx->hdr, &rx->ty);
1558+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, skb_pkt);
1559+
mctp_test_skb_set_dev(skb_pkt, dev);
1560+
mctp_test_pktqueue_init(&tpq);
1561+
1562+
rc = mctp_dst_input(&dst, skb_pkt);
1563+
if (rx->expect) {
1564+
/* Test the message is received on the expected socket */
1565+
KUNIT_EXPECT_EQ(test, rc, 0);
1566+
skb_sock = skb_recv_datagram(sock_expect->sk,
1567+
MSG_DONTWAIT, &rc);
1568+
if (!skb_sock) {
1569+
/* Find which socket received it instead */
1570+
for (size_t i = 0; i < ARRAY_SIZE(lookup_binds); i++) {
1571+
skb_sock = skb_recv_datagram(socks[i]->sk,
1572+
MSG_DONTWAIT, &rc);
1573+
if (skb_sock) {
1574+
KUNIT_FAIL(test,
1575+
"received on incorrect socket '%s', expect '%s'",
1576+
lookup_binds[i].name,
1577+
rx->expect);
1578+
goto cleanup;
1579+
}
1580+
}
1581+
KUNIT_FAIL(test, "no message received");
1582+
}
1583+
} else {
1584+
KUNIT_EXPECT_NE(test, rc, 0);
1585+
}
1586+
1587+
cleanup:
1588+
kfree_skb(skb_sock);
1589+
kfree_skb(skb_pkt);
1590+
1591+
/* Drop all binds */
1592+
for (size_t i = 0; i < ARRAY_SIZE(lookup_binds); i++)
1593+
sock_release(socks[i]);
1594+
1595+
__mctp_route_test_fini(test, dev, &dst, &tpq, sock_ty0);
1596+
}
1597+
14111598
static struct kunit_case mctp_test_cases[] = {
14121599
KUNIT_CASE_PARAM(mctp_test_fragment, mctp_frag_gen_params),
14131600
KUNIT_CASE_PARAM(mctp_test_rx_input, mctp_rx_input_gen_params),
@@ -1429,6 +1616,7 @@ static struct kunit_case mctp_test_cases[] = {
14291616
KUNIT_CASE(mctp_test_route_gw_loop),
14301617
KUNIT_CASE_PARAM(mctp_test_route_gw_mtu, mctp_route_gw_mtu_gen_params),
14311618
KUNIT_CASE(mctp_test_route_gw_output),
1619+
KUNIT_CASE_PARAM(mctp_test_bind_lookup, mctp_bind_lookup_gen_params),
14321620
{}
14331621
};
14341622

net/mctp/test/utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ struct mctp_test_bind_setup {
3939
bool have_peer;
4040
mctp_eid_t peer_addr;
4141
int peer_net;
42+
43+
/* optional name. Used for comparison in "lookup" tests */
44+
const char *name;
4245
};
4346

4447
struct mctp_test_dev *mctp_test_create_dev(void);

0 commit comments

Comments
 (0)