我正在一个项目中工作,以便从文档Linux timestamping中提到的用于TCP套接字的NIC获取接收和传输时间戳。但所有文档和测试编码都是针对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)
我使用bind()
ioctl(sockfd, SIOCSHWTSTAMP, &net_device);
启用了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()
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 message\n");
return 0;
}
if (msg.msg_flags & MSG_CTRUNC) {
printf("received truncated ancillary data\n");
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的硬件时间戳。
答案 0 :(得分:3)
Linux时间戳不支持TCP的接收硬件或软件时间戳。关于linux时间戳的文档仅提到“数据包”方面的内容。这是指UDP,用于在NIC中实现PTP硬件时钟的同步,查看PTP守护进程的代码和linux ptp以获得更多的理解。 Linux 3.18仅支持传输中的时间戳。所以基本上你无法在接收器中实现TCP的linux时间戳。
答案 1 :(得分:1)
我能够使用使用最新内核版本(4.18)的TCP套接字使用SO_TIMESTAMPING。这适用于TX和RX时间戳。我仍在研究中,但是如果您愿意,我可以尝试编写一个最小的概念证明。