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. 6 * 7 * Intel License Agreement 8 * 9 * Copyright (c) 2000, Intel Corporation 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * -Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 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. 23 * 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. 27 * 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. 40 */ 41 #ifndef _ISCSIUTIL_H_ 42 #define _ISCSIUTIL_H_ 43 44 #include "config.h" 45 46 #include <stdio.h> 47 #include <stdlib.h> 48 49 #ifdef HAVE_STDINT_H 50 #include <stdint.h> 51 #endif 52 53 #ifdef HAVE_STRING_H 54 #include <string.h> 55 #endif 56 57 #ifdef HAVE_PTHREAD_H 58 #include <pthread.h> 59 #endif 60 61 #ifdef HAVE_UNISTD_H 62 #include <unistd.h> 63 #endif 64 65 #ifdef HAVE_SYS_UIO_H 66 #include <sys/uio.h> 67 #endif 68 69 #ifdef HAVE_SYS_SOCKET_H 70 #include <sys/socket.h> 71 #endif 72 73 #ifdef HAVE_FCNTL_H 74 #include <fcntl.h> 75 #endif 76 77 #ifdef HAVE_SYSLOG_H 78 #include <syslog.h> 79 #endif 80 81 /* 82 * 83 */ 84 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))) 92 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))) 100 101 /* 102 * Debugging Levels 103 */ 104 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) 109 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) 115 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) 121 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 128 129 #define TRACE_OSD 0x00040000 130 #define TRACE_OSDFS 0x00080000 131 #define TRACE_OSDSO 0x00100000 132 #define TRACE_ALL 0xffffffff 133 134 /* 135 * Set debugging level here. Turn on debugging in Makefile. 136 */ 137 #ifndef EXTERN 138 #define EXTERN extern 139 #endif 140 141 EXTERN uint32_t iscsi_debug_level; 142 143 /* 144 * Debugging Functions 145 */ 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); 151 152 153 /* 154 * Byte Order 155 */ 156 157 #ifdef HAVE_ASM_BYTEORDER_H 158 #include <asm/byteorder.h> 159 #endif 160 161 #ifdef HAVE_SYS_BYTEORDER_H 162 #include <sys/byteorder.h> 163 #endif 164 165 #ifdef HAVE_BYTESWAP_H 166 #include <byteswap.h> 167 #endif 168 169 #ifdef HAVE_MACHINE_ENDIAN_H 170 #include <machine/endian.h> 171 #endif 172 173 #define __BYTE_ORDER _BYTE_ORDER 174 #define __BIG_ENDIAN _BIG_ENDIAN 175 #define __LITTLE_ENDIAN _LITTLE_ENDIAN 176 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) 183 184 #ifndef HAVE_SOCKLEN_T 185 typedef int socklen_t; 186 #endif 187 188 /* 189 * Memory 190 */ 191 void *iscsi_malloc(unsigned); 192 void iscsi_free(void *); 193 void *iscsi_malloc_atomic(unsigned); 194 void iscsi_free_atomic(void *); 195 196 /* 197 * Comparison 198 */ 199 200 #ifndef MIN 201 #define MIN(A,B) (((A)<(B))?(A):(B)) 202 #endif 203 204 #define MIN_3(A,B,C) (((A)<(B))?(((A)<(C))?(A):(C)):(((B)<(C))?(B):(C))) 205 206 /* Spin locks */ 207 208 typedef pthread_mutex_t iscsi_spin_t; 209 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 * ); 216 217 /* 218 * End of ISCSI spin routines 219 */ 220 221 /* 222 * Tags 223 */ 224 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) 230 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) 237 238 239 240 /* 241 * Hashing 242 */ 243 244 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; 252 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 * ); 257 258 /* 259 * Queuing 260 */ 261 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; 270 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 * ); 277 278 /* 279 * Socket Abstraction 280 */ 281 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. */ 285 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 290 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); 310 311 312 /* 313 * Mutexes 314 */ 315 316 typedef pthread_mutex_t iscsi_mutex_t; 317 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 *); 322 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; \ 327 } \ 328 } while (/* CONSTCOND */ 0) 329 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; \ 334 } \ 335 } while (/* CONSTCOND */ 0) 336 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; \ 341 } \ 342 } while (/* CONSTCOND */ 0) 343 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; \ 348 } \ 349 } while (/* CONSTCOND */ 0) 350 351 /* 352 * Condition Variable 353 */ 354 355 typedef pthread_cond_t iscsi_cond_t; 356 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 *); 361 362 #define ISCSI_COND_INIT(C, ELSE) do { \ 363 if (iscsi_cond_init(C) != 0) { \ 364 ELSE; \ 365 } \ 366 } while (/* CONSTCOND */ 0) 367 368 #define ISCSI_WAIT(C, M, ELSE) do { \ 369 if (iscsi_cond_wait(C, M) != 0) { \ 370 ELSE; \ 371 } \ 372 } while (/* CONSTCOND */ 0) 373 374 #define ISCSI_SIGNAL(C, ELSE) do { \ 375 if (iscsi_cond_signal(C) != 0) { \ 376 ELSE; \ 377 } \ 378 } while (/* CONSTCOND */ 0) 379 380 #define ISCSI_COND_DESTROY(C, ELSE) do { \ 381 if (iscsi_cond_destroy(C) != 0) { \ 382 ELSE; \ 383 } \ 384 } while (/* CONSTCOND */ 0) 385 386 /* 387 * Threading Routines 388 */ 389 390 typedef struct iscsi_thread_t { 391 pthread_t pthread; 392 } iscsi_thread_t; 393 394 int iscsi_thread_create(iscsi_thread_t * , void *(*proc) (void *), void *); 395 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) 399 400 /* 401 * Worker Thread 402 */ 403 404 #define ISCSI_WORKER_STATE_STARTED 1 405 #define ISCSI_WORKER_STATE_ERROR 2 406 #define ISCSI_WORKER_STATE_EXITING 4 407 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; 418 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) 425 426 /* 427 * Spin Lock 428 */ 429 #define ISCSI_SPIN 430 431 /* 432 * Pre/Post condition checking 433 */ 434 435 #define NO_CLEANUP {} 436 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; \ 443 } 444 #endif 445 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; \ 452 } \ 453 } while(/* CONSTCOND */0) 454 #endif 455 456 /* 457 * Misc. Functions 458 */ 459 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); 464 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 470 471 enum { 472 /* used in iscsi_sock_msg() */ 473 Receive = 0, 474 Transmit = 1 475 }; 476 477 int allow_netmask(const char *, const char *); 478 479 #endif /* _ISCSIUTIL_H_ */ 480