rxe_net.c 17 KB

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