1 /*
2 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
4 *
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:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
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.
23 *
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.
32 */
33
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>
43
44 #include "rxe.h"
45 #include "rxe_net.h"
46 #include "rxe_loc.h"
47
48 static LIST_HEAD(rxe_dev_list);
49 static DEFINE_SPINLOCK(dev_list_lock); /* spinlock for device list */
50
net_to_rxe(struct net_device * ndev)51 struct rxe_dev *net_to_rxe(struct net_device *ndev)
52 {
53 struct rxe_dev *rxe;
54 struct rxe_dev *found = NULL;
55
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;
61 }
62 }
63 spin_unlock_bh(&dev_list_lock);
64
65 return found;
66 }
67
get_rxe_by_name(const char * name)68 struct rxe_dev *get_rxe_by_name(const char *name)
69 {
70 struct rxe_dev *rxe;
71 struct rxe_dev *found = NULL;
72
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;
78 }
79 }
80 spin_unlock_bh(&dev_list_lock);
81 return found;
82 }
83
84
85 static struct rxe_recv_sockets recv_sockets;
86
rxe_dma_device(struct rxe_dev * rxe)87 struct device *rxe_dma_device(struct rxe_dev *rxe)
88 {
89 struct net_device *ndev;
90
91 ndev = rxe->ndev;
92
93 if (is_vlan_dev(ndev))
94 ndev = vlan_dev_real_dev(ndev);
95
96 return ndev->dev.parent;
97 }
98
rxe_mcast_add(struct rxe_dev * rxe,union ib_gid * mgid)99 int rxe_mcast_add(struct rxe_dev *rxe, union ib_gid *mgid)
100 {
101 int err;
102 unsigned char ll_addr[ETH_ALEN];
103
104 ipv6_eth_mc_map((struct in6_addr *)mgid->raw, ll_addr);
105 err = dev_mc_add(rxe->ndev, ll_addr);
106
107 return err;
108 }
109
rxe_mcast_delete(struct rxe_dev * rxe,union ib_gid * mgid)110 int rxe_mcast_delete(struct rxe_dev *rxe, union ib_gid *mgid)
111 {
112 int err;
113 unsigned char ll_addr[ETH_ALEN];
114
115 ipv6_eth_mc_map((struct in6_addr *)mgid->raw, ll_addr);
116 err = dev_mc_del(rxe->ndev, ll_addr);
117
118 return err;
119 }
120
rxe_find_route4(struct net_device * ndev,struct in_addr * saddr,struct in_addr * daddr)121 static struct dst_entry *rxe_find_route4(struct net_device *ndev,
122 struct in_addr *saddr,
123 struct in_addr *daddr)
124 {
125 struct rtable *rt;
126 struct flowi4 fl = { { 0 } };
127
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;
133
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;
138 }
139
140 return &rt->dst;
141 }
142
143 #if IS_ENABLED(CONFIG_IPV6)
rxe_find_route6(struct net_device * ndev,struct in6_addr * saddr,struct in6_addr * daddr)144 static struct dst_entry *rxe_find_route6(struct net_device *ndev,
145 struct in6_addr *saddr,
146 struct in6_addr *daddr)
147 {
148 struct dst_entry *ndst;
149 struct flowi6 fl6 = { { 0 } };
150
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;
156
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;
161 }
162
163 if (unlikely(ndst->error)) {
164 pr_err("no route to %pI6\n", daddr);
165 goto put;
166 }
167
168 return ndst;
169 put:
170 dst_release(ndst);
171 return NULL;
172 }
173
174 #else
175
rxe_find_route6(struct net_device * ndev,struct in6_addr * saddr,struct in6_addr * daddr)176 static struct dst_entry *rxe_find_route6(struct net_device *ndev,
177 struct in6_addr *saddr,
178 struct in6_addr *daddr)
179 {
180 return NULL;
181 }
182
183 #endif
184
rxe_find_route(struct rxe_dev * rxe,struct rxe_qp * qp,struct rxe_av * av)185 static struct dst_entry *rxe_find_route(struct rxe_dev *rxe,
186 struct rxe_qp *qp,
187 struct rxe_av *av)
188 {
189 const struct ib_gid_attr *attr;
190 struct dst_entry *dst = NULL;
191 struct net_device *ndev;
192
193 attr = rdma_get_gid_attr(&rxe->ib_dev, qp->attr.port_num,
194 av->grh.sgid_index);
195 if (IS_ERR(attr))
196 return NULL;
197 ndev = attr->ndev;
198
199 if (qp_type(qp) == IB_QPT_RC)
200 dst = sk_dst_get(qp->sk->sk);
201
202 if (!dst || !dst_check(dst, qp->dst_cookie)) {
203 if (dst)
204 dst_release(dst);
205
206 if (av->network_type == RDMA_NETWORK_IPV4) {
207 struct in_addr *saddr;
208 struct in_addr *daddr;
209
210 saddr = &av->sgid_addr._sockaddr_in.sin_addr;
211 daddr = &av->dgid_addr._sockaddr_in.sin_addr;
212 dst = rxe_find_route4(ndev, saddr, daddr);
213 } else if (av->network_type == RDMA_NETWORK_IPV6) {
214 struct in6_addr *saddr6;
215 struct in6_addr *daddr6;
216
217 saddr6 = &av->sgid_addr._sockaddr_in6.sin6_addr;
218 daddr6 = &av->dgid_addr._sockaddr_in6.sin6_addr;
219 dst = rxe_find_route6(ndev, saddr6, daddr6);
220 #if IS_ENABLED(CONFIG_IPV6)
221 if (dst)
222 qp->dst_cookie =
223 rt6_get_cookie((struct rt6_info *)dst);
224 #endif
225 }
226
227 if (dst && (qp_type(qp) == IB_QPT_RC)) {
228 dst_hold(dst);
229 sk_dst_set(qp->sk->sk, dst);
230 }
231 }
232 rdma_put_gid_attr(attr);
233 return dst;
234 }
235
rxe_udp_encap_recv(struct sock * sk,struct sk_buff * skb)236 static int rxe_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
237 {
238 struct udphdr *udph;
239 struct net_device *ndev = skb->dev;
240 struct net_device *rdev = ndev;
241 struct rxe_dev *rxe = net_to_rxe(ndev);
242 struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
243
244 if (!rxe && is_vlan_dev(rdev)) {
245 rdev = vlan_dev_real_dev(ndev);
246 rxe = net_to_rxe(rdev);
247 }
248 if (!rxe)
249 goto drop;
250
251 if (skb_linearize(skb)) {
252 pr_err("skb_linearize failed\n");
253 goto drop;
254 }
255
256 udph = udp_hdr(skb);
257 pkt->rxe = rxe;
258 pkt->port_num = 1;
259 pkt->hdr = (u8 *)(udph + 1);
260 pkt->mask = RXE_GRH_MASK;
261 pkt->paylen = be16_to_cpu(udph->len) - sizeof(*udph);
262
263 rxe_rcv(skb);
264
265 return 0;
266 drop:
267 kfree_skb(skb);
268
269 return 0;
270 }
271
rxe_setup_udp_tunnel(struct net * net,__be16 port,bool ipv6)272 static struct socket *rxe_setup_udp_tunnel(struct net *net, __be16 port,
273 bool ipv6)
274 {
275 int err;
276 struct socket *sock;
277 struct udp_port_cfg udp_cfg = { };
278 struct udp_tunnel_sock_cfg tnl_cfg = { };
279
280 if (ipv6) {
281 udp_cfg.family = AF_INET6;
282 udp_cfg.ipv6_v6only = 1;
283 } else {
284 udp_cfg.family = AF_INET;
285 }
286
287 udp_cfg.local_udp_port = port;
288
289 /* Create UDP socket */
290 err = udp_sock_create(net, &udp_cfg, &sock);
291 if (err < 0) {
292 pr_err("failed to create udp socket. err = %d\n", err);
293 return ERR_PTR(err);
294 }
295
296 tnl_cfg.encap_type = 1;
297 tnl_cfg.encap_rcv = rxe_udp_encap_recv;
298
299 /* Setup UDP tunnel */
300 setup_udp_tunnel_sock(net, sock, &tnl_cfg);
301
302 return sock;
303 }
304
rxe_release_udp_tunnel(struct socket * sk)305 static void rxe_release_udp_tunnel(struct socket *sk)
306 {
307 if (sk)
308 udp_tunnel_sock_release(sk);
309 }
310
prepare_udp_hdr(struct sk_buff * skb,__be16 src_port,__be16 dst_port)311 static void prepare_udp_hdr(struct sk_buff *skb, __be16 src_port,
312 __be16 dst_port)
313 {
314 struct udphdr *udph;
315
316 __skb_push(skb, sizeof(*udph));
317 skb_reset_transport_header(skb);
318 udph = udp_hdr(skb);
319
320 udph->dest = dst_port;
321 udph->source = src_port;
322 udph->len = htons(skb->len);
323 udph->check = 0;
324 }
325
prepare_ipv4_hdr(struct dst_entry * dst,struct sk_buff * skb,__be32 saddr,__be32 daddr,__u8 proto,__u8 tos,__u8 ttl,__be16 df,bool xnet)326 static void prepare_ipv4_hdr(struct dst_entry *dst, struct sk_buff *skb,
327 __be32 saddr, __be32 daddr, __u8 proto,
328 __u8 tos, __u8 ttl, __be16 df, bool xnet)
329 {
330 struct iphdr *iph;
331
332 skb_scrub_packet(skb, xnet);
333
334 skb_clear_hash(skb);
335 skb_dst_set(skb, dst_clone(dst));
336 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
337
338 skb_push(skb, sizeof(struct iphdr));
339 skb_reset_network_header(skb);
340
341 iph = ip_hdr(skb);
342
343 iph->version = IPVERSION;
344 iph->ihl = sizeof(struct iphdr) >> 2;
345 iph->frag_off = df;
346 iph->protocol = proto;
347 iph->tos = tos;
348 iph->daddr = daddr;
349 iph->saddr = saddr;
350 iph->ttl = ttl;
351 __ip_select_ident(dev_net(dst->dev), iph,
352 skb_shinfo(skb)->gso_segs ?: 1);
353 iph->tot_len = htons(skb->len);
354 ip_send_check(iph);
355 }
356
prepare_ipv6_hdr(struct dst_entry * dst,struct sk_buff * skb,struct in6_addr * saddr,struct in6_addr * daddr,__u8 proto,__u8 prio,__u8 ttl)357 static void prepare_ipv6_hdr(struct dst_entry *dst, struct sk_buff *skb,
358 struct in6_addr *saddr, struct in6_addr *daddr,
359 __u8 proto, __u8 prio, __u8 ttl)
360 {
361 struct ipv6hdr *ip6h;
362
363 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
364 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED
365 | IPSKB_REROUTED);
366 skb_dst_set(skb, dst_clone(dst));
367
368 __skb_push(skb, sizeof(*ip6h));
369 skb_reset_network_header(skb);
370 ip6h = ipv6_hdr(skb);
371 ip6_flow_hdr(ip6h, prio, htonl(0));
372 ip6h->payload_len = htons(skb->len);
373 ip6h->nexthdr = proto;
374 ip6h->hop_limit = ttl;
375 ip6h->daddr = *daddr;
376 ip6h->saddr = *saddr;
377 ip6h->payload_len = htons(skb->len - sizeof(*ip6h));
378 }
379
prepare4(struct rxe_dev * rxe,struct rxe_pkt_info * pkt,struct sk_buff * skb,struct rxe_av * av)380 static int prepare4(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
381 struct sk_buff *skb, struct rxe_av *av)
382 {
383 struct rxe_qp *qp = pkt->qp;
384 struct dst_entry *dst;
385 bool xnet = false;
386 __be16 df = htons(IP_DF);
387 struct in_addr *saddr = &av->sgid_addr._sockaddr_in.sin_addr;
388 struct in_addr *daddr = &av->dgid_addr._sockaddr_in.sin_addr;
389
390 dst = rxe_find_route(rxe, qp, av);
391 if (!dst) {
392 pr_err("Host not reachable\n");
393 return -EHOSTUNREACH;
394 }
395
396 if (!memcmp(saddr, daddr, sizeof(*daddr)))
397 pkt->mask |= RXE_LOOPBACK_MASK;
398
399 prepare_udp_hdr(skb, htons(RXE_ROCE_V2_SPORT),
400 htons(ROCE_V2_UDP_DPORT));
401
402 prepare_ipv4_hdr(dst, skb, saddr->s_addr, daddr->s_addr, IPPROTO_UDP,
403 av->grh.traffic_class, av->grh.hop_limit, df, xnet);
404
405 dst_release(dst);
406 return 0;
407 }
408
prepare6(struct rxe_dev * rxe,struct rxe_pkt_info * pkt,struct sk_buff * skb,struct rxe_av * av)409 static int prepare6(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
410 struct sk_buff *skb, struct rxe_av *av)
411 {
412 struct rxe_qp *qp = pkt->qp;
413 struct dst_entry *dst;
414 struct in6_addr *saddr = &av->sgid_addr._sockaddr_in6.sin6_addr;
415 struct in6_addr *daddr = &av->dgid_addr._sockaddr_in6.sin6_addr;
416
417 dst = rxe_find_route(rxe, qp, av);
418 if (!dst) {
419 pr_err("Host not reachable\n");
420 return -EHOSTUNREACH;
421 }
422
423 if (!memcmp(saddr, daddr, sizeof(*daddr)))
424 pkt->mask |= RXE_LOOPBACK_MASK;
425
426 prepare_udp_hdr(skb, htons(RXE_ROCE_V2_SPORT),
427 htons(ROCE_V2_UDP_DPORT));
428
429 prepare_ipv6_hdr(dst, skb, saddr, daddr, IPPROTO_UDP,
430 av->grh.traffic_class,
431 av->grh.hop_limit);
432
433 dst_release(dst);
434 return 0;
435 }
436
rxe_prepare(struct rxe_dev * rxe,struct rxe_pkt_info * pkt,struct sk_buff * skb,u32 * crc)437 int rxe_prepare(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
438 struct sk_buff *skb, u32 *crc)
439 {
440 int err = 0;
441 struct rxe_av *av = rxe_get_av(pkt);
442
443 if (av->network_type == RDMA_NETWORK_IPV4)
444 err = prepare4(rxe, pkt, skb, av);
445 else if (av->network_type == RDMA_NETWORK_IPV6)
446 err = prepare6(rxe, pkt, skb, av);
447
448 *crc = rxe_icrc_hdr(pkt, skb);
449
450 return err;
451 }
452
rxe_skb_tx_dtor(struct sk_buff * skb)453 static void rxe_skb_tx_dtor(struct sk_buff *skb)
454 {
455 struct sock *sk = skb->sk;
456 struct rxe_qp *qp = sk->sk_user_data;
457 int skb_out = atomic_dec_return(&qp->skb_out);
458
459 if (unlikely(qp->need_req_skb &&
460 skb_out < RXE_INFLIGHT_SKBS_PER_QP_LOW))
461 rxe_run_task(&qp->req.task, 1);
462
463 rxe_drop_ref(qp);
464 }
465
rxe_send(struct rxe_pkt_info * pkt,struct sk_buff * skb)466 int rxe_send(struct rxe_pkt_info *pkt, struct sk_buff *skb)
467 {
468 struct rxe_av *av;
469 int err;
470
471 av = rxe_get_av(pkt);
472
473 skb->destructor = rxe_skb_tx_dtor;
474 skb->sk = pkt->qp->sk->sk;
475
476 rxe_add_ref(pkt->qp);
477 atomic_inc(&pkt->qp->skb_out);
478
479 if (av->network_type == RDMA_NETWORK_IPV4) {
480 err = ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
481 } else if (av->network_type == RDMA_NETWORK_IPV6) {
482 err = ip6_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
483 } else {
484 pr_err("Unknown layer 3 protocol: %d\n", av->network_type);
485 atomic_dec(&pkt->qp->skb_out);
486 rxe_drop_ref(pkt->qp);
487 kfree_skb(skb);
488 return -EINVAL;
489 }
490
491 if (unlikely(net_xmit_eval(err))) {
492 pr_debug("error sending packet: %d\n", err);
493 return -EAGAIN;
494 }
495
496 return 0;
497 }
498
rxe_loopback(struct sk_buff * skb)499 void rxe_loopback(struct sk_buff *skb)
500 {
501 rxe_rcv(skb);
502 }
503
addr_same(struct rxe_dev * rxe,struct rxe_av * av)504 static inline int addr_same(struct rxe_dev *rxe, struct rxe_av *av)
505 {
506 return rxe->port.port_guid == av->grh.dgid.global.interface_id;
507 }
508
rxe_init_packet(struct rxe_dev * rxe,struct rxe_av * av,int paylen,struct rxe_pkt_info * pkt)509 struct sk_buff *rxe_init_packet(struct rxe_dev *rxe, struct rxe_av *av,
510 int paylen, struct rxe_pkt_info *pkt)
511 {
512 unsigned int hdr_len;
513 struct sk_buff *skb;
514 struct net_device *ndev;
515 const struct ib_gid_attr *attr;
516 const int port_num = 1;
517
518 attr = rdma_get_gid_attr(&rxe->ib_dev, port_num, av->grh.sgid_index);
519 if (IS_ERR(attr))
520 return NULL;
521 ndev = attr->ndev;
522
523 if (av->network_type == RDMA_NETWORK_IPV4)
524 hdr_len = ETH_HLEN + sizeof(struct udphdr) +
525 sizeof(struct iphdr);
526 else
527 hdr_len = ETH_HLEN + sizeof(struct udphdr) +
528 sizeof(struct ipv6hdr);
529
530 skb = alloc_skb(paylen + hdr_len + LL_RESERVED_SPACE(ndev),
531 GFP_ATOMIC);
532
533 if (unlikely(!skb))
534 goto out;
535
536 skb_reserve(skb, hdr_len + LL_RESERVED_SPACE(rxe->ndev));
537
538 skb->dev = ndev;
539 if (av->network_type == RDMA_NETWORK_IPV4)
540 skb->protocol = htons(ETH_P_IP);
541 else
542 skb->protocol = htons(ETH_P_IPV6);
543
544 pkt->rxe = rxe;
545 pkt->port_num = port_num;
546 pkt->hdr = skb_put_zero(skb, paylen);
547 pkt->mask |= RXE_GRH_MASK;
548
549 out:
550 rdma_put_gid_attr(attr);
551 return skb;
552 }
553
554 /*
555 * this is required by rxe_cfg to match rxe devices in
556 * /sys/class/infiniband up with their underlying ethernet devices
557 */
rxe_parent_name(struct rxe_dev * rxe,unsigned int port_num)558 const char *rxe_parent_name(struct rxe_dev *rxe, unsigned int port_num)
559 {
560 return rxe->ndev->name;
561 }
562
rxe_link_layer(struct rxe_dev * rxe,unsigned int port_num)563 enum rdma_link_layer rxe_link_layer(struct rxe_dev *rxe, unsigned int port_num)
564 {
565 return IB_LINK_LAYER_ETHERNET;
566 }
567
rxe_net_add(struct net_device * ndev)568 struct rxe_dev *rxe_net_add(struct net_device *ndev)
569 {
570 int err;
571 struct rxe_dev *rxe = NULL;
572
573 rxe = (struct rxe_dev *)ib_alloc_device(sizeof(*rxe));
574 if (!rxe)
575 return NULL;
576
577 rxe->ndev = ndev;
578
579 err = rxe_add(rxe, ndev->mtu);
580 if (err) {
581 ib_dealloc_device(&rxe->ib_dev);
582 return NULL;
583 }
584
585 spin_lock_bh(&dev_list_lock);
586 list_add_tail(&rxe->list, &rxe_dev_list);
587 spin_unlock_bh(&dev_list_lock);
588 return rxe;
589 }
590
rxe_remove_all(void)591 void rxe_remove_all(void)
592 {
593 spin_lock_bh(&dev_list_lock);
594 while (!list_empty(&rxe_dev_list)) {
595 struct rxe_dev *rxe =
596 list_first_entry(&rxe_dev_list, struct rxe_dev, list);
597
598 list_del(&rxe->list);
599 spin_unlock_bh(&dev_list_lock);
600 rxe_remove(rxe);
601 spin_lock_bh(&dev_list_lock);
602 }
603 spin_unlock_bh(&dev_list_lock);
604 }
605
rxe_port_event(struct rxe_dev * rxe,enum ib_event_type event)606 static void rxe_port_event(struct rxe_dev *rxe,
607 enum ib_event_type event)
608 {
609 struct ib_event ev;
610
611 ev.device = &rxe->ib_dev;
612 ev.element.port_num = 1;
613 ev.event = event;
614
615 ib_dispatch_event(&ev);
616 }
617
618 /* Caller must hold net_info_lock */
rxe_port_up(struct rxe_dev * rxe)619 void rxe_port_up(struct rxe_dev *rxe)
620 {
621 struct rxe_port *port;
622
623 port = &rxe->port;
624 port->attr.state = IB_PORT_ACTIVE;
625 port->attr.phys_state = IB_PHYS_STATE_LINK_UP;
626
627 rxe_port_event(rxe, IB_EVENT_PORT_ACTIVE);
628 pr_info("set %s active\n", rxe->ib_dev.name);
629 }
630
631 /* Caller must hold net_info_lock */
rxe_port_down(struct rxe_dev * rxe)632 void rxe_port_down(struct rxe_dev *rxe)
633 {
634 struct rxe_port *port;
635
636 port = &rxe->port;
637 port->attr.state = IB_PORT_DOWN;
638 port->attr.phys_state = IB_PHYS_STATE_LINK_DOWN;
639
640 rxe_port_event(rxe, IB_EVENT_PORT_ERR);
641 pr_info("set %s down\n", rxe->ib_dev.name);
642 }
643
rxe_notify(struct notifier_block * not_blk,unsigned long event,void * arg)644 static int rxe_notify(struct notifier_block *not_blk,
645 unsigned long event,
646 void *arg)
647 {
648 struct net_device *ndev = netdev_notifier_info_to_dev(arg);
649 struct rxe_dev *rxe = net_to_rxe(ndev);
650
651 if (!rxe)
652 goto out;
653
654 switch (event) {
655 case NETDEV_UNREGISTER:
656 list_del(&rxe->list);
657 rxe_remove(rxe);
658 break;
659 case NETDEV_UP:
660 rxe_port_up(rxe);
661 break;
662 case NETDEV_DOWN:
663 rxe_port_down(rxe);
664 break;
665 case NETDEV_CHANGEMTU:
666 pr_info("%s changed mtu to %d\n", ndev->name, ndev->mtu);
667 rxe_set_mtu(rxe, ndev->mtu);
668 break;
669 case NETDEV_CHANGE:
670 if (netif_running(ndev) && netif_carrier_ok(ndev))
671 rxe_port_up(rxe);
672 else
673 rxe_port_down(rxe);
674 break;
675 case NETDEV_REBOOT:
676 case NETDEV_GOING_DOWN:
677 case NETDEV_CHANGEADDR:
678 case NETDEV_CHANGENAME:
679 case NETDEV_FEAT_CHANGE:
680 default:
681 pr_info("ignoring netdev event = %ld for %s\n",
682 event, ndev->name);
683 break;
684 }
685 out:
686 return NOTIFY_OK;
687 }
688
689 static struct notifier_block rxe_net_notifier = {
690 .notifier_call = rxe_notify,
691 };
692
rxe_net_ipv4_init(void)693 static int rxe_net_ipv4_init(void)
694 {
695 recv_sockets.sk4 = rxe_setup_udp_tunnel(&init_net,
696 htons(ROCE_V2_UDP_DPORT), false);
697 if (IS_ERR(recv_sockets.sk4)) {
698 recv_sockets.sk4 = NULL;
699 pr_err("Failed to create IPv4 UDP tunnel\n");
700 return -1;
701 }
702
703 return 0;
704 }
705
rxe_net_ipv6_init(void)706 static int rxe_net_ipv6_init(void)
707 {
708 #if IS_ENABLED(CONFIG_IPV6)
709
710 recv_sockets.sk6 = rxe_setup_udp_tunnel(&init_net,
711 htons(ROCE_V2_UDP_DPORT), true);
712 if (IS_ERR(recv_sockets.sk6)) {
713 recv_sockets.sk6 = NULL;
714 pr_err("Failed to create IPv6 UDP tunnel\n");
715 return -1;
716 }
717 #endif
718 return 0;
719 }
720
rxe_net_exit(void)721 void rxe_net_exit(void)
722 {
723 rxe_release_udp_tunnel(recv_sockets.sk6);
724 rxe_release_udp_tunnel(recv_sockets.sk4);
725 unregister_netdevice_notifier(&rxe_net_notifier);
726 }
727
rxe_net_init(void)728 int rxe_net_init(void)
729 {
730 int err;
731
732 recv_sockets.sk6 = NULL;
733
734 err = rxe_net_ipv4_init();
735 if (err)
736 return err;
737 err = rxe_net_ipv6_init();
738 if (err)
739 goto err_out;
740 err = register_netdevice_notifier(&rxe_net_notifier);
741 if (err) {
742 pr_err("Failed to register netdev notifier\n");
743 goto err_out;
744 }
745 return 0;
746 err_out:
747 rxe_net_exit();
748 return err;
749 }
750