mctp-linux 0.2.0

Management Component Transport Protocol (MCTP) Linux transport
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// SPDX-License-Identifier: MIT OR Apache-2.0
/*
 * MCTP support through Linux kernel-based sockets
 *
 * Copyright (c) 2024 Code Construct
 */

#![warn(missing_docs)]

//! Interface for the Linux socket-based MCTP support.
//!
//! This crate provides some minimal wrappers around standard [`libc`] socket
//! operations, using MCTP-specific addressing structures.
//!
//! [`MctpSocket`] provides support for blocking socket operations
//! [sendto](MctpSocket::sendto), [`recvfrom`](MctpSocket::recvfrom) and
//! [`bind`](MctpSocket::bind).
//!
//! ```no_run
//! use mctp_linux;
//!
//! let sock = mctp_linux::MctpSocket::new()?;
//! let bind_addr = mctp_linux::MctpSockAddr::new(
//!     mctp::MCTP_ADDR_ANY.0,
//!     mctp_linux::MCTP_NET_ANY,
//!     1,
//!     0
//! );
//! sock.bind(&bind_addr)?;
//!
//! let mut buf = [0; 64];
//! let (len, src) = sock.recvfrom(&mut buf)?;
//! # Ok::<(), std::io::Error>(())
//! ```
//!
//! [`MctpLinuxReq`] provides a thin wrapper that represents a remote endpoint,
//! referenced by EID. It creates a single socket for communication with that
//! endpoint. This is a convenience for simple consumers that perform
//! single-endpoint communication; general MCTP requesters may want a different
//! socket model.

use core::mem;
use std::fmt;
use std::io::Error;
use std::os::unix::io::RawFd;
use std::time::Duration;

use mctp::{
    Eid, MsgIC, MsgType, Result, Tag, TagValue, MCTP_ADDR_ANY, MCTP_TAG_OWNER,
};

/* until we have these in libc... */
const AF_MCTP: libc::sa_family_t = 45;
#[repr(C)]
#[allow(non_camel_case_types)]
struct sockaddr_mctp {
    smctp_family: libc::sa_family_t,
    __smctp_pad0: u16,
    smctp_network: u32,
    smctp_addr: u8,
    smctp_type: u8,
    smctp_tag: u8,
    __smctp_pad1: u8,
}

/// Special value for Network ID: any network
pub const MCTP_NET_ANY: u32 = 0x00;

/// Address information for a socket
pub struct MctpSockAddr(sockaddr_mctp);

impl MctpSockAddr {
    /// Create a new address, for the given local EID, network, message type,
    /// and tag value.
    pub fn new(eid: u8, net: u32, typ: u8, tag: u8) -> Self {
        MctpSockAddr(sockaddr_mctp {
            smctp_family: AF_MCTP,
            __smctp_pad0: 0,
            smctp_network: net,
            smctp_addr: eid,
            smctp_type: typ,
            smctp_tag: tag,
            __smctp_pad1: 0,
        })
    }

    fn zero() -> Self {
        Self::new(0, MCTP_NET_ANY, 0, 0)
    }

    fn as_raw(&self) -> (*const libc::sockaddr, libc::socklen_t) {
        (
            &self.0 as *const sockaddr_mctp as *const libc::sockaddr,
            mem::size_of::<sockaddr_mctp>() as libc::socklen_t,
        )
    }

    fn as_raw_mut(&mut self) -> (*mut libc::sockaddr, libc::socklen_t) {
        (
            &mut self.0 as *mut sockaddr_mctp as *mut libc::sockaddr,
            mem::size_of::<sockaddr_mctp>() as libc::socklen_t,
        )
    }
}

impl fmt::Debug for MctpSockAddr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "McptSockAddr(family={}, net={}, addr={}, type={}, tag={})",
            self.0.smctp_family,
            self.0.smctp_network,
            self.0.smctp_addr,
            self.0.smctp_type,
            self.0.smctp_tag
        )
    }
}

/// Creates a `Tag` from a smctp_tag field.
fn tag_from_smctp(to: u8) -> Tag {
    let t = TagValue(to & !MCTP_TAG_OWNER);
    if to & MCTP_TAG_OWNER == 0 {
        Tag::Unowned(t)
    } else {
        Tag::Owned(t)
    }
}

/// Creates a `Tag` to a smctp_tag field.
fn tag_to_smctp(tag: &Tag) -> u8 {
    let to_bit = if tag.is_owner() { MCTP_TAG_OWNER } else { 0 };
    tag.tag().0 | to_bit
}

// helper for IO error construction
fn last_os_error() -> mctp::Error {
    mctp::Error::Io(Error::last_os_error())
}

