首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我收到的包装器比原始数据包大

为什么我收到的包装器比原始数据包大
EN

Stack Overflow用户
提问于 2021-08-22 17:52:53
回答 1查看 156关注 0票数 2

我正在尝试使用原始数据包(仅用于播放)将数据包从一个接口传输到另一个接口。首先,我关注的是接收到的数据包。

在我的机器(archlinux,IP为192.168.30.3 )上,我创建了以下代码:

代码语言:javascript
复制
#include <stdio.h>
#include <net/ethernet.h>       /* the L2 protocols */
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>

int main()
{
    int packet_socket = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IP));


    /* test reception */
    char packet[4096];
    struct sockaddr rcvaddr;
    struct in_addr addr;
    addr.s_addr = inet_addr("192.168.30.3");    //my ip

    // use nc to send a use packet
    while (1) {
        int len = sizeof(rcvaddr);
        int len_packet =
            recvfrom(packet_socket, packet, 4096, 0, &rcvaddr, &len);

        // check if the packet is for us
        struct iphdr *iph =
            (struct iphdr *) (packet + sizeof(struct ethhdr));
        if (iph->daddr != inet_addr("192.168.30.3"))
            continue;

        // check if tcp
        if (iph->protocol != IPPROTO_TCP)
            continue;

        printf("Total packet length: %d\n",
               sizeof(struct ethhdr) + ntohs(iph->tot_len));
    }
}

然后,我以根用户身份运行它,并执行nc -lp 12345 -n > /dev/null

在另一台机器上(debian,192.168.30.4),我运行了dd if=/dev/urandom | nc 192.168.30.3 12345,它让我的前一个程序打印出接收到的数据包的长度。

从它可以看出,有一些数据包的大小超过了MTU的大小(在两台机器上是1500 )。例如,我可以从我的程序中读取“总数据包长度: 16962”。(linux raw ethernet socket receive more bytes than MTU也观察到了)。

我知道IP碎片,所以我首先想到了IP重组。然而,我在man 7 raw中读到:“请注意,与原始套接字不同,数据包套接字不会重组IP片段。”因为我使用了数据包套接字(AF_PACKET),所以我不应该重新组装数据包,然后保持MTU大小不变,对吗?

我还在两台机器上的/proc/sys/net/ipv4/ip_no_pmtu_ sudo ethtool -K ens3 tx off sg off tso off中用值0、1、2和3进行了sudo ethtool -K ens3 tx off sg off tso off和测试。

您认为192.168.30.4发送的MTU会更多吗?或者,我的机器是否执行了一些重新组装,而不管手册中写的是什么?

ethtool -k ens3提供了:

在192.168.30.4上:

代码语言:javascript
复制
seb@SERVER:~$ sudo ethtool -k ens3 
Features for ens3:
rx-checksumming: off
tx-checksumming: off
        tx-checksum-ipv4: off [fixed]
        tx-checksum-ip-generic: off
        tx-checksum-ipv6: off [fixed]
        tx-checksum-fcoe-crc: off [fixed]
        tx-checksum-sctp: off [fixed]
scatter-gather: off
        tx-scatter-gather: off
        tx-scatter-gather-fraglist: off [fixed]
tcp-segmentation-offload: off
        tx-tcp-segmentation: off
        tx-tcp-ecn-segmentation: off [fixed]
        tx-tcp-mangleid-segmentation: off
        tx-tcp6-segmentation: off [fixed]
udp-fragmentation-offload: off
generic-segmentation-offload: off [requested on]
generic-receive-offload: on
large-receive-offload: off [fixed]
rx-vlan-offload: on
tx-vlan-offload: on [fixed]
ntuple-filters: off [fixed]
receive-hashing: off [fixed]
highdma: off [fixed]
rx-vlan-filter: on [fixed]
vlan-challenged: off [fixed]
tx-lockless: off [fixed]
netns-local: off [fixed]
tx-gso-robust: off [fixed]
tx-fcoe-segmentation: off [fixed]
tx-gre-segmentation: off [fixed]
tx-gre-csum-segmentation: off [fixed]
tx-ipxip4-segmentation: off [fixed]
tx-ipxip6-segmentation: off [fixed]
tx-udp_tnl-segmentation: off [fixed]
tx-udp_tnl-csum-segmentation: off [fixed]
tx-gso-partial: off [fixed]
tx-sctp-segmentation: off [fixed]
tx-esp-segmentation: off [fixed]
tx-udp-segmentation: off [fixed]
fcoe-mtu: off [fixed]
tx-nocache-copy: off
loopback: off [fixed]
rx-fcs: off
rx-all: off
tx-vlan-stag-hw-insert: off [fixed]
rx-vlan-stag-hw-parse: off [fixed]
rx-vlan-stag-filter: off [fixed]
l2-fwd-offload: off [fixed]
hw-tc-offload: off [fixed]
esp-hw-offload: off [fixed]
esp-tx-csum-hw-offload: off [fixed]
rx-udp_tunnel-port-offload: off [fixed]
tls-hw-tx-offload: off [fixed]
tls-hw-rx-offload: off [fixed]
rx-gro-hw: off [fixed]
tls-hw-record: off [fixed]

