【问题标题】:No message of desired type in HW time stamps硬件时间戳中没有所需类型的消息
【发布时间】:2015-05-25 15:57:12
【问题描述】:

我在多平台硬件时间戳应用程序下工作。我对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;

    }
}

【问题讨论】:

    标签: c sockets linux-kernel timestamp


    【解决方案1】:

    很明显,我从带时间戳的环回 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控制缓冲区应该在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))。或者只是跳过,它会在下一次读取时有一些延迟。

    【讨论】:

      猜你喜欢
      • 2017-06-14
      • 1970-01-01
      • 2019-07-28
      • 2021-11-09
      • 1970-01-01
      • 2013-09-19
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      相关资源
      最近更新 更多