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