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