2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
42 #include "iscsi_proto.h"
44 #ifdef ICL_KERNEL_PROXY
45 #include <sys/ioctl.h>
49 pdu_ahs_length(const struct pdu *pdu)
52 return (pdu->pdu_bhs->bhs_total_ahs_len * 4);
56 pdu_data_segment_length(const struct pdu *pdu)
60 len += pdu->pdu_bhs->bhs_data_segment_len[0];
62 len += pdu->pdu_bhs->bhs_data_segment_len[1];
64 len += pdu->pdu_bhs->bhs_data_segment_len[2];
70 pdu_set_data_segment_length(struct pdu *pdu, uint32_t len)
73 pdu->pdu_bhs->bhs_data_segment_len[2] = len;
74 pdu->pdu_bhs->bhs_data_segment_len[1] = len >> 8;
75 pdu->pdu_bhs->bhs_data_segment_len[0] = len >> 16;
79 pdu_new(struct connection *conn)
83 pdu = calloc(sizeof(*pdu), 1);
87 pdu->pdu_bhs = calloc(sizeof(*pdu->pdu_bhs), 1);
88 if (pdu->pdu_bhs == NULL)
91 pdu->pdu_connection = conn;
97 pdu_new_response(struct pdu *request)
100 return (pdu_new(request->pdu_connection));
103 #ifdef ICL_KERNEL_PROXY
106 pdu_receive_proxy(struct pdu *pdu)
108 struct iscsi_daemon_receive *idr;
112 assert(pdu->pdu_connection->conn_conf.isc_iser != 0);
114 pdu->pdu_data = malloc(ISCSI_MAX_DATA_SEGMENT_LENGTH);
115 if (pdu->pdu_data == NULL)
116 log_err(1, "malloc");
118 idr = calloc(1, sizeof(*idr));
120 log_err(1, "calloc");
122 idr->idr_session_id = pdu->pdu_connection->conn_session_id;
123 idr->idr_bhs = pdu->pdu_bhs;
124 idr->idr_data_segment_len = ISCSI_MAX_DATA_SEGMENT_LENGTH;
125 idr->idr_data_segment = pdu->pdu_data;
127 error = ioctl(pdu->pdu_connection->conn_iscsi_fd, ISCSIDRECEIVE, idr);
129 log_err(1, "ISCSIDRECEIVE");
131 len = pdu_ahs_length(pdu);
133 log_errx(1, "protocol error: non-empty AHS");
135 len = pdu_data_segment_length(pdu);
136 assert(len <= ISCSI_MAX_DATA_SEGMENT_LENGTH);
137 pdu->pdu_data_len = len;
143 pdu_send_proxy(struct pdu *pdu)
145 struct iscsi_daemon_send *ids;
148 assert(pdu->pdu_connection->conn_conf.isc_iser != 0);
150 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
152 ids = calloc(1, sizeof(*ids));
154 log_err(1, "calloc");
156 ids->ids_session_id = pdu->pdu_connection->conn_session_id;
157 ids->ids_bhs = pdu->pdu_bhs;
158 ids->ids_data_segment_len = pdu->pdu_data_len;
159 ids->ids_data_segment = pdu->pdu_data;
161 error = ioctl(pdu->pdu_connection->conn_iscsi_fd, ISCSIDSEND, ids);
163 log_err(1, "ISCSIDSEND");
168 #endif /* ICL_KERNEL_PROXY */
171 pdu_padding(const struct pdu *pdu)
174 if ((pdu->pdu_data_len % 4) != 0)
175 return (4 - (pdu->pdu_data_len % 4));
181 pdu_read(int fd, char *data, size_t len)
186 ret = read(fd, data, len);
189 log_errx(1, "exiting due to timeout");
192 log_errx(1, "read: connection lost");
199 pdu_receive(struct pdu *pdu)
204 #ifdef ICL_KERNEL_PROXY
205 if (pdu->pdu_connection->conn_conf.isc_iser != 0)
206 return (pdu_receive_proxy(pdu));
209 assert(pdu->pdu_connection->conn_conf.isc_iser == 0);
211 pdu_read(pdu->pdu_connection->conn_socket,
212 (char *)pdu->pdu_bhs, sizeof(*pdu->pdu_bhs));
214 len = pdu_ahs_length(pdu);
216 log_errx(1, "protocol error: non-empty AHS");
218 len = pdu_data_segment_length(pdu);
220 if (len > ISCSI_MAX_DATA_SEGMENT_LENGTH) {
221 log_errx(1, "protocol error: received PDU "
222 "with DataSegmentLength exceeding %d",
223 ISCSI_MAX_DATA_SEGMENT_LENGTH);
226 pdu->pdu_data_len = len;
227 pdu->pdu_data = malloc(len);
228 if (pdu->pdu_data == NULL)
229 log_err(1, "malloc");
231 pdu_read(pdu->pdu_connection->conn_socket,
232 (char *)pdu->pdu_data, pdu->pdu_data_len);
234 padding = pdu_padding(pdu);
236 assert(padding < sizeof(dummy));
237 pdu_read(pdu->pdu_connection->conn_socket,
238 (char *)dummy, padding);
244 pdu_send(struct pdu *pdu)
246 ssize_t ret, total_len;
252 #ifdef ICL_KERNEL_PROXY
253 if (pdu->pdu_connection->conn_conf.isc_iser != 0)
254 return (pdu_send_proxy(pdu));
257 assert(pdu->pdu_connection->conn_conf.isc_iser == 0);
259 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
260 iov[0].iov_base = pdu->pdu_bhs;
261 iov[0].iov_len = sizeof(*pdu->pdu_bhs);
262 total_len = iov[0].iov_len;
265 if (pdu->pdu_data_len > 0) {
266 iov[1].iov_base = pdu->pdu_data;
267 iov[1].iov_len = pdu->pdu_data_len;
268 total_len += iov[1].iov_len;
271 padding = pdu_padding(pdu);
273 assert(padding < sizeof(zero));
274 iov[2].iov_base = &zero;
275 iov[2].iov_len = padding;
276 total_len += iov[2].iov_len;
281 ret = writev(pdu->pdu_connection->conn_socket, iov, iovcnt);
284 log_errx(1, "exiting due to timeout");
285 log_err(1, "writev");
287 if (ret != total_len)
288 log_errx(1, "short write");
292 pdu_delete(struct pdu *pdu)