forked from open-iscsi/tcmu-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.c
217 lines (186 loc) · 5.12 KB
/
consumer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/*
* An example of using libtcmu to back one or more types of LIO
* userspace passthrough devices.
*/
#define _GNU_SOURCE
#define _BITS_UIO_H
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <endian.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <signal.h>
#include <poll.h>
#include <stdint.h>
#include <scsi/scsi.h>
#include "target_core_user_local.h"
#include "libtcmu.h"
#include "scsi_defs.h"
#include "libtcmu_log.h"
struct tcmu_device *tcmu_dev_array[128];
size_t dev_array_len = 0;
struct foo_state {
int fd;
uint64_t num_lbas;
uint32_t block_size;
};
static int set_medium_error(uint8_t *sense)
{
return tcmu_set_sense_data(sense, MEDIUM_ERROR, ASC_READ_ERROR, NULL);
}
static int foo_open(struct tcmu_device *dev)
{
/* open the backing file */
/* alloc private struct 'foo_state' */
/* Save a ptr to it in dev->hm_private */
/* Add new device to our horrible fixed-length array */
tcmu_dev_array[dev_array_len] = dev;
dev_array_len++;
return 0;
}
static void foo_close(struct tcmu_device *dev)
{
/* not supported in this example */
}
static int foo_handle_cmd(
struct tcmu_device *dev,
uint8_t *cdb,
struct iovec *iovec,
size_t iov_cnt,
uint8_t *sense)
{
struct foo_state *state = tcmu_get_dev_private(dev);
uint8_t cmd;
cmd = cdb[0];
switch (cmd) {
case INQUIRY:
return tcmu_emulate_inquiry(dev, NULL, cdb, iovec, iov_cnt,
sense);
break;
case TEST_UNIT_READY:
return tcmu_emulate_test_unit_ready(cdb, iovec, iov_cnt, sense);
break;
case SERVICE_ACTION_IN_16:
if (cdb[1] == READ_CAPACITY_16)
return tcmu_emulate_read_capacity_16(state->num_lbas,
state->block_size,
cdb, iovec, iov_cnt, sense);
else
return TCMU_NOT_HANDLED;
break;
case MODE_SENSE:
case MODE_SENSE_10:
return tcmu_emulate_mode_sense(dev, cdb, iovec, iov_cnt, sense);
break;
case MODE_SELECT:
case MODE_SELECT_10:
return tcmu_emulate_mode_select(dev, cdb, iovec, iov_cnt, sense);
break;
case READ_6:
case READ_10:
case READ_12:
case READ_16:
// A real "read" implementation goes here!
return set_medium_error(sense);
case WRITE_6:
case WRITE_10:
case WRITE_12:
case WRITE_16:
// A real "write" implemention goes here!
return SAM_STAT_GOOD;
default:
tcmu_err("unknown command %x\n", cdb[0]);
return TCMU_NOT_HANDLED;
}
}
static struct tcmulib_handler foo_handler = {
.name = "Handler for foo devices (example code)",
.subtype = "foo",
.cfg_desc = "a description goes here",
.added = foo_open,
.removed = foo_close,
};
int main(int argc, char **argv)
{
struct tcmulib_context *tcmulib_ctx;
struct pollfd pollfds[16];
int i;
int ret;
if (tcmu_setup_log()) {
fprintf(stderr, "Could not setup tcmu logger.\n");
exit(1);
}
/* If any TCMU devices that exist that match subtype,
handler->added() will now be called from within
tcmulib_initialize(). */
tcmulib_ctx = tcmulib_initialize(&foo_handler, 1);
if (!tcmulib_ctx) {
tcmu_err("tcmulib_initialize failed with %p\n", tcmulib_ctx);
exit(1);
}
while (1) {
pollfds[0].fd = tcmulib_get_master_fd(tcmulib_ctx);
pollfds[0].events = POLLIN;
pollfds[0].revents = 0;
for (i = 0; i < dev_array_len; i++) {
pollfds[i+1].fd = tcmu_get_dev_fd(tcmu_dev_array[i]);
pollfds[i+1].events = POLLIN;
pollfds[i+1].revents = 0;
}
/* Use ppoll instead poll to avoid poll call reschedules during signal
* handling. If we were removing a device, then the uio device's memory
* could be freed, but the poll would be rescheduled and end up accessing
* the released device. */
ret = ppoll(pollfds, dev_array_len+1, NULL, NULL);
if (ret == -1) {
tcmu_err("ppoll() returned %d, exiting\n", ret);
exit(EXIT_FAILURE);
}
if (pollfds[0].revents) {
/* If any tcmu devices have been added or removed, the
added() and removed() handler callbacks will be called
from within this. */
tcmulib_master_fd_ready(tcmulib_ctx);
/* Since devices (may) have changed, re-poll() instead of
processing per-device fds. */
continue;
}
for (i = 0; i < dev_array_len; i++) {
if (pollfds[i+1].revents) {
struct tcmulib_cmd *cmd;
struct tcmu_device *dev = tcmu_dev_array[i];
tcmulib_processing_start(dev);
while ((cmd = tcmulib_get_next_command(dev)) != NULL) {
ret = foo_handle_cmd(dev,
cmd->cdb,
cmd->iovec,
cmd->iov_cnt,
cmd->sense_buf);
tcmulib_command_complete(dev, cmd, ret);
}
tcmulib_processing_complete(dev);
}
}
}
return 0;
}