\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>\nbpf_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>\nXDP 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}]}}