summaryrefslogtreecommitdiffstats
path: root/src/nvme/mi-mctp.c
blob: 86f5df68dacb366fc9479537f3c3d8130ce3d73b (plain)
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 * This file is part of libnvme.
 * Copyright (c) 2021 Code Construct Pty Ltd
 *
 * Authors: Jeremy Kerr <jk@codeconstruct.com.au>
 */

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <poll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>

#if HAVE_LINUX_MCTP_H
#include <linux/mctp.h>
#endif

#include <ccan/endian/endian.h>

#ifdef CONFIG_LIBSYSTEMD
#include <systemd/sd-event.h>
#include <systemd/sd-bus.h>
#include <systemd/sd-id128.h>

#define MCTP_DBUS_PATH "/xyz/openbmc_project/mctp"
#define MCTP_DBUS_IFACE "xyz.openbmc_project.MCTP"
#define MCTP_DBUS_IFACE_ENDPOINT "xyz.openbmc_project.MCTP.Endpoint"
#endif

#include "private.h"
#include "log.h"
#include "mi.h"


#if !defined(AF_MCTP)
#define AF_MCTP 45
#endif

#if !HAVE_LINUX_MCTP_H
/* As of kernel v5.15, these AF_MCTP-related definitions are provided by
 * linux/mctp.h. However, we provide a set here while that header percolates
 * through to standard includes.
 *
 * These were all introduced in the same version as AF_MCTP was defined,
 * so we can key off the presence of that.
 */

typedef __u8			mctp_eid_t;

struct mctp_addr {
	mctp_eid_t		s_addr;
};

struct sockaddr_mctp {
	unsigned short int	smctp_family;
	__u16			__smctp_pad0;
	unsigned int		smctp_network;
	struct mctp_addr	smctp_addr;
	__u8			smctp_type;
	__u8			smctp_tag;
	__u8			__smctp_pad1;
};

#define MCTP_NET_ANY		0x0

#define MCTP_ADDR_NULL		0x00
#define MCTP_ADDR_ANY		0xff

#define MCTP_TAG_MASK		0x07
#define MCTP_TAG_OWNER		0x08

#endif /* !AF_MCTP */

#define MCTP_TYPE_NVME		0x04
#define MCTP_TYPE_MIC		0x80

struct nvme_mi_transport_mctp {
	int	net;
	__u8	eid;
	int	sd;
};

static int ioctl_tag(int sd, unsigned long req, struct mctp_ioc_tag_ctl *ctl)
{
	return ioctl(sd, req, ctl);
}

static struct __mi_mctp_socket_ops ops = {
	socket,
	sendmsg,
	recvmsg,
	poll,
	ioctl_tag,
};

void __nvme_mi_mctp_set_ops(const struct __mi_mctp_socket_ops *newops)
{
	ops = *newops;
}
static const struct nvme_mi_transport nvme_mi_transport_mctp;

#ifdef SIOCMCTPALLOCTAG
static __u8 nvme_mi_mctp_tag_alloc(struct nvme_mi_ep *ep)
{
	struct nvme_mi_transport_mctp *mctp;
	struct mctp_ioc_tag_ctl ctl = { 0 };
	static bool logged;
	int rc;

	mctp = ep->transport_data;

	ctl.peer_addr = mctp->eid;

	errno = 0;
	rc = ops.ioctl_tag(mctp->sd, SIOCMCTPALLOCTAG, &ctl);
	if (rc) {
		if (!logged) {
			/* not necessarily fatal, just means we can't handle
			 * "more processing required" messages */
			nvme_msg(ep->root, LOG_INFO,
				 "System does not support explicit tag allocation\n");
			logged = true;
		}
		return MCTP_TAG_OWNER;
	}

	return ctl.tag;
}

static void nvme_mi_mctp_tag_drop(struct nvme_mi_ep *ep, __u8 tag)
{
	struct nvme_mi_transport_mctp *mctp;
	struct mctp_ioc_tag_ctl ctl = { 0 };

	mctp = ep->transport_data;

	if (!(tag & MCTP_TAG_PREALLOC))
		return;

	ctl.peer_addr = mctp->eid;
	ctl.tag = tag;

	ops.ioctl_tag(mctp->sd, SIOCMCTPDROPTAG, &ctl);
}

