Linux ARP緩存配置和狀態查看命令

  • 轉載自:http://www.cnblogs.com/yorkwoo/p/4664535.html

  • 查看Linux ARP緩存老化時間

cat /proc/sys/net/ipv4/neigh/eth0/base_reachable_time
同目錄下還有一個文件gc_stale_time,官方解釋如下:
Determines how often to check for stale neighbour entries. 
When a neighbour entry is considered stale it is resolved again before sending data to it. 
Defaults to 60 seconds.

  • 查看Linux ARP緩存狀態

arp -a    #代碼對應於ioctl(s, SIOCGARP, &arpreq),沒法看到每條緩存的狀態是REACHABLE還是STALE

/isam/slot_1101/run # arp -a
? (135.251.197.136) at 00:19:8f:5f:bd:87 [ether] on eth0
? (135.251.196.1) at 00:e0:b1:ca:5a:48 [ether] on eth0

ip neigh show    #實現上是通過另一種系統調用netlink來獲取的

/proc/sys/net/ipv4/neigh/eth0 # ip neigh
135.251.197.136 dev eth0 lladdr 00:19:8f:5f:bd:87 REACHABLE
135.251.196.1 dev eth0 lladdr 00:e0:b1:ca:5a:48 STALE

  • 應用程序如何觸發arp緩存的添加和刷新呢?

經過一系列測試,結論如下:

執行arping命令,無法添加新的arp緩存。但是可以把STALE的緩存刷新爲REACHABLE狀態。

arping有如下選項,是不是我少加了什麼選項呢?註釋符#後面的解釋是man命令裏面摘過來的。

arping -I eth0 135.251.196.1 -c 10

/isam/slot_default/run # arping -h
arping: invalid option -- 'h'
BusyBox v1.22.1 (2015-07-11 21:41:15 CEST) multi-call binary.

Usage: arping [-fqbDUA] [-c CNT] [-w TIMEOUT] [-I IFACE] [-s SRC_IP] DST_IP

Send ARP requests/replies

-f Quit on first ARP reply    #Finish after the first reply confirming that target is alive.
-q Quiet    #Quiet output. Nothing is displayed.
-b Keep broadcasting, don't go unicast    #Send only MAC level broadcasts. Normally arping starts from sending broadcast, and switch to unicast after reply received.
-D Duplicated address detection mode    #Duplicate address detection mode (DAD). See RFC2131, 4.4.1.  Returns 0, if DAD succeeded i.e. no replies are received    #如果是DAD模式,則原源主機地址一直沒有設置,那麼就意味着源地址爲0.0.0.0。這樣當目的主機接到之後,就會向0.0.0.0發送回覆,就相當於廣播給以太網中所有的主機。因爲進行D重複地址檢測模式的原因很可能是由於源主機的IP地址沒有設置,從而想設置自身的IP地址。在IP地址沒有設置的時候,主機只能接受到地址爲0.0.0.0的廣播信號。
-U Unsolicited ARP mode, update your neighbors    #Unsolicited ARP mode to update neighbours<80><99> ARP caches.  No replies are expected.    #爲了更新以太網鄰居的ARP快速緩存而主動進行的ARP。也就是免費ARP(gratuitous ARP),即請求自己的ip地址的mac地址。
-A ARP answer mode, update your neighbors    #與-U選項類似,但是發送的是ARP 回覆報文,而不是ARP請求報文。
-c N Stop after sending N ARP requests
-w TIMEOUT Time to wait for ARP reply, seconds
-I IFACE Interface to use (default eth0)
-s SRC_IP Sender IP address
DST_IP Target IP address

從以上選項可以看出,我們並沒有漏掉什麼選項,所以內核就是這麼設計的。

還進行過如下測試,Linux shell ping竟然只能添加arp緩存,但是並不能把STALE的緩存刷新爲REACHABLE狀態。而且ping是通的,這也說明了STALE還在被引用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章