xref: /f-stack/lib/ff_config.c (revision c0f66684)
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, "%llx", (unsigned long long)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 parse_port_slave_list(struct ff_port_cfg *cfg, const char *v_str)
342 {
343     int res;
344     uint16_t ports[RTE_MAX_ETHPORTS];
345     int sz = RTE_MAX_ETHPORTS;
346 
347     res = __parse_config_list(ports, &sz, v_str);
348     if (! res) return res;
349 
350     uint16_t *portid_list = malloc(sizeof(uint16_t)*sz);
351 
352     if (portid_list == NULL) {
353         fprintf(stderr, "parse_port_slave_list malloc failed\n");
354         return 0;
355     }
356     memcpy(portid_list, ports, sz*sizeof(uint16_t));
357 
358     cfg->slave_portid_list = portid_list;
359     cfg->nb_slaves = sz;
360 
361     return res;
362 }
363 
364 static int
365 port_cfg_handler(struct ff_config *cfg, const char *section,
366     const char *name, const char *value) {
367 
368     if (cfg->dpdk.nb_ports == 0) {
369         fprintf(stderr, "port_cfg_handler: must config dpdk.port_list first\n");
370         return 0;
371     }
372 
373     if (cfg->dpdk.port_cfgs == NULL) {
374         struct ff_port_cfg *pc = calloc(RTE_MAX_ETHPORTS, sizeof(struct ff_port_cfg));
375         if (pc == NULL) {
376             fprintf(stderr, "port_cfg_handler malloc failed\n");
377             return 0;
378         }
379         // initialize lcore list and nb_lcores
380         int i;
381         for (i = 0; i < cfg->dpdk.nb_ports; ++i) {
382             uint16_t portid = cfg->dpdk.portid_list[i];
383 
384             struct ff_port_cfg *pconf = &pc[portid];
385             pconf->port_id = portid;
386             pconf->nb_lcores = ff_global_cfg.dpdk.nb_procs;
387             memcpy(pconf->lcore_list, ff_global_cfg.dpdk.proc_lcore,
388                    pconf->nb_lcores*sizeof(uint16_t));
389         }
390         cfg->dpdk.port_cfgs = pc;
391     }
392 
393     int portid;
394     int ret = sscanf(section, "port%d", &portid);
395     if (ret != 1) {
396         fprintf(stderr, "port_cfg_handler section[%s] error\n", section);
397         return 0;
398     }
399 
400     /* just return true if portid >= nb_ports because it has no effect */
401     if (portid > cfg->dpdk.max_portid) {
402         fprintf(stderr, "port_cfg_handler section[%s] bigger than max port id\n", section);
403         return 1;
404     }
405 
406     struct ff_port_cfg *cur = &cfg->dpdk.port_cfgs[portid];
407     if (cur->name == NULL) {
408         cur->name = strdup(section);
409         cur->port_id = portid;
410     }
411 
412     if (strcmp(name, "addr") == 0) {
413         cur->addr = strdup(value);
414     } else if (strcmp(name, "netmask") == 0) {
415         cur->netmask = strdup(value);
416     } else if (strcmp(name, "broadcast") == 0) {
417         cur->broadcast = strdup(value);
418     } else if (strcmp(name, "gateway") == 0) {
419         cur->gateway = strdup(value);
420     } else if (strcmp(name, "pcap") == 0) {
421         cur->pcap = strdup(value);
422     } else if (strcmp(name, "lcore_list") == 0) {
423         return parse_port_lcore_list(cur, value);
424     } else if (strcmp(name, "slave_port_list") == 0) {
425         return parse_port_slave_list(cur, value);
426     }
427 
428     return 1;
429 }
430 
431 static int
432 vdev_cfg_handler(struct ff_config *cfg, const char *section,
433     const char *name, const char *value) {
434 
435     if (cfg->dpdk.nb_vdev == 0) {
436         fprintf(stderr, "vdev_cfg_handler: must config dpdk.nb_vdev first\n");
437         return 0;
438     }
439 
440     if (cfg->dpdk.vdev_cfgs == NULL) {
441         struct ff_vdev_cfg *vc = calloc(RTE_MAX_ETHPORTS, sizeof(struct ff_vdev_cfg));
442         if (vc == NULL) {
443             fprintf(stderr, "vdev_cfg_handler malloc failed\n");
444             return 0;
445         }
446         cfg->dpdk.vdev_cfgs = vc;
447     }
448 
449     int vdevid;
450     int ret = sscanf(section, "vdev%d", &vdevid);
451     if (ret != 1) {
452         fprintf(stderr, "vdev_cfg_handler section[%s] error\n", section);
453         return 0;
454     }
455 
456     /* just return true if vdevid >= nb_vdev because it has no effect */
457     if (vdevid > cfg->dpdk.nb_vdev) {
458         fprintf(stderr, "vdev_cfg_handler section[%s] bigger than max vdev id\n", section);
459         return 1;
460     }
461 
462     struct ff_vdev_cfg *cur = &cfg->dpdk.vdev_cfgs[vdevid];
463     if (cur->name == NULL) {
464         cur->name = strdup(section);
465         cur->vdev_id = vdevid;
466     }
467 
468     if (strcmp(name, "iface") == 0) {
469         cur->iface = strdup(value);
470     } else if (strcmp(name, "path") == 0) {
471         cur->path = strdup(value);
472     } else if (strcmp(name, "queues") == 0) {
473         cur->nb_queues = atoi(value);
474     } else if (strcmp(name, "queue_size") == 0) {
475         cur->queue_size = atoi(value);
476     } else if (strcmp(name, "mac") == 0) {
477         cur->mac = strdup(value);
478     } else if (strcmp(name, "cq") == 0) {
479         cur->nb_cq = atoi(value);
480     }
481 
482     return 1;
483 }
484 
485 static int
486 bond_cfg_handler(struct ff_config *cfg, const char *section,
487     const char *name, const char *value) {
488 
489     if (cfg->dpdk.nb_bond == 0) {
490         fprintf(stderr, "bond_cfg_handler: must config dpdk.nb_bond first\n");
491         return 0;
492     }
493 
494     if (cfg->dpdk.bond_cfgs == NULL) {
495         struct ff_bond_cfg *vc = calloc(RTE_MAX_ETHPORTS, sizeof(struct ff_bond_cfg));
496         if (vc == NULL) {
497             fprintf(stderr, "ff_bond_cfg malloc failed\n");
498             return 0;
499         }
500         cfg->dpdk.bond_cfgs = vc;
501     }
502 
503     int bondid;
504     int ret = sscanf(section, "bond%d", &bondid);
505     if (ret != 1) {
506         fprintf(stderr, "bond_cfg_handler section[%s] error\n", section);
507         return 0;
508     }
509 
510     /* just return true if bondid >= nb_vdev because it has no effect */
511     if (bondid > cfg->dpdk.nb_bond) {
512         fprintf(stderr, "bond_cfg_handler section[%s] bigger than max bond id\n", section);
513         return 1;
514     }
515 
516     struct ff_bond_cfg *cur = &cfg->dpdk.bond_cfgs[bondid];
517     if (cur->name == NULL) {
518         cur->name = strdup(section);
519         cur->bond_id = bondid;
520     }
521 
522     if (strcmp(name, "mode") == 0) {
523         cur->mode = atoi(value);
524     } else if (strcmp(name, "slave") == 0) {
525         cur->slave = strdup(value);
526     } else if (strcmp(name, "primary") == 0) {
527         cur->primary = strdup(value);
528     } else if (strcmp(name, "socket_id") == 0) {
529         cur->socket_id = atoi(value);
530     } else if (strcmp(name, "mac") == 0) {
531         cur->bond_mac = strdup(value);
532     } else if (strcmp(name, "xmit_policy") == 0) {
533         cur->xmit_policy = strdup(value);
534     } else if (strcmp(name, "lsc_poll_period_ms") == 0) {
535         cur->lsc_poll_period_ms = atoi(value);
536     } else if (strcmp(name, "up_delay") == 0) {
537         cur->up_delay = atoi(value);
538     } else if (strcmp(name, "down_delay") == 0) {
539         cur->down_delay = atoi(value);
540     }
541 
542     return 1;
543 }
544 
545 static int
546 ini_parse_handler(void* user, const char* section, const char* name,
547     const char* value)
548 {
549     struct ff_config *pconfig = (struct ff_config*)user;
550 
551     printf("[%s]: %s=%s\n", section, name, value);
552 
553     #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
554     if (MATCH("dpdk", "channel")) {
555         pconfig->dpdk.nb_channel = atoi(value);
556     } else if (MATCH("dpdk", "memory")) {
557         pconfig->dpdk.memory = atoi(value);
558     } else if (MATCH("dpdk", "no_huge")) {
559         pconfig->dpdk.no_huge = atoi(value);
560     } else if (MATCH("dpdk", "lcore_mask")) {
561         pconfig->dpdk.lcore_mask = strdup(value);
562         return parse_lcore_mask(pconfig, pconfig->dpdk.lcore_mask);
563     } else if (MATCH("dpdk", "base_virtaddr")) {
564         pconfig->dpdk.base_virtaddr= strdup(value);
565     } else if (MATCH("dpdk", "port_list")) {
566         return parse_port_list(pconfig, value);
567     } else if (MATCH("dpdk", "nb_vdev")) {
568         pconfig->dpdk.nb_vdev = atoi(value);
569     } else if (MATCH("dpdk", "nb_bond")) {
570         pconfig->dpdk.nb_bond = atoi(value);
571     } else if (MATCH("dpdk", "promiscuous")) {
572         pconfig->dpdk.promiscuous = atoi(value);
573     } else if (MATCH("dpdk", "numa_on")) {
574         pconfig->dpdk.numa_on = atoi(value);
575     } else if (MATCH("dpdk", "tso")) {
576         pconfig->dpdk.tso = atoi(value);
577     } else if (MATCH("dpdk", "vlan_strip")) {
578         pconfig->dpdk.vlan_strip = atoi(value);
579     } else if (MATCH("dpdk", "idle_sleep")) {
580         pconfig->dpdk.idle_sleep = atoi(value);
581     } else if (MATCH("dpdk", "pkt_tx_delay")) {
582         pconfig->dpdk.pkt_tx_delay = atoi(value);
583     } else if (MATCH("kni", "enable")) {
584         pconfig->kni.enable= atoi(value);
585     } else if (MATCH("kni", "method")) {
586         pconfig->kni.method= strdup(value);
587     } else if (MATCH("kni", "tcp_port")) {
588         pconfig->kni.tcp_port = strdup(value);
589     } else if (MATCH("kni", "udp_port")) {
590         pconfig->kni.udp_port= strdup(value);
591     } else if (strcmp(section, "freebsd.boot") == 0) {
592         if (strcmp(name, "hz") == 0) {
593             pconfig->freebsd.hz = atoi(value);
594         } else if (strcmp(name, "physmem") == 0) {
595             pconfig->freebsd.physmem = atol(value);
596         } else if (strcmp(name, "fd_reserve") == 0) {
597             pconfig->freebsd.fd_reserve = atoi(value);
598         } else if (strcmp(name, "memsz_MB") == 0) {
599             pconfig->freebsd.mem_size = atoi(value);
600         } else {
601             return freebsd_conf_handler(pconfig, "boot", name, value);
602         }
603     } else if (strcmp(section, "freebsd.sysctl") == 0) {
604         return freebsd_conf_handler(pconfig, "sysctl", name, value);
605     } else if (strncmp(section, "port", 4) == 0) {
606         return port_cfg_handler(pconfig, section, name, value);
607     } else if (strncmp(section, "vdev", 4) == 0) {
608         return vdev_cfg_handler(pconfig, section, name, value);
609     } else if (strncmp(section, "bond", 4) == 0) {
610         return bond_cfg_handler(pconfig, section, name, value);
611     }
612 
613     return 1;
614 }
615 
616 static int
617 dpdk_args_setup(struct ff_config *cfg)
618 {
619     int n = 0, i;
620     dpdk_argv[n++] = strdup("f-stack");
621     char temp[DPDK_CONFIG_MAXLEN] = {0};
622 
623     if (cfg->dpdk.no_huge) {
624         dpdk_argv[n++] = strdup("--no-huge");
625     }
626     if (cfg->dpdk.proc_mask) {
627         sprintf(temp, "-c%s", cfg->dpdk.proc_mask);
628         dpdk_argv[n++] = strdup(temp);
629     }
630     if (cfg->dpdk.nb_channel) {
631         sprintf(temp, "-n%d", cfg->dpdk.nb_channel);
632         dpdk_argv[n++] = strdup(temp);
633     }
634     if (cfg->dpdk.memory) {
635         sprintf(temp, "-m%d", cfg->dpdk.memory);
636         dpdk_argv[n++] = strdup(temp);
637     }
638     if (cfg->dpdk.proc_type) {
639         sprintf(temp, "--proc-type=%s", cfg->dpdk.proc_type);
640         dpdk_argv[n++] = strdup(temp);
641     }
642     if (cfg->dpdk.base_virtaddr) {
643         sprintf(temp, "--base-virtaddr=%s", cfg->dpdk.base_virtaddr);
644         dpdk_argv[n++] = strdup(temp);
645     }
646 
647     if (cfg->dpdk.nb_vdev) {
648         for (i=0; i<cfg->dpdk.nb_vdev; i++) {
649             sprintf(temp, "--vdev=virtio_user%d,path=%s",
650                 cfg->dpdk.vdev_cfgs[i].vdev_id,
651                 cfg->dpdk.vdev_cfgs[i].path);
652             if (cfg->dpdk.vdev_cfgs[i].nb_queues) {
653                 sprintf(temp, "%s,queues=%u",
654                     temp, cfg->dpdk.vdev_cfgs[i].nb_queues);
655             }
656             if (cfg->dpdk.vdev_cfgs[i].nb_cq) {
657                 sprintf(temp, "%s,cq=%u",
658                     temp, cfg->dpdk.vdev_cfgs[i].nb_cq);
659             }
660             if (cfg->dpdk.vdev_cfgs[i].queue_size) {
661                 sprintf(temp, "%s,queue_size=%u",
662                     temp, cfg->dpdk.vdev_cfgs[i].queue_size);
663             }
664             if (cfg->dpdk.vdev_cfgs[i].mac) {
665                 sprintf(temp, "%s,mac=%s",
666                     temp, cfg->dpdk.vdev_cfgs[i].mac);
667             }
668             dpdk_argv[n++] = strdup(temp);
669         }
670         sprintf(temp, "--no-pci");
671         dpdk_argv[n++] = strdup(temp);
672         sprintf(temp, "--file-prefix=container");
673         dpdk_argv[n++] = strdup(temp);
674     }
675 
676     if (cfg->dpdk.nb_bond) {
677         for (i=0; i<cfg->dpdk.nb_bond; i++) {
678             sprintf(temp, "--vdev");
679             dpdk_argv[n++] = strdup(temp);
680             sprintf(temp, "net_bonding%d,mode=%d,slave=%s",
681                 cfg->dpdk.bond_cfgs[i].bond_id,
682                 cfg->dpdk.bond_cfgs[i].mode,
683                 cfg->dpdk.bond_cfgs[i].slave);
684 
685                 if (cfg->dpdk.bond_cfgs[i].primary) {
686                     sprintf(temp, "%s,primary=%s",
687                     temp, cfg->dpdk.bond_cfgs[i].primary);
688                 }
689 
690                 if (cfg->dpdk.bond_cfgs[i].socket_id) {
691                     sprintf(temp, "%s,socket_id=%d",
692                     temp, cfg->dpdk.bond_cfgs[i].socket_id);
693                 }
694 
695                 if (cfg->dpdk.bond_cfgs[i].bond_mac) {
696                     sprintf(temp, "%s,mac=%s",
697                     temp, cfg->dpdk.bond_cfgs[i].bond_mac);
698                 }
699 
700                 if (cfg->dpdk.bond_cfgs[i].xmit_policy) {
701                     sprintf(temp, "%s,xmit_policy=%s",
702                     temp, cfg->dpdk.bond_cfgs[i].xmit_policy);
703                 }
704 
705                 if (cfg->dpdk.bond_cfgs[i].lsc_poll_period_ms) {
706                     sprintf(temp, "%s,lsc_poll_period_ms=%d",
707                     temp, cfg->dpdk.bond_cfgs[i].lsc_poll_period_ms);
708                 }
709 
710                 if (cfg->dpdk.bond_cfgs[i].up_delay) {
711                     sprintf(temp, "%s,up_delay=%d",
712                     temp, cfg->dpdk.bond_cfgs[i].up_delay);
713                 }
714 
715                 if (cfg->dpdk.bond_cfgs[i].down_delay) {
716                     sprintf(temp, "%s,down_delay=%d",
717                     temp, cfg->dpdk.bond_cfgs[i].down_delay);
718                 }
719                 dpdk_argv[n++] = strdup(temp);
720         }
721     }
722 
723     dpdk_argc = n;
724 
725     for (i=0; i<n; i++)
726         printf("%s ", dpdk_argv[i]);
727 
728     return n;
729 }
730 
731 static int
732 ff_parse_args(struct ff_config *cfg, int argc, char *const argv[])
733 {
734     int c;
735     int index = 0;
736     optind = 1;
737     while((c = getopt_long(argc, argv, short_options, long_options, &index)) != -1) {
738         switch (c) {
739             case 'c':
740                 cfg->filename = strdup(optarg);
741                 break;
742             case 'p':
743                 cfg->dpdk.proc_id = atoi(optarg);
744                 break;
745             case 't':
746                 cfg->dpdk.proc_type = strdup(optarg);
747                 break;
748             default:
749                 return -1;
750         }
751     }
752 
753     if (cfg->dpdk.proc_type == NULL) {
754         cfg->dpdk.proc_type = strdup("auto");
755     }
756 
757     if (strcmp(cfg->dpdk.proc_type, "primary") &&
758         strcmp(cfg->dpdk.proc_type, "secondary") &&
759         strcmp(cfg->dpdk.proc_type, "auto")) {
760         printf("invalid proc-type:%s\n", cfg->dpdk.proc_type);
761         return -1;
762     }
763 
764     if ((uint16_t)cfg->dpdk.proc_id > RTE_MAX_LCORE) {
765         printf("invalid proc_id:%d, use default 0\n", cfg->dpdk.proc_id);
766         cfg->dpdk.proc_id = 0;
767     }
768 
769     return 0;
770 }
771 
772 static int
773 ff_check_config(struct ff_config *cfg)
774 {
775     if(cfg->kni.enable && !cfg->kni.method) {
776         fprintf(stderr, "conf dpdk.method is necessary\n");
777         return -1;
778     }
779 
780     if(cfg->kni.method) {
781         if(strcasecmp(cfg->kni.method,"accept") &&
782             strcasecmp(cfg->kni.method,"reject")) {
783             fprintf(stderr, "conf kni.method[accept|reject] is error(%s)\n",
784                 cfg->kni.method);
785             return -1;
786         }
787     }
788 
789     #define CHECK_VALID(n) \
790         do { \
791             if (!pc->n) { \
792                 fprintf(stderr, "port%d if config error: no %s\n", \
793                     pc->port_id, #n); \
794                 return -1; \
795             } \
796         } while (0)
797 
798     int i;
799     for (i = 0; i < cfg->dpdk.nb_ports; i++) {
800         uint16_t portid = cfg->dpdk.portid_list[i];
801         struct ff_port_cfg *pc = &cfg->dpdk.port_cfgs[portid];
802         CHECK_VALID(addr);
803         CHECK_VALID(netmask);
804         CHECK_VALID(broadcast);
805         CHECK_VALID(gateway);
806         // check if the lcores in lcore_list are enabled.
807         int k;
808         for (k = 0; k < pc->nb_lcores; k++) {
809             uint16_t lcore_id = pc->lcore_list[k];
810             if (uint16_binary_search(cfg->dpdk.proc_lcore, 0,
811                                      cfg->dpdk.nb_procs-1, lcore_id) < 0) {
812                 fprintf(stderr, "lcore %d is not enabled.\n", lcore_id);
813                 return -1;
814             }
815         }
816         /*
817          * only primary process process KNI, so if KNI enabled,
818          * primary lcore must stay in every enabled ports' lcore_list
819          */
820         if (cfg->kni.enable &&
821             strcmp(cfg->dpdk.proc_type, "primary") == 0) {
822             int found = 0;
823             int j;
824             uint16_t lcore_id = cfg->dpdk.proc_lcore[cfg->dpdk.proc_id];
825             for (j = 0; j < pc->nb_lcores; j++) {
826                 if (pc->lcore_list[j] == lcore_id) {
827                     found = 1;
828                 }
829             }
830             if (! found) {
831                 fprintf(stderr,
832                          "primary lcore %d should stay in port %d's lcore_list.\n",
833                          lcore_id, pc->port_id);
834                 return -1;
835             }
836         }
837     }
838 
839     return 0;
840 }
841 
842 static void
843 ff_default_config(struct ff_config *cfg)
844 {
845     memset(cfg, 0, sizeof(struct ff_config));
846 
847     cfg->filename = DEFAULT_CONFIG_FILE;
848 
849     cfg->dpdk.proc_id = -1;
850     cfg->dpdk.numa_on = 1;
851     cfg->dpdk.promiscuous = 1;
852     cfg->dpdk.pkt_tx_delay = BURST_TX_DRAIN_US;
853 
854     cfg->freebsd.hz = 100;
855     cfg->freebsd.physmem = 1048576*256;
856     cfg->freebsd.fd_reserve = 0;
857     cfg->freebsd.mem_size = 256;
858 }
859 
860 int
861 ff_load_config(int argc, char *const argv[])
862 {
863     ff_default_config(&ff_global_cfg);
864 
865     int ret = ff_parse_args(&ff_global_cfg, argc, argv);
866     if (ret < 0) {
867         return ret;
868     }
869 
870     ret = ini_parse(ff_global_cfg.filename, ini_parse_handler,
871         &ff_global_cfg);
872     if (ret != 0) {
873         printf("parse %s failed on line %d\n", ff_global_cfg.filename, ret);
874         return -1;
875     }
876 
877     if (ff_check_config(&ff_global_cfg)) {
878         return -1;
879     }
880 
881     if (dpdk_args_setup(&ff_global_cfg) <= 0) {
882         return -1;
883     }
884 
885     return 0;
886 }
887