Annotation of embedaddon/istgt/src/istgt.h, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2008-2011 Daisuke Aoyama <aoyama@peach.ne.jp>.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  *
                     14:  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     15:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     16:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     17:  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
                     18:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     19:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     20:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     21:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     22:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     23:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     24:  * SUCH DAMAGE.
                     25:  *
                     26:  */
                     27: 
                     28: #ifndef ISTGT_H
                     29: #define ISTGT_H
                     30: 
                     31: #ifdef HAVE_CONFIG_H
                     32: #include "config.h"
                     33: #endif
                     34: 
                     35: #ifdef USE_ATOMIC
                     36: #ifdef HAVE_SYS_TYPES_H
                     37: #include <sys/types.h>
                     38: #endif
                     39: #ifdef HAVE_MACHINE_ATOMIC_H
                     40: #include <machine/atomic.h>
                     41: #endif
                     42: #ifdef HAVE_SYS_ATOMIC_H
                     43: #include <sys/atomic.h>
                     44: #endif
                     45: #endif /* USE_ATOMIC */
                     46: 
                     47: #include "build.h"
                     48: 
                     49: #include <pthread.h>
                     50: #include <signal.h>
                     51: #include "istgt_log.h"
                     52: #include "istgt_conf.h"
                     53: 
                     54: #define MAX_TMPBUF 1024
                     55: #define MAX_ADDRBUF 64
                     56: #define MAX_INITIATOR_ADDR (MAX_ADDRBUF)
                     57: #define MAX_INITIATOR_NAME 256
                     58: #define MAX_TARGET_ADDR (MAX_ADDRBUF)
                     59: #define MAX_TARGET_NAME 256
                     60: #define MAX_ISCSI_NAME 256
                     61: 
                     62: #define MAX_UCPORTAL 16
                     63: #define MAX_PORTAL 1024
                     64: #define MAX_INITIATOR 256
                     65: #define MAX_NETMASK 256
                     66: #define MAX_INITIATOR_GROUP 4096
                     67: #define MAX_LOGICAL_UNIT 4096
                     68: #define MAX_R2T 256
                     69: 
                     70: #define DEFAULT_CONFIG BUILD_ETC_ISTGT "/istgt.conf"
                     71: #define DEFAULT_PIDFILE "/var/run/istgt.pid"
                     72: #define DEFAULT_AUTHFILE BUILD_ETC_ISTGT "/auth.conf"
                     73: #if 0
                     74: #define DEFAULT_MEDIAFILE BUILD_ETC_ISTGT "/media.conf"
                     75: #define DEFAULT_LIVEFILE BUILD_ETC_ISTGT "/istgt.live"
                     76: #endif
                     77: #define DEFAULT_MEDIADIRECTORY BUILD_VAR_ISTGT
                     78: #define DEFAULT_NODEBASE "iqn.2007-09.jp.ne.peach.istgt"
                     79: #define DEFAULT_PORT 3260
                     80: #define DEFAULT_MAX_SESSIONS 16
                     81: #define DEFAULT_MAX_CONNECTIONS 4
                     82: #define DEFAULT_MAXOUTSTANDINGR2T 16
                     83: #define DEFAULT_DEFAULTTIME2WAIT 2
                     84: #define DEFAULT_DEFAULTTIME2RETAIN 20
                     85: #define DEFAULT_FIRSTBURSTLENGTH 65536
                     86: #define DEFAULT_MAXBURSTLENGTH 262144
                     87: #define DEFAULT_MAXRECVDATASEGMENTLENGTH 8192
                     88: #define DEFAULT_INITIALR2T 1
                     89: #define DEFAULT_IMMEDIATEDATA 1
                     90: #define DEFAULT_DATAPDUINORDER 1
                     91: #define DEFAULT_DATASEQUENCEINORDER 1
                     92: #define DEFAULT_ERRORRECOVERYLEVEL 0
                     93: #define DEFAULT_TIMEOUT 30
                     94: #define DEFAULT_NOPININTERVAL 20
                     95: #define DEFAULT_MAXR2T 16
                     96: 
                     97: #define ISTGT_PG_TAG_MAX 0x0000ffffU
                     98: #define ISTGT_LU_TAG_MAX 0x0000ffffU
                     99: #define ISTGT_UC_TAG     0x00010000U
                    100: #define ISTGT_IOBUFSIZE (2 * 1024 * 1024)
                    101: #define ISTGT_SNSBUFSIZE 4096
                    102: #define ISTGT_SHORTDATASIZE 8192
                    103: #define ISTGT_SHORTPDUSIZE (48+4+ISTGT_SHORTDATASIZE+4)
                    104: #define ISTGT_CONDWAIT (50 * 1000) /* ms */
                    105: #define ISTGT_CONDWAIT_MIN (5 * 1000) /* ms */
                    106: #define ISTGT_STACKSIZE (2 * 1024 * 1024)
                    107: 
                    108: #if defined (SIGRTMIN)
                    109: #define ISTGT_SIGWAKEUP (SIGRTMIN + 1)
                    110: #define ISTGT_USE_SIGRT
                    111: #elif defined (SIGIO)
                    112: #define ISTGT_SIGWAKEUP (SIGIO)
                    113: #else
                    114: #error "no signal for internal"
                    115: #endif
                    116: #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
                    117: #define ISTGT_USE_KQUEUE
                    118: #endif
                    119: 
                    120: #define MTX_LOCK(MTX) \
                    121:        do {                                                            \
                    122:                if (pthread_mutex_lock((MTX)) != 0) {                   \
                    123:                        ISTGT_ERRLOG("lock error\n");                   \
                    124:                        pthread_exit(NULL);                             \
                    125:                }                                                       \
                    126:        } while (0)
                    127: #define MTX_UNLOCK(MTX) \
                    128:        do {                                                            \
                    129:                if (pthread_mutex_unlock((MTX)) != 0) {                 \
                    130:                        ISTGT_ERRLOG("unlock error\n");                 \
                    131:                        pthread_exit(NULL);                             \
                    132:                }                                                       \
                    133:        } while (0)
                    134: #define SPIN_LOCK(SPIN) \
                    135:        do {                                                            \
                    136:                if (pthread_spin_lock((SPIN)) != 0) {                   \
                    137:                        ISTGT_ERRLOG("lock error\n");                   \
                    138:                        pthread_exit(NULL);                             \
                    139:                }                                                       \
                    140:        } while (0)
                    141: #define SPIN_UNLOCK(SPIN) \
                    142:        do {                                                            \
                    143:                if (pthread_spin_unlock((SPIN)) != 0) {                 \
                    144:                        ISTGT_ERRLOG("unlock error\n");                 \
                    145:                        pthread_exit(NULL);                             \
                    146:                }                                                       \
                    147:        } while (0)
                    148: 
                    149: typedef struct istgt_portal_t {
                    150:        char *label;
                    151:        char *host;
                    152:        char *port;
                    153:        int idx;
                    154:        int tag;
                    155:        int sock;
                    156: } PORTAL;
                    157: typedef PORTAL *PORTAL_Ptr;
                    158: 
                    159: typedef struct istgt_initiator_group_t {
                    160:        int ninitiators;
                    161:        char **initiators;
                    162:        int nnetmasks;
                    163:        char **netmasks;
                    164:        int idx;
                    165:        int tag;
                    166: } INITIATOR_GROUP;
                    167: typedef INITIATOR_GROUP *INITIATOR_GROUP_Ptr;
                    168: 
                    169: typedef enum {
                    170:        ISTGT_STATE_INVALID = 0,
                    171:        ISTGT_STATE_INITIALIZED = 1,
                    172:        ISTGT_STATE_RUNNING = 2,
                    173:        ISTGT_STATE_EXITING = 3,
                    174:        ISTGT_STATE_SHUTDOWN = 4,
                    175: } ISTGT_STATE;
                    176: 
                    177: #define DEFAULT_ISTGT_SWMODE ISTGT_SWMODE_NORMAL
                    178: typedef enum {
                    179:        ISTGT_SWMODE_TRADITIONAL = 0,
                    180:        ISTGT_SWMODE_NORMAL = 1,
                    181:        ISTGT_SWMODE_EXPERIMENTAL = 2,
                    182: } ISTGT_SWMODE;
                    183: 
                    184: typedef struct istgt_t {
                    185:        CONFIG *config;
                    186:        char *pidfile;
                    187:        char *authfile;
                    188: #if 0
                    189:        char *mediafile;
                    190:        char *livefile;
                    191: #endif
                    192:        char *mediadirectory;
                    193:        char *nodebase;
                    194: 
                    195:        pthread_attr_t attr;
                    196:        pthread_mutex_t mutex;
                    197: 
                    198:        ISTGT_STATE state;
                    199:        ISTGT_SWMODE swmode;
                    200: 
                    201:        int nuctl_portal;
                    202:        PORTAL uctl_portal[MAX_UCPORTAL];
                    203:        int nuctl_netmasks;
                    204:        char **uctl_netmasks;
                    205: 
                    206:        int nportal;
                    207:        PORTAL portal[MAX_PORTAL];
                    208:        int ninitiator_group;
                    209:        INITIATOR_GROUP initiator_group[MAX_INITIATOR_GROUP];
                    210:        int nlogical_unit;
                    211:        struct istgt_lu_t *logical_unit[MAX_LOGICAL_UNIT];
                    212: 
                    213:        int timeout;
                    214:        int nopininterval;
                    215:        int maxr2t;
                    216:        int no_discovery_auth;
                    217:        int req_discovery_auth;
                    218:        int req_discovery_auth_mutual;
                    219:        int discovery_auth_group;
                    220:        int no_uctl_auth;
                    221:        int req_uctl_auth;
                    222:        int req_uctl_auth_mutual;
                    223:        int uctl_auth_group;
                    224: 
                    225:        int MaxSessions;
                    226:        int MaxConnections;
                    227:        int MaxOutstandingR2T;
                    228:        int DefaultTime2Wait;
                    229:        int DefaultTime2Retain;
                    230:        int FirstBurstLength;
                    231:        int MaxBurstLength;
                    232:        int MaxRecvDataSegmentLength;
                    233:        int InitialR2T;
                    234:        int ImmediateData;
                    235:        int DataPDUInOrder;
                    236:        int DataSequenceInOrder;
                    237:        int ErrorRecoveryLevel;
                    238: } ISTGT;
                    239: typedef ISTGT *ISTGT_Ptr;
                    240: 
                    241: char *istgt_get_nmval(CF_SECTION *sp, const char *key, int idx1, int idx2);
                    242: char *istgt_get_nval(CF_SECTION *sp, const char *key, int idx);
                    243: char *istgt_get_val(CF_SECTION *sp, const char *key);
                    244: int istgt_get_nintval(CF_SECTION *sp, const char *key, int idx);
                    245: int istgt_get_intval(CF_SECTION *sp, const char *key);
                    246: 
                    247: #ifdef USE_ATOMIC
                    248: static inline int
                    249: istgt_get_state(ISTGT_Ptr istgt)
                    250: {
                    251:        ISTGT_STATE state;
                    252: #if defined HAVE_ATOMIC_LOAD_ACQ_INT
                    253:        state = atomic_load_acq_int((unsigned int *)&istgt->state);
                    254: #elif defined HAVE_ATOMIC_OR_UINT_NV
                    255:        state = (int)atomic_or_uint_nv((unsigned int *)&istgt->state, 0);
                    256: #else
                    257: #error "no atomic operation"
                    258: #endif
                    259:        return state;
                    260: }
                    261: static inline void
                    262: istgt_set_state(ISTGT_Ptr istgt, ISTGT_STATE state)
                    263: {
                    264: #if defined HAVE_ATOMIC_STORE_REL_INT
                    265:        atomic_store_rel_int((unsigned int *)&istgt->state, state);
                    266: #elif defined HAVE_ATOMIC_SWAP_UINT
                    267:        (void)atomic_swap_uint((unsigned int *)&istgt->state, state);
                    268: #if defined HAVE_MEMBAR_PRODUCER
                    269:        membar_producer();
                    270: #endif
                    271: #else
                    272: #error "no atomic operation"
                    273: #endif
                    274: }
                    275: #else
                    276: static inline int
                    277: istgt_get_state(ISTGT_Ptr istgt)
                    278: {
                    279:        ISTGT_STATE state;
                    280:        MTX_LOCK(&istgt->mutex);
                    281:        state = istgt->state;
                    282:        MTX_UNLOCK(&istgt->mutex);
                    283:        return state;
                    284: }
                    285: 
                    286: static inline void
                    287: istgt_set_state(ISTGT_Ptr istgt, ISTGT_STATE state)
                    288: {
                    289:        MTX_LOCK(&istgt->mutex);
                    290:        istgt->state = state;
                    291:        MTX_UNLOCK(&istgt->mutex);
                    292: }
                    293: #endif /* USE_ATOMIC */
                    294: 
                    295: #endif /* ISTGT_H */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>