From b1abe96b06b7ea04a964123ac589eaaa400e9529 Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Mon, 20 Feb 2023 15:30:32 +0800 Subject: [PATCH] =?UTF-8?q?TSG-13842=20tsg-service-chaining-engine?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E9=93=BE=E4=B8=AD=E7=9A=84SF=E9=9C=80?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E7=AC=AC=E4=B8=89=E6=96=B9=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E7=9A=84=E7=9B=AE=E7=9A=84IP=E5=92=8C=E7=9B=AE=E7=9A=84MAC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/include/g_vxlan.h | 6 ++--- common/src/g_vxlan.cpp | 8 +++--- platform/include/health_check.h | 8 ++++++ platform/include/policy.h | 4 +++ platform/src/health_check.cpp | 20 +++++++++++++- platform/src/packet_io.cpp | 48 +++++++++++++++++---------------- platform/src/policy.cpp | 3 +++ 7 files changed, 66 insertions(+), 31 deletions(-) diff --git a/common/include/g_vxlan.h b/common/include/g_vxlan.h index 91a3df7..d360ffc 100644 --- a/common/include/g_vxlan.h +++ b/common/include/g_vxlan.h @@ -19,7 +19,7 @@ struct g_vxlan unsigned int dir_is_e2i : 1; unsigned int traffic_is_decrypted : 1; - unsigned int chain_index : 5; // max value 32 + unsigned int next_sf_index : 5; // max value 32 unsigned int online_test : 1; // Reserved 1 Bytes @@ -34,11 +34,11 @@ struct g_vxlan } __attribute__((__packed__)); void g_vxlan_set_packet_dir(struct g_vxlan *hdr, int dir_is_e2i); -void g_vxlan_set_chain_index(struct g_vxlan *hdr, int chain_index); +void g_vxlan_set_next_sf_index(struct g_vxlan *hdr, int next_sf_index); void g_vxlan_set_traffic_type(struct g_vxlan *hdr, int traffic_is_decrypted); int g_vxlan_get_packet_dir(struct g_vxlan *hdr); -int g_vxlan_get_chain_index(struct g_vxlan *hdr); +int g_vxlan_get_next_sf_index(struct g_vxlan *hdr); int g_vxlan_get_traffic_type(struct g_vxlan *hdr); // return 0 : success diff --git a/common/src/g_vxlan.cpp b/common/src/g_vxlan.cpp index a6dcdc9..1607159 100644 --- a/common/src/g_vxlan.cpp +++ b/common/src/g_vxlan.cpp @@ -9,9 +9,9 @@ void g_vxlan_set_packet_dir(struct g_vxlan *hdr, int dir_is_e2i) hdr->dir_is_e2i = (!!dir_is_e2i); } -void g_vxlan_set_chain_index(struct g_vxlan *hdr, int chain_index) +void g_vxlan_set_next_sf_index(struct g_vxlan *hdr, int next_sf_index) { - hdr->chain_index = (0x1f & chain_index); + hdr->next_sf_index = (0x1f & next_sf_index); } void g_vxlan_set_traffic_type(struct g_vxlan *hdr, int traffic_is_decrypted) @@ -24,9 +24,9 @@ int g_vxlan_get_packet_dir(struct g_vxlan *hdr) return (!!hdr->dir_is_e2i); } -int g_vxlan_get_chain_index(struct g_vxlan *hdr) +int g_vxlan_get_next_sf_index(struct g_vxlan *hdr) { - return hdr->chain_index; + return hdr->next_sf_index; } int g_vxlan_get_traffic_type(struct g_vxlan *hdr) diff --git a/platform/include/health_check.h b/platform/include/health_check.h index 777908b..5e946ca 100644 --- a/platform/include/health_check.h +++ b/platform/include/health_check.h @@ -28,6 +28,14 @@ int health_check_session_get_status(int session_id); // return -1 : key not exist int health_check_session_set_status(int session_id, int is_active); +// return 0 : success +// return -1 : key not exist +int health_check_session_get_ip(int session_id, char *ip_buff); + +// return 0 : success +// return -1 : key not exist +int health_check_session_get_mac(int session_id, char *mac_buff); + void health_check_session_foreach(); #ifdef __cpluscplus diff --git a/platform/include/policy.h b/platform/include/policy.h index 570a97b..2058158 100644 --- a/platform/include/policy.h +++ b/platform/include/policy.h @@ -97,6 +97,10 @@ struct selected_sf struct throughput_metrics rx; struct throughput_metrics tx; + + char sf_dst_ip[16]; + char sf_dst_mac[32]; + int sf_index; }; struct selected_chaining diff --git a/platform/src/health_check.cpp b/platform/src/health_check.cpp index caca405..09a1031 100644 --- a/platform/src/health_check.cpp +++ b/platform/src/health_check.cpp @@ -15,7 +15,9 @@ struct session_iterm int session_id; // key struct health_check policy; // value1: deep copy - int is_active; // value2 + int is_active; // value2 + char dst_ip[16]; // value3 + char dst_mac[32]; // value4 }; void health_check_session_init() @@ -59,6 +61,22 @@ int health_check_session_set_status(int session_id, int is_active) return 0; } +// return 0 : success +// return -1 : key not exist +int health_check_session_get_ip(int session_id, char *ip_buff) +{ + // TODO + return 0; +} + +// return 0 : success +// return -1 : key not exist +int health_check_session_get_mac(int session_id, char *mac_buff) +{ + // TODO + return 0; +} + void health_check_session_foreach() { // TODO diff --git a/platform/src/packet_io.cpp b/platform/src/packet_io.cpp index 88873e3..4485e33 100644 --- a/platform/src/packet_io.cpp +++ b/platform/src/packet_io.cpp @@ -131,11 +131,11 @@ static enum inject_pkt_action handle_inject_packet(struct packet_io *handle, mar // return 0 : success // return -1 : error -static int forward_packet_to_sf(struct packet_io *handle, marsio_buff_t *rx_buff, struct metadata *meta, int chain_index, int thread_seq, void *ctx); +static int forward_packet_to_sf(struct packet_io *handle, marsio_buff_t *rx_buff, struct metadata *meta, struct selected_sf *sf, int thread_seq, void *ctx); // return 0 : success // return -1 : error -static int forward_packet_to_nf(struct packet_io *handle, marsio_buff_t *rx_buff, struct metadata *meta, int chain_index, int thread_seq, void *ct); -static void forward_all_nf_packet_to_sf(struct packet_io *handle, marsio_buff_t *rx_buff, int thread_seq, void *ctx); +static int forward_packet_to_nf(struct packet_io *handle, marsio_buff_t *rx_buff, struct metadata *meta, int thread_seq, void *ct); +static void forward_all_nf_packet_to_sf(struct packet_io *handle, marsio_buff_t *rx_buff, struct selected_sf *sf, int thread_seq, void *ctx); static void forward_all_sf_packet_to_nf(struct packet_io *handle, marsio_buff_t *rx_buff, int thread_seq, void *ctx); // return 0 : success @@ -295,13 +295,17 @@ int packet_io_polling_nf_interface(struct packet_io *handle, int thread_seq, voi } else if (handle->config.bypass_all_traffic == 2) { + struct selected_sf sf = {0}; + sf.sf_index = 1; + memcpy(sf.sf_dst_ip, handle->config.default_dst_ip, strlen(handle->config.default_dst_ip)); + memcpy(sf.sf_dst_mac, handle->config.default_dst_mac, strlen(handle->config.default_dst_mac)); for (int j = 0; j < nr_recv; j++) { marsio_buff_t *rx_buff = rx_buffs[j]; int raw_len = marsio_buff_datalen(rx_buff); throughput_metrics_inc(&g_metrics->dev_nf_interface_rx, 1, raw_len); throughput_metrics_inc(&g_metrics->dev_endpoint_tx, 1, raw_len); - forward_all_nf_packet_to_sf(handle, rx_buff, thread_seq, ctx); + forward_all_nf_packet_to_sf(handle, rx_buff, &sf, thread_seq, ctx); } return nr_recv; } @@ -688,7 +692,7 @@ static enum raw_pkt_action handle_raw_packet(struct packet_io *handle, marsio_bu return RAW_PKT_ERR_BYPASS; } - for (int i = 0; i < chaining->chaining_size; i++) + for (int i = 0; i < chaining->chaining_used; i++) { struct selected_sf *node = &(chaining->chaining[i]); LOG_INFO("%s: session %lu %s execute policy: %d -> sff_profile_id %d -> sf_profile_id %d -> sf_need_skip %d sf_action_reason : %d", @@ -716,7 +720,7 @@ static enum raw_pkt_action handle_raw_packet(struct packet_io *handle, marsio_bu return RAW_PKT_ERR_BYPASS; } - if (forward_packet_to_sf(handle, rx_buff, &meta, i + 1, thread_seq, ctx) == 0) + if (forward_packet_to_sf(handle, rx_buff, &meta, node, thread_seq, ctx) == 0) { throughput_metrics_inc(&node->tx, 1, meta.raw_len); return RAW_PKT_HIT_FORWARD; @@ -767,7 +771,7 @@ static enum inject_pkt_action handle_inject_packet(struct packet_io *handle, mar meta.l7_offset = 0; // meta.session_id set later // meta.sids set later - int chain_index = g_vxlan_get_chain_index(g_vxlan_hdr); + int next_sf_index = g_vxlan_get_next_sf_index(g_vxlan_hdr); struct addr_tuple4 inner_addr; struct raw_pkt_parser raw_parser; @@ -800,16 +804,16 @@ static enum inject_pkt_action handle_inject_packet(struct packet_io *handle, mar } struct selected_chaining *chaining = s_ctx->chaining; - if (chaining == NULL || chain_index < 1 || chain_index > chaining->chaining_size) + if (chaining == NULL || next_sf_index < 1 || next_sf_index > chaining->chaining_used) { LOG_ERROR("%s: unexpected inject packet, session %lu %s misses chaining index, drop !!!", LOG_TAG_PKTIO, s_ctx->session_id, s_ctx->first_ctrl_pkt.addr_string); marsio_buff_free(handle->instance, &rx_buff, 1, 0, thread_seq); return INJT_PKT_ERR_DROP; } - throughput_metrics_inc(&chaining->chaining[chain_index - 1].rx, 1, meta.raw_len); + throughput_metrics_inc(&chaining->chaining[next_sf_index - 1].rx, 1, meta.raw_len); - for (int i = chain_index; i < chaining->chaining_size; i++) + for (int i = next_sf_index; i < chaining->chaining_used; i++) { struct selected_sf *node = &(chaining->chaining[i]); LOG_INFO("%s: session %lu %s execute policy: %d -> sff_profile_id %d -> sf_profile_id %d -> sf_need_skip %d sf_action_reason : %d", @@ -837,7 +841,7 @@ static enum inject_pkt_action handle_inject_packet(struct packet_io *handle, mar return INJT_PKT_ERR_DROP; } - if (forward_packet_to_sf(handle, rx_buff, &meta, i + 1, thread_seq, ctx) == 0) + if (forward_packet_to_sf(handle, rx_buff, &meta, node, thread_seq, ctx) == 0) { throughput_metrics_inc(&node->tx, 1, meta.raw_len); return INJT_PKT_HIT_FWD2SF; @@ -853,14 +857,14 @@ static enum inject_pkt_action handle_inject_packet(struct packet_io *handle, mar } } - if (chain_index != chaining->chaining_size) + if (next_sf_index != chaining->chaining_used) { LOG_ERROR("%s: unexpected inject packet, session %lu %s using invalid chaining index, drop !!!", LOG_TAG_PKTIO, s_ctx->session_id, s_ctx->first_ctrl_pkt.addr_string); marsio_buff_free(handle->instance, &rx_buff, 1, 0, thread_seq); return INJT_PKT_ERR_DROP; } - if (forward_packet_to_nf(handle, rx_buff, &meta, -1, thread_seq, ctx) == -1) + if (forward_packet_to_nf(handle, rx_buff, &meta, thread_seq, ctx) == -1) { LOG_ERROR("%s: processing inject packet, session %lu %s forwarding packet to network function failed, drop !!!", LOG_TAG_PKTIO, s_ctx->session_id, s_ctx->first_ctrl_pkt.addr_string); marsio_buff_free(handle->instance, &rx_buff, 1, 0, thread_seq); @@ -874,10 +878,8 @@ static enum inject_pkt_action handle_inject_packet(struct packet_io *handle, mar // return 0 : success // return -1 : error -static int forward_packet_to_sf(struct packet_io *handle, marsio_buff_t *rx_buff, struct metadata *meta, int chain_index, int thread_seq, void *ctx) +static int forward_packet_to_sf(struct packet_io *handle, marsio_buff_t *rx_buff, struct metadata *meta, struct selected_sf *sf, int thread_seq, void *ctx) { - // TODO get dst ip frome policy - // TODO get dst mac by dst ip marsio_buff_reset(rx_buff); struct ethhdr *eth_hdr = (struct ethhdr *)marsio_buff_prepend(rx_buff, sizeof(struct ethhdr) + sizeof(struct ip) + sizeof(struct udp_hdr) + sizeof(struct g_vxlan)); @@ -887,11 +889,11 @@ static int forward_packet_to_sf(struct packet_io *handle, marsio_buff_t *rx_buff memset(g_vxlan_hdr, 0, sizeof(struct g_vxlan)); g_vxlan_set_packet_dir(g_vxlan_hdr, meta->dir_is_e2i); - g_vxlan_set_chain_index(g_vxlan_hdr, chain_index); + g_vxlan_set_next_sf_index(g_vxlan_hdr, sf->sf_index + 1); g_vxlan_set_traffic_type(g_vxlan_hdr, meta->traffic_is_decrypted); - build_ether_header(eth_hdr, ETH_P_IP, handle->config.default_src_mac, handle->config.default_dst_mac); - build_ip_header(ip_hdr, IPPROTO_UDP, handle->config.default_src_ip, handle->config.default_dst_ip, sizeof(struct udp_hdr) + sizeof(struct g_vxlan) + meta->raw_len); + build_ether_header(eth_hdr, ETH_P_IP, handle->config.default_src_mac, sf->sf_dst_mac); + build_ip_header(ip_hdr, IPPROTO_UDP, handle->config.default_src_ip, sf->sf_dst_ip, sizeof(struct udp_hdr) + sizeof(struct g_vxlan) + meta->raw_len); build_udp_header((const char *)&ip_hdr->ip_src, 8, udp_hdr, meta->session_id % (65535 - 49152) + 49152, 4789, sizeof(struct g_vxlan) + meta->raw_len); if (marsio_send_burst(handle->dev_endpoint.mr_path, thread_seq, &rx_buff, 1) != 0) @@ -905,7 +907,7 @@ static int forward_packet_to_sf(struct packet_io *handle, marsio_buff_t *rx_buff // return 0 : success // return -1 : error -static int forward_packet_to_nf(struct packet_io *handle, marsio_buff_t *rx_buff, struct metadata *meta, int chain_index, int thread_seq, void *ct) +static int forward_packet_to_nf(struct packet_io *handle, marsio_buff_t *rx_buff, struct metadata *meta, int thread_seq, void *ct) { marsio_buff_adj(rx_buff, marsio_buff_datalen(rx_buff) - meta->raw_len); @@ -924,7 +926,7 @@ static int forward_packet_to_nf(struct packet_io *handle, marsio_buff_t *rx_buff return 0; } -static void forward_all_nf_packet_to_sf(struct packet_io *handle, marsio_buff_t *rx_buff, int thread_seq, void *ctx) +static void forward_all_nf_packet_to_sf(struct packet_io *handle, marsio_buff_t *rx_buff, struct selected_sf *sf, int thread_seq, void *ctx) { struct thread_ctx *thread = (struct thread_ctx *)ctx; struct global_metrics *g_metrics = thread->ref_metrics; @@ -990,7 +992,7 @@ static void forward_all_nf_packet_to_sf(struct packet_io *handle, marsio_buff_t // forward data forward: - if (forward_packet_to_sf(handle, rx_buff, &meta, 0, thread_seq, ctx) == 0) + if (forward_packet_to_sf(handle, rx_buff, &meta, sf, thread_seq, ctx) == 0) { LOG_ERROR("%s: processing raw packet, session %lu %s forwarding packet to service function failed, drop !!!", LOG_TAG_PKTIO, s_ctx->session_id, s_ctx->first_ctrl_pkt.addr_string); marsio_buff_free(handle->instance, &rx_buff, 1, 0, thread_seq); @@ -1057,7 +1059,7 @@ static void forward_all_sf_packet_to_nf(struct packet_io *handle, marsio_buff_t } // sendto nf - if (forward_packet_to_nf(handle, rx_buff, &meta, -1, thread_seq, ctx) == -1) + if (forward_packet_to_nf(handle, rx_buff, &meta, thread_seq, ctx) == -1) { LOG_ERROR("%s: processing inject packet, session %lu %s forwarding packet to network function failed, drop !!!", LOG_TAG_PKTIO, s_ctx->session_id, s_ctx->first_ctrl_pkt.addr_string); marsio_buff_free(handle->instance, &rx_buff, 1, 0, thread_seq); diff --git a/platform/src/policy.cpp b/platform/src/policy.cpp index 00cb5c4..a6691fa 100644 --- a/platform/src/policy.cpp +++ b/platform/src/policy.cpp @@ -1421,6 +1421,7 @@ void policy_enforce_select_chaining(struct selected_chaining *chaining, struct p item->policy_id = policy_id; item->traffic_type = chaining_param->traffic_type; item->sff_profile_id = chaining_param->sff_profile_ids[i]; + item->sf_index = chaining->chaining_used; memset(buffer, 0, sizeof(buffer)); snprintf(buffer, sizeof(buffer), "%u", item->sff_profile_id); @@ -1474,6 +1475,8 @@ void policy_enforce_select_chaining(struct selected_chaining *chaining, struct p item->sf_connectivity.int_vlan_tag = sf_param->sf_connectivity.int_vlan_tag; item->sf_connectivity.ext_vlan_tag = sf_param->sf_connectivity.ext_vlan_tag; memcpy(item->sf_connectivity.dest_ip, sf_param->sf_connectivity.dest_ip, strlen(sf_param->sf_connectivity.dest_ip)); + health_check_session_get_ip(item->sf_profile_id, item->sf_dst_ip); + health_check_session_get_mac(item->sf_profile_id, item->sf_dst_mac); chaining->chaining_used++; sf_param_free(sf_param);