Skip site navigation (1)Skip section navigation (2)
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
This is a multi-part message in MIME format.

------=_NextPart_000_009E_01C4D023.BE35B4C0
Content-Type: text/plain;
	format=flowed;
	charset="iso-8859-1";
	reply-type=original
Content-Transfer-Encoding: 8bit

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 

------=_NextPart_000_009E_01C4D023.BE35B4C0
Content-Type: application/octet-stream;
	name="patch.load.balance"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="patch.load.balance"

--- lib/libalias/alias.h.ori	Fri Nov 23 11:10:15 2001=0A=
+++ lib/libalias/alias.h	Mon Feb  3 22:51:59 2003=0A=
@@ -44,6 +44,8 @@=0A=
 /* Initialization and control functions. */=0A=
 void	 PacketAliasInit(void);=0A=
 void	 PacketAliasSetAddress(struct in_addr _addr);=0A=
+void	 PacketAliasSetBAddress(struct in_addr _addr);=0A=
+void	 PacketAliasSetBProb(double p);=0A=
 void	 PacketAliasSetFWBase(unsigned int _base, unsigned int _num);=0A=
 unsigned int=0A=
 	 PacketAliasSetMode(unsigned int _flags, unsigned int _mask);=0A=
@@ -169,6 +171,12 @@=0A=
  * PacketAliasOut() are reversed.=0A=
  */=0A=
 #define	PKT_ALIAS_REVERSE		0x80=0A=
+=0A=
+/*=0A=
+ * If PKT_ALIAS_LOAD_BALANCE is set, so we work a little bit different=0A=
+ * XXX=0A=
+ */=0A=
+#define PKT_ALIAS_LOAD_BALANCE		0x200=0A=
 =0A=
 /* Function return codes. */=0A=
 #define	PKT_ALIAS_ERROR			-1=0A=
=0A=
--- lib/libalias/alias_db.c.ori	Wed Jul 24 00:21:24 2002=0A=
+++ lib/libalias/alias_db.c	Mon Feb  3 23:10:13 2003=0A=
@@ -175,6 +175,7 @@=0A=
 /* Sizes of input and output link tables */=0A=
 #define LINK_TABLE_OUT_SIZE         101=0A=
 #define LINK_TABLE_IN_SIZE         4001=0A=
+#define LINK_TABLE_BAL_SIZE         101=0A=
 =0A=
 /* Parameters used for cleanup of expired links */=0A=
 #define ALIAS_CLEANUP_INTERVAL_SECS  60=0A=
@@ -320,6 +321,7 @@=0A=
 =0A=
     LIST_ENTRY(alias_link) list_out; /* Linked list of pointers for     =
*/=0A=
     LIST_ENTRY(alias_link) list_in;  /* input and output lookup tables  =
*/=0A=
+    LIST_ENTRY(alias_link) list_bal;=0A=
 =0A=
     union                        /* Auxiliary data                      =
*/=0A=
     {=0A=
@@ -346,6 +348,11 @@=0A=
 static struct in_addr aliasAddress;  /* Address written onto source     =
*/=0A=
                                      /*   field of IP packet.           =
*/=0A=
 =0A=
+static struct in_addr baliasAddress; /* Balance Address used for load	*/=0A=
+				     /* balancing - XXX			*/=0A=
+=0A=
+static double bprob;		     /* Balance probability             */=0A=
+=0A=
 static struct in_addr targetAddress; /* IP address incoming packets     =
*/=0A=
                                      /*   are sent to if no aliasing    =
*/=0A=
                                      /*   link already exists           =
*/=0A=
@@ -358,6 +365,8 @@=0A=
 static LIST_HEAD(, alias_link)       /*   link record is doubly indexed =
*/=0A=
 linkTableIn[LINK_TABLE_IN_SIZE];     /*   into input and output lookup  =
*/=0A=
                                      /*   tables.                       =
*/=0A=
+static LIST_HEAD(, alias_link)=0A=
+linkTableBal[LINK_TABLE_BAL_SIZE];=0A=
 =0A=
 static int icmpLinkCount;            /* Link statistics                 =
*/=0A=
 static int udpLinkCount;=0A=
@@ -423,6 +432,8 @@=0A=
 static u_int StartPointOut(struct in_addr, struct in_addr,=0A=
                            u_short, u_short, int);=0A=
 =0A=
+static u_int StartPointBal(struct in_addr, struct in_addr);=0A=
+=0A=
 static int SeqDiff(u_long, u_long);=0A=
 =0A=
 static void ShowAliasStats(void);=0A=
@@ -470,6 +481,15 @@=0A=
     return(n % LINK_TABLE_OUT_SIZE);=0A=
 }=0A=
 =0A=
