forked from sysml/clickos-ctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xs.cc
560 lines (463 loc) · 13.8 KB
/
xs.cc
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
* ClickOS Control
*
* file: xs.cc
*
* NEC Europe Ltd. PROPRIETARY INFORMATION
*
* This software is supplied under the terms of a license agreement
* or nondisclosure agreement with NEC Europe Ltd. and may not be
* copied or disclosed except in accordance with the terms of that
* agreement. The software and its source code contain valuable trade
* secrets and confidential information which have to be maintained in
* confidence.
* Any unauthorized publication, transfer to third parties or duplication
* of the object or source code - either totally or in part – is
* prohibited.
*
* Copyright (c) 2016 NEC Europe Ltd. All Rights Reserved.
*
* Authors: Filipe Manco <[email protected]>
*
* NEC Europe Ltd. DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE AND THE WARRANTY AGAINST LATENT
* DEFECTS, WITH RESPECT TO THE PROGRAM AND THE ACCOMPANYING
* DOCUMENTATION.
*
* No Liability For Consequential Damages IN NO EVENT SHALL NEC Europe
* Ltd., NEC Corporation OR ANY OF ITS SUBSIDIARIES BE LIABLE FOR ANY
* DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS
* OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF INFORMATION, OR
* OTHER PECUNIARY LOSS AND INDIRECT, CONSEQUENTIAL, INCIDENTAL,
* ECONOMIC OR PUNITIVE DAMAGES) ARISING OUT OF THE USE OF OR INABILITY
* TO USE THIS PROGRAM, EVEN IF NEC Europe Ltd. HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
*/
#include "xs.hh"
#include "util.hh"
#include <algorithm>
#include <set>
namespace clickos {
namespace xenstore {
xsctl::xsctl(void)
: xsh(NULL)
{
/* Opening a xenstore connection may fail and we can't throw exceptions.
* Therefore initialize xsh in the open method, that needs to be called
* from all the other methods. */
}
xsctl::~xsctl()
{
close();
}
int xsctl::router_install(const std::string& domain,
const std::string& name, const std::string& config, router::id_t& rid)
{
int ret;
xs_transaction_t xst;
ret = open();
if (ret) {
return ret;
}
ret = 0;
for ( ; ; ) {
xst = xs_transaction_start(xsh);
if (xst == XBT_NULL) {
ret = errno;
printf("Failed to start xenstore transaction.\n");
break;
}
domid_t domid;
std::string domain_path;
std::string click_path;
ret = get_and_check_domain(xst, domain, domid, domain_path, click_path);
if (ret) {
break;
}
/* Determine next free router id */
router::id_t nrid;
ret = next_rid(xst, click_path, nrid);
if (ret) {
break;
}
/* Install configuration on xenstore */
std::string path;
std::string router_path = click_path + "/" + std::to_string(nrid);
std::string entries[] = {"/control", "/elements"};
for (std::string e : entries) {
path = router_path + e;
if (!xs_mkdir(xsh, xst, path.c_str())) {
ret = errno;
printf("Failed to write to xenstore.\n");
break;
}
}
if (ret) {
break;
}
path = router_path + "/config_name";
if (!xs_write(xsh, xst, path.c_str(), name.c_str(), name.length())) {
ret = errno;
printf("Failed to write to xenstore.\n");
break;
}
path = router_path + "/status";
std::string status_str;
status2str(router::status_t::stopped, status_str);
if (!xs_write(xsh, xst, path.c_str(), status_str.c_str(), status_str.length())) {
ret = errno;
printf("Failed to write to xenstore.\n");
break;
}
for (unsigned int i = 0 ; ; i++) {
path = router_path + "/config/" + std::to_string(i);
unsigned int pos = i * chunk_max_len;
if (pos >= config.length()) {
break;
}
std::string chunk = config.substr(i * chunk_max_len, chunk_max_len);
if (!xs_write(xsh, xst, path.c_str(), chunk.c_str(), chunk.length())) {
ret = errno;
printf("Failed to write to xenstore.\n");
break;
}
}
if (ret) {
break;
}
if (!xs_transaction_end(xsh, xst, false)) {
if (errno == EAGAIN) {
continue;
} else {
ret = errno;
printf("Failed to write to xenstore.\n");
}
} else {
rid = nrid;
}
xst = XBT_NULL;
break;
}
if (xst != XBT_NULL) {
xs_transaction_end(xsh, xst, true);
xst = XBT_NULL;
}
return ret;
}
int xsctl::router_remove(const std::string& domain, router::id_t rid, bool force)
{
int ret;
xs_transaction_t xst;
ret = open();
if (ret) {
return ret;
}
ret = 0;
for ( ; ; ) {
xst = xs_transaction_start(xsh);
if (xst == XBT_NULL) {
ret = errno;
printf("Failed to start xenstore transaction.\n");
break;
}
domid_t domid;
std::string domain_path;
std::string click_path;
ret = get_and_check_domain(xst, domain, domid, domain_path, click_path);
if (ret) {
break;
}
/* Determine router status */
std::string router_path = click_path + "/" + std::to_string(rid);
std::string status_path = router_path + "/status";
char* result;
unsigned int length;
result = static_cast<char*>(xs_read(xsh, xst, status_path.c_str(), &length));
if (!result) {
ret = errno;
if (ret == ENOENT) {
printf("Router not found.\n");
} else {
printf("Fail to read from xenstore.\n");
}
break;
}
std::string status_str(result);
free(result);
router::status_t cstatus;
ret = str2status(status_str, cstatus);
if (ret) {
printf("Router status is invalid: %s.\n", status_str.c_str());
break;
}
if (cstatus == router::status_t::running) {
if (force) {
printf("Router running but force enable, continuing.\n");
} else {
ret = EINVAL;
printf("Cannot remove running router.\n");
break;
}
}
if (!xs_rm(xsh, xst, router_path.c_str())) {
ret = errno;
printf("Failed to write to xenstore.\n");
break;
}
if (!xs_transaction_end(xsh, xst, false)) {
if (errno == EAGAIN) {
continue;
} else {
ret = errno;
printf("Failed to write to xenstore.\n");
}
}
xst = XBT_NULL;
break;
}
if (xst != XBT_NULL) {
xs_transaction_end(xsh, xst, true);
xst = XBT_NULL;
}
return ret;
}
int xsctl::router_set_status(const std::string& domain, router::id_t rid, router::status_t nstatus)
{
int ret;
xs_transaction_t xst;
ret = open();
if (ret) {
return ret;
}
ret = 0;
for ( ; ; ) {
xst = xs_transaction_start(xsh);
if (xst == XBT_NULL) {
ret = errno;
printf("Failed to start xenstore transaction.\n");
break;
}
domid_t domid;
std::string domain_path;
std::string click_path;
ret = get_and_check_domain(xst, domain, domid, domain_path, click_path);
if (ret) {
break;
}
std::string status_path = click_path + "/" + std::to_string(rid) + "/status";
char* result;
unsigned int length;
result = static_cast<char*>(xs_read(xsh, xst, status_path.c_str(), &length));
if (!result) {
ret = errno;
if (ret == ENOENT) {
printf("Router not found.\n");
} else {
printf("Fail to read from xenstore.\n");
}
break;
}
std::string status_str(result);
free(result);
router::status_t cstatus;
ret = str2status(status_str, cstatus);
if (ret) {
printf("Router status is invalid: %s.\n", status_str.c_str());
break;
}
if (nstatus == cstatus) {
ret = EINVAL;
printf("Router already in %s state.\n", status_str.c_str());
break;
}
status2str(nstatus, status_str);
if (!xs_write(xsh, xst, status_path.c_str(), status_str.c_str(), status_str.length())) {
ret = errno;
printf("Failed to write to xenstore.\n");
break;
}
if (!xs_transaction_end(xsh, xst, false)) {
if (errno == EAGAIN) {
continue;
} else {
ret = errno;
printf("Failed to write to xenstore.\n");
}
}
xst = XBT_NULL;
break;
}
if (xst != XBT_NULL) {
xs_transaction_end(xsh, xst, true);
xst = XBT_NULL;
}
return ret;
}
int xsctl::open(void)
{
if (xsh) {
return 0;
}
xsh = xs_open(0);
if (xsh == NULL) {
printf("Failed to connect to xenstore.\n");
return errno;
}
return 0;
}
void xsctl::close(void)
{
if (!xsh) {
return;
}
xs_close(xsh);
xsh = NULL;
}
int xsctl::get_and_check_domain(xs_transaction_t xst, const std::string& domain,
domid_t& domid, std::string& domain_path, std::string& click_path)
{
int ret;
char* result;
unsigned int len;
ret = name2domid(domain, domid);
if (ret) {
if (ret == EINVAL) {
printf("Invalid domain: %s.\n", domain.c_str());
}
return ret;
}
result = xs_get_domain_path(xsh, domid);
if (!result) {
printf("Failed to retrieve domain path.\n");
return errno;
}
domain_path = std::string(result);
free(result);
click_path = domain_path + click_base_path;
result = static_cast<char*>(xs_read(xsh, xst, click_path.c_str(), &len));
if (!result) {
printf("Domain %s, doesn't seem to be a ClickOS domain.\n", domain.c_str());
return EINVAL;
}
free(result);
return 0;
}
int xsctl::next_rid(xs_transaction_t xst, const std::string& click_path, router::id_t& rid)
{
char** router_list;
unsigned int router_num;
std::set<router::id_t> rids;
router_list = xs_directory(xsh, XBT_NULL, click_path.c_str(), &router_num);
if (router_list == NULL) {
printf("Failed to read from xenstore.\n");
return EINVAL;
}
for (unsigned int i = 0; i < router_num; i++) {
router::id_t id;
if (clickos::get_int(router_list[i], id) == 0) {
rids.insert(id);
}
}
router::id_t nrid = 0;
while (!rids.empty()) {
if (nrid != *(rids.begin())) {
break;
}
nrid++;
rids.erase(rids.begin());
}
rid = nrid;
return 0;
}
int xsctl::name2domid(const std::string& name, domid_t& domid)
{
int ret;
char* path;
char* dom_name;
char** dom_list;
unsigned int length;
unsigned int dom_num;
if (clickos::get_int(name, domid) == 0) {
return 0;
}
ret = open();
if (ret) {
return ret;
}
/* All domains should have a directory under /local/domain. Get the list. */
dom_list = xs_directory(xsh, XBT_NULL, "/local/domain", &dom_num);
if (dom_list == NULL) {
printf("Failed to read from xenstore.\n");
return errno;
}
ret = EINVAL;
for (unsigned int i = 0; i < dom_num; i++) {
domid_t id;
/* Check the entry is a number (domid), otherwise it's garbage. Keep the value in case this
* is the domain we're looking for.
*/
if (clickos::get_int(dom_list[i], id)) {
continue;
}
/* Get the name under /local/domain/<domid>/name. */
if (asprintf(&path, "/local/domain/%s/name", dom_list[i]) == -1) {
ret = ENOMEM;
printf("Failed to allocate memory.\n");
break;
}
dom_name = static_cast<char*>(xs_read(xsh, XBT_NULL, path, &length));
free(path);
/* It could fail to read for multiple reasons, but according to xenstore.h there's no way
* to know if the entry doesn't exist or it actually failed, so we just ignore it.
*/
if (dom_name == NULL) {
continue;
}
std::string dom_name_str(dom_name);
free(dom_name);
if (name == dom_name_str) {
ret = 0;
domid = id;
break;
}
}
free(dom_list);
return ret;
}
int xsctl::str2status(const std::string& str, router::status_t& status)
{
if (str.empty()) {
status = router::status_t::unknown;
} else if (str == "Running") {
status = router::status_t::running;
} else if (str == "Halted") {
status = router::status_t::stopped;
} else if (str == "Error") {
status = router::status_t::error;
} else {
return EINVAL;
}
return 0;
}
void xsctl::status2str(router::status_t status, std::string& str)
{
switch (status) {
case router::status_t::unknown:
str = "";
break;
case router::status_t::running:
str = "Running";
break;
case router::status_t::stopped:
str = "Halted";
break;
case router::status_t::error:
str = "Error";
break;
}
}
} /* namespace xenstore */
} /* namespace clickos */