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