summaryrefslogtreecommitdiff
path: root/plugin/business/traffic-mirror/src/entry.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/business/traffic-mirror/src/entry.cpp')
-rw-r--r--plugin/business/traffic-mirror/src/entry.cpp74
1 files changed, 51 insertions, 23 deletions
diff --git a/plugin/business/traffic-mirror/src/entry.cpp b/plugin/business/traffic-mirror/src/entry.cpp
index 9a197b5..b460489 100644
--- a/plugin/business/traffic-mirror/src/entry.cpp
+++ b/plugin/business/traffic-mirror/src/entry.cpp
@@ -7,6 +7,8 @@
#include <traffic_mirror.h>
#include <MESA/MESA_prof_load.h>
+const static struct ether_addr ether_addr_broadcast{0xff,0xff,0xff,0xff, 0xff, 0xff};
+
struct traffic_mirror_me
{
struct profile_table_ex_data * profile_ex_data;
@@ -105,6 +107,7 @@ void policy_table_ex_data_new_cb(int table_id, const char * key, const char * ta
ex_data->atomic_refcnt = 1;
ex_data->enable = 0;
ex_data->profile_id = 0;
+ ex_data->is_profile_set = 0;
json_item = cJSON_GetObjectItem(json_subroot, "enable");
if (unlikely(!json_item || !cJSON_IsNumber(json_item)))
@@ -122,11 +125,15 @@ void policy_table_ex_data_new_cb(int table_id, const char * key, const char * ta
json_item = cJSON_GetObjectItem(json_subroot, "mirror_profile");
if (unlikely(!json_item || !cJSON_IsNumber(json_item)))
{
- TFE_LOG_ERROR(instance->logger, "invalid JSON, traffic_mirror->mirror_profile not existed or invalid type.");
- goto ignore;
+ TFE_LOG_DEBUG(instance->logger, "traffic_mirror->mirror_profile not existed, user default vlan id :%d.", instance->default_vlan_id);
+ ex_data->is_profile_set = 0;
+ ex_data->profile_id = 0;
+ }
+ else
+ {
+ ex_data->is_profile_set = 1;
+ ex_data->profile_id = json_item->valueint;
}
-
- ex_data->profile_id = json_item->valueint;
success:
TFE_LOG_DEBUG(instance->logger, "traffic mirror policy, key %s: enable = %d, profile = %d",
@@ -181,7 +188,6 @@ void profile_table_ex_data_new_cb(int table_id, const char * key, const char * t
struct traffic_mirror_instance * instance = (struct traffic_mirror_instance *) argp;
assert(instance != nullptr && instance->logger != nullptr);
- const static struct ether_addr ether_addr_broadcast{0xff,0xff,0xff,0xff, 0xff, 0xff};
char * str_json = NULL;
cJSON * json_root = NULL;
cJSON * element = NULL;
@@ -403,6 +409,8 @@ static int traffic_mirror_ethdev_init(struct traffic_mirror_instance * instance)
return -1;
}
+ MESA_load_profile_uint_def(profile, "traffic_mirror", "default_vlan_id", &(instance->default_vlan_id), 0);
+
unsigned int device_type;
MESA_load_profile_uint_def(profile, "traffic_mirror", "type", &device_type, TRAFFIC_MIRROR_ETHDEV_AF_PACKET);
@@ -553,17 +561,7 @@ int traffic_mirror_on_open_cb(const struct tfe_stream * stream, unsigned int thr
goto detach;
}
- snprintf(str_profile_id, sizeof(str_policy_id), "%u", policy_ex_data->profile_id);
- profile_ex_data = (struct profile_table_ex_data *) Maat_plugin_get_EX_data(instance->maat_feather,
- instance->profile_table_id, str_profile_id);
- if (!profile_ex_data)
- {
- TFE_LOG_ERROR(instance->logger, "failed at getting policy %s's profile, profile id = %s, "
- "detach the stream", str_policy_id, str_profile_id);
- goto detach;
- }
-
- ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_SRC_MAC, (unsigned char *) &c_ether_addr,
+ ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_SRC_MAC, (unsigned char *) &c_ether_addr,
sizeof(c_ether_addr), &opt_out_size);
if (ret < 0 || memcmp(&c_ether_addr, &zero_mac, sizeof(c_ether_addr)) == 0)
{
@@ -579,14 +577,41 @@ int traffic_mirror_on_open_cb(const struct tfe_stream * stream, unsigned int thr
memcpy(&s_ether_addr, &default_dst_mac, sizeof(s_ether_addr));
}
- target_id = random() % profile_ex_data->nr_targets;
rebuild_target = ALLOC(struct traffic_mirror_rebuild_target, 1);
- rebuild_target->vlan_tci = profile_ex_data->vlans[target_id];
- rebuild_target->ether_addr = profile_ex_data->ether_addrs[target_id];
- rebuild_target->rewrite_as_target_mac = profile_ex_data->rewrite_mac;
- rebuild_target->rewrite_as_target_vlan = profile_ex_data->rewrite_vlan;
- me = ALLOC(struct traffic_mirror_me, 1);
+ if (policy_ex_data->is_profile_set)
+ {
+ snprintf(str_profile_id, sizeof(str_policy_id), "%u", policy_ex_data->profile_id);
+ profile_ex_data = (struct profile_table_ex_data *)Maat_plugin_get_EX_data(instance->maat_feather,
+ instance->profile_table_id, str_profile_id);
+ if (!profile_ex_data)
+ {
+ TFE_LOG_ERROR(instance->logger, "failed at getting policy %s's profile, profile id = %s, "
+ "detach the stream",
+ str_policy_id, str_profile_id);
+ goto detach;
+ }
+
+ target_id = random() % profile_ex_data->nr_targets;
+ rebuild_target->vlan_tci = profile_ex_data->vlans[target_id];
+ rebuild_target->ether_addr = profile_ex_data->ether_addrs[target_id];
+ rebuild_target->rewrite_as_target_mac = profile_ex_data->rewrite_mac;
+ rebuild_target->rewrite_as_target_vlan = profile_ex_data->rewrite_vlan;
+ }
+ else
+ {
+ rebuild_target->vlan_tci = instance->default_vlan_id;
+ rebuild_target->ether_addr = ether_addr_broadcast;
+ rebuild_target->rewrite_as_target_mac = 0;
+ rebuild_target->rewrite_as_target_vlan = 1;
+ }
+
+ if (rebuild_target->vlan_tci <= 0)
+ {
+ goto detach;
+ }
+
+ me = ALLOC(struct traffic_mirror_me, 1);
me->rebuild_ctx = traffic_mirror_rebuild_create(stream->addr, &c_ether_addr, &s_ether_addr,
rebuild_target, instance->ethdev);
@@ -702,7 +727,10 @@ void traffic_mirror_on_close_cb(const struct tfe_stream * stream, unsigned int t
struct traffic_mirror_me * me = (struct traffic_mirror_me *)(*pme);
traffic_mirror_rebuild_farewell(me->rebuild_ctx, thread_id);
traffic_mirror_rebuild_destroy(me->rebuild_ctx);
- profile_table_ex_data_free(me->profile_ex_data);
+ if (me->profile_ex_data)
+ {
+ profile_table_ex_data_free(me->profile_ex_data);
+ }
free(me);
*pme = NULL;