xref: /spdk/test/unit/lib/iscsi/common.c (revision 1fc4165fe9bf8512483356ad8e6d27f793f2e3db)
1 #include "iscsi/task.h"
2 #include "iscsi/iscsi.h"
3 #include "iscsi/conn.h"
4 #include "iscsi/acceptor.h"
5 
6 #include "spdk/env.h"
7 #include "spdk/event.h"
8 #include "spdk/sock.h"
9 #include "spdk_cunit.h"
10 
11 #include "spdk_internal/log.h"
12 #include "spdk_internal/mock.h"
13 
14 #include "scsi/scsi_internal.h"
15 
16 SPDK_LOG_REGISTER_COMPONENT("iscsi", SPDK_LOG_ISCSI)
17 
18 TAILQ_HEAD(, spdk_iscsi_pdu) g_write_pdu_list;
19 
20 struct spdk_iscsi_task *
21 spdk_iscsi_task_get(struct spdk_iscsi_conn *conn,
22 		    struct spdk_iscsi_task *parent,
23 		    spdk_scsi_task_cpl cpl_fn)
24 {
25 	struct spdk_iscsi_task *task;
26 
27 	task = calloc(1, sizeof(*task));
28 	if (!task) {
29 		return NULL;
30 	}
31 	task->scsi.iovs = &task->scsi.iov;
32 	return task;
33 }
34 
35 void
36 spdk_scsi_task_put(struct spdk_scsi_task *task)
37 {
38 	free(task);
39 }
40 
41 void
42 spdk_put_pdu(struct spdk_iscsi_pdu *pdu)
43 {
44 	if (!pdu) {
45 		return;
46 	}
47 
48 	pdu->ref--;
49 	if (pdu->ref < 0) {
50 		CU_FAIL("negative ref count");
51 		pdu->ref = 0;
52 	}
53 
54 	if (pdu->ref == 0) {
55 		if (pdu->data && !pdu->data_from_mempool) {
56 			free(pdu->data);
57 		}
58 		free(pdu);
59 	}
60 }
61 
62 struct spdk_iscsi_pdu *
63 spdk_get_pdu(void)
64 {
65 	struct spdk_iscsi_pdu *pdu;
66 
67 	pdu = malloc(sizeof(*pdu));
68 	if (!pdu) {
69 		return NULL;
70 	}
71 
72 	memset(pdu, 0, offsetof(struct spdk_iscsi_pdu, ahs));
73 	pdu->ref = 1;
74 
75 	return pdu;
76 }
77 
78 DEFINE_STUB_V(spdk_scsi_task_process_null_lun, (struct spdk_scsi_task *task));
79 
80 DEFINE_STUB_V(spdk_scsi_task_process_abort, (struct spdk_scsi_task *task));
81 
82 DEFINE_STUB_V(spdk_scsi_dev_queue_task,
83 	      (struct spdk_scsi_dev *dev, struct spdk_scsi_task *task));
84 
85 DEFINE_STUB(spdk_scsi_dev_find_port_by_id, struct spdk_scsi_port *,
86 	    (struct spdk_scsi_dev *dev, uint64_t id), NULL);
87 
88 DEFINE_STUB_V(spdk_scsi_dev_queue_mgmt_task,
89 	      (struct spdk_scsi_dev *dev, struct spdk_scsi_task *task));
90 
91 const char *
92 spdk_scsi_dev_get_name(const struct spdk_scsi_dev *dev)
93 {
94 	if (dev != NULL) {
95 		return dev->name;
96 	}
97 
98 	return NULL;
99 }
100 
101 DEFINE_STUB_V(spdk_iscsi_acceptor_start, (struct spdk_iscsi_portal *p));
102 
103 DEFINE_STUB_V(spdk_iscsi_acceptor_stop, (struct spdk_iscsi_portal *p));
104 
105 struct spdk_sock *
106 spdk_sock_listen(const char *ip, int port)
107 {
108 	static int g_sock;
109 
110 	return (struct spdk_sock *)&g_sock;
111 }
112 
113 int
114 spdk_sock_close(struct spdk_sock **sock)
115 {
116 	*sock = NULL;
117 
118 	return 0;
119 }
120 
121 static struct spdk_cpuset *g_app_core_mask;
122 
123 struct spdk_cpuset *
124 spdk_app_get_core_mask(void)
125 {
126 	int i;
127 	if (!g_app_core_mask) {
128 		g_app_core_mask = spdk_cpuset_alloc();
129 		for (i = 0; i < SPDK_CPUSET_SIZE; i++) {
130 			spdk_cpuset_set_cpu(g_app_core_mask, i, true);
131 		}
132 	}
133 	return g_app_core_mask;
134 }
135 
136 int
137 spdk_app_parse_core_mask(const char *mask, struct spdk_cpuset *cpumask)
138 {
139 	int rc;
140 
141 	if (mask == NULL || cpumask == NULL) {
142 		return -1;
143 	}
144 
145 	rc = spdk_cpuset_parse(cpumask, mask);
146 	if (rc < 0) {
147 		return -1;
148 	}
149 	return 0;
150 }
151 
152 DEFINE_STUB(spdk_env_get_current_core, uint32_t, (void), 0);
153 
154 DEFINE_STUB(spdk_event_allocate, struct spdk_event *,
155 	    (uint32_t core, spdk_event_fn fn, void *arg1, void *arg2), NULL);
156 
157 DEFINE_STUB(spdk_scsi_dev_construct, struct spdk_scsi_dev *,
158 	    (const char *name, const char **bdev_name_list,
159 	     int *lun_id_list, int num_luns, uint8_t protocol_id,
160 	     void (*hotremove_cb)(const struct spdk_scsi_lun *, void *),
161 	     void *hotremove_ctx),
162 	    NULL);
163 
164 DEFINE_STUB_V(spdk_scsi_dev_destruct, (struct spdk_scsi_dev *dev));
165 
166 DEFINE_STUB(spdk_scsi_dev_add_port, int,
167 	    (struct spdk_scsi_dev *dev, uint64_t id, const char *name), 0);
168 
169 DEFINE_STUB(spdk_iscsi_drop_conns, int,
170 	    (struct spdk_iscsi_conn *conn, const char *conn_match, int drop_all),
171 	    0);
172 
173 DEFINE_STUB(spdk_scsi_dev_delete_port, int,
174 	    (struct spdk_scsi_dev *dev, uint64_t id), 0);
175 
176 DEFINE_STUB_V(spdk_shutdown_iscsi_conns, (void));
177 
178 void
179 spdk_iscsi_task_cpl(struct spdk_scsi_task *scsi_task)
180 {
181 	struct spdk_iscsi_task *iscsi_task;
182 
183 	if (scsi_task != NULL) {
184 		iscsi_task = spdk_iscsi_task_from_scsi_task(scsi_task);
185 		free(iscsi_task);
186 	}
187 }
188 
189 DEFINE_STUB_V(spdk_iscsi_task_mgmt_cpl, (struct spdk_scsi_task *scsi_task));
190 
191 DEFINE_STUB(spdk_iscsi_conn_read_data, int,
192 	    (struct spdk_iscsi_conn *conn, int bytes, void *buf), 0);
193 
194 DEFINE_STUB(spdk_iscsi_conn_readv_data, int,
195 	    (struct spdk_iscsi_conn *conn, struct iovec *iov, int iovcnt), 0);
196 
197 void
198 spdk_iscsi_conn_write_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
199 {
200 	TAILQ_INSERT_TAIL(&g_write_pdu_list, pdu, tailq);
201 }
202 
203 DEFINE_STUB_V(spdk_iscsi_conn_logout, (struct spdk_iscsi_conn *conn));
204 
205 DEFINE_STUB_V(spdk_scsi_task_set_status,
206 	      (struct spdk_scsi_task *task, int sc, int sk, int asc, int ascq));
207 
208 void
209 spdk_scsi_task_set_data(struct spdk_scsi_task *task, void *data, uint32_t len)
210 {
211 	SPDK_CU_ASSERT_FATAL(task->iovs != NULL);
212 	task->iovs[0].iov_base = data;
213 	task->iovs[0].iov_len = len;
214 }
215