一、arp_ignore和arp_announce介紹
arp_ignore和arp_announce參數(shù)都和ARP協(xié)議相關(guān),主要用于控制系統(tǒng)返回arp響應(yīng)和發(fā)送arp請求時的動作。這兩個參數(shù)很重要,特別是在LVS的DR場景下,它們的配置直接影響到DR轉(zhuǎn)發(fā)是否正常。
首先看一下Linux內(nèi)核文檔中對于它們的描述:
arp_ignore - INTEGER
Define different modes for sending replies in response to
received ARP requests that resolve local target IP addresses:
0 - (default): reply for any local target IP address, configured
on any interface
1 - reply only if the target IP address is local address
configured on the incoming interface
2 - reply only if the target IP address is local address
configured on the incoming interface and both with the
sender's IP address are part from same subnet on this interface
3 - do not reply for local addresses configured with scope host,
only resolutions for global and link addresses are replied
4-7 - reserved
8 - do not reply for all local addresses
The max value from conf/{all,interface}/arp_ignore is used
when ARP request is received on the {interface}
arp_ignore參數(shù)的作用是控制系統(tǒng)在收到外部的arp請求時,是否要返回arp響應(yīng)。
arp_ignore參數(shù)常用的取值主要有0,1,2,3~8較少用到:
0:響應(yīng)任意網(wǎng)卡上接收到的對本機(jī)IP地址的arp請求(包括環(huán)回網(wǎng)卡上的地址),而不管該目的IP是否在接收網(wǎng)卡上。
1:只響應(yīng)目的IP地址為接收網(wǎng)卡上的本地地址的arp請求。
2:只響應(yīng)目的IP地址為接收網(wǎng)卡上的本地地址的arp請求,并且arp請求的源IP必須和接收網(wǎng)卡同網(wǎng)段。
3:如果ARP請求數(shù)據(jù)包所請求的IP地址對應(yīng)的本地地址其作用域(scope)為主機(jī)(host),則不回應(yīng)ARP響應(yīng)數(shù)據(jù)包,如果作用域?yàn)槿郑╣lobal)或鏈路(link),則回應(yīng)ARP響應(yīng)數(shù)據(jù)包。
4~7:保留未使用
8:不回應(yīng)所有的arp請求
sysctl.conf中包含all和eth/lo(具體網(wǎng)卡)的arp_ignore參數(shù),取其中較大的值生效。
arp_announce - INTEGER
Define different restriction levels for announcing the local
source IP address from IP packets in ARP requests sent on
interface:
0 - (default) Use any local address, configured on any interface
1 - Try to avoid local addresses that are not in the target's
subnet for this interface. This mode is useful when target
hosts reachable via this interface require the source IP
address in ARP requests to be part of their logical network
configured on the receiving interface. When we generate the
request we will check all our subnets that include the
target IP and will preserve the source address if it is from
such subnet. If there is no such subnet we select source
address according to the rules for level 2.
2 - Always use the best local address for this target.
In this mode we ignore the source address in the IP packet
and try to select local address that we prefer for talks with
the target host. Such local address is selected by looking
for primary IP addresses on all our subnets on the outgoing
interface that include the target IP address. If no suitable
local address is found we select the first local address
we have on the outgoing interface or on all other interfaces,
with the hope we will receive reply for our request and
even sometimes no matter the source IP address we announce.
The max value from conf/{all,interface}/arp_announce is used.
arp_announce的作用是控制系統(tǒng)在對外發(fā)送arp請求時,如何選擇arp請求數(shù)據(jù)包的源IP地址。(比如系統(tǒng)準(zhǔn)備通過網(wǎng)卡發(fā)送一個數(shù)據(jù)包a,這時數(shù)據(jù)包a的源IP和目的IP一般都是知道的,而根據(jù)目的IP查詢路由表,發(fā)送網(wǎng)卡也是確定的,故源MAC地址也是知道的,這時就差確定目的MAC地址了。而想要獲取目的IP對應(yīng)的目的MAC地址,就需要發(fā)送arp請求。arp請求的目的IP自然就是想要獲取其MAC地址的IP,而arp請求的源IP是什么呢? 可能第一反應(yīng)會以為肯定是數(shù)據(jù)包a的源IP地址,但是這個也不是一定的,arp請求的源IP是可以選擇的,控制這個地址如何選擇就是arp_announce的作用)
arp_announce參數(shù)常用的取值有0,1,2。
0:允許使用任意網(wǎng)卡上的IP地址作為arp請求的源IP,通常就是使用數(shù)據(jù)包a的源IP。
1:盡量避免使用不屬于該發(fā)送網(wǎng)卡子網(wǎng)的本地地址作為發(fā)送arp請求的源IP地址。
2:忽略IP數(shù)據(jù)包的源IP地址,選擇該發(fā)送網(wǎng)卡上最合適的本地地址作為arp請求的源IP地址。
sysctl.conf中包含all和eth/lo(具體網(wǎng)卡)的arp_ignore參數(shù),取其中較大的值生效。
二、arp_ignore和arp_announce參數(shù)示例
(1)當(dāng)arp_ignore參數(shù)配置為0時,eth1網(wǎng)卡上收到目的IP為環(huán)回網(wǎng)卡IP的arp請求,但是eth1也會返回arp響應(yīng),把自己的mac地址告訴對端。
(2)當(dāng)arp_ignore參數(shù)配置為1時,eth1網(wǎng)卡上收到目的IP為環(huán)回網(wǎng)卡IP的arp請求,發(fā)現(xiàn)請求的IP不是自己網(wǎng)卡上的IP,不會回arp響應(yīng)。
(3)當(dāng)arp_announce參數(shù)配置為0時,系統(tǒng)要發(fā)送的IP包源地址為eth1的地址,IP包目的地址根據(jù)路由表查詢判斷需要從eth2網(wǎng)卡發(fā)出,這時會先從eth2網(wǎng)卡發(fā)起一個arp請求,用于獲取目的IP地址的MAC地址。該arp請求的源MAC自然是eth2網(wǎng)卡的MAC地址,但是源IP地址會選擇eth1網(wǎng)卡的地址。
(4)當(dāng)arp_announce參數(shù)配置為2時,eth2網(wǎng)卡發(fā)起arp請求時,源IP地址會選擇eth2網(wǎng)卡自身的IP地址。
三、arp_ignore和arp_announce參數(shù)在DR模式下的作用
1. arp_ignore
因?yàn)镈R模式下,每個真實(shí)服務(wù)器節(jié)點(diǎn)都要在環(huán)回網(wǎng)卡上綁定虛擬服務(wù)IP。這時候,如果客戶端對于虛擬服務(wù)IP的arp請求廣播到了各個真實(shí)服務(wù)器節(jié)點(diǎn),如果arp_ignore參數(shù)配置為0,則各個真實(shí)服務(wù)器節(jié)點(diǎn)都會響應(yīng)該arp請求,此時客戶端就無法正確獲取LVS節(jié)點(diǎn)上正確的虛擬服務(wù)IP所在網(wǎng)卡的MAC地址。假如某個真實(shí)服務(wù)器節(jié)點(diǎn)A的網(wǎng)卡eth1響應(yīng)了該arp請求,客戶端把A節(jié)點(diǎn)的eth1網(wǎng)卡的MAC地址誤認(rèn)為是LVS節(jié)點(diǎn)的虛擬服務(wù)IP所在網(wǎng)卡的MAC,從而將業(yè)務(wù)請求消息直接發(fā)到了A節(jié)點(diǎn)的eth1網(wǎng)卡。這時候雖然因?yàn)锳節(jié)點(diǎn)在環(huán)回網(wǎng)卡上也綁定了虛擬服務(wù)IP,所以A節(jié)點(diǎn)也能正常處理請求,業(yè)務(wù)暫時不會受到影響。但時此時由于客戶端請求沒有發(fā)到LVS的虛擬服務(wù)IP上,所以LVS的負(fù)載均衡能力沒有生效。造成的后果就是,A節(jié)點(diǎn)一直在單節(jié)點(diǎn)運(yùn)行,業(yè)務(wù)量過大時可能會出現(xiàn)性能瓶頸。
所以DR模式下要求arp_ignore參數(shù)要求配置為1。
2. arp_announce
每個機(jī)器或者交換機(jī)中都有一張arp表,該表用于存儲對端通信節(jié)點(diǎn)IP地址和MAC地址的對應(yīng)關(guān)系。當(dāng)收到一個未知IP地址的arp請求,就會再本機(jī)的arp表中新增對端的IP和MAC記錄;當(dāng)收到一個已知IP地址(arp表中已有記錄的地址)的arp請求,則會根據(jù)arp請求中的源MAC刷新自己的arp表。
如果arp_announce參數(shù)配置為0,則網(wǎng)卡在發(fā)送arp請求時,可能選擇的源IP地址并不是該網(wǎng)卡自身的IP地址,這時候收到該arp請求的其他節(jié)點(diǎn)或者交換機(jī)上的arp表中記錄的該網(wǎng)卡IP和MAC的對應(yīng)關(guān)系就不正確,可能會引發(fā)一些未知的網(wǎng)絡(luò)問題,存在安全隱患。
所以DR模式下要求arp_announce參數(shù)要求配置為2。
四、arp_ignore和arp_announce參數(shù)的配置方法
arp_ignore和arp_announce參數(shù)分別有all,default,lo,eth1,eth2...等對應(yīng)不同網(wǎng)卡的具體參數(shù)。當(dāng)all和具體網(wǎng)卡的參數(shù)值不一致時,取較大值生效。
一般只需修改all和某個具體網(wǎng)卡的參數(shù)即可(取決于你需要修改哪個網(wǎng)卡)。下面以修改lo網(wǎng)卡為例:
1. 修改/etc/sysctl.conf文件,然后sysctl -p刷新到內(nèi)存。
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.lo.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
net.ipv4.conf.lo.arp_announce=2
2. 使用sysctl -w直接寫入內(nèi)存:
sysctl -w net.ipv4.conf.all.arp_ignore=1
sysctl -w net.ipv4.conf.lo.arp_ignore=1
sysctl -w net.ipv4.conf.all.arp_announce=2
sysctl -w net.ipv4.conf.lo.arp_announce=2
3. 修改/proc文件系統(tǒng):
echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore
echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce