From c11085f616b59e2ed54431c73c42908ab3d4ad84 Mon Sep 17 00:00:00 2001 From: Robin Andreasen Date: Tue, 12 Jul 2022 13:26:40 -0400 Subject: [PATCH 01/19] Version 1 Random policy implementation --- examples/load_balancer/load_balancer.c | 27 ++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index bf6948b7b..3921f2e10 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -244,9 +244,10 @@ parse_app_args(int argc, char *argv[], const char *progname) { */ static int parse_backend_config(void) { - int ret, temp, i; + int ret, temp, i, weight; char ip[32]; char mac[32]; + char policy[32]; FILE *cfg; cfg = fopen(lb->cfg_filename, "r"); @@ -265,8 +266,12 @@ parse_backend_config(void) { rte_exit(EXIT_FAILURE, "Malloc failed, can't allocate server information\n"); } + ret = fscanf(cfg, "%s", policy); + lb->policy = strdup(policy); + for (i = 0; i < lb->server_count; i++) { - ret = fscanf(cfg, "%s %s", ip, mac); + ret = fscanf(cfg, "%s %s %d", ip, mac, &weight); + if (strcmp(policy, "weighted")) weight = 1; if (ret != 2) { rte_exit(EXIT_FAILURE, "Invalid backend config structure\n"); } @@ -459,11 +464,21 @@ table_add_entry(struct onvm_ft_ipv4_5tuple *key, struct flow_info **flow) { return -1; } - lb->num_stored++; - data->dest = lb->num_stored % lb->server_count; - data->last_pkt_cycles = lb->elapsed_cycles; - data->is_active = 0; + if (!strcmp(lb->policy,"random")) { + /* Intializes random number generator */ + srand((unsigned) time(&t)); + lb->num_stored++; + data->dest = rand() % lb->server_count; + data->last_pkt_cycles = lb->elapsed_cycles; + data->is_active = 0; + } + else if (!strcmp(lb->policy,"rrobin")) { + lb->num_stored++; + data->dest = lb->num_stored % lb->server_count; + data->last_pkt_cycles = lb->elapsed_cycles; + data->is_active = 0; + } *flow = data; return 0; From 3fcccd1d4216af1b17f43071ce68001c9b3e6898 Mon Sep 17 00:00:00 2001 From: andreaseno <89749710+andreaseno@users.noreply.github.com> Date: Wed, 13 Jul 2022 12:16:09 -0400 Subject: [PATCH 02/19] Update load_balancer.c --- examples/load_balancer/load_balancer.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index 3921f2e10..ca11bf1f1 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -95,6 +95,9 @@ struct loadbalance { /* config file */ char *cfg_filename; + + /* LB policy */ + char *policy; }; /* Struct for backend servers */ From 26a559b08e8ef2d5c3161dccd444ed63bc479d62 Mon Sep 17 00:00:00 2001 From: andreaseno <89749710+andreaseno@users.noreply.github.com> Date: Wed, 13 Jul 2022 12:41:25 -0400 Subject: [PATCH 03/19] Update load_balancer.c --- examples/load_balancer/load_balancer.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index ca11bf1f1..3a4492ffc 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -468,6 +468,8 @@ table_add_entry(struct onvm_ft_ipv4_5tuple *key, struct flow_info **flow) { } if (!strcmp(lb->policy,"random")) { + time_t t; + /* Intializes random number generator */ srand((unsigned) time(&t)); From 5bb21abf8fc09dec8dacf3a774c30ff1c8a2b9a4 Mon Sep 17 00:00:00 2001 From: andreaseno <89749710+andreaseno@users.noreply.github.com> Date: Wed, 13 Jul 2022 13:14:59 -0400 Subject: [PATCH 04/19] Update load_balancer.c --- examples/load_balancer/load_balancer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index 3a4492ffc..01f1a78d4 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -275,7 +275,7 @@ parse_backend_config(void) { for (i = 0; i < lb->server_count; i++) { ret = fscanf(cfg, "%s %s %d", ip, mac, &weight); if (strcmp(policy, "weighted")) weight = 1; - if (ret != 2) { + if (ret != 3) { rte_exit(EXIT_FAILURE, "Invalid backend config structure\n"); } From f64193a679b44db2259f76e0f85c94e2f360f890 Mon Sep 17 00:00:00 2001 From: jiyan Date: Thu, 14 Jul 2022 07:58:51 -0500 Subject: [PATCH 05/19] server configuration --- examples/load_balancer/server.conf | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/load_balancer/server.conf b/examples/load_balancer/server.conf index 60956afac..b77f9d5d2 100644 --- a/examples/load_balancer/server.conf +++ b/examples/load_balancer/server.conf @@ -1,3 +1,4 @@ LIST_SIZE 2 -10.10.2.1 3c:fd:fe:b4:fa:4c -10.10.2.3 3c:fd:fe:b0:f1:74 +random +10.10.1.2 90:e2:ba:ac:16:34 1 +10.10.1.3 90:e2:ba:b3:bb:7d 1 From 82a2749798c0915bce520c7680a8f77a66cafd02 Mon Sep 17 00:00:00 2001 From: Jiyan <54191383+xwedea@users.noreply.github.com> Date: Thu, 14 Jul 2022 09:13:02 -0400 Subject: [PATCH 06/19] small fix --- examples/load_balancer/load_balancer.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index 01f1a78d4..0ea40c0ec 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -467,23 +467,19 @@ table_add_entry(struct onvm_ft_ipv4_5tuple *key, struct flow_info **flow) { return -1; } + lb->num_stored++; if (!strcmp(lb->policy,"random")) { time_t t; - /* Intializes random number generator */ srand((unsigned) time(&t)); - - lb->num_stored++; data->dest = rand() % lb->server_count; - data->last_pkt_cycles = lb->elapsed_cycles; - data->is_active = 0; } else if (!strcmp(lb->policy,"rrobin")) { - lb->num_stored++; data->dest = lb->num_stored % lb->server_count; - data->last_pkt_cycles = lb->elapsed_cycles; - data->is_active = 0; } + + data->last_pkt_cycles = lb->elapsed_cycles; + data->is_active = 0; *flow = data; return 0; From 647d84417d85ebf0f90d01cfd9e0cb2a81b0ac1f Mon Sep 17 00:00:00 2001 From: Jiyan <54191383+xwedea@users.noreply.github.com> Date: Thu, 14 Jul 2022 09:19:53 -0400 Subject: [PATCH 07/19] weighted random policy (hardcoded) --- examples/load_balancer/load_balancer.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index 0ea40c0ec..6ea5d9a5a 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -274,7 +274,7 @@ parse_backend_config(void) { for (i = 0; i < lb->server_count; i++) { ret = fscanf(cfg, "%s %s %d", ip, mac, &weight); - if (strcmp(policy, "weighted")) weight = 1; + if (strcmp(policy, "weighted_random")) weight = 1; if (ret != 3) { rte_exit(EXIT_FAILURE, "Invalid backend config structure\n"); } @@ -477,6 +477,15 @@ table_add_entry(struct onvm_ft_ipv4_5tuple *key, struct flow_info **flow) { else if (!strcmp(lb->policy,"rrobin")) { data->dest = lb->num_stored % lb->server_count; } + else if (!strcmp(lb->policy,"weighted_random")) { + uint8_t w_mod = lb->num_stored % (lb->server_count + 6); + if (w_mod) { + data->dest = 1; + } else { + data->dest = 0; + } + + } data->last_pkt_cycles = lb->elapsed_cycles; data->is_active = 0; From 2d1aff873a691f4e96c48e9685a3357d697f7394 Mon Sep 17 00:00:00 2001 From: Robin Andreasen Date: Wed, 20 Jul 2022 16:50:35 -0400 Subject: [PATCH 08/19] Weighted random V1 implementation --- examples/load_balancer/load_balancer.c | 39 ++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index 6ea5d9a5a..84d5fce98 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -98,6 +98,10 @@ struct loadbalance { /* LB policy */ char *policy; + + /* structures to store server weights */ + int *weights; + int total_weight; }; /* Struct for backend servers */ @@ -263,6 +267,8 @@ parse_backend_config(void) { } lb->server_count = temp; + lb->weights = (int*)calloc(temp,sizeof(int)); + lb->server = (struct backend_server *)rte_malloc("backend server info", sizeof(struct backend_server) * lb->server_count, 0); if (lb->server == NULL) { @@ -272,6 +278,10 @@ parse_backend_config(void) { ret = fscanf(cfg, "%s", policy); lb->policy = strdup(policy); + if (!((!strcmp(lb->policy,"random")) || (!strcmp(lb->policy,"rrobin")) || (!strcmp(lb->policy,"weighted_random")))) { + rte_exit(EXIT_FAILURE, "Invalid policy. Check server.conf\n"); + } + for (i = 0; i < lb->server_count; i++) { ret = fscanf(cfg, "%s %s %d", ip, mac, &weight); if (strcmp(policy, "weighted_random")) weight = 1; @@ -288,6 +298,9 @@ parse_backend_config(void) { if (ret < 0) { rte_exit(EXIT_FAILURE, "Error parsing config MAC address #%d\n", i); } + + lb->weights[i] = weight; + lb->total_weight += weight; } fclose(cfg); @@ -478,15 +491,29 @@ table_add_entry(struct onvm_ft_ipv4_5tuple *key, struct flow_info **flow) { data->dest = lb->num_stored % lb->server_count; } else if (!strcmp(lb->policy,"weighted_random")) { - uint8_t w_mod = lb->num_stored % (lb->server_count + 6); - if (w_mod) { - data->dest = 1; - } else { - data->dest = 0; + // uint8_t w_mod = lb->num_stored % (lb->server_count + 6); + // if (w_mod) { + // data->dest = 1; + // } else { + // data->dest = 0; + // } + time_t t; + int i, wrand, cur_weight_sum; + /* Intializes random number generator */ + srand((unsigned) time(&t)); + wrand = rand() % lb->total_weight; + cur_weight_sum=0; + for (i = 0; i < lb->server_count; i++) { + cur_weight_sum+=lb->weights[i]; + if(wrand<=cur_weight_sum) { + data->dest=i; + break; + } } - + } + data->last_pkt_cycles = lb->elapsed_cycles; data->is_active = 0; *flow = data; From 2ea6147766b0b07533d68f2586d67a1c2052649e Mon Sep 17 00:00:00 2001 From: Robin Andreasen Date: Tue, 26 Jul 2022 10:25:43 -0400 Subject: [PATCH 09/19] adding json config file support (old config still supported) --- examples/load_balancer/load_balancer.c | 160 +++++++++++++++++++++++-- examples/load_balancer/server.json | 17 +++ 2 files changed, 170 insertions(+), 7 deletions(-) create mode 100644 examples/load_balancer/server.json diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index 84d5fce98..51b49547f 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -69,6 +69,10 @@ #include "onvm_flow_table.h" #include "onvm_nflib.h" #include "onvm_pkt_helper.h" +#include "onvm_config_common.h" + + +#include "cJSON.h" #define NF_TAG "load_balancer" #define TABLE_SIZE 65536 @@ -317,6 +321,154 @@ parse_backend_config(void) { return ret; } + +/* + * This function parses the backend config. It takes the filename + * and fills up the backend_server array. This includes the mac and ip + * address of the backend servers as well as their weights + */ +static int +parse_backend_json_config(void) { + int ret, i, num_objects; + // char ip[32]; + // char mac[32]; + // char policy[32]; + //FILE *cfg; + i = 0; + + cJSON *config_json = onvm_config_parse_file(lb->cfg_filename); + cJSON *list_size = NULL; + cJSON *policy = NULL; + cJSON *ip_addr = NULL; + cJSON *mac_addr = NULL; + cJSON *weight = NULL; + + if (config_json == NULL) { + rte_exit(EXIT_FAILURE, "%s file could not be parsed/not found. Assure config file" + " the directory to the config file is being specified.\n", lb->cfg_filename); + } + + num_object = onvm_config_get_item_count(config_json); + config_json = config_json -> child; + + list_size = cJSON_GetObjectItem(config_json, "LIST_SIZE"); + policy = cJSON_GetObjectItem(config_json, "policy"); + + if (list_size == NULL) rte_exit(EXIT_FAILURE, "LIST_SIZE not found/invalid\n"); + if (policy == NULL) rte_exit(EXIT_FAILURE, "Policy not found/invalid\n"); + + lb->server_count = list_size->valueint; + lb->policy = strdup(policy->valuestring); + + if (!((!strcmp(lb->policy,"random")) || (!strcmp(lb->policy,"rrobin")) || (!strcmp(lb->policy,"weighted_random")))) { + rte_exit(EXIT_FAILURE, "Invalid policy. Check server.conf\n"); + } + + + lb->weights = (int*)calloc(lb->server_count,sizeof(int)); + + lb->server = (struct backend_server *)rte_malloc("backend server info", + sizeof(struct backend_server) * lb->server_count, 0); + if (lb->server == NULL) { + rte_exit(EXIT_FAILURE, "Malloc failed, can't allocate server information\n"); + } + + config_json = config_json->next; + + + while (config_json != NULL) { + ip_addr = cJSON_GetObjectItem(config_json, "ip"); + mac_addr = cJSON_GetObjectItem(config_json, "mac_addr"); + weight = cJSON_GetObjectItem(config_json, "weight"); + + if (ip_addr == NULL) rte_exit(EXIT_FAILURE, "IP not found/invalid\n"); + if (mac_addr == NULL) rte_exit(EXIT_FAILURE, "MAC address not found/invalid\n"); + if (weight == NULL) rte_exit(EXIT_FAILURE, "Weight not found/invalid\n"); + + ret = onvm_pkt_parse_ip(ip_addr->valuestring, &lb->server[i].d_ip); + if (ret < 0) { + rte_exit(EXIT_FAILURE, "Error parsing config IP address #%d\n", i); + } + ret = onvm_pkt_parse_mac(mac_addr->valuestring, lb->server[i].d_addr_bytes); + if (ret < 0) { + rte_exit(EXIT_FAILURE, "Error parsing config MAC address #%d\n", i); + } + + if (strcmp(lb->policy, "weighted_random")) lb->weights[i] = 1; + else { + lb->weights[i] = weight->valueint; + lb->total_weight += weight->valueint; + } + i++; + + + + } + cJSON_Delete(config_json); + + //fclose(cfg); + printf("\nARP config:\n"); + for (i = 0; i < lb->server_count; i++) { + printf("%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 " ", (lb->server[i].d_ip >> 24) & 0xFF, + (lb->server[i].d_ip >> 16) & 0xFF, (lb->server[i].d_ip >> 8) & 0xFF, + lb->server[i].d_ip & 0xFF); + printf("%02x:%02x:%02x:%02x:%02x:%02x\n", lb->server[i].d_addr_bytes[0], lb->server[i].d_addr_bytes[1], + lb->server[i].d_addr_bytes[2], lb->server[i].d_addr_bytes[3], lb->server[i].d_addr_bytes[4], + lb->server[i].d_addr_bytes[5]); + } + + return ret; + + + //cfg = fopen(lb->cfg_filename, "r"); + // if (cfg == NULL) { + // rte_exit(EXIT_FAILURE, "Error openning server \'%s\' config\n", lb->cfg_filename); + // } + // ret = fscanf(cfg, "%*s %d", &temp); + // if (temp <= 0) { + // rte_exit(EXIT_FAILURE, "Error parsing config, need at least one server configurations\n"); + // } + // lb->server_count = temp; + + //lb->weights = (int*)calloc(temp,sizeof(int)); + + // lb->server = (struct backend_server *)rte_malloc("backend server info", + // sizeof(struct backend_server) * lb->server_count, 0); + // if (lb->server == NULL) { + // rte_exit(EXIT_FAILURE, "Malloc failed, can't allocate server information\n"); + // } + + // ret = fscanf(cfg, "%s", policy); + // lb->policy = strdup(policy); + + // if (!((!strcmp(lb->policy,"random")) || (!strcmp(lb->policy,"rrobin")) || (!strcmp(lb->policy,"weighted_random")))) { + // rte_exit(EXIT_FAILURE, "Invalid policy. Check server.conf\n"); + // } + + // for (i = 0; i < lb->server_count; i++) { + // ret = fscanf(cfg, "%s %s %d", ip, mac, &weight); + // if (strcmp(policy, "weighted_random")) weight = 1; + // if (ret != 3) { + // rte_exit(EXIT_FAILURE, "Invalid backend config structure\n"); + // } + + // ret = onvm_pkt_parse_ip(ip, &lb->server[i].d_ip); + // if (ret < 0) { + // rte_exit(EXIT_FAILURE, "Error parsing config IP address #%d\n", i); + // } + + // ret = onvm_pkt_parse_mac(mac, lb->server[i].d_addr_bytes); + // if (ret < 0) { + // rte_exit(EXIT_FAILURE, "Error parsing config MAC address #%d\n", i); + // } + + // lb->weights[i] = weight; + // lb->total_weight += weight; + // } + + +} + /* * This function displays stats. It uses ANSI terminal codes to clear * screen when called. It is called from a single non-master @@ -491,12 +643,6 @@ table_add_entry(struct onvm_ft_ipv4_5tuple *key, struct flow_info **flow) { data->dest = lb->num_stored % lb->server_count; } else if (!strcmp(lb->policy,"weighted_random")) { - // uint8_t w_mod = lb->num_stored % (lb->server_count + 6); - // if (w_mod) { - // data->dest = 1; - // } else { - // data->dest = 0; - // } time_t t; int i, wrand, cur_weight_sum; /* Intializes random number generator */ @@ -688,7 +834,7 @@ main(int argc, char *argv[]) { } validate_iface_config(); - parse_backend_config(); + parse_backend_json_config(); lb->expire_time = 32; lb->elapsed_cycles = rte_get_tsc_cycles(); diff --git a/examples/load_balancer/server.json b/examples/load_balancer/server.json new file mode 100644 index 000000000..218bfdad1 --- /dev/null +++ b/examples/load_balancer/server.json @@ -0,0 +1,17 @@ +{ + "Info" : { + "LIST_SIZE": 2, + "policy": "random" + }, + "Server0": { + "ip": "10.10.1.2", + "mac_addr": "90:e2:ba:ac:16:34", + "weight": 1 + }, + "Server1": { + "ip": "10.10.1.3", + "mac_addr": "90:e2:ba:b3:bb:7d", + "weight": 1 + } + +} \ No newline at end of file From 776666487452f9ff55dd80d45d85838f508b5291 Mon Sep 17 00:00:00 2001 From: Robin Andreasen Date: Tue, 26 Jul 2022 10:41:23 -0400 Subject: [PATCH 10/19] json v2 --- examples/load_balancer/load_balancer.c | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index 51b49547f..e8c98eabe 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -399,6 +399,7 @@ parse_backend_json_config(void) { lb->weights[i] = weight->valueint; lb->total_weight += weight->valueint; } + config_json = config_json->next; i++; From a7c96bce6ac26dc82cb7d4f0e3011b5fea9e97aa Mon Sep 17 00:00:00 2001 From: Robin Andreasen Date: Tue, 26 Jul 2022 14:06:38 -0400 Subject: [PATCH 11/19] final json changes before review --- examples/load_balancer/load_balancer.c | 133 +------------------------ 1 file changed, 2 insertions(+), 131 deletions(-) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index e8c98eabe..0d48a83dd 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -248,80 +248,6 @@ parse_app_args(int argc, char *argv[], const char *progname) { return optind; } -/* - * This function parses the backend config. It takes the filename - * and fills up the backend_server array. This includes the mac and ip - * address of the backend servers - */ -static int -parse_backend_config(void) { - int ret, temp, i, weight; - char ip[32]; - char mac[32]; - char policy[32]; - FILE *cfg; - - cfg = fopen(lb->cfg_filename, "r"); - if (cfg == NULL) { - rte_exit(EXIT_FAILURE, "Error openning server \'%s\' config\n", lb->cfg_filename); - } - ret = fscanf(cfg, "%*s %d", &temp); - if (temp <= 0) { - rte_exit(EXIT_FAILURE, "Error parsing config, need at least one server configurations\n"); - } - lb->server_count = temp; - - lb->weights = (int*)calloc(temp,sizeof(int)); - - lb->server = (struct backend_server *)rte_malloc("backend server info", - sizeof(struct backend_server) * lb->server_count, 0); - if (lb->server == NULL) { - rte_exit(EXIT_FAILURE, "Malloc failed, can't allocate server information\n"); - } - - ret = fscanf(cfg, "%s", policy); - lb->policy = strdup(policy); - - if (!((!strcmp(lb->policy,"random")) || (!strcmp(lb->policy,"rrobin")) || (!strcmp(lb->policy,"weighted_random")))) { - rte_exit(EXIT_FAILURE, "Invalid policy. Check server.conf\n"); - } - - for (i = 0; i < lb->server_count; i++) { - ret = fscanf(cfg, "%s %s %d", ip, mac, &weight); - if (strcmp(policy, "weighted_random")) weight = 1; - if (ret != 3) { - rte_exit(EXIT_FAILURE, "Invalid backend config structure\n"); - } - - ret = onvm_pkt_parse_ip(ip, &lb->server[i].d_ip); - if (ret < 0) { - rte_exit(EXIT_FAILURE, "Error parsing config IP address #%d\n", i); - } - - ret = onvm_pkt_parse_mac(mac, lb->server[i].d_addr_bytes); - if (ret < 0) { - rte_exit(EXIT_FAILURE, "Error parsing config MAC address #%d\n", i); - } - - lb->weights[i] = weight; - lb->total_weight += weight; - } - - fclose(cfg); - printf("\nARP config:\n"); - for (i = 0; i < lb->server_count; i++) { - printf("%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 " ", (lb->server[i].d_ip >> 24) & 0xFF, - (lb->server[i].d_ip >> 16) & 0xFF, (lb->server[i].d_ip >> 8) & 0xFF, - lb->server[i].d_ip & 0xFF); - printf("%02x:%02x:%02x:%02x:%02x:%02x\n", lb->server[i].d_addr_bytes[0], lb->server[i].d_addr_bytes[1], - lb->server[i].d_addr_bytes[2], lb->server[i].d_addr_bytes[3], lb->server[i].d_addr_bytes[4], - lb->server[i].d_addr_bytes[5]); - } - - return ret; -} - - /* * This function parses the backend config. It takes the filename * and fills up the backend_server array. This includes the mac and ip @@ -329,11 +255,7 @@ parse_backend_config(void) { */ static int parse_backend_json_config(void) { - int ret, i, num_objects; - // char ip[32]; - // char mac[32]; - // char policy[32]; - //FILE *cfg; + int ret, i; i = 0; cJSON *config_json = onvm_config_parse_file(lb->cfg_filename); @@ -348,7 +270,6 @@ parse_backend_json_config(void) { " the directory to the config file is being specified.\n", lb->cfg_filename); } - num_object = onvm_config_get_item_count(config_json); config_json = config_json -> child; list_size = cJSON_GetObjectItem(config_json, "LIST_SIZE"); @@ -407,7 +328,6 @@ parse_backend_json_config(void) { } cJSON_Delete(config_json); - //fclose(cfg); printf("\nARP config:\n"); for (i = 0; i < lb->server_count; i++) { printf("%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 " ", (lb->server[i].d_ip >> 24) & 0xFF, @@ -418,56 +338,7 @@ parse_backend_json_config(void) { lb->server[i].d_addr_bytes[5]); } - return ret; - - - //cfg = fopen(lb->cfg_filename, "r"); - // if (cfg == NULL) { - // rte_exit(EXIT_FAILURE, "Error openning server \'%s\' config\n", lb->cfg_filename); - // } - // ret = fscanf(cfg, "%*s %d", &temp); - // if (temp <= 0) { - // rte_exit(EXIT_FAILURE, "Error parsing config, need at least one server configurations\n"); - // } - // lb->server_count = temp; - - //lb->weights = (int*)calloc(temp,sizeof(int)); - - // lb->server = (struct backend_server *)rte_malloc("backend server info", - // sizeof(struct backend_server) * lb->server_count, 0); - // if (lb->server == NULL) { - // rte_exit(EXIT_FAILURE, "Malloc failed, can't allocate server information\n"); - // } - - // ret = fscanf(cfg, "%s", policy); - // lb->policy = strdup(policy); - - // if (!((!strcmp(lb->policy,"random")) || (!strcmp(lb->policy,"rrobin")) || (!strcmp(lb->policy,"weighted_random")))) { - // rte_exit(EXIT_FAILURE, "Invalid policy. Check server.conf\n"); - // } - - // for (i = 0; i < lb->server_count; i++) { - // ret = fscanf(cfg, "%s %s %d", ip, mac, &weight); - // if (strcmp(policy, "weighted_random")) weight = 1; - // if (ret != 3) { - // rte_exit(EXIT_FAILURE, "Invalid backend config structure\n"); - // } - - // ret = onvm_pkt_parse_ip(ip, &lb->server[i].d_ip); - // if (ret < 0) { - // rte_exit(EXIT_FAILURE, "Error parsing config IP address #%d\n", i); - // } - - // ret = onvm_pkt_parse_mac(mac, lb->server[i].d_addr_bytes); - // if (ret < 0) { - // rte_exit(EXIT_FAILURE, "Error parsing config MAC address #%d\n", i); - // } - - // lb->weights[i] = weight; - // lb->total_weight += weight; - // } - - + return ret; } /* From 162c5bc109265e7403b5e37b94257c97721e5c72 Mon Sep 17 00:00:00 2001 From: Robin Andreasen Date: Tue, 26 Jul 2022 14:29:15 -0400 Subject: [PATCH 12/19] removed server.conf for review --- examples/load_balancer/server.conf | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 examples/load_balancer/server.conf diff --git a/examples/load_balancer/server.conf b/examples/load_balancer/server.conf deleted file mode 100644 index b77f9d5d2..000000000 --- a/examples/load_balancer/server.conf +++ /dev/null @@ -1,4 +0,0 @@ -LIST_SIZE 2 -random -10.10.1.2 90:e2:ba:ac:16:34 1 -10.10.1.3 90:e2:ba:b3:bb:7d 1 From 28afd8ab98587424b49d51fb5c610b4316afd45c Mon Sep 17 00:00:00 2001 From: Robin Andreasen Date: Mon, 1 Aug 2022 18:52:45 -0400 Subject: [PATCH 13/19] Formatting and error checking --- examples/load_balancer/load_balancer.c | 23 ++++++++++++----------- examples/load_balancer/server.json | 5 +++-- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index 0d48a83dd..206df0b91 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -70,8 +70,6 @@ #include "onvm_nflib.h" #include "onvm_pkt_helper.h" #include "onvm_config_common.h" - - #include "cJSON.h" #define NF_TAG "load_balancer" @@ -255,8 +253,9 @@ parse_app_args(int argc, char *argv[], const char *progname) { */ static int parse_backend_json_config(void) { - int ret, i; + int ret, i, server_count; i = 0; + server_count=0; cJSON *config_json = onvm_config_parse_file(lb->cfg_filename); cJSON *list_size = NULL; @@ -266,17 +265,17 @@ parse_backend_json_config(void) { cJSON *weight = NULL; if (config_json == NULL) { - rte_exit(EXIT_FAILURE, "%s file could not be parsed/not found. Assure config file" + rte_exit(EXIT_FAILURE, "%s file could not be parsed or was not found. Assure" " the directory to the config file is being specified.\n", lb->cfg_filename); } config_json = config_json -> child; - list_size = cJSON_GetObjectItem(config_json, "LIST_SIZE"); + list_size = cJSON_GetObjectItem(config_json, "list_size"); policy = cJSON_GetObjectItem(config_json, "policy"); - if (list_size == NULL) rte_exit(EXIT_FAILURE, "LIST_SIZE not found/invalid\n"); - if (policy == NULL) rte_exit(EXIT_FAILURE, "Policy not found/invalid\n"); + if (list_size == NULL) rte_exit(EXIT_FAILURE, "list_size not found/invalid\n"); + if (policy == NULL) rte_exit(EXIT_FAILURE, "policy not found/invalid\n"); lb->server_count = list_size->valueint; lb->policy = strdup(policy->valuestring); @@ -285,7 +284,6 @@ parse_backend_json_config(void) { rte_exit(EXIT_FAILURE, "Invalid policy. Check server.conf\n"); } - lb->weights = (int*)calloc(lb->server_count,sizeof(int)); lb->server = (struct backend_server *)rte_malloc("backend server info", @@ -296,7 +294,6 @@ parse_backend_json_config(void) { config_json = config_json->next; - while (config_json != NULL) { ip_addr = cJSON_GetObjectItem(config_json, "ip"); mac_addr = cJSON_GetObjectItem(config_json, "mac_addr"); @@ -304,7 +301,7 @@ parse_backend_json_config(void) { if (ip_addr == NULL) rte_exit(EXIT_FAILURE, "IP not found/invalid\n"); if (mac_addr == NULL) rte_exit(EXIT_FAILURE, "MAC address not found/invalid\n"); - if (weight == NULL) rte_exit(EXIT_FAILURE, "Weight not found/invalid\n"); + ret = onvm_pkt_parse_ip(ip_addr->valuestring, &lb->server[i].d_ip); if (ret < 0) { @@ -317,15 +314,19 @@ parse_backend_json_config(void) { if (strcmp(lb->policy, "weighted_random")) lb->weights[i] = 1; else { + if (weight == NULL) rte_exit(EXIT_FAILURE, "Weight not found/invalid\n"); lb->weights[i] = weight->valueint; lb->total_weight += weight->valueint; } config_json = config_json->next; i++; + server_count++; + } + if (server_count!=lb->server_count) rte_exit(EXIT_FAILURE, "Invalid list_size in config file\n"); cJSON_Delete(config_json); printf("\nARP config:\n"); @@ -338,7 +339,7 @@ parse_backend_json_config(void) { lb->server[i].d_addr_bytes[5]); } - return ret; + return ret; } /* diff --git a/examples/load_balancer/server.json b/examples/load_balancer/server.json index 218bfdad1..d96c6a656 100644 --- a/examples/load_balancer/server.json +++ b/examples/load_balancer/server.json @@ -1,17 +1,18 @@ { "Info" : { - "LIST_SIZE": 2, + "list_size": 2, "policy": "random" }, + "Server0": { "ip": "10.10.1.2", "mac_addr": "90:e2:ba:ac:16:34", "weight": 1 }, + "Server1": { "ip": "10.10.1.3", "mac_addr": "90:e2:ba:b3:bb:7d", "weight": 1 } - } \ No newline at end of file From 5ae3696edd0201eba020168b83c8235d9b684219 Mon Sep 17 00:00:00 2001 From: Robin Andreasen Date: Tue, 2 Aug 2022 10:22:38 -0400 Subject: [PATCH 14/19] final formatting changes (ready for merge) --- examples/load_balancer/load_balancer.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index 206df0b91..833cb10e6 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -322,9 +322,6 @@ parse_backend_json_config(void) { i++; server_count++; - - - } if (server_count!=lb->server_count) rte_exit(EXIT_FAILURE, "Invalid list_size in config file\n"); cJSON_Delete(config_json); From e75a096530e2041994654cea2d3b1b3e663cf3d2 Mon Sep 17 00:00:00 2001 From: Robin Andreasen Date: Fri, 5 Aug 2022 15:24:17 -0400 Subject: [PATCH 15/19] resolve conflicts v1 --- examples/load_balancer/load_balancer.c | 56 ++++++++++++++++---------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index 833cb10e6..9b78b45ec 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -75,6 +75,8 @@ #define NF_TAG "load_balancer" #define TABLE_SIZE 65536 +enum lb_policy {RROBIN, RANDOM, WEIGHTED_RANDOM}; + /* Struct for load balancer information */ struct loadbalance { struct onvm_ft *ft; @@ -99,7 +101,8 @@ struct loadbalance { char *cfg_filename; /* LB policy */ - char *policy; + // char *policy; + enum lb_policy policy; /* structures to store server weights */ int *weights; @@ -277,12 +280,18 @@ parse_backend_json_config(void) { if (list_size == NULL) rte_exit(EXIT_FAILURE, "list_size not found/invalid\n"); if (policy == NULL) rte_exit(EXIT_FAILURE, "policy not found/invalid\n"); + lb->server_count = list_size->valueint; - lb->policy = strdup(policy->valuestring); + //lb->policy = strdup(policy->valuestring); - if (!((!strcmp(lb->policy,"random")) || (!strcmp(lb->policy,"rrobin")) || (!strcmp(lb->policy,"weighted_random")))) { - rte_exit(EXIT_FAILURE, "Invalid policy. Check server.conf\n"); - } + if (!strcmp(policy->valuestring, "RROBIN")) lb->policy = RROBIN; + else if (!strcmp(policy->valuestring, "RANDOM")) lb->policy = RANDOM; + else if (!strcmp(policy->valuestring, "WEIGHTED_RANDOM")) lb->policy = WEIGHTED_RANDOM; + else rte_exit(EXIT_FAILURE, "Invalid policy. Check server.json\n"); + + // if (!((!strcmp(lb->policy,"random")) || (!strcmp(lb->policy,"rrobin")) || (!strcmp(lb->policy,"weighted_random")))) { + // rte_exit(EXIT_FAILURE, "Invalid policy. Check server.conf\n"); + // } lb->weights = (int*)calloc(lb->server_count,sizeof(int)); @@ -312,7 +321,7 @@ parse_backend_json_config(void) { rte_exit(EXIT_FAILURE, "Error parsing config MAC address #%d\n", i); } - if (strcmp(lb->policy, "weighted_random")) lb->weights[i] = 1; + if (lb->policy != WEIGHTED_RANDOM) weight = 1; else { if (weight == NULL) rte_exit(EXIT_FAILURE, "Weight not found/invalid\n"); lb->weights[i] = weight->valueint; @@ -503,33 +512,32 @@ table_add_entry(struct onvm_ft_ipv4_5tuple *key, struct flow_info **flow) { } lb->num_stored++; - if (!strcmp(lb->policy,"random")) { - time_t t; - /* Intializes random number generator */ - srand((unsigned) time(&t)); + + int i, wrand, cur_weight_sum; + switch (lb->policy) + { + case RANDOM: data->dest = rand() % lb->server_count; - } - else if (!strcmp(lb->policy,"rrobin")) { + break; + case RROBIN: data->dest = lb->num_stored % lb->server_count; - } - else if (!strcmp(lb->policy,"weighted_random")) { - time_t t; - int i, wrand, cur_weight_sum; - /* Intializes random number generator */ - srand((unsigned) time(&t)); + break; + case WEIGHTED_RANDOM: wrand = rand() % lb->total_weight; cur_weight_sum=0; for (i = 0; i < lb->server_count; i++) { cur_weight_sum+=lb->weights[i]; - if(wrand<=cur_weight_sum) { + if(wrand < cur_weight_sum) { data->dest=i; break; } } - + break; + default: + rte_exit(EXIT_FAILURE, "Invalid policy while adding entry to table!\n"); + break; } - data->last_pkt_cycles = lb->elapsed_cycles; data->is_active = 0; *flow = data; @@ -668,6 +676,10 @@ main(int argc, char *argv[]) { int arg_offset; const char *progname = argv[0]; + time_t t; + /* Intializes RANDOM number generator */ + srand((unsigned) time(&t)); + nf_local_ctx = onvm_nflib_init_nf_local_ctx(); onvm_nflib_start_signal_handler(nf_local_ctx, NULL); @@ -712,6 +724,8 @@ main(int argc, char *argv[]) { onvm_nflib_run(nf_local_ctx); onvm_nflib_stop(nf_local_ctx); + + free(lb->weights); onvm_ft_free(lb->ft); rte_free(lb); printf("If we reach here, program is ending\n"); From 756240b0c8810c267d9931c743befba6da33545d Mon Sep 17 00:00:00 2001 From: Robin Andreasen Date: Fri, 5 Aug 2022 15:33:18 -0400 Subject: [PATCH 16/19] resolve conflicts v2 --- examples/load_balancer/load_balancer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index 9b78b45ec..e6a3d1d85 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -321,7 +321,7 @@ parse_backend_json_config(void) { rte_exit(EXIT_FAILURE, "Error parsing config MAC address #%d\n", i); } - if (lb->policy != WEIGHTED_RANDOM) weight = 1; + if (lb->policy != WEIGHTED_RANDOM) lb->weights[i] = 1; else { if (weight == NULL) rte_exit(EXIT_FAILURE, "Weight not found/invalid\n"); lb->weights[i] = weight->valueint; @@ -679,7 +679,7 @@ main(int argc, char *argv[]) { time_t t; /* Intializes RANDOM number generator */ srand((unsigned) time(&t)); - + nf_local_ctx = onvm_nflib_init_nf_local_ctx(); onvm_nflib_start_signal_handler(nf_local_ctx, NULL); From 92158a50b313defee5aff45da9f1cdfe10d26bb6 Mon Sep 17 00:00:00 2001 From: Robin Andreasen Date: Mon, 15 Aug 2022 16:51:27 -0400 Subject: [PATCH 17/19] debug --- examples/load_balancer/load_balancer.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index e6a3d1d85..7a45af368 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -127,6 +127,9 @@ struct loadbalance *lb; /* number of package between each print */ static uint32_t print_delay = 1000000; +/* switch for turning on debug mode */ +static int debug_mode = 0; + /* onvm struct for port info lookup */ extern struct port_info *ports; @@ -148,6 +151,7 @@ usage(const char *progname) { printf(" - `-t SERVER_PORT` : server port ID\n"); printf(" - `-f SERVER_CONFIG` : backend server config file\n"); printf(" - `-p `: number of packets between each print, e.g. `-p 1` prints every packets.\n"); + printf(" - `-d`: debug info including when connections are recieved and when packets are recieved.\n"); } /* @@ -176,7 +180,7 @@ parse_app_args(int argc, char *argv[], const char *progname) { lb->client_port = RTE_MAX_ETHPORTS; lb->server_port = RTE_MAX_ETHPORTS; - while ((c = getopt(argc, argv, "c:r:s:t:f:p:")) != -1) { + while ((c = getopt(argc, argv, "c:r:s:t:f:p:d")) != -1) { switch (c) { case 'c': ret = parse_iface_ip(strdup(optarg), &lb->ip_lb_client); @@ -204,11 +208,12 @@ parse_app_args(int argc, char *argv[], const char *progname) { case 'p': print_delay = strtoul(optarg, NULL, 10); break; + case 'd': + debug_mode = 1; + break; case '?': usage(progname); - if (optopt == 'd') - RTE_LOG(INFO, APP, "Option -%c requires an argument.\n", optopt); - else if (optopt == 'p') + if (optopt == 'p') RTE_LOG(INFO, APP, "Option -%c requires an argument.\n", optopt); else if (isprint(optopt)) RTE_LOG(INFO, APP, "Unknown option `-%c'.\n", optopt); @@ -256,9 +261,8 @@ parse_app_args(int argc, char *argv[], const char *progname) { */ static int parse_backend_json_config(void) { - int ret, i, server_count; + int ret, i; i = 0; - server_count=0; cJSON *config_json = onvm_config_parse_file(lb->cfg_filename); cJSON *list_size = NULL; @@ -282,16 +286,11 @@ parse_backend_json_config(void) { lb->server_count = list_size->valueint; - //lb->policy = strdup(policy->valuestring); if (!strcmp(policy->valuestring, "RROBIN")) lb->policy = RROBIN; else if (!strcmp(policy->valuestring, "RANDOM")) lb->policy = RANDOM; else if (!strcmp(policy->valuestring, "WEIGHTED_RANDOM")) lb->policy = WEIGHTED_RANDOM; else rte_exit(EXIT_FAILURE, "Invalid policy. Check server.json\n"); - - // if (!((!strcmp(lb->policy,"random")) || (!strcmp(lb->policy,"rrobin")) || (!strcmp(lb->policy,"weighted_random")))) { - // rte_exit(EXIT_FAILURE, "Invalid policy. Check server.conf\n"); - // } lb->weights = (int*)calloc(lb->server_count,sizeof(int)); @@ -329,10 +328,8 @@ parse_backend_json_config(void) { } config_json = config_json->next; i++; - server_count++; - } - if (server_count!=lb->server_count) rte_exit(EXIT_FAILURE, "Invalid list_size in config file\n"); + if ( i != lb->server_count) rte_exit(EXIT_FAILURE, "Invalid list_size in config file\n"); cJSON_Delete(config_json); printf("\nARP config:\n"); @@ -631,6 +628,7 @@ packet_handler(struct rte_mbuf *pkt, struct onvm_pkt_meta *meta, for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) { flow_info->s_addr_bytes[i] = ehdr->s_addr.addr_bytes[i]; } + if(debug_mode) printf("New connection made with server %d.\n",flow_info->dest); } if (pkt->port == lb->server_port) { From 23efebebcd55c88d65a0000c33260ce8efd3c849 Mon Sep 17 00:00:00 2001 From: Robin Andreasen Date: Fri, 26 Aug 2022 20:40:28 -0400 Subject: [PATCH 18/19] Debug Mode Changes --- examples/load_balancer/load_balancer.c | 9 ++++++++- examples/load_balancer/server.json | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index 7a45af368..c14406897 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -591,10 +591,12 @@ packet_handler(struct rte_mbuf *pkt, struct onvm_pkt_meta *meta, struct rte_ipv4_hdr *ip; struct rte_ether_hdr *ehdr; struct flow_info *flow_info; + struct rte_tcp_hdr* tcp; int i, ret; ehdr = onvm_pkt_ether_hdr(pkt); ip = onvm_pkt_ipv4_hdr(pkt); + tcp = onvm_pkt_tcp_hdr(pkt); /* Ignore packets without ip header, also ignore packets with invalid ip */ if (ip == NULL || ip->src_addr == 0 || ip->dst_addr == 0) { @@ -628,7 +630,12 @@ packet_handler(struct rte_mbuf *pkt, struct onvm_pkt_meta *meta, for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) { flow_info->s_addr_bytes[i] = ehdr->s_addr.addr_bytes[i]; } - if(debug_mode) printf("New connection made with server %d.\n",flow_info->dest); + if(debug_mode) { + printf("New connection made with server %d.\n",flow_info->dest); + printf("Source IP: %" PRIu32 " (%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 ":%" PRIu16") made a new connection with server %d.\n", hdr->src_addr, + hdr->src_addr & 0xFF, (hdr->src_addr >> 8) & 0xFF, (hdr->src_addr >> 16) & 0xFF, + (hdr->src_addr >> 24) & 0xFF, rte_be_to_cpu_16(tcp->src_port),flow_info->dest); + } } if (pkt->port == lb->server_port) { diff --git a/examples/load_balancer/server.json b/examples/load_balancer/server.json index d96c6a656..b6c96fea8 100644 --- a/examples/load_balancer/server.json +++ b/examples/load_balancer/server.json @@ -1,7 +1,7 @@ { "Info" : { "list_size": 2, - "policy": "random" + "policy": "RANDOM" }, "Server0": { From e0d56f15efd31c50897c069c4c2393aab3b3d35d Mon Sep 17 00:00:00 2001 From: Robin Andreasen Date: Sat, 27 Aug 2022 15:42:17 -0400 Subject: [PATCH 19/19] small fix --- examples/load_balancer/load_balancer.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/load_balancer/load_balancer.c b/examples/load_balancer/load_balancer.c index c14406897..fef512772 100644 --- a/examples/load_balancer/load_balancer.c +++ b/examples/load_balancer/load_balancer.c @@ -632,9 +632,9 @@ packet_handler(struct rte_mbuf *pkt, struct onvm_pkt_meta *meta, } if(debug_mode) { printf("New connection made with server %d.\n",flow_info->dest); - printf("Source IP: %" PRIu32 " (%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 ":%" PRIu16") made a new connection with server %d.\n", hdr->src_addr, - hdr->src_addr & 0xFF, (hdr->src_addr >> 8) & 0xFF, (hdr->src_addr >> 16) & 0xFF, - (hdr->src_addr >> 24) & 0xFF, rte_be_to_cpu_16(tcp->src_port),flow_info->dest); + printf("Source IP: %" PRIu32 " (%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 ":%" PRIu16") made a new connection with server %d.\n", ip->src_addr, + ip->src_addr & 0xFF, (ip->src_addr >> 8) & 0xFF, (ip->src_addr >> 16) & 0xFF, + (ip->src_addr >> 24) & 0xFF, rte_be_to_cpu_16(tcp->src_port),flow_info->dest); } }