iscsi_tcp.c 25 KB

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