Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to `splice()` from a socket to a buffer with "zero-copy"?

Tags:

c

linux

sockets

Is it possible to splice() from a socket to a buffer with "zero-copy"? If yes, how?


The example code I found for splice() has two steps (splice example code):

  1. splice from socket to pipe
  2. splice from pipe to file descriptor

Is there always going to be an intermediary step of using a pipe (i.e. you can't do a zero-copy operation into a buffer in user space)?

like image 201
Trevor Boyd Smith Avatar asked Oct 21 '25 03:10

Trevor Boyd Smith


1 Answers

@yeyo has answer this question in the question's comments IMO but did not provide a timely answer. So I will summarize:

You can not splice() from a socket to a buffer with “zero-copy”.


The reason you can't use splice() is because splice() "requires either the source or destination to be a pipe". In other words when the source is a socket then the destination must be a pipe because of the API's restrictions ('either the source or destination or both must be a pipe').


That answers the yes or no question but not the "why".

I still had questions about the "why" or specifically:

  • "Why does splice() require the source or destination to be a pipe?"
  • and
  • "Why can't I splice directly from a socket to user memory?"

Linus Torvalds answers those questions directly in the link and his answer seems clear and concise to me (if not with a little studying of what he writes): http://yarchive.net/comp/linux/splice.html

like image 88
Trevor Boyd Smith Avatar answered Oct 23 '25 18:10

Trevor Boyd Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!