#include <string.h>
#include <pipeline.h>
#include "pipeline_flow_classification_be.h"
#include "pipeline_actions_common.h"
#include "hash_func.h"
struct pipeline_flow_classification {
struct pipeline p;
pipeline_msg_req_handler custom_handlers[PIPELINE_FC_MSG_REQS];
uint32_t n_flows;
uint32_t key_size;
uint32_t flow_id;
uint32_t key_offset;
uint32_t hash_offset;
uint8_t *key_mask;
uint32_t flow_id_offset;
static void *
pipeline_fc_msg_req_custom_handler(struct pipeline *p, void *msg);
static pipeline_msg_req_handler handlers[] = {
[PIPELINE_MSG_REQ_PING] =
pipeline_msg_req_ping_handler,
[PIPELINE_MSG_REQ_STATS_PORT_IN] =
pipeline_msg_req_stats_port_in_handler,
[PIPELINE_MSG_REQ_STATS_PORT_OUT] =
pipeline_msg_req_stats_port_out_handler,
[PIPELINE_MSG_REQ_STATS_TABLE] =
pipeline_msg_req_stats_table_handler,
[PIPELINE_MSG_REQ_PORT_IN_ENABLE] =
pipeline_msg_req_port_in_enable_handler,
[PIPELINE_MSG_REQ_PORT_IN_DISABLE] =
pipeline_msg_req_port_in_disable_handler,
[PIPELINE_MSG_REQ_CUSTOM] =
pipeline_fc_msg_req_custom_handler,
};
static void *
pipeline_fc_msg_req_add_handler(struct pipeline *p, void *msg);
static void *
pipeline_fc_msg_req_add_bulk_handler(struct pipeline *p, void *msg);
static void *
pipeline_fc_msg_req_del_handler(struct pipeline *p, void *msg);
static void *
pipeline_fc_msg_req_add_default_handler(struct pipeline *p, void *msg);
static void *
pipeline_fc_msg_req_del_default_handler(struct pipeline *p, void *msg);
static pipeline_msg_req_handler custom_handlers[] = {
[PIPELINE_FC_MSG_REQ_FLOW_ADD] =
pipeline_fc_msg_req_add_handler,
[PIPELINE_FC_MSG_REQ_FLOW_ADD_BULK] =
pipeline_fc_msg_req_add_bulk_handler,
[PIPELINE_FC_MSG_REQ_FLOW_DEL] =
pipeline_fc_msg_req_del_handler,
[PIPELINE_FC_MSG_REQ_FLOW_ADD_DEFAULT] =
pipeline_fc_msg_req_add_default_handler,
[PIPELINE_FC_MSG_REQ_FLOW_DEL_DEFAULT] =
pipeline_fc_msg_req_del_default_handler,
};
struct flow_table_entry {
uint32_t flow_id;
uint32_t pad;
};
hash_default_key8,
hash_default_key16,
hash_default_key24,
hash_default_key32,
hash_default_key40,
hash_default_key48,
hash_default_key56,
hash_default_key64
};
static inline void
pkt_work_flow_id(
void *arg)
{
struct pipeline_flow_classification *p_fc = arg;
uint32_t *flow_id_ptr =
struct flow_table_entry *entry =
(struct flow_table_entry *) table_entry;
uint32_t flow_id = entry->flow_id;
*flow_id_ptr = flow_id;
}
static inline void
pkt4_work_flow_id(
void *arg)
{
struct pipeline_flow_classification *p_fc = arg;
uint32_t *flow_id_ptr0 =
uint32_t *flow_id_ptr1 =
uint32_t *flow_id_ptr2 =
uint32_t *flow_id_ptr3 =
struct flow_table_entry *entry0 =
(struct flow_table_entry *) table_entries[0];
struct flow_table_entry *entry1 =
(struct flow_table_entry *) table_entries[1];
struct flow_table_entry *entry2 =
(struct flow_table_entry *) table_entries[2];
struct flow_table_entry *entry3 =
(struct flow_table_entry *) table_entries[3];
uint32_t flow_id0 = entry0->flow_id;
uint32_t flow_id1 = entry1->flow_id;
uint32_t flow_id2 = entry2->flow_id;
uint32_t flow_id3 = entry3->flow_id;
*flow_id_ptr0 = flow_id0;
*flow_id_ptr1 = flow_id1;
*flow_id_ptr2 = flow_id2;
*flow_id_ptr3 = flow_id3;
}
PIPELINE_TABLE_AH_HIT(fc_table_ah_hit,
pkt_work_flow_id, pkt4_work_flow_id);
get_fc_table_ah_hit(struct pipeline_flow_classification *p)
{
if (p->flow_id)
return fc_table_ah_hit;
return NULL;
}
static int
pipeline_fc_parse_args(struct pipeline_flow_classification *p,
struct pipeline_params *params)
{
uint32_t n_flows_present = 0;
uint32_t key_offset_present = 0;
uint32_t key_size_present = 0;
uint32_t hash_offset_present = 0;
uint32_t key_mask_present = 0;
uint32_t flow_id_offset_present = 0;
uint32_t i;
char *key_mask_str = NULL;
p->hash_offset = 0;
p->flow_id = 0;
for (i = 0; i < params->n_args; i++) {
char *arg_name = params->args_name[i];
char *arg_value = params->args_value[i];
if (strcmp(arg_name, "n_flows") == 0) {
if (n_flows_present)
goto error_parse;
n_flows_present = 1;
p->n_flows = atoi(arg_value);
if (p->n_flows == 0)
goto error_parse;
continue;
}
if (strcmp(arg_name, "key_offset") == 0) {
if (key_offset_present)
goto error_parse;
key_offset_present = 1;
p->key_offset = atoi(arg_value);
continue;
}
if (strcmp(arg_name, "key_size") == 0) {
if (key_size_present)
goto error_parse;
key_size_present = 1;
p->key_size = atoi(arg_value);
if ((p->key_size == 0) ||
(p->key_size > PIPELINE_FC_FLOW_KEY_MAX_SIZE) ||
(p->key_size % 8))
goto error_parse;
continue;
}
if (strcmp(arg_name, "key_mask") == 0) {
if (key_mask_present)
goto error_parse;
key_mask_str = strdup(arg_value);
if (key_mask_str == NULL)
goto error_parse;
key_mask_present = 1;
continue;
}
if (strcmp(arg_name, "hash_offset") == 0) {
if (hash_offset_present)
goto error_parse;
hash_offset_present = 1;
p->hash_offset = atoi(arg_value);
continue;
}
if (strcmp(arg_name, "flowid_offset") == 0) {
if (flow_id_offset_present)
goto error_parse;
flow_id_offset_present = 1;
p->flow_id = 1;
p->flow_id_offset = atoi(arg_value);
continue;
}
goto error_parse;
}
if ((n_flows_present == 0) ||
(key_offset_present == 0) ||
(key_size_present == 0))
goto error_parse;
if (key_mask_present) {
if (p->key_mask == NULL)
goto error_parse;
if (parse_hex_string(key_mask_str, p->key_mask, &p->key_size)
!= 0) {
goto error_parse;
}
free(key_mask_str);
}
return 0;
error_parse:
if (key_mask_str != NULL)
free(key_mask_str);
if (p->key_mask != NULL)
free(p->key_mask);
return -1;
}
static void *pipeline_fc_init(struct pipeline_params *params,
{
struct pipeline *p;
struct pipeline_flow_classification *p_fc;
uint32_t size, i;
if (params == NULL)
return NULL;
sizeof(struct pipeline_flow_classification));
if (p == NULL)
return NULL;
p_fc = (struct pipeline_flow_classification *) p;
strcpy(p->name, params->name);
p->log_level = params->log_level;
PLOG(p, HIGH, "Flow classification");
if (pipeline_fc_parse_args(p_fc, params))
return NULL;
{
.socket_id = params->socket_id,
.offset_port_id = 0,
};
if (p->p == NULL) {
return NULL;
}
}
p->n_ports_in = params->n_ports_in;
for (i = 0; i < p->n_ports_in; i++) {
.
ops = pipeline_port_in_params_get_ops(
¶ms->port_in[i]),
.arg_create = pipeline_port_in_params_convert(
¶ms->port_in[i]),
.f_action = NULL,
.arg_ah = NULL,
.burst_size = params->port_in[i].burst_size,
};
&port_params,
&p->port_in_id[i]);
if (status) {
return NULL;
}
}
p->n_ports_out = params->n_ports_out;
for (i = 0; i < p->n_ports_out; i++) {
.
ops = pipeline_port_out_params_get_ops(
¶ms->port_out[i]),
.arg_create = pipeline_port_out_params_convert(
¶ms->port_out[i]),
.f_action = NULL,
.f_action_bulk = NULL,
.arg_ah = NULL,
};
&port_params,
&p->port_out_id[i]);
if (status) {
return NULL;
}
}
p->n_tables = 1;
{
table_hash_key8_params = {
.n_entries_ext = p_fc->n_flows,
.signature_offset = p_fc->hash_offset,
.key_offset = p_fc->key_offset,
.f_hash = hash_func[(p_fc->key_size / 8) - 1],
.seed = 0,
};
table_hash_key16_params = {
.n_entries_ext = p_fc->n_flows,
.signature_offset = p_fc->hash_offset,
.key_offset = p_fc->key_offset,
.f_hash = hash_func[(p_fc->key_size / 8) - 1],
.seed = 0,
};
table_hash_params = {
.n_keys = p_fc->n_flows,
.n_buckets = p_fc->n_flows / 4,
.n_buckets_ext = p_fc->n_flows / 4,
.f_hash = hash_func[(p_fc->key_size / 8) - 1],
.key_offset = p_fc->key_offset,
};
.arg_create = NULL,
.f_action_hit = get_fc_table_ah_hit(p_fc),
.f_action_miss = NULL,
.arg_ah = p_fc,
.action_data_size = sizeof(struct flow_table_entry) -
};
int status;
switch (p_fc->key_size) {
case 8:
if (p_fc->hash_offset != 0) {
} else {
}
table_params.
arg_create = &table_hash_key8_params;
break;
case 16:
if (p_fc->hash_offset != 0) {
} else {
}
table_params.
arg_create = &table_hash_key16_params;
break;
default:
}
&table_params,
&p->table_id[0]);
if (status) {
return NULL;
}
}
for (i = 0; i < p->n_ports_in; i++) {
p->port_in_id[i],
p->table_id[0]);
if (status) {
return NULL;
}
}
for (i = 0; i < p->n_ports_in; i++) {
p->port_in_id[i]);
if (status) {
return NULL;
}
}
return NULL;
}
p->n_msgq = params->n_msgq;
for (i = 0; i < p->n_msgq; i++)
p->msgq_in[i] = params->msgq_in[i];
for (i = 0; i < p->n_msgq; i++)
p->msgq_out[i] = params->msgq_out[i];
memcpy(p->handlers, handlers, sizeof(p->handlers));
memcpy(p_fc->custom_handlers,
custom_handlers,
sizeof(p_fc->custom_handlers));
return p;
}
static int
pipeline_fc_free(void *pipeline)
{
struct pipeline *p = (struct pipeline *) pipeline;
if (p == NULL)
return -1;
return 0;
}
static int
pipeline_fc_track(void *pipeline,
uint32_t *port_out)
{
struct pipeline *p = (struct pipeline *) pipeline;
if ((p == NULL) ||
(port_in >= p->n_ports_in) ||
(port_out == NULL))
return -1;
if (p->n_ports_in == 1) {
*port_out = 0;
return 0;
}
return -1;
}
static int
pipeline_fc_timer(void *pipeline)
{
struct pipeline *p = (struct pipeline *) pipeline;
pipeline_msg_req_handle(p);
return 0;
}
static void *
pipeline_fc_msg_req_custom_handler(struct pipeline *p, void *msg)
{
struct pipeline_flow_classification *p_fc =
(struct pipeline_flow_classification *) p;
struct pipeline_custom_msg_req *req = msg;
pipeline_msg_req_handler f_handle;
f_handle = (req->subtype < PIPELINE_FC_MSG_REQS) ?
p_fc->custom_handlers[req->subtype] :
pipeline_msg_req_invalid_handler;
if (f_handle == NULL)
f_handle = pipeline_msg_req_invalid_handler;
return f_handle(p, req);
}
static void *
pipeline_fc_msg_req_add_handler(struct pipeline *p, void *msg)
{
struct pipeline_fc_add_msg_req *req = msg;
struct pipeline_fc_add_msg_rsp *rsp = msg;
struct flow_table_entry entry = {
.head = {
{.port_id = p->port_out_id[req->port_id]},
},
.flow_id = req->flow_id,
};
p->table_id[0],
&req->key,
&rsp->key_found,
return rsp;
}
static void *
pipeline_fc_msg_req_add_bulk_handler(struct pipeline *p, void *msg)
{
struct pipeline_fc_add_bulk_msg_req *req = msg;
struct pipeline_fc_add_bulk_msg_rsp *rsp = msg;
uint32_t i;
for (i = 0; i < req->n_keys; i++) {
struct pipeline_fc_add_bulk_flow_req *flow_req = &req->req[i];
struct pipeline_fc_add_bulk_flow_rsp *flow_rsp = &req->rsp[i];
struct flow_table_entry entry = {
.head = {
{.port_id = p->port_out_id[flow_req->port_id]},
},
.flow_id = flow_req->flow_id,
};
p->table_id[0],
&flow_req->key,
&flow_rsp->key_found,
&flow_rsp->entry_ptr);
if (status)
break;
}
rsp->n_keys = i;
return rsp;
}
static void *
pipeline_fc_msg_req_del_handler(struct pipeline *p, void *msg)
{
struct pipeline_fc_del_msg_req *req = msg;
struct pipeline_fc_del_msg_rsp *rsp = msg;
p->table_id[0],
&req->key,
&rsp->key_found,
NULL);
return rsp;
}
static void *
pipeline_fc_msg_req_add_default_handler(struct pipeline *p, void *msg)
{
struct pipeline_fc_add_default_msg_req *req = msg;
struct pipeline_fc_add_default_msg_rsp *rsp = msg;
struct flow_table_entry default_entry = {
.head = {
{.port_id = p->port_out_id[req->port_id]},
},
.flow_id = 0,
};
p->table_id[0],
return rsp;
}
static void *
pipeline_fc_msg_req_del_default_handler(struct pipeline *p, void *msg)
{
struct pipeline_fc_del_default_msg_rsp *rsp = msg;
p->table_id[0],
NULL);
return rsp;
}
struct pipeline_be_ops pipeline_flow_classification_be_ops = {
.f_init = pipeline_fc_init,
.f_free = pipeline_fc_free,
.f_run = NULL,
.f_timer = pipeline_fc_timer,
.f_track = pipeline_fc_track,
};