使用多宿主配置本地虚拟网络

时间:2013-01-03 09:34:45

标签: linux networking multihomed

我需要测试超过64K连接的应用程序。我想使用同一个主机。

今天我启动服务器侦听127.0.0.1并从同一台机器连接到它,但当然它仅限于64K连接。

我想模拟一种情况,例如我有一台服务器,许多客户端连接到特定单一IP上的单个服务器。

Server Listen: 1.2.3.4

Client Connect to 1.2.3.4 From 2.1.2.1
Client Connect to 1.2.3.4 From 2.1.2.2
Client Connect to 1.2.3.4 From 2.1.2.3
Client Connect to 1.2.3.4 From 2.1.2.4

因此,我需要设置一个具有多宿主功能的虚拟网络,以便客户端能够连接多个已寻址和侦听的服务器。

如何配置?在Linux上?

1 个答案:

答案 0 :(得分:0)

第1步:为您的环回添加地址;我将10.0.0.1,10.0.0.2,10.0.0.3,10.0.0.4和10.0.0.5添加到lo

[mpenning@tsunami ~]$ ip addr show lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
[mpenning@tsunami ~]$ sudo ip addr add 10.0.0.1/24 dev lo
[mpenning@tsunami ~]$ sudo ip addr add 10.0.0.2/24 dev lo
[mpenning@tsunami ~]$ sudo ip addr add 10.0.0.3/24 dev lo
[mpenning@tsunami ~]$ sudo ip addr add 10.0.0.4/24 dev lo
[mpenning@tsunami ~]$ sudo ip addr add 10.0.0.5/24 dev lo
[mpenning@tsunami ~]$ sudo ip addr show lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet 10.0.0.1/24 scope global lo
    inet 10.0.0.2/24 scope global secondary lo
    inet 10.0.0.3/24 scope global secondary lo
    inet 10.0.0.4/24 scope global secondary lo
    inet 10.0.0.5/24 scope global secondary lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
[mpenning@tsunami ~]$

第2步:Increase max connections

第3步:开始大量连接...

from socket import socket, AF_INET, SOCK_STREAM
import select

class Server(object):
    def __init__(self, addr="0.0.0.0", port=2525):
        self.listen_addr = addr
        self.listen_port = port
        self.server = socket(AF_INET, SOCK_STREAM)
        self.server.setblocking(0)
        self.server.bind(self.socket_tuple)
        self.server.listen(5)

    def __repr__(self):
        return "TCP Server listening on %s:%s" % (self.listen_addr,
            self.listen_port)

    @property
    def socket_tuple(self):
        return (self.listen_addr, self.listen_port)

    def close(self):
        self.server.close()

class Client(object):
    def __init__(self, addr="0.0.0.0"):
        self.listen_addr = addr
        self.server_addr = None
        self.server_port = None
        self.client = socket(AF_INET, SOCK_STREAM)
        self.client.setblocking(0)
        finished = False
        while not finished:
            try:
                ### The point of my answer is here...
                self.client.bind((addr,0)) # <- Bind to specific IP and rnd port
                finished = True
            except:
                pass

    def __repr__(self):
        return "TCP Client %s->%s:%s" % (self.listen_addr,
            self.server_addr, self.server_port)

    def connect(self, socket_tuple=("0.0.0.0",0)):
        self.server_addr = socket_tuple[0]
        self.server_port = socket_tuple[1]
        self.client.connect_ex(socket_tuple)

    def close(self):
        self.client.close()

READ_ONLY = select.POLLIN | select.POLLPRI | select.POLLHUP | select.POLLERR
clients = list()
servers = list()
for server_addr, port in [('10.0.0.1', 2525), ('10.0.0.2', 2526)]:
    ss = Server(addr=server_addr, port=port)
    servers.append(ss)
    for client_addr in ['10.0.0.3', '10.0.0.4', '10.0.0.5']:
        for ii in xrange(0, 25000):
            cc = Client(addr=client_addr)
            connection = cc.connect(socket_tuple=ss.socket_tuple)
            finished = False
            print "    %s conns" % len(clients)
            while not finished:
                poller = select.poll()
                poller.register(ss.server, READ_ONLY)
                # Map file descriptors to socket objects...
                fd_to_socket = {ss.server.fileno(): ss.server,}
                events = poller.poll(1)
                for fd, flag in events:
                    s = fd_to_socket[fd]
                    if flag & (select.POLLIN|select.POLLPRI):
                        if s is ss.server:
                            # server socket is ready to accept a connection
                            connection, client_address = s.accept()
                            connection.setblocking(0)
                            fd_to_socket[connection.fileno()] = connection
                            poller.register(connection, READ_ONLY)
                            finished = True
            clients.append(cc)
            print cc

print "Total Clients:", len(clients)
for cc in clients:
    cc.close()
for ss in servers:
    ss.close()

根据您的偏好调整上面的值...但是,您应该记住,调整Linux服务器以接受如此多的连接可能具有挑战性......但是在这一点上,这不是您要问的问题。 ..