Abstract out some common code, and place it in the utilities source.

Get rid of some #ifdef __KERNEL__ abstractions - they're not needed.
This commit is contained in:
agc 2006-03-21 21:03:14 +00:00
parent b2f6added5
commit cbf5d4f8f5
4 changed files with 64 additions and 191 deletions

View File

@ -196,11 +196,7 @@ typedef int socklen_t;
* Sleeping
*/
#ifdef __KERNEL__
#define ISCSI_SLEEP(N) {uint32_t future = jiffies+N*HZ; while (jiffies<future) ISCSI_SPIN;}
#else
#define ISCSI_SLEEP(N) sleep(N)
#endif
/*
* Memory
@ -223,13 +219,7 @@ void iscsi_free_atomic(void *);
/* Spin locks */
#ifdef __KERNEL__
typedef spinlock_t
iscsi_spin_t;
#else
typedef pthread_mutex_t
iscsi_spin_t;
#endif
typedef pthread_mutex_t iscsi_spin_t;
int iscsi_spin_init(iscsi_spin_t * );
int iscsi_spin_lock(iscsi_spin_t * );
@ -303,11 +293,7 @@ int iscsi_queue_full(iscsi_queue_t * );
* Socket Abstraction
*/
#ifdef __KERNEL__
typedef struct socket *iscsi_socket_t;
#else
typedef int iscsi_socket_t;
#endif
/* Turning off Nagle's Algorithm doesn't always seem to work, */
/* so we combine two messages into one when the second's size */
@ -335,15 +321,15 @@ int iscsi_sock_getsockname(iscsi_socket_t , struct sockaddr * , unsi
int iscsi_sock_getpeername(iscsi_socket_t , struct sockaddr * , unsigned *);
int modify_iov(struct iovec ** , int *, uint32_t , uint32_t );
void cdb2lba(uint32_t *, uint16_t *, uint8_t *);
void lba2cdb(uint8_t *, uint32_t *, uint16_t *);
/*
* Mutexes
*/
#ifdef __KERNEL__
typedef struct semaphore iscsi_mutex_t;
#else
typedef pthread_mutex_t iscsi_mutex_t;
#endif
int iscsi_mutex_init(iscsi_mutex_t * );
int iscsi_mutex_lock(iscsi_mutex_t * );
@ -352,28 +338,28 @@ int iscsi_mutex_destroy(iscsi_mutex_t * );
#define ISCSI_LOCK(M, ELSE) do { \
if (iscsi_mutex_lock(M) != 0) { \
iscsi_trace_error("iscsi_mutex_lock() failed\n"); \
iscsi_trace_error("iscsi_mutex_lock() failed\n"); \
ELSE; \
} \
} while (/* CONSTCOND */ 0)
#define ISCSI_UNLOCK(M, ELSE) do { \
if (iscsi_mutex_unlock(M) != 0) { \
iscsi_trace_error("iscsi_mutex_unlock() failed\n"); \
iscsi_trace_error("iscsi_mutex_unlock() failed\n"); \
ELSE; \
} \
} while (/* CONSTCOND */ 0)
#define ISCSI_MUTEX_INIT(M, ELSE) do { \
if (iscsi_mutex_init(M) != 0) { \
iscsi_trace_error("iscsi_mutex_init() failed\n"); \
iscsi_trace_error("iscsi_mutex_init() failed\n"); \
ELSE; \
} \
} while (/* CONSTCOND */ 0)
#define ISCSI_MUTEX_DESTROY(M, ELSE) do { \
if (iscsi_mutex_destroy(M) != 0) { \
iscsi_trace_error("iscsi_mutex_destroy() failed\n"); \
iscsi_trace_error("iscsi_mutex_destroy() failed\n"); \
ELSE; \
} \
} while (/* CONSTCOND */ 0)
@ -382,11 +368,7 @@ int iscsi_mutex_destroy(iscsi_mutex_t * );
* Condition Variable
*/
#ifdef __KERNEL__
typedef struct semaphore iscsi_cond_t;
#else
typedef pthread_cond_t iscsi_cond_t;
#endif
int iscsi_cond_init(iscsi_cond_t * );
int iscsi_cond_wait(iscsi_cond_t * , iscsi_mutex_t * );
@ -422,36 +404,14 @@ int iscsi_cond_destroy(iscsi_cond_t * );
*/
typedef struct iscsi_thread_t {
#ifdef __KERNEL__
struct task_struct *pthread;
#else
pthread_t pthread;
#endif
} iscsi_thread_t;
int iscsi_thread_create(iscsi_thread_t * , void *(*proc) (void *), void *);
#ifdef __KERNEL__
#define ISCSI_SET_THREAD(ME) me->thread.pthread = current;
#if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
#define REPARENT_TO_INIT() reparent_to_init()
#define ISCSI_THREAD_START(NAME) \
lock_kernel(); \
daemonize(); \
REPARENT_TO_INIT(); \
unlock_kernel(); \
sprintf(current->comm, "%s", NAME);
#else
#define REPARENT_TO_INIT
#define ISCSI_THREAD_START(NAME) \
sprintf(current->comm, "%s", NAME);
#endif
#else
#define ISCSI_SET_THREAD(ME) /* for user pthread id set by pthread_create
* in iscsi_thread_create */
#define ISCSI_THREAD_START(NAME)
#endif
/*
* Worker Thread
@ -481,12 +441,7 @@ typedef struct {
/*
* Spin Lock
*/
#ifdef __KERNEL__
#define ISCSI_SPIN schedule()
#else
#define ISCSI_SPIN
#endif
/*
* Pre/Post condition checking

43
dist/iscsi/src/disk.c vendored
View File

@ -1,4 +1,4 @@
/* $NetBSD: disk.c,v 1.7 2006/03/20 20:45:07 agc Exp $ */
/* $NetBSD: disk.c,v 1.8 2006/03/21 21:03:14 agc Exp $ */
/*
* Copyright © 2006 Alistair Crooks. All rights reserved.
@ -934,25 +934,7 @@ device_command(target_session_t * sess, target_cmd_t * cmd)
case WRITE_10:
/* Some platforms (like strongarm) aligns on */
/* word boundaries. So HTONL and NTOHL won't */
/* work here. */
#if (BYTE_ORDER == BIG_ENDIAN)
((uint8_t *) (void *) &lba)[0] = cdb[2];
((uint8_t *) (void *) &lba)[1] = cdb[3];
((uint8_t *) (void *) &lba)[2] = cdb[4];
((uint8_t *) (void *) &lba)[3] = cdb[5];
((uint8_t *) (void *) &len)[0] = cdb[7];
((uint8_t *) (void *) &len)[1] = cdb[8];
#else
((uint8_t *) (void *) &lba)[0] = cdb[5];
((uint8_t *) (void *) &lba)[1] = cdb[4];
((uint8_t *) (void *) &lba)[2] = cdb[3];
((uint8_t *) (void *) &lba)[3] = cdb[2];
((uint8_t *) (void *) &len)[0] = cdb[8];
((uint8_t *) (void *) &len)[1] = cdb[7];
#endif
cdb2lba(&lba, &len, cdb);
iscsi_trace(TRACE_SCSI_CMD, "WRITE_10(lba %u, len %u blocks)\n", lba, len);
if (disk_write(sess, args, lun, lba, (unsigned) len) != 0) {
@ -964,26 +946,7 @@ device_command(target_session_t * sess, target_cmd_t * cmd)
case READ_10:
/* Some platforms (like strongarm) aligns on */
/* word boundaries. So HTONL and NTOHL won't */
/* work here. */
#if (BYTE_ORDER == BIG_ENDIAN)
((uint8_t *) (void *) &lba)[0] = cdb[2];
((uint8_t *) (void *) &lba)[1] = cdb[3];
((uint8_t *) (void *) &lba)[2] = cdb[4];
((uint8_t *) (void *) &lba)[3] = cdb[5];
((uint8_t *) (void *) &len)[0] = cdb[7];
((uint8_t *) (void *) &len)[1] = cdb[8];
#else
((uint8_t *) (void *) &lba)[0] = cdb[5];
((uint8_t *) (void *) &lba)[1] = cdb[4];
((uint8_t *) (void *) &lba)[2] = cdb[3];
((uint8_t *) (void *) &lba)[3] = cdb[2];
((uint8_t *) (void *) &len)[0] = cdb[8];
((uint8_t *) (void *) &len)[1] = cdb[7];
#endif
cdb2lba(&lba, &len, cdb);
iscsi_trace(TRACE_SCSI_CMD, "READ_10(lba %u, len %u blocks)\n", lba, len);
if (disk_read(sess, args, lba, len, lun) != 0) {

102
dist/iscsi/src/tests.c vendored
View File

@ -248,26 +248,7 @@ write_read_test(uint64_t target, uint32_t lun, int type)
if (type == 10) {
cdb[0] = WRITE_10;
cdb[1] = lun << 5;
/* Strongarm aligns on word boundaries. */
/* So HTONL and NTOHL won't work here. */
#if (BYTE_ORDER == BIG_ENDIAN)
cdb[2] = ((uint8_t *) &i)[2];
cdb[3] = ((uint8_t *) &i)[3];
cdb[4] = ((uint8_t *) &i)[0];
cdb[5] = ((uint8_t *) &i)[1];
cdb[7] = ((uint8_t *) &len)[0];
cdb[8] = ((uint8_t *) &len)[1];
#else
cdb[2] = ((uint8_t *) &i)[3];
cdb[3] = ((uint8_t *) &i)[2];
cdb[4] = ((uint8_t *) &i)[1];
cdb[5] = ((uint8_t *) &i)[0];
cdb[7] = ((uint8_t *) &len)[1];
cdb[8] = ((uint8_t *) &len)[0];
#endif
lba2cdb(cdb, &i, &len);
} else {
*((uint32_t *) (cdb + 0)) = ISCSI_HTONL(i);
cdb[0] = WRITE_6;
@ -302,26 +283,7 @@ write_read_test(uint64_t target, uint32_t lun, int type)
if (type == 10) {
cdb[0] = READ_10;
cdb[1] = lun << 5;
/* Strongarm aligns on word boundaries. */
/* So HTONL and NTOHL won't work here. */
#if (BYTE_ORDER == BIG_ENDIAN)
cdb[2] = ((uint8_t *) &i)[2];
cdb[3] = ((uint8_t *) &i)[3];
cdb[4] = ((uint8_t *) &i)[0];
cdb[5] = ((uint8_t *) &i)[1];
cdb[7] = ((uint8_t *) &len)[0];
cdb[8] = ((uint8_t *) &len)[1];
#else
cdb[2] = ((uint8_t *) &i)[3];
cdb[3] = ((uint8_t *) &i)[2];
cdb[4] = ((uint8_t *) &i)[1];
cdb[5] = ((uint8_t *) &i)[0];
cdb[7] = ((uint8_t *) &len)[1];
cdb[8] = ((uint8_t *) &len)[0];
#endif
lba2cdb(cdb, &i, &len);
} else {
*((uint32_t *) (cdb + 0)) = ISCSI_HTONL(i);
cdb[0] = READ_6;
@ -710,50 +672,14 @@ latency_test(uint64_t target, uint32_t lun, uint8_t op, uint32_t iters)
trans_len = block_len;
cdb[0] = READ_10;
cdb[1] = lun << 5;
/* Strongarm aligns on word boundaries. */
/* So HTONL and NTOHL won't work here. */
#if (BYTE_ORDER == BIG_ENDIAN)
cdb[2] = ((uint8_t *) &lba)[2];
cdb[3] = ((uint8_t *) &lba)[3];
cdb[4] = ((uint8_t *) &lba)[0];
cdb[5] = ((uint8_t *) &lba)[1];
cdb[7] = ((uint8_t *) &len)[0];
cdb[8] = ((uint8_t *) &len)[1];
#else
cdb[2] = ((uint8_t *) &lba)[3];
cdb[3] = ((uint8_t *) &lba)[2];
cdb[4] = ((uint8_t *) &lba)[1];
cdb[5] = ((uint8_t *) &lba)[0];
cdb[7] = ((uint8_t *) &len)[1];
cdb[8] = ((uint8_t *) &len)[0];
#endif
lba2cdb(cdb, &lba, &len);
break;
case WRITE_10:
output = 1;
trans_len = block_len;
cdb[0] = WRITE_10;
cdb[1] = lun << 5;
/* Strongarm aligns on word boundaries. */
/* So HTONL and NTOHL won't work here. */
#if (BYTE_ORDER == BIG_ENDIAN)
cdb[2] = ((uint8_t *) &lba)[2];
cdb[3] = ((uint8_t *) &lba)[3];
cdb[4] = ((uint8_t *) &lba)[0];
cdb[5] = ((uint8_t *) &lba)[1];
cdb[7] = ((uint8_t *) &len)[0];
cdb[8] = ((uint8_t *) &len)[1];
#else
cdb[2] = ((uint8_t *) &lba)[3];
cdb[3] = ((uint8_t *) &lba)[2];
cdb[4] = ((uint8_t *) &lba)[1];
cdb[5] = ((uint8_t *) &lba)[0];
cdb[7] = ((uint8_t *) &len)[1];
cdb[8] = ((uint8_t *) &len)[0];
#endif
lba2cdb(cdb, &lba, &len);
break;
default:
iscsi_trace_error("op 0x%x not implemented\n", op);
@ -892,25 +818,7 @@ scatter_gather_test(uint64_t target, uint32_t lun, uint8_t op)
sg[i].iov_base = buff[i];
sg[i].iov_len = block_len;
}
/* Strongarm aligns on word boundaries. */
/* So HTONL and NTOHL won't work here. */
#if (BYTE_ORDER == BIG_ENDIAN)
cdb[2] = ((uint8_t *) &lba)[2];
cdb[3] = ((uint8_t *) &lba)[3];
cdb[4] = ((uint8_t *) &lba)[0];
cdb[5] = ((uint8_t *) &lba)[1];
cdb[7] = ((uint8_t *) &len)[0];
cdb[8] = ((uint8_t *) &len)[1];
#else
cdb[2] = ((uint8_t *) &lba)[3];
cdb[3] = ((uint8_t *) &lba)[2];
cdb[4] = ((uint8_t *) &lba)[1];
cdb[5] = ((uint8_t *) &lba)[0];
cdb[7] = ((uint8_t *) &len)[1];
cdb[8] = ((uint8_t *) &len)[0];
#endif
lba2cdb(cdb, &lba, &len);
gettimeofday(&t_start, 0);

47
dist/iscsi/src/util.c vendored
View File

@ -1088,3 +1088,50 @@ GenRandomData(uint8_t *data, uint32_t length)
*data++ = n;
}
}
void
cdb2lba(uint32_t *lba, uint16_t *len, uint8_t *cdb)
{
/* Some platforms (like strongarm) aligns on */
/* word boundaries. So HTONL and NTOHL won't */
/* work here. */
#if (BYTE_ORDER == BIG_ENDIAN)
((uint8_t *) (void *) lba)[0] = cdb[2];
((uint8_t *) (void *) lba)[1] = cdb[3];
((uint8_t *) (void *) lba)[2] = cdb[4];
((uint8_t *) (void *) lba)[3] = cdb[5];
((uint8_t *) (void *) len)[0] = cdb[7];
((uint8_t *) (void *) len)[1] = cdb[8];
#else
((uint8_t *) (void *) lba)[0] = cdb[5];
((uint8_t *) (void *) lba)[1] = cdb[4];
((uint8_t *) (void *) lba)[2] = cdb[3];
((uint8_t *) (void *) lba)[3] = cdb[2];
((uint8_t *) (void *) len)[0] = cdb[8];
((uint8_t *) (void *) len)[1] = cdb[7];
#endif
}
void
lba2cdb(uint8_t *cdb, uint32_t *lba, uint16_t *len)
{
/* Some platforms (like strongarm) aligns on */
/* word boundaries. So HTONL and NTOHL won't */
/* work here. */
#if (BYTE_ORDER == BIG_ENDIAN)
cdb[2] = ((uint8_t *) lba)[2];
cdb[3] = ((uint8_t *) lba)[3];
cdb[4] = ((uint8_t *) lba)[0];
cdb[5] = ((uint8_t *) lba)[1];
cdb[7] = ((uint8_t *) len)[0];
cdb[8] = ((uint8_t *) len)[1];
#else
cdb[2] = ((uint8_t *) lba)[3];
cdb[3] = ((uint8_t *) lba)[2];
cdb[4] = ((uint8_t *) lba)[1];
cdb[5] = ((uint8_t *) lba)[0];
cdb[7] = ((uint8_t *) len)[1];
cdb[8] = ((uint8_t *) len)[0];
#endif
}