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 <rte_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 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 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(), 61 RING_F_SP_ENQ | RING_F_SC_DEQ); 62 if (ring_client == NULL) { 63 printf("rte_ring_create SR0 failed"); 64 return -1; 65 } 66 67 eth_dev = rte_eth_dev_attach_secondary(deviceid); 68 if (!eth_dev) { 69 printf("Failed to probe %s", deviceid); 70 return -1; 71 } 72 rte_eth_dev_probing_finish(eth_dev); 73 74 ring_client->prod.single = 0; 75 ring_client->cons.single = 0; 76 77 printf("\n***** flags = RTE_PDUMP_FLAG_TX *****\n"); 78 79 for (itr = 0; itr < NUM_ITR; itr++) { 80 ret = rte_pdump_enable(portid, QUEUE_ID, flags, ring_client, 81 mp, NULL); 82 if (ret < 0) { 83 printf("rte_pdump_enable failed\n"); 84 return -1; 85 } 86 printf("pdump_enable success\n"); 87 88 ret = rte_pdump_disable(portid, QUEUE_ID, flags); 89 if (ret < 0) { 90 printf("rte_pdump_disable failed\n"); 91 return -1; 92 } 93 printf("pdump_disable success\n"); 94 95 ret = rte_pdump_enable_by_deviceid(deviceid, QUEUE_ID, flags, 96 ring_client, mp, NULL); 97 if (ret < 0) { 98 printf("rte_pdump_enable_by_deviceid failed\n"); 99 return -1; 100 } 101 printf("pdump_enable_by_deviceid success\n"); 102 103 ret = rte_pdump_disable_by_deviceid(deviceid, QUEUE_ID, flags); 104 if (ret < 0) { 105 printf("rte_pdump_disable_by_deviceid failed\n"); 106 return -1; 107 } 108 printf("pdump_disable_by_deviceid success\n"); 109 110 if (itr == 0) { 111 flags = RTE_PDUMP_FLAG_RX; 112 printf("\n***** flags = RTE_PDUMP_FLAG_RX *****\n"); 113 } else if (itr == 1) { 114 flags = RTE_PDUMP_FLAG_RXTX; 115 printf("\n***** flags = RTE_PDUMP_FLAG_RXTX *****\n"); 116 } 117 } 118 if (ring_client != NULL) 119 test_ring_free(ring_client); 120 if (mp != NULL) 121 test_mp_free(mp); 122 123 return ret; 124 } 125 126 int 127 test_pdump_uninit(void) 128 { 129 int ret = 0; 130 131 ret = rte_pdump_uninit(); 132 if (ret < 0) { 133 printf("rte_pdump_uninit failed\n"); 134 return -1; 135 } 136 if (ring_server != NULL) 137 test_ring_free(ring_server); 138 printf("pdump_uninit success\n"); 139 test_vdev_uninit("net_ring_net_ringa"); 140 return ret; 141 } 142 143 void * 144 send_pkts(void *empty) 145 { 146 int ret = 0; 147 struct rte_mbuf *pbuf[NUM_PACKETS] = { }; 148 struct rte_mempool *mp; 149 char poolname[] = "mbuf_pool_server"; 150 151 ret = test_get_mbuf_from_pool(&mp, pbuf, poolname); 152 if (ret < 0) 153 printf("get_mbuf_from_pool failed\n"); 154 do { 155 ret = test_packet_forward(pbuf, portid, QUEUE_ID); 156 if (ret < 0) 157 printf("send pkts Failed\n"); 158 } while (flag_for_send_pkts); 159 test_put_mbuf_to_pool(mp, pbuf); 160 return empty; 161 } 162 163 /* 164 * This function is called in the primary i.e. main test, to spawn off secondary 165 * processes to run actual mp tests. Uses fork() and exec pair 166 */ 167 168 int 169 run_pdump_server_tests(void) 170 { 171 int ret = 0; 172 char coremask[10]; 173 174 #ifdef RTE_EXEC_ENV_LINUX 175 char tmp[PATH_MAX] = { 0 }; 176 char prefix[PATH_MAX] = { 0 }; 177 178 get_current_prefix(tmp, sizeof(tmp)); 179 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp); 180 #else 181 const char *prefix = ""; 182 #endif 183 184 /* good case, using secondary */ 185 const char *const argv1[] = { 186 prgname, "-c", coremask, "--proc-type=secondary", 187 prefix 188 }; 189 190 snprintf(coremask, sizeof(coremask), "%x", 191 (1 << rte_get_master_lcore())); 192 193 ret = test_pdump_init(); 194 ret |= launch_p(argv1); 195 ret |= test_pdump_uninit(); 196 return ret; 197 } 198 199 int 200 test_pdump(void) 201 { 202 int ret = 0; 203 if (rte_eal_process_type() == RTE_PROC_PRIMARY) { 204 printf("IN PRIMARY PROCESS\n"); 205 ret = run_pdump_server_tests(); 206 if (ret < 0) 207 return TEST_FAILED; 208 } else if (rte_eal_process_type() == RTE_PROC_SECONDARY) { 209 printf("IN SECONDARY PROCESS\n"); 210 sleep(5); 211 ret = run_pdump_client_tests(); 212 if (ret < 0) 213 return TEST_FAILED; 214 } 215 return TEST_SUCCESS; 216 } 217 218 REGISTER_TEST_COMMAND(pdump_autotest, test_pdump); 219