-
Notifications
You must be signed in to change notification settings - Fork 1
/
seg6_mand.js
467 lines (414 loc) · 13.4 KB
/
seg6_mand.js
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
const commandLineArgs = require("command-line-args");
const commandLineUsage = require("command-line-usage");
const { execSync } = require("child_process");
const fs = require("fs");
const mysql = require("mysql");
const { Etcd3 } = require("etcd3");
const client = new Etcd3({
hosts: ["http://epe-manager.fujisawa.vsix.wide.ad.jp:2379"],
});
const crypto = require("crypto");
const optionDefinitions = [
{
name: "help",
alias: "h",
type: Boolean,
description: "show help",
},
{
name: "debug",
alias: "d",
type: Boolean,
description: "Debug Mode",
},
{
name: "epsilon",
alias: "e",
type: Number,
description: "epsilon for epsilon-greedy algorithm",
defaultValue: 0.6,
},
{
name: "sid",
alias: "s",
type: Number,
description: "default SID used before RTT data acquisition",
defaultValue: 100,
},
];
const sections = [
{
header: "seg6_mand.js",
content:
"obtain a route and update the FIB with the route with the smallest RTT",
},
{
header: "Options",
optionList: optionDefinitions,
},
];
const options = commandLineArgs(optionDefinitions);
if (options.help) {
const usage = commandLineUsage(sections);
console.log(usage);
process.exit(0);
}
if (options.debug) {
console.log("*******DEBUG MODE********");
}
// Variable Declaration
// epsilong greedy algorithm's parameter
if (options.debug) {
console.log(`epsilon: ${options.epsilon}`);
}
// PassiveRTTが何も取得できていない場合に用いるデフォルトSID
if (options.debug) {
console.log(`default SID: ${options.sid}`);
}
// read the last rtt_db id got from file
const lastId = parseInt(fs.readFileSync("lastId.txt", { encoding: "utf-8" }));
if (options.debug) {
console.log(`lastId: ${lastId}`);
}
// Current Routes
const stdout = execSync("ip -6 route show");
if (options.debug) {
console.log("*******CURRENT ROUTE********");
}
if (options.debug) {
console.log(`$ ip -6 route show: \n${stdout.toString()}`);
}
let mysqlConfig = {
connectionLimit: 20,
host: "localhost",
port: 13306,
user: "root",
password: "root",
database: "rtt_db",
timezone: "jst",
};
// Get new prefix/sid/rtt from rtt_db
// connect to MySQL
const connection = mysql.createConnection(mysqlConfig);
// get all rows with ID greater than lastID
let query = `SELECT * FROM \`rtt_db\`.\`prefix_sid_rtt\` WHERE id > ${lastId}`;
/*
connection.query(query, (error, results, fields) => {
if (error) throw error;
if (!results.length) {
console.log("THERE IS NO NEW PREFIX");
return;
} else {
results.forEach(function (result) {
if (result.sid === null) {
if (options.debug) {
console.log("NULL!!!");
}
// etcdからそのprefixの全てのsidを取得
// 取得したprefixの1つ目はweightedECMPの優先側として経路をreplace
// get sids corresponding to dst_prefix from etcd
(async () => {
// let test = await getSids(result.dst_prefix);
// let parsed = JSON.parse(test);
parsed.forEach(function (e) {
let command =
"sudo ip -6 nexthop replace id " +
eightHash(e.sid) +
" encap seg6 mode encap segs " +
e.sid +
" dev ens192 proto 200";
execSync(command);
});
// 最初のSIDのweightを7にしてNH-Groupを作成する
let groupContents = "";
for (i = 1; i < parsed.length; i++) {
groupContents += eightHash(parsed[i].sid);
groupContents += ",1/";
}
let command =
"sudo ip nexthop replace id " +
eightHash(result.dst_prefix) +
" group " +
eightHash(parsed[0].sid) +
",7/" +
groupContents.slice(0, -1) +
" proto 200";
execSync(command);
// result.dst_prefix宛の経路を作成したNH-Groupにreplaceする
command =
"sudo ip -6 route replace " +
result.dst_prefix +
" nhid " +
eightHash(result.dst_prefix) +
" proto 200";
})();
} else {
// rtt_dbから同じprefix/sidを持つrowの中からidが最大のものをそれぞれ取得
// 全てのprefix/sidの中でRTTが最小のsidを取得
if (options.debug) {
console.log("NOT NULL SID!!!");
}
// 該当するprefixのsid/rttをMySQLから取得して,各sidでidが一番大きいものを取得
query = createQueryMinRttRow(result.sid, result.dst_prefix);
(async () => {
// let test = await getSids(result.dst_prefix);
// let parsed = JSON.parse(test);
//parsed.forEach(function (e) {});
})();
}
});
console.log(
`=======Processed ${results[results.length - 1].id} Prefix========`
);
// write the current id to the file
fs.writeFileSync(
"lastId.txt",
results[results.length - 1].id.toString(),
"utf-8",
(err) => {
if (err) {
console.log(err);
}
}
);
}
});
*/
function createQueryMinRttRow(sid, dst_prefix) {
return (
`SELECT * FROM \`rtt_db\`.\`prefix_sid_rtt\` WHERE sid = "${sid}" AND dst_prefix = "${dst_prefix}" AND sid != "NULL" ORDER BY id DESC LIMIT 1;`
);
}
// 複数のprefix_sidをipコマンドで埋め込む関数
function addRoute(prefix, sids, preferSid) {
console.log(prefix);
console.log(sids);
console.log(preferSid);
}
// get sids[] using prefix
async function getSids(prefix) {
//const sids = await client.get("/prefixes/" + prefix.replace("/", "_")).string();
const sids = await client.get("/epe/sid-and-prefixes/" + prefix.replace("/", "_")).string();
return sids.split(",");
}
async function getNexthop() {
//const nexthopList = await client.getAll().prefix('/epe/nexthop-list').keys();
// 多分ここは,keys()で一覧を取得して,keyごとにvalueを取得するのが本来の使い方(間でkeyの枝刈りなどの処理ができるから)
const nexthopList = await client.getAll().prefix("/epe/nexthop-list");
Object.values(nexthopList).forEach(function (e) {
let row = JSON.parse(e);
if (row.active == true) {
let command =
"sudo ip -6 nexthop replace id " +
eightHash(row.sid) +
" encap seg6 mode encap segs " +
row.sid +
" dev ens192 proto 200";
execSync(command);
} else {
//console.log('ここにきたやつはwithdraw');
let command =
`sudo ip -6 nexthop delete id ${eightHash(row.sid)}`
execSync(command);
}
});
}
// Hash and convert to an 8-digit string numberes
function eightHash(str) {
const md5 = crypto.createHash("md5");
let temp = md5.update(str, "binary").digest("hex");
temp = String(parseInt(temp.replace(/[^0-9]/g, "").slice(-8)));
return temp;
}
async function registerPrefix(prefix) {
const to_json = {
active: true
}
const sids = await client.put("/epe/content-servers-prefixes/camp.vsix.wide.ad.jp/" + prefix.replace("/", "_")).value(JSON.stringify(to_json));
return;
}
function renewSidNexthopObj() {
if (options.debug) {
console.log("Interval: renewSidNextHopObj()");
}
(async () => {
await getNexthop();
})();
}
function updateDefault(){
let command = "sudo ip nexthop replace id 1000 group 1000"
}
function updateFib(prefix, sids, prefSid) {
(async () => {
let test = await getSids(prefixes);
let parsed = JSON.parse(test);
// 最初のSIDのweightを7にしてNH-Groupを作成する
let groupContents = "";
for (i = 1; i < sids.length; i++) {
groupContents += eightHash(parsed[i].sid);
groupContents += ",1/";
}
let command =
`sudo ip nexthop replace id ${eightHash(prefix)} group ${eightHash(prefSid)},7/${groupContents.slice(0, -1)} proto 200`;
execSync(command);
// prefix宛の経路を作成したNH-Groupにreplaceする
command =
`sudo ip -6 route replace ${prefix} nhid ${eightHash(prefix)} proto 200 expires 300`;
execSync(command);
})();
}
async function getMyPrefixes() {
const prefixes = await client.getAll().prefix('/epe/content-servers-prefixes/camp.vsix.wide.ad.jp').keys();
//const nexthopList = await client.getAll().prefix('/epe/nexthop-list').keys();
///epe/content-servers-prefixes/camp.vsix.wide.ad.jp/2001:db8:1234::_64
let list = [];
prefixes.forEach(function (e) {
list.push(e.split('/').slice(-1)[0].replace('_','/'));
});
return list;
}
function updateRoutes() {
if (options.debug) { console.log("updateRoutes()");
}
(async () => {
const prefixes = await getMyPrefixes();
prefixes.forEach(function (e) {
//const sids = await getSids(e.replace('/','_'));
//console.log(sids);
})
// getSids(prefix);
// console.log(await getNewestRTT('2001:db8:1234::/64', '2001:200:1111:ffff::2'));
//console.log(searchPrefSid('2001:db8:1234::/64', getSids('2001:db8:1234::/64')));
console.log(await searchPrefSid('2001:200::_32'));
//getCurrentRTT(prefix,sid)
//prefix/sidごと最短RTTのSIDを計算
// rtt_dbから同じprefix/sidを持つrowの中からidが最大のものをそれぞれ取得
// 全てのprefix/sidの中でRTTが最小のsidを取得
// 該当するprefixのsid/rttをMySQLから取得して,各sidでidが一番大きいものを取得
//let test = await //getSids(dst_prefix);
//let parsed = JSON.parse(test);
})();
// updateFib();
}
async function searchPrefSid(prefix){
let bestSid = 0;
let array = {};
let sids = await getSids(prefix);
//for await (i = 0; i < sids.length; i++) {
console.log(`sids: ${JSON.parse(sids)}`);
for await (sid of JSON.parse(sids)) {
//array[sids[i]] = await getNewestRTT(prefix, sids[i]);
let tekitou = []
//tekitou = await getNewestRTT(prefix, sid.sid);
tekitou = await getNewestRTT('2001:db8:1234::/64', '2001:200:1111:ffff::2')[0].sid;
//console.log(`tekitou: ${JSON.stringify(tekitou)}`); // 空のリスト
//console.log(`tekitou: ${JSON.parse(tekitou[0].sid)}`); // 空のリスト
console.log(array[sid]);
}
// bestSid = array[sids[0]];
return bestSid;
}
async function getNewestRTT(prefix, sid) {
let query = createQueryMinRttRow(sid, prefix);
const queryResult = await new Promise((resolve, reject) => {
connection.query(query, (error, results, fields) => {
if (error) {
reject(error);
return;
}
if (!results.length) {
console.log("THERE IS NO NEW PREFIX");
resolve(results);
return;
}
// console.log(results);
console.log(`=======Processed ${results[results.length - 1].id} Prefix========`);
// write the current id to the file
fs.writeFileSync(
"lastId.txt",
results[results.length - 1].id.toString(),
"utf-8",
(err) => {
if (err) {
console.log(err);
reject(err);
}
}
);
resolve(results);
});
});
return queryResult;
}
// prefixを引数にしてetcdから該当するprefix_sid_listを取得する関数
function pushUsedPrefix() {
if (options.debug) {
console.log("updateEtcd()");
}
// rtt_dbから使用されたPrefixの一覧を取得
(async () => {
let newPrefixes = await getNewPrefix();
console.log(newPrefixes);
newPrefixes.forEach(function (result) {
console.log(result.dst_prefix);
registerPrefix(result.dst_prefix);
});
})();
// etcdにregisterPrefix()
registerPrefix('2001:db8::/64');
// /epe/content-servers/camp.vsix.wide.ad.jp/2001:db8::_64
// “active”: “true”
}
// get all rows with ID greater than lastID
async function getNewPrefix() {
let queryNewer =
`SELECT * FROM \`rtt_db\`.\`prefix_sid_rtt\` WHERE id > ${lastId}`;
const queryResult = await new Promise((resolve, reject) => {
connection.query(queryNewer, (error, results, fields) => {
if (error) {
reject(error);
}
if (!results.length) {
console.log("THERE IS NO NEW PREFIX");
resolve(results);
return;
}
// console.log(results);
console.log(`=======Processed ${results[results.length - 1].id} Prefix========`);
// write the current id to the file
fs.writeFileSync(
"lastId.txt",
results[results.length - 1].id.toString(),
"utf-8",
(err) => {
if (err) {
console.log(err);
reject(err);
}
}
);
resolve(results);
});
});
return queryResult;
}
function main() {
// etcdから現在有効なSIDのリストを取得して,無効になったSIDがあったらそのNH-Objectを削除して,増えたNH-Objectがあったらそれを追加する
//setInterval(renewSidNexthopObj, 3000);
setInterval(renewSidNexthopObj, 1000); // skyline
// etcdからprefix全てを取得して,etcdからprefixごとに対応するsidのリストを取得して,rtt_dbからprefix/sidごとのRTTを取得してベストprefixを決めて,WeightedECMPの割合を7:1:1:1で入れてFIBに入れる
// setInterval(updateRoutes, 5000);
//setInterval(updateRoutes, 1000); // skyline
// rtt_dbから最新のprefix/sidを取得して,etcdPrefixes()
// setInterval(pushUsedPrefix, 10000);
setInterval(pushUsedPrefix, 1000); // skyline
}
main();
// show current routes
if (options.debug) {
let commandResult = execSync("ip -6 route show");
console.log(`$ ip -6 route show: \n${stdout.toString()}`);
}
if (options.debug) {
console.log("*******DEBUG MODE********");
}