diff --git a/conf/tfe/tfe.conf b/conf/tfe/tfe.conf index db18093..187b565 100644 --- a/conf/tfe/tfe.conf +++ b/conf/tfe/tfe.conf @@ -161,6 +161,7 @@ enable=1 device=eth4 # 0:TRAFFIC_MIRROR_ETHDEV_AF_PACKET; 1:TRAFFIC_MIRROR_ETHDEV_MARSIO type=1 +default_vlan_id=2 table_info=resource/pangu/table_info_traffic_mirror.conf stat_file=log/traffic_mirror.status diff --git a/plugin/business/traffic-mirror/include/traffic_mirror.h b/plugin/business/traffic-mirror/include/traffic_mirror.h index 0306ab0..aedc628 100644 --- a/plugin/business/traffic-mirror/include/traffic_mirror.h +++ b/plugin/business/traffic-mirror/include/traffic_mirror.h @@ -23,6 +23,7 @@ struct traffic_mirror_instance void * logger; unsigned int enable; unsigned int nr_threads; + unsigned int default_vlan_id; Maat_feather_t maat_feather; int policy_table_id; @@ -39,6 +40,7 @@ struct policy_table_ex_data { unsigned int atomic_refcnt; unsigned int enable; + unsigned int is_profile_set; unsigned int profile_id; }; 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 #include +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;