1 // SPDX-License-Identifier: GPL-2.0
3 #include <kunit/test.h>
7 struct mctp_test_route
{
9 struct sk_buff_head pkts
;
12 static int mctp_test_route_output(struct mctp_route
*rt
, struct sk_buff
*skb
)
14 struct mctp_test_route
*test_rt
= container_of(rt
, struct mctp_test_route
, rt
);
16 skb_queue_tail(&test_rt
->pkts
, skb
);
21 /* local version of mctp_route_alloc() */
22 static struct mctp_test_route
*mctp_route_test_alloc(void)
24 struct mctp_test_route
*rt
;
26 rt
= kzalloc(sizeof(*rt
), GFP_KERNEL
);
30 INIT_LIST_HEAD(&rt
->rt
.list
);
31 refcount_set(&rt
->rt
.refs
, 1);
32 rt
->rt
.output
= mctp_test_route_output
;
34 skb_queue_head_init(&rt
->pkts
);
39 static struct mctp_test_route
*mctp_test_create_route(struct net
*net
,
44 struct mctp_test_route
*rt
;
46 rt
= mctp_route_test_alloc();
53 rt
->rt
.type
= RTN_UNSPEC
;
58 list_add_rcu(&rt
->rt
.list
, &net
->mctp
.routes
);
63 static void mctp_test_route_destroy(struct kunit
*test
,
64 struct mctp_test_route
*rt
)
69 list_del_rcu(&rt
->rt
.list
);
72 skb_queue_purge(&rt
->pkts
);
74 mctp_dev_put(rt
->rt
.dev
);
76 refs
= refcount_read(&rt
->rt
.refs
);
77 KUNIT_ASSERT_EQ_MSG(test
, refs
, 1, "route ref imbalance");
79 kfree_rcu(&rt
->rt
, rcu
);
82 static void mctp_test_skb_set_dev(struct sk_buff
*skb
,
83 struct mctp_test_dev
*dev
)
85 struct mctp_skb_cb
*cb
;
88 cb
->net
= READ_ONCE(dev
->mdev
->net
);
92 static struct sk_buff
*mctp_test_create_skb(const struct mctp_hdr
*hdr
,
93 unsigned int data_len
)
95 size_t hdr_len
= sizeof(*hdr
);
100 skb
= alloc_skb(hdr_len
+ data_len
, GFP_KERNEL
);
105 memcpy(skb_put(skb
, hdr_len
), hdr
, hdr_len
);
107 buf
= skb_put(skb
, data_len
);
108 for (i
= 0; i
< data_len
; i
++)
114 static struct sk_buff
*__mctp_test_create_skb_data(const struct mctp_hdr
*hdr
,
118 size_t hdr_len
= sizeof(*hdr
);
121 skb
= alloc_skb(hdr_len
+ data_len
, GFP_KERNEL
);
126 memcpy(skb_put(skb
, hdr_len
), hdr
, hdr_len
);
127 memcpy(skb_put(skb
, data_len
), data
, data_len
);
132 #define mctp_test_create_skb_data(h, d) \
133 __mctp_test_create_skb_data(h, d, sizeof(*d))
135 struct mctp_frag_test
{
137 unsigned int msgsize
;
138 unsigned int n_frags
;
141 static void mctp_test_fragment(struct kunit
*test
)
143 const struct mctp_frag_test
*params
;
144 int rc
, i
, n
, mtu
, msgsize
;
145 struct mctp_test_route
*rt
;
150 params
= test
->param_value
;
152 msgsize
= params
->msgsize
;
157 hdr
.flags_seq_tag
= MCTP_HDR_FLAG_TO
;
159 skb
= mctp_test_create_skb(&hdr
, msgsize
);
160 KUNIT_ASSERT_TRUE(test
, skb
);
162 rt
= mctp_test_create_route(&init_net
, NULL
, 10, mtu
);
163 KUNIT_ASSERT_TRUE(test
, rt
);
165 rc
= mctp_do_fragment_route(&rt
->rt
, skb
, mtu
, MCTP_TAG_OWNER
);
166 KUNIT_EXPECT_FALSE(test
, rc
);
170 KUNIT_EXPECT_EQ(test
, n
, params
->n_frags
);
173 struct mctp_hdr
*hdr2
;
174 struct sk_buff
*skb2
;
181 skb2
= skb_dequeue(&rt
->pkts
);
186 hdr2
= mctp_hdr(skb2
);
188 tag_mask
= MCTP_HDR_TAG_MASK
| MCTP_HDR_FLAG_TO
;
190 KUNIT_EXPECT_EQ(test
, hdr2
->ver
, hdr
.ver
);
191 KUNIT_EXPECT_EQ(test
, hdr2
->src
, hdr
.src
);
192 KUNIT_EXPECT_EQ(test
, hdr2
->dest
, hdr
.dest
);
193 KUNIT_EXPECT_EQ(test
, hdr2
->flags_seq_tag
& tag_mask
,
194 hdr
.flags_seq_tag
& tag_mask
);
196 KUNIT_EXPECT_EQ(test
,
197 !!(hdr2
->flags_seq_tag
& MCTP_HDR_FLAG_SOM
), first
);
198 KUNIT_EXPECT_EQ(test
,
199 !!(hdr2
->flags_seq_tag
& MCTP_HDR_FLAG_EOM
), last
);
201 seq2
= (hdr2
->flags_seq_tag
>> MCTP_HDR_SEQ_SHIFT
) &
208 KUNIT_EXPECT_EQ(test
, seq2
, seq
& MCTP_HDR_SEQ_MASK
);
212 KUNIT_EXPECT_EQ(test
, skb2
->len
, mtu
);
214 KUNIT_EXPECT_LE(test
, skb2
->len
, mtu
);
219 mctp_test_route_destroy(test
, rt
);
222 static const struct mctp_frag_test mctp_frag_tests
[] = {
223 {.mtu
= 68, .msgsize
= 63, .n_frags
= 1},
224 {.mtu
= 68, .msgsize
= 64, .n_frags
= 1},
225 {.mtu
= 68, .msgsize
= 65, .n_frags
= 2},
226 {.mtu
= 68, .msgsize
= 66, .n_frags
= 2},
227 {.mtu
= 68, .msgsize
= 127, .n_frags
= 2},
228 {.mtu
= 68, .msgsize
= 128, .n_frags
= 2},
229 {.mtu
= 68, .msgsize
= 129, .n_frags
= 3},
230 {.mtu
= 68, .msgsize
= 130, .n_frags
= 3},
233 static void mctp_frag_test_to_desc(const struct mctp_frag_test
*t
, char *desc
)
235 sprintf(desc
, "mtu %d len %d -> %d frags",
236 t
->msgsize
, t
->mtu
, t
->n_frags
);
239 KUNIT_ARRAY_PARAM(mctp_frag
, mctp_frag_tests
, mctp_frag_test_to_desc
);
241 struct mctp_rx_input_test
{
246 static void mctp_test_rx_input(struct kunit
*test
)
248 const struct mctp_rx_input_test
*params
;
249 struct mctp_test_route
*rt
;
250 struct mctp_test_dev
*dev
;
253 params
= test
->param_value
;
255 dev
= mctp_test_create_dev();
256 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, dev
);
258 rt
= mctp_test_create_route(&init_net
, dev
->mdev
, 8, 68);
259 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, rt
);
261 skb
= mctp_test_create_skb(¶ms
->hdr
, 1);
262 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, skb
);
264 mctp_pkttype_receive(skb
, dev
->ndev
, &mctp_packet_type
, NULL
);
266 KUNIT_EXPECT_EQ(test
, !!rt
->pkts
.qlen
, params
->input
);
268 mctp_test_route_destroy(test
, rt
);
269 mctp_test_destroy_dev(dev
);
272 #define RX_HDR(_ver, _src, _dest, _fst) \
273 { .ver = _ver, .src = _src, .dest = _dest, .flags_seq_tag = _fst }
275 /* we have a route for EID 8 only */
276 static const struct mctp_rx_input_test mctp_rx_input_tests
[] = {
277 { .hdr
= RX_HDR(1, 10, 8, 0), .input
= true },
278 { .hdr
= RX_HDR(1, 10, 9, 0), .input
= false }, /* no input route */
279 { .hdr
= RX_HDR(2, 10, 8, 0), .input
= false }, /* invalid version */
282 static void mctp_rx_input_test_to_desc(const struct mctp_rx_input_test
*t
,
285 sprintf(desc
, "{%x,%x,%x,%x}", t
->hdr
.ver
, t
->hdr
.src
, t
->hdr
.dest
,
286 t
->hdr
.flags_seq_tag
);
289 KUNIT_ARRAY_PARAM(mctp_rx_input
, mctp_rx_input_tests
,
290 mctp_rx_input_test_to_desc
);
292 /* set up a local dev, route on EID 8, and a socket listening on type 0 */
293 static void __mctp_route_test_init(struct kunit
*test
,
294 struct mctp_test_dev
**devp
,
295 struct mctp_test_route
**rtp
,
296 struct socket
**sockp
,
299 struct sockaddr_mctp addr
= {0};
300 struct mctp_test_route
*rt
;
301 struct mctp_test_dev
*dev
;
305 dev
= mctp_test_create_dev();
306 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, dev
);
307 if (netid
!= MCTP_NET_ANY
)
308 WRITE_ONCE(dev
->mdev
->net
, netid
);
310 rt
= mctp_test_create_route(&init_net
, dev
->mdev
, 8, 68);
311 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, rt
);
313 rc
= sock_create_kern(&init_net
, AF_MCTP
, SOCK_DGRAM
, 0, &sock
);
314 KUNIT_ASSERT_EQ(test
, rc
, 0);
316 addr
.smctp_family
= AF_MCTP
;
317 addr
.smctp_network
= netid
;
318 addr
.smctp_addr
.s_addr
= 8;
320 rc
= kernel_bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
321 KUNIT_ASSERT_EQ(test
, rc
, 0);
328 static void __mctp_route_test_fini(struct kunit
*test
,
329 struct mctp_test_dev
*dev
,
330 struct mctp_test_route
*rt
,
334 mctp_test_route_destroy(test
, rt
);
335 mctp_test_destroy_dev(dev
);
338 struct mctp_route_input_sk_test
{
344 static void mctp_test_route_input_sk(struct kunit
*test
)
346 const struct mctp_route_input_sk_test
*params
;
347 struct sk_buff
*skb
, *skb2
;
348 struct mctp_test_route
*rt
;
349 struct mctp_test_dev
*dev
;
353 params
= test
->param_value
;
355 __mctp_route_test_init(test
, &dev
, &rt
, &sock
, MCTP_NET_ANY
);
357 skb
= mctp_test_create_skb_data(¶ms
->hdr
, ¶ms
->type
);
358 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, skb
);
360 mctp_test_skb_set_dev(skb
, dev
);
362 rc
= mctp_route_input(&rt
->rt
, skb
);
364 if (params
->deliver
) {
365 KUNIT_EXPECT_EQ(test
, rc
, 0);
367 skb2
= skb_recv_datagram(sock
->sk
, MSG_DONTWAIT
, &rc
);
368 KUNIT_EXPECT_NOT_ERR_OR_NULL(test
, skb2
);
369 KUNIT_EXPECT_EQ(test
, skb2
->len
, 1);
371 skb_free_datagram(sock
->sk
, skb2
);
374 KUNIT_EXPECT_NE(test
, rc
, 0);
375 skb2
= skb_recv_datagram(sock
->sk
, MSG_DONTWAIT
, &rc
);
376 KUNIT_EXPECT_NULL(test
, skb2
);
379 __mctp_route_test_fini(test
, dev
, rt
, sock
);
382 #define FL_S (MCTP_HDR_FLAG_SOM)
383 #define FL_E (MCTP_HDR_FLAG_EOM)
384 #define FL_TO (MCTP_HDR_FLAG_TO)
385 #define FL_T(t) ((t) & MCTP_HDR_TAG_MASK)
387 static const struct mctp_route_input_sk_test mctp_route_input_sk_tests
[] = {
388 { .hdr
= RX_HDR(1, 10, 8, FL_S
| FL_E
| FL_TO
), .type
= 0, .deliver
= true },
389 { .hdr
= RX_HDR(1, 10, 8, FL_S
| FL_E
| FL_TO
), .type
= 1, .deliver
= false },
390 { .hdr
= RX_HDR(1, 10, 8, FL_S
| FL_E
), .type
= 0, .deliver
= false },
391 { .hdr
= RX_HDR(1, 10, 8, FL_E
| FL_TO
), .type
= 0, .deliver
= false },
392 { .hdr
= RX_HDR(1, 10, 8, FL_TO
), .type
= 0, .deliver
= false },
393 { .hdr
= RX_HDR(1, 10, 8, 0), .type
= 0, .deliver
= false },
396 static void mctp_route_input_sk_to_desc(const struct mctp_route_input_sk_test
*t
,
399 sprintf(desc
, "{%x,%x,%x,%x} type %d", t
->hdr
.ver
, t
->hdr
.src
,
400 t
->hdr
.dest
, t
->hdr
.flags_seq_tag
, t
->type
);
403 KUNIT_ARRAY_PARAM(mctp_route_input_sk
, mctp_route_input_sk_tests
,
404 mctp_route_input_sk_to_desc
);
406 struct mctp_route_input_sk_reasm_test
{
408 struct mctp_hdr hdrs
[4];
413 static void mctp_test_route_input_sk_reasm(struct kunit
*test
)
415 const struct mctp_route_input_sk_reasm_test
*params
;
416 struct sk_buff
*skb
, *skb2
;
417 struct mctp_test_route
*rt
;
418 struct mctp_test_dev
*dev
;
423 params
= test
->param_value
;
425 __mctp_route_test_init(test
, &dev
, &rt
, &sock
, MCTP_NET_ANY
);
427 for (i
= 0; i
< params
->n_hdrs
; i
++) {
429 skb
= mctp_test_create_skb_data(¶ms
->hdrs
[i
], &c
);
430 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, skb
);
432 mctp_test_skb_set_dev(skb
, dev
);
434 rc
= mctp_route_input(&rt
->rt
, skb
);
437 skb2
= skb_recv_datagram(sock
->sk
, MSG_DONTWAIT
, &rc
);
439 if (params
->rx_len
) {
440 KUNIT_EXPECT_NOT_ERR_OR_NULL(test
, skb2
);
441 KUNIT_EXPECT_EQ(test
, skb2
->len
, params
->rx_len
);
442 skb_free_datagram(sock
->sk
, skb2
);
445 KUNIT_EXPECT_NULL(test
, skb2
);
448 __mctp_route_test_fini(test
, dev
, rt
, sock
);
451 #define RX_FRAG(f, s) RX_HDR(1, 10, 8, FL_TO | (f) | ((s) << MCTP_HDR_SEQ_SHIFT))
453 static const struct mctp_route_input_sk_reasm_test mctp_route_input_sk_reasm_tests
[] = {
455 .name
= "single packet",
457 RX_FRAG(FL_S
| FL_E
, 0),
463 .name
= "single packet, offset seq",
465 RX_FRAG(FL_S
| FL_E
, 1),
471 .name
= "start & end packets",
480 .name
= "start & end packets, offset seq",
489 .name
= "start & end packets, out of order",
498 .name
= "start, middle & end packets",
508 .name
= "missing seq",
527 static void mctp_route_input_sk_reasm_to_desc(
528 const struct mctp_route_input_sk_reasm_test
*t
,
531 sprintf(desc
, "%s", t
->name
);
534 KUNIT_ARRAY_PARAM(mctp_route_input_sk_reasm
, mctp_route_input_sk_reasm_tests
,
535 mctp_route_input_sk_reasm_to_desc
);
537 struct mctp_route_input_sk_keys_test
{
539 mctp_eid_t key_peer_addr
;
540 mctp_eid_t key_local_addr
;
546 /* test packet rx in the presence of various key configurations */
547 static void mctp_test_route_input_sk_keys(struct kunit
*test
)
549 const struct mctp_route_input_sk_keys_test
*params
;
550 struct mctp_test_route
*rt
;
551 struct sk_buff
*skb
, *skb2
;
552 struct mctp_test_dev
*dev
;
553 struct mctp_sk_key
*key
;
554 struct netns_mctp
*mns
;
555 struct mctp_sock
*msk
;
562 params
= test
->param_value
;
564 dev
= mctp_test_create_dev();
565 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, dev
);
566 net
= READ_ONCE(dev
->mdev
->net
);
568 rt
= mctp_test_create_route(&init_net
, dev
->mdev
, 8, 68);
569 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, rt
);
571 rc
= sock_create_kern(&init_net
, AF_MCTP
, SOCK_DGRAM
, 0, &sock
);
572 KUNIT_ASSERT_EQ(test
, rc
, 0);
574 msk
= container_of(sock
->sk
, struct mctp_sock
, sk
);
575 mns
= &sock_net(sock
->sk
)->mctp
;
577 /* set the incoming tag according to test params */
578 key
= mctp_key_alloc(msk
, net
, params
->key_local_addr
,
579 params
->key_peer_addr
, params
->key_tag
,
582 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, key
);
584 spin_lock_irqsave(&mns
->keys_lock
, flags
);
585 mctp_reserve_tag(&init_net
, key
, msk
);
586 spin_unlock_irqrestore(&mns
->keys_lock
, flags
);
588 /* create packet and route */
590 skb
= mctp_test_create_skb_data(¶ms
->hdr
, &c
);
591 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, skb
);
593 mctp_test_skb_set_dev(skb
, dev
);
595 rc
= mctp_route_input(&rt
->rt
, skb
);
597 /* (potentially) receive message */
598 skb2
= skb_recv_datagram(sock
->sk
, MSG_DONTWAIT
, &rc
);
601 KUNIT_EXPECT_NOT_ERR_OR_NULL(test
, skb2
);
603 KUNIT_EXPECT_PTR_EQ(test
, skb2
, NULL
);
606 skb_free_datagram(sock
->sk
, skb2
);
609 __mctp_route_test_fini(test
, dev
, rt
, sock
);
612 static const struct mctp_route_input_sk_keys_test mctp_route_input_sk_keys_tests
[] = {
614 .name
= "direct match",
618 .hdr
= RX_HDR(1, 9, 8, FL_S
| FL_E
| FL_T(1)),
622 .name
= "flipped src/dest",
626 .hdr
= RX_HDR(1, 9, 8, FL_S
| FL_E
| FL_T(1)),
630 .name
= "peer addr mismatch",
634 .hdr
= RX_HDR(1, 10, 8, FL_S
| FL_E
| FL_T(1)),
638 .name
= "tag value mismatch",
642 .hdr
= RX_HDR(1, 9, 8, FL_S
| FL_E
| FL_T(2)),
646 .name
= "TO mismatch",
650 .hdr
= RX_HDR(1, 9, 8, FL_S
| FL_E
| FL_T(1) | FL_TO
),
654 .name
= "broadcast response",
655 .key_peer_addr
= MCTP_ADDR_ANY
,
658 .hdr
= RX_HDR(1, 11, 8, FL_S
| FL_E
| FL_T(1)),
662 .name
= "any local match",
664 .key_local_addr
= MCTP_ADDR_ANY
,
666 .hdr
= RX_HDR(1, 12, 8, FL_S
| FL_E
| FL_T(1)),
671 static void mctp_route_input_sk_keys_to_desc(
672 const struct mctp_route_input_sk_keys_test
*t
,
675 sprintf(desc
, "%s", t
->name
);
678 KUNIT_ARRAY_PARAM(mctp_route_input_sk_keys
, mctp_route_input_sk_keys_tests
,
679 mctp_route_input_sk_keys_to_desc
);
683 struct mctp_test_dev
*dev
;
684 struct mctp_test_route
*rt
;
687 struct mctp_sk_key
*key
;
695 mctp_test_route_input_multiple_nets_bind_init(struct kunit
*test
,
698 struct mctp_hdr hdr
= RX_HDR(1, 9, 8, FL_S
| FL_E
| FL_T(1) | FL_TO
);
700 t
->msg
.data
= t
->netid
;
702 __mctp_route_test_init(test
, &t
->dev
, &t
->rt
, &t
->sock
, t
->netid
);
704 t
->skb
= mctp_test_create_skb_data(&hdr
, &t
->msg
);
705 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, t
->skb
);
706 mctp_test_skb_set_dev(t
->skb
, t
->dev
);
710 mctp_test_route_input_multiple_nets_bind_fini(struct kunit
*test
,
713 __mctp_route_test_fini(test
, t
->dev
, t
->rt
, t
->sock
);
716 /* Test that skbs from different nets (otherwise identical) get routed to their
717 * corresponding socket via the sockets' bind()
719 static void mctp_test_route_input_multiple_nets_bind(struct kunit
*test
)
721 struct sk_buff
*rx_skb1
, *rx_skb2
;
722 struct test_net t1
, t2
;
731 mctp_test_route_input_multiple_nets_bind_init(test
, &t1
);
732 mctp_test_route_input_multiple_nets_bind_init(test
, &t2
);
734 rc
= mctp_route_input(&t1
.rt
->rt
, t1
.skb
);
735 KUNIT_ASSERT_EQ(test
, rc
, 0);
736 rc
= mctp_route_input(&t2
.rt
->rt
, t2
.skb
);
737 KUNIT_ASSERT_EQ(test
, rc
, 0);
739 rx_skb1
= skb_recv_datagram(t1
.sock
->sk
, MSG_DONTWAIT
, &rc
);
740 KUNIT_EXPECT_NOT_ERR_OR_NULL(test
, rx_skb1
);
741 KUNIT_EXPECT_EQ(test
, rx_skb1
->len
, sizeof(t1
.msg
));
742 KUNIT_EXPECT_EQ(test
,
743 *(unsigned int *)skb_pull(rx_skb1
, sizeof(t1
.msg
.data
)),
747 rx_skb2
= skb_recv_datagram(t2
.sock
->sk
, MSG_DONTWAIT
, &rc
);
748 KUNIT_EXPECT_NOT_ERR_OR_NULL(test
, rx_skb2
);
749 KUNIT_EXPECT_EQ(test
, rx_skb2
->len
, sizeof(t2
.msg
));
750 KUNIT_EXPECT_EQ(test
,
751 *(unsigned int *)skb_pull(rx_skb2
, sizeof(t2
.msg
.data
)),
755 mctp_test_route_input_multiple_nets_bind_fini(test
, &t1
);
756 mctp_test_route_input_multiple_nets_bind_fini(test
, &t2
);
760 mctp_test_route_input_multiple_nets_key_init(struct kunit
*test
,
763 struct mctp_hdr hdr
= RX_HDR(1, 9, 8, FL_S
| FL_E
| FL_T(1));
764 struct mctp_sock
*msk
;
765 struct netns_mctp
*mns
;
768 t
->msg
.data
= t
->netid
;
770 __mctp_route_test_init(test
, &t
->dev
, &t
->rt
, &t
->sock
, t
->netid
);
772 msk
= container_of(t
->sock
->sk
, struct mctp_sock
, sk
);
774 t
->key
= mctp_key_alloc(msk
, t
->netid
, hdr
.dest
, hdr
.src
, 1, GFP_KERNEL
);
775 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, t
->key
);
777 mns
= &sock_net(t
->sock
->sk
)->mctp
;
778 spin_lock_irqsave(&mns
->keys_lock
, flags
);
779 mctp_reserve_tag(&init_net
, t
->key
, msk
);
780 spin_unlock_irqrestore(&mns
->keys_lock
, flags
);
782 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, t
->key
);
783 t
->skb
= mctp_test_create_skb_data(&hdr
, &t
->msg
);
784 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, t
->skb
);
785 mctp_test_skb_set_dev(t
->skb
, t
->dev
);
789 mctp_test_route_input_multiple_nets_key_fini(struct kunit
*test
,
792 mctp_key_unref(t
->key
);
793 __mctp_route_test_fini(test
, t
->dev
, t
->rt
, t
->sock
);
796 /* test that skbs from different nets (otherwise identical) get routed to their
797 * corresponding socket via the sk_key
799 static void mctp_test_route_input_multiple_nets_key(struct kunit
*test
)
801 struct sk_buff
*rx_skb1
, *rx_skb2
;
802 struct test_net t1
, t2
;
808 /* use type 1 which is not bound */
812 mctp_test_route_input_multiple_nets_key_init(test
, &t1
);
813 mctp_test_route_input_multiple_nets_key_init(test
, &t2
);
815 rc
= mctp_route_input(&t1
.rt
->rt
, t1
.skb
);
816 KUNIT_ASSERT_EQ(test
, rc
, 0);
817 rc
= mctp_route_input(&t2
.rt
->rt
, t2
.skb
);
818 KUNIT_ASSERT_EQ(test
, rc
, 0);
820 rx_skb1
= skb_recv_datagram(t1
.sock
->sk
, MSG_DONTWAIT
, &rc
);
821 KUNIT_EXPECT_NOT_ERR_OR_NULL(test
, rx_skb1
);
822 KUNIT_EXPECT_EQ(test
, rx_skb1
->len
, sizeof(t1
.msg
));
823 KUNIT_EXPECT_EQ(test
,
824 *(unsigned int *)skb_pull(rx_skb1
, sizeof(t1
.msg
.data
)),
828 rx_skb2
= skb_recv_datagram(t2
.sock
->sk
, MSG_DONTWAIT
, &rc
);
829 KUNIT_EXPECT_NOT_ERR_OR_NULL(test
, rx_skb2
);
830 KUNIT_EXPECT_EQ(test
, rx_skb2
->len
, sizeof(t2
.msg
));
831 KUNIT_EXPECT_EQ(test
,
832 *(unsigned int *)skb_pull(rx_skb2
, sizeof(t2
.msg
.data
)),
836 mctp_test_route_input_multiple_nets_key_fini(test
, &t1
);
837 mctp_test_route_input_multiple_nets_key_fini(test
, &t2
);
840 #if IS_ENABLED(CONFIG_MCTP_FLOWS)
842 static void mctp_test_flow_init(struct kunit
*test
,
843 struct mctp_test_dev
**devp
,
844 struct mctp_test_route
**rtp
,
845 struct socket
**sock
,
846 struct sk_buff
**skbp
,
849 struct mctp_test_route
*rt
;
850 struct mctp_test_dev
*dev
;
853 /* we have a slightly odd routing setup here; the test route
854 * is for EID 8, which is our local EID. We don't do a routing
855 * lookup, so that's fine - all we require is a path through
856 * mctp_local_output, which will call rt->output on whatever
859 __mctp_route_test_init(test
, &dev
, &rt
, sock
, MCTP_NET_ANY
);
861 /* Assign a single EID. ->addrs is freed on mctp netdev release */
862 dev
->mdev
->addrs
= kmalloc(sizeof(u8
), GFP_KERNEL
);
863 dev
->mdev
->num_addrs
= 1;
864 dev
->mdev
->addrs
[0] = 8;
866 skb
= alloc_skb(len
+ sizeof(struct mctp_hdr
) + 1, GFP_KERNEL
);
867 KUNIT_ASSERT_TRUE(test
, skb
);
869 skb_reserve(skb
, sizeof(struct mctp_hdr
) + 1);
870 memset(skb_put(skb
, len
), 0, len
);
872 /* take a ref for the route, we'll decrement in local output */
873 refcount_inc(&rt
->rt
.refs
);
880 static void mctp_test_flow_fini(struct kunit
*test
,
881 struct mctp_test_dev
*dev
,
882 struct mctp_test_route
*rt
,
885 __mctp_route_test_fini(test
, dev
, rt
, sock
);
888 /* test that an outgoing skb has the correct MCTP extension data set */
889 static void mctp_test_packet_flow(struct kunit
*test
)
891 struct sk_buff
*skb
, *skb2
;
892 struct mctp_test_route
*rt
;
893 struct mctp_test_dev
*dev
;
894 struct mctp_flow
*flow
;
899 mctp_test_flow_init(test
, &dev
, &rt
, &sock
, &skb
, 30);
901 rc
= mctp_local_output(sock
->sk
, &rt
->rt
, skb
, dst
, MCTP_TAG_OWNER
);
902 KUNIT_ASSERT_EQ(test
, rc
, 0);
905 KUNIT_ASSERT_EQ(test
, n
, 1);
907 skb2
= skb_dequeue(&rt
->pkts
);
908 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, skb2
);
910 flow
= skb_ext_find(skb2
, SKB_EXT_MCTP
);
911 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, flow
);
912 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, flow
->key
);
913 KUNIT_ASSERT_PTR_EQ(test
, flow
->key
->sk
, sock
->sk
);
916 mctp_test_flow_fini(test
, dev
, rt
, sock
);
919 /* test that outgoing skbs, after fragmentation, all have the correct MCTP
920 * extension data set.
922 static void mctp_test_fragment_flow(struct kunit
*test
)
924 struct mctp_flow
*flows
[2];
925 struct sk_buff
*tx_skbs
[2];
926 struct mctp_test_route
*rt
;
927 struct mctp_test_dev
*dev
;
933 mctp_test_flow_init(test
, &dev
, &rt
, &sock
, &skb
, 100);
935 rc
= mctp_local_output(sock
->sk
, &rt
->rt
, skb
, dst
, MCTP_TAG_OWNER
);
936 KUNIT_ASSERT_EQ(test
, rc
, 0);
939 KUNIT_ASSERT_EQ(test
, n
, 2);
941 /* both resulting packets should have the same flow data */
942 tx_skbs
[0] = skb_dequeue(&rt
->pkts
);
943 tx_skbs
[1] = skb_dequeue(&rt
->pkts
);
945 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, tx_skbs
[0]);
946 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, tx_skbs
[1]);
948 flows
[0] = skb_ext_find(tx_skbs
[0], SKB_EXT_MCTP
);
949 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, flows
[0]);
950 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, flows
[0]->key
);
951 KUNIT_ASSERT_PTR_EQ(test
, flows
[0]->key
->sk
, sock
->sk
);
953 flows
[1] = skb_ext_find(tx_skbs
[1], SKB_EXT_MCTP
);
954 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, flows
[1]);
955 KUNIT_ASSERT_PTR_EQ(test
, flows
[1]->key
, flows
[0]->key
);
957 kfree_skb(tx_skbs
[0]);
958 kfree_skb(tx_skbs
[1]);
959 mctp_test_flow_fini(test
, dev
, rt
, sock
);
963 static void mctp_test_packet_flow(struct kunit
*test
)
965 kunit_skip(test
, "Requires CONFIG_MCTP_FLOWS=y");
968 static void mctp_test_fragment_flow(struct kunit
*test
)
970 kunit_skip(test
, "Requires CONFIG_MCTP_FLOWS=y");
974 /* Test that outgoing skbs cause a suitable tag to be created */
975 static void mctp_test_route_output_key_create(struct kunit
*test
)
977 const unsigned int netid
= 50;
978 const u8 dst
= 26, src
= 15;
979 struct mctp_test_route
*rt
;
980 struct mctp_test_dev
*dev
;
981 struct mctp_sk_key
*key
;
982 struct netns_mctp
*mns
;
990 dev
= mctp_test_create_dev();
991 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, dev
);
992 WRITE_ONCE(dev
->mdev
->net
, netid
);
994 rt
= mctp_test_create_route(&init_net
, dev
->mdev
, dst
, 68);
995 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, rt
);
997 rc
= sock_create_kern(&init_net
, AF_MCTP
, SOCK_DGRAM
, 0, &sock
);
998 KUNIT_ASSERT_EQ(test
, rc
, 0);
1000 dev
->mdev
->addrs
= kmalloc(sizeof(u8
), GFP_KERNEL
);
1001 dev
->mdev
->num_addrs
= 1;
1002 dev
->mdev
->addrs
[0] = src
;
1004 skb
= alloc_skb(sizeof(struct mctp_hdr
) + 1 + len
, GFP_KERNEL
);
1005 KUNIT_ASSERT_TRUE(test
, skb
);
1007 skb_reserve(skb
, sizeof(struct mctp_hdr
) + 1 + len
);
1008 memset(skb_put(skb
, len
), 0, len
);
1010 refcount_inc(&rt
->rt
.refs
);
1012 mns
= &sock_net(sock
->sk
)->mctp
;
1014 /* We assume we're starting from an empty keys list, which requires
1015 * preceding tests to clean up correctly!
1017 spin_lock_irqsave(&mns
->keys_lock
, flags
);
1018 empty
= hlist_empty(&mns
->keys
);
1019 spin_unlock_irqrestore(&mns
->keys_lock
, flags
);
1020 KUNIT_ASSERT_TRUE(test
, empty
);
1022 rc
= mctp_local_output(sock
->sk
, &rt
->rt
, skb
, dst
, MCTP_TAG_OWNER
);
1023 KUNIT_ASSERT_EQ(test
, rc
, 0);
1027 spin_lock_irqsave(&mns
->keys_lock
, flags
);
1028 if (!hlist_empty(&mns
->keys
)) {
1029 key
= hlist_entry(mns
->keys
.first
, struct mctp_sk_key
, hlist
);
1030 single
= hlist_is_singular_node(&key
->hlist
, &mns
->keys
);
1032 spin_unlock_irqrestore(&mns
->keys_lock
, flags
);
1034 KUNIT_ASSERT_NOT_NULL(test
, key
);
1035 KUNIT_ASSERT_TRUE(test
, single
);
1037 KUNIT_EXPECT_EQ(test
, key
->net
, netid
);
1038 KUNIT_EXPECT_EQ(test
, key
->local_addr
, src
);
1039 KUNIT_EXPECT_EQ(test
, key
->peer_addr
, dst
);
1040 /* key has incoming tag, so inverse of what we sent */
1041 KUNIT_EXPECT_FALSE(test
, key
->tag
& MCTP_TAG_OWNER
);
1044 mctp_test_route_destroy(test
, rt
);
1045 mctp_test_destroy_dev(dev
);
1048 static struct kunit_case mctp_test_cases
[] = {
1049 KUNIT_CASE_PARAM(mctp_test_fragment
, mctp_frag_gen_params
),
1050 KUNIT_CASE_PARAM(mctp_test_rx_input
, mctp_rx_input_gen_params
),
1051 KUNIT_CASE_PARAM(mctp_test_route_input_sk
, mctp_route_input_sk_gen_params
),
1052 KUNIT_CASE_PARAM(mctp_test_route_input_sk_reasm
,
1053 mctp_route_input_sk_reasm_gen_params
),
1054 KUNIT_CASE_PARAM(mctp_test_route_input_sk_keys
,
1055 mctp_route_input_sk_keys_gen_params
),
1056 KUNIT_CASE(mctp_test_route_input_multiple_nets_bind
),
1057 KUNIT_CASE(mctp_test_route_input_multiple_nets_key
),
1058 KUNIT_CASE(mctp_test_packet_flow
),
1059 KUNIT_CASE(mctp_test_fragment_flow
),
1060 KUNIT_CASE(mctp_test_route_output_key_create
),
1064 static struct kunit_suite mctp_test_suite
= {
1066 .test_cases
= mctp_test_cases
,
1069 kunit_test_suite(mctp_test_suite
);