{"id":252406,"date":"2022-09-19T10:05:00","date_gmt":"2022-09-19T02:05:00","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=252406"},"modified":"2022-09-14T22:05:52","modified_gmt":"2022-09-14T14:05:52","slug":"xdp-method","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/xdp-method.html","title":{"rendered":"XDP\u7684\u91cd\u5b9a\u5411\u5de5\u4f5c\u539f\u7406"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nXDP Scoket\u4e5f\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7poll\/epoll\/select\u6765\u7b49\u5f85IO\u4e8b\u4ef6\uff0c\u9700\u8981\u8bf4\u660e\u7684\u662f\uff1a\u6536\/\u53d1\u7684\u6570\u636e\u5305\u662f\u539f\u59cb\u7684\u4ee5\u592a\u7f51\u5e27\uff0c\u56e0\u6b64\u5728\u5305\u5904\u7406\u4e0a\u8981\u9ebb\u70e6\u4e00\u4e9b\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u4e00. XDP Socket\u793a\u4f8b\u89e3\u6790<\/strong><\/div>\n

\u6e90\u7801\u53c2\u89c1\uff1ahttps:\/\/github.com\/xdp-project\/xdp-tutorial\/tree\/master\/advanced03-AF_XDP \u8be5\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u901a\u8fc7BPF\u5c06\u7f51\u7edc\u6570\u636e\u5305\u4eceXDP Hook\u70b9\u65c1\u8def\u5230\u7528\u6237\u6001\u7684XDP Socket\uff0c\u89e3\u6790\u8fc7\u7a0b\u4e2d\u4e3a\u7a81\u51fa\u91cd\u70b9\uff0c\u5c06\u53ea\u5173\u6ce8\u91cd\u70b9\u4ee3\u7801\u6bb5\uff0c\u4e00\u4e9b\u51fd\u6570\u4f1a\u88ab\u7cbe\u7b80\uff0c\u6bd4\u5982\uff1a\u9519\u8bef\u5904\u7406\u7b49\u3002<\/p>\n

\u4e8c. BPF \u7a0b\u5e8f af_xdp_kern.c<\/strong><\/div>\n

BPF\u7a0b\u5e8f\u662f\u8fd0\u884c\u5728\u5185\u6838\u6001\u7684\u4e00\u6bb5\u4ee3\u7801\uff0c\u5982\u4e0b\uff1a<\/p>\n

struct bpf_map_def SEC(\"maps\") xsks_map = {\r\n    .type = BPF_MAP_TYPE_XSKMAP,\r\n    .key_size = sizeof(int),\r\n    .value_size = sizeof(int),\r\n    .max_entries = 64,  \/* Assume netdev has no more than 64 queues *\/\r\n};\r\n\r\nSEC(\"xdp_sock\")\r\nint xdp_sock_prog(struct xdp_md *ctx)\r\n{\r\n    int index = ctx->rx_queue_index;\r\n\r\n    if (bpf_map_lookup_elem(&xsks_map, &index))\r\n        return bpf_redirect_map(&xsks_map, index, 0);\r\n    \r\n    return XDP_PASS;\r\n}<\/pre>\n

struct bpf_map_def SEC(\"maps\") xsks_map\u200b\uff1a \u5b9a\u4e49\u4e86\u4e00\u4e2aBPF_MAP_TYPE_XSKMAP\u7c7b\u578b\u7684\u6620\u5c04\u8868\uff0c\u5f53\u91c7\u7528SEC(\"maps\")\u65b9\u5f0f\u6765\u663e\u793a\u5b9a\u4e49\u65f6\uff0c\u5c06\u5728\u751f\u6210\u7684bpf\u76ee\u6807\u6587\u4ef6\u7684ELF\u683c\u5f0f\u4e2d\u770b\u5230\u76f8\u5173\u63cf\u8ff0\uff0c\u5f53BPF\u7a0b\u5e8f\u88ab\u52a0\u8f7d\u5230\u5185\u6838\u65f6\uff0c\u4f1a\u81ea\u52a8\u521b\u5efa\u540d\u4e3a\u201cxsks_map\u201d\u7684\u63cf\u8ff0\u7b26\uff0c \u7528\u6237\u6001\u53ef\u901a\u8fc7\u67e5\u627e\u201cxsks_map\u201d\u6765\u83b7\u53d6\u8be5map\u7684\u63cf\u8ff0\u7b26\uff0c\u8fd9\u6837\u7528\u6237\u6001\u548c\u5185\u6838BPF\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u5171\u540c\u8bbf\u95ee\u8be5map\u3002<\/p>\n

type = BPF_MAP_TYPE_XSKMAP\uff1a\u6307\u5b9a\u8be5map\u7684\u7c7b\u578b\uff0c\u5b83\u4e0ebpf_redirect_map() \u7ed3\u5408\u4f7f\u7528\u4ee5\u5c06\u6536\u5230\u7684\u5e27\u4f20\u9012\u5230\u6307\u5b9a\u5957\u63a5\u5b57\u3002<\/p>\n

key_size = sizeof(int)\uff0cvalue_size = sizeof(int)\uff1a\u6307\u5b9akey\uff0cvalue\u957f\u5ea6\u3002<\/p>\n

\u9488\u5bf9\u4ee5\u4e0akey\uff0cvalue\u9700\u8981\u8bf4\u660e\u4e00\u4e0b\uff1a\u5bf9\u4e8eBPF_MAP_TYPE_XSKMAP\u7c7b\u578b\u7684map\uff0cvalue\u5fc5\u987b\u662fXDP socket\u63cf\u8ff0\u7b26\uff0ckey\u5fc5\u987b\u662fint\u7c7b\u578b\uff0c\u539f\u56e0\u5728\u4e8ebpf_redirect_map()\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u53c2\u89c1\u4e0b\u97622.10\u3002<\/p>\n

max_entries = 64\uff1a\u6307\u5b9amap\u6700\u591a\u5b58\u50a864\u4e2a\u5143\u7d20\u3002<\/p>\n

SEC(\"xdp_sock\")\uff1a\u6307\u5b9aprog\u51fd\u6570\u7b26\u53f7\uff0c\u5e94\u7528\u5c42\u53ef\u901a\u8fc7\u67e5\u627e\"xdp_sock\"\u52a0\u8f7d\u8be5prog\uff0c\u5e76\u7ed1\u5b9a\u5230\u6307\u5b9a\u7f51\u5361\u3002<\/p>\n

int xdp_sock_prog(struct xdp_md *ctx)\uff1a\u5f53\u7f51\u5361\u6536\u5230\u6570\u636e\u5305\u65f6\uff0c\u4f1a\u5728xdp hook\u70b9\u8c03\u7528\u8be5\u51fd\u6570\u3002<\/p>\n

int index = ctx->rx_queue_index\uff1a \u83b7\u53d6\u8be5\u6570\u636e\u5305\u6765\u81ea\u7f51\u5361\u5230\u54ea\u4e2arx\u961f\u5217ID\uff0cctx\u6709\u8bb8\u591a\u6210\u5458\uff0c\u6bd4\u5982\uff1a\u7f51\u5361ID\uff0c\u6570\u636e\u5e27\u7b49\u7b49\u3002<\/p>\n

if (bpf_map_lookup_elem(&xsks_map, &index))\uff1a \u5224\u65adxsks_map\u662f\u5426\u5b58\u5728key\u4e3aindex\uff08\u5373rx\u961f\u5217\u53f7\uff09\u7684\u6570\u636e\uff0c\u6ce8\u610f\uff0c\u8fd9\u91cc\u5b9e\u9645\u4e0a\u5c31\u662f\u5224\u65ad\u8be5\u7f51\u5361\u662f\u5426\u7ed1\u5b9a\u4e86xdp Socket\u3002<\/p>\n

bpf_redirect_map(&xsks_map, index, 0)\uff1abpf_redirect_map\u200b\u51fd\u6570\u4f5c\u7528\u5c31\u662f\u91cd\u5b9a\u5411\uff0c\u6bd4\u5982\uff1a\u5c06\u6570\u636e\u91cd\u5b9a\u5411\u5230\u67d0\u4e2a\u7f51\u5361\uff0cCPU\uff0c Socket\u7b49\u7b49\uff1b\u5f53bpf_redirect_map\u200b\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u7684map\u7c7b\u578b\u4e3aBPF_MAP_TYPE_XSKMAP\u65f6\uff0c\u5219\u8868\u793a\u5c06\u6570\u636e\u91cd\u5b9a\u5411\u5230XDP Scoket\u3002<\/p>\n

bpf_redirect_map\uff08\uff09\u4f1a\u67e5\u627e\u53c2\u65701\u5373xsks_map \u4e2d key\u4e3aindex \u7684 value \u662f\u5426\u5b58\u5728\uff0c\u82e5\u5b58\u5728\uff0c\u5219\u68c0\u67e5value\u662f\u5426\u662f\u4e00\u4e2aXDP Scoket\uff0c\u5e76\u4e14\u662f\u5426\u7ed1\u5b9a\u5230\u4e86\u8be5\u7f51\u5361\uff08\u53ef\u4ee5\u7ed1\u5b9a\u5230\u4efb\u610f\u6709\u6548\u961f\u5217\uff09\u3002<\/p>\n

\u7efc\u5408\u4ee5\u4e0a\uff0c\u8be5bpf\u7a0b\u5e8f\u5b9e\u73b0\u7684\u529f\u80fd\u5c31\u662f\uff1a\u5c06\u6536\u5230\u7684\u6570\u636e\u5305\u91cd\u5b9a\u5411\u5230xsks_map\u4e2d\u6307\u5b9a\u7684XDP Socket\u3002<\/p>\n

\u4e09. \u7528\u6237\u6001\u7a0b\u5e8f af_xdp_user.c<\/strong><\/div>\n

\u8be5\u7a0b\u5e8f\u5b9e\u73b0bpf\u52a0\u8f7d\u5230\u7f51\u5361\uff0c\u521b\u5efaXDP Scoket\u5e76\u7ed1\u5b9a\u5230\u7f51\u5361\u7684\u6307\u5b9a\u961f\u5217\uff0c\u5e76\u901a\u8fc7XDP Scoket\u6536\u53d1\u6570\u636e\uff0c\u8fd9\u91cc\u4ec5\u5206\u6790xXDP Scoket\u76f8\u5173\u90e8\u5206\u3002<\/p>\n

int main(int argc, char **argv)\r\n{\r\n    ...\r\n    bpf_obj = load_bpf_and_xdp_attach(&cfg);\r\n    map = bpf_object__find_map_by_name(bpf_obj, \"xsks_map\");\r\n    ...\r\n    xsks_map_fd = bpf_map__fd(map);\r\n    ...\r\n    umem = configure_xsk_umem(packet_buffer, packet_buffer_size);\r\n    ...\r\n    xsk_socket = xsk_configure_socket(&cfg, umem);\r\n    ...\r\n    rx_and_process(&cfg, xsk_socket);\r\n    ...\r\n}\r\n\r\nstatic struct xsk_socket_info *xsk_configure_socket(struct config *cfg,\r\n                            struct xsk_umem_info *umem)\r\n{\r\n    ...\r\n    ret = xsk_socket__create(&xsk_info->xsk, cfg->ifname,\r\n                 cfg->xsk_if_queue, umem->umem, &xsk_info->rx,\r\n                 &xsk_info->tx, &xsk_cfg);\r\n    ...<\/pre>\n

bpf_obj = load_bpf_and_xdp_attach(&cfg): \u52a0\u8f7dbpf\u7a0b\u5e8f\uff0c\u5e76\u7ed1\u5b9a\u5230\u7f51\u5361\u3002<\/p>\n

map = bpf_object__find_map_by_name(bpf_obj, \"xsks_map\")\uff1a \u67e5\u627ebpf\u7a0b\u5e8f\u5185\u5b9a\u4e49\u7684xsks_map\u3002<\/p>\n

umem = configure_xsk_umem(packet_buffer, packet_buffer_size)\uff1a \u4e3aXDP Scoket\u51c6\u5907UMEM\u3002<\/p>\n

xsk_configure_socket()\u901a\u8fc7\u8c03\u7528bpf helper\u51fd\u6570xsk_socket__create\uff08\uff09\u521b\u5efaXDP Scoket\u5e76\u7ed1\u5b9a\u5230cfg->ifname\u7f51\u5361\u7684cfg->xsk_if_queue\u961f\u5217\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5c06\u8be5\u3010cfg->xsk_if_queue\uff0c xsk_info->xsk fd\u3011\u6dfb\u52a0\u5230xsks_map, \u8fd9\u6837bpf\u7a0b\u5e8f\u5c31\u53ef\u4ee5\u91cd\u5b9a\u5411\u5230\u8be5XDP Scoket\uff08\u53c2\u89c12.9, 2.10), \u9664\u975e\u6307\u5b9aXSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD\u6807\u5fd7\u3002<\/p>\n

static void rx_and_process(struct config *cfg,\r\n               struct xsk_socket_info *xsk_socket)\r\n{\r\n    struct pollfd fds[2];\r\n    int ret, nfds = 1;\r\n\r\n    memset(fds, 0, sizeof(fds));\r\n    fds[0].fd = xsk_socket__fd(xsk_socket->xsk);\r\n    fds[0].events = POLLIN;\r\n    \r\n    while(!global_exit) {\r\n        if (cfg->xsk_poll_mode) {\r\n            ret = poll(fds, nfds, -1);\r\n            if (ret <= 0 || ret > 1)\r\n                continue;\r\n        }\r\n        handle_receive_packets(xsk_socket);\r\n    }\r\n}<\/pre>\n

XDP Scoket\u4e5f\u662f\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7poll\/epoll\/select\u6765\u7b49\u5f85IO\u4e8b\u4ef6\uff0c\u9700\u8981\u8bf4\u660e\u7684\u662f\uff1a\u6536\/\u53d1\u7684\u6570\u636e\u5305\u662f\u539f\u59cb\u7684\u4ee5\u592a\u7f51\u5e27\uff0c\u56e0\u6b64\u5728\u5305\u5904\u7406\u4e0a\u8981\u9ebb\u70e6\u4e00\u4e9b\u3002<\/p>\n

\u56db. \u603b\u7ed3<\/strong><\/div>\n

\u4ee5\u4e0a\u7b80\u7565\u5206\u6790\u4e86bpf\u7a0b\u5e8f\u5982\u4f55\u5c06\u6570\u636e\u91cd\u5b9a\u5411\u5230\u7528\u6237\u6001\u7a0b\u5e8f\uff0c\u901a\u8fc7xsks_map\u6765\u5b9e\u73b0bpf\u4e0e\u7528\u6237\u6001\u7a0b\u5e8f\u7684\u4ea4\u4e92\uff1b<\/p>\n

\u9700\u8981\u8bf4\u660e\u7684\u662f\uff0c\u8fd9\u4e9b\u5206\u6790\u4ec5\u662f\u68b3\u7406\u4e86\u6d45\u5c42\u6b21\u7684\u4ee3\u7801\uff0c\u5b9e\u9645\u4e0aBPF\u662f\u5982\u4f55\u5c06\u6570\u636e\u8bfb\u5199\u5230XDP Scoket\u6536\u53d1\u7f13\u51b2\u533a\u7684\u5462\uff1f\u5176\u5b9e\u662f\u901a\u8fc7\u521b\u5efa\u5171\u4eab\u5185\u5b58\u5e76\u5173\u8054XDP Scoket\u7684rx_ring\uff0ctx_ring\uff0c\u4ee5\u53caumem\u6765\u5b9e\u73b0\u7684\uff0c\u540e\u7eed\u7ee7\u7eed\u5206\u6790\u3002<\/p>\n

bpf\u7a0b\u5e8f\u901a\u5e38\u90fd\u975e\u5e38\u7b80\u5355\uff0c\u590d\u6742\u7684\u662f\u7528\u6237\u6001\u7a0b\u5e8f\uff0c\u6b64\u5916\uff0cBPF\u6709\u975e\u5e38\u591a\u7684\u6280\u672f\u7ec6\u8282\uff0c\u9650\u4e8e\u7bc7\u5e45\u53ca\u4e3b\u9898\u4e0d\u5728\u6b64\u5c55\u5f00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6e90\u7801\u53c2\u89c1\uff1ahttps:\/\/github.com\/xdp-project\/xdp-tutorial\/tree\/m […]<\/p>\n","protected":false},"author":1920,"featured_media":129619,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-252406","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/252406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=252406"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/252406\/revisions"}],"predecessor-version":[{"id":252778,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/252406\/revisions\/252778"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/129619"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=252406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=252406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=252406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}