isns_pdu.h revision 1.2.2.1       1  1.2.2.1      yamt /*	$NetBSD: isns_pdu.h,v 1.2.2.1 2012/04/17 00:05:28 yamt Exp $	*/
      2      1.1       agc 
      3      1.1       agc /*-
      4      1.1       agc  * Copyright (c) 2004,2009 The NetBSD Foundation, Inc.
      5      1.1       agc  * All rights reserved.
      6      1.1       agc  *
      7      1.1       agc  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1       agc  * by Wasabi Systems, Inc.
      9      1.1       agc  *
     10      1.1       agc  * Redistribution and use in source and binary forms, with or without
     11      1.1       agc  * modification, are permitted provided that the following conditions
     12      1.1       agc  * are met:
     13      1.1       agc  * 1. Redistributions of source code must retain the above copyright
     14      1.1       agc  *    notice, this list of conditions and the following disclaimer.
     15      1.1       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1       agc  *    notice, this list of conditions and the following disclaimer in the
     17      1.1       agc  *    documentation and/or other materials provided with the distribution.
     18      1.1       agc  *
     19      1.1       agc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1       agc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1       agc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1       agc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1       agc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1       agc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1       agc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1       agc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1       agc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1       agc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1       agc  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1       agc  */
     31      1.1       agc 
     32      1.1       agc /*
     33      1.1       agc  * isns_pdu.h
     34      1.1       agc  */
     35      1.1       agc 
     36      1.1       agc #ifndef _ISNS_PDU_H_
     37      1.1       agc #define _ISNS_PDU_H_
     38      1.1       agc 
     39      1.1       agc #include <pthread.h>
     40      1.2  christos #include <string.h>
     41      1.1       agc 
     42      1.1       agc #include "isns_defs.h"
     43      1.2  christos #include "isns_util.h"
     44      1.1       agc 
     45      1.1       agc #define ISNSP_VERSION		(0x0001)
     46      1.1       agc 
     47      1.1       agc /*
     48      1.1       agc  * Max PDU payload length (MUST be <= 65532 and a multiple of 4).
     49      1.1       agc  */
     50      1.1       agc #define ISNS_MAX_PDU_PAYLOAD	65532
     51      1.1       agc 
     52      1.1       agc /*
     53      1.1       agc  * ISNS buffer pool defines.
     54      1.1       agc  */
     55      1.1       agc #define ISNS_BUF_SIZE		1024
     56      1.1       agc #define ISNS_BUF_COUNT		32
     57      1.1       agc 
     58      1.1       agc #define ISNS_SMALL_BUF_SIZE	\
     59      1.1       agc     (/* CONSTCOND */MAX(sizeof(struct isns_task_s), sizeof(struct isns_trans_s)))
     60      1.1       agc #define ISNS_SMALL_BUF_COUNT	32
     61      1.1       agc 
     62      1.1       agc #define ISNS_BUF_POOL		1
     63      1.1       agc #define ISNS_BUF_MALLOC		2
     64      1.1       agc #define ISNS_BUF_STATIC		3
     65      1.1       agc 
     66      1.1       agc /*
     67      1.1       agc  * ISNS_INIT_BUFFER - initialize buffer (pointer) provided as an isns_buffer_s,
     68      1.1       agc  *		      setting the length and type specified, and initializing
     69      1.1       agc  *		      other members to appropriate values.
     70      1.1       agc  */
     71      1.1       agc #define ISNS_INIT_BUFFER(_bufp, _len, _type)					\
     72      1.1       agc 	do if ((_bufp) != NULL) {						\
     73      1.1       agc 		((struct isns_buffer_s *)(_bufp))->cur_len = 0;			\
     74      1.1       agc 		((struct isns_buffer_s *)(_bufp))->alloc_len = (_len);		\
     75      1.1       agc 		((struct isns_buffer_s *)(_bufp))->alloc_len &= ~0x03;		\
     76      1.1       agc 		((struct isns_buffer_s *)(_bufp))->buf_type = (_type);		\
     77      1.1       agc 		((struct isns_buffer_s *)(_bufp))->next = NULL;			\
     78      1.1       agc 	} while (/* CONSTCOND */0)
     79      1.1       agc 
     80      1.1       agc 
     81      1.1       agc /*
     82      1.1       agc  * ISNS buffer struct - used for trans, pdu, payload allocations in libisns.
     83      1.1       agc  */
     84      1.1       agc struct isns_buffer_s {
     85      1.1       agc 	uint32_t cur_len;
     86      1.1       agc 	uint32_t alloc_len;
     87      1.1       agc 	int buf_type;
     88      1.1       agc 
     89      1.1       agc 	struct isns_buffer_s *next;
     90      1.1       agc };
     91      1.1       agc 
     92      1.1       agc #define isns_buffer_data(_bufp, _ofs)					\
     93      1.1       agc 	(void *)(((uint8_t *)(void *)(_bufp+1))+(_ofs))
     94      1.1       agc 
     95      1.1       agc void isns_init_buffer_pool(void);
     96      1.1       agc int isns_add_buffer_pool(int, int);
     97      1.1       agc void isns_destroy_buffer_pool(void);
     98      1.1       agc struct isns_buffer_s *isns_new_buffer(int);
     99      1.1       agc void isns_free_buffer(struct isns_buffer_s *);
    100      1.1       agc 
    101      1.1       agc 
    102      1.1       agc 
    103      1.1       agc /*
    104      1.1       agc  * TLV buffer access/manipulation-related macros.
    105      1.1       agc  */
    106      1.2  christos #define ISNS_TLV_HDR_SIZE	(sizeof(uint32_t) * 2)
    107      1.1       agc 
    108      1.1       agc #define ISNS_PAD4_LEN(n)	(uint32_t)(((n)+3) & ~0x03)
    109      1.1       agc #define ISNS_PAD4_BYTES(n)	((4 - ((n) & 0x03)) & 0x03)
    110      1.1       agc 
    111      1.2  christos static inline uint32_t ISNS_TLV_GET_TAG(const void *buf) {
    112      1.2  christos 	uint32_t tag;
    113      1.2  christos 	memcpy(&tag, buf, sizeof(tag));
    114      1.2  christos 	return isns_ntohl(tag);
    115      1.2  christos }
    116      1.2  christos 
    117      1.2  christos static inline void ISNS_TLV_SET_TAG(void *buf, uint32_t tag) {
    118      1.2  christos 	tag = isns_htonl(tag);
    119      1.2  christos 	memcpy(buf, &tag, sizeof(tag));
    120      1.2  christos }
    121      1.2  christos 
    122      1.2  christos static inline uint32_t ISNS_TLV_GET_LEN(const void *buf) {
    123      1.2  christos 	uint32_t len;
    124      1.2  christos 	memcpy(&len, (const uint8_t *)buf + sizeof(len), sizeof(len));
    125      1.2  christos 	return isns_ntohl(len);
    126      1.2  christos }
    127      1.2  christos 
    128      1.2  christos static inline void ISNS_TLV_SET_LEN(void *buf, uint32_t len) {
    129      1.2  christos 	len = isns_htonl(len);
    130      1.2  christos 	memcpy((uint8_t *)buf + sizeof(len), &len, sizeof(len));
    131      1.2  christos }
    132      1.2  christos 
    133      1.2  christos static inline void *ISNS_TLV_DATA_PTR(void *buf) {
    134      1.2  christos 	return (uint8_t *)buf + ISNS_TLV_HDR_SIZE;
    135      1.2  christos }
    136      1.1       agc 
    137      1.1       agc /*
    138      1.1       agc  * ISNS transaction and PDU structs.
    139      1.1       agc  */
    140      1.1       agc 
    141      1.1       agc #define ISNS_TRANSF_COMPLETE			0x000000001
    142      1.1       agc #define ISNS_TRANSF_FREE_WHEN_COMPLETE		0x000000002
    143      1.1       agc 
    144      1.1       agc 
    145      1.1       agc struct isns_refresh_s {
    146      1.1       agc 	char node[225];
    147      1.1       agc 	int interval;
    148      1.1       agc 
    149      1.1       agc 	struct isns_trans_s *trans_p;
    150      1.1       agc };
    151      1.1       agc 
    152      1.1       agc struct isns_get_tlv_info_s {
    153      1.1       agc 	struct isns_pdu_s *pdu_p;
    154      1.1       agc 	struct isns_buffer_s *buf_p;
    155      1.1       agc 	struct isns_buffer_s *extra_buf_list;
    156      1.1       agc 	int buf_ofs;
    157      1.1       agc };
    158      1.1       agc 
    159      1.1       agc struct isns_trans_s {
    160      1.1       agc 	uint16_t id;
    161      1.1       agc 	uint16_t func_id;
    162      1.1       agc 	uint32_t flags;
    163      1.1       agc 	struct isns_config_s *cfg_p;
    164      1.1       agc 
    165      1.1       agc 	struct isns_get_tlv_info_s get_tlv_info;
    166      1.1       agc 
    167      1.1       agc 	struct isns_pdu_s *pdu_req_list;
    168      1.1       agc 	struct isns_pdu_s *pdu_rsp_list;
    169      1.1       agc 
    170      1.1       agc 	uint16_t disconnect_cnt;
    171      1.1       agc };
    172      1.1       agc 
    173      1.1       agc struct isns_pdu_hdr_s {
    174  1.2.2.1      yamt 	uint16_t isnsp_version	__packed;
    175  1.2.2.1      yamt 	uint16_t func_id	__packed;
    176  1.2.2.1      yamt 	uint16_t payload_len	__packed;
    177  1.2.2.1      yamt 	uint16_t flags		__packed;
    178  1.2.2.1      yamt 	uint16_t trans_id	__packed;
    179  1.2.2.1      yamt 	uint16_t seq_id		__packed;
    180      1.1       agc };
    181      1.1       agc 
    182      1.1       agc struct isns_pdu_s {
    183      1.1       agc 	struct isns_config_s *cfg_p;
    184      1.1       agc 	struct isns_pdu_hdr_s hdr;
    185      1.1       agc 	int byteorder_host;
    186      1.1       agc 	struct isns_buffer_s *payload_p;
    187      1.1       agc 	struct isns_pdu_s *next;
    188      1.1       agc };
    189      1.1       agc 
    190      1.1       agc 
    191      1.1       agc #define isns_get_pdu_request(_trans)					\
    192      1.1       agc 	(((_trans) == ISNS_INVALID_TRANS)				\
    193      1.1       agc 	    ? NULL : ((struct isns_trans_s *)(_trans))->pdu_req_list)
    194      1.1       agc 
    195      1.1       agc #define isns_get_pdu_response(_trans)					\
    196      1.1       agc 	(((_trans) == ISNS_INVALID_TRANS)				\
    197      1.1       agc 	    ? NULL : ((struct isns_trans_s *)(_trans))->pdu_rsp_list)
    198      1.1       agc 
    199      1.1       agc #define isns_get_next_pdu(_pdu)						\
    200      1.1       agc 	(((_pdu) == NULL) ? NULL : ((struct isns_pdu_s *)(_pdu))->next)
    201      1.1       agc 
    202      1.1       agc #define isns_get_trans_flags(_trans)					\
    203      1.1       agc 	isns_set_trans_flags((_trans), 0)
    204      1.1       agc 
    205      1.1       agc 
    206      1.1       agc void isns_complete_trans(struct isns_trans_s *);
    207      1.1       agc int isns_abort_trans(struct isns_config_s *, uint16_t);
    208      1.1       agc 
    209      1.1       agc uint32_t isns_set_trans_flags(struct isns_trans_s *, uint32_t);
    210      1.1       agc void isns_add_pdu_request(struct isns_trans_s *, struct isns_pdu_s *);
    211      1.1       agc void isns_add_pdu_response(struct isns_trans_s *, struct isns_pdu_s *);
    212      1.1       agc struct isns_pdu_s *isns_get_pdu_request_tail(struct isns_trans_s *);
    213      1.1       agc int isns_get_pdu_response_status(struct isns_trans_s *, uint32_t *);
    214      1.1       agc 
    215      1.1       agc struct isns_pdu_s *isns_new_pdu(struct isns_config_s *, uint16_t, uint16_t,
    216      1.1       agc     uint16_t);
    217      1.1       agc void isns_free_pdu(struct isns_pdu_s *);
    218      1.1       agc int isns_send_pdu(ISNS_TRANS, struct isns_pdu_s *, const struct timespec *);
    219      1.1       agc 
    220      1.1       agc #ifdef ISNS_DEBUG
    221      1.1       agc #define DUMP_PDU(_pdu)	isns_dump_pdu(_pdu)
    222      1.1       agc void isns_dump_pdu(struct isns_pdu_s *);
    223      1.1       agc #else
    224      1.1       agc #define DUMP_PDU(_pdu)
    225      1.1       agc #endif
    226      1.1       agc 
    227      1.1       agc 
    228      1.1       agc #endif /* !_ISNS_PDU_H_ */
    229