TCP套接字的Linux时间戳



我正在一个项目中工作,以从NIC获得TCP套接字的接收和传输时间戳,如文档Linux时间戳中所述。但是所有的文档和测试编码都是为UDP套接字完成的。但我得到了NIC的传输时间戳,而没有得到接收TCP数据包的时间戳。

我的界面支持以下时间戳

    Time stamping parameters for enp4s0:
Capabilities:
    hardware-transmit     (SOF_TIMESTAMPING_TX_HARDWARE)
    software-transmit     (SOF_TIMESTAMPING_TX_SOFTWARE)
    hardware-receive      (SOF_TIMESTAMPING_RX_HARDWARE)
    software-receive      (SOF_TIMESTAMPING_RX_SOFTWARE)
    software-system-clock (SOF_TIMESTAMPING_SOFTWARE)
    hardware-raw-clock    (SOF_TIMESTAMPING_RAW_HARDWARE)
PTP Hardware Clock: 3
Hardware Transmit Timestamp Modes:
    off                   (HWTSTAMP_TX_OFF)
    on                    (HWTSTAMP_TX_ON)
Hardware Receive Filter Modes:
    none                  (HWTSTAMP_FILTER_NONE)
    all                   (HWTSTAMP_FILTER_ALL)

我使用带有的ioctl(sockfd, SIOCSHWTSTAMP, &net_device);启用bind()之后NIC的时间戳

memset(&net_device, 0, sizeof(net_device));
strncpy(net_device.ifr_name, interface_name, sizeof(net_device.ifr_name));
net_device.ifr_data = (void *)&tstconfig;
memset(&tstconfig, 0, sizeof(tstconfig));
tstconfig.tx_type = HWTSTAMP_TX_OFF;
tstconfig.rx_filter = HWTSTAMP_FILTER_ALL;

然后通过setsockopt() 在NIC中启用时间戳

int opt= 0;
opt |= SOF_TIMESTAMPING_RX_HARDWARE;
opt |= SOF_TIMESTAMPING_RAW_HARDWARE;
if (setsockopt(sockfd, SOL_SOCKET, SO_TIMESTAMPING,
           (char *)&opt, sizeof(opt))) {
    error(1, 0, "setsockopt timestamping");
    bail("setsockopt SO_TIMESTAMPING");
}

在listen()和accept()之后,我执行select(),并检查fd是否为rfds,然后使用以下选项调用recvmsg()

int rc;
struct iovec vec[1];
struct msghdr msg;
char data[8192];
struct cmsghdr *cmsg;
union {
    struct cmsghdr cm;
    char control[256];
} cmsg_un;
vec[0].iov_base = data;
vec[0].iov_len = sizeof(data);
memset(&msg, 0, sizeof(msg));
memset(&from_addr, 0, sizeof(from_addr));
memset(&cmsg_un, 0, sizeof(cmsg_un));
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = vec;
msg.msg_iovlen = 1;
msg.msg_control = cmsg_un.control;
msg.msg_controllen = sizeof(cmsg_un.control);   
rc = recvmsg(flow->fd, &msg, 0);
printf("tried reading %d bytes, got %d", bytes, rc);
if (msg.msg_flags & MSG_TRUNC) {
    printf("received truncated messagen");
    return 0;
}
if (msg.msg_flags & MSG_CTRUNC) {
    printf("received truncated ancillary datan");
    return 0;
}
if (msg.msg_controllen <= 0) {
    printf("`received short ancillary data (%ld/%ld)`n",
            (long)msg.msg_controllen, (long)sizeof(cmsg_un.control));
    return 0;
}

但我总是收到以下信息,

received short ancillary data (0/256)

我没有从recvmsg()获得辅助数据,我想知道linux是否支持TCP接收NIC的硬件时间戳。

我能够使用最新的内核版本(4.18)将SO_TIMESTAMPING与TCP套接字一起使用。这对TX和RX时间戳都有效。我仍在做这方面的工作,但如果你愿意,我可以尝试写一个最低限度的概念证明。

Linux时间戳不支持TCP的接收硬件或软件时间戳。linux时间戳的文档只提到了关于"数据包"的内容。这是指UDP,它用于在NIC中实现PTP硬件时钟的同步,请查看PTP守护进程和linux PTP的代码以了解更多信息。Linux 3.18仅支持传输中的时间戳。因此,基本上您无法在接收器中实现TCP的linux时间戳。

相关内容

  • 没有找到相关文章

最新更新