Replace bcopy() to update ether_addr

Mitya mitya at cabletv.dp.ua
Mon Aug 20 14:52:31 UTC 2012


Hi.
I found some overhead code in /src/sys/net/if_ethersubr.c and 
/src/sys/netgraph/ng_ether.c

It contains strings, like bcopy(src, dst, ETHER_ADDR_LEN);
When src and dst are "struct ether_addr*", and ETHER_ADDR_LEN equal 6.
This code call every time, when we send Ethernet packet.
On example, on my machine in invoked nearly 20K per second.

Why we are use bcopy(), to copy only 6 bytes?
Answer - in some architectures we are can not directly copy unaligned data.

I propose this solution.

In file /usr/src/include/net/ethernet.h add this lines:

static inline void ether_addr_copy(ether_addr* src, ether_addr* dst) {
#if defined(__i386__) || defined(__amd64__)
     *dst = *src;
#else
     bcopy(src, dst, ETHER_ADDR_LEN);
#endif
}

On platform i386 gcc produce like this code:
     leal    -30(%ebp), %eax
     leal    6(%eax), %ecx
     leal    -44(%ebp), %edx
     movl    (%edx), %eax
     movl    %eax, (%ecx)
     movzwl  4(%edx), %eax
     movw    %ax, 4(%ecx)
And clang produce this:
     movl    -48(%ebp), %ecx
     movl    %ecx, -26(%ebp)
     movw    -44(%ebp), %si
     movw    %si, -22(%ebp)


All this variants are much faster, than bcopy()



More information about the freebsd-net mailing list