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
32 #include <sys/types.h>
41 #include "iscsi_proto.h"
43 #ifdef ICL_KERNEL_PROXY
44 #include <sys/ioctl.h>
48 pdu_ahs_length(const struct pdu *pdu)
51 return (pdu->pdu_bhs->bhs_total_ahs_len * 4);
55 pdu_data_segment_length(const struct pdu *pdu)
59 len += pdu->pdu_bhs->bhs_data_segment_len[0];
61 len += pdu->pdu_bhs->bhs_data_segment_len[1];
63 len += pdu->pdu_bhs->bhs_data_segment_len[2];
69 pdu_set_data_segment_length(struct pdu *pdu, uint32_t len)
72 pdu->pdu_bhs->bhs_data_segment_len[2] = len;
73 pdu->pdu_bhs->bhs_data_segment_len[1] = len >> 8;
74 pdu->pdu_bhs->bhs_data_segment_len[0] = len >> 16;
78 pdu_new(struct connection *conn)
82 pdu = calloc(sizeof(*pdu), 1);
86 pdu->pdu_bhs = calloc(sizeof(*pdu->pdu_bhs), 1);
87 if (pdu->pdu_bhs == NULL)
90 pdu->pdu_connection = conn;
96 pdu_new_response(struct pdu *request)
99 return (pdu_new(request->pdu_connection));
102 #ifdef ICL_KERNEL_PROXY
105 pdu_receive(struct pdu *pdu)
111 len = pdu_ahs_length(pdu);
113 log_errx(1, "protocol error: non-empty AHS");
115 len = pdu_data_segment_length(pdu);
116 assert(len <= MAX_DATA_SEGMENT_LENGTH);
117 pdu->pdu_data_len = len;
121 pdu_send(struct pdu *pdu)
124 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
128 #else /* !ICL_KERNEL_PROXY */
131 pdu_padding(const struct pdu *pdu)
134 if ((pdu->pdu_data_len % 4) != 0)
135 return (4 - (pdu->pdu_data_len % 4));
141 pdu_read(int fd, char *data, size_t len)
146 ret = read(fd, data, len);
149 log_errx(1, "exiting due to timeout");
152 log_errx(1, "read: connection lost");
159 pdu_receive(struct pdu *pdu)
164 pdu_read(pdu->pdu_connection->conn_socket,
165 (char *)pdu->pdu_bhs, sizeof(*pdu->pdu_bhs));
167 len = pdu_ahs_length(pdu);
169 log_errx(1, "protocol error: non-empty AHS");
171 len = pdu_data_segment_length(pdu);
173 if (len > MAX_DATA_SEGMENT_LENGTH) {
174 log_errx(1, "protocol error: received PDU "
175 "with DataSegmentLength exceeding %d",
176 MAX_DATA_SEGMENT_LENGTH);
179 pdu->pdu_data_len = len;
180 pdu->pdu_data = malloc(len);
181 if (pdu->pdu_data == NULL)
182 log_err(1, "malloc");
184 pdu_read(pdu->pdu_connection->conn_socket,
185 (char *)pdu->pdu_data, pdu->pdu_data_len);
187 padding = pdu_padding(pdu);
189 assert(padding < sizeof(dummy));
190 pdu_read(pdu->pdu_connection->conn_socket,
191 (char *)dummy, padding);
197 pdu_send(struct pdu *pdu)
199 ssize_t ret, total_len;
205 pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
206 iov[0].iov_base = pdu->pdu_bhs;
207 iov[0].iov_len = sizeof(*pdu->pdu_bhs);
208 total_len = iov[0].iov_len;
211 if (pdu->pdu_data_len > 0) {
212 iov[1].iov_base = pdu->pdu_data;
213 iov[1].iov_len = pdu->pdu_data_len;
214 total_len += iov[1].iov_len;
217 padding = pdu_padding(pdu);
219 assert(padding < sizeof(zero));
220 iov[2].iov_base = &zero;
221 iov[2].iov_len = padding;
222 total_len += iov[2].iov_len;
227 ret = writev(pdu->pdu_connection->conn_socket, iov, iovcnt);
230 log_errx(1, "exiting due to timeout");
231 log_err(1, "writev");
233 if (ret != total_len)
234 log_errx(1, "short write");
237 #endif /* !ICL_KERNEL_PROXY */
240 pdu_delete(struct pdu *pdu)