1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2 /* 3 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved. 4 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved. 5 */ 6 7 #include <linux/skbuff.h> 8 #include <linux/if_arp.h> 9 #include <linux/netdevice.h> 10 #include <linux/if.h> 11 #include <linux/if_vlan.h> 12 #include <net/udp_tunnel.h> 13 #include <net/sch_generic.h> 14 #include <linux/netfilter.h> 15 #include <rdma/ib_addr.h> 16 17 #include "rxe.h" 18 #include "rxe_net.h" 19 #include "rxe_loc.h" 20 21 static struct rxe_recv_sockets recv_sockets; 22 23 static struct dst_entry *rxe_find_route4(struct rxe_qp *qp, 24 struct net_device *ndev, 25 struct in_addr *saddr, 26 struct in_addr *daddr) 27 { 28 struct rtable *rt; 29 struct flowi4 fl = { { 0 } }; 30 31 memset(&fl, 0, sizeof(fl)); 32 fl.flowi4_oif = ndev->ifindex; 33 memcpy(&fl.saddr, saddr, sizeof(*saddr)); 34 memcpy(&fl.daddr, daddr, sizeof(*daddr)); 35 fl.flowi4_proto = IPPROTO_UDP; 36 37 rt = ip_route_output_key(&init_net, &fl); 38 if (IS_ERR(rt)) { 39 rxe_dbg_qp(qp, "no route to %pI4\n", &daddr->s_addr); 40 return NULL; 41 } 42 43 return &rt->dst; 44 } 45 46 #if IS_ENABLED(CONFIG_IPV6) 47 static struct dst_entry *rxe_find_route6(struct rxe_qp *qp, 48 struct net_device *ndev, 49 struct in6_addr *saddr, 50 struct in6_addr *daddr) 51 { 52 struct dst_entry *ndst; 53 struct flowi6 fl6 = { { 0 } }; 54 55 memset(&fl6, 0, sizeof(fl6)); 56 fl6.flowi6_oif = ndev->ifindex; 57 memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 58 memcpy(&fl6.daddr, daddr, sizeof(*daddr)); 59 fl6.flowi6_proto = IPPROTO_UDP; 60 61 ndst = ipv6_stub->ipv6_dst_lookup_flow(sock_net(recv_sockets.sk6->sk), 62 recv_sockets.sk6->sk, &fl6, 63 NULL); 64 if (IS_ERR(ndst)) { 65 rxe_dbg_qp(qp, "no route to %pI6\n", daddr); 66 return NULL; 67 } 68 69 if (unlikely(ndst->error)) { 70 rxe_dbg_qp(qp, "no route to %pI6\n", daddr); 71 goto put; 72 } 73 74 return ndst; 75 put: 76 dst_release(ndst); 77 return NULL; 78 } 79 80 #else 81 82 static struct dst_entry *rxe_find_route6(struct rxe_qp *qp, 83 struct net_device *ndev, 84 struct in6_addr *saddr, 85 struct in6_addr *daddr) 86 { 87 return NULL; 88 } 89 90 #endif 91 92 static struct dst_entry *rxe_find_route(struct net_device *ndev, 93 struct rxe_qp *qp, 94 struct rxe_av *av) 95 { 96 struct dst_entry *dst = NULL; 97 98 if (qp_type(qp) == IB_QPT_RC) 99 dst = sk_dst_get(qp->sk->sk); 100 101 if (!dst || !dst_check(dst, qp->dst_cookie)) { 102 if (dst) 103 dst_release(dst); 104 105 if (av->network_type == RXE_NETWORK_TYPE_IPV4) { 106 struct in_addr *saddr; 107 struct in_addr *daddr; 108 109 saddr = &av->sgid_addr._sockaddr_in.sin_addr; 110 daddr = &av->dgid_addr._sockaddr_in.sin_addr; 111 dst = rxe_find_route4(qp, ndev, saddr, daddr); 112 } else if (av->network_type == RXE_NETWORK_TYPE_IPV6) { 113 struct in6_addr *saddr6; 114 struct in6_addr *daddr6; 115 116 saddr6 = &av->sgid_addr._sockaddr_in6.sin6_addr; 117 daddr6 = &av->dgid_addr._sockaddr_in6.sin6_addr; 118 dst = rxe_find_route6(qp, ndev, saddr6, daddr6); 119 #if IS_ENABLED(CONFIG_IPV6) 120 if (dst) 121 qp->dst_cookie = 122 rt6_get_cookie((struct rt6_info *)dst); 123 #endif 124 } 125 126 if (dst && (qp_type(qp) == IB_QPT_RC)) { 127 dst_hold(dst); 128 sk_dst_set(qp->sk->sk, dst); 129 } 130 } 131 return dst; 132 } 133 134 static int rxe_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 135 { 136 struct udphdr *udph; 137 struct rxe_dev *rxe; 138 struct net_device *ndev = skb->dev; 139 struct rxe_pkt_info *pkt = SKB_TO_PKT(skb); 140 141 /* takes a reference on rxe->ib_dev 142 * drop when skb is freed 143 */ 144 rxe = rxe_get_dev_from_net(ndev); 145 if (!rxe && is_vlan_dev(ndev)) 146 rxe = rxe_get_dev_from_net(vlan_dev_real_dev(ndev)); 147 if (!rxe) 148 goto drop; 149 150 if (skb_linearize(skb)) { 151 ib_device_put(&rxe->ib_dev); 152 goto drop; 153 } 154 155 udph = udp_hdr(skb); 156 pkt->rxe = rxe; 157 pkt->port_num = 1; 158 pkt->hdr = (u8 *)(udph + 1); 159 pkt->mask = RXE_GRH_MASK; 160 pkt->paylen = be16_to_cpu(udph->len) - sizeof(*udph); 161 162 rxe_rcv(skb); 163 164 return 0; 165 drop: 166 kfree_skb(skb); 167 168 return 0; 169 } 170 171 static struct socket *rxe_setup_udp_tunnel(struct net *net, __be16 port, 172 bool ipv6) 173 { 174 int err; 175 struct socket *sock; 176 struct udp_port_cfg udp_cfg = { }; 177 struct udp_tunnel_sock_cfg tnl_cfg = { }; 178 179 if (ipv6) { 180 udp_cfg.family = AF_INET6; 181 udp_cfg.ipv6_v6only = 1; 182 } else { 183 udp_cfg.family = AF_INET; 184 } 185 186 udp_cfg.local_udp_port = port; 187 188 /* Create UDP socket */ 189 err = udp_sock_create(net, &udp_cfg, &sock); 190 if (err < 0) 191 return ERR_PTR(err); 192 193 tnl_cfg.encap_type = 1; 194 tnl_cfg.encap_rcv = rxe_udp_encap_recv; 195 196 /* Setup UDP tunnel */ 197 setup_udp_tunnel_sock(net, sock, &tnl_cfg); 198 199 return sock; 200 } 201 202 static void rxe_release_udp_tunnel(struct socket *sk) 203 { 204 if (sk) 205 udp_tunnel_sock_release(sk); 206 } 207 208 static void prepare_udp_hdr(struct sk_buff *skb, __be16 src_port, 209 __be16 dst_port) 210 { 211 struct udphdr *udph; 212 213 __skb_push(skb, sizeof(*udph)); 214 skb_reset_transport_header(skb); 215 udph = udp_hdr(skb); 216 217 udph->dest = dst_port; 218 udph->source = src_port; 219 udph->len = htons(skb->len); 220 udph->check = 0; 221 } 222 223 static void prepare_ipv4_hdr(struct dst_entry *dst, struct sk_buff *skb, 224 __be32 saddr, __be32 daddr, __u8 proto, 225 __u8 tos, __u8 ttl, __be16 df, bool xnet) 226 { 227 struct iphdr *iph; 228 229 skb_scrub_packet(skb, xnet); 230 231 skb_clear_hash(skb); 232 skb_dst_set(skb, dst_clone(dst)); 233 memset(IPCB(skb), 0, sizeof(*IPCB(skb))); 234 235 skb_push(skb, sizeof(struct iphdr)); 236 skb_reset_network_header(skb); 237 238 iph = ip_hdr(skb); 239 240 iph->version = IPVERSION; 241 iph->ihl = sizeof(struct iphdr) >> 2; 242 iph->tot_len = htons(skb->len); 243 iph->frag_off = df; 244 iph->protocol = proto; 245 iph->tos = tos; 246 iph->daddr = daddr; 247 iph->saddr = saddr; 248 iph->ttl = ttl; 249 __ip_select_ident(dev_net(dst->dev), iph, 250 skb_shinfo(skb)->gso_segs ?: 1); 251 } 252 253 static void prepare_ipv6_hdr(struct dst_entry *dst, struct sk_buff *skb, 254 struct in6_addr *saddr, struct in6_addr *daddr, 255 __u8 proto, __u8 prio, __u8 ttl) 256 { 257 struct ipv6hdr *ip6h; 258 259 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 260 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED 261 | IPSKB_REROUTED); 262 skb_dst_set(skb, dst_clone(dst)); 263 264 __skb_push(skb, sizeof(*ip6h)); 265 skb_reset_network_header(skb); 266 ip6h = ipv6_hdr(skb); 267 ip6_flow_hdr(ip6h, prio, htonl(0)); 268 ip6h->payload_len = htons(skb->len); 269 ip6h->nexthdr = proto; 270 ip6h->hop_limit = ttl; 271 ip6h->daddr = *daddr; 272 ip6h->saddr = *saddr; 273 ip6h->payload_len = htons(skb->len - sizeof(*ip6h)); 274 } 275 276 static int prepare4(struct rxe_av *av, struct rxe_pkt_info *pkt, 277 struct sk_buff *skb) 278 { 279 struct rxe_qp *qp = pkt->qp; 280 struct dst_entry *dst; 281 bool xnet = false; 282 __be16 df = htons(IP_DF); 283 struct in_addr *saddr = &av->sgid_addr._sockaddr_in.sin_addr; 284 struct in_addr *daddr = &av->dgid_addr._sockaddr_in.sin_addr; 285 286 dst = rxe_find_route(skb->dev, qp, av); 287 if (!dst) { 288 rxe_dbg_qp(qp, "Host not reachable\n"); 289 return -EHOSTUNREACH; 290 } 291 292 prepare_udp_hdr(skb, cpu_to_be16(qp->src_port), 293 cpu_to_be16(ROCE_V2_UDP_DPORT)); 294 295 prepare_ipv4_hdr(dst, skb, saddr->s_addr, daddr->s_addr, IPPROTO_UDP, 296 av->grh.traffic_class, av->grh.hop_limit, df, xnet); 297 298 dst_release(dst); 299 return 0; 300 } 301 302 static int prepare6(struct rxe_av *av, struct rxe_pkt_info *pkt, 303 struct sk_buff *skb) 304 { 305 struct rxe_qp *qp = pkt->qp; 306 struct dst_entry *dst; 307 struct in6_addr *saddr = &av->sgid_addr._sockaddr_in6.sin6_addr; 308 struct in6_addr *daddr = &av->dgid_addr._sockaddr_in6.sin6_addr; 309 310 dst = rxe_find_route(skb->dev, qp, av); 311 if (!dst) { 312 rxe_dbg_qp(qp, "Host not reachable\n"); 313 return -EHOSTUNREACH; 314 } 315 316 prepare_udp_hdr(skb, cpu_to_be16(qp->src_port), 317 cpu_to_be16(ROCE_V2_UDP_DPORT)); 318 319 prepare_ipv6_hdr(dst, skb, saddr, daddr, IPPROTO_UDP, 320 av->grh.traffic_class, 321 av->grh.hop_limit); 322 323 dst_release(dst); 324 return 0; 325 } 326 327 int rxe_prepare(struct rxe_av *av, struct rxe_pkt_info *pkt, 328 struct sk_buff *skb) 329 { 330 int err = 0; 331 332 if (skb->protocol == htons(ETH_P_IP)) 333 err = prepare4(av, pkt, skb); 334 else if (skb->protocol == htons(ETH_P_IPV6)) 335 err = prepare6(av, pkt, skb); 336 337 if (ether_addr_equal(skb->dev->dev_addr, av->dmac)) 338 pkt->mask |= RXE_LOOPBACK_MASK; 339 340 return err; 341 } 342 343 static void rxe_skb_tx_dtor(struct sk_buff *skb) 344 { 345 struct sock *sk = skb->sk; 346 struct rxe_qp *qp = sk->sk_user_data; 347 int skb_out = atomic_dec_return(&qp->skb_out); 348 349 if (unlikely(qp->need_req_skb && 350 skb_out < RXE_INFLIGHT_SKBS_PER_QP_LOW)) 351 rxe_sched_task(&qp->req.task); 352 353 rxe_put(qp); 354 } 355 356 static int rxe_send(struct sk_buff *skb, struct rxe_pkt_info *pkt) 357 { 358 int err; 359 360 skb->destructor = rxe_skb_tx_dtor; 361 skb->sk = pkt->qp->sk->sk; 362 363 rxe_get(pkt->qp); 364 atomic_inc(&pkt->qp->skb_out); 365 366 if (skb->protocol == htons(ETH_P_IP)) { 367 err = ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb); 368 } else if (skb->protocol == htons(ETH_P_IPV6)) { 369 err = ip6_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb); 370 } else { 371 rxe_dbg_qp(pkt->qp, "Unknown layer 3 protocol: %d\n", 372 skb->protocol); 373 atomic_dec(&pkt->qp->skb_out); 374 rxe_put(pkt->qp); 375 kfree_skb(skb); 376 return -EINVAL; 377 } 378 379 if (unlikely(net_xmit_eval(err))) { 380 rxe_dbg_qp(pkt->qp, "error sending packet: %d\n", err); 381 return -EAGAIN; 382 } 383 384 return 0; 385 } 386 387 /* fix up a send packet to match the packets 388 * received from UDP before looping them back 389 */ 390 static int rxe_loopback(struct sk_buff *skb, struct rxe_pkt_info *pkt) 391 { 392 memcpy(SKB_TO_PKT(skb), pkt, sizeof(*pkt)); 393 394 if (skb->protocol == htons(ETH_P_IP)) 395 skb_pull(skb, sizeof(struct iphdr)); 396 else 397 skb_pull(skb, sizeof(struct ipv6hdr)); 398 399 if (WARN_ON(!ib_device_try_get(&pkt->rxe->ib_dev))) { 400 kfree_skb(skb); 401 return -EIO; 402 } 403 404 rxe_rcv(skb); 405 406 return 0; 407 } 408 409 int rxe_xmit_packet(struct rxe_qp *qp, struct rxe_pkt_info *pkt, 410 struct sk_buff *skb) 411 { 412 int err; 413 int is_request = pkt->mask & RXE_REQ_MASK; 414 struct rxe_dev *rxe = to_rdev(qp->ibqp.device); 415 416 spin_lock_bh(&qp->state_lock); 417 if ((is_request && (qp_state(qp) < IB_QPS_RTS)) || 418 (!is_request && (qp_state(qp) < IB_QPS_RTR))) { 419 spin_unlock_bh(&qp->state_lock); 420 rxe_dbg_qp(qp, "Packet dropped. QP is not in ready state\n"); 421 goto drop; 422 } 423 spin_unlock_bh(&qp->state_lock); 424 425 rxe_icrc_generate(skb, pkt); 426 427 if (pkt->mask & RXE_LOOPBACK_MASK) 428 err = rxe_loopback(skb, pkt); 429 else 430 err = rxe_send(skb, pkt); 431 if (err) { 432 rxe_counter_inc(rxe, RXE_CNT_SEND_ERR); 433 return err; 434 } 435 436 if ((qp_type(qp) != IB_QPT_RC) && 437 (pkt->mask & RXE_END_MASK)) { 438 pkt->wqe->state = wqe_state_done; 439 rxe_sched_task(&qp->comp.task); 440 } 441 442 rxe_counter_inc(rxe, RXE_CNT_SENT_PKTS); 443 goto done; 444 445 drop: 446 kfree_skb(skb); 447 err = 0; 448 done: 449 return err; 450 } 451 452 struct sk_buff *rxe_init_packet(struct rxe_dev *rxe, struct rxe_av *av, 453 int paylen, struct rxe_pkt_info *pkt) 454 { 455 unsigned int hdr_len; 456 struct sk_buff *skb = NULL; 457 struct net_device *ndev; 458 const struct ib_gid_attr *attr; 459 const int port_num = 1; 460 461 attr = rdma_get_gid_attr(&rxe->ib_dev, port_num, av->grh.sgid_index); 462 if (IS_ERR(attr)) 463 return NULL; 464 465 if (av->network_type == RXE_NETWORK_TYPE_IPV4) 466 hdr_len = ETH_HLEN + sizeof(struct udphdr) + 467 sizeof(struct iphdr); 468 else 469 hdr_len = ETH_HLEN + sizeof(struct udphdr) + 470 sizeof(struct ipv6hdr); 471 472 rcu_read_lock(); 473 ndev = rdma_read_gid_attr_ndev_rcu(attr); 474 if (IS_ERR(ndev)) { 475 rcu_read_unlock(); 476 goto out; 477 } 478 skb = alloc_skb(paylen + hdr_len + LL_RESERVED_SPACE(ndev), 479 GFP_ATOMIC); 480 481 if (unlikely(!skb)) { 482 rcu_read_unlock(); 483 goto out; 484 } 485 486 skb_reserve(skb, hdr_len + LL_RESERVED_SPACE(ndev)); 487 488 /* FIXME: hold reference to this netdev until life of this skb. */ 489 skb->dev = ndev; 490 rcu_read_unlock(); 491 492 if (av->network_type == RXE_NETWORK_TYPE_IPV4) 493 skb->protocol = htons(ETH_P_IP); 494 else 495 skb->protocol = htons(ETH_P_IPV6); 496 497 pkt->rxe = rxe; 498 pkt->port_num = port_num; 499 pkt->hdr = skb_put(skb, paylen); 500 pkt->mask |= RXE_GRH_MASK; 501 502 out: 503 rdma_put_gid_attr(attr); 504 return skb; 505 } 506 507 /* 508 * this is required by rxe_cfg to match rxe devices in 509 * /sys/class/infiniband up with their underlying ethernet devices 510 */ 511 const char *rxe_parent_name(struct rxe_dev *rxe, unsigned int port_num) 512 { 513 return rxe->ndev->name; 514 } 515 516 int rxe_net_add(const char *ibdev_name, struct net_device *ndev) 517 { 518 int err; 519 struct rxe_dev *rxe = NULL; 520 521 rxe = ib_alloc_device(rxe_dev, ib_dev); 522 if (!rxe) 523 return -ENOMEM; 524 525 rxe->ndev = ndev; 526 527 err = rxe_add(rxe, ndev->mtu, ibdev_name); 528 if (err) { 529 ib_dealloc_device(&rxe->ib_dev); 530 return err; 531 } 532 533 return 0; 534 } 535 536 static void rxe_port_event(struct rxe_dev *rxe, 537 enum ib_event_type event) 538 { 539 struct ib_event ev; 540 541 ev.device = &rxe->ib_dev; 542 ev.element.port_num = 1; 543 ev.event = event; 544 545 ib_dispatch_event(&ev); 546 } 547 548 /* Caller must hold net_info_lock */ 549 void rxe_port_up(struct rxe_dev *rxe) 550 { 551 struct rxe_port *port; 552 553 port = &rxe->port; 554 port->attr.state = IB_PORT_ACTIVE; 555 556 rxe_port_event(rxe, IB_EVENT_PORT_ACTIVE); 557 dev_info(&rxe->ib_dev.dev, "set active\n"); 558 } 559 560 /* Caller must hold net_info_lock */ 561 void rxe_port_down(struct rxe_dev *rxe) 562 { 563 struct rxe_port *port; 564 565 port = &rxe->port; 566 port->attr.state = IB_PORT_DOWN; 567 568 rxe_port_event(rxe, IB_EVENT_PORT_ERR); 569 rxe_counter_inc(rxe, RXE_CNT_LINK_DOWNED); 570 dev_info(&rxe->ib_dev.dev, "set down\n"); 571 } 572 573 void rxe_set_port_state(struct rxe_dev *rxe) 574 { 575 if (netif_running(rxe->ndev) && netif_carrier_ok(rxe->ndev)) 576 rxe_port_up(rxe); 577 else 578 rxe_port_down(rxe); 579 } 580 581 static int rxe_notify(struct notifier_block *not_blk, 582 unsigned long event, 583 void *arg) 584 { 585 struct net_device *ndev = netdev_notifier_info_to_dev(arg); 586 struct rxe_dev *rxe = rxe_get_dev_from_net(ndev); 587 588 if (!rxe) 589 return NOTIFY_OK; 590 591 switch (event) { 592 case NETDEV_UNREGISTER: 593 ib_unregister_device_queued(&rxe->ib_dev); 594 break; 595 case NETDEV_UP: 596 rxe_port_up(rxe); 597 break; 598 case NETDEV_DOWN: 599 rxe_port_down(rxe); 600 break; 601 case NETDEV_CHANGEMTU: 602 rxe_dbg_dev(rxe, "%s changed mtu to %d\n", ndev->name, ndev->mtu); 603 rxe_set_mtu(rxe, ndev->mtu); 604 break; 605 case NETDEV_CHANGE: 606 rxe_set_port_state(rxe); 607 break; 608 case NETDEV_REBOOT: 609 case NETDEV_GOING_DOWN: 610 case NETDEV_CHANGEADDR: 611 case NETDEV_CHANGENAME: 612 case NETDEV_FEAT_CHANGE: 613 default: 614 rxe_dbg_dev(rxe, "ignoring netdev event = %ld for %s\n", 615 event, ndev->name); 616 break; 617 } 618 619 ib_device_put(&rxe->ib_dev); 620 return NOTIFY_OK; 621 } 622 623 static struct notifier_block rxe_net_notifier = { 624 .notifier_call = rxe_notify, 625 }; 626 627 static int rxe_net_ipv4_init(void) 628 { 629 recv_sockets.sk4 = rxe_setup_udp_tunnel(&init_net, 630 htons(ROCE_V2_UDP_DPORT), false); 631 if (IS_ERR(recv_sockets.sk4)) { 632 recv_sockets.sk4 = NULL; 633 pr_err("Failed to create IPv4 UDP tunnel\n"); 634 return -1; 635 } 636 637 return 0; 638 } 639 640 static int rxe_net_ipv6_init(void) 641 { 642 #if IS_ENABLED(CONFIG_IPV6) 643 644 recv_sockets.sk6 = rxe_setup_udp_tunnel(&init_net, 645 htons(ROCE_V2_UDP_DPORT), true); 646 if (PTR_ERR(recv_sockets.sk6) == -EAFNOSUPPORT) { 647 recv_sockets.sk6 = NULL; 648 pr_warn("IPv6 is not supported, can not create a UDPv6 socket\n"); 649 return 0; 650 } 651 652 if (IS_ERR(recv_sockets.sk6)) { 653 recv_sockets.sk6 = NULL; 654 pr_err("Failed to create IPv6 UDP tunnel\n"); 655 return -1; 656 } 657 #endif 658 return 0; 659 } 660 661 void rxe_net_exit(void) 662 { 663 rxe_release_udp_tunnel(recv_sockets.sk6); 664 rxe_release_udp_tunnel(recv_sockets.sk4); 665 unregister_netdevice_notifier(&rxe_net_notifier); 666 } 667 668 int rxe_net_init(void) 669 { 670 int err; 671 672 recv_sockets.sk6 = NULL; 673 674 err = rxe_net_ipv4_init(); 675 if (err) 676 return err; 677 err = rxe_net_ipv6_init(); 678 if (err) 679 goto err_out; 680 err = register_netdevice_notifier(&rxe_net_notifier); 681 if (err) { 682 pr_err("Failed to register netdev notifier\n"); 683 goto err_out; 684 } 685 return 0; 686 err_out: 687 rxe_net_exit(); 688 return err; 689 } 690