+static u_int=0A=
+StartPointBal(struct in_addr src_addr, struct in_addr dst_addr)=0A=
+{=0A=
+    u_int n;=0A=
+    n  =3D src_addr.s_addr;=0A=
+    n +=3D dst_addr.s_addr;=0A=
+=0A=
+    return(n % LINK_TABLE_BAL_SIZE);=0A=
+}=0A=
 =0A=
 static int=0A=
 SeqDiff(u_long x, u_long y)=0A=
@@ -563,6 +583,9 @@=0A=
 FindLinkOut(struct in_addr, struct in_addr, u_short, u_short, int, int);=0A=
 =0A=
 static struct alias_link *=0A=
+FindLinkOut2(struct in_addr, struct in_addr);=0A=
+=0A=
+static struct alias_link *=0A=
 FindLinkIn(struct in_addr, struct in_addr, u_short, u_short, int, int);=0A=
 =0A=
 =0A=
@@ -940,6 +963,10 @@=0A=
 /* Adjust input table pointers */=0A=
     LIST_REMOVE(link, list_in);=0A=
 =0A=
+/* Adjust balance table pointers */=0A=
+    if (packetAliasMode & PKT_ALIAS_LOAD_BALANCE)=0A=
+        LIST_REMOVE(link, list_bal);=0A=
+=0A=
 /* Close socket, if one has been allocated */=0A=
     if (link->sockfd !=3D -1)=0A=
     {=0A=
@@ -1120,6 +1147,13 @@=0A=
     /* Set up pointers for input lookup table */=0A=
         start_point =3D StartPointIn(alias_addr, link->alias_port, =
link_type);=0A=
         LIST_INSERT_HEAD(&linkTableIn[start_point], link, list_in);=0A=
+=0A=
+    /* Set up pointers for balance lookup table */=0A=
+	if (packetAliasMode & PKT_ALIAS_LOAD_BALANCE)=0A=
+	{=0A=
+            start_point =3D StartPointBal(src_addr, dst_addr);=0A=
+            LIST_INSERT_HEAD(&linkTableBal[start_point], link, =
list_bal);=0A=
+	}=0A=
     }=0A=
     else=0A=
     {=0A=
@@ -1248,6 +1282,27 @@=0A=
     return(link);=0A=
 }=0A=
 =0A=
