xref: /spdk/lib/nvmf/transport.c (revision 179ed697b3c461d100e675915d074be717b7b9cc)
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 "transport.h"
37 
38 #include "spdk/log.h"
39 #include "spdk/nvmf.h"
40 #include "spdk/queue.h"
41 #include "spdk/util.h"
42 
43 #include "ctrlr.h"
44 #include "nvmf_internal.h"
45 #include "request.h"
46 
47 static const struct spdk_nvmf_transport_ops *const g_transport_ops[] = {
48 #ifdef SPDK_CONFIG_RDMA
49 	&spdk_nvmf_transport_rdma,
50 #endif
51 };
52 
53 #define NUM_TRANSPORTS (SPDK_COUNTOF(g_transport_ops))
54 
55 struct spdk_nvmf_transport *
56 spdk_nvmf_transport_create(struct spdk_nvmf_tgt *tgt,
57 			   enum spdk_nvme_transport_type type)
58 {
59 	size_t i;
60 	const struct spdk_nvmf_transport_ops *ops = NULL;
61 	struct spdk_nvmf_transport *transport;
62 
63 	for (i = 0; i != NUM_TRANSPORTS; i++) {
64 		if (g_transport_ops[i]->type == type) {
65 			ops = g_transport_ops[i];
66 			break;
67 		}
68 	}
69 
70 	if (!ops) {
71 		SPDK_ERRLOG("Transport type %s unavailable.\n",
72 			    spdk_nvme_transport_id_trtype_str(type));
73 		return NULL;
74 	}
75 
76 	transport = ops->create(tgt);
77 	if (!transport) {
78 		SPDK_ERRLOG("Unable to create new transport of type %s\n",
79 			    spdk_nvme_transport_id_trtype_str(type));
80 		return NULL;
81 	}
82 
83 	transport->ops = ops;
84 	transport->tgt = tgt;
85 
86 	return transport;
87 }
88 
89 int
90 spdk_nvmf_transport_destroy(struct spdk_nvmf_transport *transport)
91 {
92 	return transport->ops->destroy(transport);
93 }
94 
95 int
96 spdk_nvmf_transport_listen(struct spdk_nvmf_transport *transport,
97 			   const struct spdk_nvme_transport_id *trid)
98 {
99 	return transport->ops->listen(transport, trid);
100 }
101 
102 int
103 spdk_nvmf_transport_stop_listen(struct spdk_nvmf_transport *transport,
104 				const struct spdk_nvme_transport_id *trid)
105 {
106 	return transport->ops->stop_listen(transport, trid);
107 }
108 
109 void
110 spdk_nvmf_transport_accept(struct spdk_nvmf_transport *transport)
111 {
112 	transport->ops->accept(transport);
113 }
114 
115 void
116 spdk_nvmf_transport_listen_addr_discover(struct spdk_nvmf_transport *transport,
117 		struct spdk_nvmf_listen_addr *listen_addr,
118 		struct spdk_nvmf_discovery_log_page_entry *entry)
119 {
120 	transport->ops->listen_addr_discover(transport, listen_addr, entry);
121 }
122 
123 struct spdk_nvmf_poll_group *
124 spdk_nvmf_transport_poll_group_create(struct spdk_nvmf_transport *transport)
125 {
126 	struct spdk_nvmf_poll_group *group;
127 
128 	group = transport->ops->poll_group_create(transport);
129 	group->transport = transport;
130 
131 	return group;
132 }
133 
134 void
135 spdk_nvmf_transport_poll_group_destroy(struct spdk_nvmf_poll_group *group)
136 {
137 	group->transport->ops->poll_group_destroy(group);
138 }
139 
140 int
141 spdk_nvmf_transport_poll_group_add(struct spdk_nvmf_poll_group *group,
142 				   struct spdk_nvmf_qpair *qpair)
143 {
144 	if (qpair->transport) {
145 		assert(qpair->transport == group->transport);
146 		if (qpair->transport != group->transport) {
147 			return -1;
148 		}
149 	} else {
150 		qpair->transport = group->transport;
151 	}
152 
153 	return group->transport->ops->poll_group_add(group, qpair);
154 }
155 
156 int
157 spdk_nvmf_transport_poll_group_remove(struct spdk_nvmf_poll_group *group,
158 				      struct spdk_nvmf_qpair *qpair)
159 {
160 	return group->transport->ops->poll_group_remove(group, qpair);
161 }
162 
163 int
164 spdk_nvmf_transport_req_complete(struct spdk_nvmf_request *req)
165 {
166 	return req->qpair->transport->ops->req_complete(req);
167 }
168 
169 void
170 spdk_nvmf_transport_qpair_fini(struct spdk_nvmf_qpair *qpair)
171 {
172 	qpair->transport->ops->qpair_fini(qpair);
173 }
174 
175 int
176 spdk_nvmf_transport_qpair_poll(struct spdk_nvmf_qpair *qpair)
177 {
178 	return qpair->transport->ops->qpair_poll(qpair);
179 }
180 
181 bool
182 spdk_nvmf_transport_qpair_is_idle(struct spdk_nvmf_qpair *qpair)
183 {
184 	return qpair->transport->ops->qpair_is_idle(qpair);
185 }
186