aboutsummaryrefslogtreecommitdiff
path: root/helper/cli.c
blob: 0503da230de47b3a8ce636bbf5c2b23877dc4bb4 (plain)
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/* Copyright (c) 2021, Nokia
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp/helper/cli.h>
#include <odp_api.h>
#include <odp/helper/odph_api.h>
#include <libcli.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <errno.h>
#include <poll.h>
#include <stdio.h>
#include <strings.h>

/* Socketpair socket roles. */
enum {
	SP_SERVER = 0,
	SP_CONTROL = 1,
};

#define MAX_NAME_LEN 20
#define MAX_HELP_LEN 100

typedef struct {
	odph_cli_user_cmd_func_t fn;
	char name[MAX_NAME_LEN];
	char help[MAX_HELP_LEN];
} user_cmd_t;

typedef struct {
	volatile int cli_fd;
	/* Server will exit if this is false. */
	volatile int run;
	/* Socketpair descriptors. */
	int sp[2];
	int listen_fd;
	/* Guards cli_fd and run, which must be accessed atomically. */
	odp_spinlock_t lock;
	odp_spinlock_t api_lock;
	odph_cli_param_t cli_param;
	struct sockaddr_in addr;
	uint32_t num_user_commands;
	user_cmd_t user_cmd[];
} cli_shm_t;

static const char *shm_name = "_odp_cli";

static const odph_cli_param_t param_default = {
	.address = "127.0.0.1",
	.port = 55555,
	.max_user_commands = 50,
	.hostname = "ODP",
};

void odph_cli_param_init(odph_cli_param_t *param)
{
	*param = param_default;
}

static cli_shm_t *shm_lookup(void)
{
	cli_shm_t *shm = NULL;
	odp_shm_t shm_hdl = odp_shm_lookup(shm_name);

	if (shm_hdl != ODP_SHM_INVALID)
		shm = (cli_shm_t *)odp_shm_addr(shm_hdl);

	return shm;
}

int odph_cli_init(const odph_cli_param_t *param)
{
	if (odp_shm_lookup(shm_name) != ODP_SHM_INVALID) {
		ODPH_ERR("Error: shm %s already exists\n", shm_name);
		return -1;
	}

	cli_shm_t *shm = NULL;
	int shm_size = sizeof(cli_shm_t) +
		param->max_user_commands * sizeof(user_cmd_t);
	odp_shm_t shm_hdl =
		odp_shm_reserve(shm_name, shm_size, 64, 0);

	if (shm_hdl != ODP_SHM_INVALID)
		shm = (cli_shm_t *)odp_shm_addr(shm_hdl);

	if (!shm) {
		ODPH_ERR("Error: failed to reserve shm %s\n", shm_name);
		return -1;
	}

	memset(shm, 0, shm_size);
	odp_spinlock_init(&shm->lock);
	odp_spinlock_init(&shm->api_lock);
	shm->listen_fd = -1;
	shm->cli_fd = -1;

	shm->addr.sin_family = AF_INET;
	shm->addr.sin_port = htons(param->port);

	switch (inet_pton(AF_INET, param->address, &shm->addr.sin_addr)) {
	case -1:
		ODPH_ERR("Error: inet_pton(): %s\n", strerror(errno));
		return -1;
	case 0:
		ODPH_ERR("Error: inet_pton(): illegal address format\n");
		return -1;
	default:
		break;
	}

	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, shm->sp)) {
		ODPH_ERR("Error: socketpair(): %s\n", strerror(errno));
		return -1;
	}

	shm->cli_param = *param;

	return 0;
}

int odph_cli_register_command(const char *name, odph_cli_user_cmd_func_t func,
			      const char *help)
{
	cli_shm_t *shm = shm_lookup();

	if (!shm) {
		ODPH_ERR("Error: shm %s not found\n", shm_name);
		return -1;
	}

	odp_spinlock_lock(&shm->api_lock);

	odp_spinlock_lock(&shm->lock);
	if (shm->run) {
		odp_spinlock_unlock(&shm->lock);
		ODPH_ERR("Error: cannot register commands while cli server is running\n");
		goto error;
	}
	odp_spinlock_unlock(&shm->lock);

	if (shm->num_user_commands >= shm->cli_param.max_user_commands) {
		ODPH_ERR("Error: maximum number of user commands already registered\n");
		goto error;
	}

	user_cmd_t *cmd = &shm->user_cmd[shm->num_user_commands];

	cmd->fn = func;

	if (strlen(name) >= MAX_NAME_LEN - 1) {
		ODPH_ERR("Error: command name too long\n");
		goto error;
	}
	strcpy(cmd->name, name);

	if (strlen(help) >= MAX_HELP_LEN - 1) {
		ODPH_ERR("Error: command help too long\n");
		goto error;
	}
	strcpy(cmd->help, help);

	shm->num_user_commands++;
	odp_spinlock_unlock(&shm->api_lock);
	return 0;

error:
	odp_spinlock_unlock(&shm->api_lock);
	return -1;
}

