Subject: Re[2]: calculation in UDP
To: None <tech-net@netbsd.org>
From: Thomas Finneid <tfinneid@ifi.uio.no>
List: tech-net
Date: 12/08/2002 16:31:30
On Sat, 7 Dec 2002 21:29:11 -0800 Jason R Thorpe <thorpej@wasabisystems.com> wrote:
> 
> [ snip ]
> 
>  > On Sat, 7 Dec 2002 14:57:46 +0100 netbsd@gve.ch wrote:
>  > 
>  > > hi,
>  > > it seems the code is in the file sys/netinet/udp_usrreq.c
>  > > in udp_output():
>  > > 
>  > >         /*
>  > >          * Set up checksum and output datagram.
>  > >          */
>  > >                 ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
>  > >                   ui->ui_dst.s_addr, htons((u_int16_t)len +
>  > >                   sizeof(struct udphdr) + IPPROTO_UDP));
>  > >                 m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
>  > >                 m->m_pkthdr.csum_data = offsetof(struct udphdr,
> uh_sum);
> 
> Right, so this is just some setup ... the actual payload checksum is
> performed later, just before the packet is handed to the network
> interface.
> This is so we can offload the payload checksum to hardware if the network
> interface supports it.

Ok, so this is why the ip processing is consuming so many clock cycles. I
have'nt gotten to the details of the ip part just yet. I thought the
checksum might have be done in the socket layer when copying the data from
the user process, but I could'nt find it there either. 

The page loan stuff(zero-copy path ?) in the socket layer, do I understand
it right in that it transfers ownership, temporarily, of the page from the
process to the kernel so as to avoid the data copying at that level? So the
data is only copied when transferred from memory to the NIC, 


> 
> Take a look at ip_output.c:
> 
>                 /*
>                  * Always initialize the sum to 0!  Some HW assisted
>                  * checksumming requires this.
>                  */
>                 ip->ip_sum = 0;
>                 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
> 
>                 sw_csum = m->m_pkthdr.csum_flags &
> ~ifp->if_csum_flags_tx;
> 
>                 /*
>                  * Perform any checksums that the hardware can't do
>                  * for us.
>                  *
>                  * XXX Does any hardware require the {th,uh}_sum
>                  * XXX fields to be 0?
>                  */
>                 if (sw_csum & M_CSUM_IPv4)
>                         ip->ip_sum = in_cksum(m, hlen);
>                 if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
>                         in_delayed_cksum(m);
>                         sw_csum &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
>                 }
>                 m->m_pkthdr.csum_flags &= ifp->if_csum_flags_tx;
> 


-- 
Thomas Finneid

email: tfinneid@ifi.uio.no