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 39 enum { 40 MLX5E_ST_LINK_STATE, 41 MLX5E_ST_LINK_SPEED, 42 MLX5E_ST_HEALTH_INFO, 43 #ifdef CONFIG_INET 44 MLX5E_ST_LOOPBACK, 45 #endif 46 MLX5E_ST_NUM, 47 }; 48 49 const char mlx5e_self_tests[MLX5E_ST_NUM][ETH_GSTRING_LEN] = { 50 "Link Test", 51 "Speed Test", 52 "Health Test", 53 #ifdef CONFIG_INET 54 "Loopback Test", 55 #endif 56 }; 57 58 int mlx5e_self_test_num(struct mlx5e_priv *priv) 59 { 60 return ARRAY_SIZE(mlx5e_self_tests); 61 } 62 63 static int mlx5e_test_health_info(struct mlx5e_priv *priv) 64 { 65 struct mlx5_core_health *health = &priv->mdev->priv.health; 66 67 return health->fatal_error ? 1 : 0; 68 } 69 70 static int mlx5e_test_link_state(struct mlx5e_priv *priv) 71 { 72 u8 port_state; 73 74 if (!netif_carrier_ok(priv->netdev)) 75 return 1; 76 77 port_state = mlx5_query_vport_state(priv->mdev, MLX5_VPORT_STATE_OP_MOD_VNIC_VPORT, 0); 78 return port_state == VPORT_STATE_UP ? 0 : 1; 79 } 80 81 static int mlx5e_test_link_speed(struct mlx5e_priv *priv) 82 { 83 u32 speed; 84 85 if (!netif_carrier_ok(priv->netdev)) 86 return 1; 87 88 return mlx5e_port_linkspeed(priv->mdev, &speed); 89 } 90 91 struct mlx5ehdr { 92 __be32 version; 93 __be64 magic; 94 }; 95 96 #ifdef CONFIG_INET 97 /* loopback test */ 98 #define MLX5E_TEST_PKT_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) +\ 99 sizeof(struct udphdr) + sizeof(struct mlx5ehdr)) 100 #define MLX5E_TEST_MAGIC 0x5AEED15C001ULL 101 102 static struct sk_buff *mlx5e_test_get_udp_skb(struct mlx5e_priv *priv) 103 { 104 struct sk_buff *skb = NULL; 105 struct mlx5ehdr *mlxh; 106 struct ethhdr *ethh; 107 struct udphdr *udph; 108 struct iphdr *iph; 109 int iplen; 110 111 skb = netdev_alloc_skb(priv->netdev, MLX5E_TEST_PKT_SIZE); 112 if (!skb) { 113 netdev_err(priv->netdev, "\tFailed to alloc loopback skb\n"); 114 return NULL; 115 } 116 117 net_prefetchw(skb->data); 118 skb_reserve(skb, NET_IP_ALIGN); 119 120 /* Reserve for ethernet and IP header */ 121 ethh = skb_push(skb, ETH_HLEN); 122 skb_reset_mac_header(skb); 123 124 skb_set_network_header(skb, skb->len); 125 iph = skb_put(skb, sizeof(struct iphdr)); 126 127 skb_set_transport_header(skb, skb->len); 128 udph = skb_put(skb, sizeof(struct udphdr)); 129 130 /* Fill ETH header */ 131 ether_addr_copy(ethh->h_dest, priv->netdev->dev_addr); 132 eth_zero_addr(ethh->h_source); 133 ethh->h_proto = htons(ETH_P_IP); 134 135 /* Fill UDP header */ 136 udph->source = htons(9); 137 udph->dest = htons(9); /* Discard Protocol */ 138 udph->len = htons(sizeof(struct mlx5ehdr) + sizeof(struct udphdr)); 139 udph->check = 0; 140 141 /* Fill IP header */ 142 iph->ihl = 5; 143 iph->ttl = 32; 144 iph->version = 4; 145 iph->protocol = IPPROTO_UDP; 146 iplen = sizeof(struct iphdr) + sizeof(struct udphdr) + 147 sizeof(struct mlx5ehdr); 148 iph->tot_len = htons(iplen); 149 iph->frag_off = 0; 150 iph->saddr = 0; 151 iph->daddr = 0; 152 iph->tos = 0; 153 iph->id = 0; 154 ip_send_check(iph); 155 156 /* Fill test header and data */ 157 mlxh = skb_put(skb, sizeof(*mlxh)); 158 mlxh->version = 0; 159 mlxh->magic = cpu_to_be64(MLX5E_TEST_MAGIC); 160 161 skb->csum = 0; 162 skb->ip_summed = CHECKSUM_PARTIAL; 163 udp4_hwcsum(skb, iph->saddr, iph->daddr); 164 165 skb->protocol = htons(ETH_P_IP); 166 skb->pkt_type = PACKET_HOST; 167 skb->dev = priv->netdev; 168 169 return skb; 170 } 171 172 struct mlx5e_lbt_priv { 173 struct packet_type pt; 174 struct completion comp; 175 bool loopback_ok; 176 bool local_lb; 177 }; 178 179 static int 180 mlx5e_test_loopback_validate(struct sk_buff *skb, 181 struct net_device *ndev, 182 struct packet_type *pt, 183 struct net_device *orig_ndev) 184 { 185 struct mlx5e_lbt_priv *lbtp = pt->af_packet_priv; 186 struct mlx5ehdr *mlxh; 187 struct ethhdr *ethh; 188 struct udphdr *udph; 189 struct iphdr *iph; 190 191 /* We are only going to peek, no need to clone the SKB */ 192 if (MLX5E_TEST_PKT_SIZE - ETH_HLEN > skb_headlen(skb)) 193 goto out; 194 195 ethh = (struct ethhdr *)skb_mac_header(skb); 196 if (!ether_addr_equal(ethh->h_dest, orig_ndev->dev_addr)) 197 goto out; 198 199 iph = ip_hdr(skb); 200 if (iph->protocol != IPPROTO_UDP) 201 goto out; 202 203 /* Don't assume skb_transport_header() was set */ 204 udph = (struct udphdr *)((u8 *)iph + 4 * iph->ihl); 205 if (udph->dest != htons(9)) 206 goto out; 207 208 mlxh = (struct mlx5ehdr *)((char *)udph + sizeof(*udph)); 209 if (mlxh->magic != cpu_to_be64(MLX5E_TEST_MAGIC)) 210 goto out; /* so close ! */ 211 212 /* bingo */ 213 lbtp->loopback_ok = true; 214 complete(&lbtp->comp); 215 out: 216 kfree_skb(skb); 217 return 0; 218 } 219 220 static int mlx5e_test_loopback_setup(struct mlx5e_priv *priv, 221 struct mlx5e_lbt_priv *lbtp) 222 { 223 int err = 0; 224 225 /* Temporarily enable local_lb */ 226 err = mlx5_nic_vport_query_local_lb(priv->mdev, &lbtp->local_lb); 227 if (err) 228 return err; 229 230 if (!lbtp->local_lb) { 231 err = mlx5_nic_vport_update_local_lb(priv->mdev, true); 232 if (err) 233 return err; 234 } 235 236 err = mlx5e_refresh_tirs(priv, true, false); 237 if (err) 238 goto out; 239 240 lbtp->loopback_ok = false; 241 init_completion(&lbtp->comp); 242 243 lbtp->pt.type = htons(ETH_P_IP); 244 lbtp->pt.func = mlx5e_test_loopback_validate; 245 lbtp->pt.dev = priv->netdev; 246 lbtp->pt.af_packet_priv = lbtp; 247 dev_add_pack(&lbtp->pt); 248 249 return 0; 250 251 out: 252 if (!lbtp->local_lb) 253 mlx5_nic_vport_update_local_lb(priv->mdev, false); 254 255 return err; 256 } 257 258 static void mlx5e_test_loopback_cleanup(struct mlx5e_priv *priv, 259 struct mlx5e_lbt_priv *lbtp) 260 { 261 if (!lbtp->local_lb) 262 mlx5_nic_vport_update_local_lb(priv->mdev, false); 263 264 dev_remove_pack(&lbtp->pt); 265 mlx5e_refresh_tirs(priv, false, false); 266 } 267 268 #define MLX5E_LB_VERIFY_TIMEOUT (msecs_to_jiffies(200)) 269 static int mlx5e_test_loopback(struct mlx5e_priv *priv) 270 { 271 struct mlx5e_lbt_priv *lbtp; 272 struct sk_buff *skb = NULL; 273 int err; 274 275 if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) { 276 netdev_err(priv->netdev, 277 "\tCan't perform loopback test while device is down\n"); 278 return -ENODEV; 279 } 280 281 lbtp = kzalloc(sizeof(*lbtp), GFP_KERNEL); 282 if (!lbtp) 283 return -ENOMEM; 284 lbtp->loopback_ok = false; 285 286 err = mlx5e_test_loopback_setup(priv, lbtp); 287 if (err) 288 goto out; 289 290 skb = mlx5e_test_get_udp_skb(priv); 291 if (!skb) { 292 err = -ENOMEM; 293 goto cleanup; 294 } 295 296 skb_set_queue_mapping(skb, 0); 297 err = dev_queue_xmit(skb); 298 if (err) { 299 netdev_err(priv->netdev, 300 "\tFailed to xmit loopback packet err(%d)\n", 301 err); 302 goto cleanup; 303 } 304 305 wait_for_completion_timeout(&lbtp->comp, MLX5E_LB_VERIFY_TIMEOUT); 306 err = !lbtp->loopback_ok; 307 308 cleanup: 309 mlx5e_test_loopback_cleanup(priv, lbtp); 310 out: 311 kfree(lbtp); 312 return err; 313 } 314 #endif 315 316 static int (*mlx5e_st_func[MLX5E_ST_NUM])(struct mlx5e_priv *) = { 317 mlx5e_test_link_state, 318 mlx5e_test_link_speed, 319 mlx5e_test_health_info, 320 #ifdef CONFIG_INET 321 mlx5e_test_loopback, 322 #endif 323 }; 324 325 void mlx5e_self_test(struct net_device *ndev, struct ethtool_test *etest, 326 u64 *buf) 327 { 328 struct mlx5e_priv *priv = netdev_priv(ndev); 329 int i; 330 331 memset(buf, 0, sizeof(u64) * MLX5E_ST_NUM); 332 333 mutex_lock(&priv->state_lock); 334 netdev_info(ndev, "Self test begin..\n"); 335 336 for (i = 0; i < MLX5E_ST_NUM; i++) { 337 netdev_info(ndev, "\t[%d] %s start..\n", 338 i, mlx5e_self_tests[i]); 339 buf[i] = mlx5e_st_func[i](priv); 340 netdev_info(ndev, "\t[%d] %s end: result(%lld)\n", 341 i, mlx5e_self_tests[i], buf[i]); 342 } 343 344 mutex_unlock(&priv->state_lock); 345 346 for (i = 0; i < MLX5E_ST_NUM; i++) { 347 if (buf[i]) { 348 etest->flags |= ETH_TEST_FL_FAILED; 349 break; 350 } 351 } 352 netdev_info(ndev, "Self test out: status flags(0x%x)\n", 353 etest->flags); 354 } 355