xref: /spdk/test/env/env_dpdk_post_init/env_dpdk_post_init.c (revision 9889ab2dc80e40dae92dcef361d53dcba722043d)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "spdk/nvme.h"
37 #include "spdk/env.h"
38 #include "spdk/env_dpdk.h"
39 #include <rte_config.h>
40 #include <rte_eal.h>
41 
42 #define MAX_DEVS 64
43 
44 struct dev {
45 	struct spdk_nvme_ctrlr				*ctrlr;
46 	struct spdk_nvme_ns				*ns;
47 	struct spdk_nvme_qpair				*qpair;
48 	char						name[SPDK_NVMF_TRADDR_MAX_LEN + 1];
49 };
50 
51 static struct dev g_nvme_devs[MAX_DEVS];
52 static int g_num_devs = 0;
53 static int g_failed = 0;
54 
55 static bool
56 probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
57 	 struct spdk_nvme_ctrlr_opts *opts)
58 {
59 	printf("Attaching to %s\n", trid->traddr);
60 
61 	return true;
62 }
63 
64 static void
65 attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
66 	  struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
67 {
68 	struct dev *dev;
69 	uint32_t nsid;
70 
71 	/* add to dev list */
72 	dev = &g_nvme_devs[g_num_devs++];
73 	if (g_num_devs >= MAX_DEVS) {
74 		return;
75 	}
76 
77 	dev->ctrlr = ctrlr;
78 	nsid = spdk_nvme_ctrlr_get_first_active_ns(ctrlr);
79 	dev->ns = spdk_nvme_ctrlr_get_ns(ctrlr, nsid);
80 
81 	dev->qpair = spdk_nvme_ctrlr_alloc_io_qpair(ctrlr, NULL, 0);
82 	if (dev->qpair == NULL) {
83 		g_failed = 1;
84 		return;
85 	}
86 
87 	snprintf(dev->name, sizeof(dev->name), "%s",
88 		 trid->traddr);
89 
90 	printf("Attached to %s\n", dev->name);
91 }
92 
93 int
94 main(int argc, char **argv)
95 {
96 	int ret;
97 	int i;
98 
99 	printf("Starting DPDK initialization...\n");
100 	ret = rte_eal_init(argc, argv);
101 	if (ret < 0) {
102 		fprintf(stderr, "Failed to initialize DPDK\n");
103 		return -1;
104 	}
105 
106 	printf("Starting SPDK post initialization...\n");
107 	ret = spdk_env_dpdk_post_init();
108 	if (ret < 0) {
109 		fprintf(stderr, "Failed to initialize SPDK\n");
110 		return -1;
111 	}
112 
113 	printf("SPDK NVMe probe\n");
114 	if (spdk_nvme_probe(NULL, NULL, probe_cb, attach_cb, NULL) != 0) {
115 		fprintf(stderr, "spdk_nvme_probe() failed\n");
116 		return 1;
117 	}
118 
119 	printf("Cleaning up...\n");
120 	for (i = 0; i < g_num_devs; i++) {
121 		struct dev *dev = &g_nvme_devs[i];
122 		spdk_nvme_detach(dev->ctrlr);
123 	}
124 
125 	return g_failed;
126 }
127