Merge branch 'tst'

This commit is contained in:
Ronnie Sahlberg
2025-04-26 11:30:58 +10:00
37 changed files with 1971 additions and 500 deletions

View File

@@ -0,0 +1,190 @@
/* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */
/*
Copyright (C) 2025 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _LIBISCSI_MULTITHREADING_H_
#define _LIBISCSI_MULTITHREADING_H_
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef HAVE_MULTITHREADING
#ifdef WIN32
typedef HANDLE libiscsi_thread_t;
typedef HANDLE libiscsi_sem_t;
typedef DWORD iscsi_tid_t;
#elif defined(HAVE_PTHREAD)
#include <pthread.h>
typedef pthread_t libiscsi_thread_t;
#if defined(__APPLE__) && defined(HAVE_DISPATCH_DISPATCH_H)
#include <dispatch/dispatch.h>
typedef dispatch_semaphore_t libiscsi_sem_t;
#else
#include <semaphore.h>
typedef sem_t libiscsi_sem_t;
#endif
#ifdef HAVE_PTHREAD_THREADID_NP
typedef uint64_t iscsi_tid_t;
#else
typedef pid_t iscsi_tid_t;
#endif
#endif /* HAVE_PTHREAD */
iscsi_tid_t iscsi_mt_get_tid(void);
int iscsi_mt_sem_init(libiscsi_sem_t *sem, int value);
int iscsi_mt_sem_destroy(libiscsi_sem_t *sem);
int iscsi_mt_sem_post(libiscsi_sem_t *sem);
int iscsi_mt_sem_wait(libiscsi_sem_t *sem);
#endif /* HAVE_MULTITHREADING */
/*
* We always have access to mutex functions even if multithreading
* is not enabled.
*/
#if defined(HAVE_PTHREAD)
typedef pthread_mutex_t libiscsi_mutex_t;
/*
* If this is enabled we check for the following locking violations, at the
* (slight) cost of performance:
* - Thread holding the lock again tries to lock.
* - Thread not holding the lock tries to unlock.
*
* This is very useful for catching any coding errors.
* The performance hit is not very significant so you can leave it enabled,
* but if you really care then once the code has been vetted, this can be
* undef'ed to get the perf back.
*/
#define DEBUG_PTHREAD_LOCKING_VIOLATIONS
static inline int iscsi_mt_mutex_init(libiscsi_mutex_t *mutex)
{
int ret;
#ifdef DEBUG_PTHREAD_LOCKING_VIOLATIONS
pthread_mutexattr_t attr;
ret = pthread_mutexattr_init(&attr);
if (ret != 0) {
return ret;
}
ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
if (ret != 0) {
return ret;
}
ret = pthread_mutex_init(mutex, &attr);
if (ret != 0) {
return ret;
}
#else
ret = pthread_mutex_init(mutex, NULL);
assert(ret == 0);
#endif
return ret;
}
static inline int iscsi_mt_mutex_destroy(libiscsi_mutex_t *mutex)
{
return pthread_mutex_destroy(mutex);
}
static inline int iscsi_mt_mutex_lock(libiscsi_mutex_t *mutex)
{
return pthread_mutex_lock(mutex);
}
static inline int iscsi_mt_mutex_unlock(libiscsi_mutex_t *mutex)
{
return pthread_mutex_unlock(mutex);
}
typedef pthread_spinlock_t libiscsi_spinlock_t;
static inline int iscsi_mt_spin_init(libiscsi_spinlock_t *spinlock, int shared)
{
return pthread_spin_init(spinlock, shared);
}
static inline int iscsi_mt_spin_destroy(libiscsi_spinlock_t *spinlock)
{
return pthread_spin_destroy(spinlock);
}
static inline int iscsi_mt_spin_lock(libiscsi_spinlock_t *spinlock)
{
return pthread_spin_lock(spinlock);
}
static inline int iscsi_mt_spin_unlock(libiscsi_spinlock_t *spinlock)
{
return pthread_spin_unlock(spinlock);
}
#elif defined(WIN32)
typedef HANDLE libiscsi_mutex_t;
static inline int iscsi_mt_mutex_init(libiscsi_mutex_t* mutex)
{
*mutex = CreateSemaphoreA(NULL, 1, 1, NULL);
return 0;
}
static inline int iscsi_mt_mutex_destroy(libiscsi_mutex_t* mutex)
{
CloseHandle(*mutex);
return 0;
}
static inline int iscsi_mt_mutex_lock(libiscsi_mutex_t* mutex)
{
while (WaitForSingleObject(*mutex, INFINITE) != WAIT_OBJECT_0);
return 0;
}
static inline int iscsi_mt_mutex_unlock(libiscsi_mutex_t* mutex)
{
ReleaseSemaphore(*mutex, 1, NULL);
return 0;
}
#else
typedef const int libiscsi_mutex_t;
#define iscsi_mt_mutex_init(x) ;
#define iscsi_mt_mutex_destroy(x) ;
#define iscsi_mt_mutex_lock(x) ;
#define iscsi_mt_mutex_unlock(x) ;
typedef const int libiscsi_spinlock_t;
#define iscsi_mt_spin_init(x) ;
#define iscsi_mt_spin_destroy(x) ;
#define iscsi_mt_spin_lock(x) ;
#define iscsi_mt_spin_unlock(x) ;
#endif /* mutex */
#ifdef __cplusplus
}
#endif
#endif /* !_LIBISCSI_MULTITHREADING_H_ */