/// MCTP socket object.
pub struct MctpSocket(RawFd);

impl Drop for MctpSocket {
    fn drop(&mut self) {
        unsafe { libc::close(self.0) };
    }
}

impl MctpSocket {
    /// Create a new MCTP socket. This can then be used for send/receive
    /// operations.
    pub fn new() -> Result<Self> {
        let rc = unsafe {
            libc::socket(
                AF_MCTP.into(),
                libc::SOCK_DGRAM | libc::SOCK_CLOEXEC,
                0,
            )
        };
        if rc < 0 {
            return Err(last_os_error());
        }
        Ok(MctpSocket(rc))
    }

    /// Blocking receive from a socket, into `buf`, returning a length
    /// and source address
    ///
    /// Essentially a wrapper around [libc::recvfrom], using MCTP-specific
    /// addressing.
    pub fn recvfrom(&self, buf: &mut [u8]) -> Result<(usize, MctpSockAddr)> {
        let mut addr = MctpSockAddr::zero();
        let (addr_ptr, mut addr_len) = addr.as_raw_mut();
        let buf_ptr = buf.as_mut_ptr() as *mut libc::c_void;
        let buf_len = buf.len() as libc::size_t;

        let rc = unsafe {
            libc::recvfrom(self.0, buf_ptr, buf_len, 0, addr_ptr, &mut addr_len)
        };

        if rc < 0 {
            Err(last_os_error())
        } else {
            Ok((rc as usize, addr))
        }
    }

    /// Blocking send to a socket, given a buffer and address, returning
    /// the number of bytes sent.
    ///
    /// Essentially a wrapper around [libc::sendto].
    pub fn sendto(&self, buf: &[u8], addr: &MctpSockAddr) -> Result<usize> {
        let (addr_ptr, addr_len) = addr.as_raw();
        let buf_ptr = buf.as_ptr() as *const libc::c_void;
        let buf_len = buf.len() as libc::size_t;

        let rc = unsafe {
            libc::sendto(self.0, buf_ptr, buf_len, 0, addr_ptr, addr_len)
        };

        if rc < 0 {
            Err(last_os_error())
        } else {
            Ok(rc as usize)
        }
    }

    /// Bind the socket to a local address.
    pub fn bind(&self, addr: &MctpSockAddr) -> Result<()> {
        let (addr_ptr, addr_len) = addr.as_raw();

        let rc = unsafe { libc::bind(self.0, addr_ptr, addr_len) };

        if rc < 0 {
            Err(last_os_error())
        } else {
            Ok(())
        }
    }

    /// Set the read timeout.
    ///
    /// A valid of `None` will have no timeout.
    pub fn set_read_timeout(&self, dur: Option<Duration>) -> Result<()> {
        // Avoid warnings about using time_t with musl. See comment in read_timeout().
        #![allow(deprecated)]

        let dur = dur.unwrap_or(Duration::ZERO);
        let tv = libc::timeval {
            tv_sec: dur.as_secs() as libc::time_t,
            tv_usec: dur.subsec_micros() as libc::suseconds_t,
        };
        let rc = unsafe {
            libc::setsockopt(
                self.0,
                libc::SOL_SOCKET,
                libc::SO_RCVTIMEO,
                (&tv as *const libc::timeval) as *const libc::c_void,
                std::mem::size_of::<libc::timeval>() as libc::socklen_t,
            )
        };

        if rc < 0 {
            Err(last_os_error())
        } else {
            Ok(())
        }
    }

    /// Retrieves the read timeout
    ///
    /// A value of `None` indicates no timeout.
    pub fn read_timeout(&self) -> Result<Option<Duration>> {
        // Avoid warnings about using time_t with musl. It is safe here since we are
        // only using it directly with libc, that should be compiled with the
        // same definitions as libc crate. https://github.com/rust-lang/libc/issues/1848
        #![allow(deprecated)]

        let mut tv = std::mem::MaybeUninit::<libc::timeval>::uninit();
        let mut tv_len =
            std::mem::size_of::<libc::timeval>() as libc::socklen_t;
        let rc = unsafe {
            libc::getsockopt(
                self.0,
                libc::SOL_SOCKET,
                libc::SO_RCVTIMEO,
                tv.as_mut_ptr() as *mut libc::c_void,
                &mut tv_len as *mut libc::socklen_t,
            )
        };

        if rc < 0 {
            Err(last_os_error())
        } else {
            let tv = unsafe { tv.assume_init() };
            if tv.tv_sec < 0 || tv.tv_usec < 0 {
                // Negative timeout from socket
                return Err(mctp::Error::Other);
            }

            if tv.tv_sec == 0 && tv.tv_usec == 0 {
                Ok(None)
            } else {
                Ok(Some(
                    Duration::from_secs(tv.tv_sec as u64)
                        + Duration::from_micros(tv.tv_usec as u64),
                ))
            }
        }
    }
}