在192.168.30.3上:

代码语言:javascript
复制
[seb@archlinux ~]$ sudo ethtool -k ens3
Features for ens3:
rx-checksumming: off
tx-checksumming: off
        tx-checksum-ipv4: off [fixed]
        tx-checksum-ip-generic: off
        tx-checksum-ipv6: off [fixed]
        tx-checksum-fcoe-crc: off [fixed]
        tx-checksum-sctp: off [fixed]
scatter-gather: off
        tx-scatter-gather: off
        tx-scatter-gather-fraglist: off [fixed]
tcp-segmentation-offload: off
        tx-tcp-segmentation: off
        tx-tcp-ecn-segmentation: off [fixed]
        tx-tcp-mangleid-segmentation: off
        tx-tcp6-segmentation: off [fixed]
generic-segmentation-offload: off [requested on]
generic-receive-offload: on
large-receive-offload: off [fixed]
rx-vlan-offload: on
tx-vlan-offload: on [fixed]
ntuple-filters: off [fixed]
receive-hashing: off [fixed]
highdma: off [fixed]
rx-vlan-filter: on [fixed]
vlan-challenged: off [fixed]
tx-lockless: off [fixed]
netns-local: off [fixed]
tx-gso-robust: off [fixed]
tx-fcoe-segmentation: off [fixed]
tx-gre-segmentation: off [fixed]
tx-gre-csum-segmentation: off [fixed]
tx-ipxip4-segmentation: off [fixed]
tx-ipxip6-segmentation: off [fixed]
tx-udp_tnl-segmentation: off [fixed]
tx-udp_tnl-csum-segmentation: off [fixed]
tx-gso-partial: off [fixed]
tx-tunnel-remcsum-segmentation: off [fixed]
tx-sctp-segmentation: off [fixed]
tx-esp-segmentation: off [fixed]
tx-udp-segmentation: off [fixed]
tx-gso-list: off [fixed]
fcoe-mtu: off [fixed]
tx-nocache-copy: off
loopback: off [fixed]
rx-fcs: off
rx-all: off
tx-vlan-stag-hw-insert: off [fixed]
rx-vlan-stag-hw-parse: off [fixed]
rx-vlan-stag-filter: off [fixed]
l2-fwd-offload: off [fixed]
hw-tc-offload: off [fixed]
esp-hw-offload: off [fixed]
esp-tx-csum-hw-offload: off [fixed]
rx-udp_tunnel-port-offload: off [fixed]
tls-hw-tx-offload: off [fixed]
tls-hw-rx-offload: off [fixed]
rx-gro-hw: off [fixed]
tls-hw-record: off [fixed]
rx-gro-list: off
macsec-hw-offload: off [fixed]
rx-udp-gro-forwarding: off
hsr-tag-ins-offload: off [fixed]
hsr-tag-rm-offload: off [fixed]
hsr-fwd-offload: off [fixed]
hsr-dup-offload: off [fixed]

还要注意,这两台机器是由GNS3运行的qemu机器,具有以下网络选项:-net none -device e1000,mac=0c:7e:08:49:13:00,netdev=gns3-0 -netdev socket,id=gns3-0,udp=127.0.0.1:20049,localaddr=127.0.0.1:20048

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-23 08:52:08

由于观察到的数据包总长度远大于典型巨型帧(MTU 9k)的总长度,因此很明显,接收端使用Large Receive Offload (LRO)或Generic Receive Offload (GRO),从而在网络接口驱动程序级别上将较小的数据包重组为较大的数据包。这可能解释了为什么有问题的数据包套接字看到已经重组(大)的数据包。

在此特定情况下,ethtool -k输出清楚地表明LRO始终处于禁用状态,而GRO实际上处于活动状态,并且可以调整。根据评论中的讨论,禁用GRO确实取得了成果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68883831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档