1 /* Copyright 2022 IGEL Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include "lease-config.h"
24 #define CONFIG_ERROR(x, ...) ERROR_LOG("%s: " x, filename, ##__VA_ARGS__)
26 static bool populate_connector_planes(struct connector_config *config,
29 config->nplanes = toml_array_nelem(planes);
30 config->planes = calloc(config->nplanes, sizeof(uint32_t));
31 if (!config->planes) {
32 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
36 for (int j = 0; j < config->nplanes; j++) {
37 toml_datum_t plane = toml_int_at(planes, j);
41 config->planes[j] = plane.u.i;
46 static bool populate_connector_config(struct lease_config *config,
47 toml_table_t *global_table,
50 int nconnectors = toml_array_nelem(conns);
51 config->connectors = calloc(nconnectors, sizeof(*config->connectors));
52 if (!config->connectors) {
53 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
57 config->nconnectors = nconnectors;
59 for (int i = 0; i < config->nconnectors; i++) {
60 struct connector_config *conn_config = &config->connectors[i];
61 toml_datum_t conn = toml_string_at(conns, i);
63 ERROR_LOG("Invalid connector in lease %s: idx:%d\n",
64 config->lease_name, i);
67 conn_config->name = conn.u.s;
69 toml_table_t *conn_config_data =
70 toml_table_in(global_table, conn.u.s);
71 if (!conn_config_data)
74 toml_datum_t optional =
75 toml_bool_in(conn_config_data, "optional");
77 config->connectors[i].optional = optional.u.b;
79 toml_array_t *planes =
80 toml_array_in(conn_config_data, "planes");
81 if (planes && !populate_connector_planes(conn_config, planes)) {
82 ERROR_LOG("Invalid plane id for connector: %s\n",
90 int parse_config(char *filename, struct lease_config **parsed_config)
92 struct lease_config *config = NULL;
93 int nconfigs, i, ret = 0;
94 char parse_error[160];
96 FILE *fp = fopen(filename, "r");
100 toml_table_t *t_config =
101 toml_parse_file(fp, parse_error, sizeof parse_error);
103 CONFIG_ERROR("configuration file parse error: %s\n",
109 toml_array_t *leases = toml_array_in(t_config, "lease");
112 "Invalid config - cannot find any 'lease' configs");
115 nconfigs = toml_array_nelem(leases);
116 config = calloc(nconfigs, sizeof *config);
119 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
123 for (i = 0; i < toml_array_nelem(leases); i++) {
124 toml_table_t *lease = toml_table_at(leases, i);
126 toml_datum_t name = toml_string_in(lease, "name");
128 CONFIG_ERROR("Invalid lease name in entry #%d\n", i);
129 goto err_free_config;
132 config[i].lease_name = name.u.s;
134 toml_array_t *conns = toml_array_in(lease, "connectors");
136 !populate_connector_config(&config[i], t_config, conns)) {
137 CONFIG_ERROR("Error configuring lease: %s\n",
138 config[i].lease_name);
139 goto err_free_config;
143 *parsed_config = config;
150 release_config(i, config);
154 void release_config(int num_leases, struct lease_config *config)
156 for (int i = 0; i < num_leases; i++) {
157 struct lease_config *c = &config[i];
159 for (int j = 0; j < c->nconnectors; j++) {
160 free(c->connectors[j].name);
161 free(c->connectors[j].planes);