pstream: Support memfd blocks transport

Now that we have the necessary infrastructure to memexport and
mempimport a memfd memblock, extend that support higher up in the
chain with pstreams.

A PA endpoint can now _transparently_ send a memfd memblock to the
other end by simply calling pa_pstream_send_memblock() – provided
the block's memfd pool was earlier registered with the pstream.

If the pipe does not support memfd transfers, we fall back to
sending the block's full data instead of just its reference.

** Further details:

A single pstream connection usually transfers blocks from multiple
pools including the server's srbchannel mempool, the client's
audio data mempool, and the server's global core mempool.

If these mempools are memfd-backed, we now require registering
them with the pstream before sending any blocks they cover. This
is done to minimize fd passing overhead and avoid fd leaks.

Moreover, to support all these pools without hard-coding their
number or nature in the Pulse communication protocol itself, a new
REGISTER_MEMFD_SHMID command is introduced. That command can be
sent _anytime_ during the pstream's lifetime and is used for
creating on demand SHM ID to memfd mappings.

Suggested-by: David Henningsson <david.henningsson@canonical.com>
Signed-off-by: Ahmed S. Darwish <darwish.07@gmail.com>
This commit is contained in:
Ahmed S. Darwish 2016-03-13 01:12:18 +02:00 committed by David Henningsson
parent ee2db62277
commit 27d0a3b388
17 changed files with 455 additions and 76 deletions

View file

@ -69,6 +69,7 @@
void pa_command_extension(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
static void pa_command_enable_srbchannel(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
static void pa_command_disable_srbchannel(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
static void pa_command_register_memfd_shmid(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
[PA_COMMAND_REQUEST] = pa_command_request,
@ -90,6 +91,7 @@ static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
[PA_COMMAND_RECORD_BUFFER_ATTR_CHANGED] = pa_command_stream_buffer_attr,
[PA_COMMAND_ENABLE_SRBCHANNEL] = pa_command_enable_srbchannel,
[PA_COMMAND_DISABLE_SRBCHANNEL] = pa_command_disable_srbchannel,
[PA_COMMAND_REGISTER_MEMFD_SHMID] = pa_command_register_memfd_shmid,
};
static void context_free(pa_context *c);
@ -330,7 +332,7 @@ static void pstream_die_callback(pa_pstream *p, void *userdata) {
pa_context_fail(c, PA_ERR_CONNECTIONTERMINATED);
}
static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_cmsg_ancil_data *ancil_data, void *userdata) {
static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, pa_cmsg_ancil_data *ancil_data, void *userdata) {
pa_context *c = userdata;
pa_assert(p);
@ -1432,8 +1434,7 @@ static void pa_command_enable_srbchannel(pa_pdispatch *pd, uint32_t command, uin
pa_context *c = userdata;
#ifdef HAVE_CREDS
const int *fds;
int nfd;
pa_cmsg_ancil_data *ancil = NULL;
pa_assert(pd);
pa_assert(command == PA_COMMAND_ENABLE_SRBCHANNEL);
@ -1441,26 +1442,34 @@ static void pa_command_enable_srbchannel(pa_pdispatch *pd, uint32_t command, uin
pa_assert(c);
pa_assert(PA_REFCNT_VALUE(c) >= 1);
/* Currently only one srb channel is supported, might change in future versions */
if (c->srb_template.readfd != -1) {
pa_context_fail(c, PA_ERR_PROTOCOL);
return;
}
ancil = pa_pdispatch_take_ancil_data(pd);
if (!ancil)
goto fail;
fds = pa_pdispatch_fds(pd, &nfd);
if (nfd != 2 || !fds || fds[0] == -1 || fds[1] == -1) {
pa_context_fail(c, PA_ERR_PROTOCOL);
return;
}
/* Currently only one srb channel is supported, might change in future versions */
if (c->srb_template.readfd != -1)
goto fail;
if (ancil->nfd != 2 || ancil->fds[0] == -1 || ancil->fds[1] == -1)
goto fail;
pa_context_ref(c);
c->srb_template.readfd = fds[0];
c->srb_template.writefd = fds[1];
c->srb_template.readfd = ancil->fds[0];
c->srb_template.writefd = ancil->fds[1];
c->srb_setup_tag = tag;
pa_context_unref(c);
ancil->close_fds_on_cleanup = false;
return;
fail:
if (ancil)
pa_cmsg_ancil_data_close_fds(ancil);
pa_context_fail(c, PA_ERR_PROTOCOL);
return;
#else
pa_assert(c);
pa_context_fail(c, PA_ERR_PROTOCOL);
@ -1493,6 +1502,18 @@ static void pa_command_disable_srbchannel(pa_pdispatch *pd, uint32_t command, ui
pa_pstream_send_tagstruct(c->pstream, t2);
}
static void pa_command_register_memfd_shmid(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
pa_context *c = userdata;
pa_assert(pd);
pa_assert(command == PA_COMMAND_REGISTER_MEMFD_SHMID);
pa_assert(t);
pa_assert(c);
pa_assert(PA_REFCNT_VALUE(c) >= 1);
if (pa_common_command_register_memfd_shmid(c->pstream, pd, c->version, command, t))
pa_context_fail(c, PA_ERR_PROTOCOL);
}
void pa_command_client_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
pa_context *c = userdata;