1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
7 #include <linux/skbuff.h>
8 #include <linux/if_arp.h>
9 #include <linux/netdevice.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>
21 static struct rxe_recv_sockets recv_sockets;
23 struct device *rxe_dma_device(struct rxe_dev *rxe)
25 struct net_device *ndev;
29 if (is_vlan_dev(ndev))
30 ndev = vlan_dev_real_dev(ndev);
32 return ndev->dev.parent;
35 int rxe_mcast_add(struct rxe_dev *rxe, union ib_gid *mgid)
38 unsigned char ll_addr[ETH_ALEN];
40 ipv6_eth_mc_map((struct in6_addr *)mgid->raw, ll_addr);
41 err = dev_mc_add(rxe->ndev, ll_addr);
46 int rxe_mcast_delete(struct rxe_dev *rxe, union ib_gid *mgid)
49 unsigned char ll_addr[ETH_ALEN];
51 ipv6_eth_mc_map((struct in6_addr *)mgid->raw, ll_addr);
52 err = dev_mc_del(rxe->ndev, ll_addr);
57 static struct dst_entry *rxe_find_route4(struct net_device *ndev,
58 struct in_addr *saddr,
59 struct in_addr *daddr)
62 struct flowi4 fl = { { 0 } };
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;
70 rt = ip_route_output_key(&init_net, &fl);
72 pr_err_ratelimited("no route to %pI4\n", &daddr->s_addr);
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)
84 struct dst_entry *ndst;
85 struct flowi6 fl6 = { { 0 } };
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;
93 ndst = ipv6_stub->ipv6_dst_lookup_flow(sock_net(recv_sockets.sk6->sk),
94 recv_sockets.sk6->sk, &fl6,
97 pr_err_ratelimited("no route to %pI6\n", daddr);
101 if (unlikely(ndst->error)) {
102 pr_err("no route to %pI6\n", daddr);
114 static struct dst_entry *rxe_find_route6(struct net_device *ndev,
115 struct in6_addr *saddr,
116 struct in6_addr *daddr)
123 static struct dst_entry *rxe_find_route(struct net_device *ndev,
127 struct dst_entry *dst = NULL;
129 if (qp_type(qp) == IB_QPT_RC)
130 dst = sk_dst_get(qp->sk->sk);
132 if (!dst || !dst_check(dst, qp->dst_cookie)) {
136 if (av->network_type == RXE_NETWORK_TYPE_IPV4) {
137 struct in_addr *saddr;
138 struct in_addr *daddr;
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;
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)
153 rt6_get_cookie((struct rt6_info *)dst);
157 if (dst && (qp_type(qp) == IB_QPT_RC)) {
159 sk_dst_set(qp->sk->sk, dst);
165 static int rxe_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
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);
173 if (!rxe && is_vlan_dev(rdev)) {
174 rdev = vlan_dev_real_dev(ndev);
175 rxe = rxe_get_dev_from_net(rdev);
180 if (skb_linearize(skb)) {
181 pr_err("skb_linearize failed\n");
182 ib_device_put(&rxe->ib_dev);
189 pkt->hdr = (u8 *)(udph + 1);
190 pkt->mask = RXE_GRH_MASK;
191 pkt->paylen = be16_to_cpu(udph->len) - sizeof(*udph);
196 * FIXME: this is in the wrong place, it needs to be done when pkt is
199 ib_device_put(&rxe->ib_dev);
208 static struct socket *rxe_setup_udp_tunnel(struct net *net, __be16 port,
213 struct udp_port_cfg udp_cfg = { };
214 struct udp_tunnel_sock_cfg tnl_cfg = { };
217 udp_cfg.family = AF_INET6;
218 udp_cfg.ipv6_v6only = 1;
220 udp_cfg.family = AF_INET;
223 udp_cfg.local_udp_port = port;
225 /* Create UDP socket */
226 err = udp_sock_create(net, &udp_cfg, &sock);
228 pr_err("failed to create udp socket. err = %d\n", err);
232 tnl_cfg.encap_type = 1;
233 tnl_cfg.encap_rcv = rxe_udp_encap_recv;
235 /* Setup UDP tunnel */
236 setup_udp_tunnel_sock(net, sock, &tnl_cfg);
241 static void rxe_release_udp_tunnel(struct socket *sk)
244 udp_tunnel_sock_release(sk);
247 static void prepare_udp_hdr(struct sk_buff *skb, __be16 src_port,
252 __skb_push(skb, sizeof(*udph));
253 skb_reset_transport_header(skb);
256 udph->dest = dst_port;
257 udph->source = src_port;
258 udph->len = htons(skb->len);
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)
268 skb_scrub_packet(skb, xnet);
271 skb_dst_set(skb, dst_clone(dst));
272 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
274 skb_push(skb, sizeof(struct iphdr));
275 skb_reset_network_header(skb);
279 iph->version = IPVERSION;
280 iph->ihl = sizeof(struct iphdr) >> 2;
282 iph->protocol = proto;
287 __ip_select_ident(dev_net(dst->dev), iph,
288 skb_shinfo(skb)->gso_segs ?: 1);
289 iph->tot_len = htons(skb->len);
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)
297 struct ipv6hdr *ip6h;
299 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
300 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED
302 skb_dst_set(skb, dst_clone(dst));
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));
316 static int prepare4(struct rxe_pkt_info *pkt, struct sk_buff *skb)
318 struct rxe_qp *qp = pkt->qp;
319 struct dst_entry *dst;
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;
326 dst = rxe_find_route(skb->dev, qp, av);
328 pr_err("Host not reachable\n");
329 return -EHOSTUNREACH;
332 prepare_udp_hdr(skb, cpu_to_be16(qp->src_port),
333 cpu_to_be16(ROCE_V2_UDP_DPORT));
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);
342 static int prepare6(struct rxe_pkt_info *pkt, struct sk_buff *skb)
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;
350 dst = rxe_find_route(skb->dev, qp, av);
352 pr_err("Host not reachable\n");
353 return -EHOSTUNREACH;
356 prepare_udp_hdr(skb, cpu_to_be16(qp->src_port),
357 cpu_to_be16(ROCE_V2_UDP_DPORT));
359 prepare_ipv6_hdr(dst, skb, saddr, daddr, IPPROTO_UDP,
360 av->grh.traffic_class,
367 int rxe_prepare(struct rxe_pkt_info *pkt, struct sk_buff *skb, u32 *crc)
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);
376 *crc = rxe_icrc_hdr(pkt, skb);
378 if (ether_addr_equal(skb->dev->dev_addr, rxe_get_av(pkt)->dmac))
379 pkt->mask |= RXE_LOOPBACK_MASK;
384 static void rxe_skb_tx_dtor(struct sk_buff *skb)
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);
390 if (unlikely(qp->need_req_skb &&
391 skb_out < RXE_INFLIGHT_SKBS_PER_QP_LOW))
392 rxe_run_task(&qp->req.task, 1);
397 int rxe_send(struct rxe_pkt_info *pkt, struct sk_buff *skb)
401 skb->destructor = rxe_skb_tx_dtor;
402 skb->sk = pkt->qp->sk->sk;
404 rxe_add_ref(pkt->qp);
405 atomic_inc(&pkt->qp->skb_out);
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);
412 pr_err("Unknown layer 3 protocol: %d\n", skb->protocol);
413 atomic_dec(&pkt->qp->skb_out);
414 rxe_drop_ref(pkt->qp);
419 if (unlikely(net_xmit_eval(err))) {
420 pr_debug("error sending packet: %d\n", err);
427 void rxe_loopback(struct sk_buff *skb)
432 struct sk_buff *rxe_init_packet(struct rxe_dev *rxe, struct rxe_av *av,
433 int paylen, struct rxe_pkt_info *pkt)
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;
441 attr = rdma_get_gid_attr(&rxe->ib_dev, port_num, av->grh.sgid_index);
445 if (av->network_type == RXE_NETWORK_TYPE_IPV4)
446 hdr_len = ETH_HLEN + sizeof(struct udphdr) +
447 sizeof(struct iphdr);
449 hdr_len = ETH_HLEN + sizeof(struct udphdr) +
450 sizeof(struct ipv6hdr);
453 ndev = rdma_read_gid_attr_ndev_rcu(attr);
458 skb = alloc_skb(paylen + hdr_len + LL_RESERVED_SPACE(ndev),
461 if (unlikely(!skb)) {
466 skb_reserve(skb, hdr_len + LL_RESERVED_SPACE(ndev));
468 /* FIXME: hold reference to this netdev until life of this skb. */
472 if (av->network_type == RXE_NETWORK_TYPE_IPV4)
473 skb->protocol = htons(ETH_P_IP);
475 skb->protocol = htons(ETH_P_IPV6);
478 pkt->port_num = port_num;
479 pkt->hdr = skb_put_zero(skb, paylen);
480 pkt->mask |= RXE_GRH_MASK;
483 rdma_put_gid_attr(attr);
488 * this is required by rxe_cfg to match rxe devices in
489 * /sys/class/infiniband up with their underlying ethernet devices
491 const char *rxe_parent_name(struct rxe_dev *rxe, unsigned int port_num)
493 return rxe->ndev->name;
496 int rxe_net_add(const char *ibdev_name, struct net_device *ndev)
499 struct rxe_dev *rxe = NULL;
501 rxe = ib_alloc_device(rxe_dev, ib_dev);
507 err = rxe_add(rxe, ndev->mtu, ibdev_name);
509 ib_dealloc_device(&rxe->ib_dev);
516 static void rxe_port_event(struct rxe_dev *rxe,
517 enum ib_event_type event)
521 ev.device = &rxe->ib_dev;
522 ev.element.port_num = 1;
525 ib_dispatch_event(&ev);
528 /* Caller must hold net_info_lock */
529 void rxe_port_up(struct rxe_dev *rxe)
531 struct rxe_port *port;
534 port->attr.state = IB_PORT_ACTIVE;
536 rxe_port_event(rxe, IB_EVENT_PORT_ACTIVE);
537 dev_info(&rxe->ib_dev.dev, "set active\n");
540 /* Caller must hold net_info_lock */
541 void rxe_port_down(struct rxe_dev *rxe)
543 struct rxe_port *port;
546 port->attr.state = IB_PORT_DOWN;
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");
553 void rxe_set_port_state(struct rxe_dev *rxe)
555 if (netif_running(rxe->ndev) && netif_carrier_ok(rxe->ndev))
561 static int rxe_notify(struct notifier_block *not_blk,
565 struct net_device *ndev = netdev_notifier_info_to_dev(arg);
566 struct rxe_dev *rxe = rxe_get_dev_from_net(ndev);
572 case NETDEV_UNREGISTER:
573 ib_unregister_device_queued(&rxe->ib_dev);
581 case NETDEV_CHANGEMTU:
582 pr_info("%s changed mtu to %d\n", ndev->name, ndev->mtu);
583 rxe_set_mtu(rxe, ndev->mtu);
586 rxe_set_port_state(rxe);
589 case NETDEV_GOING_DOWN:
590 case NETDEV_CHANGEADDR:
591 case NETDEV_CHANGENAME:
592 case NETDEV_FEAT_CHANGE:
594 pr_info("ignoring netdev event = %ld for %s\n",
599 ib_device_put(&rxe->ib_dev);
603 static struct notifier_block rxe_net_notifier = {
604 .notifier_call = rxe_notify,
607 static int rxe_net_ipv4_init(void)
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");
620 static int rxe_net_ipv6_init(void)
622 #if IS_ENABLED(CONFIG_IPV6)
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");
635 void rxe_net_exit(void)
637 rxe_release_udp_tunnel(recv_sockets.sk6);
638 rxe_release_udp_tunnel(recv_sockets.sk4);
639 unregister_netdevice_notifier(&rxe_net_notifier);
642 int rxe_net_init(void)
646 recv_sockets.sk6 = NULL;
648 err = rxe_net_ipv4_init();
651 err = rxe_net_ipv6_init();
654 err = register_netdevice_notifier(&rxe_net_notifier);
656 pr_err("Failed to register netdev notifier\n");