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