Receive from socket into bio without copying
Subsystems (2)
cover Receive from socket into bio without copying
Hello,
I'm (still) trying to pass data from the network to the block layer without copying. The block layer needs blocks to be contiguous in memory, and may have some alignment restrictions as well. A lot of modern network hardware will receive large packets into separate buffers, so individual large packets will end up in contiguous, aligned buffers. I would like to make use of that, but tcp currently doesn't allow me to control what ends up in which packets.
This patch series introduces a new flag for indicating to tcp when it should start a new segment. Using that on the sender side, I can get data over the network with no cpu copying at all.
[My last posting on this topic from May 8 is archived here:
http://www.spinics.net/lists/netdev/msg197788.html ]
Thanks,
Andreas
Andreas Gruenbacher (3):
tcp: Add MSG_NEW_PACKET flag to indicate preferable packet boundaries
tcp: Zero-copy receive from a socket into a bio
fs: Export bio_release_pages()
fs/bio.c | 3 +- include/linux/bio.h | 1 + include/linux/socket.h | 1 + include/net/tcp.h | 3 + net/ipv4/Makefile | 3 +- net/ipv4/tcp.c | 5 +- net/ipv4/tcp_recvbio.c | 168 ++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 180 insertions(+), 4 deletions(-) create mode 100644 net/ipv4/tcp_recvbio.c
Signature · 1 line
1.7.10.2
Quoted 16 lines · Andreas Gruenbacher
Hello,
I'm (still) trying to pass data from the network to the block layer without copying. The block layer needs blocks to be contiguous in memory, and may have some alignment restrictions as well. A lot of modern network hardware will receive large packets into separate buffers, so individual large packets will end up in contiguous, aligned buffers. I would like to make use of that, but tcp currently doesn't allow me to control what ends up in which packets.
This patch series introduces a new flag for indicating to tcp when it should start a new segment. Using that on the sender side, I can get data over the network with no cpu copying at all.
[My last posting on this topic from May 8 is archived here:
http://www.spinics.net/lists/netdev/msg197788.html ]
Quoted 7 lines
Thanks,
AndreasAndreas Gruenbacher (3):
tcp: AddMSG_NEW_PACKETflag to indicate preferable packet boundaries
tcp: Zero-copy receive from a socket into a bio
fs: Exportbio_release_pages()
This looks like yet another zero copy, needing another couple of hundred of lines.
Why splice infrastructure doesnt fit your needs ?
Quoted 2 lines · Eric Dumazet
This looks like yet another zero copy, needing another couple of hundred of lines.
Kind of, yes. We really want to make no copies at all though; the cpu just passes buffers from one device to the other.
Quoted 1 line
Why splice infrastructure doesnt fit your needs ?
The pipe api that splice is based on saves a copy between the kernel and user space, but it currently writes to files, going through the page cache. For that, the alignment of data in the network receive buffers doesn't matter.
We want to go directly to the block layer instead. This requires that the network hardware receives the data into sector aligned buffers. Hence the proposed MSG_NEW_PACKET flag.
With that, it might be possible to implement a pipe "sink" that goes to a bio instead of writing to a file. Going through the pipe infrastructure doesn't actually help in this case though, it's just overhead.
Thanks,
Andreas
Quoted 7 lines · Andreas Gruenbacher
Quoted 2 lines · Eric Dumazet
This looks like yet another zero copy, needing another couple of hundred of lines.
Kind of, yes. We really want to make no copies at all though; the cpu just passes buffers from one device to the other.
Quoted 7 lines
Quoted 1 line
Why splice infrastructure doesnt fit your needs ?
The pipe api that splice is based on saves a copy between the kernel and user space, but it currently writes to files, going through the page cache. For that, the alignment of data in the network receive buffers doesn't matter.
No files or page cache are needed for splice() usage, for example from socket to another socket.
It just works (check haproxy for an example), with 10Gb performance out of the box.
The pipe is only a container for buffers, in case the data fetched from producer cannot be fully sent to consumer. You don't want to lose this data.
Quoted 4 lines
We want to go directly to the block layer instead. This requires that the network hardware receives the data into sector aligned buffers. Hence the proposed
MSG_NEW_PACKETflag.
This only is a hint something is wrong with the approach.
Quoted 4 lines
With that, it might be possible to implement a pipe "sink" that goes to a bio instead of writing to a file. Going through the pipe infrastructure doesn't actually help in this case though, it's just overhead.
There is no expensive overhead in splice() infrastructure, only some small details that should be eventually solved instead of designing a new zero copy mode.
You didnt actually tried splice() if you believe a regular file is needed.
You only need proper splice() support (from pipe to bio), if not already there.
Quoted 5 lines · Eric Dumazet
No files or page cache are needed for
splice()usage, for example from socket to another socket.It just works (check haproxy for an example), with 10Gb performance out of the box.
bio_vec's have some alignment requirements that must be met, and anything that doesn't meet those requirements can't be passed to the block layer (without copying it first). Additional layers between the network and block layers, like a pipe, won't make that problem go away.
Quoted 3 lines
The pipe is only a container for buffers, in case the data fetched from producer cannot be fully sent to consumer. You don't want to lose this data.
Stuff that isn't pulled out of a socket receive buffer will stay there, it won't magically be lost.
Quoted 5 lines
Quoted 3 lines
We want to go directly to the block layer instead. This requires that the network hardware receives the data into sector aligned buffers. Hence the proposed
MSG_NEW_PACKETflag.This only is a hint something is wrong with the approach.
It just means that I'm trying to do something that isn't currently supported.
Quoted 2 lines
You only need proper
splice()support (from pipe to bio), if not already there.
It's not already there, it requires the alignment issue to be addresses first.
Andreas
Quoted 4 lines · Andreas Gruenbacher
We want to go directly to the block layer instead. This requires that the network hardware receives the data into sector aligned buffers. Hence the proposed
MSG_NEW_PACKETflag.
Andreas,
I didn't read your patches, but what are you looking for can't be
achieved using "normal" NICs, for that you need to use RDMA protocol with a hardware that supports RDMA.
saeed
Quoted 5 lines · Andreas Gruenbacher
bio_vec's have some alignment requirements that must be met, and anything that doesn't meet those requirements can't be passed to the block layer (without copying it first). Additional layers between the network and block layers, like a pipe, won't make that problem go away.
What are the "some alignment requirements" exactly, and how do you use TCP exactly to meet them ? (MSS= multiple of 512 ?)
I believe you try to escape from the real problem.
If the NIC driver provides non aligned data, neither splice() or your new stuff will magically align it. You _need_ a copy in either cases.
If NIC driver provides aligned data, splice(socket -> pipe) will keep this alignment for you at 0 cost.
Quoted 2 lines
It's not already there, it requires the alignment issue to be addresses first.
There is no guarantee TCP payload is aligned to a bio, ever, in linux ethernet/ip/tcp stack.
Really, your patches work for you, by pure luck, because you use one particular NIC driver that happens to prepare things for you (presumably doing header split). Nothing guarantee this wont change even for the same hardware in linux-3.8
So I will just say no to your patches, unless you demonstrate the splice() problems, and how you can fix the alignment problem in a new layer instead of in the existing zero copy standard one.
Quoted 11 lines · Eric Dumazet
Quoted 7 lines · Andreas Gruenbacher
bio_vec's have some alignment requirements that must be met, and anything that doesn't meet those requirements can't be passed to the block layer (without copying it first). Additional layers between the
network and block layers, like a pipe, won't make that problem go away.What are the "some alignment requirements" exactly, and how do you use TCP exactly to meet them ? (MSS= multiple of 512 ?)
Sectors of 512 bytes must be contiguous; some devices have additional requirements (like 4k sectors). I'm not sure if sectors always need to be aligned, but if buffers are allocated page wise and handed out as half / full pages, you get that automatically.
Quoted 4 lines
I believe you try to escape from the real problem.
If the NIC driver provides non aligned data, neither
splice()or your new stuff will magically align it. You _need_ a copy in either cases.
Yes, the NIC must provide aligned data. A prerequisite for that is that the NIC knows how to align things. With no knowledge of the application protocol, the NIC can only use the packet boundaries as hints. I'm trying to get tcp to start new packets at specific points in the protocol so that the packet boundaries will coincide with alignment boundaries. With that, NICs that do header splitting can receive packets into appropriately aligned buffers, and the problem is solved.
Quoted 2 lines
If NIC driver provides aligned data,
splice(socket -> pipe)will keep this alignment for you at 0 cost.
Yes of course. That is not the real issue here though.
Quoted 10 lines
Quoted 2 lines
It's not already there, it requires the alignment issue to be addresses first.
There is no guarantee TCP payload is aligned to a bio, ever, in linux ethernet/ip/tcp stack.
Really, your patches work for you, by pure luck, because you use one particular NIC driver that happens to prepare things for you (presumably doing header split). Nothing guarantee this wont change even for the same hardware in linux-3.8
NICs with header splitting are common enough that you don't have to resort to pure luck to get one.
Quoted 3 lines
So I will just say no to your patches, unless you demonstrate the
splice()problems, and how you can fix the alignment problem in a new layer instead of in the existing zero copy standard one.
Again, splice or not is not the issue here. It does not, by itself, allow zero copy from the network directly to disk but it could likely be made to support that if we can get the alignment right first. The proposed MSG_NEW_PACKET flag helps with that, but maybe someone has a better idea.
This doesn't have to work with arbitrary NICs and it most likely will hurt with small MTUs, but then you can still choose not to use it. It just has to almost always work with some particular NICs and with large MTUs.
Andreas
Quoted 10 lines · Andreas Gruenbacher
Quoted 3 lines · Eric Dumazet
So I will just say no to your patches, unless you demonstrate the
splice()problems, and how you can fix the alignment problem in a new layer instead of in the existing zero copy standard one.Again, splice or not is not the issue here. It does not, by itself, allow zero copy from the network directly to disk but it could likely be made to support that if we can get the alignment right first. The proposed
MSG_NEW_PACKETflag helps with that, but maybe someone has a better idea.
Eric - by using splice do you mean something like:
int filedes[2];PIPE_SIZE (64*1024)
pipe(filedes);
ret = splice (sock_fd_from, &from_offset, filedes [1], NULL, PIPE_SIZE,
SPLICE_F_MORE | SPLICE_F_MOVE);
ret = splice (filedes [0], NULL, file_fd_to,
&to_offset, ret,
SPLICE_F_MORE | SPLICE_F_MOVE);i.e. splice-in from socket to pipe, and splice-out from pipe to destination?
Andreas - if the above assumption is true then can you apply the 'MSG_NEW_PACKET' on the sender and see if the above pseudo-splice code achieves something similar to what you expect on the receive side(you can also play w/ F_SETPIPE_SZ - although I found very little reduction in CPU usage)? Note: My personal experience - using splice from an input-file-A to output-file-B bought very minimal cpu reduction(yes, both the files used O_DIRECT). Instead, a simple read/write w/ O_DIRECT from file-A to file-B was much much faster.
Chetan
Quoted 18 lines · chetan loke
Quoted 10 lines · Andreas Gruenbacher
Quoted 3 lines · Eric Dumazet
So I will just say no to your patches, unless you demonstrate the
splice()problems, and how you can fix the alignment problem in a new layer instead of in the existing zero copy standard one.Again, splice or not is not the issue here. It does not, by itself, allow zero copy from the network directly to disk but it could likely be made to support that if we can get the alignment right first. The proposed
MSG_NEW_PACKETflag helps with that, but maybe someone has a better idea.Eric - by using splice do you mean something like:
int filedes[2];
PIPE_SIZE(64*1024)
pipe(filedes);
ret = splice (sock_fd_from,&from_offset, filedes [1], NULL,PIPE_SIZE,
Quoted 7 lines
SPLICE_F_MORE | SPLICE_F_MOVE);ret = splice (filedes [0], NULL,
file_fd_to,&to_offset, ret, SPLICE_F_MORE | SPLICE_F_MOVE);
Yes, thats more or less the plan. You also can play with bigger PIPE_SIZE if needed.
Quoted 11 lines
i.e. splice-in from socket to pipe, and splice-out from pipe to destination?
Andreas - if the above assumption is true then can you apply the '
MSG_NEW_PACKET' on the sender and see if the above pseudo-splice code achieves something similar to what you expect on the receiveside(you can also play w/ F_SETPIPE_SZ - although I found very little reduction in CPU usage)? Note: My personal experience - using splice from an input-file-A to output-file-B bought very minimal cpureduction(yes, both the files used O_DIRECT). Instead, a simple read/write w/O_DIRECTfrom file-A to file-B was much much faster.
splice() performance from socket to pipe have improved a lot in linux-3.5
It was not true zero copy, until very recent patches.
(It was zero copy only on certain class of NIC, not on the ones found on appliances or cheap platforms)
Willy Tarreau mentioned a nice boost of performance with haproxy.
Willy wanted to work on a direct splice from socket to socket, but I am not sure it'll bring major speed improvement.
Hi Eric,
Quoted 22 lines · Eric Dumazet
Quoted 21 lines · chetan loke
Quoted 10 lines · Andreas Gruenbacher
Quoted 3 lines · Eric Dumazet
So I will just say no to your patches, unless you demonstrate the
splice()problems, and how you can fix the alignment problem in a new layer instead of in the existing zero copy standard one.Again, splice or not is not the issue here. It does not, by itself, allow zero copy from the network directly to disk but it could likely be made to support that if we can get the alignment right first. The proposed
MSG_NEW_PACKETflag helps with that, but maybe someone has a better idea.Eric - by using splice do you mean something like:
int filedes[2];
PIPE_SIZE(64*1024)
pipe(filedes);
ret = splice (sock_fd_from,&from_offset, filedes [1], NULL,PIPE_SIZE,SPLICE_F_MORE | SPLICE_F_MOVE);
Quoted 7 lines
Quoted 4 lines
ret = splice (filedes [0], NULL,
file_fd_to,&to_offset, ret, SPLICE_F_MORE | SPLICE_F_MOVE);Yes, thats more or less the plan. You also can play with bigger
PIPE_SIZEif needed.
I confirm, this is recommended at high bit rates if you're working with large windows.
Quoted 11 lines
Quoted 10 lines
i.e. splice-in from socket to pipe, and splice-out from pipe to destination?
Andreas - if the above assumption is true then can you apply the '
MSG_NEW_PACKET' on the sender and see if the above pseudo-splice code achieves something similar to what you expect on the receiveside(you can also play w/ F_SETPIPE_SZ - although I found very little reduction in CPU usage)? Note: My personal experience - using splice from an input-file-A to output-file-B bought very minimal cpureduction(yes, both the files used O_DIRECT). Instead, a simple read/write w/O_DIRECTfrom file-A to file-B was much much faster.
Quoted 4 lines
splice()performance from socket to pipe have improved a lot in linux-3.5It was not true zero copy, until very recent patches.
In fact it has been true zero copy in 2.6.25 until we faced a large amount of data corruption and the zero copy was disabled in 2.6.25.X. Since then it remained that way until you brought your patches to re-instantiate it.
Quoted 4 lines
(It was zero copy only on certain class of NIC, not on the ones found on appliances or cheap platforms)
Willy Tarreau mentioned a nice boost of performance with haproxy.
Yes definitely. The savings are more noticeable on small systems where memory bandwidth is limited. On a small ARM system bound by RAM bandwidth, the performance was basically doubled. But I also observed nice savings on a core2duo equipped with 2 myricom 10Gig NICs forwarding at line rate.
Quoted 2 lines
Willy wanted to work on a direct splice from socket to socket, but I am not sure it'll bring major speed improvement.
I'm not sure at all either, I'm betting a few percent saved from the reduction of syscalls, not much more. This is why I'll probably check this when I have enough time to kill.
Regards,
Willy