All of lore.kernel.org
 help / color / mirror / Atom feed
*  Questions about reading/writing/modifying packet header.
@ 2016-04-18 12:14 최익성
  2016-04-18 15:47 ` Andriy Berestovskyy
  0 siblings, 1 reply; 2+ messages in thread
From: 최익성 @ 2016-04-18 12:14 UTC (permalink / raw)
  To: dev

Dear DPDK exports.
 
I am Ick-Sung Choi living in South Korea.
 
Thank you very much for your contributions.
 
I studied a lot from your source codes.
 
However, actually I have a lot of codes/algorithms that I can't understand.
The codes seems to be incomplete, but it works in my test case.
 
If I take an example, the worker assignment method using & (not %) in load balancing was not fixed yet.
There are a lot of similar codes such as in rte_distributor_process() in distributor.
 
 
 
I have a few questions about reading/writing/modifying packet header. I know it is complex.
I will really appreciate if I can be given answer and some example codes.
 
 
Question #1) I would like to know how can I read/write/modify TCP/UDP/ICMP/IGMP/...  headers from packet in rte_mbuf.
  I will really appreciate if I can be given an example code. I guess it would be somewhat complex.
 
 
Question #2) The IP checksum does not include 6 the ptr. 6 th ptr (ptr16[5]) is missing in the example code. Is it right?
  ( ip_cksum += ptr16[5]; in the following code.)
 
 
 
The following code reads headers and write modified headers (used in DPDK source codes).
 
 
void swap_header_in_a_packet ( struct rte_mbuf *buf )
{
        struct ether_hdr *eth_hdr;
        struct ether_addr eth_src_addr, eth_dest_addr ;
        struct ipv4_hdr *ip_hdr;
        uint32_t ip_src_addr, ip_dest_addr ;
 
        // Read Eternet header.
        eth_hdr = rte_pktmbuf_mtod( buf, struct ether_hdr *);
 
        // Extract MAC addresses.
        ether_addr_copy(&eth_hdr->s_addr, &eth_src_addr );
        ether_addr_copy(&eth_hdr->d_addr, &eth_dest_addr );
 
        // Swap MAC addresses.
        ether_addr_copy(&eth_src_addr, &eth_hdr->d_addr);
        ether_addr_copy(&eth_dest_addr, &eth_hdr->s_addr);
 
        // Swap IP addresses.
        ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod( buf, unsigned char *) + sizeof(struct ether_hdr));
        ip_src_addr = (uint32_t) ip_hdr->src_addr;               // source IP address.
        ip_dest_addr = (uint32_t) ip_hdr->dst_addr;              // destination IP address.
        ip_hdr->src_addr = (uint32_t) ip_dest_addr ;
        ip_hdr->dst_addr = (uint32_t) ip_src_addr ;
        
        setup_simple_example_pkt_ip_headers( (char *) ip_hdr );
 
        copy_buf_to_pkt(&eth_hdr, sizeof(eth_hdr), buf, 0); 
        copy_buf_to_pkt(&ip_hdr, sizeof(ip_hdr), buf, sizeof(struct ether_hdr));
}
 
 
static void setup_simple_example_pkt_ip_headers( char *ip_hdr )
{
        uint16_t *ptr16; uint32_t ip_cksum; // uint16_t pkt_len;
        struct ipv4_hdr *ip_hdr1 = (struct ipv4_hdr *) ip_hdr ;
 
 
        // Initialize UDP header.
/*
        pkt_len = (uint16_t) (pkt_data_len + sizeof(struct udp_hdr));
        udp_hdr->src_port = rte_cpu_to_be_16(UDP_SRC_PORT);
        udp_hdr->dst_port = rte_cpu_to_be_16(UDP_DST_PORT);
        udp_hdr->dgram_len      = RTE_CPU_TO_BE_16(pkt_len);
        udp_hdr->dgram_cksum    = 0; // No UDP checksum. 
*/
 
        // Compute IP header checksum.
        ptr16 = (uint16_t*) ip_hdr1;
        ip_cksum = 0;
        ip_cksum += ptr16[0]; ip_cksum += ptr16[1]; ip_cksum += ptr16[2]; ip_cksum += ptr16[3]; ip_cksum += ptr16[4];
 
        ip_cksum += ptr16[5]; ?   // 6 th ptr (ptr16[5]) is missing in the example code. Is it right?
 
        ip_cksum += ptr16[6]; ip_cksum += ptr16[7];
        ip_cksum += ptr16[8]; ip_cksum += ptr16[9];
 
        // Reduce 32 bit checksum to 16 bits and complement it.
        ip_cksum = ((ip_cksum & 0xFFFF0000) >> 16) + (ip_cksum & 0x0000FFFF);
        if (ip_cksum > 65535) ip_cksum -= 65535;
        ip_cksum = (~ip_cksum) & 0x0000FFFF;
        if (ip_cksum == 0) ip_cksum = 0xFFFF;
        ip_hdr1->hdr_checksum = (uint16_t) ip_cksum;
}
 
                       
Is the Ben really coding machine?  (as in the presentation.) ^^
 
 
Thank you very much.
 
Sincerely Yours,
 
Ick-Sung Choi.


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Questions about reading/writing/modifying packet header.
  2016-04-18 12:14 Questions about reading/writing/modifying packet header 최익성
@ 2016-04-18 15:47 ` Andriy Berestovskyy
  0 siblings, 0 replies; 2+ messages in thread
From: Andriy Berestovskyy @ 2016-04-18 15:47 UTC (permalink / raw)
  To: 최익성; +Cc: dev

Hi Ick-Sung,
Please see inline.

On Mon, Apr 18, 2016 at 2:14 PM, 최익성 <pnk003@naver.com> wrote:
> If I take an example, the worker assignment method using &amp; (not %) in load balancing was not fixed yet.

If the code works, there is nothing to fix, right? ;)


> Question #1) I would like to know how can I read/write/modify TCP/UDP/ICMP/IGMP/...  headers from packet in rte_mbuf.
>   I will really appreciate if I can be given an example code. I guess it would be somewhat complex.

For an example please have a look at parse_ethernet() in test-pmd:
http://dpdk.org/browse/dpdk/tree/app/test-pmd/csumonly.c#n171

The example usage is in the same file:

eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
parse_ethernet(eth_hdr, &info);
l3_hdr = (char *)eth_hdr + info.l2_len;

if (info.l4_proto == IPPROTO_UDP) {
    udp_hdr = (struct udp_hdr *)((char *)l3_hdr + info.l3_len);
    udp_hdr->dst_port = ...
}

Then you might need to recalculate the L4 checksum, so have a look at
rte_ipv4_udptcp_cksum().


> Question #2) The IP checksum does not include 6 the ptr. 6 th ptr (ptr16[5]) is missing in the example code. Is it right?
>   ( ip_cksum += ptr16[5]; in the following code.)

The code seems fine, ptr16[5] is the checksum itself. It should be
zero, so we can skip it.


There is a users@dpdk.org mailing list now, so please use it for your
further questions. Here is the link for your convenience:
http://dpdk.org/ml

Regards,
Andriy

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-04-18 15:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-18 12:14 Questions about reading/writing/modifying packet header 최익성
2016-04-18 15:47 ` Andriy Berestovskyy

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.