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 <net/udp_tunnel.h>
12 #include <net/sch_generic.h>
13 #include <linux/netfilter.h>
14 #include <rdma/ib_addr.h>
20 static struct rxe_recv_sockets recv_sockets
;
22 int rxe_mcast_add(struct rxe_dev
*rxe
, union ib_gid
*mgid
)
25 unsigned char ll_addr
[ETH_ALEN
];
27 ipv6_eth_mc_map((struct in6_addr
*)mgid
->raw
, ll_addr
);
28 err
= dev_mc_add(rxe
->ndev
, ll_addr
);
33 int rxe_mcast_delete(struct rxe_dev
*rxe
, union ib_gid
*mgid
)
36 unsigned char ll_addr
[ETH_ALEN
];
38 ipv6_eth_mc_map((struct in6_addr
*)mgid
->raw
, ll_addr
);
39 err
= dev_mc_del(rxe
->ndev
, ll_addr
);
44 static struct dst_entry
*rxe_find_route4(struct net_device
*ndev
,
45 struct in_addr
*saddr
,
46 struct in_addr
*daddr
)
49 struct flowi4 fl
= { { 0 } };
51 memset(&fl
, 0, sizeof(fl
));
52 fl
.flowi4_oif
= ndev
->ifindex
;
53 memcpy(&fl
.saddr
, saddr
, sizeof(*saddr
));
54 memcpy(&fl
.daddr
, daddr
, sizeof(*daddr
));
55 fl
.flowi4_proto
= IPPROTO_UDP
;
57 rt
= ip_route_output_key(&init_net
, &fl
);
59 pr_err_ratelimited("no route to %pI4\n", &daddr
->s_addr
);
66 #if IS_ENABLED(CONFIG_IPV6)
67 static struct dst_entry
*rxe_find_route6(struct net_device
*ndev
,
68 struct in6_addr
*saddr
,
69 struct in6_addr
*daddr
)
71 struct dst_entry
*ndst
;
72 struct flowi6 fl6
= { { 0 } };
74 memset(&fl6
, 0, sizeof(fl6
));
75 fl6
.flowi6_oif
= ndev
->ifindex
;
76 memcpy(&fl6
.saddr
, saddr
, sizeof(*saddr
));
77 memcpy(&fl6
.daddr
, daddr
, sizeof(*daddr
));
78 fl6
.flowi6_proto
= IPPROTO_UDP
;
80 ndst
= ipv6_stub
->ipv6_dst_lookup_flow(sock_net(recv_sockets
.sk6
->sk
),
81 recv_sockets
.sk6
->sk
, &fl6
,
84 pr_err_ratelimited("no route to %pI6\n", daddr
);
88 if (unlikely(ndst
->error
)) {
89 pr_err("no route to %pI6\n", daddr
);
101 static struct dst_entry
*rxe_find_route6(struct net_device
*ndev
,
102 struct in6_addr
*saddr
,
103 struct in6_addr
*daddr
)
110 static struct dst_entry
*rxe_find_route(struct net_device
*ndev
,
114 struct dst_entry
*dst
= NULL
;
116 if (qp_type(qp
) == IB_QPT_RC
)
117 dst
= sk_dst_get(qp
->sk
->sk
);
119 if (!dst
|| !dst_check(dst
, qp
->dst_cookie
)) {
123 if (av
->network_type
== RXE_NETWORK_TYPE_IPV4
) {
124 struct in_addr
*saddr
;
125 struct in_addr
*daddr
;
127 saddr
= &av
->sgid_addr
._sockaddr_in
.sin_addr
;
128 daddr
= &av
->dgid_addr
._sockaddr_in
.sin_addr
;
129 dst
= rxe_find_route4(ndev
, saddr
, daddr
);
130 } else if (av
->network_type
== RXE_NETWORK_TYPE_IPV6
) {
131 struct in6_addr
*saddr6
;
132 struct in6_addr
*daddr6
;
134 saddr6
= &av
->sgid_addr
._sockaddr_in6
.sin6_addr
;
135 daddr6
= &av
->dgid_addr
._sockaddr_in6
.sin6_addr
;
136 dst
= rxe_find_route6(ndev
, saddr6
, daddr6
);
137 #if IS_ENABLED(CONFIG_IPV6)
140 rt6_get_cookie((struct rt6_info
*)dst
);
144 if (dst
&& (qp_type(qp
) == IB_QPT_RC
)) {
146 sk_dst_set(qp
->sk
->sk
, dst
);
152 static int rxe_udp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
155 struct net_device
*ndev
= skb
->dev
;
156 struct rxe_dev
*rxe
= rxe_get_dev_from_net(ndev
);
157 struct rxe_pkt_info
*pkt
= SKB_TO_PKT(skb
);
162 if (skb_linearize(skb
)) {
163 pr_err("skb_linearize failed\n");
164 ib_device_put(&rxe
->ib_dev
);
171 pkt
->hdr
= (u8
*)(udph
+ 1);
172 pkt
->mask
= RXE_GRH_MASK
;
173 pkt
->paylen
= be16_to_cpu(udph
->len
) - sizeof(*udph
);
178 * FIXME: this is in the wrong place, it needs to be done when pkt is
181 ib_device_put(&rxe
->ib_dev
);
190 static struct socket
*rxe_setup_udp_tunnel(struct net
*net
, __be16 port
,
195 struct udp_port_cfg udp_cfg
= { };
196 struct udp_tunnel_sock_cfg tnl_cfg
= { };
199 udp_cfg
.family
= AF_INET6
;
200 udp_cfg
.ipv6_v6only
= 1;
202 udp_cfg
.family
= AF_INET
;
205 udp_cfg
.local_udp_port
= port
;
207 /* Create UDP socket */
208 err
= udp_sock_create(net
, &udp_cfg
, &sock
);
210 pr_err("failed to create udp socket. err = %d\n", err
);
214 tnl_cfg
.encap_type
= 1;
215 tnl_cfg
.encap_rcv
= rxe_udp_encap_recv
;
217 /* Setup UDP tunnel */
218 setup_udp_tunnel_sock(net
, sock
, &tnl_cfg
);
223 static void rxe_release_udp_tunnel(struct socket
*sk
)
226 udp_tunnel_sock_release(sk
);
229 static void prepare_udp_hdr(struct sk_buff
*skb
, __be16 src_port
,
234 __skb_push(skb
, sizeof(*udph
));
235 skb_reset_transport_header(skb
);
238 udph
->dest
= dst_port
;
239 udph
->source
= src_port
;
240 udph
->len
= htons(skb
->len
);
244 static void prepare_ipv4_hdr(struct dst_entry
*dst
, struct sk_buff
*skb
,
245 __be32 saddr
, __be32 daddr
, __u8 proto
,
246 __u8 tos
, __u8 ttl
, __be16 df
, bool xnet
)
250 skb_scrub_packet(skb
, xnet
);
253 skb_dst_set(skb
, dst_clone(dst
));
254 memset(IPCB(skb
), 0, sizeof(*IPCB(skb
)));
256 skb_push(skb
, sizeof(struct iphdr
));
257 skb_reset_network_header(skb
);
261 iph
->version
= IPVERSION
;
262 iph
->ihl
= sizeof(struct iphdr
) >> 2;
264 iph
->protocol
= proto
;
269 __ip_select_ident(dev_net(dst
->dev
), iph
,
270 skb_shinfo(skb
)->gso_segs
?: 1);
271 iph
->tot_len
= htons(skb
->len
);
275 static void prepare_ipv6_hdr(struct dst_entry
*dst
, struct sk_buff
*skb
,
276 struct in6_addr
*saddr
, struct in6_addr
*daddr
,
277 __u8 proto
, __u8 prio
, __u8 ttl
)
279 struct ipv6hdr
*ip6h
;
281 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
282 IPCB(skb
)->flags
&= ~(IPSKB_XFRM_TUNNEL_SIZE
| IPSKB_XFRM_TRANSFORMED
284 skb_dst_set(skb
, dst_clone(dst
));
286 __skb_push(skb
, sizeof(*ip6h
));
287 skb_reset_network_header(skb
);
288 ip6h
= ipv6_hdr(skb
);
289 ip6_flow_hdr(ip6h
, prio
, htonl(0));
290 ip6h
->payload_len
= htons(skb
->len
);
291 ip6h
->nexthdr
= proto
;
292 ip6h
->hop_limit
= ttl
;
293 ip6h
->daddr
= *daddr
;
294 ip6h
->saddr
= *saddr
;
295 ip6h
->payload_len
= htons(skb
->len
- sizeof(*ip6h
));
298 static int prepare4(struct rxe_pkt_info
*pkt
, struct sk_buff
*skb
)
300 struct rxe_qp
*qp
= pkt
->qp
;
301 struct dst_entry
*dst
;
303 __be16 df
= htons(IP_DF
);
304 struct rxe_av
*av
= rxe_get_av(pkt
);
305 struct in_addr
*saddr
= &av
->sgid_addr
._sockaddr_in
.sin_addr
;
306 struct in_addr
*daddr
= &av
->dgid_addr
._sockaddr_in
.sin_addr
;
308 dst
= rxe_find_route(skb
->dev
, qp
, av
);
310 pr_err("Host not reachable\n");
311 return -EHOSTUNREACH
;
314 prepare_udp_hdr(skb
, cpu_to_be16(qp
->src_port
),
315 cpu_to_be16(ROCE_V2_UDP_DPORT
));
317 prepare_ipv4_hdr(dst
, skb
, saddr
->s_addr
, daddr
->s_addr
, IPPROTO_UDP
,
318 av
->grh
.traffic_class
, av
->grh
.hop_limit
, df
, xnet
);
324 static int prepare6(struct rxe_pkt_info
*pkt
, struct sk_buff
*skb
)
326 struct rxe_qp
*qp
= pkt
->qp
;
327 struct dst_entry
*dst
;
328 struct rxe_av
*av
= rxe_get_av(pkt
);
329 struct in6_addr
*saddr
= &av
->sgid_addr
._sockaddr_in6
.sin6_addr
;
330 struct in6_addr
*daddr
= &av
->dgid_addr
._sockaddr_in6
.sin6_addr
;
332 dst
= rxe_find_route(skb
->dev
, qp
, av
);
334 pr_err("Host not reachable\n");
335 return -EHOSTUNREACH
;
338 prepare_udp_hdr(skb
, cpu_to_be16(qp
->src_port
),
339 cpu_to_be16(ROCE_V2_UDP_DPORT
));
341 prepare_ipv6_hdr(dst
, skb
, saddr
, daddr
, IPPROTO_UDP
,
342 av
->grh
.traffic_class
,
349 int rxe_prepare(struct rxe_pkt_info
*pkt
, struct sk_buff
*skb
, u32
*crc
)
353 if (skb
->protocol
== htons(ETH_P_IP
))
354 err
= prepare4(pkt
, skb
);
355 else if (skb
->protocol
== htons(ETH_P_IPV6
))
356 err
= prepare6(pkt
, skb
);
358 *crc
= rxe_icrc_hdr(pkt
, skb
);
360 if (ether_addr_equal(skb
->dev
->dev_addr
, rxe_get_av(pkt
)->dmac
))
361 pkt
->mask
|= RXE_LOOPBACK_MASK
;
366 static void rxe_skb_tx_dtor(struct sk_buff
*skb
)
368 struct sock
*sk
= skb
->sk
;
369 struct rxe_qp
*qp
= sk
->sk_user_data
;
370 int skb_out
= atomic_dec_return(&qp
->skb_out
);
372 if (unlikely(qp
->need_req_skb
&&
373 skb_out
< RXE_INFLIGHT_SKBS_PER_QP_LOW
))
374 rxe_run_task(&qp
->req
.task
, 1);
379 int rxe_send(struct rxe_pkt_info
*pkt
, struct sk_buff
*skb
)
383 skb
->destructor
= rxe_skb_tx_dtor
;
384 skb
->sk
= pkt
->qp
->sk
->sk
;
386 rxe_add_ref(pkt
->qp
);
387 atomic_inc(&pkt
->qp
->skb_out
);
389 if (skb
->protocol
== htons(ETH_P_IP
)) {
390 err
= ip_local_out(dev_net(skb_dst(skb
)->dev
), skb
->sk
, skb
);
391 } else if (skb
->protocol
== htons(ETH_P_IPV6
)) {
392 err
= ip6_local_out(dev_net(skb_dst(skb
)->dev
), skb
->sk
, skb
);
394 pr_err("Unknown layer 3 protocol: %d\n", skb
->protocol
);
395 atomic_dec(&pkt
->qp
->skb_out
);
396 rxe_drop_ref(pkt
->qp
);
401 if (unlikely(net_xmit_eval(err
))) {
402 pr_debug("error sending packet: %d\n", err
);
409 void rxe_loopback(struct sk_buff
*skb
)
414 struct sk_buff
*rxe_init_packet(struct rxe_dev
*rxe
, struct rxe_av
*av
,
415 int paylen
, struct rxe_pkt_info
*pkt
)
417 unsigned int hdr_len
;
418 struct sk_buff
*skb
= NULL
;
419 struct net_device
*ndev
;
420 const struct ib_gid_attr
*attr
;
421 const int port_num
= 1;
423 attr
= rdma_get_gid_attr(&rxe
->ib_dev
, port_num
, av
->grh
.sgid_index
);
427 if (av
->network_type
== RXE_NETWORK_TYPE_IPV4
)
428 hdr_len
= ETH_HLEN
+ sizeof(struct udphdr
) +
429 sizeof(struct iphdr
);
431 hdr_len
= ETH_HLEN
+ sizeof(struct udphdr
) +
432 sizeof(struct ipv6hdr
);
435 ndev
= rdma_read_gid_attr_ndev_rcu(attr
);
440 skb
= alloc_skb(paylen
+ hdr_len
+ LL_RESERVED_SPACE(ndev
),
443 if (unlikely(!skb
)) {
448 skb_reserve(skb
, hdr_len
+ LL_RESERVED_SPACE(ndev
));
450 /* FIXME: hold reference to this netdev until life of this skb. */
454 if (av
->network_type
== RXE_NETWORK_TYPE_IPV4
)
455 skb
->protocol
= htons(ETH_P_IP
);
457 skb
->protocol
= htons(ETH_P_IPV6
);
460 pkt
->port_num
= port_num
;
461 pkt
->hdr
= skb_put_zero(skb
, paylen
);
462 pkt
->mask
|= RXE_GRH_MASK
;
465 rdma_put_gid_attr(attr
);
470 * this is required by rxe_cfg to match rxe devices in
471 * /sys/class/infiniband up with their underlying ethernet devices
473 const char *rxe_parent_name(struct rxe_dev
*rxe
, unsigned int port_num
)
475 return rxe
->ndev
->name
;
478 int rxe_net_add(const char *ibdev_name
, struct net_device
*ndev
)
481 struct rxe_dev
*rxe
= NULL
;
483 rxe
= ib_alloc_device(rxe_dev
, ib_dev
);
489 err
= rxe_add(rxe
, ndev
->mtu
, ibdev_name
);
491 ib_dealloc_device(&rxe
->ib_dev
);
498 static void rxe_port_event(struct rxe_dev
*rxe
,
499 enum ib_event_type event
)
503 ev
.device
= &rxe
->ib_dev
;
504 ev
.element
.port_num
= 1;
507 ib_dispatch_event(&ev
);
510 /* Caller must hold net_info_lock */
511 void rxe_port_up(struct rxe_dev
*rxe
)
513 struct rxe_port
*port
;
516 port
->attr
.state
= IB_PORT_ACTIVE
;
518 rxe_port_event(rxe
, IB_EVENT_PORT_ACTIVE
);
519 dev_info(&rxe
->ib_dev
.dev
, "set active\n");
522 /* Caller must hold net_info_lock */
523 void rxe_port_down(struct rxe_dev
*rxe
)
525 struct rxe_port
*port
;
528 port
->attr
.state
= IB_PORT_DOWN
;
530 rxe_port_event(rxe
, IB_EVENT_PORT_ERR
);
531 rxe_counter_inc(rxe
, RXE_CNT_LINK_DOWNED
);
532 dev_info(&rxe
->ib_dev
.dev
, "set down\n");
535 void rxe_set_port_state(struct rxe_dev
*rxe
)
537 if (netif_running(rxe
->ndev
) && netif_carrier_ok(rxe
->ndev
))
543 static int rxe_notify(struct notifier_block
*not_blk
,
547 struct net_device
*ndev
= netdev_notifier_info_to_dev(arg
);
548 struct rxe_dev
*rxe
= rxe_get_dev_from_net(ndev
);
554 case NETDEV_UNREGISTER
:
555 ib_unregister_device_queued(&rxe
->ib_dev
);
563 case NETDEV_CHANGEMTU
:
564 pr_info("%s changed mtu to %d\n", ndev
->name
, ndev
->mtu
);
565 rxe_set_mtu(rxe
, ndev
->mtu
);
568 rxe_set_port_state(rxe
);
571 case NETDEV_GOING_DOWN
:
572 case NETDEV_CHANGEADDR
:
573 case NETDEV_CHANGENAME
:
574 case NETDEV_FEAT_CHANGE
:
576 pr_info("ignoring netdev event = %ld for %s\n",
581 ib_device_put(&rxe
->ib_dev
);
585 static struct notifier_block rxe_net_notifier
= {
586 .notifier_call
= rxe_notify
,
589 static int rxe_net_ipv4_init(void)
591 recv_sockets
.sk4
= rxe_setup_udp_tunnel(&init_net
,
592 htons(ROCE_V2_UDP_DPORT
), false);
593 if (IS_ERR(recv_sockets
.sk4
)) {
594 recv_sockets
.sk4
= NULL
;
595 pr_err("Failed to create IPv4 UDP tunnel\n");
602 static int rxe_net_ipv6_init(void)
604 #if IS_ENABLED(CONFIG_IPV6)
606 recv_sockets
.sk6
= rxe_setup_udp_tunnel(&init_net
,
607 htons(ROCE_V2_UDP_DPORT
), true);
608 if (IS_ERR(recv_sockets
.sk6
)) {
609 recv_sockets
.sk6
= NULL
;
610 pr_err("Failed to create IPv6 UDP tunnel\n");
617 void rxe_net_exit(void)
619 rxe_release_udp_tunnel(recv_sockets
.sk6
);
620 rxe_release_udp_tunnel(recv_sockets
.sk4
);
621 unregister_netdevice_notifier(&rxe_net_notifier
);
624 int rxe_net_init(void)
628 recv_sockets
.sk6
= NULL
;
630 err
= rxe_net_ipv4_init();
633 err
= rxe_net_ipv6_init();
636 err
= register_netdevice_notifier(&rxe_net_notifier
);
638 pr_err("Failed to register netdev notifier\n");