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
;
52 static char *g_sock_impl_name
;
54 static bool g_is_server
;
55 static bool g_verbose
;
58 * We'll use this struct to gather housekeeping hello_context to pass between
59 * our events and callbacks.
61 struct hello_context_t
{
71 struct spdk_sock
*sock
;
73 struct spdk_sock_group
*group
;
74 struct spdk_poller
*poller_in
;
75 struct spdk_poller
*poller_out
;
76 struct spdk_poller
*time_out
;
82 * Usage function for printing parameters that are specific to this application
85 hello_sock_usage(void)
87 printf(" -H host_addr host address\n");
88 printf(" -P port port number\n");
89 printf(" -N sock_impl socket implementation, e.g., -N posix or -N vpp\n");
90 printf(" -S start in server mode\n");
91 printf(" -V print out additional informations");
95 * This function is called to parse the parameters that are specific to this application
97 static int hello_sock_parse_arg(int ch
, char *arg
)
104 g_sock_impl_name
= arg
;
107 g_port
= spdk_strtol(arg
, 10);
109 fprintf(stderr
, "Invalid port ID\n");
126 hello_sock_close_timeout_poll(void *arg
)
128 struct hello_context_t
*ctx
= arg
;
129 SPDK_NOTICELOG("Connection closed\n");
131 spdk_poller_unregister(&ctx
->time_out
);
132 spdk_poller_unregister(&ctx
->poller_in
);
133 spdk_sock_close(&ctx
->sock
);
134 spdk_sock_group_close(&ctx
->group
);
136 spdk_app_stop(ctx
->rc
);
141 hello_sock_quit(struct hello_context_t
*ctx
, int rc
)
144 spdk_poller_unregister(&ctx
->poller_out
);
145 if (!ctx
->time_out
) {
146 ctx
->time_out
= SPDK_POLLER_REGISTER(hello_sock_close_timeout_poll
, ctx
,
153 hello_sock_recv_poll(void *arg
)
155 struct hello_context_t
*ctx
= arg
;
157 char buf_in
[BUFFER_SIZE
];
162 rc
= spdk_sock_recv(ctx
->sock
, buf_in
, sizeof(buf_in
) - 1);
165 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
169 SPDK_ERRLOG("spdk_sock_recv() failed, errno %d: %s\n",
170 errno
, spdk_strerror(errno
));
177 printf("%s", buf_in
);
184 hello_sock_writev_poll(void *arg
)
186 struct hello_context_t
*ctx
= arg
;
188 char buf_out
[BUFFER_SIZE
];
192 n
= read(STDIN_FILENO
, buf_out
, sizeof(buf_out
));
193 if (n
== 0 || !g_is_running
) {
195 SPDK_NOTICELOG("Closing connection...\n");
196 hello_sock_quit(ctx
, 0);
201 * Send message to the server
203 iov
.iov_base
= buf_out
;
205 rc
= spdk_sock_writev(ctx
->sock
, &iov
, 1);
207 ctx
->bytes_out
+= rc
;
214 hello_sock_connect(struct hello_context_t
*ctx
)
217 char saddr
[ADDR_STR_LEN
], caddr
[ADDR_STR_LEN
];
218 uint16_t cport
, sport
;
220 SPDK_NOTICELOG("Connecting to the server on %s:%d with sock_impl(%s)\n", ctx
->host
, ctx
->port
,
221 ctx
->sock_impl_name
);
223 ctx
->sock
= spdk_sock_connect(ctx
->host
, ctx
->port
, ctx
->sock_impl_name
);
224 if (ctx
->sock
== NULL
) {
225 SPDK_ERRLOG("connect error(%d): %s\n", errno
, spdk_strerror(errno
));
229 rc
= spdk_sock_getaddr(ctx
->sock
, saddr
, sizeof(saddr
), &sport
, caddr
, sizeof(caddr
), &cport
);
231 SPDK_ERRLOG("Cannot get connection addresses\n");
232 spdk_sock_close(&ctx
->sock
);
236 SPDK_NOTICELOG("Connection accepted from (%s, %hu) to (%s, %hu)\n", caddr
, cport
, saddr
, sport
);
238 fcntl(STDIN_FILENO
, F_SETFL
, fcntl(STDIN_FILENO
, F_GETFL
) | O_NONBLOCK
);
241 ctx
->poller_in
= SPDK_POLLER_REGISTER(hello_sock_recv_poll
, ctx
, 0);
242 ctx
->poller_out
= SPDK_POLLER_REGISTER(hello_sock_writev_poll
, ctx
, 0);
248 hello_sock_cb(void *arg
, struct spdk_sock_group
*group
, struct spdk_sock
*sock
)
251 char buf
[BUFFER_SIZE
];
253 struct hello_context_t
*ctx
= arg
;
255 n
= spdk_sock_recv(sock
, buf
, sizeof(buf
));
257 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
258 SPDK_ERRLOG("spdk_sock_recv() failed, errno %d: %s\n",
259 errno
, spdk_strerror(errno
));
263 SPDK_ERRLOG("spdk_sock_recv() failed, errno %d: %s\n",
264 errno
, spdk_strerror(errno
));
271 n
= spdk_sock_writev(sock
, &iov
, 1);
278 /* Connection closed */
279 SPDK_NOTICELOG("Connection closed\n");
280 spdk_sock_group_remove_sock(group
, sock
);
281 spdk_sock_close(&sock
);
285 hello_sock_accept_poll(void *arg
)
287 struct hello_context_t
*ctx
= arg
;
288 struct spdk_sock
*sock
;
291 char saddr
[ADDR_STR_LEN
], caddr
[ADDR_STR_LEN
];
292 uint16_t cport
, sport
;
295 hello_sock_quit(ctx
, 0);
300 sock
= spdk_sock_accept(ctx
->sock
);
302 rc
= spdk_sock_getaddr(sock
, saddr
, sizeof(saddr
), &sport
, caddr
, sizeof(caddr
), &cport
);
304 SPDK_ERRLOG("Cannot get connection addresses\n");
305 spdk_sock_close(&ctx
->sock
);
309 SPDK_NOTICELOG("Accepting a new connection from (%s, %hu) to (%s, %hu)\n",
310 caddr
, cport
, saddr
, sport
);
312 rc
= spdk_sock_group_add_sock(ctx
->group
, sock
,
316 spdk_sock_close(&sock
);
317 SPDK_ERRLOG("failed\n");
323 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
324 SPDK_ERRLOG("accept error(%d): %s\n", errno
, spdk_strerror(errno
));
334 hello_sock_group_poll(void *arg
)
336 struct hello_context_t
*ctx
= arg
;
339 rc
= spdk_sock_group_poll(ctx
->group
);
341 SPDK_ERRLOG("Failed to poll sock_group=%p\n", ctx
->group
);
348 hello_sock_listen(struct hello_context_t
*ctx
)
350 ctx
->sock
= spdk_sock_listen(ctx
->host
, ctx
->port
, ctx
->sock_impl_name
);
351 if (ctx
->sock
== NULL
) {
352 SPDK_ERRLOG("Cannot create server socket\n");
356 SPDK_NOTICELOG("Listening connection on %s:%d with sock_impl(%s)\n", ctx
->host
, ctx
->port
,
357 ctx
->sock_impl_name
);
360 * Create sock group for server socket
362 ctx
->group
= spdk_sock_group_create(NULL
);
367 * Start acceptor and group poller
369 ctx
->poller_in
= SPDK_POLLER_REGISTER(hello_sock_accept_poll
, ctx
,
371 ctx
->poller_out
= SPDK_POLLER_REGISTER(hello_sock_group_poll
, ctx
, 0);
377 hello_sock_shutdown_cb(void)
379 g_is_running
= false;
383 * Our initial event that kicks off everything from main().
386 hello_start(void *arg1
)
388 struct hello_context_t
*ctx
= arg1
;
391 SPDK_NOTICELOG("Successfully started the application\n");
393 if (ctx
->is_server
) {
394 rc
= hello_sock_listen(ctx
);
396 rc
= hello_sock_connect(ctx
);
406 main(int argc
, char **argv
)
408 struct spdk_app_opts opts
= {};
410 struct hello_context_t hello_context
= {};
412 /* Set default values in opts structure. */
413 spdk_app_opts_init(&opts
);
414 opts
.name
= "hello_sock";
415 opts
.shutdown_cb
= hello_sock_shutdown_cb
;
417 if ((rc
= spdk_app_parse_args(argc
, argv
, &opts
, "H:N:P:SV", NULL
, hello_sock_parse_arg
,
418 hello_sock_usage
)) != SPDK_APP_PARSE_ARGS_SUCCESS
) {
421 hello_context
.is_server
= g_is_server
;
422 hello_context
.host
= g_host
;
423 hello_context
.sock_impl_name
= g_sock_impl_name
;
424 hello_context
.port
= g_port
;
425 hello_context
.verbose
= g_verbose
;
427 rc
= spdk_app_start(&opts
, hello_start
, &hello_context
);
429 SPDK_ERRLOG("ERROR starting application\n");
432 SPDK_NOTICELOG("Exiting from application\n");
434 if (hello_context
.verbose
) {
435 printf("** %d bytes received, %d bytes sent **\n",
436 hello_context
.bytes_in
, hello_context
.bytes_out
);
439 /* Gracefully close out all of the SPDK subsystems. */