af_smc.c 34 KB

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