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