# /etc/conf.d/net and multiple interfaces

## exazoid

Hi. 

I have a setup with multiple interfaces (eth0..2) . 

eth0 is dhcp controlled so it is set up correctly automaticly. 

I have set static ip-addresses for both eth1 and eth2, but I am having trouble to figure out how to set the "gateway" variable in the end of /etc/conf.d/net, I tried (w/o success) : 

gateway="eth1/192.168.0.1 eth2/192.168.1.1"

Any help is greatly appriciated  :Wink: 

Exazoid

----------

## steveb

why don't you add the stuff in /etc/conf.d/local.start:

```
route add default gw 192.168.0.1 dev eth1

route add default gw 192.168.1.1 dev eth2
```

or

```
route add 0.0.0.0 gw 192.168.0.1 dev eth1

route add 0.0.0.0 gw 192.168.1.1 dev eth2
```

cheers

SteveB

----------

## klieber

 *exazoid wrote:*   

> I am having trouble to figure out how to set the "gateway" variable in the end of /etc/conf.d/net, I tried (w/o success) : 
> 
> gateway="eth1/192.168.0.1 eth2/192.168.1.1"

 

```
gateway="eth1/192.168.0.1"

gateway="eth2/192.168.1.1"

```

--kurt

----------

## exazoid

 *klieber wrote:*   

