blob: a9db7705fe965531d3ad1904958768caf041831a [file] [log] [blame]
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +02001/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
16 */
17
18#define _GNU_SOURCE
19#include <sys/socket.h>
20#include <sys/types.h>
21#include <sys/un.h>
22#include <string.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <net/if.h>
26#include <sys/ioctl.h>
27#include <sys/uio.h>
28#include <sys/mman.h>
29#include <sys/prctl.h>
30#include <fcntl.h>
31#include <errno.h>
32
33#include <socket.h>
34#include <memif.h>
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +020035#include <memif_private.h>
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020036
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020037/* sends msg to socket */
Jakub Grajciar134f1e02021-01-04 11:10:42 +010038static int
39memif_msg_send_from_queue (memif_control_channel_t *cc)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020040{
41 struct msghdr mh = { 0 };
42 struct iovec iov[1];
43 char ctl[CMSG_SPACE (sizeof (int))];
44 int rv, err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciar134f1e02021-01-04 11:10:42 +010045 memif_msg_queue_elt_t *e;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020046
Jakub Grajciar134f1e02021-01-04 11:10:42 +010047 /* Pick the first message */
48 e = TAILQ_FIRST (&cc->msg_queue);
49 if (e == NULL)
50 return MEMIF_ERR_SUCCESS;
51
52 /* Construct the message */
53 iov[0].iov_base = (void *) &e->msg;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020054 iov[0].iov_len = sizeof (memif_msg_t);
55 mh.msg_iov = iov;
56 mh.msg_iovlen = 1;
57
Jakub Grajciar134f1e02021-01-04 11:10:42 +010058 if (e->fd > 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020059 {
60 struct cmsghdr *cmsg;
61 memset (&ctl, 0, sizeof (ctl));
62 mh.msg_control = ctl;
63 mh.msg_controllen = sizeof (ctl);
64 cmsg = CMSG_FIRSTHDR (&mh);
65 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
66 cmsg->cmsg_level = SOL_SOCKET;
67 cmsg->cmsg_type = SCM_RIGHTS;
Jakub Grajciar134f1e02021-01-04 11:10:42 +010068 memcpy (CMSG_DATA (cmsg), &e->fd, sizeof (int));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020069 }
Jakub Grajciar134f1e02021-01-04 11:10:42 +010070 rv = sendmsg (cc->fd, &mh, 0);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020071 if (rv < 0)
72 err = memif_syscall_error_handler (errno);
Jakub Grajciar134f1e02021-01-04 11:10:42 +010073 DBG ("Message type %u sent", e->msg.type);
74
75 /* If sent successfully, remove the msg from queue */
76 if (err == MEMIF_ERR_SUCCESS)
77 {
78 TAILQ_REMOVE (&cc->msg_queue, e, next);
79 cc->sock->args.free (e);
80 }
81
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020082 return err;
83}
84
Jakub Grajciar134f1e02021-01-04 11:10:42 +010085static memif_msg_queue_elt_t *
86memif_msg_enq (memif_control_channel_t *cc)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020087{
Jakub Grajciar134f1e02021-01-04 11:10:42 +010088 memif_msg_queue_elt_t *e;
89
90 e = cc->sock->args.alloc (sizeof (*e));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020091 if (e == NULL)
Jakub Grajciar134f1e02021-01-04 11:10:42 +010092 return NULL;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020093
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020094 e->fd = -1;
Jakub Grajciar134f1e02021-01-04 11:10:42 +010095 TAILQ_INSERT_TAIL (&cc->msg_queue, e, next);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020096
Jakub Grajciar134f1e02021-01-04 11:10:42 +010097 return e;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020098}
99
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100100static int
101memif_msg_enq_hello (memif_control_channel_t *cc)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200102{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100103 memif_msg_hello_t *h;
104 memif_msg_queue_elt_t *e = memif_msg_enq (cc);
105
106 if (e == NULL)
107 return MEMIF_ERR_NOMEM;
108
109 e->msg.type = MEMIF_MSG_TYPE_HELLO;
110
111 h = &e->msg.hello;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200112 h->min_version = MEMIF_VERSION;
113 h->max_version = MEMIF_VERSION;
Damjan Marion20728d42018-07-11 12:24:19 +0200114 h->max_s2m_ring = MEMIF_MAX_S2M_RING;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200115 h->max_m2s_ring = MEMIF_MAX_M2S_RING;
116 h->max_region = MEMIF_MAX_REGION;
117 h->max_log2_ring_size = MEMIF_MAX_LOG2_RING_SIZE;
118
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100119 strlcpy ((char *) h->name, (char *) cc->sock->args.app_name,
120 sizeof (h->name));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200121
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100122 return MEMIF_ERR_SUCCESS;
123}
124
125/* response from memif master - master is ready to handle next message */
126static int
127memif_msg_enq_ack (memif_control_channel_t *cc)
128{
129 memif_msg_queue_elt_t *e = memif_msg_enq (cc);
130
131 if (e == NULL)
132 return MEMIF_ERR_NOMEM;
133
134 e->msg.type = MEMIF_MSG_TYPE_ACK;
135 e->fd = -1;
136
137 return MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200138}
139
140/* send id and secret (optional) for interface identification */
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100141static int
142memif_msg_enq_init (memif_control_channel_t *cc)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200143{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100144 memif_msg_queue_elt_t *e = memif_msg_enq (cc);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200145
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100146 if (e == NULL)
147 return MEMIF_ERR_NOMEM;
148
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200149 memif_msg_init_t *i = &e->msg.init;
150
151 e->msg.type = MEMIF_MSG_TYPE_INIT;
152 e->fd = -1;
153 i->version = MEMIF_VERSION;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100154 i->id = cc->conn->args.interface_id;
155 i->mode = cc->conn->args.mode;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200156
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100157 strlcpy ((char *) i->name, (char *) cc->sock->args.app_name,
158 sizeof (i->name));
159 if (strlen ((char *) cc->conn->args.secret) > 0)
160 strlcpy ((char *) i->secret, (char *) cc->conn->args.secret,
161 sizeof (i->secret));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200162
163 return MEMIF_ERR_SUCCESS; /* 0 */
164}
165
166/* send information about region specified by region_index */
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100167static int
168memif_msg_enq_add_region (memif_control_channel_t *cc, uint8_t region_index)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200169{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100170 memif_region_t *mr = &cc->conn->regions[region_index];
171 memif_msg_queue_elt_t *e = memif_msg_enq (cc);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200172
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200173 if (e == NULL)
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100174 return MEMIF_ERR_NOMEM;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200175
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200176 memif_msg_add_region_t *ar = &e->msg.add_region;
177
178 e->msg.type = MEMIF_MSG_TYPE_ADD_REGION;
179 e->fd = mr->fd;
180 ar->index = region_index;
181 ar->size = mr->region_size;
182
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200183 return MEMIF_ERR_SUCCESS; /* 0 */
184}
185
186/* send information about ring specified by direction (S2M | M2S) and index */
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100187static int
188memif_msg_enq_add_ring (memif_control_channel_t *cc, uint8_t index,
189 uint8_t dir)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200190{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100191 memif_msg_queue_elt_t *e = memif_msg_enq (cc);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200192
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100193 if (e == NULL)
194 return MEMIF_ERR_NOMEM;
195
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200196 memif_msg_add_ring_t *ar = &e->msg.add_ring;
197
198 e->msg.type = MEMIF_MSG_TYPE_ADD_RING;
199
200 /* TODO: support multiple rings */
201 memif_queue_t *mq;
202 if (dir == MEMIF_RING_M2S)
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100203 mq = &cc->conn->rx_queues[index];
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200204 else
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100205 mq = &cc->conn->tx_queues[index];
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200206
207 e->fd = mq->int_fd;
208 ar->index = index;
209 ar->offset = mq->offset;
210 ar->region = mq->region;
211 ar->log2_ring_size = mq->log2_ring_size;
212 ar->flags = (dir == MEMIF_RING_S2M) ? MEMIF_MSG_ADD_RING_FLAG_S2M : 0;
Jakub Grajciarab7c2b02018-03-28 10:21:05 +0200213 ar->private_hdr_size = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200214
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200215 return MEMIF_ERR_SUCCESS; /* 0 */
216}
217
218/* used as connection request from slave */
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100219static int
220memif_msg_enq_connect (memif_control_channel_t *cc)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200221{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100222 memif_msg_queue_elt_t *e = memif_msg_enq (cc);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200223
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100224 if (e == NULL)
225 return MEMIF_ERR_NOMEM;
226
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200227 memif_msg_connect_t *cm = &e->msg.connect;
228
229 e->msg.type = MEMIF_MSG_TYPE_CONNECT;
230 e->fd = -1;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100231 strlcpy ((char *) cm->if_name, (char *) cc->conn->args.interface_name,
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000232 sizeof (cm->if_name));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200233
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200234 return MEMIF_ERR_SUCCESS; /* 0 */
235}
236
237/* used as confirmation of connection by master */
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100238static int
239memif_msg_enq_connected (memif_control_channel_t *cc)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200240{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100241 memif_msg_queue_elt_t *e = memif_msg_enq (cc);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200242
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100243 if (e == NULL)
244 return MEMIF_ERR_NOMEM;
245
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200246 memif_msg_connected_t *cm = &e->msg.connected;
247
248 e->msg.type = MEMIF_MSG_TYPE_CONNECTED;
249 e->fd = -1;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100250 strlcpy ((char *) cm->if_name, (char *) cc->conn->args.interface_name,
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000251 sizeof (cm->if_name));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200252
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200253 return MEMIF_ERR_SUCCESS; /* 0 */
254}
255
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200256int
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100257memif_msg_enq_disconnect (memif_control_channel_t *cc, uint8_t *err_string,
258 uint32_t err_code)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200259{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100260 memif_msg_queue_elt_t *e;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200261
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100262 e = cc->sock->args.alloc (sizeof (*e));
263 if (e == NULL)
264 return MEMIF_ERR_NOMEM;
265
266 e->fd = -1;
267 /* Insert disconenct message at the top of the msg queue */
268 TAILQ_INSERT_HEAD (&cc->msg_queue, e, next);
269
270 memif_msg_disconnect_t *d = &e->msg.disconnect;
271
272 e->msg.type = MEMIF_MSG_TYPE_DISCONNECT;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200273 d->code = err_code;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100274 uint16_t l = sizeof (d->string);
275 if (l > 96)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200276 {
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000277 DBG ("Disconnect string too long. Sending the first %d characters.",
278 sizeof (d->string) - 1);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200279 }
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000280 strlcpy ((char *) d->string, (char *) err_string, sizeof (d->string));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200281
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100282 return MEMIF_ERR_SUCCESS;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200283}
284
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100285static int
286memif_msg_parse_hello (memif_control_channel_t *cc, memif_msg_t *msg)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200287{
288 memif_msg_hello_t *h = &msg->hello;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100289 memif_connection_t *c = cc->conn;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200290
291 if (msg->hello.min_version > MEMIF_VERSION ||
292 msg->hello.max_version < MEMIF_VERSION)
293 {
294 DBG ("incompatible protocol version");
295 return MEMIF_ERR_PROTO;
296 }
297
298 c->run_args.num_s2m_rings = memif_min (h->max_s2m_ring + 1,
299 c->args.num_s2m_rings);
300 c->run_args.num_m2s_rings = memif_min (h->max_m2s_ring + 1,
301 c->args.num_m2s_rings);
302 c->run_args.log2_ring_size = memif_min (h->max_log2_ring_size,
303 c->args.log2_ring_size);
304 c->run_args.buffer_size = c->args.buffer_size;
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000305 strlcpy ((char *) c->remote_name, (char *) h->name, sizeof (c->remote_name));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200306
307 return MEMIF_ERR_SUCCESS; /* 0 */
308}
309
310/* handle interface identification (id, secret (optional)) */
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100311static int
312memif_msg_parse_init (memif_control_channel_t *cc, memif_msg_t *msg)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200313{
314 memif_msg_init_t *i = &msg->init;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200315 memif_connection_t *c = NULL;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200316 uint8_t err_string[96];
317 memset (err_string, 0, sizeof (char) * 96);
318 int err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200319
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100320 /* Check compatible meimf version */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200321 if (i->version != MEMIF_VERSION)
322 {
323 DBG ("MEMIF_VER_ERR");
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100324 memif_msg_enq_disconnect (cc, MEMIF_VER_ERR, 0);
325 return MEMIF_ERR_PROTO;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200326 }
327
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100328 /* Find endpoint on the socket */
329 TAILQ_FOREACH (c, &cc->sock->master_interfaces, next)
330 {
331 /* Match interface id */
332 if (c->args.interface_id != i->id)
333 continue;
334 /* If control channel is present, interface is connected (or connecting) */
335 if (c->control_channel != NULL)
336 {
337 memif_msg_enq_disconnect (cc, "Already connected", 0);
338 return MEMIF_ERR_ALRCONN;
339 }
340 /* Verify secret */
341 if (c->args.secret[0] != '\0')
342 {
343 if (strncmp ((char *) c->args.secret, (char *) i->secret, 24) != 0)
344 {
345 memif_msg_enq_disconnect (cc, "Incorrect secret", 0);
346 return MEMIF_ERR_SECRET;
347 }
348 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200349
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100350 /* Assign the control channel to this interface */
351 c->control_channel = cc;
352 cc->conn = c;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200353
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100354 strlcpy ((char *) c->remote_name, (char *) i->name,
355 sizeof (c->remote_name));
356 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200357
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200358 return err;
359}
360
361/* receive region information and add new region to connection (if possible) */
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100362static int
363memif_msg_parse_add_region (memif_control_channel_t *cc, memif_msg_t *msg,
364 int fd)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200365{
366 memif_msg_add_region_t *ar = &msg->add_region;
367 memif_region_t *mr;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100368 memif_connection_t *c = cc->conn;
369
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200370 if (fd < 0)
371 return MEMIF_ERR_NO_SHMFD;
372
373 if (ar->index > MEMIF_MAX_REGION)
374 return MEMIF_ERR_MAXREG;
375
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100376 mr = (memif_region_t *) cc->sock->args.realloc (
377 c->regions, sizeof (memif_region_t) * (++c->regions_num));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200378 if (mr == NULL)
379 return memif_syscall_error_handler (errno);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200380 memset (mr + ar->index, 0, sizeof (memif_region_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200381 c->regions = mr;
382 c->regions[ar->index].fd = fd;
383 c->regions[ar->index].region_size = ar->size;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200384 c->regions[ar->index].addr = NULL;
Jakub Grajciar412e7b62018-09-27 10:26:35 +0200385 /* region 0 is never external */
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100386 if (cc->sock->get_external_region_addr && (ar->index != 0))
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200387 c->regions[ar->index].is_external = 1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200388
389 return MEMIF_ERR_SUCCESS; /* 0 */
390}
391
392/* receive ring information and add new ring to connection queue
393 (based on direction S2M | M2S) */
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100394static int
395memif_msg_parse_add_ring (memif_control_channel_t *cc, memif_msg_t *msg,
396 int fd)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200397{
398 memif_msg_add_ring_t *ar = &msg->add_ring;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100399 memif_connection_t *c = cc->conn;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200400
401 memif_queue_t *mq;
402
403 if (fd < 0)
404 return MEMIF_ERR_NO_INTFD;
405
Jakub Grajciarab7c2b02018-03-28 10:21:05 +0200406 if (ar->private_hdr_size != 0)
407 return MEMIF_ERR_PRIVHDR;
408
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200409 if (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M)
410 {
411 if (ar->index > MEMIF_MAX_S2M_RING)
412 return MEMIF_ERR_MAXRING;
413 if (ar->index >= c->args.num_s2m_rings)
414 return MEMIF_ERR_MAXRING;
415
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100416 mq = (memif_queue_t *) cc->sock->args.realloc (
417 c->rx_queues, sizeof (memif_queue_t) * (++c->rx_queues_num));
Milan Lencof8b43e52018-06-26 15:16:15 +0200418 memset (mq + ar->index, 0, sizeof (memif_queue_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200419 if (mq == NULL)
420 return memif_syscall_error_handler (errno);
421 c->rx_queues = mq;
422 c->rx_queues[ar->index].int_fd = fd;
423 c->rx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
424 c->rx_queues[ar->index].region = ar->region;
425 c->rx_queues[ar->index].offset = ar->offset;
426 c->run_args.num_s2m_rings++;
427 }
428 else
429 {
430 if (ar->index > MEMIF_MAX_M2S_RING)
431 return MEMIF_ERR_MAXRING;
432 if (ar->index >= c->args.num_m2s_rings)
433 return MEMIF_ERR_MAXRING;
434
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100435 mq = (memif_queue_t *) cc->sock->args.realloc (
436 c->tx_queues, sizeof (memif_queue_t) * (++c->tx_queues_num));
Milan Lencof8b43e52018-06-26 15:16:15 +0200437 memset (mq + ar->index, 0, sizeof (memif_queue_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200438 if (mq == NULL)
439 return memif_syscall_error_handler (errno);
440 c->tx_queues = mq;
441 c->tx_queues[ar->index].int_fd = fd;
442 c->tx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
443 c->tx_queues[ar->index].region = ar->region;
444 c->tx_queues[ar->index].offset = ar->offset;
445 c->run_args.num_m2s_rings++;
446 }
447
448 return MEMIF_ERR_SUCCESS; /* 0 */
449}
450
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100451static int
452memif_configure_rx_interrupt (memif_connection_t *c)
453{
454 memif_socket_t *ms = (memif_socket_t *) c->args.socket;
455 memif_interrupt_t *idata;
456 memif_fd_event_t fde;
457 memif_fd_event_data_t *fdata;
458 void *ctx;
459 int i;
460
461 if (c->on_interrupt != NULL)
462 {
463 for (i = 0; i < c->run_args.num_m2s_rings; i++)
464 {
465 /* Allocate fd event data */
466 fdata = ms->args.alloc (sizeof (*fdata));
467 if (fdata == NULL)
468 {
469 memif_msg_enq_disconnect (c->control_channel, "Internal error",
470 0);
471 return MEMIF_ERR_NOMEM;
472 }
473 /* Allocate interrupt data */
474 idata = ms->args.alloc (sizeof (*fdata));
475 if (idata == NULL)
476 {
477 ms->args.free (fdata);
478 memif_msg_enq_disconnect (c->control_channel, "Internal error",
479 0);
480 return MEMIF_ERR_NOMEM;
481 }
482
483 /* configure interrupt data */
484 idata->c = c;
485 idata->qid = i;
486 /* configure fd event data */
487 fdata->event_handler = memif_interrupt_handler;
488 fdata->private_ctx = idata;
489 fde.fd = c->rx_queues[i].int_fd;
490 fde.type = MEMIF_FD_EVENT_READ;
491 fde.private_ctx = fdata;
492
493 /* Start listening for events */
494 ctx = ms->epfd != -1 ? ms : ms->private_ctx;
495 ms->args.on_control_fd_update (fde, ctx);
496 }
497 }
498
499 return MEMIF_ERR_SUCCESS;
500}
501
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200502/* slave -> master */
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100503static int
504memif_msg_parse_connect (memif_control_channel_t *cc, memif_msg_t *msg)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200505{
506 memif_msg_connect_t *cm = &msg->connect;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100507 memif_connection_t *c = cc->conn;
508 int err;
509
510 err = memif_connect1 (c);
511 if (err != MEMIF_ERR_SUCCESS)
512 return err;
513
514 strlcpy ((char *) c->remote_if_name, (char *) cm->if_name,
515 sizeof (c->remote_if_name));
516
517 err = memif_configure_rx_interrupt (c);
518 if (err != MEMIF_ERR_SUCCESS)
519 return err;
520
521 c->on_connect ((void *) c, c->private_ctx);
522
523 return err;
524}
525
526/* master -> slave */
527static int
528memif_msg_parse_connected (memif_control_channel_t *cc, memif_msg_t *msg)
529{
530 memif_msg_connect_t *cm = &msg->connect;
531 memif_connection_t *c = cc->conn;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200532
533 int err;
534 err = memif_connect1 (c);
535 if (err != MEMIF_ERR_SUCCESS)
536 return err;
537
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000538 strlcpy ((char *) c->remote_if_name, (char *) cm->if_name,
539 sizeof (c->remote_if_name));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200540
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100541 err = memif_configure_rx_interrupt (c);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200542 if (err != MEMIF_ERR_SUCCESS)
543 return err;
544
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200545 c->on_connect ((void *) c, c->private_ctx);
546
547 return err;
548}
549
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100550static int
551memif_msg_parse_disconnect (memif_control_channel_t *cc, memif_msg_t *msg)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200552{
553 memif_msg_disconnect_t *d = &msg->disconnect;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100554 memif_connection_t *c = cc->conn;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200555
556 memset (c->remote_disconnect_string, 0,
557 sizeof (c->remote_disconnect_string));
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100558 strlcpy ((char *) c->remote_disconnect_string, (char *) d->string,
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000559 sizeof (c->remote_disconnect_string));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200560
561 /* on returning error, handle function will call memif_disconnect () */
562 DBG ("disconnect received: %s, mode: %d",
563 c->remote_disconnect_string, c->args.mode);
564 return MEMIF_ERR_DISCONNECT;
565}
566
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100567static int
568memif_msg_receive_and_parse (memif_control_channel_t *cc)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200569{
570 char ctl[CMSG_SPACE (sizeof (int)) +
571 CMSG_SPACE (sizeof (struct ucred))] = { 0 };
572 struct msghdr mh = { 0 };
573 struct iovec iov[1];
574 memif_msg_t msg = { 0 };
575 ssize_t size;
576 int err = MEMIF_ERR_SUCCESS; /* 0 */
577 int fd = -1;
578 int i;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200579 memif_socket_t *ms = NULL;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200580
581 iov[0].iov_base = (void *) &msg;
582 iov[0].iov_len = sizeof (memif_msg_t);
583 mh.msg_iov = iov;
584 mh.msg_iovlen = 1;
585 mh.msg_control = ctl;
586 mh.msg_controllen = sizeof (ctl);
587
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100588 DBG ("recvmsg fd %d", cc->fd);
589 size = recvmsg (cc->fd, &mh, 0);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200590 if (size != sizeof (memif_msg_t))
591 {
592 if (size == 0)
593 return MEMIF_ERR_DISCONNECTED;
594 else
595 return MEMIF_ERR_MFMSG;
596 }
597
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200598 struct cmsghdr *cmsg;
599
600 cmsg = CMSG_FIRSTHDR (&mh);
601 while (cmsg)
602 {
603 if (cmsg->cmsg_level == SOL_SOCKET)
604 {
605 if (cmsg->cmsg_type == SCM_CREDENTIALS)
606 {
msardara8f554b72018-12-11 18:36:55 +0100607 /* Do nothing */ ;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200608 }
609 else if (cmsg->cmsg_type == SCM_RIGHTS)
610 {
611 int *fdp = (int *) CMSG_DATA (cmsg);
612 fd = *fdp;
613 }
614 }
615 cmsg = CMSG_NXTHDR (&mh, cmsg);
616 }
617
618 DBG ("Message type %u received", msg.type);
619
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200620 switch (msg.type)
621 {
622 case MEMIF_MSG_TYPE_ACK:
623 break;
624
625 case MEMIF_MSG_TYPE_HELLO:
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100626 if ((err = memif_msg_parse_hello (cc, &msg)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200627 return err;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100628 if ((err = memif_init_regions_and_queues (cc->conn)) !=
629 MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200630 return err;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100631 if ((err = memif_msg_enq_init (cc)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200632 return err;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100633 for (i = 0; i < cc->conn->regions_num; i++)
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200634 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100635 if ((err = memif_msg_enq_add_region (cc, i)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200636 return err;
637 }
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100638 for (i = 0; i < cc->conn->run_args.num_s2m_rings; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200639 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100640 if ((err = memif_msg_enq_add_ring (cc, i, MEMIF_RING_S2M)) !=
641 MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200642 return err;
643 }
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100644 for (i = 0; i < cc->conn->run_args.num_m2s_rings; i++)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200645 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100646 if ((err = memif_msg_enq_add_ring (cc, i, MEMIF_RING_M2S)) !=
647 MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200648 return err;
649 }
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100650 if ((err = memif_msg_enq_connect (cc)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200651 return err;
652 break;
653
654 case MEMIF_MSG_TYPE_INIT:
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100655 if ((err = memif_msg_parse_init (cc, &msg)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200656 return err;
657 /* c->remote_pid = cr->pid */
658 /* c->remote_uid = cr->uid */
659 /* c->remote_gid = cr->gid */
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100660 if ((err = memif_msg_enq_ack (cc)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200661 return err;
662 break;
663
664 case MEMIF_MSG_TYPE_ADD_REGION:
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100665 if ((err = memif_msg_parse_add_region (cc, &msg, fd)) !=
666 MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200667 return err;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100668 if ((err = memif_msg_enq_ack (cc)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200669 return err;
670 break;
671
672 case MEMIF_MSG_TYPE_ADD_RING:
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100673 if ((err = memif_msg_parse_add_ring (cc, &msg, fd)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200674 return err;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100675 if ((err = memif_msg_enq_ack (cc)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200676 return err;
677 break;
678
679 case MEMIF_MSG_TYPE_CONNECT:
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100680 if ((err = memif_msg_parse_connect (cc, &msg)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200681 return err;
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100682 if ((err = memif_msg_enq_connected (cc)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200683 return err;
684 break;
685
686 case MEMIF_MSG_TYPE_CONNECTED:
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100687 if ((err = memif_msg_parse_connected (cc, &msg)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200688 return err;
689 break;
690
691 case MEMIF_MSG_TYPE_DISCONNECT:
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100692 if ((err = memif_msg_parse_disconnect (cc, &msg)) != MEMIF_ERR_SUCCESS)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200693 return err;
694 break;
695
696 default:
697 return MEMIF_ERR_UNKNOWN_MSG;;
698 break;
699 }
700
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200701 return MEMIF_ERR_SUCCESS; /* 0 */
702}
703
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100704void
705memif_delete_control_channel (memif_control_channel_t *cc)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200706{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100707 memif_msg_queue_elt_t *e, *next;
708 memif_socket_t *ms = cc->sock;
709 memif_fd_event_t fde;
710 void *ctx;
711
712 fde.fd = cc->fd;
713 fde.type = MEMIF_FD_EVENT_DEL;
714 ctx = ms->epfd != -1 ? ms : ms->private_ctx;
715 cc->sock->args.on_control_fd_update (fde, ctx);
716
717 if (cc->fd > 0)
718 close (cc->fd);
719
720 /* Clear control message queue */
721 for (e = TAILQ_FIRST (&cc->msg_queue); e != NULL; e = next)
722 {
723 next = TAILQ_NEXT (e, next);
724 TAILQ_REMOVE (&cc->msg_queue, e, next);
725 cc->sock->args.free (e);
726 }
727
728 /* remove reference */
729 if (cc->conn != NULL)
730 cc->conn->control_channel = NULL;
731 cc->conn = NULL;
732 cc->sock->args.free (cc);
733
734 return;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200735}
736
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200737int
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100738memif_control_channel_handler (memif_fd_event_type_t type, void *private_ctx)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200739{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100740 memif_control_channel_t *cc = (memif_control_channel_t *) private_ctx;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200741 int err;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200742
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100743 /* Receive the message, parse the message and
744 * enqueue next message(s).
745 */
746 err = memif_msg_receive_and_parse (cc);
747 /* Can't assign to endpoint */
748 if (cc->conn == NULL)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200749 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100750 /* A disconnect message is already in the queue */
751 memif_msg_send_from_queue (cc);
752 memif_delete_control_channel (cc);
753
754 return MEMIF_ERR_SUCCESS;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200755 }
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100756 /* error in memif_msg_receive */
757 if (err != MEMIF_ERR_SUCCESS)
758 goto disconnect;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200759
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100760 /* Continue connecting, send next message from the queue */
761 err = memif_msg_send_from_queue (cc);
762 if (err != MEMIF_ERR_SUCCESS)
763 goto disconnect;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200764
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100765 return MEMIF_ERR_SUCCESS;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200766
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100767disconnect:
768 memif_disconnect_internal (cc->conn);
769 return MEMIF_ERR_SUCCESS;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200770}
771
772int
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100773memif_listener_handler (memif_fd_event_type_t type, void *private_ctx)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200774{
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100775 memif_socket_t *ms = (memif_socket_t *) private_ctx;
776 memif_control_channel_t *cc;
777 memif_fd_event_t fde;
778 memif_fd_event_data_t *fdata;
779 struct sockaddr_un un;
780 int err, sockfd, addr_len = sizeof (un);
781 void *ctx;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200782
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100783 if (ms == NULL)
784 return MEMIF_ERR_INVAL_ARG;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200785
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100786 if (type & MEMIF_FD_EVENT_READ)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200787 {
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100788 /* Accept connection to the listener socket */
789 sockfd = accept (ms->listener_fd, (struct sockaddr *) &un,
790 (socklen_t *) &addr_len);
791 if (sockfd < 0)
792 {
793 return memif_syscall_error_handler (errno);
794 }
795
796 /* Create new control channel */
797 cc = ms->args.alloc (sizeof (*cc));
798 if (cc == NULL)
799 {
800 err = MEMIF_ERR_NOMEM;
801 goto error;
802 }
803
804 cc->fd = sockfd;
805 /* The connection will be assigned after parsing MEMIF_MSG_TYPE_INIT msg
806 */
807 cc->conn = NULL;
808 cc->sock = ms;
809 TAILQ_INIT (&cc->msg_queue);
810
811 /* Create memif fd event */
812 fdata = ms->args.alloc (sizeof (*fdata));
813 if (fdata == NULL)
814 {
815 err = MEMIF_ERR_NOMEM;
816 goto error;
817 }
818
819 fdata->event_handler = memif_control_channel_handler;
820 fdata->private_ctx = cc;
821
822 fde.fd = sockfd;
823 fde.type = MEMIF_FD_EVENT_READ;
824 fde.private_ctx = fdata;
825
826 /* Start listenning for events on the new control channel */
827 ctx = ms->epfd != -1 ? ms : ms->private_ctx;
828 ms->args.on_control_fd_update (fde, ctx);
829
830 /* enqueue HELLO msg */
831 err = memif_msg_enq_hello (cc);
832 if (err != MEMIF_ERR_SUCCESS)
833 goto error;
834
835 /* send HELLO msg */
836 err = memif_msg_send_from_queue (cc);
837 if (err != MEMIF_ERR_SUCCESS)
838 goto error;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200839 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200840
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100841 return MEMIF_ERR_SUCCESS;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200842
Jakub Grajciar134f1e02021-01-04 11:10:42 +0100843error:
844 if (sockfd > 0)
845 close (sockfd);
846 if (cc != NULL)
847 ms->args.free (cc);
848 if (fdata != NULL)
849 ms->args.free (fdata);
Jakub Grajciar84b83772019-03-04 12:42:19 +0100850
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200851 return err;
852}