xref: /dpdk/drivers/net/mlx5/mlx5_testpmd.c (revision 448e01f1b5848b20cb0300d339100dd82f4459e9)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2021 6WIND S.A.
3  * Copyright 2021 Mellanox Technologies, Ltd
4  */
5 
6 #include <stdint.h>
7 #include <string.h>
8 #include <stdlib.h>
9 
10 #include <rte_prefetch.h>
11 #include <rte_common.h>
12 #include <rte_branch_prediction.h>
13 #include <rte_ether.h>
14 #include <rte_alarm.h>
15 #include <rte_pmd_mlx5.h>
16 #include <rte_ethdev.h>
17 #include "mlx5_testpmd.h"
18 #include "testpmd.h"
19 
20 static uint8_t host_shaper_avail_thresh_triggered[RTE_MAX_ETHPORTS];
21 #define SHAPER_DISABLE_DELAY_US 100000 /* 100ms */
22 
23 /**
24  * Disable the host shaper and re-arm available descriptor threshold event.
25  *
26  * @param[in] args
27  *   uint32_t integer combining port_id and rxq_id.
28  */
29 static void
30 mlx5_test_host_shaper_disable(void *args)
31 {
32 	uint32_t port_rxq_id = (uint32_t)(uintptr_t)args;
33 	uint16_t port_id = port_rxq_id & 0xffff;
34 	uint16_t qid = (port_rxq_id >> 16) & 0xffff;
35 	struct rte_eth_rxq_info qinfo;
36 
37 	printf("%s disable shaper\n", __func__);
38 	if (rte_eth_rx_queue_info_get(port_id, qid, &qinfo)) {
39 		printf("rx_queue_info_get returns error\n");
40 		return;
41 	}
42 	/* Rearm the available descriptor threshold event. */
43 	if (rte_eth_rx_avail_thresh_set(port_id, qid, qinfo.avail_thresh)) {
44 		printf("config avail_thresh returns error\n");
45 		return;
46 	}
47 	/* Only disable the shaper when avail_thresh_triggered is set. */
48 	if (host_shaper_avail_thresh_triggered[port_id] &&
49 	    rte_pmd_mlx5_host_shaper_config(port_id, 0, 0))
50 		printf("%s disable shaper returns error\n", __func__);
51 }
52 
53 void
54 mlx5_test_avail_thresh_event_handler(uint16_t port_id, uint16_t rxq_id)
55 {
56 	struct rte_eth_dev_info dev_info;
57 	uint32_t port_rxq_id = port_id | (rxq_id << 16);
58 
59 	/* Ensure it's MLX5 port. */
60 	if (rte_eth_dev_info_get(port_id, &dev_info) != 0 ||
61 	    (strncmp(dev_info.driver_name, "mlx5", 4) != 0))
62 		return;
63 	rte_eal_alarm_set(SHAPER_DISABLE_DELAY_US,
64 			  mlx5_test_host_shaper_disable,
65 			  (void *)(uintptr_t)port_rxq_id);
66 	printf("%s port_id:%u rxq_id:%u\n", __func__, port_id, rxq_id);
67 }
68 
69 /**
70  * Configure host shaper's avail_thresh_triggered and current rate.
71  *
72  * @param[in] avail_thresh_triggered
73  *   Disable/enable avail_thresh_triggered.
74  * @param[in] rate
75  *   Configure current host shaper rate.
76  * @return
77  *   On success, returns 0.
78  *   On failure, returns < 0.
79  */
80 static int
81 mlx5_test_set_port_host_shaper(uint16_t port_id, uint16_t avail_thresh_triggered, uint8_t rate)
82 {
83 	struct rte_eth_link link;
84 	bool port_id_valid = false;
85 	uint16_t pid;
86 	int ret;
87 
88 	RTE_ETH_FOREACH_DEV(pid)
89 		if (port_id == pid) {
90 			port_id_valid = true;
91 			break;
92 		}
93 	if (!port_id_valid)
94 		return -EINVAL;
95 	ret = rte_eth_link_get_nowait(port_id, &link);
96 	if (ret < 0)
97 		return ret;
98 	host_shaper_avail_thresh_triggered[port_id] = avail_thresh_triggered ? 1 : 0;
99 	if (!avail_thresh_triggered) {
100 		ret = rte_pmd_mlx5_host_shaper_config(port_id, 0,
101 		RTE_BIT32(MLX5_HOST_SHAPER_FLAG_AVAIL_THRESH_TRIGGERED));
102 	} else {
103 		ret = rte_pmd_mlx5_host_shaper_config(port_id, 1,
104 		RTE_BIT32(MLX5_HOST_SHAPER_FLAG_AVAIL_THRESH_TRIGGERED));
105 	}
106 	if (ret)
107 		return ret;
108 	ret = rte_pmd_mlx5_host_shaper_config(port_id, rate, 0);
109 	if (ret)
110 		return ret;
111 	return 0;
112 }
113 
114 /* *** SET HOST_SHAPER FOR A PORT *** */
115 struct cmd_port_host_shaper_result {
116 	cmdline_fixed_string_t mlx5;
117 	cmdline_fixed_string_t set;
118 	cmdline_fixed_string_t port;
119 	uint16_t port_num;
120 	cmdline_fixed_string_t host_shaper;
121 	cmdline_fixed_string_t avail_thresh_triggered;
122 	uint16_t fr;
123 	cmdline_fixed_string_t rate;
124 	uint8_t rate_num;
125 };
126 
127 static void cmd_port_host_shaper_parsed(void *parsed_result,
128 		__rte_unused struct cmdline *cl,
129 		__rte_unused void *data)
130 {
131 	struct cmd_port_host_shaper_result *res = parsed_result;
132 	int ret = 0;
133 
134 	if ((strcmp(res->mlx5, "mlx5") == 0) &&
135 	    (strcmp(res->set, "set") == 0) &&
136 	    (strcmp(res->port, "port") == 0) &&
137 	    (strcmp(res->host_shaper, "host_shaper") == 0) &&
138 	    (strcmp(res->avail_thresh_triggered, "avail_thresh_triggered") == 0) &&
139 	    (strcmp(res->rate, "rate") == 0))
140 		ret = mlx5_test_set_port_host_shaper(res->port_num, res->fr,
141 					   res->rate_num);
142 	if (ret < 0)
143 		printf("cmd_port_host_shaper error: (%s)\n", strerror(-ret));
144 }
145 
146 static cmdline_parse_token_string_t cmd_port_host_shaper_mlx5 =
147 	TOKEN_STRING_INITIALIZER(struct cmd_port_host_shaper_result,
148 				mlx5, "mlx5");
149 static cmdline_parse_token_string_t cmd_port_host_shaper_set =
150 	TOKEN_STRING_INITIALIZER(struct cmd_port_host_shaper_result,
151 				set, "set");
152 static cmdline_parse_token_string_t cmd_port_host_shaper_port =
153 	TOKEN_STRING_INITIALIZER(struct cmd_port_host_shaper_result,
154 				port, "port");
155 static cmdline_parse_token_num_t cmd_port_host_shaper_portnum =
156 	TOKEN_NUM_INITIALIZER(struct cmd_port_host_shaper_result,
157 				port_num, RTE_UINT16);
158 static cmdline_parse_token_string_t cmd_port_host_shaper_host_shaper =
159 	TOKEN_STRING_INITIALIZER(struct cmd_port_host_shaper_result,
160 				 host_shaper, "host_shaper");
161 static cmdline_parse_token_string_t cmd_port_host_shaper_avail_thresh_triggered =
162 	TOKEN_STRING_INITIALIZER(struct cmd_port_host_shaper_result,
163 				 avail_thresh_triggered, "avail_thresh_triggered");
164 static cmdline_parse_token_num_t cmd_port_host_shaper_fr =
165 	TOKEN_NUM_INITIALIZER(struct cmd_port_host_shaper_result,
166 			      fr, RTE_UINT16);
167 static cmdline_parse_token_string_t cmd_port_host_shaper_rate =
168 	TOKEN_STRING_INITIALIZER(struct cmd_port_host_shaper_result,
169 				 rate, "rate");
170 static cmdline_parse_token_num_t cmd_port_host_shaper_rate_num =
171 	TOKEN_NUM_INITIALIZER(struct cmd_port_host_shaper_result,
172 			      rate_num, RTE_UINT8);
173 static cmdline_parse_inst_t mlx5_test_cmd_port_host_shaper = {
174 	.f = cmd_port_host_shaper_parsed,
175 	.data = (void *)0,
176 	.help_str = "mlx5 set port <port_id> host_shaper avail_thresh_triggered <0|1> "
177 	"rate <rate_num>: Set HOST_SHAPER avail_thresh_triggered and rate with port_id",
178 	.tokens = {
179 		(void *)&cmd_port_host_shaper_mlx5,
180 		(void *)&cmd_port_host_shaper_set,
181 		(void *)&cmd_port_host_shaper_port,
182 		(void *)&cmd_port_host_shaper_portnum,
183 		(void *)&cmd_port_host_shaper_host_shaper,
184 		(void *)&cmd_port_host_shaper_avail_thresh_triggered,
185 		(void *)&cmd_port_host_shaper_fr,
186 		(void *)&cmd_port_host_shaper_rate,
187 		(void *)&cmd_port_host_shaper_rate_num,
188 		NULL,
189 	}
190 };
191 
192 static struct testpmd_driver_commands mlx5_driver_cmds = {
193 	.commands = {
194 		{
195 			.ctx = &mlx5_test_cmd_port_host_shaper,
196 			.help = "mlx5 set port (port_id) host_shaper avail_thresh_triggered (on|off)"
197 				"rate (rate_num):\n"
198 				"    Set HOST_SHAPER avail_thresh_triggered and rate with port_id\n\n",
199 		},
200 		{
201 			.ctx = NULL,
202 		},
203 	}
204 };
205 TESTPMD_ADD_DRIVER_COMMANDS(mlx5_driver_cmds);
206