4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #include "spdk/stdinc.h"
35 #include "spdk/thread.h"
37 #include "spdk/event.h"
39 #include "spdk/string.h"
41 #include "spdk/sock.h"
44 #define ACCEPT_TIMEOUT_US 1000
45 #define CLOSE_TIMEOUT_US 1000000
46 #define BUFFER_SIZE 1024
47 #define ADDR_STR_LEN INET6_ADDRSTRLEN
49 static bool g_is_running
;
53 static bool g_is_server
;
54 static bool g_verbose
;
57 * We'll use this struct to gather housekeeping hello_context to pass between
58 * our events and callbacks.
60 struct hello_context_t
{
69 struct spdk_sock
*sock
;
71 struct spdk_sock_group
*group
;
72 struct spdk_poller
*poller_in
;
73 struct spdk_poller
*poller_out
;
74 struct spdk_poller
*time_out
;
78 * Usage function for printing parameters that are specific to this application
81 hello_sock_usage(void)
83 printf(" -H host_addr host address\n");
84 printf(" -P port port number\n");
85 printf(" -S start in server mode\n");
86 printf(" -V print out additional informations");
90 * This function is called to parse the parameters that are specific to this application
92 static void hello_sock_parse_arg(int ch
, char *arg
)
110 hello_sock_close_timeout_poll(void *arg
)
112 struct hello_context_t
*ctx
= arg
;
113 SPDK_NOTICELOG("Connection closed\n");
115 spdk_poller_unregister(&ctx
->time_out
);
116 spdk_poller_unregister(&ctx
->poller_in
);
117 spdk_sock_close(&ctx
->sock
);
124 hello_sock_recv_poll(void *arg
)
126 struct hello_context_t
*ctx
= arg
;
128 char buf_in
[BUFFER_SIZE
];
133 rc
= spdk_sock_recv(ctx
->sock
, buf_in
, sizeof(buf_in
) - 1);
136 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
140 SPDK_ERRLOG("spdk_sock_recv() failed, errno %d: %s\n",
141 errno
, spdk_strerror(errno
));
148 printf("%s", buf_in
);
155 hello_sock_writev_poll(void *arg
)
157 struct hello_context_t
*ctx
= arg
;
159 char buf_out
[BUFFER_SIZE
];
163 n
= read(STDIN_FILENO
, buf_out
, sizeof(buf_out
));
164 if (n
== 0 || !g_is_running
) {
166 SPDK_NOTICELOG("Closing connection...\n");
168 ctx
->time_out
= spdk_poller_register(hello_sock_close_timeout_poll
, ctx
,
171 spdk_poller_unregister(&ctx
->poller_out
);
176 * Send message to the server
178 iov
.iov_base
= buf_out
;
180 rc
= spdk_sock_writev(ctx
->sock
, &iov
, 1);
182 ctx
->bytes_out
+= rc
;
189 hello_sock_connect(struct hello_context_t
*ctx
)
192 char saddr
[ADDR_STR_LEN
], caddr
[ADDR_STR_LEN
];
193 uint16_t cport
, sport
;
195 SPDK_NOTICELOG("Connecting to the server on %s:%d\n", ctx
->host
, ctx
->port
);
197 ctx
->sock
= spdk_sock_connect(ctx
->host
, ctx
->port
);
198 if (ctx
->sock
== NULL
) {
199 SPDK_ERRLOG("connect error(%d): %s\n", errno
, spdk_strerror(errno
));
203 rc
= spdk_sock_getaddr(ctx
->sock
, saddr
, sizeof(saddr
), &sport
, caddr
, sizeof(caddr
), &cport
);
205 SPDK_ERRLOG("Cannot get connection addresses\n");
206 spdk_sock_close(&ctx
->sock
);
210 SPDK_NOTICELOG("Connection accepted from (%s, %hu) to (%s, %hu)\n", caddr
, cport
, saddr
, sport
);
212 fcntl(STDIN_FILENO
, F_SETFL
, fcntl(STDIN_FILENO
, F_GETFL
) | O_NONBLOCK
);
215 ctx
->poller_in
= spdk_poller_register(hello_sock_recv_poll
, ctx
, 0);
216 ctx
->poller_out
= spdk_poller_register(hello_sock_writev_poll
, ctx
, 0);
222 hello_sock_cb(void *arg
, struct spdk_sock_group
*group
, struct spdk_sock
*sock
)
225 char buf
[BUFFER_SIZE
];
227 struct hello_context_t
*ctx
= arg
;
229 n
= spdk_sock_recv(sock
, buf
, sizeof(buf
));
231 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
232 SPDK_ERRLOG("spdk_sock_recv() failed, errno %d: %s\n",
233 errno
, spdk_strerror(errno
));
237 SPDK_ERRLOG("spdk_sock_recv() failed, errno %d: %s\n",
238 errno
, spdk_strerror(errno
));
245 n
= spdk_sock_writev(sock
, &iov
, 1);
252 /* Connection closed */
253 SPDK_NOTICELOG("Connection closed\n");
254 spdk_sock_group_remove_sock(group
, sock
);
255 spdk_sock_close(&sock
);
259 hello_sock_accept_poll(void *arg
)
261 struct hello_context_t
*ctx
= arg
;
262 struct spdk_sock
*sock
;
265 char saddr
[ADDR_STR_LEN
], caddr
[ADDR_STR_LEN
];
266 uint16_t cport
, sport
;
269 spdk_poller_unregister(&ctx
->poller_in
);
270 spdk_poller_unregister(&ctx
->poller_out
);
271 spdk_sock_close(&ctx
->sock
);
272 spdk_sock_group_close(&ctx
->group
);
278 sock
= spdk_sock_accept(ctx
->sock
);
280 spdk_sock_getaddr(sock
, saddr
, sizeof(saddr
), &sport
, caddr
, sizeof(caddr
), &cport
);
282 SPDK_NOTICELOG("Accepting a new connection from (%s, %hu) to (%s, %hu)\n",
283 caddr
, cport
, saddr
, sport
);
285 rc
= spdk_sock_group_add_sock(ctx
->group
, sock
,
289 spdk_sock_close(&sock
);
290 SPDK_ERRLOG("failed\n");
296 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
297 SPDK_ERRLOG("accept error(%d): %s\n", errno
, spdk_strerror(errno
));
307 hello_sock_group_poll(void *arg
)
309 struct hello_context_t
*ctx
= arg
;
312 rc
= spdk_sock_group_poll(ctx
->group
);
314 SPDK_ERRLOG("Failed to poll sock_group=%p\n", ctx
->group
);
321 hello_sock_listen(struct hello_context_t
*ctx
)
323 ctx
->sock
= spdk_sock_listen(ctx
->host
, ctx
->port
);
324 if (ctx
->sock
== NULL
) {
325 SPDK_ERRLOG("Cannot create server socket\n");
329 SPDK_NOTICELOG("Listening connection on %s:%d\n", ctx
->host
, ctx
->port
);
332 * Create sock group for server socket
334 ctx
->group
= spdk_sock_group_create();
339 * Start acceptor and group poller
341 ctx
->poller_in
= spdk_poller_register(hello_sock_accept_poll
, ctx
,
343 ctx
->poller_out
= spdk_poller_register(hello_sock_group_poll
, ctx
, 0);
349 hello_sock_shutdown_cb(void)
351 g_is_running
= false;
354 * Our initial event that kicks off everything from main().
357 hello_start(void *arg1
, void *arg2
)
359 struct hello_context_t
*ctx
= arg1
;
362 SPDK_NOTICELOG("Successfully started the application\n");
364 if (ctx
->is_server
) {
365 rc
= hello_sock_listen(ctx
);
367 rc
= hello_sock_connect(ctx
);
377 main(int argc
, char **argv
)
379 struct spdk_app_opts opts
= {};
381 struct hello_context_t hello_context
= {};
383 /* Set default values in opts structure. */
384 spdk_app_opts_init(&opts
);
385 opts
.name
= "hello_sock";
386 opts
.config_file
= "sock.conf";
387 opts
.shutdown_cb
= hello_sock_shutdown_cb
;
389 if ((rc
= spdk_app_parse_args(argc
, argv
, &opts
, "H:P:SV", NULL
, hello_sock_parse_arg
,
390 hello_sock_usage
)) != SPDK_APP_PARSE_ARGS_SUCCESS
) {
393 hello_context
.is_server
= g_is_server
;
394 hello_context
.host
= g_host
;
395 hello_context
.port
= g_port
;
396 hello_context
.verbose
= g_verbose
;
398 rc
= spdk_net_framework_start();
400 SPDK_ERRLOG("ERROR starting application\n");
404 rc
= spdk_app_start(&opts
, hello_start
, &hello_context
, NULL
);
406 SPDK_ERRLOG("ERROR starting application\n");
410 SPDK_NOTICELOG("Exiting from application\n");
412 if (hello_context
.verbose
) {
413 printf("** %d bytes received, %d bytes sent **\n",
414 hello_context
.bytes_in
, hello_context
.bytes_out
);
417 spdk_net_framework_fini();
419 /* Gracefully close out all of the SPDK subsystems. */