>  *exazoid wrote:*   I am having trouble to figure out how to set the "gateway" variable in the end of /etc/conf.d/net, I tried (w/o success) : 
> 
> gateway="eth1/192.168.0.1 eth2/192.168.1.1" 
> 
> ```
> ...

 

Doesn't they overwrite each other (ie. first line is killed...) ?

----------

## klieber

 *exazoid wrote:*   

> Doesn't they overwrite each other (ie. first line is killed...) ?

 

Not if you've followed the instructions in the install guide correctly and created separate init scripts for each of your interaces.  (code listing 23.3)

--kurt

----------

## exazoid

 *klieber wrote:*   

>  *exazoid wrote:*   Doesn't they overwrite each other (ie. first line is killed...) ? 
> 
> Not if you've followed the instructions in the install guide correctly and created separate init scripts for each of your interaces.  (code listing 23.3)
> 
> --kurt

 

I mean, the file is sourced ( = parsed regular by bash) so two lines after each other must collide.

Anyways, the machine is not standing at my place, and guess who forgot to compile in XFS support while upgrading kernel   :Embarassed: 

----------

## klieber

 *exazoid wrote:*   

> I mean, the file is sourced ( = parsed regular by bash) so two lines after each other must collide.

 

No, they don't.  Not if you have separate init scripts for each ethernet interface.  Read /etc/init.d/net.eth0 to see how this is done.  Here's the relevant snippet:

```
        if [ -n "${gateway}" ] && [ "${gateway%/*}" = "${IFACE}" ]

        then

                ebegin "  Setting default gateway"

                /sbin/route add default gw ${gateway#*/} dev ${gateway%/*} \

                        netmask 0.0.0.0 metric 1 >/dev/null || {

                        local error=$?

                        ifconfig ${IFACE} down &>/dev/null

                        eend ${error} "Failed to bring ${IFACE} up"

                        stop

                        return ${error}

                }

                eend 0

        fi

```

--kurt

----------

## jukka

i think exazoid is right. let's see:

to get their config data, all /etc/init.d/net.* scripts source /etc/conf.d/net, don't they? if variable 'gateway' is set as you told, it is first assigned the value 'eth1/192.168.0.1', but this is subsequently overwritten with 'eth2/192.168.1.1'.

when /etc/init.d/net.eth1 is executed, the line

```
if [ -n "${gateway}" ] && [ "${gateway%/*}" = "${IFACE}" ]
```

returns 1, so the route command is not executed, and no default gateway is set for eth1.

when /etc/init.d/net.eth2 is executed, both tests after the if clause return zero, so the default gateway is set.

btw, if i was wrong, why do you have to set the variables iface_ethN, dhcpcd_ethN, alias_ethN, broadcast_ethN and netmask_ethN for every ethernet interface N?

greetings, jukka

----------

## klieber

Welp -- don't know without combing through the net init script.  I do know, however, that that's what my net config file looks like on my firewall and it works for me. 

--kurt

----------

## Naan Yaar

This thread covers the same problem.  One solution is to have your gateway line as you had it at first:

```

gateway="eth1/192.168.0.1 eth2/192.168.1.1"

```

and modify net.ethX to add this snippet:

```

gateway=`echo $gateway|sed 's#.*\('${IFACE}'/[^ ]*\).*#\1#'`

```

before the line that says:

```

if [ -n "${gateway}" ] && [ "${gateway%/*}" = "${IFACE}" ]

```

Note: I haven't tried it out myself, so YMMV.

----------

## relyt

Isn't the "gateway" variable only for the default route?  (ie: there's only one)

----------

## Naan Yaar

There can be more than one "default" route though with the "Equal cost multipath" routing option enabled in the kernel.

 *relyt wrote:*   

> Isn't the "gateway" variable only for the default route?  (ie: there's only one)

 

----------

## exazoid

No, when you have multiple NICs with multiple connections out to the big cyberspace, you need a default route per connection AND a system default. To make sure the computer is answering through the right ip-number when processing requests.

In "plain ip", you write something like : 

```

# setup table1

ip route add $GW1_NET dev $GW1_IF src $GW1_IP table $GW1_TABLE

ip route add default via $GW1_GATEWAY table $GW1_TABLE

# setup table2

ip route add $GW2_NET dev $GW2_IF src $GW2_IP table $GW2_TABLE

ip route add default via $GW2_GATEWAY table $GW2_TABLE

# add the two routes (may be redundant)

ip route add $GW1_NET dev $GW1_IF src $GW1_IP

ip route add $GW2_NET dev $GW2_IF src $GW2_IP

# add rules to let interfaces use right table

ip rule add from $GW1_IP table $GW1_TABLE

ip rule add from $GW2_IP table $GW2_TABLE

# add default gateway (ie. the one this systems uses)

ip route add default via $DFL_GATEWAY

# or if you want to loadbalance

#ip route add default equalize nexthop via $GW1_GATEWAY dev $GW1_IF nexthop via $GW2_GATEWAY dev $GW2_IF

```

----------

## relyt

 *exazoid wrote:*   

> No, when you have multiple NICs with multiple connections out to the big cyberspace, you need a default route per connection AND a system default. To make sure the computer is answering through the right ip-number when processing requests.
> 
> 

 

Ah yes.  I was referring to the system default.  I thought that was all that "gateway" set.

----------

## relyt

 *Naan Yaar wrote:*   

> There can be more than one "default" route though with the "Equal cost multipath" routing option enabled in the kernel.
> 
> 

 

I haven't played with this yet.  Does it just allow you to assign a route to multiple devices?  Or does it create a meta-device which balances between physical devices?

----------

## Naan Yaar

AFAIK, you would assign multiple default routes to multiple interfaces.  The kernel will do "load balancing" on an IP by IP basis.

 *relyt wrote:*   

> ...
> 
> I haven't played with this yet.  Does it just allow you to assign a route to multiple devices?  Or does it create a meta-device which balances between physical devices?

 

----------

## exazoid

The way the kernel does it actually depends a little on your setup. AFAIK, if you specify : 

```
ip route add default nexthop via $ip1 dev $if1 nexthop via $ip2 dev $if2
```

Then it would be on IP-basis (ie. all requests to nn.nn.nn.nn wold go through the same route)

But if you do : 

```
ip route add default equalize nexthop via $ip1 dev $if1 nexthop via $ip2 dev $if2
```

Then it will be on stream to stream basis meaning each UDP packet will be load balanced and each distinct TPC-stream will be loadbalanced (even to the same IP)

----------

