Search code examples
clinuxnetwork-programmingprintfdpdk

Can't print IP-address in DPDK


I'm playing with DPDK example skeleton, but can't print the right IP addresses

Right after rte_eth_rx_burst() I tried the following ways of printing:

    struct rte_ipv4_hdr *ipv4_hdr = rte_pktmbuf_mtod_offset(bufs[0],
                                    struct rte_ipv4_hdr *, sizeof(struct rte_ether_hdr));

    /* 1st way, prints:
      SrcIP: 162.38.192.168
      DstIP: 250.250.0.0
    */
    struct in_addr ip_addr = {0};
    ip_addr.s_addr = ipv4_hdr->src_addr;
    printf("SrcIP: %s\n", inet_ntoa(ip_addr));
    ip_addr.s_addr = ipv4_hdr->dst_addr;
    printf("DstIP: %s\n", inet_ntoa(ip_addr));

    /* 2nd way, prints:
       Source IP: 168.192.38.162
       Dest IP: 0.0.250.250
    */
   printf("Source IP: %u.%u.%u.%u\n", (ipv4_hdr->src_addr >> 24) & 0xff,
              (ipv4_hdr->src_addr >> 16) & 0xff,
              (ipv4_hdr->src_addr >> 8) & 0xff,
              ipv4_hdr->src_addr & 0xff);
   printf("Dest IP: %u.%u.%u.%u\n", (ipv4_hdr->dst_addr >> 24) & 0xff,
            (ipv4_hdr->dst_addr >> 16) & 0xff,
            (ipv4_hdr->dst_addr >> 8) & 0xff,
            ipv4_hdr->dst_addr & 0xff);

But I send ICMP packets from 192.168.250.250 (src IP) to 192.168.250.1 (dst IP). Seems that something wrong with offset, but I can't understand how to fix it? But also seems like I'm using the right way to get a pointer to IP header


Solution

  • The DPDK's skeleton example has no ARP-response support*, and seems that your pinging host couldn't setup L2-connectivity (neighboring). So you just trying to print IP-addresses not from IPv4 header, but from ARP header. And by a happy accident of the location of the protocol fields in headers, some of the IP addresses parst end up in the printf output.

    *- in DPDK it's necessary to take care of the implementation of all the vital protocol logic yourself

    P.S. also for received packets you should classify if it's IPv4, IPv6, ARP or whatever, then do an appropriate protocol handling. Thus, you need to print IPv4 addresses after making sure that a packet with the IPv4 protocol has arrived