rofisys 0.3.0

This system crate provides Rust language bindings (via the use of Bindgen) to the Rust-OFI library.
Documentation
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
/*
 * Copyright (c) 2015-2016, Cisco Systems, Inc. All rights reserved.
 * Copyright (c) 2015, Intel Corp., Inc.  All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or 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.
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include <rdma/fi_errno.h>

#include "ofi.h"
#include "ofi_list.h"

#ifdef SYSCONFDIR
#define DEFAULT_CONF_FILE_PATH SYSCONFDIR "/libfabric.conf"
#else
#define DEFAULT_CONF_FILE_PATH "libfabric.conf"
#endif

#define MAX_CONF_LINE_LENGTH 2048

extern void fi_ini(void);
int ofi_prefer_sysconfig = 0;

struct fi_param_entry {
	const struct fi_provider *provider;
	char *name;
	enum fi_param_type type;
	char *help_string;
	char *env_var_name;
	struct dlist_entry entry;
};

struct ofi_conf_entry {
	char *name;
	char *value;
	struct dlist_entry entry;
};

/* TODO: Add locking around param_list when adding dynamic removal */
static DEFINE_LIST(param_list);
static DEFINE_LIST(conf_list);


static struct fi_param_entry *
fi_find_param(const struct fi_provider *provider, const char *param_name)
{
	struct fi_param_entry *param;
	struct dlist_entry *entry;

	for (entry = param_list.next; entry != &param_list; entry = entry->next) {
		param = container_of(entry, struct fi_param_entry, entry);
		if (param->provider == provider &&
		    strcmp(param->name, param_name) == 0) {
			return param;
		}
	}

	FI_DBG(provider, FI_LOG_CORE,
		"Failed to find parameter %s: was not defined\n", param_name);
	return NULL;
}

__attribute__((visibility ("default"),EXTERNALLY_VISIBLE))
int DEFAULT_SYMVER_PRE(fi_getparams)(struct fi_param **params, int *count)
{
	struct fi_param *vhead = NULL;
	struct fi_param_entry *param;
	struct dlist_entry *entry;
	int cnt, i;
	char *tmp;

	fi_ini();

	for (entry = param_list.next, cnt = 0; entry != &param_list;
	     entry = entry->next)
		cnt++;

	if (cnt == 0)
		goto out;

	// last extra entry will be all NULL
	vhead = calloc(cnt + 1, sizeof (*vhead));
	if (!vhead)
		return -FI_ENOMEM;

	for (entry = param_list.next, i = 0; entry != &param_list;
	     entry = entry->next, i++) {
		param = container_of(entry, struct fi_param_entry, entry);
		vhead[i].name = strdup(param->env_var_name);
		vhead[i].type = param->type;
		vhead[i].help_string = strdup(param->help_string);

		tmp = getenv(param->env_var_name);
		if (tmp)
			vhead[i].value = strdup(tmp);

		if (!vhead[i].name || !vhead[i].help_string) {
			fi_freeparams(vhead);
			return -FI_ENOMEM;
		}
	}

out:
	*count = cnt;
	*params = vhead;
	return FI_SUCCESS;
}
DEFAULT_SYMVER(fi_getparams_, fi_getparams, FABRIC_1.0);

__attribute__((visibility ("default"),EXTERNALLY_VISIBLE))
void DEFAULT_SYMVER_PRE(fi_freeparams)(struct fi_param *params)
{
	int i;
	for (i = 0; params[i].name; ++i) {
		free((void*) params[i].name);
		free((void*) params[i].help_string);
		free((void*) params[i].value);
	}
	free(params);
}
DEFAULT_SYMVER(fi_freeparams_, fi_freeparams, FABRIC_1.0);

static void fi_free_param(struct fi_param_entry *param)
{
	free(param->name);
	free(param->help_string);
	free(param->env_var_name);
	free(param);
}

static void free_conf(struct ofi_conf_entry *conf)
{
	free(conf->name);
	free(conf->value);
	free(conf);
}

