4 * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
39 #include <rte_string_fns.h>
40 #include <rte_sched.h>
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
54 _strip(char *str, unsigned len)
60 if (isspace(str[len-1])) {
61 /* strip trailing whitespace */
62 while (newlen > 0 && isspace(str[newlen - 1]))
66 if (isspace(str[0])) {
67 /* strip leading whitespace */
69 while (isspace(str[start]) && start < newlen)
73 for (i = 0; i < newlen; i++)
74 str[i] = str[i+start];
81 cfg_load(const char *filename, int flags)
83 int allocated_sections = CFG_ALLOC_SECTION_BATCH;
84 int allocated_entries = 0;
85 int curr_section = -1;
89 struct cfg_file *cfg = NULL;
91 FILE *f = fopen(filename, "r");
95 cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) * allocated_sections);
99 memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections);
101 while (fgets(buffer, sizeof(buffer), f) != NULL) {
103 size_t len = strnlen(buffer, sizeof(buffer));
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);
110 if ((pos = memchr(buffer, ';', sizeof(buffer))) != NULL) {
115 len = _strip(buffer, len);
116 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
119 if (buffer[0] == '[') {
120 /* section heading line */
121 char *end = memchr(buffer, ']', len);
123 printf("Error line %d - no terminating '[' found\n", lineno);
127 _strip(&buffer[1], end - &buffer[1]);
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;
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);
140 printf("Error - no more memory\n");
146 /* allocate space for new section */
147 allocated_entries = CFG_ALLOC_ENTRY_BATCH;
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");
156 rte_snprintf(cfg->sections[curr_section]->name,
157 sizeof(cfg->sections[0]->name),
162 if (curr_section < 0) {
163 printf("Error line %d - value outside of section\n", lineno);
167 struct cfg_section *sect = cfg->sections[curr_section];
169 if (rte_strsplit(buffer, sizeof(buffer), split, 2, '=') != 2) {
170 printf("Error at line %d - cannot split string\n", lineno);
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");
183 sect = cfg->sections[curr_section] = n_sect;
186 sect->entries[curr_entry] = malloc(sizeof(*sect->entries[0]));
187 if (sect->entries[curr_entry] == NULL) {
188 printf("Error - no more memory\n");
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)));
201 cfg->sections[curr_section]->num_entries = curr_entry + 1;
202 cfg->num_sections = curr_section + 1;
213 int cfg_close(struct cfg_file *cfg)
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]);
228 free(cfg->sections[i]);
237 cfg_num_sections(struct cfg_file *cfg, const char *sectionname, size_t length)
240 int num_sections = 0;
241 for (i = 0; i < cfg->num_sections; i++) {
242 if (strncmp(cfg->sections[i]->name, sectionname, length) == 0)
249 cfg_sections(struct cfg_file *cfg, char *sections[], int max_sections)
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);
258 static const struct cfg_section *
259 _get_section(struct cfg_file *cfg, const char *sectionname)
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];
271 cfg_has_section(struct cfg_file *cfg, const char *sectionname)
273 return (_get_section(cfg, sectionname) != NULL);
277 cfg_section_num_entries(struct cfg_file *cfg, const char *sectionname)
279 const struct cfg_section *s = _get_section(cfg, sectionname);
282 return s->num_entries;
287 cfg_section_entries(struct cfg_file *cfg, const char *sectionname,
288 struct cfg_entry *entries, int max_entries)
291 const struct cfg_section *sect = _get_section(cfg, sectionname);
294 for (i = 0; i < max_entries && i < sect->num_entries; i++)
295 entries[i] = *sect->entries[i];
300 cfg_get_entry(struct cfg_file *cfg, const char *sectionname,
301 const char *entryname)
304 const struct cfg_section *sect = _get_section(cfg, sectionname);
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;
314 cfg_has_entry(struct cfg_file *cfg, const char *sectionname,
315 const char *entryname)
317 return (cfg_get_entry(cfg, sectionname, entryname) != NULL);
322 cfg_load_port(struct cfg_file *cfg, struct rte_sched_port_params *port_params)
327 if (!cfg || !port_params)
330 entry = cfg_get_entry(cfg, "port", "frame overhead");
332 port_params->frame_overhead = (uint32_t)atoi(entry);
334 entry = cfg_get_entry(cfg, "port", "number of subports per port");
336 port_params->n_subports_per_port = (uint32_t)atoi(entry);
338 entry = cfg_get_entry(cfg, "port", "number of pipes per subport");
340 port_params->n_pipes_per_subport = (uint32_t)atoi(entry);
342 entry = cfg_get_entry(cfg, "port", "queue sizes");
346 for(j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++) {
347 port_params->qsize[j] = (uint16_t)strtol(entry, &next, 10);
355 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++) {
358 /* Parse WRED min thresholds */
359 rte_snprintf(str, sizeof(str), "tc %d wred min", j);
360 entry = cfg_get_entry(cfg, "red", str);
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);
374 /* Parse WRED max thresholds */
375 rte_snprintf(str, sizeof(str), "tc %d wred max", j);
376 entry = cfg_get_entry(cfg, "red", str);
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);
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);
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);
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);
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);
423 #endif /* RTE_SCHED_RED */
429 cfg_load_pipe(struct cfg_file *cfg, struct rte_sched_pipe_params *pipe_params)
436 if (!cfg || !pipe_params)
439 profiles = cfg_num_sections(cfg, "pipe profile", sizeof("pipe profile") - 1);
440 port_params.n_pipe_profiles = profiles;
442 for (j = 0; j < profiles; j++) {
444 rte_snprintf(pipe_name, sizeof(pipe_name), "pipe profile %d", j);
446 entry = cfg_get_entry(cfg, pipe_name, "tb rate");
448 pipe_params[j].tb_rate = (uint32_t)atoi(entry);
450 entry = cfg_get_entry(cfg, pipe_name, "tb size");
452 pipe_params[j].tb_size = (uint32_t)atoi(entry);
454 entry = cfg_get_entry(cfg, pipe_name, "tc period");
456 pipe_params[j].tc_period = (uint32_t)atoi(entry);
458 entry = cfg_get_entry(cfg, pipe_name, "tc 0 rate");
460 pipe_params[j].tc_rate[0] = (uint32_t)atoi(entry);
462 entry = cfg_get_entry(cfg, pipe_name, "tc 1 rate");
464 pipe_params[j].tc_rate[1] = (uint32_t)atoi(entry);
466 entry = cfg_get_entry(cfg, pipe_name, "tc 2 rate");
468 pipe_params[j].tc_rate[2] = (uint32_t)atoi(entry);
470 entry = cfg_get_entry(cfg, pipe_name, "tc 3 rate");
472 pipe_params[j].tc_rate[3] = (uint32_t)atoi(entry);
474 #ifdef RTE_SCHED_SUBPORT_TC_OV
475 entry = cfg_get_entry(cfg, pipe_name, "tc 0 oversubscription weight");
477 pipe_params[j].tc_ov_weight[0] = (uint8_t)atoi(entry);
479 entry = cfg_get_entry(cfg, pipe_name, "tc 1 oversubscription weight");
481 pipe_params[j].tc_ov_weight[1] = (uint8_t)atoi(entry);
483 entry = cfg_get_entry(cfg, pipe_name, "tc 2 oversubscription weight");
485 pipe_params[j].tc_ov_weight[2] = (uint8_t)atoi(entry);
487 entry = cfg_get_entry(cfg, pipe_name, "tc 3 oversubscription weight");
489 pipe_params[j].tc_ov_weight[3] = (uint8_t)atoi(entry);
492 entry = cfg_get_entry(cfg, pipe_name, "tc 0 wrr weights");
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);
502 entry = cfg_get_entry(cfg, pipe_name, "tc 1 wrr weights");
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);
512 entry = cfg_get_entry(cfg, pipe_name, "tc 2 wrr weights");
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);
522 entry = cfg_get_entry(cfg, pipe_name, "tc 3 wrr weights");
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);
537 cfg_load_subport(struct cfg_file *cfg, struct rte_sched_subport_params *subport_params)
542 if (!cfg || !subport_params)
545 memset(app_pipe_to_profile, -1, sizeof(app_pipe_to_profile));
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);
551 if (cfg_has_section(cfg, sec_name)) {
552 entry = cfg_get_entry(cfg, sec_name, "tb rate");
554 subport_params[i].tb_rate = (uint32_t)atoi(entry);
556 entry = cfg_get_entry(cfg, sec_name, "tb size");
558 subport_params[i].tb_size = (uint32_t)atoi(entry);
560 entry = cfg_get_entry(cfg, sec_name, "tc period");
562 subport_params[i].tc_period = (uint32_t)atoi(entry);
564 #ifdef RTE_SCHED_SUBPORT_TC_OV
565 entry = cfg_get_entry(cfg, sec_name, "tc oversubscription period");
567 subport_params[i].tc_ov_period = (uint32_t)atoi(entry);
570 entry = cfg_get_entry(cfg, sec_name, "tc 0 rate");
572 subport_params[i].tc_rate[0] = (uint32_t)atoi(entry);
574 entry = cfg_get_entry(cfg, sec_name, "tc 1 rate");
576 subport_params[i].tc_rate[1] = (uint32_t)atoi(entry);
578 entry = cfg_get_entry(cfg, sec_name, "tc 2 rate");
580 subport_params[i].tc_rate[2] = (uint32_t)atoi(entry);
582 entry = cfg_get_entry(cfg, sec_name, "tc 3 rate");
584 subport_params[i].tc_rate[3] = (uint32_t)atoi(entry);
586 int n_entries = cfg_section_num_entries(cfg, sec_name);
587 struct cfg_entry entries[n_entries];
589 cfg_section_entries(cfg, sec_name, entries, n_entries);
591 for (j = 0; j < n_entries; j++) {
592 if (strncmp("pipe", entries[j].name, sizeof("pipe") - 1) == 0) {
594 char *tokens[2] = {NULL, NULL};
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, '-');
602 begin = atoi(tokens[0]);
604 end = atoi(tokens[1]);
608 if (end >= MAX_SCHED_PIPES || begin > end)
611 for (k = begin; k <= end; k++) {
612 char profile_name[CFG_NAME_LEN];
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;
619 rte_exit(EXIT_FAILURE, "Wrong pipe profile %s\n",