来自不同虚拟 IP 的 1024 个连接后超时

Timeout after 1024 connections from different virtual IPs

我遇到了一个问题,我已经纠结了一段时间但没有解决。

我正在实现一个具有客户端和服务器端的流量生成器。客户端模拟具有唯一 IP 地址的设备。这些 IP 作为虚拟接口添加到客户端,然后模拟设备绑定到该客户端。然后设备将连接到服务器端并生成流量 to/from 它。

问题是我只能连接1023个设备,然后以下设备连接超时。我已经在服务器端检查了 wireshark,我可以看到连接的 SYN,但它从未在应用程序中收到。当我重用 IP 以便使用少于 2014 时,我可以建立任意数量的连接。

我创建了一个 python 程序,它更容易 运行 并且有同样的问题:

client.py

import socket
import thread
import time
from subprocess import call

TCP_IP = '192.168.169.218'
TCP_PORT = 9999
BUFFER_SIZE = 1024
MESSAGE = "-" * 1000

if __name__=='__main__':
    sockets = []
    for i in range(0, 10020):
        ip = "13.1."+ str(((i/254)%254) + 1) + "." + str((i % 254) + 1)
        cmd = "ip addr add " + ip + " dev eth1;"
        call(cmd, shell=True)
        s = socket.create_connection((TCP_IP, TCP_PORT), 10, (ip, 0))
        sockets.append(s)

    while 1:
        for s in sockets:
            s.send(MESSAGE)
            data = s.recv(BUFFER_SIZE)

    for s in sockets:
        s.close()

server.py

from socket import *
import thread

BUFF = 1024
HOST = '192.168.169.218'
PORT = 9999

def handler(clientsock,addr):
    while 1:
        data = clientsock.recv(BUFF)
        if not data: break
        clientsock.send(data)
        # type 'close' on client console to close connection from the server side
        if "close" == data.rstrip(): break     
    clientsock.close()
    print addr, "- closed connection" #log on console

if __name__=='__main__':
    count = 0   
    ADDR = (HOST, PORT)
    serversock = socket(AF_INET, SOCK_STREAM)
    serversock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    serversock.bind(ADDR)
    serversock.listen(5)
    while 1:
        print 'waiting for connection... listening on port', PORT
        clientsock, addr = serversock.accept()
        count += 1
        print count
        thread.start_new_thread(handler, (clientsock, addr))

我正在 运行ning CentOS 7.1 64 位,我测试过的 Python 版本是 2.7.5。

到目前为止我做了什么:
- 将打开文件数限制(nofile)增加到 1040000
- 将 net.core.somaxconn 增加到 65535
- 增加了 net.ipv4.tcp_max_syn_backlog 和 net.core.netdev_max_backlog 到 30000
- 增加核心和 TCP 缓冲区
- 禁用防火墙并清除所有 iptables 规则

我测试让 python 客户端在每次连接后休眠一秒钟,然后就没有问题了,所以我猜是有一些防洪措施启动了。有人有什么想法吗?

这个问题很有趣,所以我用我的虚拟机做了一个测试。我发现,您正在达到 ARP 邻居条目的限制

# sysctl -a|grep net.ipv4.neigh.default.gc_thresh
net.ipv4.neigh.default.gc_thresh1 = 128
net.ipv4.neigh.default.gc_thresh2 = 512
net.ipv4.neigh.default.gc_thresh3 = 1024

以上是默认值,当您的第 1024 个连接填满此 table 时,垃圾收集器再次开始 运行 arp - 这会减慢速度并导致您的客户端超时

我能够将这些值设置如下

net.ipv4.neigh.default.gc_thresh1 = 16384
net.ipv4.neigh.default.gc_thresh2 = 32768
net.ipv4.neigh.default.gc_thresh3 = 65536

瞧!不再有 1024 个限制 ..HTH