如何延遲套接字上的 TCP 流量?

如何延遲套接字上的 TCP 流量?

我想測試客戶端程式和伺服器程式之間的互動以了解競爭條件。他們透過 TCP 相互連接。客戶端在多個執行緒上阻塞對伺服器的呼叫(每個執行緒一個到伺服器的 TCP 連線)。我想測試一個阻塞呼叫先於另一個呼叫完成的競爭條件。

為此,我希望將 tcp 連線延遲不同的量(這樣我就不必明確重寫客戶端或伺服器)。

我希望做這樣的事情:

socat tcp-listen:$pin system:"delaycat $delay |socat - 'tcp:localhost:$pout'"

其中$pin是客戶端連接的端口,$delay是延遲秒數,$pout是伺服器偵聽的端口。 Anddelaycat是一個虛構的程序,它將輸入流延遲 n 秒。

是否有一些現有的程序可以滿足我的要求?還是我應該寫delaycat

編輯:請注意,系統範圍的延遲實際上並不理想,因為如果可能的話,我希望將各個套接字延遲不同的量。

答案1

我找不到任何東西,所以我寫了一個 python 腳本來做到這一點(這可能是有問題的):

#!/usr/bin/env python26
"""Add latency to a tcp connection"""
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from functools import partial

from twisted.internet.reactor import (
    listenTCP, connectTCP, callLater, run
)
from twisted.internet.protocol import (
    ServerFactory, ReconnectingClientFactory, Protocol
)


class ConnectionToServer(Protocol):
    def __init__(self, connection_from_client, delay_to_client):
        self._delay_to_client = delay_to_client
        self._connection_from_client = connection_from_client

    def connectionMade(self):
        self._connection_from_client.connection_to_server = self
        if self._connection_from_client.buffer_from_client:
            self.transport.write(
                self._connection_from_client.buffer_from_client
            )

    def dataReceived(self, data):
        callLater(
            self._delay_to_client,
            self._connection_from_client.transport.write, data
        )

    def connectionLost(self, reason):
        callLater(
            self._delay_to_client,
            self._connection_from_client.transport.loseConnection
        )


class ConnectionFromClient(Protocol):
    def __init__(self, server_host, server_port, delay_to_client, delay_to_server):
        self._delay_to_server = delay_to_server
        self.connection_to_server = None
        self.buffer_from_client = ''
        server_connection_factory = ReconnectingClientFactory()
        server_connection_factory.protocol = partial(
            ConnectionToServer, self, delay_to_client
        )
        self._server_connector = connectTCP(
            server_host, server_port, server_connection_factory
        )

    def dataReceived(self, data):
        callLater(self._delay_to_server, self._write, data)

    def connectionLost(self, reason):
        callLater(
            self._delay_to_server, self._server_connector.disconnect
        )

    def _write(self, data):
        if self.connection_to_server:
            self.connection_to_server.transport.write(data)
        else:
            self.buffer_from_client += data


def main():
    """Add latency to a tcp connection"""
    parser = ArgumentParser(
        description=main.__doc__,
        formatter_class=ArgumentDefaultsHelpFormatter
    )
    parser.add_argument(
        'client_port', type=int, help='client connects to this port'
    )
    parser.add_argument(
        'server_port', type=int, help='server listens on this port'
    )
    parser.add_argument(
        '-t', '--server-host', default='localhost',
        help='server is running on this host'
    )
    parser.add_argument(
        '-c', '--delay-to-client', default=0, type=float,
        help='messages to client are delayed by this many seconds'
    )
    parser.add_argument(
        '-s', '--delay-to-server', default=0, type=float,
        help='messages to server are delayed by this many seconds'
    )
    args = parser.parse_args()
    client_connection_factory = ServerFactory()
    client_connection_factory.protocol = partial(
        ConnectionFromClient, args.server_host, args.server_port,
        args.delay_to_client, args.delay_to_server
    )
    listenTCP(args.client_port, client_connection_factory)
    run()
if __name__ == '__main__':
    main()

答案2

在 Linux 機器上,您可以使用 tc內特姆

例如,該命令tc qdisc add dev eth0 root netem delay 100ms將延遲全部傳出資料包延遲 100 毫秒。要延遲傳入資料包,您可以使用ifb - 中間功能塊

相關內容