af_smc.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020
  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. * - support for alternate links postponed
  11. *
  12. * Copyright IBM Corp. 2016, 2018
  13. *
  14. * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
  15. * based on prototype from Frank Blaschka
  16. */
  17. #define KMSG_COMPONENT "smc"
  18. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  19. #include <linux/module.h>
  20. #include <linux/socket.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/in.h>
  23. #include <linux/sched/signal.h>
  24. #include <linux/if_vlan.h>
  25. #include <net/sock.h>
  26. #include <net/tcp.h>
  27. #include <net/smc.h>
  28. #include <asm/ioctls.h>
  29. #include "smc.h"
  30. #include "smc_clc.h"
  31. #include "smc_llc.h"
  32. #include "smc_cdc.h"
  33. #include "smc_core.h"
  34. #include "smc_ib.h"
  35. #include "smc_ism.h"
  36. #include "smc_pnet.h"
  37. #include "smc_tx.h"
  38. #include "smc_rx.h"
  39. #include "smc_close.h"
  40. static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group
  41. * creation
  42. */
  43. static void smc_tcp_listen_work(struct work_struct *);
  44. static void smc_connect_work(struct work_struct *);
  45. static void smc_set_keepalive(struct sock *sk, int val)
  46. {
  47. struct smc_sock *smc = smc_sk(sk);
  48. smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
  49. }
  50. static struct smc_hashinfo smc_v4_hashinfo = {
  51. .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
  52. };
  53. static struct smc_hashinfo smc_v6_hashinfo = {
  54. .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
  55. };
  56. int smc_hash_sk(struct sock *sk)
  57. {
  58. struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
  59. struct hlist_head *head;
  60. head = &h->ht;
  61. write_lock_bh(&h->lock);
  62. sk_add_node(sk, head);
  63. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  64. write_unlock_bh(&h->lock);
  65. return 0;
  66. }
  67. EXPORT_SYMBOL_GPL(smc_hash_sk);
  68. void smc_unhash_sk(struct sock *sk)
  69. {
  70. struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
  71. write_lock_bh(&h->lock);
  72. if (sk_del_node_init(sk))
  73. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  74. write_unlock_bh(&h->lock);
  75. }
  76. EXPORT_SYMBOL_GPL(smc_unhash_sk);
  77. struct proto smc_proto = {
  78. .name = "SMC",
  79. .owner = THIS_MODULE,
  80. .keepalive = smc_set_keepalive,
  81. .hash = smc_hash_sk,
  82. .unhash = smc_unhash_sk,
  83. .obj_size = sizeof(struct smc_sock),
  84. .h.smc_hash = &smc_v4_hashinfo,
  85. .slab_flags = SLAB_TYPESAFE_BY_RCU,
  86. };
  87. EXPORT_SYMBOL_GPL(smc_proto);
  88. struct proto smc_proto6 = {
  89. .name = "SMC6",
  90. .owner = THIS_MODULE,
  91. .keepalive = smc_set_keepalive,
  92. .hash = smc_hash_sk,
  93. .unhash = smc_unhash_sk,
  94. .obj_size = sizeof(struct smc_sock),
  95. .h.smc_hash = &smc_v6_hashinfo,
  96. .slab_flags = SLAB_TYPESAFE_BY_RCU,
  97. };
  98. EXPORT_SYMBOL_GPL(smc_proto6);
  99. static int smc_release(struct socket *sock)
  100. {
  101. struct sock *sk = sock->sk;
  102. struct smc_sock *smc;
  103. int rc = 0;
  104. if (!sk)
  105. goto out;
  106. smc = smc_sk(sk);
  107. /* cleanup for a dangling non-blocking connect */
  108. flush_work(&smc->connect_work);
  109. kfree(smc->connect_info);
  110. smc->connect_info = NULL;
  111. if (sk->sk_state == SMC_LISTEN)
  112. /* smc_close_non_accepted() is called and acquires
  113. * sock lock for child sockets again
  114. */
  115. lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
  116. else
  117. lock_sock(sk);
  118. if (!smc->use_fallback) {
  119. rc = smc_close_active(smc);
  120. sock_set_flag(sk, SOCK_DEAD);
  121. sk->sk_shutdown |= SHUTDOWN_MASK;
  122. }
  123. sk->sk_prot->unhash(sk);
  124. if (smc->clcsock) {
  125. if (smc->use_fallback && sk->sk_state == SMC_LISTEN) {
  126. /* wake up clcsock accept */
  127. rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
  128. }
  129. mutex_lock(&smc->clcsock_release_lock);
  130. sock_release(smc->clcsock);
  131. smc->clcsock = NULL;
  132. mutex_unlock(&smc->clcsock_release_lock);
  133. }
  134. if (smc->use_fallback) {
  135. if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
  136. sock_put(sk); /* passive closing */
  137. sk->sk_state = SMC_CLOSED;
  138. sk->sk_state_change(sk);
  139. }
  140. /* detach socket */
  141. sock_orphan(sk);
  142. sock->sk = NULL;
  143. if (!smc->use_fallback && sk->sk_state == SMC_CLOSED)
  144. smc_conn_free(&smc->conn);
  145. release_sock(sk);
  146. sock_put(sk); /* final sock_put */
  147. out:
  148. return rc;
  149. }
  150. static void smc_destruct(struct sock *sk)
  151. {
  152. if (sk->sk_state != SMC_CLOSED)
  153. return;
  154. if (!sock_flag(sk, SOCK_DEAD))
  155. return;
  156. sk_refcnt_debug_dec(sk);
  157. }
  158. static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
  159. int protocol)
  160. {
  161. struct smc_sock *smc;
  162. struct proto *prot;
  163. struct sock *sk;
  164. prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
  165. sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
  166. if (!sk)
  167. return NULL;
  168. sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
  169. sk->sk_state = SMC_INIT;
  170. sk->sk_destruct = smc_destruct;
  171. sk->sk_protocol = protocol;
  172. smc = smc_sk(sk);
  173. INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
  174. INIT_WORK(&smc->connect_work, smc_connect_work);
  175. INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
  176. INIT_LIST_HEAD(&smc->accept_q);
  177. spin_lock_init(&smc->accept_q_lock);
  178. spin_lock_init(&smc->conn.send_lock);
  179. sk->sk_prot->hash(sk);
  180. sk_refcnt_debug_inc(sk);
  181. mutex_init(&smc->clcsock_release_lock);
  182. return sk;
  183. }
  184. static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
  185. int addr_len)
  186. {
  187. struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
  188. struct sock *sk = sock->sk;
  189. struct smc_sock *smc;
  190. int rc;
  191. smc = smc_sk(sk);
  192. /* replicate tests from inet_bind(), to be safe wrt. future changes */
  193. rc = -EINVAL;
  194. if (addr_len < sizeof(struct sockaddr_in))
  195. goto out;
  196. rc = -EAFNOSUPPORT;
  197. if (addr->sin_family != AF_INET &&
  198. addr->sin_family != AF_INET6 &&
  199. addr->sin_family != AF_UNSPEC)
  200. goto out;
  201. /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
  202. if (addr->sin_family == AF_UNSPEC &&
  203. addr->sin_addr.s_addr != htonl(INADDR_ANY))
  204. goto out;
  205. lock_sock(sk);
  206. /* Check if socket is already active */
  207. rc = -EINVAL;
  208. if (sk->sk_state != SMC_INIT)
  209. goto out_rel;
  210. smc->clcsock->sk->sk_reuse = sk->sk_reuse;
  211. rc = kernel_bind(smc->clcsock, uaddr, addr_len);
  212. out_rel:
  213. release_sock(sk);
  214. out:
  215. return rc;
  216. }
  217. static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
  218. unsigned long mask)
  219. {
  220. /* options we don't get control via setsockopt for */
  221. nsk->sk_type = osk->sk_type;
  222. nsk->sk_sndbuf = osk->sk_sndbuf;
  223. nsk->sk_rcvbuf = osk->sk_rcvbuf;
  224. nsk->sk_sndtimeo = osk->sk_sndtimeo;
  225. nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
  226. nsk->sk_mark = osk->sk_mark;
  227. nsk->sk_priority = osk->sk_priority;
  228. nsk->sk_rcvlowat = osk->sk_rcvlowat;
  229. nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
  230. nsk->sk_err = osk->sk_err;
  231. nsk->sk_flags &= ~mask;
  232. nsk->sk_flags |= osk->sk_flags & mask;
  233. }
  234. #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
  235. (1UL << SOCK_KEEPOPEN) | \
  236. (1UL << SOCK_LINGER) | \
  237. (1UL << SOCK_BROADCAST) | \
  238. (1UL << SOCK_TIMESTAMP) | \
  239. (1UL << SOCK_DBG) | \
  240. (1UL << SOCK_RCVTSTAMP) | \
  241. (1UL << SOCK_RCVTSTAMPNS) | \
  242. (1UL << SOCK_LOCALROUTE) | \
  243. (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
  244. (1UL << SOCK_RXQ_OVFL) | \
  245. (1UL << SOCK_WIFI_STATUS) | \
  246. (1UL << SOCK_NOFCS) | \
  247. (1UL << SOCK_FILTER_LOCKED))
  248. /* copy only relevant settings and flags of SOL_SOCKET level from smc to
  249. * clc socket (since smc is not called for these options from net/core)
  250. */
  251. static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
  252. {
  253. smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
  254. }
  255. #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
  256. (1UL << SOCK_KEEPOPEN) | \
  257. (1UL << SOCK_LINGER) | \
  258. (1UL << SOCK_DBG))
  259. /* copy only settings and flags relevant for smc from clc to smc socket */
  260. static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
  261. {
  262. smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
  263. }
  264. /* register a new rmb, optionally send confirm_rkey msg to register with peer */
  265. static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
  266. bool conf_rkey)
  267. {
  268. /* register memory region for new rmb */
  269. if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
  270. rmb_desc->regerr = 1;
  271. return -EFAULT;
  272. }
  273. if (!conf_rkey)
  274. return 0;
  275. /* exchange confirm_rkey msg with peer */
  276. if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
  277. rmb_desc->regerr = 1;
  278. return -EFAULT;
  279. }
  280. return 0;
  281. }
  282. static int smc_clnt_conf_first_link(struct smc_sock *smc)
  283. {
  284. struct net *net = sock_net(smc->clcsock->sk);
  285. struct smc_link_group *lgr = smc->conn.lgr;
  286. struct smc_link *link;
  287. int rest;
  288. int rc;
  289. link = &lgr->lnk[SMC_SINGLE_LINK];
  290. /* receive CONFIRM LINK request from server over RoCE fabric */
  291. rest = wait_for_completion_interruptible_timeout(
  292. &link->llc_confirm,
  293. SMC_LLC_WAIT_FIRST_TIME);
  294. if (rest <= 0) {
  295. struct smc_clc_msg_decline dclc;
  296. rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
  297. SMC_CLC_DECLINE);
  298. return rc;
  299. }
  300. if (link->llc_confirm_rc)
  301. return SMC_CLC_DECL_RMBE_EC;
  302. rc = smc_ib_modify_qp_rts(link);
  303. if (rc)
  304. return SMC_CLC_DECL_ERR_RDYLNK;
  305. smc_wr_remember_qp_attr(link);
  306. if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
  307. return SMC_CLC_DECL_ERR_REGRMB;
  308. /* send CONFIRM LINK response over RoCE fabric */
  309. rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
  310. if (rc < 0)
  311. return SMC_CLC_DECL_TIMEOUT_CL;
  312. /* receive ADD LINK request from server over RoCE fabric */
  313. rest = wait_for_completion_interruptible_timeout(&link->llc_add,
  314. SMC_LLC_WAIT_TIME);
  315. if (rest <= 0) {
  316. struct smc_clc_msg_decline dclc;
  317. rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
  318. SMC_CLC_DECLINE);
  319. return rc;
  320. }
  321. /* send add link reject message, only one link supported for now */
  322. rc = smc_llc_send_add_link(link,
  323. link->smcibdev->mac[link->ibport - 1],
  324. link->gid, SMC_LLC_RESP);
  325. if (rc < 0)
  326. return SMC_CLC_DECL_TIMEOUT_AL;
  327. smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
  328. return 0;
  329. }
  330. static void smcr_conn_save_peer_info(struct smc_sock *smc,
  331. struct smc_clc_msg_accept_confirm *clc)
  332. {
  333. int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
  334. smc->conn.peer_rmbe_idx = clc->rmbe_idx;
  335. smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
  336. smc->conn.peer_rmbe_size = bufsize;
  337. atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
  338. smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
  339. }
  340. static void smcd_conn_save_peer_info(struct smc_sock *smc,
  341. struct smc_clc_msg_accept_confirm *clc)
  342. {
  343. int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
  344. smc->conn.peer_rmbe_idx = clc->dmbe_idx;
  345. smc->conn.peer_token = clc->token;
  346. /* msg header takes up space in the buffer */
  347. smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
  348. atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
  349. smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
  350. }
  351. static void smc_conn_save_peer_info(struct smc_sock *smc,
  352. struct smc_clc_msg_accept_confirm *clc)
  353. {
  354. if (smc->conn.lgr->is_smcd)
  355. smcd_conn_save_peer_info(smc, clc);
  356. else
  357. smcr_conn_save_peer_info(smc, clc);
  358. }
  359. static void smc_link_save_peer_info(struct smc_link *link,
  360. struct smc_clc_msg_accept_confirm *clc)
  361. {
  362. link->peer_qpn = ntoh24(clc->qpn);
  363. memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
  364. memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
  365. link->peer_psn = ntoh24(clc->psn);
  366. link->peer_mtu = clc->qp_mtu;
  367. }
  368. /* fall back during connect */
  369. static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
  370. {
  371. smc->use_fallback = true;
  372. smc->fallback_rsn = reason_code;
  373. smc_copy_sock_settings_to_clc(smc);
  374. if (smc->sk.sk_state == SMC_INIT)
  375. smc->sk.sk_state = SMC_ACTIVE;
  376. return 0;
  377. }
  378. /* decline and fall back during connect */
  379. static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
  380. {
  381. int rc;
  382. if (reason_code < 0) { /* error, fallback is not possible */
  383. if (smc->sk.sk_state == SMC_INIT)
  384. sock_put(&smc->sk); /* passive closing */
  385. return reason_code;
  386. }
  387. if (reason_code != SMC_CLC_DECL_PEERDECL) {
  388. rc = smc_clc_send_decline(smc, reason_code);
  389. if (rc < 0) {
  390. if (smc->sk.sk_state == SMC_INIT)
  391. sock_put(&smc->sk); /* passive closing */
  392. return rc;
  393. }
  394. }
  395. return smc_connect_fallback(smc, reason_code);
  396. }
  397. /* abort connecting */
  398. static int smc_connect_abort(struct smc_sock *smc, int reason_code,
  399. int local_contact)
  400. {
  401. if (local_contact == SMC_FIRST_CONTACT)
  402. smc_lgr_forget(smc->conn.lgr);
  403. mutex_unlock(&smc_create_lgr_pending);
  404. smc_conn_free(&smc->conn);
  405. return reason_code;
  406. }
  407. /* check if there is a rdma device available for this connection. */
  408. /* called for connect and listen */
  409. static int smc_check_rdma(struct smc_sock *smc, struct smc_ib_device **ibdev,
  410. u8 *ibport, unsigned short vlan_id, u8 gid[])
  411. {
  412. int reason_code = 0;
  413. /* PNET table look up: search active ib_device and port
  414. * within same PNETID that also contains the ethernet device
  415. * used for the internal TCP socket
  416. */
  417. smc_pnet_find_roce_resource(smc->clcsock->sk, ibdev, ibport, vlan_id,
  418. gid);
  419. if (!(*ibdev))
  420. reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
  421. return reason_code;
  422. }
  423. /* check if there is an ISM device available for this connection. */
  424. /* called for connect and listen */
  425. static int smc_check_ism(struct smc_sock *smc, struct smcd_dev **ismdev)
  426. {
  427. /* Find ISM device with same PNETID as connecting interface */
  428. smc_pnet_find_ism_resource(smc->clcsock->sk, ismdev);
  429. if (!(*ismdev))
  430. return SMC_CLC_DECL_CNFERR; /* configuration error */
  431. return 0;
  432. }
  433. /* Check for VLAN ID and register it on ISM device just for CLC handshake */
  434. static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
  435. struct smcd_dev *ismdev,
  436. unsigned short vlan_id)
  437. {
  438. if (vlan_id && smc_ism_get_vlan(ismdev, vlan_id))
  439. return SMC_CLC_DECL_CNFERR;
  440. return 0;
  441. }
  442. /* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
  443. * used, the VLAN ID will be registered again during the connection setup.
  444. */
  445. static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
  446. struct smcd_dev *ismdev,
  447. unsigned short vlan_id)
  448. {
  449. if (!is_smcd)
  450. return 0;
  451. if (vlan_id && smc_ism_put_vlan(ismdev, vlan_id))
  452. return SMC_CLC_DECL_CNFERR;
  453. return 0;
  454. }
  455. /* CLC handshake during connect */
  456. static int smc_connect_clc(struct smc_sock *smc, int smc_type,
  457. struct smc_clc_msg_accept_confirm *aclc,
  458. struct smc_ib_device *ibdev, u8 ibport,
  459. u8 gid[], struct smcd_dev *ismdev)
  460. {
  461. int rc = 0;
  462. /* do inband token exchange */
  463. rc = smc_clc_send_proposal(smc, smc_type, ibdev, ibport, gid, ismdev);
  464. if (rc)
  465. return rc;
  466. /* receive SMC Accept CLC message */
  467. return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT);
  468. }
  469. /* setup for RDMA connection of client */
  470. static int smc_connect_rdma(struct smc_sock *smc,
  471. struct smc_clc_msg_accept_confirm *aclc,
  472. struct smc_ib_device *ibdev, u8 ibport)
  473. {
  474. int local_contact = SMC_FIRST_CONTACT;
  475. struct smc_link *link;
  476. int reason_code = 0;
  477. mutex_lock(&smc_create_lgr_pending);
  478. local_contact = smc_conn_create(smc, false, aclc->hdr.flag, ibdev,
  479. ibport, &aclc->lcl, NULL, 0);
  480. if (local_contact < 0) {
  481. if (local_contact == -ENOMEM)
  482. reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
  483. else if (local_contact == -ENOLINK)
  484. reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
  485. else
  486. reason_code = SMC_CLC_DECL_INTERR; /* other error */
  487. return smc_connect_abort(smc, reason_code, 0);
  488. }
  489. link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
  490. smc_conn_save_peer_info(smc, aclc);
  491. /* create send buffer and rmb */
  492. if (smc_buf_create(smc, false))
  493. return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
  494. if (local_contact == SMC_FIRST_CONTACT)
  495. smc_link_save_peer_info(link, aclc);
  496. if (smc_rmb_rtoken_handling(&smc->conn, aclc))
  497. return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
  498. local_contact);
  499. smc_close_init(smc);
  500. smc_rx_init(smc);
  501. if (local_contact == SMC_FIRST_CONTACT) {
  502. if (smc_ib_ready_link(link))
  503. return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
  504. local_contact);
  505. } else {
  506. if (!smc->conn.rmb_desc->reused &&
  507. smc_reg_rmb(link, smc->conn.rmb_desc, true))
  508. return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
  509. local_contact);
  510. }
  511. smc_rmb_sync_sg_for_device(&smc->conn);
  512. reason_code = smc_clc_send_confirm(smc);
  513. if (reason_code)
  514. return smc_connect_abort(smc, reason_code, local_contact);
  515. smc_tx_init(smc);
  516. if (local_contact == SMC_FIRST_CONTACT) {
  517. /* QP confirmation over RoCE fabric */
  518. reason_code = smc_clnt_conf_first_link(smc);
  519. if (reason_code)
  520. return smc_connect_abort(smc, reason_code,
  521. local_contact);
  522. }
  523. mutex_unlock(&smc_create_lgr_pending);
  524. smc_copy_sock_settings_to_clc(smc);
  525. if (smc->sk.sk_state == SMC_INIT)
  526. smc->sk.sk_state = SMC_ACTIVE;
  527. return 0;
  528. }
  529. /* setup for ISM connection of client */
  530. static int smc_connect_ism(struct smc_sock *smc,
  531. struct smc_clc_msg_accept_confirm *aclc,
  532. struct smcd_dev *ismdev)
  533. {
  534. int local_contact = SMC_FIRST_CONTACT;
  535. int rc = 0;
  536. mutex_lock(&smc_create_lgr_pending);
  537. local_contact = smc_conn_create(smc, true, aclc->hdr.flag, NULL, 0,
  538. NULL, ismdev, aclc->gid);
  539. if (local_contact < 0)
  540. return smc_connect_abort(smc, SMC_CLC_DECL_MEM, 0);
  541. /* Create send and receive buffers */
  542. if (smc_buf_create(smc, true))
  543. return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
  544. smc_conn_save_peer_info(smc, aclc);
  545. smc_close_init(smc);
  546. smc_rx_init(smc);
  547. smc_tx_init(smc);
  548. rc = smc_clc_send_confirm(smc);
  549. if (rc)
  550. return smc_connect_abort(smc, rc, local_contact);
  551. mutex_unlock(&smc_create_lgr_pending);
  552. smc_copy_sock_settings_to_clc(smc);
  553. if (smc->sk.sk_state == SMC_INIT)
  554. smc->sk.sk_state = SMC_ACTIVE;
  555. return 0;
  556. }
  557. /* perform steps before actually connecting */
  558. static int __smc_connect(struct smc_sock *smc)
  559. {
  560. bool ism_supported = false, rdma_supported = false;
  561. struct smc_clc_msg_accept_confirm aclc;
  562. struct smc_ib_device *ibdev;
  563. struct smcd_dev *ismdev;
  564. u8 gid[SMC_GID_SIZE];
  565. unsigned short vlan;
  566. int smc_type;
  567. int rc = 0;
  568. u8 ibport;
  569. sock_hold(&smc->sk); /* sock put in passive closing */
  570. if (smc->use_fallback)
  571. return smc_connect_fallback(smc, smc->fallback_rsn);
  572. /* if peer has not signalled SMC-capability, fall back */
  573. if (!tcp_sk(smc->clcsock->sk)->syn_smc)
  574. return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
  575. /* IPSec connections opt out of SMC-R optimizations */
  576. if (using_ipsec(smc))
  577. return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
  578. /* check for VLAN ID */
  579. if (smc_vlan_by_tcpsk(smc->clcsock, &vlan))
  580. return smc_connect_decline_fallback(smc, SMC_CLC_DECL_CNFERR);
  581. /* check if there is an ism device available */
  582. if (!smc_check_ism(smc, &ismdev) &&
  583. !smc_connect_ism_vlan_setup(smc, ismdev, vlan)) {
  584. /* ISM is supported for this connection */
  585. ism_supported = true;
  586. smc_type = SMC_TYPE_D;
  587. }
  588. /* check if there is a rdma device available */
  589. if (!smc_check_rdma(smc, &ibdev, &ibport, vlan, gid)) {
  590. /* RDMA is supported for this connection */
  591. rdma_supported = true;
  592. if (ism_supported)
  593. smc_type = SMC_TYPE_B; /* both */
  594. else
  595. smc_type = SMC_TYPE_R; /* only RDMA */
  596. }
  597. /* if neither ISM nor RDMA are supported, fallback */
  598. if (!rdma_supported && !ism_supported)
  599. return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
  600. /* perform CLC handshake */
  601. rc = smc_connect_clc(smc, smc_type, &aclc, ibdev, ibport, gid, ismdev);
  602. if (rc) {
  603. smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
  604. return smc_connect_decline_fallback(smc, rc);
  605. }
  606. /* depending on previous steps, connect using rdma or ism */
  607. if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
  608. rc = smc_connect_rdma(smc, &aclc, ibdev, ibport);
  609. else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
  610. rc = smc_connect_ism(smc, &aclc, ismdev);
  611. else
  612. rc = SMC_CLC_DECL_MODEUNSUPP;
  613. if (rc) {
  614. smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
  615. return smc_connect_decline_fallback(smc, rc);
  616. }
  617. smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
  618. return 0;
  619. }
  620. static void smc_connect_work(struct work_struct *work)
  621. {
  622. struct smc_sock *smc = container_of(work, struct smc_sock,
  623. connect_work);
  624. int rc;
  625. lock_sock(&smc->sk);
  626. rc = kernel_connect(smc->clcsock, &smc->connect_info->addr,
  627. smc->connect_info->alen, smc->connect_info->flags);
  628. if (smc->clcsock->sk->sk_err) {
  629. smc->sk.sk_err = smc->clcsock->sk->sk_err;
  630. goto out;
  631. }
  632. if (rc < 0) {
  633. smc->sk.sk_err = -rc;
  634. goto out;
  635. }
  636. rc = __smc_connect(smc);
  637. if (rc < 0)
  638. smc->sk.sk_err = -rc;
  639. out:
  640. if (smc->sk.sk_err)
  641. smc->sk.sk_state_change(&smc->sk);
  642. else
  643. smc->sk.sk_write_space(&smc->sk);
  644. kfree(smc->connect_info);
  645. smc->connect_info = NULL;
  646. release_sock(&smc->sk);
  647. }
  648. static int smc_connect(struct socket *sock, struct sockaddr *addr,
  649. int alen, int flags)
  650. {
  651. struct sock *sk = sock->sk;
  652. struct smc_sock *smc;
  653. int rc = -EINVAL;
  654. smc = smc_sk(sk);
  655. /* separate smc parameter checking to be safe */
  656. if (alen < sizeof(addr->sa_family))
  657. goto out_err;
  658. if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
  659. goto out_err;
  660. lock_sock(sk);
  661. switch (sk->sk_state) {
  662. default:
  663. goto out;
  664. case SMC_ACTIVE:
  665. rc = -EISCONN;
  666. goto out;
  667. case SMC_INIT:
  668. rc = 0;
  669. break;
  670. }
  671. smc_copy_sock_settings_to_clc(smc);
  672. tcp_sk(smc->clcsock->sk)->syn_smc = 1;
  673. if (flags & O_NONBLOCK) {
  674. if (smc->connect_info) {
  675. rc = -EALREADY;
  676. goto out;
  677. }
  678. smc->connect_info = kzalloc(alen + 2 * sizeof(int), GFP_KERNEL);
  679. if (!smc->connect_info) {
  680. rc = -ENOMEM;
  681. goto out;
  682. }
  683. smc->connect_info->alen = alen;
  684. smc->connect_info->flags = flags ^ O_NONBLOCK;
  685. memcpy(&smc->connect_info->addr, addr, alen);
  686. schedule_work(&smc->connect_work);
  687. rc = -EINPROGRESS;
  688. } else {
  689. rc = kernel_connect(smc->clcsock, addr, alen, flags);
  690. if (rc)
  691. goto out;
  692. rc = __smc_connect(smc);
  693. if (rc < 0)
  694. goto out;
  695. else
  696. rc = 0; /* success cases including fallback */
  697. }
  698. out:
  699. release_sock(sk);
  700. out_err:
  701. return rc;
  702. }
  703. static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
  704. {
  705. struct socket *new_clcsock = NULL;
  706. struct sock *lsk = &lsmc->sk;
  707. struct sock *new_sk;
  708. int rc = -EINVAL;
  709. release_sock(lsk);
  710. new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
  711. if (!new_sk) {
  712. rc = -ENOMEM;
  713. lsk->sk_err = ENOMEM;
  714. *new_smc = NULL;
  715. lock_sock(lsk);
  716. goto out;
  717. }
  718. *new_smc = smc_sk(new_sk);
  719. mutex_lock(&lsmc->clcsock_release_lock);
  720. if (lsmc->clcsock)
  721. rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
  722. mutex_unlock(&lsmc->clcsock_release_lock);
  723. lock_sock(lsk);
  724. if (rc < 0)
  725. lsk->sk_err = -rc;
  726. if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
  727. new_sk->sk_prot->unhash(new_sk);
  728. if (new_clcsock)
  729. sock_release(new_clcsock);
  730. new_sk->sk_state = SMC_CLOSED;
  731. sock_set_flag(new_sk, SOCK_DEAD);
  732. sock_put(new_sk); /* final */
  733. *new_smc = NULL;
  734. goto out;
  735. }
  736. (*new_smc)->clcsock = new_clcsock;
  737. out:
  738. return rc;
  739. }
  740. /* add a just created sock to the accept queue of the listen sock as
  741. * candidate for a following socket accept call from user space
  742. */
  743. static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
  744. {
  745. struct smc_sock *par = smc_sk(parent);
  746. sock_hold(sk); /* sock_put in smc_accept_unlink () */
  747. spin_lock(&par->accept_q_lock);
  748. list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
  749. spin_unlock(&par->accept_q_lock);
  750. sk_acceptq_added(parent);
  751. }
  752. /* remove a socket from the accept queue of its parental listening socket */
  753. static void smc_accept_unlink(struct sock *sk)
  754. {
  755. struct smc_sock *par = smc_sk(sk)->listen_smc;
  756. spin_lock(&par->accept_q_lock);
  757. list_del_init(&smc_sk(sk)->accept_q);
  758. spin_unlock(&par->accept_q_lock);
  759. sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
  760. sock_put(sk); /* sock_hold in smc_accept_enqueue */
  761. }
  762. /* remove a sock from the accept queue to bind it to a new socket created
  763. * for a socket accept call from user space
  764. */
  765. struct sock *smc_accept_dequeue(struct sock *parent,
  766. struct socket *new_sock)
  767. {
  768. struct smc_sock *isk, *n;
  769. struct sock *new_sk;
  770. list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
  771. new_sk = (struct sock *)isk;
  772. smc_accept_unlink(new_sk);
  773. if (new_sk->sk_state == SMC_CLOSED) {
  774. new_sk->sk_prot->unhash(new_sk);
  775. if (isk->clcsock) {
  776. sock_release(isk->clcsock);
  777. isk->clcsock = NULL;
  778. }
  779. sock_put(new_sk); /* final */
  780. continue;
  781. }
  782. if (new_sock)
  783. sock_graft(new_sk, new_sock);
  784. return new_sk;
  785. }
  786. return NULL;
  787. }
  788. /* clean up for a created but never accepted sock */
  789. void smc_close_non_accepted(struct sock *sk)
  790. {
  791. struct smc_sock *smc = smc_sk(sk);
  792. lock_sock(sk);
  793. if (!sk->sk_lingertime)
  794. /* wait for peer closing */
  795. sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
  796. if (!smc->use_fallback) {
  797. smc_close_active(smc);
  798. sock_set_flag(sk, SOCK_DEAD);
  799. sk->sk_shutdown |= SHUTDOWN_MASK;
  800. }
  801. sk->sk_prot->unhash(sk);
  802. if (smc->clcsock) {
  803. struct socket *tcp;
  804. tcp = smc->clcsock;
  805. smc->clcsock = NULL;
  806. sock_release(tcp);
  807. }
  808. if (smc->use_fallback) {
  809. sock_put(sk); /* passive closing */
  810. sk->sk_state = SMC_CLOSED;
  811. } else {
  812. if (sk->sk_state == SMC_CLOSED)
  813. smc_conn_free(&smc->conn);
  814. }
  815. release_sock(sk);
  816. sock_put(sk); /* final sock_put */
  817. }
  818. static int smc_serv_conf_first_link(struct smc_sock *smc)
  819. {
  820. struct net *net = sock_net(smc->clcsock->sk);
  821. struct smc_link_group *lgr = smc->conn.lgr;
  822. struct smc_link *link;
  823. int rest;
  824. int rc;
  825. link = &lgr->lnk[SMC_SINGLE_LINK];
  826. if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
  827. return SMC_CLC_DECL_ERR_REGRMB;
  828. /* send CONFIRM LINK request to client over the RoCE fabric */
  829. rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
  830. if (rc < 0)
  831. return SMC_CLC_DECL_TIMEOUT_CL;
  832. /* receive CONFIRM LINK response from client over the RoCE fabric */
  833. rest = wait_for_completion_interruptible_timeout(
  834. &link->llc_confirm_resp,
  835. SMC_LLC_WAIT_FIRST_TIME);
  836. if (rest <= 0) {
  837. struct smc_clc_msg_decline dclc;
  838. rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
  839. SMC_CLC_DECLINE);
  840. return rc;
  841. }
  842. if (link->llc_confirm_resp_rc)
  843. return SMC_CLC_DECL_RMBE_EC;
  844. /* send ADD LINK request to client over the RoCE fabric */
  845. rc = smc_llc_send_add_link(link,
  846. link->smcibdev->mac[link->ibport - 1],
  847. link->gid, SMC_LLC_REQ);
  848. if (rc < 0)
  849. return SMC_CLC_DECL_TIMEOUT_AL;
  850. /* receive ADD LINK response from client over the RoCE fabric */
  851. rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
  852. SMC_LLC_WAIT_TIME);
  853. if (rest <= 0) {
  854. struct smc_clc_msg_decline dclc;
  855. rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
  856. SMC_CLC_DECLINE);
  857. return rc;
  858. }
  859. smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
  860. return 0;
  861. }
  862. /* listen worker: finish */
  863. static void smc_listen_out(struct smc_sock *new_smc)
  864. {
  865. struct smc_sock *lsmc = new_smc->listen_smc;
  866. struct sock *newsmcsk = &new_smc->sk;
  867. lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
  868. if (lsmc->sk.sk_state == SMC_LISTEN) {
  869. smc_accept_enqueue(&lsmc->sk, newsmcsk);
  870. } else { /* no longer listening */
  871. smc_close_non_accepted(newsmcsk);
  872. }
  873. release_sock(&lsmc->sk);
  874. /* Wake up accept */
  875. lsmc->sk.sk_data_ready(&lsmc->sk);
  876. sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
  877. }
  878. /* listen worker: finish in state connected */
  879. static void smc_listen_out_connected(struct smc_sock *new_smc)
  880. {
  881. struct sock *newsmcsk = &new_smc->sk;
  882. sk_refcnt_debug_inc(newsmcsk);
  883. if (newsmcsk->sk_state == SMC_INIT)
  884. newsmcsk->sk_state = SMC_ACTIVE;
  885. smc_listen_out(new_smc);
  886. }
  887. /* listen worker: finish in error state */
  888. static void smc_listen_out_err(struct smc_sock *new_smc)
  889. {
  890. struct sock *newsmcsk = &new_smc->sk;
  891. if (newsmcsk->sk_state == SMC_INIT)
  892. sock_put(&new_smc->sk); /* passive closing */
  893. newsmcsk->sk_state = SMC_CLOSED;
  894. smc_conn_free(&new_smc->conn);
  895. smc_listen_out(new_smc);
  896. }
  897. /* listen worker: decline and fall back if possible */
  898. static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
  899. int local_contact)
  900. {
  901. /* RDMA setup failed, switch back to TCP */
  902. if (local_contact == SMC_FIRST_CONTACT)
  903. smc_lgr_forget(new_smc->conn.lgr);
  904. if (reason_code < 0) { /* error, no fallback possible */
  905. smc_listen_out_err(new_smc);
  906. return;
  907. }
  908. smc_conn_free(&new_smc->conn);
  909. new_smc->use_fallback = true;
  910. new_smc->fallback_rsn = reason_code;
  911. if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
  912. if (smc_clc_send_decline(new_smc, reason_code) < 0) {
  913. smc_listen_out_err(new_smc);
  914. return;
  915. }
  916. }
  917. smc_listen_out_connected(new_smc);
  918. }
  919. /* listen worker: check prefixes */
  920. static int smc_listen_rdma_check(struct smc_sock *new_smc,
  921. struct smc_clc_msg_proposal *pclc)
  922. {
  923. struct smc_clc_msg_proposal_prefix *pclc_prfx;
  924. struct socket *newclcsock = new_smc->clcsock;
  925. pclc_prfx = smc_clc_proposal_get_prefix(pclc);
  926. if (smc_clc_prfx_match(newclcsock, pclc_prfx))
  927. return SMC_CLC_DECL_CNFERR;
  928. return 0;
  929. }
  930. /* listen worker: initialize connection and buffers */
  931. static int smc_listen_rdma_init(struct smc_sock *new_smc,
  932. struct smc_clc_msg_proposal *pclc,
  933. struct smc_ib_device *ibdev, u8 ibport,
  934. int *local_contact)
  935. {
  936. /* allocate connection / link group */
  937. *local_contact = smc_conn_create(new_smc, false, 0, ibdev, ibport,
  938. &pclc->lcl, NULL, 0);
  939. if (*local_contact < 0) {
  940. if (*local_contact == -ENOMEM)
  941. return SMC_CLC_DECL_MEM;/* insufficient memory*/
  942. return SMC_CLC_DECL_INTERR; /* other error */
  943. }
  944. /* create send buffer and rmb */
  945. if (smc_buf_create(new_smc, false))
  946. return SMC_CLC_DECL_MEM;
  947. return 0;
  948. }
  949. /* listen worker: initialize connection and buffers for SMC-D */
  950. static int smc_listen_ism_init(struct smc_sock *new_smc,
  951. struct smc_clc_msg_proposal *pclc,
  952. struct smcd_dev *ismdev,
  953. int *local_contact)
  954. {
  955. struct smc_clc_msg_smcd *pclc_smcd;
  956. pclc_smcd = smc_get_clc_msg_smcd(pclc);
  957. *local_contact = smc_conn_create(new_smc, true, 0, NULL, 0, NULL,
  958. ismdev, pclc_smcd->gid);
  959. if (*local_contact < 0) {
  960. if (*local_contact == -ENOMEM)
  961. return SMC_CLC_DECL_MEM;/* insufficient memory*/
  962. return SMC_CLC_DECL_INTERR; /* other error */
  963. }
  964. /* Check if peer can be reached via ISM device */
  965. if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
  966. new_smc->conn.lgr->vlan_id,
  967. new_smc->conn.lgr->smcd)) {
  968. if (*local_contact == SMC_FIRST_CONTACT)
  969. smc_lgr_forget(new_smc->conn.lgr);
  970. smc_conn_free(&new_smc->conn);
  971. return SMC_CLC_DECL_CNFERR;
  972. }
  973. /* Create send and receive buffers */
  974. if (smc_buf_create(new_smc, true)) {
  975. if (*local_contact == SMC_FIRST_CONTACT)
  976. smc_lgr_forget(new_smc->conn.lgr);
  977. smc_conn_free(&new_smc->conn);
  978. return SMC_CLC_DECL_MEM;
  979. }
  980. return 0;
  981. }
  982. /* listen worker: register buffers */
  983. static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
  984. {
  985. struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
  986. if (local_contact != SMC_FIRST_CONTACT) {
  987. if (!new_smc->conn.rmb_desc->reused) {
  988. if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
  989. return SMC_CLC_DECL_ERR_REGRMB;
  990. }
  991. }
  992. smc_rmb_sync_sg_for_device(&new_smc->conn);
  993. return 0;
  994. }
  995. /* listen worker: finish RDMA setup */
  996. static int smc_listen_rdma_finish(struct smc_sock *new_smc,
  997. struct smc_clc_msg_accept_confirm *cclc,
  998. int local_contact)
  999. {
  1000. struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
  1001. int reason_code = 0;
  1002. if (local_contact == SMC_FIRST_CONTACT)
  1003. smc_link_save_peer_info(link, cclc);
  1004. if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
  1005. reason_code = SMC_CLC_DECL_ERR_RTOK;
  1006. goto decline;
  1007. }
  1008. if (local_contact == SMC_FIRST_CONTACT) {
  1009. if (smc_ib_ready_link(link)) {
  1010. reason_code = SMC_CLC_DECL_ERR_RDYLNK;
  1011. goto decline;
  1012. }
  1013. /* QP confirmation over RoCE fabric */
  1014. reason_code = smc_serv_conf_first_link(new_smc);
  1015. if (reason_code)
  1016. goto decline;
  1017. }
  1018. return 0;
  1019. decline:
  1020. mutex_unlock(&smc_create_lgr_pending);
  1021. smc_listen_decline(new_smc, reason_code, local_contact);
  1022. return reason_code;
  1023. }
  1024. /* setup for RDMA connection of server */
  1025. static void smc_listen_work(struct work_struct *work)
  1026. {
  1027. struct smc_sock *new_smc = container_of(work, struct smc_sock,
  1028. smc_listen_work);
  1029. struct socket *newclcsock = new_smc->clcsock;
  1030. struct smc_clc_msg_accept_confirm cclc;
  1031. struct smc_clc_msg_proposal *pclc;
  1032. struct smc_ib_device *ibdev;
  1033. bool ism_supported = false;
  1034. struct smcd_dev *ismdev;
  1035. u8 buf[SMC_CLC_MAX_LEN];
  1036. int local_contact = 0;
  1037. unsigned short vlan;
  1038. int reason_code = 0;
  1039. int rc = 0;
  1040. u8 ibport;
  1041. if (new_smc->use_fallback) {
  1042. smc_listen_out_connected(new_smc);
  1043. return;
  1044. }
  1045. /* check if peer is smc capable */
  1046. if (!tcp_sk(newclcsock->sk)->syn_smc) {
  1047. new_smc->use_fallback = true;
  1048. new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
  1049. smc_listen_out_connected(new_smc);
  1050. return;
  1051. }
  1052. /* do inband token exchange -
  1053. * wait for and receive SMC Proposal CLC message
  1054. */
  1055. pclc = (struct smc_clc_msg_proposal *)&buf;
  1056. reason_code = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
  1057. SMC_CLC_PROPOSAL);
  1058. if (reason_code) {
  1059. smc_listen_decline(new_smc, reason_code, 0);
  1060. return;
  1061. }
  1062. /* IPSec connections opt out of SMC-R optimizations */
  1063. if (using_ipsec(new_smc)) {
  1064. smc_listen_decline(new_smc, SMC_CLC_DECL_IPSEC, 0);
  1065. return;
  1066. }
  1067. mutex_lock(&smc_create_lgr_pending);
  1068. smc_close_init(new_smc);
  1069. smc_rx_init(new_smc);
  1070. smc_tx_init(new_smc);
  1071. /* check if ISM is available */
  1072. if ((pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) &&
  1073. !smc_check_ism(new_smc, &ismdev) &&
  1074. !smc_listen_ism_init(new_smc, pclc, ismdev, &local_contact)) {
  1075. ism_supported = true;
  1076. }
  1077. /* check if RDMA is available */
  1078. if (!ism_supported &&
  1079. ((pclc->hdr.path != SMC_TYPE_R && pclc->hdr.path != SMC_TYPE_B) ||
  1080. smc_vlan_by_tcpsk(new_smc->clcsock, &vlan) ||
  1081. smc_check_rdma(new_smc, &ibdev, &ibport, vlan, NULL) ||
  1082. smc_listen_rdma_check(new_smc, pclc) ||
  1083. smc_listen_rdma_init(new_smc, pclc, ibdev, ibport,
  1084. &local_contact) ||
  1085. smc_listen_rdma_reg(new_smc, local_contact))) {
  1086. /* SMC not supported, decline */
  1087. mutex_unlock(&smc_create_lgr_pending);
  1088. smc_listen_decline(new_smc, SMC_CLC_DECL_MODEUNSUPP,
  1089. local_contact);
  1090. return;
  1091. }
  1092. /* send SMC Accept CLC message */
  1093. rc = smc_clc_send_accept(new_smc, local_contact);
  1094. if (rc) {
  1095. mutex_unlock(&smc_create_lgr_pending);
  1096. smc_listen_decline(new_smc, rc, local_contact);
  1097. return;
  1098. }
  1099. /* receive SMC Confirm CLC message */
  1100. reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
  1101. SMC_CLC_CONFIRM);
  1102. if (reason_code) {
  1103. mutex_unlock(&smc_create_lgr_pending);
  1104. smc_listen_decline(new_smc, reason_code, local_contact);
  1105. return;
  1106. }
  1107. /* finish worker */
  1108. if (!ism_supported) {
  1109. if (smc_listen_rdma_finish(new_smc, &cclc, local_contact))
  1110. return;
  1111. }
  1112. smc_conn_save_peer_info(new_smc, &cclc);
  1113. mutex_unlock(&smc_create_lgr_pending);
  1114. smc_listen_out_connected(new_smc);
  1115. }
  1116. static void smc_tcp_listen_work(struct work_struct *work)
  1117. {
  1118. struct smc_sock *lsmc = container_of(work, struct smc_sock,
  1119. tcp_listen_work);
  1120. struct sock *lsk = &lsmc->sk;
  1121. struct smc_sock *new_smc;
  1122. int rc = 0;
  1123. lock_sock(lsk);
  1124. while (lsk->sk_state == SMC_LISTEN) {
  1125. rc = smc_clcsock_accept(lsmc, &new_smc);
  1126. if (rc)
  1127. goto out;
  1128. if (!new_smc)
  1129. continue;
  1130. new_smc->listen_smc = lsmc;
  1131. new_smc->use_fallback = lsmc->use_fallback;
  1132. new_smc->fallback_rsn = lsmc->fallback_rsn;
  1133. sock_hold(lsk); /* sock_put in smc_listen_work */
  1134. INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
  1135. smc_copy_sock_settings_to_smc(new_smc);
  1136. new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
  1137. new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
  1138. sock_hold(&new_smc->sk); /* sock_put in passive closing */
  1139. if (!schedule_work(&new_smc->smc_listen_work))
  1140. sock_put(&new_smc->sk);
  1141. }
  1142. out:
  1143. release_sock(lsk);
  1144. sock_put(&lsmc->sk); /* sock_hold in smc_listen */
  1145. }
  1146. static int smc_listen(struct socket *sock, int backlog)
  1147. {
  1148. struct sock *sk = sock->sk;
  1149. struct smc_sock *smc;
  1150. int rc;
  1151. smc = smc_sk(sk);
  1152. lock_sock(sk);
  1153. rc = -EINVAL;
  1154. if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
  1155. goto out;
  1156. rc = 0;
  1157. if (sk->sk_state == SMC_LISTEN) {
  1158. sk->sk_max_ack_backlog = backlog;
  1159. goto out;
  1160. }
  1161. /* some socket options are handled in core, so we could not apply
  1162. * them to the clc socket -- copy smc socket options to clc socket
  1163. */
  1164. smc_copy_sock_settings_to_clc(smc);
  1165. if (!smc->use_fallback)
  1166. tcp_sk(smc->clcsock->sk)->syn_smc = 1;
  1167. rc = kernel_listen(smc->clcsock, backlog);
  1168. if (rc)
  1169. goto out;
  1170. sk->sk_max_ack_backlog = backlog;
  1171. sk->sk_ack_backlog = 0;
  1172. sk->sk_state = SMC_LISTEN;
  1173. INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
  1174. sock_hold(sk); /* sock_hold in tcp_listen_worker */
  1175. if (!schedule_work(&smc->tcp_listen_work))
  1176. sock_put(sk);
  1177. out:
  1178. release_sock(sk);
  1179. return rc;
  1180. }
  1181. static int smc_accept(struct socket *sock, struct socket *new_sock,
  1182. int flags, bool kern)
  1183. {
  1184. struct sock *sk = sock->sk, *nsk;
  1185. DECLARE_WAITQUEUE(wait, current);
  1186. struct smc_sock *lsmc;
  1187. long timeo;
  1188. int rc = 0;
  1189. lsmc = smc_sk(sk);
  1190. sock_hold(sk); /* sock_put below */
  1191. lock_sock(sk);
  1192. if (lsmc->sk.sk_state != SMC_LISTEN) {
  1193. rc = -EINVAL;
  1194. release_sock(sk);
  1195. goto out;
  1196. }
  1197. /* Wait for an incoming connection */
  1198. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  1199. add_wait_queue_exclusive(sk_sleep(sk), &wait);
  1200. while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
  1201. set_current_state(TASK_INTERRUPTIBLE);
  1202. if (!timeo) {
  1203. rc = -EAGAIN;
  1204. break;
  1205. }
  1206. release_sock(sk);
  1207. timeo = schedule_timeout(timeo);
  1208. /* wakeup by sk_data_ready in smc_listen_work() */
  1209. sched_annotate_sleep();
  1210. lock_sock(sk);
  1211. if (signal_pending(current)) {
  1212. rc = sock_intr_errno(timeo);
  1213. break;
  1214. }
  1215. }
  1216. set_current_state(TASK_RUNNING);
  1217. remove_wait_queue(sk_sleep(sk), &wait);
  1218. if (!rc)
  1219. rc = sock_error(nsk);
  1220. release_sock(sk);
  1221. if (rc)
  1222. goto out;
  1223. if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
  1224. /* wait till data arrives on the socket */
  1225. timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
  1226. MSEC_PER_SEC);
  1227. if (smc_sk(nsk)->use_fallback) {
  1228. struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
  1229. lock_sock(clcsk);
  1230. if (skb_queue_empty(&clcsk->sk_receive_queue))
  1231. sk_wait_data(clcsk, &timeo, NULL);
  1232. release_sock(clcsk);
  1233. } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
  1234. lock_sock(nsk);
  1235. smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
  1236. release_sock(nsk);
  1237. }
  1238. }
  1239. out:
  1240. sock_put(sk); /* sock_hold above */
  1241. return rc;
  1242. }
  1243. static int smc_getname(struct socket *sock, struct sockaddr *addr,
  1244. int peer)
  1245. {
  1246. struct smc_sock *smc;
  1247. if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
  1248. (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
  1249. return -ENOTCONN;
  1250. smc = smc_sk(sock->sk);
  1251. return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
  1252. }
  1253. static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  1254. {
  1255. struct sock *sk = sock->sk;
  1256. struct smc_sock *smc;
  1257. int rc = -EPIPE;
  1258. smc = smc_sk(sk);
  1259. lock_sock(sk);
  1260. if ((sk->sk_state != SMC_ACTIVE) &&
  1261. (sk->sk_state != SMC_APPCLOSEWAIT1) &&
  1262. (sk->sk_state != SMC_INIT))
  1263. goto out;
  1264. if (msg->msg_flags & MSG_FASTOPEN) {
  1265. if (sk->sk_state == SMC_INIT) {
  1266. smc->use_fallback = true;
  1267. smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
  1268. } else {
  1269. rc = -EINVAL;
  1270. goto out;
  1271. }
  1272. }
  1273. if (smc->use_fallback)
  1274. rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
  1275. else
  1276. rc = smc_tx_sendmsg(smc, msg, len);
  1277. out:
  1278. release_sock(sk);
  1279. return rc;
  1280. }
  1281. static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  1282. int flags)
  1283. {
  1284. struct sock *sk = sock->sk;
  1285. struct smc_sock *smc;
  1286. int rc = -ENOTCONN;
  1287. smc = smc_sk(sk);
  1288. lock_sock(sk);
  1289. if ((sk->sk_state == SMC_INIT) ||
  1290. (sk->sk_state == SMC_LISTEN) ||
  1291. (sk->sk_state == SMC_CLOSED))
  1292. goto out;
  1293. if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
  1294. rc = 0;
  1295. goto out;
  1296. }
  1297. if (smc->use_fallback) {
  1298. rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
  1299. } else {
  1300. msg->msg_namelen = 0;
  1301. rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
  1302. }
  1303. out:
  1304. release_sock(sk);
  1305. return rc;
  1306. }
  1307. static __poll_t smc_accept_poll(struct sock *parent)
  1308. {
  1309. struct smc_sock *isk = smc_sk(parent);
  1310. __poll_t mask = 0;
  1311. spin_lock(&isk->accept_q_lock);
  1312. if (!list_empty(&isk->accept_q))
  1313. mask = EPOLLIN | EPOLLRDNORM;
  1314. spin_unlock(&isk->accept_q_lock);
  1315. return mask;
  1316. }
  1317. static __poll_t smc_poll(struct file *file, struct socket *sock,
  1318. poll_table *wait)
  1319. {
  1320. struct sock *sk = sock->sk;
  1321. __poll_t mask = 0;
  1322. struct smc_sock *smc;
  1323. if (!sk)
  1324. return EPOLLNVAL;
  1325. smc = smc_sk(sock->sk);
  1326. if (smc->use_fallback) {
  1327. /* delegate to CLC child sock */
  1328. mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
  1329. sk->sk_err = smc->clcsock->sk->sk_err;
  1330. if (sk->sk_err)
  1331. mask |= EPOLLERR;
  1332. } else {
  1333. if (sk->sk_state != SMC_CLOSED)
  1334. sock_poll_wait(file, sock, wait);
  1335. if (sk->sk_err)
  1336. mask |= EPOLLERR;
  1337. if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
  1338. (sk->sk_state == SMC_CLOSED))
  1339. mask |= EPOLLHUP;
  1340. if (sk->sk_state == SMC_LISTEN) {
  1341. /* woken up by sk_data_ready in smc_listen_work() */
  1342. mask = smc_accept_poll(sk);
  1343. } else {
  1344. if (atomic_read(&smc->conn.sndbuf_space) ||
  1345. sk->sk_shutdown & SEND_SHUTDOWN) {
  1346. mask |= EPOLLOUT | EPOLLWRNORM;
  1347. } else {
  1348. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  1349. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  1350. }
  1351. if (atomic_read(&smc->conn.bytes_to_rcv))
  1352. mask |= EPOLLIN | EPOLLRDNORM;
  1353. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1354. mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
  1355. if (sk->sk_state == SMC_APPCLOSEWAIT1)
  1356. mask |= EPOLLIN;
  1357. if (smc->conn.urg_state == SMC_URG_VALID)
  1358. mask |= EPOLLPRI;
  1359. }
  1360. }
  1361. return mask;
  1362. }
  1363. static int smc_shutdown(struct socket *sock, int how)
  1364. {
  1365. struct sock *sk = sock->sk;
  1366. struct smc_sock *smc;
  1367. int rc = -EINVAL;
  1368. int rc1 = 0;
  1369. smc = smc_sk(sk);
  1370. if ((how < SHUT_RD) || (how > SHUT_RDWR))
  1371. return rc;
  1372. lock_sock(sk);
  1373. rc = -ENOTCONN;
  1374. if ((sk->sk_state != SMC_ACTIVE) &&
  1375. (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
  1376. (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
  1377. (sk->sk_state != SMC_APPCLOSEWAIT1) &&
  1378. (sk->sk_state != SMC_APPCLOSEWAIT2) &&
  1379. (sk->sk_state != SMC_APPFINCLOSEWAIT))
  1380. goto out;
  1381. if (smc->use_fallback) {
  1382. rc = kernel_sock_shutdown(smc->clcsock, how);
  1383. sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
  1384. if (sk->sk_shutdown == SHUTDOWN_MASK)
  1385. sk->sk_state = SMC_CLOSED;
  1386. goto out;
  1387. }
  1388. switch (how) {
  1389. case SHUT_RDWR: /* shutdown in both directions */
  1390. rc = smc_close_active(smc);
  1391. break;
  1392. case SHUT_WR:
  1393. rc = smc_close_shutdown_write(smc);
  1394. break;
  1395. case SHUT_RD:
  1396. rc = 0;
  1397. /* nothing more to do because peer is not involved */
  1398. break;
  1399. }
  1400. if (smc->clcsock)
  1401. rc1 = kernel_sock_shutdown(smc->clcsock, how);
  1402. /* map sock_shutdown_cmd constants to sk_shutdown value range */
  1403. sk->sk_shutdown |= how + 1;
  1404. out:
  1405. release_sock(sk);
  1406. return rc ? rc : rc1;
  1407. }
  1408. static int smc_setsockopt(struct socket *sock, int level, int optname,
  1409. char __user *optval, unsigned int optlen)
  1410. {
  1411. struct sock *sk = sock->sk;
  1412. struct smc_sock *smc;
  1413. int val, rc;
  1414. smc = smc_sk(sk);
  1415. /* generic setsockopts reaching us here always apply to the
  1416. * CLC socket
  1417. */
  1418. rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
  1419. optval, optlen);
  1420. if (smc->clcsock->sk->sk_err) {
  1421. sk->sk_err = smc->clcsock->sk->sk_err;
  1422. sk->sk_error_report(sk);
  1423. }
  1424. if (rc)
  1425. return rc;
  1426. if (optlen < sizeof(int))
  1427. return -EINVAL;
  1428. if (get_user(val, (int __user *)optval))
  1429. return -EFAULT;
  1430. lock_sock(sk);
  1431. switch (optname) {
  1432. case TCP_ULP:
  1433. case TCP_FASTOPEN:
  1434. case TCP_FASTOPEN_CONNECT:
  1435. case TCP_FASTOPEN_KEY:
  1436. case TCP_FASTOPEN_NO_COOKIE:
  1437. /* option not supported by SMC */
  1438. if (sk->sk_state == SMC_INIT) {
  1439. smc->use_fallback = true;
  1440. smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
  1441. } else {
  1442. if (!smc->use_fallback)
  1443. rc = -EINVAL;
  1444. }
  1445. break;
  1446. case TCP_NODELAY:
  1447. if (sk->sk_state != SMC_INIT &&
  1448. sk->sk_state != SMC_LISTEN &&
  1449. sk->sk_state != SMC_CLOSED) {
  1450. if (val && !smc->use_fallback)
  1451. mod_delayed_work(system_wq, &smc->conn.tx_work,
  1452. 0);
  1453. }
  1454. break;
  1455. case TCP_CORK:
  1456. if (sk->sk_state != SMC_INIT &&
  1457. sk->sk_state != SMC_LISTEN &&
  1458. sk->sk_state != SMC_CLOSED) {
  1459. if (!val && !smc->use_fallback)
  1460. mod_delayed_work(system_wq, &smc->conn.tx_work,
  1461. 0);
  1462. }
  1463. break;
  1464. case TCP_DEFER_ACCEPT:
  1465. smc->sockopt_defer_accept = val;
  1466. break;
  1467. default:
  1468. break;
  1469. }
  1470. release_sock(sk);
  1471. return rc;
  1472. }
  1473. static int smc_getsockopt(struct socket *sock, int level, int optname,
  1474. char __user *optval, int __user *optlen)
  1475. {
  1476. struct smc_sock *smc;
  1477. smc = smc_sk(sock->sk);
  1478. /* socket options apply to the CLC socket */
  1479. return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
  1480. optval, optlen);
  1481. }
  1482. static int smc_ioctl(struct socket *sock, unsigned int cmd,
  1483. unsigned long arg)
  1484. {
  1485. union smc_host_cursor cons, urg;
  1486. struct smc_connection *conn;
  1487. struct smc_sock *smc;
  1488. int answ;
  1489. smc = smc_sk(sock->sk);
  1490. conn = &smc->conn;
  1491. lock_sock(&smc->sk);
  1492. if (smc->use_fallback) {
  1493. if (!smc->clcsock) {
  1494. release_sock(&smc->sk);
  1495. return -EBADF;
  1496. }
  1497. answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
  1498. release_sock(&smc->sk);
  1499. return answ;
  1500. }
  1501. switch (cmd) {
  1502. case SIOCINQ: /* same as FIONREAD */
  1503. if (smc->sk.sk_state == SMC_LISTEN) {
  1504. release_sock(&smc->sk);
  1505. return -EINVAL;
  1506. }
  1507. if (smc->sk.sk_state == SMC_INIT ||
  1508. smc->sk.sk_state == SMC_CLOSED)
  1509. answ = 0;
  1510. else
  1511. answ = atomic_read(&smc->conn.bytes_to_rcv);
  1512. break;
  1513. case SIOCOUTQ:
  1514. /* output queue size (not send + not acked) */
  1515. if (smc->sk.sk_state == SMC_LISTEN) {
  1516. release_sock(&smc->sk);
  1517. return -EINVAL;
  1518. }
  1519. if (smc->sk.sk_state == SMC_INIT ||
  1520. smc->sk.sk_state == SMC_CLOSED)
  1521. answ = 0;
  1522. else
  1523. answ = smc->conn.sndbuf_desc->len -
  1524. atomic_read(&smc->conn.sndbuf_space);
  1525. break;
  1526. case SIOCOUTQNSD:
  1527. /* output queue size (not send only) */
  1528. if (smc->sk.sk_state == SMC_LISTEN) {
  1529. release_sock(&smc->sk);
  1530. return -EINVAL;
  1531. }
  1532. if (smc->sk.sk_state == SMC_INIT ||
  1533. smc->sk.sk_state == SMC_CLOSED)
  1534. answ = 0;
  1535. else
  1536. answ = smc_tx_prepared_sends(&smc->conn);
  1537. break;
  1538. case SIOCATMARK:
  1539. if (smc->sk.sk_state == SMC_LISTEN) {
  1540. release_sock(&smc->sk);
  1541. return -EINVAL;
  1542. }
  1543. if (smc->sk.sk_state == SMC_INIT ||
  1544. smc->sk.sk_state == SMC_CLOSED) {
  1545. answ = 0;
  1546. } else {
  1547. smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
  1548. smc_curs_copy(&urg, &conn->urg_curs, conn);
  1549. answ = smc_curs_diff(conn->rmb_desc->len,
  1550. &cons, &urg) == 1;
  1551. }
  1552. break;
  1553. default:
  1554. release_sock(&smc->sk);
  1555. return -ENOIOCTLCMD;
  1556. }
  1557. release_sock(&smc->sk);
  1558. return put_user(answ, (int __user *)arg);
  1559. }
  1560. static ssize_t smc_sendpage(struct socket *sock, struct page *page,
  1561. int offset, size_t size, int flags)
  1562. {
  1563. struct sock *sk = sock->sk;
  1564. struct smc_sock *smc;
  1565. int rc = -EPIPE;
  1566. smc = smc_sk(sk);
  1567. lock_sock(sk);
  1568. if (sk->sk_state != SMC_ACTIVE) {
  1569. release_sock(sk);
  1570. goto out;
  1571. }
  1572. release_sock(sk);
  1573. if (smc->use_fallback)
  1574. rc = kernel_sendpage(smc->clcsock, page, offset,
  1575. size, flags);
  1576. else
  1577. rc = sock_no_sendpage(sock, page, offset, size, flags);
  1578. out:
  1579. return rc;
  1580. }
  1581. /* Map the affected portions of the rmbe into an spd, note the number of bytes
  1582. * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
  1583. * updates till whenever a respective page has been fully processed.
  1584. * Note that subsequent recv() calls have to wait till all splice() processing
  1585. * completed.
  1586. */
  1587. static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
  1588. struct pipe_inode_info *pipe, size_t len,
  1589. unsigned int flags)
  1590. {
  1591. struct sock *sk = sock->sk;
  1592. struct smc_sock *smc;
  1593. int rc = -ENOTCONN;
  1594. smc = smc_sk(sk);
  1595. lock_sock(sk);
  1596. if (sk->sk_state == SMC_INIT ||
  1597. sk->sk_state == SMC_LISTEN ||
  1598. sk->sk_state == SMC_CLOSED)
  1599. goto out;
  1600. if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
  1601. rc = 0;
  1602. goto out;
  1603. }
  1604. if (smc->use_fallback) {
  1605. rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
  1606. pipe, len, flags);
  1607. } else {
  1608. if (*ppos) {
  1609. rc = -ESPIPE;
  1610. goto out;
  1611. }
  1612. if (flags & SPLICE_F_NONBLOCK)
  1613. flags = MSG_DONTWAIT;
  1614. else
  1615. flags = 0;
  1616. rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
  1617. }
  1618. out:
  1619. release_sock(sk);
  1620. return rc;
  1621. }
  1622. /* must look like tcp */
  1623. static const struct proto_ops smc_sock_ops = {
  1624. .family = PF_SMC,
  1625. .owner = THIS_MODULE,
  1626. .release = smc_release,
  1627. .bind = smc_bind,
  1628. .connect = smc_connect,
  1629. .socketpair = sock_no_socketpair,
  1630. .accept = smc_accept,
  1631. .getname = smc_getname,
  1632. .poll = smc_poll,
  1633. .ioctl = smc_ioctl,
  1634. .listen = smc_listen,
  1635. .shutdown = smc_shutdown,
  1636. .setsockopt = smc_setsockopt,
  1637. .getsockopt = smc_getsockopt,
  1638. .sendmsg = smc_sendmsg,
  1639. .recvmsg = smc_recvmsg,
  1640. .mmap = sock_no_mmap,
  1641. .sendpage = smc_sendpage,
  1642. .splice_read = smc_splice_read,
  1643. };
  1644. static int smc_create(struct net *net, struct socket *sock, int protocol,
  1645. int kern)
  1646. {
  1647. int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
  1648. struct smc_sock *smc;
  1649. struct sock *sk;
  1650. int rc;
  1651. rc = -ESOCKTNOSUPPORT;
  1652. if (sock->type != SOCK_STREAM)
  1653. goto out;
  1654. rc = -EPROTONOSUPPORT;
  1655. if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
  1656. goto out;
  1657. rc = -ENOBUFS;
  1658. sock->ops = &smc_sock_ops;
  1659. sk = smc_sock_alloc(net, sock, protocol);
  1660. if (!sk)
  1661. goto out;
  1662. /* create internal TCP socket for CLC handshake and fallback */
  1663. smc = smc_sk(sk);
  1664. smc->use_fallback = false; /* assume rdma capability first */
  1665. smc->fallback_rsn = 0;
  1666. rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
  1667. &smc->clcsock);
  1668. if (rc) {
  1669. sk_common_release(sk);
  1670. goto out;
  1671. }
  1672. smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
  1673. smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
  1674. out:
  1675. return rc;
  1676. }
  1677. static const struct net_proto_family smc_sock_family_ops = {
  1678. .family = PF_SMC,
  1679. .owner = THIS_MODULE,
  1680. .create = smc_create,
  1681. };
  1682. static int __init smc_init(void)
  1683. {
  1684. int rc;
  1685. rc = smc_pnet_init();
  1686. if (rc)
  1687. return rc;
  1688. rc = smc_llc_init();
  1689. if (rc) {
  1690. pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
  1691. goto out_pnet;
  1692. }
  1693. rc = smc_cdc_init();
  1694. if (rc) {
  1695. pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
  1696. goto out_pnet;
  1697. }
  1698. rc = proto_register(&smc_proto, 1);
  1699. if (rc) {
  1700. pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
  1701. goto out_pnet;
  1702. }
  1703. rc = proto_register(&smc_proto6, 1);
  1704. if (rc) {
  1705. pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
  1706. goto out_proto;
  1707. }
  1708. rc = sock_register(&smc_sock_family_ops);
  1709. if (rc) {
  1710. pr_err("%s: sock_register fails with %d\n", __func__, rc);
  1711. goto out_proto6;
  1712. }
  1713. INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
  1714. INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
  1715. rc = smc_ib_register_client();
  1716. if (rc) {
  1717. pr_err("%s: ib_register fails with %d\n", __func__, rc);
  1718. goto out_sock;
  1719. }
  1720. static_branch_enable(&tcp_have_smc);
  1721. return 0;
  1722. out_sock:
  1723. sock_unregister(PF_SMC);
  1724. out_proto6:
  1725. proto_unregister(&smc_proto6);
  1726. out_proto:
  1727. proto_unregister(&smc_proto);
  1728. out_pnet:
  1729. smc_pnet_exit();
  1730. return rc;
  1731. }
  1732. static void __exit smc_exit(void)
  1733. {
  1734. smc_core_exit();
  1735. static_branch_disable(&tcp_have_smc);
  1736. smc_ib_unregister_client();
  1737. sock_unregister(PF_SMC);
  1738. proto_unregister(&smc_proto6);
  1739. proto_unregister(&smc_proto);
  1740. smc_pnet_exit();
  1741. }
  1742. module_init(smc_init);
  1743. module_exit(smc_exit);
  1744. MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
  1745. MODULE_DESCRIPTION("smc socket address family");
  1746. MODULE_LICENSE("GPL");
  1747. MODULE_ALIAS_NETPROTO(PF_SMC);