1717//! Applications can create [`router::RouterAsyncListener`] and [`router::RouterAsyncReqChannel`]
1818//! instances to communicate over MCTP. Those implement the standard [`mctp` crate](mctp)
1919//! async traits.
20+ //!
21+ //! ## Configuration
22+ //!
23+ //! `mctp-estack` uses fixed sizes to be suitable on no-alloc platforms.
24+ //! These can be configured at build time, see [`config`]
2025
2126#![ cfg_attr( not( any( feature = "std" , test) ) , no_std) ]
2227#![ forbid( unsafe_code) ]
@@ -33,6 +38,7 @@ use mctp::{Eid, MsgType, TagValue, Tag, Error, Result};
3338
3439mod fragment;
3540mod reassemble;
41+ mod util;
3642pub mod i2c;
3743pub mod serial;
3844pub mod usb;
@@ -44,11 +50,8 @@ use reassemble::Reassembler;
4450pub use router:: Router ;
4551
4652use crate :: fmt:: * ;
53+ pub ( crate ) use config:: * ;
4754
48- const FLOWS : usize = 8 ;
49-
50- const NUM_RECEIVE : usize = 4 ;
51- const RECV_PAYLOAD : usize = 1032 ;
5255
5356/// Timeout for message reassembly.
5457///
@@ -68,6 +71,50 @@ pub const DEFERRED_TIMEOUT: u32 = 6000;
6871/// See [`Stack::update()`].
6972pub const TIMEOUT_INTERVAL : u32 = 100 ;
7073
74+ pub ( crate ) const HEADER_LEN : usize = 4 ;
75+
76+ /// Build-time configuration and defaults
77+ ///
78+ /// To set a non-default value, set the `MCTP_ESTACK_...` environment variable
79+ /// during the build. Those variables can be set in the `[env]`
80+ /// section of `.cargo/config.toml`.
81+ pub mod config {
82+ use crate :: get_build_var;
83+
84+ /// Maximum size of a MCTP message payload in bytes, default 1032
85+ ///
86+ /// This does not include the MCTP type byte.
87+ ///
88+ /// Customise with `MCTP_ESTACK_MAX_MESSAGE` environment variable.
89+ pub const MAX_PAYLOAD : usize = get_build_var ! ( "MCTP_ESTACK_MAX_MESSAGE" , 1032 ) ;
90+
91+ /// Number of concurrent receive messages, default 4
92+ ///
93+ /// The number of in-progress message reassemblies is limited to `NUM_RECEIVE`.
94+ /// Total memory used for reassembly buffers is roughly
95+ /// `MAX_PAYLOAD` * `NUM_RECEIVE` bytes.
96+ ///
97+ /// Customise with `MCTP_ESTACK_NUM_RECEIVE` environment variable.
98+ /// Number of outstanding waiting responses, default 64
99+ pub const NUM_RECEIVE : usize = get_build_var ! ( "MCTP_ESTACK_NUM_RECEIVE" , 4 ) ;
100+ ///
101+ /// After a message is sent with Tag Owner (TO) bit set, the stack will accept
102+ /// response messages with the same tag and TO _unset_. `FLOWS` defines
103+ /// the number of slots available for pending responses.
104+ ///
105+ /// Customise with `MCTP_ESTACK_FLOWS` environment variable.
106+ /// Must be a power of two.
107+ pub const FLOWS : usize = get_build_var ! ( "MCTP_ESTACK_FLOWS" , 64 ) ;
108+
109+ /// Maximum allowed MTU, default 255
110+ ///
111+ /// The largest MTU allowed for any link.
112+ ///
113+ /// Customise with `MCTP_ESTACK_MAX_MTU` environment variable.
114+ pub const MAX_MTU : usize = get_build_var ! ( "MCTP_ESTACK_MAX_MTU" , 255 ) ;
115+ const _: ( ) = assert ! ( MAX_MTU >= crate :: HEADER_LEN +1 , "MAX_MTU too small" ) ;
116+ }
117+
71118#[ derive( Debug ) ]
72119struct Flow {
73120 // preallocated flows have None expiry
@@ -79,7 +126,6 @@ struct Flow {
79126#[ derive( Debug , Eq , PartialEq , Clone , Copy , Hash ) ]
80127pub struct AppCookie ( pub usize ) ;
81128
82- pub ( crate ) const HEADER_LEN : usize = 4 ;
83129type Header = libmctp:: base_packet:: MCTPTransportHeader < [ u8 ; HEADER_LEN ] > ;
84130
85131/// A handle to a received message.
@@ -103,8 +149,8 @@ pub struct Stack {
103149 // The buffer is kept outside of the Reassembler, in case it is borrowed
104150 // from other storage locations in future.
105151 // This is [Option<>] rather than Vec so that indices remain stable
106- // for the ReceiveHandle. Could use a LinearMap instead?
107- reassemblers : [ Option < ( Reassembler , Vec < u8 , RECV_PAYLOAD > ) > ; NUM_RECEIVE ] ,
152+ // for the ReceiveHandle. Could use a Map instead?
153+ reassemblers : [ Option < ( Reassembler , Vec < u8 , MAX_PAYLOAD > ) > ; NUM_RECEIVE ] ,
108154
109155 /// monotonic time and counter.
110156 now : EventStamp ,
0 commit comments