Skip to content

Commit

Permalink
# This is a combination of 11 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

UCS/DATASTRUCT: Add allow_list data struct

# The commit message openucx#2 will be skipped:

# add test

# The commit message openucx#3 will be skipped:

# use allow_list

# The commit message openucx#4 will be skipped:

# use allow_list

# The commit message openucx#5 will be skipped:

# Add allow_list mode

# The commit message openucx#6 will be skipped:

# Use separate funcs for allow_list

# The commit message openucx#7 will be skipped:

# implement scanf, release, print

# The commit message openucx#8 will be skipped:

# implement clone

# The commit message openucx#9 will be skipped:

# tests

# The commit message openucx#10 will be skipped:

# Add test; Fix ^

# The commit message openucx#11 will be skipped:

# Add test
  • Loading branch information
gleon99 committed Dec 13, 2020
1 parent dedb40f commit 24b4d39
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/ucp/core/ucp_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,9 @@ static int ucp_tls_array_is_present(const char **tls, unsigned count,

if ((mask = ucp_str_array_search(tls, count, tl_name, NULL)) != 0) {
*tl_cfg_mask |= mask;
if (tl_name[0] == '^' || tl_name[1] == '^') {
return 1;
}
ucs_trace("enabling tl '%s'%s", tl_name, info);
return 1;
} else if ((mask = ucp_str_array_search(tls, count, tl_name, "aux")) != 0) {
Expand All @@ -493,8 +496,20 @@ static int ucp_config_is_tl_enabled(const char **names, unsigned count,
uint8_t *rsc_flags, uint64_t *tl_cfg_mask)
{
char strict_name[UCT_TL_NAME_MAX + 1];
char bad_name[UCT_TL_NAME_MAX + 1];

ucs_trace("LEO is_tl_enabled %s\n", tl_name);

snprintf(strict_name, sizeof(strict_name), "\\%s", tl_name);
snprintf(bad_name, sizeof(bad_name), "^%s", tl_name);
ucs_trace("LEO 'disabled' name: %s\n", bad_name);
if (ucp_tls_array_is_present(names, count, bad_name, "",
rsc_flags, tl_cfg_mask)) {
ucs_trace("LEO found disabled\n");

return 0;
}
ucs_trace("LEO not found\n");
return /* strict name, with leading \\ */
(!is_alias && ucp_tls_array_is_present(names, count, strict_name, "",
rsc_flags, tl_cfg_mask)) ||
Expand Down Expand Up @@ -585,6 +600,7 @@ static int ucp_is_resource_enabled(const uct_tl_resource_desc_t *resource,
uint64_t dev_cfg_masks[], uint64_t *tl_cfg_mask)
{
int device_enabled, tl_enabled;
ucs_trace("LEO is_resource_enabled %s %s\n", resource->tl_name, resource->dev_name);

/* Find the enabled devices */
device_enabled = (*rsc_flags & UCP_TL_RSC_FLAG_SOCKADDR) ||
Expand All @@ -598,6 +614,7 @@ static int ucp_is_resource_enabled(const uct_tl_resource_desc_t *resource,
(const char**)config->tls.names,
config->tls.count, rsc_flags,
tl_cfg_mask);
ucs_trace("LEO tl_enabled = %d\n", tl_enabled);

ucs_trace(UCT_TL_RESOURCE_DESC_FMT " is %sabled",
UCT_TL_RESOURCE_DESC_ARG(resource),
Expand Down
146 changes: 146 additions & 0 deletions src/ucs/config/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const char *ucs_async_mode_names[] = {
};

UCS_CONFIG_DEFINE_ARRAY(string, sizeof(char*), UCS_CONFIG_TYPE_STRING);
/* TODO: Needed? */
ucs_config_array_t ucs_config_array_allow_list = {sizeof(char*), UCS_CONFIG_TYPE_STRING};

/* Fwd */
static ucs_status_t
Expand Down Expand Up @@ -855,6 +857,150 @@ void ucs_config_help_array(char *buf, size_t max, const void *arg)
array->parser.help(buf + strlen(buf), max - strlen(buf), array->parser.arg);
}

int ucs_config_sscanf_allow_list(const char *buf, void *dest, const void *arg)
{
ucs_config_allow_list_t *field = dest;
void *temp_field = NULL;
const ucs_config_array_t *array = arg;
char *str_dup, *token, *saveptr;
int ret;
unsigned i;

str_dup = strdup(buf);
if (str_dup == NULL) {
return 0;
}

if (str_dup[0] == '^') {
field->mode = UCS_CONFIG_ALLOW_LIST_FORBID;
} else {
field->mode = UCS_CONFIG_ALLOW_LIST_ALLOW;
}

saveptr = NULL;
token = strtok_r(str_dup + (field->mode == UCS_CONFIG_ALLOW_LIST_FORBID), ",", &saveptr);

i = 0;
while (token != NULL) {
if (!strcmp(token, "all")) {
if (field->mode == UCS_CONFIG_ALLOW_LIST_FORBID || temp_field) {
/* Either "forbid all" or putting "all" after other strings */
return UCS_ERR_INVALID_PARAM;
} else {
field->mode = UCS_CONFIG_ALLOW_LIST_ALL;
field->array.count = 0;
return 1;
}
}

if (!temp_field) {
temp_field = ucs_calloc(UCS_CONFIG_ARRAY_MAX, array->elem_size, "config array");
}
ret = array->parser.read(token, (char*)temp_field + i * array->elem_size,
array->parser.arg);
if (!ret) {
ucs_free(temp_field);
free(str_dup);
return 0;
}

++i;
if (i >= UCS_CONFIG_ARRAY_MAX) {
break;
}
token = strtok_r(NULL, ",", &saveptr);
}

field->array.names = temp_field;
field->array.count = i;
free(str_dup);
return 1;
}

int ucs_config_sprintf_allow_list(char *buf, size_t max,
const void *src, const void *arg)
{
const ucs_config_allow_list_t *allow_list = src;
const ucs_config_array_t *array = arg;
size_t offset;
unsigned i;
int ret;

offset = 0;
for (i = 0; i < allow_list->array.count; ++i) {
if (i > 0 && offset < max) {
buf[offset++] = ',';
}
ret = array->parser.write(buf + offset, max - offset,
(char*)allow_list->array.names + i * array->elem_size,
array->parser.arg);
if (!ret) {
return 0;
}

offset += strlen(buf + offset);
}
return 1;
}

ucs_status_t ucs_config_clone_allow_list(const void *src, void *dest, const void *arg)
{
const ucs_config_allow_list_t *src_list = src;
const ucs_config_array_t *array = arg;
ucs_config_allow_list_t *dest_list = dest;
ucs_status_t status;
unsigned i;

dest_list->mode = src_list->mode;
if (src_list->array.count == 0) {
dest_list->array.count = 0;
return UCS_OK;
}
dest_list->array.names = ucs_calloc(src_list->array.count, array->elem_size,
"config array");
if (dest_list->array.names == NULL) {
return UCS_ERR_NO_MEMORY;
}

dest_list->array.count = src_list->array.count;
for (i = 0; i < src_list->array.count; ++i) {
status = array->parser.clone((const char*)src_list->array.names + i * array->elem_size,
(char*)dest_list->array.names + i * array->elem_size,
array->parser.arg);
if (status != UCS_OK) {
ucs_free(dest_list->array.names);
return status;
}
}

return UCS_OK;
}

void ucs_config_release_allow_list(void *ptr, const void *arg)
{
ucs_config_allow_list_t *allow_list = ptr;
const ucs_config_array_t *array = arg;
unsigned i;

if (allow_list->mode == UCS_CONFIG_ALLOW_LIST_ALL) {
return;
}

for (i = 0; i < allow_list->array.count; ++i) {
array->parser.release((char*)allow_list->array.names + i * array->elem_size,
array->parser.arg);
}
ucs_free(allow_list->array.names);
}

void ucs_config_help_allow_list(char *buf, size_t max, const void *arg)
{
const ucs_config_array_t *array = arg;

snprintf(buf, max, "comma-separated list of: ");
array->parser.help(buf + strlen(buf), max - strlen(buf), array->parser.arg);
}

int ucs_config_sscanf_table(const char *buf, void *dest, const void *arg)
{
char *tokens;
Expand Down
10 changes: 10 additions & 0 deletions src/ucs/config/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ ucs_status_t ucs_config_clone_array(const void *src, void *dest, const void *arg
void ucs_config_release_array(void *ptr, const void *arg);
void ucs_config_help_array(char *buf, size_t max, const void *arg);

int ucs_config_sscanf_allow_list(const char *buf, void *dest, const void *arg);
int ucs_config_sprintf_allow_list(char *buf, size_t max, const void *src, const void *arg);
ucs_status_t ucs_config_clone_allow_list(const void *src, void *dest, const void *arg);
void ucs_config_release_allow_list(void *ptr, const void *arg);
void ucs_config_help_allow_list(char *buf, size_t max, const void *arg);

int ucs_config_sscanf_table(const char *buf, void *dest, const void *arg);
ucs_status_t ucs_config_clone_table(const void *src, void *dest, const void *arg);
void ucs_config_release_table(void *ptr, const void *arg);
Expand Down Expand Up @@ -321,6 +327,10 @@ void ucs_config_help_generic(char *buf, size_t max, const void *arg);
ucs_config_clone_array, ucs_config_release_array, \
ucs_config_help_array, &ucs_config_array_##a}

#define UCS_CONFIG_TYPE_ALLOW_LIST {ucs_config_sscanf_allow_list, ucs_config_sprintf_allow_list, \
ucs_config_clone_allow_list, ucs_config_release_allow_list, \
ucs_config_help_allow_list, &ucs_config_array_string}

#define UCS_CONFIG_TYPE_TABLE(t) {ucs_config_sscanf_table, NULL, \
ucs_config_clone_table, ucs_config_release_table, \
ucs_config_help_table, t}
Expand Down
12 changes: 12 additions & 0 deletions src/ucs/config/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ typedef enum {

typedef UCS_CONFIG_STRING_ARRAY_FIELD(names) ucs_config_names_array_t;

typedef enum {
UCS_CONFIG_ALLOW_LIST_ALL = UCS_BIT(0),
UCS_CONFIG_ALLOW_LIST_ALLOW = UCS_BIT(1),
UCS_CONFIG_ALLOW_LIST_FORBID = UCS_BIT(2)
} ucs_config_allow_list_mode_t;


typedef struct {
ucs_config_names_array_t array;
ucs_config_allow_list_mode_t mode;
} ucs_config_allow_list_t;

/**
* @ingroup UCS_RESOURCE
* BSD socket address specification.
Expand Down
38 changes: 34 additions & 4 deletions test/gtest/ucs/test_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ typedef struct {
ucs_time_t time_value;
ucs_time_t time_auto;
ucs_time_t time_inf;
ucs_config_allow_list_t allow_list;

} car_opts_t;


Expand Down Expand Up @@ -218,6 +220,9 @@ ucs_config_field_t car_opts_table[] = {
{"TIME_INF", "inf", "Time value \"inf\"",
ucs_offsetof(car_opts_t, time_inf), UCS_CONFIG_TYPE_TIME_UNITS},

{"ALLOW_LIST", "all", "Allow-list: \"all\" OR \"val1,val2\" OR \"^val1,val2\"",
ucs_offsetof(car_opts_t, allow_list), UCS_CONFIG_TYPE_ALLOW_LIST},

{NULL}
};

Expand Down Expand Up @@ -414,6 +419,8 @@ UCS_TEST_F(test_config, parse_default) {
EXPECT_EQ(ucs_time_from_sec(1.0), opts->time_value);
EXPECT_EQ(UCS_TIME_AUTO, opts->time_auto);
EXPECT_EQ(UCS_TIME_INFINITY, opts->time_inf);
EXPECT_EQ(UCS_CONFIG_ALLOW_LIST_ALL, opts->allow_list.mode);
EXPECT_EQ(0, opts->allow_list.array.count);
}

UCS_TEST_F(test_config, clone) {
Expand All @@ -436,6 +443,7 @@ UCS_TEST_F(test_config, clone) {
}

EXPECT_EQ(COLOR_WHITE, (*opts_clone_ptr)->color);
EXPECT_EQ(UCS_CONFIG_ALLOW_LIST_ALL, (*opts_clone_ptr)->allow_list.mode);
delete opts_clone_ptr;
}

Expand Down Expand Up @@ -540,27 +548,27 @@ UCS_TEST_F(test_config, unused) {

UCS_TEST_F(test_config, dump) {
/* aliases must not be counted here */
test_config_print_opts(UCS_CONFIG_PRINT_CONFIG, 31u);
test_config_print_opts(UCS_CONFIG_PRINT_CONFIG, 32u);
}

UCS_TEST_F(test_config, dump_hidden) {
/* aliases must be counted here */
test_config_print_opts((UCS_CONFIG_PRINT_CONFIG |
UCS_CONFIG_PRINT_HIDDEN),
38u);
39u);
}

UCS_TEST_F(test_config, dump_hidden_check_alias_name) {
/* aliases must be counted here */
test_config_print_opts((UCS_CONFIG_PRINT_CONFIG |
UCS_CONFIG_PRINT_HIDDEN |
UCS_CONFIG_PRINT_DOC),
38u);
39u);

test_config_print_opts((UCS_CONFIG_PRINT_CONFIG |
UCS_CONFIG_PRINT_HIDDEN |
UCS_CONFIG_PRINT_DOC),
38u, "TEST_");
39u, "TEST_");
}

UCS_TEST_F(test_config, deprecated) {
Expand Down Expand Up @@ -594,3 +602,25 @@ UCS_TEST_F(test_config, deprecated) {
/* reset to not warn about unused env vars */
ucs_global_opts.warn_unused_env_vars = 0;
}

UCS_TEST_F(test_config, test_allow_list) {
{
ucs::scoped_setenv env1("UCX_ALLOW_LIST", "first,second");

car_opts opts(UCS_DEFAULT_ENV_PREFIX, NULL);
EXPECT_EQ(UCS_CONFIG_ALLOW_LIST_ALLOW, opts->allow_list.mode);
EXPECT_EQ(2, opts->allow_list.array.count);
EXPECT_EQ(std::string("first"), opts->allow_list.array.names[0]);
EXPECT_EQ(std::string("second"), opts->allow_list.array.names[1]);
}

{
ucs::scoped_setenv env1("UCX_ALLOW_LIST", "^first,second");

car_opts opts(UCS_DEFAULT_ENV_PREFIX, NULL);
EXPECT_EQ(UCS_CONFIG_ALLOW_LIST_FORBID, opts->allow_list.mode);
EXPECT_EQ(2, opts->allow_list.array.count);
EXPECT_EQ(std::string("first"), opts->allow_list.array.names[0]);
EXPECT_EQ(std::string("second"), opts->allow_list.array.names[1]);
}
}

0 comments on commit 24b4d39

Please sign in to comment.