Skip to content

Commit

Permalink
updateeeee
Browse files Browse the repository at this point in the history
  • Loading branch information
spolack committed Nov 8, 2024
1 parent ac47666 commit 4ec9f23
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 75 deletions.
3 changes: 2 additions & 1 deletion packages/bgpdisco/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ define Package/bgpdisco
$(Package/bgpdisco/default)
EXTRA_DEPENDS:= \
bird2, bird2c, \
ucode, ucode-mod-rtnl, \
ucode, ucode-mod-rtnl, ucode-mod-debug \
ucode-mod-uloop, ucode-mod-fs, ucode-mod-struct
endef

Expand Down Expand Up @@ -62,6 +62,7 @@ define Package/bgpdisco/install
$(INSTALL_DIR) $(1)/usr/share/ucode/bgpdisco
$(INSTALL_BIN) ./birdctl.uc $(1)/usr/share/ucode/bgpdisco/birdctl.uc
$(INSTALL_BIN) ./plugin.uc $(1)/usr/share/ucode/bgpdisco/plugin.uc
$(INSTALL_BIN) ./logger.uc $(1)/usr/share/ucode/bgpdisco/logger.uc
$(INSTALL_BIN) ./mrtdump.uc $(1)/usr/share/ucode/bgpdisco/mrtdump.uc
$(INSTALL_BIN) ./bird_config_template.ut $(1)/usr/share/ucode/bgpdisco/bird_config_template.ut

Expand Down
47 changes: 3 additions & 44 deletions packages/bgpdisco/bgpdisco.uc
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,12 @@ import * as fs from 'fs';
import * as mrtdump from 'bgpdisco.mrtdump';
import * as plugin from 'bgpdisco.plugin';
import * as birdctl from 'bgpdisco.birdctl';
import * as log from 'log';
import { DBG, INFO, WARN, ERR } from 'bgpdisco.logger';

let tty;
let bird;
let plugins;

function _log(facility, submodule, ...args) {
if (!submodule)
submodule = 'main';

let msg = sprintf(...args);

if (tty) {
let time = sprintf('%02d:%02d:%02d', t.hour, t.min, t.sec);
let fmt = '%s %s %s: %s';
printf(fmt, time, submodule, facility, msg);

} else {
let fmt = '%s %s: %s';
printf(fmt, submodule, facility, msg);
}
}

function DBG(...args) {
_log('Debug', null, ...args);
}

function INFO(...args) {
_log('Info', null, ...args);
}

function WARN(...args) {
_log('Warn', null, ...args);
}

function ERR(...args) {
_log('Error', null, ...args);
}