#else /*  !defined SIOMCTPTAGALLOC */

static __u8 nvme_mi_mctp_tag_alloc(struct nvme_mi_ep *ep)
{
	static bool logged;
	if (!logged) {
		nvme_msg(ep->root, LOG_INFO,
			 "Build does not support explicit tag allocation\n");
		logged = true;
	}
	return MCTP_TAG_OWNER;
}

static void nvme_mi_mctp_tag_drop(struct nvme_mi_ep *ep, __u8 tag)
{
}

#endif /* !defined SIOMCTPTAGALLOC */

struct nvme_mi_msg_resp_mpr {
	struct nvme_mi_msg_hdr hdr;
	__u8	status;
	__u8	rsvd0;
	__u16	mprt;
};

/* Check if this response was a More Processing Required response; if so,
 * populate the worst-case expected processing time, given in milliseconds.
 */
static bool nvme_mi_mctp_resp_is_mpr(struct nvme_mi_resp *resp, size_t len,
				     __le32 mic, unsigned int *mpr_time)
{
	struct nvme_mi_admin_resp_hdr *admin_msg;
	struct nvme_mi_msg_resp_mpr *msg;
	size_t clen;
	__u32 crc;

	/* We need at least the minimal header plus checksum */
	if (len < sizeof(*msg) + sizeof(mic))
		return false;

	msg = (struct nvme_mi_msg_resp_mpr *)resp->hdr;

	if (msg->status != NVME_MI_RESP_MPR)
		return false;

	/* Find and verify the MIC from the response, which may not be laid out
	 * in resp as we expect. We have to preserve resp->hdr_len and
	 * resp->data_len, as we will need them for the eventual reply message.
	 * Because of that, we can't use verify_resp_mic here.
	 *
	 * If the packet was at the expected response size, then mic will
	 * be set already; if not, find it within the header/data buffers.
	 */

	/* Devices may send a MPR response as a full-sized Admin response,
	 * rather than the minimal MI-only header. Allow this, but only if the
	 * type indicates admin, and the allocated response header is the
	 * correct size for an Admin response.
	 */
	if (((msg->hdr.nmp >> 3) & 0xf) == NVME_MI_MT_ADMIN &&
	    len == sizeof(*admin_msg) + sizeof(mic) &&
	    resp->hdr_len == sizeof(*admin_msg)) {
		if (resp->data_len)
			mic = *(__le32 *)resp->data;
	} else if (len == sizeof(*msg) + sizeof(mic)) {
		if (resp->hdr_len > sizeof(*msg))
			mic = *(__le32 *)(msg + 1);
		else if (resp->data_len)
			mic = *(__le32 *)(resp->data);
	} else {
		return false;
	}

	/* Since our response is just a header, we're guaranteed to have
	 * all data in resp->hdr. The response may be shorter than the expected
	 * header though, so clamp to len.
	 */
	len -= sizeof(mic);
	clen = len < resp->hdr_len ? len : resp->hdr_len;

	crc = ~nvme_mi_crc32_update(0xffffffff, resp->hdr, clen);
	if (le32_to_cpu(mic) != crc)
		return false;

	if (mpr_time)
		*mpr_time = cpu_to_le16(msg->mprt) * 100;

	return true;
}

static int nvme_mi_mctp_submit(struct nvme_mi_ep *ep,
			       struct nvme_mi_req *req,
			       struct nvme_mi_resp *resp)
{
	struct nvme_mi_transport_mctp *mctp;
	struct iovec req_iov[3], resp_iov[3];
	struct msghdr req_msg, resp_msg;
	int i, rc, errno_save, timeout;
	struct sockaddr_mctp addr;
	struct pollfd pollfds[1];
	unsigned int mpr_time;
	ssize_t len;
	__le32 mic;
	__u8 tag;

	if (ep->transport != &nvme_mi_transport_mctp) {
		errno = EINVAL;
		return -1;
	}

	/* we need enough space for at least a generic (/error) response */
	if (resp->hdr_len < sizeof(struct nvme_mi_msg_resp)) {
		errno = EINVAL;
		return -1;
	}

	mctp = ep->transport_data;
	tag = nvme_mi_mctp_tag_alloc(ep);

