iscsi_tcp.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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 <linux/types.h>
  29. #include <linux/inet.h>
  30. #include <linux/slab.h>
  31. #include <linux/file.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/crypto.h>
  34. #include <linux/delay.h>
  35. #include <linux/kfifo.h>
  36. #include <linux/scatterlist.h>
  37. #include <net/tcp.h>
  38. #include <scsi/scsi_cmnd.h>
  39. #include <scsi/scsi_device.h>
  40. #include <scsi/scsi_host.h>
  41. #include <scsi/scsi.h>
  42. #include <scsi/scsi_transport_iscsi.h>
  43. #include "iscsi_tcp.h"
  44. MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
  45. "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
  46. "Alex Aizman <itn780@yahoo.com>");
  47. MODULE_DESCRIPTION("iSCSI/TCP data-path");
  48. MODULE_LICENSE("GPL");
  49. static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport;
  50. static struct scsi_host_template iscsi_sw_tcp_sht;
  51. static struct iscsi_transport iscsi_sw_tcp_transport;
  52. static unsigned int iscsi_max_lun = 512;
  53. module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
  54. static int iscsi_sw_tcp_dbg;
  55. module_param_named(debug_iscsi_tcp, iscsi_sw_tcp_dbg, int,
  56. S_IRUGO | S_IWUSR);
  57. MODULE_PARM_DESC(debug_iscsi_tcp, "Turn on debugging for iscsi_tcp module "
  58. "Set to 1 to turn on, and zero to turn off. Default is off.");
  59. #define ISCSI_SW_TCP_DBG(_conn, dbg_fmt, arg...) \
  60. do { \
  61. if (iscsi_sw_tcp_dbg) \
  62. iscsi_conn_printk(KERN_INFO, _conn, \
  63. "%s " dbg_fmt, \
  64. __func__, ##arg); \
  65. } while (0);
  66. /**
  67. * iscsi_sw_tcp_recv - TCP receive in sendfile fashion
  68. * @rd_desc: read descriptor
  69. * @skb: socket buffer
  70. * @offset: offset in skb
  71. * @len: skb->len - offset
  72. */
  73. static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
  74. unsigned int offset, size_t len)
  75. {
  76. struct iscsi_conn *conn = rd_desc->arg.data;
  77. unsigned int consumed, total_consumed = 0;
  78. int status;
  79. ISCSI_SW_TCP_DBG(conn, "in %d bytes\n", skb->len - offset);
  80. do {
  81. status = 0;
  82. consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status);
  83. offset += consumed;
  84. total_consumed += consumed;
  85. } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE);
  86. ISCSI_SW_TCP_DBG(conn, "read %d bytes status %d\n",
  87. skb->len - offset, status);
  88. return total_consumed;
  89. }
  90. /**
  91. * iscsi_sw_sk_state_check - check socket state
  92. * @sk: socket
  93. *
  94. * If the socket is in CLOSE or CLOSE_WAIT we should
  95. * not close the connection if there is still some
  96. * data pending.
  97. */
  98. static inline int iscsi_sw_sk_state_check(struct sock *sk)
  99. {
  100. struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
  101. if ((sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) &&
  102. !atomic_read(&sk->sk_rmem_alloc)) {
  103. ISCSI_SW_TCP_DBG(conn, "TCP_CLOSE|TCP_CLOSE_WAIT\n");
  104. iscsi_conn_failure(conn, ISCSI_ERR_TCP_CONN_CLOSE);
  105. return -ECONNRESET;
  106. }
  107. return 0;
  108. }
  109. static void iscsi_sw_tcp_data_ready(struct sock *sk, int flag)
  110. {
  111. struct iscsi_conn *conn = sk->sk_user_data;
  112. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  113. read_descriptor_t rd_desc;
  114. read_lock(&sk->sk_callback_lock);
  115. /*
  116. * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
  117. * We set count to 1 because we want the network layer to
  118. * hand us all the skbs that are available. iscsi_tcp_recv
  119. * handled pdus that cross buffers or pdus that still need data.
  120. */
  121. rd_desc.arg.data = conn;
  122. rd_desc.count = 1;
  123. tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
  124. iscsi_sw_sk_state_check(sk);
  125. read_unlock(&sk->sk_callback_lock);
  126. /* If we had to (atomically) map a highmem page,
  127. * unmap it now. */
  128. iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
  129. }
  130. static void iscsi_sw_tcp_state_change(struct sock *sk)
  131. {
  132. struct iscsi_tcp_conn *tcp_conn;
  133. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  134. struct iscsi_conn *conn;
  135. struct iscsi_session *session;
  136. void (*old_state_change)(struct sock *);
  137. read_lock(&sk->sk_callback_lock);
  138. conn = (struct iscsi_conn*)sk->sk_user_data;
  139. session = conn->session;
  140. iscsi_sw_sk_state_check(sk);
  141. tcp_conn = conn->dd_data;
  142. tcp_sw_conn = tcp_conn->dd_data;
  143. old_state_change = tcp_sw_conn->old_state_change;
  144. read_unlock(&sk->sk_callback_lock);
  145. old_state_change(sk);
  146. }
  147. /**
  148. * iscsi_write_space - Called when more output buffer space is available
  149. * @sk: socket space is available for
  150. **/
  151. static void iscsi_sw_tcp_write_space(struct sock *sk)
  152. {
  153. struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
  154. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  155. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  156. tcp_sw_conn->old_write_space(sk);
  157. ISCSI_SW_TCP_DBG(conn, "iscsi_write_space\n");
  158. iscsi_conn_queue_work(conn);
  159. }
  160. static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn)
  161. {
  162. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  163. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  164. struct sock *sk = tcp_sw_conn->sock->sk;
  165. /* assign new callbacks */
  166. write_lock_bh(&sk->sk_callback_lock);
  167. sk->sk_user_data = conn;
  168. tcp_sw_conn->old_data_ready = sk->sk_data_ready;
  169. tcp_sw_conn->old_state_change = sk->sk_state_change;
  170. tcp_sw_conn->old_write_space = sk->sk_write_space;
  171. sk->sk_data_ready = iscsi_sw_tcp_data_ready;
  172. sk->sk_state_change = iscsi_sw_tcp_state_change;
  173. sk->sk_write_space = iscsi_sw_tcp_write_space;
  174. write_unlock_bh(&sk->sk_callback_lock);
  175. }
  176. static void
  177. iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_sw_tcp_conn *tcp_sw_conn)
  178. {
  179. struct sock *sk = tcp_sw_conn->sock->sk;
  180. /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
  181. write_lock_bh(&sk->sk_callback_lock);
  182. sk->sk_user_data = NULL;
  183. sk->sk_data_ready = tcp_sw_conn->old_data_ready;
  184. sk->sk_state_change = tcp_sw_conn->old_state_change;
  185. sk->sk_write_space = tcp_sw_conn->old_write_space;
  186. sk->sk_no_check = 0;
  187. write_unlock_bh(&sk->sk_callback_lock);
  188. }
  189. /**
  190. * iscsi_sw_tcp_xmit_segment - transmit segment
  191. * @tcp_conn: the iSCSI TCP connection
  192. * @segment: the buffer to transmnit
  193. *
  194. * This function transmits as much of the buffer as
  195. * the network layer will accept, and returns the number of
  196. * bytes transmitted.
  197. *
  198. * If CRC hashing is enabled, the function will compute the
  199. * hash as it goes. When the entire segment has been transmitted,
  200. * it will retrieve the hash value and send it as well.
  201. */
  202. static int iscsi_sw_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
  203. struct iscsi_segment *segment)
  204. {
  205. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  206. struct socket *sk = tcp_sw_conn->sock;
  207. unsigned int copied = 0;
  208. int r = 0;
  209. while (!iscsi_tcp_segment_done(tcp_conn, segment, 0, r)) {
  210. struct scatterlist *sg;
  211. unsigned int offset, copy;
  212. int flags = 0;
  213. r = 0;
  214. offset = segment->copied;
  215. copy = segment->size - offset;
  216. if (segment->total_copied + segment->size < segment->total_size)
  217. flags |= MSG_MORE;
  218. /* Use sendpage if we can; else fall back to sendmsg */
  219. if (!segment->data) {
  220. sg = segment->sg;
  221. offset += segment->sg_offset + sg->offset;
  222. r = tcp_sw_conn->sendpage(sk, sg_page(sg), offset,
  223. copy, flags);
  224. } else {
  225. struct msghdr msg = { .msg_flags = flags };
  226. struct kvec iov = {
  227. .iov_base = segment->data + offset,
  228. .iov_len = copy
  229. };
  230. r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
  231. }
  232. if (r < 0) {
  233. iscsi_tcp_segment_unmap(segment);
  234. return r;
  235. }
  236. copied += r;
  237. }
  238. return copied;
  239. }
  240. /**
  241. * iscsi_sw_tcp_xmit - TCP transmit
  242. **/
  243. static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
  244. {
  245. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  246. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  247. struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
  248. unsigned int consumed = 0;
  249. int rc = 0;
  250. while (1) {
  251. rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
  252. /*
  253. * We may not have been able to send data because the conn
  254. * is getting stopped. libiscsi will know so propogate err
  255. * for it to do the right thing.
  256. */
  257. if (rc == -EAGAIN)
  258. return rc;
  259. else if (rc < 0) {
  260. rc = ISCSI_ERR_XMIT_FAILED;
  261. goto error;
  262. } else if (rc == 0)
  263. break;
  264. consumed += rc;
  265. if (segment->total_copied >= segment->total_size) {
  266. if (segment->done != NULL) {
  267. rc = segment->done(tcp_conn, segment);
  268. if (rc != 0)
  269. goto error;
  270. }
  271. }
  272. }
  273. ISCSI_SW_TCP_DBG(conn, "xmit %d bytes\n", consumed);
  274. conn->txdata_octets += consumed;
  275. return consumed;
  276. error:
  277. /* Transmit error. We could initiate error recovery
  278. * here. */
  279. ISCSI_SW_TCP_DBG(conn, "Error sending PDU, errno=%d\n", rc);
  280. iscsi_conn_failure(conn, rc);
  281. return -EIO;
  282. }
  283. /**
  284. * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
  285. */
  286. static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
  287. {
  288. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  289. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  290. struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
  291. return segment->total_copied - segment->total_size;
  292. }
  293. static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
  294. {
  295. struct iscsi_conn *conn = task->conn;
  296. int rc;
  297. while (iscsi_sw_tcp_xmit_qlen(conn)) {
  298. rc = iscsi_sw_tcp_xmit(conn);
  299. if (rc == 0)
  300. return -EAGAIN;
  301. if (rc < 0)
  302. return rc;
  303. }
  304. return 0;
  305. }
  306. /*
  307. * This is called when we're done sending the header.
  308. * Simply copy the data_segment to the send segment, and return.
  309. */
  310. static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
  311. struct iscsi_segment *segment)
  312. {
  313. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  314. tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment;
  315. ISCSI_SW_TCP_DBG(tcp_conn->iscsi_conn,
  316. "Header done. Next segment size %u total_size %u\n",
  317. tcp_sw_conn->out.segment.size,
  318. tcp_sw_conn->out.segment.total_size);
  319. return 0;
  320. }
  321. static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
  322. size_t hdrlen)
  323. {
  324. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  325. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  326. ISCSI_SW_TCP_DBG(conn, "%s\n", conn->hdrdgst_en ?
  327. "digest enabled" : "digest disabled");
  328. /* Clear the data segment - needs to be filled in by the
  329. * caller using iscsi_tcp_send_data_prep() */
  330. memset(&tcp_sw_conn->out.data_segment, 0,
  331. sizeof(struct iscsi_segment));
  332. /* If header digest is enabled, compute the CRC and
  333. * place the digest into the same buffer. We make
  334. * sure that both iscsi_tcp_task and mtask have
  335. * sufficient room.
  336. */
  337. if (conn->hdrdgst_en) {
  338. iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen,
  339. hdr + hdrlen);
  340. hdrlen += ISCSI_DIGEST_SIZE;
  341. }
  342. /* Remember header pointer for later, when we need
  343. * to decide whether there's a payload to go along
  344. * with the header. */
  345. tcp_sw_conn->out.hdr = hdr;
  346. iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
  347. iscsi_sw_tcp_send_hdr_done, NULL);
  348. }
  349. /*
  350. * Prepare the send buffer for the payload data.
  351. * Padding and checksumming will all be taken care
  352. * of by the iscsi_segment routines.
  353. */
  354. static int
  355. iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
  356. unsigned int count, unsigned int offset,
  357. unsigned int len)
  358. {
  359. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  360. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  361. struct hash_desc *tx_hash = NULL;
  362. unsigned int hdr_spec_len;
  363. ISCSI_SW_TCP_DBG(conn, "offset=%d, datalen=%d %s\n", offset, len,
  364. conn->datadgst_en ?
  365. "digest enabled" : "digest disabled");
  366. /* Make sure the datalen matches what the caller
  367. said he would send. */
  368. hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
  369. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  370. if (conn->datadgst_en)
  371. tx_hash = &tcp_sw_conn->tx_hash;
  372. return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
  373. sg, count, offset, len,
  374. NULL, tx_hash);
  375. }
  376. static void
  377. iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
  378. size_t len)
  379. {
  380. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  381. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  382. struct hash_desc *tx_hash = NULL;
  383. unsigned int hdr_spec_len;
  384. ISCSI_SW_TCP_DBG(conn, "datalen=%zd %s\n", len, conn->datadgst_en ?
  385. "digest enabled" : "digest disabled");
  386. /* Make sure the datalen matches what the caller
  387. said he would send. */
  388. hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
  389. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  390. if (conn->datadgst_en)
  391. tx_hash = &tcp_sw_conn->tx_hash;
  392. iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
  393. data, len, NULL, tx_hash);
  394. }
  395. static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
  396. unsigned int offset, unsigned int count)
  397. {
  398. struct iscsi_conn *conn = task->conn;
  399. int err = 0;
  400. iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
  401. if (!count)
  402. return 0;
  403. if (!task->sc)
  404. iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
  405. else {
  406. struct scsi_data_buffer *sdb = scsi_out(task->sc);
  407. err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
  408. sdb->table.nents, offset,
  409. count);
  410. }
  411. if (err) {
  412. /* got invalid offset/len */
  413. return -EIO;
  414. }
  415. return 0;
  416. }
  417. static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
  418. {
  419. struct iscsi_tcp_task *tcp_task = task->dd_data;
  420. task->hdr = task->dd_data + sizeof(*tcp_task);
  421. task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
  422. return 0;
  423. }
  424. static struct iscsi_cls_conn *
  425. iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
  426. uint32_t conn_idx)
  427. {
  428. struct iscsi_conn *conn;
  429. struct iscsi_cls_conn *cls_conn;
  430. struct iscsi_tcp_conn *tcp_conn;
  431. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  432. cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
  433. conn_idx);
  434. if (!cls_conn)
  435. return NULL;
  436. conn = cls_conn->dd_data;
  437. tcp_conn = conn->dd_data;
  438. tcp_sw_conn = tcp_conn->dd_data;
  439. tcp_sw_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  440. CRYPTO_ALG_ASYNC);
  441. tcp_sw_conn->tx_hash.flags = 0;
  442. if (IS_ERR(tcp_sw_conn->tx_hash.tfm))
  443. goto free_conn;
  444. tcp_sw_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  445. CRYPTO_ALG_ASYNC);
  446. tcp_sw_conn->rx_hash.flags = 0;
  447. if (IS_ERR(tcp_sw_conn->rx_hash.tfm))
  448. goto free_tx_tfm;
  449. tcp_conn->rx_hash = &tcp_sw_conn->rx_hash;
  450. return cls_conn;
  451. free_tx_tfm:
  452. crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
  453. free_conn:
  454. iscsi_conn_printk(KERN_ERR, conn,
  455. "Could not create connection due to crc32c "
  456. "loading error. Make sure the crc32c "
  457. "module is built as a module or into the "
  458. "kernel\n");
  459. iscsi_tcp_conn_teardown(cls_conn);
  460. return NULL;
  461. }
  462. static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
  463. {
  464. struct iscsi_session *session = conn->session;
  465. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  466. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  467. struct socket *sock = tcp_sw_conn->sock;
  468. if (!sock)
  469. return;
  470. sock_hold(sock->sk);
  471. iscsi_sw_tcp_conn_restore_callbacks(tcp_sw_conn);
  472. sock_put(sock->sk);
  473. spin_lock_bh(&session->lock);
  474. tcp_sw_conn->sock = NULL;
  475. spin_unlock_bh(&session->lock);
  476. sockfd_put(sock);
  477. }
  478. static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
  479. {
  480. struct iscsi_conn *conn = cls_conn->dd_data;
  481. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  482. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  483. iscsi_sw_tcp_release_conn(conn);
  484. if (tcp_sw_conn->tx_hash.tfm)
  485. crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
  486. if (tcp_sw_conn->rx_hash.tfm)
  487. crypto_free_hash(tcp_sw_conn->rx_hash.tfm);
  488. iscsi_tcp_conn_teardown(cls_conn);
  489. }
  490. static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  491. {
  492. struct iscsi_conn *conn = cls_conn->dd_data;
  493. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  494. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  495. struct socket *sock = tcp_sw_conn->sock;
  496. /* userspace may have goofed up and not bound us */
  497. if (!sock)
  498. return;
  499. /*
  500. * Make sure our recv side is stopped.
  501. * Older tools called conn stop before ep_disconnect
  502. * so IO could still be coming in.
  503. */
  504. write_lock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
  505. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  506. write_unlock_bh(&tcp_sw_conn->sock->sk->sk_callback_lock);
  507. if (sk_sleep(sock->sk) && waitqueue_active(sk_sleep(sock->sk))) {
  508. sock->sk->sk_err = EIO;
  509. wake_up_interruptible(sk_sleep(sock->sk));
  510. }
  511. iscsi_conn_stop(cls_conn, flag);
  512. iscsi_sw_tcp_release_conn(conn);
  513. }
  514. static int iscsi_sw_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
  515. char *buf, int *port,
  516. int (*getname)(struct socket *,
  517. struct sockaddr *,
  518. int *addrlen))
  519. {
  520. struct sockaddr_storage *addr;
  521. struct sockaddr_in6 *sin6;
  522. struct sockaddr_in *sin;
  523. int rc = 0, len;
  524. addr = kmalloc(sizeof(*addr), GFP_KERNEL);
  525. if (!addr)
  526. return -ENOMEM;
  527. if (getname(sock, (struct sockaddr *) addr, &len)) {
  528. rc = -ENODEV;
  529. goto free_addr;
  530. }
  531. switch (addr->ss_family) {
  532. case AF_INET:
  533. sin = (struct sockaddr_in *)addr;
  534. spin_lock_bh(&conn->session->lock);
  535. sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
  536. *port = be16_to_cpu(sin->sin_port);
  537. spin_unlock_bh(&conn->session->lock);
  538. break;
  539. case AF_INET6:
  540. sin6 = (struct sockaddr_in6 *)addr;
  541. spin_lock_bh(&conn->session->lock);
  542. sprintf(buf, "%pI6", &sin6->sin6_addr);
  543. *port = be16_to_cpu(sin6->sin6_port);
  544. spin_unlock_bh(&conn->session->lock);
  545. break;
  546. }
  547. free_addr:
  548. kfree(addr);
  549. return rc;
  550. }
  551. static int
  552. iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,
  553. struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
  554. int is_leading)
  555. {
  556. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  557. struct iscsi_host *ihost = shost_priv(shost);
  558. struct iscsi_conn *conn = cls_conn->dd_data;
  559. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  560. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  561. struct sock *sk;
  562. struct socket *sock;
  563. int err;
  564. /* lookup for existing socket */
  565. sock = sockfd_lookup((int)transport_eph, &err);
  566. if (!sock) {
  567. iscsi_conn_printk(KERN_ERR, conn,
  568. "sockfd_lookup failed %d\n", err);
  569. return -EEXIST;
  570. }
  571. /*
  572. * copy these values now because if we drop the session
  573. * userspace may still want to query the values since we will
  574. * be using them for the reconnect
  575. */
  576. err = iscsi_sw_tcp_get_addr(conn, sock, conn->portal_address,
  577. &conn->portal_port, kernel_getpeername);
  578. if (err)
  579. goto free_socket;
  580. err = iscsi_sw_tcp_get_addr(conn, sock, ihost->local_address,
  581. &ihost->local_port, kernel_getsockname);
  582. if (err)
  583. goto free_socket;
  584. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  585. if (err)
  586. goto free_socket;
  587. /* bind iSCSI connection and socket */
  588. tcp_sw_conn->sock = sock;
  589. /* setup Socket parameters */
  590. sk = sock->sk;
  591. sk->sk_reuse = 1;
  592. sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
  593. sk->sk_allocation = GFP_ATOMIC;
  594. iscsi_sw_tcp_conn_set_callbacks(conn);
  595. tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
  596. /*
  597. * set receive state machine into initial state
  598. */
  599. iscsi_tcp_hdr_recv_prep(tcp_conn);
  600. return 0;
  601. free_socket:
  602. sockfd_put(sock);
  603. return err;
  604. }
  605. static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
  606. enum iscsi_param param, char *buf,
  607. int buflen)
  608. {
  609. struct iscsi_conn *conn = cls_conn->dd_data;
  610. struct iscsi_session *session = conn->session;
  611. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  612. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  613. int value;
  614. switch(param) {
  615. case ISCSI_PARAM_HDRDGST_EN:
  616. iscsi_set_param(cls_conn, param, buf, buflen);
  617. break;
  618. case ISCSI_PARAM_DATADGST_EN:
  619. iscsi_set_param(cls_conn, param, buf, buflen);
  620. tcp_sw_conn->sendpage = conn->datadgst_en ?
  621. sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
  622. break;
  623. case ISCSI_PARAM_MAX_R2T:
  624. sscanf(buf, "%d", &value);
  625. if (value <= 0 || !is_power_of_2(value))
  626. return -EINVAL;
  627. if (session->max_r2t == value)
  628. break;
  629. iscsi_tcp_r2tpool_free(session);
  630. iscsi_set_param(cls_conn, param, buf, buflen);
  631. if (iscsi_tcp_r2tpool_alloc(session))
  632. return -ENOMEM;
  633. break;
  634. default:
  635. return iscsi_set_param(cls_conn, param, buf, buflen);
  636. }
  637. return 0;
  638. }
  639. static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
  640. enum iscsi_param param, char *buf)
  641. {
  642. struct iscsi_conn *conn = cls_conn->dd_data;
  643. int len;
  644. switch(param) {
  645. case ISCSI_PARAM_CONN_PORT:
  646. spin_lock_bh(&conn->session->lock);
  647. len = sprintf(buf, "%hu\n", conn->portal_port);
  648. spin_unlock_bh(&conn->session->lock);
  649. break;
  650. case ISCSI_PARAM_CONN_ADDRESS:
  651. spin_lock_bh(&conn->session->lock);
  652. len = sprintf(buf, "%s\n", conn->portal_address);
  653. spin_unlock_bh(&conn->session->lock);
  654. break;
  655. default:
  656. return iscsi_conn_get_param(cls_conn, param, buf);
  657. }
  658. return len;
  659. }
  660. static void
  661. iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  662. struct iscsi_stats *stats)
  663. {
  664. struct iscsi_conn *conn = cls_conn->dd_data;
  665. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  666. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  667. stats->custom_length = 3;
  668. strcpy(stats->custom[0].desc, "tx_sendpage_failures");
  669. stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
  670. strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
  671. stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
  672. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  673. stats->custom[2].value = conn->eh_abort_cnt;
  674. iscsi_tcp_conn_get_stats(cls_conn, stats);
  675. }
  676. static struct iscsi_cls_session *
  677. iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
  678. uint16_t qdepth, uint32_t initial_cmdsn)
  679. {
  680. struct iscsi_cls_session *cls_session;
  681. struct iscsi_session *session;
  682. struct Scsi_Host *shost;
  683. if (ep) {
  684. printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
  685. return NULL;
  686. }
  687. shost = iscsi_host_alloc(&iscsi_sw_tcp_sht, 0, 1);
  688. if (!shost)
  689. return NULL;
  690. shost->transportt = iscsi_sw_tcp_scsi_transport;
  691. shost->cmd_per_lun = qdepth;
  692. shost->max_lun = iscsi_max_lun;
  693. shost->max_id = 0;
  694. shost->max_channel = 0;
  695. shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
  696. if (iscsi_host_add(shost, NULL))
  697. goto free_host;
  698. cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
  699. cmds_max, 0,
  700. sizeof(struct iscsi_tcp_task) +
  701. sizeof(struct iscsi_sw_tcp_hdrbuf),
  702. initial_cmdsn, 0);
  703. if (!cls_session)
  704. goto remove_host;
  705. session = cls_session->dd_data;
  706. shost->can_queue = session->scsi_cmds_max;
  707. if (iscsi_tcp_r2tpool_alloc(session))
  708. goto remove_session;
  709. return cls_session;
  710. remove_session:
  711. iscsi_session_teardown(cls_session);
  712. remove_host:
  713. iscsi_host_remove(shost);
  714. free_host:
  715. iscsi_host_free(shost);
  716. return NULL;
  717. }
  718. static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
  719. {
  720. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  721. iscsi_tcp_r2tpool_free(cls_session->dd_data);
  722. iscsi_session_teardown(cls_session);
  723. iscsi_host_remove(shost);
  724. iscsi_host_free(shost);
  725. }
  726. static int iscsi_sw_tcp_slave_alloc(struct scsi_device *sdev)
  727. {
  728. set_bit(QUEUE_FLAG_BIDI, &sdev->request_queue->queue_flags);
  729. return 0;
  730. }
  731. static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
  732. {
  733. blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
  734. blk_queue_dma_alignment(sdev->request_queue, 0);
  735. return 0;
  736. }
  737. static struct scsi_host_template iscsi_sw_tcp_sht = {
  738. .module = THIS_MODULE,
  739. .name = "iSCSI Initiator over TCP/IP",
  740. .queuecommand = iscsi_queuecommand,
  741. .change_queue_depth = iscsi_change_queue_depth,
  742. .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
  743. .sg_tablesize = 4096,
  744. .max_sectors = 0xFFFF,
  745. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  746. .eh_abort_handler = iscsi_eh_abort,
  747. .eh_device_reset_handler= iscsi_eh_device_reset,
  748. .eh_target_reset_handler = iscsi_eh_recover_target,
  749. .use_clustering = DISABLE_CLUSTERING,
  750. .slave_alloc = iscsi_sw_tcp_slave_alloc,
  751. .slave_configure = iscsi_sw_tcp_slave_configure,
  752. .target_alloc = iscsi_target_alloc,
  753. .proc_name = "iscsi_tcp",
  754. .this_id = -1,
  755. };
  756. static struct iscsi_transport iscsi_sw_tcp_transport = {
  757. .owner = THIS_MODULE,
  758. .name = "tcp",
  759. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  760. | CAP_DATADGST,
  761. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  762. ISCSI_MAX_XMIT_DLENGTH |
  763. ISCSI_HDRDGST_EN |
  764. ISCSI_DATADGST_EN |
  765. ISCSI_INITIAL_R2T_EN |
  766. ISCSI_MAX_R2T |
  767. ISCSI_IMM_DATA_EN |
  768. ISCSI_FIRST_BURST |
  769. ISCSI_MAX_BURST |
  770. ISCSI_PDU_INORDER_EN |
  771. ISCSI_DATASEQ_INORDER_EN |
  772. ISCSI_ERL |
  773. ISCSI_CONN_PORT |
  774. ISCSI_CONN_ADDRESS |
  775. ISCSI_EXP_STATSN |
  776. ISCSI_PERSISTENT_PORT |
  777. ISCSI_PERSISTENT_ADDRESS |
  778. ISCSI_TARGET_NAME | ISCSI_TPGT |
  779. ISCSI_USERNAME | ISCSI_PASSWORD |
  780. ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
  781. ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
  782. ISCSI_LU_RESET_TMO | ISCSI_TGT_RESET_TMO |
  783. ISCSI_PING_TMO | ISCSI_RECV_TMO |
  784. ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
  785. .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
  786. ISCSI_HOST_INITIATOR_NAME |
  787. ISCSI_HOST_NETDEV_NAME,
  788. /* session management */
  789. .create_session = iscsi_sw_tcp_session_create,
  790. .destroy_session = iscsi_sw_tcp_session_destroy,
  791. /* connection management */
  792. .create_conn = iscsi_sw_tcp_conn_create,
  793. .bind_conn = iscsi_sw_tcp_conn_bind,
  794. .destroy_conn = iscsi_sw_tcp_conn_destroy,
  795. .set_param = iscsi_sw_tcp_conn_set_param,
  796. .get_conn_param = iscsi_sw_tcp_conn_get_param,
  797. .get_session_param = iscsi_session_get_param,
  798. .start_conn = iscsi_conn_start,
  799. .stop_conn = iscsi_sw_tcp_conn_stop,
  800. /* iscsi host params */
  801. .get_host_param = iscsi_host_get_param,
  802. .set_host_param = iscsi_host_set_param,
  803. /* IO */
  804. .send_pdu = iscsi_conn_send_pdu,
  805. .get_stats = iscsi_sw_tcp_conn_get_stats,
  806. /* iscsi task/cmd helpers */
  807. .init_task = iscsi_tcp_task_init,
  808. .xmit_task = iscsi_tcp_task_xmit,
  809. .cleanup_task = iscsi_tcp_cleanup_task,
  810. /* low level pdu helpers */
  811. .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
  812. .init_pdu = iscsi_sw_tcp_pdu_init,
  813. .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
  814. /* recovery */
  815. .session_recovery_timedout = iscsi_session_recovery_timedout,
  816. };
  817. static int __init iscsi_sw_tcp_init(void)
  818. {
  819. if (iscsi_max_lun < 1) {
  820. printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
  821. iscsi_max_lun);
  822. return -EINVAL;
  823. }
  824. iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
  825. &iscsi_sw_tcp_transport);
  826. if (!iscsi_sw_tcp_scsi_transport)
  827. return -ENODEV;
  828. return 0;
  829. }
  830. static void __exit iscsi_sw_tcp_exit(void)
  831. {
  832. iscsi_unregister_transport(&iscsi_sw_tcp_transport);
  833. }
  834. module_init(iscsi_sw_tcp_init);
  835. module_exit(iscsi_sw_tcp_exit);