Skip to content

Commit 3638401

Browse files
authored
Merge pull request #166 from richiMarchi/pr/save_load_topology
load old topology at startup and save each modification to file
2 parents 86bc968 + 1a5a398 commit 3638401

22 files changed

+975
-54
lines changed

Documentation/polycubed/polycubed.rst

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,36 @@ If the same parameter is specified in both, the configuration file and the comma
7878
daemon: true
7979
#p: 6000 <-- this is NOT supported, only long options are
8080

81+
82+
83+
Persistency
84+
^^^^^^^^^^^
85+
86+
Polycubed has persistent capabilities which means that (1) it can automatically load the configuration that was present when the daemon was shut down, (2) each time a configuration command is issued, it is automatically dumped on disk.
87+
This allows polycubed also to recover from failures, such as rebooting the machine.
88+
To enable this feature we need to start polycubed with the ``--cubes-dump-enable`` flag.
89+
The daemon keeps in memory an instance of all the topology, including the configuration of each individual service.
90+
Topology and configuration are automatically updated at each new command; the configuration is also dumped to disk, on file ``/etc/polycube/cubes.yaml``.
91+
The standard behavior of the daemon at startup is to load, if present, the latest topology that was active at the end of the previous execution.
92+
Users can load a different topology file by using the ``--cubes-dump-file`` flag followed by the path to the file.
93+
In case we want to start polycubed with an empty topology, avoiding any possible load at startup, we can launch polycubed with the ``--cubes-dump-clean-init`` flag. Beware that in this case any existing configuration in the default file will be overwritten.
94+
``--cubes-dump-enable`` is required if we want to use any of the other two related flags.
95+
There are some limitations: (1) YANG actions, such as "append" for firewall and nat rules, are not supported, (2) some services fail to load the full configuration at once and (3) transparent services attached to netdevs are not saved in the cubes dump file.
96+
97+
::
98+
99+
# start daemon with topology saving functionalities
100+
polycubed --cubes-dump-enable
101+
102+
# start polycubed with custom cubes configuration
103+
polycubed --cubes-dump-enable --cubes-dump-file ~/Desktop/myCubes.yaml
104+
105+
# start polycubed with an empty topology
106+
polycubed --cubes-dump-enable --cubes-dump-clean-init
107+
108+
109+
81110
Rest API
82111
^^^^^^^^
83112

84-
TODO...
113+
TODO...

src/polycubed/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ set(polycubed_sources
6262
utils/veth.cpp
6363
utils/netlink.cpp
6464
utils/utils.cpp
65+
cubes_dump.cpp
6566
${server_sources}
6667
${CMAKE_CURRENT_BINARY_DIR}/load_services.cpp
6768
${CMAKE_CURRENT_BINARY_DIR}/version.cpp)

