Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions prefix_tags/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Interfaces
- Input: One UniRec interface
- Template *MUST* contain fields `SRC_IP` or `DST_IP` (see cli options).
- Output: One UniRec interface
- Output template is copied from input template with `PREFIX_TAG` field added.
- The module sends only records with matched `PREFIX_TAG`.
- Output template is copied from input template with `PREFIX_TAG` and `PREFIX_TAG_DST` fields added.
- The module sends only records with matched prefix.


Usage
Expand All @@ -40,7 +40,7 @@ Parameters of module [OPTIONS]:
-c --config <string> Configuration file.
-d --dst Use only DST_IP field for prefix matching (default is both SRC_IP and DST_IP).
-s --src Use only SRC_IP field for prefix matching (default is both SRC_IP and DST_IP).

-b --both Both SRC_IP and DST_IP is tagged. When -b is not enabled, the `PREFIX_TAG_DST` is always `0` and only PREFIX_TAG field is used
Common TRAP parameters [COMMON]:
--------------------------------
-h [trap,1] If no argument, print this message. If "trap" or 1 is given, print TRAP help.
Expand Down
37 changes: 28 additions & 9 deletions prefix_tags/prefix_tags.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
UR_FIELDS (
ipaddr SRC_IP,
ipaddr DST_IP,
uint32 PREFIX_TAG
uint32 PREFIX_TAG,
uint32 PREFIX_TAG_DST
)

trap_module_info_t *module_info = NULL;
Expand All @@ -42,19 +43,22 @@ trap_module_info_t *module_info = NULL;
#define MODULE_PARAMS(PARAM) \
PARAM('c', "config", "Configuration file.", required_argument, "string") \
PARAM('d', "dst", "Use only DST_IP field for prefix matching (default is both SRC_IP and DST_IP).", no_argument, "none") \
PARAM('s', "src", "Use only SRC_IP field for prefix matching (default is both SRC_IP and DST_IP).", no_argument, "none")
PARAM('s', "src", "Use only SRC_IP field for prefix matching (default is both SRC_IP and DST_IP).", no_argument, "none") \
PARAM('b', "both", "Tag both SRC_IP and DST_IP", no_argument,"none")

static int stop = 0;

TRAP_DEFAULT_SIGNAL_HANDLER(stop = 1)

int CHECK_SRC_IP = 1;
int CHECK_DST_IP = 1;
int CHECK_BOTH = 0;


int prefix_tags(ipps_context_t *config) {
int error = 0;
uint32_t prefix_tag;
uint32_t prefix_tag_dst;
const void *data_in = NULL;
uint16_t data_in_size;
void *data_out = NULL;
Expand All @@ -69,10 +73,9 @@ int prefix_tags(ipps_context_t *config) {
while (stop == 0) {
int recv_error = TRAP_RECEIVE(INTERFACE_IN, data_in, data_in_size, template_in);
TRAP_DEFAULT_RECV_ERROR_HANDLING(recv_error, continue, error = -2; goto cleanup)

if (recv_error == TRAP_E_FORMAT_CHANGED) {
// Copy format to output interface and add PREFIX_TAG
error = update_output_format(template_in, data_in, &template_out, &data_out);
error = update_output_format(template_in, data_in, &template_out, &data_out);
if (error) {
goto cleanup;
}
Expand All @@ -88,15 +91,29 @@ int prefix_tags(ipps_context_t *config) {

ip_addr_t src_ip = ur_get(template_in, data_in, F_SRC_IP);
ip_addr_t dst_ip = ur_get(template_in, data_in, F_DST_IP);
prefix_tag = 0;
prefix_tag_dst = 0;
int src_res = 0;
int dst_res = 0;

if (CHECK_SRC_IP || CHECK_BOTH){
src_res = is_from_configured_prefix(config, &src_ip, &prefix_tag);
}
if ((!src_res && CHECK_DST_IP) || CHECK_BOTH){
dst_res = is_from_configured_prefix(config, &dst_ip, &prefix_tag_dst);
}

if ((CHECK_SRC_IP && is_from_configured_prefix(config, &src_ip, &prefix_tag)) // Misusing short-circuit evaluation
|| (CHECK_DST_IP && is_from_configured_prefix(config, &dst_ip, &prefix_tag))) {
if (src_res || dst_res) {
debug_print("tagging %d\n", prefix_tag);
// data_out should have the right size since TRAP_E_FORMAT_CHANGED _had_ to be returned before getting here
ur_copy_fields(template_out, data_out, template_in, data_in);
// Set PREFIX_TAG field
ur_set(template_out, data_out, F_PREFIX_TAG, prefix_tag);

if (CHECK_BOTH == 0) {
ur_set(template_out, data_out, F_PREFIX_TAG, (prefix_tag!=0)?prefix_tag:prefix_tag_dst);
} else {
ur_set(template_out, data_out, F_PREFIX_TAG, prefix_tag);
ur_set(template_out, data_out, F_PREFIX_TAG_DST, prefix_tag_dst);
}
uint16_t data_out_size = ur_rec_size(template_out, data_out);
debug_print("data_out_size %d\n", data_out_size);
int send_error = trap_send(INTERFACE_OUT, data_out, data_out_size);
Expand Down Expand Up @@ -146,7 +163,9 @@ int main(int argc, char **argv)
case 's':
CHECK_DST_IP = 0;
break;

case 'b':
CHECK_BOTH = 1;
break;
}
}

Expand Down
2 changes: 1 addition & 1 deletion prefix_tags/prefix_tags_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int update_output_format(ur_template_t *template_in, const void *data_in, ur_tem
}

// Add PREFIX_TAG field
*template_out = ur_expand_template("uint32 PREFIX_TAG", *template_out);
*template_out = ur_expand_template("uint32 PREFIX_TAG,uint32 PREFIX_TAG_DST", *template_out);
if (*template_out == NULL) {
return -1;
}
Expand Down
Loading