2 * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
3 * By downloading, copying, installing or using the software you agree
4 * to this license. If you do not agree to this license, do not
5 * download, install, copy or use the software.
7 * Intel License Agreement
9 * Copyright (c) 2000, Intel Corporation
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * -Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
19 * -Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the
24 * -The name of Intel Corporation may not be used to endorse or
25 * promote products derived from this software without specific prior
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
35 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
38 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 #ifdef HAVE_SYS_SOCKET_H
70 #include <sys/socket.h>
85 #define ISCSI_HTONLL6(x) (uint64_t) \
86 ( ((uint64_t)( ((uint64_t)(x) & (uint64_t)0x0000ff0000000000uLL) >> 40)) \
87 | ((uint64_t)( ((uint64_t)(x) & (uint64_t)0x000000ff00000000uLL) >> 24)) \
88 | ((uint64_t)( ((uint64_t)(x) & (uint64_t)0x00000000ff000000uLL) >> 8)) \
89 | ((uint64_t)( ((uint64_t)(x) & (uint64_t)0x0000000000ff0000uLL) << 8)) \
90 | ((uint64_t)( ((uint64_t)(x) & (uint64_t)0x000000000000ff00uLL) << 24)) \
91 | ((uint64_t)( ((uint64_t)(x) & (uint64_t)0x00000000000000ffuLL) << 40)))
93 #define ISCSI_NTOHLL6(x) (uint64_t) \
94 ( ((uint64_t)( ((uint64_t)(x) & (uint64_t)0x0000ff0000000000uLL) >> 40)) \
95 | ((uint64_t)( ((uint64_t)(x) & (uint64_t)0x000000ff00000000uLL) >> 24)) \
96 | ((uint64_t)( ((uint64_t)(x) & (uint64_t)0x00000000ff000000uLL) >> 8)) \
97 | ((uint64_t)( ((uint64_t)(x) & (uint64_t)0x0000000000ff0000uLL) << 8)) \
98 | ((uint64_t)( ((uint64_t)(x) & (uint64_t)0x000000000000ff00uLL) << 24)) \
99 | ((uint64_t)( ((uint64_t)(x) & (uint64_t)0x00000000000000ffuLL) << 40)))
105 #define TRACE_NET_DEBUG 0x00000001
106 #define TRACE_NET_BUFF 0x00000002
107 #define TRACE_NET_IOV 0x00000004
108 #define TRACE_NET_ALL (TRACE_NET_DEBUG|TRACE_NET_BUFF|TRACE_NET_IOV)
110 #define TRACE_ISCSI_DEBUG 0x00000010
111 #define TRACE_ISCSI_CMD 0x00000020
112 #define TRACE_ISCSI_ARGS 0x00000040
113 #define TRACE_ISCSI_PARAM 0x00000080
114 #define TRACE_ISCSI_ALL (TRACE_ISCSI_DEBUG|TRACE_ISCSI_ARGS|TRACE_ISCSI_PARAM|TRACE_ISCSI_CMD)
116 #define TRACE_SCSI_DEBUG 0x00000100
117 #define TRACE_SCSI_CMD 0x00000200
118 #define TRACE_SCSI_DATA 0x00000400
119 #define TRACE_SCSI_ARGS 0x00000800
120 #define TRACE_SCSI_ALL (TRACE_SCSI_DEBUG|TRACE_SCSI_CMD|TRACE_SCSI_DATA|TRACE_SCSI_ARGS)
122 #define TRACE_DEBUG 0x00001000
123 #define TRACE_HASH 0x00002000
124 #define TRACE_SYNC 0x00004000
125 #define TRACE_QUEUE 0x00008000
126 #define TRACE_WARN 0x00010000
127 #define TRACE_MEM 0x00020000
129 #define TRACE_OSD 0x00040000
130 #define TRACE_OSDFS 0x00080000
131 #define TRACE_OSDSO 0x00100000
132 #define TRACE_ALL 0xffffffff
135 * Set debugging level here. Turn on debugging in Makefile.
138 #define EXTERN extern
141 EXTERN
uint32_t iscsi_debug_level
;
144 * Debugging Functions
146 void set_debug(const char *);
147 void iscsi_trace(const int, const char *, ...);
148 void iscsi_warn(const char *, const int, const char *, ...);
149 void iscsi_err(const char *, const int, const char *, ...);
150 void iscsi_print_buffer(const char *, const size_t);
157 #ifdef HAVE_ASM_BYTEORDER_H
158 #include <asm/byteorder.h>
161 #ifdef HAVE_SYS_BYTEORDER_H
162 #include <sys/byteorder.h>
165 #ifdef HAVE_BYTESWAP_H
166 #include <byteswap.h>
169 #ifdef HAVE_MACHINE_ENDIAN_H
170 #include <machine/endian.h>
173 #define __BYTE_ORDER _BYTE_ORDER
174 #define __BIG_ENDIAN _BIG_ENDIAN
175 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
177 #define ISCSI_NTOHLL(a) ISCSI_BE64TOH(a)
178 #define ISCSI_HTONLL(a) ISCSI_HTOBE64(a)
179 #define ISCSI_NTOHL(a) ntohl(a)
180 #define ISCSI_HTONL(a) htonl(a)
181 #define ISCSI_NTOHS(a) ntohs(a)
182 #define ISCSI_HTONS(a) htons(a)
184 #ifndef HAVE_SOCKLEN_T
185 typedef int socklen_t
;
191 void *iscsi_malloc(unsigned);
192 void iscsi_free(void *);
193 void *iscsi_malloc_atomic(unsigned);
194 void iscsi_free_atomic(void *);
201 #define MIN(A,B) (((A)<(B))?(A):(B))
204 #define MIN_3(A,B,C) (((A)<(B))?(((A)<(C))?(A):(C)):(((B)<(C))?(B):(C)))
208 typedef pthread_mutex_t iscsi_spin_t
;
210 int iscsi_spin_init(iscsi_spin_t
* );
211 int iscsi_spin_lock(iscsi_spin_t
* );
212 int iscsi_spin_unlock(iscsi_spin_t
* );
213 int iscsi_spin_lock_irqsave(iscsi_spin_t
* , uint32_t *);
214 int iscsi_spin_unlock_irqrestore(iscsi_spin_t
* , uint32_t *);
215 int iscsi_spin_destroy(iscsi_spin_t
* );
218 * End of ISCSI spin routines
225 #define ISCSI_SET_TAG(tag) do { \
226 iscsi_spin_lock(&g_tag_spin); \
228 iscsi_spin_unlock(&g_tag_spin); \
229 } while (/* CONSTCOND */ 0)
231 #define ISCSI_SET_TAG_IN_INTR(tag) do { \
233 iscsi_spin_lock_irqsave(&g_tag_spin, &flags); \
235 iscsi_spin_unlock_irqrestore(&g_tag_spin, &flags); \
236 } while (/* CONSTCOND */ 0)
245 typedef struct hash_t
{
246 struct initiator_cmd_t
**bucket
;
253 int hash_init(hash_t
* , int );
254 int hash_insert(hash_t
* , struct initiator_cmd_t
* , uint32_t );
255 struct initiator_cmd_t
*hash_remove(hash_t
* , uint32_t );
256 int hash_destroy(hash_t
* );
262 typedef struct iscsi_queue_t
{
271 int iscsi_queue_init(iscsi_queue_t
* , int );
272 void iscsi_queue_destroy(iscsi_queue_t
* );
273 int iscsi_queue_insert(iscsi_queue_t
* , void *);
274 void *iscsi_queue_remove(iscsi_queue_t
* );
275 int iscsi_queue_depth(iscsi_queue_t
* );
276 int iscsi_queue_full(iscsi_queue_t
* );
282 /* Turning off Nagle's Algorithm doesn't always seem to work, */
283 /* so we combine two messages into one when the second's size */
284 /* is less than or equal to ISCSI_SOCK_HACK_CROSSOVER. */
286 #define ISCSI_SOCK_HACK_CROSSOVER 1024
287 #define ISCSI_SOCK_CONNECT_NONBLOCK 0
288 #define ISCSI_SOCK_CONNECT_TIMEOUT 1
289 #define ISCSI_SOCK_MSG_BYTE_ALIGN 4
291 int iscsi_sock_create(int *);
292 int iscsi_socks_establish(int *, int *, int *, char *, int);
293 int iscsi_waitfor_connection(int *, int, const char *cf
, int *);
294 const char *iscsi_address_family(int);
295 int iscsi_sock_setsockopt(int * , int , int , void *, unsigned);
296 int iscsi_sock_getsockopt(int * , int , int , void *, unsigned *);
297 int iscsi_sock_bind(int , int);
298 int iscsi_sock_listen(int);
299 int iscsi_sock_connect(int , char *, int);
300 int iscsi_sock_accept(int , int *);
301 int iscsi_sock_shutdown(int , int);
302 int iscsi_sock_close(int);
303 int iscsi_sock_msg(int , int , unsigned , void *, int);
304 int iscsi_sock_send_header_and_data(int ,
306 const void *, unsigned , int);
307 int iscsi_sock_getsockname(int , struct sockaddr
* , unsigned *);
308 int iscsi_sock_getpeername(int , struct sockaddr
* , unsigned *);
309 int modify_iov(struct iovec
** , int *, uint32_t , uint32_t);
316 typedef pthread_mutex_t iscsi_mutex_t
;
318 int iscsi_mutex_init(iscsi_mutex_t
*);
319 int iscsi_mutex_lock(iscsi_mutex_t
*);
320 int iscsi_mutex_unlock(iscsi_mutex_t
*);
321 int iscsi_mutex_destroy(iscsi_mutex_t
*);
323 #define ISCSI_LOCK(M, ELSE) do { \
324 if (iscsi_mutex_lock(M) != 0) { \
325 iscsi_err(__FILE__, __LINE__, "iscsi_mutex_lock() failed\n"); \
328 } while (/* CONSTCOND */ 0)
330 #define ISCSI_UNLOCK(M, ELSE) do { \
331 if (iscsi_mutex_unlock(M) != 0) { \
332 iscsi_err(__FILE__, __LINE__, "iscsi_mutex_unlock() failed\n"); \
335 } while (/* CONSTCOND */ 0)
337 #define ISCSI_MUTEX_INIT(M, ELSE) do { \
338 if (iscsi_mutex_init(M) != 0) { \
339 iscsi_err(__FILE__, __LINE__, "iscsi_mutex_init() failed\n"); \
342 } while (/* CONSTCOND */ 0)
344 #define ISCSI_MUTEX_DESTROY(M, ELSE) do { \
345 if (iscsi_mutex_destroy(M) != 0) { \
346 iscsi_err(__FILE__, __LINE__, "iscsi_mutex_destroy() failed\n"); \
349 } while (/* CONSTCOND */ 0)
355 typedef pthread_cond_t iscsi_cond_t
;
357 int iscsi_cond_init(iscsi_cond_t
*);
358 int iscsi_cond_wait(iscsi_cond_t
* , iscsi_mutex_t
*);
359 int iscsi_cond_signal(iscsi_cond_t
*);
360 int iscsi_cond_destroy(iscsi_cond_t
*);
362 #define ISCSI_COND_INIT(C, ELSE) do { \
363 if (iscsi_cond_init(C) != 0) { \
366 } while (/* CONSTCOND */ 0)
368 #define ISCSI_WAIT(C, M, ELSE) do { \
369 if (iscsi_cond_wait(C, M) != 0) { \
372 } while (/* CONSTCOND */ 0)
374 #define ISCSI_SIGNAL(C, ELSE) do { \
375 if (iscsi_cond_signal(C) != 0) { \
378 } while (/* CONSTCOND */ 0)
380 #define ISCSI_COND_DESTROY(C, ELSE) do { \
381 if (iscsi_cond_destroy(C) != 0) { \
384 } while (/* CONSTCOND */ 0)
390 typedef struct iscsi_thread_t
{
394 int iscsi_thread_create(iscsi_thread_t
* , void *(*proc
) (void *), void *);
396 #define ISCSI_SET_THREAD(ME) /* for user pthread id set by pthread_create
397 * in iscsi_thread_create */
398 #define ISCSI_THREAD_START(NAME)
404 #define ISCSI_WORKER_STATE_STARTED 1
405 #define ISCSI_WORKER_STATE_ERROR 2
406 #define ISCSI_WORKER_STATE_EXITING 4
409 iscsi_thread_t thread
;
410 iscsi_mutex_t work_mutex
;
411 iscsi_cond_t work_cond
;
412 iscsi_mutex_t exit_mutex
;
413 iscsi_cond_t exit_cond
;
416 volatile uint32_t state
;
419 #define ISCSI_WORKER_EXIT(ME) do { \
420 iscsi_trace(TRACE_ISCSI_DEBUG ,__FILE__, __LINE__, "exiting\n");\
421 (ME)->state |= ISCSI_WORKER_STATE_EXITING; \
424 } while (/* CONSTCOND */ 0)
432 * Pre/Post condition checking
435 #define NO_CLEANUP {}
438 #define RETURN_NOT_EQUAL(NAME, V1, V2, CU, RC) \
440 iscsi_err(__FILE__, __LINE__, "Bad \"%s\": Got %u expected %u.\n", NAME, V1, V2); \
447 #define ERROR_NOT_EQUAL(NAME, V1, V2, CU) do { \
449 iscsi_err(__FILE__, __LINE__, \
450 "Bad \"%s\": Got %u expected %u.\n", NAME, V1, V2); \
453 } while(/* CONSTCOND */0)
460 uint32_t iscsi_atoi(char *);
461 int HexTextToData(const char *, uint32_t , uint8_t *, uint32_t);
462 int HexDataToText(uint8_t *, uint32_t , char *, uint32_t);
463 void GenRandomData(uint8_t *, uint32_t);
465 /* this is the maximum number of iovecs which we can use in
466 * iscsi_sock_send_header_and_data */
467 #ifndef ISCSI_MAX_IOVECS
468 #define ISCSI_MAX_IOVECS 32
472 /* used in iscsi_sock_msg() */
477 int allow_netmask(const char *, const char *);
479 #endif /* _ISCSIUTIL_H_ */