Line data Source code
1 : #include <stdio.h>
2 : #include <stdint.h>
3 : #include <string.h>
4 : #include <sys/types.h>
5 : #include <sys/stat.h>
6 : #include <unistd.h>
7 : #include <malloc.h>
8 : #include <stdbool.h>
9 : #include <arpa/inet.h>
10 : #include <sys/types.h>
11 : #include <sys/socket.h>
12 : #include <asm/types.h>
13 : #include <linux/netlink.h>
14 : #include <ctype.h>
15 : #include <stdlib.h>
16 :
17 : #include "vr_os.h"
18 : #include "ini_parser.h"
19 : #include "nl_util.h"
20 :
21 : #define BUF_LENGTH 256
22 :
23 : static char value[BUF_LENGTH];
24 : static char *ini_data = NULL;
25 : static char ini_file[] = "/etc/contrail/contrail-vrouter-agent.conf";
26 : static bool platform_vtest = false;
27 :
28 : /*
29 : * API to set plafform as vtest; Used in vtest UT executions
30 : */
31 : void
32 2951 : set_platform_vtest (void)
33 : {
34 2951 : platform_vtest = true;
35 2951 : }
36 :
37 : /*
38 : * API to check if platform is vtest
39 : */
40 : bool
41 11844 : platform_is_vtest (void)
42 : {
43 11844 : return platform_vtest;
44 : }
45 :
46 : static void
47 0 : copy_line(char *buffer, const char *line, uint32_t *index)
48 : {
49 0 : uint32_t i = 0;
50 0 : if (line[0] == '#') {
51 0 : return;
52 : }
53 :
54 0 : while (line[i]) {
55 0 : if (isspace(line[i])) {
56 0 : i++;
57 0 : continue;
58 : }
59 0 : buffer[(*index)++] = line[i++];
60 : }
61 0 : buffer[(*index)++] = '\n';
62 : }
63 :
64 : static int
65 0 : read_file_size(const char *file_path)
66 : {
67 : struct stat stat_buffer;
68 :
69 0 : if (stat(file_path, &stat_buffer) == 0) {
70 0 : return stat_buffer.st_size;
71 : }
72 0 : return 0;
73 : }
74 :
75 : int
76 2965 : parse_ini_file(void)
77 : {
78 : FILE *fp;
79 : char line[4 * BUF_LENGTH];
80 : size_t size;
81 2965 : uint32_t index = 0;
82 :
83 2965 : fp = fopen(ini_file, "r");
84 2965 : if (fp == NULL) {
85 2965 : return -1;
86 : }
87 :
88 0 : size = read_file_size(ini_file);
89 : /*
90 : * Allocate memory to hold read buffer
91 : */
92 0 : ini_data = calloc(size, sizeof(char));
93 0 : if (ini_data == NULL) {
94 0 : fclose(fp);
95 0 : return -1;
96 : }
97 :
98 0 : while (fgets(line, sizeof(line), fp) != NULL) {
99 0 : copy_line(ini_data, line, &index);
100 : }
101 :
102 0 : fclose(fp);
103 0 : return 0;
104 : }
105 :
106 : bool
107 14795 : read_value(const char *section, const char *key)
108 : {
109 14795 : const char *section_start = NULL;
110 14795 : const char *section_end = NULL;
111 14795 : const char *key_start = NULL;
112 14795 : const char *value_start = NULL;
113 : char section_buffer[BUF_LENGTH];
114 : char buffer[BUF_LENGTH];
115 :
116 14795 : if (!ini_data || !section || !key) {
117 14795 : return false;
118 : }
119 :
120 0 : snprintf(section_buffer, sizeof(section_buffer), "[%s]", section);
121 : /*
122 : * Check if section is present
123 : */
124 0 : section_start = strstr(ini_data, section_buffer);
125 :
126 : /*
127 : * Section is missing
128 : */
129 0 : if (section_start == NULL) {
130 0 : return false;
131 : }
132 :
133 0 : section_start += strlen(section_buffer) + 1;
134 0 : section_end = strstr(section_start, "[");
135 :
136 0 : key_start = strstr(section_start, key);
137 0 : if (key_start == NULL) {
138 0 : return false;
139 : }
140 :
141 0 : if (section_end && key_start > section_end) {
142 : /* key not found in same section */
143 0 : return false;
144 : }
145 :
146 0 : memset(value, 0, sizeof(value));
147 0 : buffer[sizeof(buffer) - 1] = '\0';
148 0 : strncpy(buffer, key_start, sizeof(buffer) - 1);
149 0 : value_start = strtok(buffer, "=");
150 0 : value_start = strtok(NULL, "=");
151 0 : if (value_start) {
152 0 : strcpy(value, value_start);
153 0 : char *newline = strchr(value,'\n');
154 0 : *newline = '\0';
155 0 : return true;
156 : }
157 0 : return false;
158 : }
159 :
160 : int
161 0 : read_int(const char *section, const char *key)
162 : {
163 0 : if (read_value(section, key) == false) {
164 0 : return 0;
165 : }
166 0 : return atoi(value);
167 : }
168 :
169 : const char*
170 14795 : read_string(const char *section, const char *key)
171 : {
172 14795 : if (read_value(section, key) == false) {
173 14795 : return NULL;
174 : }
175 0 : return value;
176 : }
177 :
178 : uint32_t
179 0 : read_ip(const char *section, const char *key)
180 : {
181 : struct in_addr ip;
182 :
183 0 : if (read_value(section, key) == false) {
184 0 : return 0;
185 : }
186 :
187 0 : if (inet_pton(AF_INET, value, &ip) == 1) {
188 0 : return ntohl(ip.s_addr);
189 : }
190 0 : return 0;
191 : }
192 :
193 : int
194 2951 : get_domain(void)
195 : {
196 2951 : const char *platform = read_string(DEFAULT_SECTION, PLATFORM_KEY);
197 2951 : if (platform &&
198 0 : (strcmp(platform, PLATFORM_DPDK) == 0 ||
199 0 : strcmp(platform, PLATFORM_NIC) == 0)) {
200 : #ifdef AGENT_VROUTER_TCP
201 : return AF_INET;
202 : #else
203 0 : return AF_UNIX;
204 : #endif
205 : }
206 2951 : if (platform_is_vtest()) {
207 2951 : return AF_UNIX;
208 : }
209 0 : return AF_NETLINK;
210 : }
211 :
212 : int
213 2951 : get_type(void)
214 : {
215 2951 : const char *platform = read_string(DEFAULT_SECTION, PLATFORM_KEY);
216 2951 : if (platform &&
217 0 : (strcmp(platform, PLATFORM_DPDK) == 0 ||
218 0 : strcmp(platform, PLATFORM_NIC) == 0)) {
219 0 : return SOCK_STREAM;
220 : }
221 2951 : if (platform_is_vtest()) {
222 2951 : return SOCK_STREAM;
223 : }
224 0 : return SOCK_DGRAM;
225 : }
226 :
227 : uint16_t
228 2951 : get_port(void)
229 : {
230 2951 : const char *platform = read_string(DEFAULT_SECTION, PLATFORM_KEY);
231 2951 : if (platform &&
232 0 : (strcmp(platform, PLATFORM_DPDK) == 0 ||
233 0 : strcmp(platform, PLATFORM_NIC) == 0)) {
234 0 : return vr_netlink_port;
235 : }
236 2951 : return 0;
237 : }
238 :
239 : uint32_t
240 2951 : get_ip(void)
241 : {
242 2951 : return 0x7f000001;
243 : }
244 :
245 : int
246 2951 : get_protocol(void)
247 : {
248 2951 : const char *platform = read_string(DEFAULT_SECTION, PLATFORM_KEY);
249 2951 : if (platform &&
250 0 : (strcmp(platform, PLATFORM_DPDK) == 0 ||
251 0 : strcmp(platform, PLATFORM_NIC) == 0)) {
252 0 : return 0;
253 : }
254 2951 : if (platform_is_vtest()) {
255 2951 : return 0;
256 : }
257 0 : return NETLINK_GENERIC;
258 : }
259 :
260 : int
261 2991 : get_platform(void)
262 : {
263 2991 : const char *platform = read_string(DEFAULT_SECTION, PLATFORM_KEY);
264 :
265 2991 : if (platform) {
266 0 : if (!strcmp(platform, PLATFORM_DPDK))
267 0 : return DPDK_PLATFORM;
268 0 : else if (!strcmp(platform, PLATFORM_NIC))
269 0 : return NIC_PLATFORM;
270 : else
271 0 : return LINUX_PLATFORM;
272 : }
273 2991 : if (platform_is_vtest()) {
274 2979 : return VTEST_PLATFORM;
275 : }
276 :
277 12 : return LINUX_PLATFORM;
278 : }
279 :
280 : const char *
281 0 : get_platform_str(void)
282 : {
283 0 : if (platform_is_vtest()) {
284 0 : return PLATFORM_VTEST;
285 : }
286 0 : return read_string(DEFAULT_SECTION, PLATFORM_KEY);
287 : }
288 :
289 : bool
290 0 : get_offload_enabled(void)
291 : {
292 0 : const char *offload_str = read_string(DEFAULT_SECTION, OFFLOAD_KEY);
293 0 : if (offload_str) {
294 0 : if (!strcmp(offload_str, OFFLOAD_ENABLED))
295 0 : return true;
296 : }
297 0 : return false;
298 : }
|