2aa684b6d0cbf4d1d8855b57e18ad0267945aebe
[o-du/phy.git] / fapi_5g / source / utils / nr5g_fapi_config_loader.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2019 Intel.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18 #include "nr5g_fapi_config_loader.h"
19 #include "nr5g_fapi_dpdk.h"
20 #include "sched.h"
21
22 #define NR5G_FAPI_MAX_SHMSIZE (2126512128)
23 char *fgets_s(
24     char *string,
25     size_t len,
26     FILE * fp);
27
28 p_nr5g_fapi_cfg_t nr5g_fapi_config_loader(
29     char *prgname,
30     const char *cfg_fname)
31 {
32     struct rte_cfgfile *cfg_file;
33     p_nr5g_fapi_cfg_t cfg;
34     const char *entry;
35     size_t dev_name_len;
36     unsigned int num_cpus = 0;
37     char check_core_count[255], *max_core;
38     FILE *fp = NULL;
39
40     if (cfg_fname == NULL) {
41         printf("Error: Configuration filename was not passed.");
42         return NULL;
43     }
44
45     cfg_file = rte_cfgfile_load(cfg_fname, 0);
46     if (cfg_file == NULL) {
47         printf("Error: Cofiguration file loading failed.");
48         return NULL;
49     }
50
51     cfg = (p_nr5g_fapi_cfg_t) calloc(1, sizeof(nr5g_fapi_cfg_t));
52     if (!cfg) {
53         printf("Error: not enough memory in the system");
54         return NULL;
55     }
56     cfg->prgname = prgname;
57     fp = popen("cat /proc/cpuinfo | grep processor | wc -l", "r");
58     if (NULL == fp) {
59         printf("Error: In getting maximum number of lcores in the system.");
60         free(cfg);
61         return NULL;
62     }
63     max_core = fgets_s(check_core_count, 255, fp);
64     if (NULL == max_core) {
65         printf("Error: In getting maximum number of lcores in the system.");
66         free(cfg);
67         pclose(fp);
68         return NULL;
69     }
70     num_cpus = atoi(max_core);
71     entry = rte_cfgfile_get_entry(cfg_file, "MAC2PHY_WORKER", "core_id");
72     if (entry) {
73         cfg->mac2phy_worker.core_id = (uint8_t) atoi(entry);
74         if (cfg->mac2phy_worker.core_id >= (uint8_t) num_cpus) {
75             printf("Core Id is not in the range 0 to %d: configured: %d\n",
76                 num_cpus, cfg->mac2phy_worker.core_id);
77             exit(-1);
78         }
79     }
80
81     entry =
82         rte_cfgfile_get_entry(cfg_file, "MAC2PHY_WORKER", "thread_sched_policy");
83     if (entry) {
84         cfg->mac2phy_worker.thread_sched_policy = (uint8_t) atoi(entry);
85         if (cfg->mac2phy_worker.thread_sched_policy != SCHED_FIFO &&
86             cfg->mac2phy_worker.thread_sched_policy != SCHED_RR) {
87             printf("Thread Poicy valid range is Schedule Policy [0: SCHED_FIFO"
88                 " 1: SCHED_RR]: configured: %d\n",
89                 cfg->mac2phy_worker.thread_sched_policy);
90             exit(-1);
91         }
92     }
93
94     int min_prio =
95         sched_get_priority_min(cfg->mac2phy_worker.thread_sched_policy);
96     int max_prio =
97         sched_get_priority_max(cfg->mac2phy_worker.thread_sched_policy);
98     entry =
99         rte_cfgfile_get_entry(cfg_file, "MAC2PHY_WORKER", "thread_priority");
100     if (entry) {
101         cfg->mac2phy_worker.thread_priority = (uint8_t) atoi(entry);
102         if (cfg->mac2phy_worker.thread_priority < min_prio &&
103             cfg->mac2phy_worker.thread_priority > max_prio) {
104             printf("Thread priority valid range is %d to %d: configured: %d\n",
105                 min_prio, max_prio, cfg->mac2phy_worker.thread_priority);
106             exit(-1);
107         }
108     }
109
110     entry = rte_cfgfile_get_entry(cfg_file, "PHY2MAC_WORKER", "core_id");
111     if (entry) {
112         cfg->phy2mac_worker.core_id = (uint8_t) atoi(entry);
113         if (cfg->phy2mac_worker.core_id >= (uint8_t) num_cpus) {
114             printf("Core Id is not in the range 0 to %d configured: %d\n",
115                 num_cpus, cfg->phy2mac_worker.core_id);
116             exit(-1);
117         }
118     }
119
120     entry =
121         rte_cfgfile_get_entry(cfg_file, "PHY2MAC_WORKER", "thread_sched_policy");
122     if (entry) {
123         cfg->phy2mac_worker.thread_sched_policy = (uint8_t) atoi(entry);
124         if (cfg->phy2mac_worker.thread_sched_policy != SCHED_FIFO &&
125             cfg->phy2mac_worker.thread_sched_policy != SCHED_RR) {
126             printf("Thread Poicy valid range is Schedule Policy [0: SCHED_FIFO"
127                 " 1: SCHED_RR] configured: %d\n",
128                 cfg->phy2mac_worker.thread_sched_policy);
129             exit(-1);
130         }
131     }
132
133     entry =
134         rte_cfgfile_get_entry(cfg_file, "PHY2MAC_WORKER", "thread_priority");
135     if (entry) {
136         cfg->phy2mac_worker.thread_priority = (uint8_t) atoi(entry);
137         if (cfg->phy2mac_worker.thread_priority < min_prio &&
138             cfg->phy2mac_worker.thread_priority > max_prio) {
139             printf("Thread priority valid range is %d to %d configured: %d\n",
140                 min_prio, max_prio, cfg->phy2mac_worker.thread_priority);
141             exit(-1);
142         }
143     }
144
145     entry = rte_cfgfile_get_entry(cfg_file, "WLS_CFG", "device_name");
146     if (entry) {
147         dev_name_len = (strlen(entry) > (NR5G_FAPI_DEVICE_NAME_LEN)) ?
148             (NR5G_FAPI_DEVICE_NAME_LEN) : strlen(entry);
149         rte_strlcpy(cfg->wls.device_name, entry, dev_name_len + 1);
150     }
151
152     entry = rte_cfgfile_get_entry(cfg_file, "WLS_CFG", "shmem_size");
153     if (entry) {
154         cfg->wls.shmem_size = (uint64_t) atoll(entry);
155         if (cfg->wls.shmem_size > NR5G_FAPI_MAX_SHMSIZE) {
156             printf("The memory range cannot exceed %d", NR5G_FAPI_MAX_SHMSIZE);
157             exit(-1);
158         }
159     }
160
161     entry = rte_cfgfile_get_entry(cfg_file, "LOGGER", "level");
162     if (entry) {
163         if (!strcmp(entry, "info"))
164             cfg->logger.level = INFO_LOG;
165         else if (!strcmp(entry, "debug"))
166             cfg->logger.level = DEBUG_LOG;
167         else if (!strcmp(entry, "error"))
168             cfg->logger.level = ERROR_LOG;
169         else if (!strcmp(entry, "trace"))
170             cfg->logger.level = TRACE_LOG;
171         else if (!strcmp(entry, "hexdump"))
172             cfg->logger.level = HEXDUMP_LOG;
173         else
174             cfg->logger.level = NONE_LOG;
175     }
176
177     pclose(fp);
178
179     return cfg;
180 }
181
182 char *fgets_s(
183     char *string,
184     size_t len,
185     FILE * fp)
186 {
187     if (fgets(string, len, fp) != 0) {
188         string[strcspn(string, "\n")] = '\0';
189         return string;
190     }
191     return 0;
192 }