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