• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Mellanox Technologies, Ltd.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/ip.h>
34 #include <linux/udp.h>
35 #include <net/udp.h>
36 #include "en.h"
37 #include "en/port.h"
38 #include "eswitch.h"
39 
mlx5e_test_health_info(struct mlx5e_priv * priv)40 static int mlx5e_test_health_info(struct mlx5e_priv *priv)
41 {
42 	struct mlx5_core_health *health = &priv->mdev->priv.health;
43 
44 	return health->fatal_error ? 1 : 0;
45 }
46 
mlx5e_test_link_state(struct mlx5e_priv * priv)47 static int mlx5e_test_link_state(struct mlx5e_priv *priv)
48 {
49 	u8 port_state;
50 
51 	if (!netif_carrier_ok(priv->netdev))
52 		return 1;
53 
54 	port_state = mlx5_query_vport_state(priv->mdev, MLX5_VPORT_STATE_OP_MOD_VNIC_VPORT, 0);
55 	return port_state == VPORT_STATE_UP ? 0 : 1;
56 }
57 
mlx5e_test_link_speed(struct mlx5e_priv * priv)58 static int mlx5e_test_link_speed(struct mlx5e_priv *priv)
59 {
60 	u32 speed;
61 
62 	if (!netif_carrier_ok(priv->netdev))
63 		return 1;
64 
65 	return mlx5e_port_linkspeed(priv->mdev, &speed);
66 }
67 
68 struct mlx5ehdr {
69 	__be32 version;
70 	__be64 magic;
71 };
72 
73 #ifdef CONFIG_INET
74 /* loopback test */
75 #define MLX5E_TEST_PKT_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) +\
76 			     sizeof(struct udphdr) + sizeof(struct mlx5ehdr))
77 #define MLX5E_TEST_MAGIC 0x5AEED15C001ULL
78 
mlx5e_test_get_udp_skb(struct mlx5e_priv * priv)79 static struct sk_buff *mlx5e_test_get_udp_skb(struct mlx5e_priv *priv)
80 {
81 	struct sk_buff *skb = NULL;
82 	struct mlx5ehdr *mlxh;
83 	struct ethhdr *ethh;
84 	struct udphdr *udph;
85 	struct iphdr *iph;
86 	int    iplen;
87 
88 	skb = netdev_alloc_skb(priv->netdev, MLX5E_TEST_PKT_SIZE);
89 	if (!skb) {
90 		netdev_err(priv->netdev, "\tFailed to alloc loopback skb\n");
91 		return NULL;
92 	}
93 
94 	net_prefetchw(skb->data);
95 	skb_reserve(skb, NET_IP_ALIGN);
96 
97 	/*  Reserve for ethernet and IP header  */
98 	ethh = skb_push(skb, ETH_HLEN);
99 	skb_reset_mac_header(skb);
100 
101 	skb_set_network_header(skb, skb->len);
102 	iph = skb_put(skb, sizeof(struct iphdr));
103 
104 	skb_set_transport_header(skb, skb->len);
105 	udph = skb_put(skb, sizeof(struct udphdr));
106 
107 	/* Fill ETH header */
108 	ether_addr_copy(ethh->h_dest, priv->netdev->dev_addr);
109 	eth_zero_addr(ethh->h_source);
110 	ethh->h_proto = htons(ETH_P_IP);
111 
112 	/* Fill UDP header */
113 	udph->source = htons(9);
114 	udph->dest = htons(9); /* Discard Protocol */
115 	udph->len = htons(sizeof(struct mlx5ehdr) + sizeof(struct udphdr));
116 	udph->check = 0;
117 
118 	/* Fill IP header */
119 	iph->ihl = 5;
120 	iph->ttl = 32;
121 	iph->version = 4;
122 	iph->protocol = IPPROTO_UDP;
123 	iplen = sizeof(struct iphdr) + sizeof(struct udphdr) +
124 		sizeof(struct mlx5ehdr);
125 	iph->tot_len = htons(iplen);
126 	iph->frag_off = 0;
127 	iph->saddr = 0;
128 	iph->daddr = 0;
129 	iph->tos = 0;
130 	iph->id = 0;
131 	ip_send_check(iph);
132 
133 	/* Fill test header and data */
134 	mlxh = skb_put(skb, sizeof(*mlxh));
135 	mlxh->version = 0;
136 	mlxh->magic = cpu_to_be64(MLX5E_TEST_MAGIC);
137 
138 	skb->csum = 0;
139 	skb->ip_summed = CHECKSUM_PARTIAL;
140 	udp4_hwcsum(skb, iph->saddr, iph->daddr);
141 
142 	skb->protocol = htons(ETH_P_IP);
143 	skb->pkt_type = PACKET_HOST;
144 	skb->dev = priv->netdev;
145 
146 	return skb;
147 }
148 
149 struct mlx5e_lbt_priv {
150 	struct packet_type pt;
151 	struct completion comp;
152 	bool loopback_ok;
153 	bool local_lb;
154 };
155 
156 static int
mlx5e_test_loopback_validate(struct sk_buff * skb,struct net_device * ndev,struct packet_type * pt,struct net_device * orig_ndev)157 mlx5e_test_loopback_validate(struct sk_buff *skb,
158 			     struct net_device *ndev,
159 			     struct packet_type *pt,
160 			     struct net_device *orig_ndev)
161 {
162 	struct mlx5e_lbt_priv *lbtp = pt->af_packet_priv;
163 	struct mlx5ehdr *mlxh;
164 	struct ethhdr *ethh;
165 	struct udphdr *udph;
166 	struct iphdr *iph;
167 
168 	if (skb_linearize(skb))
169 		goto out;
170 
171 	/* We are only going to peek, no need to clone the SKB */
172 	if (MLX5E_TEST_PKT_SIZE - ETH_HLEN > skb_headlen(skb))
173 		goto out;
174 
175 	ethh = (struct ethhdr *)skb_mac_header(skb);
176 	if (!ether_addr_equal(ethh->h_dest, orig_ndev->dev_addr))
177 		goto out;
178 
179 	iph = ip_hdr(skb);
180 	if (iph->protocol != IPPROTO_UDP)
181 		goto out;
182 
183 	/* Don't assume skb_transport_header() was set */
184 	udph = (struct udphdr *)((u8 *)iph + 4 * iph->ihl);
185 	if (udph->dest != htons(9))
186 		goto out;
187 
188 	mlxh = (struct mlx5ehdr *)((char *)udph + sizeof(*udph));
189 	if (mlxh->magic != cpu_to_be64(MLX5E_TEST_MAGIC))
190 		goto out; /* so close ! */
191 
192 	/* bingo */
193 	lbtp->loopback_ok = true;
194 	complete(&lbtp->comp);
195 out:
196 	kfree_skb(skb);
197 	return 0;
198 }
199 
mlx5e_test_loopback_setup(struct mlx5e_priv * priv,struct mlx5e_lbt_priv * lbtp)200 static int mlx5e_test_loopback_setup(struct mlx5e_priv *priv,
201 				     struct mlx5e_lbt_priv *lbtp)
202 {
203 	int err = 0;
204 
205 	/* Temporarily enable local_lb */
206 	err = mlx5_nic_vport_query_local_lb(priv->mdev, &lbtp->local_lb);
207 	if (err)
208 		return err;
209 
210 	if (!lbtp->local_lb) {
211 		err = mlx5_nic_vport_update_local_lb(priv->mdev, true);
212 		if (err)
213 			return err;
214 	}
215 
216 	err = mlx5e_refresh_tirs(priv, true, false);
217 	if (err)
218 		goto out;
219 
220 	lbtp->loopback_ok = false;
221 	init_completion(&lbtp->comp);
222 
223 	lbtp->pt.type = htons(ETH_P_IP);
224 	lbtp->pt.func = mlx5e_test_loopback_validate;
225 	lbtp->pt.dev = priv->netdev;
226 	lbtp->pt.af_packet_priv = lbtp;
227 	dev_add_pack(&lbtp->pt);
228 
229 	return 0;
230 
231 out:
232 	if (!lbtp->local_lb)
233 		mlx5_nic_vport_update_local_lb(priv->mdev, false);
234 
235 	return err;
236 }
237 
mlx5e_test_loopback_cleanup(struct mlx5e_priv * priv,struct mlx5e_lbt_priv * lbtp)238 static void mlx5e_test_loopback_cleanup(struct mlx5e_priv *priv,
239 					struct mlx5e_lbt_priv *lbtp)
240 {
241 	if (!lbtp->local_lb)
242 		mlx5_nic_vport_update_local_lb(priv->mdev, false);
243 
244 	dev_remove_pack(&lbtp->pt);
245 	mlx5e_refresh_tirs(priv, false, false);
246 }
247 
mlx5e_cond_loopback(struct mlx5e_priv * priv)248 static int mlx5e_cond_loopback(struct mlx5e_priv *priv)
249 {
250 	if (is_mdev_switchdev_mode(priv->mdev))
251 		return -EOPNOTSUPP;
252 
253 	return 0;
254 }
255 
256 #define MLX5E_LB_VERIFY_TIMEOUT (msecs_to_jiffies(200))
mlx5e_test_loopback(struct mlx5e_priv * priv)257 static int mlx5e_test_loopback(struct mlx5e_priv *priv)
258 {
259 	struct mlx5e_lbt_priv *lbtp;
260 	struct sk_buff *skb = NULL;
261 	int err;
262 
263 	if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) {
264 		netdev_err(priv->netdev,
265 			   "\tCan't perform loopback test while device is down\n");
266 		return -ENODEV;
267 	}
268 
269 	lbtp = kzalloc(sizeof(*lbtp), GFP_KERNEL);
270 	if (!lbtp)
271 		return -ENOMEM;
272 	lbtp->loopback_ok = false;
273 
274 	err = mlx5e_test_loopback_setup(priv, lbtp);
275 	if (err)
276 		goto out;
277 
278 	skb = mlx5e_test_get_udp_skb(priv);
279 	if (!skb) {
280 		err = -ENOMEM;
281 		goto cleanup;
282 	}
283 
284 	skb_set_queue_mapping(skb, 0);
285 	err = dev_queue_xmit(skb);
286 	if (err) {
287 		netdev_err(priv->netdev,
288 			   "\tFailed to xmit loopback packet err(%d)\n",
289 			   err);
290 		goto cleanup;
291 	}
292 
293 	wait_for_completion_timeout(&lbtp->comp, MLX5E_LB_VERIFY_TIMEOUT);
294 	err = !lbtp->loopback_ok;
295 
296 cleanup:
297 	mlx5e_test_loopback_cleanup(priv, lbtp);
298 out:
299 	kfree(lbtp);
300 	return err;
301 }
302 #endif
303 
304 typedef int (*mlx5e_st_func)(struct mlx5e_priv *);
305 
306 struct mlx5e_st {
307 	char name[ETH_GSTRING_LEN];
308 	mlx5e_st_func st_func;
309 	mlx5e_st_func cond_func;
310 };
311 
312 static struct mlx5e_st mlx5e_sts[] = {
313 	{ "Link Test", mlx5e_test_link_state },
314 	{ "Speed Test", mlx5e_test_link_speed },
315 	{ "Health Test", mlx5e_test_health_info },
316 #ifdef CONFIG_INET
317 	{ "Loopback Test", mlx5e_test_loopback, mlx5e_cond_loopback },
318 #endif
319 };
320 
321 #define MLX5E_ST_NUM ARRAY_SIZE(mlx5e_sts)
322 
mlx5e_self_test(struct net_device * ndev,struct ethtool_test * etest,u64 * buf)323 void mlx5e_self_test(struct net_device *ndev, struct ethtool_test *etest,
324 		     u64 *buf)
325 {
326 	struct mlx5e_priv *priv = netdev_priv(ndev);
327 	int i, count = 0;
328 
329 	mutex_lock(&priv->state_lock);
330 	netdev_info(ndev, "Self test begin..\n");
331 
332 	for (i = 0; i < MLX5E_ST_NUM; i++) {
333 		struct mlx5e_st st = mlx5e_sts[i];
334 
335 		if (st.cond_func && st.cond_func(priv))
336 			continue;
337 		netdev_info(ndev, "\t[%d] %s start..\n", i, st.name);
338 		buf[count] = st.st_func(priv);
339 		netdev_info(ndev, "\t[%d] %s end: result(%lld)\n", i, st.name, buf[count]);
340 		count++;
341 	}
342 
343 	mutex_unlock(&priv->state_lock);
344 
345 	for (i = 0; i < count; i++) {
346 		if (buf[i]) {
347 			etest->flags |= ETH_TEST_FL_FAILED;
348 			break;
349 		}
350 	}
351 	netdev_info(ndev, "Self test out: status flags(0x%x)\n",
352 		    etest->flags);
353 }
354 
mlx5e_self_test_fill_strings(struct mlx5e_priv * priv,u8 * data)355 int mlx5e_self_test_fill_strings(struct mlx5e_priv *priv, u8 *data)
356 {
357 	int i, count = 0;
358 
359 	for (i = 0; i < MLX5E_ST_NUM; i++) {
360 		struct mlx5e_st st = mlx5e_sts[i];
361 
362 		if (st.cond_func && st.cond_func(priv))
363 			continue;
364 		if (data)
365 			strcpy(data + count * ETH_GSTRING_LEN, st.name);
366 		count++;
367 	}
368 	return count;
369 }
370 
mlx5e_self_test_num(struct mlx5e_priv * priv)371 int mlx5e_self_test_num(struct mlx5e_priv *priv)
372 {
373 	return mlx5e_self_test_fill_strings(priv, NULL);
374 }
375