void fi_param_undefine(const struct fi_provider *provider)
{
	struct fi_param_entry *param;
	struct dlist_entry *entry;
	struct dlist_entry *next;

	for (entry = param_list.next; entry != &param_list; entry = next) {
		next = entry->next;
		param = container_of(entry, struct fi_param_entry, entry);
		if (param->provider == provider) {
			FI_DBG(provider, FI_LOG_CORE, "Removing param: %s\n", param->name);
			dlist_remove(entry);
			fi_free_param(param);
		}
	}
}

__attribute__((visibility ("default"),EXTERNALLY_VISIBLE))
int DEFAULT_SYMVER_PRE(fi_param_define)(const struct fi_provider *provider,
		const char *param_name, enum fi_param_type type,
		const char *help_string_fmt, ...)
{
	int i, ret;
	struct fi_param_entry *v;
	char *tmp_str;
	va_list vargs;

	if (!provider)
		provider = &core_prov;

	// Check for bozo cases
	if (param_name == NULL || help_string_fmt == NULL || *help_string_fmt == '\0') {
		FI_DBG(provider, FI_LOG_CORE,
			"Failed to register %s variable: provider coding error\n",
			param_name);
		return -FI_EINVAL;
	}

	v = calloc(1, sizeof(*v));
	if (!v) {
		FI_DBG(provider, FI_LOG_CORE,
			"Failed to register %s variable: ENOMEM\n", param_name);
		return -FI_ENOMEM;
	}

	v->provider = provider;
	v->name = strdup(param_name);
	v->type = type;

	va_start(vargs, help_string_fmt);
	ret = vasprintf(&v->help_string, help_string_fmt, vargs);
	va_end(vargs);
	if (ret < 0)
		v->help_string = NULL;

	if (provider != &core_prov) {
		ret = asprintf(&tmp_str, "%s: %s", provider->name, v->help_string);
		free(v->help_string);
		if (ret < 0)
			v->help_string = NULL;
		v->help_string = tmp_str;
		ret = asprintf(&v->env_var_name, "FI_%s_%s", provider->name, param_name);
		if (ret < 0)
			v->env_var_name = NULL;
	} else {
		ret = asprintf(&v->env_var_name, "FI_%s", param_name);
		if (ret < 0)
			v->env_var_name = NULL;
	}
	if (!v->name || !v->help_string || !v->env_var_name) {
		fi_free_param(v);
		FI_DBG(provider, FI_LOG_CORE,
			"Failed to register %s variable: ENOMEM\n", param_name);
		return -FI_ENOMEM;
	}

	for (i = 0; v->env_var_name[i]; ++i)
		v->env_var_name[i] = (char) toupper(v->env_var_name[i]);

	dlist_insert_tail(&v->entry, &param_list);

	FI_DBG(provider, FI_LOG_CORE, "registered var %s\n", param_name);
	return FI_SUCCESS;
}
DEFAULT_SYMVER(fi_param_define_, fi_param_define, FABRIC_1.0);

static int fi_parse_bool(const char *str_value)
{
	if (strcmp(str_value, "0") == 0 ||
	    strcasecmp(str_value, "false") == 0 ||
	    strcasecmp(str_value, "no") == 0 ||
	    strcasecmp(str_value, "off") == 0) {
		return 0;
	}

	if (strcmp(str_value, "1") == 0 ||
	    strcasecmp(str_value, "true") == 0 ||
	    strcasecmp(str_value, "yes") == 0 ||
	    strcasecmp(str_value, "on") == 0) {
		return 1;
	}

	return -1;
}

static void load_conf(void)
{
	char *tmp;
	struct ofi_conf_entry *conf;
	size_t line_len = 0;
	char line[MAX_CONF_LINE_LENGTH];

	dlist_init(&conf_list);

	FILE *fptr = fopen(DEFAULT_CONF_FILE_PATH, "r");
	if (!fptr)
		return;

	while (fgets(line, MAX_CONF_LINE_LENGTH, fptr) != NULL) {
		tmp = strchr(line, '=');
		if (!tmp)
			continue;

		line_len = strlen(line);
		tmp[0] = '\0';
		if (line[line_len-1] == '\n')
			line[line_len-1] = '\0';

		conf = calloc(1, sizeof(*conf));
		if (!conf)
			break;

		conf->name = strdup(line);
		if (!conf->name) {
			free_conf(conf);
			break;
		}

		conf->value = strdup(&tmp[1]);
		if (!conf->value) {
			free_conf(conf);
			break;
		}

		dlist_insert_tail(&conf->entry, &conf_list);
	}

	fclose(fptr);
}

