File: msg_epoll.c

package info (click to toggle)
libfabric 2.1.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,108 kB
  • sloc: ansic: 387,262; python: 3,171; sh: 2,555; makefile: 1,313; cpp: 617; perl: 474; ruby: 123; asm: 27
file content (238 lines) | stat: -rw-r--r-- 5,182 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (c) 2013-2015 Intel Corporation.  All rights reserved.
 * Copyright (c) 2016, Cisco Systems, Inc. All rights reserved.
 *
 * This software is available to you under the BSD license
 * below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#if HAVE_CONFIG_H
#  include <config.h>
#endif /* HAVE_CONFIG_H */

#include <shared.h>

#if HAVE_EPOLL == 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/epoll.h>

#include <rdma/fi_errno.h>
#include <rdma/fi_cm.h>

static int epfd;

static int alloc_epoll_res(void)
{
	struct epoll_event event;
	int ret, fd;

	epfd = epoll_create1(0);
	if (epfd < 0) {
		ret = -errno;
		FT_PRINTERR("epoll_create1", ret);
		return ret;
	}

	memset((void *) &event, 0, sizeof event);
	if (opts.dst_addr) {
		fd = tx_fd;
		event.data.ptr = (void *) &txcq->fid;
	} else {
		fd = rx_fd;
		event.data.ptr = (void *) &rxcq->fid;
	}

	event.events = EPOLLIN;
	ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event);
	if (ret) {
		ret = -errno;
		FT_PRINTERR("epoll_ctl", ret);
		return ret;
	}

	return 0;
}

static int send_recv()
{
	struct fi_cq_entry comp;
	struct epoll_event event;
	struct fid *fids[1];
	int ret;
	const char *message = "Hello from Client!";
	size_t message_len = strlen(message) + 1;

	if (opts.dst_addr) {
		fprintf(stdout, "Posting a send...\n");
		if (snprintf(tx_buf, tx_size, "%s", message) >= tx_size) {
			fprintf(stderr, "Transmit buffer too small.\n");
			return -FI_ETOOSMALL;
		}
		ret = ft_post_tx(ep, remote_fi_addr, message_len, NO_CQ_DATA, &tx_ctx);
		if (ret)
			return ret;

		memset(&event, 0, sizeof event);
		fids[0] = &txcq->fid;
		do {
			if (fi_trywait(fabric, fids, 1) == FI_SUCCESS) {
				ret = TEMP_FAILURE_RETRY(epoll_wait(epfd, &event, 1, -1));
				if (ret < 0) {
					ret = -errno;
					FT_PRINTERR("epoll_wait", ret);
					return ret;
				}

				if (event.data.ptr != &txcq->fid)
					fprintf(stdout, "unexpected event!\n");
			}

			ret = fi_cq_read(txcq, &comp, 1);
		} while (ret == -FI_EAGAIN);

		if (ret < 0) {
			if (ret == -FI_EAVAIL)
				ret = ft_cq_readerr(txcq);
			return ret;
		}

		fprintf(stdout, "Send completion received\n");
	} else {
		fprintf(stdout, "Waiting for client...\n");

		memset(&event, 0, sizeof event);
		fids[0] = &rxcq->fid;
		do {
			if (fi_trywait(fabric, fids, 1) == FI_SUCCESS) {
				ret = TEMP_FAILURE_RETRY(epoll_wait(epfd, &event, 1, -1));
				if (ret < 0) {
					ret = -errno;
					FT_PRINTERR("epoll_wait", ret);
					return ret;
				}

				if (event.data.ptr != &rxcq->fid) {
					fprintf(stdout, "unexpected event!\n");
				}
			}

			ret = fi_cq_read(rxcq, &comp, 1);
		} while (ret == -FI_EAGAIN);

		if (ret < 0) {
			if (ret == -FI_EAVAIL)
				ret = ft_cq_readerr(rxcq);
			return ret;
		}

		ret = check_recv_msg(message);
		if (ret)
			return ret;
	}

	return 0;
}

static int run(void)
{
	int ret;

	if (!opts.dst_addr) {
		ret = ft_start_server();
		if (ret)
			return ret;
	}

	ret = opts.dst_addr ? ft_client_connect() : ft_server_connect();
	if (ret) {
		return ret;
	}

	ret = alloc_epoll_res();
	if (ret)
		return ret;

	ret = send_recv();

	fi_shutdown(ep, 0);
	return ret;
}

int main(int argc, char **argv)
{
	int op, ret;

	opts = INIT_OPTS;
	opts.options |= FT_OPT_SIZE;
	opts.comp_method = FT_COMP_WAIT_FD;

	hints = fi_allocinfo();
	if (!hints)
		return EXIT_FAILURE;

	while ((op = getopt(argc, argv, "h" ADDR_OPTS INFO_OPTS)) != -1) {
		switch (op) {
		default:
			ft_parse_addr_opts(op, optarg, &opts);
			ft_parseinfo(op, optarg, hints, &opts);
			break;
		case '?':
		case 'h':
			ft_usage(argv[0], "A simple MSG client-sever example that "
				"demonstrates one possible usage of the underlying "
				"cq wait objects.");
			return EXIT_FAILURE;
		}
	}

	if (optind < argc)
		opts.dst_addr = argv[optind];

	hints->ep_attr->type		= FI_EP_MSG;
	hints->caps			= FI_MSG;
	hints->domain_attr->mr_mode 	= opts.mr_mode;
	hints->addr_format		= opts.address_format;

	ret = run();

	ft_free_res();
	close(epfd);
	return ft_exit_code(ret);
}

#else

#include <rdma/fi_errno.h>

int main(int argc, char **argv)
{
	return ft_exit_code(FI_ENODATA);
}
#endif