blob: 35625cad6246e2d68c3ef79332be7286209e7c79 [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;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080022};
23
24struct test_ctx {
Patrick Williamsa721c2d2022-12-04 14:30:26 -060025 struct mctp *mctp;
26 struct mctp_binding_bridge *bindings[2];
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080027};
28
29static int mctp_binding_bridge_tx(struct mctp_binding *b,
Patrick Williamsa721c2d2022-12-04 14:30:26 -060030 struct mctp_pktbuf *pkt)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080031{
Patrick Williamsa721c2d2022-12-04 14:30:26 -060032 struct mctp_binding_bridge *binding =
33 container_of(b, struct mctp_binding_bridge, binding);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080034
35 binding->tx_count++;
36 assert(mctp_pktbuf_size(pkt) == sizeof(struct mctp_hdr) + 1);
37 binding->last_pkt_data = *(uint8_t *)mctp_pktbuf_data(pkt);
38
39 return 0;
40}
41
Andrew Jefferycdfafd92020-02-07 17:10:34 +103042static void mctp_binding_bridge_rx(struct mctp_binding_bridge *binding,
Patrick Williamsa721c2d2022-12-04 14:30:26 -060043 uint8_t key)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080044{
45 struct mctp_pktbuf *pkt;
46 struct mctp_hdr *hdr;
47 uint8_t *buf;
48
Patrick Williamsa721c2d2022-12-04 14:30:26 -060049 pkt = mctp_pktbuf_alloc(&binding->binding, sizeof(struct mctp_hdr) + 1);
Andrew Jefferycdfafd92020-02-07 17:10:34 +103050 assert(pkt);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080051
52 hdr = mctp_pktbuf_hdr(pkt);
53 hdr->flags_seq_tag = MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM;
54
55 /* arbitrary src/dest, as we're bridging */
56 hdr->src = 1;
57 hdr->dest = 2;
58
59 buf = mctp_pktbuf_data(pkt);
60 *buf = key;
61
62 binding->rx_count++;
63 mctp_bus_rx(&binding->binding, pkt);
64}
65
Rashmica Guptaca85ad82022-08-11 10:43:36 +100066static struct mctp_binding_bridge *mctp_binding_bridge_init(char *name)
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080067{
68 struct mctp_binding_bridge *binding;
69
70 binding = __mctp_alloc(sizeof(*binding));
71 memset(binding, 0, sizeof(*binding));
Rashmica Guptaca85ad82022-08-11 10:43:36 +100072 binding->binding.name = name;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080073 binding->binding.version = 1;
74 binding->binding.tx = mctp_binding_bridge_tx;
Andrew Jeffery73c268e2020-01-30 10:16:09 +103075 binding->binding.pkt_size = MCTP_PACKET_SIZE(MCTP_BTU);
Andrew Jeffery39da3d02021-03-12 16:51:26 +103076 binding->binding.pkt_header = 0;
77 binding->binding.pkt_trailer = 0;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080078 return binding;
79}
80
81int main(void)
82{
83 struct test_ctx _ctx, *ctx = &_ctx;
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080084 ctx->mctp = mctp_init();
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080085
Rashmica Guptaca85ad82022-08-11 10:43:36 +100086 ctx->bindings[0] = mctp_binding_bridge_init("binding0");
87 ctx->bindings[1] = mctp_binding_bridge_init("binding1");
Patrick Williamsa721c2d2022-12-04 14:30:26 -060088 mctp_bridge_busses(ctx->mctp, &ctx->bindings[0]->binding,
89 &ctx->bindings[1]->binding);
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +080090
91 mctp_binding_set_tx_enabled(&ctx->bindings[0]->binding, true);
92 mctp_binding_set_tx_enabled(&ctx->bindings[1]->binding, true);
93
94 mctp_binding_bridge_rx(ctx->bindings[0], 0xaa);
95 assert(ctx->bindings[0]->tx_count == 0);
96 assert(ctx->bindings[1]->tx_count == 1);
97 assert(ctx->bindings[1]->last_pkt_data == 0xaa);
98
99 mctp_binding_bridge_rx(ctx->bindings[1], 0x55);
100 assert(ctx->bindings[1]->tx_count == 1);
101 assert(ctx->bindings[0]->tx_count == 1);
102 assert(ctx->bindings[0]->last_pkt_data == 0x55);
103
Andrew Jefferyc8908ac2020-05-29 09:30:20 +0930104 __mctp_free(ctx->bindings[1]);
105 __mctp_free(ctx->bindings[0]);
106 mctp_destroy(ctx->mctp);
107
Jeremy Kerr1a4ec3c2019-09-03 11:01:50 +0800108 return EXIT_SUCCESS;
109}