src/polycubed/src/config.cpp

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,19 @@ namespace configuration {
3232

3333
#define LOGLEVEL spdlog::level::level_enum::info
3434
#define DAEMON false
35+
#define CUBESDUMPCLEANINIT false
36+
//#define CUBESNODUMP false
37+
#define CUBESDUMPENABLED false
38+
#define CUBESDUMPFILEFLAG false
3539
#define PIDFILE "/var/run/polycube.pid"
3640
#define SERVER_PORT 9000
3741
#define SERVER_IP "localhost"
3842
#define LOGFILE "/var/log/polycube/polycubed.log"
3943
#define CONFIGFILEDIR "/etc/polycube"
4044
#define CONFIGFILENAME "polycubed.conf"
45+
#define CUBESDUMPFILENAME "cubes.yaml"
4146
#define CONFIGFILE (CONFIGFILEDIR "/" CONFIGFILENAME)
47+
#define CUBESDUMPFILEPATH (CONFIGFILEDIR "/" CUBESDUMPFILENAME)
4248

4349
static void show_usage(const std::string &name) {
4450
std::cout << std::boolalpha;
@@ -64,6 +70,14 @@ static void show_usage(const std::string &name) {
6470
<< ")" << std::endl;
6571
std::cout << "--configfile: configuration file (default: " << CONFIGFILE
6672
<< ")" << std::endl;
73+
std::cout << "--cubes-dump-file: file that keeps the last topology, "
74+
"including the configuration of all cubes (default: "
75+
<< CUBESDUMPFILEPATH << ")" << std::endl;
76+
std::cout << "--cubes-dump-clean-init: starts the daemon with an empty "
77+
"topology" << std::endl;
78+
//std::cout << "--cubes-nodump: starts the daemon without dumping updates to file" << std::endl;
79+
std::cout << "--cubes-dump-enable: enables dumping updates to file"
80+
<< std::endl;
6781
std::cout << "--cert-blacklist: path to black listed certificates"
6882
<< std::endl;
6983
std::cout << "--cert-whitelist: path to white listed certificates"
@@ -95,7 +109,12 @@ Config::Config()
95109
server_port(SERVER_PORT),
96110
server_ip(SERVER_IP),
97111
logfile(LOGFILE),
98-
configfile(CONFIGFILE) {}
112+
configfile(CONFIGFILE),
113+
cubes_dump_file(CUBESDUMPFILEPATH),
114+
cubes_dump_clean_init(CUBESDUMPCLEANINIT),
115+
//cubes_nodump(CUBESNODUMP)
116+
cubes_dump_enabled(CUBESDUMPENABLED),
117+
cubes_dump_file_flag(CUBESDUMPFILEFLAG) {}
99118

100119
Config::~Config() {}
101120

@@ -180,6 +199,40 @@ void Config::setLogFile(const std::string &value) {
180199
logfile = value;
181200
}
182201

202+
std::string Config::getCubesDumpFile() const {
203+
return cubes_dump_file;
204+
}
205+
206+
void Config::setCubesDumpFile(const std::string &value) {
207+
cubes_dump_file_flag = true;
208+
CHECK_OVERWRITE("cubes-dump-file", value, cubes_dump_file, CUBESDUMPFILEPATH);
209+
cubes_dump_file = value;
210+
}
211+
212+
bool Config::getCubesDumpCleanInit() const {
213+
return cubes_dump_clean_init;
214+
}
215+
216+
void Config::setCubesDumpCleanInit() {
217+
cubes_dump_clean_init = true;
218+
}
219+
220+
//bool Config::getCubesNoDump() const {
221+
// return cubes_nodump;
222+
//}
223+
224+
//void Config::setCubesNoDump() {
225+
// cubes_nodump = true;
226+
//}
227+
228+
bool Config::getCubesDumpEnabled() const {
229+
return cubes_dump_enabled;
230+
}
231+
232+
void Config::setCubesDumpEnabled() {
233+
cubes_dump_enabled = true;
234+
}
235+
183236
std::string Config::getCertPath() const {
184237
return cert_path;
185238
}
@@ -246,6 +299,8 @@ void Config::create_configuration_file(const std::string &path) {
246299
file << "addr: " << server_ip << std::endl;
247300
file << "# file to save polycube logs" << std::endl;
248301
file << "logfile: " << logfile << std::endl;
302+
file << "# file to save last topology" << std::endl;
303+
file << "cubes-dump-file: " << cubes_dump_file << std::endl;
249304
file << "# Security related:" << std::endl;
250305
file << "# server certificate " << std::endl;
251306
file << "#cert: path_to_certificate_file" << std::endl;
@@ -270,6 +325,10 @@ void Config::dump() {
270325
logger->info(" port: {}", server_port);
271326
logger->info(" addr: {}", server_ip);
272327
logger->info(" logfile: {}", logfile);
328+
logger->info(" cubes-dump-file: {}", cubes_dump_file);
329+
logger->info(" cubes-dump-clean-init: {}", cubes_dump_clean_init);
330+
//logger->info(" cubes-nodump: {}", cubes_nodump);
331+
logger->info(" cubes-dump-enable: {}", cubes_dump_enabled);
273332
if (!cert_path.empty()) {
274333
logger->info(" cert: {}", cert_path);
275334
}
@@ -363,6 +422,10 @@ static struct option options[] = {
363422
{"cacert", required_argument, NULL, 5},
364423
{"cert-whitelist", required_argument, NULL, 6},
365424
{"cert-blacklist", required_argument, NULL, 7},
425+
{"cubes-dump-file", required_argument, NULL, 8},
426+
{"cubes-dump-clean-init", no_argument, NULL, 9},
427+
//{"cubes-nodump", no_argument, NULL, 10},
428+
{"cubes-dump-enable", no_argument, NULL, 10},
366429
{NULL, 0, NULL, 0},
367430
};
368431

@@ -408,6 +471,16 @@ void Config::load_from_cli(int argc, char *argv[]) {
408471
case 7:
409472
setCertBlacklistPath(optarg);
410473
break;
474+
case 8:
475+
setCubesDumpFile(optarg);
476+
break;
477+
case 9:
478+
setCubesDumpCleanInit();
479+
break;
480+
case 10:
481+
//setCubesNoDump();
482+
setCubesDumpEnabled();
483+
break;
411484
}
412485
}
413486
}
@@ -437,6 +510,15 @@ bool Config::load(int argc, char *argv[]) {
437510
load_from_file(configfile);
438511
load_from_cli(argc, argv);
439512
check();
513+
514+
if (cubes_dump_clean_init) {
515+
std::ofstream output(cubes_dump_file);
516+
if (output.is_open()) {
517+
output << "{}";
518+
output.close();
519+
}
520+
}
521+
440522
return true;
441523
}
442524

@@ -472,5 +554,10 @@ void Config::check() {
472554
if (cert_blacklist_path != "" && cacert_path == "") {
473555
throw std::runtime_error("--cert-blacklist requires --cacert");
474556
}
557+
558+
if ((cubes_dump_clean_init || cubes_dump_file_flag) && !cubes_dump_enabled) {
559+
throw std::runtime_error("--cubes-dump-file and --cubes-dump-clean-init "
560+
"require --cubes-dump-enable");
561+
}
475562
}
476563
}

src/polycubed/src/config.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ class Config {
5353
std::string getLogFile() const;
5454
void setLogFile(const std::string &value);
5555

56+
// file where last topology is saved
57+
std::string getCubesDumpFile() const;
58+
void setCubesDumpFile(const std::string &value);
59+
60+
// set to get an empty topology
61+
bool getCubesDumpCleanInit() const;
62+
void setCubesDumpCleanInit();
63+
64+
// set to avoid daemon to dump updates to file
65+
//bool getCubesNoDump() const;
66+
//void setCubesNoDump();
67+
68+
// set to let daemon to dump updates to file
69+
bool getCubesDumpEnabled() const;
70+
void setCubesDumpEnabled();
71+
5672
// path of certificate & key to be used in server
5773
std::string getCertPath() const;
5874
void setCertPath(const std::string &value);
@@ -80,11 +96,17 @@ class Config {
8096

8197
spdlog::level::level_enum loglevel;
8298
bool daemon;
99+
bool cubes_dump_clean_init;
100+
//bool cubes_nodump;
101+
bool cubes_dump_enabled;
102+
// true if --cubes-dump-file flag is used
103+
bool cubes_dump_file_flag;
83104
std::string pidfile;
84105
uint16_t server_port;
85106
std::string server_ip;
86107
std::string logfile;
87108
std::string configfile;
109+
std::string cubes_dump_file;
88110
std::string cert_path, key_path;
89111
std::string cacert_path;
90112
std::string cert_whitelist_path;

0 commit comments

Comments
 (0)