/*
 * Check that number of given arguments matches required number of
 * arguments. Print error messages if this is not the case. Return 0
 * on success, -1 otherwise.
 */
static int check_num_args(struct cli_def *cli, int argc, int req_argc)
{
	if (argc < req_argc) {
		cli_error(cli, "%% Incomplete command.");
		return -1;
	}

	if (argc > req_argc) {
		cli_error(cli, "%% Extra parameter given to command.");
		return -1;
	}

	return 0;
}

static int cmd_call_odp_cls_print_all(struct cli_def *cli,
				      const char *command ODP_UNUSED,
				      char *argv[] ODP_UNUSED, int argc)
{
	if (check_num_args(cli, argc, 0))
		return CLI_ERROR;

	odp_cls_print_all();

	return CLI_OK;
}

static int cmd_call_odp_ipsec_print(struct cli_def *cli,
				    const char *command ODP_UNUSED,
				    char *argv[] ODP_UNUSED, int argc)
{
	if (check_num_args(cli, argc, 0))
		return CLI_ERROR;

	odp_ipsec_print();

	return CLI_OK;
}

static int cmd_call_odp_shm_print_all(struct cli_def *cli,
				      const char *command ODP_UNUSED,
				      char *argv[] ODP_UNUSED, int argc)
{
	if (check_num_args(cli, argc, 0))
		return CLI_ERROR;

	odp_shm_print_all();

	return CLI_OK;
}

static int cmd_call_odp_sys_config_print(struct cli_def *cli,
					 const char *command ODP_UNUSED,
					 char *argv[] ODP_UNUSED, int argc)
{
	if (check_num_args(cli, argc, 0))
		return CLI_ERROR;

	odp_sys_config_print();

	return CLI_OK;
}

static int cmd_call_odp_sys_info_print(struct cli_def *cli,
				       const char *command ODP_UNUSED,
				       char *argv[] ODP_UNUSED, int argc)
{
	if (check_num_args(cli, argc, 0))
		return CLI_ERROR;

	odp_sys_info_print();

	return CLI_OK;
}

static int cmd_call_odp_pktio_print(struct cli_def *cli,
				    const char *command ODP_UNUSED,
				    char *argv[], int argc)
{
	if (check_num_args(cli, argc, 1))
		return CLI_ERROR;

	odp_pktio_t hdl = odp_pktio_lookup(argv[0]);

	if (hdl == ODP_PKTIO_INVALID) {
		cli_error(cli, "%% Name not found.");
		return CLI_ERROR;
	}

	odp_pktio_print(hdl);

	return CLI_OK;
}

static int cmd_call_odp_pool_print(struct cli_def *cli,
				   const char *command ODP_UNUSED, char *argv[],
				   int argc)
{
	if (check_num_args(cli, argc, 1))
		return CLI_ERROR;

	odp_pool_t hdl = odp_pool_lookup(argv[0]);

	if (hdl == ODP_POOL_INVALID) {
		cli_error(cli, "%% Name not found.");
		return CLI_ERROR;
	}

	odp_pool_print(hdl);

	return CLI_OK;
}

static int cmd_call_odp_queue_print(struct cli_def *cli,
				    const char *command ODP_UNUSED,
				    char *argv[], int argc)
{
	if (check_num_args(cli, argc, 1))
		return CLI_ERROR;

	odp_queue_t hdl = odp_queue_lookup(argv[0]);

	if (hdl == ODP_QUEUE_INVALID) {
		cli_error(cli, "%% Name not found.");
		return CLI_ERROR;
	}

	odp_queue_print(hdl);

	return CLI_OK;
}

static int cmd_call_odp_queue_print_all(struct cli_def *cli,
					const char *command ODP_UNUSED,
					char *argv[] ODP_UNUSED, int argc)
{
	if (check_num_args(cli, argc, 0))
		return CLI_ERROR;

	odp_queue_print_all();

	return CLI_OK;
}

