2 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/skbuff.h>
35 #include <linux/if_arp.h>
36 #include <linux/netdevice.h>
38 #include <linux/if_vlan.h>
39 #include <net/udp_tunnel.h>
40 #include <net/sch_generic.h>
41 #include <linux/netfilter.h>
42 #include <rdma/ib_addr.h>
48 static LIST_HEAD(rxe_dev_list);
49 static spinlock_t dev_list_lock; /* spinlock for device list */
51 struct rxe_dev *net_to_rxe(struct net_device *ndev)
54 struct rxe_dev *found = NULL;
56 spin_lock_bh(&dev_list_lock);
57 list_for_each_entry(rxe, &rxe_dev_list, list) {
58 if (rxe->ndev == ndev) {
63 spin_unlock_bh(&dev_list_lock);
68 struct rxe_dev *get_rxe_by_name(const char *name)
71 struct rxe_dev *found = NULL;
73 spin_lock_bh(&dev_list_lock);
74 list_for_each_entry(rxe, &rxe_dev_list, list) {
75 if (!strcmp(name, rxe->ib_dev.name)) {
80 spin_unlock_bh(&dev_list_lock);
85 struct rxe_recv_sockets recv_sockets;
87 static __be64 rxe_mac_to_eui64(struct net_device *ndev)
89 unsigned char *mac_addr = ndev->dev_addr;
91 unsigned char *dst = (unsigned char *)&eui64;
93 dst[0] = mac_addr[0] ^ 2;
100 dst[7] = mac_addr[5];
105 __be64 rxe_node_guid(struct rxe_dev *rxe)
107 return rxe_mac_to_eui64(rxe->ndev);
110 __be64 rxe_port_guid(struct rxe_dev *rxe)
112 return rxe_mac_to_eui64(rxe->ndev);
115 struct device *rxe_dma_device(struct rxe_dev *rxe)
117 struct net_device *ndev;
121 if (ndev->priv_flags & IFF_802_1Q_VLAN)
122 ndev = vlan_dev_real_dev(ndev);
124 return ndev->dev.parent;
127 int rxe_mcast_add(struct rxe_dev *rxe, union ib_gid *mgid)
130 unsigned char ll_addr[ETH_ALEN];
132 ipv6_eth_mc_map((struct in6_addr *)mgid->raw, ll_addr);
133 err = dev_mc_add(rxe->ndev, ll_addr);
138 int rxe_mcast_delete(struct rxe_dev *rxe, union ib_gid *mgid)
141 unsigned char ll_addr[ETH_ALEN];
143 ipv6_eth_mc_map((struct in6_addr *)mgid->raw, ll_addr);
144 err = dev_mc_del(rxe->ndev, ll_addr);
149 static struct dst_entry *rxe_find_route4(struct net_device *ndev,
150 struct in_addr *saddr,
151 struct in_addr *daddr)
154 struct flowi4 fl = { { 0 } };
156 memset(&fl, 0, sizeof(fl));
157 fl.flowi4_oif = ndev->ifindex;
158 memcpy(&fl.saddr, saddr, sizeof(*saddr));
159 memcpy(&fl.daddr, daddr, sizeof(*daddr));
160 fl.flowi4_proto = IPPROTO_UDP;
162 rt = ip_route_output_key(&init_net, &fl);
164 pr_err_ratelimited("no route to %pI4\n", &daddr->s_addr);
171 #if IS_ENABLED(CONFIG_IPV6)
172 static struct dst_entry *rxe_find_route6(struct net_device *ndev,
173 struct in6_addr *saddr,
174 struct in6_addr *daddr)
176 struct dst_entry *ndst;
177 struct flowi6 fl6 = { { 0 } };
179 memset(&fl6, 0, sizeof(fl6));
180 fl6.flowi6_oif = ndev->ifindex;
181 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
182 memcpy(&fl6.daddr, daddr, sizeof(*daddr));
183 fl6.flowi6_proto = IPPROTO_UDP;
185 ndst = ipv6_stub->ipv6_dst_lookup_flow(sock_net(recv_sockets.sk6->sk),
186 recv_sockets.sk6->sk, &fl6,
188 if (unlikely(IS_ERR(ndst))) {
189 pr_err_ratelimited("no route to %pI6\n", daddr);
193 if (unlikely(ndst->error)) {
194 pr_err("no route to %pI6\n", daddr);
206 static struct dst_entry *rxe_find_route6(struct net_device *ndev,
207 struct in6_addr *saddr,
208 struct in6_addr *daddr)
215 static int rxe_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
218 struct net_device *ndev = skb->dev;
219 struct rxe_dev *rxe = net_to_rxe(ndev);
220 struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
225 if (skb_linearize(skb)) {
226 pr_err("skb_linearize failed\n");
233 pkt->hdr = (u8 *)(udph + 1);
234 pkt->mask = RXE_GRH_MASK;
235 pkt->paylen = be16_to_cpu(udph->len) - sizeof(*udph);
243 static struct socket *rxe_setup_udp_tunnel(struct net *net, __be16 port,
248 struct udp_port_cfg udp_cfg = {0};
249 struct udp_tunnel_sock_cfg tnl_cfg = {0};
252 udp_cfg.family = AF_INET6;
253 udp_cfg.ipv6_v6only = 1;
255 udp_cfg.family = AF_INET;
258 udp_cfg.local_udp_port = port;
260 /* Create UDP socket */
261 err = udp_sock_create(net, &udp_cfg, &sock);
265 tnl_cfg.encap_type = 1;
266 tnl_cfg.encap_rcv = rxe_udp_encap_recv;
268 /* Setup UDP tunnel */
269 setup_udp_tunnel_sock(net, sock, &tnl_cfg);
274 void rxe_release_udp_tunnel(struct socket *sk)
277 udp_tunnel_sock_release(sk);
280 static void prepare_udp_hdr(struct sk_buff *skb, __be16 src_port,
285 __skb_push(skb, sizeof(*udph));
286 skb_reset_transport_header(skb);
289 udph->dest = dst_port;
290 udph->source = src_port;
291 udph->len = htons(skb->len);
295 static void prepare_ipv4_hdr(struct dst_entry *dst, struct sk_buff *skb,
296 __be32 saddr, __be32 daddr, __u8 proto,
297 __u8 tos, __u8 ttl, __be16 df, bool xnet)
301 skb_scrub_packet(skb, xnet);
304 skb_dst_set(skb, dst);
305 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
307 skb_push(skb, sizeof(struct iphdr));
308 skb_reset_network_header(skb);
312 iph->version = IPVERSION;
313 iph->ihl = sizeof(struct iphdr) >> 2;
315 iph->protocol = proto;
320 __ip_select_ident(dev_net(dst->dev), iph,
321 skb_shinfo(skb)->gso_segs ?: 1);
322 iph->tot_len = htons(skb->len);
326 static void prepare_ipv6_hdr(struct dst_entry *dst, struct sk_buff *skb,
327 struct in6_addr *saddr, struct in6_addr *daddr,
328 __u8 proto, __u8 prio, __u8 ttl)
330 struct ipv6hdr *ip6h;
332 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
333 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED
335 skb_dst_set(skb, dst);
337 __skb_push(skb, sizeof(*ip6h));
338 skb_reset_network_header(skb);
339 ip6h = ipv6_hdr(skb);
340 ip6_flow_hdr(ip6h, prio, htonl(0));
341 ip6h->payload_len = htons(skb->len);
342 ip6h->nexthdr = proto;
343 ip6h->hop_limit = ttl;
344 ip6h->daddr = *daddr;
345 ip6h->saddr = *saddr;
346 ip6h->payload_len = htons(skb->len - sizeof(*ip6h));
349 static int prepare4(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
350 struct sk_buff *skb, struct rxe_av *av)
352 struct dst_entry *dst;
354 __be16 df = htons(IP_DF);
355 struct in_addr *saddr = &av->sgid_addr._sockaddr_in.sin_addr;
356 struct in_addr *daddr = &av->dgid_addr._sockaddr_in.sin_addr;
358 dst = rxe_find_route4(rxe->ndev, saddr, daddr);
360 pr_err("Host not reachable\n");
361 return -EHOSTUNREACH;
364 if (!memcmp(saddr, daddr, sizeof(*daddr)))
365 pkt->mask |= RXE_LOOPBACK_MASK;
367 prepare_udp_hdr(skb, htons(RXE_ROCE_V2_SPORT),
368 htons(ROCE_V2_UDP_DPORT));
370 prepare_ipv4_hdr(dst, skb, saddr->s_addr, daddr->s_addr, IPPROTO_UDP,
371 av->grh.traffic_class, av->grh.hop_limit, df, xnet);
375 static int prepare6(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
376 struct sk_buff *skb, struct rxe_av *av)
378 struct dst_entry *dst;
379 struct in6_addr *saddr = &av->sgid_addr._sockaddr_in6.sin6_addr;
380 struct in6_addr *daddr = &av->dgid_addr._sockaddr_in6.sin6_addr;
382 dst = rxe_find_route6(rxe->ndev, saddr, daddr);
384 pr_err("Host not reachable\n");
385 return -EHOSTUNREACH;
388 if (!memcmp(saddr, daddr, sizeof(*daddr)))
389 pkt->mask |= RXE_LOOPBACK_MASK;
391 prepare_udp_hdr(skb, htons(RXE_ROCE_V2_SPORT),
392 htons(ROCE_V2_UDP_DPORT));
394 prepare_ipv6_hdr(dst, skb, saddr, daddr, IPPROTO_UDP,
395 av->grh.traffic_class,
400 int rxe_prepare(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
401 struct sk_buff *skb, u32 *crc)
404 struct rxe_av *av = rxe_get_av(pkt);
406 if (av->network_type == RDMA_NETWORK_IPV4)
407 err = prepare4(rxe, pkt, skb, av);
408 else if (av->network_type == RDMA_NETWORK_IPV6)
409 err = prepare6(rxe, pkt, skb, av);
411 *crc = rxe_icrc_hdr(pkt, skb);
416 static void rxe_skb_tx_dtor(struct sk_buff *skb)
418 struct sock *sk = skb->sk;
419 struct rxe_qp *qp = sk->sk_user_data;
420 int skb_out = atomic_dec_return(&qp->skb_out);
422 if (unlikely(qp->need_req_skb &&
423 skb_out < RXE_INFLIGHT_SKBS_PER_QP_LOW))
424 rxe_run_task(&qp->req.task, 1);
427 int rxe_send(struct rxe_dev *rxe, struct rxe_pkt_info *pkt, struct sk_buff *skb)
429 struct sk_buff *nskb;
433 av = rxe_get_av(pkt);
435 nskb = skb_clone(skb, GFP_ATOMIC);
439 nskb->destructor = rxe_skb_tx_dtor;
440 nskb->sk = pkt->qp->sk->sk;
442 if (av->network_type == RDMA_NETWORK_IPV4) {
443 err = ip_local_out(dev_net(skb_dst(skb)->dev), nskb->sk, nskb);
444 } else if (av->network_type == RDMA_NETWORK_IPV6) {
445 err = ip6_local_out(dev_net(skb_dst(skb)->dev), nskb->sk, nskb);
447 pr_err("Unknown layer 3 protocol: %d\n", av->network_type);
452 if (unlikely(net_xmit_eval(err))) {
453 pr_debug("error sending packet: %d\n", err);
462 int rxe_loopback(struct sk_buff *skb)
467 static inline int addr_same(struct rxe_dev *rxe, struct rxe_av *av)
469 return rxe->port.port_guid == av->grh.dgid.global.interface_id;
472 struct sk_buff *rxe_init_packet(struct rxe_dev *rxe, struct rxe_av *av,
473 int paylen, struct rxe_pkt_info *pkt)
475 unsigned int hdr_len;
478 if (av->network_type == RDMA_NETWORK_IPV4)
479 hdr_len = ETH_HLEN + sizeof(struct udphdr) +
480 sizeof(struct iphdr);
482 hdr_len = ETH_HLEN + sizeof(struct udphdr) +
483 sizeof(struct ipv6hdr);
485 skb = alloc_skb(paylen + hdr_len + LL_RESERVED_SPACE(rxe->ndev),
490 skb_reserve(skb, hdr_len + LL_RESERVED_SPACE(rxe->ndev));
492 skb->dev = rxe->ndev;
493 if (av->network_type == RDMA_NETWORK_IPV4)
494 skb->protocol = htons(ETH_P_IP);
496 skb->protocol = htons(ETH_P_IPV6);
500 pkt->hdr = skb_put(skb, paylen);
501 pkt->mask |= RXE_GRH_MASK;
503 memset(pkt->hdr, 0, paylen);
509 * this is required by rxe_cfg to match rxe devices in
510 * /sys/class/infiniband up with their underlying ethernet devices
512 const char *rxe_parent_name(struct rxe_dev *rxe, unsigned int port_num)
514 return rxe->ndev->name;
517 enum rdma_link_layer rxe_link_layer(struct rxe_dev *rxe, unsigned int port_num)
519 return IB_LINK_LAYER_ETHERNET;
522 struct rxe_dev *rxe_net_add(struct net_device *ndev)
525 struct rxe_dev *rxe = NULL;
527 rxe = (struct rxe_dev *)ib_alloc_device(sizeof(*rxe));
533 err = rxe_add(rxe, ndev->mtu);
535 ib_dealloc_device(&rxe->ib_dev);
539 spin_lock_bh(&dev_list_lock);
540 list_add_tail(&rxe->list, &rxe_dev_list);
541 spin_unlock_bh(&dev_list_lock);
545 void rxe_remove_all(void)
547 spin_lock_bh(&dev_list_lock);
548 while (!list_empty(&rxe_dev_list)) {
549 struct rxe_dev *rxe =
550 list_first_entry(&rxe_dev_list, struct rxe_dev, list);
552 list_del(&rxe->list);
553 spin_unlock_bh(&dev_list_lock);
555 spin_lock_bh(&dev_list_lock);
557 spin_unlock_bh(&dev_list_lock);
559 EXPORT_SYMBOL(rxe_remove_all);
561 static void rxe_port_event(struct rxe_dev *rxe,
562 enum ib_event_type event)
566 ev.device = &rxe->ib_dev;
567 ev.element.port_num = 1;
570 ib_dispatch_event(&ev);
573 /* Caller must hold net_info_lock */
574 void rxe_port_up(struct rxe_dev *rxe)
576 struct rxe_port *port;
579 port->attr.state = IB_PORT_ACTIVE;
580 port->attr.phys_state = IB_PHYS_STATE_LINK_UP;
582 rxe_port_event(rxe, IB_EVENT_PORT_ACTIVE);
583 pr_info("set %s active\n", rxe->ib_dev.name);
586 /* Caller must hold net_info_lock */
587 void rxe_port_down(struct rxe_dev *rxe)
589 struct rxe_port *port;
592 port->attr.state = IB_PORT_DOWN;
593 port->attr.phys_state = IB_PHYS_STATE_LINK_DOWN;
595 rxe_port_event(rxe, IB_EVENT_PORT_ERR);
596 pr_info("set %s down\n", rxe->ib_dev.name);
599 static int rxe_notify(struct notifier_block *not_blk,
603 struct net_device *ndev = netdev_notifier_info_to_dev(arg);
604 struct rxe_dev *rxe = net_to_rxe(ndev);
610 case NETDEV_UNREGISTER:
611 list_del(&rxe->list);
620 case NETDEV_CHANGEMTU:
621 pr_info("%s changed mtu to %d\n", ndev->name, ndev->mtu);
622 rxe_set_mtu(rxe, ndev->mtu);
626 case NETDEV_GOING_DOWN:
627 case NETDEV_CHANGEADDR:
628 case NETDEV_CHANGENAME:
629 case NETDEV_FEAT_CHANGE:
631 pr_info("ignoring netdev event = %ld for %s\n",
639 struct notifier_block rxe_net_notifier = {
640 .notifier_call = rxe_notify,
643 int rxe_net_ipv4_init(void)
645 spin_lock_init(&dev_list_lock);
647 recv_sockets.sk4 = rxe_setup_udp_tunnel(&init_net,
648 htons(ROCE_V2_UDP_DPORT), false);
649 if (IS_ERR(recv_sockets.sk4)) {
650 recv_sockets.sk4 = NULL;
651 pr_err("Failed to create IPv4 UDP tunnel\n");
658 int rxe_net_ipv6_init(void)
660 #if IS_ENABLED(CONFIG_IPV6)
662 spin_lock_init(&dev_list_lock);
664 recv_sockets.sk6 = rxe_setup_udp_tunnel(&init_net,
665 htons(ROCE_V2_UDP_DPORT), true);
666 if (PTR_ERR(recv_sockets.sk6) == -EAFNOSUPPORT) {
667 recv_sockets.sk6 = NULL;
668 pr_warn("IPv6 is not supported, can not create a UDPv6 socket\n");
672 if (IS_ERR(recv_sockets.sk6)) {
673 recv_sockets.sk6 = NULL;
674 pr_err("Failed to create IPv6 UDP tunnel\n");
681 void rxe_net_exit(void)
683 rxe_release_udp_tunnel(recv_sockets.sk6);
684 rxe_release_udp_tunnel(recv_sockets.sk4);
685 unregister_netdevice_notifier(&rxe_net_notifier);
688 int rxe_net_init(void)
692 recv_sockets.sk6 = NULL;
694 err = rxe_net_ipv4_init();
697 err = rxe_net_ipv6_init();
700 err = register_netdevice_notifier(&rxe_net_notifier);
702 pr_err("Failed to register netdev notifier\n");