Merge "Fix latency accuracy and dumping latencies to file"
[samplevnf.git] / VNFs / DPPD-PROX / cfgfile.c
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include "cfgfile.h"
18
19 #include <rte_string_fns.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <unistd.h>
25
26 #include "parse_utils.h"
27 #include "log.h"
28 #include "quit.h"
29
30 #define UINT32_MAX_STR "4294967295"
31
32 /*
33  * Allocate cfg_file structure.
34  * Returns pointer to the allocated structure, NULL otherwise.
35  */
36 struct cfg_file *cfg_open(const char *cfg_name)
37 {
38         if (cfg_name == NULL) {
39                 plog_err("\tNo config file name provided\n");
40                 return NULL;
41         }
42         if (access(cfg_name, F_OK)) {
43                 plog_err("\tError opening config file '%s': %s\n", cfg_name, strerror(errno));
44                 return NULL;
45         }
46
47         FILE *pf = fopen(cfg_name, "rb");
48         if (pf == NULL) {
49                 plog_err("\tError opening config file '%s'\n", cfg_name);
50                 return NULL;
51         }
52
53         struct cfg_file *pcfg = calloc(1, sizeof(struct cfg_file));
54
55         if (pcfg == NULL) {
56                 fclose(pf);
57                 plog_err("\tCouldn't allocate memory for config file struct\n");
58                 return NULL;
59         }
60
61         pcfg->pfile = pf;
62         pcfg->name = strdup(cfg_name);
63
64         return pcfg;
65 }
66
67 /* Free memory allocated for cfg_file structure.
68  * Returns 0 on success, -1 if the pointer to the pcfg is invalid */
69 int cfg_close(struct cfg_file *pcfg)
70 {
71         if (pcfg == NULL) {
72                 return -1;
73         }
74
75         if (pcfg->name != NULL) {
76                 free(pcfg->name);
77         }
78         if (pcfg->err_section != NULL) {
79                 free(pcfg->err_section);
80         }
81         if (pcfg->pfile != NULL) {
82                 fclose(pcfg->pfile);
83         }
84
85         free(pcfg);
86         return 0;
87 }
88
89 static int cfg_get_pos(struct cfg_file *pcfg, fpos_t *pos)
90 {
91         pcfg->index_line = pcfg->line;
92         return fgetpos(pcfg->pfile, pos);
93 }
94
95 static int cfg_set_pos(struct cfg_file *pcfg, fpos_t *pos)
96 {
97         pcfg->line = pcfg->index_line;
98         return fsetpos(pcfg->pfile, pos);
99 }
100
101 /*
102  * Read a line from the configuration file.
103  * Returns: on success length of the line read from the file is returned,
104  *          0 to indicate End of File,
105  *         -1 in case of wrong function parameters
106  */
107 static int cfg_get_line(struct cfg_file *pcfg, char *buffer, unsigned len, int raw_lines)
108 {
109         char *ptr;
110
111         if (pcfg == NULL || pcfg->pfile == NULL || buffer == NULL || len == 0) {
112                 return -1;
113         }
114
115         do {
116                 ptr = fgets(buffer, len, pcfg->pfile);
117                 if (ptr == NULL) {
118                         return 0; /* end of file */
119                 }
120                 ++pcfg->line;
121
122                 if (raw_lines) {
123                         break;
124                 }
125
126                 /* remove comments */
127                 ptr = strchr(buffer, ';');
128                 if (ptr != NULL) {
129                         *ptr = '\0';
130                 }
131                 else {
132                         ptr = strchr(buffer, '\0');
133                 }
134
135                 /* remove trailing spaces */
136                 if (ptr != buffer) {
137                         ptr--;
138                         while (isspace(*ptr)) {
139                                 *ptr = '\0';
140                                 ptr--;
141                         }
142                 }
143
144                 ptr = buffer;
145                 /* remove leading spaces */
146                 while (*ptr && isspace(*ptr)) {
147                         ++ptr;
148                 }
149                 if (ptr != buffer) {
150                         strcpy(buffer, ptr);
151                         ptr = buffer;
152                 }
153         }
154         while (*ptr == '\0'); /* skip empty strings */
155
156         return strlen(buffer);
157 }
158
159 /*
160  * Checks if buffer contains section name specified by the cfg_section pointer.
161  * Returns NULL if section name does not match, cfg_section pointer otherwise
162  */
163 static struct cfg_section *cfg_check_section(char *buffer, struct cfg_section *psec)
164 {
165         char *pend;
166         unsigned len;
167         static const char *valid = "0123456789,hs- \t";
168
169         pend = strchr(buffer, ']');
170         if (pend == NULL) {
171                 return NULL; /* ']' not found: invalid section name */
172         }
173
174         *pend = '\0';
175
176         /* check if section is indexed */
177         pend = strchr(psec->name, '#');
178         if (pend == NULL) {
179                 return (strcmp(buffer, psec->name) == 0) ? psec : NULL;
180         }
181
182         /* get section index */
183         len = pend - psec->name;
184         if (strncmp(buffer, psec->name, len) != 0) {
185                 return NULL;
186         }
187         pend = buffer + len;
188         if (*pend == '\0') {
189                 return NULL;
190         }
191         /* only numeric characters are valid for section index
192            (currently, variables not checked!) */
193         if (pend[0] != '$') {
194                 for (len = 0; pend[len] != '\0'; ++len) {
195                         if (strchr(valid, pend[len]) == NULL) {
196                                 return NULL;
197                         }
198                 }
199         }
200
201         psec->nbindex = parse_list_set(psec->indexp, pend, MAX_INDEX);
202         PROX_PANIC(psec->nbindex == -1, "\t\tError in cfg_check_section('%s'): %s\n", buffer, get_parse_err());
203
204         for (int i = 0; i < psec->nbindex; ++i) {
205                 psec->indexp[i] |= CFG_INDEXED;
206         }
207
208         return psec;
209 }
210
211 static char *cfg_get_section_name(struct cfg_section *psec)
212 {
213         char *name;
214
215         if (!(psec->indexp[0] & CFG_INDEXED)) {
216                 return strdup(psec->name);
217         }
218
219         name = malloc(strlen(psec->name) + strlen(UINT32_MAX_STR));
220         if (name != NULL) {
221                 strcpy(name, psec->name);
222                 char *pidx = strchr(name, '#');
223                 if (pidx != NULL) {
224                         sprintf(pidx, "%u", psec->indexp[0] & ~CFG_INDEXED);
225                 }
226         }
227         return name;
228 }
229
230 /*
231  * Reads configuration file and parses section specified by psec pointer.
232  * Returns 0 on success, -1 otherwise
233  */
234 int cfg_parse(struct cfg_file *pcfg, struct cfg_section *psec)
235 {
236         int error;
237         unsigned entry = 0;
238         fpos_t pos;
239         int index_count = 0;
240         struct cfg_section *section = NULL;
241         char buffer[sizeof(pcfg->cur_line)] = {0};
242
243         if (pcfg == NULL || psec == NULL) {
244                 return -1;
245         }
246
247         pcfg->line = 0;
248         fseek(pcfg->pfile, 0, SEEK_SET);
249
250         /* read configuration file and parse section specified by psec pointer */
251         while (1) {
252                 if (psec->raw_lines) {
253                         /* skip until section starts */
254                         char *lines = pcfg->cur_line;
255                         size_t max_len = sizeof(pcfg->cur_line);
256                         char *ret;
257
258                         do {
259                                 ret = fgets(lines, max_len, pcfg->pfile);
260                                 if (ret && *ret == '[') {
261                                         section = cfg_check_section(lines + 1, psec);
262                                 }
263                         } while (!section && ret);
264
265                         if (!ret)
266                                 return 0;
267
268                         do {
269
270                                 ret = fgets(buffer, sizeof(buffer), pcfg->pfile);
271                                 if (ret && *ret != '[') {
272                                         size_t l = strlen(buffer);
273                                         strncpy(lines, buffer, max_len);
274                                         max_len -= l;
275                                         lines += l;
276                                 }
277                         } while ((ret && *ret != '['));
278
279                         if (section != NULL) {
280                                 error = section->parser(section->indexp[index_count], pcfg->cur_line, section->data);
281                                 if (error != 0) {
282                                         section->error = error;
283                                         /* log only the very first error */
284                                         if (!pcfg->err_section) {
285                                                 pcfg->err_line = pcfg->line;
286                                                 pcfg->err_entry = entry;
287                                                 pcfg->err_section = cfg_get_section_name(section);
288                                         }
289                                         return 0;
290                                 }
291                                 ++entry;
292                         }
293                         return 0;
294                 }
295
296                 while (cfg_get_line(pcfg, buffer, MAX_CFG_STRING_LEN, psec->raw_lines) > 0) {
297                         strncpy(pcfg->cur_line, buffer, sizeof(pcfg->cur_line));
298                         if (*buffer == '[') {
299                                 if (index_count + 1 < psec->nbindex) {
300                                         // Need to loop - go back to recorded postion in file
301                                         cfg_set_pos(pcfg, &pos);
302                                         ++index_count;
303                                         continue;
304                                 }
305                                 else {
306                                         section = cfg_check_section(buffer + 1, psec);
307                                         entry = 0;
308                                         index_count = 0;
309                                         cfg_get_pos(pcfg, &pos);
310                                         continue;
311                                 }
312                         }
313                         /* call parser procedure for each line in the section */
314                         if (section != NULL) {
315                                 error = section->parser(section->indexp[index_count], buffer, section->data);
316                                 if (error != 0) {
317                                         section->error = error;
318                                         /* log only the very first error */
319                                         if (!pcfg->err_section) {
320                                                 pcfg->err_line = pcfg->line;
321                                                 pcfg->err_entry = entry;
322                                                 pcfg->err_section = cfg_get_section_name(section);
323                                         }
324                                         return 0;
325                                 }
326                                 ++entry;
327                         }
328                 }
329                 if (index_count + 1 < psec->nbindex) {
330                         // Last core config contained multiple cores - loop back
331                         cfg_set_pos(pcfg, &pos);
332                         ++index_count;
333                 }
334                 else {
335                         break;
336                 }
337         }
338         return 0;
339 }