#ifndef _ISNS_PDU_H_
#define _ISNS_PDU_H_
#include <pthread.h>
#include <string.h>
#include "isns_defs.h"
#include "isns_util.h"
#define ISNSP_VERSION (0x0001)
#define ISNS_MAX_PDU_PAYLOAD 65532
#define ISNS_BUF_SIZE 1024
#define ISNS_BUF_COUNT 32
#define ISNS_SMALL_BUF_SIZE \
(MAX(sizeof(struct isns_task_s), sizeof(struct isns_trans_s)))
#define ISNS_SMALL_BUF_COUNT 32
#define ISNS_BUF_POOL 1
#define ISNS_BUF_MALLOC 2
#define ISNS_BUF_STATIC 3
#define ISNS_INIT_BUFFER(_bufp, _len, _type) \
do if ((_bufp) != NULL) { \
((struct isns_buffer_s *)(_bufp))->cur_len = 0; \
((struct isns_buffer_s *)(_bufp))->alloc_len = (_len); \
((struct isns_buffer_s *)(_bufp))->alloc_len &= ~0x03; \
((struct isns_buffer_s *)(_bufp))->buf_type = (_type); \
((struct isns_buffer_s *)(_bufp))->next = NULL; \
} while (0)
struct isns_buffer_s {
uint32_t cur_len;
uint32_t alloc_len;
int buf_type;
struct isns_buffer_s *next;
};
#define isns_buffer_data(_bufp, _ofs) \
(void *)(((uint8_t *)(void *)(_bufp+1))+(_ofs))
void isns_init_buffer_pool(void);
int isns_add_buffer_pool(int, int);
void isns_destroy_buffer_pool(void);
struct isns_buffer_s *isns_new_buffer(int);
void isns_free_buffer(struct isns_buffer_s *);
#define ISNS_TLV_HDR_SIZE (sizeof(uint32_t) * 2)
#define ISNS_PAD4_LEN(n) (uint32_t)(((n)+3) & ~0x03)
#define ISNS_PAD4_BYTES(n) ((4 - ((n) & 0x03)) & 0x03)
static inline uint32_t ISNS_TLV_GET_TAG(const void *buf) {
uint32_t tag;
memcpy(&tag, buf, sizeof(tag));
return isns_ntohl(tag);
}
static inline void ISNS_TLV_SET_TAG(void *buf, uint32_t tag) {
tag = isns_htonl(tag);
memcpy(buf, &tag, sizeof(tag));
}
static inline uint32_t ISNS_TLV_GET_LEN(const void *buf) {
uint32_t len;
memcpy(&len, (const uint8_t *)buf + sizeof(len), sizeof(len));
return isns_ntohl(len);
}
static inline void ISNS_TLV_SET_LEN(void *buf, uint32_t len) {
len = isns_htonl(len);
memcpy((uint8_t *)buf + sizeof(len), &len, sizeof(len));
}
static inline void *ISNS_TLV_DATA_PTR(void *buf) {
return (uint8_t *)buf + ISNS_TLV_HDR_SIZE;
}
#define ISNS_TRANSF_COMPLETE 0x000000001
#define ISNS_TRANSF_FREE_WHEN_COMPLETE 0x000000002
struct isns_refresh_s {
char node[225];
int interval;
struct isns_trans_s *trans_p;
};
struct isns_get_tlv_info_s {
struct isns_pdu_s *pdu_p;
struct isns_buffer_s *buf_p;
struct isns_buffer_s *extra_buf_list;
int buf_ofs;
};
struct isns_trans_s {
uint16_t id;
uint16_t func_id;
uint32_t flags;
struct isns_config_s *cfg_p;
struct isns_get_tlv_info_s get_tlv_info;
struct isns_pdu_s *pdu_req_list;
struct isns_pdu_s *pdu_rsp_list;
uint16_t disconnect_cnt;
};
struct isns_pdu_hdr_s {
uint16_t isnsp_version;
uint16_t func_id;
uint16_t payload_len;
uint16_t flags;
uint16_t trans_id;
uint16_t seq_id;
};
struct isns_pdu_s {
struct isns_config_s *cfg_p;
struct isns_pdu_hdr_s hdr;
int byteorder_host;
struct isns_buffer_s *payload_p;
struct isns_pdu_s *next;
};
#define isns_get_pdu_request(_trans) \
(((_trans) == ISNS_INVALID_TRANS) \
? NULL : ((struct isns_trans_s *)(_trans))->pdu_req_list)
#define isns_get_pdu_response(_trans) \
(((_trans) == ISNS_INVALID_TRANS) \
? NULL : ((struct isns_trans_s *)(_trans))->pdu_rsp_list)
#define isns_get_next_pdu(_pdu) \
(((_pdu) == NULL) ? NULL : ((struct isns_pdu_s *)(_pdu))->next)
#define isns_get_trans_flags(_trans) \
isns_set_trans_flags((_trans), 0)
void isns_complete_trans(struct isns_trans_s *);
int isns_abort_trans(struct isns_config_s *, uint16_t);
uint32_t isns_set_trans_flags(struct isns_trans_s *, uint32_t);
void isns_add_pdu_request(struct isns_trans_s *, struct isns_pdu_s *);
void isns_add_pdu_response(struct isns_trans_s *, struct isns_pdu_s *);
struct isns_pdu_s *isns_get_pdu_request_tail(struct isns_trans_s *);
int isns_get_pdu_response_status(struct isns_trans_s *, uint32_t *);
struct isns_pdu_s *isns_new_pdu(struct isns_config_s *, uint16_t, uint16_t,
uint16_t);
void isns_free_pdu(struct isns_pdu_s *);
int isns_send_pdu(ISNS_TRANS, struct isns_pdu_s *, const struct timespec *);
#ifdef ISNS_DEBUG
#define DUMP_PDU(_pdu) isns_dump_pdu(_pdu)
void isns_dump_pdu(struct isns_pdu_s *);
#else
#define DUMP_PDU(_pdu)
#endif
#endif