app/testeventdev: add order basic functions
[dpdk.git] / app / test-eventdev / test_order_common.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium 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 networks 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 #include "test_order_common.h"
34
35 int
36 order_test_result(struct evt_test *test, struct evt_options *opt)
37 {
38         RTE_SET_USED(opt);
39         struct test_order *t = evt_test_priv(test);
40
41         return t->result;
42 }
43
44 int
45 order_opt_check(struct evt_options *opt)
46 {
47         /* 1 producer + N workers + 1 master */
48         if (rte_lcore_count() < 3) {
49                 evt_err("test need minimum 3 lcores");
50                 return -1;
51         }
52
53         /* Validate worker lcores */
54         if (evt_lcores_has_overlap(opt->wlcores, rte_get_master_lcore())) {
55                 evt_err("worker lcores overlaps with master lcore");
56                 return -1;
57         }
58
59         if (evt_nr_active_lcores(opt->plcores) == 0) {
60                 evt_err("missing the producer lcore");
61                 return -1;
62         }
63
64         if (evt_nr_active_lcores(opt->plcores) != 1) {
65                 evt_err("only one producer lcore must be selected");
66                 return -1;
67         }
68
69         int plcore = evt_get_first_active_lcore(opt->plcores);
70
71         if (plcore < 0) {
72                 evt_err("failed to find active producer");
73                 return plcore;
74         }
75
76         if (evt_lcores_has_overlap(opt->wlcores, plcore)) {
77                 evt_err("worker lcores overlaps producer lcore");
78                 return -1;
79         }
80         if (evt_has_disabled_lcore(opt->wlcores)) {
81                 evt_err("one or more workers lcores are not enabled");
82                 return -1;
83         }
84         if (!evt_has_active_lcore(opt->wlcores)) {
85                 evt_err("minimum one worker is required");
86                 return -1;
87         }
88
89         /* Validate producer lcore */
90         if (plcore == (int)rte_get_master_lcore()) {
91                 evt_err("producer lcore and master lcore should be different");
92                 return -1;
93         }
94         if (!rte_lcore_is_enabled(plcore)) {
95                 evt_err("producer lcore is not enabled");
96                 return -1;
97         }
98
99         /* Fixups */
100         if (opt->nb_pkts == 0)
101                 opt->nb_pkts = INT64_MAX;
102
103         return 0;
104 }
105
106 int
107 order_test_setup(struct evt_test *test, struct evt_options *opt)
108 {
109         void *test_order;
110
111         test_order = rte_zmalloc_socket(test->name, sizeof(struct test_order),
112                                 RTE_CACHE_LINE_SIZE, opt->socket_id);
113         if (test_order  == NULL) {
114                 evt_err("failed to allocate test_order memory");
115                 goto nomem;
116         }
117         test->test_priv = test_order;
118
119         struct test_order *t = evt_test_priv(test);
120
121         t->producer_flow_seq = rte_zmalloc_socket("test_producer_flow_seq",
122                                  sizeof(*t->producer_flow_seq) * opt->nb_flows,
123                                 RTE_CACHE_LINE_SIZE, opt->socket_id);
124
125         if (t->producer_flow_seq  == NULL) {
126                 evt_err("failed to allocate t->producer_flow_seq memory");
127                 goto prod_nomem;
128         }
129
130         t->expected_flow_seq = rte_zmalloc_socket("test_expected_flow_seq",
131                                  sizeof(*t->expected_flow_seq) * opt->nb_flows,
132                                 RTE_CACHE_LINE_SIZE, opt->socket_id);
133
134         if (t->expected_flow_seq  == NULL) {
135                 evt_err("failed to allocate t->expected_flow_seq memory");
136                 goto exp_nomem;
137         }
138         rte_atomic64_set(&t->outstand_pkts, opt->nb_pkts);
139         t->err = false;
140         t->nb_pkts = opt->nb_pkts;
141         t->nb_flows = opt->nb_flows;
142         t->result = EVT_TEST_FAILED;
143         t->opt = opt;
144         return 0;
145
146 exp_nomem:
147         rte_free(t->producer_flow_seq);
148 prod_nomem:
149         rte_free(test->test_priv);
150 nomem:
151         return -ENOMEM;
152 }
153
154 void
155 order_test_destroy(struct evt_test *test, struct evt_options *opt)
156 {
157         RTE_SET_USED(opt);
158         struct test_order *t = evt_test_priv(test);
159
160         rte_free(t->expected_flow_seq);
161         rte_free(t->producer_flow_seq);
162         rte_free(test->test_priv);
163 }
164
165 int
166 order_mempool_setup(struct evt_test *test, struct evt_options *opt)
167 {
168         struct test_order *t = evt_test_priv(test);
169
170         t->pool  = rte_pktmbuf_pool_create(test->name, opt->pool_sz,
171                                         256 /* Cache */, 0,
172                                         512, /* Use very small mbufs */
173                                         opt->socket_id);
174         if (t->pool == NULL) {
175                 evt_err("failed to create mempool");
176                 return -ENOMEM;
177         }
178
179         return 0;
180 }
181
182 void
183 order_mempool_destroy(struct evt_test *test, struct evt_options *opt)
184 {
185         RTE_SET_USED(opt);
186         struct test_order *t = evt_test_priv(test);
187
188         rte_mempool_free(t->pool);
189 }
190
191 void
192 order_eventdev_destroy(struct evt_test *test, struct evt_options *opt)
193 {
194         RTE_SET_USED(test);
195
196         rte_event_dev_stop(opt->dev_id);
197         rte_event_dev_close(opt->dev_id);
198 }
199
200 void
201 order_opt_dump(struct evt_options *opt)
202 {
203         evt_dump_producer_lcores(opt);
204         evt_dump("nb_wrker_lcores", "%d", evt_nr_active_lcores(opt->wlcores));
205         evt_dump_worker_lcores(opt);
206         evt_dump("nb_evdev_ports", "%d", order_nb_event_ports(opt));
207 }
208
209