1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
3 */
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <stdint.h>
7 #include <limits.h>
8
9 #include <ethdev_driver.h>
10 #include <rte_pdump.h>
11 #include "rte_eal.h"
12 #include "rte_lcore.h"
13 #include "rte_mempool.h"
14 #include "rte_ring.h"
15
16 #include "sample_packet_forward.h"
17 #include "test.h"
18 #include "process.h"
19 #include "test_pdump.h"
20
21 #define launch_p(ARGV) process_dup(ARGV, RTE_DIM(ARGV), __func__)
22
23 struct rte_ring *ring_server;
24 uint16_t portid;
25 uint16_t flag_for_send_pkts = 1;
26
27 int
test_pdump_init(void)28 test_pdump_init(void)
29 {
30 int ret = 0;
31
32 ret = rte_pdump_init();
33 if (ret < 0) {
34 printf("rte_pdump_init failed\n");
35 return -1;
36 }
37 ret = test_ring_setup(&ring_server, &portid);
38 if (ret < 0) {
39 printf("test_ring_setup failed\n");
40 return -1;
41 }
42 printf("pdump_init success\n");
43 return ret;
44 }
45
46 int
run_pdump_client_tests(void)47 run_pdump_client_tests(void)
48 {
49 int flags = RTE_PDUMP_FLAG_TX, ret = 0, itr;
50 char deviceid[] = "net_ring_net_ringa";
51 struct rte_ring *ring_client;
52 struct rte_mempool *mp = NULL;
53 struct rte_eth_dev *eth_dev = NULL;
54 char poolname[] = "mbuf_pool_client";
55
56 ret = test_get_mempool(&mp, poolname);
57 if (ret < 0)
58 return -1;
59 mp->flags = 0x0000;
60 ring_client = rte_ring_create("SR0", RING_SIZE, rte_socket_id(), 0);
61 if (ring_client == NULL) {
62 printf("rte_ring_create SR0 failed");
63 return -1;
64 }
65
66 eth_dev = rte_eth_dev_attach_secondary(deviceid);
67 if (!eth_dev) {
68 printf("Failed to probe %s", deviceid);
69 return -1;
70 }
71 rte_eth_dev_probing_finish(eth_dev);
72
73 printf("\n***** flags = RTE_PDUMP_FLAG_TX *****\n");
74
75 for (itr = 0; itr < NUM_ITR; itr++) {
76 ret = rte_pdump_enable(portid, QUEUE_ID, flags, ring_client,
77 mp, NULL);
78 if (ret < 0) {
79 printf("rte_pdump_enable failed\n");
80 return -1;
81 }
82 printf("pdump_enable success\n");
83
84 ret = rte_pdump_disable(portid, QUEUE_ID, flags);
85 if (ret < 0) {
86 printf("rte_pdump_disable failed\n");
87 return -1;
88 }
89 printf("pdump_disable success\n");
90
91 ret = rte_pdump_enable_by_deviceid(deviceid, QUEUE_ID, flags,
92 ring_client, mp, NULL);
93 if (ret < 0) {
94 printf("rte_pdump_enable_by_deviceid failed\n");
95 return -1;
96 }
97 printf("pdump_enable_by_deviceid success\n");
98
99 ret = rte_pdump_disable_by_deviceid(deviceid, QUEUE_ID, flags);
100 if (ret < 0) {
101 printf("rte_pdump_disable_by_deviceid failed\n");
102 return -1;
103 }
104 printf("pdump_disable_by_deviceid success\n");
105
106 if (itr == 0) {
107 flags = RTE_PDUMP_FLAG_RX;
108 printf("\n***** flags = RTE_PDUMP_FLAG_RX *****\n");
109 } else if (itr == 1) {
110 flags = RTE_PDUMP_FLAG_RXTX;
111 printf("\n***** flags = RTE_PDUMP_FLAG_RXTX *****\n");
112 }
113 }
114 if (ring_client != NULL)
115 test_ring_free(ring_client);
116 if (mp != NULL)
117 test_mp_free(mp);
118
119 return ret;
120 }
121
122 int
test_pdump_uninit(void)123 test_pdump_uninit(void)
124 {
125 int ret = 0;
126
127 ret = rte_pdump_uninit();
128 if (ret < 0) {
129 printf("rte_pdump_uninit failed\n");
130 return -1;
131 }
132 if (ring_server != NULL)
133 test_ring_free(ring_server);
134 printf("pdump_uninit success\n");
135 test_vdev_uninit("net_ring_net_ringa");
136 return ret;
137 }
138
139 uint32_t
send_pkts(void * empty __rte_unused)140 send_pkts(void *empty __rte_unused)
141 {
142 int ret = 0;
143 struct rte_mbuf *pbuf[NUM_PACKETS] = { };
144 struct rte_mempool *mp;
145 char poolname[] = "mbuf_pool_server";
146
147 ret = test_get_mbuf_from_pool(&mp, pbuf, poolname);
148 if (ret < 0)
149 printf("get_mbuf_from_pool failed\n");
150
151 ret = test_dev_start(portid, mp);
152 if (ret < 0)
153 printf("test_dev_start(%hu, %p) failed, error code: %d\n",
154 portid, mp, ret);
155
156 while (ret >= 0 && flag_for_send_pkts) {
157 ret = test_packet_forward(pbuf, portid, QUEUE_ID);
158 if (ret < 0)
159 printf("send pkts Failed\n");
160 };
161
162 rte_eth_dev_stop(portid);
163 test_put_mbuf_to_pool(mp, pbuf);
164 return 0;
165 }
166
167 /*
168 * This function is called in the primary i.e. main test, to spawn off secondary
169 * processes to run actual mp tests. Uses fork() and exec pair
170 */
171
172 int
run_pdump_server_tests(void)173 run_pdump_server_tests(void)
174 {
175 int ret = 0;
176 char coremask[10];
177
178 #ifdef RTE_EXEC_ENV_LINUX
179 char tmp[PATH_MAX] = { 0 };
180 char prefix[PATH_MAX] = { 0 };
181
182 get_current_prefix(tmp, sizeof(tmp));
183 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
184 #else
185 const char *prefix = "";
186 #endif
187
188 /* good case, using secondary */
189 const char *const argv1[] = {
190 prgname, "-c", coremask, "--proc-type=secondary",
191 prefix
192 };
193
194 snprintf(coremask, sizeof(coremask), "%x",
195 (1 << rte_get_main_lcore()));
196
197 ret = test_pdump_init();
198 ret |= launch_p(argv1);
199 ret |= test_pdump_uninit();
200 return ret;
201 }
202
203 int
test_pdump(void)204 test_pdump(void)
205 {
206 int ret = 0;
207 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
208 printf("IN PRIMARY PROCESS\n");
209 ret = run_pdump_server_tests();
210 if (ret < 0)
211 return TEST_FAILED;
212 } else if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
213 printf("IN SECONDARY PROCESS\n");
214 sleep(5);
215 ret = run_pdump_client_tests();
216 if (ret < 0)
217 return TEST_FAILED;
218 }
219 return TEST_SUCCESS;
220 }
221
222 REGISTER_FAST_TEST(pdump_autotest, true, false, test_pdump);
223