# Need help with load balancing and gentoo init scripts

## mamunata

Hello,

I have a small office network with 10 PCs and router running gentoo. Internet connection is with pppoe and static LAN IP address (actually there is two internet connections). I want to implement load balancing with gentoo init scripts.

Here is my network configuration:

```

modules=("iproute2")

config_eth0=("192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255")

routes_eth0=("192.168.1.0/24 dev eth0 src 192.168.1.2 table btc"

                "default via 192.168.1.1"

                "192.168.1.0/24 dev eth0 src 192.168.1.2"

                "192.168.1.0/24 dev eth0 table spectrum")

config_eth2=("192.168.200.1 netmask 255.255.255.0 broadcast 192.168.200.255")

routes_eth2=( "192.168.200.0/24 dev eth2 table btc"

                "192.168.200.0/24 dev eth2 table spectrum")

config_lo=("127.0.0.1 netmask=255.0.0.0 broadcast 127.255.255.255")

routes_lo=("127.0.0.0/8 dev lo table btc"

        "127.0.0.0/8 dev lo table spectrum")

config_eth1=("null")

config_ppp0=( "ppp" )

link_ppp0="eth1"

plugins_ppp0="pppoe"

username_ppp0="username"

password_ppp0="password"

pppd_ppp0=( "debug"

        "auth"

        "nodefaultroute"

        "passive"

        "persist"

        "hide-password"

        "ipcp-accept-remote"

        "ipcp-accept-local"

        "chap-interval 60"

        "holdoff 3"

        "lcp-echo-interval 15"

        "lcp-echo-failure 3"

        "mru 1492"

        "mtu 1492")

routes_ppp0=("82.103.64.0/18 dev ppp0 src 82.103.xxx.xxx table spectrum"

                "default via 82.103.xxx.xxx table spectrum"

                "82.103.64.0/18 dev ppp0 src 82.103.xxx.xxx

                "82.103.64.0/18 dev ppp0 table btc")

depend_ppp0() {

        need net.eth1

}

```

eth2 is interface connected to my office lan  - 192.168.200.0/24

eth0 is interface connected to internet through static IP address - 192.168.1.1

and the second interface connected to internet is eth1 - ppp0 with IP=82.103.xxx.xxx 

I added two tables in /etc/iproute2/rt_tables - btc & spectrum according to ISP names and added routes to each interface.

So the problem(s) is:

1. How can I add rules to this routing tables

2. If ppp0 fails to start I can't make use of multipath routes because of missing ppp0 device. I tried to make ppp0 with static IP address but no success

3. Is the upper configuration correct or I'm missing something.

Thanks in advance - any suggestions would be appreciated.

----------

## gerdesj

I went through the multi-link palava over quite some time. 

The problem is determining when a link is down.  

If you implement policy based routing (and using the postup scripts in /etc/conf.d/net and ifplugd), then you will be able to get automatic failover/load balancing if the physical links are up or down.  However you need something that can check something remote via a particular route using eg ICMP to detect when a link is really there.

I can give you a recipe to do the policy based routing but I can't do the real link checking and I never did get to the bottom of it.  I am not a progammer really.

I then discovered pfSense and binned all attempts to do the above.  I hate to say it but unless someone can prove otherwise the BSD boys and girls have got this sewn up.  

I have many Gentoo systems and so do my customers but for funky routing with more than one link it is pfSense all the way.

I hate myself for this but if you want a really clever firewall and router (with IPSEC, OpenVPN and CARP built in) and a nice web GUI then see http://www.pfsense.com/

Use the Gentoo box for what it is good at (everything else apart from this)

If anyone else can point me and the original poster in the right direction then that would be great.

Cheers

Jon

----------

## mrness

I assume you have a static IP address on ppp0, otherwise you cannot set src on those routes in this way. Even in this case, it is safer to write /etc/ppp/ip-{up,down}/ scripts for the ppp routes because in this way you can parameterize your scripts with the negociated local & remote addresses.

I see one mistake in routes_ppp0. You forgot to put a double quote at the end of the third route.

Anyway, I don't see the connection between load balancing and the routes posted by you. A load balancing route would be a route with multiple nexthops, such as the following:

```
ip route add default nexthop via 192.168.1.1 dev eth0 weight 1 nexthop dev ppp0 weight 1
```

Being a route on multiple interfaces, you cannot stick it into a routes_xxx parameter. You have to write a postup function which will check what interfaces are available and either set a simple default route or, when both external interfaces are started, set a load balancing default route. 

