sched: initial import
[dpdk.git] / examples / qos_sched / cfg_file.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <rte_string_fns.h>
40 #include <rte_sched.h>
41
42 #include "cfg_file.h"
43 #include "main.h"
44
45
46 /** when we resize a file structure, how many extra entries
47  * for new sections do we add in */
48 #define CFG_ALLOC_SECTION_BATCH 8
49 /** when we resize a section structure, how many extra entries
50  * for new entries do we add in */
51 #define CFG_ALLOC_ENTRY_BATCH 16
52
53 static unsigned
54 _strip(char *str, unsigned len)
55 {
56         int newlen = len;
57         if (len == 0)
58                 return 0;
59
60         if (isspace(str[len-1])) {
61                 /* strip trailing whitespace */
62                 while (newlen > 0 && isspace(str[newlen - 1]))
63                         str[--newlen] = '\0';
64         }
65
66         if (isspace(str[0])) {
67                 /* strip leading whitespace */
68                 int i,start = 1;
69                 while (isspace(str[start]) && start < newlen)
70                         start++
71                         ; /* do nothing */
72                 newlen -= start;
73                 for (i = 0; i < newlen; i++)
74                         str[i] = str[i+start];
75                 str[i] = '\0';
76         }
77         return newlen;
78 }
79
80 struct cfg_file *
81 cfg_load(const char *filename, int flags)
82 {
83         int allocated_sections = CFG_ALLOC_SECTION_BATCH;
84         int allocated_entries = 0;
85         int curr_section = -1;
86         int curr_entry = -1;
87         char buffer[256];
88         int lineno = 0;
89         struct cfg_file *cfg = NULL;
90
91         FILE *f = fopen(filename, "r");
92         if (f == NULL)
93                 return NULL;
94
95         cfg = malloc(sizeof(*cfg) +     sizeof(cfg->sections[0]) * allocated_sections);
96         if (cfg == NULL)
97                 goto error2;
98
99         memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections);
100
101         while (fgets(buffer, sizeof(buffer), f) != NULL) {
102                 char *pos = NULL;
103                 size_t len = strnlen(buffer, sizeof(buffer));
104                 lineno++;
105                 if (len >=sizeof(buffer) - 1 && buffer[len-1] != '\n'){
106                         printf("Error line %d - no \\n found on string. "
107                                         "Check if line too long\n", lineno);
108                         goto error1;
109                 }
110                 if ((pos = memchr(buffer, ';', sizeof(buffer))) != NULL) {
111                         *pos = '\0';
112                         len = pos -  buffer;
113                 }
114
115                 len = _strip(buffer, len);
116                 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
117                         continue;
118
119                 if (buffer[0] == '[') {
120                         /* section heading line */
121                         char *end = memchr(buffer, ']', len);
122                         if (end == NULL) {
123                                 printf("Error line %d - no terminating '[' found\n", lineno);
124                                 goto error1;
125                         }
126                         *end = '\0';
127                         _strip(&buffer[1], end - &buffer[1]);
128
129                         /* close off old section and add start new one */
130                         if (curr_section >= 0)
131                                 cfg->sections[curr_section]->num_entries = curr_entry + 1;
132                         curr_section++;
133
134                         /* resize overall struct if we don't have room for more sections */
135                         if (curr_section == allocated_sections) {
136                                 allocated_sections += CFG_ALLOC_SECTION_BATCH;
137                                 struct cfg_file *n_cfg = realloc(cfg, sizeof(*cfg) +
138                                                 sizeof(cfg->sections[0]) * allocated_sections);
139                                 if (n_cfg == NULL) {
140                                         printf("Error - no more memory\n");
141                                         goto error1;
142                                 }
143                                 cfg = n_cfg;
144                         }
145
146                         /* allocate space for new section */
147                         allocated_entries = CFG_ALLOC_ENTRY_BATCH;
148                         curr_entry = -1;
149                         cfg->sections[curr_section] = malloc(sizeof(*cfg->sections[0]) +
150                                         sizeof(cfg->sections[0]->entries[0]) * allocated_entries);
151                         if (cfg->sections[curr_section] == NULL) {
152                                 printf("Error - no more memory\n");
153                                 goto error1;
154                         }
155
156                         rte_snprintf(cfg->sections[curr_section]->name,
157                                         sizeof(cfg->sections[0]->name),
158                                         "%s", &buffer[1]);
159                 }
160                 else {
161                         /* value line */
162                         if (curr_section < 0) {
163                                 printf("Error line %d - value outside of section\n", lineno);
164                                 goto error1;
165                         }
166
167                         struct cfg_section *sect = cfg->sections[curr_section];
168                         char *split[2];
169                         if (rte_strsplit(buffer, sizeof(buffer), split, 2, '=') != 2) {
170                                 printf("Error at line %d - cannot split string\n", lineno);
171                                 goto error1;
172                         }
173
174                         curr_entry++;
175                         if (curr_entry == allocated_entries) {
176                                 allocated_entries += CFG_ALLOC_ENTRY_BATCH;
177                                 struct cfg_section *n_sect = realloc(sect, sizeof(*sect) +
178                                                 sizeof(sect->entries[0]) * allocated_entries);
179                                 if (n_sect == NULL) {
180                                         printf("Error - no more memory\n");
181                                         goto error1;
182                                 }
183                                 sect = cfg->sections[curr_section] = n_sect;
184                         }
185
186                         sect->entries[curr_entry] = malloc(sizeof(*sect->entries[0]));
187                         if (sect->entries[curr_entry] == NULL) {
188                                 printf("Error - no more memory\n");
189                                 goto error1;
190                         }
191
192                         struct cfg_entry *entry = sect->entries[curr_entry];
193                         rte_snprintf(entry->name, sizeof(entry->name), "%s", split[0]);
194                         rte_snprintf(entry->value, sizeof(entry->value), "%s", split[1]);
195                         _strip(entry->name, strnlen(entry->name, sizeof(entry->name)));
196                         _strip(entry->value, strnlen(entry->value, sizeof(entry->value)));
197                 }
198         }
199         fclose(f);
200         cfg->flags = flags;
201         cfg->sections[curr_section]->num_entries = curr_entry + 1;
202         cfg->num_sections = curr_section + 1;
203         return cfg;
204
205 error1:
206         cfg_close(cfg);
207 error2:
208         fclose(f);
209         return NULL;
210 }
211
212
213 int cfg_close(struct cfg_file *cfg)
214 {
215         int i, j;
216
217         if (cfg == NULL)
218                 return -1;
219
220         for(i = 0; i < cfg->num_sections; i++) {
221                 if (cfg->sections[i] != NULL) {
222                         if (cfg->sections[i]->num_entries) {
223                                 for(j = 0; j < cfg->sections[i]->num_entries; j++) {
224                                         if (cfg->sections[i]->entries[j] != NULL)
225                                                 free(cfg->sections[i]->entries[j]);
226                                 }
227                         }
228                         free(cfg->sections[i]);
229                 }
230         }
231         free(cfg);
232
233         return 0;
234 }
235
236 int
237 cfg_num_sections(struct cfg_file *cfg, const char *sectionname, size_t length)
238 {
239         int i;
240         int num_sections = 0;
241         for (i = 0; i < cfg->num_sections; i++) {
242                 if (strncmp(cfg->sections[i]->name, sectionname, length) == 0)
243                         num_sections++;
244         }
245         return num_sections;
246 }
247
248 int
249 cfg_sections(struct cfg_file *cfg, char *sections[], int max_sections)
250 {
251         int i;
252         for (i = 0; i < cfg->num_sections && i < max_sections; i++) {
253                 rte_snprintf(sections[i], CFG_NAME_LEN, "%s",  cfg->sections[i]->name);
254         }
255         return i;
256 }
257
258 static const struct cfg_section *
259 _get_section(struct cfg_file *cfg, const char *sectionname)
260 {
261         int i;
262         for (i = 0; i < cfg->num_sections; i++) {
263                 if (strncmp(cfg->sections[i]->name, sectionname,
264                                 sizeof(cfg->sections[0]->name)) == 0)
265                         return cfg->sections[i];
266         }
267         return NULL;
268 }
269
270 int
271 cfg_has_section(struct cfg_file *cfg, const char *sectionname)
272 {
273         return (_get_section(cfg, sectionname) != NULL);
274 }
275
276 int
277 cfg_section_num_entries(struct cfg_file *cfg, const char *sectionname)
278 {
279         const struct cfg_section *s = _get_section(cfg, sectionname);
280         if (s == NULL)
281                 return -1;
282         return s->num_entries;
283 }
284
285
286 int
287 cfg_section_entries(struct cfg_file *cfg, const char *sectionname,
288                 struct cfg_entry *entries, int max_entries)
289 {
290         int i;
291         const struct cfg_section *sect = _get_section(cfg, sectionname);
292         if (sect == NULL)
293                 return -1;
294         for (i = 0; i < max_entries && i < sect->num_entries; i++)
295                 entries[i] = *sect->entries[i];
296         return i;
297 }
298
299 const char *
300 cfg_get_entry(struct cfg_file *cfg, const char *sectionname,
301                 const char *entryname)
302 {
303         int i;
304         const struct cfg_section *sect = _get_section(cfg, sectionname);
305         if (sect == NULL)
306                 return NULL;
307         for (i = 0; i < sect->num_entries; i++)
308                 if (strncmp(sect->entries[i]->name, entryname, CFG_NAME_LEN) == 0)
309                         return sect->entries[i]->value;
310         return NULL;
311 }
312
313 int
314 cfg_has_entry(struct cfg_file *cfg, const char *sectionname,
315                 const char *entryname)
316 {
317         return (cfg_get_entry(cfg, sectionname, entryname) != NULL);
318 }
319
320
321 int
322 cfg_load_port(struct cfg_file *cfg, struct rte_sched_port_params *port_params)
323 {
324         const char *entry;
325         int j;
326
327         if (!cfg || !port_params)
328                 return -1;
329
330         entry = cfg_get_entry(cfg, "port", "frame overhead");
331         if (entry)
332                 port_params->frame_overhead = (uint32_t)atoi(entry);
333
334         entry = cfg_get_entry(cfg, "port", "number of subports per port");
335         if (entry)
336                 port_params->n_subports_per_port = (uint32_t)atoi(entry);
337         
338         entry = cfg_get_entry(cfg, "port", "number of pipes per subport");
339         if (entry)
340                 port_params->n_pipes_per_subport = (uint32_t)atoi(entry);
341
342         entry = cfg_get_entry(cfg, "port", "queue sizes");
343         if (entry) {
344                 char *next;
345                 
346                 for(j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++) {
347                         port_params->qsize[j] = (uint16_t)strtol(entry, &next, 10);
348                         if (next == NULL)
349                                 break;
350                         entry = next;
351                 }
352         }
353
354 #ifdef RTE_SCHED_RED
355         for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++) {
356                 char str[32];
357
358                 /* Parse WRED min thresholds */
359                 rte_snprintf(str, sizeof(str), "tc %d wred min", j);
360                 entry = cfg_get_entry(cfg, "red", str);
361                 if (entry) {
362                         char *next;
363                         int k;
364                         /* for each packet colour (green, yellow, red) */
365                         for (k = 0; k < e_RTE_METER_COLORS; k++) {
366                                 port_params->red_params[j][k].min_th
367                                         = (uint16_t)strtol(entry, &next, 10);
368                                 if (next == NULL)
369                                         break;
370                                 entry = next;
371                         }
372                 }
373
374                 /* Parse WRED max thresholds */
375                 rte_snprintf(str, sizeof(str), "tc %d wred max", j);
376                 entry = cfg_get_entry(cfg, "red", str);
377                 if (entry) {
378                         char *next;
379                         int k;
380                         /* for each packet colour (green, yellow, red) */
381                         for (k = 0; k < e_RTE_METER_COLORS; k++) {
382                                 port_params->red_params[j][k].max_th
383                                         = (uint16_t)strtol(entry, &next, 10);
384                                 if (next == NULL)
385                                         break;
386                                 entry = next;
387                         }
388                 }
389
390                 /* Parse WRED inverse mark probabilities */
391                 rte_snprintf(str, sizeof(str), "tc %d wred inv prob", j);
392                 entry = cfg_get_entry(cfg, "red", str);
393                 if (entry) {
394                         char *next;
395                         int k;
396                         /* for each packet colour (green, yellow, red) */
397                         for (k = 0; k < e_RTE_METER_COLORS; k++) {
398                                 port_params->red_params[j][k].maxp_inv
399                                         = (uint8_t)strtol(entry, &next, 10);
400
401                                 if (next == NULL)
402                                         break;
403                                 entry = next;
404                         }
405                 }
406
407                 /* Parse WRED EWMA filter weights */
408                 rte_snprintf(str, sizeof(str), "tc %d wred weight", j);
409                 entry = cfg_get_entry(cfg, "red", str);
410                 if (entry) {
411                         char *next;
412                         int k;
413                         /* for each packet colour (green, yellow, red) */
414                         for (k = 0; k < e_RTE_METER_COLORS; k++) {
415                                 port_params->red_params[j][k].wq_log2
416                                         = (uint8_t)strtol(entry, &next, 10);
417                                 if (next == NULL)
418                                         break;
419                                 entry = next;
420                         }
421                 }
422         }
423 #endif /* RTE_SCHED_RED */
424         
425         return 0;
426 }
427
428 int
429 cfg_load_pipe(struct cfg_file *cfg, struct rte_sched_pipe_params *pipe_params)
430 {
431         int i, j;
432         char *next;
433         const char *entry;
434         int profiles;
435
436         if (!cfg || !pipe_params)
437                 return -1;
438
439         profiles = cfg_num_sections(cfg, "pipe profile", sizeof("pipe profile") - 1);
440         port_params.n_pipe_profiles = profiles;
441
442         for (j = 0; j < profiles; j++) {
443                 char pipe_name[32];
444                 rte_snprintf(pipe_name, sizeof(pipe_name), "pipe profile %d", j);
445
446                 entry = cfg_get_entry(cfg, pipe_name, "tb rate");
447                 if (entry)
448                         pipe_params[j].tb_rate = (uint32_t)atoi(entry);
449
450                 entry = cfg_get_entry(cfg, pipe_name, "tb size");
451                 if (entry)
452                         pipe_params[j].tb_size = (uint32_t)atoi(entry);
453
454                 entry = cfg_get_entry(cfg, pipe_name, "tc period");
455                 if (entry)
456                         pipe_params[j].tc_period = (uint32_t)atoi(entry);
457
458                 entry = cfg_get_entry(cfg, pipe_name, "tc 0 rate");
459                 if (entry)
460                         pipe_params[j].tc_rate[0] = (uint32_t)atoi(entry);
461                         
462                 entry = cfg_get_entry(cfg, pipe_name, "tc 1 rate");
463                 if (entry)
464                         pipe_params[j].tc_rate[1] = (uint32_t)atoi(entry);
465                         
466                 entry = cfg_get_entry(cfg, pipe_name, "tc 2 rate");
467                 if (entry)
468                         pipe_params[j].tc_rate[2] = (uint32_t)atoi(entry);
469                         
470                 entry = cfg_get_entry(cfg, pipe_name, "tc 3 rate");
471                 if (entry)
472                         pipe_params[j].tc_rate[3] = (uint32_t)atoi(entry);
473
474 #ifdef RTE_SCHED_SUBPORT_TC_OV
475                 entry = cfg_get_entry(cfg, pipe_name, "tc 0 oversubscription weight");
476                 if (entry)
477                         pipe_params[j].tc_ov_weight[0] = (uint8_t)atoi(entry);
478                         
479                 entry = cfg_get_entry(cfg, pipe_name, "tc 1 oversubscription weight");
480                 if (entry)
481                         pipe_params[j].tc_ov_weight[1] = (uint8_t)atoi(entry);
482                         
483                 entry = cfg_get_entry(cfg, pipe_name, "tc 2 oversubscription weight");
484                 if (entry)
485                         pipe_params[j].tc_ov_weight[2] = (uint8_t)atoi(entry);
486                         
487                 entry = cfg_get_entry(cfg, pipe_name, "tc 3 oversubscription weight");
488                 if (entry)
489                         pipe_params[j].tc_ov_weight[3] = (uint8_t)atoi(entry);
490 #endif
491
492                 entry = cfg_get_entry(cfg, pipe_name, "tc 0 wrr weights");
493                 if (entry) {
494                         for(i = 0; i < RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; i++) {
495                                 pipe_params[j].wrr_weights[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE*0 + i] =
496                                         (uint8_t)strtol(entry, &next, 10);
497                                 if (next == NULL)
498                                         break;
499                                 entry = next;
500                         }
501                 }
502                 entry = cfg_get_entry(cfg, pipe_name, "tc 1 wrr weights");
503                 if (entry) {
504                         for(i = 0; i < RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; i++) {
505                                 pipe_params[j].wrr_weights[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE*1 + i] =
506                                         (uint8_t)strtol(entry, &next, 10);
507                                 if (next == NULL)
508                                         break;
509                                 entry = next;
510                         }
511                 }
512                 entry = cfg_get_entry(cfg, pipe_name, "tc 2 wrr weights");
513                 if (entry) {
514                         for(i = 0; i < RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; i++) {
515                                 pipe_params[j].wrr_weights[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE*2 + i] =
516                                         (uint8_t)strtol(entry, &next, 10);
517                                 if (next == NULL)
518                                         break;
519                                 entry = next;
520                         }
521                 }
522                 entry = cfg_get_entry(cfg, pipe_name, "tc 3 wrr weights");
523                 if (entry) {
524                         for(i = 0; i < RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; i++) {
525                                 pipe_params[j].wrr_weights[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE*3 + i] =
526                                         (uint8_t)strtol(entry, &next, 10);
527                                 if (next == NULL)
528                                         break;
529                                 entry = next;
530                         }
531                 }
532         }
533         return 0;
534 }
535
536 int
537 cfg_load_subport(struct cfg_file *cfg, struct rte_sched_subport_params *subport_params)
538 {
539         const char *entry;
540         int i, j, k;
541
542         if (!cfg || !subport_params)
543                 return -1;
544
545         memset(app_pipe_to_profile, -1, sizeof(app_pipe_to_profile));
546
547         for (i = 0; i < MAX_SCHED_SUBPORTS; i++) {
548                 char sec_name[CFG_NAME_LEN];
549                 rte_snprintf(sec_name, sizeof(sec_name), "subport %d", i);
550
551                 if (cfg_has_section(cfg, sec_name)) {
552                         entry = cfg_get_entry(cfg, sec_name, "tb rate");
553                         if (entry)
554                                 subport_params[i].tb_rate = (uint32_t)atoi(entry);
555
556                         entry = cfg_get_entry(cfg, sec_name, "tb size");
557                         if (entry)
558                                 subport_params[i].tb_size = (uint32_t)atoi(entry);
559
560                         entry = cfg_get_entry(cfg, sec_name, "tc period");
561                         if (entry)
562                                 subport_params[i].tc_period = (uint32_t)atoi(entry);
563
564 #ifdef RTE_SCHED_SUBPORT_TC_OV
565                         entry = cfg_get_entry(cfg, sec_name, "tc oversubscription period");
566                         if (entry)
567                                 subport_params[i].tc_ov_period = (uint32_t)atoi(entry);
568 #endif
569
570                         entry = cfg_get_entry(cfg, sec_name, "tc 0 rate");
571                         if (entry)
572                                 subport_params[i].tc_rate[0] = (uint32_t)atoi(entry);
573
574                         entry = cfg_get_entry(cfg, sec_name, "tc 1 rate");
575                         if (entry)
576                                 subport_params[i].tc_rate[1] = (uint32_t)atoi(entry);
577
578                         entry = cfg_get_entry(cfg, sec_name, "tc 2 rate");
579                         if (entry)
580                                 subport_params[i].tc_rate[2] = (uint32_t)atoi(entry);
581
582                         entry = cfg_get_entry(cfg, sec_name, "tc 3 rate");
583                         if (entry)
584                                 subport_params[i].tc_rate[3] = (uint32_t)atoi(entry);
585
586                         int n_entries = cfg_section_num_entries(cfg, sec_name);
587                         struct cfg_entry entries[n_entries];
588
589                         cfg_section_entries(cfg, sec_name, entries, n_entries);
590
591                         for (j = 0; j < n_entries; j++) {
592                                 if (strncmp("pipe", entries[j].name, sizeof("pipe") - 1) == 0) {
593                                         int profile;
594                                         char *tokens[2] = {NULL, NULL};
595                                         int n_tokens;
596                                         int begin, end;
597
598                                         profile = atoi(entries[j].value);
599                                         n_tokens = rte_strsplit(&entries[j].name[sizeof("pipe")],
600                                                         strnlen(entries[j].name, CFG_NAME_LEN), tokens, 2, '-');
601
602                                         begin =  atoi(tokens[0]);
603                                         if (n_tokens == 2)
604                                                 end = atoi(tokens[1]);
605                                         else
606                                                 end = begin;
607
608                                         if (end >= MAX_SCHED_PIPES || begin > end)
609                                                 return -1;
610
611                                         for (k = begin; k <= end; k++) {
612                                                 char profile_name[CFG_NAME_LEN];
613
614                                                 rte_snprintf(profile_name, sizeof(profile_name),
615                                                                 "pipe profile %d", profile);
616                                                 if (cfg_has_section(cfg, profile_name))
617                                                         app_pipe_to_profile[i][k] = profile;
618                                                 else
619                                                         rte_exit(EXIT_FAILURE, "Wrong pipe profile %s\n",
620                                                                         entries[j].value);
621
622                                         }
623                                 }
624                         }
625                 }
626         }
627
628         return 0;
629 }
630
631