UDPソケットのSO_INCOMING_CPUを検索しますか?

UDPソケットのSO_INCOMING_CPUを検索しますか?

ソケットAPIは、SO_INCOMING_CPU以下で詳しく説明するオプションを提供します。マニュアルページ:

   SO_INCOMING_CPU (gettable since Linux 3.19, settable since Linux 4.4)
         Sets or gets the CPU affinity of a socket.  Expects an integer
         flag.

             int cpu = 1;
             setsockopt(fd, SOL_SOCKET, SO_INCOMING_CPU, &cpu, sizeof(cpu));

         Because all of the packets for a single stream (i.e., all
         packets for the same 4-tuple) arrive on the single RX queue
         that is associated with a particular CPU, the typical use case
         is to employ one listening process per RX queue, with the
         incoming flow being handled by a listener on the same CPU that
         is handling the RX queue.  This provides optimal NUMA behavior
         and keeps CPU caches hot.

UDPソケットを作成し、インターフェイスとデータにバインドするサーバーがありますrecv()。このソケットを手に入れたいですSO_INCOMING_CPU。それは可能ですか?

テストプログラムを作成しましたが、テストプログラムは常に-1を返します。

デバッグをしましたが、問題がソケットに関連していprintk()ないようです。inet_daddrhttps://github.com/torvalds/linux/blob/v4.20-rc2/net/ipv4/udp.c#L1870

私はこれが意味があると思います。 UDPには接続がないため、ソケットは任意のアドレスからパケットを受信できます。ソケットのCPUアフィニティは4タプルの関数なので、ソケットが必ずしも固定CPUアフィニティを持つ必要はないという内容をどこかで読んだようです。

ところで、udp.cにCPU Affinityを初期化するコードがあると思ったら、何か抜けたものがあるのではないかと思いました。

SO_INCOMING_CPUサーバー側UDPソケットを使用する方法はありますか?

私が言及した(少し乱雑な)テストプログラムは次のとおりです。

#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

int main()
{
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);

    if (sockfd < 0)
    {  
        perror("Error opening socket");
        return 0;
    }

    struct sockaddr_in myaddr;

    memset((char *)&myaddr, 0, sizeof(myaddr));
    myaddr.sin_family = AF_INET;
    myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    myaddr.sin_port = htons(4242);

    if (bind(sockfd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
        perror("bind failed");
        return 0;
    }

    #define BUFSIZE 20
    unsigned char buf[BUFSIZE];     /* receive buffer */
    struct sockaddr_in remaddr;     /* remote address */
    socklen_t addrlen = sizeof(remaddr);            /* length of addresses */
    int recvlen = recvfrom(sockfd, buf, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen);

    sleep(1);

    int cpu = 0;
    socklen_t len = sizeof(cpu);
    int ret = getsockopt(sockfd, SOL_SOCKET, SO_INCOMING_CPU, &cpu, &len);

    // Sample Output: Incoming CPU is -1 (ret = 0, recvlen = 5)
    printf("Incoming CPU is %d (ret = %d, recvlen = %d)\n", cpu, ret, recvlen);
}

ベストアンサー1

おすすめ記事