iscsi_tcp.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * iSCSI Initiator over TCP/IP Data-Path
  4. *
  5. * Copyright (C) 2004 Dmitry Yusupov
  6. * Copyright (C) 2004 Alex Aizman
  7. * Copyright (C) 2005 - 2006 Mike Christie
  8. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  9. * maintained by open-iscsi@googlegroups.com
  10. *
  11. * See the file COPYING included with this distribution for more details.
  12. *
  13. * Credits:
  14. * Christoph Hellwig
  15. * FUJITA Tomonori
  16. * Arne Redlich
  17. * Zhenyu Wang
  18. */
  19. #include <linux/types.h>
  20. #include <linux/inet.h>
  21. #include <linux/slab.h>
  22. #include <linux/sched/mm.h>
  23. #include <linux/file.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/delay.h>
  26. #include <linux/kfifo.h>
  27. #include <linux/scatterlist.h>
  28. #include <linux/module.h>
  29. #include <linux/backing-dev.h>
  30. #include <net/tcp.h>
  31. #include <scsi/scsi_cmnd.h>
  32. #include <scsi/scsi_device.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi.h>
  35. #include <scsi/scsi_transport_iscsi.h>
  36. #include <trace/events/iscsi.h>
  37. #include <trace/events/sock.h>
  38. #include "iscsi_tcp.h"
  39. MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
  40. "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
  41. "Alex Aizman <itn780@yahoo.com>");
  42. MODULE_DESCRIPTION("iSCSI/TCP data-path");
  43. MODULE_LICENSE("GPL");
  44. static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport;
  45. static const struct scsi_host_template iscsi_sw_tcp_sht;
  46. static struct iscsi_transport iscsi_sw_tcp_transport;
  47. static unsigned int iscsi_max_lun = ~0;
  48. module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
  49. static bool iscsi_recv_from_iscsi_q;
  50. module_param_named(recv_from_iscsi_q, iscsi_recv_from_iscsi_q, bool, 0644);
  51. MODULE_PARM_DESC(recv_from_iscsi_q, "Set to true to read iSCSI data/headers from the iscsi_q workqueue. The default is false which will perform reads from the network softirq context.");
  52. static int iscsi_sw_tcp_dbg;
  53. module_param_named(debug_iscsi_tcp, iscsi_sw_tcp_dbg, int,
  54. S_IRUGO | S_IWUSR);
  55. MODULE_PARM_DESC(debug_iscsi_tcp, "Turn on debugging for iscsi_tcp module "
  56. "Set to 1 to turn on, and zero to turn off. Default is off.");
  57. #define ISCSI_SW_TCP_DBG(_conn, dbg_fmt, arg...) \
  58. do { \
  59. if (iscsi_sw_tcp_dbg) \
  60. iscsi_conn_printk(KERN_INFO, _conn, \
  61. "%s " dbg_fmt, \
  62. __func__, ##arg); \
  63. iscsi_dbg_trace(trace_iscsi_dbg_sw_tcp, \
  64. &(_conn)->cls_conn->dev, \
  65. "%s " dbg_fmt, __func__, ##arg);\
  66. } while (0);
  67. /**
  68. * iscsi_sw_tcp_recv - TCP receive in sendfile fashion
  69. * @rd_desc: read descriptor
  70. * @skb: socket buffer
  71. * @offset: offset in skb
  72. * @len: skb->len - offset
  73. */
  74. static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
  75. unsigned int offset, size_t len)
  76. {
  77. struct iscsi_conn *conn = rd_desc->arg.data;
  78. unsigned int consumed, total_consumed = 0;
  79. int status;
  80. ISCSI_SW_TCP_DBG(conn, "in %d bytes\n", skb->len - offset);
  81. do {
  82. status = 0;
  83. consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status);
  84. offset += consumed;
  85. total_consumed += consumed;
  86. } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE);
  87. ISCSI_SW_TCP_DBG(conn, "read %d bytes status %d\n",
  88. skb->len - offset, status);
  89. return total_consumed;
  90. }
  91. /**
  92. * iscsi_sw_sk_state_check - check socket state
  93. * @sk: socket
  94. *
  95. * If the socket is in CLOSE or CLOSE_WAIT we should
  96. * not close the connection if there is still some
  97. * data pending.
  98. *
  99. * Must be called with sk_callback_lock.
  100. */
  101. static inline int iscsi_sw_sk_state_check(struct sock *sk)
  102. {
  103. struct iscsi_conn *conn = sk->sk_user_data;
  104. if ((sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) &&
  105. (conn->session->state != ISCSI_STATE_LOGGING_OUT) &&
  106. !atomic_read(&sk->sk_rmem_alloc)) {
  107. ISCSI_SW_TCP_DBG(conn, "TCP_CLOSE|TCP_CLOSE_WAIT\n");
  108. iscsi_conn_failure(conn, ISCSI_ERR_TCP_CONN_CLOSE);
  109. return -ECONNRESET;
  110. }
  111. return 0;
  112. }
  113. static void iscsi_sw_tcp_recv_data(struct iscsi_conn *conn)
  114. {
  115. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  116. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  117. struct sock *sk = tcp_sw_conn->sock->sk;
  118. read_descriptor_t rd_desc;
  119. /*
  120. * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
  121. * We set count to 1 because we want the network layer to
  122. * hand us all the skbs that are available. iscsi_tcp_recv
  123. * handled pdus that cross buffers or pdus that still need data.
  124. */
  125. rd_desc.arg.data = conn;
  126. rd_desc.count = 1;
  127. tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
  128. /* If we had to (atomically) map a highmem page,
  129. * unmap it now. */
  130. iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
  131. iscsi_sw_sk_state_check(sk);
  132. }
  133. static void iscsi_sw_tcp_recv_data_work(struct work_struct *work)
  134. {
  135. struct iscsi_conn *conn = container_of(work, struct iscsi_conn,
  136. recvwork);
  137. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  138. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  139. struct sock *sk = tcp_sw_conn->sock->sk;
  140. lock_sock(sk);
  141. iscsi_sw_tcp_recv_data(conn);
  142. release_sock(sk);
  143. }
  144. static void iscsi_sw_tcp_data_ready(struct sock *sk)
  145. {
  146. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  147. struct iscsi_tcp_conn *tcp_conn;
  148. struct iscsi_conn *conn;
  149. trace_sk_data_ready(sk);
  150. read_lock_bh(&sk->sk_callback_lock);
  151. conn = sk->sk_user_data;
  152. if (!conn) {
  153. read_unlock_bh(&sk->sk_callback_lock);
  154. return;
  155. }
  156. tcp_conn = conn->dd_data;
  157. tcp_sw_conn = tcp_conn->dd_data;
  158. if (tcp_sw_conn->queue_recv)
  159. iscsi_conn_queue_recv(conn);
  160. else
  161. iscsi_sw_tcp_recv_data(conn);
  162. read_unlock_bh(&sk->sk_callback_lock);
  163. }
  164. static void iscsi_sw_tcp_state_change(struct sock *sk)
  165. {
  166. struct iscsi_tcp_conn *tcp_conn;
  167. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  168. struct iscsi_conn *conn;
  169. void (*old_state_change)(struct sock *);
  170. read_lock_bh(&sk->sk_callback_lock);
  171. conn = sk->sk_user_data;
  172. if (!conn) {
  173. read_unlock_bh(&sk->sk_callback_lock);
  174. return;
  175. }
  176. iscsi_sw_sk_state_check(sk);
  177. tcp_conn = conn->dd_data;
  178. tcp_sw_conn = tcp_conn->dd_data;
  179. old_state_change = tcp_sw_conn->old_state_change;
  180. read_unlock_bh(&sk->sk_callback_lock);
  181. old_state_change(sk);
  182. }
  183. /**
  184. * iscsi_sw_tcp_write_space - Called when more output buffer space is available
  185. * @sk: socket space is available for
  186. **/
  187. static void iscsi_sw_tcp_write_space(struct sock *sk)
  188. {
  189. struct iscsi_conn *conn;
  190. struct iscsi_tcp_conn *tcp_conn;
  191. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  192. void (*old_write_space)(struct sock *);
  193. read_lock_bh(&sk->sk_callback_lock);
  194. conn = sk->sk_user_data;
  195. if (!conn) {
  196. read_unlock_bh(&sk->sk_callback_lock);
  197. return;
  198. }
  199. tcp_conn = conn->dd_data;
  200. tcp_sw_conn = tcp_conn->dd_data;
  201. old_write_space = tcp_sw_conn->old_write_space;
  202. read_unlock_bh(&sk->sk_callback_lock);
  203. old_write_space(sk);
  204. ISCSI_SW_TCP_DBG(conn, "iscsi_write_space\n");
  205. iscsi_conn_queue_xmit(conn);
  206. }
  207. static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn)
  208. {
  209. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  210. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  211. struct sock *sk = tcp_sw_conn->sock->sk;
  212. /* assign new callbacks */
  213. write_lock_bh(&sk->sk_callback_lock);
  214. sk->sk_user_data = conn;
  215. tcp_sw_conn->old_data_ready = sk->sk_data_ready;
  216. tcp_sw_conn->old_state_change = sk->sk_state_change;
  217. tcp_sw_conn->old_write_space = sk->sk_write_space;
  218. sk->sk_data_ready = iscsi_sw_tcp_data_ready;
  219. sk->sk_state_change = iscsi_sw_tcp_state_change;
  220. sk->sk_write_space = iscsi_sw_tcp_write_space;
  221. write_unlock_bh(&sk->sk_callback_lock);
  222. }
  223. static void
  224. iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_conn *conn)
  225. {
  226. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  227. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  228. struct sock *sk = tcp_sw_conn->sock->sk;
  229. /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
  230. write_lock_bh(&sk->sk_callback_lock);
  231. sk->sk_user_data = NULL;
  232. sk->sk_data_ready = tcp_sw_conn->old_data_ready;
  233. sk->sk_state_change = tcp_sw_conn->old_state_change;
  234. sk->sk_write_space = tcp_sw_conn->old_write_space;
  235. sk->sk_no_check_tx = 0;
  236. write_unlock_bh(&sk->sk_callback_lock);
  237. }
  238. /**
  239. * iscsi_sw_tcp_xmit_segment - transmit segment
  240. * @tcp_conn: the iSCSI TCP connection
  241. * @segment: the buffer to transmnit
  242. *
  243. * This function transmits as much of the buffer as
  244. * the network layer will accept, and returns the number of
  245. * bytes transmitted.
  246. *
  247. * If CRC hashing is enabled, the function will compute the
  248. * hash as it goes. When the entire segment has been transmitted,
  249. * it will retrieve the hash value and send it as well.
  250. */
  251. static int iscsi_sw_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
  252. struct iscsi_segment *segment)
  253. {
  254. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  255. struct socket *sk = tcp_sw_conn->sock;
  256. unsigned int copied = 0;
  257. int r = 0;
  258. while (!iscsi_tcp_segment_done(tcp_conn, segment, 0, r)) {
  259. struct scatterlist *sg;
  260. struct msghdr msg = {};
  261. struct bio_vec bv;
  262. unsigned int offset, copy;
  263. r = 0;
  264. offset = segment->copied;
  265. copy = segment->size - offset;
  266. if (segment->total_copied + segment->size < segment->total_size)
  267. msg.msg_flags |= MSG_MORE;
  268. if (tcp_sw_conn->queue_recv)
  269. msg.msg_flags |= MSG_DONTWAIT;
  270. if (!segment->data) {
  271. if (!tcp_conn->iscsi_conn->datadgst_en)
  272. msg.msg_flags |= MSG_SPLICE_PAGES;
  273. sg = segment->sg;
  274. offset += segment->sg_offset + sg->offset;
  275. bvec_set_page(&bv, sg_page(sg), copy, offset);
  276. } else {
  277. bvec_set_virt(&bv, segment->data + offset, copy);
  278. }
  279. iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bv, 1, copy);
  280. r = sock_sendmsg(sk, &msg);
  281. if (r < 0) {
  282. iscsi_tcp_segment_unmap(segment);
  283. return r;
  284. }
  285. copied += r;
  286. }
  287. return copied;
  288. }
  289. /**
  290. * iscsi_sw_tcp_xmit - TCP transmit
  291. * @conn: iscsi connection
  292. **/
  293. static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
  294. {
  295. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  296. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  297. struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
  298. unsigned int consumed = 0;
  299. int rc = 0;
  300. while (1) {
  301. rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
  302. /*
  303. * We may not have been able to send data because the conn
  304. * is getting stopped. libiscsi will know so propagate err
  305. * for it to do the right thing.
  306. */
  307. if (rc == -EAGAIN)
  308. return rc;
  309. else if (rc < 0) {
  310. rc = ISCSI_ERR_XMIT_FAILED;
  311. goto error;
  312. } else if (rc == 0)
  313. break;
  314. consumed += rc;
  315. if (segment->total_copied >= segment->total_size) {
  316. if (segment->done != NULL) {
  317. rc = segment->done(tcp_conn, segment);
  318. if (rc != 0)
  319. goto error;
  320. }
  321. }
  322. }
  323. ISCSI_SW_TCP_DBG(conn, "xmit %d bytes\n", consumed);
  324. conn->txdata_octets += consumed;
  325. return consumed;
  326. error:
  327. /* Transmit error. We could initiate error recovery
  328. * here. */
  329. ISCSI_SW_TCP_DBG(conn, "Error sending PDU, errno=%d\n", rc);
  330. iscsi_conn_failure(conn, rc);
  331. return -EIO;
  332. }
  333. /**
  334. * iscsi_sw_tcp_xmit_qlen - return the number of bytes queued for xmit
  335. * @conn: iscsi connection
  336. */
  337. static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
  338. {
  339. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  340. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  341. struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
  342. return segment->total_copied - segment->total_size;
  343. }
  344. static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
  345. {
  346. struct iscsi_conn *conn = task->conn;
  347. unsigned int noreclaim_flag;
  348. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  349. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  350. int rc = 0;
  351. if (!tcp_sw_conn->sock) {
  352. iscsi_conn_printk(KERN_ERR, conn,
  353. "Transport not bound to socket!\n");
  354. return -EINVAL;
  355. }
  356. noreclaim_flag = memalloc_noreclaim_save();
  357. while (iscsi_sw_tcp_xmit_qlen(conn)) {
  358. rc = iscsi_sw_tcp_xmit(conn);
  359. if (rc == 0) {
  360. rc = -EAGAIN;
  361. break;
  362. }
  363. if (rc < 0)
  364. break;
  365. rc = 0;
  366. }
  367. memalloc_noreclaim_restore(noreclaim_flag);
  368. return rc;
  369. }
  370. /*
  371. * This is called when we're done sending the header.
  372. * Simply copy the data_segment to the send segment, and return.
  373. */
  374. static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
  375. struct iscsi_segment *segment)
  376. {
  377. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  378. tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment;
  379. ISCSI_SW_TCP_DBG(tcp_conn->iscsi_conn,
  380. "Header done. Next segment size %u total_size %u\n",
  381. tcp_sw_conn->out.segment.size,
  382. tcp_sw_conn->out.segment.total_size);
  383. return 0;
  384. }
  385. static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
  386. size_t hdrlen)
  387. {
  388. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  389. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  390. ISCSI_SW_TCP_DBG(conn, "%s\n", conn->hdrdgst_en ?
  391. "digest enabled" : "digest disabled");
  392. /* Clear the data segment - needs to be filled in by the
  393. * caller using iscsi_tcp_send_data_prep() */
  394. memset(&tcp_sw_conn->out.data_segment, 0,
  395. sizeof(struct iscsi_segment));
  396. /* If header digest is enabled, compute the CRC and
  397. * place the digest into the same buffer. We make
  398. * sure that both iscsi_tcp_task and mtask have
  399. * sufficient room.
  400. */
  401. if (conn->hdrdgst_en) {
  402. iscsi_tcp_dgst_header(hdr, hdrlen, hdr + hdrlen);
  403. hdrlen += ISCSI_DIGEST_SIZE;
  404. }
  405. /* Remember header pointer for later, when we need
  406. * to decide whether there's a payload to go along
  407. * with the header. */
  408. tcp_sw_conn->out.hdr = hdr;
  409. iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
  410. iscsi_sw_tcp_send_hdr_done, NULL);
  411. }
  412. /*
  413. * Prepare the send buffer for the payload data.
  414. * Padding and checksumming will all be taken care
  415. * of by the iscsi_segment routines.
  416. */
  417. static int
  418. iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
  419. unsigned int count, unsigned int offset,
  420. unsigned int len)
  421. {
  422. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  423. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  424. u32 *tx_crcp = NULL;
  425. unsigned int hdr_spec_len;
  426. ISCSI_SW_TCP_DBG(conn, "offset=%d, datalen=%d %s\n", offset, len,
  427. conn->datadgst_en ?
  428. "digest enabled" : "digest disabled");
  429. /* Make sure the datalen matches what the caller
  430. said he would send. */
  431. hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
  432. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  433. if (conn->datadgst_en)
  434. tx_crcp = &tcp_sw_conn->tx_crc;
  435. return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
  436. sg, count, offset, len, NULL, tx_crcp);
  437. }
  438. static void
  439. iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
  440. size_t len)
  441. {
  442. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  443. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  444. u32 *tx_crcp = NULL;
  445. unsigned int hdr_spec_len;
  446. ISCSI_SW_TCP_DBG(conn, "datalen=%zd %s\n", len, conn->datadgst_en ?
  447. "digest enabled" : "digest disabled");
  448. /* Make sure the datalen matches what the caller
  449. said he would send. */
  450. hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
  451. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  452. if (conn->datadgst_en)
  453. tx_crcp = &tcp_sw_conn->tx_crc;
  454. iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
  455. data, len, NULL, tx_crcp);
  456. }
  457. static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
  458. unsigned int offset, unsigned int count)
  459. {
  460. struct iscsi_conn *conn = task->conn;
  461. int err = 0;
  462. iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
  463. if (!count)
  464. return 0;
  465. if (!task->sc)
  466. iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
  467. else {
  468. struct scsi_data_buffer *sdb = &task->sc->sdb;
  469. err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
  470. sdb->table.nents, offset,
  471. count);
  472. }
  473. if (err) {
  474. /* got invalid offset/len */
  475. return -EIO;
  476. }
  477. return 0;
  478. }
  479. static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
  480. {
  481. struct iscsi_tcp_task *tcp_task = task->dd_data;
  482. task->hdr = task->dd_data + sizeof(*tcp_task);
  483. task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
  484. return 0;
  485. }
  486. static struct iscsi_cls_conn *
  487. iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
  488. uint32_t conn_idx)
  489. {
  490. struct iscsi_conn *conn;
  491. struct iscsi_cls_conn *cls_conn;
  492. struct iscsi_tcp_conn *tcp_conn;
  493. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  494. cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
  495. conn_idx);
  496. if (!cls_conn)
  497. return NULL;
  498. conn = cls_conn->dd_data;
  499. tcp_conn = conn->dd_data;
  500. tcp_sw_conn = tcp_conn->dd_data;
  501. INIT_WORK(&conn->recvwork, iscsi_sw_tcp_recv_data_work);
  502. tcp_sw_conn->queue_recv = iscsi_recv_from_iscsi_q;
  503. mutex_init(&tcp_sw_conn->sock_lock);
  504. tcp_conn->rx_crcp = &tcp_sw_conn->rx_crc;
  505. return cls_conn;
  506. }
  507. static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
  508. {
  509. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  510. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  511. struct socket *sock = tcp_sw_conn->sock;
  512. /*
  513. * The iscsi transport class will make sure we are not called in
  514. * parallel with start, stop, bind and destroys. However, this can be
  515. * called twice if userspace does a stop then a destroy.
  516. */
  517. if (!sock)
  518. return;
  519. /*
  520. * Make sure we start socket shutdown now in case userspace is up
  521. * but delayed in releasing the socket.
  522. */
  523. kernel_sock_shutdown(sock, SHUT_RDWR);
  524. sock_hold(sock->sk);
  525. iscsi_sw_tcp_conn_restore_callbacks(conn);
  526. sock_put(sock->sk);
  527. iscsi_suspend_rx(conn);
  528. mutex_lock(&tcp_sw_conn->sock_lock);
  529. tcp_sw_conn->sock = NULL;
  530. mutex_unlock(&tcp_sw_conn->sock_lock);
  531. sockfd_put(sock);
  532. }
  533. static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
  534. {
  535. struct iscsi_conn *conn = cls_conn->dd_data;
  536. iscsi_sw_tcp_release_conn(conn);
  537. iscsi_tcp_conn_teardown(cls_conn);
  538. }
  539. static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  540. {
  541. struct iscsi_conn *conn = cls_conn->dd_data;
  542. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  543. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  544. struct socket *sock = tcp_sw_conn->sock;
  545. /* userspace may have goofed up and not bound us */
  546. if (!sock)
  547. return;
  548. sock->sk->sk_err = EIO;
  549. wake_up_interruptible(sk_sleep(sock->sk));
  550. /* stop xmit side */
  551. iscsi_suspend_tx(conn);
  552. /* stop recv side and release socket */
  553. iscsi_sw_tcp_release_conn(conn);
  554. iscsi_conn_stop(cls_conn, flag);
  555. }
  556. static int
  557. iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,
  558. struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
  559. int is_leading)
  560. {
  561. struct iscsi_conn *conn = cls_conn->dd_data;
  562. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  563. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  564. struct sock *sk;
  565. struct socket *sock;
  566. int err;
  567. /* lookup for existing socket */
  568. sock = sockfd_lookup((int)transport_eph, &err);
  569. if (!sock) {
  570. iscsi_conn_printk(KERN_ERR, conn,
  571. "sockfd_lookup failed %d\n", err);
  572. return -EEXIST;
  573. }
  574. err = -EINVAL;
  575. if (!sk_is_tcp(sock->sk))
  576. goto free_socket;
  577. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  578. if (err)
  579. goto free_socket;
  580. mutex_lock(&tcp_sw_conn->sock_lock);
  581. /* bind iSCSI connection and socket */
  582. tcp_sw_conn->sock = sock;
  583. mutex_unlock(&tcp_sw_conn->sock_lock);
  584. /* setup Socket parameters */
  585. sk = sock->sk;
  586. sk->sk_reuse = SK_CAN_REUSE;
  587. sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
  588. sk->sk_allocation = GFP_ATOMIC;
  589. sk->sk_use_task_frag = false;
  590. sk_set_memalloc(sk);
  591. sock_no_linger(sk);
  592. iscsi_sw_tcp_conn_set_callbacks(conn);
  593. /*
  594. * set receive state machine into initial state
  595. */
  596. iscsi_tcp_hdr_recv_prep(tcp_conn);
  597. return 0;
  598. free_socket:
  599. sockfd_put(sock);
  600. return err;
  601. }
  602. static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
  603. enum iscsi_param param, char *buf,
  604. int buflen)
  605. {
  606. struct iscsi_conn *conn = cls_conn->dd_data;
  607. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  608. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  609. switch(param) {
  610. case ISCSI_PARAM_HDRDGST_EN:
  611. iscsi_set_param(cls_conn, param, buf, buflen);
  612. break;
  613. case ISCSI_PARAM_DATADGST_EN:
  614. mutex_lock(&tcp_sw_conn->sock_lock);
  615. if (!tcp_sw_conn->sock) {
  616. mutex_unlock(&tcp_sw_conn->sock_lock);
  617. return -ENOTCONN;
  618. }
  619. iscsi_set_param(cls_conn, param, buf, buflen);
  620. mutex_unlock(&tcp_sw_conn->sock_lock);
  621. break;
  622. case ISCSI_PARAM_MAX_R2T:
  623. return iscsi_tcp_set_max_r2t(conn, buf);
  624. default:
  625. return iscsi_set_param(cls_conn, param, buf, buflen);
  626. }
  627. return 0;
  628. }
  629. static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
  630. enum iscsi_param param, char *buf)
  631. {
  632. struct iscsi_conn *conn = cls_conn->dd_data;
  633. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  634. struct iscsi_tcp_conn *tcp_conn;
  635. struct sockaddr_in6 addr;
  636. struct socket *sock;
  637. int rc;
  638. switch(param) {
  639. case ISCSI_PARAM_CONN_PORT:
  640. case ISCSI_PARAM_CONN_ADDRESS:
  641. case ISCSI_PARAM_LOCAL_PORT:
  642. spin_lock_bh(&conn->session->frwd_lock);
  643. if (!conn->session->leadconn) {
  644. spin_unlock_bh(&conn->session->frwd_lock);
  645. return -ENOTCONN;
  646. }
  647. /*
  648. * The conn has been setup and bound, so just grab a ref
  649. * incase a destroy runs while we are in the net layer.
  650. */
  651. iscsi_get_conn(conn->cls_conn);
  652. spin_unlock_bh(&conn->session->frwd_lock);
  653. tcp_conn = conn->dd_data;
  654. tcp_sw_conn = tcp_conn->dd_data;
  655. mutex_lock(&tcp_sw_conn->sock_lock);
  656. sock = tcp_sw_conn->sock;
  657. if (!sock) {
  658. rc = -ENOTCONN;
  659. goto sock_unlock;
  660. }
  661. if (param == ISCSI_PARAM_LOCAL_PORT)
  662. rc = kernel_getsockname(sock,
  663. (struct sockaddr *)&addr);
  664. else
  665. rc = kernel_getpeername(sock,
  666. (struct sockaddr *)&addr);
  667. sock_unlock:
  668. mutex_unlock(&tcp_sw_conn->sock_lock);
  669. iscsi_put_conn(conn->cls_conn);
  670. if (rc < 0)
  671. return rc;
  672. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  673. &addr, param, buf);
  674. default:
  675. return iscsi_conn_get_param(cls_conn, param, buf);
  676. }
  677. return 0;
  678. }
  679. static int iscsi_sw_tcp_host_get_param(struct Scsi_Host *shost,
  680. enum iscsi_host_param param, char *buf)
  681. {
  682. struct iscsi_sw_tcp_host *tcp_sw_host = iscsi_host_priv(shost);
  683. struct iscsi_session *session;
  684. struct iscsi_conn *conn;
  685. struct iscsi_tcp_conn *tcp_conn;
  686. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  687. struct sockaddr_in6 addr;
  688. struct socket *sock;
  689. int rc;
  690. switch (param) {
  691. case ISCSI_HOST_PARAM_IPADDRESS:
  692. session = tcp_sw_host->session;
  693. if (!session)
  694. return -ENOTCONN;
  695. spin_lock_bh(&session->frwd_lock);
  696. conn = session->leadconn;
  697. if (!conn) {
  698. spin_unlock_bh(&session->frwd_lock);
  699. return -ENOTCONN;
  700. }
  701. tcp_conn = conn->dd_data;
  702. tcp_sw_conn = tcp_conn->dd_data;
  703. /*
  704. * The conn has been setup and bound, so just grab a ref
  705. * incase a destroy runs while we are in the net layer.
  706. */
  707. iscsi_get_conn(conn->cls_conn);
  708. spin_unlock_bh(&session->frwd_lock);
  709. mutex_lock(&tcp_sw_conn->sock_lock);
  710. sock = tcp_sw_conn->sock;
  711. if (!sock)
  712. rc = -ENOTCONN;
  713. else
  714. rc = kernel_getsockname(sock, (struct sockaddr *)&addr);
  715. mutex_unlock(&tcp_sw_conn->sock_lock);
  716. iscsi_put_conn(conn->cls_conn);
  717. if (rc < 0)
  718. return rc;
  719. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  720. &addr,
  721. (enum iscsi_param)param, buf);
  722. default:
  723. return iscsi_host_get_param(shost, param, buf);
  724. }
  725. return 0;
  726. }
  727. static void
  728. iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  729. struct iscsi_stats *stats)
  730. {
  731. struct iscsi_conn *conn = cls_conn->dd_data;
  732. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  733. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  734. stats->custom_length = 3;
  735. strcpy(stats->custom[0].desc, "tx_sendpage_failures");
  736. stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
  737. strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
  738. stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
  739. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  740. stats->custom[2].value = conn->eh_abort_cnt;
  741. iscsi_tcp_conn_get_stats(cls_conn, stats);
  742. }
  743. static struct iscsi_cls_session *
  744. iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
  745. uint16_t qdepth, uint32_t initial_cmdsn)
  746. {
  747. struct iscsi_cls_session *cls_session;
  748. struct iscsi_session *session;
  749. struct iscsi_sw_tcp_host *tcp_sw_host;
  750. struct Scsi_Host *shost;
  751. int rc;
  752. if (ep) {
  753. printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
  754. return NULL;
  755. }
  756. shost = iscsi_host_alloc(&iscsi_sw_tcp_sht,
  757. sizeof(struct iscsi_sw_tcp_host), 1);
  758. if (!shost)
  759. return NULL;
  760. shost->transportt = iscsi_sw_tcp_scsi_transport;
  761. shost->cmd_per_lun = qdepth;
  762. shost->max_lun = iscsi_max_lun;
  763. shost->max_id = 0;
  764. shost->max_channel = 0;
  765. shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
  766. shost->dma_alignment = 0;
  767. rc = iscsi_host_get_max_scsi_cmds(shost, cmds_max);
  768. if (rc < 0)
  769. goto free_host;
  770. shost->can_queue = rc;
  771. if (iscsi_host_add(shost, NULL))
  772. goto free_host;
  773. cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
  774. cmds_max, 0,
  775. sizeof(struct iscsi_tcp_task) +
  776. sizeof(struct iscsi_sw_tcp_hdrbuf),
  777. initial_cmdsn, 0);
  778. if (!cls_session)
  779. goto remove_host;
  780. session = cls_session->dd_data;
  781. if (iscsi_tcp_r2tpool_alloc(session))
  782. goto remove_session;
  783. /* We are now fully setup so expose the session to sysfs. */
  784. tcp_sw_host = iscsi_host_priv(shost);
  785. tcp_sw_host->session = session;
  786. return cls_session;
  787. remove_session:
  788. iscsi_session_teardown(cls_session);
  789. remove_host:
  790. iscsi_host_remove(shost, false);
  791. free_host:
  792. iscsi_host_free(shost);
  793. return NULL;
  794. }
  795. static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
  796. {
  797. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  798. struct iscsi_session *session = cls_session->dd_data;
  799. if (WARN_ON_ONCE(session->leadconn))
  800. return;
  801. iscsi_session_remove(cls_session);
  802. /*
  803. * Our get_host_param needs to access the session, so remove the
  804. * host from sysfs before freeing the session to make sure userspace
  805. * is no longer accessing the callout.
  806. */
  807. iscsi_host_remove(shost, false);
  808. iscsi_tcp_r2tpool_free(cls_session->dd_data);
  809. iscsi_session_free(cls_session);
  810. iscsi_host_free(shost);
  811. }
  812. static umode_t iscsi_sw_tcp_attr_is_visible(int param_type, int param)
  813. {
  814. switch (param_type) {
  815. case ISCSI_HOST_PARAM:
  816. switch (param) {
  817. case ISCSI_HOST_PARAM_NETDEV_NAME:
  818. case ISCSI_HOST_PARAM_HWADDRESS:
  819. case ISCSI_HOST_PARAM_IPADDRESS:
  820. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  821. return S_IRUGO;
  822. default:
  823. return 0;
  824. }
  825. case ISCSI_PARAM:
  826. switch (param) {
  827. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  828. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  829. case ISCSI_PARAM_HDRDGST_EN:
  830. case ISCSI_PARAM_DATADGST_EN:
  831. case ISCSI_PARAM_CONN_ADDRESS:
  832. case ISCSI_PARAM_CONN_PORT:
  833. case ISCSI_PARAM_LOCAL_PORT:
  834. case ISCSI_PARAM_EXP_STATSN:
  835. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  836. case ISCSI_PARAM_PERSISTENT_PORT:
  837. case ISCSI_PARAM_PING_TMO:
  838. case ISCSI_PARAM_RECV_TMO:
  839. case ISCSI_PARAM_INITIAL_R2T_EN:
  840. case ISCSI_PARAM_MAX_R2T:
  841. case ISCSI_PARAM_IMM_DATA_EN:
  842. case ISCSI_PARAM_FIRST_BURST:
  843. case ISCSI_PARAM_MAX_BURST:
  844. case ISCSI_PARAM_PDU_INORDER_EN:
  845. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  846. case ISCSI_PARAM_ERL:
  847. case ISCSI_PARAM_TARGET_NAME:
  848. case ISCSI_PARAM_TPGT:
  849. case ISCSI_PARAM_USERNAME:
  850. case ISCSI_PARAM_PASSWORD:
  851. case ISCSI_PARAM_USERNAME_IN:
  852. case ISCSI_PARAM_PASSWORD_IN:
  853. case ISCSI_PARAM_FAST_ABORT:
  854. case ISCSI_PARAM_ABORT_TMO:
  855. case ISCSI_PARAM_LU_RESET_TMO:
  856. case ISCSI_PARAM_TGT_RESET_TMO:
  857. case ISCSI_PARAM_IFACE_NAME:
  858. case ISCSI_PARAM_INITIATOR_NAME:
  859. return S_IRUGO;
  860. default:
  861. return 0;
  862. }
  863. }
  864. return 0;
  865. }
  866. static int iscsi_sw_tcp_sdev_configure(struct scsi_device *sdev,
  867. struct queue_limits *lim)
  868. {
  869. struct iscsi_sw_tcp_host *tcp_sw_host = iscsi_host_priv(sdev->host);
  870. struct iscsi_session *session = tcp_sw_host->session;
  871. struct iscsi_conn *conn = session->leadconn;
  872. if (conn->datadgst_en)
  873. lim->features |= BLK_FEAT_STABLE_WRITES;
  874. return 0;
  875. }
  876. static const struct scsi_host_template iscsi_sw_tcp_sht = {
  877. .module = THIS_MODULE,
  878. .name = "iSCSI Initiator over TCP/IP",
  879. .queuecommand = iscsi_queuecommand,
  880. .change_queue_depth = scsi_change_queue_depth,
  881. .can_queue = ISCSI_TOTAL_CMDS_MAX,
  882. .sg_tablesize = 4096,
  883. .max_sectors = 0xFFFF,
  884. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  885. .eh_timed_out = iscsi_eh_cmd_timed_out,
  886. .eh_abort_handler = iscsi_eh_abort,
  887. .eh_device_reset_handler= iscsi_eh_device_reset,
  888. .eh_target_reset_handler = iscsi_eh_recover_target,
  889. .dma_boundary = PAGE_SIZE - 1,
  890. .sdev_configure = iscsi_sw_tcp_sdev_configure,
  891. .proc_name = "iscsi_tcp",
  892. .this_id = -1,
  893. .track_queue_depth = 1,
  894. .cmd_size = sizeof(struct iscsi_cmd),
  895. };
  896. static struct iscsi_transport iscsi_sw_tcp_transport = {
  897. .owner = THIS_MODULE,
  898. .name = "tcp",
  899. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  900. | CAP_DATADGST,
  901. /* session management */
  902. .create_session = iscsi_sw_tcp_session_create,
  903. .destroy_session = iscsi_sw_tcp_session_destroy,
  904. /* connection management */
  905. .create_conn = iscsi_sw_tcp_conn_create,
  906. .bind_conn = iscsi_sw_tcp_conn_bind,
  907. .destroy_conn = iscsi_sw_tcp_conn_destroy,
  908. .attr_is_visible = iscsi_sw_tcp_attr_is_visible,
  909. .set_param = iscsi_sw_tcp_conn_set_param,
  910. .get_conn_param = iscsi_sw_tcp_conn_get_param,
  911. .get_session_param = iscsi_session_get_param,
  912. .start_conn = iscsi_conn_start,
  913. .stop_conn = iscsi_sw_tcp_conn_stop,
  914. /* iscsi host params */
  915. .get_host_param = iscsi_sw_tcp_host_get_param,
  916. .set_host_param = iscsi_host_set_param,
  917. /* IO */
  918. .send_pdu = iscsi_conn_send_pdu,
  919. .get_stats = iscsi_sw_tcp_conn_get_stats,
  920. /* iscsi task/cmd helpers */
  921. .init_task = iscsi_tcp_task_init,
  922. .xmit_task = iscsi_tcp_task_xmit,
  923. .cleanup_task = iscsi_tcp_cleanup_task,
  924. /* low level pdu helpers */
  925. .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
  926. .init_pdu = iscsi_sw_tcp_pdu_init,
  927. .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
  928. /* recovery */
  929. .session_recovery_timedout = iscsi_session_recovery_timedout,
  930. };
  931. static int __init iscsi_sw_tcp_init(void)
  932. {
  933. if (iscsi_max_lun < 1) {
  934. printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
  935. iscsi_max_lun);
  936. return -EINVAL;
  937. }
  938. iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
  939. &iscsi_sw_tcp_transport);
  940. if (!iscsi_sw_tcp_scsi_transport)
  941. return -ENODEV;
  942. return 0;
  943. }
  944. static void __exit iscsi_sw_tcp_exit(void)
  945. {
  946. iscsi_unregister_transport(&iscsi_sw_tcp_transport);
  947. }
  948. module_init(iscsi_sw_tcp_init);
  949. module_exit(iscsi_sw_tcp_exit);