-
Notifications
You must be signed in to change notification settings - Fork 429
/
ucp_client_server.c
1149 lines (989 loc) · 35.8 KB
/
ucp_client_server.c
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2018. ALL RIGHTS RESERVED.
*
* See file LICENSE for terms.
*/
/*
* UCP client - server example utility
* -----------------------------------------------
*
* Server side:
*
* ./ucp_client_server
*
* Client side:
*
* ./ucp_client_server -a <server-ip>
*
* Notes:
*
* - The server will listen to incoming connection requests on INADDR_ANY.
* - The client needs to pass the IP address of the server side to connect to
* as an argument to the test.
* - Currently, the passed IP needs to be an IPoIB or a RoCE address.
* - The port which the server side would listen on can be modified with the
* '-p' option and should be used on both sides. The default port to use is
* 13337.
*/
#include "hello_world_util.h"
#include "ucp_util.h"
#include <ucp/api/ucp.h>
#include <string.h> /* memset */
#include <arpa/inet.h> /* inet_addr */
#include <unistd.h> /* getopt */
#include <stdlib.h> /* atoi */
#define DEFAULT_PORT 13337
#define IP_STRING_LEN 50
#define PORT_STRING_LEN 8
#define TAG 0xCAFE
#define COMM_TYPE_DEFAULT "STREAM"
#define PRINT_INTERVAL 2000
#define DEFAULT_NUM_ITERATIONS 1
#define TEST_AM_ID 0
static long test_string_length = 16;
static long iov_cnt = 1;
static uint16_t server_port = DEFAULT_PORT;
static sa_family_t ai_family = AF_INET;
static int num_iterations = DEFAULT_NUM_ITERATIONS;
static int connection_closed = 1;
typedef enum {
CLIENT_SERVER_SEND_RECV_STREAM = UCS_BIT(0),
CLIENT_SERVER_SEND_RECV_TAG = UCS_BIT(1),
CLIENT_SERVER_SEND_RECV_AM = UCS_BIT(2),
CLIENT_SERVER_SEND_RECV_DEFAULT = CLIENT_SERVER_SEND_RECV_STREAM
} send_recv_type_t;
/**
* Server's application context to be used in the user's connection request
* callback.
* It holds the server's listener and the handle to an incoming connection request.
*/
typedef struct ucx_server_ctx {
volatile ucp_conn_request_h conn_request;
ucp_listener_h listener;
} ucx_server_ctx_t;
/**
* Stream request context. Holds a value to indicate whether or not the
* request is completed.
*/
typedef struct test_req {
int complete;
} test_req_t;
/**
* Descriptor of the data received with AM API.
*/
static struct {
volatile int complete;
int is_rndv;
void *desc;
void *recv_buf;
} am_data_desc = {0, 0, NULL, NULL};
/**
* Print this application's usage help message.
*/
static void usage(void);
static void buffer_free(ucp_dt_iov_t *iov, size_t iov_size)
{
size_t idx;
for (idx = 0; idx < iov_size; idx++) {
mem_type_free(iov[idx].buffer);
}
}
static int buffer_malloc(ucp_dt_iov_t *iov)
{
size_t idx;
for (idx = 0; idx < iov_cnt; idx++) {
iov[idx].length = test_string_length;
iov[idx].buffer = mem_type_malloc(iov[idx].length);
if (iov[idx].buffer == NULL) {
buffer_free(iov, idx);
return -1;
}
}
return 0;
}
int fill_buffer(ucp_dt_iov_t *iov)
{
int ret = 0;
size_t idx;
for (idx = 0; idx < iov_cnt; idx++) {
ret = generate_test_string(iov[idx].buffer, iov[idx].length);
if (ret != 0) {
break;
}
}
CHKERR_ACTION(ret != 0, "generate test string", return -1;);
return 0;
}
static void common_cb(void *user_data, const char *type_str)
{
test_req_t *ctx;
if (user_data == NULL) {
fprintf(stderr, "user_data passed to %s mustn't be NULL\n", type_str);
return;
}
ctx = user_data;
ctx->complete = 1;
}
static void tag_recv_cb(void *request, ucs_status_t status,
const ucp_tag_recv_info_t *info, void *user_data)
{
common_cb(user_data, "tag_recv_cb");
}
/**
* The callback on the receiving side, which is invoked upon receiving the
* stream message.
*/
static void stream_recv_cb(void *request, ucs_status_t status, size_t length,
void *user_data)
{
common_cb(user_data, "stream_recv_cb");
}
/**
* The callback on the receiving side, which is invoked upon receiving the
* active message.
*/
static void am_recv_cb(void *request, ucs_status_t status, size_t length,
void *user_data)
{
common_cb(user_data, "am_recv_cb");
}
/**
* The callback on the sending side, which is invoked after finishing sending
* the message.
*/
static void send_cb(void *request, ucs_status_t status, void *user_data)
{
common_cb(user_data, "send_cb");
}
/**
* Error handling callback.
*/
static void err_cb(void *arg, ucp_ep_h ep, ucs_status_t status)
{
printf("error handling callback was invoked with status %d (%s)\n",
status, ucs_status_string(status));
connection_closed = 1;
}
/**
* Set an address for the server to listen on - INADDR_ANY on a well known port.
*/
void set_sock_addr(const char *address_str, struct sockaddr_storage *saddr)
{
struct sockaddr_in *sa_in;
struct sockaddr_in6 *sa_in6;
/* The server will listen on INADDR_ANY */
memset(saddr, 0, sizeof(*saddr));
switch (ai_family) {
case AF_INET:
sa_in = (struct sockaddr_in*)saddr;
if (address_str != NULL) {
inet_pton(AF_INET, address_str, &sa_in->sin_addr);
} else {
sa_in->sin_addr.s_addr = INADDR_ANY;
}
sa_in->sin_family = AF_INET;
sa_in->sin_port = htons(server_port);
break;
case AF_INET6:
sa_in6 = (struct sockaddr_in6*)saddr;
if (address_str != NULL) {
inet_pton(AF_INET6, address_str, &sa_in6->sin6_addr);
} else {
sa_in6->sin6_addr = in6addr_any;
}
sa_in6->sin6_family = AF_INET6;
sa_in6->sin6_port = htons(server_port);
break;
default:
fprintf(stderr, "Invalid address family");
break;
}
}
/**
* Initialize the client side. Create an endpoint from the client side to be
* connected to the remote server (to the given IP).
*/
static ucs_status_t start_client(ucp_worker_h ucp_worker,
const char *address_str, ucp_ep_h *client_ep)
{
ucp_ep_params_t ep_params;
struct sockaddr_storage connect_addr;
ucs_status_t status;
set_sock_addr(address_str, &connect_addr);
/*
* Endpoint field mask bits:
* UCP_EP_PARAM_FIELD_FLAGS - Use the value of the 'flags' field.
* UCP_EP_PARAM_FIELD_SOCK_ADDR - Use a remote sockaddr to connect
* to the remote peer.
* UCP_EP_PARAM_FIELD_ERR_HANDLING_MODE - Error handling mode - this flag
* is temporarily required since the
* endpoint will be closed with
* UCP_EP_CLOSE_MODE_FORCE which
* requires this mode.
* Once UCP_EP_CLOSE_MODE_FORCE is
* removed, the error handling mode
* will be removed.
*/
ep_params.field_mask = UCP_EP_PARAM_FIELD_FLAGS |
UCP_EP_PARAM_FIELD_SOCK_ADDR |
UCP_EP_PARAM_FIELD_ERR_HANDLER |
UCP_EP_PARAM_FIELD_ERR_HANDLING_MODE;
ep_params.err_mode = UCP_ERR_HANDLING_MODE_PEER;
ep_params.err_handler.cb = err_cb;
ep_params.err_handler.arg = NULL;
ep_params.flags = UCP_EP_PARAMS_FLAGS_CLIENT_SERVER;
ep_params.sockaddr.addr = (struct sockaddr*)&connect_addr;
ep_params.sockaddr.addrlen = sizeof(connect_addr);
status = ucp_ep_create(ucp_worker, &ep_params, client_ep);
if (status != UCS_OK) {
fprintf(stderr, "failed to connect to %s (%s)\n", address_str,
ucs_status_string(status));
}
return status;
}
static void print_iov(const ucp_dt_iov_t *iov)
{
char *msg = alloca(test_string_length);
size_t idx;
for (idx = 0; idx < iov_cnt; idx++) {
/* In case of Non-System memory */
mem_type_memcpy(msg, iov[idx].buffer, test_string_length);
printf("%s.\n", msg);
}
}
/**
* Print the received message on the server side or the sent data on the client
* side.
*/
static
void print_result(int is_server, const ucp_dt_iov_t *iov, int current_iter)
{
if (is_server) {
printf("Server: iteration #%d\n", (current_iter + 1));
printf("UCX data message was received\n");
printf("\n\n----- UCP TEST SUCCESS -------\n\n");
} else {
printf("Client: iteration #%d\n", (current_iter + 1));
printf("\n\n------------------------------\n\n");
}
print_iov(iov);
printf("\n\n------------------------------\n\n");
}
/**
* Progress the request until it completes.
*/
static ucs_status_t request_wait(ucp_worker_h ucp_worker, void *request,
test_req_t *ctx)
{
ucs_status_t status;
/* if operation was completed immediately */
if (request == NULL) {
return UCS_OK;
}
if (UCS_PTR_IS_ERR(request)) {
return UCS_PTR_STATUS(request);
}
while (ctx->complete == 0) {
ucp_worker_progress(ucp_worker);
}
status = ucp_request_check_status(request);
ucp_request_free(request);
return status;
}
static int request_finalize(ucp_worker_h ucp_worker, test_req_t *request,
test_req_t *ctx, int is_server, ucp_dt_iov_t *iov,
int current_iter)
{
int ret = 0;
ucs_status_t status;
status = request_wait(ucp_worker, request, ctx);
if (status != UCS_OK) {
fprintf(stderr, "unable to %s UCX message (%s)\n",
is_server ? "receive": "send", ucs_status_string(status));
ret = -1;
goto release_iov;
}
/* Print the output of the first, last and every PRINT_INTERVAL iteration */
if ((current_iter == 0) || (current_iter == (num_iterations - 1)) ||
!((current_iter + 1) % (PRINT_INTERVAL))) {
print_result(is_server, iov, current_iter);
}
release_iov:
buffer_free(iov, iov_cnt);
return ret;
}
static int
fill_request_param(ucp_dt_iov_t *iov, int is_client,
void **msg, size_t *msg_length,
test_req_t *ctx, ucp_request_param_t *param)
{
CHKERR_ACTION(buffer_malloc(iov) != 0, "allocate memory", return -1;);
if (is_client && (fill_buffer(iov) != 0)) {
buffer_free(iov, iov_cnt);
return -1;
}
*msg = (iov_cnt == 1) ? iov[0].buffer : iov;
*msg_length = (iov_cnt == 1) ? iov[0].length : iov_cnt;
ctx->complete = 0;
param->op_attr_mask = UCP_OP_ATTR_FIELD_CALLBACK |
UCP_OP_ATTR_FIELD_DATATYPE |
UCP_OP_ATTR_FIELD_USER_DATA;
param->datatype = (iov_cnt == 1) ? ucp_dt_make_contig(1) :
UCP_DATATYPE_IOV;
param->user_data = ctx;
return 0;
}
/**
* Send and receive a message using the Stream API.
* The client sends a message to the server and waits until the send it completed.
* The server receives a message from the client and waits for its completion.
*/
static int send_recv_stream(ucp_worker_h ucp_worker, ucp_ep_h ep, int is_server,
int current_iter)
{
ucp_dt_iov_t *iov = alloca(iov_cnt * sizeof(ucp_dt_iov_t));
ucp_request_param_t param;
test_req_t *request;
size_t msg_length;
void *msg;
test_req_t ctx;
memset(iov, 0, iov_cnt * sizeof(*iov));
if (fill_request_param(iov, !is_server, &msg, &msg_length,
&ctx, ¶m) != 0) {
return -1;
}
if (!is_server) {
/* Client sends a message to the server using the stream API */
param.cb.send = send_cb;
request = ucp_stream_send_nbx(ep, msg, msg_length, ¶m);
} else {
/* Server receives a message from the client using the stream API */
param.op_attr_mask |= UCP_OP_ATTR_FIELD_FLAGS;
param.flags = UCP_STREAM_RECV_FLAG_WAITALL;
param.cb.recv_stream = stream_recv_cb;
request = ucp_stream_recv_nbx(ep, msg, msg_length,
&msg_length, ¶m);
}
return request_finalize(ucp_worker, request, &ctx, is_server, iov,
current_iter);
}
/**
* Send and receive a message using the Tag-Matching API.
* The client sends a message to the server and waits until the send it completed.
* The server receives a message from the client and waits for its completion.
*/
static int send_recv_tag(ucp_worker_h ucp_worker, ucp_ep_h ep, int is_server,
int current_iter)
{
ucp_dt_iov_t *iov = alloca(iov_cnt * sizeof(ucp_dt_iov_t));
ucp_request_param_t param;
void *request;
size_t msg_length;
void *msg;
test_req_t ctx;
memset(iov, 0, iov_cnt * sizeof(*iov));
if (fill_request_param(iov, !is_server, &msg, &msg_length,
&ctx, ¶m) != 0) {
return -1;
}
if (!is_server) {
/* Client sends a message to the server using the Tag-Matching API */
param.cb.send = send_cb;
request = ucp_tag_send_nbx(ep, msg, msg_length, TAG, ¶m);
} else {
/* Server receives a message from the client using the Tag-Matching API */
param.cb.recv = tag_recv_cb;
request = ucp_tag_recv_nbx(ucp_worker, msg, msg_length, TAG, 0,
¶m);
}
return request_finalize(ucp_worker, request, &ctx, is_server, iov,
current_iter);
}
ucs_status_t ucp_am_data_cb(void *arg, const void *header, size_t header_length,
void *data, size_t length,
const ucp_am_recv_param_t *param)
{
ucp_dt_iov_t *iov;
size_t idx;
size_t offset;
if (length != iov_cnt * test_string_length) {
fprintf(stderr, "received wrong data length %ld (expected %ld)",
length, iov_cnt * test_string_length);
return UCS_OK;
}
if (header_length != 0) {
fprintf(stderr, "received unexpected header, length %ld", header_length);
}
am_data_desc.complete++;
if (param->recv_attr & UCP_AM_RECV_ATTR_FLAG_RNDV) {
/* Rendezvous request arrived, data contains an internal UCX descriptor,
* which has to be passed to ucp_am_recv_data_nbx function to confirm
* data transfer.
*/
am_data_desc.is_rndv = 1;
am_data_desc.desc = data;
return UCS_INPROGRESS;
}
/* Message delivered with eager protocol, data should be available
* immediately
*/
am_data_desc.is_rndv = 0;
iov = am_data_desc.recv_buf;
offset = 0;
for (idx = 0; idx < iov_cnt; idx++) {
mem_type_memcpy(iov[idx].buffer, UCS_PTR_BYTE_OFFSET(data, offset),
iov[idx].length);
offset += iov[idx].length;
}
return UCS_OK;
}
/**
* Send and receive a message using Active Message API.
* The client sends a message to the server and waits until the send is completed.
* The server gets a message from the client and if it is rendezvous request,
* initiates receive operation.
*/
static int send_recv_am(ucp_worker_h ucp_worker, ucp_ep_h ep, int is_server,
int current_iter)
{
static int last = 0;
ucp_dt_iov_t *iov = alloca(iov_cnt * sizeof(ucp_dt_iov_t));
test_req_t *request;
ucp_request_param_t params;
size_t msg_length;
void *msg;
test_req_t ctx;
memset(iov, 0, iov_cnt * sizeof(*iov));
if (fill_request_param(iov, !is_server, &msg, &msg_length,
&ctx, ¶ms) != 0) {
return -1;
}
if (is_server) {
am_data_desc.recv_buf = iov;
/* waiting for AM callback has called */
while (last == am_data_desc.complete) {
ucp_worker_progress(ucp_worker);
}
last++;
if (am_data_desc.is_rndv) {
/* Rendezvous request has arrived, need to invoke receive operation
* to confirm data transfer from the sender to the "recv_message"
* buffer. */
params.op_attr_mask |= UCP_OP_ATTR_FLAG_NO_IMM_CMPL;
params.cb.recv_am = am_recv_cb;
request = ucp_am_recv_data_nbx(ucp_worker,
am_data_desc.desc,
msg, msg_length,
¶ms);
} else {
/* Data has arrived eagerly and is ready for use, no need to
* initiate receive operation. */
request = NULL;
}
} else {
/* Client sends a message to the server using the AM API */
params.cb.send = (ucp_send_nbx_callback_t)send_cb;
request = ucp_am_send_nbx(ep, TEST_AM_ID, NULL, 0ul, msg,
msg_length, ¶ms);
}
return request_finalize(ucp_worker, request, &ctx, is_server, iov,
current_iter);
}
/**
* Print this application's usage help message.
*/
static void usage()
{
fprintf(stderr, "Usage: ucp_client_server [parameters]\n");
fprintf(stderr, "UCP client-server example utility\n");
fprintf(stderr, "\nParameters are:\n");
fprintf(stderr, " -a Set IP address of the server "
"(required for client and should not be specified "
"for the server)\n");
fprintf(stderr, " -l Set IP address where server listens "
"(If not specified, server uses INADDR_ANY; "
"Irrelevant at client)\n");
fprintf(stderr, " -p Port number to listen/connect to (default = %d). "
"0 on the server side means select a random port and print it\n",
DEFAULT_PORT);
fprintf(stderr, " -c Communication type for the client and server. "
" Valid values are:\n"
" 'stream' : Stream API\n"
" 'tag' : Tag API\n"
" 'am' : AM API\n"
" If not specified, %s API will be used.\n", COMM_TYPE_DEFAULT);
fprintf(stderr, " -i Number of iterations to run. Client and server must "
"have the same value. (default = %d).\n",
num_iterations);
fprintf(stderr, " -v Number of buffers in a single data "
"transfer function call. (default = %ld).\n",
iov_cnt);
print_common_help();
fprintf(stderr, "\n");
}
/**
* Parse the command line arguments.
*/
static parse_cmd_status_t parse_cmd(int argc, char *const argv[],
char **server_addr, char **listen_addr,
send_recv_type_t *send_recv_type)
{
int c = 0;
int port;
while ((c = getopt(argc, argv, "a:l:p:c:6i:s:v:m:h")) != -1) {
switch (c) {
case 'a':
*server_addr = optarg;
break;
case 'c':
if (!strcasecmp(optarg, "stream")) {
*send_recv_type = CLIENT_SERVER_SEND_RECV_STREAM;
} else if (!strcasecmp(optarg, "tag")) {
*send_recv_type = CLIENT_SERVER_SEND_RECV_TAG;
} else if (!strcasecmp(optarg, "am")) {
*send_recv_type = CLIENT_SERVER_SEND_RECV_AM;
} else {
fprintf(stderr, "Wrong communication type %s. "
"Using %s as default\n", optarg, COMM_TYPE_DEFAULT);
*send_recv_type = CLIENT_SERVER_SEND_RECV_DEFAULT;
}
break;
case 'l':
*listen_addr = optarg;
break;
case 'p':
port = atoi(optarg);
if ((port < 0) || (port > UINT16_MAX)) {
fprintf(stderr, "Wrong server port number %d\n", port);
return PARSE_CMD_STATUS_ERROR;
}
server_port = port;
break;
case '6':
ai_family = AF_INET6;
break;
case 'i':
num_iterations = atoi(optarg);
break;
case 's':
test_string_length = atol(optarg);
if (test_string_length < 0) {
fprintf(stderr, "Wrong string size %ld\n", test_string_length);
return PARSE_CMD_STATUS_ERROR;
}
break;
case 'v':
iov_cnt = atol(optarg);
if (iov_cnt <= 0) {
fprintf(stderr, "Wrong iov count %ld\n", iov_cnt);
return PARSE_CMD_STATUS_ERROR;
}
break;
case 'm':
test_mem_type = parse_mem_type(optarg);
if (test_mem_type == UCS_MEMORY_TYPE_LAST) {
return PARSE_CMD_STATUS_ERROR;
}
break;
case 'h':
usage();
return PARSE_CMD_STATUS_PRINT_HELP;
default:
usage();
return PARSE_CMD_STATUS_ERROR;
}
}
return PARSE_CMD_STATUS_OK;
}
static char* sockaddr_get_ip_str(const struct sockaddr_storage *sock_addr,
char *ip_str, size_t max_size)
{
struct sockaddr_in addr_in;
struct sockaddr_in6 addr_in6;
switch (sock_addr->ss_family) {
case AF_INET:
memcpy(&addr_in, sock_addr, sizeof(struct sockaddr_in));
inet_ntop(AF_INET, &addr_in.sin_addr, ip_str, max_size);
return ip_str;
case AF_INET6:
memcpy(&addr_in6, sock_addr, sizeof(struct sockaddr_in6));
inet_ntop(AF_INET6, &addr_in6.sin6_addr, ip_str, max_size);
return ip_str;
default:
return "Invalid address family";
}
}
static char* sockaddr_get_port_str(const struct sockaddr_storage *sock_addr,
char *port_str, size_t max_size)
{
struct sockaddr_in addr_in;
struct sockaddr_in6 addr_in6;
switch (sock_addr->ss_family) {
case AF_INET:
memcpy(&addr_in, sock_addr, sizeof(struct sockaddr_in));
snprintf(port_str, max_size, "%d", ntohs(addr_in.sin_port));
return port_str;
case AF_INET6:
memcpy(&addr_in6, sock_addr, sizeof(struct sockaddr_in6));
snprintf(port_str, max_size, "%d", ntohs(addr_in6.sin6_port));
return port_str;
default:
return "Invalid address family";
}
}
static int client_server_communication(ucp_worker_h worker, ucp_ep_h ep,
send_recv_type_t send_recv_type,
int is_server, int current_iter)
{
int ret;
switch (send_recv_type) {
case CLIENT_SERVER_SEND_RECV_STREAM:
/* Client-Server communication via Stream API */
ret = send_recv_stream(worker, ep, is_server, current_iter);
break;
case CLIENT_SERVER_SEND_RECV_TAG:
/* Client-Server communication via Tag-Matching API */
ret = send_recv_tag(worker, ep, is_server, current_iter);
break;
case CLIENT_SERVER_SEND_RECV_AM:
/* Client-Server communication via AM API. */
ret = send_recv_am(worker, ep, is_server, current_iter);
break;
default:
fprintf(stderr, "unknown send-recv type %d\n", send_recv_type);
return -1;
}
return ret;
}
/**
* Create a ucp worker on the given ucp context.
*/
static int init_worker(ucp_context_h ucp_context, ucp_worker_h *ucp_worker)
{
ucp_worker_params_t worker_params;
ucs_status_t status;
int ret = 0;
memset(&worker_params, 0, sizeof(worker_params));
worker_params.field_mask = UCP_WORKER_PARAM_FIELD_THREAD_MODE;
worker_params.thread_mode = UCS_THREAD_MODE_SINGLE;
status = ucp_worker_create(ucp_context, &worker_params, ucp_worker);
if (status != UCS_OK) {
fprintf(stderr, "failed to ucp_worker_create (%s)\n", ucs_status_string(status));
ret = -1;
}
return ret;
}
/**
* The callback on the server side which is invoked upon receiving a connection
* request from the client.
*/
static void server_conn_handle_cb(ucp_conn_request_h conn_request, void *arg)
{
ucx_server_ctx_t *context = arg;
ucp_conn_request_attr_t attr;
char ip_str[IP_STRING_LEN];
char port_str[PORT_STRING_LEN];
ucs_status_t status;
attr.field_mask = UCP_CONN_REQUEST_ATTR_FIELD_CLIENT_ADDR;
status = ucp_conn_request_query(conn_request, &attr);
if (status == UCS_OK) {
printf("Server received a connection request from client at address %s:%s\n",
sockaddr_get_ip_str(&attr.client_address, ip_str, sizeof(ip_str)),
sockaddr_get_port_str(&attr.client_address, port_str, sizeof(port_str)));
} else if (status != UCS_ERR_UNSUPPORTED) {
fprintf(stderr, "failed to query the connection request (%s)\n",
ucs_status_string(status));
}
if (context->conn_request == NULL) {
context->conn_request = conn_request;
} else {
/* The server is already handling a connection request from a client,
* reject this new one */
printf("Rejecting a connection request. "
"Only one client at a time is supported.\n");
status = ucp_listener_reject(context->listener, conn_request);
if (status != UCS_OK) {
fprintf(stderr, "server failed to reject a connection request: (%s)\n",
ucs_status_string(status));
}
}
}
static ucs_status_t server_create_ep(ucp_worker_h data_worker,
ucp_conn_request_h conn_request,
ucp_ep_h *server_ep)
{
ucp_ep_params_t ep_params;
ucs_status_t status;
/* Server creates an ep to the client on the data worker.
* This is not the worker the listener was created on.
* The client side should have initiated the connection, leading
* to this ep's creation */
ep_params.field_mask = UCP_EP_PARAM_FIELD_ERR_HANDLER |
UCP_EP_PARAM_FIELD_CONN_REQUEST;
ep_params.conn_request = conn_request;
ep_params.err_handler.cb = err_cb;
ep_params.err_handler.arg = NULL;
status = ucp_ep_create(data_worker, &ep_params, server_ep);
if (status != UCS_OK) {
fprintf(stderr, "failed to create an endpoint on the server: (%s)\n",
ucs_status_string(status));
}
return status;
}
/**
* Initialize the server side. The server starts listening on the set address.
*/
static ucs_status_t
start_server(ucp_worker_h ucp_worker, ucx_server_ctx_t *context,
ucp_listener_h *listener_p, const char *address_str)
{
struct sockaddr_storage listen_addr;
ucp_listener_params_t params;
ucp_listener_attr_t attr;
ucs_status_t status;
char ip_str[IP_STRING_LEN];
char port_str[PORT_STRING_LEN];
set_sock_addr(address_str, &listen_addr);
params.field_mask = UCP_LISTENER_PARAM_FIELD_SOCK_ADDR |
UCP_LISTENER_PARAM_FIELD_CONN_HANDLER;
params.sockaddr.addr = (const struct sockaddr*)&listen_addr;
params.sockaddr.addrlen = sizeof(listen_addr);
params.conn_handler.cb = server_conn_handle_cb;
params.conn_handler.arg = context;
/* Create a listener on the server side to listen on the given address.*/
status = ucp_listener_create(ucp_worker, ¶ms, listener_p);
if (status != UCS_OK) {
fprintf(stderr, "failed to listen (%s)\n", ucs_status_string(status));
goto out;
}
/* Query the created listener to get the port it is listening on. */
attr.field_mask = UCP_LISTENER_ATTR_FIELD_SOCKADDR;
status = ucp_listener_query(*listener_p, &attr);
if (status != UCS_OK) {
fprintf(stderr, "failed to query the listener (%s)\n",
ucs_status_string(status));
ucp_listener_destroy(*listener_p);
goto out;
}
fprintf(stderr, "server is listening on IP %s port %s\n",
sockaddr_get_ip_str(&attr.sockaddr, ip_str, IP_STRING_LEN),
sockaddr_get_port_str(&attr.sockaddr, port_str, PORT_STRING_LEN));
printf("Waiting for connection...\n");
out:
return status;
}
ucs_status_t register_am_recv_callback(ucp_worker_h worker)
{
ucp_am_handler_param_t param;
param.field_mask = UCP_AM_HANDLER_PARAM_FIELD_ID |
UCP_AM_HANDLER_PARAM_FIELD_CB |
UCP_AM_HANDLER_PARAM_FIELD_ARG;
param.id = TEST_AM_ID;
param.cb = ucp_am_data_cb;
param.arg = worker; /* not used in our callback */
return ucp_worker_set_am_recv_handler(worker, ¶m);
}
static int client_server_do_work(ucp_worker_h ucp_worker, ucp_ep_h ep,
send_recv_type_t send_recv_type, int is_server)
{
int i, ret = 0;
ucs_status_t status;
connection_closed = 0;
for (i = 0; i < num_iterations; i++) {
ret = client_server_communication(ucp_worker, ep, send_recv_type,
is_server, i);
if (ret != 0) {
fprintf(stderr, "%s failed on iteration #%d\n",
(is_server ? "server": "client"), i + 1);
goto out;
}
}
/* Register recv callback on the client side to receive FIN message */
if (!is_server && (send_recv_type == CLIENT_SERVER_SEND_RECV_AM)) {
status = register_am_recv_callback(ucp_worker);
if (status != UCS_OK) {
ret = -1;
goto out;
}
}
/* FIN message in reverse direction to acknowledge delivery */
ret = client_server_communication(ucp_worker, ep, send_recv_type,
!is_server, i + 1);
if (ret != 0) {
fprintf(stderr, "%s failed on FIN message\n",
(is_server ? "server": "client"));
goto out;
}
printf("%s FIN message\n", is_server ? "sent" : "received");
/* Server waits until the client closed the connection after receiving FIN */
while (is_server && !connection_closed) {
ucp_worker_progress(ucp_worker);
}
out:
return ret;
}
static int run_server(ucp_context_h ucp_context, ucp_worker_h ucp_worker,
char *listen_addr, send_recv_type_t send_recv_type)
{
ucx_server_ctx_t context;
ucp_worker_h ucp_data_worker;
ucp_ep_h server_ep;
ucs_status_t status;
int ret;
/* Create a data worker (to be used for data exchange between the server
* and the client after the connection between them was established) */
ret = init_worker(ucp_context, &ucp_data_worker);
if (ret != 0) {
goto err;
}
if (send_recv_type == CLIENT_SERVER_SEND_RECV_AM) {
status = register_am_recv_callback(ucp_data_worker);
if (status != UCS_OK) {
ret = -1;
goto err_worker;
}
}
/* Initialize the server's context. */
context.conn_request = NULL;
/* Create a listener on the worker created at first. The 'connection
* worker' - used for connection establishment between client and server.
* This listener will stay open for listening to incoming connection
* requests from the client */
status = start_server(ucp_worker, &context, &context.listener, listen_addr);
if (status != UCS_OK) {
ret = -1;
goto err_worker;
}
/* Server is always up listening */
while (1) {
/* Wait for the server to receive a connection request from the client.
* If there are multiple clients for which the server's connection request
* callback is invoked, i.e. several clients are trying to connect in
* parallel, the server will handle only the first one and reject the rest */
while (context.conn_request == NULL) {
ucp_worker_progress(ucp_worker);
}