View File

@@ -28,6 +28,32 @@
#include "iscsi.h"
#ifdef HAVE_MULTITHREADING
#ifdef HAVE_STDATOMIC_H
#include <stdatomic.h>
#define ATOMIC_INC(rpc, x) \
atomic_fetch_add_explicit(&x, 1, memory_order_relaxed)
#define ATOMIC_DEC(rpc, x) \
atomic_fetch_sub_explicit(&x, 1, memory_order_relaxed)
#else /* HAVE_STDATOMIC_H */
#define ATOMIC_INC(rpc, x) \
iscs_mt_mutex_lock(&iscs->atomic_int_mutex); \
} \
x++; \
iscsi_mt_mutex_unlock(&iscsi->atomic_int_mutex);
#define ATOMIC_DEC(rpc, x) \
nfs_mt_mutex_lock(&rpc->atomic_int_mutex); \
x--; \
nfs_mt_mutex_unlock(&rpc->atomic_int_mutex);
#endif /* HAVE_STDATOMIC_H */
#else /* HAVE_MULTITHREADING */
/* no multithreading support, no need to protect the increment */
#define ATOMIC_INC(rpc, x) x++
#define ATOMIC_DEC(rpc, x) x--
#endif /* HAVE_MULTITHREADING */
#include "iscsi-multithreading.h"
#ifdef __cplusplus
extern "C" {
#endif
@@ -105,12 +131,12 @@ struct iscsi_context {
enum iscsi_session_type session_type;
unsigned char isid[6];
uint8_t rdma_ack_timeout;
uint32_t itt;
uint32_t cmdsn;
uint32_t min_cmdsn_waiting;
uint32_t expcmdsn;
uint32_t maxcmdsn;
uint32_t statsn;
uint32_t itt; /* Protected by iscsi_lock */
uint32_t cmdsn; /* Protected by iscsi_lock */
uint32_t min_cmdsn_waiting; /* Protected by iscsi_lock */
uint32_t expcmdsn; /* Protected by iscsi_lock */
uint32_t maxcmdsn; /* Protected by iscsi_lock */
uint32_t statsn; /* Protected by iscsi_lock */
enum iscsi_header_digest want_header_digest;
enum iscsi_header_digest header_digest;
enum iscsi_data_digest want_data_digest;
@@ -144,11 +170,10 @@ struct iscsi_context {
iscsi_command_cb socket_status_cb;
void *connect_data;
struct iscsi_pdu *outqueue;
struct iscsi_pdu *outqueue_current;
struct iscsi_pdu *waitpdu;
struct iscsi_in_pdu *incoming;
struct iscsi_pdu *outqueue; /* Protected by iscsi_lock */
struct iscsi_pdu *outqueue_current; /* Protected by iscsi_lock */
struct iscsi_pdu *waitpdu; /* Protected by iscsi_lock */
struct iscsi_in_pdu *incoming; /* Protected by iscsi_lock */
uint32_t max_burst_length;
uint32_t first_burst_length;
@@ -168,14 +193,10 @@ struct iscsi_context {
int log_level;
iscsi_log_fn log_fn;
int mallocs;
int reallocs;
int frees;
int smallocs;
void* smalloc_ptrs[SMALL_ALLOC_MAX_FREE];
int smalloc_free;
size_t smalloc_size;
int cache_allocations;
int mallocs; //needs protection?
int reallocs; //needs protection?
int frees; //needs protection?
int cache_allocations; //needs ptotection?
time_t next_reconnect;
int scsi_timeout;
@@ -184,6 +205,17 @@ struct iscsi_context {
int no_ua_on_reconnect;
void (*fd_dup_cb)(struct iscsi_context *iscsi, void *opaque);
void *fd_dup_opaque;
#ifdef HAVE_MULTITHREADING
int multithreading_enabled;
libiscsi_spinlock_t iscsi_lock;
libiscsi_mutex_t iscsi_mutex;
libiscsi_thread_t service_thread;
int poll_timeout;
#ifndef HAVE_STDATOMIC_H
libiscsi_mutex_t atomic_int_mutex;
#endif /* HAVE_STDATOMIC_H */
#endif /* HAVE_MULTITHREADING */
};
#define ISCSI_PDU_IMMEDIATE 0x40
@@ -308,10 +340,11 @@ void iscsi_cancel_pdus(struct iscsi_context *iscsi);
void iscsi_cancel_lun_pdus(struct iscsi_context *iscsi, uint32_t lun);
int iscsi_pdu_add_data(struct iscsi_context *iscsi, struct iscsi_pdu *pdu,
const unsigned char *dptr, int dsize);
int iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu);
void iscsi_queue_pdu(struct iscsi_context *iscsi, struct iscsi_pdu *pdu);
int iscsi_add_data(struct iscsi_context *iscsi, struct iscsi_data *data,
const unsigned char *dptr, int dsize, int pdualignment);
void iscsi_add_to_outqueue(struct iscsi_context *iscsi, struct iscsi_pdu *pdu);
struct scsi_task;
void iscsi_pdu_set_cdb(struct iscsi_pdu *pdu, struct scsi_task *task);
@@ -358,9 +391,6 @@ void* iscsi_zmalloc(struct iscsi_context *iscsi, size_t size);
void* iscsi_realloc(struct iscsi_context *iscsi, void* ptr, size_t size);
void iscsi_free(struct iscsi_context *iscsi, void* ptr);
char* iscsi_strdup(struct iscsi_context *iscsi, const char* str);
void* iscsi_smalloc(struct iscsi_context *iscsi, size_t size);
void* iscsi_szmalloc(struct iscsi_context *iscsi, size_t size);
void iscsi_sfree(struct iscsi_context *iscsi, void* ptr);
uint32_t crc32c(uint8_t *buf, int len);
void crc32c_init(uint32_t *crc_ptr);
@@ -381,9 +411,6 @@ void iscsi_decrement_iface_rr(void);
void __attribute__((format(printf, 3, 4)))
iscsi_log_message(struct iscsi_context *iscsi, int level, const char *format, ...);
void
iscsi_add_to_outqueue(struct iscsi_context *iscsi, struct iscsi_pdu *pdu);
int iscsi_serial32_compare(uint32_t s1, uint32_t s2);
uint32_t iscsi_itt_post_increment(struct iscsi_context *iscsi);
@@ -407,7 +434,7 @@ union socket_address;
typedef struct iscsi_transport {
int (*connect)(struct iscsi_context *iscsi, union socket_address *sa, int ai_family);
int (*queue_pdu)(struct iscsi_context *iscsi, struct iscsi_pdu *pdu);
void (*queue_pdu)(struct iscsi_context *iscsi, struct iscsi_pdu *pdu);
struct iscsi_pdu* (*new_pdu)(struct iscsi_context *iscsi, size_t size);
int (*disconnect)(struct iscsi_context *iscsi);
void (*free_pdu)(struct iscsi_context *iscsi, struct iscsi_pdu *pdu);

View File

@@ -1704,6 +1704,18 @@ iscsi_set_fd_dup_cb(struct iscsi_context *iscsi,
void (*cb)(struct iscsi_context *iscsi, void *opaque),
void *opaque);
/*
* MULTITHREADING
*/
/*
* This function starts a separate service thread for multithreading support.
*/
EXTERN int iscsi_mt_service_thread_start(struct iscsi_context *iscsi);
/*
* Shutdown multithreading support.
*/
EXTERN void iscsi_mt_service_thread_stop(struct iscsi_context *iscsi);
#ifdef __cplusplus
}
#endif