Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weighted Random Policy #327

Merged
merged 23 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 54 additions & 5 deletions examples/load_balancer/load_balancer.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ struct loadbalance {

/* config file */
char *cfg_filename;

/* LB policy */
char *policy;
xwedea marked this conversation as resolved.
Show resolved Hide resolved

/* structures to store server weights */
int *weights;
int total_weight;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your data structure here will allow us to represent weighted load balancers, but there could be other policies we could come up with in the future that might need totally different data. For now this is fine, but you should think about how you would make this data structure generic so it works for all LBs (but still allows customization by each policy in some way)

};

/* Struct for backend servers */
Expand Down Expand Up @@ -244,9 +251,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");
Expand All @@ -259,15 +267,25 @@ 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) {
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", ip, mac);
if (ret != 2) {
ret = fscanf(cfg, "%s %s %d", ip, mac, &weight);
xwedea marked this conversation as resolved.
Show resolved Hide resolved
if (strcmp(policy, "weighted_random")) weight = 1;
xwedea marked this conversation as resolved.
Show resolved Hide resolved
if (ret != 3) {
rte_exit(EXIT_FAILURE, "Invalid backend config structure\n");
}

Expand All @@ -280,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);
Expand Down Expand Up @@ -460,10 +481,34 @@ table_add_entry(struct onvm_ft_ipv4_5tuple *key, struct flow_info **flow) {
}

lb->num_stored++;
data->dest = lb->num_stored % lb->server_count;
if (!strcmp(lb->policy,"random")) {
xwedea marked this conversation as resolved.
Show resolved Hide resolved
time_t t;
/* Intializes random number generator */
srand((unsigned) time(&t));
xwedea marked this conversation as resolved.
Show resolved Hide resolved
data->dest = rand() % lb->server_count;
}
else if (!strcmp(lb->policy,"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));
xwedea marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}

}
xwedea marked this conversation as resolved.
Show resolved Hide resolved

xwedea marked this conversation as resolved.
Show resolved Hide resolved
data->last_pkt_cycles = lb->elapsed_cycles;
data->is_active = 0;

*flow = data;

return 0;
Expand Down Expand Up @@ -644,6 +689,10 @@ main(int argc, char *argv[]) {
onvm_nflib_run(nf_local_ctx);

onvm_nflib_stop(nf_local_ctx);

free(lb->weights);
free(lb->policy);

xwedea marked this conversation as resolved.
Show resolved Hide resolved
onvm_ft_free(lb->ft);
rte_free(lb);
printf("If we reach here, program is ending\n");
Expand Down
5 changes: 3 additions & 2 deletions examples/load_balancer/server.conf
Original file line number Diff line number Diff line change
@@ -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
weighted_random
10.10.1.2 90:e2:ba:ac:16:34 1
10.10.1.3 90:e2:ba:b3:bb:7d 7
7 changes: 7 additions & 0 deletions run_manager.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/bash
cd /local/onvm/openNetVM/scripts
source ./setup_cloudlab.sh
cd /local/onvm/openNetVM
cd onvm && make && cd ..
cd examples && make && cd ..
./onvm/go.sh -k 3 -n 0xFF -s stdout
xwedea marked this conversation as resolved.
Show resolved Hide resolved