/* * Add or modify flow table entry. * * When modifying, the stats can be optionally cleared */ static void flow_cmd_new(struct dpdk_flow_message *request) { struct dpdk_message reply = {0}; int32_t pos = 0; pos = rte_hash_lookup(handle, &request->key); if (pos < 0) { if (request->flags & FLAG_CREATE) { pos = rte_hash_add_key(handle, &request->key); flow_table_add_flow(pos, &request->key, request->action, true); reply.type = 0; } else { reply.type = ENOENT; } } else { if (request->flags & FLAG_REPLACE) { /* Retrieve flow stats*/ flow_table_get_flow(pos, NULL, NULL, &request->stats); /* Depending on the value of request->clear we will either update * or keep the same stats */ flow_table_add_flow(pos, &request->key, request->action, request->clear); reply.type = 0; } else { reply.type = EEXIST; } } reply.flow_msg = *request; send_reply_to_vswitchd(&reply); }
/* Try to delete all flows, which should succeed */ static void test_flow_table_del_all(int argc, char *argv[]) { struct flow_key key1 = {1}; struct flow_key key2 = {2}; struct flow_key key_check = {0}; struct action action_multiple[MAX_ACTIONS] = {0}; struct action action_check[MAX_ACTIONS] = {0}; struct flow_stats stats_check = {0}; int ret = 0; flow_table_init(); action_output_build(&action_multiple[0], 1); action_output_build(&action_multiple[1], 2); action_null_build(&action_multiple[2]); /* Add two flowss to be deleted */ flow_table_add_flow(&key1, action_multiple); ret = flow_table_get_flow(&key1, NULL, NULL); assert(ret >= 0); flow_table_add_flow(&key2, action_multiple); ret = flow_table_get_flow(&key2, NULL, NULL); assert(ret >= 0); /* delete all flows and ensure flows are deleted */ flow_table_del_all(); ret = flow_table_get_first_flow(&key_check, action_check, &stats_check); assert(ret < 0); }
/* Try to add a normal flow and duplicate flow, and add a flow with * incorrect parameters, which should succeed, fail with -1 and fail * with -1 respectively */ static void test_flow_table_add_flow(int argc, char *argv[]) { struct flow_key key1 = {1}; struct action action_multiple[MAX_ACTIONS] = {0}; int ret = 0; flow_table_init(); /* TODO: Break this into multiple tests? */ action_output_build(&action_multiple[0], 1); action_output_build(&action_multiple[1], 2); action_null_build(&action_multiple[2]); ret = flow_table_add_flow(&key1, action_multiple); assert(ret >= 0); /* check no duplicates */ ret = flow_table_add_flow(&key1, action_multiple); assert(ret < 0); /* check incorrect parameters */ flow_table_del_flow(&key1); ret = flow_table_add_flow(NULL, action_multiple); assert(ret < 0); ret = flow_table_add_flow(&key1, NULL); assert(ret < 0); }
/* Try to modify a flow, which should succeed */ static void test_flow_table_mod_flow(int argc, char *argv[]) { struct flow_key key1 = {1}; struct action action_multiple[MAX_ACTIONS] = {0}; struct action action_check[MAX_ACTIONS] = {0}; struct flow_stats stats_zero = {0}; struct flow_stats stats_check = {0}; int ret = 0; flow_table_init(); /* add a flow with 2 actions */ action_output_build(&action_multiple[0], 2); action_output_build(&action_multiple[1], 1); action_null_build(&action_multiple[2]); flow_table_add_flow(&key1, action_multiple); action_output_build(&action_multiple[0], 1); action_null_build(&action_multiple[1]); /*modify it to only have 1 action */ flow_table_mod_flow(&key1, action_multiple, true); ret = flow_table_get_flow(&key1, action_check, &stats_check); assert(ret >= 0); assert(memcmp(&action_multiple[0], action_check, sizeof(struct action)) == 0); /* check that our flow now only has one entry */ assert(action_check[1].type == ACTION_NULL); assert(memcmp(&stats_zero, &stats_check , sizeof(struct flow_stats)) == 0 ); }
/* Try to get next flow, which should succeed */ static void test_flow_table_get_next_flow(int argc, char *argv[]) { struct flow_key key1 = {1}; struct flow_key key2 = {2}; struct flow_key key_check = {0}; struct action action_multiple[MAX_ACTIONS] = {0}; struct action action_check[MAX_ACTIONS] = {0}; struct flow_stats stats_zero = {0}; struct flow_stats stats_check = {0}; int ret = 0; flow_table_init(); flow_table_del_all(); action_output_build(&action_multiple[0], 1); action_null_build(&action_multiple[1]); ret = flow_table_add_flow(&key1, action_multiple); assert(ret >= 0); ret = flow_table_add_flow(&key2, action_multiple); assert(ret >= 0); ret = flow_table_get_first_flow(&key_check, action_check, &stats_check); assert(ret >= 0); if (memcmp(&key1, &key_check, sizeof(struct flow_key)) == 0 ) { ret = flow_table_get_next_flow(&key_check, &key_check, action_check, &stats_check); assert(ret >= 0); assert(memcmp(action_multiple, action_check, sizeof(struct action)) == 0); assert(memcmp(&stats_zero, &stats_check, sizeof(struct flow_stats)) == 0 ); assert(memcmp(&key2, &key_check, sizeof(struct flow_key)) == 0 ); } else if (memcmp(&key2, &key_check, sizeof(struct flow_key) == 0)) { ret = flow_table_get_next_flow(&key_check, &key_check, action_check, &stats_check); assert(ret >= 0); assert(memcmp(action_multiple, action_check, sizeof(struct action)) == 0); assert(memcmp(&stats_zero, &stats_check, sizeof(struct flow_stats)) == 0 ); assert(memcmp(&key1, &key_check, sizeof(struct flow_key)) == 0 ); } else { assert(1==0); } }
/* Try to get a flow, which should succeed */ static void test_flow_table_get_flow(int argc, char *argv[]) { struct flow_key key1 = {1}; struct action action_multiple[MAX_ACTIONS] = {0}; struct action action_check[MAX_ACTIONS] = {0}; struct flow_stats stats_zero = {0}; struct flow_stats stats_check = {0}; int ret = 0; flow_table_init(); action_output_build(&action_multiple[0], 1); action_output_build(&action_multiple[1], 2); action_null_build(&action_multiple[2]); flow_table_add_flow(&key1, action_multiple); ret = flow_table_get_flow(&key1, action_check, &stats_check); assert(ret >= 0); assert(memcmp(&action_multiple[0], action_check, sizeof(struct action)) == 0); assert(memcmp(&action_multiple[1], &action_check[1], sizeof(struct action)) == 0); assert(memcmp(&stats_zero, &stats_check , sizeof(struct flow_stats)) == 0 ); }