xref: /f-stack/lib/ff_config.c (revision 813e23e2)
1 /*
2  * Copyright (C) 2017 THL A29 Limited, a Tencent company.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *   list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *   this list of conditions and the following disclaimer in the documentation
12  *   and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <getopt.h>
32 #include <ctype.h>
33 #include <rte_config.h>
34 #include <rte_string_fns.h>
35 
36 #include "ff_config.h"
37 #include "ff_ini_parser.h"
38 
39 #define DEFAULT_CONFIG_FILE   "config.ini"
40 
41 #define BITS_PER_HEX 4
42 
43 struct ff_config ff_global_cfg;
44 int dpdk_argc;
45 char *dpdk_argv[DPDK_CONFIG_NUM + 1];
46 
47 char* const short_options = "c:t:p:";
48 struct option long_options[] = {
49     { "conf", 1, NULL, 'c'},
50     { "proc-type", 1, NULL, 't'},
51     { "proc-id", 1, NULL, 'p'},
52     { 0, 0, 0, 0},
53 };
54 
55 static int
56 xdigit2val(unsigned char c)
57 {
58     int val;
59 
60     if (isdigit(c))
61         val = c - '0';
62     else if (isupper(c))
63         val = c - 'A' + 10;
64     else
65         val = c - 'a' + 10;
66     return val;
67 }
68 
69 static int
70 parse_lcore_mask(struct ff_config *cfg, const char *coremask)
71 {
72     int i, j, idx = 0, shift = 0, zero_num = 0;
73     unsigned count = 0;
74     char c;
75     int val;
76     uint16_t *proc_lcore;
77     char buf[RTE_MAX_LCORE] = {0};
78     char zero[RTE_MAX_LCORE] = {0};
79 
80     if (coremask == NULL)
81         return 0;
82 
83     cfg->dpdk.proc_lcore = (uint16_t *)calloc(RTE_MAX_LCORE, sizeof(uint16_t));
84     if (cfg->dpdk.proc_lcore == NULL) {
85         fprintf(stderr, "parse_lcore_mask malloc failed\n");
86         return 0;
87     }
88     proc_lcore = cfg->dpdk.proc_lcore;
89 
90     /*
91      * Remove all blank characters ahead and after.
92      * Remove 0x/0X if exists.
93      */
94     while (isblank(*coremask))
95         coremask++;
96     if (coremask[0] == '0' && ((coremask[1] == 'x')
97         || (coremask[1] == 'X')))
98         coremask += 2;
99 
100     i = strlen(coremask);
101     while ((i > 0) && isblank(coremask[i - 1]))
102         i--;
103 
104     if (i == 0)
105         return 0;
106 
107     for (i = i - 1; i >= 0 && idx < RTE_MAX_LCORE; i--) {
108         c = coremask[i];
109         if (isxdigit(c) == 0) {
110             return 0;
111         }
112         val = xdigit2val(c);
113         for (j = 0; j < BITS_PER_HEX && idx < RTE_MAX_LCORE; j++, idx++) {
114             if ((1 << j) & val) {
115                 proc_lcore[count] = idx;
116                 if (cfg->dpdk.proc_id == count) {
117 		    zero_num = idx >> 2;
118                     shift = idx & 0x3;
119                     memset(zero,'0',zero_num);
120                     sprintf(buf, "%llx%s", (unsigned long long)1<<shift, zero);
121                     cfg->dpdk.proc_mask = strdup(buf);
122 		}
123                 count++;
124             }
125         }
126     }
127 
128     for (; i >= 0; i--)
129         if (coremask[i] != '0')
130             return 0;
131 
132     if (cfg->dpdk.proc_id >= count)
133         return 0;
134 
135     cfg->dpdk.nb_procs = count;
136 
137     return 1;
138 }
139 
140 static int
141 is_integer(const char *s)
142 {
143     if (*s == '-' || *s == '+')
144         s++;
145     if (*s < '0' || '9' < *s)
146         return 0;
147     s++;
148     while ('0' <= *s && *s <= '9')
149         s++;
150     return (*s == '\0');
151 }
152 
153 static int
154 freebsd_conf_handler(struct ff_config *cfg, const char *section,
155     const char *name, const char *value)
156 {
157     struct ff_freebsd_cfg *newconf, **cur;
158     newconf = (struct ff_freebsd_cfg *)malloc(sizeof(struct ff_freebsd_cfg));
159     if (newconf == NULL) {
160         fprintf(stderr, "freebsd conf malloc failed\n");
161         return 0;
162     }
163 
164     newconf->name = strdup(name);
165     newconf->str = strdup(value);
166 
167     if (strcmp(section, "boot") == 0) {
168         cur = &cfg->freebsd.boot;
169 
170         newconf->value = (void *)newconf->str;
171         newconf->vlen = strlen(value);
172     } else if (strcmp(section, "sysctl") == 0) {
173         cur = &cfg->freebsd.sysctl;
174 
175         if (is_integer(value)) {
176             if (strcmp(name, "kern.ipc.maxsockbuf") == 0) {
177                 long *p = (long *)malloc(sizeof(long));
178                 *p = atol(value);
179                 newconf->value = (void *)p;
180                 newconf->vlen = sizeof(*p);
181             } else {
182                  int *p = (int *)malloc(sizeof(int));
183                  *p = atoi(value);
184                  newconf->value = (void *)p;
185                  newconf->vlen = sizeof(*p);
186             }
187         } else {
188             newconf->value = (void *)newconf->str;
189             newconf->vlen = strlen(value);
190         }
191     } else {
192         fprintf(stderr, "freebsd conf section[%s] error\n", section);
193         return 0;
194     }
195 
196     if (*cur == NULL) {
197         newconf->next = NULL;
198         *cur = newconf;
199     } else {
200         newconf->next = (*cur)->next;
201         (*cur)->next = newconf;
202     }
203 
204     return 1;
205 }
206 // A recursive binary search function. It returns location of x in
207 // given array arr[l..r] is present, otherwise -1
208 static int
209 uint16_binary_search(uint16_t arr[], int l, int r, uint16_t x)
210 {
211     if (r >= l) {
212         int mid = l + (r - l)/2;
213 
214         // If the element is present at the middle itself
215         if (arr[mid] == x)  return mid;
216 
217         // If element is smaller than mid, then it can only be present
218         // in left subarray
219         if (arr[mid] > x) return uint16_binary_search(arr, l, mid-1, x);
220 
221         // Else the element can only be present in right subarray
222         return uint16_binary_search(arr, mid+1, r, x);
223     }
224 
225     // We reach here when element is not present in array
226     return -1;
227 }
228 
229 static int
230 uint16_cmp (const void * a, const void * b)
231 {
232     return ( *(uint16_t*)a - *(uint16_t*)b );
233 }
234 
235 static inline void
236 sort_uint16_array(uint16_t arr[], int n)
237 {
238     qsort(arr, n, sizeof(uint16_t), uint16_cmp);
239 }
240 
241 static inline char *
242 __strstrip(char *s)
243 {
244     char *end = s + strlen(s) - 1;
245     while(*s == ' ') s++;
246     for (; end >= s; --end) {
247         if (*end != ' ') break;
248     }
249     *(++end) = '\0';
250     return s;
251 }
252 
253 static int
254 __parse_config_list(uint16_t *arr, int *sz, const char *value) {
255     int i, j;
256     char input[4096];
257     char *tokens[128];
258     int nTokens = 0;
259     char *endptr;
260     int nr_ele = 0;
261     int max_ele = *sz;
262 
263     strncpy(input, value, 4096);
264     nTokens = rte_strsplit(input, sizeof(input), tokens, 128, ',');
265     for (i = 0; i < nTokens; i++) {
266         char *tok = tokens[i];
267         char *middle = strchr(tok, '-');
268         if (middle == NULL) {
269             tok = __strstrip(tok);
270             long v = strtol(tok, &endptr, 10);
271             if (*endptr != '\0') {
272                 fprintf(stderr, "%s is not a integer.", tok);
273                 return 0;
274             }
275             if (nr_ele > max_ele) {
276                 fprintf(stderr, "too many elements in list %s\n", value);
277                 return 0;
278             }
279             arr[nr_ele++] = (uint16_t)v;
280         } else {
281             *middle = '\0';
282             char *lbound = __strstrip(tok);
283             char *rbound = __strstrip(middle+1);
284             long lv = strtol(lbound, &endptr, 10);
285             if (*endptr != '\0') {
286                 fprintf(stderr, "%s is not a integer.", lbound);
287                 return 0;
288             }
289             long rv = strtol(rbound, &endptr, 10);
290             if (*endptr != '\0') {
291                 fprintf(stderr, "%s is not a integer.", rbound);
292                 return 0;
293             }
294             for (j = lv; j <= rv; ++j) {
295                 if (nr_ele > max_ele) {
296                     fprintf(stderr, "too many elements in list %s.\n", value);
297                     return 0;
298                 }
299                 arr[nr_ele++] = (uint16_t)j;
300             }
301         }
302     }
303     if (nr_ele <= 0) {
304         fprintf(stderr, "list %s is empty\n", value);
305         return 1;
306     }
307     sort_uint16_array(arr, nr_ele);
308     *sz = nr_ele;
309     return 1;
310 }
311 
312 static int
313 parse_port_lcore_list(struct ff_port_cfg *cfg, const char *v_str)
314 {
315     cfg->nb_lcores = DPDK_MAX_LCORE;
316     uint16_t *cores = cfg->lcore_list;
317     return __parse_config_list(cores, &cfg->nb_lcores, v_str);
318 }
319 
320 static int
321 parse_port_list(struct ff_config *cfg, const char *v_str)
322 {
323     int res;
324     uint16_t ports[RTE_MAX_ETHPORTS];
325     int sz = RTE_MAX_ETHPORTS;
326 
327     res = __parse_config_list(ports, &sz, v_str);
328     if (! res) return res;
329 
330     uint16_t *portid_list = malloc(sizeof(uint16_t)*sz);
331 
332     if (portid_list == NULL) {
333         fprintf(stderr, "parse_port_list malloc failed\n");
334         return 0;
335     }
336     memcpy(portid_list, ports, sz*sizeof(uint16_t));
337 
338     cfg->dpdk.portid_list = portid_list;
339     cfg->dpdk.nb_ports = sz;
340     cfg->dpdk.max_portid = portid_list[sz-1];
341     return res;
342 }
343 
344 static int
345 port_cfg_handler(struct ff_config *cfg, const char *section,
346     const char *name, const char *value) {
347 
348     if (cfg->dpdk.nb_ports == 0) {
349         fprintf(stderr, "port_cfg_handler: must config dpdk.port_list first\n");
350         return 0;
351     }
352 
353     if (cfg->dpdk.port_cfgs == NULL) {
354         struct ff_port_cfg *pc = calloc(RTE_MAX_ETHPORTS, sizeof(struct ff_port_cfg));
355         if (pc == NULL) {
356             fprintf(stderr, "port_cfg_handler malloc failed\n");
357             return 0;
358         }
359         // initialize lcore list and nb_lcores
360         int i;
361         for (i = 0; i < cfg->dpdk.nb_ports; ++i) {
362             uint16_t portid = cfg->dpdk.portid_list[i];
363 
364             struct ff_port_cfg *pconf = &pc[portid];
365             pconf->port_id = portid;
366             pconf->nb_lcores = ff_global_cfg.dpdk.nb_procs;
367             memcpy(pconf->lcore_list, ff_global_cfg.dpdk.proc_lcore,
368                    pconf->nb_lcores*sizeof(uint16_t));
369         }
370         cfg->dpdk.port_cfgs = pc;
371     }
372 
373     int portid;
374     int ret = sscanf(section, "port%d", &portid);
375     if (ret != 1) {
376         fprintf(stderr, "port_cfg_handler section[%s] error\n", section);
377         return 0;
378     }
379 
380     /* just return true if portid >= nb_ports because it has no effect */
381     if (portid > cfg->dpdk.max_portid) {
382         fprintf(stderr, "port_cfg_handler section[%s] bigger than max port id\n", section);
383         return 1;
384     }
385 
386     struct ff_port_cfg *cur = &cfg->dpdk.port_cfgs[portid];
387     if (cur->name == NULL) {
388         cur->name = strdup(section);
389         cur->port_id = portid;
390     }
391 
392     if (strcmp(name, "addr") == 0) {
393         cur->addr = strdup(value);
394     } else if (strcmp(name, "netmask") == 0) {
395         cur->netmask = strdup(value);
396     } else if (strcmp(name, "broadcast") == 0) {
397         cur->broadcast = strdup(value);
398     } else if (strcmp(name, "gateway") == 0) {
399         cur->gateway = strdup(value);
400     } else if (strcmp(name, "pcap") == 0) {
401         cur->pcap = strdup(value);
402     } else if (strcmp(name, "lcore_list") == 0) {
403         return parse_port_lcore_list(cur, value);
404     }
405 
406     return 1;
407 }
408 
409 static int
410 vdev_cfg_handler(struct ff_config *cfg, const char *section,
411     const char *name, const char *value) {
412 
413     if (cfg->dpdk.nb_vdev == 0) {
414         fprintf(stderr, "vdev_cfg_handler: must config dpdk.nb_vdev first\n");
415         return 0;
416     }
417 
418     if (cfg->dpdk.vdev_cfgs == NULL) {
419         struct ff_vdev_cfg *vc = calloc(RTE_MAX_ETHPORTS, sizeof(struct ff_vdev_cfg));
420         if (vc == NULL) {
421             fprintf(stderr, "vdev_cfg_handler malloc failed\n");
422             return 0;
423         }
424         cfg->dpdk.vdev_cfgs = vc;
425     }
426 
427     int vdevid;
428     int ret = sscanf(section, "vdev%d", &vdevid);
429     if (ret != 1) {
430         fprintf(stderr, "vdev_cfg_handler section[%s] error\n", section);
431         return 0;
432     }
433 
434     /* just return true if vdevid >= nb_vdev because it has no effect */
435     if (vdevid > cfg->dpdk.nb_vdev) {
436         fprintf(stderr, "vdev_cfg_handler section[%s] bigger than max vdev id\n", section);
437         return 1;
438     }
439 
440     struct ff_vdev_cfg *cur = &cfg->dpdk.vdev_cfgs[vdevid];
441     if (cur->name == NULL) {
442         cur->name = strdup(section);
443         cur->vdev_id = vdevid;
444     }
445 
446     if (strcmp(name, "iface") == 0) {
447         cur->iface = strdup(value);
448     } else if (strcmp(name, "path") == 0) {
449         cur->path = strdup(value);
450     } else if (strcmp(name, "queues") == 0) {
451         cur->nb_queues = atoi(value);
452     } else if (strcmp(name, "queue_size") == 0) {
453         cur->queue_size = atoi(value);
454     } else if (strcmp(name, "mac") == 0) {
455         cur->mac = strdup(value);
456     } else if (strcmp(name, "cq") == 0) {
457         cur->nb_cq = atoi(value);
458     }
459 
460     return 1;
461 }
462 
463 
464 static int
465 ini_parse_handler(void* user, const char* section, const char* name,
466     const char* value)
467 {
468     struct ff_config *pconfig = (struct ff_config*)user;
469 
470     printf("[%s]: %s=%s\n", section, name, value);
471 
472     #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
473     if (MATCH("dpdk", "channel")) {
474         pconfig->dpdk.nb_channel = atoi(value);
475     } else if (MATCH("dpdk", "memory")) {
476         pconfig->dpdk.memory = atoi(value);
477     } else if (MATCH("dpdk", "no_huge")) {
478         pconfig->dpdk.no_huge = atoi(value);
479     } else if (MATCH("dpdk", "lcore_mask")) {
480         pconfig->dpdk.lcore_mask = strdup(value);
481         return parse_lcore_mask(pconfig, pconfig->dpdk.lcore_mask);
482     } else if (MATCH("dpdk", "base_virtaddr")) {
483         pconfig->dpdk.base_virtaddr= strdup(value);
484     } else if (MATCH("dpdk", "port_list")) {
485         return parse_port_list(pconfig, value);
486     } else if (MATCH("dpdk", "nb_vdev")) {
487         pconfig->dpdk.nb_vdev = atoi(value);
488     } else if (MATCH("dpdk", "promiscuous")) {
489         pconfig->dpdk.promiscuous = atoi(value);
490     } else if (MATCH("dpdk", "numa_on")) {
491         pconfig->dpdk.numa_on = atoi(value);
492     } else if (MATCH("dpdk", "tso")) {
493         pconfig->dpdk.tso = atoi(value);
494     } else if (MATCH("dpdk", "vlan_strip")) {
495         pconfig->dpdk.vlan_strip = atoi(value);
496     } else if (MATCH("dpdk", "idle_sleep")) {
497         pconfig->dpdk.idle_sleep = atoi(value);
498     } else if (MATCH("dpdk", "pkt_tx_delay")) {
499         pconfig->dpdk.pkt_tx_delay = atoi(value);
500     } else if (MATCH("kni", "enable")) {
501         pconfig->kni.enable= atoi(value);
502     } else if (MATCH("kni", "method")) {
503         pconfig->kni.method= strdup(value);
504     } else if (MATCH("kni", "tcp_port")) {
505         pconfig->kni.tcp_port = strdup(value);
506     } else if (MATCH("kni", "udp_port")) {
507         pconfig->kni.udp_port= strdup(value);
508     } else if (strcmp(section, "freebsd.boot") == 0) {
509         if (strcmp(name, "hz") == 0) {
510             pconfig->freebsd.hz = atoi(value);
511         } else if (strcmp(name, "physmem") == 0) {
512             pconfig->freebsd.physmem = atol(value);
513         } else if (strcmp(name, "fd_reserve") == 0) {
514             pconfig->freebsd.fd_reserve = atoi(value);
515         } else if (strcmp(name, "memsz_MB") == 0) {
516             pconfig->freebsd.mem_size = atoi(value);
517         } else {
518             return freebsd_conf_handler(pconfig, "boot", name, value);
519         }
520     } else if (strcmp(section, "freebsd.sysctl") == 0) {
521         return freebsd_conf_handler(pconfig, "sysctl", name, value);
522     } else if (strncmp(section, "port", 4) == 0) {
523         return port_cfg_handler(pconfig, section, name, value);
524     } else if (strncmp(section, "vdev", 4) == 0) {
525         return vdev_cfg_handler(pconfig, section, name, value);
526     }
527 
528     return 1;
529 }
530 
531 static int
532 dpdk_args_setup(struct ff_config *cfg)
533 {
534     int n = 0, i;
535     dpdk_argv[n++] = strdup("f-stack");
536     char temp[DPDK_CONFIG_MAXLEN] = {0};
537 
538     if (cfg->dpdk.no_huge) {
539         dpdk_argv[n++] = strdup("--no-huge");
540     }
541     if (cfg->dpdk.proc_mask) {
542         sprintf(temp, "-c%s", cfg->dpdk.proc_mask);
543         dpdk_argv[n++] = strdup(temp);
544     }
545     if (cfg->dpdk.nb_channel) {
546         sprintf(temp, "-n%d", cfg->dpdk.nb_channel);
547         dpdk_argv[n++] = strdup(temp);
548     }
549     if (cfg->dpdk.memory) {
550         sprintf(temp, "-m%d", cfg->dpdk.memory);
551         dpdk_argv[n++] = strdup(temp);
552     }
553     if (cfg->dpdk.proc_type) {
554         sprintf(temp, "--proc-type=%s", cfg->dpdk.proc_type);
555         dpdk_argv[n++] = strdup(temp);
556     }
557     if (cfg->dpdk.base_virtaddr) {
558         sprintf(temp, "--base-virtaddr=%s", cfg->dpdk.base_virtaddr);
559         dpdk_argv[n++] = strdup(temp);
560     }
561 
562     if (cfg->dpdk.nb_vdev) {
563         for (i=0; i<cfg->dpdk.nb_vdev; i++) {
564             sprintf(temp, "--vdev=virtio_user%d,path=%s",
565                 cfg->dpdk.vdev_cfgs[i].vdev_id,
566                 cfg->dpdk.vdev_cfgs[i].path);
567             if (cfg->dpdk.vdev_cfgs[i].nb_queues) {
568                 sprintf(temp, "%s,queues=%u",
569                     temp, cfg->dpdk.vdev_cfgs[i].nb_queues);
570             }
571             if (cfg->dpdk.vdev_cfgs[i].nb_cq) {
572                 sprintf(temp, "%s,cq=%u",
573                     temp, cfg->dpdk.vdev_cfgs[i].nb_cq);
574             }
575             if (cfg->dpdk.vdev_cfgs[i].queue_size) {
576                 sprintf(temp, "%s,queue_size=%u",
577                     temp, cfg->dpdk.vdev_cfgs[i].queue_size);
578             }
579             if (cfg->dpdk.vdev_cfgs[i].mac) {
580                 sprintf(temp, "%s,mac=%s",
581                     temp, cfg->dpdk.vdev_cfgs[i].mac);
582             }
583             dpdk_argv[n++] = strdup(temp);
584         }
585         sprintf(temp, "--no-pci");
586         dpdk_argv[n++] = strdup(temp);
587         sprintf(temp, "--file-prefix=container");
588         dpdk_argv[n++] = strdup(temp);
589     }
590 
591     dpdk_argc = n;
592 
593     for (i=0; i<n; i++)
594 	    printf("%s ", dpdk_argv[i]);
595 
596     return n;
597 }
598 
599 static int
600 ff_parse_args(struct ff_config *cfg, int argc, char *const argv[])
601 {
602     int c;
603     int index = 0;
604     optind = 1;
605     while((c = getopt_long(argc, argv, short_options, long_options, &index)) != -1) {
606         switch (c) {
607             case 'c':
608                 cfg->filename = strdup(optarg);
609                 break;
610             case 'p':
611                 cfg->dpdk.proc_id = atoi(optarg);
612                 break;
613             case 't':
614                 cfg->dpdk.proc_type = strdup(optarg);
615                 break;
616             default:
617                 return -1;
618         }
619     }
620 
621     if (cfg->dpdk.proc_type == NULL) {
622         cfg->dpdk.proc_type = strdup("auto");
623     }
624 
625     if (strcmp(cfg->dpdk.proc_type, "primary") &&
626         strcmp(cfg->dpdk.proc_type, "secondary") &&
627         strcmp(cfg->dpdk.proc_type, "auto")) {
628         printf("invalid proc-type:%s\n", cfg->dpdk.proc_type);
629         return -1;
630     }
631 
632     if ((uint16_t)cfg->dpdk.proc_id > RTE_MAX_LCORE) {
633         printf("invalid proc_id:%d, use default 0\n", cfg->dpdk.proc_id);
634         cfg->dpdk.proc_id = 0;
635     }
636 
637     return 0;
638 }
639 
640 static int
641 ff_check_config(struct ff_config *cfg)
642 {
643     if(cfg->kni.enable && !cfg->kni.method) {
644         fprintf(stderr, "conf dpdk.method is necessary\n");
645         return -1;
646     }
647 
648     if(cfg->kni.method) {
649         if(strcasecmp(cfg->kni.method,"accept") &&
650             strcasecmp(cfg->kni.method,"reject")) {
651             fprintf(stderr, "conf kni.method[accept|reject] is error(%s)\n",
652                 cfg->kni.method);
653             return -1;
654         }
655     }
656 
657     #define CHECK_VALID(n) \
658         do { \
659             if (!pc->n) { \
660                 fprintf(stderr, "port%d if config error: no %s\n", \
661                     pc->port_id, #n); \
662                 return -1; \
663             } \
664         } while (0)
665 
666     int i;
667     for (i = 0; i < cfg->dpdk.nb_ports; i++) {
668         uint16_t portid = cfg->dpdk.portid_list[i];
669         struct ff_port_cfg *pc = &cfg->dpdk.port_cfgs[portid];
670         CHECK_VALID(addr);
671         CHECK_VALID(netmask);
672         CHECK_VALID(broadcast);
673         CHECK_VALID(gateway);
674         // check if the lcores in lcore_list are enabled.
675         int k;
676         for (k = 0; k < pc->nb_lcores; k++) {
677             uint16_t lcore_id = pc->lcore_list[k];
678             if (uint16_binary_search(cfg->dpdk.proc_lcore, 0,
679                                      cfg->dpdk.nb_procs-1, lcore_id) < 0) {
680                 fprintf(stderr, "lcore %d is not enabled.\n", lcore_id);
681                 return -1;
682             }
683         }
684         /*
685          * only primary process process KNI, so if KNI enabled,
686          * primary lcore must stay in every enabled ports' lcore_list
687          */
688         if (cfg->kni.enable &&
689             strcmp(cfg->dpdk.proc_type, "primary") == 0) {
690             int found = 0;
691             int j;
692             uint16_t lcore_id = cfg->dpdk.proc_lcore[cfg->dpdk.proc_id];
693             for (j = 0; j < pc->nb_lcores; j++) {
694                 if (pc->lcore_list[j] == lcore_id) {
695                     found = 1;
696                 }
697             }
698             if (! found) {
699                 fprintf(stderr,
700                          "primary lcore %d should stay in port %d's lcore_list.\n",
701                          lcore_id, pc->port_id);
702                 return -1;
703             }
704         }
705     }
706 
707     return 0;
708 }
709 
710 static void
711 ff_default_config(struct ff_config *cfg)
712 {
713     memset(cfg, 0, sizeof(struct ff_config));
714 
715     cfg->filename = DEFAULT_CONFIG_FILE;
716 
717     cfg->dpdk.proc_id = -1;
718     cfg->dpdk.numa_on = 1;
719     cfg->dpdk.promiscuous = 1;
720     cfg->dpdk.pkt_tx_delay = BURST_TX_DRAIN_US;
721 
722     cfg->freebsd.hz = 100;
723     cfg->freebsd.physmem = 1048576*256;
724     cfg->freebsd.fd_reserve = 0;
725     cfg->freebsd.mem_size = 256;
726 }
727 
728 int
729 ff_load_config(int argc, char *const argv[])
730 {
731     ff_default_config(&ff_global_cfg);
732 
733     int ret = ff_parse_args(&ff_global_cfg, argc, argv);
734     if (ret < 0) {
735         return ret;
736     }
737 
738     ret = ini_parse(ff_global_cfg.filename, ini_parse_handler,
739         &ff_global_cfg);
740     if (ret != 0) {
741         printf("parse %s failed on line %d\n", ff_global_cfg.filename, ret);
742         return -1;
743     }
744 
745     if (ff_check_config(&ff_global_cfg)) {
746         return -1;
747     }
748 
749     if (dpdk_args_setup(&ff_global_cfg) <= 0) {
750         return -1;
751     }
752 
753     return 0;
754 }
755