xref: /spdk/test/unit/lib/iscsi/common.c (revision d453c9400ef7738fa7e1f2cb8bc52bdb9bf10fdf)
1 #include "iscsi/task.h"
2 #include "iscsi/iscsi.h"
3 #include "iscsi/conn.h"
4 
5 #include "spdk/env.h"
6 #include "spdk/event.h"
7 #include "spdk/sock.h"
8 #include "spdk_cunit.h"
9 
10 #include "spdk_internal/log.h"
11 #include "spdk_internal/mock.h"
12 
13 #include "scsi/scsi_internal.h"
14 
15 SPDK_LOG_REGISTER_COMPONENT("iscsi", SPDK_LOG_ISCSI)
16 
17 TAILQ_HEAD(, spdk_iscsi_pdu) g_write_pdu_list = TAILQ_HEAD_INITIALIZER(g_write_pdu_list);
18 
19 static bool g_task_pool_is_empty = false;
20 static bool g_pdu_pool_is_empty = false;
21 
22 struct spdk_iscsi_task *
23 spdk_iscsi_task_get(struct spdk_iscsi_conn *conn,
24 		    struct spdk_iscsi_task *parent,
25 		    spdk_scsi_task_cpl cpl_fn)
26 {
27 	struct spdk_iscsi_task *task;
28 
29 	if (g_task_pool_is_empty) {
30 		return NULL;
31 	}
32 
33 	task = calloc(1, sizeof(*task));
34 	if (!task) {
35 		return NULL;
36 	}
37 	task->scsi.iovs = &task->scsi.iov;
38 	return task;
39 }
40 
41 void
42 spdk_scsi_task_put(struct spdk_scsi_task *task)
43 {
44 	free(task);
45 }
46 
47 void
48 spdk_put_pdu(struct spdk_iscsi_pdu *pdu)
49 {
50 	if (!pdu) {
51 		return;
52 	}
53 
54 	pdu->ref--;
55 	if (pdu->ref < 0) {
56 		CU_FAIL("negative ref count");
57 		pdu->ref = 0;
58 	}
59 
60 	if (pdu->ref == 0) {
61 		if (pdu->data && !pdu->data_from_mempool) {
62 			free(pdu->data);
63 		}
64 		free(pdu);
65 	}
66 }
67 
68 struct spdk_iscsi_pdu *
69 spdk_get_pdu(void)
70 {
71 	struct spdk_iscsi_pdu *pdu;
72 
73 	if (g_pdu_pool_is_empty) {
74 		return NULL;
75 	}
76 
77 	pdu = malloc(sizeof(*pdu));
78 	if (!pdu) {
79 		return NULL;
80 	}
81 
82 	memset(pdu, 0, offsetof(struct spdk_iscsi_pdu, ahs));
83 	pdu->ref = 1;
84 
85 	return pdu;
86 }
87 
88 DEFINE_STUB_V(spdk_scsi_task_process_null_lun, (struct spdk_scsi_task *task));
89 
90 DEFINE_STUB_V(spdk_scsi_task_process_abort, (struct spdk_scsi_task *task));
91 
92 DEFINE_STUB_V(spdk_scsi_dev_queue_task,
93 	      (struct spdk_scsi_dev *dev, struct spdk_scsi_task *task));
94 
95 DEFINE_STUB(spdk_scsi_dev_find_port_by_id, struct spdk_scsi_port *,
96 	    (struct spdk_scsi_dev *dev, uint64_t id), NULL);
97 
98 DEFINE_STUB_V(spdk_scsi_dev_queue_mgmt_task,
99 	      (struct spdk_scsi_dev *dev, struct spdk_scsi_task *task));
100 
101 const char *
102 spdk_scsi_dev_get_name(const struct spdk_scsi_dev *dev)
103 {
104 	if (dev != NULL) {
105 		return dev->name;
106 	}
107 
108 	return NULL;
109 }
110 
111 DEFINE_STUB(spdk_scsi_dev_construct, struct spdk_scsi_dev *,
112 	    (const char *name, const char **bdev_name_list,
113 	     int *lun_id_list, int num_luns, uint8_t protocol_id,
114 	     void (*hotremove_cb)(const struct spdk_scsi_lun *, void *),
115 	     void *hotremove_ctx),
116 	    NULL);
117 
118 DEFINE_STUB_V(spdk_scsi_dev_destruct,
119 	      (struct spdk_scsi_dev *dev, spdk_scsi_dev_destruct_cb_t cb_fn, void *cb_arg));
120 
121 DEFINE_STUB(spdk_scsi_dev_add_port, int,
122 	    (struct spdk_scsi_dev *dev, uint64_t id, const char *name), 0);
123 
124 DEFINE_STUB(spdk_iscsi_drop_conns, int,
125 	    (struct spdk_iscsi_conn *conn, const char *conn_match, int drop_all),
126 	    0);
127 
128 DEFINE_STUB(spdk_scsi_dev_delete_port, int,
129 	    (struct spdk_scsi_dev *dev, uint64_t id), 0);
130 
131 DEFINE_STUB_V(spdk_shutdown_iscsi_conns, (void));
132 
133 DEFINE_STUB_V(spdk_iscsi_conns_request_logout, (struct spdk_iscsi_tgt_node *target));
134 
135 DEFINE_STUB(spdk_iscsi_get_active_conns, int, (struct spdk_iscsi_tgt_node *target), 0);
136 
137 void
138 spdk_iscsi_task_cpl(struct spdk_scsi_task *scsi_task)
139 {
140 	struct spdk_iscsi_task *iscsi_task;
141 
142 	if (scsi_task != NULL) {
143 		iscsi_task = spdk_iscsi_task_from_scsi_task(scsi_task);
144 		free(iscsi_task);
145 	}
146 }
147 
148 DEFINE_STUB_V(spdk_iscsi_task_mgmt_cpl, (struct spdk_scsi_task *scsi_task));
149 
150 DEFINE_STUB(spdk_iscsi_conn_read_data, int,
151 	    (struct spdk_iscsi_conn *conn, int bytes, void *buf), 0);
152 
153 DEFINE_STUB(spdk_iscsi_conn_readv_data, int,
154 	    (struct spdk_iscsi_conn *conn, struct iovec *iov, int iovcnt), 0);
155 
156 void
157 spdk_iscsi_conn_write_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
158 {
159 	TAILQ_INSERT_TAIL(&g_write_pdu_list, pdu, tailq);
160 }
161 
162 DEFINE_STUB_V(spdk_iscsi_conn_logout, (struct spdk_iscsi_conn *conn));
163 
164 DEFINE_STUB_V(spdk_scsi_task_set_status,
165 	      (struct spdk_scsi_task *task, int sc, int sk, int asc, int ascq));
166 
167 void
168 spdk_scsi_task_set_data(struct spdk_scsi_task *task, void *data, uint32_t len)
169 {
170 	SPDK_CU_ASSERT_FATAL(task->iovs != NULL);
171 	task->iovs[0].iov_base = data;
172 	task->iovs[0].iov_len = len;
173 }
174