HW时间戳中没有所需类型的消息

时间:2015-05-25 15:57:12

标签: c sockets linux-kernel timestamp

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

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

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

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

  4. 因此看起来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;
    
        }
    }
    

1 个答案:

答案 0 :(得分: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的。

bundle update coffee-script-source

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

skb2 = skb_peek(&sk->sk_error_queue);

skb2之前来自

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

如果errqueue包含skb(skb cb控制缓冲区应该包含ee_errno中的错误),则无论从哪个队列读取,recvmsg都会报告错误。因为udp_recvmsg调用__skb_recv_datagram 并检查struct sk是否包含错误。

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

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

如果是这样,它将报告-1,其中包含在skb中找到的错误。因此,udp从sk_error_queue读取所有消息至关重要。因为在上次读取时sk-> sk_err将重置(或getsockopt(SO_ERROR))。或者只是跳过它,下次会有一些延迟读取。