AF_MCTP is the socket address family used by the Linux kernel to expose the Management Component Transport Protocol (MCTP) to user space. Based on the Linux kernel docs, Management Component Transport Protocol (MCTP), this post covers how Linux MCTP sockets work, how the mctp utility and mctpd daemon differ, and how libpldm and OpenBMC’s pldmd handle PLDM messages.

Summary

The Linux kernel provides MCTP as a network stack where applications open sockets via socket(AF_MCTP, SOCK_DGRAM, 0) and specify an EID and message type to exchange messages. The mctp command-line utility queries and configures links, addresses, and routes, while the mctpd daemon acts as the bus owner to assign EIDs to remote endpoints and manage routes and neighbour-table entries. The libpldm library encodes and decodes PLDM messages, and pldmd runs PLDM as a service in OpenBMC. In this post’s test environment—where the kernel does not provide MCTP and returns Errno 97—we verify building the user-space tools (mctp, libpldm) and encoding a PLDM Base GetPLDMTypes request into bytes.

How does Linux provide MCTP via AF_MCTP?

One-line answer: The Linux kernel provides MCTP through the AF_MCTP (PF_MCTP) address family and SOCK_DGRAM sockets, using struct sockaddr_mctp to specify the network, EID, message type, and tag.

According to the Linux kernel MCTP documentation, on a kernel with MCTP support, applications use the AF_MCTP (or PF_MCTP) address family, and only SOCK_DGRAM sockets are supported. A socket is opened with protocol 0 via int sd = socket(AF_MCTP, SOCK_DGRAM, 0);. Physical interface drivers correspond to the “bindings” of DSP0236, and each interface (“link”) is an instance of a binding.

The socket address structure struct sockaddr_mctp contains smctp_family, smctp_network, smctp_addr, smctp_type, and smctp_tag, along with wildcard constants MCTP_NET_ANY (0x0) and MCTP_ADDR_ANY (0xff). A receiving (request-handling) socket calls bind(). Below is the bind() example from the kernel documentation (kernel documentation example, not run here):

struct sockaddr_mctp addr;

addr.smctp_family = AF_MCTP;
addr.smctp_network = MCTP_NET_ANY;
addr.smctp_addr.s_addr = MCTP_ADDR_ANY;
addr.smctp_type = MCTP_TYPE_PLDM;
addr.smctp_tag = MCTP_TAG_OWNER;

int rc = bind(sd, (struct sockaddr *)&addr, sizeof(addr));

A bound socket only receives messages with the TO (Tag Owner) bit set, meaning incoming requests; for bind(), the only valid smctp_tag value is MCTP_TAG_OWNER. Sending uses sendto(), sendmsg(), or send(), where the network and address fields define the remote address. When sending with MCTP_TAG_OWNER, the kernel generates a tag, and if no tag can be allocated the call reports EAGAIN. The application places the message type byte as the first byte of the buffer.

For the meaning of MCTP header fields, EIDs, and bindings, see “What are MCTP and PLDM? Server device management and how MCTP packets, EIDs and bindings work” (previous in series).

Linux AF_MCTP socket, mctp, mctpd, libpldm, and pldmd layers

How do the mctp command and mctpd differ?

One-line answer: mctp is a command-line utility similar to iproute2’s ip for querying and managing the kernel MCTP stack, whereas mctpd is an MCTP control protocol daemon that acts as a bus owner to assign EIDs and manage routes and neighbour-table entries.

According to the CodeConstruct/mctp README, mctp is a small command-line utility to query and manage the state of the kernel MCTP stack, similar to iproute2’s ip. The subcommands listed in the README usage are link, address, route, neigh, and monitor. Below are syntax lines from the README (README usage text, not run output):

mctp link set <ifname> [up|down] [mtu <mtu>] [network <net>] [bus-owner <physaddr>]
mctp address add <eid> dev <ifname>
mctp route add <eid>[-<eid>] via <ifname> [mtu <mtu>]

By contrast, mctpd is a daemon implementing the MCTP control protocol and is needed for the local host to act as bus owner. Its main job is assigning EIDs to remote endpoints and managing the resulting routes and neighbour-table entries. mctpd should run as a system service after interfaces are enabled and local addresses are assigned; it exposes a D-Bus interface and reads configuration from /etc/mctpd.conf by default. The project builds with Meson, and passing -Dtests=false skips test dependencies.

Where is PLDM handled?

One-line answer: The libpldm library handles encoding and decoding PLDM messages, while pldmd in the openbmc/pldm repository is the daemon that handles PLDM as a service in OpenBMC.

openbmc/libpldm is a PLDM encode/decode library. In its base.h header, the GetPLDMTypes command code is 0x4 and PLDM_GET_TYPES_REQ_BYTES is 0.

In the openbmc/pldm repository, meson.build builds an executable named pldmd (the OpenBMC PLDM daemon that handles PLDM as a service), and its README shows systemctl restart pldmd. For details on each PLDM type, see “What are PLDM types? Base, monitoring (PDRs and sensors), firmware update and RDE” (previous in series).

What was verified in this post’s test environment?

One-line answer: Because the test machine’s kernel does not provide AF_MCTP and fails with Errno 97 on socket creation, no packets were sent or received; verification covered building the user-space tools (mctp, libpldm) and encoding a GetPLDMTypes request into bytes.

Without changing kernel settings, we first checked whether the test machine’s kernel provides AF_MCTP. Attempting to create a SOCK_DGRAM socket in python3 using constant 45 for AF_MCTP produced the following result (the ... line indicates omitted traceback lines):