	memset(&addr, 0, sizeof(addr));
	addr.smctp_family = AF_MCTP;
	addr.smctp_network = mctp->net;
	addr.smctp_addr.s_addr = mctp->eid;
	addr.smctp_type = MCTP_TYPE_NVME | MCTP_TYPE_MIC;
	addr.smctp_tag = tag;

	i = 0;
	req_iov[i].iov_base = ((__u8 *)req->hdr) + 1;
	req_iov[i].iov_len = req->hdr_len - 1;
	i++;

	if (req->data_len) {
		req_iov[i].iov_base = req->data;
		req_iov[i].iov_len = req->data_len;
		i++;
	}

	mic = cpu_to_le32(req->mic);
	req_iov[i].iov_base = &mic;
	req_iov[i].iov_len = sizeof(mic);
	i++;

	memset(&req_msg, 0, sizeof(req_msg));
	req_msg.msg_name = &addr;
	req_msg.msg_namelen = sizeof(addr);
	req_msg.msg_iov = req_iov;
	req_msg.msg_iovlen = i;

	len = ops.sendmsg(mctp->sd, &req_msg, 0);
	if (len < 0) {
		errno_save = errno;
		nvme_msg(ep->root, LOG_ERR,
			 "Failure sending MCTP message: %m\n");
		errno = errno_save;
		rc = -1;
		goto out;
	}

	resp_iov[0].iov_base = ((__u8 *)resp->hdr) + 1;
	resp_iov[0].iov_len = resp->hdr_len - 1;

	resp_iov[1].iov_base = ((__u8 *)resp->data);
	resp_iov[1].iov_len = resp->data_len;

	resp_iov[2].iov_base = &mic;
	resp_iov[2].iov_len = sizeof(mic);

	memset(&resp_msg, 0, sizeof(resp_msg));
	resp_msg.msg_name = &addr;
	resp_msg.msg_namelen = sizeof(addr);
	resp_msg.msg_iov = resp_iov;
	resp_msg.msg_iovlen = 3;

	pollfds[0].fd = mctp->sd;
	pollfds[0].events = POLLIN;
	timeout = ep->timeout ?: -1;
retry:
	rc = ops.poll(pollfds, 1, timeout);
	if (rc < 0) {
		if (errno == EINTR)
			goto retry;
		errno_save = errno;
		nvme_msg(ep->root, LOG_ERR,
			 "Failed polling on MCTP socket: %m");
		errno = errno_save;
		return -1;
	}

	if (rc == 0) {
		nvme_msg(ep->root, LOG_DEBUG, "Timeout on MCTP socket");
		errno = ETIMEDOUT;
		return -1;
	}

	rc = -1;
	len = ops.recvmsg(mctp->sd, &resp_msg, MSG_DONTWAIT);

	if (len < 0) {
		errno_save = errno;
		nvme_msg(ep->root, LOG_ERR,
			 "Failure receiving MCTP message: %m\n");
		errno = errno_save;
		goto out;
	}


	if (len == 0) {
		nvme_msg(ep->root, LOG_WARNING, "No data from MCTP endpoint\n");
		errno = EIO;
		goto out;
	}

	/* Re-add the type byte, so we can work on aligned lengths from here */
	resp->hdr->type = MCTP_TYPE_NVME | MCTP_TYPE_MIC;
	len += 1;

	/* The smallest response data is 8 bytes: generic 4-byte message header
	 * plus four bytes of error data (excluding MIC). Ensure we have enough.
	 */
	if (len < 8 + sizeof(mic)) {
		nvme_msg(ep->root, LOG_ERR,
			 "Invalid MCTP response: too short (%zd bytes, needed %zd)\n",
			 len, 8 + sizeof(mic));
		errno = EPROTO;
		goto out;
	}

	/* We can't have header/payload data that isn't a multiple of 4 bytes */
	if (len & 0x3) {
		nvme_msg(ep->root, LOG_WARNING,
			 "Response message has unaligned length (%zd)!\n",
			 len);
		errno = EPROTO;
		goto out;
	}

