-
Notifications
You must be signed in to change notification settings - Fork 7
/
clickos.cc
211 lines (177 loc) · 5.72 KB
/
clickos.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
/*
* ClickOS Control
*
* file: clickos.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 "args.hh"
#include "clickos.hh"
#include "util.hh"
#include "xs.hh"
#include <list>
#include <string>
#include <cerrno>
#include <cstdint>
#include <cstddef>
extern "C"
{
#include <xenctrl.h>
#include <xenstore.h>
}
static int op_install(clickos::conf& c)
{
int ret;
std::string router_config;
ret = clickos::read_click_config(c.click_config_path, router_config);
if (ret) {
return ret;
}
std::string name;
clickos::basename(c.click_config_path, name);
ret = 0;
switch (c.mech) {
case clickos::mechanism::xenstore: {
clickos::router::id_t rid;
clickos::xenstore::xsctl xsc;
ret = xsc.router_install(c.domain, name, router_config, rid);
if (ret) {
printf("[ %s ] failed to install router.\n", c.domain.c_str());
break;
} else {
printf("[ %s ][ %u ]: installed new router.\n", c.domain.c_str(), rid);
}
if (c.install_start) {
ret = xsc.router_set_status(c.domain, rid, clickos::router::status_t::running);
if (ret) {
printf("[ %s ][ %u ]: failed to start router.\n", c.domain.c_str(), rid);
break;
} else {
printf("[ %s ][ %u ]: started router.\n", c.domain.c_str(), rid);
}
}
} break;
}
return ret;
}
static int op_remove(clickos::conf& c)
{
int ret;
ret = 0;
switch (c.mech) {
case clickos::mechanism::xenstore: {
clickos::xenstore::xsctl xsc;
if (c.remove_stop) {
ret = xsc.router_set_status(c.domain, c.router_id,
clickos::router::status_t::stopped);
if (ret) {
printf("[ %s ][ %u ]: failed to stop router.\n", c.domain.c_str(), c.router_id);
if (!c.remove_force) {
break;
}
}
}
ret = xsc.router_remove(c.domain, c.router_id, c.remove_force);
if (ret) {
printf("[ %s ][ %u ]: failed to remove router.\n", c.domain.c_str(), c.router_id);
} else {
printf("[ %s ][ %u ]: router removed.\n", c.domain.c_str(), c.router_id);
}
} break;
}
return ret;
}
static int op_start(clickos::conf& c)
{
int ret;
ret = 0;
switch (c.mech) {
case clickos::mechanism::xenstore: {
clickos::xenstore::xsctl xsc;
ret = xsc.router_set_status(c.domain, c.router_id, clickos::router::status_t::running);
if (ret) {
printf("[ %s ][ %u ]: failed to start router.\n", c.domain.c_str(), c.router_id);
} else {
printf("[ %s ][ %u ]: router started.\n", c.domain.c_str(), c.router_id);
}
} break;
}
return ret;
}
static int op_stop(clickos::conf& c)
{
int ret;
ret = 0;
switch (c.mech) {
case clickos::mechanism::xenstore: {
clickos::xenstore::xsctl xsc;
ret = xsc.router_set_status(c.domain, c.router_id, clickos::router::status_t::stopped);
if (ret) {
printf("[ %s ][ %u ]: failed to stop router.\n", c.domain.c_str(), c.router_id);
} else {
printf("[ %s ][ %u ]: router stopped.\n", c.domain.c_str(), c.router_id);
}
} break;
}
return ret;
}
int main (int argc, char** argv)
{
clickos::conf c;
if (c.parse(argc, argv)) {
c.print_usage(argv[0]);
return -EINVAL;
}
if (c.help) {
c.print_usage(argv[0]);
return 0;
}
switch (c.op) {
case clickos::operation::install: {
op_install(c);
} break;
case clickos::operation::remove: {
op_remove(c);
} break;
case clickos::operation::start: {
op_start(c);
} break;
case clickos::operation::stop: {
op_stop(c);
} break;
case clickos::operation::none: {
} break;
}
return 0;
}