blob: c8c1dffc23172284e32a39988f48a74d4fb948c2 [file] [log] [blame]
Jeremy Kerr3d36ee22019-05-30 11:15:37 +08001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +08003#define _GNU_SOURCE
4
5#include <assert.h>
6#include <err.h>
7#include <getopt.h>
8#include <poll.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <unistd.h>
13
14#include <sys/socket.h>
15#include <sys/un.h>
16
17#include "libmctp.h"
18#include "libmctp-serial.h"
19#include "libmctp-astlpc.h"
20
21#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
22
23static const mctp_eid_t local_eid_default = 8;
24static char sockname[] = "\0mctp-mux";
25
26struct binding {
27 const char *name;
28 int (*init)(struct mctp *mctp, struct binding *binding,
29 mctp_eid_t eid, int n_params,
30 char * const * params);
31 int (*get_fd)(struct binding *binding);
32 int (*process)(struct binding *binding);
33 void *data;
34};
35
36struct client {
37 bool active;
38 int sock;
39 uint8_t type;
40};
41
42struct ctx {
43 struct mctp *mctp;
44 struct binding *binding;
45 bool verbose;
46 int local_eid;
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +080047 void *buf;
48 size_t buf_size;
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +080049
50 int sock;
51 struct pollfd *pollfds;
52
53 struct client *clients;
54 int n_clients;
55};
56
57static void tx_message(struct ctx *ctx, mctp_eid_t eid, void *msg, size_t len)
58{
59 mctp_message_tx(ctx->mctp, eid, msg, len);
60}
61
62static void client_remove_inactive(struct ctx *ctx)
63{
64 int i;
65
66 for (i = 0; i < ctx->n_clients; i++) {
67 struct client *client = &ctx->clients[i];
68 if (client->active)
69 continue;
70 close(client->sock);
71
72 ctx->n_clients--;
73 memmove(&ctx->clients[i], &ctx->clients[i+1],
74 (ctx->n_clients - i) * sizeof(*ctx->clients));
75 ctx->clients = realloc(ctx->clients,
76 ctx->n_clients * sizeof(*ctx->clients));
77 }
78}
79
80static void rx_message(uint8_t eid, void *data, void *msg, size_t len)
81{
82 struct ctx *ctx = data;
83 struct iovec iov[2];
84 struct msghdr msghdr;
85 bool removed;
86 uint8_t type;
87 int i, rc;
88
89 if (len < 2)
90 return;
91
92 type = *(uint8_t *)msg;
93
94 if (ctx->verbose)
95 fprintf(stderr, "MCTP message received: len %zd, type %d\n",
96 len, type);
97
98 memset(&msghdr, 0, sizeof(msghdr));
99 msghdr.msg_iov = iov;
100 msghdr.msg_iovlen = 2;
101 iov[0].iov_base = &eid;
102 iov[0].iov_len = 1;
103 iov[1].iov_base = msg;
104 iov[1].iov_len = len;
105
106 for (i = 0; i < ctx->n_clients; i++) {
107 struct client *client = &ctx->clients[i];
108
109 if (client->type != type)
110 continue;
111
112 if (ctx->verbose)
113 fprintf(stderr, " forwarding to client %d\n", i);
114
115 rc = sendmsg(client->sock, &msghdr, 0);
116 if (rc != (ssize_t)(len + 1)) {
117 client->active = false;
118 removed = true;
119 }
120 }
121
122 if (removed)
123 client_remove_inactive(ctx);
124
125}
126
127static int binding_serial_init(struct mctp *mctp, struct binding *binding,
128 mctp_eid_t eid, int n_params, char * const *params)
129{
130 struct mctp_binding_serial *serial;
131 const char *path;
132 int rc;
133
134 if (n_params != 1) {
135 warnx("serial binding requires device param");
136 return -1;
137 }
138
139 path = params[0];
140
141 serial = mctp_serial_init();
142 assert(serial);
143
144 rc = mctp_serial_open_path(serial, path);
145 if (rc)
146 return -1;
147
148 mctp_serial_register_bus(serial, mctp, eid);
149
150 binding->data = serial;
151
152 return 0;
153}
154
155static int binding_serial_get_fd(struct binding *binding)
156{
157 return mctp_serial_get_fd(binding->data);
158}
159
160static int binding_serial_process(struct binding *binding)
161{
162 return mctp_serial_read(binding->data);
163}
164
165static int binding_astlpc_init(struct mctp *mctp, struct binding *binding,
166 mctp_eid_t eid, int n_params,
167 char * const *params __attribute__((unused)))
168{
169 struct mctp_binding_astlpc *astlpc;
170
171 if (n_params) {
172 warnx("astlpc binding does not accept parameters");
173 return -1;
174 }
175
176 astlpc = mctp_astlpc_init();
177 if (!astlpc) {
178 warnx("could not initialise astlpc binding");
179 return -1;
180 }
181
182 mctp_astlpc_register_bus(astlpc, mctp, eid);
183 binding->data = astlpc;
184 return 0;
185}
186
187static int binding_astlpc_get_fd(struct binding *binding)
188{
189 return mctp_astlpc_get_fd(binding->data);
190}
191
192static int binding_astlpc_process(struct binding *binding)
193{
194 return mctp_astlpc_poll(binding->data);
195}
196
197struct binding bindings[] = {
198 {
199 .name = "serial",
200 .init = binding_serial_init,
201 .get_fd = binding_serial_get_fd,
202 .process = binding_serial_process,
203 },
204 {
205 .name = "astlpc",
206 .init = binding_astlpc_init,
207 .get_fd = binding_astlpc_get_fd,
208 .process = binding_astlpc_process,
209 }
210};
211
212struct binding *binding_lookup(const char *name)
213{
214 struct binding *binding;
215 unsigned int i;
216
217 for (i = 0; i < ARRAY_SIZE(bindings); i++) {
218 binding = &bindings[i];
219
220 if (!strcmp(binding->name, name))
221 return binding;
222 }
223
224 return NULL;
225}
226
227static int socket_init(struct ctx *ctx)
228{
229 struct sockaddr_un addr;
230 int namelen, rc;
231
232 namelen = sizeof(sockname) - 1;
233 addr.sun_family = AF_UNIX;
234 memcpy(addr.sun_path, sockname, namelen);
235
236 ctx->sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
237 if (ctx->sock < 0) {
238 warn("can't create socket");
239 return -1;
240 }
241
242 rc = bind(ctx->sock, (struct sockaddr *)&addr,
243 sizeof(addr.sun_family) + namelen);
244 if (rc) {
245 warn("can't bind socket");
246 goto err_close;
247 }
248
249 rc = listen(ctx->sock, 1);
250 if (rc) {
251 warn("can't listen on socket");
252 goto err_close;
253 }
254
255 return 0;
256
257err_close:
258 close(ctx->sock);
259 return -1;
260}
261
262static int socket_process(struct ctx *ctx)
263{
264 struct client *client;
265 int fd;
266
267 fd = accept4(ctx->sock, NULL, 0, SOCK_NONBLOCK);
268 if (fd < 0)
269 return -1;
270
271 ctx->n_clients++;
272 ctx->clients = realloc(ctx->clients,
273 ctx->n_clients * sizeof(struct client));
274
275 client = &ctx->clients[ctx->n_clients-1];
276 memset(client, 0, sizeof(client));
277 client->active = true;
278 client->sock = fd;
279
280 return 0;
281}
282
283static int client_process_recv(struct ctx *ctx, int idx)
284{
285 struct client *client = &ctx->clients[idx];
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +0800286 uint8_t eid;
287 ssize_t len;
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800288 int rc;
289
290 /* are we waiting for a type message? */
291 if (!client->type) {
292 uint8_t type;
293 rc = read(client->sock, &type, 1);
294 if (rc <= 0)
295 goto out_close;
296
297 if (type == 0) {
298 rc = -1;
299 goto out_close;
300 }
301 if (ctx->verbose)
302 fprintf(stderr, "client[%d] registered for type %u\n",
303 idx, type);
304 client->type = type;
305 return 0;
306 }
307
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +0800308 len = recv(client->sock, NULL, 0, MSG_PEEK | MSG_TRUNC);
309 if (len < 0) {
310 warn("can't receive(peek) from client");
311 goto out_close;
312 }
313
314 if (len > ctx->buf_size) {
315 void *tmp;
316
317 tmp = realloc(ctx->buf, len);
318 if (!tmp) {
319 warn("can't allocate for incoming message");
320 goto out_close;
321 }
322 ctx->buf = tmp;
323 ctx->buf_size = len;
324 }
325
326 rc = recv(client->sock, ctx->buf, ctx->buf_size, 0);
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800327 if (rc < 0) {
328 warn("can't receive from client");
329 goto out_close;
330 }
331
Jeremy Kerr195a7c52019-06-24 14:24:56 +0800332 if (rc <= 0) {
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800333 rc = -1;
334 goto out_close;
335 }
336
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +0800337 eid = *(uint8_t *)ctx->buf;
Jeremy Kerrd690d8e2019-08-01 21:16:58 +0800338
Jeremy Kerr195a7c52019-06-24 14:24:56 +0800339 if (ctx->verbose)
340 fprintf(stderr,
341 "client[%d] sent message: dest 0x%02x len %d\n",
342 idx, eid, rc - 1);
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800343
Jeremy Kerr195a7c52019-06-24 14:24:56 +0800344
345 if (eid == ctx->local_eid)
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +0800346 rx_message(eid, ctx, ctx->buf + 1, rc - 1);
Jeremy Kerr195a7c52019-06-24 14:24:56 +0800347 else
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +0800348 tx_message(ctx, eid, ctx->buf + 1, rc - 1);
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800349
350 return 0;
351
352out_close:
353 client->active = false;
354 return rc;
355}
356
357static int binding_init(struct ctx *ctx, const char *name,
358 int argc, char * const *argv)
359{
360 int rc;
361
362 ctx->binding = binding_lookup(name);
363 if (!ctx->binding) {
364 warnx("no such binding '%s'", name);
365 return -1;
366 }
367
368 rc = ctx->binding->init(ctx->mctp, ctx->binding, ctx->local_eid,
369 argc, argv);
370 return rc;
371}
372
373enum {
374 FD_BINDING = 0,
375 FD_SOCKET,
376 FD_NR,
377};
378
379static int run_daemon(struct ctx *ctx)
380{
381 bool clients_changed = false;
382 int rc, i;
383
384 ctx->pollfds = malloc(FD_NR * sizeof(struct pollfd));
385
386 ctx->pollfds[FD_BINDING].fd =
387 ctx->binding->get_fd(ctx->binding);
388 ctx->pollfds[FD_BINDING].events = POLLIN;
389
390 ctx->pollfds[FD_SOCKET].fd = ctx->sock;
391 ctx->pollfds[FD_SOCKET].events = POLLIN;
392
393 mctp_set_rx_all(ctx->mctp, rx_message, ctx);
394
395 for (;;) {
396 if (clients_changed) {
397 int i;
398
399 ctx->pollfds = realloc(ctx->pollfds,
400 (ctx->n_clients + FD_NR) *
401 sizeof(struct pollfd));
402
403 for (i = 0; i < ctx->n_clients; i++) {
404 ctx->pollfds[FD_NR+i].fd =
405 ctx->clients[i].sock;
406 ctx->pollfds[FD_NR+i].events = POLLIN;
407 }
408 clients_changed = false;
409 }
410
411 rc = poll(ctx->pollfds, ctx->n_clients + FD_NR, -1);
412 if (rc < 0) {
413 warn("poll failed");
414 break;
415 }
416
417 if (!rc)
418 continue;
419
420 if (ctx->pollfds[FD_BINDING].revents) {
421 rc = ctx->binding->process(ctx->binding);
422 if (rc)
423 break;
424 }
425
426 for (i = 0; i < ctx->n_clients; i++) {
427 if (!ctx->pollfds[FD_NR+i].revents)
428 continue;
429
430 rc = client_process_recv(ctx, i);
431 if (rc)
432 clients_changed = true;
433 }
434
435 if (ctx->pollfds[FD_SOCKET].revents) {
436 rc = socket_process(ctx);
437 if (rc)
438 break;
439 clients_changed = true;
440 }
441
442 if (clients_changed)
443 client_remove_inactive(ctx);
444
445 }
446
447
448 free(ctx->pollfds);
449
450 return rc;
451}
452
453static const struct option options[] = {
454 { "verbose", no_argument, 0, 'v' },
455 { "eid", required_argument, 0, 'e' },
456 { 0 },
457};
458
459static void usage(const char *progname)
460{
461 unsigned int i;
462
463 fprintf(stderr, "usage: %s <binding> [params]\n", progname);
464 fprintf(stderr, "Available bindings:\n");
465 for (i = 0; i < ARRAY_SIZE(bindings); i++)
466 fprintf(stderr, " %s\n", bindings[i].name);
467}
468
469int main(int argc, char * const *argv)
470{
471 struct ctx *ctx, _ctx;
472 int rc;
473
474 ctx = &_ctx;
475 ctx->clients = NULL;
476 ctx->n_clients = 0;
477 ctx->local_eid = local_eid_default;
478
479 for (;;) {
480 rc = getopt_long(argc, argv, "e:v", options, NULL);
481 if (rc == -1)
482 break;
483 switch (rc) {
484 case 'v':
485 ctx->verbose = true;
486 break;
487 case 'e':
488 ctx->local_eid = atoi(optarg);
489 break;
490 default:
491 fprintf(stderr, "Invalid argument\n");
492 return EXIT_FAILURE;
493 }
494 }
495
496 if (optind >= argc) {
497 fprintf(stderr, "missing binding argument\n");
498 usage(argv[0]);
499 return EXIT_FAILURE;
500 }
501
Jeremy Kerrf49b2ac2019-08-02 15:10:51 +0800502 /* setup initial buffer */
503 ctx->buf_size = 4096;
504 ctx->buf = malloc(ctx->buf_size);
505
Jeremy Kerr0b278a62019-05-30 21:27:21 +0800506 mctp_set_log_stdio(ctx->verbose ? MCTP_LOG_DEBUG : MCTP_LOG_WARNING);
507
Jeremy Kerr8e31cfd2019-05-07 17:13:51 +0800508 ctx->mctp = mctp_init();
509 assert(ctx->mctp);
510
511 rc = binding_init(ctx, argv[optind], argc - optind - 1, argv + optind + 1);
512 if (rc)
513 return EXIT_FAILURE;
514
515 rc = socket_init(ctx);
516 if (rc)
517 return EXIT_FAILURE;
518
519 rc = run_daemon(ctx);
520
521 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
522
523}