MOS Source Code
Loading...
Searching...
No Matches
ipc_io.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2// io_t wrapper for IPC channels
3
4#include "mos/ipc/ipc_io.h"
5
6#include "mos/io/io.h"
7#include "mos/ipc/ipc.h"
10
11#include <mos_stdlib.h>
12
13typedef struct _ipc_server_io
14{
18
21
24
25static void ipc_control_io_close(io_t *io)
26{
27 if (io->type != IO_IPC)
28 mos_panic("ipc_control_io_close: io->type != IO_IPC"); // handle error cases in io_close
29
30 // we only deannounce the server, we don't free it
31 ipc_server_io_t *server_io = container_of(io, ipc_server_io_t, control_io);
32 ipc_server_close(server_io->server);
33 kfree(server_io);
34}
35
36static const io_op_t ipc_control_io_op = {
37 .close = ipc_control_io_close,
38};
39
40static size_t ipc_client_io_write(io_t *io, const void *buf, size_t size)
41{
43 return ipc_client_write(conn->ipc, buf, size);
44}
45
46static size_t ipc_client_io_read(io_t *io, void *buf, size_t size)
47{
49 return ipc_client_read(conn->ipc, buf, size);
50}
51
52static void ipc_client_io_close(io_t *io)
53{
54 if (io->type != IO_IPC)
55 mos_panic("ipc_client_io_close: io->type != IO_IPC"); // handle error cases in io_close
56
59 kfree(conn);
60}
61
62static size_t ipc_server_io_write(io_t *io, const void *buf, size_t size)
63{
65 return ipc_server_write(conn->ipc, buf, size);
66}
67
68static size_t ipc_server_io_read(io_t *io, void *buf, size_t size)
69{
71 return ipc_server_read(conn->ipc, buf, size);
72}
73
74static void ipc_server_io_close(io_t *io)
75{
76 if (io->type != IO_IPC)
77 mos_panic("ipc_server_io_close: io->type != IO_IPC"); // handle error cases in io_close
78
81 kfree(conn);
82}
83
84static const io_op_t ipc_client_io_op = {
85 .read = ipc_client_io_read,
86 .write = ipc_client_io_write,
87 .close = ipc_client_io_close,
88};
89
90static const io_op_t ipc_server_io_op = {
91 .read = ipc_server_io_read,
92 .write = ipc_server_io_write,
93 .close = ipc_server_io_close,
94};
95
96ipc_conn_io_t *ipc_conn_io_create(ipc_t *ipc, bool is_server_side)
97{
98 ipc_conn_io_t *io = kmalloc(ipc_conn_io_slab);
99 if (IS_ERR(io))
100 return ERR(io);
101 io->ipc = ipc;
103 return io;
104}
105
106io_t *ipc_create(const char *name, size_t max_pending_connections)
107{
108 ipc_server_t *server = ipc_server_create(name, max_pending_connections);
109 if (IS_ERR(server))
110 return ERR(server);
111
112 ipc_server_io_t *io = kmalloc(ipc_server_io_slab);
113 if (IS_ERR(io))
114 return ERR(io);
115
116 io->server = server;
118 return &io->control_io;
119}
120
122{
123 if (server->type != IO_IPC)
124 return ERR_PTR(-EBADF); // not an ipc server
125
126 ipc_server_io_t *ipc_server = container_of(server, ipc_server_io_t, control_io);
127 ipc_t *ipc = ipc_server_accept(ipc_server->server);
128 if (IS_ERR(ipc))
129 return ERR(ipc);
130
131 ipc_conn_io_t *io = ipc_conn_io_create(ipc, true);
132 if (IS_ERR(io))
133 return ERR(io);
134
135 return &io->io;
136}
137
138io_t *ipc_connect(const char *name, size_t buffer_size)
139{
140 ipc_t *ipc = ipc_connect_to_server(name, buffer_size);
141 if (IS_ERR(ipc))
142 return ERR(ipc);
143
144 ipc_conn_io_t *connio = ipc_conn_io_create(ipc, false);
145 if (IS_ERR(connio))
146 return ERR(connio);
147
148 return &connio->io;
149}
void io_init(io_t *io, io_type_t type, io_flags_t flags, const io_op_t *ops)
Definition io.c:44
@ IO_READABLE
Definition io.h:27
@ IO_WRITABLE
Definition io.h:28
@ IO_NONE
Definition io.h:26
@ IO_IPC
Definition io.h:19
static slab_t * ipc_conn_io_slab
Definition ipc_io.c:22
static const io_op_t ipc_control_io_op
Definition ipc_io.c:36
ipc_conn_io_t * ipc_conn_io_create(ipc_t *ipc, bool is_server_side)
Create a new IPC connection io descriptor.
Definition ipc_io.c:96
io_t * ipc_connect(const char *name, size_t buffer_size)
Connect to an IPC servers.
Definition ipc_io.c:138
static slab_t * ipc_server_io_slab
Definition ipc_io.c:19
static const io_op_t ipc_client_io_op
Definition ipc_io.c:84
static size_t ipc_server_io_write(io_t *io, const void *buf, size_t size)
Definition ipc_io.c:62
static const io_op_t ipc_server_io_op
Definition ipc_io.c:90
static void ipc_control_io_close(io_t *io)
Definition ipc_io.c:25
io_t * ipc_accept(io_t *server)
Accept a new connection on an IPC server.
Definition ipc_io.c:121
io_t * ipc_create(const char *name, size_t max_pending_connections)
Create a new IPC server.
Definition ipc_io.c:106
static size_t ipc_server_io_read(io_t *io, void *buf, size_t size)
Definition ipc_io.c:68
static void ipc_server_io_close(io_t *io)
Definition ipc_io.c:74
static size_t ipc_client_io_write(io_t *io, const void *buf, size_t size)
Definition ipc_io.c:40
static void ipc_client_io_close(io_t *io)
Definition ipc_io.c:52
static size_t ipc_client_io_read(io_t *io, void *buf, size_t size)
Definition ipc_io.c:46
void ipc_client_close_channel(ipc_t *ipc)
Definition ipc.c:140
size_t ipc_client_write(ipc_t *ipc, const void *buffer, size_t size)
Definition ipc.c:125
size_t ipc_server_write(ipc_t *ipc, const void *buffer, size_t size)
Definition ipc.c:135
void ipc_server_close(ipc_server_t *server)
Definition ipc.c:73
void ipc_server_close_channel(ipc_t *ipc)
Definition ipc.c:155
ipc_t * ipc_server_accept(ipc_server_t *server)
Definition ipc.c:236
ipc_server_t * ipc_server_create(const char *name, size_t max_pending_connections)
Definition ipc.c:177
size_t ipc_client_read(ipc_t *ipc, void *buffer, size_t size)
Definition ipc.c:120
ipc_t * ipc_connect_to_server(const char *name, size_t buffer_size)
Definition ipc.c:291
size_t ipc_server_read(ipc_t *ipc, void *buffer, size_t size)
Definition ipc.c:130
#define container_of(ptr, type, member)
Definition mos_global.h:50
#define mos_panic(fmt,...)
Definition panic.h:55
#define NULL
Definition pb_syshdr.h:46
size_t size
Definition slab.c:30
const char * name
Definition slab.c:31
#define SLAB_AUTOINIT(name, var, type)
Definition io.h:35
Definition io.h:46
io_type_t type
Definition io.h:50
ipc_t * ipc
Definition ipc_io.h:12
io_t control_io
Definition ipc_io.c:15
ipc_server_t * server
Definition ipc_io.c:16
Definition ipc.c:30
Definition slab.h:45