Understanding sendfile () and splice ()

sendfile() can be used to transfer data from a file descriptor to a socket descriptor to get data from machine A to machine B. Is it possible to receive data on the receiving side from a socket to a file with the same zero-copy semantics? I think sendfile() does not help here, because sendfile() needs a data source for the page / buffer cache. Do I understand correctly? Can splice() help in this situation?

+6
source share
1 answer

You are right about limiting sendfile for this. And yes, splice can help, but it’s not trivial: splice requires that at least one of the descriptors of the source or target file be a channel. Thus, you cannot directly splice from a socket into a simple file descriptor.

Conceptually, what you can do to make it work:

  • configure your inbound fd socket and your fd output file as usual.
  • create channel with pipe(2)
  • in the loop:
    • read from the socket to the write side of the handset using splice
    • write on the read side of the channel to the file using splice also

Repeat the last steps until all data has been read.

Zero-Copy on Linux with sendfile () and splice () has an implementation of this technique.

+11
source

Source: https://habr.com/ru/post/904437/


All Articles