af_smc.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  1. /*
  2. * Shared Memory Communications over RDMA (SMC-R) and RoCE
  3. *
  4. * AF_SMC protocol family socket handler keeping the AF_INET sock address type
  5. * applies to SOCK_STREAM sockets only
  6. * offers an alternative communication option for TCP-protocol sockets
  7. * applicable with RoCE-cards only
  8. *
  9. * Initial restrictions:
  10. * - non-blocking connect postponed
  11. * - IPv6 support postponed
  12. * - support for alternate links postponed
  13. * - partial support for non-blocking sockets only
  14. * - support for urgent data postponed
  15. *
  16. * Copyright IBM Corp. 2016
  17. *
  18. * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
  19. * based on prototype from Frank Blaschka
  20. */
  21. #define KMSG_COMPONENT "smc"
  22. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  23. #include <linux/module.h>
  24. #include <linux/socket.h>
  25. #include <linux/inetdevice.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/in.h>
  28. #include <linux/sched/signal.h>
  29. #include <net/sock.h>
  30. #include <net/tcp.h>
  31. #include <net/smc.h>
  32. #include "smc.h"
  33. #include "smc_clc.h"
  34. #include "smc_llc.h"
  35. #include "smc_cdc.h"
  36. #include "smc_core.h"
  37. #include "smc_ib.h"
  38. #include "smc_pnet.h"
  39. #include "smc_tx.h"
  40. #include "smc_rx.h"
  41. #include "smc_close.h"
  42. static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group
  43. * creation
  44. */
  45. struct smc_lgr_list smc_lgr_list = { /* established link groups */
  46. .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
  47. .list = LIST_HEAD_INIT(smc_lgr_list.list),
  48. };
  49. static void smc_tcp_listen_work(struct work_struct *);
  50. static void smc_set_keepalive(struct sock *sk, int val)
  51. {
  52. struct smc_sock *smc = smc_sk(sk);
  53. smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
  54. }
  55. static struct smc_hashinfo smc_v4_hashinfo = {
  56. .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
  57. };
  58. int smc_hash_sk(struct sock *sk)
  59. {
  60. struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
  61. struct hlist_head *head;
  62. head = &h->ht;
  63. write_lock_bh(&h->lock);
  64. sk_add_node(sk, head);
  65. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  66. write_unlock_bh(&h->lock);
  67. return 0;
  68. }
  69. EXPORT_SYMBOL_GPL(smc_hash_sk);
  70. void smc_unhash_sk(struct sock *sk)
  71. {
  72. struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
  73. write_lock_bh(&h->lock);
  74. if (sk_del_node_init(sk))
  75. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  76. write_unlock_bh(&h->lock);
  77. }
  78. EXPORT_SYMBOL_GPL(smc_unhash_sk);
  79. struct proto smc_proto = {
  80. .name = "SMC",
  81. .owner = THIS_MODULE,
  82. .keepalive = smc_set_keepalive,
  83. .hash = smc_hash_sk,
  84. .unhash = smc_unhash_sk,
  85. .obj_size = sizeof(struct smc_sock),
  86. .h.smc_hash = &smc_v4_hashinfo,
  87. .slab_flags = SLAB_TYPESAFE_BY_RCU,
  88. };
  89. EXPORT_SYMBOL_GPL(smc_proto);
  90. static int smc_release(struct socket *sock)
  91. {
  92. struct sock *sk = sock->sk;
  93. struct smc_sock *smc;
  94. int rc = 0;
  95. if (!sk)
  96. goto out;
  97. smc = smc_sk(sk);
  98. sock_hold(sk);
  99. if (sk->sk_state == SMC_LISTEN)
  100. /* smc_close_non_accepted() is called and acquires
  101. * sock lock for child sockets again
  102. */
  103. lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
  104. else
  105. lock_sock(sk);
  106. if (smc->use_fallback) {
  107. sk->sk_state = SMC_CLOSED;
  108. sk->sk_state_change(sk);
  109. } else {
  110. rc = smc_close_active(smc);
  111. sock_set_flag(sk, SOCK_DEAD);
  112. sk->sk_shutdown |= SHUTDOWN_MASK;
  113. }
  114. if (smc->clcsock) {
  115. sock_release(smc->clcsock);
  116. smc->clcsock = NULL;
  117. }
  118. /* detach socket */
  119. sock_orphan(sk);
  120. sock->sk = NULL;
  121. if (smc->use_fallback) {
  122. schedule_delayed_work(&smc->sock_put_work, TCP_TIMEWAIT_LEN);
  123. } else if (sk->sk_state == SMC_CLOSED) {
  124. smc_conn_free(&smc->conn);
  125. schedule_delayed_work(&smc->sock_put_work,
  126. SMC_CLOSE_SOCK_PUT_DELAY);
  127. }
  128. release_sock(sk);
  129. sock_put(sk);
  130. out:
  131. return rc;
  132. }
  133. static void smc_destruct(struct sock *sk)
  134. {
  135. if (sk->sk_state != SMC_CLOSED)
  136. return;
  137. if (!sock_flag(sk, SOCK_DEAD))
  138. return;
  139. sk_refcnt_debug_dec(sk);
  140. }
  141. static struct sock *smc_sock_alloc(struct net *net, struct socket *sock)
  142. {
  143. struct smc_sock *smc;
  144. struct sock *sk;
  145. sk = sk_alloc(net, PF_SMC, GFP_KERNEL, &smc_proto, 0);
  146. if (!sk)
  147. return NULL;
  148. sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
  149. sk->sk_state = SMC_INIT;
  150. sk->sk_destruct = smc_destruct;
  151. sk->sk_protocol = SMCPROTO_SMC;
  152. smc = smc_sk(sk);
  153. INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
  154. INIT_LIST_HEAD(&smc->accept_q);
  155. spin_lock_init(&smc->accept_q_lock);
  156. INIT_DELAYED_WORK(&smc->sock_put_work, smc_close_sock_put_work);
  157. sk->sk_prot->hash(sk);
  158. sk_refcnt_debug_inc(sk);
  159. return sk;
  160. }
  161. static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
  162. int addr_len)
  163. {
  164. struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
  165. struct sock *sk = sock->sk;
  166. struct smc_sock *smc;
  167. int rc;
  168. smc = smc_sk(sk);
  169. /* replicate tests from inet_bind(), to be safe wrt. future changes */
  170. rc = -EINVAL;
  171. if (addr_len < sizeof(struct sockaddr_in))
  172. goto out;
  173. rc = -EAFNOSUPPORT;
  174. /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
  175. if ((addr->sin_family != AF_INET) &&
  176. ((addr->sin_family != AF_UNSPEC) ||
  177. (addr->sin_addr.s_addr != htonl(INADDR_ANY))))
  178. goto out;
  179. lock_sock(sk);
  180. /* Check if socket is already active */
  181. rc = -EINVAL;
  182. if (sk->sk_state != SMC_INIT)
  183. goto out_rel;
  184. smc->clcsock->sk->sk_reuse = sk->sk_reuse;
  185. rc = kernel_bind(smc->clcsock, uaddr, addr_len);
  186. out_rel:
  187. release_sock(sk);
  188. out:
  189. return rc;
  190. }
  191. static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
  192. unsigned long mask)
  193. {
  194. /* options we don't get control via setsockopt for */
  195. nsk->sk_type = osk->sk_type;
  196. nsk->sk_sndbuf = osk->sk_sndbuf;
  197. nsk->sk_rcvbuf = osk->sk_rcvbuf;
  198. nsk->sk_sndtimeo = osk->sk_sndtimeo;
  199. nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
  200. nsk->sk_mark = osk->sk_mark;
  201. nsk->sk_priority = osk->sk_priority;
  202. nsk->sk_rcvlowat = osk->sk_rcvlowat;
  203. nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
  204. nsk->sk_err = osk->sk_err;
  205. nsk->sk_flags &= ~mask;
  206. nsk->sk_flags |= osk->sk_flags & mask;
  207. }
  208. #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
  209. (1UL << SOCK_KEEPOPEN) | \
  210. (1UL << SOCK_LINGER) | \
  211. (1UL << SOCK_BROADCAST) | \
  212. (1UL << SOCK_TIMESTAMP) | \
  213. (1UL << SOCK_DBG) | \
  214. (1UL << SOCK_RCVTSTAMP) | \
  215. (1UL << SOCK_RCVTSTAMPNS) | \
  216. (1UL << SOCK_LOCALROUTE) | \
  217. (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
  218. (1UL << SOCK_RXQ_OVFL) | \
  219. (1UL << SOCK_WIFI_STATUS) | \
  220. (1UL << SOCK_NOFCS) | \
  221. (1UL << SOCK_FILTER_LOCKED))
  222. /* copy only relevant settings and flags of SOL_SOCKET level from smc to
  223. * clc socket (since smc is not called for these options from net/core)
  224. */
  225. static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
  226. {
  227. smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
  228. }
  229. #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
  230. (1UL << SOCK_KEEPOPEN) | \
  231. (1UL << SOCK_LINGER) | \
  232. (1UL << SOCK_DBG))
  233. /* copy only settings and flags relevant for smc from clc to smc socket */
  234. static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
  235. {
  236. smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
  237. }
  238. /* determine subnet and mask of internal TCP socket */
  239. int smc_netinfo_by_tcpsk(struct socket *clcsock,
  240. __be32 *subnet, u8 *prefix_len)
  241. {
  242. struct dst_entry *dst = sk_dst_get(clcsock->sk);
  243. struct sockaddr_in addr;
  244. int rc = -ENOENT;
  245. int len;
  246. if (!dst) {
  247. rc = -ENOTCONN;
  248. goto out;
  249. }
  250. if (!dst->dev) {
  251. rc = -ENODEV;
  252. goto out_rel;
  253. }
  254. /* get address to which the internal TCP socket is bound */
  255. kernel_getsockname(clcsock, (struct sockaddr *)&addr, &len);
  256. /* analyze IPv4 specific data of net_device belonging to TCP socket */
  257. for_ifa(dst->dev->ip_ptr) {
  258. if (ifa->ifa_address != addr.sin_addr.s_addr)
  259. continue;
  260. *prefix_len = inet_mask_len(ifa->ifa_mask);
  261. *subnet = ifa->ifa_address & ifa->ifa_mask;
  262. rc = 0;
  263. break;
  264. } endfor_ifa(dst->dev->ip_ptr);
  265. out_rel:
  266. dst_release(dst);
  267. out:
  268. return rc;
  269. }
  270. static int smc_clnt_conf_first_link(struct smc_sock *smc, union ib_gid *gid)
  271. {
  272. struct smc_link_group *lgr = smc->conn.lgr;
  273. struct smc_link *link;
  274. int rest;
  275. int rc;
  276. link = &lgr->lnk[SMC_SINGLE_LINK];
  277. /* receive CONFIRM LINK request from server over RoCE fabric */
  278. rest = wait_for_completion_interruptible_timeout(
  279. &link->llc_confirm,
  280. SMC_LLC_WAIT_FIRST_TIME);
  281. if (rest <= 0) {
  282. struct smc_clc_msg_decline dclc;
  283. rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
  284. SMC_CLC_DECLINE);
  285. return rc;
  286. }
  287. rc = smc_ib_modify_qp_rts(link);
  288. if (rc)
  289. return SMC_CLC_DECL_INTERR;
  290. smc_wr_remember_qp_attr(link);
  291. rc = smc_wr_reg_send(link,
  292. smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]);
  293. if (rc)
  294. return SMC_CLC_DECL_INTERR;
  295. /* send CONFIRM LINK response over RoCE fabric */
  296. rc = smc_llc_send_confirm_link(link,
  297. link->smcibdev->mac[link->ibport - 1],
  298. gid, SMC_LLC_RESP);
  299. if (rc < 0)
  300. return SMC_CLC_DECL_TCL;
  301. return rc;
  302. }
  303. static void smc_conn_save_peer_info(struct smc_sock *smc,
  304. struct smc_clc_msg_accept_confirm *clc)
  305. {
  306. smc->conn.peer_conn_idx = clc->conn_idx;
  307. smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
  308. smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size);
  309. atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
  310. }
  311. static void smc_link_save_peer_info(struct smc_link *link,
  312. struct smc_clc_msg_accept_confirm *clc)
  313. {
  314. link->peer_qpn = ntoh24(clc->qpn);
  315. memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
  316. memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
  317. link->peer_psn = ntoh24(clc->psn);
  318. link->peer_mtu = clc->qp_mtu;
  319. }
  320. /* setup for RDMA connection of client */
  321. static int smc_connect_rdma(struct smc_sock *smc)
  322. {
  323. struct sockaddr_in *inaddr = (struct sockaddr_in *)smc->addr;
  324. struct smc_clc_msg_accept_confirm aclc;
  325. int local_contact = SMC_FIRST_CONTACT;
  326. struct smc_ib_device *smcibdev;
  327. struct smc_link *link;
  328. u8 srv_first_contact;
  329. int reason_code = 0;
  330. int rc = 0;
  331. u8 ibport;
  332. /* IPSec connections opt out of SMC-R optimizations */
  333. if (using_ipsec(smc)) {
  334. reason_code = SMC_CLC_DECL_IPSEC;
  335. goto decline_rdma;
  336. }
  337. /* PNET table look up: search active ib_device and port
  338. * within same PNETID that also contains the ethernet device
  339. * used for the internal TCP socket
  340. */
  341. smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
  342. if (!smcibdev) {
  343. reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
  344. goto decline_rdma;
  345. }
  346. /* do inband token exchange */
  347. reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
  348. if (reason_code < 0) {
  349. rc = reason_code;
  350. goto out_err;
  351. }
  352. if (reason_code > 0) /* configuration error */
  353. goto decline_rdma;
  354. /* receive SMC Accept CLC message */
  355. reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
  356. SMC_CLC_ACCEPT);
  357. if (reason_code < 0) {
  358. rc = reason_code;
  359. goto out_err;
  360. }
  361. if (reason_code > 0)
  362. goto decline_rdma;
  363. srv_first_contact = aclc.hdr.flag;
  364. mutex_lock(&smc_create_lgr_pending);
  365. local_contact = smc_conn_create(smc, inaddr->sin_addr.s_addr, smcibdev,
  366. ibport, &aclc.lcl, srv_first_contact);
  367. if (local_contact < 0) {
  368. rc = local_contact;
  369. if (rc == -ENOMEM)
  370. reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
  371. else if (rc == -ENOLINK)
  372. reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
  373. goto decline_rdma_unlock;
  374. }
  375. link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
  376. smc_conn_save_peer_info(smc, &aclc);
  377. /* create send buffer and rmb */
  378. rc = smc_buf_create(smc);
  379. if (rc) {
  380. reason_code = SMC_CLC_DECL_MEM;
  381. goto decline_rdma_unlock;
  382. }
  383. if (local_contact == SMC_FIRST_CONTACT)
  384. smc_link_save_peer_info(link, &aclc);
  385. rc = smc_rmb_rtoken_handling(&smc->conn, &aclc);
  386. if (rc) {
  387. reason_code = SMC_CLC_DECL_INTERR;
  388. goto decline_rdma_unlock;
  389. }
  390. smc_close_init(smc);
  391. smc_rx_init(smc);
  392. if (local_contact == SMC_FIRST_CONTACT) {
  393. rc = smc_ib_ready_link(link);
  394. if (rc) {
  395. reason_code = SMC_CLC_DECL_INTERR;
  396. goto decline_rdma_unlock;
  397. }
  398. } else {
  399. struct smc_buf_desc *buf_desc = smc->conn.rmb_desc;
  400. if (!buf_desc->reused) {
  401. /* register memory region for new rmb */
  402. rc = smc_wr_reg_send(link,
  403. buf_desc->mr_rx[SMC_SINGLE_LINK]);
  404. if (rc) {
  405. reason_code = SMC_CLC_DECL_INTERR;
  406. goto decline_rdma_unlock;
  407. }
  408. }
  409. }
  410. smc_rmb_sync_sg_for_device(&smc->conn);
  411. rc = smc_clc_send_confirm(smc);
  412. if (rc)
  413. goto out_err_unlock;
  414. if (local_contact == SMC_FIRST_CONTACT) {
  415. /* QP confirmation over RoCE fabric */
  416. reason_code = smc_clnt_conf_first_link(
  417. smc, &smcibdev->gid[ibport - 1]);
  418. if (reason_code < 0) {
  419. rc = reason_code;
  420. goto out_err_unlock;
  421. }
  422. if (reason_code > 0)
  423. goto decline_rdma_unlock;
  424. }
  425. mutex_unlock(&smc_create_lgr_pending);
  426. smc_tx_init(smc);
  427. out_connected:
  428. smc_copy_sock_settings_to_clc(smc);
  429. if (smc->sk.sk_state == SMC_INIT)
  430. smc->sk.sk_state = SMC_ACTIVE;
  431. return rc ? rc : local_contact;
  432. decline_rdma_unlock:
  433. mutex_unlock(&smc_create_lgr_pending);
  434. smc_conn_free(&smc->conn);
  435. decline_rdma:
  436. /* RDMA setup failed, switch back to TCP */
  437. smc->use_fallback = true;
  438. if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
  439. rc = smc_clc_send_decline(smc, reason_code, 0);
  440. if (rc < sizeof(struct smc_clc_msg_decline))
  441. goto out_err;
  442. }
  443. goto out_connected;
  444. out_err_unlock:
  445. mutex_unlock(&smc_create_lgr_pending);
  446. smc_conn_free(&smc->conn);
  447. out_err:
  448. return rc;
  449. }
  450. static int smc_connect(struct socket *sock, struct sockaddr *addr,
  451. int alen, int flags)
  452. {
  453. struct sock *sk = sock->sk;
  454. struct smc_sock *smc;
  455. int rc = -EINVAL;
  456. smc = smc_sk(sk);
  457. /* separate smc parameter checking to be safe */
  458. if (alen < sizeof(addr->sa_family))
  459. goto out_err;
  460. if (addr->sa_family != AF_INET)
  461. goto out_err;
  462. smc->addr = addr; /* needed for nonblocking connect */
  463. lock_sock(sk);
  464. switch (sk->sk_state) {
  465. default:
  466. goto out;
  467. case SMC_ACTIVE:
  468. rc = -EISCONN;
  469. goto out;
  470. case SMC_INIT:
  471. rc = 0;
  472. break;
  473. }
  474. smc_copy_sock_settings_to_clc(smc);
  475. rc = kernel_connect(smc->clcsock, addr, alen, flags);
  476. if (rc)
  477. goto out;
  478. /* setup RDMA connection */
  479. rc = smc_connect_rdma(smc);
  480. if (rc < 0)
  481. goto out;
  482. else
  483. rc = 0; /* success cases including fallback */
  484. out:
  485. release_sock(sk);
  486. out_err:
  487. return rc;
  488. }
  489. static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
  490. {
  491. struct sock *sk = &lsmc->sk;
  492. struct socket *new_clcsock;
  493. struct sock *new_sk;
  494. int rc;
  495. release_sock(&lsmc->sk);
  496. new_sk = smc_sock_alloc(sock_net(sk), NULL);
  497. if (!new_sk) {
  498. rc = -ENOMEM;
  499. lsmc->sk.sk_err = ENOMEM;
  500. *new_smc = NULL;
  501. lock_sock(&lsmc->sk);
  502. goto out;
  503. }
  504. *new_smc = smc_sk(new_sk);
  505. rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
  506. lock_sock(&lsmc->sk);
  507. if (rc < 0) {
  508. lsmc->sk.sk_err = -rc;
  509. new_sk->sk_state = SMC_CLOSED;
  510. sock_set_flag(new_sk, SOCK_DEAD);
  511. sk->sk_prot->unhash(new_sk);
  512. sock_put(new_sk);
  513. *new_smc = NULL;
  514. goto out;
  515. }
  516. if (lsmc->sk.sk_state == SMC_CLOSED) {
  517. if (new_clcsock)
  518. sock_release(new_clcsock);
  519. new_sk->sk_state = SMC_CLOSED;
  520. sock_set_flag(new_sk, SOCK_DEAD);
  521. sk->sk_prot->unhash(new_sk);
  522. sock_put(new_sk);
  523. *new_smc = NULL;
  524. goto out;
  525. }
  526. (*new_smc)->clcsock = new_clcsock;
  527. out:
  528. return rc;
  529. }
  530. /* add a just created sock to the accept queue of the listen sock as
  531. * candidate for a following socket accept call from user space
  532. */
  533. static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
  534. {
  535. struct smc_sock *par = smc_sk(parent);
  536. sock_hold(sk);
  537. spin_lock(&par->accept_q_lock);
  538. list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
  539. spin_unlock(&par->accept_q_lock);
  540. sk_acceptq_added(parent);
  541. }
  542. /* remove a socket from the accept queue of its parental listening socket */
  543. static void smc_accept_unlink(struct sock *sk)
  544. {
  545. struct smc_sock *par = smc_sk(sk)->listen_smc;
  546. spin_lock(&par->accept_q_lock);
  547. list_del_init(&smc_sk(sk)->accept_q);
  548. spin_unlock(&par->accept_q_lock);
  549. sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
  550. sock_put(sk);
  551. }
  552. /* remove a sock from the accept queue to bind it to a new socket created
  553. * for a socket accept call from user space
  554. */
  555. struct sock *smc_accept_dequeue(struct sock *parent,
  556. struct socket *new_sock)
  557. {
  558. struct smc_sock *isk, *n;
  559. struct sock *new_sk;
  560. list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
  561. new_sk = (struct sock *)isk;
  562. smc_accept_unlink(new_sk);
  563. if (new_sk->sk_state == SMC_CLOSED) {
  564. new_sk->sk_prot->unhash(new_sk);
  565. sock_put(new_sk);
  566. continue;
  567. }
  568. if (new_sock)
  569. sock_graft(new_sk, new_sock);
  570. return new_sk;
  571. }
  572. return NULL;
  573. }
  574. /* clean up for a created but never accepted sock */
  575. void smc_close_non_accepted(struct sock *sk)
  576. {
  577. struct smc_sock *smc = smc_sk(sk);
  578. sock_hold(sk);
  579. lock_sock(sk);
  580. if (!sk->sk_lingertime)
  581. /* wait for peer closing */
  582. sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
  583. if (smc->use_fallback) {
  584. sk->sk_state = SMC_CLOSED;
  585. } else {
  586. smc_close_active(smc);
  587. sock_set_flag(sk, SOCK_DEAD);
  588. sk->sk_shutdown |= SHUTDOWN_MASK;
  589. }
  590. if (smc->clcsock) {
  591. struct socket *tcp;
  592. tcp = smc->clcsock;
  593. smc->clcsock = NULL;
  594. sock_release(tcp);
  595. }
  596. if (smc->use_fallback) {
  597. schedule_delayed_work(&smc->sock_put_work, TCP_TIMEWAIT_LEN);
  598. } else if (sk->sk_state == SMC_CLOSED) {
  599. smc_conn_free(&smc->conn);
  600. schedule_delayed_work(&smc->sock_put_work,
  601. SMC_CLOSE_SOCK_PUT_DELAY);
  602. }
  603. release_sock(sk);
  604. sock_put(sk);
  605. }
  606. static int smc_serv_conf_first_link(struct smc_sock *smc)
  607. {
  608. struct smc_link_group *lgr = smc->conn.lgr;
  609. struct smc_link *link;
  610. int rest;
  611. int rc;
  612. link = &lgr->lnk[SMC_SINGLE_LINK];
  613. rc = smc_wr_reg_send(link,
  614. smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]);
  615. if (rc)
  616. return SMC_CLC_DECL_INTERR;
  617. /* send CONFIRM LINK request to client over the RoCE fabric */
  618. rc = smc_llc_send_confirm_link(link,
  619. link->smcibdev->mac[link->ibport - 1],
  620. &link->smcibdev->gid[link->ibport - 1],
  621. SMC_LLC_REQ);
  622. if (rc < 0)
  623. return SMC_CLC_DECL_TCL;
  624. /* receive CONFIRM LINK response from client over the RoCE fabric */
  625. rest = wait_for_completion_interruptible_timeout(
  626. &link->llc_confirm_resp,
  627. SMC_LLC_WAIT_FIRST_TIME);
  628. if (rest <= 0) {
  629. struct smc_clc_msg_decline dclc;
  630. rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
  631. SMC_CLC_DECLINE);
  632. }
  633. return rc;
  634. }
  635. /* setup for RDMA connection of server */
  636. static void smc_listen_work(struct work_struct *work)
  637. {
  638. struct smc_sock *new_smc = container_of(work, struct smc_sock,
  639. smc_listen_work);
  640. struct socket *newclcsock = new_smc->clcsock;
  641. struct smc_sock *lsmc = new_smc->listen_smc;
  642. struct smc_clc_msg_accept_confirm cclc;
  643. int local_contact = SMC_REUSE_CONTACT;
  644. struct sock *newsmcsk = &new_smc->sk;
  645. struct smc_clc_msg_proposal pclc;
  646. struct smc_ib_device *smcibdev;
  647. struct sockaddr_in peeraddr;
  648. struct smc_link *link;
  649. int reason_code = 0;
  650. int rc = 0, len;
  651. __be32 subnet;
  652. u8 prefix_len;
  653. u8 ibport;
  654. /* do inband token exchange -
  655. *wait for and receive SMC Proposal CLC message
  656. */
  657. reason_code = smc_clc_wait_msg(new_smc, &pclc, sizeof(pclc),
  658. SMC_CLC_PROPOSAL);
  659. if (reason_code < 0)
  660. goto out_err;
  661. if (reason_code > 0)
  662. goto decline_rdma;
  663. /* IPSec connections opt out of SMC-R optimizations */
  664. if (using_ipsec(new_smc)) {
  665. reason_code = SMC_CLC_DECL_IPSEC;
  666. goto decline_rdma;
  667. }
  668. /* PNET table look up: search active ib_device and port
  669. * within same PNETID that also contains the ethernet device
  670. * used for the internal TCP socket
  671. */
  672. smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
  673. if (!smcibdev) {
  674. reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
  675. goto decline_rdma;
  676. }
  677. /* determine subnet and mask from internal TCP socket */
  678. rc = smc_netinfo_by_tcpsk(newclcsock, &subnet, &prefix_len);
  679. if (rc) {
  680. reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
  681. goto decline_rdma;
  682. }
  683. if ((pclc.outgoing_subnet != subnet) ||
  684. (pclc.prefix_len != prefix_len)) {
  685. reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
  686. goto decline_rdma;
  687. }
  688. /* get address of the peer connected to the internal TCP socket */
  689. kernel_getpeername(newclcsock, (struct sockaddr *)&peeraddr, &len);
  690. /* allocate connection / link group */
  691. mutex_lock(&smc_create_lgr_pending);
  692. local_contact = smc_conn_create(new_smc, peeraddr.sin_addr.s_addr,
  693. smcibdev, ibport, &pclc.lcl, 0);
  694. if (local_contact < 0) {
  695. rc = local_contact;
  696. if (rc == -ENOMEM)
  697. reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
  698. else if (rc == -ENOLINK)
  699. reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
  700. goto decline_rdma;
  701. }
  702. link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
  703. /* create send buffer and rmb */
  704. rc = smc_buf_create(new_smc);
  705. if (rc) {
  706. reason_code = SMC_CLC_DECL_MEM;
  707. goto decline_rdma;
  708. }
  709. smc_close_init(new_smc);
  710. smc_rx_init(new_smc);
  711. if (local_contact != SMC_FIRST_CONTACT) {
  712. struct smc_buf_desc *buf_desc = new_smc->conn.rmb_desc;
  713. if (!buf_desc->reused) {
  714. /* register memory region for new rmb */
  715. rc = smc_wr_reg_send(link,
  716. buf_desc->mr_rx[SMC_SINGLE_LINK]);
  717. if (rc) {
  718. reason_code = SMC_CLC_DECL_INTERR;
  719. goto decline_rdma;
  720. }
  721. }
  722. }
  723. smc_rmb_sync_sg_for_device(&new_smc->conn);
  724. rc = smc_clc_send_accept(new_smc, local_contact);
  725. if (rc)
  726. goto out_err;
  727. /* receive SMC Confirm CLC message */
  728. reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
  729. SMC_CLC_CONFIRM);
  730. if (reason_code < 0)
  731. goto out_err;
  732. if (reason_code > 0)
  733. goto decline_rdma;
  734. smc_conn_save_peer_info(new_smc, &cclc);
  735. if (local_contact == SMC_FIRST_CONTACT)
  736. smc_link_save_peer_info(link, &cclc);
  737. rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc);
  738. if (rc) {
  739. reason_code = SMC_CLC_DECL_INTERR;
  740. goto decline_rdma;
  741. }
  742. if (local_contact == SMC_FIRST_CONTACT) {
  743. rc = smc_ib_ready_link(link);
  744. if (rc) {
  745. reason_code = SMC_CLC_DECL_INTERR;
  746. goto decline_rdma;
  747. }
  748. /* QP confirmation over RoCE fabric */
  749. reason_code = smc_serv_conf_first_link(new_smc);
  750. if (reason_code < 0) {
  751. /* peer is not aware of a problem */
  752. rc = reason_code;
  753. goto out_err;
  754. }
  755. if (reason_code > 0)
  756. goto decline_rdma;
  757. }
  758. smc_tx_init(new_smc);
  759. out_connected:
  760. sk_refcnt_debug_inc(newsmcsk);
  761. if (newsmcsk->sk_state == SMC_INIT)
  762. newsmcsk->sk_state = SMC_ACTIVE;
  763. enqueue:
  764. mutex_unlock(&smc_create_lgr_pending);
  765. lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
  766. if (lsmc->sk.sk_state == SMC_LISTEN) {
  767. smc_accept_enqueue(&lsmc->sk, newsmcsk);
  768. } else { /* no longer listening */
  769. smc_close_non_accepted(newsmcsk);
  770. }
  771. release_sock(&lsmc->sk);
  772. /* Wake up accept */
  773. lsmc->sk.sk_data_ready(&lsmc->sk);
  774. sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
  775. return;
  776. decline_rdma:
  777. /* RDMA setup failed, switch back to TCP */
  778. smc_conn_free(&new_smc->conn);
  779. new_smc->use_fallback = true;
  780. if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
  781. rc = smc_clc_send_decline(new_smc, reason_code, 0);
  782. if (rc < sizeof(struct smc_clc_msg_decline))
  783. goto out_err;
  784. }
  785. goto out_connected;
  786. out_err:
  787. newsmcsk->sk_state = SMC_CLOSED;
  788. smc_conn_free(&new_smc->conn);
  789. goto enqueue; /* queue new sock with sk_err set */
  790. }
  791. static void smc_tcp_listen_work(struct work_struct *work)
  792. {
  793. struct smc_sock *lsmc = container_of(work, struct smc_sock,
  794. tcp_listen_work);
  795. struct smc_sock *new_smc;
  796. int rc = 0;
  797. lock_sock(&lsmc->sk);
  798. while (lsmc->sk.sk_state == SMC_LISTEN) {
  799. rc = smc_clcsock_accept(lsmc, &new_smc);
  800. if (rc)
  801. goto out;
  802. if (!new_smc)
  803. continue;
  804. new_smc->listen_smc = lsmc;
  805. new_smc->use_fallback = false; /* assume rdma capability first*/
  806. sock_hold(&lsmc->sk); /* sock_put in smc_listen_work */
  807. INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
  808. smc_copy_sock_settings_to_smc(new_smc);
  809. schedule_work(&new_smc->smc_listen_work);
  810. }
  811. out:
  812. release_sock(&lsmc->sk);
  813. lsmc->sk.sk_data_ready(&lsmc->sk); /* no more listening, wake accept */
  814. }
  815. static int smc_listen(struct socket *sock, int backlog)
  816. {
  817. struct sock *sk = sock->sk;
  818. struct smc_sock *smc;
  819. int rc;
  820. smc = smc_sk(sk);
  821. lock_sock(sk);
  822. rc = -EINVAL;
  823. if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
  824. goto out;
  825. rc = 0;
  826. if (sk->sk_state == SMC_LISTEN) {
  827. sk->sk_max_ack_backlog = backlog;
  828. goto out;
  829. }
  830. /* some socket options are handled in core, so we could not apply
  831. * them to the clc socket -- copy smc socket options to clc socket
  832. */
  833. smc_copy_sock_settings_to_clc(smc);
  834. rc = kernel_listen(smc->clcsock, backlog);
  835. if (rc)
  836. goto out;
  837. sk->sk_max_ack_backlog = backlog;
  838. sk->sk_ack_backlog = 0;
  839. sk->sk_state = SMC_LISTEN;
  840. INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
  841. schedule_work(&smc->tcp_listen_work);
  842. out:
  843. release_sock(sk);
  844. return rc;
  845. }
  846. static int smc_accept(struct socket *sock, struct socket *new_sock,
  847. int flags, bool kern)
  848. {
  849. struct sock *sk = sock->sk, *nsk;
  850. DECLARE_WAITQUEUE(wait, current);
  851. struct smc_sock *lsmc;
  852. long timeo;
  853. int rc = 0;
  854. lsmc = smc_sk(sk);
  855. lock_sock(sk);
  856. if (lsmc->sk.sk_state != SMC_LISTEN) {
  857. rc = -EINVAL;
  858. goto out;
  859. }
  860. /* Wait for an incoming connection */
  861. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  862. add_wait_queue_exclusive(sk_sleep(sk), &wait);
  863. while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
  864. set_current_state(TASK_INTERRUPTIBLE);
  865. if (!timeo) {
  866. rc = -EAGAIN;
  867. break;
  868. }
  869. release_sock(sk);
  870. timeo = schedule_timeout(timeo);
  871. /* wakeup by sk_data_ready in smc_listen_work() */
  872. sched_annotate_sleep();
  873. lock_sock(sk);
  874. if (signal_pending(current)) {
  875. rc = sock_intr_errno(timeo);
  876. break;
  877. }
  878. }
  879. set_current_state(TASK_RUNNING);
  880. remove_wait_queue(sk_sleep(sk), &wait);
  881. if (!rc)
  882. rc = sock_error(nsk);
  883. out:
  884. release_sock(sk);
  885. return rc;
  886. }
  887. static int smc_getname(struct socket *sock, struct sockaddr *addr,
  888. int *len, int peer)
  889. {
  890. struct smc_sock *smc;
  891. if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
  892. (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
  893. return -ENOTCONN;
  894. smc = smc_sk(sock->sk);
  895. return smc->clcsock->ops->getname(smc->clcsock, addr, len, peer);
  896. }
  897. static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  898. {
  899. struct sock *sk = sock->sk;
  900. struct smc_sock *smc;
  901. int rc = -EPIPE;
  902. smc = smc_sk(sk);
  903. lock_sock(sk);
  904. if ((sk->sk_state != SMC_ACTIVE) &&
  905. (sk->sk_state != SMC_APPCLOSEWAIT1) &&
  906. (sk->sk_state != SMC_INIT))
  907. goto out;
  908. if (smc->use_fallback)
  909. rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
  910. else
  911. rc = smc_tx_sendmsg(smc, msg, len);
  912. out:
  913. release_sock(sk);
  914. return rc;
  915. }
  916. static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  917. int flags)
  918. {
  919. struct sock *sk = sock->sk;
  920. struct smc_sock *smc;
  921. int rc = -ENOTCONN;
  922. smc = smc_sk(sk);
  923. lock_sock(sk);
  924. if ((sk->sk_state == SMC_INIT) ||
  925. (sk->sk_state == SMC_LISTEN) ||
  926. (sk->sk_state == SMC_CLOSED))
  927. goto out;
  928. if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
  929. rc = 0;
  930. goto out;
  931. }
  932. if (smc->use_fallback)
  933. rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
  934. else
  935. rc = smc_rx_recvmsg(smc, msg, len, flags);
  936. out:
  937. release_sock(sk);
  938. return rc;
  939. }
  940. static unsigned int smc_accept_poll(struct sock *parent)
  941. {
  942. struct smc_sock *isk;
  943. struct sock *sk;
  944. lock_sock(parent);
  945. list_for_each_entry(isk, &smc_sk(parent)->accept_q, accept_q) {
  946. sk = (struct sock *)isk;
  947. if (sk->sk_state == SMC_ACTIVE) {
  948. release_sock(parent);
  949. return POLLIN | POLLRDNORM;
  950. }
  951. }
  952. release_sock(parent);
  953. return 0;
  954. }
  955. static unsigned int smc_poll(struct file *file, struct socket *sock,
  956. poll_table *wait)
  957. {
  958. struct sock *sk = sock->sk;
  959. unsigned int mask = 0;
  960. struct smc_sock *smc;
  961. int rc;
  962. smc = smc_sk(sock->sk);
  963. if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
  964. /* delegate to CLC child sock */
  965. mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
  966. /* if non-blocking connect finished ... */
  967. lock_sock(sk);
  968. if ((sk->sk_state == SMC_INIT) && (mask & POLLOUT)) {
  969. sk->sk_err = smc->clcsock->sk->sk_err;
  970. if (sk->sk_err) {
  971. mask |= POLLERR;
  972. } else {
  973. rc = smc_connect_rdma(smc);
  974. if (rc < 0)
  975. mask |= POLLERR;
  976. else
  977. /* success cases including fallback */
  978. mask |= POLLOUT | POLLWRNORM;
  979. }
  980. }
  981. release_sock(sk);
  982. } else {
  983. sock_poll_wait(file, sk_sleep(sk), wait);
  984. if (sk->sk_state == SMC_LISTEN)
  985. /* woken up by sk_data_ready in smc_listen_work() */
  986. mask |= smc_accept_poll(sk);
  987. if (sk->sk_err)
  988. mask |= POLLERR;
  989. if (atomic_read(&smc->conn.sndbuf_space) ||
  990. (sk->sk_shutdown & SEND_SHUTDOWN)) {
  991. mask |= POLLOUT | POLLWRNORM;
  992. } else {
  993. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  994. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  995. }
  996. if (atomic_read(&smc->conn.bytes_to_rcv))
  997. mask |= POLLIN | POLLRDNORM;
  998. if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
  999. (sk->sk_state == SMC_CLOSED))
  1000. mask |= POLLHUP;
  1001. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1002. mask |= POLLIN | POLLRDNORM | POLLRDHUP;
  1003. if (sk->sk_state == SMC_APPCLOSEWAIT1)
  1004. mask |= POLLIN;
  1005. }
  1006. return mask;
  1007. }
  1008. static int smc_shutdown(struct socket *sock, int how)
  1009. {
  1010. struct sock *sk = sock->sk;
  1011. struct smc_sock *smc;
  1012. int rc = -EINVAL;
  1013. int rc1 = 0;
  1014. smc = smc_sk(sk);
  1015. if ((how < SHUT_RD) || (how > SHUT_RDWR))
  1016. return rc;
  1017. lock_sock(sk);
  1018. rc = -ENOTCONN;
  1019. if ((sk->sk_state != SMC_LISTEN) &&
  1020. (sk->sk_state != SMC_ACTIVE) &&
  1021. (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
  1022. (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
  1023. (sk->sk_state != SMC_APPCLOSEWAIT1) &&
  1024. (sk->sk_state != SMC_APPCLOSEWAIT2) &&
  1025. (sk->sk_state != SMC_APPFINCLOSEWAIT))
  1026. goto out;
  1027. if (smc->use_fallback) {
  1028. rc = kernel_sock_shutdown(smc->clcsock, how);
  1029. sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
  1030. if (sk->sk_shutdown == SHUTDOWN_MASK)
  1031. sk->sk_state = SMC_CLOSED;
  1032. goto out;
  1033. }
  1034. switch (how) {
  1035. case SHUT_RDWR: /* shutdown in both directions */
  1036. rc = smc_close_active(smc);
  1037. break;
  1038. case SHUT_WR:
  1039. rc = smc_close_shutdown_write(smc);
  1040. break;
  1041. case SHUT_RD:
  1042. if (sk->sk_state == SMC_LISTEN)
  1043. rc = smc_close_active(smc);
  1044. else
  1045. rc = 0;
  1046. /* nothing more to do because peer is not involved */
  1047. break;
  1048. }
  1049. rc1 = kernel_sock_shutdown(smc->clcsock, how);
  1050. /* map sock_shutdown_cmd constants to sk_shutdown value range */
  1051. sk->sk_shutdown |= how + 1;
  1052. out:
  1053. release_sock(sk);
  1054. return rc ? rc : rc1;
  1055. }
  1056. static int smc_setsockopt(struct socket *sock, int level, int optname,
  1057. char __user *optval, unsigned int optlen)
  1058. {
  1059. struct sock *sk = sock->sk;
  1060. struct smc_sock *smc;
  1061. smc = smc_sk(sk);
  1062. /* generic setsockopts reaching us here always apply to the
  1063. * CLC socket
  1064. */
  1065. return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
  1066. optval, optlen);
  1067. }
  1068. static int smc_getsockopt(struct socket *sock, int level, int optname,
  1069. char __user *optval, int __user *optlen)
  1070. {
  1071. struct smc_sock *smc;
  1072. smc = smc_sk(sock->sk);
  1073. /* socket options apply to the CLC socket */
  1074. return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
  1075. optval, optlen);
  1076. }
  1077. static int smc_ioctl(struct socket *sock, unsigned int cmd,
  1078. unsigned long arg)
  1079. {
  1080. struct smc_sock *smc;
  1081. smc = smc_sk(sock->sk);
  1082. if (smc->use_fallback)
  1083. return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
  1084. else
  1085. return sock_no_ioctl(sock, cmd, arg);
  1086. }
  1087. static ssize_t smc_sendpage(struct socket *sock, struct page *page,
  1088. int offset, size_t size, int flags)
  1089. {
  1090. struct sock *sk = sock->sk;
  1091. struct smc_sock *smc;
  1092. int rc = -EPIPE;
  1093. smc = smc_sk(sk);
  1094. lock_sock(sk);
  1095. if (sk->sk_state != SMC_ACTIVE)
  1096. goto out;
  1097. if (smc->use_fallback)
  1098. rc = kernel_sendpage(smc->clcsock, page, offset,
  1099. size, flags);
  1100. else
  1101. rc = sock_no_sendpage(sock, page, offset, size, flags);
  1102. out:
  1103. release_sock(sk);
  1104. return rc;
  1105. }
  1106. static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
  1107. struct pipe_inode_info *pipe, size_t len,
  1108. unsigned int flags)
  1109. {
  1110. struct sock *sk = sock->sk;
  1111. struct smc_sock *smc;
  1112. int rc = -ENOTCONN;
  1113. smc = smc_sk(sk);
  1114. lock_sock(sk);
  1115. if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
  1116. goto out;
  1117. if (smc->use_fallback) {
  1118. rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
  1119. pipe, len, flags);
  1120. } else {
  1121. rc = -EOPNOTSUPP;
  1122. }
  1123. out:
  1124. release_sock(sk);
  1125. return rc;
  1126. }
  1127. /* must look like tcp */
  1128. static const struct proto_ops smc_sock_ops = {
  1129. .family = PF_SMC,
  1130. .owner = THIS_MODULE,
  1131. .release = smc_release,
  1132. .bind = smc_bind,
  1133. .connect = smc_connect,
  1134. .socketpair = sock_no_socketpair,
  1135. .accept = smc_accept,
  1136. .getname = smc_getname,
  1137. .poll = smc_poll,
  1138. .ioctl = smc_ioctl,
  1139. .listen = smc_listen,
  1140. .shutdown = smc_shutdown,
  1141. .setsockopt = smc_setsockopt,
  1142. .getsockopt = smc_getsockopt,
  1143. .sendmsg = smc_sendmsg,
  1144. .recvmsg = smc_recvmsg,
  1145. .mmap = sock_no_mmap,
  1146. .sendpage = smc_sendpage,
  1147. .splice_read = smc_splice_read,
  1148. };
  1149. static int smc_create(struct net *net, struct socket *sock, int protocol,
  1150. int kern)
  1151. {
  1152. struct smc_sock *smc;
  1153. struct sock *sk;
  1154. int rc;
  1155. rc = -ESOCKTNOSUPPORT;
  1156. if (sock->type != SOCK_STREAM)
  1157. goto out;
  1158. rc = -EPROTONOSUPPORT;
  1159. if ((protocol != IPPROTO_IP) && (protocol != IPPROTO_TCP))
  1160. goto out;
  1161. rc = -ENOBUFS;
  1162. sock->ops = &smc_sock_ops;
  1163. sk = smc_sock_alloc(net, sock);
  1164. if (!sk)
  1165. goto out;
  1166. /* create internal TCP socket for CLC handshake and fallback */
  1167. smc = smc_sk(sk);
  1168. smc->use_fallback = false; /* assume rdma capability first */
  1169. rc = sock_create_kern(net, PF_INET, SOCK_STREAM,
  1170. IPPROTO_TCP, &smc->clcsock);
  1171. if (rc)
  1172. sk_common_release(sk);
  1173. smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
  1174. smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
  1175. out:
  1176. return rc;
  1177. }
  1178. static const struct net_proto_family smc_sock_family_ops = {
  1179. .family = PF_SMC,
  1180. .owner = THIS_MODULE,
  1181. .create = smc_create,
  1182. };
  1183. static int __init smc_init(void)
  1184. {
  1185. int rc;
  1186. rc = smc_pnet_init();
  1187. if (rc)
  1188. return rc;
  1189. rc = smc_llc_init();
  1190. if (rc) {
  1191. pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
  1192. goto out_pnet;
  1193. }
  1194. rc = smc_cdc_init();
  1195. if (rc) {
  1196. pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
  1197. goto out_pnet;
  1198. }
  1199. rc = proto_register(&smc_proto, 1);
  1200. if (rc) {
  1201. pr_err("%s: proto_register fails with %d\n", __func__, rc);
  1202. goto out_pnet;
  1203. }
  1204. rc = sock_register(&smc_sock_family_ops);
  1205. if (rc) {
  1206. pr_err("%s: sock_register fails with %d\n", __func__, rc);
  1207. goto out_proto;
  1208. }
  1209. INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
  1210. rc = smc_ib_register_client();
  1211. if (rc) {
  1212. pr_err("%s: ib_register fails with %d\n", __func__, rc);
  1213. goto out_sock;
  1214. }
  1215. return 0;
  1216. out_sock:
  1217. sock_unregister(PF_SMC);
  1218. out_proto:
  1219. proto_unregister(&smc_proto);
  1220. out_pnet:
  1221. smc_pnet_exit();
  1222. return rc;
  1223. }
  1224. static void __exit smc_exit(void)
  1225. {
  1226. struct smc_link_group *lgr, *lg;
  1227. LIST_HEAD(lgr_freeing_list);
  1228. spin_lock_bh(&smc_lgr_list.lock);
  1229. if (!list_empty(&smc_lgr_list.list))
  1230. list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
  1231. spin_unlock_bh(&smc_lgr_list.lock);
  1232. list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
  1233. list_del_init(&lgr->list);
  1234. smc_lgr_free(lgr); /* free link group */
  1235. }
  1236. smc_ib_unregister_client();
  1237. sock_unregister(PF_SMC);
  1238. proto_unregister(&smc_proto);
  1239. smc_pnet_exit();
  1240. }
  1241. module_init(smc_init);
  1242. module_exit(smc_exit);
  1243. MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
  1244. MODULE_DESCRIPTION("smc socket address family");
  1245. MODULE_LICENSE("GPL");
  1246. MODULE_ALIAS_NETPROTO(PF_SMC);