60c62de34acf8cce30eb5e33c12171739011c49b
[dpdk.git] / app / test / test_sched.c
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 "test.h"
41
42 #include <rte_cycles.h>
43 #include <rte_ether.h>
44 #include <rte_ip.h>
45 #include <rte_byteorder.h>
46 #include <rte_sched.h>
47
48
49 #define SUBPORT         0
50 #define PIPE            1
51 #define TC                      2
52 #define QUEUE           3
53
54 static struct rte_sched_subport_params subport_param[] = {
55         {
56                 .tb_rate = 1250000000,
57                 .tb_size = 1000000,
58
59                 .tc_rate = {1250000000, 1250000000, 1250000000, 1250000000},
60                 .tc_period = 10,
61         },
62 };
63
64 static struct rte_sched_pipe_params pipe_profile[] = {
65         { /* Profile #0 */
66                 .tb_rate = 305175,
67                 .tb_size = 1000000,
68
69                 .tc_rate = {305175, 305175, 305175, 305175},
70                 .tc_period = 40,
71
72                 .wrr_weights = {1, 1, 1, 1,  1, 1, 1, 1,  1, 1, 1, 1,  1, 1, 1, 1},
73         },
74 };
75
76 static struct rte_sched_port_params port_param = {
77         .socket = 0, /* computed */
78         .rate = 0, /* computed */
79         .mtu = 1522,
80         .frame_overhead = RTE_SCHED_FRAME_OVERHEAD_DEFAULT,
81         .n_subports_per_port = 1,
82         .n_pipes_per_subport = 4096,
83         .qsize = {64, 64, 64, 64},
84         .pipe_profiles = pipe_profile,
85         .n_pipe_profiles = 1,
86 };
87
88 #define NB_MBUF          32
89 #define MAX_PACKET_SZ    2048
90 #define MBUF_SZ (MAX_PACKET_SZ + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
91 #define PKT_BURST_SZ     32
92 #define MEMPOOL_CACHE_SZ PKT_BURST_SZ
93 #define SOCKET           0
94
95
96 static struct rte_mempool *
97 create_mempool(void)
98 {
99         struct rte_mempool * mp;
100
101         mp = rte_mempool_lookup("test_sched");
102         if (!mp)
103                 mp = rte_mempool_create("test_sched",
104                                 NB_MBUF,
105                                 MBUF_SZ,
106                                 MEMPOOL_CACHE_SZ,
107                                 sizeof(struct rte_pktmbuf_pool_private),
108                                 rte_pktmbuf_pool_init,
109                                 NULL,
110                                 rte_pktmbuf_init,
111                                 NULL,
112                                 SOCKET,
113                                 0);
114
115         return mp;
116 }
117
118 static void
119 prepare_pkt(struct rte_mbuf *mbuf)
120 {
121         struct ether_hdr *eth_hdr;
122         struct vlan_hdr *vlan1, *vlan2;
123         struct ipv4_hdr *ip_hdr;
124
125         /* Simulate a classifier */
126         eth_hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
127         vlan1 = (struct vlan_hdr *)(&eth_hdr->ether_type );
128         vlan2 = (struct vlan_hdr *)((uintptr_t)&eth_hdr->ether_type + sizeof(struct vlan_hdr));
129         eth_hdr = (struct ether_hdr *)((uintptr_t)&eth_hdr->ether_type + 2 *sizeof(struct vlan_hdr));
130         ip_hdr = (struct ipv4_hdr *)((uintptr_t)eth_hdr +  sizeof(eth_hdr->ether_type));
131
132         vlan1->vlan_tci = rte_cpu_to_be_16(SUBPORT);
133         vlan2->vlan_tci = rte_cpu_to_be_16(PIPE);
134         eth_hdr->ether_type =  rte_cpu_to_be_16(ETHER_TYPE_IPv4);
135         ip_hdr->dst_addr = IPv4(0,0,TC,QUEUE);
136
137
138         rte_sched_port_pkt_write(mbuf, SUBPORT, PIPE, TC, QUEUE, e_RTE_METER_YELLOW);
139
140         /* 64 byte packet */
141         mbuf->pkt_len  = 60;
142         mbuf->data_len = 60;
143 }
144
145
146 /**
147  * test main entrance for library sched
148  */
149 static int
150 test_sched(void)
151 {
152         struct rte_mempool *mp = NULL;
153         struct rte_sched_port *port = NULL;
154         uint32_t pipe;
155         struct rte_mbuf *in_mbufs[10];
156         struct rte_mbuf *out_mbufs[10];
157         int i;
158
159         int err;
160
161         mp = create_mempool();
162         TEST_ASSERT_NOT_NULL(mp, "Error creating mempool\n");
163
164         port_param.socket = 0;
165         port_param.rate = (uint64_t) 10000 * 1000 * 1000 / 8;
166
167         port = rte_sched_port_config(&port_param);
168         TEST_ASSERT_NOT_NULL(port, "Error config sched port\n");
169
170         err = rte_sched_subport_config(port, SUBPORT, subport_param);
171         TEST_ASSERT_SUCCESS(err, "Error config sched, err=%d\n", err);
172
173         for (pipe = 0; pipe < port_param.n_pipes_per_subport; pipe ++) {
174                 err = rte_sched_pipe_config(port, SUBPORT, pipe, 0);
175                 TEST_ASSERT_SUCCESS(err, "Error config sched pipe %u, err=%d\n", pipe, err);
176         }
177
178         for (i = 0; i < 10; i++) {
179                 in_mbufs[i] = rte_pktmbuf_alloc(mp);
180                 TEST_ASSERT_NOT_NULL(in_mbufs[i], "Packet allocation failed\n");
181                 prepare_pkt(in_mbufs[i]);
182         }
183
184
185         err = rte_sched_port_enqueue(port, in_mbufs, 10);
186         TEST_ASSERT_EQUAL(err, 10, "Wrong enqueue, err=%d\n", err);
187
188         err = rte_sched_port_dequeue(port, out_mbufs, 10);
189         TEST_ASSERT_EQUAL(err, 10, "Wrong dequeue, err=%d\n", err);
190
191         for (i = 0; i < 10; i++) {
192                 enum rte_meter_color color;
193                 uint32_t subport, traffic_class, queue;
194
195                 color = rte_sched_port_pkt_read_color(out_mbufs[i]);
196                 TEST_ASSERT_EQUAL(color, e_RTE_METER_YELLOW, "Wrong color\n");
197
198                 rte_sched_port_pkt_read_tree_path(out_mbufs[i],
199                                 &subport, &pipe, &traffic_class, &queue);
200
201                 TEST_ASSERT_EQUAL(subport, SUBPORT, "Wrong subport\n");
202                 TEST_ASSERT_EQUAL(pipe, PIPE, "Wrong pipe\n");
203                 TEST_ASSERT_EQUAL(traffic_class, TC, "Wrong traffic_class\n");
204                 TEST_ASSERT_EQUAL(queue, QUEUE, "Wrong queue\n");
205
206         }
207
208
209         struct rte_sched_subport_stats subport_stats;
210         uint32_t tc_ov;
211         rte_sched_subport_read_stats(port, SUBPORT, &subport_stats, &tc_ov);
212 #if 0
213         TEST_ASSERT_EQUAL(subport_stats.n_pkts_tc[TC-1], 10, "Wrong subport stats\n");
214 #endif
215         struct rte_sched_queue_stats queue_stats;
216         uint16_t qlen;
217         rte_sched_queue_read_stats(port, QUEUE, &queue_stats, &qlen);
218 #if 0
219         TEST_ASSERT_EQUAL(queue_stats.n_pkts, 10, "Wrong queue stats\n");
220 #endif
221
222         rte_sched_port_free(port);
223
224         return 0;
225 }
226
227 static struct test_command sched_cmd = {
228         .command = "sched_autotest",
229         .callback = test_sched,
230 };
231 REGISTER_TEST_COMMAND(sched_cmd);