+static struct alias_link *=0A=
+FindLinkOut2(struct in_addr src_addr,=0A=
+            struct in_addr dst_addr)=0A=
+{=0A=
+    u_int i;=0A=
+    struct alias_link *link;=0A=
+    i =3D StartPointBal(src_addr, dst_addr);=0A=
+=0A=
+    LIST_FOREACH(link, &linkTableBal[i], list_bal)=0A=
+    {=0A=
+        if (link->src_addr.s_addr =3D=3D src_addr.s_addr=0A=
+         && link->server          =3D=3D NULL=0A=
+         && link->dst_addr.s_addr =3D=3D dst_addr.s_addr)=0A=
+        {=0A=
+            link->timestamp =3D timeStamp;=0A=
+            break;=0A=
+        }=0A=
+    }=0A=
+=0A=
+    return(link);=0A=
+}=0A=
 =0A=
 static struct alias_link *=0A=
 _FindLinkIn(struct in_addr dst_addr,=0A=
@@ -1628,7 +1681,7 @@=0A=
               int             create)=0A=
 {=0A=
     int link_type;=0A=
-    struct alias_link *link;=0A=
+    struct alias_link *link, *linkb;=0A=
 =0A=
     switch (proto)=0A=
     {=0A=
@@ -1650,6 +1703,23 @@=0A=
         struct in_addr alias_addr;=0A=
 =0A=
         alias_addr =3D FindAliasAddress(src_addr);=0A=
+=0A=
+	/* load balance hack - XXX */=0A=
+	if (packetAliasMode & PKT_ALIAS_LOAD_BALANCE)=0A=
+	{=0A=
+	    linkb =3D FindLinkOut2(src_addr, dst_addr);=0A=
+	    if (linkb =3D=3D NULL)=0A=
+	    {=0A=
+		if (alias_addr.s_addr =3D=3D aliasAddress.s_addr)=0A=
+		{=0A=
+		    if (random() < (long)(bprob * 0x7fffffff))=0A=
+			alias_addr =3D baliasAddress;=0A=
+		}=0A=
+	    } else=0A=
+		if (linkb->alias_addr.s_addr !=3D alias_addr.s_addr)=0A=
+		    alias_addr =3D linkb->alias_addr;=0A=
+	}=0A=
+=0A=
         link =3D AddLink(src_addr, dst_addr, alias_addr,=0A=
                        src_port, dst_port, GET_ALIAS_PORT,=0A=
                        link_type);=0A=
@@ -2518,6 +2588,25 @@=0A=
     aliasAddress =3D addr;=0A=
 }=0A=
 =0A=
+=0A=
+/* Set Balance Alias Address for load balancing - XXX */=0A=
+void=0A=
+PacketAliasSetBAddress(struct in_addr addr)=0A=
+{=0A=
+    baliasAddress =3D addr;=0A=
+    if (! (packetAliasMode & PKT_ALIAS_LOAD_BALANCE))=0A=
+	packetAliasMode |=3D PKT_ALIAS_LOAD_BALANCE;=0A=
+}=0A=
+=0A=
+/* Set Balance probability */=0A=
+void=0A=
+PacketAliasSetBProb(double p)=0A=
+{=0A=
+    if (p > 0 && p <=3D 1)=0A=
+	bprob =3D p;=0A=
+    else=0A=
+	bprob =3D 0;=0A=
+}=0A=
 =0A=
 void=0A=
 PacketAliasSetTarget(struct in_addr target_addr)=0A=
=0A=
--- sbin/natd/natd.c.ori	Fri Feb  1 07:18:32 2002=0A=
+++ sbin/natd/natd.c	Mon Feb  3 23:06:27 2003=0A=
@@ -113,6 +113,8 @@=0A=
 static	u_short			outPort;=0A=
 static	u_short			inOutPort;=0A=
 static	struct in_addr		aliasAddr;=0A=
+static	struct in_addr		baliasAddr;=0A=
+static	double			bprob;=0A=
 static 	int			dynamicMode;=0A=
 static  int			ifMTU;=0A=
 static	int			aliasOverhead;=0A=
@@ -150,6 +152,8 @@=0A=
 	running			=3D 1;=0A=
 	assignAliasAddr		=3D 0;=0A=
 	aliasAddr.s_addr	=3D INADDR_NONE;=0A=
+	baliasAddr.s_addr	=3D INADDR_NONE;=0A=
+	bprob			=3D 0;=0A=
 	aliasOverhead		=3D 12;=0A=
 	dynamicMode		=3D 0;=0A=
  	logDropped		=3D 0;=0A=
@@ -176,6 +180,16 @@=0A=
 	if (aliasAddr.s_addr !=3D INADDR_NONE && ifName !=3D NULL)=0A=
 		errx (1, "both alias address and interface "=0A=
 			 "name are not allowed");=0A=
+=0A=
+/*=0A=
+ * Check if load balance probability is Ok=0A=
+ */=0A=
+	if (baliasAddr.s_addr !=3D INADDR_NONE)=0A=
+	{=0A=
+		if (bprob < 0 || bprob > 100)=0A=
+			errx (1, "balance probability valid value is 0..100");=0A=
+	}=0A=
+=0A=
 /*=0A=
  * Check that valid port number is known.=0A=
  */=0A=
@@ -298,6 +312,17 @@=0A=
  */=0A=
 	if (aliasAddr.s_addr !=3D INADDR_NONE)=0A=
 		PacketAliasSetAddress (aliasAddr);=0A=
+=0A=
+/*=0A=
+ * Set balancing address if it has been set=0A=
+ */=0A=
+	if (baliasAddr.s_addr !=3D INADDR_NONE)=0A=
+	{=0A=
+		PacketAliasSetBAddress (baliasAddr);=0A=
+		if (bprob !=3D 0)=0A=
+			PacketAliasSetBProb (bprob / 100);=0A=
+	}=0A=
+=0A=
 /*=0A=
  * We need largest descriptor number for select.=0A=
  */=0A=
@@ -822,6 +847,8 @@=0A=
 	OutPort,=0A=
 	Port,=0A=
 	AliasAddress,=0A=
+	BAliasAddress,=0A=
+	BProb,=0A=
 	TargetAddress,=0A=
 	InterfaceName,=0A=
 	RedirectPort,=0A=
@@ -971,6 +998,22 @@=0A=
 		"alias_address",=0A=
 		"a" },=0A=
 	=0A=
+	{ BAliasAddress,=0A=
+		0,=0A=
+		Address,=0A=
+		"x.x.x.x",=0A=
+		"address to use for load balance",=0A=
+		"balance_adddress",=0A=
+		"b" },=0A=
+=0A=
+	{ BProb,=0A=
+		0,=0A=
+		Numeric,=0A=
+		"n",=0A=
+		"balance probability in percent",=0A=
+		"balance_prob",=0A=
+		"r" },=0A=
+=0A=
 	{ TargetAddress,=0A=
 		0,=0A=
 		Address,=0A=
@@ -1183,6 +1226,14 @@=0A=
 =0A=
 	case AliasAddress:=0A=
 		memcpy (&aliasAddr, &addrValue, sizeof (struct in_addr));=0A=
+		break;=0A=
+=0A=
+	case BAliasAddress:=0A=
+		memcpy (&baliasAddr, &addrValue, sizeof (struct in_addr));=0A=
+		break;=0A=
+=0A=
+	case BProb:=0A=
+		bprob =3D (double) numValue;=0A=
 		break;=0A=
 =0A=
 	case TargetAddress:=0A=

------=_NextPart_000_009E_01C4D023.BE35B4C0--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?00a101c4d03c$e3d9e660$f000a8c0>