CV1.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "CV1.hpp"
  14. #ifdef ZT_CONTROLLER_USE_LIBPQ
  15. #include "../node/Constants.hpp"
  16. #include "../node/SHA512.hpp"
  17. #include "EmbeddedNetworkController.hpp"
  18. #include "../version.h"
  19. #include "Redis.hpp"
  20. #include "CtlUtil.hpp"
  21. #include <smeeclient.h>
  22. #include <libpq-fe.h>
  23. #include <sstream>
  24. #include <iomanip>
  25. #include <climits>
  26. #include <chrono>
  27. // #define REDIS_TRACE 1
  28. using json = nlohmann::json;
  29. namespace {
  30. static const int DB_MINIMUM_VERSION = 38;
  31. } // anonymous namespace
  32. using namespace ZeroTier;
  33. using Attrs = std::vector<std::pair<std::string, std::string>>;
  34. using Item = std::pair<std::string, Attrs>;
  35. using ItemStream = std::vector<Item>;
  36. CV1::CV1(const Identity &myId, const char *path, int listenPort, RedisConfig *rc)
  37. : DB()
  38. , _pool()
  39. , _myId(myId)
  40. , _myAddress(myId.address())
  41. , _ready(0)
  42. , _connected(1)
  43. , _run(1)
  44. , _waitNoticePrinted(false)
  45. , _listenPort(listenPort)
  46. , _rc(rc)
  47. , _redis(NULL)
  48. , _cluster(NULL)
  49. , _redisMemberStatus(false)
  50. , _smee(NULL)
  51. {
  52. char myAddress[64];
  53. _myAddressStr = myId.address().toString(myAddress);
  54. _connString = std::string(path);
  55. auto f = std::make_shared<PostgresConnFactory>(_connString);
  56. _pool = std::make_shared<ConnectionPool<PostgresConnection> >(
  57. 15, 5, std::static_pointer_cast<ConnectionFactory>(f));
  58. memset(_ssoPsk, 0, sizeof(_ssoPsk));
  59. char *const ssoPskHex = getenv("ZT_SSO_PSK");
  60. #ifdef ZT_TRACE
  61. fprintf(stderr, "ZT_SSO_PSK: %s\n", ssoPskHex);
  62. #endif
  63. if (ssoPskHex) {
  64. // SECURITY: note that ssoPskHex will always be null-terminated if libc actually
  65. // returns something non-NULL. If the hex encodes something shorter than 48 bytes,
  66. // it will be padded at the end with zeroes. If longer, it'll be truncated.
  67. Utils::unhex(ssoPskHex, _ssoPsk, sizeof(_ssoPsk));
  68. }
  69. const char *redisMemberStatus = getenv("ZT_REDIS_MEMBER_STATUS");
  70. if (redisMemberStatus && (strcmp(redisMemberStatus, "true") == 0)) {
  71. _redisMemberStatus = true;
  72. fprintf(stderr, "Using redis for member status\n");
  73. }
  74. auto c = _pool->borrow();
  75. pqxx::work txn{*c->c};
  76. pqxx::row r{txn.exec1("SELECT version FROM ztc_database")};
  77. int dbVersion = r[0].as<int>();
  78. txn.commit();
  79. if (dbVersion < DB_MINIMUM_VERSION) {
  80. fprintf(stderr, "Central database schema version too low. This controller version requires a minimum schema version of %d. Please upgrade your Central instance", DB_MINIMUM_VERSION);
  81. exit(1);
  82. }
  83. _pool->unborrow(c);
  84. if (_rc != NULL) {
  85. sw::redis::ConnectionOptions opts;
  86. sw::redis::ConnectionPoolOptions poolOpts;
  87. opts.host = _rc->hostname;
  88. opts.port = _rc->port;
  89. opts.password = _rc->password;
  90. opts.db = 0;
  91. opts.keep_alive = true;
  92. opts.connect_timeout = std::chrono::seconds(3);
  93. poolOpts.size = 25;
  94. poolOpts.wait_timeout = std::chrono::seconds(5);
  95. poolOpts.connection_lifetime = std::chrono::minutes(3);
  96. poolOpts.connection_idle_time = std::chrono::minutes(1);
  97. if (_rc->clusterMode) {
  98. fprintf(stderr, "Using Redis in Cluster Mode\n");
  99. _cluster = std::make_shared<sw::redis::RedisCluster>(opts, poolOpts);
  100. } else {
  101. fprintf(stderr, "Using Redis in Standalone Mode\n");
  102. _redis = std::make_shared<sw::redis::Redis>(opts, poolOpts);
  103. }
  104. }
  105. _readyLock.lock();
  106. fprintf(stderr, "[%s] NOTICE: %.10llx controller PostgreSQL waiting for initial data download..." ZT_EOL_S, ::_timestr(), (unsigned long long)_myAddress.toInt());
  107. _waitNoticePrinted = true;
  108. initializeNetworks();
  109. initializeMembers();
  110. _heartbeatThread = std::thread(&CV1::heartbeat, this);
  111. _membersDbWatcher = std::thread(&CV1::membersDbWatcher, this);
  112. _networksDbWatcher = std::thread(&CV1::networksDbWatcher, this);
  113. for (int i = 0; i < ZT_CENTRAL_CONTROLLER_COMMIT_THREADS; ++i) {
  114. _commitThread[i] = std::thread(&CV1::commitThread, this);
  115. }
  116. _onlineNotificationThread = std::thread(&CV1::onlineNotificationThread, this);
  117. configureSmee();
  118. }
  119. CV1::~CV1()
  120. {
  121. if (_smee != NULL) {
  122. smeeclient::smee_client_delete(_smee);
  123. _smee = NULL;
  124. }
  125. _run = 0;
  126. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  127. _heartbeatThread.join();
  128. _membersDbWatcher.join();
  129. _networksDbWatcher.join();
  130. _commitQueue.stop();
  131. for (int i = 0; i < ZT_CENTRAL_CONTROLLER_COMMIT_THREADS; ++i) {
  132. _commitThread[i].join();
  133. }
  134. _onlineNotificationThread.join();
  135. }
  136. void CV1::configureSmee()
  137. {
  138. const char *TEMPORAL_SCHEME = "ZT_TEMPORAL_SCHEME";
  139. const char *TEMPORAL_HOST = "ZT_TEMPORAL_HOST";
  140. const char *TEMPORAL_PORT = "ZT_TEMPORAL_PORT";
  141. const char *TEMPORAL_NAMESPACE = "ZT_TEMPORAL_NAMESPACE";
  142. const char *SMEE_TASK_QUEUE = "ZT_SMEE_TASK_QUEUE";
  143. const char *scheme = getenv(TEMPORAL_SCHEME);
  144. if (scheme == NULL) {
  145. scheme = "http";
  146. }
  147. const char *host = getenv(TEMPORAL_HOST);
  148. const char *port = getenv(TEMPORAL_PORT);
  149. const char *ns = getenv(TEMPORAL_NAMESPACE);
  150. const char *task_queue = getenv(SMEE_TASK_QUEUE);
  151. if (scheme != NULL && host != NULL && port != NULL && ns != NULL && task_queue != NULL) {
  152. fprintf(stderr, "creating smee client\n");
  153. std::string hostPort = std::string(scheme) + std::string("://") + std::string(host) + std::string(":") + std::string(port);
  154. this->_smee = smeeclient::smee_client_new(hostPort.c_str(), ns, task_queue);
  155. } else {
  156. fprintf(stderr, "Smee client not configured\n");
  157. }
  158. }
  159. bool CV1::waitForReady()
  160. {
  161. while (_ready < 2) {
  162. _readyLock.lock();
  163. _readyLock.unlock();
  164. }
  165. return true;
  166. }
  167. bool CV1::isReady()
  168. {
  169. return ((_ready == 2)&&(_connected));
  170. }
  171. bool CV1::save(nlohmann::json &record,bool notifyListeners)
  172. {
  173. bool modified = false;
  174. try {
  175. if (!record.is_object()) {
  176. fprintf(stderr, "record is not an object?!?\n");
  177. return false;
  178. }
  179. const std::string objtype = record["objtype"];
  180. if (objtype == "network") {
  181. //fprintf(stderr, "network save\n");
  182. const uint64_t nwid = OSUtils::jsonIntHex(record["id"],0ULL);
  183. if (nwid) {
  184. nlohmann::json old;
  185. get(nwid,old);
  186. if ((!old.is_object())||(!_compareRecords(old,record))) {
  187. record["revision"] = OSUtils::jsonInt(record["revision"],0ULL) + 1ULL;
  188. _commitQueue.post(std::pair<nlohmann::json,bool>(record,notifyListeners));
  189. modified = true;
  190. }
  191. }
  192. } else if (objtype == "member") {
  193. std::string networkId = record["nwid"];
  194. std::string memberId = record["id"];
  195. const uint64_t nwid = OSUtils::jsonIntHex(record["nwid"],0ULL);
  196. const uint64_t id = OSUtils::jsonIntHex(record["id"],0ULL);
  197. //fprintf(stderr, "member save %s-%s\n", networkId.c_str(), memberId.c_str());
  198. if ((id)&&(nwid)) {
  199. nlohmann::json network,old;
  200. get(nwid,network,id,old);
  201. if ((!old.is_object())||(!_compareRecords(old,record))) {
  202. //fprintf(stderr, "commit queue post\n");
  203. record["revision"] = OSUtils::jsonInt(record["revision"],0ULL) + 1ULL;
  204. _commitQueue.post(std::pair<nlohmann::json,bool>(record,notifyListeners));
  205. modified = true;
  206. } else {
  207. //fprintf(stderr, "no change\n");
  208. }
  209. }
  210. } else {
  211. fprintf(stderr, "uhh waaat\n");
  212. }
  213. } catch (std::exception &e) {
  214. fprintf(stderr, "Error on PostgreSQL::save: %s\n", e.what());
  215. } catch (...) {
  216. fprintf(stderr, "Unknown error on PostgreSQL::save\n");
  217. }
  218. return modified;
  219. }
  220. void CV1::eraseNetwork(const uint64_t networkId)
  221. {
  222. fprintf(stderr, "PostgreSQL::eraseNetwork\n");
  223. char tmp2[24];
  224. waitForReady();
  225. Utils::hex(networkId, tmp2);
  226. std::pair<nlohmann::json,bool> tmp;
  227. tmp.first["id"] = tmp2;
  228. tmp.first["objtype"] = "_delete_network";
  229. tmp.second = true;
  230. _commitQueue.post(tmp);
  231. nlohmann::json nullJson;
  232. _networkChanged(tmp.first, nullJson, true);
  233. }
  234. void CV1::eraseMember(const uint64_t networkId, const uint64_t memberId)
  235. {
  236. fprintf(stderr, "PostgreSQL::eraseMember\n");
  237. char tmp2[24];
  238. waitForReady();
  239. std::pair<nlohmann::json,bool> tmp, nw;
  240. Utils::hex(networkId, tmp2);
  241. tmp.first["nwid"] = tmp2;
  242. Utils::hex(memberId, tmp2);
  243. tmp.first["id"] = tmp2;
  244. tmp.first["objtype"] = "_delete_member";
  245. tmp.second = true;
  246. _commitQueue.post(tmp);
  247. nlohmann::json nullJson;
  248. _memberChanged(tmp.first, nullJson, true);
  249. }
  250. void CV1::nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress &physicalAddress)
  251. {
  252. std::lock_guard<std::mutex> l(_lastOnline_l);
  253. std::pair<int64_t, InetAddress> &i = _lastOnline[std::pair<uint64_t,uint64_t>(networkId, memberId)];
  254. i.first = OSUtils::now();
  255. if (physicalAddress) {
  256. i.second = physicalAddress;
  257. }
  258. }
  259. AuthInfo CV1::getSSOAuthInfo(const nlohmann::json &member, const std::string &redirectURL)
  260. {
  261. Metrics::db_get_sso_info++;
  262. // NONCE is just a random character string. no semantic meaning
  263. // state = HMAC SHA384 of Nonce based on shared sso key
  264. //
  265. // need nonce timeout in database? make sure it's used within X time
  266. // X is 5 minutes for now. Make configurable later?
  267. //
  268. // how do we tell when a nonce is used? if auth_expiration_time is set
  269. std::string networkId = member["nwid"];
  270. std::string memberId = member["id"];
  271. char authenticationURL[4096] = {0};
  272. AuthInfo info;
  273. info.enabled = true;
  274. //if (memberId == "a10dccea52" && networkId == "8056c2e21c24673d") {
  275. // fprintf(stderr, "invalid authinfo for grant's machine\n");
  276. // info.version=1;
  277. // return info;
  278. //}
  279. // fprintf(stderr, "PostgreSQL::updateMemberOnLoad: %s-%s\n", networkId.c_str(), memberId.c_str());
  280. std::shared_ptr<PostgresConnection> c;
  281. try {
  282. c = _pool->borrow();
  283. pqxx::work w(*c->c);
  284. char nonceBytes[16] = {0};
  285. std::string nonce = "";
  286. // check if the member exists first.
  287. pqxx::row count = w.exec_params1("SELECT count(id) FROM ztc_member WHERE id = $1 AND network_id = $2 AND deleted = false", memberId, networkId);
  288. if (count[0].as<int>() == 1) {
  289. // get active nonce, if exists.
  290. pqxx::result r = w.exec_params("SELECT nonce FROM ztc_sso_expiry "
  291. "WHERE network_id = $1 AND member_id = $2 "
  292. "AND ((NOW() AT TIME ZONE 'UTC') <= authentication_expiry_time) AND ((NOW() AT TIME ZONE 'UTC') <= nonce_expiration)",
  293. networkId, memberId);
  294. if (r.size() == 0) {
  295. // no active nonce.
  296. // find an unused nonce, if one exists.
  297. pqxx::result r = w.exec_params("SELECT nonce FROM ztc_sso_expiry "
  298. "WHERE network_id = $1 AND member_id = $2 "
  299. "AND authentication_expiry_time IS NULL AND ((NOW() AT TIME ZONE 'UTC') <= nonce_expiration)",
  300. networkId, memberId);
  301. if (r.size() == 1) {
  302. // we have an existing nonce. Use it
  303. nonce = r.at(0)[0].as<std::string>();
  304. Utils::unhex(nonce.c_str(), nonceBytes, sizeof(nonceBytes));
  305. } else if (r.empty()) {
  306. // create a nonce
  307. Utils::getSecureRandom(nonceBytes, 16);
  308. char nonceBuf[64] = {0};
  309. Utils::hex(nonceBytes, sizeof(nonceBytes), nonceBuf);
  310. nonce = std::string(nonceBuf);
  311. pqxx::result ir = w.exec_params0("INSERT INTO ztc_sso_expiry "
  312. "(nonce, nonce_expiration, network_id, member_id) VALUES "
  313. "($1, TO_TIMESTAMP($2::double precision/1000), $3, $4)",
  314. nonce, OSUtils::now() + 300000, networkId, memberId);
  315. w.commit();
  316. } else {
  317. // > 1 ?!? Thats an error!
  318. fprintf(stderr, "> 1 unused nonce!\n");
  319. exit(6);
  320. }
  321. } else if (r.size() == 1) {
  322. nonce = r.at(0)[0].as<std::string>();
  323. Utils::unhex(nonce.c_str(), nonceBytes, sizeof(nonceBytes));
  324. } else {
  325. // more than 1 nonce in use? Uhhh...
  326. fprintf(stderr, "> 1 nonce in use for network member?!?\n");
  327. exit(7);
  328. }
  329. r = w.exec_params(
  330. "SELECT oc.client_id, oc.authorization_endpoint, oc.issuer, oc.provider, oc.sso_impl_version "
  331. "FROM ztc_network AS n "
  332. "INNER JOIN ztc_org o "
  333. " ON o.owner_id = n.owner_id "
  334. "LEFT OUTER JOIN ztc_network_oidc_config noc "
  335. " ON noc.network_id = n.id "
  336. "LEFT OUTER JOIN ztc_oidc_config oc "
  337. " ON noc.client_id = oc.client_id AND oc.org_id = o.org_id "
  338. "WHERE n.id = $1 AND n.sso_enabled = true", networkId);
  339. std::string client_id = "";
  340. std::string authorization_endpoint = "";
  341. std::string issuer = "";
  342. std::string provider = "";
  343. uint64_t sso_version = 0;
  344. if (r.size() == 1) {
  345. client_id = r.at(0)[0].as<std::optional<std::string>>().value_or("");
  346. authorization_endpoint = r.at(0)[1].as<std::optional<std::string>>().value_or("");
  347. issuer = r.at(0)[2].as<std::optional<std::string>>().value_or("");
  348. provider = r.at(0)[3].as<std::optional<std::string>>().value_or("");
  349. sso_version = r.at(0)[4].as<std::optional<uint64_t>>().value_or(1);
  350. } else if (r.size() > 1) {
  351. fprintf(stderr, "ERROR: More than one auth endpoint for an organization?!?!? NetworkID: %s\n", networkId.c_str());
  352. } else {
  353. fprintf(stderr, "No client or auth endpoint?!?\n");
  354. }
  355. info.version = sso_version;
  356. // no catch all else because we don't actually care if no records exist here. just continue as normal.
  357. if ((!client_id.empty())&&(!authorization_endpoint.empty())) {
  358. uint8_t state[48];
  359. HMACSHA384(_ssoPsk, nonceBytes, sizeof(nonceBytes), state);
  360. char state_hex[256];
  361. Utils::hex(state, 48, state_hex);
  362. if (info.version == 0) {
  363. char url[2048] = {0};
  364. OSUtils::ztsnprintf(url, sizeof(authenticationURL),
  365. "%s?response_type=id_token&response_mode=form_post&scope=openid+email+profile&redirect_uri=%s&nonce=%s&state=%s&client_id=%s",
  366. authorization_endpoint.c_str(),
  367. url_encode(redirectURL).c_str(),
  368. nonce.c_str(),
  369. state_hex,
  370. client_id.c_str());
  371. info.authenticationURL = std::string(url);
  372. } else if (info.version == 1) {
  373. info.ssoClientID = client_id;
  374. info.issuerURL = issuer;
  375. info.ssoProvider = provider;
  376. info.ssoNonce = nonce;
  377. info.ssoState = std::string(state_hex) + "_" +networkId;
  378. info.centralAuthURL = redirectURL;
  379. #ifdef ZT_DEBUG
  380. fprintf(
  381. stderr,
  382. "ssoClientID: %s\nissuerURL: %s\nssoNonce: %s\nssoState: %s\ncentralAuthURL: %s\nprovider: %s\n",
  383. info.ssoClientID.c_str(),
  384. info.issuerURL.c_str(),
  385. info.ssoNonce.c_str(),
  386. info.ssoState.c_str(),
  387. info.centralAuthURL.c_str(),
  388. provider.c_str());
  389. #endif
  390. }
  391. } else {
  392. fprintf(stderr, "client_id: %s\nauthorization_endpoint: %s\n", client_id.c_str(), authorization_endpoint.c_str());
  393. }
  394. }
  395. _pool->unborrow(c);
  396. } catch (std::exception &e) {
  397. fprintf(stderr, "ERROR: Error updating member on load for network %s: %s\n", networkId.c_str(), e.what());
  398. }
  399. return info; //std::string(authenticationURL);
  400. }
  401. void CV1::initializeNetworks()
  402. {
  403. try {
  404. std::string setKey = "networks:{" + _myAddressStr + "}";
  405. fprintf(stderr, "Initializing Networks...\n");
  406. if (_redisMemberStatus) {
  407. fprintf(stderr, "Init Redis for networks...\n");
  408. try {
  409. if (_rc->clusterMode) {
  410. _cluster->del(setKey);
  411. } else {
  412. _redis->del(setKey);
  413. }
  414. } catch (sw::redis::Error &e) {
  415. // ignore. if this key doesn't exist, there's no reason to delete it
  416. }
  417. }
  418. std::unordered_set<std::string> networkSet;
  419. char qbuf[2048] = {0};
  420. sprintf(qbuf,
  421. "SELECT n.id, (EXTRACT(EPOCH FROM n.creation_time AT TIME ZONE 'UTC')*1000)::bigint as creation_time, n.capabilities, "
  422. "n.enable_broadcast, (EXTRACT(EPOCH FROM n.last_modified AT TIME ZONE 'UTC')*1000)::bigint AS last_modified, n.mtu, n.multicast_limit, n.name, n.private, n.remote_trace_level, "
  423. "n.remote_trace_target, n.revision, n.rules, n.tags, n.v4_assign_mode, n.v6_assign_mode, n.sso_enabled, (CASE WHEN n.sso_enabled THEN noc.client_id ELSE NULL END) as client_id, "
  424. "(CASE WHEN n.sso_enabled THEN oc.authorization_endpoint ELSE NULL END) as authorization_endpoint, "
  425. "(CASE WHEN n.sso_enabled THEN oc.provider ELSE NULL END) as provider, d.domain, d.servers, "
  426. "ARRAY(SELECT CONCAT(host(ip_range_start),'|', host(ip_range_end)) FROM ztc_network_assignment_pool WHERE network_id = n.id) AS assignment_pool, "
  427. "ARRAY(SELECT CONCAT(host(address),'/',bits::text,'|',COALESCE(host(via), 'NULL'))FROM ztc_network_route WHERE network_id = n.id) AS routes "
  428. "FROM ztc_network n "
  429. "LEFT OUTER JOIN ztc_org o "
  430. " ON o.owner_id = n.owner_id "
  431. "LEFT OUTER JOIN ztc_network_oidc_config noc "
  432. " ON noc.network_id = n.id "
  433. "LEFT OUTER JOIN ztc_oidc_config oc "
  434. " ON noc.client_id = oc.client_id AND oc.org_id = o.org_id "
  435. "LEFT OUTER JOIN ztc_network_dns d "
  436. " ON d.network_id = n.id "
  437. "WHERE deleted = false AND controller_id = '%s'", _myAddressStr.c_str());
  438. auto c = _pool->borrow();
  439. auto c2 = _pool->borrow();
  440. pqxx::work w{*c->c};
  441. fprintf(stderr, "Load networks from psql...\n");
  442. auto stream = pqxx::stream_from::query(w, qbuf);
  443. std::tuple<
  444. std::string // network ID
  445. , std::optional<int64_t> // creationTime
  446. , std::optional<std::string> // capabilities
  447. , std::optional<bool> // enableBroadcast
  448. , std::optional<uint64_t> // lastModified
  449. , std::optional<int> // mtu
  450. , std::optional<int> // multicastLimit
  451. , std::optional<std::string> // name
  452. , bool // private
  453. , std::optional<int> // remoteTraceLevel
  454. , std::optional<std::string> // remoteTraceTarget
  455. , std::optional<uint64_t> // revision
  456. , std::optional<std::string> // rules
  457. , std::optional<std::string> // tags
  458. , std::optional<std::string> // v4AssignMode
  459. , std::optional<std::string> // v6AssignMode
  460. , std::optional<bool> // ssoEnabled
  461. , std::optional<std::string> // clientId
  462. , std::optional<std::string> // authorizationEndpoint
  463. , std::optional<std::string> // ssoProvider
  464. , std::optional<std::string> // domain
  465. , std::optional<std::string> // servers
  466. , std::string // assignmentPoolString
  467. , std::string // routeString
  468. > row;
  469. uint64_t count = 0;
  470. auto tmp = std::chrono::high_resolution_clock::now();
  471. uint64_t total = 0;
  472. while (stream >> row) {
  473. auto start = std::chrono::high_resolution_clock::now();
  474. json empty;
  475. json config;
  476. initNetwork(config);
  477. std::string nwid = std::get<0>(row);
  478. std::optional<int64_t> creationTime = std::get<1>(row);
  479. std::optional<std::string> capabilities = std::get<2>(row);
  480. std::optional<bool> enableBroadcast = std::get<3>(row);
  481. std::optional<uint64_t> lastModified = std::get<4>(row);
  482. std::optional<int> mtu = std::get<5>(row);
  483. std::optional<int> multicastLimit = std::get<6>(row);
  484. std::optional<std::string> name = std::get<7>(row);
  485. bool isPrivate = std::get<8>(row);
  486. std::optional<int> remoteTraceLevel = std::get<9>(row);
  487. std::optional<std::string> remoteTraceTarget = std::get<10>(row);
  488. std::optional<uint64_t> revision = std::get<11>(row);
  489. std::optional<std::string> rules = std::get<12>(row);
  490. std::optional<std::string> tags = std::get<13>(row);
  491. std::optional<std::string> v4AssignMode = std::get<14>(row);
  492. std::optional<std::string> v6AssignMode = std::get<15>(row);
  493. std::optional<bool> ssoEnabled = std::get<16>(row);
  494. std::optional<std::string> clientId = std::get<17>(row);
  495. std::optional<std::string> authorizationEndpoint = std::get<18>(row);
  496. std::optional<std::string> ssoProvider = std::get<19>(row);
  497. std::optional<std::string> dnsDomain = std::get<20>(row);
  498. std::optional<std::string> dnsServers = std::get<21>(row);
  499. std::string assignmentPoolString = std::get<22>(row);
  500. std::string routesString = std::get<23>(row);
  501. config["id"] = nwid;
  502. config["nwid"] = nwid;
  503. config["creationTime"] = creationTime.value_or(0);
  504. config["capabilities"] = json::parse(capabilities.value_or("[]"));
  505. config["enableBroadcast"] = enableBroadcast.value_or(false);
  506. config["lastModified"] = lastModified.value_or(0);
  507. config["mtu"] = mtu.value_or(2800);
  508. config["multicastLimit"] = multicastLimit.value_or(64);
  509. config["name"] = name.value_or("");
  510. config["private"] = isPrivate;
  511. config["remoteTraceLevel"] = remoteTraceLevel.value_or(0);
  512. config["remoteTraceTarget"] = remoteTraceTarget.value_or("");
  513. config["revision"] = revision.value_or(0);
  514. config["rules"] = json::parse(rules.value_or("[]"));
  515. config["tags"] = json::parse(tags.value_or("[]"));
  516. config["v4AssignMode"] = json::parse(v4AssignMode.value_or("{}"));
  517. config["v6AssignMode"] = json::parse(v6AssignMode.value_or("{}"));
  518. config["ssoEnabled"] = ssoEnabled.value_or(false);
  519. config["objtype"] = "network";
  520. config["ipAssignmentPools"] = json::array();
  521. config["routes"] = json::array();
  522. config["clientId"] = clientId.value_or("");
  523. config["authorizationEndpoint"] = authorizationEndpoint.value_or("");
  524. config["provider"] = ssoProvider.value_or("");
  525. networkSet.insert(nwid);
  526. if (dnsDomain.has_value()) {
  527. std::string serverList = dnsServers.value();
  528. json obj;
  529. auto servers = json::array();
  530. if (serverList.rfind("{",0) != std::string::npos) {
  531. serverList = serverList.substr(1, serverList.size()-2);
  532. std::stringstream ss(serverList);
  533. while(ss.good()) {
  534. std::string server;
  535. std::getline(ss, server, ',');
  536. servers.push_back(server);
  537. }
  538. }
  539. obj["domain"] = dnsDomain.value();
  540. obj["servers"] = servers;
  541. config["dns"] = obj;
  542. }
  543. config["ipAssignmentPools"] = json::array();
  544. if (assignmentPoolString != "{}") {
  545. std::string tmp = assignmentPoolString.substr(1, assignmentPoolString.size()-2);
  546. std::vector<std::string> assignmentPools = split(tmp, ',');
  547. for (auto it = assignmentPools.begin(); it != assignmentPools.end(); ++it) {
  548. std::vector<std::string> r = split(*it, '|');
  549. json ip;
  550. ip["ipRangeStart"] = r[0];
  551. ip["ipRangeEnd"] = r[1];
  552. config["ipAssignmentPools"].push_back(ip);
  553. }
  554. }
  555. config["routes"] = json::array();
  556. if (routesString != "{}") {
  557. std::string tmp = routesString.substr(1, routesString.size()-2);
  558. std::vector<std::string> routes = split(tmp, ',');
  559. for (auto it = routes.begin(); it != routes.end(); ++it) {
  560. std::vector<std::string> r = split(*it, '|');
  561. json route;
  562. route["target"] = r[0];
  563. route["via"] = ((route["via"] == "NULL")? nullptr : r[1]);
  564. config["routes"].push_back(route);
  565. }
  566. }
  567. Metrics::network_count++;
  568. _networkChanged(empty, config, false);
  569. auto end = std::chrono::high_resolution_clock::now();
  570. auto dur = std::chrono::duration_cast<std::chrono::microseconds>(end - start);;
  571. total += dur.count();
  572. ++count;
  573. if (count > 0 && count % 10000 == 0) {
  574. fprintf(stderr, "Averaging %llu us per network\n", (total/count));
  575. }
  576. }
  577. if (count > 0) {
  578. fprintf(stderr, "Took %llu us per network to load\n", (total/count));
  579. }
  580. stream.complete();
  581. w.commit();
  582. _pool->unborrow(c2);
  583. _pool->unborrow(c);
  584. fprintf(stderr, "done.\n");
  585. if (!networkSet.empty()) {
  586. if (_redisMemberStatus) {
  587. fprintf(stderr, "adding networks to redis...\n");
  588. if (_rc->clusterMode) {
  589. auto tx = _cluster->transaction(_myAddressStr, true, false);
  590. uint64_t count = 0;
  591. for (std::string nwid : networkSet) {
  592. tx.sadd(setKey, nwid);
  593. if (++count % 30000 == 0) {
  594. tx.exec();
  595. tx = _cluster->transaction(_myAddressStr, true, false);
  596. }
  597. }
  598. tx.exec();
  599. } else {
  600. auto tx = _redis->transaction(true, false);
  601. uint64_t count = 0;
  602. for (std::string nwid : networkSet) {
  603. tx.sadd(setKey, nwid);
  604. if (++count % 30000 == 0) {
  605. tx.exec();
  606. tx = _redis->transaction(true, false);
  607. }
  608. }
  609. tx.exec();
  610. }
  611. fprintf(stderr, "done.\n");
  612. }
  613. }
  614. if (++this->_ready == 2) {
  615. if (_waitNoticePrinted) {
  616. fprintf(stderr,"[%s] NOTICE: %.10llx controller PostgreSQL data download complete." ZT_EOL_S,_timestr(),(unsigned long long)_myAddress.toInt());
  617. }
  618. _readyLock.unlock();
  619. }
  620. fprintf(stderr, "network init done.\n");
  621. } catch (sw::redis::Error &e) {
  622. fprintf(stderr, "ERROR: Error initializing networks in Redis: %s\n", e.what());
  623. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  624. exit(-1);
  625. } catch (std::exception &e) {
  626. fprintf(stderr, "ERROR: Error initializing networks: %s\n", e.what());
  627. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  628. exit(-1);
  629. }
  630. }
  631. void CV1::initializeMembers()
  632. {
  633. std::string memberId;
  634. std::string networkId;
  635. try {
  636. std::unordered_map<std::string, std::string> networkMembers;
  637. fprintf(stderr, "Initializing Members...\n");
  638. std::string setKeyBase = "network-nodes-all:{" + _myAddressStr + "}:";
  639. if (_redisMemberStatus) {
  640. fprintf(stderr, "Initialize Redis for members...\n");
  641. std::unique_lock<std::shared_mutex> l(_networks_l);
  642. std::unordered_set<std::string> deletes;
  643. for ( auto it : _networks) {
  644. uint64_t nwid_i = it.first;
  645. char nwidTmp[64] = {0};
  646. OSUtils::ztsnprintf(nwidTmp, sizeof(nwidTmp), "%.16llx", nwid_i);
  647. std::string nwid(nwidTmp);
  648. std::string key = setKeyBase + nwid;
  649. deletes.insert(key);
  650. }
  651. if (!deletes.empty()) {
  652. try {
  653. if (_rc->clusterMode) {
  654. auto tx = _cluster->transaction(_myAddressStr, true, false);
  655. for (std::string k : deletes) {
  656. tx.del(k);
  657. }
  658. tx.exec();
  659. } else {
  660. auto tx = _redis->transaction(true, false);
  661. for (std::string k : deletes) {
  662. tx.del(k);
  663. }
  664. tx.exec();
  665. }
  666. } catch (sw::redis::Error &e) {
  667. // ignore
  668. }
  669. }
  670. }
  671. char qbuf[2048];
  672. sprintf(qbuf,
  673. "SELECT m.id, m.network_id, m.active_bridge, m.authorized, m.capabilities, "
  674. "(EXTRACT(EPOCH FROM m.creation_time AT TIME ZONE 'UTC')*1000)::bigint, m.identity, "
  675. "(EXTRACT(EPOCH FROM m.last_authorized_time AT TIME ZONE 'UTC')*1000)::bigint, "
  676. "(EXTRACT(EPOCH FROM m.last_deauthorized_time AT TIME ZONE 'UTC')*1000)::bigint, "
  677. "m.remote_trace_level, m.remote_trace_target, m.tags, m.v_major, m.v_minor, m.v_rev, m.v_proto, "
  678. "m.no_auto_assign_ips, m.revision, m.sso_exempt, "
  679. "(CASE WHEN n.sso_enabled = TRUE AND m.sso_exempt = FALSE THEN "
  680. " ( "
  681. " SELECT (EXTRACT(EPOCH FROM e.authentication_expiry_time)*1000)::bigint "
  682. " FROM ztc_sso_expiry e "
  683. " INNER JOIN ztc_network n1 "
  684. " ON n1.id = e.network_id AND n1.deleted = TRUE "
  685. " WHERE e.network_id = m.network_id AND e.member_id = m.id AND n.sso_enabled = TRUE AND e.authentication_expiry_time IS NOT NULL "
  686. " ORDER BY e.authentication_expiry_time DESC LIMIT 1 "
  687. " ) "
  688. " ELSE NULL "
  689. " END) AS authentication_expiry_time, "
  690. "ARRAY(SELECT DISTINCT address FROM ztc_member_ip_assignment WHERE member_id = m.id AND network_id = m.network_id) AS assigned_addresses "
  691. "FROM ztc_member m "
  692. "INNER JOIN ztc_network n "
  693. " ON n.id = m.network_id "
  694. "WHERE n.controller_id = '%s' AND n.deleted = FALSE AND m.deleted = FALSE", _myAddressStr.c_str());
  695. auto c = _pool->borrow();
  696. auto c2 = _pool->borrow();
  697. pqxx::work w{*c->c};
  698. fprintf(stderr, "Load members from psql...\n");
  699. auto stream = pqxx::stream_from::query(w, qbuf);
  700. std::tuple<
  701. std::string // memberId
  702. , std::string // memberId
  703. , std::optional<bool> // activeBridge
  704. , std::optional<bool> // authorized
  705. , std::optional<std::string> // capabilities
  706. , std::optional<uint64_t> // creationTime
  707. , std::optional<std::string> // identity
  708. , std::optional<uint64_t> // lastAuthorizedTime
  709. , std::optional<uint64_t> // lastDeauthorizedTime
  710. , std::optional<int> // remoteTraceLevel
  711. , std::optional<std::string> // remoteTraceTarget
  712. , std::optional<std::string> // tags
  713. , std::optional<int> // vMajor
  714. , std::optional<int> // vMinor
  715. , std::optional<int> // vRev
  716. , std::optional<int> // vProto
  717. , std::optional<bool> // noAutoAssignIps
  718. , std::optional<uint64_t> // revision
  719. , std::optional<bool> // ssoExempt
  720. , std::optional<uint64_t> // authenticationExpiryTime
  721. , std::string // assignedAddresses
  722. > row;
  723. uint64_t count = 0;
  724. auto tmp = std::chrono::high_resolution_clock::now();
  725. uint64_t total = 0;
  726. while (stream >> row) {
  727. auto start = std::chrono::high_resolution_clock::now();
  728. json empty;
  729. json config;
  730. initMember(config);
  731. memberId = std::get<0>(row);
  732. networkId = std::get<1>(row);
  733. std::optional<bool> activeBridge = std::get<2>(row);
  734. std::optional<bool> authorized = std::get<3>(row);
  735. std::optional<std::string> capabilities = std::get<4>(row);
  736. std::optional<uint64_t> creationTime = std::get<5>(row);
  737. std::optional<std::string> identity = std::get<6>(row);
  738. std::optional<uint64_t> lastAuthorizedTime = std::get<7>(row);
  739. std::optional<uint64_t> lastDeauthorizedTime = std::get<8>(row);
  740. std::optional<int> remoteTraceLevel = std::get<9>(row);
  741. std::optional<std::string> remoteTraceTarget = std::get<10>(row);
  742. std::optional<std::string> tags = std::get<11>(row);
  743. std::optional<int> vMajor = std::get<12>(row);
  744. std::optional<int> vMinor = std::get<13>(row);
  745. std::optional<int> vRev = std::get<14>(row);
  746. std::optional<int> vProto = std::get<15>(row);
  747. std::optional<bool> noAutoAssignIps = std::get<16>(row);
  748. std::optional<uint64_t> revision = std::get<17>(row);
  749. std::optional<bool> ssoExempt = std::get<18>(row);
  750. std::optional<uint64_t> authenticationExpiryTime = std::get<19>(row);
  751. std::string assignedAddresses = std::get<20>(row);
  752. networkMembers.insert(std::pair<std::string, std::string>(setKeyBase+networkId, memberId));
  753. config["id"] = memberId;
  754. config["address"] = memberId;
  755. config["nwid"] = networkId;
  756. config["activeBridge"] = activeBridge.value_or(false);
  757. config["authorized"] = authorized.value_or(false);
  758. config["capabilities"] = json::parse(capabilities.value_or("[]"));
  759. config["creationTime"] = creationTime.value_or(0);
  760. config["identity"] = identity.value_or("");
  761. config["lastAuthorizedTime"] = lastAuthorizedTime.value_or(0);
  762. config["lastDeauthorizedTime"] = lastDeauthorizedTime.value_or(0);
  763. config["remoteTraceLevel"] = remoteTraceLevel.value_or(0);
  764. config["remoteTraceTarget"] = remoteTraceTarget.value_or("");
  765. config["tags"] = json::parse(tags.value_or("[]"));
  766. config["vMajor"] = vMajor.value_or(-1);
  767. config["vMinor"] = vMinor.value_or(-1);
  768. config["vRev"] = vRev.value_or(-1);
  769. config["vProto"] = vProto.value_or(-1);
  770. config["noAutoAssignIps"] = noAutoAssignIps.value_or(false);
  771. config["revision"] = revision.value_or(0);
  772. config["ssoExempt"] = ssoExempt.value_or(false);
  773. config["authenticationExpiryTime"] = authenticationExpiryTime.value_or(0);
  774. config["objtype"] = "member";
  775. config["ipAssignments"] = json::array();
  776. if (assignedAddresses != "{}") {
  777. std::string tmp = assignedAddresses.substr(1, assignedAddresses.size()-2);
  778. std::vector<std::string> addrs = split(tmp, ',');
  779. for (auto it = addrs.begin(); it != addrs.end(); ++it) {
  780. config["ipAssignments"].push_back(*it);
  781. }
  782. }
  783. Metrics::member_count++;
  784. _memberChanged(empty, config, false);
  785. memberId = "";
  786. networkId = "";
  787. auto end = std::chrono::high_resolution_clock::now();
  788. auto dur = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  789. total += dur.count();
  790. ++count;
  791. if (count > 0 && count % 10000 == 0) {
  792. fprintf(stderr, "Averaging %llu us per member\n", (total/count));
  793. }
  794. }
  795. if (count > 0) {
  796. fprintf(stderr, "Took %llu us per member to load\n", (total/count));
  797. }
  798. stream.complete();
  799. w.commit();
  800. _pool->unborrow(c2);
  801. _pool->unborrow(c);
  802. fprintf(stderr, "done.\n");
  803. if (!networkMembers.empty()) {
  804. if (_redisMemberStatus) {
  805. fprintf(stderr, "Load member data into redis...\n");
  806. if (_rc->clusterMode) {
  807. auto tx = _cluster->transaction(_myAddressStr, true, false);
  808. uint64_t count = 0;
  809. for (auto it : networkMembers) {
  810. tx.sadd(it.first, it.second);
  811. if (++count % 30000 == 0) {
  812. tx.exec();
  813. tx = _cluster->transaction(_myAddressStr, true, false);
  814. }
  815. }
  816. tx.exec();
  817. } else {
  818. auto tx = _redis->transaction(true, false);
  819. uint64_t count = 0;
  820. for (auto it : networkMembers) {
  821. tx.sadd(it.first, it.second);
  822. if (++count % 30000 == 0) {
  823. tx.exec();
  824. tx = _redis->transaction(true, false);
  825. }
  826. }
  827. tx.exec();
  828. }
  829. fprintf(stderr, "done.\n");
  830. }
  831. }
  832. fprintf(stderr, "Done loading members...\n");
  833. if (++this->_ready == 2) {
  834. if (_waitNoticePrinted) {
  835. fprintf(stderr,"[%s] NOTICE: %.10llx controller PostgreSQL data download complete." ZT_EOL_S,_timestr(),(unsigned long long)_myAddress.toInt());
  836. }
  837. _readyLock.unlock();
  838. }
  839. } catch (sw::redis::Error &e) {
  840. fprintf(stderr, "ERROR: Error initializing members (redis): %s\n", e.what());
  841. exit(-1);
  842. } catch (std::exception &e) {
  843. fprintf(stderr, "ERROR: Error initializing member: %s-%s %s\n", networkId.c_str(), memberId.c_str(), e.what());
  844. exit(-1);
  845. }
  846. }
  847. void CV1::heartbeat()
  848. {
  849. char publicId[1024];
  850. char hostnameTmp[1024];
  851. _myId.toString(false,publicId);
  852. if (gethostname(hostnameTmp, sizeof(hostnameTmp))!= 0) {
  853. hostnameTmp[0] = (char)0;
  854. } else {
  855. for (int i = 0; i < (int)sizeof(hostnameTmp); ++i) {
  856. if ((hostnameTmp[i] == '.')||(hostnameTmp[i] == 0)) {
  857. hostnameTmp[i] = (char)0;
  858. break;
  859. }
  860. }
  861. }
  862. const char *controllerId = _myAddressStr.c_str();
  863. const char *publicIdentity = publicId;
  864. const char *hostname = hostnameTmp;
  865. while (_run == 1) {
  866. // fprintf(stderr, "%s: heartbeat\n", controllerId);
  867. auto c = _pool->borrow();
  868. int64_t ts = OSUtils::now();
  869. if(c->c) {
  870. std::string major = std::to_string(ZEROTIER_ONE_VERSION_MAJOR);
  871. std::string minor = std::to_string(ZEROTIER_ONE_VERSION_MINOR);
  872. std::string rev = std::to_string(ZEROTIER_ONE_VERSION_REVISION);
  873. std::string build = std::to_string(ZEROTIER_ONE_VERSION_BUILD);
  874. std::string now = std::to_string(ts);
  875. std::string host_port = std::to_string(_listenPort);
  876. std::string use_redis = (_rc != NULL) ? "true" : "false";
  877. std::string redis_mem_status = (_redisMemberStatus) ? "true" : "false";
  878. try {
  879. pqxx::work w{*c->c};
  880. pqxx::result res =
  881. w.exec0("INSERT INTO ztc_controller (id, cluster_host, last_alive, public_identity, v_major, v_minor, v_rev, v_build, host_port, use_redis, redis_member_status) "
  882. "VALUES ("+w.quote(controllerId)+", "+w.quote(hostname)+", TO_TIMESTAMP("+now+"::double precision/1000), "+
  883. w.quote(publicIdentity)+", "+major+", "+minor+", "+rev+", "+build+", "+host_port+", "+use_redis+", "+redis_mem_status+") "
  884. "ON CONFLICT (id) DO UPDATE SET cluster_host = EXCLUDED.cluster_host, last_alive = EXCLUDED.last_alive, "
  885. "public_identity = EXCLUDED.public_identity, v_major = EXCLUDED.v_major, v_minor = EXCLUDED.v_minor, "
  886. "v_rev = EXCLUDED.v_rev, v_build = EXCLUDED.v_rev, host_port = EXCLUDED.host_port, "
  887. "use_redis = EXCLUDED.use_redis, redis_member_status = EXCLUDED.redis_member_status");
  888. w.commit();
  889. } catch (std::exception &e) {
  890. fprintf(stderr, "%s: Heartbeat update failed: %s\n", controllerId, e.what());
  891. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  892. continue;
  893. }
  894. }
  895. _pool->unborrow(c);
  896. try {
  897. if (_redisMemberStatus) {
  898. if (_rc->clusterMode) {
  899. _cluster->zadd("controllers", "controllerId", ts);
  900. } else {
  901. _redis->zadd("controllers", "controllerId", ts);
  902. }
  903. }
  904. } catch (sw::redis::Error &e) {
  905. fprintf(stderr, "ERROR: Redis error in heartbeat thread: %s\n", e.what());
  906. }
  907. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  908. }
  909. fprintf(stderr, "Exited heartbeat thread\n");
  910. }
  911. void CV1::membersDbWatcher()
  912. {
  913. if (_rc) {
  914. _membersWatcher_Redis();
  915. } else {
  916. _membersWatcher_Postgres();
  917. }
  918. if (_run == 1) {
  919. fprintf(stderr, "ERROR: %s membersDbWatcher should still be running! Exiting Controller.\n", _myAddressStr.c_str());
  920. exit(9);
  921. }
  922. fprintf(stderr, "Exited membersDbWatcher\n");
  923. }
  924. void CV1::_membersWatcher_Postgres() {
  925. auto c = _pool->borrow();
  926. std::string stream = "member_" + _myAddressStr;
  927. fprintf(stderr, "Listening to member stream: %s\n", stream.c_str());
  928. MemberNotificationReceiver m(this, *c->c, stream);
  929. while(_run == 1) {
  930. c->c->await_notification(5, 0);
  931. }
  932. _pool->unborrow(c);
  933. }
  934. void CV1::_membersWatcher_Redis() {
  935. char buf[11] = {0};
  936. std::string key = "member-stream:{" + std::string(_myAddress.toString(buf)) + "}";
  937. std::string lastID = "0";
  938. fprintf(stderr, "Listening to member stream: %s\n", key.c_str());
  939. while (_run == 1) {
  940. try {
  941. json tmp;
  942. std::unordered_map<std::string, ItemStream> result;
  943. if (_rc->clusterMode) {
  944. _cluster->xread(key, lastID, std::chrono::seconds(1), 0, std::inserter(result, result.end()));
  945. } else {
  946. _redis->xread(key, lastID, std::chrono::seconds(1), 0, std::inserter(result, result.end()));
  947. }
  948. if (!result.empty()) {
  949. for (auto element : result) {
  950. #ifdef REDIS_TRACE
  951. fprintf(stdout, "Received notification from: %s\n", element.first.c_str());
  952. #endif
  953. for (auto rec : element.second) {
  954. std::string id = rec.first;
  955. auto attrs = rec.second;
  956. #ifdef REDIS_TRACE
  957. fprintf(stdout, "Record ID: %s\n", id.c_str());
  958. fprintf(stdout, "attrs len: %lu\n", attrs.size());
  959. #endif
  960. for (auto a : attrs) {
  961. #ifdef REDIS_TRACE
  962. fprintf(stdout, "key: %s\nvalue: %s\n", a.first.c_str(), a.second.c_str());
  963. #endif
  964. try {
  965. tmp = json::parse(a.second);
  966. json &ov = tmp["old_val"];
  967. json &nv = tmp["new_val"];
  968. json oldConfig, newConfig;
  969. if (ov.is_object()) oldConfig = ov;
  970. if (nv.is_object()) newConfig = nv;
  971. if (oldConfig.is_object()||newConfig.is_object()) {
  972. _memberChanged(oldConfig,newConfig,(this->_ready >= 2));
  973. }
  974. } catch (...) {
  975. fprintf(stderr, "json parse error in _membersWatcher_Redis: %s\n", a.second.c_str());
  976. }
  977. }
  978. if (_rc->clusterMode) {
  979. _cluster->xdel(key, id);
  980. } else {
  981. _redis->xdel(key, id);
  982. }
  983. lastID = id;
  984. Metrics::redis_mem_notification++;
  985. }
  986. }
  987. }
  988. } catch (sw::redis::Error &e) {
  989. fprintf(stderr, "Error in Redis members watcher: %s\n", e.what());
  990. }
  991. }
  992. fprintf(stderr, "membersWatcher ended\n");
  993. }
  994. void CV1::networksDbWatcher()
  995. {
  996. if (_rc) {
  997. _networksWatcher_Redis();
  998. } else {
  999. _networksWatcher_Postgres();
  1000. }
  1001. if (_run == 1) {
  1002. fprintf(stderr, "ERROR: %s networksDbWatcher should still be running! Exiting Controller.\n", _myAddressStr.c_str());
  1003. exit(8);
  1004. }
  1005. fprintf(stderr, "Exited networksDbWatcher\n");
  1006. }
  1007. void CV1::_networksWatcher_Postgres() {
  1008. std::string stream = "network_" + _myAddressStr;
  1009. fprintf(stderr, "Listening to member stream: %s\n", stream.c_str());
  1010. auto c = _pool->borrow();
  1011. NetworkNotificationReceiver n(this, *c->c, stream);
  1012. while(_run == 1) {
  1013. c->c->await_notification(5,0);
  1014. }
  1015. }
  1016. void CV1::_networksWatcher_Redis() {
  1017. char buf[11] = {0};
  1018. std::string key = "network-stream:{" + std::string(_myAddress.toString(buf)) + "}";
  1019. std::string lastID = "0";
  1020. while (_run == 1) {
  1021. try {
  1022. json tmp;
  1023. std::unordered_map<std::string, ItemStream> result;
  1024. if (_rc->clusterMode) {
  1025. _cluster->xread(key, lastID, std::chrono::seconds(1), 0, std::inserter(result, result.end()));
  1026. } else {
  1027. _redis->xread(key, lastID, std::chrono::seconds(1), 0, std::inserter(result, result.end()));
  1028. }
  1029. if (!result.empty()) {
  1030. for (auto element : result) {
  1031. #ifdef REDIS_TRACE
  1032. fprintf(stdout, "Received notification from: %s\n", element.first.c_str());
  1033. #endif
  1034. for (auto rec : element.second) {
  1035. std::string id = rec.first;
  1036. auto attrs = rec.second;
  1037. #ifdef REDIS_TRACE
  1038. fprintf(stdout, "Record ID: %s\n", id.c_str());
  1039. fprintf(stdout, "attrs len: %lu\n", attrs.size());
  1040. #endif
  1041. for (auto a : attrs) {
  1042. #ifdef REDIS_TRACE
  1043. fprintf(stdout, "key: %s\nvalue: %s\n", a.first.c_str(), a.second.c_str());
  1044. #endif
  1045. try {
  1046. tmp = json::parse(a.second);
  1047. json &ov = tmp["old_val"];
  1048. json &nv = tmp["new_val"];
  1049. json oldConfig, newConfig;
  1050. if (ov.is_object()) oldConfig = ov;
  1051. if (nv.is_object()) newConfig = nv;
  1052. if (oldConfig.is_object()||newConfig.is_object()) {
  1053. _networkChanged(oldConfig,newConfig,(this->_ready >= 2));
  1054. }
  1055. } catch (std::exception &e) {
  1056. fprintf(stderr, "json parse error in networkWatcher_Redis: what: %s json: %s\n", e.what(), a.second.c_str());
  1057. }
  1058. }
  1059. if (_rc->clusterMode) {
  1060. _cluster->xdel(key, id);
  1061. } else {
  1062. _redis->xdel(key, id);
  1063. }
  1064. lastID = id;
  1065. }
  1066. Metrics::redis_net_notification++;
  1067. }
  1068. }
  1069. } catch (sw::redis::Error &e) {
  1070. fprintf(stderr, "Error in Redis networks watcher: %s\n", e.what());
  1071. }
  1072. }
  1073. fprintf(stderr, "networksWatcher ended\n");
  1074. }
  1075. void CV1::commitThread()
  1076. {
  1077. fprintf(stderr, "%s: commitThread start\n", _myAddressStr.c_str());
  1078. std::pair<nlohmann::json,bool> qitem;
  1079. while(_commitQueue.get(qitem)&(_run == 1)) {
  1080. //fprintf(stderr, "commitThread tick\n");
  1081. if (!qitem.first.is_object()) {
  1082. fprintf(stderr, "not an object\n");
  1083. continue;
  1084. }
  1085. std::shared_ptr<PostgresConnection> c;
  1086. try {
  1087. c = _pool->borrow();
  1088. } catch (std::exception &e) {
  1089. fprintf(stderr, "ERROR: %s\n", e.what());
  1090. continue;
  1091. }
  1092. if (!c) {
  1093. fprintf(stderr, "Error getting database connection\n");
  1094. continue;
  1095. }
  1096. Metrics::pgsql_commit_ticks++;
  1097. try {
  1098. nlohmann::json &config = (qitem.first);
  1099. const std::string objtype = config["objtype"];
  1100. if (objtype == "member") {
  1101. // fprintf(stderr, "%s: commitThread: member\n", _myAddressStr.c_str());
  1102. std::string memberId;
  1103. std::string networkId;
  1104. try {
  1105. pqxx::work w(*c->c);
  1106. memberId = config["id"];
  1107. networkId = config["nwid"];
  1108. std::string target = "NULL";
  1109. if (!config["remoteTraceTarget"].is_null()) {
  1110. target = config["remoteTraceTarget"];
  1111. }
  1112. pqxx::row nwrow = w.exec_params1("SELECT COUNT(id) FROM ztc_network WHERE id = $1", networkId);
  1113. int nwcount = nwrow[0].as<int>();
  1114. if (nwcount != 1) {
  1115. fprintf(stderr, "network %s does not exist. skipping member upsert\n", networkId.c_str());
  1116. w.abort();
  1117. _pool->unborrow(c);
  1118. continue;
  1119. }
  1120. pqxx::row mrow = w.exec_params1("SELECT COUNT(id) FROM ztc_member WHERE id = $1 AND network_id = $2", memberId, networkId);
  1121. int membercount = mrow[0].as<int>();
  1122. bool isNewMember = false;
  1123. if (membercount == 0) {
  1124. // new member
  1125. isNewMember = true;
  1126. pqxx::result res = w.exec_params0(
  1127. "INSERT INTO ztc_member (id, network_id, active_bridge, authorized, capabilities, "
  1128. "identity, last_authorized_time, last_deauthorized_time, no_auto_assign_ips, "
  1129. "remote_trace_level, remote_trace_target, revision, tags, v_major, v_minor, v_rev, v_proto) "
  1130. "VALUES ($1, $2, $3, $4, $5, $6, "
  1131. "TO_TIMESTAMP($7::double precision/1000), TO_TIMESTAMP($8::double precision/1000), "
  1132. "$9, $10, $11, $12, $13, $14, $15, $16, $17)",
  1133. memberId,
  1134. networkId,
  1135. (bool)config["activeBridge"],
  1136. (bool)config["authorized"],
  1137. OSUtils::jsonDump(config["capabilities"], -1),
  1138. OSUtils::jsonString(config["identity"], ""),
  1139. (uint64_t)config["lastAuthorizedTime"],
  1140. (uint64_t)config["lastDeauthorizedTime"],
  1141. (bool)config["noAutoAssignIps"],
  1142. (int)config["remoteTraceLevel"],
  1143. target,
  1144. (uint64_t)config["revision"],
  1145. OSUtils::jsonDump(config["tags"], -1),
  1146. (int)config["vMajor"],
  1147. (int)config["vMinor"],
  1148. (int)config["vRev"],
  1149. (int)config["vProto"]);
  1150. } else {
  1151. // existing member
  1152. pqxx::result res = w.exec_params0(
  1153. "UPDATE ztc_member "
  1154. "SET active_bridge = $3, authorized = $4, capabilities = $5, identity = $6, "
  1155. "last_authorized_time = TO_TIMESTAMP($7::double precision/1000), "
  1156. "last_deauthorized_time = TO_TIMESTAMP($8::double precision/1000), "
  1157. "no_auto_assign_ips = $9, remote_trace_level = $10, remote_trace_target= $11, "
  1158. "revision = $12, tags = $13, v_major = $14, v_minor = $15, v_rev = $16, v_proto = $17 "
  1159. "WHERE id = $1 AND network_id = $2",
  1160. memberId,
  1161. networkId,
  1162. (bool)config["activeBridge"],
  1163. (bool)config["authorized"],
  1164. OSUtils::jsonDump(config["capabilities"], -1),
  1165. OSUtils::jsonString(config["identity"], ""),
  1166. (uint64_t)config["lastAuthorizedTime"],
  1167. (uint64_t)config["lastDeauthorizedTime"],
  1168. (bool)config["noAutoAssignIps"],
  1169. (int)config["remoteTraceLevel"],
  1170. target,
  1171. (uint64_t)config["revision"],
  1172. OSUtils::jsonDump(config["tags"], -1),
  1173. (int)config["vMajor"],
  1174. (int)config["vMinor"],
  1175. (int)config["vRev"],
  1176. (int)config["vProto"]
  1177. );
  1178. }
  1179. if (!isNewMember) {
  1180. pqxx::result res = w.exec_params0("DELETE FROM ztc_member_ip_assignment WHERE member_id = $1 AND network_id = $2",
  1181. memberId, networkId);
  1182. }
  1183. std::vector<std::string> assignments;
  1184. bool ipAssignError = false;
  1185. for (auto i = config["ipAssignments"].begin(); i != config["ipAssignments"].end(); ++i) {
  1186. std::string addr = *i;
  1187. if (std::find(assignments.begin(), assignments.end(), addr) != assignments.end()) {
  1188. continue;
  1189. }
  1190. pqxx::result res = w.exec_params0(
  1191. "INSERT INTO ztc_member_ip_assignment (member_id, network_id, address) VALUES ($1, $2, $3) ON CONFLICT (network_id, member_id, address) DO NOTHING",
  1192. memberId, networkId, addr);
  1193. assignments.push_back(addr);
  1194. }
  1195. if (ipAssignError) {
  1196. fprintf(stderr, "%s: ipAssignError\n", _myAddressStr.c_str());
  1197. w.abort();
  1198. _pool->unborrow(c);
  1199. c.reset();
  1200. continue;
  1201. }
  1202. w.commit();
  1203. if (_smee != NULL && isNewMember) {
  1204. pqxx::row row = w.exec_params1(
  1205. "SELECT "
  1206. " count(h.hook_id) "
  1207. "FROM "
  1208. " ztc_hook h "
  1209. " INNER JOIN ztc_org o ON o.org_id = h.org_id "
  1210. " INNER JOIN ztc_network n ON n.owner_id = o.owner_id "
  1211. " WHERE "
  1212. "n.id = $1 ",
  1213. networkId
  1214. );
  1215. int64_t hookCount = row[0].as<int64_t>();
  1216. if (hookCount > 0) {
  1217. notifyNewMember(networkId, memberId);
  1218. }
  1219. }
  1220. const uint64_t nwidInt = OSUtils::jsonIntHex(config["nwid"], 0ULL);
  1221. const uint64_t memberidInt = OSUtils::jsonIntHex(config["id"], 0ULL);
  1222. if (nwidInt && memberidInt) {
  1223. nlohmann::json nwOrig;
  1224. nlohmann::json memOrig;
  1225. nlohmann::json memNew(config);
  1226. get(nwidInt, nwOrig, memberidInt, memOrig);
  1227. _memberChanged(memOrig, memNew, qitem.second);
  1228. } else {
  1229. fprintf(stderr, "%s: Can't notify of change. Error parsing nwid or memberid: %llu-%llu\n", _myAddressStr.c_str(), (unsigned long long)nwidInt, (unsigned long long)memberidInt);
  1230. }
  1231. } catch (std::exception &e) {
  1232. fprintf(stderr, "%s ERROR: Error updating member %s-%s: %s\n", _myAddressStr.c_str(), networkId.c_str(), memberId.c_str(), e.what());
  1233. }
  1234. } else if (objtype == "network") {
  1235. try {
  1236. // fprintf(stderr, "%s: commitThread: network\n", _myAddressStr.c_str());
  1237. pqxx::work w(*c->c);
  1238. std::string id = config["id"];
  1239. std::string remoteTraceTarget = "";
  1240. if(!config["remoteTraceTarget"].is_null()) {
  1241. remoteTraceTarget = config["remoteTraceTarget"];
  1242. }
  1243. std::string rulesSource = "";
  1244. if (config["rulesSource"].is_string()) {
  1245. rulesSource = config["rulesSource"];
  1246. }
  1247. // This ugly query exists because when we want to mirror networks to/from
  1248. // another data store (e.g. FileDB or LFDB) it is possible to get a network
  1249. // that doesn't exist in Central's database. This does an upsert and sets
  1250. // the owner_id to the "first" global admin in the user DB if the record
  1251. // did not previously exist. If the record already exists owner_id is left
  1252. // unchanged, so owner_id should be left out of the update clause.
  1253. pqxx::result res = w.exec_params0(
  1254. "INSERT INTO ztc_network (id, creation_time, owner_id, controller_id, capabilities, enable_broadcast, "
  1255. "last_modified, mtu, multicast_limit, name, private, "
  1256. "remote_trace_level, remote_trace_target, rules, rules_source, "
  1257. "tags, v4_assign_mode, v6_assign_mode, sso_enabled) VALUES ("
  1258. "$1, TO_TIMESTAMP($5::double precision/1000), "
  1259. "(SELECT user_id AS owner_id FROM ztc_global_permissions WHERE authorize = true AND del = true AND modify = true AND read = true LIMIT 1),"
  1260. "$2, $3, $4, TO_TIMESTAMP($5::double precision/1000), "
  1261. "$6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) "
  1262. "ON CONFLICT (id) DO UPDATE set controller_id = EXCLUDED.controller_id, "
  1263. "capabilities = EXCLUDED.capabilities, enable_broadcast = EXCLUDED.enable_broadcast, "
  1264. "last_modified = EXCLUDED.last_modified, mtu = EXCLUDED.mtu, "
  1265. "multicast_limit = EXCLUDED.multicast_limit, name = EXCLUDED.name, "
  1266. "private = EXCLUDED.private, remote_trace_level = EXCLUDED.remote_trace_level, "
  1267. "remote_trace_target = EXCLUDED.remote_trace_target, rules = EXCLUDED.rules, "
  1268. "rules_source = EXCLUDED.rules_source, tags = EXCLUDED.tags, "
  1269. "v4_assign_mode = EXCLUDED.v4_assign_mode, v6_assign_mode = EXCLUDED.v6_assign_mode, "
  1270. "sso_enabled = EXCLUDED.sso_enabled",
  1271. id,
  1272. _myAddressStr,
  1273. OSUtils::jsonDump(config["capabilities"], -1),
  1274. (bool)config["enableBroadcast"],
  1275. OSUtils::now(),
  1276. (int)config["mtu"],
  1277. (int)config["multicastLimit"],
  1278. OSUtils::jsonString(config["name"],""),
  1279. (bool)config["private"],
  1280. (int)config["remoteTraceLevel"],
  1281. remoteTraceTarget,
  1282. OSUtils::jsonDump(config["rules"], -1),
  1283. rulesSource,
  1284. OSUtils::jsonDump(config["tags"], -1),
  1285. OSUtils::jsonDump(config["v4AssignMode"],-1),
  1286. OSUtils::jsonDump(config["v6AssignMode"], -1),
  1287. OSUtils::jsonBool(config["ssoEnabled"], false));
  1288. res = w.exec_params0("DELETE FROM ztc_network_assignment_pool WHERE network_id = $1", 0);
  1289. auto pool = config["ipAssignmentPools"];
  1290. bool err = false;
  1291. for (auto i = pool.begin(); i != pool.end(); ++i) {
  1292. std::string start = (*i)["ipRangeStart"];
  1293. std::string end = (*i)["ipRangeEnd"];
  1294. res = w.exec_params0(
  1295. "INSERT INTO ztc_network_assignment_pool (network_id, ip_range_start, ip_range_end) "
  1296. "VALUES ($1, $2, $3)", id, start, end);
  1297. }
  1298. res = w.exec_params0("DELETE FROM ztc_network_route WHERE network_id = $1", id);
  1299. auto routes = config["routes"];
  1300. err = false;
  1301. for (auto i = routes.begin(); i != routes.end(); ++i) {
  1302. std::string t = (*i)["target"];
  1303. std::vector<std::string> target;
  1304. std::istringstream f(t);
  1305. std::string s;
  1306. while(std::getline(f, s, '/')) {
  1307. target.push_back(s);
  1308. }
  1309. if (target.empty() || target.size() != 2) {
  1310. continue;
  1311. }
  1312. std::string targetAddr = target[0];
  1313. std::string targetBits = target[1];
  1314. std::string via = "NULL";
  1315. if (!(*i)["via"].is_null()) {
  1316. via = (*i)["via"];
  1317. }
  1318. res = w.exec_params0("INSERT INTO ztc_network_route (network_id, address, bits, via) VALUES ($1, $2, $3, $4)",
  1319. id, targetAddr, targetBits, (via == "NULL" ? NULL : via.c_str()));
  1320. }
  1321. if (err) {
  1322. fprintf(stderr, "%s: route add error\n", _myAddressStr.c_str());
  1323. w.abort();
  1324. _pool->unborrow(c);
  1325. continue;
  1326. }
  1327. auto dns = config["dns"];
  1328. std::string domain = dns["domain"];
  1329. std::stringstream servers;
  1330. servers << "{";
  1331. for (auto j = dns["servers"].begin(); j < dns["servers"].end(); ++j) {
  1332. servers << *j;
  1333. if ( (j+1) != dns["servers"].end()) {
  1334. servers << ",";
  1335. }
  1336. }
  1337. servers << "}";
  1338. std::string s = servers.str();
  1339. res = w.exec_params0("INSERT INTO ztc_network_dns (network_id, domain, servers) VALUES ($1, $2, $3) ON CONFLICT (network_id) DO UPDATE SET domain = EXCLUDED.domain, servers = EXCLUDED.servers",
  1340. id, domain, s);
  1341. w.commit();
  1342. const uint64_t nwidInt = OSUtils::jsonIntHex(config["nwid"], 0ULL);
  1343. if (nwidInt) {
  1344. nlohmann::json nwOrig;
  1345. nlohmann::json nwNew(config);
  1346. get(nwidInt, nwOrig);
  1347. _networkChanged(nwOrig, nwNew, qitem.second);
  1348. } else {
  1349. fprintf(stderr, "%s: Can't notify network changed: %llu\n", _myAddressStr.c_str(), (unsigned long long)nwidInt);
  1350. }
  1351. } catch (std::exception &e) {
  1352. fprintf(stderr, "%s ERROR: Error updating network: %s\n", _myAddressStr.c_str(), e.what());
  1353. }
  1354. if (_redisMemberStatus) {
  1355. try {
  1356. std::string id = config["id"];
  1357. std::string controllerId = _myAddressStr.c_str();
  1358. std::string key = "networks:{" + controllerId + "}";
  1359. if (_rc->clusterMode) {
  1360. _cluster->sadd(key, id);
  1361. } else {
  1362. _redis->sadd(key, id);
  1363. }
  1364. } catch (sw::redis::Error &e) {
  1365. fprintf(stderr, "ERROR: Error adding network to Redis: %s\n", e.what());
  1366. }
  1367. }
  1368. } else if (objtype == "_delete_network") {
  1369. // fprintf(stderr, "%s: commitThread: delete network\n", _myAddressStr.c_str());
  1370. try {
  1371. pqxx::work w(*c->c);
  1372. std::string networkId = config["nwid"];
  1373. pqxx::result res = w.exec_params0("UPDATE ztc_network SET deleted = true WHERE id = $1",
  1374. networkId);
  1375. w.commit();
  1376. } catch (std::exception &e) {
  1377. fprintf(stderr, "%s ERROR: Error deleting network: %s\n", _myAddressStr.c_str(), e.what());
  1378. }
  1379. if (_redisMemberStatus) {
  1380. try {
  1381. std::string id = config["id"];
  1382. std::string controllerId = _myAddressStr.c_str();
  1383. std::string key = "networks:{" + controllerId + "}";
  1384. if (_rc->clusterMode) {
  1385. _cluster->srem(key, id);
  1386. _cluster->del("network-nodes-online:{"+controllerId+"}:"+id);
  1387. } else {
  1388. _redis->srem(key, id);
  1389. _redis->del("network-nodes-online:{"+controllerId+"}:"+id);
  1390. }
  1391. } catch (sw::redis::Error &e) {
  1392. fprintf(stderr, "ERROR: Error adding network to Redis: %s\n", e.what());
  1393. }
  1394. }
  1395. } else if (objtype == "_delete_member") {
  1396. // fprintf(stderr, "%s commitThread: delete member\n", _myAddressStr.c_str());
  1397. try {
  1398. pqxx::work w(*c->c);
  1399. std::string memberId = config["id"];
  1400. std::string networkId = config["nwid"];
  1401. pqxx::result res = w.exec_params0(
  1402. "UPDATE ztc_member SET hidden = true, deleted = true WHERE id = $1 AND network_id = $2",
  1403. memberId, networkId);
  1404. w.commit();
  1405. } catch (std::exception &e) {
  1406. fprintf(stderr, "%s ERROR: Error deleting member: %s\n", _myAddressStr.c_str(), e.what());
  1407. }
  1408. if (_redisMemberStatus) {
  1409. try {
  1410. std::string memberId = config["id"];
  1411. std::string networkId = config["nwid"];
  1412. std::string controllerId = _myAddressStr.c_str();
  1413. std::string key = "network-nodes-all:{" + controllerId + "}:" + networkId;
  1414. if (_rc->clusterMode) {
  1415. _cluster->srem(key, memberId);
  1416. _cluster->del("member:{"+controllerId+"}:"+networkId+":"+memberId);
  1417. } else {
  1418. _redis->srem(key, memberId);
  1419. _redis->del("member:{"+controllerId+"}:"+networkId+":"+memberId);
  1420. }
  1421. } catch (sw::redis::Error &e) {
  1422. fprintf(stderr, "ERROR: Error deleting member from Redis: %s\n", e.what());
  1423. }
  1424. }
  1425. } else {
  1426. fprintf(stderr, "%s ERROR: unknown objtype\n", _myAddressStr.c_str());
  1427. }
  1428. } catch (std::exception &e) {
  1429. fprintf(stderr, "%s ERROR: Error getting objtype: %s\n", _myAddressStr.c_str(), e.what());
  1430. }
  1431. _pool->unborrow(c);
  1432. c.reset();
  1433. }
  1434. fprintf(stderr, "%s commitThread finished\n", _myAddressStr.c_str());
  1435. }
  1436. void CV1::notifyNewMember(const std::string &networkID, const std::string &memberID) {
  1437. smeeclient::smee_client_notify_network_joined(
  1438. _smee,
  1439. networkID.c_str(),
  1440. memberID.c_str());
  1441. }
  1442. void CV1::onlineNotificationThread()
  1443. {
  1444. waitForReady();
  1445. if (_redisMemberStatus) {
  1446. onlineNotification_Redis();
  1447. } else {
  1448. onlineNotification_Postgres();
  1449. }
  1450. }
  1451. /**
  1452. * ONLY UNCOMMENT FOR TEMPORARY DB MAINTENANCE
  1453. *
  1454. * This define temporarily turns off writing to the member status table
  1455. * so it can be reindexed when the indexes get too large.
  1456. */
  1457. // #define DISABLE_MEMBER_STATUS 1
  1458. void CV1::onlineNotification_Postgres()
  1459. {
  1460. _connected = 1;
  1461. nlohmann::json jtmp1, jtmp2;
  1462. while (_run == 1) {
  1463. auto c = _pool->borrow();
  1464. auto c2 = _pool->borrow();
  1465. try {
  1466. fprintf(stderr, "%s onlineNotification_Postgres\n", _myAddressStr.c_str());
  1467. std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > lastOnline;
  1468. {
  1469. std::lock_guard<std::mutex> l(_lastOnline_l);
  1470. lastOnline.swap(_lastOnline);
  1471. }
  1472. #ifndef DISABLE_MEMBER_STATUS
  1473. pqxx::work w(*c->c);
  1474. pqxx::work w2(*c2->c);
  1475. fprintf(stderr, "online notification tick\n");
  1476. bool firstRun = true;
  1477. bool memberAdded = false;
  1478. int updateCount = 0;
  1479. pqxx::pipeline pipe(w);
  1480. for (auto i=lastOnline.begin(); i != lastOnline.end(); ++i) {
  1481. updateCount += 1;
  1482. uint64_t nwid_i = i->first.first;
  1483. char nwidTmp[64];
  1484. char memTmp[64];
  1485. char ipTmp[64];
  1486. OSUtils::ztsnprintf(nwidTmp,sizeof(nwidTmp), "%.16llx", nwid_i);
  1487. OSUtils::ztsnprintf(memTmp,sizeof(memTmp), "%.10llx", i->first.second);
  1488. if(!get(nwid_i, jtmp1, i->first.second, jtmp2)) {
  1489. continue; // skip non existent networks/members
  1490. }
  1491. std::string networkId(nwidTmp);
  1492. std::string memberId(memTmp);
  1493. try {
  1494. pqxx::row r = w2.exec_params1("SELECT id, network_id FROM ztc_member WHERE network_id = $1 AND id = $2",
  1495. networkId, memberId);
  1496. } catch (pqxx::unexpected_rows &e) {
  1497. continue;
  1498. }
  1499. int64_t ts = i->second.first;
  1500. std::string ipAddr = i->second.second.toIpString(ipTmp);
  1501. std::string timestamp = std::to_string(ts);
  1502. std::stringstream memberUpdate;
  1503. memberUpdate << "INSERT INTO ztc_member_status (network_id, member_id, address, last_updated) VALUES "
  1504. << "('" << networkId << "', '" << memberId << "', ";
  1505. if (ipAddr.empty()) {
  1506. memberUpdate << "NULL, ";
  1507. } else {
  1508. memberUpdate << "'" << ipAddr << "', ";
  1509. }
  1510. memberUpdate << "TO_TIMESTAMP(" << timestamp << "::double precision/1000)) "
  1511. << " ON CONFLICT (network_id, member_id) DO UPDATE SET address = EXCLUDED.address, last_updated = EXCLUDED.last_updated";
  1512. pipe.insert(memberUpdate.str());
  1513. Metrics::pgsql_node_checkin++;
  1514. }
  1515. while(!pipe.empty()) {
  1516. pipe.retrieve();
  1517. }
  1518. pipe.complete();
  1519. w.commit();
  1520. fprintf(stderr, "%s: Updated online status of %d members\n", _myAddressStr.c_str(), updateCount);
  1521. #endif
  1522. } catch (std::exception &e) {
  1523. fprintf(stderr, "%s: error in onlinenotification thread: %s\n", _myAddressStr.c_str(), e.what());
  1524. }
  1525. _pool->unborrow(c2);
  1526. _pool->unborrow(c);
  1527. ConnectionPoolStats stats = _pool->get_stats();
  1528. fprintf(stderr, "%s pool stats: in use size: %llu, available size: %llu, total: %llu\n",
  1529. _myAddressStr.c_str(), stats.borrowed_size, stats.pool_size, (stats.borrowed_size + stats.pool_size));
  1530. std::this_thread::sleep_for(std::chrono::seconds(10));
  1531. }
  1532. fprintf(stderr, "%s: Fell out of run loop in onlineNotificationThread\n", _myAddressStr.c_str());
  1533. if (_run == 1) {
  1534. fprintf(stderr, "ERROR: %s onlineNotificationThread should still be running! Exiting Controller.\n", _myAddressStr.c_str());
  1535. exit(6);
  1536. }
  1537. }
  1538. void CV1::onlineNotification_Redis()
  1539. {
  1540. _connected = 1;
  1541. char buf[11] = {0};
  1542. std::string controllerId = std::string(_myAddress.toString(buf));
  1543. while (_run == 1) {
  1544. fprintf(stderr, "onlineNotification tick\n");
  1545. auto start = std::chrono::high_resolution_clock::now();
  1546. uint64_t count = 0;
  1547. std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > lastOnline;
  1548. {
  1549. std::lock_guard<std::mutex> l(_lastOnline_l);
  1550. lastOnline.swap(_lastOnline);
  1551. }
  1552. try {
  1553. if (!lastOnline.empty()) {
  1554. if (_rc->clusterMode) {
  1555. auto tx = _cluster->transaction(controllerId, true, false);
  1556. count = _doRedisUpdate(tx, controllerId, lastOnline);
  1557. } else {
  1558. auto tx = _redis->transaction(true, false);
  1559. count = _doRedisUpdate(tx, controllerId, lastOnline);
  1560. }
  1561. }
  1562. } catch (sw::redis::Error &e) {
  1563. fprintf(stderr, "Error in online notification thread (redis): %s\n", e.what());
  1564. }
  1565. auto end = std::chrono::high_resolution_clock::now();
  1566. auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  1567. auto total = dur.count();
  1568. fprintf(stderr, "onlineNotification ran in %llu ms\n", total);
  1569. std::this_thread::sleep_for(std::chrono::seconds(5));
  1570. }
  1571. }
  1572. uint64_t CV1::_doRedisUpdate(sw::redis::Transaction &tx, std::string &controllerId,
  1573. std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > &lastOnline)
  1574. {
  1575. nlohmann::json jtmp1, jtmp2;
  1576. uint64_t count = 0;
  1577. for (auto i=lastOnline.begin(); i != lastOnline.end(); ++i) {
  1578. uint64_t nwid_i = i->first.first;
  1579. uint64_t memberid_i = i->first.second;
  1580. char nwidTmp[64];
  1581. char memTmp[64];
  1582. char ipTmp[64];
  1583. OSUtils::ztsnprintf(nwidTmp,sizeof(nwidTmp), "%.16llx", nwid_i);
  1584. OSUtils::ztsnprintf(memTmp,sizeof(memTmp), "%.10llx", memberid_i);
  1585. if (!get(nwid_i, jtmp1, memberid_i, jtmp2)){
  1586. continue; // skip non existent members/networks
  1587. }
  1588. std::string networkId(nwidTmp);
  1589. std::string memberId(memTmp);
  1590. int64_t ts = i->second.first;
  1591. std::string ipAddr = i->second.second.toIpString(ipTmp);
  1592. std::string timestamp = std::to_string(ts);
  1593. std::unordered_map<std::string, std::string> record = {
  1594. {"id", memberId},
  1595. {"address", ipAddr},
  1596. {"last_updated", std::to_string(ts)}
  1597. };
  1598. tx.zadd("nodes-online:{"+controllerId+"}", memberId, ts)
  1599. .zadd("nodes-online2:{"+controllerId+"}", networkId+"-"+memberId, ts)
  1600. .zadd("network-nodes-online:{"+controllerId+"}:"+networkId, memberId, ts)
  1601. .zadd("active-networks:{"+controllerId+"}", networkId, ts)
  1602. .sadd("network-nodes-all:{"+controllerId+"}:"+networkId, memberId)
  1603. .hmset("member:{"+controllerId+"}:"+networkId+":"+memberId, record.begin(), record.end());
  1604. ++count;
  1605. Metrics::redis_node_checkin++;
  1606. }
  1607. // expire records from all-nodes and network-nodes member list
  1608. uint64_t expireOld = OSUtils::now() - 300000;
  1609. tx.zremrangebyscore("nodes-online:{"+controllerId+"}",
  1610. sw::redis::RightBoundedInterval<double>(expireOld,
  1611. sw::redis::BoundType::LEFT_OPEN));
  1612. tx.zremrangebyscore("nodes-online2:{"+controllerId+"}",
  1613. sw::redis::RightBoundedInterval<double>(expireOld,
  1614. sw::redis::BoundType::LEFT_OPEN));
  1615. tx.zremrangebyscore("active-networks:{"+controllerId+"}",
  1616. sw::redis::RightBoundedInterval<double>(expireOld,
  1617. sw::redis::BoundType::LEFT_OPEN));
  1618. {
  1619. std::shared_lock<std::shared_mutex> l(_networks_l);
  1620. for (const auto &it : _networks) {
  1621. uint64_t nwid_i = it.first;
  1622. char nwidTmp[64];
  1623. OSUtils::ztsnprintf(nwidTmp,sizeof(nwidTmp), "%.16llx", nwid_i);
  1624. tx.zremrangebyscore("network-nodes-online:{"+controllerId+"}:"+nwidTmp,
  1625. sw::redis::RightBoundedInterval<double>(expireOld, sw::redis::BoundType::LEFT_OPEN));
  1626. }
  1627. }
  1628. tx.exec();
  1629. fprintf(stderr, "%s: Updated online status of %d members\n", _myAddressStr.c_str(), count);
  1630. return count;
  1631. }
  1632. #endif //ZT_CONTROLLER_USE_LIBPQ