{"id":383,"date":"2023-06-18T10:02:53","date_gmt":"2023-06-18T02:02:53","guid":{"rendered":"https:\/\/www.gzlcy.cn\/?p=383"},"modified":"2023-06-18T10:02:53","modified_gmt":"2023-06-18T02:02:53","slug":"linux-%e7%bd%91%e7%bb%9c%e5%8d%8f%e8%ae%ae%e6%a0%88%ef%bc%88%e9%93%be%e8%b7%af%e5%b1%82%ef%bc%89","status":"publish","type":"post","link":"http:\/\/www.gzlcy.cn\/?p=383","title":{"rendered":"linux \u7f51\u7edc\u534f\u8bae\u6808\uff08\u94fe\u8def\u5c42\uff09"},"content":{"rendered":"<p>1.int netif_receive_skb(struct sk_buff *skb)\uff0c\u8be5<a href='https:\/\/www.gzlcy.cn\/?p=199' title='\u51fd\u6570' target='_blank' rel=\"noopener\">\u51fd\u6570<\/a>\u662f\u7f51\u7edc\u8bbe\u5907\u9a71\u52a8\u5230\u94fe\u8def\u5c42<a href='https:\/\/www.gzlcy.cn\/?p=307' title='\u534f\u8bae' target='_blank' rel=\"noopener\">\u534f\u8bae<\/a>\u6808\u7684<a href='https:\/\/www.gzlcy.cn\/?p=321' title='\u63a5\u53e3' target='_blank' rel=\"noopener\">\u63a5\u53e3<\/a>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u6700\u540e\u4f1a\u8c03\u7528__netif_receive_skb_core\u51fd\u6570\uff0c\u4e0b\u9762\u4e3b\u8981\u4ecb\u7ecd\u51fd\u6570\u6d41\u7a0b<\/p>\n<p>1.1<\/p>\n<p><pre class=\"has\"><code class=\"language-cpp\">  list_for_each_entry_rcu(ptype, &amp;ptype_all, list) {  \/\/\u904d\u5386ptype_all\uff0c\u5982\u679c\u6709\u5219\u505a\u76f8\u5e94\u5904\u7406\uff0c\u4f8b\u5982raw socket\u548ctcpdump\u5b9e\u73b0  \n        if (pt_prev)  \n            ret = deliver_skb(skb, pt_prev, orig_dev);  \n        pt_prev = ptype;  \n    } <\/code><\/pre>\n<\/p>\n<p>\u4e3b\u8981\u904d\u5386ptype_all\u94fe\u8868\u7684\u6240\u6709\u6210\u5458\uff0c\u7136\u540e\u6267\u884c\u6210\u5458\u91cc\u7684\u5904\u7406\u51fd\u6570\uff0cPF_PACKETsocket\u548ctcpdump\u7b49\u5b9e\u73b0\u90fd\u5728\u8fd9\u91cc\u4e86<\/p>\n<p>1.2<\/p>\n<p><pre class=\"has\"><code class=\"language-cpp\">rx_handler = rcu_dereference(skb-&gt;dev-&gt;rx_handler);   \n     if (rx_handler) {  \n         if (pt_prev) {  \n             ret = deliver_skb(skb, pt_prev, orig_dev);  \n             pt_prev = NULL;  \n         }  \n\/\/\u6839\u636e\u5904\u7406\u7ed3\u679c\uff0c\u5224\u65ad\u63a5\u4e0b\u6765\u5bf9\u6570\u636e\u5305\u5982\u4f55\u8fdb\u4e00\u6b65\u5904\u7406\u3002  \n         switch (rx_handler(&amp;skb)) {  \n\/\/\u6570\u636e\u5305\u5df2\u6210\u529f\u63a5\u6536\uff0c\u4e0d\u9700\u8981\u518d\u5904\u7406  \n         case RX_HANDLER_CONSUMED:  \n             ret = NET_RX_SUCCESS;  \n             goto unlock;  \n\/\/\u5f53rx_handler\u6539\u53d8\u8fc7skb-&gt;dev\u65f6\uff0c\u5728\u63a5\u6536\u56de\u8def\u4e2d\u518d\u4e00\u6b21\u5904\u7406\u3002  \n         case RX_HANDLER_ANOTHER:  \n             goto another_round;  \n\/\/\u4e0d\u4f7f\u7528\u5339\u914d\u7684\u65b9\u5f0f\uff0c\u7cbe\u786e\u4f20\u9012\u3002  \n         case RX_HANDLER_EXACT:  \n             deliver_exact = true;  \n\/\/\u5ffd\u7565rx_handler\u7684\u5f71\u54cd\u3002  \n         case RX_HANDLER_PASS:  \n             break;  \n        default:  \n             BUG();  \n         }  \n     }  <\/code><\/pre>\n<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u8fd9\u91cc\u9996\u5148\u4ece\u8bbe\u5907\u7ed3\u6784net_device\u4e2d\u83b7\u53d6\u5176rx_handler\u6307\u9488\uff0c\u8be5\u6307\u9488\u5728\u7f51\u5361\u7684\u6df7\u6742\u6a21\u5f0f\u4e0b\u6307\u5411\u4e00\u4e2a\u5904\u7406\u51fd\u6570\u53eb\u505abr_handle_frame\uff0c\u5373\u7f51\u6865\u7684\u5904\u7406\u6d41\u7a0b<\/p>\n<p>1.3<\/p>\n<p><pre class=\"has\"><code class=\"language-cpp\">    if (likely(!deliver_exact)) {  \n        deliver_ptype_list_skb(skb, &amp;pt_prev, orig_dev, type,   \/\/\u6839\u636e\u5168\u5c40\u5b9a\u4e49\u7684\u534f\u8bae\u5904\u7406\u62a5\u6587  \n                       &amp;ptype_base[ntohs(type) &amp;  \n                           PTYPE_HASH_MASK]);  \n    }<\/code><\/pre>\n<\/p>\n<p>\u8fd9\u91cc\u662f\u4ece\u94fe\u8def\u5c42\u8fdb\u5165\u4e0a\u5c42\u7684\u5730\u65b9<\/p>\n<p>\u94fe\u8def\u5c42\u548c\u4e0a\u5c42\u8fde\u63a5\u4e3b\u8981\u901a\u8fc7\uff1aptype_allptype_base\u4e24\u94fe\u8868<\/p>\n<p>\uff081\uff09 ptype_all\u7ba1\u7406\u7684\u534f\u8bae\u4e3b\u8981\u7528\u4e8e\u5206\u6790\u76ee\u7684\uff0c\u5b83\u63a5\u6536\u6240\u6709\u5230\u8fbe\u7b2c3\u5c42\u534f\u8bae\u7684\u6570\u636e\u5305\u3002<\/p>\n<p>\uff082\uff09 ptype_base\u7ba1\u7406\u6b63\u5e38\u76843\u5c42\u534f\u8bae\uff0c\u4ec5\u63a5\u6536\u5177\u6709\u6b63\u786e\u534f\u8bae\u6807\u5fd7\u7b26\u7684\u6570\u636e\u5305\uff0c\u4f8b\u5982\uff0cInternet\u76840x0800\u3002<\/p>\n<p><img src='https:\/\/img-blog.csdn.net\/20180530103104181?watermark\/2\/text\/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Bhb3lpbmdkYXBhbg==\/font\/5a6L5L2T\/fontsize\/400\/fill\/I0JBQkFCMA==\/dissolve\/70' alt='\u7f51\u7edc\u5806\u6808\u662f\u4ec0\u4e48_\u7f51\u7edc\u5806\u6808\u662f\u5426\u8981\u5f00\u542f_bios\u7f51\u7edc\u5806\u6808\u6709\u4ec0\u4e48\u7528' \/><\/p>\n<p>\u8fd9\u4e2a\u76ee\u524d\u652f\u6301\u7684\u534f\u8bae\u7c7b\u578b\uff1a\u5176\u4e2dETH_P_IP, ETH_P_ARP, ETH_P_8021Q, \u662f\u6211\u4eec\u6bd4\u8f83\u5173\u6ce8\u7684<\/p>\n<p><img src='https:\/\/img-blog.csdn.net\/20180530103311274?watermark\/2\/text\/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Bhb3lpbmdkYXBhbg==\/font\/5a6L5L2T\/fontsize\/400\/fill\/I0JBQkFCMA==\/dissolve\/70' alt='bios\u7f51\u7edc\u5806\u6808\u6709\u4ec0\u4e48\u7528_\u7f51\u7edc\u5806\u6808\u662f\u5426\u8981\u5f00\u542f_\u7f51\u7edc\u5806\u6808\u662f\u4ec0\u4e48' \/><\/p>\n<p>2. \u4e0b\u9762\u4ecb\u7ecd\u6865\u5904\u7406\uff1abr_handle_frame<\/p>\n<p>\u5148\u4e0a\u4e00\u5f20\u56fe<\/p>\n<p><img src='https:\/\/img-blog.csdn.net\/20180530100406626?watermark\/2\/text\/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Bhb3lpbmdkYXBhbg==\/font\/5a6L5L2T\/fontsize\/400\/fill\/I0JBQkFCMA==\/dissolve\/70' alt='\u7f51\u7edc\u5806\u6808\u662f\u5426\u8981\u5f00\u542f_bios\u7f51\u7edc\u5806\u6808\u6709\u4ec0\u4e48\u7528_\u7f51\u7edc\u5806\u6808\u662f\u4ec0\u4e48' \/><\/p>\n<p>2.<\/p>\n<p>\u9996\u5148\u6839\u636edev-&gt;br_port\u5224\u65ad\u6536\u5230\u62a5\u6587\u7684\u63a5\u53e3\u662f\u5426\u52a0\u5165\u67d0\u4e2a\u6865\uff0c\u5982\u679c\u52a0\u5165\u5219\u8be5\u62a5\u6587\u8fdb\u5165\u6865\u5904\u7406\u3002<\/p>\n<p>\u6865\u5904\u7406\u9996\u5148\u5224\u65ad\u8be5\u62a5\u6587\u662f\u5426\u662f stp\u534f\u8bae\uff0c\u5982\u679c\u662fstp\u62a5\u6587\uff0c\u5219\u5224\u65ad\u662f\u5426stp\u5f00\u542f\uff0c\u5982\u679c\u5f00\u542f\uff0c\u8fdb\u53bbstp\u5904\u7406\u6d41\u7a0b\uff0c\u5982\u679c\uff0c\u6ca1\u6709\u5219\u4e0d\u5904\u7406\u3002<\/p>\n<p>\u5982\u679c\u62a5\u6587\u4e0d\u662fstp\u534f\u8bae\uff0c\u5219\u5224\u65ad\u76ee\u7684mac\u662f\u5426\u662f\u7f51\u6865\u4e0b\u7684\u67d0\u4e2a\u63a5\u53e3\u7684mac\uff0c\u5982\u679c\u662f\u5219\u628ain_dev\u6539\u6210\u7f51\u6865\u81ea\u5df1\uff0c\u7136\u540e\u91cd\u65b0\u8d70\u4e00\u904d\u534f\u8bae\u6808\uff0c\u7136\u540e\u8fd9\u4e2a\u62a5\u6587\u88ab\u9001\u8fdb\u7f51\u7edc\u5c42\u8fdb\u884c\u8def\u7531\u3002\u5982\u679c\u76ee\u7684mac\u4e0d\u662f\u7f51\u6865\u4e0b\u7684\u63a5\u53e3\u7684mac\uff0c\u76f4\u63a5\u8fdb\u884c\u4e8c\u5c42\u8f6c\u53d1\u3002<\/p>\n<p>\u4e0a\u9762\u8bb2\u5230\u8fdb\u884c\u4e09\u5c42\u8def\u7531\uff0c\u5982\u679c\u8def\u7531\u7684\u76ee\u7684ip\u4efb\u7136\u662f\u4e00\u4e2a\u7f51\u6865\uff0c\u5728\u8c03\u7528dev_queue_xmit\u53d1\u9001\u7684\u65f6\u5019\uff0c\u56e0\u4e3a\u51fa\u63a5\u53e3\u662f\u7f51\u6865\uff0c\u5219\u4f1a\u88abbr_dev_xmit\u63a5\u7ba1\uff0c\u7136\u540e\u8fdb\u884c\u4e8c\u5c42\u8f6c\u53d1\uff0c\u627e\u5230\u5b9e\u9645\u51fa\u63a5\u53e3\uff0c\u518d\u6b21\u8c03\u7528dev_queue_xmit\u53d1\u9001\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.int netif_receive_skb(struct sk_buff *skb)\uff0c\u8be5\u51fd\u6570\u662f\u7f51\u7edc\u8bbe\u5907\u9a71\u52a8\u5230\u94fe\u8def\u5c42\u534f\u8bae\u6808\u7684\u63a5\u53e3\u51fd\u6570<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[761,254,551,632,760],"_links":{"self":[{"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=\/wp\/v2\/posts\/383"}],"collection":[{"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=383"}],"version-history":[{"count":0,"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=\/wp\/v2\/posts\/383\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=383"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}