New upstream version 18.02
[deb_dpdk.git] / examples / flow_filtering / main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 Mellanox.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Mellanox. nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <sys/queue.h>
40 #include <netinet/in.h>
41 #include <setjmp.h>
42 #include <stdarg.h>
43 #include <ctype.h>
44 #include <errno.h>
45 #include <getopt.h>
46 #include <signal.h>
47 #include <stdbool.h>
48
49 #include <rte_eal.h>
50 #include <rte_common.h>
51 #include <rte_malloc.h>
52 #include <rte_ether.h>
53 #include <rte_ethdev.h>
54 #include <rte_mempool.h>
55 #include <rte_mbuf.h>
56 #include <rte_net.h>
57 #include <rte_flow.h>
58 #include <rte_cycles.h>
59
60 static volatile bool force_quit;
61
62 static uint16_t port_id;
63 static uint16_t nr_queues = 5;
64 static uint8_t selected_queue = 1;
65 struct rte_mempool *mbuf_pool;
66 struct rte_flow *flow;
67
68 #define SRC_IP ((0<<24) + (0<<16) + (0<<8) + 0) /* src ip = 0.0.0.0 */
69 #define DEST_IP ((192<<24) + (168<<16) + (1<<8) + 1) /* dest ip = 192.168.1.1 */
70 #define FULL_MASK 0xffffffff /* full mask */
71 #define EMPTY_MASK 0x0 /* empty mask */
72
73 #include "flow_blocks.c"
74
75 static inline void
76 print_ether_addr(const char *what, struct ether_addr *eth_addr)
77 {
78         char buf[ETHER_ADDR_FMT_SIZE];
79         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
80         printf("%s%s", what, buf);
81 }
82
83 static void
84 main_loop(void)
85 {
86         struct rte_mbuf *mbufs[32];
87         struct ether_hdr *eth_hdr;
88         struct rte_flow_error error;
89         uint16_t nb_rx;
90         uint16_t i;
91         uint16_t j;
92
93         while (!force_quit) {
94                 for (i = 0; i < nr_queues; i++) {
95                         nb_rx = rte_eth_rx_burst(port_id,
96                                                 i, mbufs, 32);
97                         if (nb_rx) {
98                                 for (j = 0; j < nb_rx; j++) {
99                                         struct rte_mbuf *m = mbufs[j];
100
101                                         eth_hdr = rte_pktmbuf_mtod(m,
102                                                         struct ether_hdr *);
103                                         print_ether_addr("src=",
104                                                         &eth_hdr->s_addr);
105                                         print_ether_addr(" - dst=",
106                                                         &eth_hdr->d_addr);
107                                         printf(" - queue=0x%x",
108                                                         (unsigned int)i);
109                                         printf("\n");
110
111                                         rte_pktmbuf_free(m);
112                                 }
113                         }
114                 }
115         }
116
117         /* closing and releasing resources */
118         rte_flow_flush(port_id, &error);
119         rte_eth_dev_stop(port_id);
120         rte_eth_dev_close(port_id);
121 }
122
123 #define CHECK_INTERVAL 1000  /* 100ms */
124 #define MAX_REPEAT_TIMES 90  /* 9s (90 * 100ms) in total */
125
126 static void
127 assert_link_status(void)
128 {
129         struct rte_eth_link link;
130         uint8_t rep_cnt = MAX_REPEAT_TIMES;
131
132         memset(&link, 0, sizeof(link));
133         do {
134                 rte_eth_link_get(port_id, &link);
135                 if (link.link_status == ETH_LINK_UP)
136                         break;
137                 rte_delay_ms(CHECK_INTERVAL);
138         } while (--rep_cnt);
139
140         if (link.link_status == ETH_LINK_DOWN)
141                 rte_exit(EXIT_FAILURE, ":: error: link is still down\n");
142 }
143
144 static void
145 init_port(void)
146 {
147         int ret;
148         uint16_t i;
149         struct rte_eth_conf port_conf = {
150                 .rxmode = {
151                         .split_hdr_size = 0,
152                         .ignore_offload_bitfield = 1,
153                         .offloads = DEV_RX_OFFLOAD_CRC_STRIP,
154                 },
155                 .txmode = {
156                         .offloads =
157                                 DEV_TX_OFFLOAD_VLAN_INSERT |
158                                 DEV_TX_OFFLOAD_IPV4_CKSUM  |
159                                 DEV_TX_OFFLOAD_UDP_CKSUM   |
160                                 DEV_TX_OFFLOAD_TCP_CKSUM   |
161                                 DEV_TX_OFFLOAD_SCTP_CKSUM  |
162                                 DEV_TX_OFFLOAD_TCP_TSO,
163                 },
164         };
165         struct rte_eth_txconf txq_conf;
166         struct rte_eth_rxconf rxq_conf;
167         struct rte_eth_dev_info dev_info;
168
169         printf(":: initializing port: %d\n", port_id);
170         ret = rte_eth_dev_configure(port_id,
171                                 nr_queues, nr_queues, &port_conf);
172         if (ret < 0) {
173                 rte_exit(EXIT_FAILURE,
174                         ":: cannot configure device: err=%d, port=%u\n",
175                         ret, port_id);
176         }
177
178         rte_eth_dev_info_get(port_id, &dev_info);
179         rxq_conf = dev_info.default_rxconf;
180         rxq_conf.offloads = port_conf.rxmode.offloads;
181         /* only set Rx queues: something we care only so far */
182         for (i = 0; i < nr_queues; i++) {
183                 ret = rte_eth_rx_queue_setup(port_id, i, 512,
184                                      rte_eth_dev_socket_id(port_id),
185                                      &rxq_conf,
186                                      mbuf_pool);
187                 if (ret < 0) {
188                         rte_exit(EXIT_FAILURE,
189                                 ":: Rx queue setup failed: err=%d, port=%u\n",
190                                 ret, port_id);
191                 }
192         }
193
194         txq_conf = dev_info.default_txconf;
195         txq_conf.offloads = port_conf.txmode.offloads;
196
197         for (i = 0; i < nr_queues; i++) {
198                 ret = rte_eth_tx_queue_setup(port_id, i, 512,
199                                 rte_eth_dev_socket_id(port_id),
200                                 &txq_conf);
201                 if (ret < 0) {
202                         rte_exit(EXIT_FAILURE,
203                                 ":: Tx queue setup failed: err=%d, port=%u\n",
204                                 ret, port_id);
205                 }
206         }
207
208         rte_eth_promiscuous_enable(port_id);
209         ret = rte_eth_dev_start(port_id);
210         if (ret < 0) {
211                 rte_exit(EXIT_FAILURE,
212                         "rte_eth_dev_start:err=%d, port=%u\n",
213                         ret, port_id);
214         }
215
216         assert_link_status();
217
218         printf(":: initializing port: %d done\n", port_id);
219 }
220
221 static void
222 signal_handler(int signum)
223 {
224         if (signum == SIGINT || signum == SIGTERM) {
225                 printf("\n\nSignal %d received, preparing to exit...\n",
226                                 signum);
227                 force_quit = true;
228         }
229 }
230
231 int
232 main(int argc, char **argv)
233 {
234         int ret;
235         uint8_t nr_ports;
236         struct rte_flow_error error;
237
238         ret = rte_eal_init(argc, argv);
239         if (ret < 0)
240                 rte_exit(EXIT_FAILURE, ":: invalid EAL arguments\n");
241
242         force_quit = false;
243         signal(SIGINT, signal_handler);
244         signal(SIGTERM, signal_handler);
245
246         nr_ports = rte_eth_dev_count();
247         if (nr_ports == 0)
248                 rte_exit(EXIT_FAILURE, ":: no Ethernet ports found\n");
249         port_id = 0;
250         if (nr_ports != 1) {
251                 printf(":: warn: %d ports detected, but we use only one: port %u\n",
252                         nr_ports, port_id);
253         }
254         mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", 4096, 128, 0,
255                                             RTE_MBUF_DEFAULT_BUF_SIZE,
256                                             rte_socket_id());
257         if (mbuf_pool == NULL)
258                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
259
260         init_port();
261
262         /* create flow for send packet with */
263         flow = generate_ipv4_flow(port_id, selected_queue,
264                                 SRC_IP, EMPTY_MASK,
265                                 DEST_IP, FULL_MASK, &error);
266         if (!flow) {
267                 printf("Flow can't be created %d message: %s\n",
268                         error.type,
269                         error.message ? error.message : "(no stated reason)");
270                 rte_exit(EXIT_FAILURE, "error in creating flow");
271         }
272
273         main_loop();
274
275         return 0;
276 }