END_TEST START_TEST (set_opt_invalid_option) { uint32_t initFlags = 0; uint32_t capabilities = 0; uint32_t status = 0; cci_device_t const ** const devices = NULL; /* available device structure */ cci_endpoint_t* endpointP = NULL; /* pointer to endpoint structure */ int32_t deviceFlags = 0; /* not yet implemented */ cci_os_handle_t fd; /* endpoint handle */ cci_opt_handle_t *optHandle; int32_t* option; /* Initialize cci library */ status = cci_init(CCI_ABI_VERSION, initFlags, &capabilities); fail_unless(status == CCI_SUCCESS, "cci_init() failed with status %s", cci_strerror(NULL, status)); /* get a list of the available devices */ status = cci_get_devices((cci_device_t * const **) &devices); fail_unless(status == CCI_SUCCESS, "cci_get_devices() failed with status %s", cci_strerror(NULL, status)); /* create an endpoint. For now, simply use the first device - remember that fd can be used in select() */ status = cci_create_endpoint((cci_device_t*) *devices, deviceFlags, &endpointP, &fd); fail_unless(status == CCI_SUCCESS, "cci_create_endpoint() failed with status %s", cci_strerror(NULL, status)); /* get an endpoint option */ optHandle = endpointP; status = cci_set_opt(optHandle, 100, (void*) &option, sizeof(option)); fail_unless(status == CCI_EINVAL, "cci_set_opt failed with status %s\n", cci_strerror(NULL, status)); }
END_TEST START_TEST (create_endpoint_loop) { uint32_t initFlags = 0; uint32_t capabilities = 0; uint32_t status = 0; cci_device_t const ** const devices = NULL; /* available device structure */ cci_device_t const ** d = NULL; cci_endpoint_t* endpointP = NULL; /* pointer to endpoint structure */ int32_t deviceFlags = 0; /* not yet implemented */ cci_os_handle_t fd; /* endpoint handle */ uint32_t i; /* Initialize cci library */ status = cci_init(CCI_ABI_VERSION, initFlags, &capabilities); fail_unless(status == CCI_SUCCESS, "cci_init() failed with status %s", cci_strerror(NULL, status)); /* get a list of the available devices */ status = cci_get_devices((cci_device_t * const **) &devices); fail_unless(status == CCI_SUCCESS, "cci_get_devices() failed with status %s", cci_strerror(NULL, status)); for(i=0; i<10; ++i) { for(d = devices; *d != NULL; ++d) { status = cci_create_endpoint((cci_device_t*) *d, deviceFlags, &endpointP, &fd); fail_unless(status == CCI_SUCCESS, "cci_create_endpoint() failed with status %s", cci_strerror(NULL, status)); status = cci_destroy_endpoint(endpointP); fail_unless(status == CCI_SUCCESS, "cci_destroy_endpoint() failed with status %s", cci_strerror(NULL, status)); } } }
END_TEST /* * Try endpoint = NULL (should fail with EINVAL) */ START_TEST (create_endpoint_null_endpoint) { uint32_t initFlags = 1; uint32_t capabilities = 0; uint32_t status = 0; cci_device_t const ** const devices = NULL; /* available device structure */ cci_endpoint_t* endpointP = NULL; /* pointer to endpoint structure */ int32_t deviceFlags = 0; /* not yet implemented (MBZ) */ cci_os_handle_t fd; /* endpoint handle */ /* Initialize cci library */ status = cci_init(CCI_ABI_VERSION, initFlags, &capabilities); fail_unless(status == CCI_SUCCESS, "cci_init() failed with status %s", cci_strerror(NULL, status)); /* get a list of the available devices */ status = cci_get_devices((cci_device_t * const **) &devices); fail_unless(status == CCI_SUCCESS, "cci_get_devices() failed with status %s", cci_strerror(NULL, status)); /* attempt to create an endpoint with flags != 0 - should get EINVAL */ status = cci_create_endpoint((cci_device_t*) *devices, deviceFlags, (cci_endpoint_t**) endpointP, &fd); fail_unless(status == CCI_EINVAL, "cci_create_endpoint() with endpoint = NULL received status %s", cci_strerror(NULL, status)); }
END_TEST START_TEST (get_devices_null) { uint32_t initFlags = 0; uint32_t capabilities = 0; uint32_t status = 0; cci_device_t const ** const devices = NULL; /* available device structure */ /* Initialize cci library */ status = cci_init(CCI_ABI_VERSION, initFlags, &capabilities); fail_unless(status == CCI_SUCCESS, "cci_init() failed with status %s", cci_strerror(NULL, status)); /* get a list of the available devices */ status = cci_get_devices((cci_device_t * const **) devices); fail_if(status == CCI_SUCCESS, "cci_get_devices() failed with status %s", cci_strerror(NULL, status)); }
static void check_return(cci_endpoint_t *endpoint, char *func, int ret) { if (ret) { fprintf(stderr, "%s() returned %s\n", func, cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } return; }
END_TEST START_TEST (create_endpoint_all_devices) { uint32_t initFlags = 0; uint32_t capabilities = 0; uint32_t status = 0; cci_device_t const ** const devices = NULL; /* available device structure */ cci_device_t const ** d = NULL; cci_endpoint_t* endpointP = NULL; /* pointer to endpoint structure */ int32_t deviceFlags = 0; /* not yet implemented */ cci_os_handle_t fd; /* endpoint handle */ char* ip; char* equalP; char** keyval; char* colonP; uint32_t i; /* Initialize cci library */ status = cci_init(CCI_ABI_VERSION, initFlags, &capabilities); fail_unless(status == CCI_SUCCESS, "cci_init() failed with status %s", cci_strerror(NULL, status)); /* get a list of the available devices */ status = cci_get_devices((cci_device_t * const **) &devices); fail_unless(status == CCI_SUCCESS, "cci_get_devices() failed with status %s", cci_strerror(NULL, status)); for(d = devices, i=0; *d != NULL ; ++d, ++i) { /* create an endpoint. */ /* should have new endpointP and fd for each one, but we won't for this test */ status = cci_create_endpoint((cci_device_t*) *d, deviceFlags, &endpointP, &fd); fail_unless(status == CCI_SUCCESS, "cci_create_endpoint() failed with status %s", cci_strerror(NULL, status)); for(keyval = (char**) (*d)->conf_argv; *keyval != NULL; keyval++) { if(strstr(*keyval, "ip") != NULL) { equalP = strchr(*keyval, '='); ip = strchr(endpointP->name, '/'); ip += 2; colonP = strchr(ip, ':'); *colonP = '\0'; equalP++; fail_unless(strcmp(ip, equalP) == 0, "endpoint Name != device IP (%s) != (%s)\n", ip, equalP); } } } }
END_TEST /* * devices = NULL, check to see if device0 was chosen */ START_TEST (create_endpoint_null_device) { uint32_t initFlags = 0; uint32_t capabilities = 0; uint32_t status = 0; cci_device_t const ** const devices = NULL; /* available device structure */ cci_device_t const ** d = NULL; cci_endpoint_t* endpointP = NULL; /* pointer to endpoint structure */ int32_t deviceFlags = 0; /* not yet implemented */ cci_os_handle_t fd; /* endpoint handle */ char* ip; char* equalP; char** keyval; char* colonP; /* Initialize cci library */ status = cci_init(CCI_ABI_VERSION, initFlags, &capabilities); fail_unless(status == CCI_SUCCESS, "cci_init() failed with status %s", cci_strerror(NULL, status)); /* create an endpoint. Note that we haven't called get_devices() */ status = cci_create_endpoint((cci_device_t*) devices, deviceFlags, &endpointP, &fd); fail_unless(status == CCI_SUCCESS, "cci_create_endpoint() failed with status %s", cci_strerror(NULL, status)); /* get a list of the available devices */ status = cci_get_devices((cci_device_t * const **) &devices); fail_unless(status == CCI_SUCCESS, "cci_get_devices() failed with status %s", cci_strerror(NULL, status)); d = devices; for(keyval = (char**) (*d)->conf_argv; *keyval != NULL; keyval++) { if(strstr(*keyval, "ip") != NULL) { equalP = strchr(*keyval, '='); ip = strchr(endpointP->name, '/'); ip += 2; colonP = strchr(ip, ':'); *colonP = '\0'; equalP++; fail_unless(strcmp(ip, equalP) == 0, "endpoint Name != device IP (%s) != (%s)\n", ip, equalP); } } }
int main(int argc, char *argv[]) { int rc; /* This is supposed to fail because we will pass value instead of address. */ if (CCI_SUCCESS != (rc = cci_init(CCI_ABI_VERSION, 0, 0))) { fprintf(stderr, "Got error back from cci:init: %s\n", cci_strerror(NULL, rc)); return -1; } return 0; }
static inline void poll_ctl_events (void) { cci_event_t *event; int ret; ret = cci_get_event (ctl_ep, &event); if (ret == CCI_SUCCESS) { switch (event->type) { case CCI_EVENT_CONNECT_REQUEST: ctl_opts = *((options_t *) event->request.data_ptr); ret = cci_accept (event, NULL); check_return (ctl_ep, "cci_accept", ret, 1); break; case CCI_EVENT_ACCEPT: ctl_conn = event->accept.connection; break; case CCI_EVENT_RECV: if (is_server && strncmp (event->recv.ptr, "start", 5) == 0) { printf ("Connection accepted, creating endpoint for testing...\n"); ret = cci_create_endpoint(NULL, 0, &endpoint, NULL); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(endpoint, CCI_OPT_ENDPT_URI, &test_uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_send (ctl_conn, test_uri, strlen (test_uri), (void*)0xdeadfeeb, ctl_opts.flags); check_return(ctl_ep, "cci_send", ret, 0); printf("Opened %s\n", test_uri); } if (is_server && strncmp (event->recv.ptr, "bye", 3) == 0) { done = 1; } if (!is_server) { test_uri = strdup (event->recv.ptr); printf ("Opening a connection to %s\n", test_uri); ret = cci_create_endpoint(NULL, 0, &endpoint, NULL); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(endpoint, CCI_OPT_ENDPT_URI, &uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_connect (endpoint, test_uri, &opts, sizeof (opts), attr, NULL, 0, NULL); check_return (endpoint, "cci_connect", ret, 1); } break; case CCI_EVENT_CONNECT: if (!is_server) { ctl_conn = event->connect.connection; printf ("Control connection established, simulating a conn_reject now...\n"); /* Send the start message */ ret = cci_send (ctl_conn, "start", 5, (void*)0xdeadbeed, ctl_opts.flags); check_return(ctl_ep, "cci_send", ret, 0); } break; case CCI_EVENT_SEND: break; default: fprintf (stderr, "Ignoring event type %d\n", event->type); } cci_return_event (event); } }
int main(int argc, char *argv[]) { int done = 0, ret, i = 0, c; uint32_t caps = 0; char *server_uri = NULL; cci_os_handle_t *fd = NULL; cci_endpoint_t *endpoint = NULL; cci_connection_t *connection = NULL; uint32_t timeout = 30 * 1000000; while ((c = getopt(argc, argv, "h:c:b")) != -1) { switch (c) { case 'h': server_uri = strdup(optarg); break; case 'c': if (strncasecmp ("ru", optarg, 2) == 0) attr = CCI_CONN_ATTR_RU; else if (strncasecmp ("ro", optarg, 2) == 0) attr = CCI_CONN_ATTR_RO; else if (strncasecmp ("uu", optarg, 2) == 0) attr = CCI_CONN_ATTR_UU; break; case 'b': flags |= CCI_FLAG_BLOCKING; break; default: fprintf(stderr, "usage: %s -h <server_uri> [-c <type>]\n", argv[0]); fprintf(stderr, "\t-c\tConnection type (UU, RU, or RO) " "set by client; RO by default\n"); exit(EXIT_FAILURE); } } if (!server_uri) { fprintf(stderr, "usage: %s -h <server_uri> [-c <type>]\n", argv[0]); fprintf(stderr, "\t-c\tConnection type (UU, RU, or RO) " "set by client; RO by default\n"); exit(EXIT_FAILURE); } ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* create an endpoint */ ret = cci_create_endpoint(NULL, 0, &endpoint, fd); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* set conn tx timeout */ cci_set_opt(endpoint, CCI_OPT_ENDPT_SEND_TIMEOUT, &timeout); if (ret) { fprintf(stderr, "cci_set_opt() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } /* initiate connect */ ret = cci_connect(endpoint, server_uri, "Hello World!", 12, attr, CONNECT_CONTEXT, 0, NULL); if (ret) { fprintf(stderr, "cci_connect() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } /* poll for connect completion */ while (!done) poll_events(endpoint, &connection, &done); if (!connection) exit(0); done = 0; /* begin communication with server */ for (i = 0; i < iters; i++) { char data[128]; memset(data, 0, sizeof(data)); sprintf(data, "%4d", i); sprintf(data + 4, "Hello World!"); ret = cci_send(connection, data, (uint32_t) strlen(data) + 4, (void *)(uintptr_t) i, flags); if (ret) fprintf(stderr, "send %d failed with %s\n", i, cci_strerror(endpoint, ret)); if (flags & CCI_FLAG_BLOCKING) fprintf(stderr, "send %d completed with %d\n", i, ret); } if (flags == CCI_FLAG_BLOCKING) send_done = iters; while (!done) poll_events(endpoint, &connection, &done); ret = cci_send(connection, "bye", 3, (void *)(uintptr_t) iters, flags); if (ret) fprintf(stderr, "sending \"bye\" failed with %s\n", cci_strerror(endpoint, ret)); if (flags & CCI_FLAG_BLOCKING) done = 2; while (done != 2) poll_events(endpoint, &connection, &done); /* clean up */ ret = cci_destroy_endpoint(endpoint); if (ret) { fprintf(stderr, "cci_destroy_endpoint() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } ret = cci_finalize(); if (ret) { fprintf(stderr, "cci_finalize() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } return 0; }
int main(int argc, char *argv[]) { int ret, c; uint32_t caps = 0; cci_os_handle_t *os_handle = NULL; char *uri = NULL; name = argv[0]; while ((c = getopt(argc, argv, "h:sRc:nwrm:Ci:W:bo")) != -1) { switch (c) { case 'h': server_uri = strdup(optarg); break; case 's': is_server = 1; break; case 'R': accept = 0; break; case 'i': iters = strtoul(optarg, NULL, 0); break; case 'W': warmup = strtoul(optarg, NULL, 0); break; case 'c': if (strncasecmp("ru", optarg, 2) == 0) attr = CCI_CONN_ATTR_RU; else if (strncasecmp("ro", optarg, 2) == 0) attr = CCI_CONN_ATTR_RO; else if (strncasecmp("uu", optarg, 2) == 0) attr = CCI_CONN_ATTR_UU; else print_usage(); printf("Using %s connection\n", attr == CCI_CONN_ATTR_UU ? "UU" : attr == CCI_CONN_ATTR_RU ? "RU" : "RO"); break; case 'n': opts.flags = CCI_FLAG_NO_COPY; break; case 'w': opts.method = RMA_WRITE; break; case 'r': opts.method = RMA_READ; break; case 'm': opts.max_rma_size = strtoul(optarg, NULL, 0); break; case 'C': remote_completion = 1; break; case 'b': blocking = 1; os_handle = &fd; break; case 'o': ignore_os_handle = 1; os_handle = &fd; break; default: print_usage(); } } if (!is_server && !server_uri) { fprintf(stderr, "Must select -h or -s\n"); print_usage(); } if (blocking && ignore_os_handle) { fprintf(stderr, "-b and -o are not compatible.\n"); fprintf(stderr, "-b will block using select() using the OS handle.\n"); fprintf(stderr, "-o will obtain the OS handle, but not use it to wait.\n"); print_usage(); } if (attr == CCI_CONN_ATTR_UU) { if (opts.method != MSGS) { fprintf(stderr, "RMA %s not allowed with UU connections\n", opts.method == RMA_WRITE ? "WRITE" : "READ"); print_usage(); } if (opts.max_rma_size) { printf("ignoring max_rma_size (-m) with MSGs\n"); opts.max_rma_size = 0; } } else { /* RO or RU */ if (opts.flags == CCI_FLAG_NO_COPY) { printf("Ignoring CCI_FLAG_NO_COPY (-n) with RMA %s\n", opts.method == RMA_WRITE ? "WRITE" : "READ"); opts.flags &= ~(CCI_FLAG_NO_COPY); } if (!opts.max_rma_size) opts.max_rma_size = MAX_RMA_SIZE; } ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* create an endpoint */ ret = cci_create_endpoint(NULL, 0, &endpoint, os_handle); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(endpoint, CCI_OPT_ENDPT_URI, &uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } printf("Opened %s\n", uri); if (blocking) { nfds = fd + 1; FD_ZERO(&rfds); FD_SET(fd, &rfds); } if (is_server) do_server(); else do_client(); /* clean up */ ret = cci_destroy_endpoint(endpoint); if (ret) { fprintf(stderr, "cci_destroy_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } if (buffer) free(buffer); free(uri); free(server_uri); ret = cci_finalize(); if (ret) { fprintf(stderr, "cci_finalize() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } return 0; }
static void do_server(void) { int ret; while (!ready) { cci_event_t *event; if (blocking) { FD_ZERO(&rfds); FD_SET(fd, &rfds); ret = select(nfds, &rfds, NULL, NULL, NULL); if (!ret) return; } ret = cci_get_event(endpoint, &event); if (ret == CCI_SUCCESS) { switch (event->type) { case CCI_EVENT_CONNECT_REQUEST: if (accept) { opts = *((options_t *) event->request. data_ptr); ret = cci_accept(event, NULL); check_return(endpoint, "cci_accept", ret, 1); } else { ret = cci_reject(event); check_return(endpoint, "cci_reject", ret, 1); } break; case CCI_EVENT_ACCEPT: { int len; ready = 1; connection = event->accept.connection; if (opts.method == MSGS) len = connection->max_send_size; else len = opts.max_rma_size; ret = posix_memalign((void **)&buffer, 4096, len); check_return(endpoint, "memalign buffer", ret, 1); memset(buffer, 'a', len); if (opts.method != MSGS) { ret = cci_rma_register(endpoint, buffer, opts. max_rma_size, opts.method == RMA_WRITE ? CCI_FLAG_WRITE : CCI_FLAG_READ, &server_rma_handle); check_return(endpoint, "cci_rma_register", ret, 1); memcpy(&opts.rma_handle, server_rma_handle, sizeof(*server_rma_handle)); } ret = cci_send(connection, &opts, sizeof(opts), NULL, 0); check_return(endpoint, "cci_send", ret, 1); break; } default: fprintf(stderr, "%s: ignoring unexpected event %d\n", __func__, event->type); break; } ret = cci_return_event(event); if (ret) fprintf(stderr, "cci_return_event() failed with %s\n", cci_strerror(endpoint, ret)); } } while (!done) poll_events(); if (opts.method != MSGS) { ret = cci_rma_deregister(endpoint, server_rma_handle); check_return(endpoint, "cci_rma_deregister", ret, 1); } printf("server done\n"); sleep(1); return; }
static void poll_events(void) { int ret; cci_event_t *event; if (blocking) { FD_ZERO(&rfds); FD_SET(fd, &rfds); ret = select(nfds, &rfds, NULL, NULL, NULL); if (!ret) return; } ret = cci_get_event(endpoint, &event); if (ret == CCI_SUCCESS) { assert(event); switch (event->type) { case CCI_EVENT_SEND: assert(event->send.status == CCI_SUCCESS); if (opts.method != MSGS) { if (!is_server && event->send.context == (void *)1) { count++; if (count < warmup + iters) { ret = cci_rma(connection, rmt_comp_msg, rmt_comp_len, local_rma_handle, 0, &opts.rma_handle, 0, current_size, (void *)1, opts.flags); check_return(endpoint, "cci_rma", ret, 1); } } } if (!is_server && event->send.context == (void *)0xdeadbeef) done = 1; break; case CCI_EVENT_RECV: { if (!is_server && opts.method != MSGS && event->recv.ptr == (void *)1) { count++; if (count < warmup + iters) { ret = cci_rma(connection, rmt_comp_msg, rmt_comp_len, local_rma_handle, 0, &opts.rma_handle, 0, current_size, (void *)1, opts.flags); check_return(endpoint, "cci_rma", ret, 1); } } if (!ready) { ready = 1; if (opts.method != MSGS && !is_server) { /* get server_rma_handle */ opts = *((options_t *) event->recv. ptr); } } else if (is_server && event->recv.len == 3) { done = 1; break; } else if (opts.method == MSGS) { if (is_server) { count++; if (event->recv.len > current_size) { current_size = event->recv.len; count = 1; } } else { if (event->recv.len == current_size) count++; } if (is_server || count < warmup + iters) { ret = cci_send(connection, buffer, current_size, NULL, opts.flags); if (ret) fprintf(stderr, "%s: %s: send returned %s\n", __func__, is_server ? "server" : "client", cci_strerror (endpoint, ret)); check_return(endpoint, "cci_send", ret, 1); } } break; } case CCI_EVENT_CONNECT: if (!is_server) { connect_done = 1; connection = event->connect.connection; } break; default: fprintf(stderr, "ignoring event type %d\n", event->type); } cci_return_event(event); } return; }
int main(int argc, char *argv[]) { int ret; uint32_t caps = 0; char *uri = NULL; cci_endpoint_t *endpoint = NULL; cci_os_handle_t ep_fd; cci_connection_t *connection = NULL; /* init */ ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* create an endpoint */ ret = cci_create_endpoint(NULL, 0, &endpoint, &ep_fd); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(endpoint, CCI_OPT_ENDPT_URI, &uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } printf("Opened %s\n", uri); while (1) { cci_event_t *event; ret = cci_get_event(endpoint, &event); if (ret != CCI_SUCCESS) { if (ret != CCI_EAGAIN) { fprintf(stderr, "cci_get_event() returned %s", cci_strerror(endpoint, ret)); } continue; } /* fprintf(stderr, "ret: %d, event: %d (RECV:%d, SEND:%d, CONNECT_REQ:%d, ACCEPT:%d)\n", */ /* ret, event->type, CCI_EVENT_RECV, CCI_EVENT_SEND, CCI_EVENT_CONNECT_REQUEST, */ /* CCI_EVENT_ACCEPT); */ fprintf(stderr, "Event: %s, ret:%d len: %lu\n", cci_event_type_str(event->type), ret, event->recv.len); switch (event->type) { case CCI_EVENT_RECV: { // fprintf(stderr, "=====: %p %p %d\n", buffer, event->recv.ptr, event->recv.len); // memcpy(buffer, // event->recv.ptr, event->recv.len); // fprintf(stderr, "=====\n"); // buffer[event->recv.len] = 0; // printf("recv'd \n"); /* echo the message to the client */ ret = cci_send(connection, event->recv.ptr, event->recv.len, NULL, 0); if (ret != CCI_SUCCESS) fprintf(stderr, "send returned %s\n", cci_strerror(endpoint, ret)); break; } case CCI_EVENT_SEND: // printf("completed send\n"); break; case CCI_EVENT_CONNECT_REQUEST: { int accept = 1; if (accept) { ret = cci_accept(event, NULL); if (ret != CCI_SUCCESS) { fprintf(stderr, "cci_accept() returned %s", cci_strerror(endpoint, ret)); } } else { cci_reject(event); } } break; case CCI_EVENT_ACCEPT: connection = event->accept.connection; if (!buffer) { buffer = calloc(1, connection->max_send_size + 1); fprintf(stderr, "allocated buffer: %p , size %d", buffer, connection->max_send_size + 1); /* check for buffer ... */ } else { fprintf(stderr, "Not allocated\n"); } break; default: fprintf(stderr, "unexpected event %d", event->type); break; } cci_return_event(event); } /* clean up */ cci_destroy_endpoint(endpoint); /* add cci_finalize() here */ return 0; }
int main(int argc, char *argv[]) { int ret, c; uint32_t caps = 0; cci_os_handle_t *os_handle = NULL; char *uri = NULL; pid_t pid = 0; pid = getpid(); srandom(pid); memset(&msg, 0, sizeof(msg)); name = argv[0]; while ((c = getopt(argc, argv, "h:si:c:wrl:o:O:R:BI")) != -1) { switch (c) { case 'h': server_uri = strdup(optarg); is_client = 1; break; case 's': is_server = 1; break; case 'i': iters = strtoul(optarg, NULL, 0); break; case 'c': if (strncasecmp("ru", optarg, 2) == 0) attr = CCI_CONN_ATTR_RU; else if (strncasecmp("ro", optarg, 2) == 0) attr = CCI_CONN_ATTR_RO; else print_usage(); printf("Using %s connection\n", attr == CCI_CONN_ATTR_RU ? "RU" : "RO"); break; case 'w': opts.method = RMA_WRITE; break; case 'r': opts.method = RMA_READ; break; case 'l': length = strtoul(optarg, NULL, 0); break; case 'R': opts.reg_len = strtoul(optarg, NULL, 0); break; case 'o': local_offset = strtoul(optarg, NULL, 0); break; case 'O': remote_offset = strtoul(optarg, NULL, 0); break; case 'B': blocking = 1; os_handle = &fd; break; case 'I': ignore_os_handle = 1; os_handle = &fd; break; default: print_usage(); } } if (!is_server && !server_uri) { fprintf(stderr, "Must select -h or -s\n"); print_usage(); } if (is_server && is_client) { fprintf(stderr, "Must select -h or -s, not both\n"); print_usage(); } if (blocking && ignore_os_handle) { fprintf(stderr, "-B and -I are not compatible.\n"); fprintf(stderr, "-B will block using select() using the OS handle.\n"); fprintf(stderr, "-I will obtain the OS handle, but not use it to wait.\n"); print_usage(); } if (!opts.method) opts.method = RMA_WRITE; if (!opts.reg_len) { if (!length) { opts.reg_len = RMA_REG_LEN; } else { opts.reg_len = length; } } if (!length) { if (!opts.reg_len) length = RMA_REG_LEN; else length = opts.reg_len; } if (opts.reg_len == length) { if (local_offset || remote_offset) { fprintf(stderr, "*** RMA registration length == RMA length " "and an offset was requested. ***\n" "*** This should cause an error. ***\n"); } } if (is_client) fprintf(stderr, "Testing with local_offset %"PRIu64" " "remote_offset %"PRIu64" " "reg_len %"PRIu64" length %"PRIu64"\n", local_offset, remote_offset, opts.reg_len, length); ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* create an endpoint */ ret = cci_create_endpoint(NULL, 0, &endpoint, os_handle); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(endpoint, CCI_OPT_ENDPT_URI, &uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } printf("Opened %s\n", uri); if (blocking) { nfds = fd + 1; FD_ZERO(&rfds); FD_SET(fd, &rfds); } if (is_server) do_server(); else do_client(); /* clean up */ ret = cci_destroy_endpoint(endpoint); if (ret) { fprintf(stderr, "cci_destroy_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } free(buffer); free(uri); free(server_uri); ret = cci_finalize(); if (ret) { fprintf(stderr, "cci_finalize() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } return 0; }
static void do_server(void) { int ret = 0; hdr_t *h = NULL; while (!ready) { cci_event_t *event; if (blocking) { FD_ZERO(&rfds); FD_SET(fd, &rfds); ret = select(nfds, &rfds, NULL, NULL, NULL); if (!ret) return; } ret = cci_get_event(endpoint, &event); if (ret == CCI_SUCCESS) { switch (event->type) { case CCI_EVENT_CONNECT_REQUEST: { int which = 0; h = (void*)event->request.data_ptr; if (h->generic.type == MSG_CONN_REQ) { opts = h->request.opts; which = TEST; } else { which = CONTROL; } ret = cci_accept(event, (void*)((uintptr_t)which)); check_return(endpoint, "cci_accept", ret, 1); break; } case CCI_EVENT_ACCEPT: { if ((uintptr_t)event->accept.context == (uintptr_t)CONTROL) { control = event->accept.connection; } else { int len; test = event->accept.connection; len = opts.reg_len; ret = posix_memalign((void **)&buffer, 4096, len); check_return(endpoint, "memalign buffer", ret, 1); init_buffer(0); print_buffer(buffer, opts.reg_len); ret = cci_rma_register(endpoint, buffer, opts.reg_len, opts.method == RMA_WRITE ? CCI_FLAG_WRITE : CCI_FLAG_READ, &local_rma_handle); check_return(endpoint, "cci_rma_register", ret, 1); } if (test && control) { hdr_t msg; ready = 1; msg.reply.type = MSG_CONN_REPLY; msg.reply.handle = *local_rma_handle; ret = cci_send(test, &msg, sizeof(msg.reply), NULL, 0); check_return(endpoint, "cci_send", ret, 1); } break; } default: fprintf(stderr, "%s: ignoring unexpected event %s\n", __func__, cci_event_type_str(event->type)); break; } ret = cci_return_event(event); if (ret) fprintf(stderr, "cci_return_event() failed with %s\n", cci_strerror(endpoint, ret)); } } while (!done) poll_events(); ret = cci_rma_deregister(endpoint, local_rma_handle); check_return(endpoint, "cci_rma_deregister", ret, 1); printf("server done\n"); sleep(1); return; }
void server() { int ret, context, num_request = 0, num_accept = 0; uint32_t caps = 0; char *uri = NULL; cci_endpoint_t *endpoint = NULL; cci_os_handle_t *ep_fd = NULL; cci_connection_t *connection = NULL; ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* create an endpoint */ ret = cci_create_endpoint(NULL, 0, &endpoint, ep_fd); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(endpoint, CCI_OPT_ENDPT_URI, &uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } printf("Opened %s\n", uri); fflush(stdout); while (num_request < 3 || num_accept < 1) { cci_event_t *event; ret = cci_get_event(endpoint, &event); if (ret != 0) { if (ret != CCI_EAGAIN) fprintf(stderr, "cci_get_event() returned %s\n", cci_strerror(endpoint, ret)); continue; } switch (event->type) { case CCI_EVENT_CONNECT_REQUEST: fprintf(stderr, "connect request\n"); num_request++; /* accept the first, reject the second, let the third timeout */ if (num_request == 1) { context = 123; cci_accept(event, (int *) &context); } else if (num_request == 2 && 0) { /* XXX: cci_reject segfaults */ cci_reject(event); } break; case CCI_EVENT_ACCEPT: fprintf(stderr, "completed accept\n"); connection = event->accept.connection; context = *(int *) event->accept.context; printf("CCI_EVENT_ACCEPT: %d\n", context); num_accept++; break; default: fprintf(stderr, "ignoring event type %d\n", event->type); } cci_return_event(event); } printf("Pausing while client times out...\n"); sleep(5); printf("test passed\n"); fflush(stdout); server_cleanup: ret = cci_destroy_endpoint(endpoint); if (ret) { fprintf(stderr, "cci_destroy_endpoint() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } ret = cci_finalize(); if (ret) { fprintf(stderr, "cci_finalize() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } free(uri); }
int main(int argc, char *argv[]) { int done = 0, ret, i = 0, c; uint32_t caps = 0; char *server_uri = NULL; /* ip://1.2.3.4 */ char *uri = NULL; cci_os_handle_t fd; cci_endpoint_t *endpoint = NULL; cci_connection_t *connection = NULL; uint32_t timeout_us = 30 * 1000000; /* microseconds */ proc_name = argv[0]; while ((c = getopt(argc, argv, "h:")) != -1) { switch (c) { case 'h': server_uri = strdup(optarg); break; default: usage(); } } /* init */ ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() returned %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* create an endpoint */ ret = cci_create_endpoint(NULL, 0, &endpoint, &fd); if (ret) { fprintf(stderr, "cci_create_endpoint() returned %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(endpoint, CCI_OPT_ENDPT_URI, &uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } printf("Opened %s\n", uri); /* set endpoint tx timeout */ cci_set_opt(endpoint, CCI_OPT_ENDPT_SEND_TIMEOUT, &timeout_us); if (ret) { fprintf(stderr, "cci_set_opt() returned %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } /* initiate connect */ ret = cci_connect(endpoint, server_uri, "Hello World!", 12, CCI_CONN_ATTR_UU, NULL, 0, NULL); if (ret) { fprintf(stderr, "cci_connect() returned %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } /* poll for connect completion */ while (!done) poll_events(endpoint, &connection, &done); if (!connection) { fprintf(stderr, "no connection\n"); exit(EXIT_FAILURE); } /* begin communication with server */ char *data; int max = connection->max_send_size; data = (char*)malloc(max); memset(data, 1, sizeof(data)); int len = 128; int repeat = 1000; while (len <= max) { s = cci_get_time(); for (i = 0; i < repeat; i++) { /* ret = cci_send(connection, data, (uint32_t) strlen(data), */ /* (void *)(uintptr_t) i, 0); */ ret = cci_send(connection, data, len, (void *)(uintptr_t) i, 0); if (ret > 0) { fprintf(stderr, "send %d returned %s\n", i, cci_strerror(endpoint, ret)); exit(1); } done = 0; while (!done) poll_events(endpoint, &connection, &done); } e = cci_get_time(); fprintf(stderr, "%d %f %f\n", len, (e - s) / repeat, (len / (e - s)) * repeat); len = len << 1; sleep(1); } /* clean up */ ret = cci_disconnect(connection); if (ret) { fprintf(stderr, "cci_disconnect() returned %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } ret = cci_destroy_endpoint(endpoint); if (ret) { fprintf(stderr, "cci_destroy_endpoint() returned %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* add cci_finalize() here */ return 0; }
int main (int argc, char *argv[]) { int ret, c; uint32_t caps = 0; char *ctl_uri = NULL; name = argv[0]; while ((c = getopt (argc, argv, "h:s")) != -1) { switch (c) { case 'h': server_uri = strdup (optarg); break; case 's': is_server = 1; break; default: print_usage (); } } ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* Create the endpoint for the control connection */ ret = cci_create_endpoint(NULL, 0, &ctl_ep, NULL); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(ctl_ep, CCI_OPT_ENDPT_URI, &ctl_uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } printf("Opened control connection %s\n", ctl_uri); if (is_server) do_server (); else do_client (); ret = cci_destroy_endpoint (ctl_ep); if (ret) { fprintf (stderr, "cci_destroy_endpoint() failed with %s\n", cci_strerror (NULL, ret)); exit (EXIT_FAILURE); } free (server_uri); ret = cci_finalize (); if (ret) { fprintf (stderr, "cci_finalize() failed with %s\n", cci_strerror (NULL, ret)); exit (EXIT_FAILURE); } return 0; }
int main(int argc, char *argv[]) { cci_os_handle_t *fd = NULL; //endpoint 생성시, process를 block하는데 사용 cci_endpoint_t *endpoint = NULL; cci_connection_t *connection = NULL; int ret, c, ft_start = 0 , done = 0, i = 0, connect = 0; pthread_t send; uint32_t caps = 0;//?? char *server_uri = NULL; char id[16]=""; t_data thread_data; while ((c = getopt(argc, argv, "h:c:b")) != -1) { //client 실행시 option check switch (c) { case 'h': server_uri = strdup(optarg);//-h에 대한 인자를 가리키는 optarg 포인터가 생김 http://weed2758.tistory.com/entry/Linux-C-getopt-%ED%95%A8%EC%88%98 break; case 'c': if (strncasecmp ("ru", optarg, 2) == 0) attr = CCI_CONN_ATTR_RU; else if (strncasecmp ("ro", optarg, 2) == 0) attr = CCI_CONN_ATTR_RO; else if (strncasecmp ("uu", optarg, 2) == 0) attr = CCI_CONN_ATTR_UU; break; case 'b': flags |= CCI_FLAG_BLOCKING; break; default: fprintf(stderr, "usage: %s -h <server_uri> [-c <type>]\n", argv[0]); fprintf(stderr, "\t-c\tConnection type (UU, RU, or RO) " "set by client; RO by default\n"); exit(EXIT_FAILURE); } } if (!server_uri) { fprintf(stderr, "usage: %s -h <server_uri> [-c <type>]\n", argv[0]); fprintf(stderr, "\t-c\tConnection type (UU, RU, or RO) " "set by client; RO by default\n"); exit(EXIT_FAILURE); } ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* create an endpoint */ ret = cci_create_endpoint(NULL, 0, &endpoint, fd); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } if (ret) { fprintf(stderr, "cci_set_opt() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } /* initiate connect */ ret = cci_connect(endpoint, server_uri, "Connect request", 15, attr, CONNECT_CONTEXT, 0, NULL); if (ret) { fprintf(stderr, "cci_connect() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } while (!done) poll_events(endpoint, &connection, &done, id); if (!connection) exit(0); done = 0; thread_data.connection=connection; thread_data.flag=flags; thread_data.id=id; pthread_create(&send,NULL,send_msg,&thread_data); while(!done) poll_events(endpoint, &connection, &done, id); pthread_join(send,NULL); /* clean up */ ret = cci_destroy_endpoint(endpoint); if (ret) { fprintf(stderr, "cci_destroy_endpoint() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } ret = cci_finalize(); if (ret) { fprintf(stderr, "cci_finalize() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } return 0; }
int main(int argc, char *argv[]) { int ret, number_of_connections=0; int client_end[MAX_CONNECTION_SIZE]={0,}; uint32_t caps = 0; char *uri = NULL; long file_size[MAX_CONNECTION_SIZE]={0,}; cci_endpoint_t *endpoint = NULL; cci_os_handle_t *ep_fd = NULL; cci_connection_t *connection[MAX_CONNECTION_SIZE] = {NULL,}; ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* create an endpoint */ ret = cci_create_endpoint(NULL, 0, &endpoint, ep_fd); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(endpoint, CCI_OPT_ENDPT_URI, &uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } printf("Opened %s\n", uri); while (1) { int accept = 1; cci_event_t *event; ret = cci_get_event(endpoint, &event); if (ret != 0) { if (ret != CCI_EAGAIN) fprintf(stderr, "cci_get_event() returned %s\n", cci_strerror(endpoint, ret)); continue; } switch (event->type) { case CCI_EVENT_RECV:{ char buf[MAX_BUFFER_SIZE]; char *number, *data; char exit_msg[32]; long read_size=0; int len = event->recv.len; int i=0, j=0; int id; memset(buf, 0, MAX_BUFFER_SIZE); memcpy(buf, event->recv.ptr, len); if(strncasecmp((char*)event->recv.ptr,"client", 6)==0){ /* 문자인 경우 */ fprintf(stderr, "%s", buf); strtok(buf," :"); /* 필요한 정보 파싱 (누구한테, 첫번째 문자)*/ number=strtok(NULL," :"); data=strtok(NULL," :"); id=atoi(number); //누구한테서 왔는지 memcpy(buf, event->recv.ptr, len); if(event->recv.connection==connection[id-1]){ if(strncasecmp(data,"bye\n", 4)==0){ //종료를 원할 경우 fprintf(stderr, "Client%d want to termainate this program.\n", id); client_end[id-1]=1; }else if(strncasecmp(data,"file\n", 5)==0) //file 전송을 원할 경우 fprintf(stderr, "Client %d send a file...\n", id); for(j=0; j<number_of_connections; j++){ if(j !=id-1 && !client_end[j]) //받는놈은 종료되지 않은놈이고 보내는놈이 아님 ret = cci_send(connection[j], buf, len, SEND_CONTEXT, 0); //보냄 if(client_end[id-1]){ //보내는놈이 종료를 원한 경우 sprintf(exit_msg,"client%d exits..\n", id); ret = cci_send(connection[j], exit_msg, strlen(exit_msg), SEND_CONTEXT, 0); //종료메세지 보냄 } } //end for j } }else{ /* 파일인 경우 */ for(i=0; i<number_of_connections; i++){ //어떤 connection에서 왔는지 check if(event->recv.connection == connection[i]){ if(strncasecmp((char*)event->recv.ptr,"file send completed\0", 20)==0){ //file 전송종료신호면 그만 fprintf(stderr,"%s. file size is %ldbytes.\n",(char*)event->recv.ptr, file_size[i]); memcpy(buf,event->recv.ptr, len); //종료신호 고대로 클라에게 read_size=len; file_size[i]=0; }else{ read_size=event->recv.len; //읽은 크기 file_size[i]+=read_size; //file size check fprintf(stderr, "%ld\n", file_size[i]); memcpy(buf, event->recv.ptr, read_size); //읽은거 buf에 써주기 }//end file trasport for(j=0; j<number_of_connections; j++){ /* broadcast */ if(j !=i && !client_end[j]){ ret = cci_send(connection[j], buf, read_size, SEND_CONTEXT, 0); if(ret) fprintf(stderr, "file send failed!\n"); } }//end for j break; }//end if }//end for i } break; }//end recv case case CCI_EVENT_SEND: assert(event->send.context == SEND_CONTEXT); assert(event->send.connection->context == ACCEPT_CONTEXT); fprintf(stderr, "completed send\n"); break; case CCI_EVENT_CONNECT_REQUEST: if (accept) { cci_accept(event, ACCEPT_CONTEXT); }else { cci_reject(event); } break; case CCI_EVENT_ACCEPT:{ char number[MAX_CONNECTION_SIZE]; assert(event->accept.connection != NULL); assert(event->accept.connection->context == ACCEPT_CONTEXT); connection[number_of_connections] = event->accept.connection; fprintf(stderr, "completed accept\n"); sprintf(number,"%d",number_of_connections+1); ret = cci_send(connection[number_of_connections], number, strlen(number), SEND_CONTEXT, 0); //몇번째 클라이언트 인지 알려줌 number_of_connections++; break; } default: printf("event type %d\n", event->type); break; } //end switch cci_return_event(event); } /* clean up */ cci_destroy_endpoint(endpoint); cci_finalize(); free(uri); return 0; }
int main(int argc, char *argv[]) { int ret, done = 0; uint32_t caps = 0; char *uri = NULL; cci_endpoint_t *endpoint = NULL; cci_os_handle_t *ep_fd = NULL; cci_connection_t *connection = NULL; ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* create an endpoint */ ret = cci_create_endpoint(NULL, 0, &endpoint, ep_fd); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(endpoint, CCI_OPT_ENDPT_URI, &uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } printf("Opened %s\n", uri); while (!done) { int accept = 1; cci_event_t *event; ret = cci_get_event(endpoint, &event); if (ret != 0) { if (ret != CCI_EAGAIN) fprintf(stderr, "cci_get_event() returned %s\n", cci_strerror(endpoint, ret)); continue; } switch (event->type) { case CCI_EVENT_RECV:{ char buf[8192]; char *data = "data:"; int offset = 0; int len = event->recv.len; assert(event->recv.connection == connection); assert(event->recv.connection->context == ACCEPT_CONTEXT); if (len == 3) { done = 1; continue; } memset(buf, 0, 8192); offset = strlen(data); memcpy(buf, data, offset); memcpy(buf + offset, event->recv.ptr, len); offset += len; fprintf(stderr, "recv'd \"%s\"\n", buf); ret = cci_send(connection, buf, offset, SEND_CONTEXT, 0); if (ret) fprintf(stderr, "send returned %s\n", cci_strerror(endpoint, ret)); break; } case CCI_EVENT_SEND: fprintf(stderr, "completed send\n"); assert(event->send.context == SEND_CONTEXT); assert(event->send.connection == connection); assert(event->send.connection->context == ACCEPT_CONTEXT); break; case CCI_EVENT_CONNECT_REQUEST: /* inspect conn_req_t and decide to accept or reject */ if (accept) { /* associate this connect request with this endpoint */ cci_accept(event, ACCEPT_CONTEXT); } else { cci_reject(event); } break; case CCI_EVENT_ACCEPT: fprintf(stderr, "completed accept\n"); assert(event->accept.connection != NULL); assert(event->accept.connection->context == ACCEPT_CONTEXT); connection = event->accept.connection; break; default: printf("event type %d\n", event->type); break; } cci_return_event(event); } /* clean up */ cci_destroy_endpoint(endpoint); cci_finalize(); free(uri); return 0; }
static void poll_events(void) { int ret; cci_event_t *event; if (blocking) { FD_ZERO(&rfds); FD_SET(fd, &rfds); ret = select(nfds, &rfds, NULL, NULL, NULL); if (!ret) return; } ret = cci_get_event(endpoint, &event); if (ret == CCI_SUCCESS) { assert(event); switch (event->type) { case CCI_EVENT_SEND: if (event->send.status != CCI_SUCCESS) { fprintf(stderr, "RMA failed with %s.\n", cci_strerror(endpoint, event->send.status)); cci_disconnect(test); test = NULL; done = 1; } if (is_server) break; /* Client */ if (event->send.context == (void *)0xdeadbeef) { done = 1; break; } break; case CCI_EVENT_RECV: if (is_client) { hdr_t *h = (void*)event->recv.ptr; if (!ready) { ready = 1; memcpy((void*)&remote_rma_handle, &h->reply.handle, sizeof(remote_rma_handle)); } else { /* RMA status msg */ if (opts.method == RMA_WRITE) { if (h->status.crc != msg.check.crc) { fprintf(stderr, "Server reported " "CRC failed.\n" "Local CRC 0x%x != " "remote CRC 0x%x.\n" "count=%d current_size=%u\n", msg.check.crc, h->status.crc, count, current_size); } } else { uint32_t crc = 0; void *ptr = (void*)((uintptr_t)buffer + local_offset); /* Compute the CRC only on a valid buffer */ if (current_size + local_offset <= opts.reg_len) crc = crc32(0, ptr, current_size); else crc = 0; if (crc != h->status.crc) { fprintf(stderr, "Server reported " "CRC failed.\n" "Local CRC 0x%x != " "remote CRC 0x%x.\n" "count=%d current_size=%u\n", crc, h->status.crc, count, current_size); } } /* RMA completed */ count++; if (count < iters) { ret = cci_rma(test, &msg, msg_len, local_rma_handle, local_offset, &remote_rma_handle, remote_offset, current_size, NULL, opts.flags); check_return(endpoint, "cci_rma", ret, 1); } } } else { hdr_t *h = (void*)event->recv.ptr; /* is_server */ if (event->recv.len == 3) { done = 1; } else { uint32_t crc = 0; void *ptr = (void*)((uintptr_t)buffer + h->check.offset); /* RMA check request */ if ((h->check.len + h->check.offset) <= opts.reg_len) crc = crc32(0, ptr, h->check.len); else crc = 0; msg.status.type = MSG_RMA_STATUS; msg.status.crc = crc; if (opts.method == RMA_WRITE) { fprintf(stderr, "server: client crc=0x%x " "server crc=0x%x\n", h->check.crc, crc); } print_buffer(ptr, h->check.len); ret = cci_send(test, &msg, sizeof(msg.status), NULL, CCI_FLAG_SILENT); check_return(endpoint, "cci_send", ret, 1); } } break; case CCI_EVENT_CONNECT: if (event->connect.status != CCI_SUCCESS) { fprintf(stderr, "Connection rejected.\n"); exit(0); } if ((uintptr_t)event->connect.context == (uintptr_t)CONTROL) { control = event->connect.connection; } else { test = event->connect.connection; } if (control && test) connect_done = 1; break; case CCI_EVENT_CONNECT_REQUEST: fprintf(stderr, "Peer is reconnecting? Rejecting.\n"); cci_reject(event); break; default: fprintf(stderr, "ignoring event type %s\n", cci_event_type_str(event->type)); } cci_return_event(event); } return; }
void client(char *server_uri) { int num_connect = 0, ret, i = 0, c; uint32_t caps = 0; cci_os_handle_t *fd = NULL; cci_endpoint_t *endpoint = NULL; cci_connection_t *connection = NULL; uint32_t timeout = 10 * 1000000; int context[3] = {0, 1, 2}; struct timeval wait; ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* create an endpoint */ ret = cci_create_endpoint(NULL, 0, &endpoint, fd); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* set conn tx timeout */ cci_set_opt(endpoint, CCI_OPT_ENDPT_SEND_TIMEOUT, &timeout); if (ret) { fprintf(stderr, "cci_set_opt() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } ret = cci_connect(endpoint, server_uri, "Happy", 5, CCI_CONN_ATTR_UU, (int *) &context[0], 0, NULL); if (ret) { fprintf(stderr, "cci_connect(0) failed with %s\n", cci_strerror(endpoint, ret)); } ret = cci_connect(endpoint, server_uri, "New", 3, CCI_CONN_ATTR_UU, (int *) &context[1], 0, NULL); if (ret) { fprintf(stderr, "cci_connect(1) failed with %s\n", cci_strerror(endpoint, ret)); } wait.tv_sec = 2; wait.tv_usec = 0; ret = cci_connect(endpoint, server_uri, "Year", 4, CCI_CONN_ATTR_UU, (int *) &context[2], 0, &wait); if (ret) { fprintf(stderr, "cci_connect(2) failed with %s\n", cci_strerror(endpoint, ret)); } /* poll for connect completion */ //while (num_connect < 3) while (num_connect < 1) /* connect timeouts are not registering */ poll_events(endpoint, &connection, &num_connect); printf("test passed\n"); fflush(stdout); /* server has to close first to avoid hang */ sleep(5); client_cleanup: ret = cci_destroy_endpoint(endpoint); if (ret) { fprintf(stderr, "cci_destroy_endpoint() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } ret = cci_finalize(); if (ret) { fprintf(stderr, "cci_finalize() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } }