Jeremy Kerr | 3d36ee2 | 2019-05-30 11:15:37 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 2 | |
Rashmica Gupta | e915aad | 2022-08-10 16:28:59 +1000 | [diff] [blame] | 3 | #ifdef NDEBUG |
| 4 | #undef NDEBUG |
| 5 | #endif |
| 6 | |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 7 | #include <string.h> |
| 8 | #include <assert.h> |
| 9 | |
| 10 | #include <libmctp.h> |
| 11 | #include <libmctp-alloc.h> |
| 12 | |
| 13 | #include "test-utils.h" |
| 14 | |
Matt Johnston | 0a96544 | 2024-09-16 16:49:20 +0800 | [diff] [blame] | 15 | /* mctp_binding_test can be used for loopback in tests. Senders must use |
| 16 | * the local EID as the destination */ |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 17 | struct mctp_binding_test { |
Andrew Jeffery | 69eda31 | 2022-09-30 14:22:32 +0930 | [diff] [blame] | 18 | struct mctp_binding binding; |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 19 | uint8_t tx_storage[MCTP_PKTBUF_SIZE(MCTP_BTU)]; |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 20 | }; |
| 21 | |
Matt Johnston | 0a96544 | 2024-09-16 16:49:20 +0800 | [diff] [blame] | 22 | static int mctp_binding_test_tx(struct mctp_binding *b, struct mctp_pktbuf *pkt) |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 23 | { |
Matt Johnston | 0a96544 | 2024-09-16 16:49:20 +0800 | [diff] [blame] | 24 | mctp_bus_rx(b, pkt); |
Andrew Jeffery | a501a58 | 2020-03-10 22:25:16 +1030 | [diff] [blame] | 25 | return 0; |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | struct mctp_binding_test *mctp_binding_test_init(void) |
| 29 | { |
| 30 | struct mctp_binding_test *test; |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 31 | test = __mctp_alloc(sizeof(*test)); |
Andrew Jeffery | 225c46d | 2020-03-10 23:20:01 +1030 | [diff] [blame] | 32 | memset(test, '\0', sizeof(*test)); |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 33 | test->binding.name = "test"; |
| 34 | test->binding.version = 1; |
| 35 | test->binding.tx = mctp_binding_test_tx; |
Andrew Jeffery | 73c268e | 2020-01-30 10:16:09 +1030 | [diff] [blame] | 36 | test->binding.pkt_size = MCTP_PACKET_SIZE(MCTP_BTU); |
Andrew Jeffery | 39da3d0 | 2021-03-12 16:51:26 +1030 | [diff] [blame] | 37 | test->binding.pkt_header = 0; |
| 38 | test->binding.pkt_trailer = 0; |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 39 | test->binding.tx_storage = test->tx_storage; |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 40 | return test; |
| 41 | } |
| 42 | |
Andrew Jeffery | 5c5673b | 2020-03-10 23:23:58 +1030 | [diff] [blame] | 43 | void mctp_binding_test_destroy(struct mctp_binding_test *test) |
| 44 | { |
| 45 | __mctp_free(test); |
| 46 | } |
| 47 | |
Andrew Jeffery | 69eda31 | 2022-09-30 14:22:32 +0930 | [diff] [blame] | 48 | void mctp_binding_test_rx_raw(struct mctp_binding_test *test, void *buf, |
| 49 | size_t len) |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 50 | { |
| 51 | struct mctp_pktbuf *pkt; |
| 52 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 53 | pkt = mctp_pktbuf_alloc(&test->binding, len); |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 54 | assert(pkt); |
| 55 | memcpy(mctp_pktbuf_hdr(pkt), buf, len); |
| 56 | mctp_bus_rx(&test->binding, pkt); |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 57 | mctp_pktbuf_free(pkt); |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void mctp_binding_test_register_bus(struct mctp_binding_test *binding, |
Andrew Jeffery | 69eda31 | 2022-09-30 14:22:32 +0930 | [diff] [blame] | 61 | struct mctp *mctp, mctp_eid_t eid) |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 62 | { |
| 63 | mctp_register_bus(mctp, &binding->binding, eid); |
| 64 | } |
| 65 | |
| 66 | void mctp_test_stack_init(struct mctp **mctp, |
Andrew Jeffery | 69eda31 | 2022-09-30 14:22:32 +0930 | [diff] [blame] | 67 | struct mctp_binding_test **binding, mctp_eid_t eid) |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 68 | { |
| 69 | *mctp = mctp_init(); |
| 70 | assert(*mctp); |
| 71 | |
| 72 | *binding = mctp_binding_test_init(); |
| 73 | assert(*binding); |
| 74 | |
| 75 | mctp_binding_test_register_bus(*binding, *mctp, eid); |
Matt Johnston | 0a96544 | 2024-09-16 16:49:20 +0800 | [diff] [blame] | 76 | mctp_binding_set_tx_enabled(&(*binding)->binding, true); |
Jeremy Kerr | 1892511 | 2019-03-13 15:43:08 +0800 | [diff] [blame] | 77 | } |