drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / net / mctp / test / route-test.c
blob8551dab1d1e69836c84f68509bc9dab43a96cc67
1 // SPDX-License-Identifier: GPL-2.0
3 #include <kunit/test.h>
5 #include "utils.h"
7 struct mctp_test_route {
8 struct mctp_route rt;
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);
18 return 0;
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);
27 if (!rt)
28 return NULL;
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);
36 return rt;
39 static struct mctp_test_route *mctp_test_create_route(struct net *net,
40 struct mctp_dev *dev,
41 mctp_eid_t eid,
42 unsigned int mtu)
44 struct mctp_test_route *rt;
46 rt = mctp_route_test_alloc();
47 if (!rt)
48 return NULL;
50 rt->rt.min = eid;
51 rt->rt.max = eid;
52 rt->rt.mtu = mtu;
53 rt->rt.type = RTN_UNSPEC;
54 if (dev)
55 mctp_dev_hold(dev);
56 rt->rt.dev = dev;
58 list_add_rcu(&rt->rt.list, &net->mctp.routes);
60 return rt;
63 static void mctp_test_route_destroy(struct kunit *test,
64 struct mctp_test_route *rt)
66 unsigned int refs;
68 rtnl_lock();
69 list_del_rcu(&rt->rt.list);
70 rtnl_unlock();
72 skb_queue_purge(&rt->pkts);
73 if (rt->rt.dev)
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;
87 cb = mctp_cb(skb);
88 cb->net = READ_ONCE(dev->mdev->net);
89 skb->dev = dev->ndev;
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);
96 struct sk_buff *skb;
97 unsigned int i;
98 u8 *buf;
100 skb = alloc_skb(hdr_len + data_len, GFP_KERNEL);
101 if (!skb)
102 return NULL;
104 __mctp_cb(skb);
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++)
109 buf[i] = i & 0xff;
111 return skb;
114 static struct sk_buff *__mctp_test_create_skb_data(const struct mctp_hdr *hdr,
115 const void *data,
116 size_t data_len)
118 size_t hdr_len = sizeof(*hdr);
119 struct sk_buff *skb;
121 skb = alloc_skb(hdr_len + data_len, GFP_KERNEL);
122 if (!skb)
123 return NULL;
125 __mctp_cb(skb);
126 memcpy(skb_put(skb, hdr_len), hdr, hdr_len);
127 memcpy(skb_put(skb, data_len), data, data_len);
129 return skb;
132 #define mctp_test_create_skb_data(h, d) \
133 __mctp_test_create_skb_data(h, d, sizeof(*d))
135 struct mctp_frag_test {
136 unsigned int mtu;
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;
146 struct sk_buff *skb;
147 struct mctp_hdr hdr;
148 u8 seq;
150 params = test->param_value;
151 mtu = params->mtu;
152 msgsize = params->msgsize;
154 hdr.ver = 1;
155 hdr.src = 8;
156 hdr.dest = 10;
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);
168 n = rt->pkts.qlen;
170 KUNIT_EXPECT_EQ(test, n, params->n_frags);
172 for (i = 0;; i++) {
173 struct mctp_hdr *hdr2;
174 struct sk_buff *skb2;
175 u8 tag_mask, seq2;
176 bool first, last;
178 first = i == 0;
179 last = i == (n - 1);
181 skb2 = skb_dequeue(&rt->pkts);
183 if (!skb2)
184 break;
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) &
202 MCTP_HDR_SEQ_MASK;
204 if (first) {
205 seq = seq2;
206 } else {
207 seq++;
208 KUNIT_EXPECT_EQ(test, seq2, seq & MCTP_HDR_SEQ_MASK);
211 if (!last)
212 KUNIT_EXPECT_EQ(test, skb2->len, mtu);
213 else
214 KUNIT_EXPECT_LE(test, skb2->len, mtu);
216 kfree_skb(skb2);
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 {
242 struct mctp_hdr hdr;
243 bool input;
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;
251 struct sk_buff *skb;
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(&params->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,
283 char *desc)
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,
297 unsigned int netid)
299 struct sockaddr_mctp addr = {0};
300 struct mctp_test_route *rt;
301 struct mctp_test_dev *dev;
302 struct socket *sock;
303 int rc;
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;
319 addr.smctp_type = 0;
320 rc = kernel_bind(sock, (struct sockaddr *)&addr, sizeof(addr));
321 KUNIT_ASSERT_EQ(test, rc, 0);
323 *rtp = rt;
324 *devp = dev;
325 *sockp = sock;
328 static void __mctp_route_test_fini(struct kunit *test,
329 struct mctp_test_dev *dev,
330 struct mctp_test_route *rt,
331 struct socket *sock)
333 sock_release(sock);
334 mctp_test_route_destroy(test, rt);
335 mctp_test_destroy_dev(dev);
338 struct mctp_route_input_sk_test {
339 struct mctp_hdr hdr;
340 u8 type;
341 bool deliver;
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;
350 struct socket *sock;
351 int rc;
353 params = test->param_value;
355 __mctp_route_test_init(test, &dev, &rt, &sock, MCTP_NET_ANY);
357 skb = mctp_test_create_skb_data(&params->hdr, &params->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);
373 } else {
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,
397 char *desc)
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 {
407 const char *name;
408 struct mctp_hdr hdrs[4];
409 int n_hdrs;
410 int rx_len;
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;
419 struct socket *sock;
420 int i, rc;
421 u8 c;
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++) {
428 c = i;
429 skb = mctp_test_create_skb_data(&params->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);
444 } else {
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",
456 .hdrs = {
457 RX_FRAG(FL_S | FL_E, 0),
459 .n_hdrs = 1,
460 .rx_len = 1,
463 .name = "single packet, offset seq",
464 .hdrs = {
465 RX_FRAG(FL_S | FL_E, 1),
467 .n_hdrs = 1,
468 .rx_len = 1,
471 .name = "start & end packets",
472 .hdrs = {
473 RX_FRAG(FL_S, 0),
474 RX_FRAG(FL_E, 1),
476 .n_hdrs = 2,
477 .rx_len = 2,
480 .name = "start & end packets, offset seq",
481 .hdrs = {
482 RX_FRAG(FL_S, 1),
483 RX_FRAG(FL_E, 2),
485 .n_hdrs = 2,
486 .rx_len = 2,
489 .name = "start & end packets, out of order",
490 .hdrs = {
491 RX_FRAG(FL_E, 1),
492 RX_FRAG(FL_S, 0),
494 .n_hdrs = 2,
495 .rx_len = 0,
498 .name = "start, middle & end packets",
499 .hdrs = {
500 RX_FRAG(FL_S, 0),
501 RX_FRAG(0, 1),
502 RX_FRAG(FL_E, 2),
504 .n_hdrs = 3,
505 .rx_len = 3,
508 .name = "missing seq",
509 .hdrs = {
510 RX_FRAG(FL_S, 0),
511 RX_FRAG(FL_E, 2),
513 .n_hdrs = 2,
514 .rx_len = 0,
517 .name = "seq wrap",
518 .hdrs = {
519 RX_FRAG(FL_S, 3),
520 RX_FRAG(FL_E, 0),
522 .n_hdrs = 2,
523 .rx_len = 2,
527 static void mctp_route_input_sk_reasm_to_desc(
528 const struct mctp_route_input_sk_reasm_test *t,
529 char *desc)
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 {
538 const char *name;
539 mctp_eid_t key_peer_addr;
540 mctp_eid_t key_local_addr;
541 u8 key_tag;
542 struct mctp_hdr hdr;
543 bool deliver;
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;
556 struct socket *sock;
557 unsigned long flags;
558 unsigned int net;
559 int rc;
560 u8 c;
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,
580 GFP_KERNEL);
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 */
589 c = 0;
590 skb = mctp_test_create_skb_data(&params->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);
600 if (params->deliver)
601 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, skb2);
602 else
603 KUNIT_EXPECT_PTR_EQ(test, skb2, NULL);
605 if (skb2)
606 skb_free_datagram(sock->sk, skb2);
608 mctp_key_unref(key);
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",
615 .key_peer_addr = 9,
616 .key_local_addr = 8,
617 .key_tag = 1,
618 .hdr = RX_HDR(1, 9, 8, FL_S | FL_E | FL_T(1)),
619 .deliver = true,
622 .name = "flipped src/dest",
623 .key_peer_addr = 8,
624 .key_local_addr = 9,
625 .key_tag = 1,
626 .hdr = RX_HDR(1, 9, 8, FL_S | FL_E | FL_T(1)),
627 .deliver = false,
630 .name = "peer addr mismatch",
631 .key_peer_addr = 9,
632 .key_local_addr = 8,
633 .key_tag = 1,
634 .hdr = RX_HDR(1, 10, 8, FL_S | FL_E | FL_T(1)),
635 .deliver = false,
638 .name = "tag value mismatch",
639 .key_peer_addr = 9,
640 .key_local_addr = 8,
641 .key_tag = 1,
642 .hdr = RX_HDR(1, 9, 8, FL_S | FL_E | FL_T(2)),
643 .deliver = false,
646 .name = "TO mismatch",
647 .key_peer_addr = 9,
648 .key_local_addr = 8,
649 .key_tag = 1,
650 .hdr = RX_HDR(1, 9, 8, FL_S | FL_E | FL_T(1) | FL_TO),
651 .deliver = false,
654 .name = "broadcast response",
655 .key_peer_addr = MCTP_ADDR_ANY,
656 .key_local_addr = 8,
657 .key_tag = 1,
658 .hdr = RX_HDR(1, 11, 8, FL_S | FL_E | FL_T(1)),
659 .deliver = true,
662 .name = "any local match",
663 .key_peer_addr = 12,
664 .key_local_addr = MCTP_ADDR_ANY,
665 .key_tag = 1,
666 .hdr = RX_HDR(1, 12, 8, FL_S | FL_E | FL_T(1)),
667 .deliver = true,
671 static void mctp_route_input_sk_keys_to_desc(
672 const struct mctp_route_input_sk_keys_test *t,
673 char *desc)
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);
681 struct test_net {
682 unsigned int netid;
683 struct mctp_test_dev *dev;
684 struct mctp_test_route *rt;
685 struct socket *sock;
686 struct sk_buff *skb;
687 struct mctp_sk_key *key;
688 struct {
689 u8 type;
690 unsigned int data;
691 } msg;
694 static void
695 mctp_test_route_input_multiple_nets_bind_init(struct kunit *test,
696 struct test_net *t)
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);
709 static void
710 mctp_test_route_input_multiple_nets_bind_fini(struct kunit *test,
711 struct test_net *t)
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;
723 int rc;
725 t1.netid = 1;
726 t2.netid = 2;
728 t1.msg.type = 0;
729 t2.msg.type = 0;
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)),
744 t1.netid);
745 kfree_skb(rx_skb1);
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)),
752 t2.netid);
753 kfree_skb(rx_skb2);
755 mctp_test_route_input_multiple_nets_bind_fini(test, &t1);
756 mctp_test_route_input_multiple_nets_bind_fini(test, &t2);
759 static void
760 mctp_test_route_input_multiple_nets_key_init(struct kunit *test,
761 struct test_net *t)
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;
766 unsigned long flags;
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);
788 static void
789 mctp_test_route_input_multiple_nets_key_fini(struct kunit *test,
790 struct test_net *t)
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;
803 int rc;
805 t1.netid = 1;
806 t2.netid = 2;
808 /* use type 1 which is not bound */
809 t1.msg.type = 1;
810 t2.msg.type = 1;
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)),
825 t1.netid);
826 kfree_skb(rx_skb1);
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)),
833 t2.netid);
834 kfree_skb(rx_skb2);
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,
847 unsigned int len)
849 struct mctp_test_route *rt;
850 struct mctp_test_dev *dev;
851 struct sk_buff *skb;
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
857 * route we provide
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);
868 __mctp_cb(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);
875 *devp = dev;
876 *rtp = rt;
877 *skbp = skb;
880 static void mctp_test_flow_fini(struct kunit *test,
881 struct mctp_test_dev *dev,
882 struct mctp_test_route *rt,
883 struct socket *sock)
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;
895 struct socket *sock;
896 u8 dst = 8;
897 int n, rc;
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);
904 n = rt->pkts.qlen;
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);
915 kfree_skb(skb2);
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;
928 struct sk_buff *skb;
929 struct socket *sock;
930 u8 dst = 8;
931 int n, rc;
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);
938 n = rt->pkts.qlen;
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);
962 #else
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");
972 #endif
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;
983 unsigned long flags;
984 struct socket *sock;
985 struct sk_buff *skb;
986 bool empty, single;
987 const int len = 2;
988 int rc;
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);
1006 __mctp_cb(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);
1025 key = NULL;
1026 single = false;
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);
1043 sock_release(sock);
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 = {
1065 .name = "mctp",
1066 .test_cases = mctp_test_cases,
1069 kunit_test_suite(mctp_test_suite);