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