diff --git a/common/include/utils.h b/common/include/utils.h index 81ffd3b..58a7e44 100644 --- a/common/include/utils.h +++ b/common/include/utils.h @@ -30,12 +30,14 @@ extern "C" #define likely(expr) __builtin_expect((expr), 1) #define unlikely(expr) __builtin_expect((expr), 0) +#include +#include +#include + /****************************************************************************** * fixed_num_array ******************************************************************************/ -#include - struct fixed_num_array { uint64_t elems[128]; @@ -105,16 +107,16 @@ struct udp_hdr } __attribute__((__packed__)); void build_udp_header(const char *l3_hdr, int l3_hdr_len, struct udp_hdr *udp_hdr, uint16_t udp_sport, uint16_t udp_dport, int payload_len); -void build_ip_header(struct ip *ip_hdr, uint8_t next_protocol, uint16_t ipid, const char *src_addr, const char *dst_addr, uint16_t payload_len); -void build_ether_header(struct ethhdr *eth_hdr, uint16_t next_protocol, const char *src_mac, const char *dst_mac); +void build_ip_header(struct ip *ip_hdr, uint8_t next_protocol, uint16_t ipid, const in_addr_t src_ip, const in_addr_t dst_ip, uint16_t payload_len); +void build_ether_header(struct ethhdr *eth_hdr, uint16_t next_protocol, const u_char src_mac[], const u_char dst_mac[]); /****************************************************************************** * device ******************************************************************************/ -int get_ip_by_device_name(const char *dev_name, char *ip_buff); -int get_mac_by_device_name(const char *dev_name, char *mac_buff); -int str_to_mac(const char *str, char *mac_buff); +int get_ip_by_device_name(const char *dev_name, char *ip_str); +int get_mac_by_device_name(const char *dev_name, char *mac_str); +int str_to_mac(const char *mac_str, u_char mac[]); #define CHECKSUM_CARRY(x) (x = (x >> 16) + (x & 0xffff), (~(x + (x >> 16)) & 0xffff)) diff --git a/common/src/utils.cpp b/common/src/utils.cpp index 6ba90e3..52130e9 100644 --- a/common/src/utils.cpp +++ b/common/src/utils.cpp @@ -196,7 +196,7 @@ void build_udp_header(const char *l3_hdr, int l3_hdr_len, struct udp_hdr *udp_hd udp_hdr->uh_sum = CHECKSUM_CARRY(sum); } -void build_ip_header(struct ip *ip_hdr, uint8_t next_protocol, uint16_t ipid, const char *src_addr, const char *dst_addr, uint16_t payload_len) +void build_ip_header(struct ip *ip_hdr, uint8_t next_protocol, uint16_t ipid, const in_addr_t src_ip, const in_addr_t dst_ip, uint16_t payload_len) { memset(ip_hdr, 0, sizeof(struct ip)); @@ -206,8 +206,8 @@ void build_ip_header(struct ip *ip_hdr, uint8_t next_protocol, uint16_t ipid, co ip_hdr->ip_id = htons(ipid); /* IP ID */ ip_hdr->ip_ttl = 80; /* time to live */ ip_hdr->ip_p = next_protocol; /* transport protocol */ - ip_hdr->ip_src.s_addr = inet_addr(src_addr); - ip_hdr->ip_dst.s_addr = inet_addr(dst_addr); + ip_hdr->ip_src.s_addr = src_ip; + ip_hdr->ip_dst.s_addr = dst_ip; ip_hdr->ip_len = htons(sizeof(struct ip) + payload_len); /* total length */ ip_hdr->ip_off = htons(0); /* fragmentation flags */ ip_hdr->ip_sum = 0; /* do this later */ @@ -217,12 +217,12 @@ void build_ip_header(struct ip *ip_hdr, uint8_t next_protocol, uint16_t ipid, co } // l3_protocol: ETH_P_IPV6/ETH_P_IP -void build_ether_header(struct ethhdr *eth_hdr, uint16_t next_protocol, const char *src_mac, const char *dst_mac) +void build_ether_header(struct ethhdr *eth_hdr, uint16_t next_protocol, const u_char src_mac[], const u_char dst_mac[]) { memset(eth_hdr, 0, sizeof(struct ethhdr)); - str_to_mac(src_mac, (char *)eth_hdr->h_source); - str_to_mac(dst_mac, (char *)eth_hdr->h_dest); + memcpy(eth_hdr->h_source, src_mac, ETH_ALEN); + memcpy(eth_hdr->h_dest, dst_mac, ETH_ALEN); eth_hdr->h_proto = htons(next_protocol); } @@ -230,7 +230,7 @@ void build_ether_header(struct ethhdr *eth_hdr, uint16_t next_protocol, const ch * device ******************************************************************************/ -int get_ip_by_device_name(const char *dev_name, char *ip_buff) +int get_ip_by_device_name(const char *dev_name, char *ip_str) { int fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd == -1) @@ -248,13 +248,13 @@ int get_ip_by_device_name(const char *dev_name, char *ip_buff) return -1; } - strcpy(ip_buff, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); + strcpy(ip_str, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); close(fd); return 0; } -int get_mac_by_device_name(const char *dev_name, char *mac_buff) +int get_mac_by_device_name(const char *dev_name, char *mac_str) { int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); if (fd == -1) @@ -271,16 +271,16 @@ int get_mac_by_device_name(const char *dev_name, char *mac_buff) return -1; } - unsigned char *mac = (unsigned char *)ifr.ifr_hwaddr.sa_data; - sprintf(mac_buff, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + u_char *mac = (u_char *)ifr.ifr_hwaddr.sa_data; + sprintf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); close(fd); return 0; } -int str_to_mac(const char *str, char *mac_buff) +int str_to_mac(const char *mac_str, u_char mac[]) { - if (sscanf(str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &(mac_buff[0]), &(mac_buff[1]), &(mac_buff[2]), &(mac_buff[3]), &(mac_buff[4]), &(mac_buff[5])) == 6) + if (sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &(mac[0]), &(mac[1]), &(mac[2]), &(mac[3]), &(mac[4]), &(mac[5])) == 6) { return 0; } diff --git a/platform/include/health_check.h b/platform/include/health_check.h index 0b43536..039b777 100644 --- a/platform/include/health_check.h +++ b/platform/include/health_check.h @@ -30,7 +30,7 @@ int health_check_session_set_status(uint64_t session_id, int is_active); // return 0 : success // return -1 : key not exist -int health_check_session_get_mac(uint64_t session_id, char *mac_buff); +int health_check_session_get_mac(uint64_t session_id, u_char mac[]); #ifdef __cpluscplus } diff --git a/platform/include/policy.h b/platform/include/policy.h index 69cbaf7..7fef346 100644 --- a/platform/include/policy.h +++ b/platform/include/policy.h @@ -8,6 +8,7 @@ extern "C" #include "utils.h" #include "raw_packet.h" +#include enum traffic_type { @@ -97,8 +98,8 @@ struct selected_sf struct throughput_metrics rx; struct throughput_metrics tx; - char sf_dst_ip[16]; - char sf_dst_mac[32]; + in_addr_t sf_dst_ip; + u_char sf_dst_mac[ETH_ALEN]; int sf_index; }; diff --git a/platform/src/health_check.cpp b/platform/src/health_check.cpp index cab03c0..474c311 100644 --- a/platform/src/health_check.cpp +++ b/platform/src/health_check.cpp @@ -29,7 +29,6 @@ #define BUF_SIZE 4096 #define SEND_MAX 1 #define PACKET_SIZE 64 -#define HC_MAC_LEN 6 #define HC_DEV_NAME_LEN 16 #define HC_LOCAL_ADDRESS_LEN 64 @@ -63,7 +62,7 @@ struct node_addr { char address[64]; // key - uint8_t mac[HC_MAC_LEN]; + uint8_t mac[ETH_ALEN]; int ref_cnt; // reference UT_hash_handle hh; /* handle for first hash table */ @@ -82,7 +81,7 @@ char path[BFD_PATHLEN]; char hc_dev_name[HC_DEV_NAME_LEN]; char local_address[HC_LOCAL_ADDRESS_LEN]; char gateway_address[HC_LOCAL_ADDRESS_LEN]; -uint8_t default_gw_mac[HC_MAC_LEN]; +uint8_t default_gw_mac[ETH_ALEN]; static int get_mac_by_addr(char *addr, uint8_t *buf); static int health_check_session_foreach(); @@ -98,7 +97,7 @@ static int health_check_method_table_set_mac(struct session_table_addr *table, c return 1; } - memcpy(tmp->mac, mac, HC_MAC_LEN); + memcpy(tmp->mac, mac, ETH_ALEN); pthread_rwlock_unlock(&(table->rwlock)); return 0; } @@ -114,7 +113,7 @@ static int health_check_method_table_get_mac(struct session_table_addr *table, c return 1; } - memcpy(out_mac, tmp->mac, HC_MAC_LEN); + memcpy(out_mac, tmp->mac, ETH_ALEN); pthread_rwlock_unlock(&(table->rwlock)); return 0; } @@ -214,7 +213,7 @@ static void *_listen_arp_table(void *arg) struct in_addr ipaddr; uint8_t *mac = NULL; char str_addr[INET_ADDRSTRLEN] = {0}; - uint8_t init_mac[HC_MAC_LEN] = {0}; + uint8_t init_mac[ETH_ALEN] = {0}; start: nl_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); @@ -432,7 +431,7 @@ static uint64_t health_check_get_session_id() uint64_t health_check_session_add(int profile_id, int vsys_id, const struct health_check *policy) { uint64_t session_id = 0; - uint8_t mac[HC_MAC_LEN] = {0}; + uint8_t mac[ETH_ALEN] = {0}; struct session_iterm *tmp = NULL; if (enable == 0) @@ -585,7 +584,7 @@ static int get_mac_by_addr(char *addr, uint8_t *buf) ret = ioctl(sfd, SIOCGARP, &arp_req); if (ret == 0) - memcpy(buf, arp_req.arp_ha.sa_data, HC_MAC_LEN); + memcpy(buf, arp_req.arp_ha.sa_data, ETH_ALEN); LOG_DEBUG("IP:%s, MAC: %02x:%02x:%02x:%02x:%02x:%02x", addr, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); @@ -600,8 +599,8 @@ static void *_health_check_session_foreach(void *arg) struct bfd_vtysh_client client; struct session_iterm *tmp = NULL; struct session_iterm *node = NULL; - uint8_t mac[HC_MAC_LEN] = {0}; - uint8_t init_mac[HC_MAC_LEN] = {0}; + uint8_t mac[ETH_ALEN] = {0}; + uint8_t init_mac[ETH_ALEN] = {0}; struct sockaddr_in addr; struct timespec current_time; @@ -638,7 +637,7 @@ static void *_health_check_session_foreach(void *arg) if (node->is_active != is_active) { node->is_active = is_active; if (node->is_active == 1) { - memset(mac, 0, HC_MAC_LEN); + memset(mac, 0, ETH_ALEN); get_mac_by_addr(node->policy.address, mac); health_check_method_table_set_mac(&g_handle_bfd, node->policy.address, mac); } @@ -713,13 +712,12 @@ static const char *health_check_method_str(enum health_check_method method) // return 0 : success // return -1 : key not exist -int health_check_session_get_mac(uint64_t session_id, char *mac_buff) +int health_check_session_get_mac(uint64_t session_id, u_char mac_buff[]) { - uint8_t *p = NULL; const char *str_method = NULL; struct session_iterm *tmp = NULL; - uint8_t mac[HC_MAC_LEN] = {0}; - uint8_t init_mac[HC_MAC_LEN] = {0}; + uint8_t mac[ETH_ALEN] = {0}; + uint8_t init_mac[ETH_ALEN] = {0}; if (enable == 0) { @@ -747,17 +745,16 @@ int health_check_session_get_mac(uint64_t session_id, char *mac_buff) health_check_method_table_get_mac(&g_handle_none, tmp->policy.address, mac); } - if (memcmp(mac, init_mac, HC_MAC_LEN) == 0) { + if (memcmp(mac, init_mac, ETH_ALEN) == 0) { health_check_method_table_get_mac(&g_handle_none, gateway_address, mac); - if (memcmp(mac, init_mac, HC_MAC_LEN) == 0) { + if (memcmp(mac, init_mac, ETH_ALEN) == 0) { LOG_DEBUG("health check session id [%lu] profile id [%d] health check method [%s] get mac [null]", session_id, tmp->profile_id, str_method); pthread_rwlock_unlock(&g_handle.rwlock); return -1; } } - p = mac; - snprintf(mac_buff, 18, "%02x:%02x:%02x:%02x:%02x:%02x", p[0], p[1], p[2], p[3], p[4], p[5]); - LOG_DEBUG("health check session id [%lu] profile id [%d] health check method [%s] get mac [%s]", session_id, tmp->profile_id, str_method, mac_buff); + memcpy(mac_buff, mac, ETH_ALEN); + LOG_DEBUG("health check session id [%lu] profile id [%d] health check method [%s] get mac [%02x:%02x:%02x:%02x:%02x:%02x]", session_id, tmp->profile_id, str_method, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); pthread_rwlock_unlock(&g_handle.rwlock); return 0; } \ No newline at end of file diff --git a/platform/src/packet_io.cpp b/platform/src/packet_io.cpp index 6164bd2..0a658d0 100644 --- a/platform/src/packet_io.cpp +++ b/platform/src/packet_io.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include #include @@ -29,8 +31,11 @@ struct config char app_symbol[256]; char dev_endpoint[256]; char dev_nf_interface[256]; - char dev_endpoint_src_ip[16]; - char dev_endpoint_src_mac[32]; + char dev_endpoint_src_ip_str[16]; + char dev_endpoint_src_mac_str[32]; + + in_addr_t dev_endpoint_src_ip; + u_char dev_endpoint_src_mac[ETH_ALEN]; }; struct device @@ -395,8 +400,8 @@ static struct session_ctx *inject_packet_search_session(struct session_table *ta ******************************************************************************/ static void vxlan_encapsulate(char *buffer, - const char *src_mac_str, const char *dst_mac_str, - const char *src_ip_str, const char *dst_ip_str, + const u_char src_mac[], const u_char dst_mac[], + const in_addr_t src_ip, const in_addr_t dst_ip, int payload_len, int is_e2i, int is_decrypted, int sf_index, uint64_t session_id, uint16_t ipid) { @@ -410,8 +415,8 @@ static void vxlan_encapsulate(char *buffer, g_vxlan_set_sf_index(g_vxlan_hdr, sf_index); g_vxlan_set_traffic_type(g_vxlan_hdr, is_decrypted); - build_ether_header(eth_hdr, ETH_P_IP, src_mac_str, dst_mac_str); - build_ip_header(ip_hdr, IPPROTO_UDP, ipid, src_ip_str, dst_ip_str, sizeof(struct udp_hdr) + sizeof(struct g_vxlan) + payload_len); + build_ether_header(eth_hdr, ETH_P_IP, src_mac, dst_mac); + build_ip_header(ip_hdr, IPPROTO_UDP, ipid, src_ip, dst_ip, sizeof(struct udp_hdr) + sizeof(struct g_vxlan) + payload_len); build_udp_header((const char *)&ip_hdr->ip_src, 8, udp_hdr, session_id % (65535 - 49152) + 49152, 4789, sizeof(struct g_vxlan) + payload_len); } @@ -421,10 +426,6 @@ static int send_packet_to_sf(marsio_buff_t *rx_buff, struct metadata *meta, stru struct packet_io *packet_io = thread_ctx->ref_io; int thread_index = thread_ctx->thread_index; - const char *src_mac_str = packet_io->config.dev_endpoint_src_mac; - const char *dst_mac_str = sf->sf_dst_mac; - const char *src_ip_str = packet_io->config.dev_endpoint_src_ip; - const char *dst_ip_str = sf->sf_dst_ip; int payload_len = meta->raw_len; int is_e2i = meta->is_e2i_dir; int is_decrypted = meta->is_decrypted; @@ -438,7 +439,7 @@ static int send_packet_to_sf(marsio_buff_t *rx_buff, struct metadata *meta, stru case PACKAGE_METHOD_VXLAN_G: prepend_len = sizeof(struct ethhdr) + sizeof(struct ip) + sizeof(struct udp_hdr) + sizeof(struct g_vxlan); buffer = marsio_buff_prepend(rx_buff, prepend_len); - vxlan_encapsulate(buffer, src_mac_str, dst_mac_str, src_ip_str, dst_ip_str, payload_len, is_e2i, is_decrypted, sf_index, meta->session_id, thread_ctx->tx_packets_to_sf % 65535); + vxlan_encapsulate(buffer, packet_io->config.dev_endpoint_src_mac, sf->sf_dst_mac, packet_io->config.dev_endpoint_src_ip, sf->sf_dst_ip, payload_len, is_e2i, is_decrypted, sf_index, meta->session_id, thread_ctx->tx_packets_to_sf % 65535); break; case PACKAGE_METHOD_LAYER2_SWITCH: // TODO @@ -1140,8 +1141,8 @@ static int packet_io_config(const char *profile, struct config *config) MESA_load_profile_string_nodef(profile, "PACKET_IO", "dev_endpoint", config->dev_endpoint, sizeof(config->dev_endpoint)); MESA_load_profile_string_nodef(profile, "PACKET_IO", "dev_nf_interface", config->dev_nf_interface, sizeof(config->dev_nf_interface)); - MESA_load_profile_string_nodef(profile, "PACKET_IO", "dev_endpoint_src_ip", config->dev_endpoint_src_ip, sizeof(config->dev_endpoint_src_ip)); - MESA_load_profile_string_nodef(profile, "PACKET_IO", "dev_endpoint_src_mac", config->dev_endpoint_src_mac, sizeof(config->dev_endpoint_src_mac)); + MESA_load_profile_string_nodef(profile, "PACKET_IO", "dev_endpoint_src_ip", config->dev_endpoint_src_ip_str, sizeof(config->dev_endpoint_src_ip_str)); + MESA_load_profile_string_nodef(profile, "PACKET_IO", "dev_endpoint_src_mac", config->dev_endpoint_src_mac_str, sizeof(config->dev_endpoint_src_mac_str)); if (config->rx_burst_max > RX_BURST_MAX) { @@ -1172,10 +1173,10 @@ static int packet_io_config(const char *profile, struct config *config) LOG_DEBUG("%s: PACKET_IO->app_symbol : %s", LOG_TAG_PKTIO, config->app_symbol); LOG_DEBUG("%s: PACKET_IO->dev_endpoint : %s", LOG_TAG_PKTIO, config->dev_endpoint); LOG_DEBUG("%s: PACKET_IO->dev_nf_interface : %s", LOG_TAG_PKTIO, config->dev_nf_interface); - LOG_DEBUG("%s: PACKET_IO->dev_endpoint_src_ip : %s", LOG_TAG_PKTIO, config->dev_endpoint_src_ip); - if (strlen(config->dev_endpoint_src_mac)) + LOG_DEBUG("%s: PACKET_IO->dev_endpoint_src_ip : %s", LOG_TAG_PKTIO, config->dev_endpoint_src_ip_str); + if (strlen(config->dev_endpoint_src_mac_str)) { - LOG_DEBUG("%s: PACKET_IO->dev_endpoint_src_mac : %s (get from configuration file)", LOG_TAG_PKTIO, config->dev_endpoint_src_mac); + LOG_DEBUG("%s: PACKET_IO->dev_endpoint_src_mac : %s (get from configuration file)", LOG_TAG_PKTIO, config->dev_endpoint_src_mac_str); } return 0; @@ -1246,11 +1247,13 @@ struct packet_io *packet_io_create(const char *profile, int thread_num, cpu_set_ goto error_out; } - if (strlen(handle->config.dev_endpoint_src_mac) == 0) + if (strlen(handle->config.dev_endpoint_src_mac_str) == 0) { - marsio_get_device_ether_addr(handle->dev_endpoint.mr_dev, handle->config.dev_endpoint_src_mac, sizeof(handle->config.dev_endpoint_src_mac)); - LOG_DEBUG("%s: PACKET_IO->dev_endpoint_src_mac : %s (get from marsio api)", LOG_TAG_PKTIO, handle->config.dev_endpoint_src_mac); + marsio_get_device_ether_addr(handle->dev_endpoint.mr_dev, handle->config.dev_endpoint_src_mac_str, sizeof(handle->config.dev_endpoint_src_mac_str)); + LOG_DEBUG("%s: PACKET_IO->dev_endpoint_src_mac : %s (get from marsio api)", LOG_TAG_PKTIO, handle->config.dev_endpoint_src_mac_str); } + str_to_mac(handle->config.dev_endpoint_src_mac_str, handle->config.dev_endpoint_src_mac); + handle->config.dev_endpoint_src_ip = inet_addr(handle->config.dev_endpoint_src_ip_str); return handle; diff --git a/platform/src/policy.cpp b/platform/src/policy.cpp index 776e8d5..173d3dd 100644 --- a/platform/src/policy.cpp +++ b/platform/src/policy.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -1082,7 +1083,7 @@ static enum session_action select_sf_by_ldbc(struct policy_enforcer *enforcer, s health_check_session_id = sf_param->health_check_session_id; sf_param_free(sf_param); - memset(sf->sf_dst_mac, 0, 32); + memset(sf->sf_dst_mac, 0, sizeof(sf->sf_dst_mac)); if (health_check_session_get_mac(health_check_session_id, sf->sf_dst_mac) == 0) { ATOMIC_INC(&(g_metrics->sf_status.active)); @@ -1695,7 +1696,7 @@ void policy_enforce_select_chainings(struct policy_enforcer *enforcer, struct se item->sf_vsys_id = sf_param->sf_vsys_id; connectivity_copy(&item->sf_connectivity, &sf_param->sf_connectivity); - memcpy(item->sf_dst_ip, sf_param->sf_connectivity.dest_ip, strlen(sf_param->sf_connectivity.dest_ip)); + item->sf_dst_ip = inet_addr(sf_param->sf_connectivity.dest_ip); chaining->chaining_used++; sf_param_free(sf_param);