Careful though, you have to policy route packets based on source IP address like this

```
    ip rule add prio 50 from 192.168.1.0/24 lookup btc

    ip rule add prio 50 from  $PPP0_ADDRESS lookup spectrum
```

If you also use NAT, you will have to mark packets based on conntrack information and add similar rules, but this time with "fwmark ..." (e.g. connections that were NATed using the btc IP address will have to be routed through btc).

----------

## mrness

 *gerdesj wrote:*   

> Use the Gentoo box for what it is good at (everything else apart from this)

 

Really? And why is that? Could that be because you don't have a clue how to use it?  :Shocked: 

iproute2 is the same on all distributions, but we have something called baselayout. I doubt you will find something similar in distributions like RedHat or Suse.  :Twisted Evil: 

----------

## mamunata

mrness, thank you for replies

I know my config file is incomplete but I was wondering where to put rules & routes for *real* load balancing. 

May be I will try to write rules for checking if interface is up in postup and if so to add rules for load balancing. But I'm still casting about how to do that .........

----------

## mamunata

there is an annoying moment with my network configuration - i saw similar problems in forum but not resolved

the problem is that when starting the ppp interface i receive: "WARNING: net.ppp0 has started, but is inactive

" and as result postup() function, where i want to check if ppp0 interface is up, is not executed

any suggestions ???

----------

## gerdesj

 *mrness wrote:*   

>  *gerdesj wrote:*   Use the Gentoo box for what it is good at (everything else apart from this) 
> 
> Really? And why is that? Could that be because you don't have a clue how to use it? 
> 
> 

 

I am a great fan of baselayout and as you say it is unmatched amongst Linux distros.  I have spent countless hours trying to do what mamunata is asking but after I achieved what he is asking I found that I could not get it to deal effectively with a link out further on.  

I have three ADSL links with a PPPoA router attached and then four NICs in a Gentoo box.  One each for the routers and one for the LAN.  I then set up the routes in "net" and a post up script to add/remove rules and ifplugd to manage it all.  That's fine if you get a link failure between any of the routers and the Gentoo box.  However if one of the ADSL links drops you can't detect that using just baselayout without using some sort of daemon (or cron job) to use ICMP to ping the ADSL interface on the routers to determine a failure and remove the route.

No one ever replied to my requests for help on this in the past and when I found pfSense I just gave up.  It is easy to do this very job on it.

So, I'd say I have a very good idea of how baselayout works and I think this extra functionality would make a fine addition to it.  I am unable to do it myself without assistance.  Perhaps you would care to show me?  This will also assist mamunata 

 *Quote:*   

> iproute2 is the same on all distributions

 

It may well be but pfSense does not use iproute2.  The clue is in the name - pf is used in FreeBSD and not Linux.

Cheers

Jon

----------

## mrness

 *mamunata wrote:*   

> the problem is that when starting the ppp interface i receive: "WARNING: net.ppp0 has started, but is inactive
> 
> " and as result postup() function, where i want to check if ppp0 interface is up, is not executed

 

postup will be executed by pppd every time the PPP link goes up. It is NOT executed by the process that starts the PPP daemon because at the end of the pppd startup the link might still be down.

I had to configure a backup connection so I did it like this:

  - ip rules: added/removed in postup/predown functions, depending on the interface name

  - table specific ip routes : added/removed through routes_*

  - exception: since my backup PPP link has a dynamic IP, I had to use /etc/ppp/ip-{up,down}.d scripts for setting rules and routes (at least rules depend on the IP address of the interface)

I don't use load balancing routes, but postup() can be easily modified to install the proper default route depending on the started interfaces. You will have to write some sh code, but it is not that complicated.

----------

## mamunata

 *mrness wrote:*   

> 
> 
> postup will be executed by pppd every time the PPP link goes up. It is NOT executed by the process that starts the PPP daemon because at the end of the pppd startup the link might still be down.
> 
> 

 

so you mean that postup() function will be triggered when interface ppp0 is up, or may be i misunderstand something ......

----------

## mrness

 *mamunata wrote:*   

> so you mean that postup() function will be triggered when interface ppp0 is up, or may be i misunderstand something ......

 

yep, postup() will be called every time ppp0 goes up. The inactive->started transition is performed by /etc/ppp/ip-up.d/50-initd.sh script.

----------

