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
;
80 * Usage function for printing parameters that are specific to this application
83 hello_sock_usage(void)
85 printf(" -H host_addr host address\n");
86 printf(" -P port port number\n");
87 printf(" -S start in server mode\n");
88 printf(" -V print out additional informations");
92 * This function is called to parse the parameters that are specific to this application
94 static int hello_sock_parse_arg(int ch
, char *arg
)
101 g_port
= spdk_strtol(arg
, 10);
103 fprintf(stderr
, "Invalid port ID\n");
120 hello_sock_net_fini_cb(void *cb_arg
)
122 struct hello_context_t
*ctx
= cb_arg
;
123 spdk_app_stop(ctx
->rc
);
127 hello_sock_close_timeout_poll(void *arg
)
129 struct hello_context_t
*ctx
= arg
;
130 SPDK_NOTICELOG("Connection closed\n");
132 spdk_poller_unregister(&ctx
->time_out
);
133 spdk_poller_unregister(&ctx
->poller_in
);
134 spdk_sock_close(&ctx
->sock
);
135 spdk_sock_group_close(&ctx
->group
);
137 spdk_net_framework_fini(hello_sock_net_fini_cb
, arg
);
142 hello_sock_quit(struct hello_context_t
*ctx
, int rc
)
145 spdk_poller_unregister(&ctx
->poller_out
);
146 if (!ctx
->time_out
) {
147 ctx
->time_out
= spdk_poller_register(hello_sock_close_timeout_poll
, ctx
,
154 hello_sock_recv_poll(void *arg
)
156 struct hello_context_t
*ctx
= arg
;
158 char buf_in
[BUFFER_SIZE
];
163 rc
= spdk_sock_recv(ctx
->sock
, buf_in
, sizeof(buf_in
) - 1);
166 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
170 SPDK_ERRLOG("spdk_sock_recv() failed, errno %d: %s\n",
171 errno
, spdk_strerror(errno
));
178 printf("%s", buf_in
);
185 hello_sock_writev_poll(void *arg
)
187 struct hello_context_t
*ctx
= arg
;
189 char buf_out
[BUFFER_SIZE
];
193 n
= read(STDIN_FILENO
, buf_out
, sizeof(buf_out
));
194 if (n
== 0 || !g_is_running
) {
196 SPDK_NOTICELOG("Closing connection...\n");
197 hello_sock_quit(ctx
, 0);
202 * Send message to the server
204 iov
.iov_base
= buf_out
;
206 rc
= spdk_sock_writev(ctx
->sock
, &iov
, 1);
208 ctx
->bytes_out
+= rc
;
215 hello_sock_connect(struct hello_context_t
*ctx
)
218 char saddr
[ADDR_STR_LEN
], caddr
[ADDR_STR_LEN
];
219 uint16_t cport
, sport
;
221 SPDK_NOTICELOG("Connecting to the server on %s:%d\n", ctx
->host
, ctx
->port
);
223 ctx
->sock
= spdk_sock_connect(ctx
->host
, ctx
->port
);
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
);
351 if (ctx
->sock
== NULL
) {
352 SPDK_ERRLOG("Cannot create server socket\n");
356 SPDK_NOTICELOG("Listening connection on %s:%d\n", ctx
->host
, ctx
->port
);
359 * Create sock group for server socket
361 ctx
->group
= spdk_sock_group_create();
366 * Start acceptor and group poller
368 ctx
->poller_in
= spdk_poller_register(hello_sock_accept_poll
, ctx
,
370 ctx
->poller_out
= spdk_poller_register(hello_sock_group_poll
, ctx
, 0);
376 hello_sock_shutdown_cb(void)
378 g_is_running
= false;
382 * Our initial event that kicks off everything from main().
385 hello_start(void *arg1
, int rc
)
387 struct hello_context_t
*ctx
= arg1
;
390 SPDK_ERRLOG("ERROR starting application\n");
395 SPDK_NOTICELOG("Successfully started the application\n");
397 if (ctx
->is_server
) {
398 rc
= hello_sock_listen(ctx
);
400 rc
= hello_sock_connect(ctx
);
410 start_net_framework(void *arg1
)
412 spdk_net_framework_start(hello_start
, arg1
);
416 main(int argc
, char **argv
)
418 struct spdk_app_opts opts
= {};
420 struct hello_context_t hello_context
= {};
422 /* Set default values in opts structure. */
423 spdk_app_opts_init(&opts
);
424 opts
.name
= "hello_sock";
425 opts
.config_file
= "sock.conf";
426 opts
.shutdown_cb
= hello_sock_shutdown_cb
;
428 if ((rc
= spdk_app_parse_args(argc
, argv
, &opts
, "H:P:SV", NULL
, hello_sock_parse_arg
,
429 hello_sock_usage
)) != SPDK_APP_PARSE_ARGS_SUCCESS
) {
432 hello_context
.is_server
= g_is_server
;
433 hello_context
.host
= g_host
;
434 hello_context
.port
= g_port
;
435 hello_context
.verbose
= g_verbose
;
437 rc
= spdk_app_start(&opts
, start_net_framework
, &hello_context
);
439 SPDK_ERRLOG("ERROR starting application\n");
442 SPDK_NOTICELOG("Exiting from application\n");
444 if (hello_context
.verbose
) {
445 printf("** %d bytes received, %d bytes sent **\n",
446 hello_context
.bytes_in
, hello_context
.bytes_out
);
449 /* Gracefully close out all of the SPDK subsystems. */