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