blob: 73705ba4ffe28f800dc189fa02d7a297b45307b5 [file] [log] [blame]
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +08001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2
Rashmica Guptae915aad2022-08-10 16:28:59 +10003#ifdef NDEBUG
4#undef NDEBUG
5#endif
6
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +08007#include <assert.h>
8#include <stdlib.h>
9#include <stdio.h>
10#include <string.h>
11
12#include <libmctp.h>
13#include <libmctp-alloc.h>
14
15#include "test-utils.h"
16
17struct mctp_binding_bridge {
Patrick Williamsa721c2d2022-12-04 14:30:26 -060018 struct mctp_binding binding;
19 int rx_count;
20 int tx_count;
21 uint8_t last_pkt_data;
Matt Johnston4a09e1d2024-09-13 14:55:58 +080022 uint8_t tx_storage[MCTP_PKTBUF_SIZE(MCTP_BTU)];
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080023};
24
25struct test_ctx {
Patrick Williamsa721c2d2022-12-04 14:30:26 -060026 struct mctp *mctp;
27 struct mctp_binding_bridge *bindings[2];
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080028};
29
30static int mctp_binding_bridge_tx(struct mctp_binding *b,
Patrick Williamsa721c2d2022-12-04 14:30:26 -060031 struct mctp_pktbuf *pkt)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080032{
Patrick Williamsa721c2d2022-12-04 14:30:26 -060033 struct mctp_binding_bridge *binding =
34 container_of(b, struct mctp_binding_bridge, binding);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080035
36 binding->tx_count++;
37 assert(mctp_pktbuf_size(pkt) == sizeof(struct mctp_hdr) + 1);
38 binding->last_pkt_data = *(uint8_t *)mctp_pktbuf_data(pkt);
39
40 return 0;
41}
42
Andrew Jefferycdfafd92020-02-07 17:10:34 +103043static void mctp_binding_bridge_rx(struct mctp_binding_bridge *binding,
Patrick Williamsa721c2d2022-12-04 14:30:26 -060044 uint8_t key)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080045{
46 struct mctp_pktbuf *pkt;
47 struct mctp_hdr *hdr;
48 uint8_t *buf;
49
Patrick Williamsa721c2d2022-12-04 14:30:26 -060050 pkt = mctp_pktbuf_alloc(&binding->binding, sizeof(struct mctp_hdr) + 1);
Andrew Jefferycdfafd92020-02-07 17:10:34 +103051 assert(pkt);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080052
53 hdr = mctp_pktbuf_hdr(pkt);
54 hdr->flags_seq_tag = MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM;
55
56 /* arbitrary src/dest, as we're bridging */
57 hdr->src = 1;
58 hdr->dest = 2;
59
60 buf = mctp_pktbuf_data(pkt);
61 *buf = key;
62
63 binding->rx_count++;
64 mctp_bus_rx(&binding->binding, pkt);
Matt Johnston4a09e1d2024-09-13 14:55:58 +080065 mctp_pktbuf_free(pkt);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080066}
67
Rashmica Guptaca85ad82022-08-11 10:43:36 +100068static struct mctp_binding_bridge *mctp_binding_bridge_init(char *name)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080069{
70 struct mctp_binding_bridge *binding;
71
72 binding = __mctp_alloc(sizeof(*binding));
73 memset(binding, 0, sizeof(*binding));
Rashmica Guptaca85ad82022-08-11 10:43:36 +100074 binding->binding.name = name;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080075 binding->binding.version = 1;
76 binding->binding.tx = mctp_binding_bridge_tx;
Andrew Jeffery73c268e2020-01-30 10:16:09 +103077 binding->binding.pkt_size = MCTP_PACKET_SIZE(MCTP_BTU);
Andrew Jeffery39da3d02021-03-12 16:51:26 +103078 binding->binding.pkt_header = 0;
79 binding->binding.pkt_trailer = 0;
Matt Johnston4a09e1d2024-09-13 14:55:58 +080080 binding->binding.tx_storage = binding->tx_storage;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080081 return binding;
82}
83
84int main(void)
85{
86 struct test_ctx _ctx, *ctx = &_ctx;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080087 ctx->mctp = mctp_init();
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080088
Rashmica Guptaca85ad82022-08-11 10:43:36 +100089 ctx->bindings[0] = mctp_binding_bridge_init("binding0");
90 ctx->bindings[1] = mctp_binding_bridge_init("binding1");
Patrick Williamsa721c2d2022-12-04 14:30:26 -060091 mctp_bridge_busses(ctx->mctp, &ctx->bindings[0]->binding,
92 &ctx->bindings[1]->binding);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080093
94 mctp_binding_set_tx_enabled(&ctx->bindings[0]->binding, true);
95 mctp_binding_set_tx_enabled(&ctx->bindings[1]->binding, true);
96
97 mctp_binding_bridge_rx(ctx->bindings[0], 0xaa);
98 assert(ctx->bindings[0]->tx_count == 0);
99 assert(ctx->bindings[1]->tx_count == 1);
100 assert(ctx->bindings[1]->last_pkt_data == 0xaa);
101
102 mctp_binding_bridge_rx(ctx->bindings[1], 0x55);
103 assert(ctx->bindings[1]->tx_count == 1);
104 assert(ctx->bindings[0]->tx_count == 1);
105 assert(ctx->bindings[0]->last_pkt_data == 0x55);
106
Andrew Jefferyc8908ac2020-05-29 09:30:20 +0930107 __mctp_free(ctx->bindings[1]);
108 __mctp_free(ctx->bindings[0]);
109 mctp_destroy(ctx->mctp);
110
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800111 return EXIT_SUCCESS;
112}