gpt4 book ai didi

c - 硬件时间戳中没有所需类型的消息

转载 作者:太空宇宙 更新时间:2023-11-04 03:38:25 26 4
gpt4 key购买 nike

我在多平台硬件时间戳应用程序下工作。我对 linux 时间戳行为有点困惑。我从 recvmsg 收到错误“没有所需类型的消息”,并尝试像错误一样处理它。我的调试代码如下。正如我所看到的预期行为:

  1. 传出数据包环回的发送时间戳附加了发送时间戳的套接字错误队列。

  2. 可以使用带有标志 |= MSG_ERRQUEUE 的 recvmsg 接收已发送数据包的克隆。

  3. recvmsg 返回带 sock_extended_err (ee_errno==ENOMSG) 的传出数据包。 ENOMSG 是“没有所需类型的消息”。

所以看起来 linux 应该在错误队列中保留传出数据包的克隆以进行特征时间计算。我应该在我的错误处理程序代码中跳过 ENOMSG 吗?

if (errno == EAGAIN || errno == EINTR || errno == ENOMSG)
break;

为什么它通过错误消息报告?可能不清楚 ENOMSG 预期或不预期的原因?

我从 recvmsg 得到:错误描述 = '没有所需类型的消息'。

recvmsg(sock, &msg, recvmsg_flags|MSG_ERRQUEUE);

for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
{
...
switch (cmsg->cmsg_level) {
case SOL_IP:
...
pkt.cmsg = (const struct cmsghdr*)cmsg;
pkt.msg = (const struct msghdr*)msg;
print_sol_ip(&pkt);
break;
}
}
/* Network level L3.
* Note that there is no TCP error queue;
* MSG_ERRQUEUE flag can not be used for socket type SOCK_STREAM.
* Thus, any errors can only be obtained as the value returned by the socket,
* or through option SO_ERROR.
*/
static void print_sol_ip(struct cmh_packet *pkt)
{
const int type = pkt->cmsg->cmsg_type;
const struct cmsghdr *cmsg = pkt->cmsg;
if (pkt->cmsg->cmsg_level != SOL_IP) {
printf("Wrong handler.\n");
return;
}
printf("SOL::IP::");
switch (type) {
case IP_RECVERR:
printf("RECVERR::\n");
struct sock_extended_err *err;
struct sockaddr *sk_addr;
struct sockaddr_in *sk_addrin;
socklen_t sk_addrlen;
err = (struct sock_extended_err *)CMSG_DATA(pkt->cmsg);
if ((sk_addr = malloc(sizeof(struct sockaddr))) == NULL) return;

/*
* The original destination address of the datagram that caused the error is supplied via msg_name
* For local errors, no address is passed (this can be checked with the cmsg_len member of the cmsghdr).
*/
printf("pointer to the data: %p\n", pkt->cmsg->__cmsg_data);
printf("data byte count, including header: %zd\n", pkt->cmsg->cmsg_len); /* CMSG_LEN */
printf("originating protocol: %d\n", pkt->cmsg->cmsg_level); /* SOL_SOCKET */
printf("protocol-specific type: %d\n", pkt->cmsg->cmsg_type); /* SCM_RIGHTS */

printf("%s = %d \n", "error number", err->ee_errno);
printf("%s = %d \n", "error origin", err->ee_origin); /* origin: SO_EE_ORIGIN_ICMP..LOCAL..NONE */
printf("%s = %d \n", "error type", err->ee_type); /* type: ICMP_NET_UNREACH..ICMP_HOST_UNREACH */
printf("%s = %d \n", "error code", err->ee_code);
printf("%s = %d \n", "error pad", err->ee_pad);
printf("%s = %d \n", "error info", err->ee_info);
printf("%s = %d \n", "error data", err->ee_data);
printf("error description = '%s'\n", strerror(err->ee_errno));

sk_addr = (struct sockaddr*)pkt->msg->msg_name;
sk_addrlen = pkt->msg->msg_namelen;
sk_addrin = (struct sockaddr_in*)sk_addr;
printf("%s:%d addrlen: %d\n", inet_ntoa(sk_addrin->sin_addr), ntohs(sk_addrin->sin_port), sk_addrlen);

print_af(sk_addr->sa_family);
break;

case IP_PKTINFO:
printf("PKTINFO::\n");
struct in_pktinfo *pki;
pki = (struct in_pktinfo *)CMSG_DATA(pkt->cmsg);
printf("Source interface index %u local address %s destination address %s",
pki->ipi_ifindex,
inet_ntoa(pki->ipi_spec_dst),
inet_ntoa(pki->ipi_addr));
break;
case IP_RECVOPTS: /* Routing header and other options are already installed on the local host. */
printf("IP_RECVOPTS::\n");
break;

case IP_RETOPTS:
printf("IP_RETOPTS::\n");
break;

case IP_TOS: /* the field is used to create network packet priorities.
There are several default values flag TOS: IPTOS_LOWDELAY,
in order to minimize delays for the traffic, IPTOS_THROUGHPUT,
to improve throughput, IPTOS_RELIABILITY, to increase
reliability, IPTOS_MINCOST, should be used for "optional data"
that can be sent at the minimum speed. */
printf("IP_TOS::\n");
break;

case IP_TTL: /* ip_default_ttl */
printf("IP_TTL::\n");
int ttl;
int *pttl;
pttl = (int *) CMSG_DATA(pkt->cmsg);
ttl = *pttl;
printf("ttl value = %d\n", ttl);
break;
case IP_HDRINCL: /* Enabling this flag means that the user has already added the IP header to the top of their data. */
printf("IP_HDRINCL::\n");
break;
case IP_MTU:
printf("IP_MTU::\n");
//TODO: getsockopt(sock, level, IP_MTU, IP_MTU_value_get, size);
break;

case IP_ROUTER_ALERT: /* It transmits this socket all packets that are sent with the option
* of IP Router Alert. This option is used only in the
* type of sockets RAW.
* */
printf("IP_ROUTER_ALERT::\n");
break;
case IP_MULTICAST_TTL:
printf("IP_MULTICAST_TTL::\n");
break;
default:
printf("TYPE %d\n", cmsg->cmsg_type);
break;

}
}