function render_bird_config() {
DBG('render_bird_config()');
let plugin_data = plugins.provide_data();
Expand Down Expand Up @@ -75,7 +40,7 @@ function retrieve_data_from_bird() {
fs.unlink(cfg.bird_mrt_file);

DBG('request new mrt dump');
call_bird_ctl('mrt dump table "*_nameservice" to "' + cfg.bird_mrt_file + '"');
bird.cmd('mrt dump table "*_nameservice" to "' + cfg.bird_mrt_file + '"');


DBG('parse mrt dump');
Expand Down Expand Up @@ -161,17 +126,11 @@ function cb_nl_newneigh(ev) {
DBG('new ip neighbor - triggering resync', ev);
}

function check_tty() {
let f = fs.fdopen(0, 'r');
tty = f.isatty();
}


check_tty();
INFO('start');

bird = birdctl.init();
plugins = plugin.init('/PLUGINDIR');
plugins = plugin.init('/usr/share/ucode/bgpdisco/plugins');


// Setup Sync Peers Timer
Expand Down
6 changes: 4 additions & 2 deletions packages/bgpdisco/birdctl.uc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const RE_BABEL_NEIGHBORS = regexp('(fe80\S*) (\S*)', 'g');

function cmd(cmd) {
let escaped_command = replace(cmd,'"','\\"');

let cmd_string = sprintf('%s -s %s %s', BIRDC_PATH, sock_bctl_path, cmd);
let p = popen(cmd_string);
let response = p.read('all');
Expand All @@ -32,6 +32,8 @@ function init(bird_ctl) {
}
return {
cmd: cmd,
get_babel_neighbors: get_babel_neighbors
get_babel_neighbors: get_babel_neighbors
};
}

export { init };
48 changes: 48 additions & 0 deletions packages/bgpdisco/logger.uc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {fdopen} from 'fs';
import {traceback} from 'debug';

let tty;

function _infos_from_stacktrace() {
let st = traceback(4)[0];
let _fn = split(st.filename, '/');
return {
filename: pop(_fn)
};
}

function _log(facility, ...args) {
let info = _infos_from_stacktrace();
let msg = sprintf(...args);

if (tty) {
let t = localtime();
let time_str = sprintf('%02d:%02d:%02d', t.hour, t.min, t.sec);
let fmt = '%s [%s] %s: %s\n';
printf(fmt, time_str, facility, info.filename, msg);

} else {
let fmt = '[%s] %s: %s';
printf(fmt, facility, info.filename, msg);
}
}

function DBG(...args) {
_log('Debug', ...args);
}

function INFO(...args) {
_log('Info', ...args);
}

function WARN(...args) {
_log('Warn', ...args);
}

function ERR(...args) {
_log('Error', ...args);
}

tty = fdopen(0, 'r').isatty();

export { DBG, INFO, WARN, ERR };
17 changes: 9 additions & 8 deletions packages/bgpdisco/mrtdump.uc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { pack, unpack } from 'struct';
import { open } from 'fs';
import { DBG, INFO, WARN, ERR } from 'bgpdisco.logger';

function get_routes(filename) {
let _routes = [];
Expand All @@ -9,19 +10,19 @@ function get_routes(filename) {
const RIB_ENTRY_SEQPFXL_LEN=5;
let seqpfxl = fd.read(RIB_ENTRY_SEQPFXL_LEN);
let _seqpfxl = unpack(RIB_ENTRY_SEQPFXL_FMT, seqpfxl);

let entry_seqno = _seqpfxl[0];
let entry_prefix_length = _seqpfxl[1];

const RIB_ENTRY_PREFIX_FMT = '>' + address_size + 'B';
let pfx = fd.read(address_size);
let entry_prefix = arrtoip(unpack(RIB_ENTRY_PREFIX_FMT, pfx));
print("- Seq ", entry_seqno, ":", entry_prefix, "/", entry_prefix_length, "\n");

let entrycnt= fd.read(2);
let entry_count = unpack('>H', entrycnt)[0];
print(" Entry Count: ", entry_count, "\n");

const RIB_ENTRY_VIA_HDR_FMT=">HIH";
for (let i=0; i<entry_count; i++) {
let rt = fd.read(8);
Expand All @@ -38,7 +39,7 @@ function get_routes(filename) {
timestamp: route_timestamp,
attributes: {}
};

while (route_attribute_len > 0) {
print('--Reading atribute', '\n');
let ft = unpack('>cB', fd.read(2)); // [flags, type]
Expand All @@ -54,7 +55,7 @@ function get_routes(filename) {
_field_attr_len = 2;
_field_attr_unpack_str = '>H';
}

let attr_len = unpack(_field_attr_unpack_str, fd.read(_field_attr_len))[0];
print("--Processing RtAttr type:", attr_type, ", length: ", attr_len, '\n');

Expand All @@ -64,7 +65,7 @@ function get_routes(filename) {
let attr_data_unpack_str = '>*';
attr_data = unpack(attr_data_unpack_str, _attributes)[0];
}

route_attribute_len -= 2 + _field_attr_len + attr_len;

__route_info.attributes[attr_type] = attr_data;
Expand All @@ -78,7 +79,7 @@ function get_routes(filename) {

function process_record(fd, timestamp, type, subtype, length) {
print("Process Record. Header: Timestamp: ", timestamp, "; Type: ", type, "; Subtype: ", subtype, "; Length: ", length, "\n");

const MRT_ENTRY_TYPE_TABLE_DUMP_V2 = 13;

const MRT_ENTRY_SUBTYPE_PEER_INDEX_TABLE = 1;
Expand All @@ -103,7 +104,7 @@ function get_routes(filename) {
address_size = 4; // IPv4
}


while (process_prefix(fd, address_size)) {
//print("Processing Prefix..");
}
Expand Down
24 changes: 12 additions & 12 deletions packages/bgpdisco/nameservice.uc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//export init;
import * as rtnl from 'rtnl';
import * as fs from 'fs';
import { DBG, INFO, WARN, ERR } from 'bgpdisco.logger';

const PLUGIN_UID = 0;
const DOMAIN = 'ff';
Expand All @@ -18,7 +19,7 @@ function get_interfaces_with_ip() {
// Scope filtering doesnt work via rtnl, idk
if (i.scope != rtnl.const.RT_SCOPE_UNIVERSE)
continue;

result[split(i.address, '/')[0]] = i.dev;
}
return result;
Expand All @@ -27,7 +28,7 @@ function get_interfaces_with_ip() {
function is_v4(ip) {
if (substr(':'))
return false;
return true;
return true;
}

function get_hostname() {
Expand Down Expand Up @@ -64,16 +65,16 @@ function get_local_hosts() {
lo_v4 = ip;
if (!lo_v6 && !is_v4)
lo_v6 = ip;
}
}

push(result[ip], name);
}
// Add records for the hostname

// Add records for the hostname
push(result[lo_v4 || first_v4], hostname);
push(result[lo_v6 || first_v6], hostname);
return result;

return result;
}


Expand All @@ -89,16 +90,15 @@ function write_hostnames(data) {
fp.write('\n' + '# Written by ffnameservice on ');
fp.write(join('-', [time.year, time.mon, time.mday]) + ' ');
fp.write(join(':', [time.hour, time.min, time.sec]));
fp.close();
fp.close();
}


function register(handler) {
handler.register(handler.DIRECTION.OUT, PLUGIN_UID, get_local_hosts);
handler.register(handler.DIRECTION.IN, PLUGIN_UID, write_hostnames);
function register(plug) {
plug.register(plug.TYPE.DATA_PROVIDER, PLUGIN_UID, get_local_hosts);
plug.register(plug.TYPE.DATA_HANDLER, PLUGIN_UID, write_hostnames);
}


return {
init: register
};
23 changes: 15 additions & 8 deletions packages/bgpdisco/plugin.uc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { lsdir } from 'fs';
import { DBG, INFO, WARN, ERR } from 'bgpdisco.logger';

const CB_TYPE = {
DATA_PROVIDER: 0,
Expand All @@ -11,6 +13,7 @@ let cache_handler_data = {};


function provide_data() {
DBG('Requesting data from all registered plugins');
let data = {};
for (let plugin in cb_providers) {
let id = plugin[0];
Expand All @@ -36,28 +39,32 @@ function provide_data() {

function handle_data(data) {
let leftover_ids = map(keys(data), (x) => int(x));
DBG('Handling incoming data for IDs: %s', leftover_ids);
for (let plugin in cb_handlers) {
let id = plugin[0];
let cb = plugin[1];
call(cb,null,null,data[id]);
leftover_ids = filter(leftover_ids, (i) => i != id); // remove id from list
}
//if (length(leftover_ids) > 0)
// debug('inform_plugins: Leftover ids which were not handled: ' + leftover_ids);
if (length(leftover_ids) > 0)
WARN('Received data without a registered handler: %s', leftover_ids);
}

export function init(plugin_dir) {
let cb_register = function (type, id, cb_handler) {
if (type == CB_TYPE.PROVIDER) {
push(cb_providers, [id, cb_handler]);
} else if (type == CB_TYPE.HANDLER) {
push(cb_handlers, [id, cb_handler]);
let cb_register = function (type, id, cb) {
if (type == CB_TYPE.DATA_PROVIDER) {
INFO('Registering plugin as provider for ID %d', id);
push(cb_providers, [id, cb]);
} else if (type == CB_TYPE.DATA_HANDLER) {
INFO('Registering plugin as handler for ID %d', id);
push(cb_handlers, [id, cb]);
}
};

for (plugin_file in fs.lsdir(plugin_dir)) {
for (let plugin_file in lsdir(plugin_dir)) {
let mod = loadfile(plugin_dir + '/' + plugin_file);
let mod_entry = mod();
INFO('Initializing plugin %s', plugin_file);
mod_entry.init({register: cb_register, TYPE: CB_TYPE});
}

Expand Down

0 comments on commit 4ec9f23

Please sign in to comment.