b28729fd267790f5bea065589f6073fb8fd2c1c0
[dpdk.git] / examples / qos_sched / args.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <locale.h>
40 #include <unistd.h>
41 #include <limits.h>
42 #include <getopt.h>
43
44 #include <rte_log.h>
45 #include <rte_eal.h>
46 #include <rte_lcore.h>
47 #include <rte_string_fns.h>
48
49 #include "main.h"
50
51 #define APP_NAME "qos_sched"
52 #define MAX_OPT_VALUES 8
53 #define SYS_CPU_DIR "/sys/devices/system/cpu/cpu%u/topology/"
54
55 static uint32_t app_master_core = 1;
56 static uint32_t app_numa_mask;
57 static uint64_t app_used_core_mask = 0;
58 static uint64_t app_used_port_mask = 0;
59 static uint64_t app_used_rx_port_mask = 0;
60 static uint64_t app_used_tx_port_mask = 0;
61
62
63 static const char usage[] =
64         "                                                                               \n"
65         "    %s <APP PARAMS>                                                            \n"
66         "                                                                               \n"
67         "Application mandatory parameters:                                              \n"
68         "    --pfc \"RX PORT, TX PORT, RX LCORE, WT LCORE\" : Packet flow configuration \n"
69         "           multiple pfc can be configured in command line                      \n"
70         "                                                                               \n"
71         "Application optional parameters:                                               \n"
72         "    --mst I : master core index (default value is %u)                          \n" 
73         "    --rsz \"A, B, C\" :   Ring sizes                                           \n"
74         "           A = Size (in number of buffer descriptors) of each of the NIC RX    \n"
75         "               rings read by the I/O RX lcores (default value is %u)           \n"
76         "           B = Size (in number of elements) of each of the SW rings used by the\n"
77         "               I/O RX lcores to send packets to worker lcores (default value is\n"
78         "               %u)                                                             \n"
79         "           C = Size (in number of buffer descriptors) of each of the NIC TX    \n"
80         "               rings written by worker lcores (default value is %u)            \n"
81         "    --bsz \"A, B, C, D\": Burst sizes                                          \n"
82         "           A = I/O RX lcore read burst size from NIC RX (default value is %u)  \n"
83         "           B = I/O RX lcore write burst size to output SW rings,               \n"
84         "               Worker lcore read burst size from input SW rings,               \n"
85         "               QoS enqueue size (default value is %u)                          \n"
86         "           C = QoS dequeue size (default value is %u)                          \n"
87         "           D = Worker lcore write burst size to NIC TX (default value is %u)   \n"
88         "    --msz M : Mempool size (in number of mbufs) for each pfc (default %u)      \n"
89         "    --rth \"A, B, C\" :   RX queue threshold parameters                        \n"
90         "           A = RX prefetch threshold (default value is %u)                     \n"
91         "           B = RX host threshold (default value is %u)                         \n"
92         "           C = RX write-back threshold (default value is %u)                   \n"
93         "    --tth \"A, B, C\" :   TX queue threshold parameters                        \n"
94         "           A = TX prefetch threshold (default value is %u)                     \n"
95         "           B = TX host threshold (default value is %u)                         \n"
96         "           C = TX write-back threshold (default value is %u)                   \n"
97         "    --cfg FILE : profile configuration to load                                 \n"
98 ;
99
100 /* display usage */
101 static void
102 app_usage(const char *prgname)
103 {
104         printf(usage, prgname, app_master_core,
105                 APP_RX_DESC_DEFAULT, APP_RING_SIZE, APP_TX_DESC_DEFAULT,
106                 MAX_PKT_RX_BURST, PKT_ENQUEUE, PKT_DEQUEUE,
107                 MAX_PKT_TX_BURST, NB_MBUF,
108                 RX_PTHRESH, RX_HTHRESH, RX_WTHRESH,
109                 TX_PTHRESH, TX_HTHRESH, TX_WTHRESH
110                 );
111 }
112
113 static inline int str_is(const char *str, const char *is)
114 {
115         return (strcmp(str, is) == 0);
116 }
117
118 /* returns core mask used by DPDK */
119 static uint64_t
120 app_eal_core_mask(void)
121 {
122         uint32_t i;
123         uint64_t cm = 0;
124         struct rte_config *cfg = rte_eal_get_configuration();
125
126         for (i = 0; i < RTE_MAX_LCORE; i++) {
127                 if (cfg->lcore_role[i] == ROLE_RTE)
128                         cm |= (1ULL << i);
129         }
130
131         cm |= (1ULL << cfg->master_lcore);
132
133         return cm;
134 }
135
136
137 /* returns total number of cores presented in a system */
138 static uint32_t
139 app_cpu_core_count(void)
140 {
141         int i, len;
142         char path[PATH_MAX];
143         uint32_t ncores = 0;
144
145         for(i = 0; i < RTE_MAX_LCORE; i++) {
146                 len = rte_snprintf(path, sizeof(path), SYS_CPU_DIR, i);
147                 if (len <= 0 || (unsigned)len >= sizeof(path))
148                         continue;
149
150                 if (access(path, F_OK) == 0)
151                         ncores++;
152         }
153
154         return ncores;
155 }
156
157 /* returns:
158          number of values parsed
159         -1 in case of error
160 */
161 static int
162 app_parse_opt_vals(const char *conf_str, char separator, uint32_t n_vals, uint32_t *opt_vals)
163 {
164         char *string;
165         uint32_t i, n_tokens;
166         char *tokens[MAX_OPT_VALUES];
167
168         if (conf_str == NULL || opt_vals == NULL || n_vals == 0 || n_vals > MAX_OPT_VALUES)
169                 return -1;
170
171         /* duplicate configuration string before splitting it to tokens */
172         string = strdup(conf_str);
173         if (string == NULL)
174                 return -1;
175
176         n_tokens = rte_strsplit(string, strnlen(string, 32), tokens, n_vals, separator);
177
178         for(i = 0; i < n_tokens; i++) {
179                 opt_vals[i] = (uint32_t)atol(tokens[i]);
180         }
181         
182         free(string);
183
184         return n_tokens;
185 }
186
187 static int
188 app_parse_ring_conf(const char *conf_str)
189 {
190         int ret;
191         uint32_t vals[3];
192
193         ret = app_parse_opt_vals(conf_str, ',', 3, vals);
194         if (ret != 3)   
195                 return ret;
196
197         ring_conf.rx_size = vals[0];
198         ring_conf.ring_size = vals[1];
199         ring_conf.tx_size = vals[2];
200
201         return 0;
202 }
203
204 static int
205 app_parse_rth_conf(const char *conf_str)
206 {
207         int ret;
208         uint32_t vals[3];
209
210         ret = app_parse_opt_vals(conf_str, ',', 3, vals);
211         if (ret != 3)   
212                 return ret;
213
214         rx_thresh.pthresh = (uint8_t)vals[0];
215         rx_thresh.hthresh = (uint8_t)vals[1];
216         rx_thresh.wthresh = (uint8_t)vals[2];
217
218         return 0;
219 }
220
221 static int
222 app_parse_tth_conf(const char *conf_str)
223 {
224         int ret;
225         uint32_t vals[3];
226
227         ret = app_parse_opt_vals(conf_str, ',', 3, vals);
228         if (ret != 3)   
229                 return ret;
230
231         tx_thresh.pthresh = (uint8_t)vals[0];
232         tx_thresh.hthresh = (uint8_t)vals[1];
233         tx_thresh.wthresh = (uint8_t)vals[2];
234
235         return 0;
236 }
237
238 static int
239 app_parse_flow_conf(const char *conf_str)
240 {
241         int ret;
242         uint32_t vals[5];
243         struct flow_conf *pconf;
244         uint64_t mask;
245
246         ret = app_parse_opt_vals(conf_str, ',', 6, vals);
247         if (ret < 4 || ret > 5)
248                 return ret;
249
250         pconf = &qos_conf[nb_pfc];
251
252         pconf->rx_port = (uint8_t)vals[0];
253         pconf->tx_port = (uint8_t)vals[1];
254         pconf->rx_core = (uint8_t)vals[2];
255         pconf->wt_core = (uint8_t)vals[3];
256         if (ret == 5)
257                 pconf->tx_core = (uint8_t)vals[4];
258         else
259                 pconf->tx_core = pconf->wt_core;
260
261         if (pconf->rx_core == pconf->wt_core) {
262                 RTE_LOG(ERR, APP, "pfc %u: rx thread and worker thread cannot share same core\n", nb_pfc);
263                 return -1;
264         }
265
266         if (pconf->rx_port >= RTE_MAX_ETHPORTS) {
267                 RTE_LOG(ERR, APP, "pfc %u: invalid rx port %hu index\n", nb_pfc, pconf->rx_port);
268                 return -1;
269         }
270         if (pconf->tx_port >= RTE_MAX_ETHPORTS) {
271                 RTE_LOG(ERR, APP, "pfc %u: invalid tx port %hu index\n", nb_pfc, pconf->rx_port);
272                 return -1;
273         }
274
275         mask = 1lu << pconf->rx_port;
276         if (app_used_rx_port_mask & mask) {
277                 RTE_LOG(ERR, APP, "pfc %u: rx port %hu is used already\n", nb_pfc, pconf->rx_port);
278                 return -1;
279         }
280         app_used_rx_port_mask |= mask;
281         app_used_port_mask |= mask;
282
283         mask = 1lu << pconf->tx_port;
284         if (app_used_tx_port_mask & mask) {
285                 RTE_LOG(ERR, APP, "pfc %u: port %hu is used already\n", nb_pfc, pconf->tx_port);
286                 return -1;
287         }
288         app_used_tx_port_mask |= mask;
289         app_used_port_mask |= mask;
290
291         mask = 1lu << pconf->rx_core;
292         app_used_core_mask |= mask;
293
294         mask = 1lu << pconf->wt_core;
295         app_used_core_mask |= mask;
296
297         mask = 1lu << pconf->tx_core;
298         app_used_core_mask |= mask;
299
300         nb_pfc++;
301
302         return 0;
303 }
304
305 static int
306 app_parse_burst_conf(const char *conf_str)
307 {
308         int ret;
309         uint32_t vals[4];
310
311         ret = app_parse_opt_vals(conf_str, ',', 4, vals);
312         if (ret != 4)
313                 return ret;
314
315         burst_conf.rx_burst    = (uint16_t)vals[0];
316         burst_conf.ring_burst  = (uint16_t)vals[1];
317         burst_conf.qos_dequeue = (uint16_t)vals[2];
318         burst_conf.tx_burst    = (uint16_t)vals[3];
319
320         return 0;
321 }
322
323 /* 
324  * Parses the argument given in the command line of the application,
325  * calculates mask for used cores and initializes EAL with calculated core mask
326  */
327 int
328 app_parse_args(int argc, char **argv)
329 {
330         int opt, ret;
331         int option_index;
332         const char *optname;
333         char *prgname = argv[0];
334         uint32_t i, nb_lcores;
335
336         static struct option lgopts[] = {
337                 { "pfc", 1, 0, 0 },
338                 { "mst", 1, 0, 0 },
339                 { "rsz", 1, 0, 0 },
340                 { "bsz", 1, 0, 0 },
341                 { "msz", 1, 0, 0 },
342                 { "rth", 1, 0, 0 },
343                 { "tth", 1, 0, 0 },
344                 { "cfg", 1, 0, 0 },
345                 { NULL,  0, 0, 0 }
346         };
347
348         /* initialize EAL first */
349         ret = rte_eal_init(argc, argv);
350         if (ret < 0)
351                 return -1;
352
353         argc -= ret;
354         argv += ret;
355
356         /* set en_US locale to print big numbers with ',' */
357         setlocale(LC_NUMERIC, "en_US.utf-8");
358
359         while ((opt = getopt_long(argc, argv, "",
360                 lgopts, &option_index)) != EOF) {
361
362                         switch (opt) {
363                         /* long options */
364                         case 0:
365                                 optname = lgopts[option_index].name;
366                                 if (str_is(optname, "pfc")) {
367                                         ret = app_parse_flow_conf(optarg);
368                                         if (ret) {
369                                                 RTE_LOG(ERR, APP, "Invalid pipe configuration %s\n", optarg);
370                                                 return -1;
371                                         }
372                                         break;
373                                 }
374                                 if (str_is(optname, "mst")) {
375                                         app_master_core = (uint32_t)atoi(optarg);
376                                         break;
377                                 }
378                                 if (str_is(optname, "rsz")) {
379                                         ret = app_parse_ring_conf(optarg);
380                                         if (ret) {
381                                                 RTE_LOG(ERR, APP, "Invalid ring configuration %s\n", optarg);
382                                                 return -1;
383                                         }
384                                         break;
385                                 }
386                                 if (str_is(optname, "bsz")) {
387                                         ret = app_parse_burst_conf(optarg);
388                                         if (ret) {
389                                                 RTE_LOG(ERR, APP, "Invalid burst configuration %s\n", optarg);
390                                                 return -1;
391                                         }
392                                         break;
393                                 }
394                                 if (str_is(optname, "msz")) {
395                                         mp_size = atoi(optarg);
396                                         if (mp_size <= 0) {
397                                                 RTE_LOG(ERR, APP, "Invalid mempool size %s\n", optarg);
398                                                 return -1;
399                                         }
400                                         break;
401                                 }
402                                 if (str_is(optname, "rth")) {
403                                         ret = app_parse_rth_conf(optarg);
404                                         if (ret) {
405                                                 RTE_LOG(ERR, APP, "Invalid RX threshold configuration %s\n", optarg);
406                                                 return -1;
407                                         }
408                                         break;
409                                 }
410                                 if (str_is(optname, "tth")) {
411                                         ret = app_parse_tth_conf(optarg);
412                                         if (ret) {
413                                                 RTE_LOG(ERR, APP, "Invalid TX threshold configuration %s\n", optarg);
414                                                 return -1;
415                                         }
416                                         break;
417                                 }
418                                 if (str_is(optname, "cfg")) {
419                                         cfg_profile = optarg;
420                                         break;
421                                 }
422                                 break;
423
424                         default:
425                                 app_usage(prgname);
426                                 return -1;
427                         }
428         }
429
430         /* check master core index validity */
431         for(i = 0; i <= app_master_core; i++) {
432                 if (app_used_core_mask & (1u << app_master_core)) {
433                         RTE_LOG(ERR, APP, "Master core index is not configured properly\n");
434                         app_usage(prgname);
435                         return -1;
436                 }
437         }
438         app_used_core_mask |= 1u << app_master_core;
439
440         if ((app_used_core_mask != app_eal_core_mask()) ||
441                         (app_master_core != rte_get_master_lcore())) {
442                 RTE_LOG(ERR, APP, "EAL core mask not configured properly, must be %" PRIx64
443                                 " instead of %" PRIx64 "\n" , app_used_core_mask, app_eal_core_mask());
444                 return -1;
445         }
446
447         if (nb_pfc == 0) {
448                 RTE_LOG(ERR, APP, "Packet flow not configured!\n");
449                 app_usage(prgname);
450                 return -1;
451         }
452
453         /* sanity check for cores assignment */
454         nb_lcores = app_cpu_core_count();
455
456         for(i = 0; i < nb_pfc; i++) {
457                 if (qos_conf[i].rx_core >= nb_lcores) {
458                         RTE_LOG(ERR, APP, "pfc %u: invalid RX lcore index %u\n", i + 1,
459                                         qos_conf[i].rx_core);
460                         return -1;
461                 }
462                 if (qos_conf[i].wt_core >= nb_lcores) {
463                         RTE_LOG(ERR, APP, "pfc %u: invalid WT lcore index %u\n", i + 1,
464                                         qos_conf[i].wt_core);
465                         return -1;
466                 }
467                 uint32_t rx_sock = rte_lcore_to_socket_id(qos_conf[i].rx_core);
468                 uint32_t wt_sock = rte_lcore_to_socket_id(qos_conf[i].wt_core);
469                 if (rx_sock != wt_sock) {
470                         RTE_LOG(ERR, APP, "pfc %u: RX and WT must be on the same socket\n", i + 1);
471                         return -1;
472                 }
473                 app_numa_mask |= 1 << rte_lcore_to_socket_id(qos_conf[i].rx_core);
474         }
475
476         return 0;
477 }
478