最佳答案

很明显,我从带有回送时间戳的 skb 中获得了 ENOMSG。如 linux/Documentation/networking/timestamping.txt 中所述。最后我发现 ip_recv_error() 从 sk->sk_error_queue (errqueue) 中提取 skb 并重置错误 sk->sk_err = 0。但它也检查 sk_error_queue 中是否存在额外的 skb。我的 errqueue 在 sk_error_queue 中有几个 skb 这就是为什么 ip_recv_error 最后检查 errqueue 发现它有 skb 的原因。

skb2 = skb_peek(&sk->sk_error_queue);

它有 skb2 和 sk->sk_err 被重置回 ENOMSG。

sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;

skb2以前来自

void skb_tstamp_tx(struct sk_buff *orig_skb,
struct skb_shared_hwtstamps *hwtstamps)

serr = SKB_EXT_ERR(skb);
serr->ee.ee_errno = ENOMSG;

如果errqueue包含skb(skb cb control buffer should contain error in ee_errno),无论从哪个队列读取,recvmsg都会报错。因为 udp_recvmsg 调用了 __skb_recv_datagram并检查 struct sk 是否包含错误。

int error = sock_error(sk); 

如果是这样,它会报告 -1 以及在 skb 中发现的错误。所以udp从sk_error_queue中读取所有消息是很关键的。因为在上次读取期间 sk->sk_err 将重置(或 getsockopt(SO_ERROR))。或者直接跳过,下次读取时会延迟一段时间。

关于c - 硬件时间戳中没有所需类型的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30441991/

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