blob: b801cac75bae8a2a40bf274a9e715d9d79f27946 [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 */
38static_fn int
39memif_msg_send (int fd, memif_msg_t * msg, int afd)
40{
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 */
45
46 iov[0].iov_base = (void *) msg;
47 iov[0].iov_len = sizeof (memif_msg_t);
48 mh.msg_iov = iov;
49 mh.msg_iovlen = 1;
50
51 if (afd > 0)
52 {
53 struct cmsghdr *cmsg;
54 memset (&ctl, 0, sizeof (ctl));
55 mh.msg_control = ctl;
56 mh.msg_controllen = sizeof (ctl);
57 cmsg = CMSG_FIRSTHDR (&mh);
58 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
59 cmsg->cmsg_level = SOL_SOCKET;
60 cmsg->cmsg_type = SCM_RIGHTS;
61 memcpy (CMSG_DATA (cmsg), &afd, sizeof (int));
62 }
63 rv = sendmsg (fd, &mh, 0);
64 if (rv < 0)
65 err = memif_syscall_error_handler (errno);
66 DBG ("Message type %u sent", msg->type);
67 return err;
68}
69
70/* response from memif master - master is ready to handle next message */
71static_fn int
72memif_msg_enq_ack (memif_connection_t * c)
73{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +020074 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020075 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +020076 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +020077 if (e == NULL)
78 return memif_syscall_error_handler (errno);
79
80 memset (&e->msg, 0, sizeof (e->msg));
81 e->msg.type = MEMIF_MSG_TYPE_ACK;
82 e->fd = -1;
83
84 e->next = NULL;
85 if (c->msg_queue == NULL)
86 {
87 c->msg_queue = e;
88 return MEMIF_ERR_SUCCESS; /* 0 */
89 }
90
91 memif_msg_queue_elt_t *cur = c->msg_queue;
92 while (cur->next != NULL)
93 {
94 cur = cur->next;
95 }
96 cur->next = e;
97
98 return MEMIF_ERR_SUCCESS; /* 0 */
99}
100
101static_fn int
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200102memif_msg_send_hello (libmemif_main_t * lm, int fd)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200103{
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200104 memif_msg_t msg = { 0 };
105 memif_msg_hello_t *h = &msg.hello;
106 msg.type = MEMIF_MSG_TYPE_HELLO;
107 h->min_version = MEMIF_VERSION;
108 h->max_version = MEMIF_VERSION;
Damjan Marion20728d42018-07-11 12:24:19 +0200109 h->max_s2m_ring = MEMIF_MAX_S2M_RING;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200110 h->max_m2s_ring = MEMIF_MAX_M2S_RING;
111 h->max_region = MEMIF_MAX_REGION;
112 h->max_log2_ring_size = MEMIF_MAX_LOG2_RING_SIZE;
113
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000114 strlcpy ((char *) h->name, (char *) lm->app_name, sizeof (h->name));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200115
116 /* msg hello is not enqueued but sent directly,
117 because it is the first msg to be sent */
118 return memif_msg_send (fd, &msg, -1);
119}
120
121/* send id and secret (optional) for interface identification */
122static_fn int
123memif_msg_enq_init (memif_connection_t * c)
124{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200125 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200126 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200127 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200128 if (e == NULL)
129 return memif_syscall_error_handler (errno);
130 memset (e, 0, sizeof (memif_msg_queue_elt_t));
131
132 memset (&e->msg, 0, sizeof (e->msg));
133 memif_msg_init_t *i = &e->msg.init;
134
135 e->msg.type = MEMIF_MSG_TYPE_INIT;
136 e->fd = -1;
137 i->version = MEMIF_VERSION;
138 i->id = c->args.interface_id;
139 i->mode = c->args.mode;
140
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000141 strlcpy ((char *) i->name, (char *) lm->app_name, sizeof (i->name));
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200142 if (strlen ((char *) c->args.secret) > 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200143 strncpy ((char *) i->secret, (char *) c->args.secret, sizeof (i->secret));
144
145 e->next = NULL;
146 if (c->msg_queue == NULL)
147 {
148 c->msg_queue = e;
149 return MEMIF_ERR_SUCCESS; /* 0 */
150 }
151
152 memif_msg_queue_elt_t *cur = c->msg_queue;
153 while (cur->next != NULL)
154 {
155 cur = cur->next;
156 }
157 cur->next = e;
158
159 return MEMIF_ERR_SUCCESS; /* 0 */
160}
161
162/* send information about region specified by region_index */
163static_fn int
164memif_msg_enq_add_region (memif_connection_t * c, uint8_t region_index)
165{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200166 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200167 memif_region_t *mr = &c->regions[region_index];
168
169 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200170 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200171 if (e == NULL)
172 return memif_syscall_error_handler (errno);
173
174 memset (&e->msg, 0, sizeof (e->msg));
175 memif_msg_add_region_t *ar = &e->msg.add_region;
176
177 e->msg.type = MEMIF_MSG_TYPE_ADD_REGION;
178 e->fd = mr->fd;
179 ar->index = region_index;
180 ar->size = mr->region_size;
181
182 e->next = NULL;
183 if (c->msg_queue == NULL)
184 {
185 c->msg_queue = e;
186 return MEMIF_ERR_SUCCESS; /* 0 */
187 }
188
189 memif_msg_queue_elt_t *cur = c->msg_queue;
190 while (cur->next != NULL)
191 {
192 cur = cur->next;
193 }
194 cur->next = e;
195
196 return MEMIF_ERR_SUCCESS; /* 0 */
197}
198
199/* send information about ring specified by direction (S2M | M2S) and index */
200static_fn int
201memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index, uint8_t dir)
202{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200203 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200204 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200205 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200206 if (e == NULL)
207 return memif_syscall_error_handler (errno);
208
209 memset (&e->msg, 0, sizeof (e->msg));
210 memif_msg_add_ring_t *ar = &e->msg.add_ring;
211
212 e->msg.type = MEMIF_MSG_TYPE_ADD_RING;
213
214 /* TODO: support multiple rings */
215 memif_queue_t *mq;
216 if (dir == MEMIF_RING_M2S)
217 mq = &c->rx_queues[index];
218 else
219 mq = &c->tx_queues[index];
220
221 e->fd = mq->int_fd;
222 ar->index = index;
223 ar->offset = mq->offset;
224 ar->region = mq->region;
225 ar->log2_ring_size = mq->log2_ring_size;
226 ar->flags = (dir == MEMIF_RING_S2M) ? MEMIF_MSG_ADD_RING_FLAG_S2M : 0;
Jakub Grajciarab7c2b02018-03-28 10:21:05 +0200227 ar->private_hdr_size = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200228
229 e->next = NULL;
230 if (c->msg_queue == NULL)
231 {
232 c->msg_queue = e;
233 return MEMIF_ERR_SUCCESS; /* 0 */
234 }
235
236 memif_msg_queue_elt_t *cur = c->msg_queue;
237 while (cur->next != NULL)
238 {
239 cur = cur->next;
240 }
241 cur->next = e;
242
243 return MEMIF_ERR_SUCCESS; /* 0 */
244}
245
246/* used as connection request from slave */
247static_fn int
248memif_msg_enq_connect (memif_connection_t * c)
249{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200250 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200251 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200252 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200253 if (e == NULL)
254 return memif_syscall_error_handler (errno);
255
256 memset (&e->msg, 0, sizeof (e->msg));
257 memif_msg_connect_t *cm = &e->msg.connect;
258
259 e->msg.type = MEMIF_MSG_TYPE_CONNECT;
260 e->fd = -1;
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000261 strlcpy ((char *) cm->if_name, (char *) c->args.interface_name,
262 sizeof (cm->if_name));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200263
264 e->next = NULL;
265 if (c->msg_queue == NULL)
266 {
267 c->msg_queue = e;
268 return MEMIF_ERR_SUCCESS; /* 0 */
269 }
270
271 memif_msg_queue_elt_t *cur = c->msg_queue;
272 while (cur->next != NULL)
273 {
274 cur = cur->next;
275 }
276 cur->next = e;
277
278 return MEMIF_ERR_SUCCESS; /* 0 */
279}
280
281/* used as confirmation of connection by master */
282static_fn int
283memif_msg_enq_connected (memif_connection_t * c)
284{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200285 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200286 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200287 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200288 if (e == NULL)
289 return memif_syscall_error_handler (errno);
290
291 memset (&e->msg, 0, sizeof (e->msg));
292 memif_msg_connected_t *cm = &e->msg.connected;
293
294 e->msg.type = MEMIF_MSG_TYPE_CONNECTED;
295 e->fd = -1;
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000296 strlcpy ((char *) cm->if_name, (char *) c->args.interface_name,
297 sizeof (cm->if_name));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200298
299 e->next = NULL;
300 if (c->msg_queue == NULL)
301 {
302 c->msg_queue = e;
303 return MEMIF_ERR_SUCCESS; /* 0 */
304 }
305
306 memif_msg_queue_elt_t *cur = c->msg_queue;
307 while (cur->next != NULL)
308 {
309 cur = cur->next;
310 }
311 cur->next = e;
312
313 return MEMIF_ERR_SUCCESS; /* 0 */
314}
315
316/* immediately send disconnect msg */
Paul Vinciguerraf4fbfd62020-05-15 23:13:36 -0400317 /* specify protocol for disconnect msg err_code
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200318 so that it will be compatible with VPP? (header/doc) */
319int
320memif_msg_send_disconnect (int fd, uint8_t * err_string, uint32_t err_code)
321{
322 memif_msg_t msg = { 0 };
323 memif_msg_disconnect_t *d = &msg.disconnect;
324
325 msg.type = MEMIF_MSG_TYPE_DISCONNECT;
326 d->code = err_code;
327 uint16_t l = strlen ((char *) err_string);
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000328 if (l > sizeof (d->string) - 1)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200329 {
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000330 DBG ("Disconnect string too long. Sending the first %d characters.",
331 sizeof (d->string) - 1);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200332 }
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000333 strlcpy ((char *) d->string, (char *) err_string, sizeof (d->string));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200334
335 return memif_msg_send (fd, &msg, -1);
336}
337
338static_fn int
339memif_msg_receive_hello (memif_connection_t * c, memif_msg_t * msg)
340{
341 memif_msg_hello_t *h = &msg->hello;
342
343 if (msg->hello.min_version > MEMIF_VERSION ||
344 msg->hello.max_version < MEMIF_VERSION)
345 {
346 DBG ("incompatible protocol version");
347 return MEMIF_ERR_PROTO;
348 }
349
350 c->run_args.num_s2m_rings = memif_min (h->max_s2m_ring + 1,
351 c->args.num_s2m_rings);
352 c->run_args.num_m2s_rings = memif_min (h->max_m2s_ring + 1,
353 c->args.num_m2s_rings);
354 c->run_args.log2_ring_size = memif_min (h->max_log2_ring_size,
355 c->args.log2_ring_size);
356 c->run_args.buffer_size = c->args.buffer_size;
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000357 strlcpy ((char *) c->remote_name, (char *) h->name, sizeof (c->remote_name));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200358
359 return MEMIF_ERR_SUCCESS; /* 0 */
360}
361
362/* handle interface identification (id, secret (optional)) */
363static_fn int
364memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
365{
366 memif_msg_init_t *i = &msg->init;
367 memif_list_elt_t *elt = NULL;
368 memif_list_elt_t elt2;
369 memif_connection_t *c = NULL;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200370 libmemif_main_t *lm = get_libmemif_main (ms);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200371 uint8_t err_string[96];
372 memset (err_string, 0, sizeof (char) * 96);
373 int err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200374
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200375 if (i->version != MEMIF_VERSION)
376 {
377 DBG ("MEMIF_VER_ERR");
378 strncpy ((char *) err_string, MEMIF_VER_ERR, strlen (MEMIF_VER_ERR));
379 err = MEMIF_ERR_PROTO;
380 goto error;
381 }
382
383 get_list_elt (&elt, ms->interface_list, ms->interface_list_len, i->id);
384 if (elt == NULL)
385 {
386 DBG ("MEMIF_ID_ERR");
387 strncpy ((char *) err_string, MEMIF_ID_ERR, strlen (MEMIF_ID_ERR));
388 err = MEMIF_ERR_ID;
389 goto error;
390 }
391
392 c = (memif_connection_t *) elt->data_struct;
393
394 if (!(c->args.is_master))
395 {
396 DBG ("MEMIF_SLAVE_ERR");
397 strncpy ((char *) err_string, MEMIF_SLAVE_ERR,
398 strlen (MEMIF_SLAVE_ERR));
399 err = MEMIF_ERR_ACCSLAVE;
400 goto error;
401 }
402 if (c->fd != -1)
403 {
404 DBG ("MEMIF_CONN_ERR");
405 strncpy ((char *) err_string, MEMIF_CONN_ERR, strlen (MEMIF_CONN_ERR));
406 err = MEMIF_ERR_ALRCONN;
407 goto error;
408 }
409
410 c->fd = fd;
411
412 if (i->mode != c->args.mode)
413 {
414 DBG ("MEMIF_MODE_ERR");
415 strncpy ((char *) err_string, MEMIF_MODE_ERR, strlen (MEMIF_MODE_ERR));
416 err = MEMIF_ERR_MODE;
417 goto error;
418 }
419
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000420 strlcpy ((char *) c->remote_name, (char *) i->name, sizeof (c->remote_name));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200421
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200422 if (strlen ((char *) c->args.secret) > 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200423 {
424 int r;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200425 if (strlen ((char *) i->secret) > 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200426 {
427 if (strlen ((char *) c->args.secret) != strlen ((char *) i->secret))
428 {
429 DBG ("MEMIF_SECRET_ERR");
430 strncpy ((char *) err_string,
431 MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
432 err = MEMIF_ERR_SECRET;
433 goto error;
434 }
435 r = strncmp ((char *) i->secret, (char *) c->args.secret,
436 strlen ((char *) c->args.secret));
437 if (r != 0)
438 {
439 DBG ("MEMIF_SECRET_ERR");
440 strncpy ((char *) err_string,
441 MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
442 err = MEMIF_ERR_SECRET;
443 goto error;
444 }
445 }
446 else
447 {
448 DBG ("MEMIF_NOSECRET_ERR");
449 strncpy ((char *) err_string,
450 MEMIF_NOSECRET_ERR, strlen (MEMIF_NOSECRET_ERR));
451 err = MEMIF_ERR_NOSECRET;
452 goto error;
453 }
454 }
455
456 c->read_fn = memif_conn_fd_read_ready;
457 c->write_fn = memif_conn_fd_write_ready;
458 c->error_fn = memif_conn_fd_error;
459
460 elt2.key = c->fd;
461 elt2.data_struct = c;
462
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200463 add_list_elt (lm, &elt2, &lm->control_list, &lm->control_list_len);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200464 free_list_elt (lm->pending_list, lm->pending_list_len, fd);
465
466 return err;
467
468error:
469 memif_msg_send_disconnect (fd, err_string, 0);
Jakub Grajciar6f090fa2019-11-14 10:47:25 +0100470 lm->control_fd_update (fd, MEMIF_FD_EVENT_DEL, lm->private_ctx);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200471 free_list_elt (lm->pending_list, lm->pending_list_len, fd);
472 close (fd);
473 fd = -1;
474 return err;
475}
476
477/* receive region information and add new region to connection (if possible) */
478static_fn int
479memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
480 int fd)
481{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200482 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200483
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200484 memif_msg_add_region_t *ar = &msg->add_region;
485 memif_region_t *mr;
486 if (fd < 0)
487 return MEMIF_ERR_NO_SHMFD;
488
489 if (ar->index > MEMIF_MAX_REGION)
490 return MEMIF_ERR_MAXREG;
491
492 mr =
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200493 (memif_region_t *) lm->realloc (c->regions,
494 sizeof (memif_region_t) *
495 (++c->regions_num));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200496 if (mr == NULL)
497 return memif_syscall_error_handler (errno);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200498 memset (mr + ar->index, 0, sizeof (memif_region_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200499 c->regions = mr;
500 c->regions[ar->index].fd = fd;
501 c->regions[ar->index].region_size = ar->size;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200502 c->regions[ar->index].addr = NULL;
503
Jakub Grajciar412e7b62018-09-27 10:26:35 +0200504 /* region 0 is never external */
505 if (lm->get_external_region_addr && (ar->index != 0))
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200506 c->regions[ar->index].is_external = 1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200507
508 return MEMIF_ERR_SUCCESS; /* 0 */
509}
510
511/* receive ring information and add new ring to connection queue
512 (based on direction S2M | M2S) */
513static_fn int
514memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
515{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200516 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200517
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200518 memif_msg_add_ring_t *ar = &msg->add_ring;
519
520 memif_queue_t *mq;
521
522 if (fd < 0)
523 return MEMIF_ERR_NO_INTFD;
524
Jakub Grajciarab7c2b02018-03-28 10:21:05 +0200525 if (ar->private_hdr_size != 0)
526 return MEMIF_ERR_PRIVHDR;
527
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200528 if (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M)
529 {
530 if (ar->index > MEMIF_MAX_S2M_RING)
531 return MEMIF_ERR_MAXRING;
532 if (ar->index >= c->args.num_s2m_rings)
533 return MEMIF_ERR_MAXRING;
534
535 mq =
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200536 (memif_queue_t *) lm->realloc (c->rx_queues,
537 sizeof (memif_queue_t) *
538 (++c->rx_queues_num));
Milan Lencof8b43e52018-06-26 15:16:15 +0200539 memset (mq + ar->index, 0, sizeof (memif_queue_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200540 if (mq == NULL)
541 return memif_syscall_error_handler (errno);
542 c->rx_queues = mq;
543 c->rx_queues[ar->index].int_fd = fd;
544 c->rx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
545 c->rx_queues[ar->index].region = ar->region;
546 c->rx_queues[ar->index].offset = ar->offset;
547 c->run_args.num_s2m_rings++;
548 }
549 else
550 {
551 if (ar->index > MEMIF_MAX_M2S_RING)
552 return MEMIF_ERR_MAXRING;
553 if (ar->index >= c->args.num_m2s_rings)
554 return MEMIF_ERR_MAXRING;
555
556 mq =
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200557 (memif_queue_t *) lm->realloc (c->tx_queues,
558 sizeof (memif_queue_t) *
559 (++c->tx_queues_num));
Milan Lencof8b43e52018-06-26 15:16:15 +0200560 memset (mq + ar->index, 0, sizeof (memif_queue_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200561 if (mq == NULL)
562 return memif_syscall_error_handler (errno);
563 c->tx_queues = mq;
564 c->tx_queues[ar->index].int_fd = fd;
565 c->tx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
566 c->tx_queues[ar->index].region = ar->region;
567 c->tx_queues[ar->index].offset = ar->offset;
568 c->run_args.num_m2s_rings++;
569 }
570
571 return MEMIF_ERR_SUCCESS; /* 0 */
572}
573
574/* slave -> master */
575static_fn int
576memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg)
577{
578 memif_msg_connect_t *cm = &msg->connect;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200579 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200580 memif_list_elt_t elt;
581
582 int err;
583 err = memif_connect1 (c);
584 if (err != MEMIF_ERR_SUCCESS)
585 return err;
586
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000587 strlcpy ((char *) c->remote_if_name, (char *) cm->if_name,
588 sizeof (c->remote_if_name));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200589
590 int i;
591 if (c->on_interrupt != NULL)
592 {
593 for (i = 0; i < c->run_args.num_m2s_rings; i++)
594 {
595 elt.key = c->rx_queues[i].int_fd;
596 elt.data_struct = c;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200597 add_list_elt (lm, &elt, &lm->interrupt_list,
598 &lm->interrupt_list_len);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200599
Jakub Grajciar12df4972019-07-01 14:24:48 +0200600 lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ,
Jakub Grajciar6f090fa2019-11-14 10:47:25 +0100601 lm->private_ctx);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200602 }
603
604 }
605
606 c->on_connect ((void *) c, c->private_ctx);
607
608 return err;
609}
610
611/* master -> slave */
612static_fn int
613memif_msg_receive_connected (memif_connection_t * c, memif_msg_t * msg)
614{
615 memif_msg_connect_t *cm = &msg->connect;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200616 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200617
618 int err;
619 err = memif_connect1 (c);
620 if (err != MEMIF_ERR_SUCCESS)
621 return err;
622
623 strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000624 sizeof (c->remote_if_name));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200625
626 int i;
627 if (c->on_interrupt != NULL)
628 {
629 for (i = 0; i < c->run_args.num_s2m_rings; i++)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200630 {
Jakub Grajciar12df4972019-07-01 14:24:48 +0200631 lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ,
Jakub Grajciar6f090fa2019-11-14 10:47:25 +0100632 lm->private_ctx);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200633 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200634 }
635
636 c->on_connect ((void *) c, c->private_ctx);
637
638 return err;
639}
640
641static_fn int
642memif_msg_receive_disconnect (memif_connection_t * c, memif_msg_t * msg)
643{
644 memif_msg_disconnect_t *d = &msg->disconnect;
645
646 memset (c->remote_disconnect_string, 0,
647 sizeof (c->remote_disconnect_string));
648 strncpy ((char *) c->remote_disconnect_string, (char *) d->string,
Andrew Yourtchenkoe5b7ca42021-01-29 14:18:12 +0000649 sizeof (c->remote_disconnect_string));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200650
651 /* on returning error, handle function will call memif_disconnect () */
652 DBG ("disconnect received: %s, mode: %d",
653 c->remote_disconnect_string, c->args.mode);
654 return MEMIF_ERR_DISCONNECT;
655}
656
657static_fn int
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200658memif_msg_receive (libmemif_main_t * lm, int ifd)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200659{
660 char ctl[CMSG_SPACE (sizeof (int)) +
661 CMSG_SPACE (sizeof (struct ucred))] = { 0 };
662 struct msghdr mh = { 0 };
663 struct iovec iov[1];
664 memif_msg_t msg = { 0 };
665 ssize_t size;
666 int err = MEMIF_ERR_SUCCESS; /* 0 */
667 int fd = -1;
668 int i;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200669 memif_connection_t *c = NULL;
670 memif_socket_t *ms = NULL;
671 memif_list_elt_t *elt = NULL;
672
673 iov[0].iov_base = (void *) &msg;
674 iov[0].iov_len = sizeof (memif_msg_t);
675 mh.msg_iov = iov;
676 mh.msg_iovlen = 1;
677 mh.msg_control = ctl;
678 mh.msg_controllen = sizeof (ctl);
679
680 DBG ("recvmsg fd %d", ifd);
681 size = recvmsg (ifd, &mh, 0);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200682 if (size != sizeof (memif_msg_t))
683 {
684 if (size == 0)
685 return MEMIF_ERR_DISCONNECTED;
686 else
687 return MEMIF_ERR_MFMSG;
688 }
689
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200690 struct cmsghdr *cmsg;
691
692 cmsg = CMSG_FIRSTHDR (&mh);
693 while (cmsg)
694 {
695 if (cmsg->cmsg_level == SOL_SOCKET)
696 {
697 if (cmsg->cmsg_type == SCM_CREDENTIALS)
698 {
msardara8f554b72018-12-11 18:36:55 +0100699 /* Do nothing */ ;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200700 }
701 else if (cmsg->cmsg_type == SCM_RIGHTS)
702 {
703 int *fdp = (int *) CMSG_DATA (cmsg);
704 fd = *fdp;
705 }
706 }
707 cmsg = CMSG_NXTHDR (&mh, cmsg);
708 }
709
710 DBG ("Message type %u received", msg.type);
711
712 get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
713 if (elt != NULL)
714 c = (memif_connection_t *) elt->data_struct;
715
716 switch (msg.type)
717 {
718 case MEMIF_MSG_TYPE_ACK:
719 break;
720
721 case MEMIF_MSG_TYPE_HELLO:
722 if ((err = memif_msg_receive_hello (c, &msg)) != MEMIF_ERR_SUCCESS)
723 return err;
724 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
725 return err;
726 if ((err = memif_msg_enq_init (c)) != MEMIF_ERR_SUCCESS)
727 return err;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200728 for (i = 0; i < c->regions_num; i++)
729 {
730 if ((err = memif_msg_enq_add_region (c, i)) != MEMIF_ERR_SUCCESS)
731 return err;
732 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200733 for (i = 0; i < c->run_args.num_s2m_rings; i++)
734 {
735 if ((err =
736 memif_msg_enq_add_ring (c, i,
737 MEMIF_RING_S2M)) != MEMIF_ERR_SUCCESS)
738 return err;
739 }
740 for (i = 0; i < c->run_args.num_m2s_rings; i++)
741 {
742 if ((err =
743 memif_msg_enq_add_ring (c, i,
744 MEMIF_RING_M2S)) != MEMIF_ERR_SUCCESS)
745 return err;
746 }
747 if ((err = memif_msg_enq_connect (c)) != MEMIF_ERR_SUCCESS)
748 return err;
749 break;
750
751 case MEMIF_MSG_TYPE_INIT:
752 get_list_elt (&elt, lm->pending_list, lm->pending_list_len, ifd);
753 if (elt == NULL)
754 return -1;
755 ms = (memif_socket_t *) elt->data_struct;
756 if ((err = memif_msg_receive_init (ms, ifd, &msg)) != MEMIF_ERR_SUCCESS)
757 return err;
758 /* c->remote_pid = cr->pid */
759 /* c->remote_uid = cr->uid */
760 /* c->remote_gid = cr->gid */
761 get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
762 if (elt == NULL)
763 return -1;
764 c = (memif_connection_t *) elt->data_struct;
765 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
766 return err;
767 break;
768
769 case MEMIF_MSG_TYPE_ADD_REGION:
770 if ((err =
771 memif_msg_receive_add_region (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
772 return err;
773 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
774 return err;
775 break;
776
777 case MEMIF_MSG_TYPE_ADD_RING:
778 if ((err =
779 memif_msg_receive_add_ring (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
780 return err;
781 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
782 return err;
783 break;
784
785 case MEMIF_MSG_TYPE_CONNECT:
786 if ((err = memif_msg_receive_connect (c, &msg)) != MEMIF_ERR_SUCCESS)
787 return err;
788 if ((err = memif_msg_enq_connected (c)) != MEMIF_ERR_SUCCESS)
789 return err;
790 break;
791
792 case MEMIF_MSG_TYPE_CONNECTED:
793 if ((err = memif_msg_receive_connected (c, &msg)) != MEMIF_ERR_SUCCESS)
794 return err;
795 break;
796
797 case MEMIF_MSG_TYPE_DISCONNECT:
798 if ((err = memif_msg_receive_disconnect (c, &msg)) != MEMIF_ERR_SUCCESS)
799 return err;
800 break;
801
802 default:
803 return MEMIF_ERR_UNKNOWN_MSG;;
804 break;
805 }
806
807 if (c != NULL)
808 c->flags |= MEMIF_CONNECTION_FLAG_WRITE;
Jakub Grajciar12df4972019-07-01 14:24:48 +0200809
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200810 return MEMIF_ERR_SUCCESS; /* 0 */
811}
812
813int
814memif_conn_fd_error (memif_connection_t * c)
815{
816 DBG ("connection fd error");
817 strncpy ((char *) c->remote_disconnect_string, "connection fd error", 19);
818 int err = memif_disconnect_internal (c);
819 return err;
820}
821
822/* calls memif_msg_receive to handle pending messages on socket */
823int
824memif_conn_fd_read_ready (memif_connection_t * c)
825{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200826 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200827 int err;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200828
829 err = memif_msg_receive (lm, c->fd);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200830 if (err != 0)
831 {
832 err = memif_disconnect_internal (c);
833 }
834 return err;
835}
836
837/* get msg from msg queue buffer and send it to socket */
838int
839memif_conn_fd_write_ready (memif_connection_t * c)
840{
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200841 libmemif_main_t *lm = get_libmemif_main (c->args.socket);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200842 int err = MEMIF_ERR_SUCCESS; /* 0 */
843
844
845 if ((c->flags & MEMIF_CONNECTION_FLAG_WRITE) == 0)
846 goto done;
847
848 memif_msg_queue_elt_t *e = c->msg_queue;
849 if (e == NULL)
850 goto done;
851
852 c->msg_queue = c->msg_queue->next;
853
854 c->flags &= ~MEMIF_CONNECTION_FLAG_WRITE;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200855
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200856 err = memif_msg_send (c->fd, &e->msg, e->fd);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200857 lm->free (e);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200858 goto done;
859
860done:
861 return err;
862}
863
864int
865memif_conn_fd_accept_ready (memif_socket_t * ms)
866{
867 int addr_len;
868 struct sockaddr_un client;
869 int conn_fd;
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200870 libmemif_main_t *lm = get_libmemif_main (ms);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200871
872 DBG ("accept called");
873
874 addr_len = sizeof (client);
875 conn_fd =
876 accept (ms->fd, (struct sockaddr *) &client, (socklen_t *) & addr_len);
877
878 if (conn_fd < 0)
879 {
880 return memif_syscall_error_handler (errno);
881 }
882 DBG ("accept fd %d", ms->fd);
883 DBG ("conn fd %d", conn_fd);
884
885 memif_list_elt_t elt;
886 elt.key = conn_fd;
887 elt.data_struct = ms;
888
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200889 add_list_elt (lm, &elt, &lm->pending_list, &lm->pending_list_len);
Jakub Grajciar12df4972019-07-01 14:24:48 +0200890 lm->control_fd_update (conn_fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE,
Jakub Grajciar6f090fa2019-11-14 10:47:25 +0100891 lm->private_ctx);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200892
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200893 return memif_msg_send_hello (lm, conn_fd);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200894}
895
896int
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200897memif_read_ready (libmemif_main_t * lm, int fd)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200898{
899 int err;
Jakub Grajciar84b83772019-03-04 12:42:19 +0100900
Jakub Grajciar17f2a7b2019-07-31 14:40:52 +0200901 err = memif_msg_receive (lm, fd);
Jakub Grajciar84b83772019-03-04 12:42:19 +0100902
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200903 return err;
904}