1d1f59fb7SJerin Jacob /*
2d1f59fb7SJerin Jacob  *   BSD LICENSE
3d1f59fb7SJerin Jacob  *
4d1f59fb7SJerin Jacob  *   Copyright (C) Cavium 2017.
5d1f59fb7SJerin Jacob  *
6d1f59fb7SJerin Jacob  *   Redistribution and use in source and binary forms, with or without
7d1f59fb7SJerin Jacob  *   modification, are permitted provided that the following conditions
8d1f59fb7SJerin Jacob  *   are met:
9d1f59fb7SJerin Jacob  *
10d1f59fb7SJerin Jacob  *     * Redistributions of source code must retain the above copyright
11d1f59fb7SJerin Jacob  *       notice, this list of conditions and the following disclaimer.
12d1f59fb7SJerin Jacob  *     * Redistributions in binary form must reproduce the above copyright
13d1f59fb7SJerin Jacob  *       notice, this list of conditions and the following disclaimer in
14d1f59fb7SJerin Jacob  *       the documentation and/or other materials provided with the
15d1f59fb7SJerin Jacob  *       distribution.
16d1f59fb7SJerin Jacob  *     * Neither the name of Cavium networks nor the names of its
17d1f59fb7SJerin Jacob  *       contributors may be used to endorse or promote products derived
18d1f59fb7SJerin Jacob  *       from this software without specific prior written permission.
19d1f59fb7SJerin Jacob  *
20d1f59fb7SJerin Jacob  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21d1f59fb7SJerin Jacob  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22d1f59fb7SJerin Jacob  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23d1f59fb7SJerin Jacob  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24d1f59fb7SJerin Jacob  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25d1f59fb7SJerin Jacob  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26d1f59fb7SJerin Jacob  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27d1f59fb7SJerin Jacob  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28d1f59fb7SJerin Jacob  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29d1f59fb7SJerin Jacob  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30d1f59fb7SJerin Jacob  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31d1f59fb7SJerin Jacob  */
32d1f59fb7SJerin Jacob 
33d1f59fb7SJerin Jacob #ifndef _TEST_ORDER_COMMON_
34d1f59fb7SJerin Jacob #define _TEST_ORDER_COMMON_
35d1f59fb7SJerin Jacob 
36d1f59fb7SJerin Jacob #include <stdio.h>
37d1f59fb7SJerin Jacob #include <stdbool.h>
38d1f59fb7SJerin Jacob 
39d1f59fb7SJerin Jacob #include <rte_cycles.h>
40d1f59fb7SJerin Jacob #include <rte_eventdev.h>
41d1f59fb7SJerin Jacob #include <rte_lcore.h>
42d1f59fb7SJerin Jacob #include <rte_malloc.h>
43d1f59fb7SJerin Jacob #include <rte_mbuf.h>
44d1f59fb7SJerin Jacob 
45d1f59fb7SJerin Jacob #include "evt_common.h"
46d1f59fb7SJerin Jacob #include "evt_options.h"
47d1f59fb7SJerin Jacob #include "evt_test.h"
48d1f59fb7SJerin Jacob 
49d1f59fb7SJerin Jacob #define BURST_SIZE 16
50d1f59fb7SJerin Jacob 
51d1f59fb7SJerin Jacob struct test_order;
52d1f59fb7SJerin Jacob 
53d1f59fb7SJerin Jacob struct worker_data {
54d1f59fb7SJerin Jacob 	uint8_t dev_id;
55d1f59fb7SJerin Jacob 	uint8_t port_id;
56d1f59fb7SJerin Jacob 	struct test_order *t;
57d1f59fb7SJerin Jacob };
58d1f59fb7SJerin Jacob 
59d1f59fb7SJerin Jacob struct prod_data {
60d1f59fb7SJerin Jacob 	uint8_t dev_id;
61d1f59fb7SJerin Jacob 	uint8_t port_id;
62d1f59fb7SJerin Jacob 	uint8_t queue_id;
63d1f59fb7SJerin Jacob 	struct test_order *t;
64d1f59fb7SJerin Jacob };
65d1f59fb7SJerin Jacob 
66d1f59fb7SJerin Jacob struct test_order {
67d1f59fb7SJerin Jacob 	/* Don't change the offset of "err". Signal handler use this memory
68d1f59fb7SJerin Jacob 	 * to terminate all lcores work.
69d1f59fb7SJerin Jacob 	 */
70d1f59fb7SJerin Jacob 	int err;
71d1f59fb7SJerin Jacob 	/*
72d1f59fb7SJerin Jacob 	 * The atomic_* is an expensive operation,Since it is a functional test,
73d1f59fb7SJerin Jacob 	 * We are using the atomic_ operation to reduce the code complexity.
74d1f59fb7SJerin Jacob 	 */
75d1f59fb7SJerin Jacob 	rte_atomic64_t outstand_pkts;
76d1f59fb7SJerin Jacob 	enum evt_test_result result;
77d1f59fb7SJerin Jacob 	uint32_t nb_flows;
78d1f59fb7SJerin Jacob 	uint64_t nb_pkts;
79d1f59fb7SJerin Jacob 	struct rte_mempool *pool;
80d1f59fb7SJerin Jacob 	struct prod_data prod;
81d1f59fb7SJerin Jacob 	struct worker_data worker[EVT_MAX_PORTS];
82d1f59fb7SJerin Jacob 	uint32_t *producer_flow_seq;
83d1f59fb7SJerin Jacob 	uint32_t *expected_flow_seq;
84d1f59fb7SJerin Jacob 	struct evt_options *opt;
85d1f59fb7SJerin Jacob } __rte_cache_aligned;
86d1f59fb7SJerin Jacob 
87ba11ebf1SJerin Jacob static inline int
88ba11ebf1SJerin Jacob order_nb_event_ports(struct evt_options *opt)
89ba11ebf1SJerin Jacob {
90ba11ebf1SJerin Jacob 	return evt_nr_active_lcores(opt->wlcores) + 1 /* producer */;
91ba11ebf1SJerin Jacob }
92ba11ebf1SJerin Jacob 
93*4d04346fSJerin Jacob static inline __attribute__((always_inline)) void
94*4d04346fSJerin Jacob order_process_stage_1(struct test_order *const t,
95*4d04346fSJerin Jacob 		struct rte_event *const ev, const uint32_t nb_flows,
96*4d04346fSJerin Jacob 		uint32_t *const expected_flow_seq,
97*4d04346fSJerin Jacob 		rte_atomic64_t *const outstand_pkts)
98*4d04346fSJerin Jacob {
99*4d04346fSJerin Jacob 	const uint32_t flow = (uintptr_t)ev->mbuf % nb_flows;
100*4d04346fSJerin Jacob 	/* compare the seqn against expected value */
101*4d04346fSJerin Jacob 	if (ev->mbuf->seqn != expected_flow_seq[flow]) {
102*4d04346fSJerin Jacob 		evt_err("flow=%x seqn mismatch got=%x expected=%x",
103*4d04346fSJerin Jacob 			flow, ev->mbuf->seqn, expected_flow_seq[flow]);
104*4d04346fSJerin Jacob 		t->err = true;
105*4d04346fSJerin Jacob 		rte_smp_wmb();
106*4d04346fSJerin Jacob 	}
107*4d04346fSJerin Jacob 	/*
108*4d04346fSJerin Jacob 	 * Events from an atomic flow of an event queue can be scheduled only to
109*4d04346fSJerin Jacob 	 * a single port at a time. The port is guaranteed to have exclusive
110*4d04346fSJerin Jacob 	 * (atomic) access for given atomic flow.So we don't need to update
111*4d04346fSJerin Jacob 	 * expected_flow_seq in critical section.
112*4d04346fSJerin Jacob 	 */
113*4d04346fSJerin Jacob 	expected_flow_seq[flow]++;
114*4d04346fSJerin Jacob 	rte_pktmbuf_free(ev->mbuf);
115*4d04346fSJerin Jacob 	rte_atomic64_sub(outstand_pkts, 1);
116*4d04346fSJerin Jacob }
117*4d04346fSJerin Jacob 
118*4d04346fSJerin Jacob static inline __attribute__((always_inline)) void
119*4d04346fSJerin Jacob order_process_stage_invalid(struct test_order *const t,
120*4d04346fSJerin Jacob 			struct rte_event *const ev)
121*4d04346fSJerin Jacob {
122*4d04346fSJerin Jacob 	evt_err("invalid queue %d", ev->queue_id);
123*4d04346fSJerin Jacob 	t->err = true;
124*4d04346fSJerin Jacob 	rte_smp_wmb();
125*4d04346fSJerin Jacob }
126*4d04346fSJerin Jacob 
127*4d04346fSJerin Jacob #define ORDER_WORKER_INIT\
128*4d04346fSJerin Jacob 	struct worker_data *w  = arg;\
129*4d04346fSJerin Jacob 	struct test_order *t = w->t;\
130*4d04346fSJerin Jacob 	struct evt_options *opt = t->opt;\
131*4d04346fSJerin Jacob 	const uint8_t dev_id = w->dev_id;\
132*4d04346fSJerin Jacob 	const uint8_t port = w->port_id;\
133*4d04346fSJerin Jacob 	const uint32_t nb_flows = t->nb_flows;\
134*4d04346fSJerin Jacob 	uint32_t *expected_flow_seq = t->expected_flow_seq;\
135*4d04346fSJerin Jacob 	rte_atomic64_t *outstand_pkts = &t->outstand_pkts;\
136*4d04346fSJerin Jacob 	if (opt->verbose_level > 1)\
137*4d04346fSJerin Jacob 		printf("%s(): lcore %d dev_id %d port=%d\n",\
138*4d04346fSJerin Jacob 			__func__, rte_lcore_id(), dev_id, port)
139*4d04346fSJerin Jacob 
140ba11ebf1SJerin Jacob int order_test_result(struct evt_test *test, struct evt_options *opt);
141ba11ebf1SJerin Jacob int order_opt_check(struct evt_options *opt);
142d1f59fb7SJerin Jacob int order_test_setup(struct evt_test *test, struct evt_options *opt);
143ba11ebf1SJerin Jacob int order_mempool_setup(struct evt_test *test, struct evt_options *opt);
14433b7483dSJerin Jacob int order_launch_lcores(struct evt_test *test, struct evt_options *opt,
14533b7483dSJerin Jacob 			int (*worker)(void *));
1465710e751SJerin Jacob int order_event_dev_port_setup(struct evt_test *test, struct evt_options *opt,
1475710e751SJerin Jacob 				uint8_t nb_workers, uint8_t nb_queues);
148d1f59fb7SJerin Jacob void order_test_destroy(struct evt_test *test, struct evt_options *opt);
149ba11ebf1SJerin Jacob void order_opt_dump(struct evt_options *opt);
150ba11ebf1SJerin Jacob void order_mempool_destroy(struct evt_test *test, struct evt_options *opt);
151ba11ebf1SJerin Jacob void order_eventdev_destroy(struct evt_test *test, struct evt_options *opt);
152d1f59fb7SJerin Jacob 
153d1f59fb7SJerin Jacob #endif /* _TEST_ORDER_COMMON_ */
154