xref: /dpdk/app/test/test_sched.c (revision e0a8442ccd15bafbb7eb150c35331c8e3b828c53)
1a9de470cSBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2a9de470cSBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
3a9de470cSBruce Richardson  */
4a9de470cSBruce Richardson 
5a9de470cSBruce Richardson #include <stdlib.h>
6a9de470cSBruce Richardson #include <stdio.h>
7a9de470cSBruce Richardson #include <string.h>
8a9de470cSBruce Richardson #include <stdint.h>
9a9de470cSBruce Richardson #include <unistd.h>
10a9de470cSBruce Richardson 
11a9de470cSBruce Richardson #include "test.h"
12a9de470cSBruce Richardson 
13a9de470cSBruce Richardson #include <rte_cycles.h>
14a9de470cSBruce Richardson #include <rte_ether.h>
15a9de470cSBruce Richardson #include <rte_ip.h>
16a9de470cSBruce Richardson #include <rte_byteorder.h>
17a9de470cSBruce Richardson 
183c60274cSJie Zhou #ifdef RTE_EXEC_ENV_WINDOWS
193c60274cSJie Zhou static int
test_sched(void)203c60274cSJie Zhou test_sched(void)
213c60274cSJie Zhou {
223c60274cSJie Zhou 	printf("sched not supported on Windows, skipping test\n");
233c60274cSJie Zhou 	return TEST_SKIPPED;
243c60274cSJie Zhou }
253c60274cSJie Zhou #else
263c60274cSJie Zhou 
273c60274cSJie Zhou #include <rte_sched.h>
28a9de470cSBruce Richardson 
29a9de470cSBruce Richardson #define SUBPORT         0
30a9de470cSBruce Richardson #define PIPE            1
31a9de470cSBruce Richardson #define TC              2
320569926cSJasvinder Singh #define QUEUE           0
33b393ad5fSSavinay Dharmappa #define MAX_SCHED_SUBPORT_PROFILES  8
34a9de470cSBruce Richardson 
35a9de470cSBruce Richardson static struct rte_sched_pipe_params pipe_profile[] = {
36a9de470cSBruce Richardson 	{ /* Profile #0 */
37a9de470cSBruce Richardson 		.tb_rate = 305175,
38a9de470cSBruce Richardson 		.tb_size = 1000000,
39a9de470cSBruce Richardson 
400569926cSJasvinder Singh 		.tc_rate = {305175, 305175, 305175, 305175, 305175, 305175,
410569926cSJasvinder Singh 			305175, 305175, 305175, 305175, 305175, 305175, 305175},
42a9de470cSBruce Richardson 		.tc_period = 40,
430569926cSJasvinder Singh 		.tc_ov_weight = 1,
44a9de470cSBruce Richardson 
45e16b06daSJasvinder Singh 		.wrr_weights = {1, 1, 1, 1},
46a9de470cSBruce Richardson 	},
47a9de470cSBruce Richardson };
48a9de470cSBruce Richardson 
49b393ad5fSSavinay Dharmappa static struct rte_sched_subport_profile_params
50b393ad5fSSavinay Dharmappa 		subport_profile[] = {
517cb98976SJasvinder Singh 	{
527cb98976SJasvinder Singh 		.tb_rate = 1250000000,
537cb98976SJasvinder Singh 		.tb_size = 1000000,
547cb98976SJasvinder Singh 		.tc_rate = {1250000000, 1250000000, 1250000000, 1250000000,
557cb98976SJasvinder Singh 			1250000000, 1250000000, 1250000000, 1250000000, 1250000000,
567cb98976SJasvinder Singh 			1250000000, 1250000000, 1250000000, 1250000000},
577cb98976SJasvinder Singh 		.tc_period = 10,
58b393ad5fSSavinay Dharmappa 	},
59b393ad5fSSavinay Dharmappa };
60b393ad5fSSavinay Dharmappa 
61b393ad5fSSavinay Dharmappa static struct rte_sched_subport_params subport_param[] = {
62b393ad5fSSavinay Dharmappa 	{
637cb98976SJasvinder Singh 		.n_pipes_per_subport_enabled = 1024,
647cb98976SJasvinder Singh 		.qsize = {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32},
657cb98976SJasvinder Singh 		.pipe_profiles = pipe_profile,
667cb98976SJasvinder Singh 		.n_pipe_profiles = 1,
677cb98976SJasvinder Singh 		.n_max_pipe_profiles = 1,
687cb98976SJasvinder Singh 	},
697cb98976SJasvinder Singh };
707cb98976SJasvinder Singh 
71a9de470cSBruce Richardson static struct rte_sched_port_params port_param = {
72a9de470cSBruce Richardson 	.socket = 0, /* computed */
73a9de470cSBruce Richardson 	.rate = 0, /* computed */
74a9de470cSBruce Richardson 	.mtu = 1522,
75a9de470cSBruce Richardson 	.frame_overhead = RTE_SCHED_FRAME_OVERHEAD_DEFAULT,
76a9de470cSBruce Richardson 	.n_subports_per_port = 1,
77b393ad5fSSavinay Dharmappa 	.n_subport_profiles = 1,
78b393ad5fSSavinay Dharmappa 	.subport_profiles = subport_profile,
79b393ad5fSSavinay Dharmappa 	.n_max_subport_profiles = MAX_SCHED_SUBPORT_PROFILES,
80a9de470cSBruce Richardson 	.n_pipes_per_subport = 1024,
81a9de470cSBruce Richardson };
82a9de470cSBruce Richardson 
83a9de470cSBruce Richardson #define NB_MBUF          32
84a9de470cSBruce Richardson #define MBUF_DATA_SZ     (2048 + RTE_PKTMBUF_HEADROOM)
85a9de470cSBruce Richardson #define MEMPOOL_CACHE_SZ 0
86a9de470cSBruce Richardson #define SOCKET           0
87a9de470cSBruce Richardson 
88a9de470cSBruce Richardson 
89a9de470cSBruce Richardson static struct rte_mempool *
create_mempool(void)90a9de470cSBruce Richardson create_mempool(void)
91a9de470cSBruce Richardson {
92a9de470cSBruce Richardson 	struct rte_mempool * mp;
93a9de470cSBruce Richardson 
94a9de470cSBruce Richardson 	mp = rte_mempool_lookup("test_sched");
95a9de470cSBruce Richardson 	if (!mp)
96a9de470cSBruce Richardson 		mp = rte_pktmbuf_pool_create("test_sched", NB_MBUF,
97a9de470cSBruce Richardson 			MEMPOOL_CACHE_SZ, 0, MBUF_DATA_SZ, SOCKET);
98a9de470cSBruce Richardson 
99a9de470cSBruce Richardson 	return mp;
100a9de470cSBruce Richardson }
101a9de470cSBruce Richardson 
102a9de470cSBruce Richardson static void
prepare_pkt(struct rte_sched_port * port,struct rte_mbuf * mbuf)103a9de470cSBruce Richardson prepare_pkt(struct rte_sched_port *port, struct rte_mbuf *mbuf)
104a9de470cSBruce Richardson {
1056d13ea8eSOlivier Matz 	struct rte_ether_hdr *eth_hdr;
1066d13ea8eSOlivier Matz 	struct rte_vlan_hdr *vlan1, *vlan2;
107a7c528e5SOlivier Matz 	struct rte_ipv4_hdr *ip_hdr;
108a9de470cSBruce Richardson 
109a9de470cSBruce Richardson 	/* Simulate a classifier */
1106d13ea8eSOlivier Matz 	eth_hdr = rte_pktmbuf_mtod(mbuf, struct rte_ether_hdr *);
1116d13ea8eSOlivier Matz 	vlan1 = (struct rte_vlan_hdr *)(&eth_hdr->ether_type);
1126d13ea8eSOlivier Matz 	vlan2 = (struct rte_vlan_hdr *)(
1136d13ea8eSOlivier Matz 		(uintptr_t)&eth_hdr->ether_type + sizeof(struct rte_vlan_hdr));
1146d13ea8eSOlivier Matz 	eth_hdr = (struct rte_ether_hdr *)(
1156d13ea8eSOlivier Matz 		(uintptr_t)&eth_hdr->ether_type +
1166d13ea8eSOlivier Matz 		2 * sizeof(struct rte_vlan_hdr));
117a7c528e5SOlivier Matz 	ip_hdr = (struct rte_ipv4_hdr *)(
1186d13ea8eSOlivier Matz 		(uintptr_t)eth_hdr + sizeof(eth_hdr->ether_type));
119a9de470cSBruce Richardson 
120a9de470cSBruce Richardson 	vlan1->vlan_tci = rte_cpu_to_be_16(SUBPORT);
121a9de470cSBruce Richardson 	vlan2->vlan_tci = rte_cpu_to_be_16(PIPE);
1220c9da755SDavid Marchand 	eth_hdr->ether_type =  rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
1230c9da755SDavid Marchand 	ip_hdr->dst_addr = RTE_IPV4(0,0,TC,QUEUE);
124a9de470cSBruce Richardson 
125a9de470cSBruce Richardson 
126a9de470cSBruce Richardson 	rte_sched_port_pkt_write(port, mbuf, SUBPORT, PIPE, TC, QUEUE,
127c1656328SJasvinder Singh 					RTE_COLOR_YELLOW);
128a9de470cSBruce Richardson 
129a9de470cSBruce Richardson 	/* 64 byte packet */
130a9de470cSBruce Richardson 	mbuf->pkt_len  = 60;
131a9de470cSBruce Richardson 	mbuf->data_len = 60;
132a9de470cSBruce Richardson }
133a9de470cSBruce Richardson 
134a9de470cSBruce Richardson 
135a9de470cSBruce Richardson /**
136a9de470cSBruce Richardson  * test main entrance for library sched
137a9de470cSBruce Richardson  */
138a9de470cSBruce Richardson static int
test_sched(void)139a9de470cSBruce Richardson test_sched(void)
140a9de470cSBruce Richardson {
141a9de470cSBruce Richardson 	struct rte_mempool *mp = NULL;
142a9de470cSBruce Richardson 	struct rte_sched_port *port = NULL;
143a9de470cSBruce Richardson 	uint32_t pipe;
144a9de470cSBruce Richardson 	struct rte_mbuf *in_mbufs[10];
145a9de470cSBruce Richardson 	struct rte_mbuf *out_mbufs[10];
146a9de470cSBruce Richardson 	int i;
147a9de470cSBruce Richardson 
148a9de470cSBruce Richardson 	int err;
149a9de470cSBruce Richardson 
150a9de470cSBruce Richardson 	mp = create_mempool();
151a9de470cSBruce Richardson 	TEST_ASSERT_NOT_NULL(mp, "Error creating mempool\n");
152a9de470cSBruce Richardson 
153a9de470cSBruce Richardson 	port_param.socket = 0;
154a9de470cSBruce Richardson 	port_param.rate = (uint64_t) 10000 * 1000 * 1000 / 8;
155a9de470cSBruce Richardson 
156a9de470cSBruce Richardson 	port = rte_sched_port_config(&port_param);
157a9de470cSBruce Richardson 	TEST_ASSERT_NOT_NULL(port, "Error config sched port\n");
158a9de470cSBruce Richardson 
159ac6fcb84SSavinay Dharmappa 	err = rte_sched_subport_config(port, SUBPORT, subport_param, 0);
160a9de470cSBruce Richardson 	TEST_ASSERT_SUCCESS(err, "Error config sched, err=%d\n", err);
161a9de470cSBruce Richardson 
1627cb98976SJasvinder Singh 	for (pipe = 0; pipe < subport_param[0].n_pipes_per_subport_enabled; pipe++) {
163a9de470cSBruce Richardson 		err = rte_sched_pipe_config(port, SUBPORT, pipe, 0);
164a9de470cSBruce Richardson 		TEST_ASSERT_SUCCESS(err, "Error config sched pipe %u, err=%d\n", pipe, err);
165a9de470cSBruce Richardson 	}
166a9de470cSBruce Richardson 
167a9de470cSBruce Richardson 	for (i = 0; i < 10; i++) {
168a9de470cSBruce Richardson 		in_mbufs[i] = rte_pktmbuf_alloc(mp);
169a9de470cSBruce Richardson 		TEST_ASSERT_NOT_NULL(in_mbufs[i], "Packet allocation failed\n");
170a9de470cSBruce Richardson 		prepare_pkt(port, in_mbufs[i]);
171a9de470cSBruce Richardson 	}
172a9de470cSBruce Richardson 
173a9de470cSBruce Richardson 
174a9de470cSBruce Richardson 	err = rte_sched_port_enqueue(port, in_mbufs, 10);
175a9de470cSBruce Richardson 	TEST_ASSERT_EQUAL(err, 10, "Wrong enqueue, err=%d\n", err);
176a9de470cSBruce Richardson 
177a9de470cSBruce Richardson 	err = rte_sched_port_dequeue(port, out_mbufs, 10);
178a9de470cSBruce Richardson 	TEST_ASSERT_EQUAL(err, 10, "Wrong dequeue, err=%d\n", err);
179a9de470cSBruce Richardson 
180a9de470cSBruce Richardson 	for (i = 0; i < 10; i++) {
181c1656328SJasvinder Singh 		enum rte_color color;
182a9de470cSBruce Richardson 		uint32_t subport, traffic_class, queue;
183a9de470cSBruce Richardson 
184a9de470cSBruce Richardson 		color = rte_sched_port_pkt_read_color(out_mbufs[i]);
185c1656328SJasvinder Singh 		TEST_ASSERT_EQUAL(color, RTE_COLOR_YELLOW, "Wrong color\n");
186a9de470cSBruce Richardson 
187a9de470cSBruce Richardson 		rte_sched_port_pkt_read_tree_path(port, out_mbufs[i],
188a9de470cSBruce Richardson 				&subport, &pipe, &traffic_class, &queue);
189a9de470cSBruce Richardson 
190a9de470cSBruce Richardson 		TEST_ASSERT_EQUAL(subport, SUBPORT, "Wrong subport\n");
191a9de470cSBruce Richardson 		TEST_ASSERT_EQUAL(pipe, PIPE, "Wrong pipe\n");
192a9de470cSBruce Richardson 		TEST_ASSERT_EQUAL(traffic_class, TC, "Wrong traffic_class\n");
193a9de470cSBruce Richardson 		TEST_ASSERT_EQUAL(queue, QUEUE, "Wrong queue\n");
194a9de470cSBruce Richardson 
195a9de470cSBruce Richardson 	}
196a9de470cSBruce Richardson 
197a9de470cSBruce Richardson 
198a9de470cSBruce Richardson 	struct rte_sched_subport_stats subport_stats;
199a9de470cSBruce Richardson 	uint32_t tc_ov;
200a9de470cSBruce Richardson 	rte_sched_subport_read_stats(port, SUBPORT, &subport_stats, &tc_ov);
201a9de470cSBruce Richardson #if 0
202a9de470cSBruce Richardson 	TEST_ASSERT_EQUAL(subport_stats.n_pkts_tc[TC-1], 10, "Wrong subport stats\n");
203a9de470cSBruce Richardson #endif
204a9de470cSBruce Richardson 	struct rte_sched_queue_stats queue_stats;
205a9de470cSBruce Richardson 	uint16_t qlen;
206a9de470cSBruce Richardson 	rte_sched_queue_read_stats(port, QUEUE, &queue_stats, &qlen);
207a9de470cSBruce Richardson #if 0
208a9de470cSBruce Richardson 	TEST_ASSERT_EQUAL(queue_stats.n_pkts, 10, "Wrong queue stats\n");
209a9de470cSBruce Richardson #endif
210a9de470cSBruce Richardson 
211a9de470cSBruce Richardson 	rte_sched_port_free(port);
212a9de470cSBruce Richardson 
213a9de470cSBruce Richardson 	return 0;
214a9de470cSBruce Richardson }
215a9de470cSBruce Richardson 
2163c60274cSJie Zhou #endif /* !RTE_EXEC_ENV_WINDOWS */
2173c60274cSJie Zhou 
218*e0a8442cSBruce Richardson REGISTER_FAST_TEST(sched_autotest, true, true, test_sched);
219