rxe_net.c 16 KB

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