1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium, Inc 2017.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Cavium, Inc nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef _EVT_OPTIONS_
34 #define _EVT_OPTIONS_
35 
36 #include <stdio.h>
37 #include <stdbool.h>
38 
39 #include <rte_common.h>
40 #include <rte_eventdev.h>
41 #include <rte_lcore.h>
42 
43 #include "evt_common.h"
44 
45 #define EVT_BOOL_FMT(x)          ((x) ? "true" : "false")
46 
47 #define EVT_VERBOSE              ("verbose")
48 #define EVT_DEVICE               ("dev")
49 #define EVT_TEST                 ("test")
50 #define EVT_PROD_LCORES          ("plcores")
51 #define EVT_WORK_LCORES          ("wlcores")
52 #define EVT_NB_FLOWS             ("nb_flows")
53 #define EVT_SOCKET_ID            ("socket_id")
54 #define EVT_POOL_SZ              ("pool_sz")
55 #define EVT_WKR_DEQ_DEP          ("worker_deq_depth")
56 #define EVT_NB_PKTS              ("nb_pkts")
57 #define EVT_NB_STAGES            ("nb_stages")
58 #define EVT_SCHED_TYPE_LIST      ("stlist")
59 #define EVT_FWD_LATENCY          ("fwd_latency")
60 #define EVT_QUEUE_PRIORITY       ("queue_priority")
61 #define EVT_HELP                 ("help")
62 
63 struct evt_options {
64 #define EVT_TEST_NAME_MAX_LEN     32
65 	char test_name[EVT_TEST_NAME_MAX_LEN];
66 	bool plcores[RTE_MAX_LCORE];
67 	bool wlcores[RTE_MAX_LCORE];
68 	uint8_t sched_type_list[EVT_MAX_STAGES];
69 	uint32_t nb_flows;
70 	int socket_id;
71 	int pool_sz;
72 	int nb_stages;
73 	int verbose_level;
74 	uint64_t nb_pkts;
75 	uint16_t wkr_deq_dep;
76 	uint8_t dev_id;
77 	uint32_t fwd_latency:1;
78 	uint32_t q_priority:1;
79 };
80 
81 void evt_options_default(struct evt_options *opt);
82 int evt_options_parse(struct evt_options *opt, int argc, char **argv);
83 void evt_options_dump(struct evt_options *opt);
84 
85 /* options check helpers */
86 static inline bool
87 evt_lcores_has_overlap(bool lcores[], int lcore)
88 {
89 	if (lcores[lcore] == true) {
90 		evt_err("lcore overlaps at %d", lcore);
91 		return true;
92 	}
93 
94 	return false;
95 }
96 
97 static inline bool
98 evt_lcores_has_overlap_multi(bool lcoresx[], bool lcoresy[])
99 {
100 	int i;
101 
102 	for (i = 0; i < RTE_MAX_LCORE; i++) {
103 		if (lcoresx[i] && lcoresy[i]) {
104 			evt_err("lcores overlaps at %d", i);
105 			return true;
106 		}
107 	}
108 	return false;
109 }
110 
111 static inline bool
112 evt_has_active_lcore(bool lcores[])
113 {
114 	int i;
115 
116 	for (i = 0; i < RTE_MAX_LCORE; i++)
117 		if (lcores[i])
118 			return true;
119 	return false;
120 }
121 
122 static inline int
123 evt_nr_active_lcores(bool lcores[])
124 {
125 	int i;
126 	int c = 0;
127 
128 	for (i = 0; i < RTE_MAX_LCORE; i++)
129 		if (lcores[i])
130 			c++;
131 	return c;
132 }
133 
134 static inline int
135 evt_get_first_active_lcore(bool lcores[])
136 {
137 	int i;
138 
139 	for (i = 0; i < RTE_MAX_LCORE; i++)
140 		if (lcores[i])
141 			return i;
142 	return -1;
143 }
144 
145 static inline bool
146 evt_has_disabled_lcore(bool lcores[])
147 {
148 	int i;
149 
150 	for (i = 0; i < RTE_MAX_LCORE; i++)
151 		if ((lcores[i] == true) && !(rte_lcore_is_enabled(i)))
152 			return true;
153 	return false;
154 }
155 
156 static inline bool
157 evt_has_invalid_stage(struct evt_options *opt)
158 {
159 	if (!opt->nb_stages) {
160 		evt_err("need minimum one stage, check --stlist");
161 		return true;
162 	}
163 	if (opt->nb_stages > EVT_MAX_STAGES) {
164 		evt_err("requested changes are beyond EVT_MAX_STAGES=%d",
165 			EVT_MAX_STAGES);
166 		return true;
167 	}
168 	return false;
169 }
170 
171 static inline bool
172 evt_has_invalid_sched_type(struct evt_options *opt)
173 {
174 	int i;
175 
176 	for (i = 0; i < opt->nb_stages; i++) {
177 		if (opt->sched_type_list[i] > RTE_SCHED_TYPE_PARALLEL) {
178 			evt_err("invalid sched_type %d at %d",
179 				opt->sched_type_list[i], i);
180 			return true;
181 		}
182 	}
183 	return false;
184 }
185 
186 /* option dump helpers */
187 static inline void
188 evt_dump_worker_lcores(struct evt_options *opt)
189 {
190 	int c;
191 
192 	evt_dump_begin("worker lcores");
193 	for  (c = 0; c < RTE_MAX_LCORE; c++) {
194 		if (opt->wlcores[c])
195 			printf("%d ", c);
196 	}
197 	evt_dump_end;
198 }
199 
200 static inline void
201 evt_dump_producer_lcores(struct evt_options *opt)
202 {
203 	int c;
204 
205 	evt_dump_begin("producer lcores");
206 	for  (c = 0; c < RTE_MAX_LCORE; c++) {
207 		if (opt->plcores[c])
208 			printf("%d ", c);
209 	}
210 	evt_dump_end;
211 }
212 
213 static inline void
214 evt_dump_nb_flows(struct evt_options *opt)
215 {
216 	evt_dump("nb_flows", "%d", opt->nb_flows);
217 }
218 
219 static inline void
220 evt_dump_worker_dequeue_depth(struct evt_options *opt)
221 {
222 	evt_dump("worker deq depth", "%d", opt->wkr_deq_dep);
223 }
224 
225 static inline void
226 evt_dump_nb_stages(struct evt_options *opt)
227 {
228 	evt_dump("nb_stages", "%d", opt->nb_stages);
229 }
230 
231 static inline void
232 evt_dump_fwd_latency(struct evt_options *opt)
233 {
234 	evt_dump("fwd_latency", "%s", EVT_BOOL_FMT(opt->fwd_latency));
235 }
236 
237 static inline void
238 evt_dump_queue_priority(struct evt_options *opt)
239 {
240 	evt_dump("queue_priority", "%s", EVT_BOOL_FMT(opt->q_priority));
241 }
242 
243 static inline const char*
244 evt_sched_type_2_str(uint8_t sched_type)
245 {
246 
247 	if (sched_type == RTE_SCHED_TYPE_ORDERED)
248 		return "O";
249 	else if (sched_type == RTE_SCHED_TYPE_ATOMIC)
250 		return "A";
251 	else if (sched_type == RTE_SCHED_TYPE_PARALLEL)
252 		return "P";
253 	else
254 		return "I";
255 }
256 
257 static inline void
258 evt_dump_sched_type_list(struct evt_options *opt)
259 {
260 	int i;
261 
262 	evt_dump_begin("sched_type_list");
263 	for (i = 0; i < opt->nb_stages; i++)
264 		printf("%s ", evt_sched_type_2_str(opt->sched_type_list[i]));
265 
266 	evt_dump_end;
267 }
268 
269 #endif /* _EVT_OPTIONS_ */
270