	/* Check for a More Processing Required response. This is a slight
	 * layering violation, as we're pre-checking the MIC and inspecting
	 * header fields. However, we need to do this in the transport in order
	 * to keep the tag allocated and retry the recvmsg
	 */
	if (nvme_mi_mctp_resp_is_mpr(resp, len, mic, &mpr_time)) {
		nvme_msg(ep->root, LOG_DEBUG,
			 "Received More Processing Required, waiting for response\n");

		/* if the controller hasn't set MPRT, fall back to our command/
		 * response timeout, or the largest possible MPRT if none set */
		if (!mpr_time)
			mpr_time = ep->timeout ?: 0xffff;

		/* clamp to the endpoint max */
		if (ep->mprt_max && mpr_time > ep->mprt_max)
			mpr_time = ep->mprt_max;

		timeout = mpr_time;
		goto retry;
	}

	/* If we have a shorter than expected response, we need to find the
	 * MIC and the correct split between header & data. We know that the
	 * split is 4-byte aligned, so the MIC will be entirely within one
	 * of the iovecs.
	 */
	if (len == resp->hdr_len + resp->data_len + sizeof(mic)) {
		/* Common case: expected data length. Header, data and MIC
		 * are already laid-out correctly. Nothing to do. */

	} else if (len < resp->hdr_len + sizeof(mic)) {
		/* Response is smaller than the expected header. MIC is
		 * somewhere in the header buf */
		resp->hdr_len = len - sizeof(mic);
		resp->data_len = 0;
		memcpy(&mic, ((uint8_t *)resp->hdr) + resp->hdr_len,
		       sizeof(mic));

	} else {
		/* We have a full header, but data is truncated - possibly
		 * zero bytes. MIC is somewhere in the data buf */
		resp->data_len = len - resp->hdr_len - sizeof(mic);
		memcpy(&mic, ((uint8_t *)resp->data) + resp->data_len,
		       sizeof(mic));
	}

	resp->mic = le32_to_cpu(mic);

	rc = 0;

out:
	nvme_mi_mctp_tag_drop(ep, tag);

	return rc;
}

static void nvme_mi_mctp_close(struct nvme_mi_ep *ep)
{
	struct nvme_mi_transport_mctp *mctp;

	if (ep->transport != &nvme_mi_transport_mctp)
		return;

	mctp = ep->transport_data;
	close(mctp->sd);
	free(ep->transport_data);
}

static int nvme_mi_mctp_desc_ep(struct nvme_mi_ep *ep, char *buf, size_t len)
{
	struct nvme_mi_transport_mctp *mctp;

	if (ep->transport != &nvme_mi_transport_mctp) {
		errno = EINVAL;
		return -1;
	}

	mctp = ep->transport_data;

	snprintf(buf, len, "net %d eid %d", mctp->net, mctp->eid);

	return 0;
}

static const struct nvme_mi_transport nvme_mi_transport_mctp = {
	.name = "mctp",
	.mic_enabled = true,
	.submit = nvme_mi_mctp_submit,
	.close = nvme_mi_mctp_close,
	.desc_ep = nvme_mi_mctp_desc_ep,
};

nvme_mi_ep_t nvme_mi_open_mctp(nvme_root_t root, unsigned int netid, __u8 eid)
{
	struct nvme_mi_transport_mctp *mctp;
	struct nvme_mi_ep *ep;
	int errno_save;

	ep = nvme_mi_init_ep(root);
	if (!ep)
		return NULL;

	mctp = malloc(sizeof(*mctp));
	if (!mctp)
		goto err_free_ep;

	mctp->net = netid;
	mctp->eid = eid;

	mctp->sd = ops.socket(AF_MCTP, SOCK_DGRAM, 0);
	if (mctp->sd < 0)
		goto err_free_ep;

	ep->transport = &nvme_mi_transport_mctp;
	ep->transport_data = mctp;

	/* Assuming an i2c transport at 100kHz, smallest MTU (64+4). Given
	 * a worst-case clock stretch, and largest-sized packets, we can
	 * expect up to 1.6s per command/response pair. Allowing for a
	 * retry or two (handled by lower layers), 5s is a reasonable timeout.
	 */
	ep->timeout = 5000;

	return ep;

err_free_ep:
	errno_save = errno;
	free(ep);
	free(mctp);
	errno = errno_save;
	return NULL;
}

#ifdef CONFIG_LIBSYSTEMD

