This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ipc.c
455 lines (372 loc) · 10 KB
/
ipc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <uapi/err.h>
#include <lk/list.h>
#include <stdlib.h>
#include <trusty_std.h>
#include "ipc.h"
#define MSG_BUF_MAX_SIZE 4096
#ifndef USER_BUILD
#define TLOGE(args...) fprintf(stderr, "ipc: " args)
#else
#define TLOGE(args...) do {} while(0)
#endif
static void *msg_buf;
static size_t msg_buf_size;
static void handle_channel(struct ipc_context *ctx, const struct uevent *ev);
static void handle_port(struct ipc_context *ctx, const struct uevent *ev);
static int maybe_grow_msg_buf(size_t new_max_size)
{
if (new_max_size > msg_buf_size) {
uint8_t *tmp = realloc(msg_buf, new_max_size);
if (tmp == NULL) {
return ERR_NO_MEMORY;
}
msg_buf = tmp;
msg_buf_size = new_max_size;
}
return NO_ERROR;
}
static inline void handle_port_errors(const uevent_t *ev)
{
if ((ev->event & IPC_HANDLE_POLL_ERROR) ||
(ev->event & IPC_HANDLE_POLL_HUP) ||
(ev->event & IPC_HANDLE_POLL_MSG) ||
(ev->event & IPC_HANDLE_POLL_SEND_UNBLOCKED)) {
/* should never happen with port handles */
TLOGE("error event (0x%x) for port (%d)\n", ev->event,
ev->handle);
abort();
}
}
static inline void handle_chan_errors(const uevent_t *ev)
{
if ((ev->event & IPC_HANDLE_POLL_ERROR) ||
(ev->event & IPC_HANDLE_POLL_READY)) {
TLOGE("error event (0x%x) for chan (%d)\n", ev->event,
ev->handle);
abort();
}
}
static int is_valid_port_ops(struct ipc_port_ops *ops)
{
return (ops->on_connect != NULL);
}
static bool is_valid_chan_ops(struct ipc_channel_ops *ops)
{
return (ops->on_disconnect != NULL);
}
static struct ipc_port_context *to_port_context(struct ipc_context *context)
{
assert(context);
return containerof(context, struct ipc_port_context, common);
}
static struct ipc_channel_context *to_channel_context(struct ipc_context *context)
{
assert(context);
return containerof(context, struct ipc_channel_context, common);
}
static int do_connect(struct ipc_port_context *ctx, const uevent_t *ev)
{
int rc;
handle_t chan_handle;
struct ipc_channel_context *chan_ctx;
if (ev->event & IPC_HANDLE_POLL_READY) {
/* incoming connection: accept it */
uuid_t peer_uuid;
rc = accept(ev->handle, &peer_uuid);
if (rc < 0) {
TLOGE("failed (%d) to accept on port %d\n", rc,
ev->handle);
return rc;
}
chan_handle = (handle_t) rc;
chan_ctx = ctx->ops.on_connect(ctx, &peer_uuid, chan_handle);
if (chan_ctx == NULL) {
TLOGE("%s: failure initializing channel state (%d)\n",
__func__, rc);
rc = ERR_GENERIC;
goto err_on_connect;
}
assert(is_valid_chan_ops(&chan_ctx->ops));
chan_ctx->common.evt_handler = handle_channel;
chan_ctx->common.handle = chan_handle;
rc = set_cookie(chan_handle, chan_ctx);
if (rc < 0) {
TLOGE("failed (%d) to set_cookie on chan %d\n", rc,
chan_handle);
goto err_set_cookie;
}
}
return NO_ERROR;
err_set_cookie:
chan_ctx->ops.on_disconnect(chan_ctx);
err_on_connect:
close(chan_handle);
return rc;
}
static int do_handle_msg(struct ipc_channel_context *ctx, const uevent_t *ev)
{
handle_t chan = ev->handle;
/* get message info */
ipc_msg_info_t msg_inf;
int rc = get_msg(chan, &msg_inf);
if (rc == ERR_NO_MSG)
return NO_ERROR; /* no new messages */
if (rc != NO_ERROR) {
TLOGE("failed (%d) to get_msg for chan (%d), closing "
"connection\n",
rc, chan);
return rc;
}
if (msg_inf.len > MSG_BUF_MAX_SIZE) {
TLOGE("%s: message too large %d\n", __func__, msg_inf.len);
put_msg(chan, msg_inf.id);
return ERR_NOT_ENOUGH_BUFFER;
}
/* read msg content */
iovec_t iov = {
.base = msg_buf,
.len = msg_inf.len,
};
ipc_msg_t msg = {
.iov = &iov,
.num_iov = 1,
};
rc = read_msg(chan, msg_inf.id, 0, &msg);
put_msg(chan, msg_inf.id);
if (rc < 0) {
TLOGE("failed to read msg (%d, %d)\n", rc, chan);
return rc;
}
if (((size_t) rc) < msg_inf.len) {
TLOGE("invalid message of size (%d, %d)\n", rc, chan);
return ERR_NOT_VALID;
}
rc = ctx->ops.on_handle_msg(ctx, msg_buf, msg_inf.len);
err_handle_msg:
err_read_msg:
return rc;
}
static void do_disconnect(struct ipc_channel_context *context, const uevent_t *ev)
{
context->ops.on_disconnect(context);
close(ev->handle);
}
static void handle_port(struct ipc_context *ctx, const struct uevent *ev)
{
struct ipc_port_context *port_ctx = to_port_context(ctx);
assert(is_valid_port_ops(&port_ctx->ops));
handle_port_errors(ev);
do_connect(port_ctx, ev);
}
static void handle_channel(struct ipc_context *ctx, const struct uevent *ev)
{
struct ipc_channel_context *channel_ctx = to_channel_context(ctx);
assert(is_valid_chan_ops(&channel_ctx->ops));
handle_chan_errors(ev);
if (ev->event & IPC_HANDLE_POLL_MSG) {
if (channel_ctx->ops.on_handle_msg != NULL) {
int rc = do_handle_msg(channel_ctx, ev);
if (rc < 0) {
TLOGE("error (%d) in channel, disconnecting "
"peer\n", rc);
do_disconnect(channel_ctx, ev);
return;
}
} else {
TLOGE("error: unexpected message in channel (%d). closing...\n", ev->handle);
do_disconnect(channel_ctx, ev);
return;
}
}
if (ev->event & IPC_HANDLE_POLL_HUP) {
do_disconnect(channel_ctx, ev);
}
}
static int read_response(handle_t session, uint32_t msg_id, iovec_t *iovecs,
size_t iovec_count)
{
struct ipc_msg rx_msg = {
.iov = iovecs,
.num_iov = iovec_count,
};
long rc = read_msg(session, msg_id, 0, &rx_msg);
put_msg(session, msg_id);
if (rc < 0) {
TLOGE("%s: failed to read msg (%ld)\n", __func__, rc);
return rc;
}
size_t read_size = (size_t) rc;
return read_size;
}
static int await_response(handle_t session, struct ipc_msg_info *inf)
{
uevent_t uevt;
long rc = wait(session, &uevt, -1);
if (rc != NO_ERROR) {
TLOGE("%s: interrupted waiting for response (%ld)", __func__, rc);
return rc;
}
rc = get_msg(session, inf);
if (rc != NO_ERROR) {
TLOGE("%s: failed to get_msg (%ld)\n", __func__, rc);
}
return rc;
}
static int wait_to_send(handle_t session, struct ipc_msg *msg)
{
int rc;
struct uevent ev = UEVENT_INITIAL_VALUE(ev);
rc = wait(session, &ev, -1);
if (rc < 0) {
TLOGE("failed to wait for outgoing queue to free up\n");
return rc;
}
if (ev.event & IPC_HANDLE_POLL_SEND_UNBLOCKED) {
return send_msg(session, msg);
}
if (ev.event & IPC_HANDLE_POLL_MSG) {
return ERR_BUSY;
}
if (ev.event & IPC_HANDLE_POLL_HUP) {
return ERR_CHANNEL_CLOSED;
}
return rc;
}
int sync_ipc_send_msg(handle_t session, iovec_t *tx_iovecs, size_t tx_iovec_count,
iovec_t *rx_iovecs, size_t rx_iovec_count)
{
struct ipc_msg tx_msg = {
.iov = tx_iovecs,
.num_iov = tx_iovec_count,
};
long rc = send_msg(session, &tx_msg);
if (rc == ERR_NOT_ENOUGH_BUFFER) {
rc = wait_to_send(session, &tx_msg);
}
if (rc < 0) {
TLOGE("%s: failed (%ld) to send_msg\n", __func__, rc);
return rc;
}
if (rx_iovecs == NULL || rx_iovec_count == 0) {
assert(rx_iovec_count == 0);
assert(rx_iovecs == NULL);
return NO_ERROR;
}
struct ipc_msg_info inf;
rc = await_response(session, &inf);
if (rc < 0) {
TLOGE("%s: failed (%ld) to await response\n", __func__, rc);
return rc;
}
uint32_t min_len = rx_iovecs[0].len;
if (inf.len < min_len) {
TLOGE("%s: invalid response length (%d)\n", __func__, inf.len);
put_msg(session, inf.id);
return ERR_NOT_VALID;
}
/* calculate total message size */
uint32_t resp_size = 0;
for (size_t i = 0; i < rx_iovec_count; ++i) {
resp_size += rx_iovecs[i].len;
}
if (resp_size < inf.len) {
TLOGE("%s: response buffer too short (%d < %d) \n", __func__,
resp_size, inf.len);
put_msg(session, inf.id);
return ERR_BAD_LEN;
}
rc = read_response(session, inf.id, rx_iovecs, rx_iovec_count);
put_msg(session, inf.id);
if (rc < 0) {
TLOGE("%s: response has error (%ld)\n", __func__, rc);
return rc;
}
size_t read_len = (size_t) rc;
if (read_len != inf.len) {
// data read in does not match message length
TLOGE("%s: invalid response length (%zd)\n", __func__, read_len);
return ERR_IO;
}
return read_len;
}
static void dispatch_event(const uevent_t *ev)
{
assert(ev);
if (ev->event == IPC_HANDLE_POLL_NONE) {
return;
}
struct ipc_context *context = ev->cookie;
assert(context);
assert(context->evt_handler);
context->evt_handler(context, ev);
}
int ipc_port_create(struct ipc_port_context *ctxp, const char *port_name,
size_t queue_size, size_t max_buffer_size, uint32_t flags)
{
int rc;
assert(ctxp);
assert(is_valid_port_ops(&ctxp->ops));
rc = port_create(port_name, queue_size, max_buffer_size, flags);
if (rc < 0) {
TLOGE("Failed to create port %s %d\n", port_name, rc);
return rc;
}
handle_t port_handle = (handle_t) rc;
rc = set_cookie(port_handle, ctxp);
if (rc < 0) {
TLOGE("Failed to set cookie on port %s (%d)\n", port_name, rc);
goto err_set_cookie;
}
rc = maybe_grow_msg_buf(max_buffer_size);
if (rc < 0) {
TLOGE("Failed to create msg buffer of size %zu (%d)\n",
max_buffer_size, rc);
goto err_grow_msg;
}
ctxp->common.handle = port_handle;
ctxp->common.evt_handler = handle_port;
return NO_ERROR;
err_set_cookie:
err_grow_msg:
close(port_handle);
return rc;
}
int ipc_port_destroy(struct ipc_port_context *ctx)
{
close(ctx->common.handle);
return NO_ERROR;
}
void ipc_loop(void)
{
int rc;
uevent_t event;
while (true) {
event.handle = INVALID_IPC_HANDLE;
event.event = 0;
event.cookie = NULL;
rc = wait_any(&event, -1);
if (rc < 0) {
TLOGE("wait_any failed (%d)\n", rc);
break;
}
if (rc == NO_ERROR) { /* got an event */
dispatch_event(&event);
}
}
}