]> git.proxmox.com Git - ceph.git/blob - ceph/src/dpdk/examples/qos_meter/main.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / dpdk / examples / qos_meter / main.c
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright(c) 2010-2016 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 <stdio.h>
35 #include <getopt.h>
36
37 #include <rte_common.h>
38 #include <rte_eal.h>
39 #include <rte_malloc.h>
40 #include <rte_mempool.h>
41 #include <rte_ethdev.h>
42 #include <rte_cycles.h>
43 #include <rte_mbuf.h>
44 #include <rte_meter.h>
45
46 /*
47 * Traffic metering configuration
48 *
49 */
50 #define APP_MODE_FWD 0
51 #define APP_MODE_SRTCM_COLOR_BLIND 1
52 #define APP_MODE_SRTCM_COLOR_AWARE 2
53 #define APP_MODE_TRTCM_COLOR_BLIND 3
54 #define APP_MODE_TRTCM_COLOR_AWARE 4
55
56 #define APP_MODE APP_MODE_SRTCM_COLOR_BLIND
57
58
59 #include "main.h"
60
61
62 #define APP_PKT_FLOW_POS 33
63 #define APP_PKT_COLOR_POS 5
64
65
66 #if APP_PKT_FLOW_POS > 64 || APP_PKT_COLOR_POS > 64
67 #error Byte offset needs to be less than 64
68 #endif
69
70 /*
71 * Buffer pool configuration
72 *
73 ***/
74 #define NB_MBUF 8192
75 #define MEMPOOL_CACHE_SIZE 256
76
77 static struct rte_mempool *pool = NULL;
78
79 /*
80 * NIC configuration
81 *
82 ***/
83 static struct rte_eth_conf port_conf = {
84 .rxmode = {
85 .mq_mode = ETH_MQ_RX_RSS,
86 .max_rx_pkt_len = ETHER_MAX_LEN,
87 .split_hdr_size = 0,
88 .header_split = 0,
89 .hw_ip_checksum = 1,
90 .hw_vlan_filter = 0,
91 .jumbo_frame = 0,
92 .hw_strip_crc = 0,
93 },
94 .rx_adv_conf = {
95 .rss_conf = {
96 .rss_key = NULL,
97 .rss_hf = ETH_RSS_IP,
98 },
99 },
100 .txmode = {
101 .mq_mode = ETH_DCB_NONE,
102 },
103 };
104
105 #define NIC_RX_QUEUE_DESC 128
106 #define NIC_TX_QUEUE_DESC 512
107
108 #define NIC_RX_QUEUE 0
109 #define NIC_TX_QUEUE 0
110
111 /*
112 * Packet RX/TX
113 *
114 ***/
115 #define PKT_RX_BURST_MAX 32
116 #define PKT_TX_BURST_MAX 32
117 #define TIME_TX_DRAIN 200000ULL
118
119 static uint8_t port_rx;
120 static uint8_t port_tx;
121 static struct rte_mbuf *pkts_rx[PKT_RX_BURST_MAX];
122 struct rte_eth_dev_tx_buffer *tx_buffer;
123
124 struct rte_meter_srtcm_params app_srtcm_params[] = {
125 {.cir = 1000000 * 46, .cbs = 2048, .ebs = 2048},
126 };
127
128 struct rte_meter_trtcm_params app_trtcm_params[] = {
129 {.cir = 1000000 * 46, .pir = 1500000 * 46, .cbs = 2048, .pbs = 2048},
130 };
131
132 #define APP_FLOWS_MAX 256
133
134 FLOW_METER app_flows[APP_FLOWS_MAX];
135
136 static int
137 app_configure_flow_table(void)
138 {
139 uint32_t i, j;
140 int ret;
141
142 for (i = 0, j = 0; i < APP_FLOWS_MAX;
143 i ++, j = (j + 1) % RTE_DIM(PARAMS)) {
144 ret = FUNC_CONFIG(&app_flows[i], &PARAMS[j]);
145 if (ret)
146 return ret;
147 }
148
149 return 0;
150 }
151
152 static inline void
153 app_set_pkt_color(uint8_t *pkt_data, enum policer_action color)
154 {
155 pkt_data[APP_PKT_COLOR_POS] = (uint8_t)color;
156 }
157
158 static inline int
159 app_pkt_handle(struct rte_mbuf *pkt, uint64_t time)
160 {
161 uint8_t input_color, output_color;
162 uint8_t *pkt_data = rte_pktmbuf_mtod(pkt, uint8_t *);
163 uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt) - sizeof(struct ether_hdr);
164 uint8_t flow_id = (uint8_t)(pkt_data[APP_PKT_FLOW_POS] & (APP_FLOWS_MAX - 1));
165 input_color = pkt_data[APP_PKT_COLOR_POS];
166 enum policer_action action;
167
168 /* color input is not used for blind modes */
169 output_color = (uint8_t) FUNC_METER(&app_flows[flow_id], time, pkt_len,
170 (enum rte_meter_color) input_color);
171
172 /* Apply policing and set the output color */
173 action = policer_table[input_color][output_color];
174 app_set_pkt_color(pkt_data, action);
175
176 return action;
177 }
178
179
180 static __attribute__((noreturn)) int
181 main_loop(__attribute__((unused)) void *dummy)
182 {
183 uint64_t current_time, last_time = rte_rdtsc();
184 uint32_t lcore_id = rte_lcore_id();
185
186 printf("Core %u: port RX = %d, port TX = %d\n", lcore_id, port_rx, port_tx);
187
188 while (1) {
189 uint64_t time_diff;
190 int i, nb_rx;
191
192 /* Mechanism to avoid stale packets in the output buffer */
193 current_time = rte_rdtsc();
194 time_diff = current_time - last_time;
195 if (unlikely(time_diff > TIME_TX_DRAIN)) {
196 /* Flush tx buffer */
197 rte_eth_tx_buffer_flush(port_tx, NIC_TX_QUEUE, tx_buffer);
198 last_time = current_time;
199 }
200
201 /* Read packet burst from NIC RX */
202 nb_rx = rte_eth_rx_burst(port_rx, NIC_RX_QUEUE, pkts_rx, PKT_RX_BURST_MAX);
203
204 /* Handle packets */
205 for (i = 0; i < nb_rx; i ++) {
206 struct rte_mbuf *pkt = pkts_rx[i];
207
208 /* Handle current packet */
209 if (app_pkt_handle(pkt, current_time) == DROP)
210 rte_pktmbuf_free(pkt);
211 else
212 rte_eth_tx_buffer(port_tx, NIC_TX_QUEUE, tx_buffer, pkt);
213 }
214 }
215 }
216
217 static void
218 print_usage(const char *prgname)
219 {
220 printf ("%s [EAL options] -- -p PORTMASK\n"
221 " -p PORTMASK: hexadecimal bitmask of ports to configure\n",
222 prgname);
223 }
224
225 static int
226 parse_portmask(const char *portmask)
227 {
228 char *end = NULL;
229 unsigned long pm;
230
231 /* parse hexadecimal string */
232 pm = strtoul(portmask, &end, 16);
233 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
234 return -1;
235
236 if (pm == 0)
237 return -1;
238
239 return pm;
240 }
241
242 /* Parse the argument given in the command line of the application */
243 static int
244 parse_args(int argc, char **argv)
245 {
246 int opt;
247 char **argvopt;
248 int option_index;
249 char *prgname = argv[0];
250 static struct option lgopts[] = {
251 {NULL, 0, 0, 0}
252 };
253 uint64_t port_mask, i, mask;
254
255 argvopt = argv;
256
257 while ((opt = getopt_long(argc, argvopt, "p:", lgopts, &option_index)) != EOF) {
258 switch (opt) {
259 case 'p':
260 port_mask = parse_portmask(optarg);
261 if (port_mask == 0) {
262 printf("invalid port mask (null port mask)\n");
263 print_usage(prgname);
264 return -1;
265 }
266
267 for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
268 if (mask & port_mask){
269 port_rx = i;
270 port_mask &= ~ mask;
271 break;
272 }
273 }
274
275 for (i = 0, mask = 1; i < 64; i ++, mask <<= 1){
276 if (mask & port_mask){
277 port_tx = i;
278 port_mask &= ~ mask;
279 break;
280 }
281 }
282
283 if (port_mask != 0) {
284 printf("invalid port mask (more than 2 ports)\n");
285 print_usage(prgname);
286 return -1;
287 }
288 break;
289
290 default:
291 print_usage(prgname);
292 return -1;
293 }
294 }
295
296 if (optind <= 1) {
297 print_usage(prgname);
298 return -1;
299 }
300
301 argv[optind-1] = prgname;
302
303 optind = 0; /* reset getopt lib */
304 return 0;
305 }
306
307 int
308 main(int argc, char **argv)
309 {
310 uint32_t lcore_id;
311 int ret;
312
313 /* EAL init */
314 ret = rte_eal_init(argc, argv);
315 if (ret < 0)
316 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
317 argc -= ret;
318 argv += ret;
319 if (rte_lcore_count() != 1) {
320 rte_exit(EXIT_FAILURE, "This application does not accept more than one core. "
321 "Please adjust the \"-c COREMASK\" parameter accordingly.\n");
322 }
323
324 /* Application non-EAL arguments parse */
325 ret = parse_args(argc, argv);
326 if (ret < 0)
327 rte_exit(EXIT_FAILURE, "Invalid input arguments\n");
328
329 /* Buffer pool init */
330 pool = rte_pktmbuf_pool_create("pool", NB_MBUF, MEMPOOL_CACHE_SIZE,
331 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
332 if (pool == NULL)
333 rte_exit(EXIT_FAILURE, "Buffer pool creation error\n");
334
335 /* NIC init */
336 ret = rte_eth_dev_configure(port_rx, 1, 1, &port_conf);
337 if (ret < 0)
338 rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_rx, ret);
339
340 ret = rte_eth_rx_queue_setup(port_rx, NIC_RX_QUEUE, NIC_RX_QUEUE_DESC,
341 rte_eth_dev_socket_id(port_rx),
342 NULL, pool);
343 if (ret < 0)
344 rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_rx, ret);
345
346 ret = rte_eth_tx_queue_setup(port_rx, NIC_TX_QUEUE, NIC_TX_QUEUE_DESC,
347 rte_eth_dev_socket_id(port_rx),
348 NULL);
349 if (ret < 0)
350 rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_rx, ret);
351
352 ret = rte_eth_dev_configure(port_tx, 1, 1, &port_conf);
353 if (ret < 0)
354 rte_exit(EXIT_FAILURE, "Port %d configuration error (%d)\n", port_tx, ret);
355
356 ret = rte_eth_rx_queue_setup(port_tx, NIC_RX_QUEUE, NIC_RX_QUEUE_DESC,
357 rte_eth_dev_socket_id(port_tx),
358 NULL, pool);
359 if (ret < 0)
360 rte_exit(EXIT_FAILURE, "Port %d RX queue setup error (%d)\n", port_tx, ret);
361
362 ret = rte_eth_tx_queue_setup(port_tx, NIC_TX_QUEUE, NIC_TX_QUEUE_DESC,
363 rte_eth_dev_socket_id(port_tx),
364 NULL);
365 if (ret < 0)
366 rte_exit(EXIT_FAILURE, "Port %d TX queue setup error (%d)\n", port_tx, ret);
367
368 tx_buffer = rte_zmalloc_socket("tx_buffer",
369 RTE_ETH_TX_BUFFER_SIZE(PKT_TX_BURST_MAX), 0,
370 rte_eth_dev_socket_id(port_tx));
371 if (tx_buffer == NULL)
372 rte_exit(EXIT_FAILURE, "Port %d TX buffer allocation error\n",
373 port_tx);
374
375 rte_eth_tx_buffer_init(tx_buffer, PKT_TX_BURST_MAX);
376
377 ret = rte_eth_dev_start(port_rx);
378 if (ret < 0)
379 rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_rx, ret);
380
381 ret = rte_eth_dev_start(port_tx);
382 if (ret < 0)
383 rte_exit(EXIT_FAILURE, "Port %d start error (%d)\n", port_tx, ret);
384
385 rte_eth_promiscuous_enable(port_rx);
386
387 rte_eth_promiscuous_enable(port_tx);
388
389 /* App configuration */
390 ret = app_configure_flow_table();
391 if (ret < 0)
392 rte_exit(EXIT_FAILURE, "Invalid configure flow table\n");
393
394 /* Launch per-lcore init on every lcore */
395 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
396 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
397 if (rte_eal_wait_lcore(lcore_id) < 0)
398 return -1;
399 }
400
401 return 0;
402 }