/* helper for handling dbus errors: D-Bus API returns a negtive errno on
 * failure; set errno and log.
 */
static void _dbus_err(nvme_root_t root, int rc, int line)
{
	nvme_msg(root, LOG_ERR, "MCTP D-Bus failed line %d: %s %d\n",
		line, strerror(-rc), rc);
	errno = -rc;
}

#define dbus_err(r, rc) _dbus_err(r, rc, __LINE__)

static int nvme_mi_mctp_add(nvme_root_t root, unsigned int netid, __u8 eid)
{
	nvme_mi_ep_t ep = NULL;

	/* ensure we don't already have an endpoint with the same net/eid. if
	 * we do, just skip, no need to re-add. */
	list_for_each(&root->endpoints, ep, root_entry) {
		if (ep->transport != &nvme_mi_transport_mctp) {
			continue;
		}
		const struct nvme_mi_transport_mctp *t = ep->transport_data;
		if (t->eid == eid && t->net == netid)
			return 0;
	}

	ep = nvme_mi_open_mctp(root, netid, eid);
	if (!ep)
		return -1;

	return 0;
}

/* We can't rely on sd_bus_message_enter_container() == 0 at the end of
   a dictionary (it returns -ENXIO) so we test separately */
static bool container_end(sd_bus_message *m)
{
	return sd_bus_message_peek_type(m, NULL, NULL) == 0;
}

static int handle_mctp_endpoint(nvme_root_t root, const char* objpath,
	sd_bus_message *m)
{
	bool have_eid = false, have_net = false, have_nvmemi = false;
	mctp_eid_t eid;
	int net;
	int rc;

	/* Iterate properties on this interface */
	while (!container_end(m)) {
		/* Enter property dict */
		rc = sd_bus_message_enter_container(m, 'a', "{sv}");
		if (rc < 0) {
			dbus_err(root, rc);
			return -1;
		}

		while (!container_end(m)) {
			char *propname = NULL;
			size_t sz;
			const uint8_t *types = NULL;
			/* Enter property item */
			rc = sd_bus_message_enter_container(m, 'e', "sv");
			if (rc < 0) {
				dbus_err(root, rc);
				return -1;
			}

			rc = sd_bus_message_read(m, "s", &propname);
			if (rc < 0) {
				dbus_err(root, rc);
				return -1;
			}

			if (strcmp(propname, "EID") == 0) {
				rc = sd_bus_message_read(m, "v", "y", &eid);
				have_eid = true;
			} else if (strcmp(propname, "NetworkId") == 0) {
				rc = sd_bus_message_read(m, "v", "i", &net);
				have_net = true;
			} else if (strcmp(propname, "SupportedMessageTypes") == 0) {
				sd_bus_message_enter_container(m, 'v', "ay");
				rc = sd_bus_message_read_array(m, 'y', (const void**)&types, &sz);
				if (rc >= 0)
					for (size_t s = 0; s < sz; s++)
						if (types[s] == MCTP_TYPE_NVME)
							have_nvmemi = true;
				sd_bus_message_exit_container(m);
			} else {
				rc = sd_bus_message_skip(m, "v");
			}

			if (rc < 0) {
				dbus_err(root, rc);
				return -1;
			}

			/* Exit prop item */
			rc = sd_bus_message_exit_container(m);
			if (rc < 0) {
				dbus_err(root, rc);
				return -1;
			}
		}

		/* Exit property dict */
		rc = sd_bus_message_exit_container(m);
		if (rc < 0) {
			dbus_err(root, rc);
			return -1;
		}
	}

	if (have_nvmemi) {
		if (!(have_eid && have_net)) {
			nvme_msg(root, LOG_ERR,
				 "Missing property for %s\n", objpath);
			errno = ENOENT;
			return -1;
		}
		rc = nvme_mi_mctp_add(root, net, eid);
		if (rc < 0) {
			int errno_save = errno;
			nvme_msg(root, LOG_ERR,
				 "Error adding net %d eid %d: %m\n", net, eid);
			errno = errno_save;
		}
	} else {
		/* Ignore other endpoints */
		rc = 0;
	}
	return rc;
}

