xref: /spdk/lib/nvmf/nvmf.c (revision b58a5d73ef70b9458ef589f5858c2a72c1f8e47c)
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 <arpa/inet.h>
35 
36 #include "spdk/conf.h"
37 #include "spdk/nvmf.h"
38 #include "spdk/trace.h"
39 
40 #include "spdk_internal/log.h"
41 
42 #include "subsystem.h"
43 #include "transport.h"
44 
45 SPDK_LOG_REGISTER_TRACE_FLAG("nvmf", SPDK_TRACE_NVMF)
46 
47 #define MAX_SUBSYSTEMS 4
48 
49 struct spdk_nvmf_tgt g_nvmf_tgt;
50 
51 int
52 spdk_nvmf_tgt_init(uint16_t max_queue_depth, uint16_t max_queues_per_sess,
53 		   uint32_t in_capsule_data_size, uint32_t max_io_size)
54 {
55 	int rc;
56 
57 	g_nvmf_tgt.max_queues_per_session = max_queues_per_sess;
58 	g_nvmf_tgt.max_queue_depth = max_queue_depth;
59 	g_nvmf_tgt.in_capsule_data_size = in_capsule_data_size;
60 	g_nvmf_tgt.max_io_size = max_io_size;
61 	g_nvmf_tgt.discovery_genctr = 0;
62 	g_nvmf_tgt.discovery_log_page = NULL;
63 	g_nvmf_tgt.discovery_log_page_size = 0;
64 	TAILQ_INIT(&g_nvmf_tgt.subsystems);
65 
66 	SPDK_TRACELOG(SPDK_TRACE_NVMF, "Max Queues Per Session: %d\n", max_queues_per_sess);
67 	SPDK_TRACELOG(SPDK_TRACE_NVMF, "Max Queue Depth: %d\n", max_queue_depth);
68 	SPDK_TRACELOG(SPDK_TRACE_NVMF, "Max In Capsule Data: %d bytes\n", in_capsule_data_size);
69 	SPDK_TRACELOG(SPDK_TRACE_NVMF, "Max I/O Size: %d bytes\n", max_io_size);
70 
71 	rc = spdk_nvmf_transport_init();
72 	if (rc <= 0) {
73 		SPDK_ERRLOG("Transport initialization failed\n");
74 		return -1;
75 	}
76 
77 	return 0;
78 }
79 
80 int
81 spdk_nvmf_tgt_fini(void)
82 {
83 	spdk_nvmf_transport_fini();
84 
85 	return 0;
86 }
87 
88 struct spdk_nvmf_listen_addr *
89 spdk_nvmf_listen_addr_create(const char *trname, const char *traddr, const char *trsvcid)
90 {
91 	struct spdk_nvmf_listen_addr *listen_addr;
92 	const struct spdk_nvmf_transport *transport;
93 
94 	transport = spdk_nvmf_transport_get(trname);
95 	if (!transport) {
96 		return NULL;
97 	}
98 
99 	listen_addr = calloc(1, sizeof(*listen_addr));
100 	if (!listen_addr) {
101 		return NULL;
102 	}
103 
104 	listen_addr->traddr = strdup(traddr);
105 	if (!listen_addr->traddr) {
106 		free(listen_addr);
107 		return NULL;
108 	}
109 
110 	listen_addr->trsvcid = strdup(trsvcid);
111 	if (!listen_addr->trsvcid) {
112 		free(listen_addr->traddr);
113 		free(listen_addr);
114 		return NULL;
115 	}
116 
117 	listen_addr->trname = strdup(trname);
118 	if (!listen_addr->trname) {
119 		free(listen_addr->traddr);
120 		free(listen_addr->trsvcid);
121 		free(listen_addr);
122 		return NULL;
123 	}
124 
125 	return listen_addr;
126 }
127 
128 void
129 spdk_nvmf_listen_addr_destroy(struct spdk_nvmf_listen_addr *addr)
130 {
131 	const struct spdk_nvmf_transport *transport;
132 
133 	transport = spdk_nvmf_transport_get(addr->trname);
134 	assert(transport != NULL);
135 	transport->listen_addr_remove(addr);
136 
137 	spdk_nvmf_listen_addr_cleanup(addr);
138 }
139 
140 void
141 spdk_nvmf_listen_addr_cleanup(struct spdk_nvmf_listen_addr *addr)
142 {
143 	free(addr->trname);
144 	free(addr->trsvcid);
145 	free(addr->traddr);
146 	free(addr);
147 }
148 
149 SPDK_TRACE_REGISTER_FN(nvmf_trace)
150 {
151 	spdk_trace_register_object(OBJECT_NVMF_IO, 'r');
152 	spdk_trace_register_description("NVMF_IO_START", "", TRACE_NVMF_IO_START,
153 					OWNER_NONE, OBJECT_NVMF_IO, 1, 0, 0, "");
154 	spdk_trace_register_description("NVMF_RDMA_READ_START", "", TRACE_RDMA_READ_START,
155 					OWNER_NONE, OBJECT_NVMF_IO, 0, 0, 0, "");
156 	spdk_trace_register_description("NVMF_RDMA_WRITE_START", "", TRACE_RDMA_WRITE_START,
157 					OWNER_NONE, OBJECT_NVMF_IO, 0, 0, 0, "");
158 	spdk_trace_register_description("NVMF_RDMA_READ_COMPLETE", "", TRACE_RDMA_READ_COMPLETE,
159 					OWNER_NONE, OBJECT_NVMF_IO, 0, 0, 0, "");
160 	spdk_trace_register_description("NVMF_RDMA_WRITE_COMPLETE", "", TRACE_RDMA_WRITE_COMPLETE,
161 					OWNER_NONE, OBJECT_NVMF_IO, 0, 0, 0, "");
162 	spdk_trace_register_description("NVMF_LIB_READ_START", "", TRACE_NVMF_LIB_READ_START,
163 					OWNER_NONE, OBJECT_NVMF_IO, 0, 0, 0, "");
164 	spdk_trace_register_description("NVMF_LIB_WRITE_START", "", TRACE_NVMF_LIB_WRITE_START,
165 					OWNER_NONE, OBJECT_NVMF_IO, 0, 0, 0, "");
166 	spdk_trace_register_description("NVMF_LIB_COMPLETE", "", TRACE_NVMF_LIB_COMPLETE,
167 					OWNER_NONE, OBJECT_NVMF_IO, 0, 0, 0, "");
168 	spdk_trace_register_description("NVMF_IO_COMPLETION_DONE", "", TRACE_NVMF_IO_COMPLETE,
169 					OWNER_NONE, OBJECT_NVMF_IO, 0, 0, 0, "");
170 }
171