Skip to content

Commit acdbea2

Browse files
committed
prefix_tags: Added tagging capability for both SRC and DST IP addresses.
1 parent 58eb6c5 commit acdbea2

3 files changed

Lines changed: 26 additions & 13 deletions

File tree

prefix_tags/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ Interfaces
1717
- Input: One UniRec interface
1818
- Template *MUST* contain fields `SRC_IP` or `DST_IP` (see cli options).
1919
- Output: One UniRec interface
20-
- Output template is copied from input template with `PREFIX_TAG` field added.
21-
- The module sends only records with matched `PREFIX_TAG`.
20+
- Output template is copied from input template with `PREFIX_TAG` and `PREFIX_TAG_DST` fields added.
21+
- The module sends only records with matched prefix.
2222

2323

2424
Usage
@@ -40,7 +40,7 @@ Parameters of module [OPTIONS]:
4040
-c --config <string> Configuration file.
4141
-d --dst Use only DST_IP field for prefix matching (default is both SRC_IP and DST_IP).
4242
-s --src Use only SRC_IP field for prefix matching (default is both SRC_IP and DST_IP).
43-
43+
-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
4444
Common TRAP parameters [COMMON]:
4545
--------------------------------
4646
-h [trap,1] If no argument, print this message. If "trap" or 1 is given, print TRAP help.

prefix_tags/prefix_tags.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
UR_FIELDS (
3232
ipaddr SRC_IP,
3333
ipaddr DST_IP,
34-
uint32 PREFIX_TAG
34+
uint32 PREFIX_TAG,
35+
uint32 PREFIX_TAG_DST
3536
)
3637

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

4749
static int stop = 0;
4850

4951
TRAP_DEFAULT_SIGNAL_HANDLER(stop = 1)
5052

5153
int CHECK_SRC_IP = 1;
5254
int CHECK_DST_IP = 1;
55+
int CHECK_BOTH = 0;
5356

5457

5558
int prefix_tags(ipps_context_t *config) {
5659
int error = 0;
5760
uint32_t prefix_tag;
61+
uint32_t prefix_tag_dst;
5862
const void *data_in = NULL;
5963
uint16_t data_in_size;
6064
void *data_out = NULL;
@@ -69,10 +73,9 @@ int prefix_tags(ipps_context_t *config) {
6973
while (stop == 0) {
7074
int recv_error = TRAP_RECEIVE(INTERFACE_IN, data_in, data_in_size, template_in);
7175
TRAP_DEFAULT_RECV_ERROR_HANDLING(recv_error, continue, error = -2; goto cleanup)
72-
7376
if (recv_error == TRAP_E_FORMAT_CHANGED) {
7477
// Copy format to output interface and add PREFIX_TAG
75-
error = update_output_format(template_in, data_in, &template_out, &data_out);
78+
error = update_output_format(template_in, data_in, &template_out, &data_out);
7679
if (error) {
7780
goto cleanup;
7881
}
@@ -88,15 +91,23 @@ int prefix_tags(ipps_context_t *config) {
8891

8992
ip_addr_t src_ip = ur_get(template_in, data_in, F_SRC_IP);
9093
ip_addr_t dst_ip = ur_get(template_in, data_in, F_DST_IP);
94+
prefix_tag = 0;
95+
prefix_tag_dst = 0;
96+
97+
int src_res = is_from_configured_prefix(config, &src_ip, &prefix_tag);
98+
int dst_res = is_from_configured_prefix(config, &dst_ip, &prefix_tag_dst);
9199

92-
if ((CHECK_SRC_IP && is_from_configured_prefix(config, &src_ip, &prefix_tag)) // Misusing short-circuit evaluation
93-
|| (CHECK_DST_IP && is_from_configured_prefix(config, &dst_ip, &prefix_tag))) {
100+
if ((CHECK_SRC_IP && src_res) || (CHECK_DST_IP && dst_res)) {
94101
debug_print("tagging %d\n", prefix_tag);
95102
// data_out should have the right size since TRAP_E_FORMAT_CHANGED _had_ to be returned before getting here
96103
ur_copy_fields(template_out, data_out, template_in, data_in);
97104
// Set PREFIX_TAG field
98-
ur_set(template_out, data_out, F_PREFIX_TAG, prefix_tag);
99-
105+
if (CHECK_BOTH == 0) {
106+
ur_set(template_out, data_out, F_PREFIX_TAG, (prefix_tag!=0)?prefix_tag:prefix_tag_dst);
107+
} else {
108+
ur_set(template_out, data_out, F_PREFIX_TAG, prefix_tag);
109+
ur_set(template_out, data_out, F_PREFIX_TAG_DST, prefix_tag_dst);
110+
}
100111
uint16_t data_out_size = ur_rec_size(template_out, data_out);
101112
debug_print("data_out_size %d\n", data_out_size);
102113
int send_error = trap_send(INTERFACE_OUT, data_out, data_out_size);
@@ -146,7 +157,9 @@ int main(int argc, char **argv)
146157
case 's':
147158
CHECK_DST_IP = 0;
148159
break;
149-
160+
case 'b':
161+
CHECK_BOTH = 1;
162+
break;
150163
}
151164
}
152165

prefix_tags/prefix_tags_functions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int update_output_format(ur_template_t *template_in, const void *data_in, ur_tem
2626
}
2727

2828
// Add PREFIX_TAG field
29-
*template_out = ur_expand_template("uint32 PREFIX_TAG", *template_out);
29+
*template_out = ur_expand_template("uint32 PREFIX_TAG,uint32 PREFIX_TAG_DST", *template_out);
3030
if (*template_out == NULL) {
3131
return -1;
3232
}

0 commit comments

Comments
 (0)