{"id":208408,"date":"2025-09-15T17:30:05","date_gmt":"2025-09-15T09:30:05","guid":{"rendered":"https:\/\/www.server.hk\/blog\/?p=208408"},"modified":"2025-09-15T17:30:05","modified_gmt":"2025-09-15T09:30:05","slug":"%e6%8e%8c%e6%8f%a1linux%e5%86%85%e6%a0%b8%e7%bd%91%e7%bb%9c%ef%bc%9a%e6%a0%b8%e5%bf%83%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84sk_buff","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208408\/","title":{"rendered":"\u638c\u63e1Linux\u5185\u6838\u7f51\u7edc\uff1a\u6838\u5fc3\u6570\u636e\u7ed3\u6784sk_buff"},"content":{"rendered":"<p dir=\"auto\">\u5728Linux\u5185\u6838\u7f51\u7edc\u7684\u590d\u6742\u4e16\u754c\u4e2d\uff0c\u5bf9\u4e8e\u4ece\u4e8b\u7f51\u7edc\u534f\u8bae\u548c\u8bbe\u5907\u9a71\u52a8\u5f00\u53d1\u7684\u5f00\u53d1\u8005\u548c\u7cfb\u7edf\u7ba1\u7406\u5458\u6765\u8bf4\uff0c\u7406\u89e3\u6838\u5fc3\u6570\u636e\u7ed3\u6784\u81f3\u5173\u91cd\u8981\u3002\u5176\u4e2d\uff0csk_buff\uff08socket buffer\uff0c\u5957\u63a5\u5b57\u7f13\u51b2\u533a\uff09\u4f5c\u4e3a\u57fa\u7840\u5143\u7d20\u8131\u9896\u800c\u51fa\uff0c\u5b83\u662f\u6574\u4e2a\u534f\u8bae\u6808\u4e2d\u7f51\u7edc\u6570\u636e\u5305\u7684\u4e3b\u8981\u5bb9\u5668\u3002\u672c\u6587\u6df1\u5165\u63a2\u8ba8\u4e86sk_buff\u7684\u7ed3\u6784\u3001\u5b57\u6bb5\u548c\u64cd\u4f5c\uff0c\u63d0\u4f9b\u4e86\u4e00\u4efd\u5168\u9762\u6307\u5357\uff0c\u89e3\u91ca\u5b83\u5982\u4f55\u4ece\u63a5\u6536\u5230\u4f20\u8f93\u5b9e\u73b0\u9ad8\u6548\u7684\u6570\u636e\u5305\u5904\u7406\u3002<\/p>\n<h2 dir=\"auto\">Linux\u7f51\u7edc\u4e2dsk_buff\u6982\u8ff0<\/h2>\n<p dir=\"auto\">sk_buff\u7ed3\u6784\u662fLinux\u7f51\u7edc\u5b50\u7cfb\u7edf\u7684\u57fa\u77f3\uff0c\u5728\u5185\u6838\u5934\u6587\u4ef6&lt;linux\/skbuff.h&gt;\u4e2d\u5b9a\u4e49\u3002\u5b83\u5c01\u88c5\u4e86\u7f51\u7edc\u6570\u636e\u5305\uff0c\u5305\u62ec\u534f\u8bae\u5934\u3001\u8d1f\u8f7d\u6570\u636e\u4ee5\u53ca\u534f\u8bae\u6808\u5404\u5c42\u6240\u9700\u7684\u5143\u6570\u636e\uff0c\u4f8b\u5982\u7b2c2\u5c42\uff08MAC\uff09\u3001\u7b2c3\u5c42\uff08IP\uff09\u548c\u7b2c4\u5c42\uff08TCP\/UDP\uff09\u3002<\/p>\n<p dir=\"auto\">\u5f53\u6570\u636e\u5305\u7a7f\u8d8a\u534f\u8bae\u6808\u65f6\uff0csk_buff\u5b9e\u4f8b\u5728\u5404\u5c42\u4e4b\u95f4\u4f20\u9012\uff0c\u800c\u65e0\u9700\u8fdb\u884c\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u62f7\u8d1d\u3002\u5bf9\u4e8e\u51fa\u7ad9\u6570\u636e\u5305\uff0c\u901a\u8fc7\u79fb\u52a8\u6307\u9488\u800c\u4e0d\u662f\u91cd\u65b0\u5206\u914d\u5185\u5b58\u6765\u9884\u7f6e\u5934\u90e8\u3002\u5bf9\u4e8e\u5165\u7ad9\u6570\u636e\u5305\uff0c\u901a\u8fc7\u8c03\u6574\u6570\u636e\u6307\u9488\u6765\u8df3\u8fc7\u8f83\u4f4e\u5c42\u7684\u5934\u90e8\uff0c\u4ece\u800c\u63d0\u5347\u6548\u7387\u3002\u8fd9\u79cd\u8bbe\u8ba1\u5728\u9ad8\u541e\u5410\u91cf\u73af\u5883\u4e2d\u80fd\u6700\u5927\u9650\u5ea6\u5730\u51cf\u5c11\u5f00\u9500\u3002<\/p>\n<p dir=\"auto\">sk_buff\u4e2d\u7684\u5b57\u6bb5\u4e3b\u8981\u5206\u4e3a\u5e03\u5c40\u7ba1\u7406\u3001\u901a\u7528\u5c5e\u6027\u3001\u534f\u8bae\u7279\u5b9a\u5143\u7d20\u4ee5\u53ca\u901a\u8fc7\u5185\u6838\u914d\u7f6e\u9009\u9879\u542f\u7528\u7684\u6761\u4ef6\u7279\u6027\u3002\u8fd9\u4e9b\u5b57\u6bb5\u968f\u7740\u5185\u6838\u7684\u8fed\u4ee3\u800c\u6f14\u8fdb\uff0c\u4ee5\u9002\u5e94\u65b0\u7684\u7f51\u7edc\u9700\u6c42\u3002<\/p>\n<h2 dir=\"auto\">\u5e03\u5c40\u5b57\u6bb5\uff1a\u7ba1\u7406sk_buff\u94fe<\/h2>\n<p dir=\"auto\">sk_buff\u5b9e\u4f8b\u901a\u5e38\u7ec4\u7ec7\u6210\u53cc\u5411\u94fe\u8868\uff0c\u7528\u4e8e\u6570\u636e\u5305\u961f\u5217\uff0c\u4f8b\u5982\u5728\u8bbe\u5907\u9a71\u52a8\u6216\u534f\u8bae\u961f\u5217\u4e2d\u3002\u4e3a\u4e86\u4fbf\u4e8e\u4ece\u4efb\u610f\u8282\u70b9\u5feb\u901f\u8bbf\u95ee\u94fe\u8868\u5934\u90e8\uff0c\u5185\u6838\u5728\u524d\u7aef\u5f15\u5165\u4e86\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684sk_buff_head\u7ed3\u6784\uff1a<\/p>\n<ul dir=\"auto\">\n<li><strong>next<\/strong> \u548c <strong>prev<\/strong>\uff1a\u6307\u5411\u540e\u7eed\u548c\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\uff0c\u4e0e\u6807\u51c6\u53cc\u5411\u94fe\u8868\u673a\u5236\u76f8\u540c\u3002<\/li>\n<li><strong>qlen<\/strong>\uff1a\u961f\u5217\u4e2d\u8282\u70b9\u6570\u91cf\u7684\u8ba1\u6570\u5668\u3002<\/li>\n<li><strong>lock<\/strong>\uff1a\u7528\u4e8e\u591a\u6838\u73af\u5883\u4e2d\u7ebf\u7a0b\u5b89\u5168\u7684\u81ea\u65cb\u9501\u3002<\/li>\n<\/ul>\n<p dir=\"auto\">\u6bcf\u4e2ask_buff\u8282\u70b9\u90fd\u5305\u542b\u4e00\u4e2alist\u6307\u9488\uff0c\u76f4\u63a5\u6307\u5411sk_buff_head\uff0c\u4ece\u800c\u5b9e\u73b0O(1)\u65f6\u95f4\u904d\u5386\u5230\u6839\u90e8\u3002\u8fd9\u79cd\u8bbe\u7f6e\u5141\u8bb8\u4e24\u79cd\u7ed3\u6784\u65e0\u7f1d\u96c6\u6210\u5728\u540c\u4e00\u94fe\u8868\u4e2d\uff0c\u5e76\u5171\u4eab\u64cd\u4f5c\u51fd\u6570\u3002<\/p>\n<p dir=\"auto\">\u8fd9\u79cd\u7ec4\u7ec7\u5bf9\u4e8e\u6027\u80fd\u5173\u952e\u8def\u5f84\u81f3\u5173\u91cd\u8981\uff0c\u4f8b\u5982\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u4e2d\uff0c\u9700\u8981\u5feb\u901f\u7684\u5165\u961f\/\u51fa\u961f\u64cd\u4f5c\u3002<\/p>\n<h2 dir=\"auto\">\u6838\u5fc3\u5b57\u6bb5\uff1a\u6570\u636e\u5305\u6570\u636e\u548c\u5143\u6570\u636e<\/h2>\n<p dir=\"auto\">sk_buff\u4e2d\u6709\u51e0\u4e2a\u5b57\u6bb5\u8d1f\u8d23\u5904\u7406\u6570\u636e\u5305\u7684\u6570\u636e\u5e03\u5c40\u548c\u57fa\u672c\u5c5e\u6027\uff1a<\/p>\n<ul dir=\"auto\">\n<li>\n<p dir=\"auto\"><strong>sk<\/strong>\uff1a\u6307\u5411\u5173\u8054\u7684struct sock\u7684\u6307\u9488\uff0c\u5c06\u7f13\u51b2\u533a\u94fe\u63a5\u5230\u5957\u63a5\u5b57\uff0c\u7528\u4e8e\u672c\u5730\u751f\u6210\u6216\u63a5\u6536\u7684\u6570\u636e\u3002\u5bf9\u4e8e\u8f6c\u53d1\u7684\u6570\u636e\u5305\uff08\u6e90\u5730\u5740\u548c\u76ee\u6807\u5730\u5740\u5747\u4e3a\u8fdc\u7a0b\uff09\uff0c\u6b64\u503c\u901a\u5e38\u4e3aNULL\u3002<\/p>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>len<\/strong>\uff1a\u7f13\u51b2\u533a\u4e2d\u6570\u636e\u7684\u603b\u957f\u5ea6\uff0c\u5305\u62ec\u4e3b\u8981\u8d1f\u8f7d\u548c\u4efb\u4f55\u7247\u6bb5\u3002\u968f\u7740\u5934\u90e8\u5728\u6808\u904d\u5386\u4e2d\u88ab\u6dfb\u52a0\u6216\u5265\u79bb\uff0c\u8be5\u503c\u4f1a\u53d1\u751f\u53d8\u5316\u3002<\/p>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>data_len<\/strong>\uff1a\u4ec5\u975e\u8fde\u7eed\u7247\u6bb5\u4e2d\u6570\u636e\u7684\u957f\u5ea6\uff0c\u4e0d\u5305\u62ec\u7ebf\u6027\u90e8\u5206\u3002<\/p>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>mac_len<\/strong>\uff1a\u7b2c2\u5c42\uff08MAC\uff09\u5934\u90e8\u7684\u957f\u5ea6\u3002<\/p>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>users<\/strong>\uff1a\u7f13\u51b2\u533a\u7684\u539f\u5b50\u5f15\u7528\u8ba1\u6570\u5668\uff0c\u8ddf\u8e2a\u6d3b\u8dc3\u7528\u6237\u3002\u901a\u8fc7skb_get()\u9012\u589e\uff0c\u5e76\u901a\u8fc7kfree_skb()\u9012\u51cf\uff0c\u4ee5\u9632\u6b62\u8fc7\u65e9\u91ca\u653e\u3002<\/p>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>truesize<\/strong>\uff1a\u7f13\u51b2\u533a\u7684\u5b8c\u6574\u5185\u5b58\u5360\u7528\uff0c\u5305\u62ecsk_buff\u5f00\u9500\u548c\u5bf9\u9f50\u586b\u5145\u3002\u5728\u5206\u914d\u65f6\u8ba1\u7b97\u4e3a\uff1a<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>text<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div>\n<pre tabindex=\"0\"><code>SKB_TRUESIZE(X) = (X) + SKB_DATA_ALIGN(sizeof(struct sk_buff)) + SKB_DATA_ALIGN(sizeof(struct skb_shared_info))<\/code><\/pre>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<\/li>\n<\/ul>\n<p dir=\"auto\">\u8fb9\u754c\u6307\u9488\u5b9a\u4e49\u4e86\u7f13\u51b2\u533a\u7684\u5185\u5b58\u533a\u57df\uff1a<\/p>\n<ul dir=\"auto\">\n<li><strong>head<\/strong>\uff1a\u5206\u914d\u7f13\u51b2\u533a\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002<\/li>\n<li><strong>end<\/strong>\uff1a\u5206\u914d\u7f13\u51b2\u533a\u7684\u7ed3\u675f\u4f4d\u7f6e\u3002<\/li>\n<li><strong>data<\/strong>\uff1a\u5f53\u524d\u6570\u636e\u7684\u8d77\u59cb\u4f4d\u7f6e\uff08\u521d\u59cb\u65f6\u5728\u9884\u7559\u5934\u90e8\u7a7a\u95f4\u4e4b\u540e\uff09\u3002<\/li>\n<li><strong>tail<\/strong>\uff1a\u5f53\u524d\u6570\u636e\u7684\u7ed3\u675f\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<p dir=\"auto\">\u8fd9\u4e9b\u6307\u9488\u5141\u8bb8\u5404\u5c42\u5728head\u548cdata\u4e4b\u95f4\u63d2\u5165\u5934\u90e8\uff0c\u6216\u5728tail\u548cend\u4e4b\u95f4\u8ffd\u52a0\u8d1f\u8f7d\uff0c\u800c\u65e0\u9700\u91cd\u65b0\u5206\u914d\u3002<\/p>\n<ul dir=\"auto\">\n<li><strong>destructor<\/strong>\uff1a\u7f13\u51b2\u533a\u91ca\u653e\u65f6\u8c03\u7528\u7684\u56de\u8c03\u51fd\u6570\u3002\u5bf9\u4e8e\u7ed1\u5b9a\u5230\u5957\u63a5\u5b57\u7684\u7f13\u51b2\u533a\uff0c\u901a\u5e38\u6307\u5411sock_rfree\u6216sock_wfree\uff0c\u4ee5\u66f4\u65b0\u5957\u63a5\u5b57\u5185\u5b58\u7edf\u8ba1\u3002<\/li>\n<\/ul>\n<h2 dir=\"auto\">\u901a\u7528\u5b57\u6bb5\uff1a\u65f6\u95f4\u6233\u548c\u8bbe\u5907\u4fe1\u606f<\/h2>\n<p dir=\"auto\">\u901a\u7528\u5b57\u6bb5\u63d0\u4f9b\u4e0e\u7279\u5b9a\u7279\u6027\u65e0\u5173\u7684\u4e0a\u4e0b\u6587\uff1a<\/p>\n<ul dir=\"auto\">\n<li><strong>stamp<\/strong>\uff1a\u4e00\u4e2astruct timeval\u65f6\u95f4\u6233\uff0c\u7528\u4e8e\u63a5\u6536\u6216\u8c03\u5ea6\u7684\u6570\u636e\u5305\uff0c\u7531\u8bbe\u5907\u9a71\u52a8\u4e2d\u7684netif_rx()\u8bbe\u7f6e\u3002<\/li>\n<li><strong>dev<\/strong>\uff1a\u6307\u5411\u4f20\u8f93\u7684\u8f93\u51fa\u8bbe\u5907\u6216\u5165\u7ad9\u6570\u636e\u5305\u7684\u63a5\u6536\u63a5\u53e3\u7684\u6307\u9488\u3002<\/li>\n<li><strong>input_dev<\/strong>\uff1a\u63a5\u6536\u6570\u636e\u5305\u7684\u5165\u53e3\u8bbe\u5907\uff1b\u5bf9\u4e8e\u672c\u5730\u751f\u6210\u7684\u6570\u636e\u5305\uff0c\u4e3aNULL\u3002<\/li>\n<li><strong>real_dev<\/strong>\uff1a\u5bf9\u4e8e\u865a\u62df\u63a5\u53e3\uff08\u4f8b\u5982VLAN\u6216\u7ed1\u5b9a\uff09\uff0c\u6307\u5411\u5e95\u5c42\u7269\u7406\u8bbe\u5907\u3002<\/li>\n<\/ul>\n<p dir=\"auto\">\u534f\u8bae\u5934\u8054\u5408\u4f53\u4fbf\u4e8e\u5c42\u7279\u5b9a\u8bbf\u95ee\uff1a<\/p>\n<ul dir=\"auto\">\n<li><strong>h<\/strong>\uff1a\u7b2c4\u5c42\u5934\u90e8\uff08\u4f8b\u5982TCP\/UDP\uff09\u3002<\/li>\n<li><strong>nh<\/strong>\uff1a\u7b2c3\u5c42\u5934\u90e8\uff08\u4f8b\u5982IP\uff09\u3002<\/li>\n<li><strong>mac<\/strong>\uff1a\u7b2c2\u5c42\u5934\u90e8\uff08\u4f8b\u5982Ethernet\uff09\u3002<\/li>\n<\/ul>\n<p dir=\"auto\">\u6bcf\u4e2a\u8054\u5408\u4f53\u5305\u62ec\u4e00\u4e2araw\u6210\u5458\u7528\u4e8e\u901a\u7528\u8bbf\u95ee\uff0c\u4ee5\u53ca\u534f\u8bae\u7279\u5b9a\u7ed3\u6784\u3002\u5728\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u4e00\u5c42\u521d\u59cb\u5316\u5176\u8054\u5408\u4f53\uff08\u4f8b\u5982IP\u8bbe\u7f6enh\uff09\uff0c\u5728skb-&gt;data\u5904\u5904\u7406\u5934\u90e8\uff0c\u7136\u540e\u5c06data\u63a8\u8fdb\u5230\u4e0b\u4e00\u5c42\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002<\/p>\n<ul dir=\"auto\">\n<li>\n<p dir=\"auto\"><strong>dst<\/strong>\uff1a\u6765\u81ea\u76ee\u6807\u7f13\u5b58\u7684\u8def\u7531\u6761\u76ee\uff0c\u7531\u8def\u7531\u5b50\u7cfb\u7edf\u4f7f\u7528\u3002<\/p>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>cb[40]<\/strong>\uff1a\u4e00\u4e2a40\u5b57\u8282\u7684\u63a7\u5236\u7f13\u51b2\u533a\uff0c\u7528\u4e8e\u5c42\u79c1\u6709\u6570\u636e\u3002\u901a\u8fc7\u5b8f\u5982TCP_SKB_CB(__skb)\u8bbf\u95ee\uff0c\u7528\u4e8eTCP\u5e8f\u5217\u53f7\u3001\u6807\u5fd7\u548cACK\u3002\u4f8b\u5982TCP\u4e2d\u7684\u7528\u6cd5\uff1a<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>text<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div>\n<pre tabindex=\"0\"><code>TCP_SKB_CB(skb)-&gt;seq = ntohl(th-&gt;seq);\r\nTCP_SKB_CB(skb)-&gt;end_seq = TCP_SKB_CB(skb)-&gt;seq + th-&gt;syn + th-&gt;fin + skb-&gt;len - (th-&gt;doff * 4);<\/code><\/pre>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>csum<\/strong> \u548c <strong>ip_summed<\/strong>\uff1a\u6821\u9a8c\u548c\u503c\u53ca\u9a8c\u8bc1\u6807\u5fd7\u3002<\/p>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>cloned<\/strong>\uff1a\u6307\u793a\u514b\u9686\u7f13\u51b2\u533a\uff08\u5171\u4eab\u6570\u636e\uff09\u7684\u6807\u5fd7\u3002<\/p>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>pkt_type<\/strong>\uff1a\u6839\u636e\u7b2c2\u5c42\u76ee\u6807\u5730\u5740\u5bf9\u5e27\u8fdb\u884c\u5206\u7c7b\uff08\u4f8b\u5982PACKET_HOST\u7528\u4e8e\u672c\u5730\u5355\u64ad\uff0cPACKET_MULTICAST\u7528\u4e8e\u7ec4\u5730\u5740\uff0cPACKET_LOOPBACK\u7528\u4e8e\u73af\u56de\u6d41\u91cf\uff09\u3002<\/p>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>protocol<\/strong>\uff1a\u66f4\u9ad8\u5c42\u534f\u8bae\uff08\u4f8b\u5982ETH_P_IP\u7528\u4e8eIPv4\uff09\uff0c\u7531\u9a71\u52a8\u5728netif_rx()\u524d\u8bbe\u7f6e\u3002<\/p>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>security<\/strong>\uff1a\u6570\u636e\u5305\u5b89\u5168\u7ea7\u522b\uff0c\u6700\u521d\u4e3aIPsec\u5f15\u5165\u3002<\/p>\n<\/li>\n<\/ul>\n<h2 dir=\"auto\">\u7279\u6027\u7279\u5b9a\u5b57\u6bb5\uff1a\u6761\u4ef6\u5185\u6838\u9009\u9879<\/h2>\n<p dir=\"auto\">\u4e3a\u4e86\u652f\u6301\u5982Netfilter\u6216QoS\u7684\u6a21\u5757\u5316\u7279\u6027\uff0c\u5b57\u6bb5\u4f7f\u7528#ifdef\u6761\u4ef6\u7f16\u8bd1\uff1a<\/p>\n<ul dir=\"auto\">\n<li>Netfilter\uff1anfmark\u3001nfcache\u3001nfctinfo\u3001nfct\u3001nfdebug\u3001nf_bridge\u3002<\/li>\n<li>\u6d41\u91cf\u63a7\u5236\uff1atc_index\u3001tc_verd\u3001tc_classid\u3002<\/li>\n<li>HIPPI\uff1aprivate\u8054\u5408\u4f53\uff0c\u7528\u4e8e\u9ad8\u6027\u80fd\u63a5\u53e3\u3002<\/li>\n<\/ul>\n<p dir=\"auto\">\u8fd9\u4e9b\u5b57\u6bb5\u542f\u7528\u5982\u9632\u706b\u5899\u6570\u636e\u5305\u6807\u8bb0\u6216QoS\u5206\u7c7b\u7684\u529f\u80fd\uff0c\u800c\u4e0d\u81a8\u80c0\u57fa\u672c\u7ed3\u6784\u3002<\/p>\n<h2 dir=\"auto\">\u7ba1\u7406\u51fd\u6570\uff1a\u64cd\u7eb5\u7f13\u51b2\u533a\u5e03\u5c40<\/h2>\n<p dir=\"auto\">\u5185\u6838\u51fd\u6570\u9ad8\u6548\u8c03\u6574\u7f13\u51b2\u533a\u7684\u6570\u636e\u6307\u9488\uff0c\u800c\u907f\u514d\u6570\u636e\u79fb\u52a8\uff1a<\/p>\n<div dir=\"auto\">\n<div>\n<div><\/div>\n<\/div>\n<table dir=\"auto\" data-wide=\"false\">\n<thead>\n<tr>\n<th data-col-size=\"md\">\u51fd\u6570<\/th>\n<th data-col-size=\"md\">\u76ee\u7684<\/th>\n<th data-col-size=\"lg\">\u5bf9\u6307\u9488\u7684\u5f71\u54cd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"md\">skb_put(len)<\/td>\n<td data-col-size=\"md\">\u5728\u5c3e\u90e8\u8ffd\u52a0\u7a7a\u95f4\u7528\u4e8e\u65b0\u6570\u636e\u3002<\/td>\n<td data-col-size=\"lg\">\u5c06tail\u63a8\u8fdblen\uff1b\u8fd4\u56de\u65b0\u5c3e\u90e8\u6307\u9488\u3002<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">skb_push(len)<\/td>\n<td data-col-size=\"md\">\u5728\u5934\u90e8\u9884\u7f6e\u7a7a\u95f4\u7528\u4e8e\u5934\u90e8\u3002<\/td>\n<td data-col-size=\"lg\">\u5c06data\u5411\u540e\u63a8\u8fdblen\uff1b\u8fd4\u56de\u65b0\u6570\u636e\u6307\u9488\u3002<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">skb_pull(len)<\/td>\n<td data-col-size=\"md\">\u4ece\u5934\u90e8\u79fb\u9664\u6570\u636e\uff08\u4f8b\u5982\u5904\u7406\u5934\u90e8\u540e\uff09\u3002<\/td>\n<td data-col-size=\"lg\">\u5c06data\u5411\u524d\u63a8\u8fdblen\uff1b\u51cf\u5c11len\u3002<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">skb_reserve(len)<\/td>\n<td data-col-size=\"md\">\u63d0\u524d\u9884\u7559\u5934\u90e8\u7a7a\u95f4\uff0c\u7528\u4e8e\u672a\u6765\u5934\u90e8\u6216\u5bf9\u9f50\u6570\u636e\u3002<\/td>\n<td data-col-size=\"lg\">\u5c06data\u548ctail\u90fd\u63a8\u8fdblen\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p dir=\"auto\">\u8fd9\u4e9b\u51fd\u6570\u901a\u5e38\u6709__\u53d8\u4f53\u7528\u4e8e\u5e95\u5c42\u4f7f\u7528\uff0c\u7531\u5e26\u6709\u68c0\u67e5\u7684\u5b89\u5168\u7248\u672c\u5305\u88c5\u3002\u4f8b\u5982\uff0c\u4ee5\u592a\u7f51\u9a71\u52a8\u4f7f\u7528skb_reserve(skb, 2)\u572814\u5b57\u8282MAC\u5934\u90e8\u540e\u5c06IP\u5934\u90e8\u5bf9\u9f50\u523016\u5b57\u8282\u8fb9\u754c\u3002<\/p>\n<p dir=\"auto\">\u5728\u4f20\u8f93\u4e2d\uff0cTCP\u5728\u5206\u914d\u65f6\u9884\u7559\u6240\u6709\u6f5c\u5728\u5934\u90e8\u7684\u7a7a\u95f4\uff08\u901a\u8fc7MAX_TCP_HEADER\uff09\uff0c\u7136\u540e\u6bcf\u5c42\u901a\u8fc7\u5411\u4e0b\u79fb\u52a8\u6307\u9488\u9884\u7f6e\u5176\u5934\u90e8\u3002<\/p>\n<h2 dir=\"auto\">\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e<\/h2>\n<h3 dir=\"auto\">\u5206\u914d<\/h3>\n<ul dir=\"auto\">\n<li><strong>alloc_skb(size, gfp_mask)<\/strong>\uff1a\u5728net\/core\/skbuff.c\u4e2d\u7684\u4e3b\u8981\u5206\u914d\u5668\u3002\u4eceslab\u7f13\u5b58\u901a\u8fc7kmem_cache_alloc\u83b7\u53d6sk_buff\uff0c\u5e76\u901a\u8fc7kmalloc\u83b7\u53d6\u6570\u636e\u7f13\u51b2\u533a\uff08\u4f7f\u7528SKB_DATA_ALIGN\u5bf9\u9f50\uff09\u3002\u521d\u59cb\u5316truesize\u3001\u6307\u9488\uff0c\u5e76\u9644\u52a0skb_shared_info\u4ee5\u652f\u6301\u5206\u7247\u3002<\/li>\n<li><strong>dev_alloc_skb(size)<\/strong>\uff1a\u9002\u5408\u9a71\u52a8\u7684\u4e2d\u65ad\u4e0a\u4e0b\u6587\u5305\u88c5\u5668\uff08GFP_ATOMIC\uff09\u3002\u9884\u7559\u989d\u5916\u5934\u90e8\u7a7a\u95f4\uff08\u901a\u5e3816\u5b57\u8282\uff09\u7528\u4e8e\u5bf9\u9f50\u3002<\/li>\n<\/ul>\n<p dir=\"auto\">\u7ed3\u679c\u5305\u62ec\u5bf9\u9f50\u586b\u5145\u548c\u5c3e\u90e8\u7684skb_shared_info\u5757\u3002<\/p>\n<h3 dir=\"auto\">\u91ca\u653e<\/h3>\n<ul dir=\"auto\">\n<li><strong>kfree_skb(skb)<\/strong>\uff1a\u9012\u51cfusers\uff1b\u4ec5\u5f53\u8ba1\u6570\u4e3a\u96f6\u65f6\u91ca\u653e\uff0c\u8fd4\u56de\u5230slab\u7f13\u5b58\u3002dev_kfree_skb\u662f\u5b8f\u522b\u540d\u3002<\/li>\n<li>\u5f15\u7528\u8ba1\u6570\u786e\u4fdd\u5171\u4eab\u7f13\u51b2\u533a\uff08\u4f8b\u5982\u514b\u9686\u7684\uff09\u5728\u6240\u6709\u7528\u6237\u91ca\u653e\u524d\u6301\u4e45\u5b58\u5728\u3002<\/li>\n<\/ul>\n<h2 dir=\"auto\">\u6570\u636e\u5bf9\u9f50\u548cskb_shared_info<\/h2>\n<p dir=\"auto\">skb_reserve\u7528\u7a7a\u95f4\u6362\u65f6\u95f4\uff0c\u901a\u8fc7\u9884\u5206\u914d\u5934\u90e8\u7a7a\u95f4\uff0c\u4ec5\u66f4\u65b0\u6307\u9488\uff1a<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>c<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div>\n<pre tabindex=\"0\"><code>static inline void skb_reserve(struct sk_buff *skb, int len) {\r\n    skb-&gt;data += len;\r\n    skb-&gt;tail += len;\r\n}<\/code><\/pre>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">\u5bf9\u4e8e\u5206\u7247\u6570\u636e\u5305\uff08\u4f8b\u5982\u7531\u4e8eMTU\u7684IP\uff09\uff0cskb_shared_info\uff08\u901a\u8fc7skb_shinfo()\u8bbf\u95ee\uff09\u8ddf\u8e2a\uff1a<\/p>\n<ul dir=\"auto\">\n<li><strong>dataref<\/strong>\uff1a\u6570\u636e\u533a\u57df\u7684\u5f15\u7528\u8ba1\u6570\u3002<\/li>\n<li><strong>nr_frags<\/strong>\u3001<strong>frag_list<\/strong>\u3001<strong>frags[MAX_SKB_FRAGS]<\/strong>\uff1a\u7247\u6bb5\u63cf\u8ff0\u7b26\u3002<\/li>\n<\/ul>\n<p dir=\"auto\">\u4f7f\u7528skb_is_nonlinear()\u68c0\u67e5\u5206\u7247\uff0c\u5e76\u4f7f\u7528skb_linearize()\u5408\u5e76\u6210\u7ebf\u6027\u7f13\u51b2\u533a\u3002<\/p>\n<h2 dir=\"auto\">sk_buff\u7684\u514b\u9686\u548c\u62f7\u8d1d<\/h2>\n<p dir=\"auto\">\u4e3a\u4e86\u5728\u4e0d\u5b8c\u5168\u590d\u5236\u7684\u60c5\u51b5\u4e0b\u5171\u4eab\u7f13\u51b2\u533a\uff0cskb_clone()\u62f7\u8d1dsk_buff\u5934\u90e8\uff08\u8bbe\u7f6ecloned=1\uff0c\u514b\u9686\u7684users=1\uff0c\u9012\u589e\u539f\u59cbdataref\uff09\uff0c\u4f46\u5171\u4eab\u6570\u636e\u6307\u9488\u3002\u9002\u5408\u53ea\u8bfb\u5206\u53d1\uff08\u4f8b\u5982\u5230\u7f51\u7edctap\uff09\u3002<\/p>\n<p dir=\"auto\">\u5bf9\u4e8e\u4fee\u6539\uff1a<\/p>\n<ul dir=\"auto\">\n<li><strong>pskb_copy()<\/strong>\uff1a\u4ec5\u514b\u9686\u7ebf\u6027\u6570\u636e\u533a\u57df\uff08head\u548cend\u4e4b\u95f4\uff09\uff0c\u7247\u6bb5\u4fdd\u6301\u5171\u4eab\u3002<\/li>\n<li><strong>skb_copy()<\/strong>\uff1a\u5b8c\u6574\u6df1\u62f7\u8d1d\uff0c\u5305\u62ec\u7247\u6bb5\uff0c\u7528\u4e8e\u65e0\u9650\u5236\u66f4\u6539\u3002<\/li>\n<\/ul>\n<p dir=\"auto\">\u5f00\u53d1\u4eba\u5458\u5fc5\u987b\u5728\u4fee\u6539\u524d\u62f7\u8d1d\uff0c\u4ee5\u5728\u6a21\u5757\u5316\u5185\u6838\u7ec4\u4ef6\u4e2d\u4fdd\u7559\u4e0d\u53d8\u6027\u3002<\/p>\n<h2 dir=\"auto\">\u961f\u5217\u7ba1\u7406\u51fd\u6570<\/h2>\n<p dir=\"auto\">sk_buff\u961f\u5217\u4f7f\u7528\u81ea\u65cb\u9501\u539f\u5b50\u64cd\u4f5c\uff1a<\/p>\n<ul dir=\"auto\">\n<li>\n<p dir=\"auto\"><strong>skb_queue_head_init(q)<\/strong>\uff1a\u521d\u59cb\u5316\u7a7a\u7684sk_buff_head\u3002<\/p>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>skb_queue_head(q, skb) \/ skb_queue_tail(q, skb)<\/strong>\uff1a\u5728\u5934\u90e8\u6216\u5c3e\u90e8\u5165\u961f\u3002<\/p>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>skb_dequeue(q) \/ skb_dequeue_tail(q)<\/strong>\uff1a\u4ece\u5934\u90e8\u6216\u5c3e\u90e8\u51fa\u961f\u3002<\/p>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>skb_queue_purge(q)<\/strong>\uff1a\u6e05\u7a7a\u961f\u5217\uff0c\u91ca\u653e\u6240\u6709\u8282\u70b9\u3002<\/p>\n<\/li>\n<li>\n<p dir=\"auto\"><strong>skb_queue_walk(q, skb)<\/strong>\uff1a\u901a\u8fc7\u5b8f\u8fed\u4ee3\uff1a<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>c<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div>\n<pre tabindex=\"0\"><code>#define skb_queue_walk(queue, skb) \\\r\n    for (skb = (queue)-&gt;next; skb != (struct sk_buff *)(queue); skb = skb-&gt;next)<\/code><\/pre>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<\/li>\n<\/ul>\n<p dir=\"auto\">\u8fd9\u4e9b\u786e\u4fdd\u5bf9\u4e2d\u65ad\u6216\u5b9a\u65f6\u5668\u7684\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<h2 dir=\"auto\">\u7ed3\u8bba\uff1a\u5229\u7528sk_buff\u5b9e\u73b0\u9ad8\u6548\u7f51\u7edc<\/h2>\n<p dir=\"auto\">sk_buff\u7ed3\u6784\u4f53\u73b0\u4e86Linux\u5185\u6838\u5728\u7f51\u7edc\u4e2d\u7684\u6027\u80fd\u548c\u6a21\u5757\u5316\u91cd\u70b9\u3002\u901a\u8fc7\u638c\u63e1\u5176\u5b57\u6bb5\u3001\u5206\u914d\u7b56\u7565\u548c\u7ba1\u7406\u51fd\u6570\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5728\u81ea\u5b9a\u4e49\u9a71\u52a8\u6216\u6a21\u5757\u4e2d\u4f18\u5316\u6570\u636e\u5305\u5904\u7406\u3002\u65e0\u8bba\u662f\u5904\u7406\u9ad8\u901f\u4ee5\u592a\u7f51\u8fd8\u662f\u5206\u7247IP\u6d41\u91cf\uff0csk_buff\u90fd\u662f\u5185\u6838\u7f51\u7edc\u5b9e\u73b0\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u7ec4\u6210\u90e8\u5206\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Linux\u5185\u6838\u7f51\u7edc\u7684\u590d\u6742\u4e16\u754c\u4e2d&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-208408","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208408"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208408\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}