/*
 * ng_fwdswitch.h
 *
 * Copyright (c) 2002, Iscanet Internet Services S.r.l.
 * Copyright (c) 2000, Whistle Communications, Inc.
 * All rights reserved.
 * 
 * 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 or object code must include the above
 *    copyright notice, this list of conditions, the following disclaimer
 *    in the documentation and/or other materials provider with the
 *    distribution.
 * 2. Neither the name of the copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
 * OWNER 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.
 *
 * Author: Rocco Lucia <rlucia@iscanet.com>
 *
 * $Id: ng_fwdswitch.h,v 1.5 2004/02/12 01:52:16 rlucia Exp $
 */

#ifndef _NETGRAPH_NG_FWDSWITCH_H_
#define _NETGRAPH_NG_FWDSWITCH_H_

/* Node type name and magic cookie */
#define NG_FWDSWITCH_NODE_TYPE		"fwdswitch"
#define NGM_FWDSWITCH_COOKIE	200205221

/* Hook names */
#define NG_FWDSWITCH_HOOK_ONE		"in0"
#define NG_FWDSWITCH_HOOK_TWO		"in1"
#define NG_FWDSWITCH_HOOK_MANY_PREFIX	"out"	 /* append decimal integer */
#define NG_FWDSWITCH_HOOK_MANY_FMT	"out%d" /* for use with printf(3) */

/* Maximum number of supported "in" links */
#define NG_FWDSWITCH_MAX_INPUTS		64

/* Maximum number of supported "out" links */
#define NG_FWDSWITCH_MAX_LINKS		64

/* Link number used to indicate the "in" hook */
#define NG_FWDSWITCH_ONE_LINKNUM		(-1)
#define NG_FWDSWITCH_TWO_LINKNUM		(-2)

/* Algorithms for outgoing packet distribution (XXX only one so far) */
#define NG_FWDSWITCH_XMIT_ROUNDROBIN	1	/* round-robin delivery */

/* Algorithms for detecting link failure (XXX only one so far) */
#define NG_FWDSWITCH_FAIL_MANUAL		1	/* use enabledLinks[] array */

/* Node configuration structure */
struct ng_fwdswitch_config {
	u_int32_t	xmitAlg;		/* how to distribute packets */
	u_int32_t	failAlg;		/* how to detect link failure */
	u_char		enabledLinks[NG_FWDSWITCH_MAX_LINKS];
};

/* Keep this in sync with the above structure definition */
#define NG_FWDSWITCH_CONFIG_TYPE_INFO(atype)	{		\
	  { "xmitAlg",		&ng_parse_uint32_type	},	\
	  { "failAlg",		&ng_parse_uint32_type	},	\
	  { "enabledLinks",	(atype)			},	\
	  { NULL }						\
}

/* Forwarding switch configuration structure */
struct ng_fwdswitch_fwdconfig_entry {
	struct	in_addr	ipaddr;
	struct	in_addr	netmask;
	u_int8_t	dstlink;
	u_int32_t	matchcount;
};

struct ng_fwdswitch_fwdconfig {
	int8_t					fwd_deflink;
	int32_t					fwd_table_len;
	struct ng_fwdswitch_fwdconfig_entry	fwd_table[0];
};

#define NG_FWDSWITCH_FWDCONFIG_SIZE(numInsn)   \
        (sizeof(struct ng_fwdswitch_fwdconfig) + (numInsn) * sizeof(struct ng_fwdswitch_fwdconfig_entry))

#define	NG_FWDSWITCH_FWDCONFIG_TYPE_INFO(fctype)	{	\
	  { "defaultlink",	&ng_parse_int8_type	},	\
	  { "fwdtable_len",	&ng_parse_int32_type	},	\
	  { "fwdtable",		(fctype)		},	\
	  { NULL }						\
}

/* Statistics structure (one for each link) */
struct ng_fwdswitch_link_stats {
	u_int64_t	recvOctets;	/* total octets rec'd on link */
	u_int64_t	recvPackets;	/* total pkts rec'd on link */
	u_int64_t	xmitOctets;	/* total octets xmit'd on link */
	u_int64_t	xmitPackets;	/* total pkts xmit'd on link */
};

/* Keep this in sync with the above structure definition */
#define NG_FWDSWITCH_LINK_STATS_TYPE_INFO	{		\
	  { "recvOctets",	&ng_parse_uint64_type	},	\
	  { "recvPackets",	&ng_parse_uint64_type	},	\
	  { "xmitOctets",	&ng_parse_uint64_type	},	\
	  { "xmitPackets",	&ng_parse_uint64_type	},	\
	  { NULL }						\
}

/* Netgraph control messages */
enum {
	NGM_FWDSWITCH_SET_CONFIG,	/* set configuration */
	NGM_FWDSWITCH_GET_CONFIG,	/* get configuration */
	NGM_FWDSWITCH_GET_STATS,		/* get link stats */
	NGM_FWDSWITCH_CLR_STATS,		/* clear link stats */
	NGM_FWDSWITCH_GETCLR_STATS,	/* atomically get & clear link stats */
	NGM_FWDSWITCH_SET_FWDCONFIG,	/* set forwarding switch configuration */
	NGM_FWDSWITCH_GET_FWDCONFIG,	/* get forwarding switch configuration */
};

#endif /* _NETGRAPH_NG_FWDSWITCH_H_ */

