Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 29 Oct 2004 13:35:06 +0100
From:      Ollie Cook <ollie@uk.clara.net>
To:        freebsd-net@freebsd.org
Subject:   Efficient copying between sockets
Message-ID:  <20041029123506.GG19662@mutare.noc.clara.net>

next in thread | raw e-mail | index | archive | help
Good afternoon,

I am currently writing a potentially high bandwidth (think fileserver)
application which will proxy data from one PF_INET socket to another (no reason
it has to be PF_INET, but that's how the application stands).

At the moment this is implemented as follows (in pseudo-C; no error-checking
for brevity and clarity):

  written_bytes = 0;
  read_bytes = read(sock_src, buffer, sz_buffer);
  while (written_bytes < read_bytes)
    written_bytes -= write(sock_dst, buffer, read_bytes - written_bytes)

I am wondering if there is a more efficient approach to this which might avoid
copying data into a userspace buffer, and then writing it back to the kernel?

In actual fact, I know in advance exactly how many bytes need to be copied from
one socket to the other, so if there was any way of doing something like:

  socket_redirect(sock_src, sock_dst, bytes_to_copy);

it would be ideal. However I'd be very surprised if such a trivial way to do
that did actually exist.

If anyone has any advice at all on a more efficient way to copy data between
sockets I'd be very glad to hear about it. The software is very much prototype
at the moment, but I'd like to make it as efficient as possible from the
beginning and this seems like a prime area for optimisation.

Yours,

Ollie

-- 
Ollie Cook         Systems Architect, Claranet UK
ollie@uk.clara.net               +44 20 7685 8065



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20041029123506.GG19662>