impl std::os::fd::AsRawFd for MctpSocket {
    fn as_raw_fd(&self) -> RawFd {
        self.0
    }
}

/// Encapsulation of a remote endpoint: a socket and an Endpoint ID.
pub struct MctpLinuxReq {
    eid: Eid,
    net: u32,
    sock: MctpSocket,
    sent: bool,
}

impl MctpLinuxReq {
    /// Create a new `MctpLinuxReq` with EID `eid`
    pub fn new(eid: Eid, net: Option<u32>) -> Result<Self> {
        let net = net.unwrap_or(MCTP_NET_ANY);
        Ok(Self {
            eid,
            net,
            sock: MctpSocket::new()?,
            sent: false,
        })
    }

    /// Borrow the internal MCTP socket
    pub fn as_socket(&mut self) -> &mut MctpSocket {
        &mut self.sock
    }

    /// Returns the MCTP Linux network, or None for the default `MCTP_NET_ANY`
    pub fn net(&self) -> Option<u32> {
        if self.net == MCTP_NET_ANY {
            None
        } else {
            Some(self.net)
        }
    }
}

impl mctp::ReqChannel for MctpLinuxReq {
    /// Send a MCTP message
    ///
    /// Linux MCTP can also send a preallocated owned tag, but that is not
    /// yet supported in `MctpLinuxReq`.
    fn send_vectored(
        &mut self,
        typ: MsgType,
        ic: MsgIC,
        bufs: &[&[u8]],
    ) -> Result<()> {
        let typ_ic = mctp::encode_type_ic(typ, ic);
        let addr = MctpSockAddr::new(
            self.eid.0,
            self.net,
            typ_ic,
            mctp::MCTP_TAG_OWNER,
        );
        // TODO: implement sendmsg() with iovecs
        let concat = bufs
            .iter()
            .flat_map(|b| b.iter().cloned())
            .collect::<Vec<u8>>();
        self.sock.sendto(&concat, &addr)?;
        self.sent = true;
        Ok(())
    }

    fn recv<'f>(
        &mut self,
        buf: &'f mut [u8],
    ) -> Result<(MsgType, MsgIC, &'f mut [u8])> {
        if !self.sent {
            return Err(mctp::Error::BadArgument);
        }
        let (sz, addr) = self.sock.recvfrom(buf)?;
        let src = Eid(addr.0.smctp_addr);
        let (typ, ic) = mctp::decode_type_ic(addr.0.smctp_type);
        if src != self.eid {
            // Kernel gave us a message from a different sender?
            return Err(mctp::Error::Other);
        }
        Ok((typ, ic, &mut buf[..sz]))
    }

    fn remote_eid(&self) -> Eid {
        self.eid
    }
}

/// A Listener for Linux MCTP messages
pub struct MctpLinuxListener {
    sock: MctpSocket,
    net: u32,
    typ: MsgType,
}

impl MctpLinuxListener {
    /// Create a new `MctpLinuxListener`.
    ///
    /// This will listen for MCTP message type `typ`, on an optional
    /// Linux network `net`. `None` network defaults to `MCTP_NET_ANY`.
    pub fn new(typ: MsgType, net: Option<u32>) -> Result<Self> {
        let sock = MctpSocket::new()?;
        // Linux requires MCTP_ADDR_ANY for binds.
        let net = net.unwrap_or(MCTP_NET_ANY);
        let addr = MctpSockAddr::new(
            MCTP_ADDR_ANY.0,
            net,
            typ.0,
            mctp::MCTP_TAG_OWNER,
        );
        sock.bind(&addr)?;
        Ok(Self { sock, net, typ })
    }

    /// Borrow the internal MCTP socket
    pub fn as_socket(&mut self) -> &mut MctpSocket {
        &mut self.sock
    }

    /// Returns the MCTP Linux network, or None for the default `MCTP_NET_ANY`
    pub fn net(&self) -> Option<u32> {
        if self.net == MCTP_NET_ANY {
            None
        } else {
            Some(self.net)
        }
    }
}

impl mctp::Listener for MctpLinuxListener {
    type RespChannel<'a> = MctpLinuxResp<'a>;

