Update to odulow per maintenance bronze
[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     pclose(fp);
71     num_cpus = atoi(max_core);
72     entry = rte_cfgfile_get_entry(cfg_file, "MAC2PHY_WORKER", "core_id");
73     if (entry) {
74         cfg->mac2phy_worker.core_id = (uint8_t) atoi(entry);
75         if (cfg->mac2phy_worker.core_id >= (uint8_t) num_cpus) {
76             printf("Core Id is not in the range 0 to %d: configured: %d\n",
77                 num_cpus, cfg->mac2phy_worker.core_id);
78             exit(-1);
79         }
80     }
81
82     entry =
83         rte_cfgfile_get_entry(cfg_file, "MAC2PHY_WORKER",
84         "thread_sched_policy");
85     if (entry) {
86         cfg->mac2phy_worker.thread_sched_policy = (uint8_t) atoi(entry);
87         if (cfg->mac2phy_worker.thread_sched_policy != SCHED_FIFO &&
88             cfg->mac2phy_worker.thread_sched_policy != SCHED_RR) {
89             printf("Thread Poicy valid range is Schedule Policy [0: SCHED_FIFO"
90                 " 1: SCHED_RR]: configured: %d\n",
91                 cfg->mac2phy_worker.thread_sched_policy);
92             exit(-1);
93         }
94     }
95
96     int min_prio =
97         sched_get_priority_min(cfg->mac2phy_worker.thread_sched_policy);
98     int max_prio =
99         sched_get_priority_max(cfg->mac2phy_worker.thread_sched_policy);
100     entry =
101         rte_cfgfile_get_entry(cfg_file, "MAC2PHY_WORKER", "thread_priority");
102     if (entry) {
103         cfg->mac2phy_worker.thread_priority = (uint8_t) atoi(entry);
104         if (cfg->mac2phy_worker.thread_priority < min_prio &&
105             cfg->mac2phy_worker.thread_priority > max_prio) {
106             printf("Thread priority valid range is %d to %d: configured: %d\n",
107                 min_prio, max_prio, cfg->mac2phy_worker.thread_priority);
108             exit(-1);
109         }
110     }
111
112     entry = rte_cfgfile_get_entry(cfg_file, "PHY2MAC_WORKER", "core_id");
113     if (entry) {
114         cfg->phy2mac_worker.core_id = (uint8_t) atoi(entry);
115         if (cfg->phy2mac_worker.core_id >= (uint8_t) num_cpus) {
116             printf("Core Id is not in the range 0 to %d configured: %d\n",
117                 num_cpus, cfg->phy2mac_worker.core_id);
118             exit(-1);
119         }
120     }
121
122     entry =
123         rte_cfgfile_get_entry(cfg_file, "PHY2MAC_WORKER",
124         "thread_sched_policy");
125     if (entry) {
126         cfg->phy2mac_worker.thread_sched_policy = (uint8_t) atoi(entry);
127         if (cfg->phy2mac_worker.thread_sched_policy != SCHED_FIFO &&
128             cfg->phy2mac_worker.thread_sched_policy != SCHED_RR) {
129             printf("Thread Poicy valid range is Schedule Policy [0: SCHED_FIFO"
130                 " 1: SCHED_RR] configured: %d\n",
131                 cfg->phy2mac_worker.thread_sched_policy);
132             exit(-1);
133         }
134     }
135
136     entry =
137         rte_cfgfile_get_entry(cfg_file, "PHY2MAC_WORKER", "thread_priority");
138     if (entry) {
139         cfg->phy2mac_worker.thread_priority = (uint8_t) atoi(entry);
140         if (cfg->phy2mac_worker.thread_priority < min_prio &&
141             cfg->phy2mac_worker.thread_priority > max_prio) {
142             printf("Thread priority valid range is %d to %d configured: %d\n",
143                 min_prio, max_prio, cfg->phy2mac_worker.thread_priority);
144             exit(-1);
145         }
146     }
147
148     entry = rte_cfgfile_get_entry(cfg_file, "WLS_CFG", "device_name");
149     if (entry) {
150         dev_name_len = (strlen(entry) > (NR5G_FAPI_DEVICE_NAME_LEN)) ?
151             (NR5G_FAPI_DEVICE_NAME_LEN) : strlen(entry);
152         rte_strlcpy(cfg->wls.device_name, entry, dev_name_len + 1);
153     }
154
155     entry = rte_cfgfile_get_entry(cfg_file, "WLS_CFG", "shmem_size");
156     if (entry) {
157         cfg->wls.shmem_size = (uint64_t) atoll(entry);
158         if (cfg->wls.shmem_size > NR5G_FAPI_MAX_SHMSIZE) {
159             printf("The memory range cannot exceed %d", NR5G_FAPI_MAX_SHMSIZE);
160             exit(-1);
161         }
162     }
163
164     entry = rte_cfgfile_get_entry(cfg_file, "LOGGER", "level");
165     if (entry) {
166         if (!strcmp(entry, "info"))
167             cfg->logger.level = INFO_LOG;
168         else if (!strcmp(entry, "debug"))
169             cfg->logger.level = DEBUG_LOG;
170         else if (!strcmp(entry, "error"))
171             cfg->logger.level = ERROR_LOG;
172         else if (!strcmp(entry, "trace"))
173             cfg->logger.level = TRACE_LOG;
174         else
175             cfg->logger.level = NONE_LOG;
176     }
177
178     entry = rte_cfgfile_get_entry(cfg_file, "DPDK", "dpdk_iova_mode");
179     if (entry) {
180         cfg->dpdk.iova_mode = (uint8_t) atoi(entry);
181         if (cfg->dpdk.iova_mode >= DPDK_IOVA_MAX_MODE) {
182             printf("The mode is out of range: %d", cfg->dpdk.iova_mode);
183             exit(-1);
184         }
185     }
186
187     return cfg;
188 }
189
190 char *fgets_s(
191     char *string,
192     size_t len,
193     FILE * fp)
194 {
195     if (fgets(string, len, fp) != 0) {
196         string[strcspn(string, "\n")] = '\0';
197         return string;
198     }
199     return 0;
200 }