1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation 3 */ 4 5 #include <stdint.h> 6 7 #include <rte_log.h> 8 #include <rte_mbuf.h> 9 #include <rte_malloc.h> 10 #include <rte_cycles.h> 11 #include <rte_ethdev.h> 12 #include <rte_memcpy.h> 13 #include <rte_byteorder.h> 14 #include <rte_branch_prediction.h> 15 #include <rte_sched.h> 16 17 #include "main.h" 18 19 /* 20 * QoS parameters are encoded as follows: 21 * Outer VLAN ID defines subport 22 * Inner VLAN ID defines pipe 23 * Destination IP 0.0.XXX.0 defines traffic class 24 * Destination IP host (0.0.0.XXX) defines queue 25 * Values below define offset to each field from start of frame 26 */ 27 #define SUBPORT_OFFSET 7 28 #define PIPE_OFFSET 9 29 #define TC_OFFSET 20 30 #define QUEUE_OFFSET 20 31 #define COLOR_OFFSET 19 32 33 static inline int 34 get_pkt_sched(struct rte_mbuf *m, uint32_t *subport, uint32_t *pipe, 35 uint32_t *traffic_class, uint32_t *queue, uint32_t *color) 36 { 37 uint16_t *pdata = rte_pktmbuf_mtod(m, uint16_t *); 38 39 *subport = (rte_be_to_cpu_16(pdata[SUBPORT_OFFSET]) & 0x0FFF) & 40 (port_params.n_subports_per_port - 1); /* Outer VLAN ID*/ 41 *pipe = (rte_be_to_cpu_16(pdata[PIPE_OFFSET]) & 0x0FFF) & 42 (port_params.n_pipes_per_subport - 1); /* Inner VLAN ID */ 43 *traffic_class = (pdata[QUEUE_OFFSET] & 0x0F) & 44 (RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE - 1); /* Destination IP */ 45 *queue = ((pdata[QUEUE_OFFSET] >> 8) & 0x0F) & 46 (RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS - 1) ; /* Destination IP */ 47 *color = pdata[COLOR_OFFSET] & 0x03; /* Destination IP */ 48 49 return 0; 50 } 51 52 void 53 app_rx_thread(struct thread_conf **confs) 54 { 55 uint32_t i, nb_rx; 56 struct rte_mbuf *rx_mbufs[burst_conf.rx_burst] __rte_cache_aligned; 57 struct thread_conf *conf; 58 int conf_idx = 0; 59 60 uint32_t subport; 61 uint32_t pipe; 62 uint32_t traffic_class; 63 uint32_t queue; 64 uint32_t color; 65 66 while ((conf = confs[conf_idx])) { 67 nb_rx = rte_eth_rx_burst(conf->rx_port, conf->rx_queue, rx_mbufs, 68 burst_conf.rx_burst); 69 70 if (likely(nb_rx != 0)) { 71 APP_STATS_ADD(conf->stat.nb_rx, nb_rx); 72 73 for(i = 0; i < nb_rx; i++) { 74 get_pkt_sched(rx_mbufs[i], 75 &subport, &pipe, &traffic_class, &queue, &color); 76 rte_sched_port_pkt_write(rx_mbufs[i], subport, pipe, 77 traffic_class, queue, (enum rte_meter_color) color); 78 } 79 80 if (unlikely(rte_ring_sp_enqueue_bulk(conf->rx_ring, 81 (void **)rx_mbufs, nb_rx, NULL) == 0)) { 82 for(i = 0; i < nb_rx; i++) { 83 rte_pktmbuf_free(rx_mbufs[i]); 84 85 APP_STATS_ADD(conf->stat.nb_drop, 1); 86 } 87 } 88 } 89 conf_idx++; 90 if (confs[conf_idx] == NULL) 91 conf_idx = 0; 92 } 93 } 94 95 96 97 /* Send the packet to an output interface 98 * For performance reason function returns number of packets dropped, not sent, 99 * so 0 means that all packets were sent successfully 100 */ 101 102 static inline void 103 app_send_burst(struct thread_conf *qconf) 104 { 105 struct rte_mbuf **mbufs; 106 uint32_t n, ret; 107 108 mbufs = (struct rte_mbuf **)qconf->m_table; 109 n = qconf->n_mbufs; 110 111 do { 112 ret = rte_eth_tx_burst(qconf->tx_port, qconf->tx_queue, mbufs, (uint16_t)n); 113 /* we cannot drop the packets, so re-send */ 114 /* update number of packets to be sent */ 115 n -= ret; 116 mbufs = (struct rte_mbuf **)&mbufs[ret]; 117 } while (n); 118 } 119 120 121 /* Send the packet to an output interface */ 122 static void 123 app_send_packets(struct thread_conf *qconf, struct rte_mbuf **mbufs, uint32_t nb_pkt) 124 { 125 uint32_t i, len; 126 127 len = qconf->n_mbufs; 128 for(i = 0; i < nb_pkt; i++) { 129 qconf->m_table[len] = mbufs[i]; 130 len++; 131 /* enough pkts to be sent */ 132 if (unlikely(len == burst_conf.tx_burst)) { 133 qconf->n_mbufs = len; 134 app_send_burst(qconf); 135 len = 0; 136 } 137 } 138 139 qconf->n_mbufs = len; 140 } 141 142 void 143 app_tx_thread(struct thread_conf **confs) 144 { 145 struct rte_mbuf *mbufs[burst_conf.qos_dequeue]; 146 struct thread_conf *conf; 147 int conf_idx = 0; 148 int retval; 149 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US; 150 151 while ((conf = confs[conf_idx])) { 152 retval = rte_ring_sc_dequeue_bulk(conf->tx_ring, (void **)mbufs, 153 burst_conf.qos_dequeue, NULL); 154 if (likely(retval != 0)) { 155 app_send_packets(conf, mbufs, burst_conf.qos_dequeue); 156 157 conf->counter = 0; /* reset empty read loop counter */ 158 } 159 160 conf->counter++; 161 162 /* drain ring and TX queues */ 163 if (unlikely(conf->counter > drain_tsc)) { 164 /* now check is there any packets left to be transmitted */ 165 if (conf->n_mbufs != 0) { 166 app_send_burst(conf); 167 168 conf->n_mbufs = 0; 169 } 170 conf->counter = 0; 171 } 172 173 conf_idx++; 174 if (confs[conf_idx] == NULL) 175 conf_idx = 0; 176 } 177 } 178 179 180 void 181 app_worker_thread(struct thread_conf **confs) 182 { 183 struct rte_mbuf *mbufs[burst_conf.ring_burst]; 184 struct thread_conf *conf; 185 int conf_idx = 0; 186 187 while ((conf = confs[conf_idx])) { 188 uint32_t nb_pkt; 189 190 /* Read packet from the ring */ 191 nb_pkt = rte_ring_sc_dequeue_burst(conf->rx_ring, (void **)mbufs, 192 burst_conf.ring_burst, NULL); 193 if (likely(nb_pkt)) { 194 int nb_sent = rte_sched_port_enqueue(conf->sched_port, mbufs, 195 nb_pkt); 196 197 APP_STATS_ADD(conf->stat.nb_drop, nb_pkt - nb_sent); 198 APP_STATS_ADD(conf->stat.nb_rx, nb_pkt); 199 } 200 201 nb_pkt = rte_sched_port_dequeue(conf->sched_port, mbufs, 202 burst_conf.qos_dequeue); 203 if (likely(nb_pkt > 0)) 204 while (rte_ring_sp_enqueue_bulk(conf->tx_ring, 205 (void **)mbufs, nb_pkt, NULL) == 0) 206 ; /* empty body */ 207 208 conf_idx++; 209 if (confs[conf_idx] == NULL) 210 conf_idx = 0; 211 } 212 } 213 214 215 void 216 app_mixed_thread(struct thread_conf **confs) 217 { 218 struct rte_mbuf *mbufs[burst_conf.ring_burst]; 219 struct thread_conf *conf; 220 int conf_idx = 0; 221 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US; 222 223 while ((conf = confs[conf_idx])) { 224 uint32_t nb_pkt; 225 226 /* Read packet from the ring */ 227 nb_pkt = rte_ring_sc_dequeue_burst(conf->rx_ring, (void **)mbufs, 228 burst_conf.ring_burst, NULL); 229 if (likely(nb_pkt)) { 230 int nb_sent = rte_sched_port_enqueue(conf->sched_port, mbufs, 231 nb_pkt); 232 233 APP_STATS_ADD(conf->stat.nb_drop, nb_pkt - nb_sent); 234 APP_STATS_ADD(conf->stat.nb_rx, nb_pkt); 235 } 236 237 238 nb_pkt = rte_sched_port_dequeue(conf->sched_port, mbufs, 239 burst_conf.qos_dequeue); 240 if (likely(nb_pkt > 0)) { 241 app_send_packets(conf, mbufs, nb_pkt); 242 243 conf->counter = 0; /* reset empty read loop counter */ 244 } 245 246 conf->counter++; 247 248 /* drain ring and TX queues */ 249 if (unlikely(conf->counter > drain_tsc)) { 250 251 /* now check is there any packets left to be transmitted */ 252 if (conf->n_mbufs != 0) { 253 app_send_burst(conf); 254 255 conf->n_mbufs = 0; 256 } 257 conf->counter = 0; 258 } 259 260 conf_idx++; 261 if (confs[conf_idx] == NULL) 262 conf_idx = 0; 263 } 264 } 265