static int handle_mctp_obj(nvme_root_t root, sd_bus_message *m)
{
	char *objpath = NULL;
	char *ifname = NULL;
	int rc;

	rc = sd_bus_message_read(m, "o", &objpath);
	if (rc < 0) {
		dbus_err(root, rc);
		return -1;
	}

	/* Enter response object: our array of (string, property dict)
	 * values */
	rc = sd_bus_message_enter_container(m, 'a', "{sa{sv}}");
	if (rc < 0) {
		dbus_err(root, rc);
		return -1;
	}


	/* for each interface */
	while (!container_end(m)) {
		/* Enter interface item */
		rc = sd_bus_message_enter_container(m, 'e', "sa{sv}");
		if (rc < 0) {
			dbus_err(root, rc);
			return -1;
		}

		rc = sd_bus_message_read(m, "s", &ifname);
		if (rc < 0) {
			dbus_err(root, rc);
			return -1;
		}

		if (!strcmp(ifname, MCTP_DBUS_IFACE_ENDPOINT)) {

			rc = handle_mctp_endpoint(root, objpath, m);
			if (rc < 0) {
				/* continue to next object */
			}
		} else {
			/* skip the interfaces we don't care about */
			rc = sd_bus_message_skip(m, "a{sv}");
			if (rc < 0) {
				dbus_err(root, rc);
				return -1;
			}
		}

		/* Exit interface item */
		rc = sd_bus_message_exit_container(m);
		if (rc < 0) {
			dbus_err(root, rc);
			return -1;
		}
	}

	/* Exit response object */
	rc = sd_bus_message_exit_container(m);
	if (rc < 0) {
		dbus_err(root, rc);
		return -1;
	}

	return 0;
}

nvme_root_t nvme_mi_scan_mctp(void)
{
	sd_bus *bus = NULL;
	sd_bus_message *resp = NULL;
	sd_bus_error berr = SD_BUS_ERROR_NULL;
	int rc, errno_save;
	nvme_root_t root;

	root = nvme_mi_create_root(NULL, DEFAULT_LOGLEVEL);
	if (!root) {
		errno = ENOMEM;
		rc = -1;
		goto out;
	}

	rc = sd_bus_default_system(&bus);
	if (rc < 0) {
		nvme_msg(root, LOG_ERR, "Failed opening D-Bus: %s\n",
			 strerror(-rc));
		errno = -rc;
		rc = -1;
		goto out;
	}

	rc = sd_bus_call_method(bus,
			       MCTP_DBUS_IFACE,
			       MCTP_DBUS_PATH,
			       "org.freedesktop.DBus.ObjectManager",
			       "GetManagedObjects",
			       &berr,
			       &resp,
			       "");
	if (rc < 0) {
		nvme_msg(root, LOG_ERR, "Failed querying MCTP D-Bus: %s (%s)\n",
			 berr.message, berr.name);
		errno = -rc;
		rc = -1;
		goto out;
	}

	rc = sd_bus_message_enter_container(resp, 'a', "{oa{sa{sv}}}");
	if (rc != 1) {
		dbus_err(root, rc);
		if (rc == 0)
			errno = EPROTO;
		rc = -1;
		goto out;
	}

	/* Iterate over all managed objects */
	while (!container_end(resp)) {
		rc = sd_bus_message_enter_container(resp, 'e', "oa{sa{sv}}");
		if (rc < 0) {
			dbus_err(root, rc);
			rc = -1;
			goto out;
		}

		handle_mctp_obj(root, resp);

		rc = sd_bus_message_exit_container(resp);
		if (rc < 0) {
			dbus_err(root, rc);
			rc = -1;
			goto out;
		}
	}

	rc = sd_bus_message_exit_container(resp);
	if (rc < 0) {
		dbus_err(root, rc);
		rc = -1;
		goto out;
	}
	rc = 0;

out:
	errno_save = errno;
	sd_bus_error_free(&berr);
	sd_bus_message_unref(resp);
	sd_bus_unref(bus);

	if (rc < 0) {
		if (root) {
			nvme_mi_free_root(root);
		}
		errno = errno_save;
		root = NULL;
	}
	return root;
}

#else /* CONFIG_LIBSYSTEMD */

nvme_root_t nvme_mi_scan_mctp(void)
{
	return NULL;
}

#endif /* CONFIG_LIBSYSTEMD */