iscsi_tcp.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 <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. !atomic_read(&sk->sk_rmem_alloc)) {
  106. ISCSI_SW_TCP_DBG(conn, "TCP_CLOSE|TCP_CLOSE_WAIT\n");
  107. iscsi_conn_failure(conn, ISCSI_ERR_TCP_CONN_CLOSE);
  108. return -ECONNRESET;
  109. }
  110. return 0;
  111. }
  112. static void iscsi_sw_tcp_data_ready(struct sock *sk, int flag)
  113. {
  114. struct iscsi_conn *conn;
  115. struct iscsi_tcp_conn *tcp_conn;
  116. read_descriptor_t rd_desc;
  117. read_lock(&sk->sk_callback_lock);
  118. conn = sk->sk_user_data;
  119. if (!conn) {
  120. read_unlock(&sk->sk_callback_lock);
  121. return;
  122. }
  123. tcp_conn = conn->dd_data;
  124. /*
  125. * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
  126. * We set count to 1 because we want the network layer to
  127. * hand us all the skbs that are available. iscsi_tcp_recv
  128. * handled pdus that cross buffers or pdus that still need data.
  129. */
  130. rd_desc.arg.data = conn;
  131. rd_desc.count = 1;
  132. tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
  133. iscsi_sw_sk_state_check(sk);
  134. /* If we had to (atomically) map a highmem page,
  135. * unmap it now. */
  136. iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
  137. read_unlock(&sk->sk_callback_lock);
  138. }
  139. static void iscsi_sw_tcp_state_change(struct sock *sk)
  140. {
  141. struct iscsi_tcp_conn *tcp_conn;
  142. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  143. struct iscsi_conn *conn;
  144. struct iscsi_session *session;
  145. void (*old_state_change)(struct sock *);
  146. read_lock(&sk->sk_callback_lock);
  147. conn = sk->sk_user_data;
  148. if (!conn) {
  149. read_unlock(&sk->sk_callback_lock);
  150. return;
  151. }
  152. session = conn->session;
  153. iscsi_sw_sk_state_check(sk);
  154. tcp_conn = conn->dd_data;
  155. tcp_sw_conn = tcp_conn->dd_data;
  156. old_state_change = tcp_sw_conn->old_state_change;
  157. read_unlock(&sk->sk_callback_lock);
  158. old_state_change(sk);
  159. }
  160. /**
  161. * iscsi_write_space - Called when more output buffer space is available
  162. * @sk: socket space is available for
  163. **/
  164. static void iscsi_sw_tcp_write_space(struct sock *sk)
  165. {
  166. struct iscsi_conn *conn;
  167. struct iscsi_tcp_conn *tcp_conn;
  168. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  169. void (*old_write_space)(struct sock *);
  170. read_lock_bh(&sk->sk_callback_lock);
  171. conn = sk->sk_user_data;
  172. if (!conn) {
  173. read_unlock_bh(&sk->sk_callback_lock);
  174. return;
  175. }
  176. tcp_conn = conn->dd_data;
  177. tcp_sw_conn = tcp_conn->dd_data;
  178. old_write_space = tcp_sw_conn->old_write_space;
  179. read_unlock_bh(&sk->sk_callback_lock);
  180. old_write_space(sk);
  181. ISCSI_SW_TCP_DBG(conn, "iscsi_write_space\n");
  182. iscsi_conn_queue_work(conn);
  183. }
  184. static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn)
  185. {
  186. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  187. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  188. struct sock *sk = tcp_sw_conn->sock->sk;
  189. /* assign new callbacks */
  190. write_lock_bh(&sk->sk_callback_lock);
  191. sk->sk_user_data = conn;
  192. tcp_sw_conn->old_data_ready = sk->sk_data_ready;
  193. tcp_sw_conn->old_state_change = sk->sk_state_change;
  194. tcp_sw_conn->old_write_space = sk->sk_write_space;
  195. sk->sk_data_ready = iscsi_sw_tcp_data_ready;
  196. sk->sk_state_change = iscsi_sw_tcp_state_change;
  197. sk->sk_write_space = iscsi_sw_tcp_write_space;
  198. write_unlock_bh(&sk->sk_callback_lock);
  199. }
  200. static void
  201. iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_conn *conn)
  202. {
  203. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  204. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  205. struct sock *sk = tcp_sw_conn->sock->sk;
  206. /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
  207. write_lock_bh(&sk->sk_callback_lock);
  208. sk->sk_user_data = NULL;
  209. sk->sk_data_ready = tcp_sw_conn->old_data_ready;
  210. sk->sk_state_change = tcp_sw_conn->old_state_change;
  211. sk->sk_write_space = tcp_sw_conn->old_write_space;
  212. sk->sk_no_check = 0;
  213. write_unlock_bh(&sk->sk_callback_lock);
  214. }
  215. /**
  216. * iscsi_sw_tcp_xmit_segment - transmit segment
  217. * @tcp_conn: the iSCSI TCP connection
  218. * @segment: the buffer to transmnit
  219. *
  220. * This function transmits as much of the buffer as
  221. * the network layer will accept, and returns the number of
  222. * bytes transmitted.
  223. *
  224. * If CRC hashing is enabled, the function will compute the
  225. * hash as it goes. When the entire segment has been transmitted,
  226. * it will retrieve the hash value and send it as well.
  227. */
  228. static int iscsi_sw_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
  229. struct iscsi_segment *segment)
  230. {
  231. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  232. struct socket *sk = tcp_sw_conn->sock;
  233. unsigned int copied = 0;
  234. int r = 0;
  235. while (!iscsi_tcp_segment_done(tcp_conn, segment, 0, r)) {
  236. struct scatterlist *sg;
  237. unsigned int offset, copy;
  238. int flags = 0;
  239. r = 0;
  240. offset = segment->copied;
  241. copy = segment->size - offset;
  242. if (segment->total_copied + segment->size < segment->total_size)
  243. flags |= MSG_MORE;
  244. /* Use sendpage if we can; else fall back to sendmsg */
  245. if (!segment->data) {
  246. sg = segment->sg;
  247. offset += segment->sg_offset + sg->offset;
  248. r = tcp_sw_conn->sendpage(sk, sg_page(sg), offset,
  249. copy, flags);
  250. } else {
  251. struct msghdr msg = { .msg_flags = flags };
  252. struct kvec iov = {
  253. .iov_base = segment->data + offset,
  254. .iov_len = copy
  255. };
  256. r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
  257. }
  258. if (r < 0) {
  259. iscsi_tcp_segment_unmap(segment);
  260. return r;
  261. }
  262. copied += r;
  263. }
  264. return copied;
  265. }
  266. /**
  267. * iscsi_sw_tcp_xmit - TCP transmit
  268. **/
  269. static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
  270. {
  271. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  272. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  273. struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
  274. unsigned int consumed = 0;
  275. int rc = 0;
  276. while (1) {
  277. rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
  278. /*
  279. * We may not have been able to send data because the conn
  280. * is getting stopped. libiscsi will know so propagate err
  281. * for it to do the right thing.
  282. */
  283. if (rc == -EAGAIN)
  284. return rc;
  285. else if (rc < 0) {
  286. rc = ISCSI_ERR_XMIT_FAILED;
  287. goto error;
  288. } else if (rc == 0)
  289. break;
  290. consumed += rc;
  291. if (segment->total_copied >= segment->total_size) {
  292. if (segment->done != NULL) {
  293. rc = segment->done(tcp_conn, segment);
  294. if (rc != 0)
  295. goto error;
  296. }
  297. }
  298. }
  299. ISCSI_SW_TCP_DBG(conn, "xmit %d bytes\n", consumed);
  300. conn->txdata_octets += consumed;
  301. return consumed;
  302. error:
  303. /* Transmit error. We could initiate error recovery
  304. * here. */
  305. ISCSI_SW_TCP_DBG(conn, "Error sending PDU, errno=%d\n", rc);
  306. iscsi_conn_failure(conn, rc);
  307. return -EIO;
  308. }
  309. /**
  310. * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
  311. */
  312. static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
  313. {
  314. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  315. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  316. struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
  317. return segment->total_copied - segment->total_size;
  318. }
  319. static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
  320. {
  321. struct iscsi_conn *conn = task->conn;
  322. unsigned long pflags = current->flags;
  323. int rc = 0;
  324. current->flags |= PF_MEMALLOC;
  325. while (iscsi_sw_tcp_xmit_qlen(conn)) {
  326. rc = iscsi_sw_tcp_xmit(conn);
  327. if (rc == 0) {
  328. rc = -EAGAIN;
  329. break;
  330. }
  331. if (rc < 0)
  332. break;
  333. rc = 0;
  334. }
  335. tsk_restore_flags(current, pflags, PF_MEMALLOC);
  336. return rc;
  337. }
  338. /*
  339. * This is called when we're done sending the header.
  340. * Simply copy the data_segment to the send segment, and return.
  341. */
  342. static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
  343. struct iscsi_segment *segment)
  344. {
  345. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  346. tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment;
  347. ISCSI_SW_TCP_DBG(tcp_conn->iscsi_conn,
  348. "Header done. Next segment size %u total_size %u\n",
  349. tcp_sw_conn->out.segment.size,
  350. tcp_sw_conn->out.segment.total_size);
  351. return 0;
  352. }
  353. static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
  354. size_t hdrlen)
  355. {
  356. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  357. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  358. ISCSI_SW_TCP_DBG(conn, "%s\n", conn->hdrdgst_en ?
  359. "digest enabled" : "digest disabled");
  360. /* Clear the data segment - needs to be filled in by the
  361. * caller using iscsi_tcp_send_data_prep() */
  362. memset(&tcp_sw_conn->out.data_segment, 0,
  363. sizeof(struct iscsi_segment));
  364. /* If header digest is enabled, compute the CRC and
  365. * place the digest into the same buffer. We make
  366. * sure that both iscsi_tcp_task and mtask have
  367. * sufficient room.
  368. */
  369. if (conn->hdrdgst_en) {
  370. iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen,
  371. hdr + hdrlen);
  372. hdrlen += ISCSI_DIGEST_SIZE;
  373. }
  374. /* Remember header pointer for later, when we need
  375. * to decide whether there's a payload to go along
  376. * with the header. */
  377. tcp_sw_conn->out.hdr = hdr;
  378. iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
  379. iscsi_sw_tcp_send_hdr_done, NULL);
  380. }
  381. /*
  382. * Prepare the send buffer for the payload data.
  383. * Padding and checksumming will all be taken care
  384. * of by the iscsi_segment routines.
  385. */
  386. static int
  387. iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
  388. unsigned int count, unsigned int offset,
  389. unsigned int len)
  390. {
  391. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  392. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  393. struct hash_desc *tx_hash = NULL;
  394. unsigned int hdr_spec_len;
  395. ISCSI_SW_TCP_DBG(conn, "offset=%d, datalen=%d %s\n", offset, len,
  396. conn->datadgst_en ?
  397. "digest enabled" : "digest disabled");
  398. /* Make sure the datalen matches what the caller
  399. said he would send. */
  400. hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
  401. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  402. if (conn->datadgst_en)
  403. tx_hash = &tcp_sw_conn->tx_hash;
  404. return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
  405. sg, count, offset, len,
  406. NULL, tx_hash);
  407. }
  408. static void
  409. iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
  410. size_t len)
  411. {
  412. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  413. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  414. struct hash_desc *tx_hash = NULL;
  415. unsigned int hdr_spec_len;
  416. ISCSI_SW_TCP_DBG(conn, "datalen=%zd %s\n", len, conn->datadgst_en ?
  417. "digest enabled" : "digest disabled");
  418. /* Make sure the datalen matches what the caller
  419. said he would send. */
  420. hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
  421. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  422. if (conn->datadgst_en)
  423. tx_hash = &tcp_sw_conn->tx_hash;
  424. iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
  425. data, len, NULL, tx_hash);
  426. }
  427. static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
  428. unsigned int offset, unsigned int count)
  429. {
  430. struct iscsi_conn *conn = task->conn;
  431. int err = 0;
  432. iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
  433. if (!count)
  434. return 0;
  435. if (!task->sc)
  436. iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
  437. else {
  438. struct scsi_data_buffer *sdb = scsi_out(task->sc);
  439. err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
  440. sdb->table.nents, offset,
  441. count);
  442. }
  443. if (err) {
  444. /* got invalid offset/len */
  445. return -EIO;
  446. }
  447. return 0;
  448. }
  449. static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
  450. {
  451. struct iscsi_tcp_task *tcp_task = task->dd_data;
  452. task->hdr = task->dd_data + sizeof(*tcp_task);
  453. task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
  454. return 0;
  455. }
  456. static struct iscsi_cls_conn *
  457. iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
  458. uint32_t conn_idx)
  459. {
  460. struct iscsi_conn *conn;
  461. struct iscsi_cls_conn *cls_conn;
  462. struct iscsi_tcp_conn *tcp_conn;
  463. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  464. cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
  465. conn_idx);
  466. if (!cls_conn)
  467. return NULL;
  468. conn = cls_conn->dd_data;
  469. tcp_conn = conn->dd_data;
  470. tcp_sw_conn = tcp_conn->dd_data;
  471. tcp_sw_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  472. CRYPTO_ALG_ASYNC);
  473. tcp_sw_conn->tx_hash.flags = 0;
  474. if (IS_ERR(tcp_sw_conn->tx_hash.tfm))
  475. goto free_conn;
  476. tcp_sw_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  477. CRYPTO_ALG_ASYNC);
  478. tcp_sw_conn->rx_hash.flags = 0;
  479. if (IS_ERR(tcp_sw_conn->rx_hash.tfm))
  480. goto free_tx_tfm;
  481. tcp_conn->rx_hash = &tcp_sw_conn->rx_hash;
  482. return cls_conn;
  483. free_tx_tfm:
  484. crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
  485. free_conn:
  486. iscsi_conn_printk(KERN_ERR, conn,
  487. "Could not create connection due to crc32c "
  488. "loading error. Make sure the crc32c "
  489. "module is built as a module or into the "
  490. "kernel\n");
  491. iscsi_tcp_conn_teardown(cls_conn);
  492. return NULL;
  493. }
  494. static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
  495. {
  496. struct iscsi_session *session = conn->session;
  497. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  498. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  499. struct socket *sock = tcp_sw_conn->sock;
  500. if (!sock)
  501. return;
  502. sock_hold(sock->sk);
  503. iscsi_sw_tcp_conn_restore_callbacks(conn);
  504. sock_put(sock->sk);
  505. spin_lock_bh(&session->lock);
  506. tcp_sw_conn->sock = NULL;
  507. spin_unlock_bh(&session->lock);
  508. sockfd_put(sock);
  509. }
  510. static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
  511. {
  512. struct iscsi_conn *conn = cls_conn->dd_data;
  513. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  514. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  515. iscsi_sw_tcp_release_conn(conn);
  516. if (tcp_sw_conn->tx_hash.tfm)
  517. crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
  518. if (tcp_sw_conn->rx_hash.tfm)
  519. crypto_free_hash(tcp_sw_conn->rx_hash.tfm);
  520. iscsi_tcp_conn_teardown(cls_conn);
  521. }
  522. static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  523. {
  524. struct iscsi_conn *conn = cls_conn->dd_data;
  525. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  526. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  527. struct socket *sock = tcp_sw_conn->sock;
  528. /* userspace may have goofed up and not bound us */
  529. if (!sock)
  530. return;
  531. sock->sk->sk_err = EIO;
  532. wake_up_interruptible(sk_sleep(sock->sk));
  533. /* stop xmit side */
  534. iscsi_suspend_tx(conn);
  535. /* stop recv side and release socket */
  536. iscsi_sw_tcp_release_conn(conn);
  537. iscsi_conn_stop(cls_conn, flag);
  538. }
  539. static int
  540. iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,
  541. struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
  542. int is_leading)
  543. {
  544. struct iscsi_session *session = cls_session->dd_data;
  545. struct iscsi_conn *conn = cls_conn->dd_data;
  546. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  547. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  548. struct sock *sk;
  549. struct socket *sock;
  550. int err;
  551. /* lookup for existing socket */
  552. sock = sockfd_lookup((int)transport_eph, &err);
  553. if (!sock) {
  554. iscsi_conn_printk(KERN_ERR, conn,
  555. "sockfd_lookup failed %d\n", err);
  556. return -EEXIST;
  557. }
  558. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  559. if (err)
  560. goto free_socket;
  561. spin_lock_bh(&session->lock);
  562. /* bind iSCSI connection and socket */
  563. tcp_sw_conn->sock = sock;
  564. spin_unlock_bh(&session->lock);
  565. /* setup Socket parameters */
  566. sk = sock->sk;
  567. sk->sk_reuse = SK_CAN_REUSE;
  568. sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
  569. sk->sk_allocation = GFP_ATOMIC;
  570. sk_set_memalloc(sk);
  571. iscsi_sw_tcp_conn_set_callbacks(conn);
  572. tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
  573. /*
  574. * set receive state machine into initial state
  575. */
  576. iscsi_tcp_hdr_recv_prep(tcp_conn);
  577. return 0;
  578. free_socket:
  579. sockfd_put(sock);
  580. return err;
  581. }
  582. static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
  583. enum iscsi_param param, char *buf,
  584. int buflen)
  585. {
  586. struct iscsi_conn *conn = cls_conn->dd_data;
  587. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  588. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  589. switch(param) {
  590. case ISCSI_PARAM_HDRDGST_EN:
  591. iscsi_set_param(cls_conn, param, buf, buflen);
  592. break;
  593. case ISCSI_PARAM_DATADGST_EN:
  594. iscsi_set_param(cls_conn, param, buf, buflen);
  595. tcp_sw_conn->sendpage = conn->datadgst_en ?
  596. sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
  597. break;
  598. case ISCSI_PARAM_MAX_R2T:
  599. return iscsi_tcp_set_max_r2t(conn, buf);
  600. default:
  601. return iscsi_set_param(cls_conn, param, buf, buflen);
  602. }
  603. return 0;
  604. }
  605. static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
  606. enum iscsi_param param, char *buf)
  607. {
  608. struct iscsi_conn *conn = cls_conn->dd_data;
  609. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  610. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  611. struct sockaddr_in6 addr;
  612. int rc, len;
  613. switch(param) {
  614. case ISCSI_PARAM_CONN_PORT:
  615. case ISCSI_PARAM_CONN_ADDRESS:
  616. spin_lock_bh(&conn->session->lock);
  617. if (!tcp_sw_conn || !tcp_sw_conn->sock) {
  618. spin_unlock_bh(&conn->session->lock);
  619. return -ENOTCONN;
  620. }
  621. rc = kernel_getpeername(tcp_sw_conn->sock,
  622. (struct sockaddr *)&addr, &len);
  623. spin_unlock_bh(&conn->session->lock);
  624. if (rc)
  625. return rc;
  626. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  627. &addr, param, buf);
  628. default:
  629. return iscsi_conn_get_param(cls_conn, param, buf);
  630. }
  631. return 0;
  632. }
  633. static int iscsi_sw_tcp_host_get_param(struct Scsi_Host *shost,
  634. enum iscsi_host_param param, char *buf)
  635. {
  636. struct iscsi_sw_tcp_host *tcp_sw_host = iscsi_host_priv(shost);
  637. struct iscsi_session *session = tcp_sw_host->session;
  638. struct iscsi_conn *conn;
  639. struct iscsi_tcp_conn *tcp_conn;
  640. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  641. struct sockaddr_in6 addr;
  642. int rc, len;
  643. switch (param) {
  644. case ISCSI_HOST_PARAM_IPADDRESS:
  645. spin_lock_bh(&session->lock);
  646. conn = session->leadconn;
  647. if (!conn) {
  648. spin_unlock_bh(&session->lock);
  649. return -ENOTCONN;
  650. }
  651. tcp_conn = conn->dd_data;
  652. tcp_sw_conn = tcp_conn->dd_data;
  653. if (!tcp_sw_conn->sock) {
  654. spin_unlock_bh(&session->lock);
  655. return -ENOTCONN;
  656. }
  657. rc = kernel_getsockname(tcp_sw_conn->sock,
  658. (struct sockaddr *)&addr, &len);
  659. spin_unlock_bh(&session->lock);
  660. if (rc)
  661. return rc;
  662. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  663. &addr, param, buf);
  664. default:
  665. return iscsi_host_get_param(shost, param, buf);
  666. }
  667. return 0;
  668. }
  669. static void
  670. iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  671. struct iscsi_stats *stats)
  672. {
  673. struct iscsi_conn *conn = cls_conn->dd_data;
  674. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  675. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  676. stats->custom_length = 3;
  677. strcpy(stats->custom[0].desc, "tx_sendpage_failures");
  678. stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
  679. strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
  680. stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
  681. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  682. stats->custom[2].value = conn->eh_abort_cnt;
  683. iscsi_tcp_conn_get_stats(cls_conn, stats);
  684. }
  685. static struct iscsi_cls_session *
  686. iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
  687. uint16_t qdepth, uint32_t initial_cmdsn)
  688. {
  689. struct iscsi_cls_session *cls_session;
  690. struct iscsi_session *session;
  691. struct iscsi_sw_tcp_host *tcp_sw_host;
  692. struct Scsi_Host *shost;
  693. if (ep) {
  694. printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
  695. return NULL;
  696. }
  697. shost = iscsi_host_alloc(&iscsi_sw_tcp_sht,
  698. sizeof(struct iscsi_sw_tcp_host), 1);
  699. if (!shost)
  700. return NULL;
  701. shost->transportt = iscsi_sw_tcp_scsi_transport;
  702. shost->cmd_per_lun = qdepth;
  703. shost->max_lun = iscsi_max_lun;
  704. shost->max_id = 0;
  705. shost->max_channel = 0;
  706. shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
  707. if (iscsi_host_add(shost, NULL))
  708. goto free_host;
  709. cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
  710. cmds_max, 0,
  711. sizeof(struct iscsi_tcp_task) +
  712. sizeof(struct iscsi_sw_tcp_hdrbuf),
  713. initial_cmdsn, 0);
  714. if (!cls_session)
  715. goto remove_host;
  716. session = cls_session->dd_data;
  717. tcp_sw_host = iscsi_host_priv(shost);
  718. tcp_sw_host->session = session;
  719. shost->can_queue = session->scsi_cmds_max;
  720. if (iscsi_tcp_r2tpool_alloc(session))
  721. goto remove_session;
  722. return cls_session;
  723. remove_session:
  724. iscsi_session_teardown(cls_session);
  725. remove_host:
  726. iscsi_host_remove(shost);
  727. free_host:
  728. iscsi_host_free(shost);
  729. return NULL;
  730. }
  731. static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
  732. {
  733. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  734. iscsi_tcp_r2tpool_free(cls_session->dd_data);
  735. iscsi_session_teardown(cls_session);
  736. iscsi_host_remove(shost);
  737. iscsi_host_free(shost);
  738. }
  739. static umode_t iscsi_sw_tcp_attr_is_visible(int param_type, int param)
  740. {
  741. switch (param_type) {
  742. case ISCSI_HOST_PARAM:
  743. switch (param) {
  744. case ISCSI_HOST_PARAM_NETDEV_NAME:
  745. case ISCSI_HOST_PARAM_HWADDRESS:
  746. case ISCSI_HOST_PARAM_IPADDRESS:
  747. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  748. return S_IRUGO;
  749. default:
  750. return 0;
  751. }
  752. case ISCSI_PARAM:
  753. switch (param) {
  754. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  755. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  756. case ISCSI_PARAM_HDRDGST_EN:
  757. case ISCSI_PARAM_DATADGST_EN:
  758. case ISCSI_PARAM_CONN_ADDRESS:
  759. case ISCSI_PARAM_CONN_PORT:
  760. case ISCSI_PARAM_EXP_STATSN:
  761. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  762. case ISCSI_PARAM_PERSISTENT_PORT:
  763. case ISCSI_PARAM_PING_TMO:
  764. case ISCSI_PARAM_RECV_TMO:
  765. case ISCSI_PARAM_INITIAL_R2T_EN:
  766. case ISCSI_PARAM_MAX_R2T:
  767. case ISCSI_PARAM_IMM_DATA_EN:
  768. case ISCSI_PARAM_FIRST_BURST:
  769. case ISCSI_PARAM_MAX_BURST:
  770. case ISCSI_PARAM_PDU_INORDER_EN:
  771. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  772. case ISCSI_PARAM_ERL:
  773. case ISCSI_PARAM_TARGET_NAME:
  774. case ISCSI_PARAM_TPGT:
  775. case ISCSI_PARAM_USERNAME:
  776. case ISCSI_PARAM_PASSWORD:
  777. case ISCSI_PARAM_USERNAME_IN:
  778. case ISCSI_PARAM_PASSWORD_IN:
  779. case ISCSI_PARAM_FAST_ABORT:
  780. case ISCSI_PARAM_ABORT_TMO:
  781. case ISCSI_PARAM_LU_RESET_TMO:
  782. case ISCSI_PARAM_TGT_RESET_TMO:
  783. case ISCSI_PARAM_IFACE_NAME:
  784. case ISCSI_PARAM_INITIATOR_NAME:
  785. return S_IRUGO;
  786. default:
  787. return 0;
  788. }
  789. }
  790. return 0;
  791. }
  792. static int iscsi_sw_tcp_slave_alloc(struct scsi_device *sdev)
  793. {
  794. set_bit(QUEUE_FLAG_BIDI, &sdev->request_queue->queue_flags);
  795. return 0;
  796. }
  797. static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
  798. {
  799. blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
  800. blk_queue_dma_alignment(sdev->request_queue, 0);
  801. return 0;
  802. }
  803. static struct scsi_host_template iscsi_sw_tcp_sht = {
  804. .module = THIS_MODULE,
  805. .name = "iSCSI Initiator over TCP/IP",
  806. .queuecommand = iscsi_queuecommand,
  807. .change_queue_depth = iscsi_change_queue_depth,
  808. .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
  809. .sg_tablesize = 4096,
  810. .max_sectors = 0xFFFF,
  811. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  812. .eh_abort_handler = iscsi_eh_abort,
  813. .eh_device_reset_handler= iscsi_eh_device_reset,
  814. .eh_target_reset_handler = iscsi_eh_recover_target,
  815. .use_clustering = DISABLE_CLUSTERING,
  816. .slave_alloc = iscsi_sw_tcp_slave_alloc,
  817. .slave_configure = iscsi_sw_tcp_slave_configure,
  818. .target_alloc = iscsi_target_alloc,
  819. .proc_name = "iscsi_tcp",
  820. .this_id = -1,
  821. };
  822. static struct iscsi_transport iscsi_sw_tcp_transport = {
  823. .owner = THIS_MODULE,
  824. .name = "tcp",
  825. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  826. | CAP_DATADGST,
  827. /* session management */
  828. .create_session = iscsi_sw_tcp_session_create,
  829. .destroy_session = iscsi_sw_tcp_session_destroy,
  830. /* connection management */
  831. .create_conn = iscsi_sw_tcp_conn_create,
  832. .bind_conn = iscsi_sw_tcp_conn_bind,
  833. .destroy_conn = iscsi_sw_tcp_conn_destroy,
  834. .attr_is_visible = iscsi_sw_tcp_attr_is_visible,
  835. .set_param = iscsi_sw_tcp_conn_set_param,
  836. .get_conn_param = iscsi_sw_tcp_conn_get_param,
  837. .get_session_param = iscsi_session_get_param,
  838. .start_conn = iscsi_conn_start,
  839. .stop_conn = iscsi_sw_tcp_conn_stop,
  840. /* iscsi host params */
  841. .get_host_param = iscsi_sw_tcp_host_get_param,
  842. .set_host_param = iscsi_host_set_param,
  843. /* IO */
  844. .send_pdu = iscsi_conn_send_pdu,
  845. .get_stats = iscsi_sw_tcp_conn_get_stats,
  846. /* iscsi task/cmd helpers */
  847. .init_task = iscsi_tcp_task_init,
  848. .xmit_task = iscsi_tcp_task_xmit,
  849. .cleanup_task = iscsi_tcp_cleanup_task,
  850. /* low level pdu helpers */
  851. .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
  852. .init_pdu = iscsi_sw_tcp_pdu_init,
  853. .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
  854. /* recovery */
  855. .session_recovery_timedout = iscsi_session_recovery_timedout,
  856. };
  857. static int __init iscsi_sw_tcp_init(void)
  858. {
  859. if (iscsi_max_lun < 1) {
  860. printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
  861. iscsi_max_lun);
  862. return -EINVAL;
  863. }
  864. iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
  865. &iscsi_sw_tcp_transport);
  866. if (!iscsi_sw_tcp_scsi_transport)
  867. return -ENODEV;
  868. return 0;
  869. }
  870. static void __exit iscsi_sw_tcp_exit(void)
  871. {
  872. iscsi_unregister_transport(&iscsi_sw_tcp_transport);
  873. }
  874. module_init(iscsi_sw_tcp_init);
  875. module_exit(iscsi_sw_tcp_exit);