Don't use .Xo/.Xc. Fix date format.
[netbsd-mini2440.git] / external / bsd / iscsi / dist / include / iscsiutil.h
blob0f3d1fe89b58fec52df1b9160a20507a8755e171
1 /*
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
14 * are met:
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
22 * distribution.
24 * -The name of Intel Corporation may not be used to endorse or
25 * promote products derived from this software without specific prior
26 * written permission.
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
39 * SUCH DAMAGE.
41 #ifndef _ISCSIUTIL_H_
42 #define _ISCSIUTIL_H_
44 #include "config.h"
46 #include <stdio.h>
47 #include <stdlib.h>
49 #ifdef HAVE_STDINT_H
50 #include <stdint.h>
51 #endif
53 #ifdef HAVE_STRING_H
54 #include <string.h>
55 #endif
57 #ifdef HAVE_PTHREAD_H
58 #include <pthread.h>
59 #endif
61 #ifdef HAVE_UNISTD_H
62 #include <unistd.h>
63 #endif
65 #ifdef HAVE_SYS_UIO_H
66 #include <sys/uio.h>
67 #endif
69 #ifdef HAVE_SYS_SOCKET_H
70 #include <sys/socket.h>
71 #endif
73 #ifdef HAVE_FCNTL_H
74 #include <fcntl.h>
75 #endif
77 #ifdef HAVE_SYSLOG_H
78 #include <syslog.h>
79 #endif
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)))
102 * Debugging Levels
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.
137 #ifndef EXTERN
138 #define EXTERN extern
139 #endif
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);
154 * Byte Order
157 #ifdef HAVE_ASM_BYTEORDER_H
158 #include <asm/byteorder.h>
159 #endif
161 #ifdef HAVE_SYS_BYTEORDER_H
162 #include <sys/byteorder.h>
163 #endif
165 #ifdef HAVE_BYTESWAP_H
166 #include <byteswap.h>
167 #endif
169 #ifdef HAVE_MACHINE_ENDIAN_H
170 #include <machine/endian.h>
171 #endif
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;
186 #endif
189 * Memory
191 void *iscsi_malloc(unsigned);
192 void iscsi_free(void *);
193 void *iscsi_malloc_atomic(unsigned);
194 void iscsi_free_atomic(void *);
197 * Comparison
200 #ifndef MIN
201 #define MIN(A,B) (((A)<(B))?(A):(B))
202 #endif
204 #define MIN_3(A,B,C) (((A)<(B))?(((A)<(C))?(A):(C)):(((B)<(C))?(B):(C)))
206 /* Spin locks */
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
222 * Tags
225 #define ISCSI_SET_TAG(tag) do { \
226 iscsi_spin_lock(&g_tag_spin); \
227 *tag = g_tag++; \
228 iscsi_spin_unlock(&g_tag_spin); \
229 } while (/* CONSTCOND */ 0)
231 #define ISCSI_SET_TAG_IN_INTR(tag) do { \
232 uint32_t flags; \
233 iscsi_spin_lock_irqsave(&g_tag_spin, &flags); \
234 *tag = g_tag++; \
235 iscsi_spin_unlock_irqrestore(&g_tag_spin, &flags); \
236 } while (/* CONSTCOND */ 0)
241 * Hashing
245 typedef struct hash_t {
246 struct initiator_cmd_t **bucket;
247 int collisions;
248 int insertions;
249 int n;
250 iscsi_spin_t lock;
251 } hash_t;
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 * );
259 * Queuing
262 typedef struct iscsi_queue_t {
263 int head;
264 int tail;
265 int count;
266 void **elem;
267 int depth;
268 iscsi_spin_t lock;
269 } 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 * );
279 * Socket Abstraction
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 ,
305 void *, unsigned ,
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);
313 * Mutexes
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"); \
326 ELSE; \
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"); \
333 ELSE; \
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"); \
340 ELSE; \
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"); \
347 ELSE; \
349 } while (/* CONSTCOND */ 0)
352 * Condition Variable
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) { \
364 ELSE; \
366 } while (/* CONSTCOND */ 0)
368 #define ISCSI_WAIT(C, M, ELSE) do { \
369 if (iscsi_cond_wait(C, M) != 0) { \
370 ELSE; \
372 } while (/* CONSTCOND */ 0)
374 #define ISCSI_SIGNAL(C, ELSE) do { \
375 if (iscsi_cond_signal(C) != 0) { \
376 ELSE; \
378 } while (/* CONSTCOND */ 0)
380 #define ISCSI_COND_DESTROY(C, ELSE) do { \
381 if (iscsi_cond_destroy(C) != 0) { \
382 ELSE; \
384 } while (/* CONSTCOND */ 0)
387 * Threading Routines
390 typedef struct iscsi_thread_t {
391 pthread_t pthread;
392 } 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)
401 * Worker Thread
404 #define ISCSI_WORKER_STATE_STARTED 1
405 #define ISCSI_WORKER_STATE_ERROR 2
406 #define ISCSI_WORKER_STATE_EXITING 4
408 typedef struct {
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;
414 int id;
415 int pid;
416 volatile uint32_t state;
417 } iscsi_worker_t;
419 #define ISCSI_WORKER_EXIT(ME) do { \
420 iscsi_trace(TRACE_ISCSI_DEBUG ,__FILE__, __LINE__, "exiting\n");\
421 (ME)->state |= ISCSI_WORKER_STATE_EXITING; \
422 return 0; \
423 /* NOTREACHED */ \
424 } while (/* CONSTCOND */ 0)
427 * Spin Lock
429 #define ISCSI_SPIN
432 * Pre/Post condition checking
435 #define NO_CLEANUP {}
437 #if 0
438 #define RETURN_NOT_EQUAL(NAME, V1, V2, CU, RC) \
439 if ((V1)!=(V2)) { \
440 iscsi_err(__FILE__, __LINE__, "Bad \"%s\": Got %u expected %u.\n", NAME, V1, V2); \
441 CU; \
442 return RC; \
444 #endif
446 #if 0
447 #define ERROR_NOT_EQUAL(NAME, V1, V2, CU) do { \
448 if ((V1)!=(V2)) { \
449 iscsi_err(__FILE__, __LINE__, \
450 "Bad \"%s\": Got %u expected %u.\n", NAME, V1, V2); \
451 CU; \
453 } while(/* CONSTCOND */0)
454 #endif
457 * Misc. Functions
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
469 #endif
471 enum {
472 /* used in iscsi_sock_msg() */
473 Receive = 0,
474 Transmit = 1
477 int allow_netmask(const char *, const char *);
479 #endif /* _ISCSIUTIL_H_ */