xref: /f-stack/lib/ff_config.c (revision 37324d91)
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 ini_parse_handler(void* user, const char* section, const char* name,
407     const char* value)
408 {
409     struct ff_config *pconfig = (struct ff_config*)user;
410 
411     printf("[%s]: %s=%s\n", section, name, value);
412 
413     #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
414     if (MATCH("dpdk", "channel")) {
415         pconfig->dpdk.nb_channel = atoi(value);
416     } else if (MATCH("dpdk", "memory")) {
417         pconfig->dpdk.memory = atoi(value);
418     } else if (MATCH("dpdk", "no_huge")) {
419         pconfig->dpdk.no_huge = atoi(value);
420     } else if (MATCH("dpdk", "lcore_mask")) {
421         pconfig->dpdk.lcore_mask = strdup(value);
422         return parse_lcore_mask(pconfig, pconfig->dpdk.lcore_mask);
423     } else if (MATCH("dpdk", "base_virtaddr")) {
424         pconfig->dpdk.base_virtaddr= strdup(value);
425     } else if (MATCH("dpdk", "port_list")) {
426         return parse_port_list(pconfig, value);
427     } else if (MATCH("dpdk", "promiscuous")) {
428         pconfig->dpdk.promiscuous = atoi(value);
429     } else if (MATCH("dpdk", "numa_on")) {
430         pconfig->dpdk.numa_on = atoi(value);
431     } else if (MATCH("dpdk", "tso")) {
432         pconfig->dpdk.tso = atoi(value);
433     } else if (MATCH("dpdk", "vlan_strip")) {
434         pconfig->dpdk.vlan_strip = atoi(value);
435     } else if (MATCH("dpdk", "idle_sleep")) {
436         pconfig->dpdk.idle_sleep = atoi(value);
437     } else if (MATCH("kni", "enable")) {
438         pconfig->kni.enable= atoi(value);
439     } else if (MATCH("kni", "method")) {
440         pconfig->kni.method= strdup(value);
441     } else if (MATCH("kni", "tcp_port")) {
442         pconfig->kni.tcp_port = strdup(value);
443     } else if (MATCH("kni", "udp_port")) {
444         pconfig->kni.udp_port= strdup(value);
445     } else if (strcmp(section, "freebsd.boot") == 0) {
446         if (strcmp(name, "hz") == 0) {
447             pconfig->freebsd.hz = atoi(value);
448         } else if (strcmp(name, "physmem") == 0) {
449             pconfig->freebsd.physmem = atol(value);
450         } else if (strcmp(name, "fd_reserve") == 0) {
451             pconfig->freebsd.fd_reserve = atoi(value);
452         } else {
453             return freebsd_conf_handler(pconfig, "boot", name, value);
454         }
455     } else if (strcmp(section, "freebsd.sysctl") == 0) {
456         return freebsd_conf_handler(pconfig, "sysctl", name, value);
457     } else if (strncmp(section, "port", 4) == 0) {
458         return port_cfg_handler(pconfig, section, name, value);
459     }
460 
461     return 1;
462 }
463 
464 static int
465 dpdk_args_setup(struct ff_config *cfg)
466 {
467     int n = 0, i;
468     dpdk_argv[n++] = strdup("f-stack");
469     char temp[DPDK_CONFIG_MAXLEN] = {0};
470 
471     if (cfg->dpdk.no_huge) {
472         dpdk_argv[n++] = strdup("--no-huge");
473     }
474     if (cfg->dpdk.proc_mask) {
475         sprintf(temp, "-c%s", cfg->dpdk.proc_mask);
476         dpdk_argv[n++] = strdup(temp);
477     }
478     if (cfg->dpdk.nb_channel) {
479         sprintf(temp, "-n%d", cfg->dpdk.nb_channel);
480         dpdk_argv[n++] = strdup(temp);
481     }
482     if (cfg->dpdk.memory) {
483         sprintf(temp, "-m%d", cfg->dpdk.memory);
484         dpdk_argv[n++] = strdup(temp);
485     }
486     if (cfg->dpdk.proc_type) {
487         sprintf(temp, "--proc-type=%s", cfg->dpdk.proc_type);
488         dpdk_argv[n++] = strdup(temp);
489     }
490     if (cfg->dpdk.base_virtaddr) {
491         sprintf(temp, "--base-virtaddr=%s", cfg->dpdk.base_virtaddr);
492         dpdk_argv[n++] = strdup(temp);
493     }
494 
495     dpdk_argc = n;
496 
497     return n;
498 }
499 
500 static int
501 ff_parse_args(struct ff_config *cfg, int argc, char *const argv[])
502 {
503     int c;
504     int index = 0;
505     optind = 1;
506     while((c = getopt_long(argc, argv, short_options, long_options, &index)) != -1) {
507         switch (c) {
508             case 'c':
509                 cfg->filename = strdup(optarg);
510                 break;
511             case 'p':
512                 cfg->dpdk.proc_id = atoi(optarg);
513                 break;
514             case 't':
515                 cfg->dpdk.proc_type = strdup(optarg);
516                 break;
517             default:
518                 return -1;
519         }
520     }
521 
522     if (cfg->dpdk.proc_type == NULL) {
523         cfg->dpdk.proc_type = strdup("auto");
524     }
525 
526     if (strcmp(cfg->dpdk.proc_type, "primary") &&
527         strcmp(cfg->dpdk.proc_type, "secondary") &&
528         strcmp(cfg->dpdk.proc_type, "auto")) {
529         printf("invalid proc-type:%s\n", cfg->dpdk.proc_type);
530         return -1;
531     }
532 
533     if ((uint16_t)cfg->dpdk.proc_id > RTE_MAX_LCORE) {
534         printf("invalid proc_id:%d, use default 0\n", cfg->dpdk.proc_id);
535         cfg->dpdk.proc_id = 0;
536     }
537 
538     return 0;
539 }
540 
541 static int
542 ff_check_config(struct ff_config *cfg)
543 {
544     if(cfg->kni.enable && !cfg->kni.method) {
545         fprintf(stderr, "conf dpdk.method is necessary\n");
546         return -1;
547     }
548 
549     if(cfg->kni.method) {
550         if(strcasecmp(cfg->kni.method,"accept") &&
551             strcasecmp(cfg->kni.method,"reject")) {
552             fprintf(stderr, "conf kni.method[accept|reject] is error(%s)\n",
553                 cfg->kni.method);
554             return -1;
555         }
556     }
557 
558     #define CHECK_VALID(n) \
559         do { \
560             if (!pc->n) { \
561                 fprintf(stderr, "port%d if config error: no %s\n", \
562                     pc->port_id, #n); \
563                 return -1; \
564             } \
565         } while (0)
566 
567     int i;
568     for (i = 0; i < cfg->dpdk.nb_ports; i++) {
569         uint16_t portid = cfg->dpdk.portid_list[i];
570         struct ff_port_cfg *pc = &cfg->dpdk.port_cfgs[portid];
571         CHECK_VALID(addr);
572         CHECK_VALID(netmask);
573         CHECK_VALID(broadcast);
574         CHECK_VALID(gateway);
575         // check if the lcores in lcore_list are enabled.
576         int k;
577         for (k = 0; k < pc->nb_lcores; k++) {
578             uint16_t lcore_id = pc->lcore_list[k];
579             if (uint16_binary_search(cfg->dpdk.proc_lcore, 0,
580                                      cfg->dpdk.nb_procs-1, lcore_id) < 0) {
581                 fprintf(stderr, "lcore %d is not enabled.\n", lcore_id);
582                 return -1;
583             }
584         }
585         /*
586          * only primary process process KNI, so if KNI enabled,
587          * primary lcore must stay in every enabled ports' lcore_list
588          */
589         if (cfg->kni.enable &&
590             strcmp(cfg->dpdk.proc_type, "primary") == 0) {
591             int found = 0;
592             int j;
593             uint16_t lcore_id = cfg->dpdk.proc_lcore[cfg->dpdk.proc_id];
594             for (j = 0; j < pc->nb_lcores; j++) {
595                 if (pc->lcore_list[j] == lcore_id) {
596                     found = 1;
597                 }
598             }
599             if (! found) {
600                 fprintf(stderr,
601                          "primary lcore %d should stay in port %d's lcore_list.\n",
602                          lcore_id, pc->port_id);
603                 return -1;
604             }
605         }
606     }
607 
608     return 0;
609 }
610 
611 static void
612 ff_default_config(struct ff_config *cfg)
613 {
614     memset(cfg, 0, sizeof(struct ff_config));
615 
616     cfg->filename = DEFAULT_CONFIG_FILE;
617 
618     cfg->dpdk.proc_id = -1;
619     cfg->dpdk.numa_on = 1;
620     cfg->dpdk.promiscuous = 1;
621 
622     cfg->freebsd.hz = 100;
623     cfg->freebsd.physmem = 1048576*256;
624     cfg->freebsd.fd_reserve = 0;
625 }
626 
627 int
628 ff_load_config(int argc, char *const argv[])
629 {
630     ff_default_config(&ff_global_cfg);
631 
632     int ret = ff_parse_args(&ff_global_cfg, argc, argv);
633     if (ret < 0) {
634         return ret;
635     }
636 
637     ret = ini_parse(ff_global_cfg.filename, ini_parse_handler,
638         &ff_global_cfg);
639     if (ret != 0) {
640         printf("parse %s failed on line %d\n", ff_global_cfg.filename, ret);
641         return -1;
642     }
643 
644     if (ff_check_config(&ff_global_cfg)) {
645         return -1;
646     }
647 
648     if (dpdk_args_setup(&ff_global_cfg) <= 0) {
649         return -1;
650     }
651 
652     return 0;
653 }
654