static int cmd_call_odp_shm_print(struct cli_def *cli,
				  const char *command ODP_UNUSED, char *argv[],
				  int argc)
{
	if (check_num_args(cli, argc, 1))
		return CLI_ERROR;

	odp_shm_t hdl = odp_shm_lookup(argv[0]);

	if (hdl == ODP_SHM_INVALID) {
		cli_error(cli, "%% Name not found.");
		return CLI_ERROR;
	}

	odp_shm_print(hdl);

	return CLI_OK;
}

static int cmd_user_cmd(struct cli_def *cli ODP_UNUSED, const char *command,
			char *argv[], int argc)
{
	cli_shm_t *shm = shm_lookup();

	if (!shm) {
		ODPH_ERR("Error: shm %s not found\n", shm_name);
		return CLI_ERROR;
	}

	for (uint32_t i = 0; i < shm->num_user_commands; i++) {
		if (!strcasecmp(command, shm->user_cmd[i].name)) {
			shm->user_cmd[i].fn(argc, argv);
			break;
		}
	}

	return CLI_OK;
}

static struct cli_def *create_cli(cli_shm_t *shm)
{
	struct cli_command *c;
	struct cli_def *cli;

	cli = cli_init();
	cli_set_banner(cli, NULL);
	cli_set_hostname(cli, shm->cli_param.hostname);

