Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / infiniband / sw / rxe / rxe_net.c
blob159246b03867812e3feae2cfec4651faae893e3c
1 /*
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
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
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
31 * SOFTWARE.
34 #include <linux/skbuff.h>
35 #include <linux/if_arp.h>
36 #include <linux/netdevice.h>
37 #include <linux/if.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>
44 #include "rxe.h"
45 #include "rxe_net.h"
46 #include "rxe_loc.h"
48 static LIST_HEAD(rxe_dev_list);
49 static DEFINE_SPINLOCK(dev_list_lock); /* spinlock for device list */
51 struct rxe_dev *net_to_rxe(struct net_device *ndev)
53 struct rxe_dev *rxe;
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) {
59 found = rxe;
60 break;
63 spin_unlock_bh(&dev_list_lock);
65 return found;
68 struct rxe_dev *get_rxe_by_name(const char *name)
70 struct rxe_dev *rxe;
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)) {
76 found = rxe;
77 break;
80 spin_unlock_bh(&dev_list_lock);
81 return found;
85 static struct rxe_recv_sockets recv_sockets;
87 struct device *rxe_dma_device(struct rxe_dev *rxe)
89 struct net_device *ndev;
91 ndev = rxe->ndev;
93 if (is_vlan_dev(ndev))
94 ndev = vlan_dev_real_dev(ndev);
96 return ndev->dev.parent;
99 int rxe_mcast_add(struct rxe_dev *rxe, union ib_gid *mgid)
101 int err;
102 unsigned char ll_addr[ETH_ALEN];
104 ipv6_eth_mc_map((struct in6_addr *)mgid->raw, ll_addr);
105 err = dev_mc_add(rxe->ndev, ll_addr);
107 return err;
110 int rxe_mcast_delete(struct rxe_dev *rxe, union ib_gid *mgid)
112 int err;
113 unsigned char ll_addr[ETH_ALEN];
115 ipv6_eth_mc_map((struct in6_addr *)mgid->raw, ll_addr);
116 err = dev_mc_del(rxe->ndev, ll_addr);
118 return err;
121 static struct dst_entry *rxe_find_route4(struct net_device *ndev,
122 struct in_addr *saddr,
123 struct in_addr *daddr)
125 struct rtable *rt;
126 struct flowi4 fl = { { 0 } };
128 memset(&fl, 0, sizeof(fl));
129 fl.flowi4_oif = ndev->ifindex;
130 memcpy(&fl.saddr, saddr, sizeof(*saddr));
131 memcpy(&fl.daddr, daddr, sizeof(*daddr));
132 fl.flowi4_proto = IPPROTO_UDP;
134 rt = ip_route_output_key(&init_net, &fl);
135 if (IS_ERR(rt)) {
136 pr_err_ratelimited("no route to %pI4\n", &daddr->s_addr);
137 return NULL;
140 return &rt->dst;
143 #if IS_ENABLED(CONFIG_IPV6)
144 static struct dst_entry *rxe_find_route6(struct net_device *ndev,
145 struct in6_addr *saddr,
146 struct in6_addr *daddr)
148 struct dst_entry *ndst;
149 struct flowi6 fl6 = { { 0 } };
151 memset(&fl6, 0, sizeof(fl6));
152 fl6.flowi6_oif = ndev->ifindex;
153 memcpy(&fl6.saddr, saddr, sizeof(*saddr));
154 memcpy(&fl6.daddr, daddr, sizeof(*daddr));
155 fl6.flowi6_proto = IPPROTO_UDP;
157 if (unlikely(ipv6_stub->ipv6_dst_lookup(sock_net(recv_sockets.sk6->sk),
158 recv_sockets.sk6->sk, &ndst, &fl6))) {
159 pr_err_ratelimited("no route to %pI6\n", daddr);
160 goto put;
163 if (unlikely(ndst->error)) {
164 pr_err("no route to %pI6\n", daddr);
165 goto put;
168 return ndst;
169 put:
170 dst_release(ndst);
171 return NULL;
174 #else
176 static struct dst_entry *rxe_find_route6(struct net_device *ndev,
177 struct in6_addr *saddr,
178 struct in6_addr *daddr)
180 return NULL;
183 #endif
185 static struct dst_entry *rxe_find_route(struct rxe_dev *rxe,
186 struct rxe_qp *qp,
187 struct rxe_av *av)
189 struct dst_entry *dst = NULL;
191 if (qp_type(qp) == IB_QPT_RC)
192 dst = sk_dst_get(qp->sk->sk);
194 if (!dst || !dst_check(dst, qp->dst_cookie)) {
195 if (dst)
196 dst_release(dst);
198 if (av->network_type == RDMA_NETWORK_IPV4) {
199 struct in_addr *saddr;
200 struct in_addr *daddr;
202 saddr = &av->sgid_addr._sockaddr_in.sin_addr;
203 daddr = &av->dgid_addr._sockaddr_in.sin_addr;
204 dst = rxe_find_route4(rxe->ndev, saddr, daddr);
205 } else if (av->network_type == RDMA_NETWORK_IPV6) {
206 struct in6_addr *saddr6;
207 struct in6_addr *daddr6;
209 saddr6 = &av->sgid_addr._sockaddr_in6.sin6_addr;
210 daddr6 = &av->dgid_addr._sockaddr_in6.sin6_addr;
211 dst = rxe_find_route6(rxe->ndev, saddr6, daddr6);
212 #if IS_ENABLED(CONFIG_IPV6)
213 if (dst)
214 qp->dst_cookie =
215 rt6_get_cookie((struct rt6_info *)dst);
216 #endif
220 return dst;
223 static int rxe_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
225 struct udphdr *udph;
226 struct net_device *ndev = skb->dev;
227 struct rxe_dev *rxe = net_to_rxe(ndev);
228 struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
230 if (!rxe)
231 goto drop;
233 if (skb_linearize(skb)) {
234 pr_err("skb_linearize failed\n");
235 goto drop;
238 udph = udp_hdr(skb);
239 pkt->rxe = rxe;
240 pkt->port_num = 1;
241 pkt->hdr = (u8 *)(udph + 1);
242 pkt->mask = RXE_GRH_MASK;
243 pkt->paylen = be16_to_cpu(udph->len) - sizeof(*udph);
245 return rxe_rcv(skb);
246 drop:
247 kfree_skb(skb);
248 return 0;
251 static struct socket *rxe_setup_udp_tunnel(struct net *net, __be16 port,
252 bool ipv6)
254 int err;
255 struct socket *sock;
256 struct udp_port_cfg udp_cfg = { };
257 struct udp_tunnel_sock_cfg tnl_cfg = { };
259 if (ipv6) {
260 udp_cfg.family = AF_INET6;
261 udp_cfg.ipv6_v6only = 1;
262 } else {
263 udp_cfg.family = AF_INET;
266 udp_cfg.local_udp_port = port;
268 /* Create UDP socket */
269 err = udp_sock_create(net, &udp_cfg, &sock);
270 if (err < 0) {
271 pr_err("failed to create udp socket. err = %d\n", err);
272 return ERR_PTR(err);
275 tnl_cfg.encap_type = 1;
276 tnl_cfg.encap_rcv = rxe_udp_encap_recv;
278 /* Setup UDP tunnel */
279 setup_udp_tunnel_sock(net, sock, &tnl_cfg);
281 return sock;
284 void rxe_release_udp_tunnel(struct socket *sk)
286 if (sk)
287 udp_tunnel_sock_release(sk);
290 static void prepare_udp_hdr(struct sk_buff *skb, __be16 src_port,
291 __be16 dst_port)
293 struct udphdr *udph;
295 __skb_push(skb, sizeof(*udph));
296 skb_reset_transport_header(skb);
297 udph = udp_hdr(skb);
299 udph->dest = dst_port;
300 udph->source = src_port;
301 udph->len = htons(skb->len);
302 udph->check = 0;
305 static void prepare_ipv4_hdr(struct dst_entry *dst, struct sk_buff *skb,
306 __be32 saddr, __be32 daddr, __u8 proto,
307 __u8 tos, __u8 ttl, __be16 df, bool xnet)
309 struct iphdr *iph;
311 skb_scrub_packet(skb, xnet);
313 skb_clear_hash(skb);
314 skb_dst_set(skb, dst_clone(dst));
315 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
317 skb_push(skb, sizeof(struct iphdr));
318 skb_reset_network_header(skb);
320 iph = ip_hdr(skb);
322 iph->version = IPVERSION;
323 iph->ihl = sizeof(struct iphdr) >> 2;
324 iph->frag_off = df;
325 iph->protocol = proto;
326 iph->tos = tos;
327 iph->daddr = daddr;
328 iph->saddr = saddr;
329 iph->ttl = ttl;
330 __ip_select_ident(dev_net(dst->dev), iph,
331 skb_shinfo(skb)->gso_segs ?: 1);
332 iph->tot_len = htons(skb->len);
333 ip_send_check(iph);
336 static void prepare_ipv6_hdr(struct dst_entry *dst, struct sk_buff *skb,
337 struct in6_addr *saddr, struct in6_addr *daddr,
338 __u8 proto, __u8 prio, __u8 ttl)
340 struct ipv6hdr *ip6h;
342 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
343 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED
344 | IPSKB_REROUTED);
345 skb_dst_set(skb, dst_clone(dst));
347 __skb_push(skb, sizeof(*ip6h));
348 skb_reset_network_header(skb);
349 ip6h = ipv6_hdr(skb);
350 ip6_flow_hdr(ip6h, prio, htonl(0));
351 ip6h->payload_len = htons(skb->len);
352 ip6h->nexthdr = proto;
353 ip6h->hop_limit = ttl;
354 ip6h->daddr = *daddr;
355 ip6h->saddr = *saddr;
356 ip6h->payload_len = htons(skb->len - sizeof(*ip6h));
359 static int prepare4(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
360 struct sk_buff *skb, struct rxe_av *av)
362 struct rxe_qp *qp = pkt->qp;
363 struct dst_entry *dst;
364 bool xnet = false;
365 __be16 df = htons(IP_DF);
366 struct in_addr *saddr = &av->sgid_addr._sockaddr_in.sin_addr;
367 struct in_addr *daddr = &av->dgid_addr._sockaddr_in.sin_addr;
369 dst = rxe_find_route(rxe, qp, av);
370 if (!dst) {
371 pr_err("Host not reachable\n");
372 return -EHOSTUNREACH;
375 if (!memcmp(saddr, daddr, sizeof(*daddr)))
376 pkt->mask |= RXE_LOOPBACK_MASK;
378 prepare_udp_hdr(skb, htons(RXE_ROCE_V2_SPORT),
379 htons(ROCE_V2_UDP_DPORT));
381 prepare_ipv4_hdr(dst, skb, saddr->s_addr, daddr->s_addr, IPPROTO_UDP,
382 av->grh.traffic_class, av->grh.hop_limit, df, xnet);
384 if (qp_type(qp) == IB_QPT_RC)
385 sk_dst_set(qp->sk->sk, dst);
386 else
387 dst_release(dst);
389 return 0;
392 static int prepare6(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
393 struct sk_buff *skb, struct rxe_av *av)
395 struct rxe_qp *qp = pkt->qp;
396 struct dst_entry *dst;
397 struct in6_addr *saddr = &av->sgid_addr._sockaddr_in6.sin6_addr;
398 struct in6_addr *daddr = &av->dgid_addr._sockaddr_in6.sin6_addr;
400 dst = rxe_find_route(rxe, qp, av);
401 if (!dst) {
402 pr_err("Host not reachable\n");
403 return -EHOSTUNREACH;
406 if (!memcmp(saddr, daddr, sizeof(*daddr)))
407 pkt->mask |= RXE_LOOPBACK_MASK;
409 prepare_udp_hdr(skb, htons(RXE_ROCE_V2_SPORT),
410 htons(ROCE_V2_UDP_DPORT));
412 prepare_ipv6_hdr(dst, skb, saddr, daddr, IPPROTO_UDP,
413 av->grh.traffic_class,
414 av->grh.hop_limit);
416 if (qp_type(qp) == IB_QPT_RC)
417 sk_dst_set(qp->sk->sk, dst);
418 else
419 dst_release(dst);
421 return 0;
424 int rxe_prepare(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
425 struct sk_buff *skb, u32 *crc)
427 int err = 0;
428 struct rxe_av *av = rxe_get_av(pkt);
430 if (av->network_type == RDMA_NETWORK_IPV4)
431 err = prepare4(rxe, pkt, skb, av);
432 else if (av->network_type == RDMA_NETWORK_IPV6)
433 err = prepare6(rxe, pkt, skb, av);
435 *crc = rxe_icrc_hdr(pkt, skb);
437 return err;
440 static void rxe_skb_tx_dtor(struct sk_buff *skb)
442 struct sock *sk = skb->sk;
443 struct rxe_qp *qp = sk->sk_user_data;
444 int skb_out = atomic_dec_return(&qp->skb_out);
446 if (unlikely(qp->need_req_skb &&
447 skb_out < RXE_INFLIGHT_SKBS_PER_QP_LOW))
448 rxe_run_task(&qp->req.task, 1);
450 rxe_drop_ref(qp);
453 int rxe_send(struct rxe_dev *rxe, struct rxe_pkt_info *pkt, struct sk_buff *skb)
455 struct rxe_av *av;
456 int err;
458 av = rxe_get_av(pkt);
460 skb->destructor = rxe_skb_tx_dtor;
461 skb->sk = pkt->qp->sk->sk;
463 rxe_add_ref(pkt->qp);
464 atomic_inc(&pkt->qp->skb_out);
466 if (av->network_type == RDMA_NETWORK_IPV4) {
467 err = ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
468 } else if (av->network_type == RDMA_NETWORK_IPV6) {
469 err = ip6_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
470 } else {
471 pr_err("Unknown layer 3 protocol: %d\n", av->network_type);
472 atomic_dec(&pkt->qp->skb_out);
473 rxe_drop_ref(pkt->qp);
474 kfree_skb(skb);
475 return -EINVAL;
478 if (unlikely(net_xmit_eval(err))) {
479 pr_debug("error sending packet: %d\n", err);
480 return -EAGAIN;
483 return 0;
486 int rxe_loopback(struct sk_buff *skb)
488 return rxe_rcv(skb);
491 static inline int addr_same(struct rxe_dev *rxe, struct rxe_av *av)
493 return rxe->port.port_guid == av->grh.dgid.global.interface_id;
496 struct sk_buff *rxe_init_packet(struct rxe_dev *rxe, struct rxe_av *av,
497 int paylen, struct rxe_pkt_info *pkt)
499 unsigned int hdr_len;
500 struct sk_buff *skb;
502 if (av->network_type == RDMA_NETWORK_IPV4)
503 hdr_len = ETH_HLEN + sizeof(struct udphdr) +
504 sizeof(struct iphdr);
505 else
506 hdr_len = ETH_HLEN + sizeof(struct udphdr) +
507 sizeof(struct ipv6hdr);
509 skb = alloc_skb(paylen + hdr_len + LL_RESERVED_SPACE(rxe->ndev),
510 GFP_ATOMIC);
511 if (unlikely(!skb))
512 return NULL;
514 skb_reserve(skb, hdr_len + LL_RESERVED_SPACE(rxe->ndev));
516 skb->dev = rxe->ndev;
517 if (av->network_type == RDMA_NETWORK_IPV4)
518 skb->protocol = htons(ETH_P_IP);
519 else
520 skb->protocol = htons(ETH_P_IPV6);
522 pkt->rxe = rxe;
523 pkt->port_num = 1;
524 pkt->hdr = skb_put(skb, paylen);
525 pkt->mask |= RXE_GRH_MASK;
527 memset(pkt->hdr, 0, paylen);
529 return skb;
533 * this is required by rxe_cfg to match rxe devices in
534 * /sys/class/infiniband up with their underlying ethernet devices
536 const char *rxe_parent_name(struct rxe_dev *rxe, unsigned int port_num)
538 return rxe->ndev->name;
541 enum rdma_link_layer rxe_link_layer(struct rxe_dev *rxe, unsigned int port_num)
543 return IB_LINK_LAYER_ETHERNET;
546 struct rxe_dev *rxe_net_add(struct net_device *ndev)
548 int err;
549 struct rxe_dev *rxe = NULL;
551 rxe = (struct rxe_dev *)ib_alloc_device(sizeof(*rxe));
552 if (!rxe)
553 return NULL;
555 rxe->ndev = ndev;
557 err = rxe_add(rxe, ndev->mtu);
558 if (err) {
559 ib_dealloc_device(&rxe->ib_dev);
560 return NULL;
563 spin_lock_bh(&dev_list_lock);
564 list_add_tail(&rxe->list, &rxe_dev_list);
565 spin_unlock_bh(&dev_list_lock);
566 return rxe;
569 void rxe_remove_all(void)
571 spin_lock_bh(&dev_list_lock);
572 while (!list_empty(&rxe_dev_list)) {
573 struct rxe_dev *rxe =
574 list_first_entry(&rxe_dev_list, struct rxe_dev, list);
576 list_del(&rxe->list);
577 spin_unlock_bh(&dev_list_lock);
578 rxe_remove(rxe);
579 spin_lock_bh(&dev_list_lock);
581 spin_unlock_bh(&dev_list_lock);
583 EXPORT_SYMBOL(rxe_remove_all);
585 static void rxe_port_event(struct rxe_dev *rxe,
586 enum ib_event_type event)
588 struct ib_event ev;
590 ev.device = &rxe->ib_dev;
591 ev.element.port_num = 1;
592 ev.event = event;
594 ib_dispatch_event(&ev);
597 /* Caller must hold net_info_lock */
598 void rxe_port_up(struct rxe_dev *rxe)
600 struct rxe_port *port;
602 port = &rxe->port;
603 port->attr.state = IB_PORT_ACTIVE;
604 port->attr.phys_state = IB_PHYS_STATE_LINK_UP;
606 rxe_port_event(rxe, IB_EVENT_PORT_ACTIVE);
607 pr_info("set %s active\n", rxe->ib_dev.name);
610 /* Caller must hold net_info_lock */
611 void rxe_port_down(struct rxe_dev *rxe)
613 struct rxe_port *port;
615 port = &rxe->port;
616 port->attr.state = IB_PORT_DOWN;
617 port->attr.phys_state = IB_PHYS_STATE_LINK_DOWN;
619 rxe_port_event(rxe, IB_EVENT_PORT_ERR);
620 pr_info("set %s down\n", rxe->ib_dev.name);
623 static int rxe_notify(struct notifier_block *not_blk,
624 unsigned long event,
625 void *arg)
627 struct net_device *ndev = netdev_notifier_info_to_dev(arg);
628 struct rxe_dev *rxe = net_to_rxe(ndev);
630 if (!rxe)
631 goto out;
633 switch (event) {
634 case NETDEV_UNREGISTER:
635 list_del(&rxe->list);
636 rxe_remove(rxe);
637 break;
638 case NETDEV_UP:
639 rxe_port_up(rxe);
640 break;
641 case NETDEV_DOWN:
642 rxe_port_down(rxe);
643 break;
644 case NETDEV_CHANGEMTU:
645 pr_info("%s changed mtu to %d\n", ndev->name, ndev->mtu);
646 rxe_set_mtu(rxe, ndev->mtu);
647 break;
648 case NETDEV_CHANGE:
649 if (netif_running(ndev) && netif_carrier_ok(ndev))
650 rxe_port_up(rxe);
651 else
652 rxe_port_down(rxe);
653 break;
654 case NETDEV_REBOOT:
655 case NETDEV_GOING_DOWN:
656 case NETDEV_CHANGEADDR:
657 case NETDEV_CHANGENAME:
658 case NETDEV_FEAT_CHANGE:
659 default:
660 pr_info("ignoring netdev event = %ld for %s\n",
661 event, ndev->name);
662 break;
664 out:
665 return NOTIFY_OK;
668 struct notifier_block rxe_net_notifier = {
669 .notifier_call = rxe_notify,
672 static int rxe_net_ipv4_init(void)
674 recv_sockets.sk4 = rxe_setup_udp_tunnel(&init_net,
675 htons(ROCE_V2_UDP_DPORT), false);
676 if (IS_ERR(recv_sockets.sk4)) {
677 recv_sockets.sk4 = NULL;
678 pr_err("Failed to create IPv4 UDP tunnel\n");
679 return -1;
682 return 0;
685 static int rxe_net_ipv6_init(void)
687 #if IS_ENABLED(CONFIG_IPV6)
689 recv_sockets.sk6 = rxe_setup_udp_tunnel(&init_net,
690 htons(ROCE_V2_UDP_DPORT), true);
691 if (IS_ERR(recv_sockets.sk6)) {
692 recv_sockets.sk6 = NULL;
693 pr_err("Failed to create IPv6 UDP tunnel\n");
694 return -1;
696 #endif
697 return 0;
700 void rxe_net_exit(void)
702 rxe_release_udp_tunnel(recv_sockets.sk6);
703 rxe_release_udp_tunnel(recv_sockets.sk4);
704 unregister_netdevice_notifier(&rxe_net_notifier);
707 int rxe_net_init(void)
709 int err;
711 recv_sockets.sk6 = NULL;
713 err = rxe_net_ipv4_init();
714 if (err)
715 return err;
716 err = rxe_net_ipv6_init();
717 if (err)
718 goto err_out;
719 err = register_netdevice_notifier(&rxe_net_notifier);
720 if (err) {
721 pr_err("Failed to register netdev notifier\n");
722 goto err_out;
724 return 0;
725 err_out:
726 rxe_net_exit();
727 return err;