$ python3 -c 'import socket; socket.socket(45, socket.SOCK_DGRAM)'
...
OSError: [Errno 97] Address family not supported by protocol

Because socket() failed with Errno 97 (Address family not supported by protocol), the running kernel does not provide AF_MCTP. Nothing was sent or received, no link, address, or route was configured, and neither mctpd nor pldmd was run.

Next, we cloned and built the user-space repository CodeConstruct/mctp (commit f3b843d) with -Dtests=false (the meson setup output is discarded and only the last ninja line is kept with tail -1):

$ git clone --depth 1 https://github.com/CodeConstruct/mctp src-mctp
$ (cd src-mctp && rm -rf build && meson setup build -Dtests=false >/dev/null && ninja -C build | tail -1)
[22/22] Linking target mctp
$ ls src-mctp/build | grep -E "^mctp"
mctp
mctp-bench
mctp-bench.p
mctp-client
mctp-client.p
mctp-echo
mctp-echo.p
mctp-req
mctp-req.p
mctp.p

mctpd is not in the build output because the Meson log reports Run-time dependency libsystemd found: NO (tried pkg-config and cmake) and the project’s meson.build only builds mctpd when libsystemd is found.

We then cloned and built openbmc/libpldm (commit be4f63e) the same way:

$ git clone --depth 1 https://github.com/openbmc/libpldm src-libpldm
$ (cd src-libpldm && rm -rf build && meson setup build -Dtests=false >/dev/null && ninja -C build | tail -1)
[33/33] Linking target bindings/cpp/libpldm++.so.0.18.0

Using libpldm, we wrote a small C program (pldm_encode.c) whose comment notes that it encodes a PLDM Base (Type 0) GetPLDMTypes request message and prints the resulting bytes without any MCTP transmission:

/* libpldm로 PLDM Base(Type 0) "GetPLDMTypes" 요청 메시지를 인코딩해 바이트를 출력 (MCTP 전송 없음, 인코딩만) */
#include <libpldm/base.h>
#include <stdio.h>
#include <stdint.h>
int main(void) {
    uint8_t buf[sizeof(struct pldm_msg_hdr) + PLDM_GET_TYPES_REQ_BYTES];
    struct pldm_msg *msg = (struct pldm_msg *)buf;
    int rc = encode_get_types_req(1 /* instance id */, msg);
    printf("encode_get_types_req rc=%d len=%zu bytes:", rc, sizeof(buf));
    for (size_t i = 0; i < sizeof(buf); i++) printf(" %02x", buf[i]);
    printf("\n");
    printf("request=%u instance_id=%u header_ver=%u pldm_type=%u command=0x%02x\n",
           msg->hdr.request, msg->hdr.instance_id, msg->hdr.header_ver, msg->hdr.type, msg->hdr.command);
    return rc;
}

Building and running pldm_encode.c produced the following output:

$ gcc -std=c11 -Wall -Isrc-libpldm/include -Isrc-libpldm/build/include pldm_encode.c -Lsrc-libpldm/build/src -lpldm -o pldm_encode
$ env LD_LIBRARY_PATH=src-libpldm/build/src ./pldm_encode
encode_get_types_req rc=0 len=3 bytes: 81 00 04
request=1 instance_id=1 header_ver=0 pldm_type=0 command=0x04

As the second output line states, the encoded 3-byte message (81 00 04) has header fields request=1, instance_id=1, header_ver=0, pldm_type=0, and command=0x04. The message was only encoded into the buffer and was not sent over MCTP.

What else is needed to communicate with a real MCTP device?

One-line answer: On a kernel with MCTP support and a physical binding driver, you bring the link up and configure the local EID and route with mctp, run mctpd as a system service, and send the encoded PLDM request bytes over an AF_MCTP socket.

On a kernel with MCTP support—built with MCTP enabled plus a binding driver such as I2C—you first bring the link up and configure a local address and route using mctp. Below are example commands (not run here, no output):

mctp link set <ifname> up
mctp address add <eid> dev <ifname>
mctp route add <eid> via <ifname>

After the link and local address are configured, you run mctpd as a system service so that it acts as bus owner and assigns EIDs to remote endpoints. An application can then open an AF_MCTP socket and send a PLDM request—such as the encoded GetPLDMTypes bytes with the message type byte placed first in the buffer. This flow assumes a kernel with MCTP support and was not run in this post’s test environment.

Flow to bring up MCTP link, address, and route, run mctpd, and send PLDM over AF_MCTP

What are the frequently asked questions?

One-line answer: These questions cover how to check whether the running kernel provides MCTP, privilege requirements for configuring links and addresses, and what was tested from Python.

How do you check whether the kernel provides MCTP?

If socket(AF_MCTP, SOCK_DGRAM, 0) fails with Errno 97 (Address family not supported by protocol), the running kernel does not provide MCTP—as observed in this post’s test.

Are root privileges required?

In general, changing network link, address, and route settings requires administrator privileges.

Can you try this from Python?

This post only tried creating the socket in Python with constant 45 for AF_MCTP, and it failed with Errno 97 on this kernel; beyond that, it is untested here.

One-line answer: These previous parts of the series cover MCTP packet headers, EIDs, and bindings, as well as PLDM types and message flows.

What are the sources?

One-line answer: The sources are the Linux kernel MCTP documentation and the CodeConstruct/mctp, openbmc/libpldm, and openbmc/pldm repositories.