Commit 32d308e5 authored by yangjian's avatar yangjian

fix hard-coded -> config file

parent 4c1b7b0d
build/PLUGIN
......@@ -4,6 +4,8 @@ PLUGIN =
{
ALGORITHM = "RR";
TIMESTAMP_FILE = "/home/share/Unicom-NWDAF/2021-10-28/plugin_timestamp.txt";
INTERFACES:
{
# PLUGIN binded interface for Nudsf interface
......@@ -26,6 +28,15 @@ PLUGIN =
IPV4_ADDRESS = "192.168.2.35";#amf地址
PORT = 8282; # YOUR NETWORK CONFIG HERE
};
#AMF IPV4 ADDRESS POOL
AMF_INSTANCES_POOL = (
{AMF_INSTANCES_ID = 1; IPV4_ADDRESS = "192.168.2.35"; PORT = 8282; WEIGHT = 1; SELECTED = true},
{AMF_INSTANCES_ID = 2; IPV4_ADDRESS = "192.168.2.172"; PORT = 8282; WEIGHT = 2; SELECTED = false},
{AMF_INSTANCES_ID = 3; IPV4_ADDRESS = "192.168.2.173"; PORT = 8282; WEIGHT = 3; SELECTED = false},
{AMF_INSTANCES_ID = 4; IPV4_ADDRESS = "192.168.2.174"; PORT = 8282; WEIGHT = 4; SELECTED = false}
);
};
......
......@@ -28,7 +28,7 @@ long msg_len[4]={0};
long pro_cap[4] = {0};
bool isall[4] = {0};
extern int weight[15];
//extern int weight[15];
extern plugin_config plugin_cfg;
using json = nlohmann::json;
......@@ -74,30 +74,42 @@ void PluginApi::algorithm_omf_handler(const Pistache::Rest::Request &request, Pi
Logger::plugin_server().debug("ip address : %s",transdata.ip_addr.c_str());
Logger::plugin_server().debug("current message queue length : %d",transdata.mq_len);
Logger::plugin_server().debug("current capbility : %f",transdata.cap);
if(!transdata.ip_addr.compare("192.168.2.35")){
msg_len[0] = transdata.mq_len;
pro_cap[0] = (long)transdata.cap;
isall[0] = true;
Logger::plugin_server().debug("capability(0) %d",pro_cap[0]);
}
if(!transdata.ip_addr.compare("192.168.2.172")){
msg_len[1] = transdata.mq_len;
pro_cap[1] = (long)transdata.cap;
isall[1] = true;
Logger::plugin_server().debug("capability(1) %d",pro_cap[1]);
}
if(!transdata.ip_addr.compare("192.168.2.173")){
msg_len[2] = transdata.mq_len;
pro_cap[2] = (long)transdata.cap;
isall[2] = true;
Logger::plugin_server().debug("capability(2) %d",pro_cap[2]);
}
if(!transdata.ip_addr.compare("10.103.238.75")){
msg_len[3] = transdata.mq_len;
pro_cap[3] = (long)transdata.cap;
isall[3] = true;
Logger::plugin_server().debug("capability(3) %d",pro_cap[3]);
int count = (plugin_cfg.amf_pool.size() < 4)?plugin_cfg.amf_pool.size():4;
for(int i=0;i<count;i++)
{
if(!transdata.ip_addr.compare(plugin_cfg.amf_pool[i].ipv4)){
msg_len[i] = transdata.mq_len;
pro_cap[i] = (long)transdata.cap;
isall[i] = true;
Logger::plugin_server().debug("capability(%d) %d",i,pro_cap[i]);
}
}
// if(!transdata.ip_addr.compare("192.168.20.35")){
// msg_len[0] = transdata.mq_len;
// pro_cap[0] = (long)transdata.cap;
// isall[0] = true;
// Logger::plugin_server().debug("capability(0) %d",pro_cap[0]);
// }
// if(!transdata.ip_addr.compare("192.168.20.172")){
// msg_len[1] = transdata.mq_len;
// pro_cap[1] = (long)transdata.cap;
// isall[1] = true;
// Logger::plugin_server().debug("capability(1) %d",pro_cap[1]);
// }
// if(!transdata.ip_addr.compare("192.168.20.173")){
// msg_len[2] = transdata.mq_len;
// pro_cap[2] = (long)transdata.cap;
// isall[2] = true;
// Logger::plugin_server().debug("capability(2) %d",pro_cap[2]);
// }
// if(!transdata.ip_addr.compare("10.103.238.75")){
// msg_len[3] = transdata.mq_len;
// pro_cap[3] = (long)transdata.cap;
// isall[3] = true;
// Logger::plugin_server().debug("capability(3) %d",pro_cap[3]);
// }
bool update = true;
for(int i=0; i<4; i++){
if(!isall[i]){
......@@ -106,7 +118,7 @@ void PluginApi::algorithm_omf_handler(const Pistache::Rest::Request &request, Pi
break;
}
}
long N = 50;
long N = 50;
long K[4] = {0};
double sum_c = 0;
double sum = 0;
......@@ -159,7 +171,7 @@ void PluginApi::algorithm_omf_handler(const Pistache::Rest::Request &request, Pi
}
for(int j=0; j<4; j++){
Logger::plugin_server().debug("weight[%d] %f",j,tmp_w[j]/dwt);
weight[j] = std::floor(tmp_w[j]/dwt*N);
plugin_cfg.amf_pool[j].weight = std::floor(tmp_w[j]/dwt*N);
}
}
response.send(Pistache::Http::Code::Ok, "Updated \n");
......@@ -220,7 +232,7 @@ void PluginApi::algorithm_omf_handler(const Pistache::Rest::Request &request, Pi
//weight[1] = 50*(len_72+len_116+len_112)/(len_116+len_114+len_112+len_72);
//weight[2] = 50*(len_72+len_114+len_116)/(len_116+len_114+len_112+len_72);
//weight[3] = 50-weight[0]-weight[1]-weight[2];
Logger::plugin_server().debug("Updated weight, weight[0]=%d, weight[1]=%d, weight[2]=%d, weight[3]=%d", weight[0],weight[1],weight[2],weight[3]);
// Logger::plugin_server().debug("Updated weight, weight[0]=%d, weight[1]=%d, weight[2]=%d, weight[3]=%d", weight[0],weight[1],weight[2],weight[3]);
//this->n2_trans_to_gnb(assoc_id,stream,transdata,response);
} catch (nlohmann::detail::exception &e) {
//send a 400 error
......
......@@ -17,10 +17,10 @@ extern std::string amf_ip;
extern unsigned int amf_port;
extern std::map<std::string,long> ip2len;
ofstream timestamp_plugin("/home/share/Unicom-NWDAF/2021-10-28/plugin_timestamp.txt");
string amf_ip_list[15] = {"192.168.2.35","192.168.2.172","192.168.2.173","192.168.2.35","10.103.239.112","10.103.238.72","10.103.239.53","10.103.239.61","10.103.239.63","10.103.238.105","10.103.238.104","10.103.238.103","10.103.238.98","10.103.238.97","10.103.238.96"};
int amf_ports[15] = {8282,8282,8282,8282,8282,8282,8282,8282,8282,8282,8282,8282,8282,8282,8282};
int weight[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
ofstream timestamp_plugin(plugin_cfg.timestamp_file);
//string amf_ip_list[15] = {"192.168.20.35","192.168.20.172","192.168.20.173","192.168.2.35","10.103.239.112","10.103.238.72","10.103.239.53","10.103.239.61","10.103.239.63","10.103.238.105","10.103.238.104","10.103.238.103","10.103.238.98","10.103.238.97","10.103.238.96"};
//int amf_ports[15] = {8282,8282,8282,8282,8282,8282,8282,8282,8282,8282,8282,8282,8282,8282,8282};
//int weight[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
namespace amf_application{
plugin_app::plugin_app() {}
......@@ -47,7 +47,7 @@ static std::size_t callback(
int index = -1;
int random_index = 0;
int choose = 0;
int num_inst = 2;
int num_inst = 1;
void plugin_app::handle_receive(
bstring payload, sctp_assoc_id_t assoc_id, sctp_stream_id_t stream,
......@@ -73,6 +73,10 @@ void plugin_app::handle_receive(
// }
// Logger::plugin_sbi().debug("\"num_inst\" = %d",num_inst );
if(num_inst > plugin_cfg.amf_pool.size())
num_inst = plugin_cfg.amf_pool.size();
struct timeval tv1; struct timezone tz1; gettimeofday(&tv1,&tz1); long start = tv1.tv_sec*1000000 +tv1.tv_usec;
Logger::plugin_sbi().debug("Begining at time %ld", start);
timestamp_plugin<< "time_stamp "<<start<<endl;
......@@ -92,21 +96,21 @@ void plugin_app::handle_receive(
if(plugin_cfg.is_wrr || plugin_cfg.is_omf){
Logger::plugin_sbi().debug("Algorithm WRR or OMF");
Logger::plugin_sbi().debug("current weight weight[0]=%d, weight[0]=%d, weight[0]=%d, weight[0]=%d", weight[0],weight[1],weight[2],weight[3]);
// Logger::plugin_sbi().debug("current weight weight[0]=%d, weight[0]=%d, weight[0]=%d, weight[0]=%d", weight[0],weight[1],weight[2],weight[3]);
int sum = 0;
int index_num = 0;
for(int i=0; i < num_inst; i++)
index_num += weight[i];
index_num += plugin_cfg.amf_pool[i].weight;
for(int i=0; i <= choose; i++)
sum += weight[i];
sum += plugin_cfg.amf_pool[i].weight;
if((index + 1) > sum)
choose = (choose + 1) % num_inst;
if((index + 1) > index_num)
index = 0;
Logger::sctp().debug("Choosing %d th AMF instance ", choose+1);
url =
"http://" + std::string(amf_ip_list[choose].c_str()) +
":" + std::to_string(amf_ports[choose]) + "/namf-comm/v1/plugin_n2/" +
"http://" + std::string(plugin_cfg.amf_pool[choose].ipv4.c_str()) +
":" + std::to_string(plugin_cfg.amf_pool[choose].port) + "/namf-comm/v1/plugin_n2/" +
to_string (assoc_id ) + "/" + to_string (stream) ;
}
#if 0
......@@ -122,8 +126,8 @@ void plugin_app::handle_receive(
if(plugin_cfg.is_rr){
Logger::plugin_sbi().debug("Algorithm RR ");
url =
"http://" + std::string(amf_ip_list[index % num_inst].c_str()) +
":" + std::to_string(amf_ports[index % num_inst]) + "/namf-comm/v1/plugin_n2/" +
"http://" + std::string(plugin_cfg.amf_pool[index % num_inst].ipv4.c_str()) +
":" + std::to_string(plugin_cfg.amf_pool[index % num_inst].port) + "/namf-comm/v1/plugin_n2/" +
to_string (assoc_id ) + "/" + to_string (stream) ;
}
......
......@@ -23,6 +23,8 @@ plugin_config::plugin_config() {
is_rr = false;
is_omf = false;
rrRate = 0;
timestamp_file = "/var/run/plugin_timestamp.txt";
}
plugin_config::~plugin_config() {}
......@@ -61,6 +63,7 @@ int plugin_config::load(const std ::string &config_file) {
Logger::plugin_app().warn("%s : %s, using defaults", nfex.what(),
nfex.getPath());
}
try {
plugin_cfg.lookupValue("REQUESTRATE", rrRate);
} catch (const SettingNotFoundException &nfex) {
......@@ -68,6 +71,17 @@ int plugin_config::load(const std ::string &config_file) {
nfex.getPath());
}
try {
auto value = plugin_cfg.lookupValue("TIMESTAMP_FILE", timestamp_file);
if(!value)
{
Logger::plugin_app().warn("Timestamp file is not configured, using defaults( \"%s\" )", timestamp_file.c_str());
}
} catch (const SettingNotFoundException &nfex) {
Logger::plugin_app().error("%s : %s", nfex.what(),nfex.getPath());
}
try {
const Setting &new_if_cfg = plugin_cfg[PLUGIN_CONFIG_STRING_INTERFACES];
const Setting& n2_cfg =new_if_cfg[PLUGIN_CONFIG_STRING_INTERFACE_NGAP];
......@@ -86,6 +100,33 @@ int plugin_config::load(const std ::string &config_file) {
return -1;
}
try {
const Setting& new_if_cfg = plugin_cfg[PLUGIN_CONFIG_STRING_INTERFACES];
const Setting& amf_addr_pool = new_if_cfg[PLUGIN_CONFIG_STRING_AMF_INSTANCES_POOL];
int count = amf_addr_pool.getLength();
for (int i = 0; i < count; i++) {
const Setting& amf_addr_item = amf_addr_pool[i];
amf_inst_t amf_inst;
amf_addr_item.lookupValue(PLUGIN_CONFIG_STRING_AMF_INSTANCES_SELECTED, amf_inst.selected);
amf_addr_item.lookupValue(PLUGIN_CONFIG_STRING_AMF_INSTANCES_ID, amf_inst.id);
amf_addr_item.lookupValue(PLUGIN_CONFIG_STRING_AMF_INSTANCES_IPV4_ADDRESS, amf_inst.ipv4);
amf_addr_item.lookupValue(PLUGIN_CONFIG_STRING_AMF_INSTANCES_PORT, amf_inst.port);
amf_addr_item.lookupValue(PLUGIN_CONFIG_STRING_AMF_INSTANCES_WEIGHT, amf_inst.weight);
amf_pool_src.push_back(amf_inst);
if (amf_inst.selected)
{
amf_pool.push_back(amf_inst);
}
}
} catch (const SettingNotFoundException& nfex) {
Logger::plugin_app().error(
"%s : %s", nfex.what(), nfex.getPath());
return -1;
}
return 0;
}
int plugin_config::load_interface(const libconfig::Setting &if_cfg,
......@@ -130,6 +171,15 @@ void plugin_config::display() {
"====================== plugin =====================");
Logger::config().info("Configuration plugin:");
Logger::config().info("");
if(is_wrr)
Logger::config().info(" algorithm WRR ........: true");
if(is_rr)
Logger::config().info(" algorithm RR .........: true");
if(is_omf)
Logger::config().info(" algorithm OMF ........: true");
Logger::config().info(" timestamp file .......: %s", timestamp_file.c_str());
Logger::config().info("- N2 Networking:");
Logger::config().info(" iface ................: %s", n2.if_name.c_str());
Logger::config().info(" ip ...................: %s", n2.addr4.c_str());
......@@ -147,13 +197,16 @@ void plugin_config::display() {
Logger::config().info(" ip ...................: %s", nrf.addr4.c_str());
Logger::config().info(" port .................: %d", nrf.port);
Logger::config().info("");
if(is_wrr)
Logger::config().info(" algorithm WRR.......: true");
if(is_rr)
Logger::config().info(" algorithm RR........: true");
if(is_omf)
Logger::config().info(" algorithm OMF.......: true");
Logger::config().info("- Remote AMF Pool ........: ");
for (int i = 0; i < amf_pool_src.size(); i++) {
std::string selected;
if (amf_pool_src[i].selected)
selected = "true";
else
selected = "false";
Logger::config().info(" AMF_INSTANCE_ID %d (%s:%d, weight %d) is selected: %s",
amf_pool_src[i].id, amf_pool_src[i].ipv4.c_str(), amf_pool_src[i].port, amf_pool_src[i].weight, selected.c_str());
}
}
} // namespace config
......@@ -3,6 +3,7 @@
#include <libconfig.h++>
#include <string>
#include <vector>
#define PLUGIN_CONFIG_STRING_PLUGIN_CONFIG "PLUGIN"
......@@ -14,10 +15,23 @@
#define PLUGIN_CONFIG_STRING_IPV4_ADDRESS "IPV4_ADDRESS"
#define PLUGIN_CONFIG_STRING_PORT "PORT"
#define PLUGIN_CONFIG_STRING_AMF_INSTANCES_POOL "AMF_INSTANCES_POOL"
#define PLUGIN_CONFIG_STRING_AMF_INSTANCES_ID "AMF_INSTANCES_ID"
#define PLUGIN_CONFIG_STRING_AMF_INSTANCES_IPV4_ADDRESS "IPV4_ADDRESS"
#define PLUGIN_CONFIG_STRING_AMF_INSTANCES_PORT "PORT"
#define PLUGIN_CONFIG_STRING_AMF_INSTANCES_WEIGHT "WEIGHT"
#define PLUGIN_CONFIG_STRING_AMF_INSTANCES_SELECTED "SELECTED"
using namespace libconfig;
namespace config {
typedef struct {
int id;
std::string ipv4;
unsigned int port;
unsigned int weight;
bool selected;
} amf_inst_t;
typedef struct interface_cfg_s {
......@@ -45,6 +59,11 @@ public:
bool is_rr;
bool is_omf;
int rrRate;
std::string timestamp_file;
std::vector<amf_inst_t> amf_pool;
std::vector<amf_inst_t> amf_pool_src;
};
} // namespace config
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment