test mbuf attach
[dpdk.git] / examples / flow_filtering / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 Mellanox Technologies, Ltd
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdint.h>
9 #include <inttypes.h>
10 #include <sys/types.h>
11 #include <sys/queue.h>
12 #include <netinet/in.h>
13 #include <setjmp.h>
14 #include <stdarg.h>
15 #include <ctype.h>
16 #include <errno.h>
17 #include <getopt.h>
18 #include <signal.h>
19 #include <stdbool.h>
20
21 #include <rte_eal.h>
22 #include <rte_common.h>
23 #include <rte_malloc.h>
24 #include <rte_ether.h>
25 #include <rte_ethdev.h>
26 #include <rte_mempool.h>
27 #include <rte_mbuf.h>
28 #include <rte_net.h>
29 #include <rte_flow.h>
30 #include <rte_cycles.h>
31
32 static volatile bool force_quit;
33
34 static uint16_t port_id;
35 static uint16_t nr_queues = 5;
36 static uint8_t selected_queue = 1;
37 struct rte_mempool *mbuf_pool;
38 struct rte_flow *flow;
39
40 #define SRC_IP ((0<<24) + (0<<16) + (0<<8) + 0) /* src ip = 0.0.0.0 */
41 #define DEST_IP ((192<<24) + (168<<16) + (1<<8) + 1) /* dest ip = 192.168.1.1 */
42 #define FULL_MASK 0xffffffff /* full mask */
43 #define EMPTY_MASK 0x0 /* empty mask */
44
45 #include "flow_blocks.c"
46
47 static inline void
48 print_ether_addr(const char *what, struct rte_ether_addr *eth_addr)
49 {
50         char buf[RTE_ETHER_ADDR_FMT_SIZE];
51         rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
52         printf("%s%s", what, buf);
53 }
54
55 static void
56 main_loop(void)
57 {
58         struct rte_mbuf *mbufs[32];
59         struct rte_ether_hdr *eth_hdr;
60         struct rte_flow_error error;
61         uint16_t nb_rx;
62         uint16_t i;
63         uint16_t j;
64
65         while (!force_quit) {
66                 for (i = 0; i < nr_queues; i++) {
67                         nb_rx = rte_eth_rx_burst(port_id,
68                                                 i, mbufs, 32);
69                         if (nb_rx) {
70                                 for (j = 0; j < nb_rx; j++) {
71                                         struct rte_mbuf *m = mbufs[j];
72
73                                         eth_hdr = rte_pktmbuf_mtod(m,
74                                                         struct rte_ether_hdr *);
75                                         print_ether_addr("src=",
76                                                         &eth_hdr->s_addr);
77                                         print_ether_addr(" - dst=",
78                                                         &eth_hdr->d_addr);
79                                         printf(" - queue=0x%x",
80                                                         (unsigned int)i);
81                                         printf("\n");
82
83                                         rte_pktmbuf_free(m);
84                                 }
85                         }
86                 }
87         }
88
89         /* closing and releasing resources */
90         rte_flow_flush(port_id, &error);
91         rte_eth_dev_stop(port_id);
92         rte_eth_dev_close(port_id);
93 }
94
95 #define CHECK_INTERVAL 1000  /* 100ms */
96 #define MAX_REPEAT_TIMES 90  /* 9s (90 * 100ms) in total */
97
98 static void
99 assert_link_status(void)
100 {
101         struct rte_eth_link link;
102         uint8_t rep_cnt = MAX_REPEAT_TIMES;
103         int link_get_err = -EINVAL;
104
105         memset(&link, 0, sizeof(link));
106         do {
107                 link_get_err = rte_eth_link_get(port_id, &link);
108                 if (link_get_err == 0 && link.link_status == ETH_LINK_UP)
109                         break;
110                 rte_delay_ms(CHECK_INTERVAL);
111         } while (--rep_cnt);
112
113         if (link_get_err < 0)
114                 rte_exit(EXIT_FAILURE, ":: error: link get is failing: %s\n",
115                          rte_strerror(-link_get_err));
116         if (link.link_status == ETH_LINK_DOWN)
117                 rte_exit(EXIT_FAILURE, ":: error: link is still down\n");
118 }
119
120 static void
121 init_port(void)
122 {
123         int ret;
124         uint16_t i;
125         struct rte_eth_conf port_conf = {
126                 .rxmode = {
127                         .split_hdr_size = 0,
128                 },
129                 .txmode = {
130                         .offloads =
131                                 DEV_TX_OFFLOAD_VLAN_INSERT |
132                                 DEV_TX_OFFLOAD_IPV4_CKSUM  |
133                                 DEV_TX_OFFLOAD_UDP_CKSUM   |
134                                 DEV_TX_OFFLOAD_TCP_CKSUM   |
135                                 DEV_TX_OFFLOAD_SCTP_CKSUM  |
136                                 DEV_TX_OFFLOAD_TCP_TSO,
137                 },
138         };
139         struct rte_eth_txconf txq_conf;
140         struct rte_eth_rxconf rxq_conf;
141         struct rte_eth_dev_info dev_info;
142
143         ret = rte_eth_dev_info_get(port_id, &dev_info);
144         if (ret != 0)
145                 rte_exit(EXIT_FAILURE,
146                         "Error during getting device (port %u) info: %s\n",
147                         port_id, strerror(-ret));
148
149         port_conf.txmode.offloads &= dev_info.tx_offload_capa;
150         printf(":: initializing port: %d\n", port_id);
151         ret = rte_eth_dev_configure(port_id,
152                                 nr_queues, nr_queues, &port_conf);
153         if (ret < 0) {
154                 rte_exit(EXIT_FAILURE,
155                         ":: cannot configure device: err=%d, port=%u\n",
156                         ret, port_id);
157         }
158
159         rxq_conf = dev_info.default_rxconf;
160         rxq_conf.offloads = port_conf.rxmode.offloads;
161         for (i = 0; i < nr_queues; i++) {
162                 ret = rte_eth_rx_queue_setup(port_id, i, 512,
163                                      rte_eth_dev_socket_id(port_id),
164                                      &rxq_conf,
165                                      mbuf_pool);
166                 if (ret < 0) {
167                         rte_exit(EXIT_FAILURE,
168                                 ":: Rx queue setup failed: err=%d, port=%u\n",
169                                 ret, port_id);
170                 }
171         }
172
173         txq_conf = dev_info.default_txconf;
174         txq_conf.offloads = port_conf.txmode.offloads;
175
176         for (i = 0; i < nr_queues; i++) {
177                 ret = rte_eth_tx_queue_setup(port_id, i, 512,
178                                 rte_eth_dev_socket_id(port_id),
179                                 &txq_conf);
180                 if (ret < 0) {
181                         rte_exit(EXIT_FAILURE,
182                                 ":: Tx queue setup failed: err=%d, port=%u\n",
183                                 ret, port_id);
184                 }
185         }
186
187         ret = rte_eth_promiscuous_enable(port_id);
188         if (ret != 0)
189                 rte_exit(EXIT_FAILURE,
190                         ":: promiscuous mode enable failed: err=%s, port=%u\n",
191                         rte_strerror(-ret), port_id);
192
193         ret = rte_eth_dev_start(port_id);
194         if (ret < 0) {
195                 rte_exit(EXIT_FAILURE,
196                         "rte_eth_dev_start:err=%d, port=%u\n",
197                         ret, port_id);
198         }
199
200         assert_link_status();
201
202         printf(":: initializing port: %d done\n", port_id);
203 }
204
205 static void
206 signal_handler(int signum)
207 {
208         if (signum == SIGINT || signum == SIGTERM) {
209                 printf("\n\nSignal %d received, preparing to exit...\n",
210                                 signum);
211                 force_quit = true;
212         }
213 }
214
215 int
216 main(int argc, char **argv)
217 {
218         int ret;
219         uint16_t nr_ports;
220         struct rte_flow_error error;
221
222         ret = rte_eal_init(argc, argv);
223         if (ret < 0)
224                 rte_exit(EXIT_FAILURE, ":: invalid EAL arguments\n");
225
226         force_quit = false;
227         signal(SIGINT, signal_handler);
228         signal(SIGTERM, signal_handler);
229
230         nr_ports = rte_eth_dev_count_avail();
231         if (nr_ports == 0)
232                 rte_exit(EXIT_FAILURE, ":: no Ethernet ports found\n");
233         port_id = 0;
234         if (nr_ports != 1) {
235                 printf(":: warn: %d ports detected, but we use only one: port %u\n",
236                         nr_ports, port_id);
237         }
238         mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", 4096, 128, 0,
239                                             RTE_MBUF_DEFAULT_BUF_SIZE,
240                                             rte_socket_id());
241         if (mbuf_pool == NULL)
242                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
243
244         init_port();
245
246         /* create flow for send packet with */
247         flow = generate_ipv4_flow(port_id, selected_queue,
248                                 SRC_IP, EMPTY_MASK,
249                                 DEST_IP, FULL_MASK, &error);
250         if (!flow) {
251                 printf("Flow can't be created %d message: %s\n",
252                         error.type,
253                         error.message ? error.message : "(no stated reason)");
254                 rte_exit(EXIT_FAILURE, "error in creating flow");
255         }
256
257         main_loop();
258
259         return 0;
260 }