Date: Sun, 21 Nov 2004 23:42:20 -0300 From: =?iso-8859-1?Q?Luiz_Ot=E1vio_Souza?= <luiz@microeletronica.com.br> To: <freebsd-net@freebsd.org> Message-ID: <00a101c4d03c$e3d9e660$f000a8c0@ad.adseguros.com.br>
next in thread | raw e-mail | index | archive | help
[-- Attachment #1 --]
Hi,
I've write this patch (before pf port, but still on use) for
libalias/natd that permit a second alias address (-b or balance_address on
natd) and use probability for second address (-r or balance_prob on natd).
The -r option works with percent, from 0 to 100. With 0 the second alias
address is disabled, with 100 the first alias address (-a) is disabled.
With this, i can do load balance with two different links and compensate
the difference.
This patch work for me and i think that can work for others.
Thanks for understanding my really bad english,
______________________
Luiz Otávio Souza
luiz@microeletronica.com.br
14.8111.5025
[-- Attachment #2 --]
--- lib/libalias/alias.h.ori Fri Nov 23 11:10:15 2001
+++ lib/libalias/alias.h Mon Feb 3 22:51:59 2003
@@ -44,6 +44,8 @@
/* Initialization and control functions. */
void PacketAliasInit(void);
void PacketAliasSetAddress(struct in_addr _addr);
+void PacketAliasSetBAddress(struct in_addr _addr);
+void PacketAliasSetBProb(double p);
void PacketAliasSetFWBase(unsigned int _base, unsigned int _num);
unsigned int
PacketAliasSetMode(unsigned int _flags, unsigned int _mask);
@@ -169,6 +171,12 @@
* PacketAliasOut() are reversed.
*/
#define PKT_ALIAS_REVERSE 0x80
+
+/*
+ * If PKT_ALIAS_LOAD_BALANCE is set, so we work a little bit different
+ * XXX
+ */
+#define PKT_ALIAS_LOAD_BALANCE 0x200
/* Function return codes. */
#define PKT_ALIAS_ERROR -1
--- lib/libalias/alias_db.c.ori Wed Jul 24 00:21:24 2002
+++ lib/libalias/alias_db.c Mon Feb 3 23:10:13 2003
@@ -175,6 +175,7 @@
/* Sizes of input and output link tables */
#define LINK_TABLE_OUT_SIZE 101
#define LINK_TABLE_IN_SIZE 4001
+#define LINK_TABLE_BAL_SIZE 101
/* Parameters used for cleanup of expired links */
#define ALIAS_CLEANUP_INTERVAL_SECS 60
@@ -320,6 +321,7 @@
LIST_ENTRY(alias_link) list_out; /* Linked list of pointers for */
LIST_ENTRY(alias_link) list_in; /* input and output lookup tables */
+ LIST_ENTRY(alias_link) list_bal;
union /* Auxiliary data */
{
@@ -346,6 +348,11 @@
static struct in_addr aliasAddress; /* Address written onto source */
/* field of IP packet. */
+static struct in_addr baliasAddress; /* Balance Address used for load */
+ /* balancing - XXX */
+
+static double bprob; /* Balance probability */
+
static struct in_addr targetAddress; /* IP address incoming packets */
/* are sent to if no aliasing */
/* link already exists */
@@ -358,6 +365,8 @@
static LIST_HEAD(, alias_link) /* link record is doubly indexed */
linkTableIn[LINK_TABLE_IN_SIZE]; /* into input and output lookup */
/* tables. */
+static LIST_HEAD(, alias_link)
+linkTableBal[LINK_TABLE_BAL_SIZE];
static int icmpLinkCount; /* Link statistics */
static int udpLinkCount;
@@ -423,6 +432,8 @@
static u_int StartPointOut(struct in_addr, struct in_addr,
u_short, u_short, int);
+static u_int StartPointBal(struct in_addr, struct in_addr);
+
static int SeqDiff(u_long, u_long);
static void ShowAliasStats(void);
@@ -470,6 +481,15 @@
return(n % LINK_TABLE_OUT_SIZE);
}
+static u_int
+StartPointBal(struct in_addr src_addr, struct in_addr dst_addr)
+{
+ u_int n;
+ n = src_addr.s_addr;
+ n += dst_addr.s_addr;
+
+ return(n % LINK_TABLE_BAL_SIZE);
+}
static int
SeqDiff(u_long x, u_long y)
@@ -563,6 +583,9 @@
FindLinkOut(struct in_addr, struct in_addr, u_short, u_short, int, int);
static struct alias_link *
+FindLinkOut2(struct in_addr, struct in_addr);
+
+static struct alias_link *
FindLinkIn(struct in_addr, struct in_addr, u_short, u_short, int, int);
@@ -940,6 +963,10 @@
/* Adjust input table pointers */
LIST_REMOVE(link, list_in);
+/* Adjust balance table pointers */
+ if (packetAliasMode & PKT_ALIAS_LOAD_BALANCE)
+ LIST_REMOVE(link, list_bal);
+
/* Close socket, if one has been allocated */
if (link->sockfd != -1)
{
@@ -1120,6 +1147,13 @@
/* Set up pointers for input lookup table */
start_point = StartPointIn(alias_addr, link->alias_port, link_type);
LIST_INSERT_HEAD(&linkTableIn[start_point], link, list_in);
+
+ /* Set up pointers for balance lookup table */
+ if (packetAliasMode & PKT_ALIAS_LOAD_BALANCE)
+ {
+ start_point = StartPointBal(src_addr, dst_addr);
+ LIST_INSERT_HEAD(&linkTableBal[start_point], link, list_bal);
+ }
}
else
{
@@ -1248,6 +1282,27 @@
return(link);
}
+static struct alias_link *
+FindLinkOut2(struct in_addr src_addr,
+ struct in_addr dst_addr)
+{
+ u_int i;
+ struct alias_link *link;
+ i = StartPointBal(src_addr, dst_addr);
+
+ LIST_FOREACH(link, &linkTableBal[i], list_bal)
+ {
+ if (link->src_addr.s_addr == src_addr.s_addr
+ && link->server == NULL
+ && link->dst_addr.s_addr == dst_addr.s_addr)
+ {
+ link->timestamp = timeStamp;
+ break;
+ }
+ }
+
+ return(link);
+}
static struct alias_link *
_FindLinkIn(struct in_addr dst_addr,
@@ -1628,7 +1681,7 @@
int create)
{
int link_type;
- struct alias_link *link;
+ struct alias_link *link, *linkb;
switch (proto)
{
@@ -1650,6 +1703,23 @@
struct in_addr alias_addr;
alias_addr = FindAliasAddress(src_addr);
+
+ /* load balance hack - XXX */
+ if (packetAliasMode & PKT_ALIAS_LOAD_BALANCE)
+ {
+ linkb = FindLinkOut2(src_addr, dst_addr);
+ if (linkb == NULL)
+ {
+ if (alias_addr.s_addr == aliasAddress.s_addr)
+ {
+ if (random() < (long)(bprob * 0x7fffffff))
+ alias_addr = baliasAddress;
+ }
+ } else
+ if (linkb->alias_addr.s_addr != alias_addr.s_addr)
+ alias_addr = linkb->alias_addr;
+ }
+
link = AddLink(src_addr, dst_addr, alias_addr,
src_port, dst_port, GET_ALIAS_PORT,
link_type);
@@ -2518,6 +2588,25 @@
aliasAddress = addr;
}
+
+/* Set Balance Alias Address for load balancing - XXX */
+void
+PacketAliasSetBAddress(struct in_addr addr)
+{
+ baliasAddress = addr;
+ if (! (packetAliasMode & PKT_ALIAS_LOAD_BALANCE))
+ packetAliasMode |= PKT_ALIAS_LOAD_BALANCE;
+}
+
+/* Set Balance probability */
+void
+PacketAliasSetBProb(double p)
+{
+ if (p > 0 && p <= 1)
+ bprob = p;
+ else
+ bprob = 0;
+}
void
PacketAliasSetTarget(struct in_addr target_addr)
--- sbin/natd/natd.c.ori Fri Feb 1 07:18:32 2002
+++ sbin/natd/natd.c Mon Feb 3 23:06:27 2003
@@ -113,6 +113,8 @@
static u_short outPort;
static u_short inOutPort;
static struct in_addr aliasAddr;
+static struct in_addr baliasAddr;
+static double bprob;
static int dynamicMode;
static int ifMTU;
static int aliasOverhead;
@@ -150,6 +152,8 @@
running = 1;
assignAliasAddr = 0;
aliasAddr.s_addr = INADDR_NONE;
+ baliasAddr.s_addr = INADDR_NONE;
+ bprob = 0;
aliasOverhead = 12;
dynamicMode = 0;
logDropped = 0;
@@ -176,6 +180,16 @@
if (aliasAddr.s_addr != INADDR_NONE && ifName != NULL)
errx (1, "both alias address and interface "
"name are not allowed");
+
+/*
+ * Check if load balance probability is Ok
+ */
+ if (baliasAddr.s_addr != INADDR_NONE)
+ {
+ if (bprob < 0 || bprob > 100)
+ errx (1, "balance probability valid value is 0..100");
+ }
+
/*
* Check that valid port number is known.
*/
@@ -298,6 +312,17 @@
*/
if (aliasAddr.s_addr != INADDR_NONE)
PacketAliasSetAddress (aliasAddr);
+
+/*
+ * Set balancing address if it has been set
+ */
+ if (baliasAddr.s_addr != INADDR_NONE)
+ {
+ PacketAliasSetBAddress (baliasAddr);
+ if (bprob != 0)
+ PacketAliasSetBProb (bprob / 100);
+ }
+
/*
* We need largest descriptor number for select.
*/
@@ -822,6 +847,8 @@
OutPort,
Port,
AliasAddress,
+ BAliasAddress,
+ BProb,
TargetAddress,
InterfaceName,
RedirectPort,
@@ -971,6 +998,22 @@
"alias_address",
"a" },
+ { BAliasAddress,
+ 0,
+ Address,
+ "x.x.x.x",
+ "address to use for load balance",
+ "balance_adddress",
+ "b" },
+
+ { BProb,
+ 0,
+ Numeric,
+ "n",
+ "balance probability in percent",
+ "balance_prob",
+ "r" },
+
{ TargetAddress,
0,
Address,
@@ -1183,6 +1226,14 @@
case AliasAddress:
memcpy (&aliasAddr, &addrValue, sizeof (struct in_addr));
+ break;
+
+ case BAliasAddress:
+ memcpy (&baliasAddr, &addrValue, sizeof (struct in_addr));
+ break;
+
+ case BProb:
+ bprob = (double) numValue;
break;
case TargetAddress:
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?00a101c4d03c$e3d9e660$f000a8c0>
