Linux內核參數之arp_ignore和arp_announce

一、arp_ignore和arp_announce介紹

   arp_ignore和arp_announce參數都和ARP協議相關,主要用於控制系統返回arp響應和發送arp請求時的動作。這兩個參數很重要,特別是在LVS的DR場景下,它們的配置直接影響到DR轉發是否正常。
首先看一下Linux內核文檔中對於它們的描述:
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參數的作用是控制系統在收到外部的arp請求時,是否要返回arp響應。
  arp_ignore參數常用的取值主要有0,1,2,3~8較少用到:
0:響應任意網卡上接收到的對本機IP地址的arp請求(包括環回網卡上的地址),而不管該目的IP是否在接收網卡上。
1:只響應目的IP地址爲接收網卡上的本地地址的arp請求。
2:只響應目的IP地址爲接收網卡上的本地地址的arp請求,並且arp請求的源IP必須和接收網卡同網段。
3:如果ARP請求數據包所請求的IP地址對應的本地地址其作用域(scope)爲主機(host),則不迴應ARP響應數據包,如果作用域爲全局(global)或鏈路(link),則迴應ARP響應數據包。
4~7:保留未使用
8:不迴應所有的arp請求
  sysctl.conf中包含all和eth/lo(具體網卡)的arp_ignore參數,取其中較大的值生效。

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的作用是控制系統在對外發送arp請求時,如何選擇arp請求數據包的源IP地址。(比如系統準備通過網卡發送一個數據包a,這時數據包a的源IP和目的IP一般都是知道的,而根據目的IP查詢路由表,發送網卡也是確定的,故源MAC地址也是知道的,這時就差確定目的MAC地址了。而想要獲取目的IP對應的目的MAC地址,就需要發送arp請求。arp請求的目的IP自然就是想要獲取其MAC地址的IP,而arp請求的源IP是什麼呢? 可能第一反應會以爲肯定是數據包a的源IP地址,但是這個也不是一定的,arp請求的源IP是可以選擇的,控制這個地址如何選擇就是arp_announce的作用)
  arp_announce參數常用的取值有0,1,2。
0:允許使用任意網卡上的IP地址作爲arp請求的源IP,通常就是使用數據包a的源IP。
1:儘量避免使用不屬於該發送網卡子網的本地地址作爲發送arp請求的源IP地址。
2:忽略IP數據包的源IP地址,選擇該發送網卡上最合適的本地地址作爲arp請求的源IP地址。
  sysctl.conf中包含all和eth/lo(具體網卡)的arp_ignore參數,取其中較大的值生效。

二、arp_ignore和arp_announce參數示例

 

(1)當arp_ignore參數配置爲0時,eth1網卡上收到目的IP爲環回網卡IP的arp請求,但是eth1也會返回arp響應,把自己的mac地址告訴對端。

(2)當arp_ignore參數配置爲1時,eth1網卡上收到目的IP爲環回網卡IP的arp請求,發現請求的IP不是自己網卡上的IP,不會回arp響應。

 

 

(3)當arp_announce參數配置爲0時,系統要發送的IP包源地址爲eth1的地址,IP包目的地址根據路由表查詢判斷需要從eth2網卡發出,這時會先從eth2網卡發起一個arp請求,用於獲取目的IP地址的MAC地址。該arp請求的源MAC自然是eth2網卡的MAC地址,但是源IP地址會選擇eth1網卡的地址。

 

(4)當arp_announce參數配置爲2時,eth2網卡發起arp請求時,源IP地址會選擇eth2網卡自身的IP地址。

 

三、arp_ignore和arp_announce參數在DR模式下的作用

  1. arp_ignore

  因爲DR模式下,每個真實服務器節點都要在環回網卡上綁定虛擬服務IP。這時候,如果客戶端對於虛擬服務IP的arp請求廣播到了各個真實服務器節點,如果arp_ignore參數配置爲0,則各個真實服務器節點都會響應該arp請求,此時客戶端就無法正確獲取LVS節點上正確的虛擬服務IP所在網卡的MAC地址。假如某個真實服務器節點A的網卡eth1響應了該arp請求,客戶端把A節點的eth1網卡的MAC地址誤認爲是LVS節點的虛擬服務IP所在網卡的MAC,從而將業務請求消息直接發到了A節點的eth1網卡。這時候雖然因爲A節點在環回網卡上也綁定了虛擬服務IP,所以A節點也能正常處理請求,業務暫時不會受到影響。但時此時由於客戶端請求沒有發到LVS的虛擬服務IP上,所以LVS的負載均衡能力沒有生效。造成的後果就是,A節點一直在單節點運行,業務量過大時可能會出現性能瓶頸。

  所以DR模式下要求arp_ignore參數要求配置爲1。

  2. arp_announce

    每個機器或者交換機中都有一張arp表,該表用於存儲對端通信節點IP地址和MAC地址的對應關係。當收到一個未知IP地址的arp請求,就會再本機的arp表中新增對端的IP和MAC記錄;當收到一個已知IP地址(arp表中已有記錄的地址)的arp請求,則會根據arp請求中的源MAC刷新自己的arp表。
  如果arp_announce參數配置爲0,則網卡在發送arp請求時,可能選擇的源IP地址並不是該網卡自身的IP地址,這時候收到該arp請求的其他節點或者交換機上的arp表中記錄的該網卡IP和MAC的對應關係就不正確,可能會引發一些未知的網絡問題,存在安全隱患。
  所以DR模式下要求arp_announce參數要求配置爲2。

四、arp_ignore和arp_announce參數的配置方法

arp_ignore和arp_announce參數分別有all,default,lo,eth1,eth2...等對應不同網卡的具體參數。當all和具體網卡的參數值不一致時,取較大值生效。

一般只需修改all和某個具體網卡的參數即可(取決於你需要修改哪個網卡)。下面以修改lo網卡爲例:

 1. 修改/etc/sysctl.conf文件,然後sysctl -p刷新到內存。

 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直接寫入內存:

 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文件系統:

 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