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