为什么当linux环境中多个MAC在同一子网域内时内核总是通过某一个MAC来发送数据包

作者:Davis Zhang

这个问题是linux kernel TCP/IP Stack对于同一子网的IP处理方式决定的,严格来说是个linux普遍问题,不在于xilinx device、IP或者driver。

当eth0和eth1的IP 地址在一个subnet,TCP/IP stack会选取一个MAC作为主,eth0和eth1收到的ping包都会通过这个主MAC发送返回包,通常来说ifconfig先使能哪个MAC,它就是主MAC。比如eth0为主MAC,这个时候可以通过eth0的网线来ping eth1的IP,返回包也是直接通过eth0发送,就是说对eth1 IP的ping包不会在eth1收到,也不通过它发送,stack直接作出回应,并通过eth0发送。如果这个时候通过eth1的网线发送对eth1 IP的ping包,eth1可以收到,但是stack还是会通过eth0发送返回包,现象就是ping不通。

如果eth0和eth1的IP不在同一subnet,比如192.168.1.10/192.168.2.10,就没有这些问题。通常不建议在linux里对多个MAC使用同一subnet,甚至有些vendor禁止这样做。

https://access.redhat.com/solutions/30564
https://www.ibm.com/support/pages/node/6466713

如果你那里确实需要使用同一subnet,可以参考下面的方法来重新设置路由。

//the below steps redirects packets meant to be output from eth0 to properly exit from eth1.

//enable support for multiple routing tables in kernel config.

Kernel Configuration

→ Networking support → Networking options

[*] IP: advanced router

[*] IP: policy routing

CONFIG_IP_ADVANCED_ROUTER

CONFIG_IP_MULTIPLE_TABLES

//type below command in linux console

echo -ne 0 > /proc/sys/net/ipv4/conf/all/rp_filter
echo -ne 0 > /proc/sys/net/ipv4/conf/eth0/rp_filter
echo -ne 0 > /proc/sys/net/ipv4/conf/eth1/rp_filter

//For proper functionality i.e. ARP replies from eth1 to get generated when both eth0 and eth1 are in same subnet
echo -ne 0 > /proc/sys/net/ipv4/conf/all/arp_filter
echo -ne 2 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo -ne 0 > /proc/sys/net/ipv4/conf/eth0/arp_filter
echo -ne 2 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
echo -ne 0 > /proc/sys/net/ipv4/conf/eth1/arp_filter
echo -ne 2 > /proc/sys/net/ipv4/conf/eth1/arp_ignore

//Create a table called "new_rt_table" and create a routing rule that says any packet with a mark equal to '1' gets routed according to the "new_rt_table"(can name it whatever you want) table. The file /etc/iproute2/rt_tables is the only source of table names on the system. Internally, routing tables have integer identifiers.

echo 1 new_rt_table >> /etc/iproute2/rt_tables
ip rule add from all fwmark 1 table new_rt_table

//setup the "new_rt_table" table to route the packets via eth1
ip route add default dev eth1 table new_rt_table
ip route show table new_rt_table

//mark packets so that 'ip route' can route it through eth1
iptables -F -t mangle
iptables -t mangle -I OUTPUT -s -o eth0 -j MARK --set-mark 1

最新文章

最新文章