1 /* $NetBSD: isns_pdu.h,v 1.5 2022/04/19 20:32:16 rillig 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 #include <string.h>
41
42 #include "isns_defs.h"
43 #include "isns_util.h"
44
45 #define ISNSP_VERSION (0x0001)
46
47 /*
48 * Max PDU payload length (MUST be <= 65532 and a multiple of 4).
49 */
50 #define ISNS_MAX_PDU_PAYLOAD 65532
51
52 /*
53 * ISNS buffer pool defines.
54 */
55 #define ISNS_BUF_SIZE 1024
56 #define ISNS_BUF_COUNT 32
57
58 #define ISNS_SMALL_BUF_SIZE \
59 (/* CONSTCOND */MAX(sizeof(struct isns_task_s), sizeof(struct isns_trans_s)))
60 #define ISNS_SMALL_BUF_COUNT 32
61
62 #define ISNS_BUF_POOL 1
63 #define ISNS_BUF_MALLOC 2
64 #define ISNS_BUF_STATIC 3
65
66 /*
67 * ISNS_INIT_BUFFER - initialize buffer (pointer) provided as an isns_buffer_s,
68 * setting the length and type specified, and initializing
69 * other members to appropriate values.
70 */
71 #define ISNS_INIT_BUFFER(_bufp, _len, _type) \
72 do if ((_bufp) != NULL) { \
73 ((struct isns_buffer_s *)(_bufp))->cur_len = 0; \
74 ((struct isns_buffer_s *)(_bufp))->alloc_len = (_len); \
75 ((struct isns_buffer_s *)(_bufp))->alloc_len &= ~0x03; \
76 ((struct isns_buffer_s *)(_bufp))->buf_type = (_type); \
77 ((struct isns_buffer_s *)(_bufp))->next = NULL; \
78 } while (0)
79
80
81 /*
82 * ISNS buffer struct - used for trans, pdu, payload allocations in libisns.
83 */
84 struct isns_buffer_s {
85 uint32_t cur_len;
86 uint32_t alloc_len;
87 int buf_type;
88
89 struct isns_buffer_s *next;
90 };
91
92 #define isns_buffer_data(_bufp, _ofs) \
93 (void *)(((uint8_t *)(void *)(_bufp+1))+(_ofs))
94
95 void isns_init_buffer_pool(void);
96 int isns_add_buffer_pool(int, int);
97 void isns_destroy_buffer_pool(void);
98 struct isns_buffer_s *isns_new_buffer(int);
99 void isns_free_buffer(struct isns_buffer_s *);
100
101
102
103 /*
104 * TLV buffer access/manipulation-related macros.
105 */
106 #define ISNS_TLV_HDR_SIZE (sizeof(uint32_t) * 2)
107
108 #define ISNS_PAD4_LEN(n) (uint32_t)(((n)+3) & ~0x03)
109 #define ISNS_PAD4_BYTES(n) ((4 - ((n) & 0x03)) & 0x03)
110
ISNS_TLV_GET_TAG(const void * buf)111 static inline uint32_t ISNS_TLV_GET_TAG(const void *buf) {
112 uint32_t tag;
113 memcpy(&tag, buf, sizeof(tag));
114 return isns_ntohl(tag);
115 }
116
ISNS_TLV_SET_TAG(void * buf,uint32_t tag)117 static inline void ISNS_TLV_SET_TAG(void *buf, uint32_t tag) {
118 tag = isns_htonl(tag);
119 memcpy(buf, &tag, sizeof(tag));
120 }
121
ISNS_TLV_GET_LEN(const void * buf)122 static inline uint32_t ISNS_TLV_GET_LEN(const void *buf) {
123 uint32_t len;
124 memcpy(&len, (const uint8_t *)buf + sizeof(len), sizeof(len));
125 return isns_ntohl(len);
126 }
127
ISNS_TLV_SET_LEN(void * buf,uint32_t len)128 static inline void ISNS_TLV_SET_LEN(void *buf, uint32_t len) {
129 len = isns_htonl(len);
130 memcpy((uint8_t *)buf + sizeof(len), &len, sizeof(len));
131 }
132
ISNS_TLV_DATA_PTR(void * buf)133 static inline void *ISNS_TLV_DATA_PTR(void *buf) {
134 return (uint8_t *)buf + ISNS_TLV_HDR_SIZE;
135 }
136
137 /*
138 * ISNS transaction and PDU structs.
139 */
140
141 #define ISNS_TRANSF_COMPLETE 0x000000001
142 #define ISNS_TRANSF_FREE_WHEN_COMPLETE 0x000000002
143
144
145 struct isns_refresh_s {
146 char node[225];
147 int interval;
148
149 struct isns_trans_s *trans_p;
150 };
151
152 struct isns_get_tlv_info_s {
153 struct isns_pdu_s *pdu_p;
154 struct isns_buffer_s *buf_p;
155 struct isns_buffer_s *extra_buf_list;
156 int buf_ofs;
157 };
158
159 struct isns_trans_s {
160 uint16_t id;
161 uint16_t func_id;
162 uint32_t flags;
163 struct isns_config_s *cfg_p;
164
165 struct isns_get_tlv_info_s get_tlv_info;
166
167 struct isns_pdu_s *pdu_req_list;
168 struct isns_pdu_s *pdu_rsp_list;
169
170 uint16_t disconnect_cnt;
171 };
172
173 struct isns_pdu_hdr_s {
174 uint16_t isnsp_version;
175 uint16_t func_id;
176 uint16_t payload_len;
177 uint16_t flags;
178 uint16_t trans_id;
179 uint16_t seq_id;
180 };
181
182 struct isns_pdu_s {
183 struct isns_config_s *cfg_p;
184 struct isns_pdu_hdr_s hdr;
185 int byteorder_host;
186 struct isns_buffer_s *payload_p;
187 struct isns_pdu_s *next;
188 };
189
190
191 #define isns_get_pdu_request(_trans) \
192 (((_trans) == ISNS_INVALID_TRANS) \
193 ? NULL : ((struct isns_trans_s *)(_trans))->pdu_req_list)
194
195 #define isns_get_pdu_response(_trans) \
196 (((_trans) == ISNS_INVALID_TRANS) \
197 ? NULL : ((struct isns_trans_s *)(_trans))->pdu_rsp_list)
198
199 #define isns_get_next_pdu(_pdu) \
200 (((_pdu) == NULL) ? NULL : ((struct isns_pdu_s *)(_pdu))->next)
201
202 #define isns_get_trans_flags(_trans) \
203 isns_set_trans_flags((_trans), 0)
204
205
206 void isns_complete_trans(struct isns_trans_s *);
207 int isns_abort_trans(struct isns_config_s *, uint16_t);
208
209 uint32_t isns_set_trans_flags(struct isns_trans_s *, uint32_t);
210 void isns_add_pdu_request(struct isns_trans_s *, struct isns_pdu_s *);
211 void isns_add_pdu_response(struct isns_trans_s *, struct isns_pdu_s *);
212 struct isns_pdu_s *isns_get_pdu_request_tail(struct isns_trans_s *);
213 int isns_get_pdu_response_status(struct isns_trans_s *, uint32_t *);
214
215 struct isns_pdu_s *isns_new_pdu(struct isns_config_s *, uint16_t, uint16_t,
216 uint16_t);
217 void isns_free_pdu(struct isns_pdu_s *);
218 int isns_send_pdu(ISNS_TRANS, struct isns_pdu_s *, const struct timespec *);
219
220 #ifdef ISNS_DEBUG
221 #define DUMP_PDU(_pdu) isns_dump_pdu(_pdu)
222 void isns_dump_pdu(struct isns_pdu_s *);
223 #else
224 #define DUMP_PDU(_pdu)
225 #endif
226
227
228 #endif /* !_ISNS_PDU_H_ */
229