-
Notifications
You must be signed in to change notification settings - Fork 7
/
cmnnti.c
2120 lines (1892 loc) · 69.2 KB
/
cmnnti.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
/***** Includes *****/
#include "config.h"
#include <sys/types.h>
#ifdef ENET_FOUND
#include <enet/enet.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#undef NDEBUG
#include <assert.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#include <arpa/inet.h>
#include <atl.h>
#include "evpath.h"
#include "cm_transport.h"
#include <Trios_nnti.h>
#if defined (__INTEL_COMPILER)
# pragma warning (disable: 869)
# pragma warning (disable: 310)
# pragma warning (disable: 1418)
# pragma warning (disable: 180)
# pragma warning (disable: 177)
# pragma warning (disable: 2259)
# pragma warning (disable: 981)
#endif
typedef enum {MAP_EACH_MESSAGE_PULL, MAP_EACH_MESSAGE_PUSH,
PERSISTENT_MAPPED_BUFFER_PULL, PERSISTENT_MAPPED_BUFFER_PUSH,
LAST_PROTOCOL} messaging_protocols;
extern attr_list
libcmnnti_LTX_non_blocking_listen(CManager cm, CMtrans_services svc,
transport_entry trans, attr_list listen_info);
struct nnti_connection_data;
static atom_t CM_PEER_IP = -1;
static atom_t CM_PEER_LISTEN_PORT = -1;
static atom_t CM_NNTI_PORT = -1;
static atom_t CM_NNTI_PARAMS = -1;
static atom_t CM_NNTI_ADDR = -1;
static atom_t CM_NNTI_ENET_CONTROL = -1;
static atom_t CM_NNTI_IMMEDIATE_PULL_WAIT = -1;
static atom_t CM_IP_HOSTNAME = -1;
static atom_t CM_TRANSPORT = -1;
static atom_t CM_NNTI_TRANSPORT = -1;
static atom_t CM_ENET_PORT = -1;
static atom_t CM_ENET_ADDR = -1;
static atom_t CM_TRANSPORT_RELIABLE = -1;
char *NNTI_result_string[] = {
"NNTI_OK",
"NNTI_EIO",
"NNTI_EMSGSIZE",
"NNTI_ECANCELED",
"NNTI_ETIMEDOUT",
"NNTI_EINVAL",
"NNTI_ENOMEM",
"NNTI_ENOENT",
"NNTI_ENOTSUP",
"NNTI_EEXIST",
"NNTI_EBADRPC",
"NNTI_ENOTINIT",
"NNTI_EPERM",
"NNTI_EAGAIN"};
#define NNTI_ERROR_STRING(error) ((error == 0)? NNTI_result_string[error] : NNTI_result_string[error-1000])
/* requet queue for rdma pull scheduling */
struct pull_request;
struct pull_request_queue;
struct pull_sched_context;
struct client_message;
typedef int (* nnti_pull_sched_func) (struct pull_request *, struct pull_sched_context *, void *callback_data);
typedef void (* nnti_pull_completion_func) (struct pull_request *, struct pull_sched_context *, void *callback_data);
typedef struct nnti_transport_data {
CManager cm;
CMtrans_services svc;
int socket_fd;
int self_ip;
int self_port;
attr_list listen_attrs;
char *self_hostname;
char* incoming;
NNTI_buffer_t mr_recvs;
NNTI_work_request_t wr_recvs;
NNTI_transport_t trans_hdl;
pthread_t listen_thread;
pthread_t listen_thread2;
pthread_cond_t cond;
int use_enet;
int immediate_pull_wait;
int shutdown_listen_thread;
struct nnti_connection_data *connections;
int cache_maps;
/* for scheduling */
struct pull_request_queue *pull_req_queue;
struct pull_request_queue *ongoing_req_queue;
nnti_pull_sched_func nnti_pull_scheduler;
void *nnti_pull_sched_data;
nnti_pull_completion_func nnti_pull_completion;
void *nnti_pull_completion_data;
int request_size;
struct nnti_connection_data **request_list;
#ifdef ENET_FOUND
/* enet support */
ENetHost *enet_server;
#endif
int enet_listen_port;
attr_list characteristics;
} *nnti_transport_data_ptr;
typedef struct nnti_connection_data {
char *peer_hostname;
int nnti_port;
char* nnti_params;
CMbuffer read_buffer;
int read_buf_len;
nnti_transport_data_ptr ntd;
CMConnection conn;
attr_list attrs;
struct nnti_connection_data *next;
NNTI_peer_t peer_hdl;
long size;
uint64_t cksum;
uint64_t raddr;
int acks_offset;
NNTI_buffer_t mr_send; // registered memory region to send a message to client
char *send_buffer;
NNTI_buffer_t res_addr;
NNTI_buffer_t buf_addr;
int piggyback_size_max;
void *outgoing_mapped_region;
long outgoing_mapped_size;
NNTI_buffer_t outgoing_mapped_mr;
CMbuffer mapped_write_buffer;
NNTI_buffer_t incoming_mapped_region;
long incoming_mapped_size;
NNTI_buffer_t incoming_mapped_mr;
void *pending_request_msg_info;
long pending_request_size;
/* enet support */
char *remote_host;
int remote_IP;
int remote_contact_port;
int use_enet;
#ifdef ENET_FOUND
ENetPeer *peer;
ENetPacket *packet;
#endif
NNTI_buffer_t mr_pull;
NNTI_work_request_t wr_pull;
} *nnti_conn_data_ptr;
#ifndef IOV_MAX
/* this is not defined in some places where it should be. Conservative. */
#define IOV_MAX 16
#endif
#define ENET_PIGGYBACK_SIZE 102500
typedef enum {enet, nnti} control_transport;
typedef struct _send_handle {
control_transport t;
int size;
#ifdef ENET_FOUND
ENetPacket *packet;
#endif
nnti_conn_data_ptr ncd;
} send_handle;
static int buffer_pack(NNTI_transport_t *trans_hdl, void *input, void **output, uint64_t *output_size)
{
NNTI_dt_sizeof(trans_hdl, input, output_size);
*output=(char*)malloc(*output_size);
NNTI_dt_pack(trans_hdl, input, *output, *output_size);
return(0);
}
//static int buffer_free(NNTI_transport_t *trans_hdl, void *input)
//{
// NNTI_dt_free(trans_hdl, input);
//
// return(0);
//}
static int buffer_unpack(NNTI_transport_t *trans_hdl, void *input, int32_t input_size, void *output)
{
NNTI_dt_unpack(trans_hdl, output, input, input_size);
return(0);
}
send_handle
get_control_message_buffer(nnti_conn_data_ptr ncd, struct client_message **mp,
int size)
{
send_handle ret;
memset(&ret, 0, sizeof(ret));
if (ncd->use_enet) {
ret.t = enet;
#ifdef ENET_FOUND
/* Create a reliable packet of the right size */
ret.packet = enet_packet_create (NULL, size,
ENET_PACKET_FLAG_RELIABLE);
*mp = (struct client_message *) ret.packet->data;
memset(ret.packet->data, 0, size);
#endif
} else {
assert(size < NNTI_REQUEST_BUFFER_SIZE);
ret.t = nnti;
*mp = (struct client_message*)ncd -> send_buffer;
}
ret.ncd = ncd;
ret.size = size;
return ret;
}
int
send_control_message(send_handle h)
{
CManager cm = h.ncd->ntd->cm;
CMtrans_services svc = h.ncd->ntd->svc;
if (h.t == enet) {
#ifdef ENET_FOUND
svc->trace_out(cm, "CMNNTI/ENET control write of %d bytes on peer %p",
h.size, h.ncd->peer);
/* Send the packet to the peer over channel id 0. */
if (enet_peer_send (h.ncd->peer, 0, h.packet) == -1) {
svc->trace_out(cm, "CMNNTI/ENET control write failed.");
return 0;
}
enet_host_flush (h.ncd->ntd->enet_server);
#ifdef NOT_DEF
static time_t last_flush_call = 0;
if (last_flush_call == 0) {
enet_host_flush (h.ncd->ntd->enet_server);
last_flush_call = time(NULL);
} else {
time_t now = time(NULL);
if (now > last_flush_call) {
last_flush_call = now;
enet_host_flush (h.ncd->ntd->enet_server);
}
}
#endif
#endif
} else {
svc->trace_out(cm, "CMNNTI control write of %d bytes",
h.size);
NNTI_status_t status;
int timeout = 1000;
NNTI_work_request_t send_wr;
int err = NNTI_send(&h.ncd->peer_hdl, &h.ncd->mr_send, NULL, &send_wr);
if (err != NNTI_OK) {
fprintf (stderr, "Error: NNTI_send() returned non-zero: %d %s\n", err, NNTI_ERROR_STRING(err));
return 1;
}
svc->trace_out(cm, " NNTI_send() returned. Call wait... ");
/* Wait for message to be sent */
timeout = 1000;
again:
err = NNTI_wait(&send_wr, timeout, &status);
if (err == NNTI_ETIMEDOUT) {
timeout *=2;
if (h.ncd->ntd->shutdown_listen_thread) return 0;
goto again;
}
if (err != NNTI_OK) {
fprintf (stderr, "Error: NNTI_wait() for sending returned non-zero: %d %s\n", err, NNTI_ERROR_STRING(err));
return 1;
}
svc->trace_out(cm, " NNTI_wait() of send request returned... ");
}
return 1;
}
#ifdef ENET_FOUND
extern void
enet_non_blocking_listen(CManager cm, CMtrans_services svc,
transport_entry trans, attr_list listen_info);
#endif
static nnti_conn_data_ptr
create_nnti_conn_data(svc)
CMtrans_services svc;
{
nnti_conn_data_ptr nnti_conn_data =
svc->malloc_func(sizeof(struct nnti_connection_data));
memset(nnti_conn_data, 0, sizeof(struct nnti_connection_data));
nnti_conn_data->read_buffer = NULL;
nnti_conn_data->nnti_port = -1;
nnti_conn_data->peer_hostname = NULL;
nnti_conn_data->next = NULL;
return nnti_conn_data;
}
static void
add_connection(nnti_transport_data_ptr ntd, nnti_conn_data_ptr ncd)
{
nnti_conn_data_ptr tmp = ntd->connections;
ntd->connections = ncd;
ncd->next = tmp;
}
static void
unlink_connection(nnti_transport_data_ptr ntd, nnti_conn_data_ptr ncd)
{
if (ntd->connections == ncd) {
ntd->connections = ncd->next;
ncd->next = NULL;
} else {
nnti_conn_data_ptr tmp = ntd->connections;
while (tmp != NULL) {
if (tmp->next == ncd) {
tmp->next = ncd->next;
ncd->next = NULL;
return;
}
}
printf("Serious internal error, NNTI unlink_connection, connection not found\n");
}
}
enum {CMNNTI_CONNECT=1, CMNNTI_PIGGYBACK=2, CMNNTI_PULL_REQUEST=3, CMNNTI_PULL_COMPLETE=4, CMNNTI_LAST_MSG_TYPE=7};
char *msg_type_name[] =
{"NO MESSAGE", "CMNNTI_CONNECT", "CMNNTI_PIGGYBACK", "CMNNTI_PULL_REQUEST", "CMNNTI_PULL_COMPLETE"};
struct connect_message {
short message_type;
int nnti_port;
int enet_port;
uint32_t enet_ip;
uint32_t name_len;
char name[1];
};
static int
initiate_nnti_conn(cm, svc, trans, attrs, nnti_conn_data, conn_attr_list)
CManager cm;
CMtrans_services svc;
transport_entry trans;
attr_list attrs;
nnti_conn_data_ptr nnti_conn_data;
attr_list conn_attr_list;
{
int int_port_num;
nnti_transport_data_ptr ntd = (nnti_transport_data_ptr) trans->trans_data;
char *host_name, *nnti_transport, *params;
char server_url[256];
struct connect_message *cmsg;
NNTI_work_request_t send_wr;
if (!query_attr(attrs, CM_IP_HOSTNAME, /* type pointer */ NULL,
/* value pointer */ (attr_value *)(long) & host_name)) {
svc->trace_out(cm, "NNTI transport found no NNTI_HOST attribute");
host_name = NULL;
} else {
svc->trace_out(cm, "NNTI transport connect to host %s", host_name);
}
if (host_name == NULL)
return -1;
if (!query_attr(attrs, CM_NNTI_PORT, /* type pointer */ NULL,
/* value pointer */ (attr_value *) (long) &int_port_num)) {
svc->trace_out(cm, "CMNNTI transport found no NNTI_PORT attribute");
return -1;
} else {
svc->trace_out(cm, "CMNNTI transport connect to port %d", int_port_num);
}
if (!query_attr(attrs, CM_NNTI_PARAMS, /* type pointer */ NULL,
/* value pointer */ (attr_value *) (long) ¶ms)) {
svc->trace_out(cm, "CMNNTI transport found no NNTI_PARAMS attribute");
params = strdup("");
} else {
svc->trace_out(cm, "CMNNTI transport connect with params %s", params);
}
if (!get_string_attr(attrs, CM_NNTI_TRANSPORT, &nnti_transport)) {
svc->trace_out(cm, "NNTI transport found no NNTI_TRANSPORT attribute");
return -1;
} else {
svc->trace_out(cm, "NNTI transport connect using transport %s", nnti_transport);
}
sprintf(server_url, "%s://%s:%d/%s", nnti_transport, host_name, int_port_num, params);
if (ntd->self_port == -1) {
libcmnnti_LTX_non_blocking_listen(cm, svc, trans, NULL);
}
#ifdef ENET_FOUND
if (ntd->enet_server == NULL) {
enet_non_blocking_listen(cm, svc, trans, NULL);
}
#endif
int timeout = 500;
ntd->svc->trace_out(trans->cm, "Connecting to URL \"%s\"", server_url);
DROP_CM_LOCK(svc, trans->cm);
int err = NNTI_connect(&ntd->trans_hdl, server_url, timeout,
&nnti_conn_data->peer_hdl);
ACQUIRE_CM_LOCK(svc, trans->cm);
if (err != NNTI_OK) {
fprintf (stderr, "Error: NNTI_connect() returned non-zero: %d, %s\n", err, NNTI_ERROR_STRING(err));
return 1;
}
/* register memory regions */
err = NNTI_alloc(&ntd->trans_hdl, NNTI_REQUEST_BUFFER_SIZE, 1,
NNTI_SEND_SRC, &nnti_conn_data->mr_send);
if (err != NNTI_OK) {
fprintf (stderr, "Error: NNTI_alloc(SEND_SRC) for client message returned non-zero: %d %s\n", err, NNTI_ERROR_STRING(err));
return 1;
}
cmsg = (void*)NNTI_BUFFER_C_POINTER(&nnti_conn_data->mr_send);
cmsg->message_type = CMNNTI_CONNECT;
cmsg->nnti_port = ntd->self_port;
cmsg->enet_port = ntd->enet_listen_port;
cmsg->enet_ip = ntd->self_ip;
cmsg->name_len = strlen(ntd->self_hostname);
strcpy(&cmsg->name[0], ntd->self_hostname);
svc->trace_out(cm, "CMNNTI initiate sending connect message to remote host");
err = NNTI_send(&nnti_conn_data->peer_hdl, &nnti_conn_data->mr_send,
NULL, &send_wr);
if (err != NNTI_OK) {
fprintf (stderr, "Error: NNTI_send() returned non-zero: %d %s\n", err, NNTI_ERROR_STRING(err));
return 1;
}
svc->trace_out(trans->cm, " NNTI_send() returned. Call wait... ");
nnti_conn_data->nnti_port = int_port_num;
nnti_conn_data->peer_hostname = strdup(host_name);
nnti_conn_data->ntd = ntd;
/* Wait for message to be sent */
NNTI_status_t status;
timeout = 500;
again:
err = NNTI_wait(&send_wr, timeout, &status);
if ((err == NNTI_ETIMEDOUT) || (err == NNTI_EAGAIN)) {
if (nnti_conn_data->ntd->shutdown_listen_thread) return 0;
timeout *=2;
goto again;
}
if (err != NNTI_OK) {
fprintf (stderr, "Error: NNTI_wait() for sending returned non-zero: %d %s\n", err, NNTI_ERROR_STRING(err));
return 1;
}
svc->trace_out(trans->cm, " NNTI_wait() of send CONNECT request returned... ");
svc->trace_out(cm, "--> NNTI Connection established");
nnti_conn_data->nnti_port = int_port_num;
nnti_conn_data->nnti_params = params;
nnti_conn_data->ntd = ntd;
nnti_conn_data->send_buffer = (char*)NNTI_BUFFER_C_POINTER(&nnti_conn_data->mr_send);;
return 1;
}
#ifdef ENET_FOUND
static void nnti_enet_service_network(CManager cm, void *void_trans);
extern void
enet_non_blocking_listen(CManager cm, CMtrans_services svc,
transport_entry trans, attr_list listen_info)
{
nnti_transport_data_ptr ntd = (nnti_transport_data_ptr) trans->trans_data;
ENetAddress address;
ENetHost * server;
svc->trace_out(cm, "CMnnti begin enet listen\n");
address.host = ENET_HOST_ANY;
if (ntd->enet_server != NULL) {
/* we're already listening */
return;
}
long seedval = time(NULL) + getpid();
/* port num is free. Constrain to range 26000 : 26100 */
int low_bound = 26000;
int high_bound = 26100;
int size = high_bound - low_bound;
int tries = 10;
srand48(seedval);
while (tries > 0) {
int target = low_bound + size * drand48();
address.port = target;
svc->trace_out(cm, "CMnnti trying to bind enet port %d", target);
server = enet_host_create (& address /* the address to bind the server host to */,
0 /* allow dynamic clients (This is supported by the GaTech mod ENET only) */,
1 /* allow up to 2 channels to be used, 0 and 1 */,
0 /* assume any amount of incoming bandwidth */,
0 /* assume any amount of outgoing bandwidth */);
tries--;
if (server != NULL) tries = 0;
if (tries == 5) {
/* try reseeding in case we're in sync with another process */
srand48(time(NULL) + getpid());
}
}
if (server == NULL) {
fprintf(stderr, "Failed after 5 attempts to bind to a random port. Lots of undead servers on this host?\n");
return;
}
ntd->enet_server = server;
ntd->enet_listen_port = address.port;
svc->fd_add_select(cm, enet_host_get_sock_fd (server),
(select_list_func) nnti_enet_service_network, (void*)cm, (void*)trans);
return;
}
static int
initiate_enet_link(CManager cm, CMtrans_services svc, transport_entry trans,
nnti_conn_data_ptr nnti_conn_data, char *host_name, int int_port_num)
{
nnti_transport_data_ptr ntd = (nnti_transport_data_ptr) trans->trans_data;
ENetAddress address;
ENetEvent event;
ENetPeer *peer;
struct in_addr sin_addr;
enet_address_set_host (& address, host_name);
sin_addr.s_addr = address.host;
svc->trace_out(cm, "Attempting ENET RUDP connection, host=\"%s\", IP = %s, port %d",
host_name == 0 ? "(unknown)" : host_name,
inet_ntoa(sin_addr),
int_port_num);
enet_address_set_host (& address, host_name);
address.port = (unsigned short) int_port_num;
if (ntd->enet_server == NULL) {
enet_non_blocking_listen(cm, svc, trans, NULL);
}
/* Initiate the connection, allocating the two channels 0 and 1. */
peer = enet_host_connect (ntd->enet_server, & address, 1, 0);
if (peer == NULL) {
fprintf (stderr,
"No available peers for initiating an ENet connection.\n");
exit (EXIT_FAILURE);
}
/* Wait up to 5 seconds for the connection attempt to succeed. */
if (enet_host_service (ntd->enet_server, & event, 5000) > 0 &&
event.type == ENET_EVENT_TYPE_CONNECT) {
svc->trace_out(cm, "Connection to %s:%d succeeded.\n", inet_ntoa(sin_addr), address.port);
} else {
/* Either the 5 seconds are up or a disconnect event was */
/* received. Reset the peer in the event the 5 seconds */
/* had run out without any significant event. */
enet_peer_reset (peer);
printf ("Connection to %s:%d failed.", inet_ntoa(sin_addr), address.port);
return 0;
}
svc->trace_out(cm, "--> Enet Connection established");
nnti_conn_data->remote_host = host_name == NULL ? NULL : strdup(host_name);
nnti_conn_data->remote_IP = address.host;
nnti_conn_data->remote_contact_port = int_port_num;
nnti_conn_data->ntd = ntd;
nnti_conn_data->peer = peer;
peer->data = nnti_conn_data;
return 1;
}
#endif
#ifdef ENET_FOUND
static int
initiate_enet_conn(CManager cm, CMtrans_services svc, transport_entry trans,
attr_list attrs, nnti_conn_data_ptr nnti_conn_data,
attr_list conn_attr_list)
{
int int_port_num;
char *host_name;
static int host_ip = 0;
(void)conn_attr_list;
if (!query_attr(attrs, CM_IP_HOSTNAME, /* type pointer */ NULL,
/* value pointer */ (attr_value *)(long) & host_name)) {
svc->trace_out(cm, "Cmnnti/Enet transport found no CM_IP_HOSTNAME attribute");
host_name = NULL;
} else {
svc->trace_out(cm, "Cmnnti/Enet transport connect to host %s", host_name);
}
if (!query_attr(attrs, CM_ENET_ADDR, /* type pointer */ NULL,
/* value pointer */ (attr_value *)(long) & host_ip)) {
svc->trace_out(cm, "Cmnnti/Enet transport found no CM_ENET_ADDR attribute");
/* wasn't there */
host_ip = 0;
} else {
svc->trace_out(cm, "Cmnnti/Enet transport connect to host_IP %lx", host_ip);
}
if ((host_name == NULL) && (host_ip == 0))
return -1;
if (!query_attr(attrs, CM_ENET_PORT, /* type pointer */ NULL,
/* value pointer */ (attr_value *)(long) & int_port_num)) {
svc->trace_out(cm, "Cmnnti/Enet transport found no CM_ENET_PORT attribute");
return -1;
} else {
svc->trace_out(cm, "Cmnnti/Enet transport connect to port %d", int_port_num);
}
return initiate_enet_link(cm, svc, trans, nnti_conn_data, host_name, int_port_num);
}
#endif
/*
* Initiate a connection to a nnti group.
*/
extern CMConnection
libcmnnti_LTX_initiate_conn(cm, svc, trans, attrs)
CManager cm;
CMtrans_services svc;
transport_entry trans;
attr_list attrs;
{
nnti_conn_data_ptr nnti_conn_data = create_nnti_conn_data(svc);
attr_list conn_attr_list = create_attr_list();
CMConnection conn;
if (initiate_nnti_conn(cm, svc, trans, attrs, nnti_conn_data, conn_attr_list) != 1) {
return NULL;
}
/* this size might be overridden */
nnti_conn_data->piggyback_size_max = NNTI_REQUEST_BUFFER_SIZE;
#ifdef ENET_FOUND
int enet_conn_status;
sleep(1);
enet_conn_status = initiate_enet_conn(cm, svc, trans, attrs, nnti_conn_data, conn_attr_list);
switch (enet_conn_status) {
case -1:
svc->trace_out(cm, "NO ENET Contact info provided.");
nnti_conn_data->use_enet = 0;
break;
case 0:
svc->trace_out(cm, "ENET Connection failed.");
return NULL;
default:
nnti_conn_data->use_enet = 1;
nnti_conn_data->piggyback_size_max = ENET_PIGGYBACK_SIZE;
}
#endif
add_attr(conn_attr_list, CM_IP_HOSTNAME, Attr_String,
(attr_value) strdup(nnti_conn_data->peer_hostname));
add_attr(conn_attr_list, CM_NNTI_PORT, Attr_Int4,
(attr_value) (long)nnti_conn_data->nnti_port);
add_attr(conn_attr_list, CM_NNTI_PARAMS, Attr_String,
(attr_value) (long)nnti_conn_data->nnti_params);
conn = svc->connection_create(trans, nnti_conn_data, conn_attr_list);
add_connection(nnti_conn_data->ntd, nnti_conn_data);
nnti_conn_data->conn = conn;
nnti_conn_data->attrs = conn_attr_list;
return conn;
}
/*
* Check to see that if we were to attempt to initiate a connection as
* indicated by the attribute list, would we be connecting to ourselves?
*/
extern int
libcmnnti_LTX_self_check(cm, svc, trans, attrs)
CManager cm;
CMtrans_services svc;
transport_entry trans;
attr_list attrs;
{
nnti_transport_data_ptr ntd = trans->trans_data;
int int_port_num;
char *host_name;
char my_host_name[256];
static int IP = 0;
get_IP_config(my_host_name, sizeof(host_name), &IP, NULL, NULL, NULL,
NULL, svc->trace_out, (void *)cm);
if (IP == 0) {
IP = ntohl(INADDR_LOOPBACK);
}
if (!query_attr(attrs, CM_IP_HOSTNAME, /* type pointer */ NULL,
/* value pointer */ (attr_value *)(long) & host_name)) {
svc->trace_out(cm, "CMself check NNTI transport found no IP_HOST attribute");
host_name = NULL;
}
if (!query_attr(attrs, CM_NNTI_PORT, /* type pointer */ NULL,
/* value pointer */ (attr_value *)(long) & int_port_num)) {
svc->trace_out(cm, "CMself check NNTI transport found no NNTI_PORT attribute");
return 0;
}
if (host_name && (strcmp(host_name, my_host_name) != 0)) {
svc->trace_out(cm, "CMself check - Hostnames don't match");
return 0;
}
if (int_port_num != ntd->self_port) {
svc->trace_out(cm, "CMself check - Ports don't match");
return 0;
}
svc->trace_out(cm, "CMself check returning TRUE");
return 1;
}
extern int
libcmnnti_LTX_connection_eq(cm, svc, trans, attrs, ncd)
CManager cm;
CMtrans_services svc;
transport_entry trans;
attr_list attrs;
nnti_conn_data_ptr ncd;
{
int int_port_num;
char *host_name = NULL;
if (!query_attr(attrs, CM_IP_HOSTNAME, /* type pointer */ NULL,
/* value pointer */ (attr_value *)(long) & host_name)) {
svc->trace_out(cm, "NNTI transport found no NNTI_HOST attribute");
host_name = NULL;
} else {
svc->trace_out(cm, "NNTI transport connect to host %s", host_name);
}
if (!query_attr(attrs, CM_NNTI_PORT, /* type pointer */ NULL,
/* value pointer */ (attr_value *) (long) &int_port_num)) {
svc->trace_out(cm, "Conn Eq CMNnti transport found no NNTI_PORT attribute");
return 0;
}
svc->trace_out(cm, "CMNnti Conn_eq comparing host/ports %s/%d and %s/%d",
ncd->peer_hostname, ncd->nnti_port,
host_name, int_port_num);
if ((ncd->nnti_port == int_port_num) &&
(strcmp(ncd->peer_hostname, host_name) == 0)) {
svc->trace_out(cm, "CMNnti Conn_eq returning TRUE");
return 1;
}
svc->trace_out(cm, "CMNnti Conn_eq returning FALSE");
return 0;
}
struct piggyback {
unsigned int size; // size of message payload
char payload[1];
};
struct pull_request {
unsigned long size; /* size of message to pull */
void *msg_info; /* returned unchanged */
int reuse_prior_mapping;
char packed_src_buf[1];
};
struct pull_complete_notify {
void *msg_info;
};
struct client_message {
short message_type;
union {
struct piggyback pig;
struct pull_request pull;
struct pull_complete_notify pull_complete;
};
};
enum pull_request_state {
GET_SCHED_QUEUED, // in queue, ready to be scheduled
GET_SCHED_GET_ISSUED, // GET command has been issued
GET_SCHED_COMPLETED, // GET command has finished successfully
GET_SCHED_ERROR, // some error/failure has happened
};
struct pull_sched_context {
nnti_conn_data_ptr ncd; // from which connection
int target_stone_id; // local target stone
void *context_data; // other context informaiton such as application phases
// TODO: add source process id and data format id
};
/* queue structure for delayed pull requests */
struct pull_request_queue {
enum pull_request_state state;
int num_errors;
struct pull_sched_context context;
struct pull_request_queue *next;
struct pull_request request;
};
static void
handle_pull_request_message(nnti_conn_data_ptr ncd, CMtrans_services svc, transport_entry trans,
struct client_message *m);
static void
handle_pull_complete_message(nnti_conn_data_ptr ncd, CMtrans_services svc, transport_entry trans,
struct client_message *m);
static void
handle_control_request(nnti_conn_data_ptr ncd, CMtrans_services svc, transport_entry trans,
struct client_message *m);
static int nnti_conn_count = 0;
#define WAIT_MODE_BOTH 1
#define WAIT_MODE_REQUEST 2
#define WAIT_MODE_RDMA 3
typedef struct listen_struct {
nnti_transport_data_ptr ntd;
CMtrans_services svc;
transport_entry trans;
int wait_mode;
} *listen_struct_p;
static void
handle_request_buffer_event(listen_struct_p lsp, NNTI_status_t *wait_status)
{
nnti_transport_data_ptr ntd = lsp->ntd;
CMtrans_services svc = lsp->svc;
transport_entry trans = lsp->trans;
struct connect_message *cm = (struct connect_message *)(wait_status->start+wait_status->offset);
nnti_conn_data_ptr ncd = ntd->connections;
while (ncd != NULL) {
if (strcmp(&wait_status->src.url[0], &ncd->peer_hdl.url[0]) == 0) {
ntd->svc->trace_out(trans->cm, "NNTI data available on existing connection, from host %s, port %d, type %s (%d)",
ncd->peer_hostname, ncd->nnti_port,
msg_type_name[cm->message_type], cm->message_type);
break;
}
ncd = ncd->next;
}
if ((cm->message_type <= 0) || (cm->message_type > CMNNTI_LAST_MSG_TYPE)) {
printf("BAD INCOMING SHORT MESSAGE! (value was %d)\n", cm->message_type);
exit(1);
}
switch (cm->message_type){
case CMNNTI_CONNECT:
{
int err;
attr_list conn_attr_list = NULL;
ntd->svc->trace_out(trans->cm, " client %s:%d (enet %d, %x) is connecting",
cm->name, cm->nnti_port, cm->enet_port, cm->enet_ip);
assert(ncd == NULL);
ncd = create_nnti_conn_data(svc);
ncd->ntd = ntd;
ncd->peer_hdl = wait_status->src;
ncd->peer_hostname = strdup(cm->name);
ncd->nnti_port = cm->nnti_port;
ncd->remote_contact_port = cm->enet_port;
ncd->remote_IP = cm->enet_ip;
ncd->piggyback_size_max = NNTI_REQUEST_BUFFER_SIZE;
ncd->size = NNTI_REQUEST_BUFFER_SIZE;
ncd->raddr = 0;
ncd->cksum = 0;
ncd->acks_offset=(nnti_conn_count++)*NNTI_REQUEST_BUFFER_SIZE;
// ncd->buf_addr = cm->buf_addr;
// ncd->res_addr = cm->res_addr;
/* register memory region for sending acknowledgement to this client */
ntd->svc->trace_out(trans->cm, " ID %d register small send buffer to client %d (offset=%d)...",
cm->nnti_port, nnti_conn_count-1, ncd->acks_offset);
conn_attr_list = create_attr_list();
ncd->conn = svc->connection_create(trans, ncd, conn_attr_list);
ncd->attrs = conn_attr_list;
add_connection(ntd, ncd);
/* alloc memory regions */
err = NNTI_alloc(&ntd->trans_hdl, NNTI_REQUEST_BUFFER_SIZE, 1,
NNTI_SEND_SRC, &ncd->mr_send);
if (err != NNTI_OK) {
fprintf (stderr, "Error: NNTI_alloc(SEND_SRC) for client message returned non-zero: %d %s\n", err, NNTI_ERROR_STRING(err));
return;
}
ncd->send_buffer = (void*)NNTI_BUFFER_C_POINTER(&ncd->mr_send);
}
break;
case CMNNTI_PIGGYBACK:
{
struct client_message *m = (struct client_message *)cm;
CMbuffer read_buffer;
if (ncd == NULL) {
printf("Incoming message failed to match connection!\n");
}
read_buffer = ntd->svc->get_data_buffer(trans->cm, (int)m->pig.size);
memcpy(&((char*)read_buffer->buffer)[0], &(m->pig.payload[0]), m->pig.size);
ntd->svc->add_buffer_to_pending_queue(trans->cm, ncd->conn, read_buffer, m->pig.size);
}
break;
default:
{
struct client_message *m = (struct client_message *)(wait_status->start+wait_status->offset);
if (ncd == NULL) {
printf("Default handler: incoming message failed to match connection!\n");
}
handle_control_request(ncd, svc, trans, m);
}
}
}
int
listen_thread_func(void *vlsp)
{
int timeout = 1000;
listen_struct_p lsp = vlsp;
nnti_transport_data_ptr ntd = lsp->ntd;
CMtrans_services svc = lsp->svc;
transport_entry trans = lsp->trans;
int err;
NNTI_status_t wait_status;
ACQUIRE_CM_LOCK(svc, trans->cm);
int buf_size = 10; // initial size
NNTI_work_request_t **wr_list = malloc(sizeof(wr_list[0]) * buf_size);
while (1) {
unsigned int which;
int wr_count = 1;
if (lsp->wait_mode != WAIT_MODE_RDMA) {
wr_list[0] = &ntd->wr_recvs;
} else {
wr_count = 0;
}
if (lsp->wait_mode != WAIT_MODE_REQUEST) {
if (buf_size < ntd->request_size) {
wr_list = realloc(wr_list, sizeof(wr_list[0]) * ntd->request_size);
buf_size = ntd->request_size;
}
for(which=1; which <= ntd->request_size; which++) {
if (ntd->request_list[which-1] != NULL) {
wr_list[wr_count] = &ntd->request_list[which-1]->wr_pull;
wr_count++;
}
}
}
//err = NNTI_wait(&ntd->mr_recvs, NNTI_RECV_QUEUE, timeout, &wait_status);
if (wr_count > 0) {
DROP_CM_LOCK(svc, trans->cm);
err = NNTI_waitany(wr_list, wr_count, timeout, &which, &wait_status);
ACQUIRE_CM_LOCK(svc, trans->cm);
} else {
svc->cond_wait_CM_lock((trans->cm),&(ntd->cond), __FILE__, __LINE__);
/* nothing to do but go back and wait for other stuff */
continue;
}
if (ntd->shutdown_listen_thread) {
DROP_CM_LOCK(svc, trans->cm);
return 0;
}
if ((err == NNTI_ETIMEDOUT) || (err==NNTI_EAGAIN)) {
//ntd->svc->trace_out(trans->cm, "NNTI_wait() on receiving result %d timed out...", err);