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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
/*
* Emulator Control Server - HDS
*
* Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
*
* Contact:
* Chulho Song <ch81.song@samsung.com>
* Jinhyung Choi <jinh0.choi@samsung.com>
* Sangho Park <sangho.p@samsung.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributors:
* - S-Core Co., Ltd
*
*/
#include <string.h>
#include "qemu/osdep.h"
#include "qemu-common.h"
#include "fsdev/qemu-fsdev.h"
#include "emul_state.h"
#include "ecs_hds.h"
#include "util/hds.h"
#include "util/device_hotplug.h"
#include "debug_ch.h"
MULTI_DEBUG_CHANNEL(qemu, hds);
/* send # of available PCI slot */
static void ecs_hds_send_available_pci(char *cmd)
{
int n_possible;
int n_hidden;
int n_available = 0;
char slot_data[OUT_BUF_SIZE];
n_possible = hds_get_pci_available_slot_num();
if (n_possible > 0) {
n_hidden = hds_get_num_hidden_conn();
if (n_hidden > ECS_HDS_HIDDEN_RESERVED) {
n_hidden = 0;
} else {
n_hidden = ECS_HDS_HIDDEN_RESERVED - n_hidden;
}
n_available = n_possible - n_hidden;
if (n_available < 0) {
n_available = 0;
}
}
snprintf(slot_data, sizeof(slot_data), "%d", n_available);
make_send_device_ntf(cmd, ECS_HDS_MSG_GROUP, ECS_HDS_ACTION_PCI, slot_data);
}
static bool check_pci_slot(enum hds_level level)
{
int n_possible;
int n_hidden;
n_possible = hds_get_pci_available_slot_num();
if (n_possible <= 0) {
return false;
}
g_assert(n_possible > 0);
if (level == hds_level_hidden) {
return true;
}
n_hidden = hds_get_num_hidden_conn();
if (n_hidden < ECS_HDS_HIDDEN_RESERVED) {
n_hidden = ECS_HDS_HIDDEN_RESERVED - n_hidden;
n_possible = n_possible - n_hidden;
if (n_possible <= 0) {
return false;
}
}
return true;
}
static void ecs_hds_do_status(char *cmd, type_group group)
{
char *data = get_hds_lists();
LOG_INFO("hds status: %s\n", data);
make_send_device_ntf(cmd, group, ECS_HDS_ACTION_STATE, data);
ecs_hds_send_available_pci(cmd);
}
static void ecs_hds_do_mount(char *cmd, type_group group, char *data)
{
char token[] = "\n";
char *host;
char *level_token;
char *hds_id;
char *guest;
int level = 0;
if (data == NULL) {
LOG_WARNING("ecs_hds_do_mount data is empty.\n");
make_send_device_ntf(cmd, group, ECS_HDS_ACT_MOUNT_FAIL, NULL);
return;
}
host = strtok(data, token);
if (host == NULL) {
LOG_WARNING("ecs_hds_do_mount host token is empty.\n");
make_send_device_ntf(cmd, group, ECS_HDS_ACT_MOUNT_FAIL, NULL);
return;
} else if (hds_is_host_path_used((const char *)host)) {
LOG_WARNING("host path '%s' is already taken.\n", host);
make_send_device_ntf(cmd, group, ECS_HDS_ACT_MOUNT_DUP_HOST_PATH, NULL);
return;
}
guest = strtok(NULL, token);
if (guest == NULL) {
LOG_WARNING("ecs_hds_do_mount guest token is empty.\n");
make_send_device_ntf(cmd, group, ECS_HDS_ACT_MOUNT_FAIL, NULL);
return;
} else if (hds_is_guest_path_used((const char *)guest)) {
LOG_WARNING("guest path '%s' is already taken.\n", guest);
make_send_device_ntf(cmd, group, ECS_HDS_ACT_MOUNT_DUP_GUEST_PATH, NULL);
return;
}
level_token = strtok(NULL, token);
if (level_token != NULL) {
level = atoi(level_token);
}
/* check available PCI slot num */
if (!check_pci_slot(level)) {
LOG_WARNING("No available pci slot.\n");
make_send_device_ntf(cmd, group, ECS_HDS_ACT_MOUNT_NO_SPACE, NULL);
return;
}
hds_id = new_hds_list(host, guest, level);
if (hds_id != NULL) {
LOG_INFO("do attach tag: %s, host: %s, guest: %s, level: %d\n", hds_id, host, guest, level);
do_hotplug(ATTACH_HDS, hds_id, strlen(hds_id) + 1);
} else {
LOG_WARNING("ecs_hds_do_mount hds_id is empty.\n");
make_send_device_ntf(cmd, group, ECS_HDS_ACT_MOUNT_FAIL, NULL);
}
}
static void ecs_hds_do_umount(char *cmd, type_group group, type_action action, char *data)
{
int level = 0;
int len = 0;
enum hds_level tag_level = 0;
char token[] = "\n";
char *tag;
char *level_token;
char *guest_path;
char emuld_data[OUT_BUF_SIZE];
if (data == NULL) {
LOG_WARNING("ecs_hds_do_umount data is empty.\n");
make_send_device_ntf(cmd, group, ECS_HDS_ACT_UMOUNT_FAIL, NULL);
return;
}
tag = strtok(data, token);
if (tag == NULL) {
LOG_WARNING("ecs_hds_do_umount tag token is empty.\n");
make_send_device_ntf(cmd, group, ECS_HDS_ACT_UMOUNT_FAIL, NULL);
return;
}
level_token = strtok(NULL, token);
if (level_token != NULL) {
level = atoi(level_token);
}
LOG_INFO("try detach with is_hds_attached : %s, %d, level: %d\n",
tag, is_hds_attached(tag), level);
if (!is_hds_attached(tag)) {
LOG_WARNING("hds is not attached. do not try detach it.\n");
make_send_device_ntf(cmd, group, ECS_HDS_ACT_UMOUNT_FAIL, NULL);
return;
}
guest_path = get_guest_path_by_id(tag);
if (guest_path == NULL) {
LOG_WARNING("hds guest path is empty with tag %s.\n", tag);
make_send_device_ntf(cmd, group, ECS_HDS_ACT_UMOUNT_FAIL, NULL);
return;
}
tag_level = get_hds_level_by_id(tag);
if (tag_level > level) {
LOG_WARNING("HDS Level is not enough to unmount.\n");
make_send_device_ntf(cmd, group, ECS_HDS_ACT_UMOUNT_NOT_ENOUGH_LEVEL, NULL);
return;
}
len = snprintf(emuld_data, sizeof(emuld_data), "%s\n%s\n", data, guest_path);
LOG_INFO("send emuld to umount.%s\n", emuld_data);
send_msg_to_guest(cmd, group, action, emuld_data, len + 1);
}
void msgproc_device_req_hds(ECS_Client *ccli, ECS__DeviceReq *msg, char * cmd)
{
char *data = NULL;
type_group group = (type_group) (msg->group & 0xff);
type_action action = (type_action) (msg->action & 0xff);
if (msg->has_data && msg->data.len > 0) {
data = (char *) g_malloc0(msg->data.len + 1);
memcpy(data, msg->data.data, msg->data.len);
}
LOG_INFO("hds group: %d, action : %d\n", group, action);
if (group == MSG_GROUP_STATUS) {
ecs_hds_do_status(cmd, group);
} else if (group == ECS_HDS_MSG_GROUP && action == ECS_HDS_ACTION_MOUNT) {
ecs_hds_do_mount(cmd, group, data);
} else if (group == ECS_HDS_MSG_GROUP && action == ECS_HDS_ACTION_UMOUNT) {
ecs_hds_do_umount(cmd, group, action, data);
} else {
LOG_WARNING("hds unknown command: group %d action %d\n", group, action);
}
g_free(data);
}
static void send_hds_mount_request(char *list)
{
int len = 0;
char token[] = ",";
char emuld_data[OUT_BUF_SIZE];
char *id;
char *host;
char *guest;
LOG_INFO("handling mount request: %s\n", list);
id = strtok(list, token);
if (id == NULL) {
LOG_WARNING("cannot send mount request because of id\n");
return;
}
host = strtok(NULL, token);
if (host == NULL) {
LOG_WARNING("cannot send mount request because of host\n");
return;
}
guest = strtok(NULL, token);
if (guest == NULL) {
LOG_WARNING("cannot send mount request because of guest\n");
return;
}
len = snprintf(emuld_data, sizeof(emuld_data), "%s\n%s\n", id, guest);
send_msg_to_guest(MSG_TYPE_HDS, ECS_HDS_MSG_GROUP,
ECS_HDS_ACTION_MOUNT, emuld_data, len + 1);
}
static void *hds_mount_request_thread(void *args)
{
char *list = (char *)args;
char token[] = "\n";
char *hds_list;
hds_list = strtok(list, token);
if (hds_list == NULL) {
LOG_INFO("no hds mount request\n");
free(list);
return NULL;
}
send_hds_mount_request(hds_list);
while ((hds_list = strtok(NULL, token)) != NULL) {
send_hds_mount_request(hds_list);
}
free(list);
return NULL;
}
static void ecs_hds_do_get_list(void)
{
char *list;
QemuThread hds_thread_id;
list = get_hds_lists();
if (strlen(list) == 0) {
LOG_INFO("none of mount candidates available.\n");
return;
}
qemu_thread_create(&hds_thread_id, "hds_mount", hds_mount_request_thread,
(void *)list, QEMU_THREAD_DETACHED);
}
void msgproc_injector_do_hds(char *cat, type_action action, const char *data)
{
FsDriverEntry *entry;
char msg[OUT_BUF_SIZE];
char *host;
char *guest;
enum hds_level level;
LOG_INFO("hds status is %d, %s\n", action, data);
switch (action) {
case ECS_HDS_ACT_GET_LIST:
ecs_hds_do_get_list();
break;
case ECS_HDS_ACT_ADD_LIST_FOR_DEFAULT:
entry = get_fsdev_fsentry((char *)DEFAULT_STATIC_HDS_ID);
if (entry == NULL) {
LOG_WARNING("cannot find fsdev entry.\n");
break;
}
if (!add_hds_list(DEFAULT_STATIC_HDS_ID, entry->path, DEFAULT_HDS_GUEST_PATH, hds_level_normal)) {
LOG_WARNING("cannot add into hds list.\n");
break;
}
set_hds_attached(DEFAULT_STATIC_HDS_ID, true);
data = DEFAULT_STATIC_HDS_ID;
/* pass-through */
case ECS_HDS_ACT_MOUNT_SUCCESS:
if (data == NULL) {
LOG_WARNING("error: hds data is null.\n");
break;
}
host = get_host_path_by_id(data);
if (host == NULL) {
LOG_WARNING("get_host_path_by_id failed with %s\n", data);
break;
}
guest = get_guest_path_by_id(data);
if (guest == NULL) {
LOG_WARNING("get_guest_path_by_id failed with %s\n", data);
break;
}
level = get_hds_level_by_id(data);
snprintf(msg, sizeof(msg), "%s,%s,%s,%d,", data, host, guest, level);
make_send_device_ntf(cat, ECS_HDS_MSG_GROUP, action, msg);
break;
case ECS_HDS_ACT_MOUNT_FAIL:
case ECS_HDS_ACT_MOUNT_NOT_PERMITTED: /* not exist on the possible path */
case ECS_HDS_ACT_MOUNT_NOT_VALID_PATH: /* not a vaild path */
if (data == NULL) {
LOG_WARNING("error: hds data is null.\n");
break;
}
remove_hds_list((char *)data);
do_hotplug(DETACH_HDS, (char *)data, strlen(data) + 1);
make_send_device_ntf(cat, ECS_HDS_MSG_GROUP, action, (char *)data);
break;
case ECS_HDS_ACT_UMOUNT_SUCCESS:
if (data == NULL) {
LOG_WARNING("error: hds data is null.\n");
break;
}
do_hotplug(DETACH_HDS, (char *)data, strlen(data) + 1);
make_send_device_ntf(cat, ECS_HDS_MSG_GROUP, action, (char *)data);
break;
case ECS_HDS_ACT_UMOUNT_FAIL:
if (data == NULL) {
LOG_WARNING("error: hds data is null.\n");
break;
}
make_send_device_ntf(cat, ECS_HDS_MSG_GROUP, action, (char *)data);
break;
default:
LOG_WARNING("unknown action: %s.\n", action);
break;
}
}
|