xref: /dpdk/app/test/test_sched.c (revision fdf7471cccb8be023037c218d1402c0549eb2c8e)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdint.h>
9 #include <unistd.h>
10 
11 #include "test.h"
12 
13 #include <rte_cycles.h>
14 #include <rte_ether.h>
15 #include <rte_ip.h>
16 #include <rte_byteorder.h>
17 #include <rte_sched.h>
18 
19 
20 #define SUBPORT         0
21 #define PIPE            1
22 #define TC              2
23 #define QUEUE           0
24 
25 static struct rte_sched_pipe_params pipe_profile[] = {
26 	{ /* Profile #0 */
27 		.tb_rate = 305175,
28 		.tb_size = 1000000,
29 
30 		.tc_rate = {305175, 305175, 305175, 305175, 305175, 305175,
31 			305175, 305175, 305175, 305175, 305175, 305175, 305175},
32 		.tc_period = 40,
33 		.tc_ov_weight = 1,
34 
35 		.wrr_weights = {1, 1, 1, 1},
36 	},
37 };
38 
39 static struct rte_sched_subport_params subport_param[] = {
40 	{
41 		.tb_rate = 1250000000,
42 		.tb_size = 1000000,
43 
44 		.tc_rate = {1250000000, 1250000000, 1250000000, 1250000000,
45 			1250000000, 1250000000, 1250000000, 1250000000, 1250000000,
46 			1250000000, 1250000000, 1250000000, 1250000000},
47 		.tc_period = 10,
48 		.n_pipes_per_subport_enabled = 1024,
49 		.qsize = {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32},
50 		.pipe_profiles = pipe_profile,
51 		.n_pipe_profiles = 1,
52 		.n_max_pipe_profiles = 1,
53 	},
54 };
55 
56 static struct rte_sched_port_params port_param = {
57 	.socket = 0, /* computed */
58 	.rate = 0, /* computed */
59 	.mtu = 1522,
60 	.frame_overhead = RTE_SCHED_FRAME_OVERHEAD_DEFAULT,
61 	.n_subports_per_port = 1,
62 	.n_pipes_per_subport = 1024,
63 };
64 
65 #define NB_MBUF          32
66 #define MBUF_DATA_SZ     (2048 + RTE_PKTMBUF_HEADROOM)
67 #define MEMPOOL_CACHE_SZ 0
68 #define SOCKET           0
69 
70 
71 static struct rte_mempool *
72 create_mempool(void)
73 {
74 	struct rte_mempool * mp;
75 
76 	mp = rte_mempool_lookup("test_sched");
77 	if (!mp)
78 		mp = rte_pktmbuf_pool_create("test_sched", NB_MBUF,
79 			MEMPOOL_CACHE_SZ, 0, MBUF_DATA_SZ, SOCKET);
80 
81 	return mp;
82 }
83 
84 static void
85 prepare_pkt(struct rte_sched_port *port, struct rte_mbuf *mbuf)
86 {
87 	struct rte_ether_hdr *eth_hdr;
88 	struct rte_vlan_hdr *vlan1, *vlan2;
89 	struct rte_ipv4_hdr *ip_hdr;
90 
91 	/* Simulate a classifier */
92 	eth_hdr = rte_pktmbuf_mtod(mbuf, struct rte_ether_hdr *);
93 	vlan1 = (struct rte_vlan_hdr *)(&eth_hdr->ether_type);
94 	vlan2 = (struct rte_vlan_hdr *)(
95 		(uintptr_t)&eth_hdr->ether_type + sizeof(struct rte_vlan_hdr));
96 	eth_hdr = (struct rte_ether_hdr *)(
97 		(uintptr_t)&eth_hdr->ether_type +
98 		2 * sizeof(struct rte_vlan_hdr));
99 	ip_hdr = (struct rte_ipv4_hdr *)(
100 		(uintptr_t)eth_hdr + sizeof(eth_hdr->ether_type));
101 
102 	vlan1->vlan_tci = rte_cpu_to_be_16(SUBPORT);
103 	vlan2->vlan_tci = rte_cpu_to_be_16(PIPE);
104 	eth_hdr->ether_type =  rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
105 	ip_hdr->dst_addr = RTE_IPV4(0,0,TC,QUEUE);
106 
107 
108 	rte_sched_port_pkt_write(port, mbuf, SUBPORT, PIPE, TC, QUEUE,
109 					RTE_COLOR_YELLOW);
110 
111 	/* 64 byte packet */
112 	mbuf->pkt_len  = 60;
113 	mbuf->data_len = 60;
114 }
115 
116 
117 /**
118  * test main entrance for library sched
119  */
120 static int
121 test_sched(void)
122 {
123 	struct rte_mempool *mp = NULL;
124 	struct rte_sched_port *port = NULL;
125 	uint32_t pipe;
126 	struct rte_mbuf *in_mbufs[10];
127 	struct rte_mbuf *out_mbufs[10];
128 	int i;
129 
130 	int err;
131 
132 	mp = create_mempool();
133 	TEST_ASSERT_NOT_NULL(mp, "Error creating mempool\n");
134 
135 	port_param.socket = 0;
136 	port_param.rate = (uint64_t) 10000 * 1000 * 1000 / 8;
137 
138 	port = rte_sched_port_config(&port_param);
139 	TEST_ASSERT_NOT_NULL(port, "Error config sched port\n");
140 
141 	err = rte_sched_subport_config(port, SUBPORT, subport_param);
142 	TEST_ASSERT_SUCCESS(err, "Error config sched, err=%d\n", err);
143 
144 	for (pipe = 0; pipe < subport_param[0].n_pipes_per_subport_enabled; pipe++) {
145 		err = rte_sched_pipe_config(port, SUBPORT, pipe, 0);
146 		TEST_ASSERT_SUCCESS(err, "Error config sched pipe %u, err=%d\n", pipe, err);
147 	}
148 
149 	for (i = 0; i < 10; i++) {
150 		in_mbufs[i] = rte_pktmbuf_alloc(mp);
151 		TEST_ASSERT_NOT_NULL(in_mbufs[i], "Packet allocation failed\n");
152 		prepare_pkt(port, in_mbufs[i]);
153 	}
154 
155 
156 	err = rte_sched_port_enqueue(port, in_mbufs, 10);
157 	TEST_ASSERT_EQUAL(err, 10, "Wrong enqueue, err=%d\n", err);
158 
159 	err = rte_sched_port_dequeue(port, out_mbufs, 10);
160 	TEST_ASSERT_EQUAL(err, 10, "Wrong dequeue, err=%d\n", err);
161 
162 	for (i = 0; i < 10; i++) {
163 		enum rte_color color;
164 		uint32_t subport, traffic_class, queue;
165 
166 		color = rte_sched_port_pkt_read_color(out_mbufs[i]);
167 		TEST_ASSERT_EQUAL(color, RTE_COLOR_YELLOW, "Wrong color\n");
168 
169 		rte_sched_port_pkt_read_tree_path(port, out_mbufs[i],
170 				&subport, &pipe, &traffic_class, &queue);
171 
172 		TEST_ASSERT_EQUAL(subport, SUBPORT, "Wrong subport\n");
173 		TEST_ASSERT_EQUAL(pipe, PIPE, "Wrong pipe\n");
174 		TEST_ASSERT_EQUAL(traffic_class, TC, "Wrong traffic_class\n");
175 		TEST_ASSERT_EQUAL(queue, QUEUE, "Wrong queue\n");
176 
177 	}
178 
179 
180 	struct rte_sched_subport_stats subport_stats;
181 	uint32_t tc_ov;
182 	rte_sched_subport_read_stats(port, SUBPORT, &subport_stats, &tc_ov);
183 #if 0
184 	TEST_ASSERT_EQUAL(subport_stats.n_pkts_tc[TC-1], 10, "Wrong subport stats\n");
185 #endif
186 	struct rte_sched_queue_stats queue_stats;
187 	uint16_t qlen;
188 	rte_sched_queue_read_stats(port, QUEUE, &queue_stats, &qlen);
189 #if 0
190 	TEST_ASSERT_EQUAL(queue_stats.n_pkts, 10, "Wrong queue stats\n");
191 #endif
192 
193 	rte_sched_port_free(port);
194 
195 	return 0;
196 }
197 
198 REGISTER_TEST_COMMAND(sched_autotest, test_sched);
199