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_load_port(struct rte_cfgfile *cfg, struct rte_sched_port_params *port_params)
241 if (!cfg || !port_params)
244 entry = rte_cfgfile_get_entry(cfg, "port", "frame overhead");
246 port_params->frame_overhead = (uint32_t)atoi(entry);
248 entry = rte_cfgfile_get_entry(cfg, "port", "number of subports per port");
250 port_params->n_subports_per_port = (uint32_t)atoi(entry);
252 entry = rte_cfgfile_get_entry(cfg, "port", "number of pipes per subport");
254 port_params->n_pipes_per_subport = (uint32_t)atoi(entry);
256 entry = rte_cfgfile_get_entry(cfg, "port", "queue sizes");
260 for(j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++) {
261 port_params->qsize[j] = (uint16_t)strtol(entry, &next, 10);
269 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++) {
272 /* Parse WRED min thresholds */
273 snprintf(str, sizeof(str), "tc %d wred min", j);
274 entry = rte_cfgfile_get_entry(cfg, "red", str);
278 /* for each packet colour (green, yellow, red) */
279 for (k = 0; k < e_RTE_METER_COLORS; k++) {
280 port_params->red_params[j][k].min_th
281 = (uint16_t)strtol(entry, &next, 10);
288 /* Parse WRED max thresholds */
289 snprintf(str, sizeof(str), "tc %d wred max", j);
290 entry = rte_cfgfile_get_entry(cfg, "red", str);
294 /* for each packet colour (green, yellow, red) */
295 for (k = 0; k < e_RTE_METER_COLORS; k++) {
296 port_params->red_params[j][k].max_th
297 = (uint16_t)strtol(entry, &next, 10);
304 /* Parse WRED inverse mark probabilities */
305 snprintf(str, sizeof(str), "tc %d wred inv prob", j);
306 entry = rte_cfgfile_get_entry(cfg, "red", str);
310 /* for each packet colour (green, yellow, red) */
311 for (k = 0; k < e_RTE_METER_COLORS; k++) {
312 port_params->red_params[j][k].maxp_inv
313 = (uint8_t)strtol(entry, &next, 10);
321 /* Parse WRED EWMA filter weights */
322 snprintf(str, sizeof(str), "tc %d wred weight", j);
323 entry = rte_cfgfile_get_entry(cfg, "red", str);
327 /* for each packet colour (green, yellow, red) */
328 for (k = 0; k < e_RTE_METER_COLORS; k++) {
329 port_params->red_params[j][k].wq_log2
330 = (uint8_t)strtol(entry, &next, 10);
337 #endif /* RTE_SCHED_RED */
343 cfg_load_pipe(struct rte_cfgfile *cfg, struct rte_sched_pipe_params *pipe_params)
350 if (!cfg || !pipe_params)
353 profiles = rte_cfgfile_num_sections(cfg, "pipe profile", sizeof("pipe profile") - 1);
354 port_params.n_pipe_profiles = profiles;
356 for (j = 0; j < profiles; j++) {
358 snprintf(pipe_name, sizeof(pipe_name), "pipe profile %d", j);
360 entry = rte_cfgfile_get_entry(cfg, pipe_name, "tb rate");
362 pipe_params[j].tb_rate = (uint32_t)atoi(entry);
364 entry = rte_cfgfile_get_entry(cfg, pipe_name, "tb size");
366 pipe_params[j].tb_size = (uint32_t)atoi(entry);
368 entry = rte_cfgfile_get_entry(cfg, pipe_name, "tc period");
370 pipe_params[j].tc_period = (uint32_t)atoi(entry);
372 entry = rte_cfgfile_get_entry(cfg, pipe_name, "tc 0 rate");
374 pipe_params[j].tc_rate[0] = (uint32_t)atoi(entry);
376 entry = rte_cfgfile_get_entry(cfg, pipe_name, "tc 1 rate");
378 pipe_params[j].tc_rate[1] = (uint32_t)atoi(entry);
380 entry = rte_cfgfile_get_entry(cfg, pipe_name, "tc 2 rate");
382 pipe_params[j].tc_rate[2] = (uint32_t)atoi(entry);
384 entry = rte_cfgfile_get_entry(cfg, pipe_name, "tc 3 rate");
386 pipe_params[j].tc_rate[3] = (uint32_t)atoi(entry);
388 #ifdef RTE_SCHED_SUBPORT_TC_OV
389 entry = rte_cfgfile_get_entry(cfg, pipe_name, "tc 3 oversubscription weight");
391 pipe_params[j].tc_ov_weight = (uint8_t)atoi(entry);
394 entry = rte_cfgfile_get_entry(cfg, pipe_name, "tc 0 wrr weights");
396 for(i = 0; i < RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; i++) {
397 pipe_params[j].wrr_weights[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE*0 + i] =
398 (uint8_t)strtol(entry, &next, 10);
404 entry = rte_cfgfile_get_entry(cfg, pipe_name, "tc 1 wrr weights");
406 for(i = 0; i < RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; i++) {
407 pipe_params[j].wrr_weights[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE*1 + i] =
408 (uint8_t)strtol(entry, &next, 10);
414 entry = rte_cfgfile_get_entry(cfg, pipe_name, "tc 2 wrr weights");
416 for(i = 0; i < RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; i++) {
417 pipe_params[j].wrr_weights[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE*2 + i] =
418 (uint8_t)strtol(entry, &next, 10);
424 entry = rte_cfgfile_get_entry(cfg, pipe_name, "tc 3 wrr weights");
426 for(i = 0; i < RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS; i++) {
427 pipe_params[j].wrr_weights[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE*3 + i] =
428 (uint8_t)strtol(entry, &next, 10);
439 cfg_load_subport(struct rte_cfgfile *cfg, struct rte_sched_subport_params *subport_params)
444 if (!cfg || !subport_params)
447 memset(app_pipe_to_profile, -1, sizeof(app_pipe_to_profile));
449 for (i = 0; i < MAX_SCHED_SUBPORTS; i++) {
450 char sec_name[CFG_NAME_LEN];
451 snprintf(sec_name, sizeof(sec_name), "subport %d", i);
453 if (rte_cfgfile_has_section(cfg, sec_name)) {
454 entry = rte_cfgfile_get_entry(cfg, sec_name, "tb rate");
456 subport_params[i].tb_rate = (uint32_t)atoi(entry);
458 entry = rte_cfgfile_get_entry(cfg, sec_name, "tb size");
460 subport_params[i].tb_size = (uint32_t)atoi(entry);
462 entry = rte_cfgfile_get_entry(cfg, sec_name, "tc period");
464 subport_params[i].tc_period = (uint32_t)atoi(entry);
466 entry = rte_cfgfile_get_entry(cfg, sec_name, "tc 0 rate");
468 subport_params[i].tc_rate[0] = (uint32_t)atoi(entry);
470 entry = rte_cfgfile_get_entry(cfg, sec_name, "tc 1 rate");
472 subport_params[i].tc_rate[1] = (uint32_t)atoi(entry);
474 entry = rte_cfgfile_get_entry(cfg, sec_name, "tc 2 rate");
476 subport_params[i].tc_rate[2] = (uint32_t)atoi(entry);
478 entry = rte_cfgfile_get_entry(cfg, sec_name, "tc 3 rate");
480 subport_params[i].tc_rate[3] = (uint32_t)atoi(entry);
482 int n_entries = rte_cfgfile_section_num_entries(cfg, sec_name);
483 struct rte_cfgfile_entry entries[n_entries];
485 rte_cfgfile_section_entries(cfg, sec_name, entries, n_entries);
487 for (j = 0; j < n_entries; j++) {
488 if (strncmp("pipe", entries[j].name, sizeof("pipe") - 1) == 0) {
490 char *tokens[2] = {NULL, NULL};
494 profile = atoi(entries[j].value);
495 n_tokens = rte_strsplit(&entries[j].name[sizeof("pipe")],
496 strnlen(entries[j].name, CFG_NAME_LEN), tokens, 2, '-');
498 begin = atoi(tokens[0]);
500 end = atoi(tokens[1]);
504 if (end >= MAX_SCHED_PIPES || begin > end)
507 for (k = begin; k <= end; k++) {
508 char profile_name[CFG_NAME_LEN];
510 snprintf(profile_name, sizeof(profile_name),
511 "pipe profile %d", profile);
512 if (rte_cfgfile_has_section(cfg, profile_name))
513 app_pipe_to_profile[i][k] = profile;
515 rte_exit(EXIT_FAILURE, "Wrong pipe profile %s\n",