    fn recv<'f>(
        &mut self,
        buf: &'f mut [u8],
    ) -> Result<(MsgType, MsgIC, &'f mut [u8], MctpLinuxResp<'_>)> {
        let (sz, addr) = self.sock.recvfrom(buf)?;
        let src = Eid(addr.0.smctp_addr);
        let (typ, ic) = mctp::decode_type_ic(addr.0.smctp_type);
        let tag = tag_from_smctp(addr.0.smctp_tag);
        if let Tag::Unowned(_) = tag {
            // bind() shouldn't give non-owned packets.
            return Err(mctp::Error::InternalError);
        }
        if typ != self.typ {
            // bind() should return the requested type
            return Err(mctp::Error::InternalError);
        }
        let ep = MctpLinuxResp {
            eid: src,
            tv: tag.tag(),
            listener: self,
            typ,
        };
        Ok((typ, ic, &mut buf[..sz], ep))
    }
}

/// A Linux MCTP Listener response channel
pub struct MctpLinuxResp<'a> {
    eid: Eid,
    // An unowned tag
    tv: TagValue,
    listener: &'a MctpLinuxListener,
    typ: MsgType,
}

impl mctp::RespChannel for MctpLinuxResp<'_> {
    type ReqChannel = MctpLinuxReq;

    /// Send a MCTP message
    ///
    /// Linux MCTP can also send a preallocated owned tag, but that is not
    /// yet supported in `MctpLinuxReq`.
    fn send_vectored(&mut self, ic: MsgIC, bufs: &[&[u8]]) -> Result<()> {
        let typ_ic = mctp::encode_type_ic(self.typ, ic);
        let tag = tag_to_smctp(&Tag::Unowned(self.tv));
        let addr =
            MctpSockAddr::new(self.eid.0, self.listener.net, typ_ic, tag);
        // TODO: implement sendmsg() with iovecs
        let concat = bufs
            .iter()
            .flat_map(|b| b.iter().cloned())
            .collect::<Vec<u8>>();
        self.listener.sock.sendto(&concat, &addr)?;
        Ok(())
    }

    fn remote_eid(&self) -> Eid {
        self.eid
    }

    fn req_channel(&self) -> Result<Self::ReqChannel> {
        MctpLinuxReq::new(self.eid, Some(self.listener.net))
    }
}

/// Helper for applications taking an MCTP address as an argument,
/// configuration, etc.
///
/// Address specifications can either be `<eid>`, or `<net>,<eid>`
///
/// EID may be either specified in decimal or hex, the latter requiring an '0x'
/// prefix.
///
/// Net must be in decimal.
///
/// If no network is specified, the default of MCTP_NET_ANY is used.
#[derive(Debug)]
pub struct MctpAddr {
    eid: Eid,
    net: Option<u32>,
}

impl std::str::FromStr for MctpAddr {
    type Err = String;

    fn from_str(s: &str) -> std::result::Result<MctpAddr, String> {
        let mut parts = s.split(',');

        let p1 = parts.next();
        let p2 = parts.next();

        let (net_str, eid_str) = match (p1, p2) {
            (Some(n), Some(e)) => (Some(n), e),
            (Some(e), None) => (None, e),
            _ => return Err("invalid MCTP address format".to_string()),
        };

        const HEX_PREFIX: &str = "0x";
        const HEX_PREFIX_LEN: usize = HEX_PREFIX.len();

        let eid = if eid_str.to_ascii_lowercase().starts_with(HEX_PREFIX) {
            u8::from_str_radix(&eid_str[HEX_PREFIX_LEN..], 16)
        } else {
            eid_str.parse()
        }
        .map_err(|e| e.to_string())?;
        let eid = Eid(eid);

        let net: Option<u32> = match net_str {
            Some(n) => Some(
                n.parse()
                    .map_err(|e: std::num::ParseIntError| e.to_string())?,
            ),
            None => None,
        };

        Ok(MctpAddr { net, eid })
    }
}

impl MctpAddr {
    /// Return the MCTP Endpoint ID for this address.
    pub fn eid(&self) -> Eid {
        self.eid
    }

    /// Return the MCTP Network ID for this address, defaulting to MCTP_NET_ANY
    /// if none was provided originally.
    pub fn net(&self) -> u32 {
        self.net.unwrap_or(MCTP_NET_ANY)
    }

    /// Create an `MctpLinuxReq` using the net & eid values in this address.
    pub fn create_endpoint(&self) -> Result<MctpLinuxReq> {
        MctpLinuxReq::new(self.eid, self.net)
    }

    /// Create an `MCTPListener`.
    ///
    /// The net of the listener comes from this address, with the MCTP
    /// message type as an argument.
    pub fn create_listener(&self, typ: MsgType) -> Result<MctpLinuxListener> {
        MctpLinuxListener::new(typ, self.net)
    }
}