xref: /spdk/test/app/bdev_svc/bdev_svc.c (revision 179ed697b3c461d100e675915d074be717b7b9cc)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "spdk/env.h"
37 #include "spdk/event.h"
38 
39 static char g_path[256];
40 
41 static void
42 usage(char *executable_name)
43 {
44 	printf("%s [options]\n", executable_name);
45 	printf("options:\n");
46 	printf(" -c config  config file [required]\n");
47 	printf(" -i shared memory ID\n");
48 	printf(" -m mask    core mask for DPDK\n");
49 	printf(" -n channel number of memory channels used for DPDK\n");
50 	printf(" -p core    master (primary) core for DPDK\n");
51 	printf(" -s size    memory size in MB for DPDK\n");
52 	printf(" -H         show this usage\n");
53 }
54 
55 static void
56 bdev_svc_start(void *arg1, void *arg2)
57 {
58 	int fd;
59 	int shm_id = (intptr_t)arg1;
60 
61 	spdk_unaffinitize_thread();
62 
63 	snprintf(g_path, sizeof(g_path), "/var/run/spdk_bdev%d", shm_id);
64 	fd = open(g_path, O_CREAT | O_EXCL | O_RDWR, S_IFREG);
65 	if (fd < 0) {
66 		fprintf(stderr, "could not create sentinel file %s\n", g_path);
67 		exit(1);
68 	}
69 	close(fd);
70 }
71 
72 static void
73 bdev_svc_shutdown(void)
74 {
75 	unlink(g_path);
76 	spdk_app_stop(0);
77 }
78 
79 int
80 main(int argc, char **argv)
81 {
82 	int ch, rc;
83 	struct spdk_app_opts opts = {};
84 
85 	/* default value in opts structure */
86 	spdk_app_opts_init(&opts);
87 
88 	opts.name = "bdev_svc";
89 
90 	while ((ch = getopt(argc, argv, "c:i:m:n:p:s:H")) != -1) {
91 		switch (ch) {
92 		case 'c':
93 			opts.config_file = optarg;
94 			break;
95 		case 'i':
96 			opts.shm_id = atoi(optarg);
97 			break;
98 		case 'm':
99 			opts.reactor_mask = optarg;
100 			break;
101 		case 'n':
102 			opts.mem_channel = atoi(optarg);
103 			break;
104 		case 'p':
105 			opts.master_core = atoi(optarg);
106 			break;
107 		case 's':
108 			opts.mem_size = atoi(optarg);
109 			break;
110 		case 'H':
111 		default:
112 			usage(argv[0]);
113 			exit(EXIT_SUCCESS);
114 		}
115 	}
116 
117 	if (!opts.config_file) {
118 		fprintf(stderr, "Configuration file is required.\n");
119 		usage(argv[0]);
120 		exit(1);
121 	}
122 
123 	opts.shutdown_cb = bdev_svc_shutdown;
124 	opts.max_delay_us = 1000 * 1000;
125 
126 	spdk_app_start(&opts, bdev_svc_start, (void *)(intptr_t)opts.shm_id, NULL);
127 
128 	rc = spdk_app_fini();
129 
130 	return rc;
131 }
132