build: use static deps for pkg-config libs.private
[dpdk.git] / examples / qos_sched / app_thread.c
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(conf->sched_port,
77                                                 rx_mbufs[i],
78                                                 subport, pipe,
79                                                 traffic_class, queue,
80                                                 (enum rte_meter_color) color);
81                         }
82
83                         if (unlikely(rte_ring_sp_enqueue_bulk(conf->rx_ring,
84                                         (void **)rx_mbufs, nb_rx, NULL) == 0)) {
85                                 for(i = 0; i < nb_rx; i++) {
86                                         rte_pktmbuf_free(rx_mbufs[i]);
87
88                                         APP_STATS_ADD(conf->stat.nb_drop, 1);
89                                 }
90                         }
91                 }
92                 conf_idx++;
93                 if (confs[conf_idx] == NULL)
94                         conf_idx = 0;
95         }
96 }
97
98
99
100 /* Send the packet to an output interface
101  * For performance reason function returns number of packets dropped, not sent,
102  * so 0 means that all packets were sent successfully
103  */
104
105 static inline void
106 app_send_burst(struct thread_conf *qconf)
107 {
108         struct rte_mbuf **mbufs;
109         uint32_t n, ret;
110
111         mbufs = (struct rte_mbuf **)qconf->m_table;
112         n = qconf->n_mbufs;
113
114         do {
115                 ret = rte_eth_tx_burst(qconf->tx_port, qconf->tx_queue, mbufs, (uint16_t)n);
116                 /* we cannot drop the packets, so re-send */
117                 /* update number of packets to be sent */
118                 n -= ret;
119                 mbufs = (struct rte_mbuf **)&mbufs[ret];
120         } while (n);
121 }
122
123
124 /* Send the packet to an output interface */
125 static void
126 app_send_packets(struct thread_conf *qconf, struct rte_mbuf **mbufs, uint32_t nb_pkt)
127 {
128         uint32_t i, len;
129
130         len = qconf->n_mbufs;
131         for(i = 0; i < nb_pkt; i++) {
132                 qconf->m_table[len] = mbufs[i];
133                 len++;
134                 /* enough pkts to be sent */
135                 if (unlikely(len == burst_conf.tx_burst)) {
136                         qconf->n_mbufs = len;
137                         app_send_burst(qconf);
138                         len = 0;
139                 }
140         }
141
142         qconf->n_mbufs = len;
143 }
144
145 void
146 app_tx_thread(struct thread_conf **confs)
147 {
148         struct rte_mbuf *mbufs[burst_conf.qos_dequeue];
149         struct thread_conf *conf;
150         int conf_idx = 0;
151         int retval;
152         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
153
154         while ((conf = confs[conf_idx])) {
155                 retval = rte_ring_sc_dequeue_bulk(conf->tx_ring, (void **)mbufs,
156                                         burst_conf.qos_dequeue, NULL);
157                 if (likely(retval != 0)) {
158                         app_send_packets(conf, mbufs, burst_conf.qos_dequeue);
159
160                         conf->counter = 0; /* reset empty read loop counter */
161                 }
162
163                 conf->counter++;
164
165                 /* drain ring and TX queues */
166                 if (unlikely(conf->counter > drain_tsc)) {
167                         /* now check is there any packets left to be transmitted */
168                         if (conf->n_mbufs != 0) {
169                                 app_send_burst(conf);
170
171                                 conf->n_mbufs = 0;
172                         }
173                         conf->counter = 0;
174                 }
175
176                 conf_idx++;
177                 if (confs[conf_idx] == NULL)
178                         conf_idx = 0;
179         }
180 }
181
182
183 void
184 app_worker_thread(struct thread_conf **confs)
185 {
186         struct rte_mbuf *mbufs[burst_conf.ring_burst];
187         struct thread_conf *conf;
188         int conf_idx = 0;
189
190         while ((conf = confs[conf_idx])) {
191                 uint32_t nb_pkt;
192
193                 /* Read packet from the ring */
194                 nb_pkt = rte_ring_sc_dequeue_burst(conf->rx_ring, (void **)mbufs,
195                                         burst_conf.ring_burst, NULL);
196                 if (likely(nb_pkt)) {
197                         int nb_sent = rte_sched_port_enqueue(conf->sched_port, mbufs,
198                                         nb_pkt);
199
200                         APP_STATS_ADD(conf->stat.nb_drop, nb_pkt - nb_sent);
201                         APP_STATS_ADD(conf->stat.nb_rx, nb_pkt);
202                 }
203
204                 nb_pkt = rte_sched_port_dequeue(conf->sched_port, mbufs,
205                                         burst_conf.qos_dequeue);
206                 if (likely(nb_pkt > 0))
207                         while (rte_ring_sp_enqueue_bulk(conf->tx_ring,
208                                         (void **)mbufs, nb_pkt, NULL) == 0)
209                                 ; /* empty body */
210
211                 conf_idx++;
212                 if (confs[conf_idx] == NULL)
213                         conf_idx = 0;
214         }
215 }
216
217
218 void
219 app_mixed_thread(struct thread_conf **confs)
220 {
221         struct rte_mbuf *mbufs[burst_conf.ring_burst];
222         struct thread_conf *conf;
223         int conf_idx = 0;
224         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
225
226         while ((conf = confs[conf_idx])) {
227                 uint32_t nb_pkt;
228
229                 /* Read packet from the ring */
230                 nb_pkt = rte_ring_sc_dequeue_burst(conf->rx_ring, (void **)mbufs,
231                                         burst_conf.ring_burst, NULL);
232                 if (likely(nb_pkt)) {
233                         int nb_sent = rte_sched_port_enqueue(conf->sched_port, mbufs,
234                                         nb_pkt);
235
236                         APP_STATS_ADD(conf->stat.nb_drop, nb_pkt - nb_sent);
237                         APP_STATS_ADD(conf->stat.nb_rx, nb_pkt);
238                 }
239
240
241                 nb_pkt = rte_sched_port_dequeue(conf->sched_port, mbufs,
242                                         burst_conf.qos_dequeue);
243                 if (likely(nb_pkt > 0)) {
244                         app_send_packets(conf, mbufs, nb_pkt);
245
246                         conf->counter = 0; /* reset empty read loop counter */
247                 }
248
249                 conf->counter++;
250
251                 /* drain ring and TX queues */
252                 if (unlikely(conf->counter > drain_tsc)) {
253
254                         /* now check is there any packets left to be transmitted */
255                         if (conf->n_mbufs != 0) {
256                                 app_send_burst(conf);
257
258                                 conf->n_mbufs = 0;
259                         }
260                         conf->counter = 0;
261                 }
262
263                 conf_idx++;
264                 if (confs[conf_idx] == NULL)
265                         conf_idx = 0;
266         }
267 }