static struct ofi_conf_entry *
find_conf_entry(const char *param_name)
{
	struct ofi_conf_entry *conf;

	dlist_foreach_container(&conf_list, struct ofi_conf_entry, conf, entry) {
		if (strcmp(conf->name, param_name) == 0)
			return conf;
	}

	return NULL;
}

void ofi_dump_sysconfig(void)
{
	struct ofi_conf_entry *conf;
	dlist_foreach_container(&conf_list, struct ofi_conf_entry, conf, entry) {
		FI_INFO(&core_prov, FI_LOG_CORE,
                        "Read config variable: %s=%s\n",conf->name, conf->value);
	}
}

__attribute__((visibility ("default"),EXTERNALLY_VISIBLE))
int DEFAULT_SYMVER_PRE(fi_param_get)(struct fi_provider *provider,
		const char *param_name, void *value)
{
	struct fi_param_entry *param;
	struct ofi_conf_entry *conf;
	char *str_value;
	int parsed_boolean;
	int ret = FI_SUCCESS;

	if (!provider)
		provider = &core_prov;

	if (!param_name || !value) {
		FI_DBG(provider, FI_LOG_CORE,
			"Failed to read %s variable: provider coding error\n",
			param_name);
		return -FI_EINVAL;
	}

	param = fi_find_param(provider, param_name);
	if (!param)
		return -FI_ENOENT;

	conf = find_conf_entry(param->env_var_name);
	str_value = getenv(param->env_var_name);
	if ((!str_value || ofi_prefer_sysconfig) && conf)
		str_value = conf->value;

	if (!str_value) {
		FI_INFO(provider, FI_LOG_CORE,
			"variable %s=<not set>\n", param_name);
		ret = -FI_ENODATA;
		goto out;
	}

	switch (param->type) {
	case FI_PARAM_STRING:
		* ((char **) value) = str_value;
		FI_INFO(provider, FI_LOG_CORE,
			"read string var %s=%s\n", param_name, *(char **) value);
		break;
	case FI_PARAM_INT:
		* ((int *) value) = strtol(str_value, NULL, 0);
		FI_INFO(provider, FI_LOG_CORE,
			"read int var %s=%d\n", param_name, *(int *) value);
		break;
	case FI_PARAM_BOOL:
		parsed_boolean = fi_parse_bool(str_value);
		if (parsed_boolean == -1) {
			ret = -FI_EINVAL;
			FI_WARN(provider, FI_LOG_CORE,
					"failed to parse bool var %s=%s\n", param_name, str_value);
			break;
		}

		* ((int *) value) = parsed_boolean;
		FI_INFO(provider, FI_LOG_CORE,
			"read bool var %s=%d\n", param_name, *(int *) value);
		break;
	case FI_PARAM_SIZE_T:
		* ((size_t *) value) = strtol(str_value, NULL, 0);
		FI_INFO(provider, FI_LOG_CORE,
			"read long var %s=%zu\n", param_name, *(size_t *) value);
		break;
	}

out:
	return ret;
}
DEFAULT_SYMVER(fi_param_get_, fi_param_get, FABRIC_1.0);


void fi_param_init(void)
{
	dlist_init(&param_list);
	load_conf();

	fi_param_define(NULL, "prefer_sysconfig", FI_PARAM_BOOL,
			"Prefer system configured variables when loading the "
			"environment and variables are defined in both the "
			"system config (libfabric.conf) and in runtime "
			"environment. (default: false)");
	fi_param_get_bool(NULL, "prefer_sysconfig", &ofi_prefer_sysconfig);
}

void fi_param_fini(void)
{
	struct fi_param_entry *param;
	struct ofi_conf_entry *conf;
	struct dlist_entry *entry;

	while (!dlist_empty(&param_list)) {
		entry = param_list.next;
		param = container_of(entry, struct fi_param_entry, entry);
		dlist_remove(entry);
		fi_free_param(param);
	}

	while (!dlist_empty(&conf_list)) {
		entry = conf_list.next;
		conf = container_of(entry, struct ofi_conf_entry, entry);
		dlist_remove(entry);
		free_conf(conf);
	}
}