4 * Copyright(c) 2010-2014 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.
38 #include <rte_string_fns.h>
39 #include <rte_sched.h>
45 /** when we resize a file structure, how many extra entries
46 * for new sections do we add in */
47 #define CFG_ALLOC_SECTION_BATCH 8
48 /** when we resize a section structure, how many extra entries
49 * for new entries do we add in */
50 #define CFG_ALLOC_ENTRY_BATCH 16
53 _strip(char *str, unsigned len)
59 if (isspace(str[len-1])) {
60 /* strip trailing whitespace */
61 while (newlen > 0 && isspace(str[newlen - 1]))
65 if (isspace(str[0])) {
66 /* strip leading whitespace */
68 while (isspace(str[start]) && start < newlen)
72 for (i = 0; i < newlen; i++)
73 str[i] = str[i+start];
80 cfg_load(const char *filename, int flags)
82 int allocated_sections = CFG_ALLOC_SECTION_BATCH;
83 int allocated_entries = 0;
84 int curr_section = -1;
88 struct cfg_file *cfg = NULL;
90 FILE *f = fopen(filename, "r");
94 cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) * allocated_sections);
98 memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections);
100 while (fgets(buffer, sizeof(buffer), f) != NULL) {
102 size_t len = strnlen(buffer, sizeof(buffer));
104 if (len >=sizeof(buffer) - 1 && buffer[len-1] != '\n'){
105 printf("Error line %d - no \\n found on string. "
106 "Check if line too long\n", lineno);
109 if ((pos = memchr(buffer, ';', sizeof(buffer))) != NULL) {
114 len = _strip(buffer, len);
115 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
118 if (buffer[0] == '[') {
119 /* section heading line */
120 char *end = memchr(buffer, ']', len);
122 printf("Error line %d - no terminating '[' found\n", lineno);
126 _strip(&buffer[1], end - &buffer[1]);
128 /* close off old section and add start new one */
129 if (curr_section >= 0)
130 cfg->sections[curr_section]->num_entries = curr_entry + 1;
133 /* resize overall struct if we don't have room for more sections */
134 if (curr_section == allocated_sections) {
135 allocated_sections += CFG_ALLOC_SECTION_BATCH;
136 struct cfg_file *n_cfg = realloc(cfg, sizeof(*cfg) +
137 sizeof(cfg->sections[0]) * allocated_sections);
139 printf("Error - no more memory\n");
145 /* allocate space for new section */
146 allocated_entries = CFG_ALLOC_ENTRY_BATCH;
148 cfg->sections[curr_section] = malloc(sizeof(*cfg->sections[0]) +
149 sizeof(cfg->sections[0]->entries[0]) * allocated_entries);
150 if (cfg->sections[curr_section] == NULL) {
151 printf("Error - no more memory\n");
155 snprintf(cfg->sections[curr_section]->name,
156 sizeof(cfg->sections[0]->name),
161 if (curr_section < 0) {
162 printf("Error line %d - value outside of section\n", lineno);
166 struct cfg_section *sect = cfg->sections[curr_section];
168 if (rte_strsplit(buffer, sizeof(buffer), split, 2, '=') != 2) {
169 printf("Error at line %d - cannot split string\n", lineno);
174 if (curr_entry == allocated_entries) {
175 allocated_entries += CFG_ALLOC_ENTRY_BATCH;
176 struct cfg_section *n_sect = realloc(sect, sizeof(*sect) +
177 sizeof(sect->entries[0]) * allocated_entries);
178 if (n_sect == NULL) {
179 printf("Error - no more memory\n");
182 sect = cfg->sections[curr_section] = n_sect;
185 sect->entries[curr_entry] = malloc(sizeof(*sect->entries[0]));
186 if (sect->entries[curr_entry] == NULL) {
187 printf("Error - no more memory\n");
191 struct cfg_entry *entry = sect->entries[curr_entry];
192 snprintf(entry->name, sizeof(entry->name), "%s", split[0]);
193 snprintf(entry->value, sizeof(entry->value), "%s", split[1]);
194 _strip(entry->name, strnlen(entry->name, sizeof(entry->name)));
195 _strip(entry->value, strnlen(entry->value, sizeof(entry->value)));
200 cfg->sections[curr_section]->num_entries = curr_entry + 1;
201 cfg->num_sections = curr_section + 1;
212 int cfg_close(struct cfg_file *cfg)
219 for(i = 0; i < cfg->num_sections; i++) {
220 if (cfg->sections[i] != NULL) {
221 if (cfg->sections[i]->num_entries) {
222 for(j = 0; j < cfg->sections[i]->num_entries; j++) {
223 if (cfg->sections[i]->entries[j] != NULL)
224 free(cfg->sections[i]->entries[j]);
227 free(cfg->sections[i]);
236 cfg_num_sections(struct cfg_file *cfg, const char *sectionname, size_t length)
239 int num_sections = 0;
240 for (i = 0; i < cfg->num_sections; i++) {
241 if (strncmp(cfg->sections[i]->name, sectionname, length) == 0)
248 cfg_sections(struct cfg_file *cfg, char *sections[], int max_sections)
251 for (i = 0; i < cfg->num_sections && i < max_sections; i++) {
252 snprintf(sections[i], CFG_NAME_LEN, "%s", cfg->sections[i]->name);
257 static const struct cfg_section *
258 _get_section(struct cfg_file *cfg, const char *sectionname)
261 for (i = 0; i < cfg->num_sections; i++) {
262 if (strncmp(cfg->sections[i]->name, sectionname,
263 sizeof(cfg->sections[0]->name)) == 0)
264 return cfg->sections[i];
270 cfg_has_section(struct cfg_file *cfg, const char *sectionname)
272 return (_get_section(cfg, sectionname) != NULL);
276 cfg_section_num_entries(struct cfg_file *cfg, const char *sectionname)
278 const struct cfg_section *s = _get_section(cfg, sectionname);
281 return s->num_entries;
286 cfg_section_entries(struct cfg_file *cfg, const char *sectionname,
287 struct cfg_entry *entries, int max_entries)
290 const struct cfg_section *sect = _get_section(cfg, sectionname);
293 for (i = 0; i < max_entries && i < sect->num_entries; i++)
294 entries[i] = *sect->entries[i];
299 cfg_get_entry(struct cfg_file *cfg, const char *sectionname,
300 const char *entryname)
303 const struct cfg_section *sect = _get_section(cfg, sectionname);
306 for (i = 0; i < sect->num_entries; i++)
307 if (strncmp(sect->entries[i]->name, entryname, CFG_NAME_LEN) == 0)
308 return sect->entries[i]->value;
313 cfg_has_entry(struct cfg_file *cfg, const char *sectionname,
314 const char *entryname)
316 return (cfg_get_entry(cfg, sectionname, entryname) != NULL);
321 cfg_load_port(struct cfg_file *cfg, struct rte_sched_port_params *port_params)
326 if (!cfg || !port_params)
329 entry = cfg_get_entry(cfg, "port", "frame overhead");
331 port_params->frame_overhead = (uint32_t)atoi(entry);
333 entry = cfg_get_entry(cfg, "port", "number of subports per port");
335 port_params->n_subports_per_port = (uint32_t)atoi(entry);
337 entry = cfg_get_entry(cfg, "port", "number of pipes per subport");
339 port_params->n_pipes_per_subport = (uint32_t)atoi(entry);
341 entry = cfg_get_entry(cfg, "port", "queue sizes");
345 for(j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++) {
346 port_params->qsize[j] = (uint16_t)strtol(entry, &next, 10);
354 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++) {
357 /* Parse WRED min thresholds */
358 snprintf(str, sizeof(str), "tc %d wred min", j);
359 entry = cfg_get_entry(cfg, "red", str);
363 /* for each packet colour (green, yellow, red) */
364 for (k = 0; k < e_RTE_METER_COLORS; k++) {
365 port_params->red_params[j][k].min_th
366 = (uint16_t)strtol(entry, &next, 10);
373 /* Parse WRED max thresholds */
374 snprintf(str, sizeof(str), "tc %d wred max", j);
375 entry = cfg_get_entry(cfg, "red", str);
379 /* for each packet colour (green, yellow, red) */
380 for (k = 0; k < e_RTE_METER_COLORS; k++) {
381 port_params->red_params[j][k].max_th
382 = (uint16_t)strtol(entry, &next, 10);
389 /* Parse WRED inverse mark probabilities */
390 snprintf(str, sizeof(str), "tc %d wred inv prob", j);
391 entry = cfg_get_entry(cfg, "red", str);
395 /* for each packet colour (green, yellow, red) */
396 for (k = 0; k < e_RTE_METER_COLORS; k++) {
397 port_params->red_params[j][k].maxp_inv
398 = (uint8_t)strtol(entry, &next, 10);
406 /* Parse WRED EWMA filter weights */
407 snprintf(str, sizeof(str), "tc %d wred weight", j);
408 entry = cfg_get_entry(cfg, "red", str);
412 /* for each packet colour (green, yellow, red) */
413 for (k = 0; k < e_RTE_METER_COLORS; k++) {
414 port_params->red_params[j][k].wq_log2
415 = (uint8_t)strtol(entry, &next, 10);
422 #endif /* RTE_SCHED_RED */
428 cfg_load_pipe(struct cfg_file *cfg, struct rte_sched_pipe_params *pipe_params)
435 if (!cfg || !pipe_params)
438 profiles = cfg_num_sections(cfg, "pipe profile", sizeof("pipe profile") - 1);
439 port_params.n_pipe_profiles = profiles;
441 for (j = 0; j < profiles; j++) {
443 snprintf(pipe_name, sizeof(pipe_name), "pipe profile %d", j);
445 entry = cfg_get_entry(cfg, pipe_name, "tb rate");
447 pipe_params[j].tb_rate = (uint32_t)atoi(entry);
449 entry = cfg_get_entry(cfg, pipe_name, "tb size");
451 pipe_params[j].tb_size = (uint32_t)atoi(entry);
453 entry = cfg_get_entry(cfg, pipe_name, "tc period");
455 pipe_params[j].tc_period = (uint32_t)atoi(entry);
457 entry = cfg_get_entry(cfg, pipe_name, "tc 0 rate");
459 pipe_params[j].tc_rate[0] = (uint32_t)atoi(entry);
461 entry = cfg_get_entry(cfg, pipe_name, "tc 1 rate");
463 pipe_params[j].tc_rate[1] = (uint32_t)atoi(entry);
465 entry = cfg_get_entry(cfg, pipe_name, "tc 2 rate");
467 pipe_params[j].tc_rate[2] = (uint32_t)atoi(entry);
469 entry = cfg_get_entry(cfg, pipe_name, "tc 3 rate");
471 pipe_params[j].tc_rate[3] = (uint32_t)atoi(entry);
473 #ifdef RTE_SCHED_SUBPORT_TC_OV
474 entry = cfg_get_entry(cfg, pipe_name, "tc 3 oversubscription weight");
476 pipe_params[j].tc_ov_weight = (uint8_t)atoi(entry);
479 entry = cfg_get_entry(cfg, pipe_name, "tc 0 wrr weights");
481 for(i = 0; i < RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; i++) {
482 pipe_params[j].wrr_weights[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE*0 + i] =
483 (uint8_t)strtol(entry, &next, 10);
489 entry = cfg_get_entry(cfg, pipe_name, "tc 1 wrr weights");
491 for(i = 0; i < RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; i++) {
492 pipe_params[j].wrr_weights[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE*1 + i] =
493 (uint8_t)strtol(entry, &next, 10);
499 entry = cfg_get_entry(cfg, pipe_name, "tc 2 wrr weights");
501 for(i = 0; i < RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; i++) {
502 pipe_params[j].wrr_weights[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE*2 + i] =
503 (uint8_t)strtol(entry, &next, 10);
509 entry = cfg_get_entry(cfg, pipe_name, "tc 3 wrr weights");
511 for(i = 0; i < RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; i++) {
512 pipe_params[j].wrr_weights[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE*3 + i] =
513 (uint8_t)strtol(entry, &next, 10);
524 cfg_load_subport(struct cfg_file *cfg, struct rte_sched_subport_params *subport_params)
529 if (!cfg || !subport_params)
532 memset(app_pipe_to_profile, -1, sizeof(app_pipe_to_profile));
534 for (i = 0; i < MAX_SCHED_SUBPORTS; i++) {
535 char sec_name[CFG_NAME_LEN];
536 snprintf(sec_name, sizeof(sec_name), "subport %d", i);
538 if (cfg_has_section(cfg, sec_name)) {
539 entry = cfg_get_entry(cfg, sec_name, "tb rate");
541 subport_params[i].tb_rate = (uint32_t)atoi(entry);
543 entry = cfg_get_entry(cfg, sec_name, "tb size");
545 subport_params[i].tb_size = (uint32_t)atoi(entry);
547 entry = cfg_get_entry(cfg, sec_name, "tc period");
549 subport_params[i].tc_period = (uint32_t)atoi(entry);
551 entry = cfg_get_entry(cfg, sec_name, "tc 0 rate");
553 subport_params[i].tc_rate[0] = (uint32_t)atoi(entry);
555 entry = cfg_get_entry(cfg, sec_name, "tc 1 rate");
557 subport_params[i].tc_rate[1] = (uint32_t)atoi(entry);
559 entry = cfg_get_entry(cfg, sec_name, "tc 2 rate");
561 subport_params[i].tc_rate[2] = (uint32_t)atoi(entry);
563 entry = cfg_get_entry(cfg, sec_name, "tc 3 rate");
565 subport_params[i].tc_rate[3] = (uint32_t)atoi(entry);
567 int n_entries = cfg_section_num_entries(cfg, sec_name);
568 struct cfg_entry entries[n_entries];
570 cfg_section_entries(cfg, sec_name, entries, n_entries);
572 for (j = 0; j < n_entries; j++) {
573 if (strncmp("pipe", entries[j].name, sizeof("pipe") - 1) == 0) {
575 char *tokens[2] = {NULL, NULL};
579 profile = atoi(entries[j].value);
580 n_tokens = rte_strsplit(&entries[j].name[sizeof("pipe")],
581 strnlen(entries[j].name, CFG_NAME_LEN), tokens, 2, '-');
583 begin = atoi(tokens[0]);
585 end = atoi(tokens[1]);
589 if (end >= MAX_SCHED_PIPES || begin > end)
592 for (k = begin; k <= end; k++) {
593 char profile_name[CFG_NAME_LEN];
595 snprintf(profile_name, sizeof(profile_name),
596 "pipe profile %d", profile);
597 if (cfg_has_section(cfg, profile_name))
598 app_pipe_to_profile[i][k] = profile;
600 rte_exit(EXIT_FAILURE, "Wrong pipe profile %s\n",