hana_shinのLinux技術ブログ

Linuxの技術情報を掲載しています。特にネットワークをメインに掲載していきます。

ソケットオプションの使い方(TCP_NODELAY)

1 TCP_NODELAYオプションとは?

TCP_NODELAYオプションは、Nagleアルゴリズムを無効にするオプションです。Nagleアルゴリズムは、小さなサイズのTCPパケットを1つずつ送信するのではなく、1つのTCPパケットにまとめて送信する仕組みです。なお、Nagleアルゴリズムはデフォルトで有効になっています。ここでは、TCP_NODELAYを有効にした場合の効果を確認してみます。

2 検証環境

2.1 ネットワーク構成

サーバとクライアントの2台構成です。図中のens33はNICの名前です。

                          192.168.2.0/24
client(ens33) -------------------------------------(ens33) server
        .105                                       .100

2.2 版数

サーバ、クライアントともに下記版数です。

[root@server ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

3 TP(テストプログラム)作成

3.1 作成するTP

サーバで動作するTPを作成します。なお、エラー処理は見やすくするため意図的に省略しています。

[root@server ~]# cat sv.c
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main(int argc, char *argv[])
{
    int lfd, cfd;
    socklen_t len;
    struct sockaddr_in sv, cl;
    char buf[32];
    ssize_t n;

    lfd = socket(AF_INET, SOCK_STREAM, 0);

    sv.sin_family = AF_INET;
    sv.sin_port = htons(11111);
    sv.sin_addr.s_addr = INADDR_ANY;

    bind(lfd, (struct sockaddr *)&sv, sizeof(sv));
    listen(lfd, 5);
    memset(buf, 0, sizeof(buf));
    len = sizeof(cl);
    cfd = accept(lfd, (struct sockaddr *)&cl, &len);

    while (1) {
        n = read(cfd, buf, sizeof(buf));
        if(n > 0) {
            continue;
        }
        else if(n == 0) {
            fprintf(stdout,"we received EOF\n");
            close(cfd);
            return 0;
        }
        else {
            perror("read");
            return 1;
        }
    }
    close(lfd);
    return 0;
}

クライアントで動作するTPを作成します。なお、エラー処理は見やすくするため意図的に省略しています。

[root@client ~]# cat cl.c
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>

int main(int argc, char *argv[])
{
    struct sockaddr_in server;
    int sock, i, val;
    ssize_t n;
    char buf[1]="9";

    sock = socket(AF_INET, SOCK_STREAM, 0);

    server.sin_family = AF_INET;
    server.sin_port = htons(11111);
    server.sin_addr.s_addr = inet_addr("192.168.2.100");

    val = atoi(argv[1]);
    setsockopt(sock, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
    connect(sock, (struct sockaddr *)&server, sizeof(server));

    for(i=0; i<1000; i++) {
        n = write(sock, buf, sizeof(buf));
        if(n == -1) {
            perror("write");
            return 1;
        }
    }
    close(sock);
    return 0;
}

3.2 コンパイル

サーバのTPをコンパイルします。

[root@server ~]# gcc -Wall -o sv sv.c

クライアントのTPをコンパイルします。

[root@client ~]# gcc -Wall -o cl cl.c

4 ポート番号の開放

サーバでポート番号を開放します。firewall-cmdの詳細な使い方は、firewall-cmdの使い方 - hana_shinのLinux技術ブログを参照してください。

[root@server ~]# firewall-cmd --add-port=11111/tcp

開放したポート番号を確認します。11111番ポートが開放されたことがわかります。

[root@server ~]# firewall-cmd --list-ports
11111/tcp

5 TCP_NODELAY無効の場合

tcpdumpを実行します。このとき、クライアントからサーバに送信するデータサイズ(length)だけを確認したいので、tcpdumpのパラメータは、"dst port 11111"と指定しています。なお、tcpdumpの使い方は、tcpdumpの使い方(基本編) - hana_shinのLinux技術ブログを参照してください。

[root@server ~]# tcpdump -i ens33 dst port 11111 -nn

サーバでTPを実行します。

[root@server ~]# ./sv

クライアントでTPを実行します。このとき、TCP_NODELAYオプションを無効(0)にします。

[root@client ~]# ./cl 0

tcpdumpの実行結果を確認してみます。各行の右端にlengthが表示されています。lengthは、TCPパケットのペイロード長を表しています。1,2行目はTCP 3way handshakeの結果です。tcpdumpのフィルタ条件が宛先ポート番号を指定しているので、サーバからのACKは表示されていません。3行目からが、クライアントからサーバへのTCPパケットの送信になります。writeシステムコールで1バイトずつ送信しましたが、例えば4行目では110バイトにまとめて送信されていることがわかります。

[root@server ~]# tcpdump -i ens33 dst port 11111 -nn
-snip-
18:27:03.370050 IP 192.168.2.105.39734 > 192.168.2.100.11111: Flags [S], seq 1004776758, win 29200, options [mss 1460,sackOK,TS val 4311314 ecr 0,nop,wscale 7], length 0
18:27:03.370507 IP 192.168.2.105.39734 > 192.168.2.100.11111: Flags [.], ack 1019816726, win 229, options [nop,nop,TS val 4311317 ecr 6661927], length 0
18:27:03.370581 IP 192.168.2.105.39734 > 192.168.2.100.11111: Flags [P.], seq 0:1, ack 1, win 229, options [nop,nop,TS val 4311318 ecr 6661927], length 1
18:27:03.370877 IP 192.168.2.105.39734 > 192.168.2.100.11111: Flags [P.], seq 1:111, ack 1, win 229, options [nop,nop,TS val 4311318 ecr 6661928], length 110
18:27:03.371393 IP 192.168.2.105.39734 > 192.168.2.100.11111: Flags [P.], seq 111:250, ack 1, win 229, options [nop,nop,TS val 4311318 ecr 6661928], length 139
18:27:03.371729 IP 192.168.2.105.39734 > 192.168.2.100.11111: Flags [P.], seq 250:414, ack 1, win 229, options [nop,nop,TS val 4311319 ecr 6661928], length 164
18:27:03.372092 IP 192.168.2.105.39734 > 192.168.2.100.11111: Flags [P.], seq 414:530, ack 1, win 229, options [nop,nop,TS val 4311319 ecr 6661929], length 116
18:27:03.372555 IP 192.168.2.105.39734 > 192.168.2.100.11111: Flags [P.], seq 530:670, ack 1, win 229, options [nop,nop,TS val 4311320 ecr 6661929], length 140
18:27:03.373054 IP 192.168.2.105.39734 > 192.168.2.100.11111: Flags [P.], seq 670:769, ack 1, win 229, options [nop,nop,TS val 4311320 ecr 6661930], length 99
18:27:03.373753 IP 192.168.2.105.39734 > 192.168.2.100.11111: Flags [P.], seq 769:955, ack 1, win 229, options [nop,nop,TS val 4311321 ecr 6661930], length 186
18:27:03.374369 IP 192.168.2.105.39734 > 192.168.2.100.11111: Flags [FP.], seq 955:1000, ack 1, win 229, options [nop,nop,TS val 4311321 ecr 6661930], length 45
18:27:03.375292 IP 192.168.2.105.39734 > 192.168.2.100.11111: Flags [.], ack 2, win 229, options [nop,nop,TS val 4311322 ecr 6661931], length 0

6 TCP_NODELAY有効の場合

tcpdumpを実行します。このとき、クライアントからサーバに送信するデータサイズ(length)だけを確認したいので、tcpdumpのパラメータは、"dst port 11111"と指定しています。

[root@server ~]# tcpdump -i ens33 dst port 11111 -nn

サーバでTPを実行します。

[root@server ~]# ./sv

クライアントでTPを実行します。このとき、TCP_NODELAYオプションを有効(1)にします。

[root@client ~]# ./cl 1

tcpdumpの実行結果を確認してみます。データサイズが1(byte)のTCPパケットが多数送信されていることがわかります。つまり、送信バッファにTCPパケットを溜めず、すぐに送信していることになります。

[root@server ~]# tcpdump -i ens33 dst port 11111 -nn
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
20:09:13.232895 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [S], seq 4063029477, win 29200, options [mss 1460,sackOK,TS val 10441181 ecr 0,nop,wscale 7], length 0
20:09:13.233300 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [.], ack 2058424398, win 229, options [nop,nop,TS val 10441182 ecr 12791790], length 0
20:09:13.233538 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 0:1, ack 1, win 229, options [nop,nop,TS val 10441182 ecr 12791790], length 1
20:09:13.233602 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 1:2, ack 1, win 229, options [nop,nop,TS val 10441182 ecr 12791790], length 1
20:09:13.233663 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 2:3, ack 1, win 229, options [nop,nop,TS val 10441182 ecr 12791790], length 1
20:09:13.233707 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 3:4, ack 1, win 229, options [nop,nop,TS val 10441182 ecr 12791790], length 1
20:09:13.233714 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 4:5, ack 1, win 229, options [nop,nop,TS val 10441182 ecr 12791790], length 1
20:09:13.233930 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 5:6, ack 1, win 229, options [nop,nop,TS val 10441182 ecr 12791791], length 1
20:09:13.233959 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 6:7, ack 1, win 229, options [nop,nop,TS val 10441182 ecr 12791791], length 1
20:09:13.233962 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 7:8, ack 1, win 229, options [nop,nop,TS val 10441182 ecr 12791791], length 1
20:09:13.234155 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 8:9, ack 1, win 229, options [nop,nop,TS val 10441182 ecr 12791791], length 1
20:09:13.234162 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 9:10, ack 1, win 229, options [nop,nop,TS val 10441183 ecr 12791791], length 1
20:09:13.234285 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 10:11, ack 1, win 229, options [nop,nop,TS val 10441183 ecr 12791791], length 1
20:09:13.234442 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 11:12, ack 1, win 229, options [nop,nop,TS val 10441183 ecr 12791791], length 1
20:09:13.234602 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 12:13, ack 1, win 229, options [nop,nop,TS val 10441183 ecr 12791791], length 1
20:09:13.234610 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 13:14, ack 1, win 229, options [nop,nop,TS val 10441183 ecr 12791791], length 1
20:09:13.234650 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 14:15, ack 1, win 229, options [nop,nop,TS val 10441183 ecr 12791791], length 1
20:09:13.234713 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 15:16, ack 1, win 229, options [nop,nop,TS val 10441183 ecr 12791791], length 1
20:09:13.234989 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 16:30, ack 1, win 229, options [nop,nop,TS val 10441183 ecr 12791791], length 14
20:09:13.234995 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 30:31, ack 1, win 229, options [nop,nop,TS val 10441183 ecr 12791792], length 1
20:09:13.234997 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 31:32, ack 1, win 229, options [nop,nop,TS val 10441183 ecr 12791792], length 1
20:09:13.234999 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 32:33, ack 1, win 229, options [nop,nop,TS val 10441183 ecr 12791792], length 1
20:09:13.235231 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 33:34, ack 1, win 229, options [nop,nop,TS val 10441183 ecr 12791792], length 1
20:09:13.235263 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 34:35, ack 1, win 229, options [nop,nop,TS val 10441184 ecr 12791792], length 1
20:09:13.235556 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 35:36, ack 1, win 229, options [nop,nop,TS val 10441184 ecr 12791792], length 1
20:09:13.235597 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 36:38, ack 1, win 229, options [nop,nop,TS val 10441184 ecr 12791792], length 2
20:09:13.235601 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 38:39, ack 1, win 229, options [nop,nop,TS val 10441184 ecr 12791792], length 1
20:09:13.235603 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 39:40, ack 1, win 229, options [nop,nop,TS val 10441184 ecr 12791792], length 1
20:09:13.235739 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 40:41, ack 1, win 229, options [nop,nop,TS val 10441184 ecr 12791792], length 1
20:09:13.235749 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 41:42, ack 1, win 229, options [nop,nop,TS val 10441184 ecr 12791792], length 1
20:09:13.235905 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 42:43, ack 1, win 229, options [nop,nop,TS val 10441184 ecr 12791793], length 1
20:09:13.235910 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 43:44, ack 1, win 229, options [nop,nop,TS val 10441184 ecr 12791793], length 1
20:09:13.235912 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 44:45, ack 1, win 229, options [nop,nop,TS val 10441184 ecr 12791793], length 1
20:09:13.236013 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 45:46, ack 1, win 229, options [nop,nop,TS val 10441184 ecr 12791793], length 1
20:09:13.236223 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 46:47, ack 1, win 229, options [nop,nop,TS val 10441185 ecr 12791793], length 1
20:09:13.236249 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 47:48, ack 1, win 229, options [nop,nop,TS val 10441185 ecr 12791793], length 1
20:09:13.236624 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 48:49, ack 1, win 229, options [nop,nop,TS val 10441185 ecr 12791793], length 1
20:09:13.236631 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 49:50, ack 1, win 229, options [nop,nop,TS val 10441185 ecr 12791793], length 1
20:09:13.236632 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 50:51, ack 1, win 229, options [nop,nop,TS val 10441185 ecr 12791793], length 1
20:09:13.236635 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 51:52, ack 1, win 229, options [nop,nop,TS val 10441185 ecr 12791793], length 1
20:09:13.236637 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 52:53, ack 1, win 229, options [nop,nop,TS val 10441185 ecr 12791793], length 1
20:09:13.236774 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 53:54, ack 1, win 229, options [nop,nop,TS val 10441185 ecr 12791793], length 1
20:09:13.236780 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 54:55, ack 1, win 229, options [nop,nop,TS val 10441185 ecr 12791793], length 1
20:09:13.236781 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 55:56, ack 1, win 229, options [nop,nop,TS val 10441185 ecr 12791793], length 1
20:09:13.236990 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 56:57, ack 1, win 229, options [nop,nop,TS val 10441185 ecr 12791793], length 1
20:09:13.237000 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 57:58, ack 1, win 229, options [nop,nop,TS val 10441185 ecr 12791793], length 1
20:09:13.237003 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 58:59, ack 1, win 229, options [nop,nop,TS val 10441185 ecr 12791793], length 1
20:09:13.237134 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 59:60, ack 1, win 229, options [nop,nop,TS val 10441185 ecr 12791794], length 1
20:09:13.237237 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 60:61, ack 1, win 229, options [nop,nop,TS val 10441186 ecr 12791794], length 1
20:09:13.237328 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 61:62, ack 1, win 229, options [nop,nop,TS val 10441186 ecr 12791794], length 1
20:09:13.237504 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 62:63, ack 1, win 229, options [nop,nop,TS val 10441186 ecr 12791794], length 1
20:09:13.237686 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 63:64, ack 1, win 229, options [nop,nop,TS val 10441186 ecr 12791794], length 1
20:09:13.237692 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 64:65, ack 1, win 229, options [nop,nop,TS val 10441186 ecr 12791794], length 1
20:09:13.237694 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 65:66, ack 1, win 229, options [nop,nop,TS val 10441186 ecr 12791794], length 1
20:09:13.237854 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 66:67, ack 1, win 229, options [nop,nop,TS val 10441186 ecr 12791794], length 1
20:09:13.237986 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 67:68, ack 1, win 229, options [nop,nop,TS val 10441186 ecr 12791794], length 1
20:09:13.237994 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 68:69, ack 1, win 229, options [nop,nop,TS val 10441186 ecr 12791794], length 1
20:09:13.238140 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 69:70, ack 1, win 229, options [nop,nop,TS val 10441186 ecr 12791794], length 1
20:09:13.238149 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 70:71, ack 1, win 229, options [nop,nop,TS val 10441186 ecr 12791794], length 1
20:09:13.238151 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 71:72, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791794], length 1
20:09:13.238638 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 72:73, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.238645 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 73:74, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.238647 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 74:75, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.238654 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 75:76, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.238657 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 76:77, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.238659 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 77:78, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.238661 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 78:79, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.238663 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 79:80, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.238666 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 80:81, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.238908 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 81:82, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.238915 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 82:83, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.238918 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 83:84, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.238923 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 84:85, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.238925 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 85:86, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.239107 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 86:87, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.239118 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 87:88, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.239120 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 88:89, ack 1, win 229, options [nop,nop,TS val 10441187 ecr 12791795], length 1
20:09:13.239364 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 89:90, ack 1, win 229, options [nop,nop,TS val 10441188 ecr 12791796], length 1
20:09:13.239373 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 90:91, ack 1, win 229, options [nop,nop,TS val 10441188 ecr 12791796], length 1
20:09:13.239375 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 91:92, ack 1, win 229, options [nop,nop,TS val 10441188 ecr 12791796], length 1
20:09:13.239377 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 92:93, ack 1, win 229, options [nop,nop,TS val 10441188 ecr 12791796], length 1
20:09:13.239439 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 93:94, ack 1, win 229, options [nop,nop,TS val 10441188 ecr 12791796], length 1
20:09:13.240092 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 94:95, ack 1, win 229, options [nop,nop,TS val 10441188 ecr 12791796], length 1
20:09:13.240098 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 95:96, ack 1, win 229, options [nop,nop,TS val 10441188 ecr 12791796], length 1
20:09:13.240100 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 96:97, ack 1, win 229, options [nop,nop,TS val 10441188 ecr 12791796], length 1
20:09:13.240101 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 97:98, ack 1, win 229, options [nop,nop,TS val 10441188 ecr 12791796], length 1
20:09:13.240102 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 98:99, ack 1, win 229, options [nop,nop,TS val 10441188 ecr 12791796], length 1
20:09:13.240104 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 99:100, ack 1, win 229, options [nop,nop,TS val 10441188 ecr 12791796], length 1
20:09:13.240105 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 100:101, ack 1, win 229, options [nop,nop,TS val 10441188 ecr 12791796], length 1
20:09:13.240106 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 101:102, ack 1, win 229, options [nop,nop,TS val 10441188 ecr 12791796], length 1
20:09:13.240107 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 102:103, ack 1, win 229, options [nop,nop,TS val 10441189 ecr 12791796], length 1
20:09:13.240260 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 103:104, ack 1, win 229, options [nop,nop,TS val 10441189 ecr 12791796], length 1
20:09:13.240264 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 104:105, ack 1, win 229, options [nop,nop,TS val 10441189 ecr 12791796], length 1
20:09:13.240377 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 105:106, ack 1, win 229, options [nop,nop,TS val 10441189 ecr 12791796], length 1
20:09:13.240382 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 106:107, ack 1, win 229, options [nop,nop,TS val 10441189 ecr 12791796], length 1
20:09:13.240384 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 107:108, ack 1, win 229, options [nop,nop,TS val 10441189 ecr 12791796], length 1
20:09:13.240488 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 108:109, ack 1, win 229, options [nop,nop,TS val 10441189 ecr 12791796], length 1
20:09:13.240594 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 109:110, ack 1, win 229, options [nop,nop,TS val 10441189 ecr 12791796], length 1
20:09:13.240669 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 110:111, ack 1, win 229, options [nop,nop,TS val 10441189 ecr 12791797], length 1
20:09:13.240864 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 111:112, ack 1, win 229, options [nop,nop,TS val 10441189 ecr 12791798], length 1
20:09:13.240890 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 112:113, ack 1, win 229, options [nop,nop,TS val 10441189 ecr 12791798], length 1
20:09:13.241198 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 113:114, ack 1, win 229, options [nop,nop,TS val 10441189 ecr 12791798], length 1
20:09:13.241230 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 114:115, ack 1, win 229, options [nop,nop,TS val 10441189 ecr 12791798], length 1
20:09:13.241235 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 115:116, ack 1, win 229, options [nop,nop,TS val 10441190 ecr 12791798], length 1
20:09:13.241237 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 116:117, ack 1, win 229, options [nop,nop,TS val 10441190 ecr 12791798], length 1
20:09:13.241270 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 117:118, ack 1, win 229, options [nop,nop,TS val 10441190 ecr 12791798], length 1
20:09:13.241491 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 118:119, ack 1, win 229, options [nop,nop,TS val 10441190 ecr 12791798], length 1
20:09:13.241538 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 119:120, ack 1, win 229, options [nop,nop,TS val 10441190 ecr 12791798], length 1
20:09:13.241541 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 120:121, ack 1, win 229, options [nop,nop,TS val 10441190 ecr 12791798], length 1
20:09:13.241680 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 121:122, ack 1, win 229, options [nop,nop,TS val 10441190 ecr 12791798], length 1
20:09:13.242199 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 122:123, ack 1, win 229, options [nop,nop,TS val 10441190 ecr 12791798], length 1
20:09:13.242407 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 123:124, ack 1, win 229, options [nop,nop,TS val 10441190 ecr 12791798], length 1
20:09:13.242434 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 124:125, ack 1, win 229, options [nop,nop,TS val 10441190 ecr 12791799], length 1
20:09:13.242436 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 125:126, ack 1, win 229, options [nop,nop,TS val 10441190 ecr 12791799], length 1
20:09:13.242443 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 126:127, ack 1, win 229, options [nop,nop,TS val 10441190 ecr 12791799], length 1
20:09:13.242444 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 127:246, ack 1, win 229, options [nop,nop,TS val 10441191 ecr 12791799], length 119
20:09:13.242617 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 246:247, ack 1, win 229, options [nop,nop,TS val 10441191 ecr 12791799], length 1
20:09:13.242628 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 247:321, ack 1, win 229, options [nop,nop,TS val 10441191 ecr 12791799], length 74
20:09:13.243579 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 321:322, ack 1, win 229, options [nop,nop,TS val 10441191 ecr 12791799], length 1
20:09:13.243628 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 322:596, ack 1, win 229, options [nop,nop,TS val 10441192 ecr 12791800], length 274
20:09:13.244053 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 596:597, ack 1, win 229, options [nop,nop,TS val 10441192 ecr 12791800], length 1
20:09:13.244105 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 597:912, ack 1, win 229, options [nop,nop,TS val 10441192 ecr 12791801], length 315
20:09:13.244304 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [P.], seq 912:913, ack 1, win 229, options [nop,nop,TS val 10441192 ecr 12791801], length 1
20:09:13.244366 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [FP.], seq 913:1000, ack 1, win 229, options [nop,nop,TS val 10441192 ecr 12791801], length 87
20:09:13.246058 IP 192.168.2.105.39736 > 192.168.2.100.11111: Flags [.], ack 2, win 229, options [nop,nop,TS val 10441193 ecr 12791801], length 0

7 まとめ

実験結果より次のことがわかりました。

  • TCP_NODELAY有効の場合:送信データを送信バッファにためることなく、すぐに送信する。
  • TCP_NODELAY無効の場合:送信データを送信バッファにためてから送信する。(デフォルトの動作)

Z 参考情報

私が業務や記事執筆で参考にした書籍を以下のページに記載します。
Linux技術のスキルアップをしよう! - hana_shinのLinux技術ブログ