xref: /dpdk/app/test/test_sched.c (revision e9d48c0072d36eb6423b45fba4ec49d0def6c36f)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdint.h>
38 #include <unistd.h>
39 
40 #include <cmdline_parse.h>
41 
42 #include "test.h"
43 
44 #if defined(RTE_LIBRTE_SCHED) && defined(RTE_ARCH_X86_64)
45 
46 #include <rte_cycles.h>
47 #include <rte_ether.h>
48 #include <rte_ip.h>
49 #include <rte_byteorder.h>
50 #include <rte_sched.h>
51 
52 
53 #define VERIFY(exp,fmt,args...)                    	                \
54 		if (!(exp)) {                                               \
55 			printf(fmt, ##args);                                    \
56 			return -1;                                              \
57 		}
58 
59 
60 #define SUBPORT 	0
61 #define PIPE 		1
62 #define TC 			2
63 #define QUEUE 		3
64 
65 static struct rte_sched_subport_params subport_param[] = {
66 	{
67 		.tb_rate = 1250000000,
68 		.tb_size = 1000000,
69 
70 		.tc_rate = {1250000000, 1250000000, 1250000000, 1250000000},
71 		.tc_period = 10,
72 	},
73 };
74 
75 static struct rte_sched_pipe_params pipe_profile[] = {
76 	{ /* Profile #0 */
77 		.tb_rate = 305175,
78 		.tb_size = 1000000,
79 
80 		.tc_rate = {305175, 305175, 305175, 305175},
81 		.tc_period = 40,
82 
83 		.wrr_weights = {1, 1, 1, 1,  1, 1, 1, 1,  1, 1, 1, 1,  1, 1, 1, 1},
84 	},
85 };
86 
87 static struct rte_sched_port_params port_param = {
88 	.name = "port_0",
89 	.socket = 0, /* computed */
90 	.rate = 0, /* computed */
91 	.mtu = 1522,
92 	.frame_overhead = RTE_SCHED_FRAME_OVERHEAD_DEFAULT,
93 	.n_subports_per_port = 1,
94 	.n_pipes_per_subport = 4096,
95 	.qsize = {64, 64, 64, 64},
96 	.pipe_profiles = pipe_profile,
97 	.n_pipe_profiles = 1,
98 };
99 
100 #define NB_MBUF          32
101 #define MAX_PACKET_SZ    2048
102 #define MBUF_SZ (MAX_PACKET_SZ + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
103 #define PKT_BURST_SZ     32
104 #define MEMPOOL_CACHE_SZ PKT_BURST_SZ
105 #define SOCKET           0
106 
107 
108 static struct rte_mempool *
109 create_mempool(void)
110 {
111 	struct rte_mempool * mp;
112 
113 	mp = rte_mempool_lookup("test_sched");
114 	if (!mp)
115 		mp = rte_mempool_create("test_sched",
116 				NB_MBUF,
117 				MBUF_SZ,
118 				MEMPOOL_CACHE_SZ,
119 				sizeof(struct rte_pktmbuf_pool_private),
120 				rte_pktmbuf_pool_init,
121 				NULL,
122 				rte_pktmbuf_init,
123 				NULL,
124 				SOCKET,
125 				0);
126 
127 	return mp;
128 }
129 
130 static void
131 prepare_pkt(struct rte_mbuf *mbuf)
132 {
133 	struct ether_hdr *eth_hdr;
134 	struct vlan_hdr *vlan1, *vlan2;
135 	struct ipv4_hdr *ip_hdr;
136 
137 	/* Simulate a classifier */
138 	eth_hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
139 	vlan1 = (struct vlan_hdr *)(&eth_hdr->ether_type );
140 	vlan2 = (struct vlan_hdr *)((uintptr_t)&eth_hdr->ether_type + sizeof(struct vlan_hdr));
141 	eth_hdr = (struct ether_hdr *)((uintptr_t)&eth_hdr->ether_type + 2 *sizeof(struct vlan_hdr));
142 	ip_hdr = (struct ipv4_hdr *)((uintptr_t)eth_hdr +  sizeof(eth_hdr->ether_type));
143 
144 	vlan1->vlan_tci = rte_cpu_to_be_16(SUBPORT);
145 	vlan2->vlan_tci = rte_cpu_to_be_16(PIPE);
146 	eth_hdr->ether_type =  rte_cpu_to_be_16(ETHER_TYPE_IPv4);
147 	ip_hdr->dst_addr = IPv4(0,0,TC,QUEUE);
148 
149 
150 	rte_sched_port_pkt_write(mbuf, SUBPORT, PIPE, TC, QUEUE, e_RTE_METER_YELLOW);
151 
152 	/* 64 byte packet */
153 	mbuf->pkt.pkt_len  = 60;
154 	mbuf->pkt.data_len = 60;
155 }
156 
157 
158 /**
159  * test main entrance for library sched
160  */
161 int
162 test_sched(void)
163 {
164 	struct rte_mempool *mp = NULL;
165 	struct rte_sched_port *port = NULL;
166 	uint32_t pipe;
167 	struct rte_mbuf *in_mbufs[10];
168 	struct rte_mbuf *out_mbufs[10];
169 	int i;
170 
171 	int err;
172 
173 	mp = create_mempool();
174 
175 	port_param.socket = 0;
176 	port_param.rate = (uint64_t) 10000 * 1000 * 1000 / 8;
177 	port_param.name = "port_0";
178 
179 	port = rte_sched_port_config(&port_param);
180 	VERIFY(port != NULL, "Error config sched port\n");
181 
182 
183 	err = rte_sched_subport_config(port, SUBPORT, subport_param);
184 	VERIFY(err == 0, "Error config sched, err=%d\n", err);
185 
186 	for (pipe = 0; pipe < port_param.n_pipes_per_subport; pipe ++) {
187 		err = rte_sched_pipe_config(port, SUBPORT, pipe, 0);
188 		VERIFY(err == 0, "Error config sched pipe %u, err=%d\n", pipe, err);
189 	}
190 
191 	for (i = 0; i < 10; i++) {
192 		in_mbufs[i] = rte_pktmbuf_alloc(mp);
193 		prepare_pkt(in_mbufs[i]);
194 	}
195 
196 
197 	err = rte_sched_port_enqueue(port, in_mbufs, 10);
198 	VERIFY(err == 10, "Wrong enqueue, err=%d\n", err);
199 
200 	err = rte_sched_port_dequeue(port, out_mbufs, 10);
201 	VERIFY(err == 10, "Wrong dequeue, err=%d\n", err);
202 
203 	for (i = 0; i < 10; i++) {
204 		enum rte_meter_color color;
205 		uint32_t subport, traffic_class, queue;
206 
207 		color = rte_sched_port_pkt_read_color(out_mbufs[i]);
208 		VERIFY(color == e_RTE_METER_YELLOW, "Wrong color\n");
209 
210 		rte_sched_port_pkt_read_tree_path(out_mbufs[i],
211 				&subport, &pipe, &traffic_class, &queue);
212 
213 		VERIFY(subport == SUBPORT, "Wrong subport\n");
214 		VERIFY(pipe == PIPE, "Wrong pipe\n");
215 		VERIFY(traffic_class == TC, "Wrong traffic_class\n");
216 		VERIFY(queue == QUEUE, "Wrong queue\n");
217 
218 	}
219 
220 
221 	struct rte_sched_subport_stats subport_stats;
222 	uint32_t tc_ov;
223 	rte_sched_subport_read_stats(port, SUBPORT, &subport_stats, &tc_ov);
224 	//VERIFY(subport_stats.n_pkts_tc[TC-1] == 10, "Wrong subport stats\n");
225 
226 	struct rte_sched_queue_stats queue_stats;
227 	uint16_t qlen;
228 	rte_sched_queue_read_stats(port, QUEUE, &queue_stats, &qlen);
229 	//VERIFY(queue_stats.n_pkts == 10, "Wrong queue stats\n");
230 
231 	rte_sched_port_free(port);
232 
233 	return 0;
234 }
235 
236 #else /* RTE_LIBRTE_SCHED */
237 
238 int
239 test_sched(void)
240 {
241 	printf("The Scheduler library is not included in this build\n");
242 	return 0;
243 }
244 #endif /* RTE_LIBRTE_SCHED */
245