	c = cli_register_command(cli, NULL, "call", NULL,
				 PRIVILEGE_UNPRIVILEGED, MODE_EXEC,
				 "Call ODP API function.");
	cli_register_command(cli, c, "odp_cls_print_all",
			     cmd_call_odp_cls_print_all,
			     PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
	cli_register_command(cli, c, "odp_ipsec_print",
			     cmd_call_odp_ipsec_print,
			     PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
	cli_register_command(cli, c, "odp_pktio_print",
			     cmd_call_odp_pktio_print,
			     PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "<name>");
	cli_register_command(cli, c, "odp_pool_print",
			     cmd_call_odp_pool_print,
			     PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "<name>");
	cli_register_command(cli, c, "odp_queue_print",
			     cmd_call_odp_queue_print,
			     PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "<name>");
	cli_register_command(cli, c, "odp_queue_print_all",
			     cmd_call_odp_queue_print_all,
			     PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
	cli_register_command(cli, c, "odp_shm_print_all",
			     cmd_call_odp_shm_print_all,
			     PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
	cli_register_command(cli, c, "odp_shm_print",
			     cmd_call_odp_shm_print,
			     PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "<name>");
	cli_register_command(cli, c, "odp_sys_config_print",
			     cmd_call_odp_sys_config_print,
			     PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);
	cli_register_command(cli, c, "odp_sys_info_print",
			     cmd_call_odp_sys_info_print,
			     PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL);

	for (uint32_t i = 0; i < shm->num_user_commands; i++) {
		cli_register_command(cli, NULL, shm->user_cmd[i].name,
				     cmd_user_cmd, PRIVILEGE_UNPRIVILEGED,
				     MODE_EXEC, shm->user_cmd[i].help);
	}

	return cli;
}

/* Not shared, used only in the server thread. */
static struct cli_def *cli;
static char *cli_log_fn_buf;

ODP_PRINTF_FORMAT(2, 0)
static int cli_log_va(odp_log_level_t level, const char *fmt, va_list in_args)
{
	(void)level;

	va_list args;
	char *str, *p, *last;
	int len;

	/*
	 * This function should be just a simple call to cli_vabufprint().
	 * Unfortunately libcli (at least versions 1.9.7 - 1.10.4) has a few
	 * bugs. cli_print() prints a newline at the end even if the string
	 * doesn't end in a newline. cli_*bufprint() on the other hand just
	 * throws away everything after the last newline.
	 *
	 * The following code ensures that each cli_*print() ends in a newline.
	 * If the string does not end in a newline, we keep the part of the
	 * string after the last newline and use it the next time we're called.
	 */
	va_copy(args, in_args);
	len = vsnprintf(NULL, 0, fmt, args) + 1;
	va_end(args);
	str = malloc(len);

	if (!str) {
		ODPH_ERR("malloc failed\n");
		return -1;
	}

	va_copy(args, in_args);
	vsnprintf(str, len, fmt, args);
	va_end(args);
	p = str;
	last = strrchr(p, '\n');

	if (last) {
		*last++ = 0;
		if (cli_log_fn_buf) {
			cli_bufprint(cli, "%s%s\n", cli_log_fn_buf, p);
			free(cli_log_fn_buf);
			cli_log_fn_buf = NULL;
		} else {
			cli_bufprint(cli, "%s\n", p);
		}
		p = last;
	}

	if (*p) {
		if (cli_log_fn_buf) {
			char *buffer_new =
				malloc(strlen(cli_log_fn_buf) + strlen(p) + 1);

			if (!buffer_new) {
				ODPH_ERR("malloc failed\n");
				goto out;
			}

			strcpy(buffer_new, cli_log_fn_buf);
			strcat(buffer_new, p);
			free(cli_log_fn_buf);
			cli_log_fn_buf = buffer_new;
		} else {
			cli_log_fn_buf = malloc(strlen(p) + 1);

			if (!cli_log_fn_buf) {
				ODPH_ERR("malloc failed\n");
				goto out;
			}

			strcpy(cli_log_fn_buf, p);
		}
	}

out:
	free(str);

	return len;
}

ODP_PRINTF_FORMAT(2, 3)
static int cli_log(odp_log_level_t level, const char *fmt, ...)
{
	(void)level;

	int r;
	va_list args;

	va_start(args, fmt);
	r = cli_log_va(level, fmt, args);
	va_end(args);

	return r;
}

ODP_PRINTF_FORMAT(1, 2)
int odph_cli_log(const char *fmt, ...)
{
	int r;
	va_list args;

	va_start(args, fmt);
	r = cli_log_va(ODP_LOG_PRINT, fmt, args);
	va_end(args);

	return r;
}

ODP_PRINTF_FORMAT(1, 0)
int odph_cli_log_va(const char *fmt, va_list in_args)
{
	int r;

	r = cli_log_va(ODP_LOG_PRINT, fmt, in_args);
	return r;
}

static int msg_recv(int fd)
{
	uint32_t msg;
	int num = recv(fd, &msg, sizeof(msg), MSG_NOSIGNAL);

	if (num != sizeof(msg)) {
		ODPH_ERR("Error: recv() = %d: %s\n", num, strerror(errno));
		return -1;
	}

	return 0;
}

static int msg_send(int fd)
{
	uint32_t msg = 0;
	int num = send(fd, &msg, sizeof(msg), MSG_DONTWAIT | MSG_NOSIGNAL);

	if (num != sizeof(msg)) {
		ODPH_ERR("Error: send() = %d: %s\n", num, strerror(errno));
		return -1;
	}

	return 0;
}

static int cli_server(cli_shm_t *shm)
{
	cli = create_cli(shm);

	while (1) {
		struct pollfd pfd[2] = {
			{ .fd = shm->sp[SP_SERVER], .events = POLLIN, },
			{ .fd = shm->listen_fd, .events = POLLIN, },
		};

		if (poll(pfd, 2, -1) < 0) {
			ODPH_ERR("Error: poll(): %s\n", strerror(errno));
			break;
		}

		/*
		 * If we have an event on a socketpair socket, it's
		 * time to exit.
		 */
		if (pfd[0].revents)
			break;

		/*
		 * If we don't have an event on the listening socket, poll
		 * again.
		 */
		if (!pfd[1].revents)
			continue;

		int fd = accept(shm->listen_fd, NULL, 0);

		if (fd < 0) {
			if (errno == EAGAIN || errno == EINTR)
				continue;

			ODPH_ERR("Error: accept(): %s\n", strerror(errno));
			break;
		}

		/*
		 * The only way to stop cli_loop() is to close the socket, after
		 * which cli_loop() gets an error on the next select() and then
		 * calls close() before returning. This is a problem because the
		 * fd may be reused before the select() or the final close().
		 *
		 * To avoid this problem, switch to a higher fd number
		 * (select() maximum). We will still run into problems if the
		 * descriptor numbers in the process reach FD_SETSIZE - 1 =
		 * 1023.
		 */
		int newfd = dup2(fd, FD_SETSIZE - 1);

		if (newfd < 0) {
			ODPH_ERR("Error: dup2(): %s\n", strerror(errno));
			close(fd);
			continue;
		}

		close(fd);
		fd = newfd;

		odp_spinlock_lock(&shm->lock);
		if (!shm->run) {
			odp_spinlock_unlock(&shm->lock);
			/*
			 * odph_cli_stop() has been called. Close the
			 * socket we just accepted and exit.
			 */
			close(fd);
			break;
		}
		shm->cli_fd = fd;
		odp_spinlock_unlock(&shm->lock);

		odp_log_thread_fn_set(cli_log);
		/*
		 * cli_loop() returns only when client is disconnected. One
		 * possible reason for disconnect is odph_cli_stop().
		 */
		cli_loop(cli, shm->cli_fd);
		odp_log_thread_fn_set(NULL);

		odp_spinlock_lock(&shm->lock);
		/*
		 * cli_loop() closes the socket before returning (undocumented).
		 */
		shm->cli_fd = -1;
		odp_spinlock_unlock(&shm->lock);

		/*
		 * Throw away anything left in the buffer (in case the last
		 * print didn't end in a newline).
		 */
		free(cli_log_fn_buf);
		cli_log_fn_buf = NULL;
	}

	cli_done(cli);

	if (msg_send(shm->sp[SP_SERVER]))
		return -1;

	return 0;
}

int odph_cli_run(void)
{
	cli_shm_t *shm = shm_lookup();

	if (!shm) {
		ODPH_ERR("Error: shm %s not found\n", shm_name);
		return -1;
	}

	odp_spinlock_lock(&shm->api_lock);
	odp_spinlock_lock(&shm->lock);
	if (shm->run) {
		odp_spinlock_unlock(&shm->lock);
		odp_spinlock_unlock(&shm->api_lock);
		ODPH_ERR("Error: cli server is already running\n");
		return -1;
	}
	shm->run = 1;
	shm->cli_fd = -1;
	odp_spinlock_unlock(&shm->lock);

	/* Create listening socket. */

	shm->listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (shm->listen_fd < 0) {
		ODPH_ERR("Error: socket(): %s\n", strerror(errno));
		goto error;
	}

	int on = 1;

	if (setsockopt(shm->listen_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
		ODPH_ERR("Error: setsockopt(): %s\n", strerror(errno));
		goto error;
	}

	if (bind(shm->listen_fd, (struct sockaddr *)&shm->addr,
		 sizeof(shm->addr))) {
		ODPH_ERR("Error: bind(): %s\n", strerror(errno));
		goto error;
	}

	if (listen(shm->listen_fd, 1)) {
		ODPH_ERR("Error: listen(): %s\n", strerror(errno));
		goto error;
	}

	odp_spinlock_unlock(&shm->api_lock);

	return cli_server(shm);

error:
	shm->run = 0;
	if (shm->listen_fd >= 0)
		close(shm->listen_fd);
	if (shm->cli_fd >= 0)
		close(shm->cli_fd);
	odp_spinlock_unlock(&shm->api_lock);
	return -1;
}

int odph_cli_stop(void)
{
	cli_shm_t *shm = shm_lookup();

	if (!shm) {
		ODPH_ERR("Error: shm %s not found\n", shm_name);
		return -1;
	}

	odp_spinlock_lock(&shm->api_lock);
	odp_spinlock_lock(&shm->lock);
	if (!shm->run) {
		odp_spinlock_unlock(&shm->lock);
		odp_spinlock_unlock(&shm->api_lock);
		ODPH_ERR("Error: cli server has not been started\n");
		return -1;
	}
	shm->run = 0;
	/*
	 * Close the current cli connection. This stops cli_loop(). If cli
	 * client is disconnecting at the same time, cli_fd may already have
	 * been closed.
	 */
	if (shm->cli_fd >= 0) {
		close(shm->cli_fd);
		shm->cli_fd = -1;
	}
	odp_spinlock_unlock(&shm->lock);

	/*
	 * Send a message to the server thread in order to break it out of a
	 * blocking poll() call.
	 */
	if (msg_send(shm->sp[SP_CONTROL]))
		goto error;

	/*
	 * Wait for the server to exit.
	 */
	if (msg_recv(shm->sp[SP_CONTROL]))
		goto error;

	close(shm->listen_fd);
	odp_spinlock_unlock(&shm->api_lock);
	return 0;

error:
	odp_spinlock_unlock(&shm->api_lock);
	return -1;
}

int odph_cli_term(void)
{
	cli_shm_t *shm = NULL;
	odp_shm_t shm_hdl = odp_shm_lookup(shm_name);

	if (shm_hdl != ODP_SHM_INVALID)
		shm = (cli_shm_t *)odp_shm_addr(shm_hdl);

	if (!shm) {
		ODPH_ERR("Error: shm %s not found\n", shm_name);
		return -1;
	}

	close(shm->sp[SP_SERVER]);
	close(shm->sp[SP_CONTROL]);

	if (odp_shm_free(shm_hdl)) {
		ODPH_ERR("Error: odp_shm_free() failed\n");
		return -1;
	}

	return 0;
}