-
Notifications
You must be signed in to change notification settings - Fork 1
/
wires.c
222 lines (183 loc) · 5.62 KB
/
wires.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
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <inttypes.h> /* PRIx8, etc. */
#include <libgen.h> /* basename */
#include <pthread.h> /* pthread_mutex_t */
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h> /* sigaction */
#include <sys/param.h> /* for MIN, MAX */
#include <ucp/api/ucp.h>
#include "rxpool.h"
#include "util.h"
#include "wiring.h"
#include "wireup.h"
static sig_atomic_t go = 1;
static void
handle_intr(int wiring_unused signo)
{
go = 0;
}
static void
usage(const char *_progname)
{
char *progname = strdup(_progname);
assert(progname != NULL);
fprintf(stderr, "usage: %s [remote address]\n", basename(progname));
free(progname);
exit(EXIT_FAILURE);
}
static bool
event_cb(wire_event_info_t evinfo, void wiring_unused *arg)
{
printf("wire event '%s'\n", wire_event_string(evinfo.event));
return true;
}
static bool
run_client(wiring_t *wiring, ucp_worker_h wiring_unused worker,
ucp_address_t *laddr, size_t laddrlen,
ucp_address_t *raddr, size_t raddrlen)
{
wire_id_t id = wireup_start(wiring, laddr, laddrlen, raddr, raddrlen,
event_cb, NULL, NULL);
return wire_is_valid(id);
}
static void
custom_lock(wiring_t wiring_unused *wiring, void *arg)
{
pthread_mutex_t *mtx = arg;
const int rc = pthread_mutex_lock(mtx);
assert(rc == 0);
}
static void
custom_unlock(wiring_t wiring_unused *wiring, void *arg)
{
pthread_mutex_t *mtx = arg;
const int rc = pthread_mutex_unlock(mtx);
assert(rc == 0);
}
static bool
custom_assert_locked(wiring_t wiring_unused *wiring, void *arg)
{
pthread_mutex_t *mtx = arg;
const int rc = pthread_mutex_trylock(mtx);
if (rc == EBUSY)
return true;
if (rc == 0) {
pthread_mutex_unlock(mtx);
return false;
}
abort();
}
int
main(int argc, char **argv)
{
ucp_context_attr_t context_attrs;
ucp_params_t global_params = {
.field_mask = UCP_PARAM_FIELD_FEATURES | UCP_PARAM_FIELD_REQUEST_SIZE
, .features = UCP_FEATURE_TAG | UCP_FEATURE_RMA
, .request_size = sizeof(rxdesc_t)
};
ucp_worker_params_t worker_params = {
.field_mask = UCP_WORKER_PARAM_FIELD_THREAD_MODE
, .thread_mode = UCS_THREAD_MODE_MULTI
};
struct sigaction sa = {.sa_handler = handle_intr}, osa;
wiring_t *wiring;
ucp_config_t *config;
ucp_context_h context;
ucp_worker_h worker;
ucp_address_t *laddr;
ucp_address_t *raddr;
const char *delim = "";
size_t i, laddrlen, raddrlen;
ucs_status_t status;
int rc = EXIT_SUCCESS;
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
wiring_lock_bundle_t lkb = {
.arg = &mtx
, .lock = custom_lock
, .unlock = custom_unlock
, .assert_locked = custom_assert_locked
};
if (sigemptyset(&sa.sa_mask) == -1)
err(EXIT_FAILURE, "%s: sigemptyset", __func__);
if (argc > 2)
usage(argv[0]);
if (argc == 2) {
uint8_t *buf;
if (colon_separated_octets_to_bytes(argv[1], &buf, &raddrlen) == -1)
errx(EXIT_FAILURE, "could not parse remote address `%s`", argv[1]);
printf("parsed %zu-byte remote address\n", raddrlen);
raddr = (void *)buf;
} else {
raddr = NULL;
}
if ((status = ucp_config_read(NULL, NULL, &config)) != UCS_OK)
errx(EXIT_FAILURE, "%s: ucp_config_read", __func__);
status = ucp_init(&global_params, config, &context);
ucp_config_release(config);
if (status != UCS_OK) {
errx(EXIT_FAILURE, "%s: ucp_init: %s", __func__,
ucs_status_string(status));
}
context_attrs.field_mask = UCP_ATTR_FIELD_REQUEST_SIZE;
status = ucp_context_query(context, &context_attrs);
if (status != UCS_OK)
errx(EXIT_FAILURE, "%s: ucp_context_query", __func__);
if ((context_attrs.field_mask & UCP_ATTR_FIELD_REQUEST_SIZE) == 0)
errx(EXIT_FAILURE, "context attributes contain no request size");
status = ucp_worker_create(context, &worker_params, &worker);
if (status != UCS_OK) {
warnx("%s: ucp_worker_create", __func__);
goto cleanup_context;
}
status = ucp_worker_get_address(worker, &laddr, &laddrlen);
if (status != UCS_OK) {
warnx("%s: ucp_worker_get_address", __func__);
goto cleanup_worker;
}
printf("%zu-byte local address ", laddrlen);
for (i = 0; i < laddrlen; i++) {
printf("%s%02" PRIx8, delim, ((uint8_t *)laddr)[i]);
delim = ":";
}
printf("\n");
wiring = wiring_create(worker, context_attrs.request_size, &lkb, NULL,
NULL);
if (wiring == NULL)
errx(EXIT_FAILURE, "%s: could not create wiring", __func__);
if (raddr != NULL) { /* * * client mode * * */
bool ok;
ok = run_client(wiring, worker, laddr, laddrlen, raddr, raddrlen);
ucp_worker_release_address(worker, laddr);
free(raddr);
if (!ok) {
warnx("%s: could not start wireup", __func__);
rc = EXIT_FAILURE;
goto cleanup_wiring;
}
} else
ucp_worker_release_address(worker, laddr);
if (sigaction(SIGINT, &sa, &osa) == -1)
err(EXIT_FAILURE, "%s.%d: sigaction", __func__, __LINE__);
while (go) {
int ret;
while ((ret = wireup_once(wiring)) > 0)
;
if (ret < 0)
break;
ucp_worker_progress(worker);
}
if (sigaction(SIGINT, &osa, NULL) == -1)
err(EXIT_FAILURE, "%s.%d: sigaction", __func__, __LINE__);
cleanup_wiring:
wiring_destroy(wiring, true);
cleanup_worker:
ucp_worker_destroy(worker);
cleanup_context:
ucp_cleanup(context);
return rc;
}