[BACK]Return to isns_pdu.h CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libisns

File: [cvs.NetBSD.org] / src / lib / libisns / isns_pdu.h (download)

Revision 1.3, Mon Nov 28 12:44:18 2011 UTC (12 years, 4 months ago) by joerg
Branch: MAIN
CVS Tags: yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20190609, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9-3-RELEASE, netbsd-9-2-RELEASE, netbsd-9-1-RELEASE, netbsd-9-0-RELEASE, netbsd-9-0-RC2, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, matt-nb8-mediatek-base, matt-nb8-mediatek, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, localcount-20160914, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, agc-symver-base, agc-symver
Branch point for: phil-wifi
Changes since 1.2: +7 -7 lines

Kill a bunch of manual __attribute__ usages and replace them with the
equivalent sys/cdefs.h invocation.

/*	$NetBSD: isns_pdu.h,v 1.3 2011/11/28 12:44:18 joerg Exp $	*/

/*-
 * Copyright (c) 2004,2009 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Wasabi Systems, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * isns_pdu.h
 */

#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)

/*
 * Max PDU payload length (MUST be <= 65532 and a multiple of 4).
 */
#define ISNS_MAX_PDU_PAYLOAD	65532

/*
 * ISNS buffer pool defines.
 */
#define ISNS_BUF_SIZE		1024
#define ISNS_BUF_COUNT		32

#define ISNS_SMALL_BUF_SIZE	\
    (/* CONSTCOND */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

/*
 * ISNS_INIT_BUFFER - initialize buffer (pointer) provided as an isns_buffer_s,
 *		      setting the length and type specified, and initializing
 *		      other members to appropriate values.
 */
#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 (/* CONSTCOND */0)


/*
 * ISNS buffer struct - used for trans, pdu, payload allocations in libisns.
 */
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 *);



/*
 * TLV buffer access/manipulation-related macros.
 */
#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;
}

/*
 * ISNS transaction and PDU structs.
 */

#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	__packed;
	uint16_t func_id	__packed;
	uint16_t payload_len	__packed;
	uint16_t flags		__packed;
	uint16_t trans_id	__packed;
	uint16_t seq_id		__packed;
};

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 /* !_ISNS_PDU_H_ */