af_smc.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  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 in_device *in_dev;
  244. struct sockaddr_in addr;
  245. int rc = -ENOENT;
  246. int len;
  247. if (!dst) {
  248. rc = -ENOTCONN;
  249. goto out;
  250. }
  251. if (!dst->dev) {
  252. rc = -ENODEV;
  253. goto out_rel;
  254. }
  255. /* get address to which the internal TCP socket is bound */
  256. kernel_getsockname(clcsock, (struct sockaddr *)&addr, &len);
  257. /* analyze IPv4 specific data of net_device belonging to TCP socket */
  258. rcu_read_lock();
  259. in_dev = __in_dev_get_rcu(dst->dev);
  260. for_ifa(in_dev) {
  261. if (!inet_ifa_match(addr.sin_addr.s_addr, ifa))
  262. continue;
  263. *prefix_len = inet_mask_len(ifa->ifa_mask);
  264. *subnet = ifa->ifa_address & ifa->ifa_mask;
  265. rc = 0;
  266. break;
  267. } endfor_ifa(in_dev);
  268. rcu_read_unlock();
  269. out_rel:
  270. dst_release(dst);
  271. out:
  272. return rc;
  273. }
  274. static int smc_clnt_conf_first_link(struct smc_sock *smc, union ib_gid *gid)
  275. {
  276. struct smc_link_group *lgr = smc->conn.lgr;
  277. struct smc_link *link;
  278. int rest;
  279. int rc;
  280. link = &lgr->lnk[SMC_SINGLE_LINK];
  281. /* receive CONFIRM LINK request from server over RoCE fabric */
  282. rest = wait_for_completion_interruptible_timeout(
  283. &link->llc_confirm,
  284. SMC_LLC_WAIT_FIRST_TIME);
  285. if (rest <= 0) {
  286. struct smc_clc_msg_decline dclc;
  287. rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
  288. SMC_CLC_DECLINE);
  289. return rc;
  290. }
  291. rc = smc_ib_modify_qp_rts(link);
  292. if (rc)
  293. return SMC_CLC_DECL_INTERR;
  294. smc_wr_remember_qp_attr(link);
  295. rc = smc_wr_reg_send(link,
  296. smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]);
  297. if (rc)
  298. return SMC_CLC_DECL_INTERR;
  299. /* send CONFIRM LINK response over RoCE fabric */
  300. rc = smc_llc_send_confirm_link(link,
  301. link->smcibdev->mac[link->ibport - 1],
  302. gid, SMC_LLC_RESP);
  303. if (rc < 0)
  304. return SMC_CLC_DECL_TCL;
  305. return rc;
  306. }
  307. static void smc_conn_save_peer_info(struct smc_sock *smc,
  308. struct smc_clc_msg_accept_confirm *clc)
  309. {
  310. smc->conn.peer_conn_idx = clc->conn_idx;
  311. smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
  312. smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size);
  313. atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
  314. }
  315. static void smc_link_save_peer_info(struct smc_link *link,
  316. struct smc_clc_msg_accept_confirm *clc)
  317. {
  318. link->peer_qpn = ntoh24(clc->qpn);
  319. memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
  320. memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
  321. link->peer_psn = ntoh24(clc->psn);
  322. link->peer_mtu = clc->qp_mtu;
  323. }
  324. /* setup for RDMA connection of client */
  325. static int smc_connect_rdma(struct smc_sock *smc)
  326. {
  327. struct sockaddr_in *inaddr = (struct sockaddr_in *)smc->addr;
  328. struct smc_clc_msg_accept_confirm aclc;
  329. int local_contact = SMC_FIRST_CONTACT;
  330. struct smc_ib_device *smcibdev;
  331. struct smc_link *link;
  332. u8 srv_first_contact;
  333. int reason_code = 0;
  334. int rc = 0;
  335. u8 ibport;
  336. /* IPSec connections opt out of SMC-R optimizations */
  337. if (using_ipsec(smc)) {
  338. reason_code = SMC_CLC_DECL_IPSEC;
  339. goto decline_rdma;
  340. }
  341. /* PNET table look up: search active ib_device and port
  342. * within same PNETID that also contains the ethernet device
  343. * used for the internal TCP socket
  344. */
  345. smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
  346. if (!smcibdev) {
  347. reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
  348. goto decline_rdma;
  349. }
  350. /* do inband token exchange */
  351. reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
  352. if (reason_code < 0) {
  353. rc = reason_code;
  354. goto out_err;
  355. }
  356. if (reason_code > 0) /* configuration error */
  357. goto decline_rdma;
  358. /* receive SMC Accept CLC message */
  359. reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
  360. SMC_CLC_ACCEPT);
  361. if (reason_code < 0) {
  362. rc = reason_code;
  363. goto out_err;
  364. }
  365. if (reason_code > 0)
  366. goto decline_rdma;
  367. srv_first_contact = aclc.hdr.flag;
  368. mutex_lock(&smc_create_lgr_pending);
  369. local_contact = smc_conn_create(smc, inaddr->sin_addr.s_addr, smcibdev,
  370. ibport, &aclc.lcl, srv_first_contact);
  371. if (local_contact < 0) {
  372. rc = local_contact;
  373. if (rc == -ENOMEM)
  374. reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
  375. else if (rc == -ENOLINK)
  376. reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
  377. goto decline_rdma_unlock;
  378. }
  379. link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
  380. smc_conn_save_peer_info(smc, &aclc);
  381. /* create send buffer and rmb */
  382. rc = smc_buf_create(smc);
  383. if (rc) {
  384. reason_code = SMC_CLC_DECL_MEM;
  385. goto decline_rdma_unlock;
  386. }
  387. if (local_contact == SMC_FIRST_CONTACT)
  388. smc_link_save_peer_info(link, &aclc);
  389. rc = smc_rmb_rtoken_handling(&smc->conn, &aclc);
  390. if (rc) {
  391. reason_code = SMC_CLC_DECL_INTERR;
  392. goto decline_rdma_unlock;
  393. }
  394. smc_close_init(smc);
  395. smc_rx_init(smc);
  396. if (local_contact == SMC_FIRST_CONTACT) {
  397. rc = smc_ib_ready_link(link);
  398. if (rc) {
  399. reason_code = SMC_CLC_DECL_INTERR;
  400. goto decline_rdma_unlock;
  401. }
  402. } else {
  403. struct smc_buf_desc *buf_desc = smc->conn.rmb_desc;
  404. if (!buf_desc->reused) {
  405. /* register memory region for new rmb */
  406. rc = smc_wr_reg_send(link,
  407. buf_desc->mr_rx[SMC_SINGLE_LINK]);
  408. if (rc) {
  409. reason_code = SMC_CLC_DECL_INTERR;
  410. goto decline_rdma_unlock;
  411. }
  412. }
  413. }
  414. smc_rmb_sync_sg_for_device(&smc->conn);
  415. rc = smc_clc_send_confirm(smc);
  416. if (rc)
  417. goto out_err_unlock;
  418. if (local_contact == SMC_FIRST_CONTACT) {
  419. /* QP confirmation over RoCE fabric */
  420. reason_code = smc_clnt_conf_first_link(
  421. smc, &smcibdev->gid[ibport - 1]);
  422. if (reason_code < 0) {
  423. rc = reason_code;
  424. goto out_err_unlock;
  425. }
  426. if (reason_code > 0)
  427. goto decline_rdma_unlock;
  428. }
  429. mutex_unlock(&smc_create_lgr_pending);
  430. smc_tx_init(smc);
  431. out_connected:
  432. smc_copy_sock_settings_to_clc(smc);
  433. if (smc->sk.sk_state == SMC_INIT)
  434. smc->sk.sk_state = SMC_ACTIVE;
  435. return rc ? rc : local_contact;
  436. decline_rdma_unlock:
  437. mutex_unlock(&smc_create_lgr_pending);
  438. smc_conn_free(&smc->conn);
  439. decline_rdma:
  440. /* RDMA setup failed, switch back to TCP */
  441. smc->use_fallback = true;
  442. if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
  443. rc = smc_clc_send_decline(smc, reason_code);
  444. if (rc < sizeof(struct smc_clc_msg_decline))
  445. goto out_err;
  446. }
  447. goto out_connected;
  448. out_err_unlock:
  449. mutex_unlock(&smc_create_lgr_pending);
  450. smc_conn_free(&smc->conn);
  451. out_err:
  452. return rc;
  453. }
  454. static int smc_connect(struct socket *sock, struct sockaddr *addr,
  455. int alen, int flags)
  456. {
  457. struct sock *sk = sock->sk;
  458. struct smc_sock *smc;
  459. int rc = -EINVAL;
  460. smc = smc_sk(sk);
  461. /* separate smc parameter checking to be safe */
  462. if (alen < sizeof(addr->sa_family))
  463. goto out_err;
  464. if (addr->sa_family != AF_INET)
  465. goto out_err;
  466. smc->addr = addr; /* needed for nonblocking connect */
  467. lock_sock(sk);
  468. switch (sk->sk_state) {
  469. default:
  470. goto out;
  471. case SMC_ACTIVE:
  472. rc = -EISCONN;
  473. goto out;
  474. case SMC_INIT:
  475. rc = 0;
  476. break;
  477. }
  478. smc_copy_sock_settings_to_clc(smc);
  479. rc = kernel_connect(smc->clcsock, addr, alen, flags);
  480. if (rc)
  481. goto out;
  482. /* setup RDMA connection */
  483. rc = smc_connect_rdma(smc);
  484. if (rc < 0)
  485. goto out;
  486. else
  487. rc = 0; /* success cases including fallback */
  488. out:
  489. release_sock(sk);
  490. out_err:
  491. return rc;
  492. }
  493. static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
  494. {
  495. struct sock *sk = &lsmc->sk;
  496. struct socket *new_clcsock;
  497. struct sock *new_sk;
  498. int rc;
  499. release_sock(&lsmc->sk);
  500. new_sk = smc_sock_alloc(sock_net(sk), NULL);
  501. if (!new_sk) {
  502. rc = -ENOMEM;
  503. lsmc->sk.sk_err = ENOMEM;
  504. *new_smc = NULL;
  505. lock_sock(&lsmc->sk);
  506. goto out;
  507. }
  508. *new_smc = smc_sk(new_sk);
  509. rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
  510. lock_sock(&lsmc->sk);
  511. if (rc < 0) {
  512. lsmc->sk.sk_err = -rc;
  513. new_sk->sk_state = SMC_CLOSED;
  514. sock_set_flag(new_sk, SOCK_DEAD);
  515. sk->sk_prot->unhash(new_sk);
  516. sock_put(new_sk);
  517. *new_smc = NULL;
  518. goto out;
  519. }
  520. if (lsmc->sk.sk_state == SMC_CLOSED) {
  521. if (new_clcsock)
  522. sock_release(new_clcsock);
  523. new_sk->sk_state = SMC_CLOSED;
  524. sock_set_flag(new_sk, SOCK_DEAD);
  525. sk->sk_prot->unhash(new_sk);
  526. sock_put(new_sk);
  527. *new_smc = NULL;
  528. goto out;
  529. }
  530. (*new_smc)->clcsock = new_clcsock;
  531. out:
  532. return rc;
  533. }
  534. /* add a just created sock to the accept queue of the listen sock as
  535. * candidate for a following socket accept call from user space
  536. */
  537. static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
  538. {
  539. struct smc_sock *par = smc_sk(parent);
  540. sock_hold(sk);
  541. spin_lock(&par->accept_q_lock);
  542. list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
  543. spin_unlock(&par->accept_q_lock);
  544. sk_acceptq_added(parent);
  545. }
  546. /* remove a socket from the accept queue of its parental listening socket */
  547. static void smc_accept_unlink(struct sock *sk)
  548. {
  549. struct smc_sock *par = smc_sk(sk)->listen_smc;
  550. spin_lock(&par->accept_q_lock);
  551. list_del_init(&smc_sk(sk)->accept_q);
  552. spin_unlock(&par->accept_q_lock);
  553. sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
  554. sock_put(sk);
  555. }
  556. /* remove a sock from the accept queue to bind it to a new socket created
  557. * for a socket accept call from user space
  558. */
  559. struct sock *smc_accept_dequeue(struct sock *parent,
  560. struct socket *new_sock)
  561. {
  562. struct smc_sock *isk, *n;
  563. struct sock *new_sk;
  564. list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
  565. new_sk = (struct sock *)isk;
  566. smc_accept_unlink(new_sk);
  567. if (new_sk->sk_state == SMC_CLOSED) {
  568. new_sk->sk_prot->unhash(new_sk);
  569. sock_put(new_sk);
  570. continue;
  571. }
  572. if (new_sock)
  573. sock_graft(new_sk, new_sock);
  574. return new_sk;
  575. }
  576. return NULL;
  577. }
  578. /* clean up for a created but never accepted sock */
  579. void smc_close_non_accepted(struct sock *sk)
  580. {
  581. struct smc_sock *smc = smc_sk(sk);
  582. sock_hold(sk);
  583. lock_sock(sk);
  584. if (!sk->sk_lingertime)
  585. /* wait for peer closing */
  586. sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
  587. if (smc->use_fallback) {
  588. sk->sk_state = SMC_CLOSED;
  589. } else {
  590. smc_close_active(smc);
  591. sock_set_flag(sk, SOCK_DEAD);
  592. sk->sk_shutdown |= SHUTDOWN_MASK;
  593. }
  594. if (smc->clcsock) {
  595. struct socket *tcp;
  596. tcp = smc->clcsock;
  597. smc->clcsock = NULL;
  598. sock_release(tcp);
  599. }
  600. if (smc->use_fallback) {
  601. schedule_delayed_work(&smc->sock_put_work, TCP_TIMEWAIT_LEN);
  602. } else if (sk->sk_state == SMC_CLOSED) {
  603. smc_conn_free(&smc->conn);
  604. schedule_delayed_work(&smc->sock_put_work,
  605. SMC_CLOSE_SOCK_PUT_DELAY);
  606. }
  607. release_sock(sk);
  608. sock_put(sk);
  609. }
  610. static int smc_serv_conf_first_link(struct smc_sock *smc)
  611. {
  612. struct smc_link_group *lgr = smc->conn.lgr;
  613. struct smc_link *link;
  614. int rest;
  615. int rc;
  616. link = &lgr->lnk[SMC_SINGLE_LINK];
  617. rc = smc_wr_reg_send(link,
  618. smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]);
  619. if (rc)
  620. return SMC_CLC_DECL_INTERR;
  621. /* send CONFIRM LINK request to client over the RoCE fabric */
  622. rc = smc_llc_send_confirm_link(link,
  623. link->smcibdev->mac[link->ibport - 1],
  624. &link->smcibdev->gid[link->ibport - 1],
  625. SMC_LLC_REQ);
  626. if (rc < 0)
  627. return SMC_CLC_DECL_TCL;
  628. /* receive CONFIRM LINK response from client over the RoCE fabric */
  629. rest = wait_for_completion_interruptible_timeout(
  630. &link->llc_confirm_resp,
  631. SMC_LLC_WAIT_FIRST_TIME);
  632. if (rest <= 0) {
  633. struct smc_clc_msg_decline dclc;
  634. rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
  635. SMC_CLC_DECLINE);
  636. }
  637. return rc;
  638. }
  639. /* setup for RDMA connection of server */
  640. static void smc_listen_work(struct work_struct *work)
  641. {
  642. struct smc_sock *new_smc = container_of(work, struct smc_sock,
  643. smc_listen_work);
  644. struct socket *newclcsock = new_smc->clcsock;
  645. struct smc_sock *lsmc = new_smc->listen_smc;
  646. struct smc_clc_msg_accept_confirm cclc;
  647. int local_contact = SMC_REUSE_CONTACT;
  648. struct sock *newsmcsk = &new_smc->sk;
  649. struct smc_clc_msg_proposal pclc;
  650. struct smc_ib_device *smcibdev;
  651. struct sockaddr_in peeraddr;
  652. struct smc_link *link;
  653. int reason_code = 0;
  654. int rc = 0, len;
  655. __be32 subnet;
  656. u8 prefix_len;
  657. u8 ibport;
  658. /* do inband token exchange -
  659. *wait for and receive SMC Proposal CLC message
  660. */
  661. reason_code = smc_clc_wait_msg(new_smc, &pclc, sizeof(pclc),
  662. SMC_CLC_PROPOSAL);
  663. if (reason_code < 0)
  664. goto out_err;
  665. if (reason_code > 0)
  666. goto decline_rdma;
  667. /* IPSec connections opt out of SMC-R optimizations */
  668. if (using_ipsec(new_smc)) {
  669. reason_code = SMC_CLC_DECL_IPSEC;
  670. goto decline_rdma;
  671. }
  672. /* PNET table look up: search active ib_device and port
  673. * within same PNETID that also contains the ethernet device
  674. * used for the internal TCP socket
  675. */
  676. smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
  677. if (!smcibdev) {
  678. reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
  679. goto decline_rdma;
  680. }
  681. /* determine subnet and mask from internal TCP socket */
  682. rc = smc_netinfo_by_tcpsk(newclcsock, &subnet, &prefix_len);
  683. if (rc) {
  684. reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
  685. goto decline_rdma;
  686. }
  687. if ((pclc.outgoing_subnet != subnet) ||
  688. (pclc.prefix_len != prefix_len)) {
  689. reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
  690. goto decline_rdma;
  691. }
  692. /* get address of the peer connected to the internal TCP socket */
  693. kernel_getpeername(newclcsock, (struct sockaddr *)&peeraddr, &len);
  694. /* allocate connection / link group */
  695. mutex_lock(&smc_create_lgr_pending);
  696. local_contact = smc_conn_create(new_smc, peeraddr.sin_addr.s_addr,
  697. smcibdev, ibport, &pclc.lcl, 0);
  698. if (local_contact < 0) {
  699. rc = local_contact;
  700. if (rc == -ENOMEM)
  701. reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
  702. goto decline_rdma;
  703. }
  704. link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
  705. /* create send buffer and rmb */
  706. rc = smc_buf_create(new_smc);
  707. if (rc) {
  708. reason_code = SMC_CLC_DECL_MEM;
  709. goto decline_rdma;
  710. }
  711. smc_close_init(new_smc);
  712. smc_rx_init(new_smc);
  713. if (local_contact != SMC_FIRST_CONTACT) {
  714. struct smc_buf_desc *buf_desc = new_smc->conn.rmb_desc;
  715. if (!buf_desc->reused) {
  716. /* register memory region for new rmb */
  717. rc = smc_wr_reg_send(link,
  718. buf_desc->mr_rx[SMC_SINGLE_LINK]);
  719. if (rc) {
  720. reason_code = SMC_CLC_DECL_INTERR;
  721. goto decline_rdma;
  722. }
  723. }
  724. }
  725. smc_rmb_sync_sg_for_device(&new_smc->conn);
  726. rc = smc_clc_send_accept(new_smc, local_contact);
  727. if (rc)
  728. goto out_err;
  729. /* receive SMC Confirm CLC message */
  730. reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
  731. SMC_CLC_CONFIRM);
  732. if (reason_code < 0)
  733. goto out_err;
  734. if (reason_code > 0)
  735. goto decline_rdma;
  736. smc_conn_save_peer_info(new_smc, &cclc);
  737. if (local_contact == SMC_FIRST_CONTACT)
  738. smc_link_save_peer_info(link, &cclc);
  739. rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc);
  740. if (rc) {
  741. reason_code = SMC_CLC_DECL_INTERR;
  742. goto decline_rdma;
  743. }
  744. if (local_contact == SMC_FIRST_CONTACT) {
  745. rc = smc_ib_ready_link(link);
  746. if (rc) {
  747. reason_code = SMC_CLC_DECL_INTERR;
  748. goto decline_rdma;
  749. }
  750. /* QP confirmation over RoCE fabric */
  751. reason_code = smc_serv_conf_first_link(new_smc);
  752. if (reason_code < 0) {
  753. /* peer is not aware of a problem */
  754. rc = reason_code;
  755. goto out_err;
  756. }
  757. if (reason_code > 0)
  758. goto decline_rdma;
  759. }
  760. smc_tx_init(new_smc);
  761. out_connected:
  762. sk_refcnt_debug_inc(newsmcsk);
  763. if (newsmcsk->sk_state == SMC_INIT)
  764. newsmcsk->sk_state = SMC_ACTIVE;
  765. enqueue:
  766. mutex_unlock(&smc_create_lgr_pending);
  767. lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
  768. if (lsmc->sk.sk_state == SMC_LISTEN) {
  769. smc_accept_enqueue(&lsmc->sk, newsmcsk);
  770. } else { /* no longer listening */
  771. smc_close_non_accepted(newsmcsk);
  772. }
  773. release_sock(&lsmc->sk);
  774. /* Wake up accept */
  775. lsmc->sk.sk_data_ready(&lsmc->sk);
  776. sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
  777. return;
  778. decline_rdma:
  779. /* RDMA setup failed, switch back to TCP */
  780. smc_conn_free(&new_smc->conn);
  781. new_smc->use_fallback = true;
  782. if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
  783. rc = smc_clc_send_decline(new_smc, reason_code);
  784. if (rc < sizeof(struct smc_clc_msg_decline))
  785. goto out_err;
  786. }
  787. goto out_connected;
  788. out_err:
  789. newsmcsk->sk_state = SMC_CLOSED;
  790. smc_conn_free(&new_smc->conn);
  791. goto enqueue; /* queue new sock with sk_err set */
  792. }
  793. static void smc_tcp_listen_work(struct work_struct *work)
  794. {
  795. struct smc_sock *lsmc = container_of(work, struct smc_sock,
  796. tcp_listen_work);
  797. struct smc_sock *new_smc;
  798. int rc = 0;
  799. lock_sock(&lsmc->sk);
  800. while (lsmc->sk.sk_state == SMC_LISTEN) {
  801. rc = smc_clcsock_accept(lsmc, &new_smc);
  802. if (rc)
  803. goto out;
  804. if (!new_smc)
  805. continue;
  806. new_smc->listen_smc = lsmc;
  807. new_smc->use_fallback = false; /* assume rdma capability first*/
  808. sock_hold(&lsmc->sk); /* sock_put in smc_listen_work */
  809. INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
  810. smc_copy_sock_settings_to_smc(new_smc);
  811. schedule_work(&new_smc->smc_listen_work);
  812. }
  813. out:
  814. release_sock(&lsmc->sk);
  815. lsmc->sk.sk_data_ready(&lsmc->sk); /* no more listening, wake accept */
  816. }
  817. static int smc_listen(struct socket *sock, int backlog)
  818. {
  819. struct sock *sk = sock->sk;
  820. struct smc_sock *smc;
  821. int rc;
  822. smc = smc_sk(sk);
  823. lock_sock(sk);
  824. rc = -EINVAL;
  825. if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
  826. goto out;
  827. rc = 0;
  828. if (sk->sk_state == SMC_LISTEN) {
  829. sk->sk_max_ack_backlog = backlog;
  830. goto out;
  831. }
  832. /* some socket options are handled in core, so we could not apply
  833. * them to the clc socket -- copy smc socket options to clc socket
  834. */
  835. smc_copy_sock_settings_to_clc(smc);
  836. rc = kernel_listen(smc->clcsock, backlog);
  837. if (rc)
  838. goto out;
  839. sk->sk_max_ack_backlog = backlog;
  840. sk->sk_ack_backlog = 0;
  841. sk->sk_state = SMC_LISTEN;
  842. INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
  843. schedule_work(&smc->tcp_listen_work);
  844. out:
  845. release_sock(sk);
  846. return rc;
  847. }
  848. static int smc_accept(struct socket *sock, struct socket *new_sock,
  849. int flags, bool kern)
  850. {
  851. struct sock *sk = sock->sk, *nsk;
  852. DECLARE_WAITQUEUE(wait, current);
  853. struct smc_sock *lsmc;
  854. long timeo;
  855. int rc = 0;
  856. lsmc = smc_sk(sk);
  857. lock_sock(sk);
  858. if (lsmc->sk.sk_state != SMC_LISTEN) {
  859. rc = -EINVAL;
  860. goto out;
  861. }
  862. /* Wait for an incoming connection */
  863. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  864. add_wait_queue_exclusive(sk_sleep(sk), &wait);
  865. while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
  866. set_current_state(TASK_INTERRUPTIBLE);
  867. if (!timeo) {
  868. rc = -EAGAIN;
  869. break;
  870. }
  871. release_sock(sk);
  872. timeo = schedule_timeout(timeo);
  873. /* wakeup by sk_data_ready in smc_listen_work() */
  874. sched_annotate_sleep();
  875. lock_sock(sk);
  876. if (signal_pending(current)) {
  877. rc = sock_intr_errno(timeo);
  878. break;
  879. }
  880. }
  881. set_current_state(TASK_RUNNING);
  882. remove_wait_queue(sk_sleep(sk), &wait);
  883. if (!rc)
  884. rc = sock_error(nsk);
  885. out:
  886. release_sock(sk);
  887. return rc;
  888. }
  889. static int smc_getname(struct socket *sock, struct sockaddr *addr,
  890. int *len, int peer)
  891. {
  892. struct smc_sock *smc;
  893. if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
  894. (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
  895. return -ENOTCONN;
  896. smc = smc_sk(sock->sk);
  897. return smc->clcsock->ops->getname(smc->clcsock, addr, len, peer);
  898. }
  899. static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  900. {
  901. struct sock *sk = sock->sk;
  902. struct smc_sock *smc;
  903. int rc = -EPIPE;
  904. smc = smc_sk(sk);
  905. lock_sock(sk);
  906. if ((sk->sk_state != SMC_ACTIVE) &&
  907. (sk->sk_state != SMC_APPCLOSEWAIT1) &&
  908. (sk->sk_state != SMC_INIT))
  909. goto out;
  910. if (smc->use_fallback)
  911. rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
  912. else
  913. rc = smc_tx_sendmsg(smc, msg, len);
  914. out:
  915. release_sock(sk);
  916. return rc;
  917. }
  918. static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  919. int flags)
  920. {
  921. struct sock *sk = sock->sk;
  922. struct smc_sock *smc;
  923. int rc = -ENOTCONN;
  924. smc = smc_sk(sk);
  925. lock_sock(sk);
  926. if ((sk->sk_state == SMC_INIT) ||
  927. (sk->sk_state == SMC_LISTEN) ||
  928. (sk->sk_state == SMC_CLOSED))
  929. goto out;
  930. if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
  931. rc = 0;
  932. goto out;
  933. }
  934. if (smc->use_fallback)
  935. rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
  936. else
  937. rc = smc_rx_recvmsg(smc, msg, len, flags);
  938. out:
  939. release_sock(sk);
  940. return rc;
  941. }
  942. static unsigned int smc_accept_poll(struct sock *parent)
  943. {
  944. struct smc_sock *isk;
  945. struct sock *sk;
  946. lock_sock(parent);
  947. list_for_each_entry(isk, &smc_sk(parent)->accept_q, accept_q) {
  948. sk = (struct sock *)isk;
  949. if (sk->sk_state == SMC_ACTIVE) {
  950. release_sock(parent);
  951. return POLLIN | POLLRDNORM;
  952. }
  953. }
  954. release_sock(parent);
  955. return 0;
  956. }
  957. static unsigned int smc_poll(struct file *file, struct socket *sock,
  958. poll_table *wait)
  959. {
  960. struct sock *sk = sock->sk;
  961. unsigned int mask = 0;
  962. struct smc_sock *smc;
  963. int rc;
  964. smc = smc_sk(sock->sk);
  965. if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
  966. /* delegate to CLC child sock */
  967. mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
  968. /* if non-blocking connect finished ... */
  969. lock_sock(sk);
  970. if ((sk->sk_state == SMC_INIT) && (mask & POLLOUT)) {
  971. sk->sk_err = smc->clcsock->sk->sk_err;
  972. if (sk->sk_err) {
  973. mask |= POLLERR;
  974. } else {
  975. rc = smc_connect_rdma(smc);
  976. if (rc < 0)
  977. mask |= POLLERR;
  978. else
  979. /* success cases including fallback */
  980. mask |= POLLOUT | POLLWRNORM;
  981. }
  982. }
  983. release_sock(sk);
  984. } else {
  985. sock_poll_wait(file, sk_sleep(sk), wait);
  986. if (sk->sk_state == SMC_LISTEN)
  987. /* woken up by sk_data_ready in smc_listen_work() */
  988. mask |= smc_accept_poll(sk);
  989. if (sk->sk_err)
  990. mask |= POLLERR;
  991. if (atomic_read(&smc->conn.sndbuf_space) ||
  992. (sk->sk_shutdown & SEND_SHUTDOWN)) {
  993. mask |= POLLOUT | POLLWRNORM;
  994. } else {
  995. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  996. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  997. }
  998. if (atomic_read(&smc->conn.bytes_to_rcv))
  999. mask |= POLLIN | POLLRDNORM;
  1000. if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
  1001. (sk->sk_state == SMC_CLOSED))
  1002. mask |= POLLHUP;
  1003. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1004. mask |= POLLIN | POLLRDNORM | POLLRDHUP;
  1005. if (sk->sk_state == SMC_APPCLOSEWAIT1)
  1006. mask |= POLLIN;
  1007. }
  1008. return mask;
  1009. }
  1010. static int smc_shutdown(struct socket *sock, int how)
  1011. {
  1012. struct sock *sk = sock->sk;
  1013. struct smc_sock *smc;
  1014. int rc = -EINVAL;
  1015. int rc1 = 0;
  1016. smc = smc_sk(sk);
  1017. if ((how < SHUT_RD) || (how > SHUT_RDWR))
  1018. return rc;
  1019. lock_sock(sk);
  1020. rc = -ENOTCONN;
  1021. if ((sk->sk_state != SMC_LISTEN) &&
  1022. (sk->sk_state != SMC_ACTIVE) &&
  1023. (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
  1024. (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
  1025. (sk->sk_state != SMC_APPCLOSEWAIT1) &&
  1026. (sk->sk_state != SMC_APPCLOSEWAIT2) &&
  1027. (sk->sk_state != SMC_APPFINCLOSEWAIT))
  1028. goto out;
  1029. if (smc->use_fallback) {
  1030. rc = kernel_sock_shutdown(smc->clcsock, how);
  1031. sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
  1032. if (sk->sk_shutdown == SHUTDOWN_MASK)
  1033. sk->sk_state = SMC_CLOSED;
  1034. goto out;
  1035. }
  1036. switch (how) {
  1037. case SHUT_RDWR: /* shutdown in both directions */
  1038. rc = smc_close_active(smc);
  1039. break;
  1040. case SHUT_WR:
  1041. rc = smc_close_shutdown_write(smc);
  1042. break;
  1043. case SHUT_RD:
  1044. if (sk->sk_state == SMC_LISTEN)
  1045. rc = smc_close_active(smc);
  1046. else
  1047. rc = 0;
  1048. /* nothing more to do because peer is not involved */
  1049. break;
  1050. }
  1051. rc1 = kernel_sock_shutdown(smc->clcsock, how);
  1052. /* map sock_shutdown_cmd constants to sk_shutdown value range */
  1053. sk->sk_shutdown |= how + 1;
  1054. out:
  1055. release_sock(sk);
  1056. return rc ? rc : rc1;
  1057. }
  1058. static int smc_setsockopt(struct socket *sock, int level, int optname,
  1059. char __user *optval, unsigned int optlen)
  1060. {
  1061. struct sock *sk = sock->sk;
  1062. struct smc_sock *smc;
  1063. smc = smc_sk(sk);
  1064. /* generic setsockopts reaching us here always apply to the
  1065. * CLC socket
  1066. */
  1067. return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
  1068. optval, optlen);
  1069. }
  1070. static int smc_getsockopt(struct socket *sock, int level, int optname,
  1071. char __user *optval, int __user *optlen)
  1072. {
  1073. struct smc_sock *smc;
  1074. smc = smc_sk(sock->sk);
  1075. /* socket options apply to the CLC socket */
  1076. return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
  1077. optval, optlen);
  1078. }
  1079. static int smc_ioctl(struct socket *sock, unsigned int cmd,
  1080. unsigned long arg)
  1081. {
  1082. struct smc_sock *smc;
  1083. smc = smc_sk(sock->sk);
  1084. if (smc->use_fallback)
  1085. return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
  1086. else
  1087. return sock_no_ioctl(sock, cmd, arg);
  1088. }
  1089. static ssize_t smc_sendpage(struct socket *sock, struct page *page,
  1090. int offset, size_t size, int flags)
  1091. {
  1092. struct sock *sk = sock->sk;
  1093. struct smc_sock *smc;
  1094. int rc = -EPIPE;
  1095. smc = smc_sk(sk);
  1096. lock_sock(sk);
  1097. if (sk->sk_state != SMC_ACTIVE)
  1098. goto out;
  1099. if (smc->use_fallback)
  1100. rc = kernel_sendpage(smc->clcsock, page, offset,
  1101. size, flags);
  1102. else
  1103. rc = sock_no_sendpage(sock, page, offset, size, flags);
  1104. out:
  1105. release_sock(sk);
  1106. return rc;
  1107. }
  1108. static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
  1109. struct pipe_inode_info *pipe, size_t len,
  1110. unsigned int flags)
  1111. {
  1112. struct sock *sk = sock->sk;
  1113. struct smc_sock *smc;
  1114. int rc = -ENOTCONN;
  1115. smc = smc_sk(sk);
  1116. lock_sock(sk);
  1117. if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
  1118. goto out;
  1119. if (smc->use_fallback) {
  1120. rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
  1121. pipe, len, flags);
  1122. } else {
  1123. rc = -EOPNOTSUPP;
  1124. }
  1125. out:
  1126. release_sock(sk);
  1127. return rc;
  1128. }
  1129. /* must look like tcp */
  1130. static const struct proto_ops smc_sock_ops = {
  1131. .family = PF_SMC,
  1132. .owner = THIS_MODULE,
  1133. .release = smc_release,
  1134. .bind = smc_bind,
  1135. .connect = smc_connect,
  1136. .socketpair = sock_no_socketpair,
  1137. .accept = smc_accept,
  1138. .getname = smc_getname,
  1139. .poll = smc_poll,
  1140. .ioctl = smc_ioctl,
  1141. .listen = smc_listen,
  1142. .shutdown = smc_shutdown,
  1143. .setsockopt = smc_setsockopt,
  1144. .getsockopt = smc_getsockopt,
  1145. .sendmsg = smc_sendmsg,
  1146. .recvmsg = smc_recvmsg,
  1147. .mmap = sock_no_mmap,
  1148. .sendpage = smc_sendpage,
  1149. .splice_read = smc_splice_read,
  1150. };
  1151. static int smc_create(struct net *net, struct socket *sock, int protocol,
  1152. int kern)
  1153. {
  1154. struct smc_sock *smc;
  1155. struct sock *sk;
  1156. int rc;
  1157. rc = -ESOCKTNOSUPPORT;
  1158. if (sock->type != SOCK_STREAM)
  1159. goto out;
  1160. rc = -EPROTONOSUPPORT;
  1161. if ((protocol != IPPROTO_IP) && (protocol != IPPROTO_TCP))
  1162. goto out;
  1163. rc = -ENOBUFS;
  1164. sock->ops = &smc_sock_ops;
  1165. sk = smc_sock_alloc(net, sock);
  1166. if (!sk)
  1167. goto out;
  1168. /* create internal TCP socket for CLC handshake and fallback */
  1169. smc = smc_sk(sk);
  1170. smc->use_fallback = false; /* assume rdma capability first */
  1171. rc = sock_create_kern(net, PF_INET, SOCK_STREAM,
  1172. IPPROTO_TCP, &smc->clcsock);
  1173. if (rc)
  1174. sk_common_release(sk);
  1175. smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
  1176. smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
  1177. out:
  1178. return rc;
  1179. }
  1180. static const struct net_proto_family smc_sock_family_ops = {
  1181. .family = PF_SMC,
  1182. .owner = THIS_MODULE,
  1183. .create = smc_create,
  1184. };
  1185. static int __init smc_init(void)
  1186. {
  1187. int rc;
  1188. rc = smc_pnet_init();
  1189. if (rc)
  1190. return rc;
  1191. rc = smc_llc_init();
  1192. if (rc) {
  1193. pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
  1194. goto out_pnet;
  1195. }
  1196. rc = smc_cdc_init();
  1197. if (rc) {
  1198. pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
  1199. goto out_pnet;
  1200. }
  1201. rc = proto_register(&smc_proto, 1);
  1202. if (rc) {
  1203. pr_err("%s: proto_register fails with %d\n", __func__, rc);
  1204. goto out_pnet;
  1205. }
  1206. rc = sock_register(&smc_sock_family_ops);
  1207. if (rc) {
  1208. pr_err("%s: sock_register fails with %d\n", __func__, rc);
  1209. goto out_proto;
  1210. }
  1211. INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
  1212. rc = smc_ib_register_client();
  1213. if (rc) {
  1214. pr_err("%s: ib_register fails with %d\n", __func__, rc);
  1215. goto out_sock;
  1216. }
  1217. return 0;
  1218. out_sock:
  1219. sock_unregister(PF_SMC);
  1220. out_proto:
  1221. proto_unregister(&smc_proto);
  1222. out_pnet:
  1223. smc_pnet_exit();
  1224. return rc;
  1225. }
  1226. static void __exit smc_exit(void)
  1227. {
  1228. struct smc_link_group *lgr, *lg;
  1229. LIST_HEAD(lgr_freeing_list);
  1230. spin_lock_bh(&smc_lgr_list.lock);
  1231. if (!list_empty(&smc_lgr_list.list))
  1232. list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
  1233. spin_unlock_bh(&smc_lgr_list.lock);
  1234. list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
  1235. list_del_init(&lgr->list);
  1236. smc_lgr_free(lgr); /* free link group */
  1237. }
  1238. smc_ib_unregister_client();
  1239. sock_unregister(PF_SMC);
  1240. proto_unregister(&smc_proto);
  1241. smc_pnet_exit();
  1242. }
  1243. module_init(smc_init);
  1244. module_exit(smc_exit);
  1245. MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
  1246. MODULE_DESCRIPTION("smc socket address family");
  1247. MODULE_LICENSE("GPL");
  1248. MODULE_ALIAS_NETPROTO(PF_SMC);