sched: initial import
[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         "    --rth \"A, B, C\" :   RX queue threshold parameters                        \n"
89         "           A = RX prefetch threshold (default value is %u)                     \n"
90         "           B = RX host threshold (default value is %u)                         \n"
91         "           C = RX write-back threshold (default value is %u)                   \n"
92         "    --tth \"A, B, C\" :   TX queue threshold parameters                        \n"
93         "           A = TX prefetch threshold (default value is %u)                     \n"
94         "           B = TX host threshold (default value is %u)                         \n"
95         "           C = TX write-back threshold (default value is %u)                   \n"
96         "    --cfg FILE : profile configuration to load                                 \n"
97 ;
98
99 /* display usage */
100 static void
101 app_usage(const char *prgname)
102 {
103         printf(usage, prgname, app_master_core,
104                 APP_RX_DESC_DEFAULT, APP_RING_SIZE, APP_TX_DESC_DEFAULT,
105                 MAX_PKT_RX_BURST, PKT_ENQUEUE, PKT_DEQUEUE, MAX_PKT_TX_BURST,
106                 RX_PTHRESH, RX_HTHRESH, RX_WTHRESH,
107                 TX_PTHRESH, TX_HTHRESH, TX_WTHRESH
108                 );
109 }
110
111 static inline int str_is(const char *str, const char *is)
112 {
113         return (strcmp(str, is) == 0);
114 }
115
116 /* returns core mask used by DPDK */
117 static uint64_t
118 app_eal_core_mask(void)
119 {
120         uint32_t i;
121         uint64_t cm = 0;
122         struct rte_config *cfg = rte_eal_get_configuration();
123
124         for (i = 0; i < RTE_MAX_LCORE; i++) {
125                 if (cfg->lcore_role[i] == ROLE_RTE)
126                         cm |= (1ULL << i);
127         }
128
129         cm |= (1ULL << cfg->master_lcore);
130
131         return cm;
132 }
133
134
135 /* returns total number of cores presented in a system */
136 static uint32_t
137 app_cpu_core_count(void)
138 {
139         int i, len;
140         char path[PATH_MAX];
141         uint32_t ncores = 0;
142
143         for(i = 0; i < RTE_MAX_LCORE; i++) {
144                 len = rte_snprintf(path, sizeof(path), SYS_CPU_DIR, i);
145                 if (len <= 0 || (unsigned)len >= sizeof(path))
146                         continue;
147
148                 if (access(path, F_OK) == 0)
149                         ncores++;
150         }
151
152         return ncores;
153 }
154
155 /* returns:
156          number of values parsed
157         -1 in case of error
158 */
159 static int
160 app_parse_opt_vals(const char *conf_str, char separator, uint32_t n_vals, uint32_t *opt_vals)
161 {
162         char *string;
163         uint32_t i, n_tokens;
164         char *tokens[MAX_OPT_VALUES];
165
166         if (conf_str == NULL || opt_vals == NULL || n_vals == 0 || n_vals > MAX_OPT_VALUES)
167                 return -1;
168
169         /* duplicate configuration string before splitting it to tokens */
170         string = strdup(conf_str);
171         if (string == NULL)
172                 return -1;
173
174         n_tokens = rte_strsplit(string, strnlen(string, 32), tokens, n_vals, separator);
175
176         for(i = 0; i < n_tokens; i++) {
177                 opt_vals[i] = (uint32_t)atol(tokens[i]);
178         }
179         
180         free(string);
181
182         return n_tokens;
183 }
184
185 static int
186 app_parse_ring_conf(const char *conf_str)
187 {
188         int ret;
189         uint32_t vals[3];
190
191         ret = app_parse_opt_vals(conf_str, ',', 3, vals);
192         if (ret != 3)   
193                 return ret;
194
195         ring_conf.rx_size = vals[0];
196         ring_conf.ring_size = vals[1];
197         ring_conf.tx_size = vals[2];
198
199         return 0;
200 }
201
202 static int
203 app_parse_rth_conf(const char *conf_str)
204 {
205         int ret;
206         uint32_t vals[3];
207
208         ret = app_parse_opt_vals(conf_str, ',', 3, vals);
209         if (ret != 3)   
210                 return ret;
211
212         rx_thresh.pthresh = (uint8_t)vals[0];
213         rx_thresh.hthresh = (uint8_t)vals[1];
214         rx_thresh.wthresh = (uint8_t)vals[2];
215
216         return 0;
217 }
218
219 static int
220 app_parse_tth_conf(const char *conf_str)
221 {
222         int ret;
223         uint32_t vals[3];
224
225         ret = app_parse_opt_vals(conf_str, ',', 3, vals);
226         if (ret != 3)   
227                 return ret;
228
229         tx_thresh.pthresh = (uint8_t)vals[0];
230         tx_thresh.hthresh = (uint8_t)vals[1];
231         tx_thresh.wthresh = (uint8_t)vals[2];
232
233         return 0;
234 }
235
236 static int
237 app_parse_flow_conf(const char *conf_str)
238 {
239         int ret;
240         uint32_t vals[5];
241         struct flow_conf *pconf;
242         uint64_t mask;
243
244         ret = app_parse_opt_vals(conf_str, ',', 6, vals);
245         if (ret < 4 || ret > 5)
246                 return ret;
247
248         pconf = &qos_conf[nb_pfc];
249
250         pconf->rx_port = (uint8_t)vals[0];
251         pconf->tx_port = (uint8_t)vals[1];
252         pconf->rx_core = (uint8_t)vals[2];
253         pconf->wt_core = (uint8_t)vals[3];
254         if (ret == 5)
255                 pconf->tx_core = (uint8_t)vals[4];
256         else
257                 pconf->tx_core = pconf->wt_core;
258
259         if (pconf->rx_core == pconf->wt_core) {
260                 RTE_LOG(ERR, APP, "pfc %u: rx thread and worker thread cannot share same core\n", nb_pfc);
261                 return -1;
262         }
263
264         if (pconf->rx_port >= RTE_MAX_ETHPORTS) {
265                 RTE_LOG(ERR, APP, "pfc %u: invalid rx port %hu index\n", nb_pfc, pconf->rx_port);
266                 return -1;
267         }
268         if (pconf->tx_port >= RTE_MAX_ETHPORTS) {
269                 RTE_LOG(ERR, APP, "pfc %u: invalid tx port %hu index\n", nb_pfc, pconf->rx_port);
270                 return -1;
271         }
272
273         mask = 1lu << pconf->rx_port;
274         if (app_used_rx_port_mask & mask) {
275                 RTE_LOG(ERR, APP, "pfc %u: rx port %hu is used already\n", nb_pfc, pconf->rx_port);
276                 return -1;
277         }
278         app_used_rx_port_mask |= mask;
279         app_used_port_mask |= mask;
280
281         mask = 1lu << pconf->tx_port;
282         if (app_used_tx_port_mask & mask) {
283                 RTE_LOG(ERR, APP, "pfc %u: port %hu is used already\n", nb_pfc, pconf->tx_port);
284                 return -1;
285         }
286         app_used_tx_port_mask |= mask;
287         app_used_port_mask |= mask;
288
289         mask = 1lu << pconf->rx_core;
290         app_used_core_mask |= mask;
291
292         mask = 1lu << pconf->wt_core;
293         app_used_core_mask |= mask;
294
295         mask = 1lu << pconf->tx_core;
296         app_used_core_mask |= mask;
297
298         nb_pfc++;
299
300         return 0;
301 }
302
303 static int
304 app_parse_burst_conf(const char *conf_str)
305 {
306         int ret;
307         uint32_t vals[4];
308
309         ret = app_parse_opt_vals(conf_str, ',', 4, vals);
310         if (ret != 4)
311                 return ret;
312
313         burst_conf.rx_burst    = (uint16_t)vals[0];
314         burst_conf.ring_burst  = (uint16_t)vals[1];
315         burst_conf.qos_dequeue = (uint16_t)vals[2];
316         burst_conf.tx_burst    = (uint16_t)vals[3];
317
318         return 0;
319 }
320
321 /* 
322  * Parses the argument given in the command line of the application,
323  * calculates mask for used cores and initializes EAL with calculated core mask
324  */
325 int
326 app_parse_args(int argc, char **argv)
327 {
328         int opt, ret;
329         int option_index;
330         const char *optname;
331         char *prgname = argv[0];
332         uint32_t i, nb_lcores;
333
334         static struct option lgopts[] = {
335                 { "pfc", 1, 0, 0 },
336                 { "mst", 1, 0, 0 },
337                 { "rsz", 1, 0, 0 },
338                 { "bsz", 1, 0, 0 },
339                 { "rth", 1, 0, 0 },
340                 { "tth", 1, 0, 0 },
341                 { "cfg", 1, 0, 0 },
342                 { NULL,  0, 0, 0 }
343         };
344
345         /* initialize EAL first */
346         ret = rte_eal_init(argc, argv);
347         if (ret < 0)
348                 return -1;
349
350         argc -= ret;
351         argv += ret;
352
353         /* set en_US locale to print big numbers with ',' */
354         setlocale(LC_NUMERIC, "en_US.utf-8");
355
356         while ((opt = getopt_long(argc, argv, "",
357                 lgopts, &option_index)) != EOF) {
358
359                         switch (opt) {
360                         /* long options */
361                         case 0:
362                                 optname = lgopts[option_index].name;
363                                 if (str_is(optname, "pfc")) {
364                                         ret = app_parse_flow_conf(optarg);
365                                         if (ret) {
366                                                 RTE_LOG(ERR, APP, "Invalid pipe configuration %s\n", optarg);
367                                                 return -1;
368                                         }
369                                         break;
370                                 }
371                                 if (str_is(optname, "mst")) {
372                                         app_master_core = (uint32_t)atoi(optarg);
373                                         break;
374                                 }
375                                 if (str_is(optname, "rsz")) {
376                                         ret = app_parse_ring_conf(optarg);
377                                         if (ret) {
378                                                 RTE_LOG(ERR, APP, "Invalid ring configuration %s\n", optarg);
379                                                 return -1;
380                                         }
381                                         break;
382                                 }
383                                 if (str_is(optname, "bsz")) {
384                                         ret = app_parse_burst_conf(optarg);
385                                         if (ret) {
386                                                 RTE_LOG(ERR, APP, "Invalid burst configuration %s\n", optarg);
387                                                 return -1;
388                                         }
389                                         break;
390                                 }
391                                 if (str_is(optname, "rth")) {
392                                         ret = app_parse_rth_conf(optarg);
393                                         if (ret) {
394                                                 RTE_LOG(ERR, APP, "Invalid RX threshold configuration %s\n", optarg);
395                                                 return -1;
396                                         }
397                                         break;
398                                 }
399                                 if (str_is(optname, "tth")) {
400                                         ret = app_parse_tth_conf(optarg);
401                                         if (ret) {
402                                                 RTE_LOG(ERR, APP, "Invalid TX threshold configuration %s\n", optarg);
403                                                 return -1;
404                                         }
405                                         break;
406                                 }
407                                 if (str_is(optname, "cfg")) {
408                                         cfg_profile = optarg;
409                                         break;
410                                 }
411                                 break;
412
413                         default:
414                                 app_usage(prgname);
415                                 return -1;
416                         }
417         }
418
419         /* check master core index validity */
420         for(i = 0; i <= app_master_core; i++) {
421                 if (app_used_core_mask & (1u << app_master_core)) {
422                         RTE_LOG(ERR, APP, "Master core index is not configured properly\n");
423                         app_usage(prgname);
424                         return -1;
425                 }
426         }
427         app_used_core_mask |= 1u << app_master_core;
428
429         if ((app_used_core_mask != app_eal_core_mask()) ||
430                         (app_master_core != rte_get_master_lcore())) {
431                 RTE_LOG(ERR, APP, "EAL core mask not configured properly, must be %" PRIx64
432                                 " instead of %" PRIx64 "\n" , app_used_core_mask, app_eal_core_mask());
433                 return -1;
434         }
435
436         if (nb_pfc == 0) {
437                 RTE_LOG(ERR, APP, "Packet flow not configured!\n");
438                 app_usage(prgname);
439                 return -1;
440         }
441
442         /* sanity check for cores assignment */
443         nb_lcores = app_cpu_core_count();
444
445         for(i = 0; i < nb_pfc; i++) {
446                 if (qos_conf[i].rx_core >= nb_lcores) {
447                         RTE_LOG(ERR, APP, "pfc %u: invalid RX lcore index %u\n", i + 1,
448                                         qos_conf[i].rx_core);
449                         return -1;
450                 }
451                 if (qos_conf[i].wt_core >= nb_lcores) {
452                         RTE_LOG(ERR, APP, "pfc %u: invalid WT lcore index %u\n", i + 1,
453                                         qos_conf[i].wt_core);
454                         return -1;
455                 }
456                 uint32_t rx_sock = rte_lcore_to_socket_id(qos_conf[i].rx_core);
457                 uint32_t wt_sock = rte_lcore_to_socket_id(qos_conf[i].wt_core);
458                 if (rx_sock != wt_sock) {
459                         RTE_LOG(ERR, APP, "pfc %u: RX and WT must be on the same socket\n", i + 1);
460                         return -1;
461                 }
462                 app_numa_mask |= 1 << rte_lcore_to_socket_id(qos_conf[i].rx_core);
463         }
464
465         return 0;
466 }
467