iscsi_tcp.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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/file.h>
  33. #include <linux/blkdev.h>
  34. #include <linux/delay.h>
  35. #include <linux/kfifo.h>
  36. #include <linux/scatterlist.h>
  37. #include <linux/module.h>
  38. #include <net/tcp.h>
  39. #include <scsi/scsi_cmnd.h>
  40. #include <scsi/scsi_device.h>
  41. #include <scsi/scsi_host.h>
  42. #include <scsi/scsi.h>
  43. #include <scsi/scsi_transport_iscsi.h>
  44. #include "iscsi_tcp.h"
  45. MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
  46. "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
  47. "Alex Aizman <itn780@yahoo.com>");
  48. MODULE_DESCRIPTION("iSCSI/TCP data-path");
  49. MODULE_LICENSE("GPL");
  50. static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport;
  51. static struct scsi_host_template iscsi_sw_tcp_sht;
  52. static struct iscsi_transport iscsi_sw_tcp_transport;
  53. static unsigned int iscsi_max_lun = ~0;
  54. module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
  55. static int iscsi_sw_tcp_dbg;
  56. module_param_named(debug_iscsi_tcp, iscsi_sw_tcp_dbg, int,
  57. S_IRUGO | S_IWUSR);
  58. MODULE_PARM_DESC(debug_iscsi_tcp, "Turn on debugging for iscsi_tcp module "
  59. "Set to 1 to turn on, and zero to turn off. Default is off.");
  60. #define ISCSI_SW_TCP_DBG(_conn, dbg_fmt, arg...) \
  61. do { \
  62. if (iscsi_sw_tcp_dbg) \
  63. iscsi_conn_printk(KERN_INFO, _conn, \
  64. "%s " dbg_fmt, \
  65. __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_data_ready(struct sock *sk)
  114. {
  115. struct iscsi_conn *conn;
  116. struct iscsi_tcp_conn *tcp_conn;
  117. read_descriptor_t rd_desc;
  118. read_lock_bh(&sk->sk_callback_lock);
  119. conn = sk->sk_user_data;
  120. if (!conn) {
  121. read_unlock_bh(&sk->sk_callback_lock);
  122. return;
  123. }
  124. tcp_conn = conn->dd_data;
  125. /*
  126. * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
  127. * We set count to 1 because we want the network layer to
  128. * hand us all the skbs that are available. iscsi_tcp_recv
  129. * handled pdus that cross buffers or pdus that still need data.
  130. */
  131. rd_desc.arg.data = conn;
  132. rd_desc.count = 1;
  133. tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
  134. iscsi_sw_sk_state_check(sk);
  135. /* If we had to (atomically) map a highmem page,
  136. * unmap it now. */
  137. iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
  138. read_unlock_bh(&sk->sk_callback_lock);
  139. }
  140. static void iscsi_sw_tcp_state_change(struct sock *sk)
  141. {
  142. struct iscsi_tcp_conn *tcp_conn;
  143. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  144. struct iscsi_conn *conn;
  145. struct iscsi_session *session;
  146. void (*old_state_change)(struct sock *);
  147. read_lock_bh(&sk->sk_callback_lock);
  148. conn = sk->sk_user_data;
  149. if (!conn) {
  150. read_unlock_bh(&sk->sk_callback_lock);
  151. return;
  152. }
  153. session = conn->session;
  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. **/
  270. static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
  271. {
  272. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  273. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  274. struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
  275. unsigned int consumed = 0;
  276. int rc = 0;
  277. while (1) {
  278. rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
  279. /*
  280. * We may not have been able to send data because the conn
  281. * is getting stopped. libiscsi will know so propagate err
  282. * for it to do the right thing.
  283. */
  284. if (rc == -EAGAIN)
  285. return rc;
  286. else if (rc < 0) {
  287. rc = ISCSI_ERR_XMIT_FAILED;
  288. goto error;
  289. } else if (rc == 0)
  290. break;
  291. consumed += rc;
  292. if (segment->total_copied >= segment->total_size) {
  293. if (segment->done != NULL) {
  294. rc = segment->done(tcp_conn, segment);
  295. if (rc != 0)
  296. goto error;
  297. }
  298. }
  299. }
  300. ISCSI_SW_TCP_DBG(conn, "xmit %d bytes\n", consumed);
  301. conn->txdata_octets += consumed;
  302. return consumed;
  303. error:
  304. /* Transmit error. We could initiate error recovery
  305. * here. */
  306. ISCSI_SW_TCP_DBG(conn, "Error sending PDU, errno=%d\n", rc);
  307. iscsi_conn_failure(conn, rc);
  308. return -EIO;
  309. }
  310. /**
  311. * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
  312. */
  313. static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
  314. {
  315. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  316. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  317. struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
  318. return segment->total_copied - segment->total_size;
  319. }
  320. static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
  321. {
  322. struct iscsi_conn *conn = task->conn;
  323. unsigned long pflags = current->flags;
  324. int rc = 0;
  325. current->flags |= PF_MEMALLOC;
  326. while (iscsi_sw_tcp_xmit_qlen(conn)) {
  327. rc = iscsi_sw_tcp_xmit(conn);
  328. if (rc == 0) {
  329. rc = -EAGAIN;
  330. break;
  331. }
  332. if (rc < 0)
  333. break;
  334. rc = 0;
  335. }
  336. tsk_restore_flags(current, pflags, PF_MEMALLOC);
  337. return rc;
  338. }
  339. /*
  340. * This is called when we're done sending the header.
  341. * Simply copy the data_segment to the send segment, and return.
  342. */
  343. static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
  344. struct iscsi_segment *segment)
  345. {
  346. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  347. tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment;
  348. ISCSI_SW_TCP_DBG(tcp_conn->iscsi_conn,
  349. "Header done. Next segment size %u total_size %u\n",
  350. tcp_sw_conn->out.segment.size,
  351. tcp_sw_conn->out.segment.total_size);
  352. return 0;
  353. }
  354. static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
  355. size_t hdrlen)
  356. {
  357. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  358. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  359. ISCSI_SW_TCP_DBG(conn, "%s\n", conn->hdrdgst_en ?
  360. "digest enabled" : "digest disabled");
  361. /* Clear the data segment - needs to be filled in by the
  362. * caller using iscsi_tcp_send_data_prep() */
  363. memset(&tcp_sw_conn->out.data_segment, 0,
  364. sizeof(struct iscsi_segment));
  365. /* If header digest is enabled, compute the CRC and
  366. * place the digest into the same buffer. We make
  367. * sure that both iscsi_tcp_task and mtask have
  368. * sufficient room.
  369. */
  370. if (conn->hdrdgst_en) {
  371. iscsi_tcp_dgst_header(tcp_sw_conn->tx_hash, hdr, hdrlen,
  372. hdr + hdrlen);
  373. hdrlen += ISCSI_DIGEST_SIZE;
  374. }
  375. /* Remember header pointer for later, when we need
  376. * to decide whether there's a payload to go along
  377. * with the header. */
  378. tcp_sw_conn->out.hdr = hdr;
  379. iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
  380. iscsi_sw_tcp_send_hdr_done, NULL);
  381. }
  382. /*
  383. * Prepare the send buffer for the payload data.
  384. * Padding and checksumming will all be taken care
  385. * of by the iscsi_segment routines.
  386. */
  387. static int
  388. iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
  389. unsigned int count, unsigned int offset,
  390. unsigned int len)
  391. {
  392. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  393. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  394. struct ahash_request *tx_hash = NULL;
  395. unsigned int hdr_spec_len;
  396. ISCSI_SW_TCP_DBG(conn, "offset=%d, datalen=%d %s\n", offset, len,
  397. conn->datadgst_en ?
  398. "digest enabled" : "digest disabled");
  399. /* Make sure the datalen matches what the caller
  400. said he would send. */
  401. hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
  402. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  403. if (conn->datadgst_en)
  404. tx_hash = tcp_sw_conn->tx_hash;
  405. return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
  406. sg, count, offset, len,
  407. NULL, tx_hash);
  408. }
  409. static void
  410. iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
  411. size_t len)
  412. {
  413. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  414. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  415. struct ahash_request *tx_hash = NULL;
  416. unsigned int hdr_spec_len;
  417. ISCSI_SW_TCP_DBG(conn, "datalen=%zd %s\n", len, conn->datadgst_en ?
  418. "digest enabled" : "digest disabled");
  419. /* Make sure the datalen matches what the caller
  420. said he would send. */
  421. hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
  422. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  423. if (conn->datadgst_en)
  424. tx_hash = tcp_sw_conn->tx_hash;
  425. iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
  426. data, len, NULL, tx_hash);
  427. }
  428. static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
  429. unsigned int offset, unsigned int count)
  430. {
  431. struct iscsi_conn *conn = task->conn;
  432. int err = 0;
  433. iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
  434. if (!count)
  435. return 0;
  436. if (!task->sc)
  437. iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
  438. else {
  439. struct scsi_data_buffer *sdb = scsi_out(task->sc);
  440. err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
  441. sdb->table.nents, offset,
  442. count);
  443. }
  444. if (err) {
  445. /* got invalid offset/len */
  446. return -EIO;
  447. }
  448. return 0;
  449. }
  450. static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
  451. {
  452. struct iscsi_tcp_task *tcp_task = task->dd_data;
  453. task->hdr = task->dd_data + sizeof(*tcp_task);
  454. task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
  455. return 0;
  456. }
  457. static struct iscsi_cls_conn *
  458. iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
  459. uint32_t conn_idx)
  460. {
  461. struct iscsi_conn *conn;
  462. struct iscsi_cls_conn *cls_conn;
  463. struct iscsi_tcp_conn *tcp_conn;
  464. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  465. struct crypto_ahash *tfm;
  466. cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
  467. conn_idx);
  468. if (!cls_conn)
  469. return NULL;
  470. conn = cls_conn->dd_data;
  471. tcp_conn = conn->dd_data;
  472. tcp_sw_conn = tcp_conn->dd_data;
  473. tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
  474. if (IS_ERR(tfm))
  475. goto free_conn;
  476. tcp_sw_conn->tx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
  477. if (!tcp_sw_conn->tx_hash)
  478. goto free_tfm;
  479. ahash_request_set_callback(tcp_sw_conn->tx_hash, 0, NULL, NULL);
  480. tcp_sw_conn->rx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
  481. if (!tcp_sw_conn->rx_hash)
  482. goto free_tx_hash;
  483. ahash_request_set_callback(tcp_sw_conn->rx_hash, 0, NULL, NULL);
  484. tcp_conn->rx_hash = tcp_sw_conn->rx_hash;
  485. return cls_conn;
  486. free_tx_hash:
  487. ahash_request_free(tcp_sw_conn->tx_hash);
  488. free_tfm:
  489. crypto_free_ahash(tfm);
  490. free_conn:
  491. iscsi_conn_printk(KERN_ERR, conn,
  492. "Could not create connection due to crc32c "
  493. "loading error. Make sure the crc32c "
  494. "module is built as a module or into the "
  495. "kernel\n");
  496. iscsi_tcp_conn_teardown(cls_conn);
  497. return NULL;
  498. }
  499. static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
  500. {
  501. struct iscsi_session *session = conn->session;
  502. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  503. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  504. struct socket *sock = tcp_sw_conn->sock;
  505. if (!sock)
  506. return;
  507. sock_hold(sock->sk);
  508. iscsi_sw_tcp_conn_restore_callbacks(conn);
  509. sock_put(sock->sk);
  510. spin_lock_bh(&session->frwd_lock);
  511. tcp_sw_conn->sock = NULL;
  512. spin_unlock_bh(&session->frwd_lock);
  513. sockfd_put(sock);
  514. }
  515. static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
  516. {
  517. struct iscsi_conn *conn = cls_conn->dd_data;
  518. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  519. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  520. iscsi_sw_tcp_release_conn(conn);
  521. ahash_request_free(tcp_sw_conn->rx_hash);
  522. if (tcp_sw_conn->tx_hash) {
  523. struct crypto_ahash *tfm;
  524. tfm = crypto_ahash_reqtfm(tcp_sw_conn->tx_hash);
  525. ahash_request_free(tcp_sw_conn->tx_hash);
  526. crypto_free_ahash(tfm);
  527. }
  528. iscsi_tcp_conn_teardown(cls_conn);
  529. }
  530. static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  531. {
  532. struct iscsi_conn *conn = cls_conn->dd_data;
  533. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  534. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  535. struct socket *sock = tcp_sw_conn->sock;
  536. /* userspace may have goofed up and not bound us */
  537. if (!sock)
  538. return;
  539. sock->sk->sk_err = EIO;
  540. wake_up_interruptible(sk_sleep(sock->sk));
  541. /* stop xmit side */
  542. iscsi_suspend_tx(conn);
  543. /* stop recv side and release socket */
  544. iscsi_sw_tcp_release_conn(conn);
  545. iscsi_conn_stop(cls_conn, flag);
  546. }
  547. static int
  548. iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,
  549. struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
  550. int is_leading)
  551. {
  552. struct iscsi_session *session = cls_session->dd_data;
  553. struct iscsi_conn *conn = cls_conn->dd_data;
  554. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  555. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  556. struct sock *sk;
  557. struct socket *sock;
  558. int err;
  559. /* lookup for existing socket */
  560. sock = sockfd_lookup((int)transport_eph, &err);
  561. if (!sock) {
  562. iscsi_conn_printk(KERN_ERR, conn,
  563. "sockfd_lookup failed %d\n", err);
  564. return -EEXIST;
  565. }
  566. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  567. if (err)
  568. goto free_socket;
  569. spin_lock_bh(&session->frwd_lock);
  570. /* bind iSCSI connection and socket */
  571. tcp_sw_conn->sock = sock;
  572. spin_unlock_bh(&session->frwd_lock);
  573. /* setup Socket parameters */
  574. sk = sock->sk;
  575. sk->sk_reuse = SK_CAN_REUSE;
  576. sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
  577. sk->sk_allocation = GFP_ATOMIC;
  578. sk_set_memalloc(sk);
  579. iscsi_sw_tcp_conn_set_callbacks(conn);
  580. tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
  581. /*
  582. * set receive state machine into initial state
  583. */
  584. iscsi_tcp_hdr_recv_prep(tcp_conn);
  585. return 0;
  586. free_socket:
  587. sockfd_put(sock);
  588. return err;
  589. }
  590. static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
  591. enum iscsi_param param, char *buf,
  592. int buflen)
  593. {
  594. struct iscsi_conn *conn = cls_conn->dd_data;
  595. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  596. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  597. switch(param) {
  598. case ISCSI_PARAM_HDRDGST_EN:
  599. iscsi_set_param(cls_conn, param, buf, buflen);
  600. break;
  601. case ISCSI_PARAM_DATADGST_EN:
  602. iscsi_set_param(cls_conn, param, buf, buflen);
  603. tcp_sw_conn->sendpage = conn->datadgst_en ?
  604. sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
  605. break;
  606. case ISCSI_PARAM_MAX_R2T:
  607. return iscsi_tcp_set_max_r2t(conn, buf);
  608. default:
  609. return iscsi_set_param(cls_conn, param, buf, buflen);
  610. }
  611. return 0;
  612. }
  613. static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
  614. enum iscsi_param param, char *buf)
  615. {
  616. struct iscsi_conn *conn = cls_conn->dd_data;
  617. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  618. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  619. struct sockaddr_in6 addr;
  620. int rc, len;
  621. switch(param) {
  622. case ISCSI_PARAM_CONN_PORT:
  623. case ISCSI_PARAM_CONN_ADDRESS:
  624. case ISCSI_PARAM_LOCAL_PORT:
  625. spin_lock_bh(&conn->session->frwd_lock);
  626. if (!tcp_sw_conn || !tcp_sw_conn->sock) {
  627. spin_unlock_bh(&conn->session->frwd_lock);
  628. return -ENOTCONN;
  629. }
  630. if (param == ISCSI_PARAM_LOCAL_PORT)
  631. rc = kernel_getsockname(tcp_sw_conn->sock,
  632. (struct sockaddr *)&addr, &len);
  633. else
  634. rc = kernel_getpeername(tcp_sw_conn->sock,
  635. (struct sockaddr *)&addr, &len);
  636. spin_unlock_bh(&conn->session->frwd_lock);
  637. if (rc)
  638. return rc;
  639. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  640. &addr, param, buf);
  641. default:
  642. return iscsi_conn_get_param(cls_conn, param, buf);
  643. }
  644. return 0;
  645. }
  646. static int iscsi_sw_tcp_host_get_param(struct Scsi_Host *shost,
  647. enum iscsi_host_param param, char *buf)
  648. {
  649. struct iscsi_sw_tcp_host *tcp_sw_host = iscsi_host_priv(shost);
  650. struct iscsi_session *session = tcp_sw_host->session;
  651. struct iscsi_conn *conn;
  652. struct iscsi_tcp_conn *tcp_conn;
  653. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  654. struct sockaddr_in6 addr;
  655. int rc, len;
  656. switch (param) {
  657. case ISCSI_HOST_PARAM_IPADDRESS:
  658. if (!session)
  659. return -ENOTCONN;
  660. spin_lock_bh(&session->frwd_lock);
  661. conn = session->leadconn;
  662. if (!conn) {
  663. spin_unlock_bh(&session->frwd_lock);
  664. return -ENOTCONN;
  665. }
  666. tcp_conn = conn->dd_data;
  667. tcp_sw_conn = tcp_conn->dd_data;
  668. if (!tcp_sw_conn->sock) {
  669. spin_unlock_bh(&session->frwd_lock);
  670. return -ENOTCONN;
  671. }
  672. rc = kernel_getsockname(tcp_sw_conn->sock,
  673. (struct sockaddr *)&addr, &len);
  674. spin_unlock_bh(&session->frwd_lock);
  675. if (rc)
  676. return rc;
  677. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  678. &addr, param, buf);
  679. default:
  680. return iscsi_host_get_param(shost, param, buf);
  681. }
  682. return 0;
  683. }
  684. static void
  685. iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  686. struct iscsi_stats *stats)
  687. {
  688. struct iscsi_conn *conn = cls_conn->dd_data;
  689. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  690. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  691. stats->custom_length = 3;
  692. strcpy(stats->custom[0].desc, "tx_sendpage_failures");
  693. stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
  694. strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
  695. stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
  696. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  697. stats->custom[2].value = conn->eh_abort_cnt;
  698. iscsi_tcp_conn_get_stats(cls_conn, stats);
  699. }
  700. static struct iscsi_cls_session *
  701. iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
  702. uint16_t qdepth, uint32_t initial_cmdsn)
  703. {
  704. struct iscsi_cls_session *cls_session;
  705. struct iscsi_session *session;
  706. struct iscsi_sw_tcp_host *tcp_sw_host;
  707. struct Scsi_Host *shost;
  708. if (ep) {
  709. printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
  710. return NULL;
  711. }
  712. shost = iscsi_host_alloc(&iscsi_sw_tcp_sht,
  713. sizeof(struct iscsi_sw_tcp_host), 1);
  714. if (!shost)
  715. return NULL;
  716. shost->transportt = iscsi_sw_tcp_scsi_transport;
  717. shost->cmd_per_lun = qdepth;
  718. shost->max_lun = iscsi_max_lun;
  719. shost->max_id = 0;
  720. shost->max_channel = 0;
  721. shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
  722. if (iscsi_host_add(shost, NULL))
  723. goto free_host;
  724. cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
  725. cmds_max, 0,
  726. sizeof(struct iscsi_tcp_task) +
  727. sizeof(struct iscsi_sw_tcp_hdrbuf),
  728. initial_cmdsn, 0);
  729. if (!cls_session)
  730. goto remove_host;
  731. session = cls_session->dd_data;
  732. tcp_sw_host = iscsi_host_priv(shost);
  733. tcp_sw_host->session = session;
  734. shost->can_queue = session->scsi_cmds_max;
  735. if (iscsi_tcp_r2tpool_alloc(session))
  736. goto remove_session;
  737. return cls_session;
  738. remove_session:
  739. iscsi_session_teardown(cls_session);
  740. remove_host:
  741. iscsi_host_remove(shost);
  742. free_host:
  743. iscsi_host_free(shost);
  744. return NULL;
  745. }
  746. static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
  747. {
  748. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  749. iscsi_tcp_r2tpool_free(cls_session->dd_data);
  750. iscsi_session_teardown(cls_session);
  751. iscsi_host_remove(shost);
  752. iscsi_host_free(shost);
  753. }
  754. static umode_t iscsi_sw_tcp_attr_is_visible(int param_type, int param)
  755. {
  756. switch (param_type) {
  757. case ISCSI_HOST_PARAM:
  758. switch (param) {
  759. case ISCSI_HOST_PARAM_NETDEV_NAME:
  760. case ISCSI_HOST_PARAM_HWADDRESS:
  761. case ISCSI_HOST_PARAM_IPADDRESS:
  762. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  763. return S_IRUGO;
  764. default:
  765. return 0;
  766. }
  767. case ISCSI_PARAM:
  768. switch (param) {
  769. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  770. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  771. case ISCSI_PARAM_HDRDGST_EN:
  772. case ISCSI_PARAM_DATADGST_EN:
  773. case ISCSI_PARAM_CONN_ADDRESS:
  774. case ISCSI_PARAM_CONN_PORT:
  775. case ISCSI_PARAM_LOCAL_PORT:
  776. case ISCSI_PARAM_EXP_STATSN:
  777. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  778. case ISCSI_PARAM_PERSISTENT_PORT:
  779. case ISCSI_PARAM_PING_TMO:
  780. case ISCSI_PARAM_RECV_TMO:
  781. case ISCSI_PARAM_INITIAL_R2T_EN:
  782. case ISCSI_PARAM_MAX_R2T:
  783. case ISCSI_PARAM_IMM_DATA_EN:
  784. case ISCSI_PARAM_FIRST_BURST:
  785. case ISCSI_PARAM_MAX_BURST:
  786. case ISCSI_PARAM_PDU_INORDER_EN:
  787. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  788. case ISCSI_PARAM_ERL:
  789. case ISCSI_PARAM_TARGET_NAME:
  790. case ISCSI_PARAM_TPGT:
  791. case ISCSI_PARAM_USERNAME:
  792. case ISCSI_PARAM_PASSWORD:
  793. case ISCSI_PARAM_USERNAME_IN:
  794. case ISCSI_PARAM_PASSWORD_IN:
  795. case ISCSI_PARAM_FAST_ABORT:
  796. case ISCSI_PARAM_ABORT_TMO:
  797. case ISCSI_PARAM_LU_RESET_TMO:
  798. case ISCSI_PARAM_TGT_RESET_TMO:
  799. case ISCSI_PARAM_IFACE_NAME:
  800. case ISCSI_PARAM_INITIATOR_NAME:
  801. return S_IRUGO;
  802. default:
  803. return 0;
  804. }
  805. }
  806. return 0;
  807. }
  808. static int iscsi_sw_tcp_slave_alloc(struct scsi_device *sdev)
  809. {
  810. set_bit(QUEUE_FLAG_BIDI, &sdev->request_queue->queue_flags);
  811. return 0;
  812. }
  813. static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
  814. {
  815. blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
  816. blk_queue_dma_alignment(sdev->request_queue, 0);
  817. return 0;
  818. }
  819. static struct scsi_host_template iscsi_sw_tcp_sht = {
  820. .module = THIS_MODULE,
  821. .name = "iSCSI Initiator over TCP/IP",
  822. .queuecommand = iscsi_queuecommand,
  823. .change_queue_depth = scsi_change_queue_depth,
  824. .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
  825. .sg_tablesize = 4096,
  826. .max_sectors = 0xFFFF,
  827. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  828. .eh_abort_handler = iscsi_eh_abort,
  829. .eh_device_reset_handler= iscsi_eh_device_reset,
  830. .eh_target_reset_handler = iscsi_eh_recover_target,
  831. .use_clustering = DISABLE_CLUSTERING,
  832. .slave_alloc = iscsi_sw_tcp_slave_alloc,
  833. .slave_configure = iscsi_sw_tcp_slave_configure,
  834. .target_alloc = iscsi_target_alloc,
  835. .proc_name = "iscsi_tcp",
  836. .this_id = -1,
  837. .track_queue_depth = 1,
  838. };
  839. static struct iscsi_transport iscsi_sw_tcp_transport = {
  840. .owner = THIS_MODULE,
  841. .name = "tcp",
  842. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  843. | CAP_DATADGST,
  844. /* session management */
  845. .create_session = iscsi_sw_tcp_session_create,
  846. .destroy_session = iscsi_sw_tcp_session_destroy,
  847. /* connection management */
  848. .create_conn = iscsi_sw_tcp_conn_create,
  849. .bind_conn = iscsi_sw_tcp_conn_bind,
  850. .destroy_conn = iscsi_sw_tcp_conn_destroy,
  851. .attr_is_visible = iscsi_sw_tcp_attr_is_visible,
  852. .set_param = iscsi_sw_tcp_conn_set_param,
  853. .get_conn_param = iscsi_sw_tcp_conn_get_param,
  854. .get_session_param = iscsi_session_get_param,
  855. .start_conn = iscsi_conn_start,
  856. .stop_conn = iscsi_sw_tcp_conn_stop,
  857. /* iscsi host params */
  858. .get_host_param = iscsi_sw_tcp_host_get_param,
  859. .set_host_param = iscsi_host_set_param,
  860. /* IO */
  861. .send_pdu = iscsi_conn_send_pdu,
  862. .get_stats = iscsi_sw_tcp_conn_get_stats,
  863. /* iscsi task/cmd helpers */
  864. .init_task = iscsi_tcp_task_init,
  865. .xmit_task = iscsi_tcp_task_xmit,
  866. .cleanup_task = iscsi_tcp_cleanup_task,
  867. /* low level pdu helpers */
  868. .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
  869. .init_pdu = iscsi_sw_tcp_pdu_init,
  870. .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
  871. /* recovery */
  872. .session_recovery_timedout = iscsi_session_recovery_timedout,
  873. };
  874. static int __init iscsi_sw_tcp_init(void)
  875. {
  876. if (iscsi_max_lun < 1) {
  877. printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
  878. iscsi_max_lun);
  879. return -EINVAL;
  880. }
  881. iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
  882. &iscsi_sw_tcp_transport);
  883. if (!iscsi_sw_tcp_scsi_transport)
  884. return -ENODEV;
  885. return 0;
  886. }
  887. static void __exit iscsi_sw_tcp_exit(void)
  888. {
  889. iscsi_unregister_transport(&iscsi_sw_tcp_transport);
  890. }
  891. module_init(iscsi_sw_tcp_init);
  892. module_exit(iscsi_sw_tcp_exit);