iscsi_tcp.c 29 KB

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