{"id":3944,"date":"2013-06-02T19:25:49","date_gmt":"2013-06-02T10:25:49","guid":{"rendered":"http:\/\/umezawa.dyndns.info\/wordpress\/?p=3944"},"modified":"2015-01-07T22:50:57","modified_gmt":"2015-01-07T13:50:57","slug":"linux-kernel-3-9-%e3%81%ae-so_reuseport-2","status":"publish","type":"post","link":"http:\/\/umezawa.dyndns.info\/wordpress\/?p=3944","title":{"rendered":"Linux Kernel 3.9 \u306e SO_REUSEPORT"},"content":{"rendered":"<p>Linux Kernel 3.9 \u306b\u306f SO_REUSEPORT \u304c\u8ffd\u52a0\u3055\u308c\u3066\u3044\u308b\u305d\u3046\u3067\u3059\u3002<a href=\"https:\/\/www.google.co.jp\/search?q=SO_REUSEPORT&#038;ie=utf-8&#038;oe=utf-8&#038;lr=lang_ja\">SO_REUSEPORT \u3067\u30b0\u30b0\u308b\u3068\u65e5\u672c\u8a9e\u306e\u30da\u30fc\u30b8\u306e\u4e2d\u3067\u4e00\u756a\u4e0a\u306b\u51fa\u3066\u304f\u308b\u5f53blog\uff082013-06-02\u73fe\u5728\uff09<\/a>\u3068\u3057\u3066\u306f\u8abf\u67fb\u3057\u306a\u3044\u308f\u3051\u306b\u306f\u3044\u304d\u307e\u305b\u3093 :-)<\/p>\n<p><!--more--><\/p>\n<p>Linux \u306e SO_REUSEPORT \u306f\u300cTCP \u30bd\u30b1\u30c3\u30c8\u3092\u5b8c\u5168\u91cd\u8907 bind \u3057\u3001\u53d7\u3051\u4ed8\u3051\u305f\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3092\u305d\u308c\u305e\u308c\u306e\u30bd\u30b1\u30c3\u30c8\u306b\u9069\u5f53\u306b\u5272\u308a\u632f\u308b\u300d\u3068\u3044\u3046\u6a5f\u80fd\u306e\u3088\u3046\u3067\u3059\u3002\u3053\u308c\u306f\u3001BSD \u306e SO_REUSEPORT \u3088\u308a\u6a5f\u80fd\u304c\u300c\u5f37\u3044\u300d\u3068\u3044\u3046\u3053\u3068\u3067\u3059\uff08<a href=\"http:\/\/umezawa.dyndns.info\/wordpress\/?p=2609\">\u53c2\u8003<\/a>\uff09\u3002SO_REUSEADDR \u306e\u6642\u3082\u305d\u3046\u3067\u3059\u304c\u3001\u540c\u3058\u540d\u524d\u3067\u9055\u3046\u6a5f\u80fd\u306b\u3059\u308b\u306e\u3084\u3081\u3066\u304f\u308c\u306a\u3044\u304b\u306a\u3041\u2026<\/p>\n<h3>\u5b9f\u9a13<\/h3>\n<p>SO_REUSEPORT \u304c\u8ffd\u52a0\u3055\u308c\u305f\u306e\u306f3\u670810\u65e5\u3054\u308d\u306e\u3088\u3046\u306a\u306e\u3067\u3059\u304c\u3001\u65e2\u306b3\u304b\u6708\u305f\u3063\u3066\u3044\u3066 Fedora 18 \u304c Kernel 3.9 \u30d9\u30fc\u30b9\u306b\u306a\u3063\u3066\u304a\u308a\u3001\u8a66\u3059\u305f\u3081\u306b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u304b\u3089\u30d3\u30eb\u30c9\u3068\u304b\u306f\u3057\u306a\u304f\u3066\u3082\u69cb\u3044\u307e\u305b\u3093\u3002<\/p>\n<blockquote>\n<pre>[root@h150n000 ~]# uname -a\r\nLinux h150n000.local 3.9.4-200.fc18.i686.PAE #1 SMP Fri May 24 20:24:58 UTC 2013 i686 i686 i386 GNU\/Linux\r\n[root@h150n000 ~]# grep -r SO_REUSEPORT \/usr\/include\r\n\/usr\/include\/asm-generic\/socket.h:#define SO_REUSEPORT  15\r\n[root@h150n000 ~]# <\/pre>\n<\/blockquote>\n<p>\u3068\u3044\u3046\u308f\u3051\u3067\u30c6\u30b9\u30c8\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u3059\u3002\u5b50\u30d7\u30ed\u30bb\u30b9\u30924\u3064\u4f5c\u3063\u3066\u3001\u305d\u308c\u305e\u308c\u304c SO_REUSEPORT \u3092\u4ed8\u3051\u305f\u30bd\u30b1\u30c3\u30c8\u3092\u540c\u4e00\u306e\u30a2\u30c9\u30ec\u30b9\/\u30dd\u30fc\u30c8\u306b bind \u3057\u307e\u3059\u3002<\/p>\n<blockquote>\n<pre>[root@h150n000 ~]# cat reuse.c\r\n#include &lt;sys\/types.h&gt;\r\n#include &lt;sys\/socket.h&gt;\r\n#include &lt;sys\/wait.h&gt;\r\n\r\n#include &lt;netdb.h&gt;\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\n#include &lt;netinet\/in.h&gt;\r\n\r\nvoid child(int idx)\r\n{\r\n        int S, C;\r\n        const int on = 1;\r\n        struct addrinfo *res;\r\n        struct addrinfo hints;\r\n        struct sockaddr_in sin4;\r\n        socklen_t slen;\r\n\r\n        memset(&hints, sizeof(hints), 0);\r\n        hints.ai_family = AF_INET;\r\n        hints.ai_socktype = SOCK_STREAM;\r\n        hints.ai_protocol = IPPROTO_TCP;\r\n        hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;\r\n        getaddrinfo(\"127.0.0.1\", \"10000\", &hints, &res);\r\n        S = socket(res-&gt;ai_family, res-&gt;ai_socktype, res-&gt;ai_protocol);\r\n        setsockopt(S, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on));\r\n        bind(S, res-&gt;ai_addr, res-&gt;ai_addrlen);\r\n        freeaddrinfo(res);\r\n        listen(S, 100);\r\n\r\n        slen = sizeof(sin4);\r\n        while ((C = accept(S, (struct sockaddr *)&sin4, &slen)) &gt;= 0) {\r\n                close(C);\r\n                printf(\"accepted by child #%d\\n\", idx);\r\n        }\r\n}\r\n\r\nint main(void)\r\n{\r\n        int status;\r\n        int i;\r\n\r\n        for (i = 0; i &lt; 4; i++) {\r\n                pid_t pid = fork();\r\n                if (pid == 0) {\r\n                        child(i);\r\n                        return 0;\r\n                } else if (pid < 0) {\r\n                        perror(\"fork\");\r\n                        return 1;\r\n                }\r\n        }\r\n\r\n        wait(&#038;status);\r\n\r\n        return 0;\r\n}\r\n[root@h150n000 ~]#<\/pre>\n<\/blockquote>\n<p>\u8d70\u3089\u305b\u3066\u540c\u4e00\u30db\u30b9\u30c8\u4e0a\u304b\u3089 <code>nc 127.0.0.1 10000<\/code> \u3068\u9023\u6253\u3059\u308b\u3068\u3001\u4f8b\u3048\u3070\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u7d50\u679c\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<blockquote>\n<pre>[root@h150n000 ~]# .\/reuse\r\naccepted by child #0\r\naccepted by child #2\r\naccepted by child #1\r\naccepted by child #0\r\naccepted by child #2\r\naccepted by child #0\r\naccepted by child #1\r\naccepted by child #3\r\naccepted by child #0\r\naccepted by child #1\r\naccepted by child #3\r\naccepted by child #1\r\naccepted by child #2\r\naccepted by child #2\r\naccepted by child #3\r\naccepted by child #3\r\naccepted by child #3<\/pre>\n<\/blockquote>\n<p>\u304a\u304a\u3080\u306d\u9069\u5f53\u306b\u5272\u308a\u632f\u3089\u308c\u3066\u3044\u308b\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u3002<\/p>\n<p>\u4e00\u65b9\u3001\u540c\u3058\u30d7\u30ed\u30b0\u30e9\u30e0\u3092 BSD \u4e0a\u3067\u52d5\u304b\u3059\u3068\u3001\u4f8b\u3048\u3070\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u7d50\u679c\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<blockquote>\n<pre>[root@h150n000 ~]# .\/reuse\r\naccepted by child #3\r\naccepted by child #3\r\naccepted by child #3\r\naccepted by child #3\r\naccepted by child #3\r\naccepted by child #3\r\naccepted by child #3\r\naccepted by child #3\r\naccepted by child #3\r\naccepted by child #3\r\naccepted by child #3<\/pre>\n<\/blockquote>\n<p>BSD \u306e\u5834\u5408\u306f\u300c\u9069\u5f53\u306b\u5272\u308a\u632f\u308b\u300d\u3068\u3044\u3046\u6a5f\u80fd\u304c\u306a\u3044\u306e\u3067\u3001\u4f55\u56de\u3084\u3063\u3066\u3082\u540c\u3058\u30bd\u30b1\u30c3\u30c8\u3067 accept \u3055\u308c\u307e\u3059\u3002<\/p>\n<h3>\u7528\u9014<\/h3>\n<p>\u8907\u6570\u306e\u30d7\u30ed\u30bb\u30b9\uff08\u3042\u308b\u3044\u306f\u30b9\u30ec\u30c3\u30c9\u3002\u4ee5\u4e0b\u540c\u3058\uff09\u3067\u5358\u4e00\u306e\u30a2\u30c9\u30ec\u30b9\/\u30dd\u30fc\u30c8\u3092\u5f85\u3061\u53d7\u3051\u308b\u5834\u5408\u3067\u3042\u3063\u3066\u3082\u3001\u5f85\u3061\u53d7\u3051\u308b\u30dd\u30fc\u30c8\u304c1\u3064\u3067\u3001\u304b\u3064\u5f85\u3063\u3066\u308b\u9593\u4f55\u3082\u3057\u306a\u304f\u3066\u3044\u3044\uff08accept \u3067\u30d6\u30ed\u30c3\u30af\u3057\u3066\u3044\u3066\u3044\u3044\uff09\u306e\u3067\u3042\u308c\u3070\u3001SO_REUSEPORT \u306f\u5168\u304f\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3002\u5358\u306b\u30bd\u30b1\u30c3\u30c8\u30921\u3064\u3060\u3051\u4f5c\u3063\u3066 bind \u3057\u3001\u305d\u308c\u3092\u5168\u3066\u306e\u30d7\u30ed\u30bb\u30b9\u3067 accept \u3059\u308c\u3070\u3044\u3044\u3060\u3051\u3067\u3059\u3002<\/p>\n<p>SO_REUSEPORT \u306b\u610f\u5473\u304c\u51fa\u3066\u304f\u308b\u306e\u306f\u5f85\u3061\u53d7\u3051\u308b\u30dd\u30fc\u30c8\u304c\u305f\u304f\u3055\u3093\u3042\u308b\uff08\u304b\u3064\u3001\u30dd\u30fc\u30c8\u3054\u3068\u306b\u62c5\u5f53\u7528\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u7528\u610f\u3057\u306a\u3044\uff09\u5834\u5408\u3067\u3059\u3002\u3053\u306e\u72b6\u614b\u3067\u306f\u5404\u3005\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u8907\u6570\u306e\u30dd\u30fc\u30c8=\u30bd\u30b1\u30c3\u30c8\u306e\u9762\u5012\u3092\u898b\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304c\u3001select \u306a\u3069\u3067\u5f85\u3063\u3066\u3044\u308b\u30681\u3064\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u6765\u305f\u3060\u3051\u3067\u5168\u30d7\u30ed\u30bb\u30b9\u304c\u4e00\u6589\u306b\u8d77\u304d\u4e0a\u304c\u3063\u3066\u3057\u307e\u3046\uff08\u3067\u3082\u5b9f\u969b\u306b accept \u3059\u308b\u306e\u306f1\u30d7\u30ed\u30bb\u30b9\u3060\u3051\uff09\u305f\u3081\u3001\u7121\u99c4\u304c\u51fa\u307e\u3059\u3002<\/p>\n<p>\u5404\u30d7\u30ed\u30bb\u30b9\u304c SO_REUSEPORT \u3067\u5168\u30dd\u30fc\u30c8\u306b\u30bd\u30b1\u30c3\u30c8\u3092\u4f5c\u3063\u3066\u304a\u3051\u3070\u3001\u8d77\u304d\u308b\u30d7\u30ed\u30bb\u30b9\u306f1\u3064\u3060\u3051\u306b\u306a\u308a\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u3053\u306e\u5834\u5408\u3001\u6687\u306a\u30d7\u30ed\u30bb\u30b9\u304c\u4ed6\u306b\u3042\u308b\u306e\u306b\u5fd9\u3057\u3044\u30d7\u30ed\u30bb\u30b9\uff08\u306e\u30bd\u30b1\u30c3\u30c8\uff09\u306b\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u5230\u7740\u3059\u308b\u3068\u6b8b\u5ff5\u306a\u3053\u3068\u306b\u306a\u308b\u306e\u3067\u3001\u305d\u306e\u8fba\u3092\u3061\u3083\u3093\u3068\u8003\u3048\u3066\u304a\u304b\u306a\u3044\u3068\u7d50\u5c40\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u4e0a\u304c\u3089\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<p># \u30ea\u30b9\u30ca\u30fc\u30bd\u30b1\u30c3\u30c8\u306e\u914d\u5217\u3092\u6e21\u3057\u3066\u3069\u308c\u304b\u304b\u3089 accept \u3059\u308b\u3001\u3068\u3044\u3046\u30b7\u30b9\u30c6\u30e0\u30b3\u30fc\u30eb\u304c\u3042\u308c\u3070\u3044\u3044\u3093\u3058\u3083\u306d\u30fc\u306e\uff1f\u3068\u4eca\u601d\u3063\u305f<\/p>\n<h3>UDP \u306e\u5834\u5408\u306f\uff1f<\/h3>\n<p>\u8a66\u3057\u3066\u307e\u305b\u3093\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux Kernel 3.9 \u306b\u306f SO_REUSEPORT \u304c\u8ffd\u52a0\u3055\u308c\u3066\u3044\u308b\u305d\u3046\u3067\u3059\u3002SO_REUSEPORT \u3067\u30b0\u30b0\u308b\u3068\u65e5\u672c\u8a9e\u306e\u30da\u30fc\u30b8\u306e\u4e2d\u3067\u4e00\u756a\u4e0a\u306b\u51fa\u3066\u304f\u308b\u5f53blog\uff082013-06-02\u73fe\u5728\uff09\u3068\u3057\u3066\u306f\u8abf\u67fb\u3057\u306a\u3044 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-3944","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3944","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3944"}],"version-history":[{"count":8,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3944\/revisions"}],"predecessor-version":[{"id":5288,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3944\/revisions\/5288"}],"wp:attachment":[{"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3944"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/umezawa.dyndns.info\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}