gpt4 book ai didi

c - Linux内核UDP接收时间戳

转载 作者:IT王子 更新时间:2023-10-29 00:21:06 28 4
gpt4 key购买 nike

我一直在阅读 network timestamping linux 内核的文档,有些东西我不清楚。

SO_TIMESTAMPNS 提供的时间戳在哪里生成?在硬件中还是在内核中?如果是这样,它会在引发新数据包中断后立即生成吗?

SO_TIMESTAMPING 还应该允许生成硬件时间戳。所有网卡都支持吗? SO_TIMESTAMPING 如何使用选项 SOF_TIMESTAMPING_RX_HARDWARE 和 SO_TIMESTAMPNS?在那种情况下,硬件时间戳是指系统时钟还是 NIC 时钟?在第二种情况下如何检索 NIC 时钟以计算耗时?

最佳答案

用于软件时间戳的套接字属性是 SO_TIMESTAMPNS。此套接字属性返回系统时钟的时间。它不是在硬件中生成的,而是在软件中处理中断时系统时间的快照。我们可以通过不属于套接字有效负载的辅助数据 (CMSG) 访问此时间戳,使用:

int level, type;
struct cmsghdr *cm;
struct timespec *ts = NULL;
for (cm = CMSG_FIRSTHDR(&msg); cm != NULL; cm = CMSG_NXTHDR(&msg, cm))
{
level = cm->cmsg_level;
type = cm->cmsg_type;
if (SOL_SOCKET == level && SO_TIMESTAMPNS == type) {
ts = (struct timespec *) CMSG_DATA(cm);
printf("SW TIMESTAMP %ld.%09ld\n", (long)ts[0].tv_sec, (long)ts[0].tv_nsec);
}
}

SO_TIMESTAMPING 套接字选项提供了许多不同的标志,其中一些是,

SOF_TIMESTAMPING_TX_HARDWARE // Transmit timestamp generated in hardware by NIC clock
SOF_TIMESTAMPING_RX_HARDWARE // Receive timestamp generated in hardware by NIC clock
SOF_TIMESTAMPING_TX_SOFTWARE // Transmit timestamp generated in kernel driver by NIC clock
SOF_TIMESTAMPING_RX_SOFTWARE // Receive timestamp generated in kernel driver by NIC clock

并非所有网络接口(interface)卡 (NIC) 都支持此套接字选项。目前许多以太网 NIC 支持 SO_TIMESTAMPING。为了查找特定接口(interface)驱动程序是否支持 SO_TIMESTAMPING,请使用:

ethtool -T ethX // where X corresponds to your particular interface

这将返回 ethX 支持时间戳的所有套接字属性。

要使用特定 NIC 提供的硬件时间戳功能,请使用代码:

int flags;
flags = SOF_TIMESTAMPING_TX_HARDWARE
| SOF_TIMESTAMPING_RX_HARDWARE
| SOF_TIMESTAMPING_TX_SOFTWARE
| SOF_TIMESTAMPING_RX_SOFTWARE
| SOF_TIMESTAMPING_RAW_HARDWARE;
if (setsockopt(sd, SOL_SOCKET, SO_TIMESTAMPING, &flags, sizeof(flags)) < 0)
printf("ERROR: setsockopt SO_TIMESTAMPING\n");

int level, type;
struct cmsghdr *cm;
struct timespec *ts = NULL;
for (cm = CMSG_FIRSTHDR(&msg); cm != NULL; cm = CMSG_NXTHDR(&msg, cm))
{
if (SOL_SOCKET == level && SO_TIMESTAMPING == type) {
ts = (struct timespec *) CMSG_DATA(cm);
printf("HW TIMESTAMP %ld.%09ld\n", (long)ts[2].tv_sec, (long)ts[2].tv_nsec);
}
}

关于c - Linux内核UDP接收时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41805687/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com