blob: 495cef9ad43bb82888b1114f80afec68348c5ec8 [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 Grajciarecfa2aa2018-03-26 11:26:34 +020074 libmemif_main_t *lm = &libmemif_main;
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
102memif_msg_send_hello (int fd)
103{
104 libmemif_main_t *lm = &libmemif_main;
105 memif_msg_t msg = { 0 };
106 memif_msg_hello_t *h = &msg.hello;
107 msg.type = MEMIF_MSG_TYPE_HELLO;
108 h->min_version = MEMIF_VERSION;
109 h->max_version = MEMIF_VERSION;
Damjan Marion20728d42018-07-11 12:24:19 +0200110 h->max_s2m_ring = MEMIF_MAX_S2M_RING;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200111 h->max_m2s_ring = MEMIF_MAX_M2S_RING;
112 h->max_region = MEMIF_MAX_REGION;
113 h->max_log2_ring_size = MEMIF_MAX_LOG2_RING_SIZE;
114
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200115 strncpy ((char *) h->name, (char *) lm->app_name,
116 strlen ((char *) lm->app_name));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200117
118 /* msg hello is not enqueued but sent directly,
119 because it is the first msg to be sent */
120 return memif_msg_send (fd, &msg, -1);
121}
122
123/* send id and secret (optional) for interface identification */
124static_fn int
125memif_msg_enq_init (memif_connection_t * c)
126{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200127 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200128 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200129 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200130 if (e == NULL)
131 return memif_syscall_error_handler (errno);
132 memset (e, 0, sizeof (memif_msg_queue_elt_t));
133
134 memset (&e->msg, 0, sizeof (e->msg));
135 memif_msg_init_t *i = &e->msg.init;
136
137 e->msg.type = MEMIF_MSG_TYPE_INIT;
138 e->fd = -1;
139 i->version = MEMIF_VERSION;
140 i->id = c->args.interface_id;
141 i->mode = c->args.mode;
142
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200143 strncpy ((char *) i->name, (char *) lm->app_name,
144 strlen ((char *) lm->app_name));
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200145 if (strlen ((char *) c->args.secret) > 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200146 strncpy ((char *) i->secret, (char *) c->args.secret, sizeof (i->secret));
147
148 e->next = NULL;
149 if (c->msg_queue == NULL)
150 {
151 c->msg_queue = e;
152 return MEMIF_ERR_SUCCESS; /* 0 */
153 }
154
155 memif_msg_queue_elt_t *cur = c->msg_queue;
156 while (cur->next != NULL)
157 {
158 cur = cur->next;
159 }
160 cur->next = e;
161
162 return MEMIF_ERR_SUCCESS; /* 0 */
163}
164
165/* send information about region specified by region_index */
166static_fn int
167memif_msg_enq_add_region (memif_connection_t * c, uint8_t region_index)
168{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200169 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200170 memif_region_t *mr = &c->regions[region_index];
171
172 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200173 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200174 if (e == NULL)
175 return memif_syscall_error_handler (errno);
176
177 memset (&e->msg, 0, sizeof (e->msg));
178 memif_msg_add_region_t *ar = &e->msg.add_region;
179
180 e->msg.type = MEMIF_MSG_TYPE_ADD_REGION;
181 e->fd = mr->fd;
182 ar->index = region_index;
183 ar->size = mr->region_size;
184
185 e->next = NULL;
186 if (c->msg_queue == NULL)
187 {
188 c->msg_queue = e;
189 return MEMIF_ERR_SUCCESS; /* 0 */
190 }
191
192 memif_msg_queue_elt_t *cur = c->msg_queue;
193 while (cur->next != NULL)
194 {
195 cur = cur->next;
196 }
197 cur->next = e;
198
199 return MEMIF_ERR_SUCCESS; /* 0 */
200}
201
202/* send information about ring specified by direction (S2M | M2S) and index */
203static_fn int
204memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index, uint8_t dir)
205{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200206 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200207 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200208 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200209 if (e == NULL)
210 return memif_syscall_error_handler (errno);
211
212 memset (&e->msg, 0, sizeof (e->msg));
213 memif_msg_add_ring_t *ar = &e->msg.add_ring;
214
215 e->msg.type = MEMIF_MSG_TYPE_ADD_RING;
216
217 /* TODO: support multiple rings */
218 memif_queue_t *mq;
219 if (dir == MEMIF_RING_M2S)
220 mq = &c->rx_queues[index];
221 else
222 mq = &c->tx_queues[index];
223
224 e->fd = mq->int_fd;
225 ar->index = index;
226 ar->offset = mq->offset;
227 ar->region = mq->region;
228 ar->log2_ring_size = mq->log2_ring_size;
229 ar->flags = (dir == MEMIF_RING_S2M) ? MEMIF_MSG_ADD_RING_FLAG_S2M : 0;
Jakub Grajciarab7c2b02018-03-28 10:21:05 +0200230 ar->private_hdr_size = 0;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200231
232 e->next = NULL;
233 if (c->msg_queue == NULL)
234 {
235 c->msg_queue = e;
236 return MEMIF_ERR_SUCCESS; /* 0 */
237 }
238
239 memif_msg_queue_elt_t *cur = c->msg_queue;
240 while (cur->next != NULL)
241 {
242 cur = cur->next;
243 }
244 cur->next = e;
245
246 return MEMIF_ERR_SUCCESS; /* 0 */
247}
248
249/* used as connection request from slave */
250static_fn int
251memif_msg_enq_connect (memif_connection_t * c)
252{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200253 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200254 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200255 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200256 if (e == NULL)
257 return memif_syscall_error_handler (errno);
258
259 memset (&e->msg, 0, sizeof (e->msg));
260 memif_msg_connect_t *cm = &e->msg.connect;
261
262 e->msg.type = MEMIF_MSG_TYPE_CONNECT;
263 e->fd = -1;
264 strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
265 strlen ((char *) c->args.interface_name));
266
267 e->next = NULL;
268 if (c->msg_queue == NULL)
269 {
270 c->msg_queue = e;
271 return MEMIF_ERR_SUCCESS; /* 0 */
272 }
273
274 memif_msg_queue_elt_t *cur = c->msg_queue;
275 while (cur->next != NULL)
276 {
277 cur = cur->next;
278 }
279 cur->next = e;
280
281 return MEMIF_ERR_SUCCESS; /* 0 */
282}
283
284/* used as confirmation of connection by master */
285static_fn int
286memif_msg_enq_connected (memif_connection_t * c)
287{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200288 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200289 memif_msg_queue_elt_t *e =
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200290 (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200291 if (e == NULL)
292 return memif_syscall_error_handler (errno);
293
294 memset (&e->msg, 0, sizeof (e->msg));
295 memif_msg_connected_t *cm = &e->msg.connected;
296
297 e->msg.type = MEMIF_MSG_TYPE_CONNECTED;
298 e->fd = -1;
299 strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
300 strlen ((char *) c->args.interface_name));
301
302 e->next = NULL;
303 if (c->msg_queue == NULL)
304 {
305 c->msg_queue = e;
306 return MEMIF_ERR_SUCCESS; /* 0 */
307 }
308
309 memif_msg_queue_elt_t *cur = c->msg_queue;
310 while (cur->next != NULL)
311 {
312 cur = cur->next;
313 }
314 cur->next = e;
315
316 return MEMIF_ERR_SUCCESS; /* 0 */
317}
318
319/* immediately send disconnect msg */
320 /* specifie protocol for disconnect msg err_code
321 so that it will be compatible with VPP? (header/doc) */
322int
323memif_msg_send_disconnect (int fd, uint8_t * err_string, uint32_t err_code)
324{
325 memif_msg_t msg = { 0 };
326 memif_msg_disconnect_t *d = &msg.disconnect;
327
328 msg.type = MEMIF_MSG_TYPE_DISCONNECT;
329 d->code = err_code;
330 uint16_t l = strlen ((char *) err_string);
331 if (l > 96)
332 {
333 DBG ("Disconnect string too long. Sending first 96 characters.");
334 l = 96;
335 }
336 strncpy ((char *) d->string, (char *) err_string, l);
337
338 return memif_msg_send (fd, &msg, -1);
339}
340
341static_fn int
342memif_msg_receive_hello (memif_connection_t * c, memif_msg_t * msg)
343{
344 memif_msg_hello_t *h = &msg->hello;
345
346 if (msg->hello.min_version > MEMIF_VERSION ||
347 msg->hello.max_version < MEMIF_VERSION)
348 {
349 DBG ("incompatible protocol version");
350 return MEMIF_ERR_PROTO;
351 }
352
353 c->run_args.num_s2m_rings = memif_min (h->max_s2m_ring + 1,
354 c->args.num_s2m_rings);
355 c->run_args.num_m2s_rings = memif_min (h->max_m2s_ring + 1,
356 c->args.num_m2s_rings);
357 c->run_args.log2_ring_size = memif_min (h->max_log2_ring_size,
358 c->args.log2_ring_size);
359 c->run_args.buffer_size = c->args.buffer_size;
360 strncpy ((char *) c->remote_name, (char *) h->name,
361 strlen ((char *) h->name));
362
363 return MEMIF_ERR_SUCCESS; /* 0 */
364}
365
366/* handle interface identification (id, secret (optional)) */
367static_fn int
368memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
369{
370 memif_msg_init_t *i = &msg->init;
371 memif_list_elt_t *elt = NULL;
372 memif_list_elt_t elt2;
373 memif_connection_t *c = NULL;
374 libmemif_main_t *lm = &libmemif_main;
375 uint8_t err_string[96];
376 memset (err_string, 0, sizeof (char) * 96);
377 int err = MEMIF_ERR_SUCCESS; /* 0 */
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200378 if (i->version != MEMIF_VERSION)
379 {
380 DBG ("MEMIF_VER_ERR");
381 strncpy ((char *) err_string, MEMIF_VER_ERR, strlen (MEMIF_VER_ERR));
382 err = MEMIF_ERR_PROTO;
383 goto error;
384 }
385
386 get_list_elt (&elt, ms->interface_list, ms->interface_list_len, i->id);
387 if (elt == NULL)
388 {
389 DBG ("MEMIF_ID_ERR");
390 strncpy ((char *) err_string, MEMIF_ID_ERR, strlen (MEMIF_ID_ERR));
391 err = MEMIF_ERR_ID;
392 goto error;
393 }
394
395 c = (memif_connection_t *) elt->data_struct;
396
397 if (!(c->args.is_master))
398 {
399 DBG ("MEMIF_SLAVE_ERR");
400 strncpy ((char *) err_string, MEMIF_SLAVE_ERR,
401 strlen (MEMIF_SLAVE_ERR));
402 err = MEMIF_ERR_ACCSLAVE;
403 goto error;
404 }
405 if (c->fd != -1)
406 {
407 DBG ("MEMIF_CONN_ERR");
408 strncpy ((char *) err_string, MEMIF_CONN_ERR, strlen (MEMIF_CONN_ERR));
409 err = MEMIF_ERR_ALRCONN;
410 goto error;
411 }
412
413 c->fd = fd;
414
415 if (i->mode != c->args.mode)
416 {
417 DBG ("MEMIF_MODE_ERR");
418 strncpy ((char *) err_string, MEMIF_MODE_ERR, strlen (MEMIF_MODE_ERR));
419 err = MEMIF_ERR_MODE;
420 goto error;
421 }
422
423 strncpy ((char *) c->remote_name, (char *) i->name,
424 strlen ((char *) i->name));
425
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200426 if (strlen ((char *) c->args.secret) > 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200427 {
428 int r;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200429 if (strlen ((char *) i->secret) > 0)
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200430 {
431 if (strlen ((char *) c->args.secret) != strlen ((char *) i->secret))
432 {
433 DBG ("MEMIF_SECRET_ERR");
434 strncpy ((char *) err_string,
435 MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
436 err = MEMIF_ERR_SECRET;
437 goto error;
438 }
439 r = strncmp ((char *) i->secret, (char *) c->args.secret,
440 strlen ((char *) c->args.secret));
441 if (r != 0)
442 {
443 DBG ("MEMIF_SECRET_ERR");
444 strncpy ((char *) err_string,
445 MEMIF_SECRET_ERR, strlen (MEMIF_SECRET_ERR));
446 err = MEMIF_ERR_SECRET;
447 goto error;
448 }
449 }
450 else
451 {
452 DBG ("MEMIF_NOSECRET_ERR");
453 strncpy ((char *) err_string,
454 MEMIF_NOSECRET_ERR, strlen (MEMIF_NOSECRET_ERR));
455 err = MEMIF_ERR_NOSECRET;
456 goto error;
457 }
458 }
459
460 c->read_fn = memif_conn_fd_read_ready;
461 c->write_fn = memif_conn_fd_write_ready;
462 c->error_fn = memif_conn_fd_error;
463
464 elt2.key = c->fd;
465 elt2.data_struct = c;
466
467 add_list_elt (&elt2, &lm->control_list, &lm->control_list_len);
468 free_list_elt (lm->pending_list, lm->pending_list_len, fd);
469
470 return err;
471
472error:
473 memif_msg_send_disconnect (fd, err_string, 0);
474 lm->control_fd_update (fd, MEMIF_FD_EVENT_DEL);
475 free_list_elt (lm->pending_list, lm->pending_list_len, fd);
476 close (fd);
477 fd = -1;
478 return err;
479}
480
481/* receive region information and add new region to connection (if possible) */
482static_fn int
483memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
484 int fd)
485{
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200486 libmemif_main_t *lm = &libmemif_main;
487
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200488 memif_msg_add_region_t *ar = &msg->add_region;
489 memif_region_t *mr;
490 if (fd < 0)
491 return MEMIF_ERR_NO_SHMFD;
492
493 if (ar->index > MEMIF_MAX_REGION)
494 return MEMIF_ERR_MAXREG;
495
496 mr =
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200497 (memif_region_t *) lm->realloc (c->regions,
498 sizeof (memif_region_t) *
499 (++c->regions_num));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200500 if (mr == NULL)
501 return memif_syscall_error_handler (errno);
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200502 memset (mr + ar->index, 0, sizeof (memif_region_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200503 c->regions = mr;
504 c->regions[ar->index].fd = fd;
505 c->regions[ar->index].region_size = ar->size;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200506 c->regions[ar->index].addr = NULL;
507
Jakub Grajciar412e7b62018-09-27 10:26:35 +0200508 /* region 0 is never external */
509 if (lm->get_external_region_addr && (ar->index != 0))
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200510 c->regions[ar->index].is_external = 1;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200511
512 return MEMIF_ERR_SUCCESS; /* 0 */
513}
514
515/* receive ring information and add new ring to connection queue
516 (based on direction S2M | M2S) */
517static_fn int
518memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
519{
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200520 libmemif_main_t *lm = &libmemif_main;
521
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200522 memif_msg_add_ring_t *ar = &msg->add_ring;
523
524 memif_queue_t *mq;
525
526 if (fd < 0)
527 return MEMIF_ERR_NO_INTFD;
528
Jakub Grajciarab7c2b02018-03-28 10:21:05 +0200529 if (ar->private_hdr_size != 0)
530 return MEMIF_ERR_PRIVHDR;
531
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200532 if (ar->flags & MEMIF_MSG_ADD_RING_FLAG_S2M)
533 {
534 if (ar->index > MEMIF_MAX_S2M_RING)
535 return MEMIF_ERR_MAXRING;
536 if (ar->index >= c->args.num_s2m_rings)
537 return MEMIF_ERR_MAXRING;
538
539 mq =
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200540 (memif_queue_t *) lm->realloc (c->rx_queues,
541 sizeof (memif_queue_t) *
542 (++c->rx_queues_num));
Milan Lencof8b43e52018-06-26 15:16:15 +0200543 memset (mq + ar->index, 0, sizeof (memif_queue_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200544 if (mq == NULL)
545 return memif_syscall_error_handler (errno);
546 c->rx_queues = mq;
547 c->rx_queues[ar->index].int_fd = fd;
548 c->rx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
549 c->rx_queues[ar->index].region = ar->region;
550 c->rx_queues[ar->index].offset = ar->offset;
551 c->run_args.num_s2m_rings++;
552 }
553 else
554 {
555 if (ar->index > MEMIF_MAX_M2S_RING)
556 return MEMIF_ERR_MAXRING;
557 if (ar->index >= c->args.num_m2s_rings)
558 return MEMIF_ERR_MAXRING;
559
560 mq =
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200561 (memif_queue_t *) lm->realloc (c->tx_queues,
562 sizeof (memif_queue_t) *
563 (++c->tx_queues_num));
Milan Lencof8b43e52018-06-26 15:16:15 +0200564 memset (mq + ar->index, 0, sizeof (memif_queue_t));
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200565 if (mq == NULL)
566 return memif_syscall_error_handler (errno);
567 c->tx_queues = mq;
568 c->tx_queues[ar->index].int_fd = fd;
569 c->tx_queues[ar->index].log2_ring_size = ar->log2_ring_size;
570 c->tx_queues[ar->index].region = ar->region;
571 c->tx_queues[ar->index].offset = ar->offset;
572 c->run_args.num_m2s_rings++;
573 }
574
575 return MEMIF_ERR_SUCCESS; /* 0 */
576}
577
578/* slave -> master */
579static_fn int
580memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg)
581{
582 memif_msg_connect_t *cm = &msg->connect;
583 libmemif_main_t *lm = &libmemif_main;
584 memif_list_elt_t elt;
585
586 int err;
587 err = memif_connect1 (c);
588 if (err != MEMIF_ERR_SUCCESS)
589 return err;
590
591 strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
592 strlen ((char *) cm->if_name));
593
594 int i;
595 if (c->on_interrupt != NULL)
596 {
597 for (i = 0; i < c->run_args.num_m2s_rings; i++)
598 {
599 elt.key = c->rx_queues[i].int_fd;
600 elt.data_struct = c;
601 add_list_elt (&elt, &lm->interrupt_list, &lm->interrupt_list_len);
602
603 lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
604 }
605
606 }
607
608 c->on_connect ((void *) c, c->private_ctx);
609
610 return err;
611}
612
613/* master -> slave */
614static_fn int
615memif_msg_receive_connected (memif_connection_t * c, memif_msg_t * msg)
616{
617 memif_msg_connect_t *cm = &msg->connect;
618 libmemif_main_t *lm = &libmemif_main;
619
620 int err;
621 err = memif_connect1 (c);
622 if (err != MEMIF_ERR_SUCCESS)
623 return err;
624
625 strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
626 strlen ((char *) cm->if_name));
627
628 int i;
629 if (c->on_interrupt != NULL)
630 {
631 for (i = 0; i < c->run_args.num_s2m_rings; i++)
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200632 {
633 lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200634 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200635 }
636
637 c->on_connect ((void *) c, c->private_ctx);
638
639 return err;
640}
641
642static_fn int
643memif_msg_receive_disconnect (memif_connection_t * c, memif_msg_t * msg)
644{
645 memif_msg_disconnect_t *d = &msg->disconnect;
646
647 memset (c->remote_disconnect_string, 0,
648 sizeof (c->remote_disconnect_string));
649 strncpy ((char *) c->remote_disconnect_string, (char *) d->string,
650 strlen ((char *) d->string));
651
652 /* on returning error, handle function will call memif_disconnect () */
653 DBG ("disconnect received: %s, mode: %d",
654 c->remote_disconnect_string, c->args.mode);
655 return MEMIF_ERR_DISCONNECT;
656}
657
658static_fn int
659memif_msg_receive (int ifd)
660{
661 char ctl[CMSG_SPACE (sizeof (int)) +
662 CMSG_SPACE (sizeof (struct ucred))] = { 0 };
663 struct msghdr mh = { 0 };
664 struct iovec iov[1];
665 memif_msg_t msg = { 0 };
666 ssize_t size;
667 int err = MEMIF_ERR_SUCCESS; /* 0 */
668 int fd = -1;
669 int i;
670 libmemif_main_t *lm = &libmemif_main;
671 memif_connection_t *c = NULL;
672 memif_socket_t *ms = NULL;
673 memif_list_elt_t *elt = NULL;
674
675 iov[0].iov_base = (void *) &msg;
676 iov[0].iov_len = sizeof (memif_msg_t);
677 mh.msg_iov = iov;
678 mh.msg_iovlen = 1;
679 mh.msg_control = ctl;
680 mh.msg_controllen = sizeof (ctl);
681
682 DBG ("recvmsg fd %d", ifd);
683 size = recvmsg (ifd, &mh, 0);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200684 if (size != sizeof (memif_msg_t))
685 {
686 if (size == 0)
687 return MEMIF_ERR_DISCONNECTED;
688 else
689 return MEMIF_ERR_MFMSG;
690 }
691
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200692 struct cmsghdr *cmsg;
693
694 cmsg = CMSG_FIRSTHDR (&mh);
695 while (cmsg)
696 {
697 if (cmsg->cmsg_level == SOL_SOCKET)
698 {
699 if (cmsg->cmsg_type == SCM_CREDENTIALS)
700 {
msardara8f554b72018-12-11 18:36:55 +0100701 /* Do nothing */ ;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200702 }
703 else if (cmsg->cmsg_type == SCM_RIGHTS)
704 {
705 int *fdp = (int *) CMSG_DATA (cmsg);
706 fd = *fdp;
707 }
708 }
709 cmsg = CMSG_NXTHDR (&mh, cmsg);
710 }
711
712 DBG ("Message type %u received", msg.type);
713
714 get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
715 if (elt != NULL)
716 c = (memif_connection_t *) elt->data_struct;
717
718 switch (msg.type)
719 {
720 case MEMIF_MSG_TYPE_ACK:
721 break;
722
723 case MEMIF_MSG_TYPE_HELLO:
724 if ((err = memif_msg_receive_hello (c, &msg)) != MEMIF_ERR_SUCCESS)
725 return err;
726 if ((err = memif_init_regions_and_queues (c)) != MEMIF_ERR_SUCCESS)
727 return err;
728 if ((err = memif_msg_enq_init (c)) != MEMIF_ERR_SUCCESS)
729 return err;
Jakub Grajciar93a5dd12018-08-20 14:26:32 +0200730 for (i = 0; i < c->regions_num; i++)
731 {
732 if ((err = memif_msg_enq_add_region (c, i)) != MEMIF_ERR_SUCCESS)
733 return err;
734 }
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200735 for (i = 0; i < c->run_args.num_s2m_rings; i++)
736 {
737 if ((err =
738 memif_msg_enq_add_ring (c, i,
739 MEMIF_RING_S2M)) != MEMIF_ERR_SUCCESS)
740 return err;
741 }
742 for (i = 0; i < c->run_args.num_m2s_rings; i++)
743 {
744 if ((err =
745 memif_msg_enq_add_ring (c, i,
746 MEMIF_RING_M2S)) != MEMIF_ERR_SUCCESS)
747 return err;
748 }
749 if ((err = memif_msg_enq_connect (c)) != MEMIF_ERR_SUCCESS)
750 return err;
751 break;
752
753 case MEMIF_MSG_TYPE_INIT:
754 get_list_elt (&elt, lm->pending_list, lm->pending_list_len, ifd);
755 if (elt == NULL)
756 return -1;
757 ms = (memif_socket_t *) elt->data_struct;
758 if ((err = memif_msg_receive_init (ms, ifd, &msg)) != MEMIF_ERR_SUCCESS)
759 return err;
760 /* c->remote_pid = cr->pid */
761 /* c->remote_uid = cr->uid */
762 /* c->remote_gid = cr->gid */
763 get_list_elt (&elt, lm->control_list, lm->control_list_len, ifd);
764 if (elt == NULL)
765 return -1;
766 c = (memif_connection_t *) elt->data_struct;
767 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
768 return err;
769 break;
770
771 case MEMIF_MSG_TYPE_ADD_REGION:
772 if ((err =
773 memif_msg_receive_add_region (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
774 return err;
775 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
776 return err;
777 break;
778
779 case MEMIF_MSG_TYPE_ADD_RING:
780 if ((err =
781 memif_msg_receive_add_ring (c, &msg, fd)) != MEMIF_ERR_SUCCESS)
782 return err;
783 if ((err = memif_msg_enq_ack (c)) != MEMIF_ERR_SUCCESS)
784 return err;
785 break;
786
787 case MEMIF_MSG_TYPE_CONNECT:
788 if ((err = memif_msg_receive_connect (c, &msg)) != MEMIF_ERR_SUCCESS)
789 return err;
790 if ((err = memif_msg_enq_connected (c)) != MEMIF_ERR_SUCCESS)
791 return err;
792 break;
793
794 case MEMIF_MSG_TYPE_CONNECTED:
795 if ((err = memif_msg_receive_connected (c, &msg)) != MEMIF_ERR_SUCCESS)
796 return err;
797 break;
798
799 case MEMIF_MSG_TYPE_DISCONNECT:
800 if ((err = memif_msg_receive_disconnect (c, &msg)) != MEMIF_ERR_SUCCESS)
801 return err;
802 break;
803
804 default:
805 return MEMIF_ERR_UNKNOWN_MSG;;
806 break;
807 }
808
809 if (c != NULL)
810 c->flags |= MEMIF_CONNECTION_FLAG_WRITE;
811/* libmemif_main_t *lm = &libmemif_main;
812 lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); */
813 return MEMIF_ERR_SUCCESS; /* 0 */
814}
815
816int
817memif_conn_fd_error (memif_connection_t * c)
818{
819 DBG ("connection fd error");
820 strncpy ((char *) c->remote_disconnect_string, "connection fd error", 19);
821 int err = memif_disconnect_internal (c);
822 return err;
823}
824
825/* calls memif_msg_receive to handle pending messages on socket */
826int
827memif_conn_fd_read_ready (memif_connection_t * c)
828{
829 int err;
830 err = memif_msg_receive (c->fd);
831 if (err != 0)
832 {
833 err = memif_disconnect_internal (c);
834 }
835 return err;
836}
837
838/* get msg from msg queue buffer and send it to socket */
839int
840memif_conn_fd_write_ready (memif_connection_t * c)
841{
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200842 libmemif_main_t *lm = &libmemif_main;
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200843 int err = MEMIF_ERR_SUCCESS; /* 0 */
844
845
846 if ((c->flags & MEMIF_CONNECTION_FLAG_WRITE) == 0)
847 goto done;
848
849 memif_msg_queue_elt_t *e = c->msg_queue;
850 if (e == NULL)
851 goto done;
852
853 c->msg_queue = c->msg_queue->next;
854
855 c->flags &= ~MEMIF_CONNECTION_FLAG_WRITE;
856/*
857 libmemif_main_t *lm = &libmemif_main;
858
859 lm->control_fd_update (c->fd,
860 MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE | MEMIF_FD_EVENT_MOD);
861*/
862 err = memif_msg_send (c->fd, &e->msg, e->fd);
Jakub Grajciarecfa2aa2018-03-26 11:26:34 +0200863 lm->free (e);
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200864 goto done;
865
866done:
867 return err;
868}
869
870int
871memif_conn_fd_accept_ready (memif_socket_t * ms)
872{
873 int addr_len;
874 struct sockaddr_un client;
875 int conn_fd;
876 libmemif_main_t *lm = &libmemif_main;
877
878 DBG ("accept called");
879
880 addr_len = sizeof (client);
881 conn_fd =
882 accept (ms->fd, (struct sockaddr *) &client, (socklen_t *) & addr_len);
883
884 if (conn_fd < 0)
885 {
886 return memif_syscall_error_handler (errno);
887 }
888 DBG ("accept fd %d", ms->fd);
889 DBG ("conn fd %d", conn_fd);
890
891 memif_list_elt_t elt;
892 elt.key = conn_fd;
893 elt.data_struct = ms;
894
895 add_list_elt (&elt, &lm->pending_list, &lm->pending_list_len);
896 lm->control_fd_update (conn_fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE);
897
898 return memif_msg_send_hello (conn_fd);
899}
900
901int
902memif_read_ready (int fd)
903{
904 int err;
Jakub Grajciar84b83772019-03-04 12:42:19 +0100905
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200906 err = memif_msg_receive (fd);
Jakub Grajciar84b83772019-03-04 12:42:19 +0100907
Jakub Grajciar7c5c40d2017-08-30 10:13:25 +0200908 return err;
909}