diff options
| author | lijie <[email protected]> | 2018-11-11 15:45:10 +0800 |
|---|---|---|
| committer | lijie <[email protected]> | 2018-11-11 15:45:10 +0800 |
| commit | 417434fbc0bde8352a17555c1964851ac3b10b9a (patch) | |
| tree | e82ccba3f6820a0218bb9d69e34fc83c32bcfe96 | |
测试完成读取配置文件功能
| -rw-r--r-- | Makefile | 7 | ||||
| -rw-r--r-- | bin/conf/mrl.conf | 33 | ||||
| -rw-r--r-- | include/mr_redis.h | 49 | ||||
| -rw-r--r-- | include/mrl_base.h | 3 | ||||
| -rw-r--r-- | include/mrl_packet.h | 2 | ||||
| -rw-r--r-- | src/Makefile | 37 | ||||
| -rw-r--r-- | src/mrl_base.c | 140 | ||||
| -rw-r--r-- | src/mrl_main.c | 368 | ||||
| -rw-r--r-- | src/mrl_packet.c | 234 | ||||
| -rw-r--r-- | src/mrl_redis.c | 247 |
10 files changed, 1120 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..eafbeb4 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +.PHONY: all clean install
+all:
+ cd src && ${MAKE}
+clean:
+ cd src && ${MAKE} clean
+install:
+ cd src && ${MAKE} install
\ No newline at end of file diff --git a/bin/conf/mrl.conf b/bin/conf/mrl.conf new file mode 100644 index 0000000..19afbea --- /dev/null +++ b/bin/conf/mrl.conf @@ -0,0 +1,33 @@ +[LOG_INFO]
+log_level=20
+log_path=./log/mrl1.log
+[SOCK_INFO]
+mrl_port=8000
+mgw_port=8001
+mgw_ip=127.0.0.1
+[MAAT_INFO]
+Maat_table_path=./conf/Maat_table_info1.conf
+Maat_max_threads=2
+Maat_redis_ip=127.1.1.1
+Maat_redis_port=8002
+Maat_stat_path=./log/Maat_stat1.log
+[HTABLE_INFO]
+ht_slots=1000
+ht_max_element_num=1
+ht_mutex_num=2
+[VXLAN_INFO]
+vxlan_gdev_num=3
+vxlan_gdev_mac=00:00:00:00:00:00,11:11:11:11:11:11,22:22:22:22:22:22
+vxlan_gdev_ip=1.1.1.1,2.2.2.2,3.3.3.3
+vxlan_gdev_port=8000,8001,8002
+vxlan_encap_type=0000,0000,0000
+vxlan_link_id=1,2,3
+vxlan_link_dir=0,0,0
+vxlan_inner_smac=33:33:33:33:33:33,44:44:44:44:44:44,55:55:55:55:55:55
+vxlan_inner_dmac=66:66:66:66:66:66,77:77:77:77:77:77,88:88:88:88:88:88
+vxlan_local_mac=AA:BB:CC:DD:EE:FF
+vxlan_local_ip=1.2.3.4
+vxlan_local_port=7999
+vxlan_inner_ip=4.3.2.1
+vxlan_inner_sport=7890
+vxlan_inner_dport=80
\ No newline at end of file diff --git a/include/mr_redis.h b/include/mr_redis.h new file mode 100644 index 0000000..63883d7 --- /dev/null +++ b/include/mr_redis.h @@ -0,0 +1,49 @@ +struct mrl_nominee_item{
+ int region_id;
+ int group_id;
+ int addr_type;
+ char src_ip[MRL_STR_IP_LEN];
+ char mask_src_ip[MRL_STR_IP_LEN];
+ char dport[MRL_STR_PORT_LEN];
+ char mask_dport[MRL_STR_PORT_LEN];
+ int protocol;
+ int direction;
+ int is_vaild;
+ int action;
+ int service;
+ char date[DATE_LEN];
+ int type;
+ int strategy_group_id;
+};
+
+struct mrl_vxlan_info{
+ char vxlan_outer_local_mac[MRL_STR_MAC_LEN];//���յ������ݰ��ı����豸MAC��ַ
+ char vxlan_outer_gdev_mac[MRL_STR_MAC_LEN];//���յ������ݰ��Ĵ����豸MAC��ַ
+ char vxlan_outer_local_ip[MRL_STR_IP_LEN];
+ char vxlan_outer_gdev_ip[MRL_STR_IP_LEN];//�����豸IP
+ char vxlan_outer_gdev_port[MRL_STR_PORT_LEN];
+ char vxlan_outer_local_port[MRL_STR_PORT_LEN];
+ unsigned char vxlan_encap_type;
+ int vxlan_link_id;
+ unsigned char vxlan_link_dir;
+ char vxlan_inner_smac[MRL_STR_MAC_LEN];
+ char vxlan_inner_dmac[MRL_STR_MAC_LEN];
+};
+struct mrl_candidate_item{
+ int candidate_id;
+ int addr_type;
+ char client_ip[MRL_STR_IP_LEN];//�ͻ���IP
+ int type;
+ int location;
+ int group_id;
+ char mrl_ip[MRL_STR_IP_LEN];
+ struct mrl_vxlan_info vxlan_info;
+};
+
+MESA_htable_handle mrl_htable_init();
+Maat_feather_t mrl_Maat_feather_init(const char * instance_name,const char *table_name, Maat_start_callback_t *start,Maat_update_callback_t *update,Maat_finish_callback_t *finish,void *u_para);
+void nominee_update_cb(int table_id,const char* table_line,void* u_para);
+void candidate_update_cb(int table_id,const char* table_line,void* u_para);
+
+
+
diff --git a/include/mrl_base.h b/include/mrl_base.h new file mode 100644 index 0000000..d7a848b --- /dev/null +++ b/include/mrl_base.h @@ -0,0 +1,3 @@ +unsigned int mrl_split_str(char *str, const char *delim, char **dest);
+
+
diff --git a/include/mrl_packet.h b/include/mrl_packet.h new file mode 100644 index 0000000..47224f7 --- /dev/null +++ b/include/mrl_packet.h @@ -0,0 +1,2 @@ +void mrl_socket_init();
+
diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..6a6a5ba --- /dev/null +++ b/src/Makefile @@ -0,0 +1,37 @@ +CC = gcc
+
+CXX = g++
+
+CCFLAGS = -Wall -g -fPIC
+
+CXXFLAGS = -Wall -g -fPIC
+
+INCLUDES = -I ../include
+
+INCLUDES += -I /opt/MESA/include
+
+INCLUDES += -I /opt/MESA/include/MESA
+
+LIBS = -L /opt/MESA/lib
+
+LIBS += -lMESA_prof_load -lMESA_handle_logger -lpthread -lmaatframe -lMESA_htable
+
+TARGET = mrl
+
+OBJS = mrl_main.o mrl_base.o
+
+.PHONY: all clean install
+
+all:$(TARGET)
+
+$(TARGET):$(OBJS)
+ $(CXX) $(LIBS) $^ -o $@
+
+$(OBJS):%.o:%.c
+ $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
+
+clean:
+ rm -rf *.o
+
+install:
+ cp $(TARGET) ../bin
diff --git a/src/mrl_base.c b/src/mrl_base.c new file mode 100644 index 0000000..c7a29a4 --- /dev/null +++ b/src/mrl_base.c @@ -0,0 +1,140 @@ +#include<string.h>
+#include<stdio.h>
+#include<assert.h>
+#include <ctype.h>
+#include "mrl_base.h"
+/* ascii�ַ�ת16���� */
+char mrl_ascii_to_hex(char ascii)
+{
+ char c = 0;
+
+ switch(ascii)
+ {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ c = ascii - 0x30;
+ break;
+
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ c = 10 + ascii - 0x61;
+ break;
+
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ c = 10 + ascii - 0x41;
+ break;
+ }
+
+ return c;
+}
+
+/* 2012-04-11 LiJia add,��MAC�ַ�����ʽת��Ϊ16����MAC��ַ.
+����:
+ str: MAC��ַ�ַ���
+ delim: �ַ����ָ���������Ϊ':', '-'��,��: xx:xx:xx:xx:xx:xx
+ ����ַ����ָ�����delim��Ϊ-1.
+ mac: �洢MAC��ַ������(ָ��),���Ϊ������,
+ ������MAC��ַΪ11:22:33:44:55:66,��mac[0]Ϊ0x11,mac[5]Ϊ0x66.
+����ֵ:
+ 0: ����
+ -1:����
+*/
+int mrl_mac_pton(const char *str, int delim, char *mac)
+{
+#define MAC_STR_LEN_DELIM (17) /* length of "11:22:33:44:55:66" */
+#define MAC_STR_LEN_NODELIM (12) /* length of "112233445566" */
+ const char *s = str;
+ int i;
+
+ /* �������Ϸ��� */
+ if(delim != -1)
+ {
+ if(strlen(str) != MAC_STR_LEN_DELIM)
+ {
+ printf("MAC string length error!\n");
+ assert(0);
+ return -1;
+ }
+ }
+ else
+ {
+ if(strlen(str) != MAC_STR_LEN_NODELIM)
+ {
+ printf("MAC string length error!\n");
+ assert(0);
+ return -1;
+ }
+ }
+
+ /* �������Ϸ��ԣ�ͬʱת����16����ֵ */
+ for(i = 0; i < 6; i++)
+ {
+ mac[i] = 0; /* �����㣬��ֵ��䶼�ǻ���� */
+ if(isxdigit(*s)==0)
+ {
+ printf("MAC string type error!\n");
+ assert(0);
+ return -1;
+ }
+ mac[i] |= mrl_ascii_to_hex(*s) << 4;
+ s++;
+
+ if(isxdigit(*s)==0)
+ {
+ printf("MAC string type error!\n");
+ assert(0);
+ return -1;
+ }
+ mac[i] |= mrl_ascii_to_hex(*s);
+ s++;
+
+ if((delim != -1) && i<5 && (*s++ != (char)delim))
+ {
+ printf("MAC string type error!\n");
+ assert(0);
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+unsigned int mrl_split_str(char *str, const char *delim, char **dest)
+{
+ unsigned int i = 0;
+ char *temp;
+ size_t len = 0;
+ temp = strtok(str, delim);
+ while (temp != NULL)
+ {
+ len = strlen(temp);
+ /* While there are tokens in "string"*/
+ if(memcpy(dest[i], temp, len) <0)
+ {
+ assert(0);
+ }
+ dest[i][len]='\0';
+ i++;
+ /* Get next token: */
+ temp = strtok(NULL, delim);
+ }
+ return i;
+}
+
diff --git a/src/mrl_main.c b/src/mrl_main.c new file mode 100644 index 0000000..3a0fe9b --- /dev/null +++ b/src/mrl_main.c @@ -0,0 +1,368 @@ +#include <stdio.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <string.h>
+#include <assert.h>
+#include <arpa/inet.h>
+#include <sys/types.h>
+
+#include "stream_inc/stream_rawpkt.h"
+#include "MESA_prof_load.h"
+#include "MESA_handle_logger.h"
+#include "MESA_htable.h"
+#include "Maat_rule.h"
+#include "Maat_command.h"
+
+#include "mrl_base.h"
+#include "mrl_redis.h"
+#include "mrl_socket.h"
+
+#define MRL_MAC_LEN 6
+#define MRL_BUFF_LEN 3000
+#define MRL_MAX_PATH 50
+#define MRL_MAX_STRING 50
+
+#define MRL_STR_MAC_LEN 18
+#define MRL_STR_PORT_LEN 6
+#define MRL_STR_IP_LEN 16
+#define MRL_STR_ENCAP_TYPE_LEN 5
+#define MRL_STR_LINK_ID_LEN 5
+#define MRL_STR_LINK_DIR_LEN 2
+
+
+#define MRL_IPV4_TYPE 4
+
+#define DATE_LEN 50
+
+#define RLOG_LV_DEBUG 10
+#define RLOG_LV_INFO 20
+#define RLOG_LV_FATAL 30
+#define CMD_LINE_NUM 1
+
+
+#define PORT_MODE (1<<10) -1
+#define PORT_MASK ((1<<15) -1)^((1<<6) -1)
+
+struct mrl_global_cfg
+{
+ //log info
+ char mrl_log_path[MRL_MAX_PATH];
+ int mrl_log_level;
+
+ //socket info
+ char mgw_ip[MRL_STR_IP_LEN];
+ unsigned short mgw_port;//mgw listen udp port
+ unsigned short mrl_port;//mrl listen udp port
+
+ //maat info
+ char Maat_table_path[MRL_MAX_PATH];
+ char Maat_redis_ip[MRL_STR_IP_LEN];
+ unsigned short Maat_redis_port;
+ char Maat_stat_path[MRL_MAX_PATH];
+ unsigned int Maat_max_threads;
+
+ //htable info
+ unsigned int ht_slots;
+ unsigned int ht_max_element_num;
+ int ht_mutex_num;
+
+ /*vxlan info*/
+ unsigned int vxlan_gdev_num;
+ char **vxlan_outer_gdev_mac;
+ char vxlan_outer_local_mac[MRL_STR_MAC_LEN];
+ char **vxlan_outer_gdev_ip;
+ char vxlan_outer_local_ip[MRL_STR_IP_LEN];
+ char **vxlan_outer_gdev_port;
+ char xvlan_outer_local_port[MRL_STR_PORT_LEN];
+ char **vxlan_encap_type;
+ char **vxlan_link_id;
+ char **vxlan_link_dir;
+ char **vxlan_inner_smac;
+ char **vxlan_inner_dmac;
+
+ char vxlan_inner_dip[MRL_STR_IP_LEN];
+ char vxlan_inner_dport[MRL_STR_PORT_LEN];
+ char vxlan_inner_sport[MRL_STR_PORT_LEN];
+};
+
+struct mrl_global_instance
+{
+ void *mrl_log_handle;
+ struct mrl_global_cfg mrl_cfg;
+
+ Maat_feather_t nominee_feather;
+ Maat_feather_t candidate_feather;
+
+ MESA_htable_handle ht_nominee;
+ MESA_htable_handle ht_candidate;
+
+ int *mrl_snd_fd;
+ int mrl_rcv_fd;
+ struct sockaddr_in mgw_addr;
+};
+
+struct mrl_global_instance mrl_instance;
+
+void mrl_cfg_init(const char *profile)
+{
+ char *temp_mac_str;
+ char *temp_port_str;
+ char *temp_ip_str;
+ char *temp_encap_type_str;
+ char *temp_link_id_str;
+ char *temp_link_dir_str;
+
+ unsigned int split_num = 0;
+ short temp_port = 0;
+ unsigned int i=0;
+
+ /********** load log info **********/
+ MESA_load_profile_int_def(profile, "LOG_INFO", "log_level", &(mrl_instance.mrl_cfg.mrl_log_level),RLOG_LV_DEBUG);
+ MESA_load_profile_string_def(profile, "LOG_INFO", "log_path", mrl_instance.mrl_cfg.mrl_log_path, MRL_MAX_PATH, "./log/mrl.log");
+
+
+ /********** load sock info **********/
+ MESA_load_profile_short_def(profile, "SOCK_INFO", "mrl_port",&(temp_port),8888);
+ assert(temp_port > 0 && temp_port <= 65535);
+ mrl_instance.mrl_cfg.mrl_port = temp_port;
+
+ MESA_load_profile_short_def(profile, "SOCK_INFO", "mgw_port",&(temp_port),8888);
+ assert(temp_port > 0 && temp_port <= 65535);
+ mrl_instance.mrl_cfg.mgw_port = temp_port;
+
+ MESA_load_profile_string_def(profile, "SOCK_INFO", "mgw_ip", mrl_instance.mrl_cfg.mgw_ip, MRL_STR_IP_LEN, "0.0.0.0");
+
+
+ /********** load maat info **********/
+ MESA_load_profile_string_def(profile, "MAAT_INFO", "Maat_table_path", mrl_instance.mrl_cfg.Maat_table_path, MRL_MAX_PATH, "./conf/Maat_table_info.conf");
+ MESA_load_profile_uint_def(profile, "MAAT_INFO", "Maat_max_threads", &(mrl_instance.mrl_cfg.Maat_max_threads), 1);
+ MESA_load_profile_string_def(profile, "MAAT_INFO", "Maat_redis_ip", mrl_instance.mrl_cfg.Maat_redis_ip, MRL_STR_IP_LEN, "127.0.0.1");
+ MESA_load_profile_short_def(profile, "MAAT_INFO", "Maat_redis_port", &(temp_port), 6379);
+ assert(temp_port > 0 && temp_port <= 65535);
+ mrl_instance.mrl_cfg.Maat_redis_port = (unsigned short)temp_port;
+ MESA_load_profile_string_def(profile, "MAAT_INFO", "Maat_stat_path", mrl_instance.mrl_cfg.Maat_stat_path,MRL_MAX_PATH, "./log/Maat_stat.log");
+
+
+ /********** load htable info *********/
+ MESA_load_profile_uint_def(profile, "HTABLE_INFO", "ht_slots", &(mrl_instance.mrl_cfg.ht_slots), 1048576);
+ MESA_load_profile_uint_def(profile, "HTABLE_INFO", "ht_max_element_num", &(mrl_instance.mrl_cfg.ht_max_element_num), 0);
+ MESA_load_profile_int_def(profile, "HTABLE_INFO", "ht_mutex_num", &(mrl_instance.mrl_cfg.ht_mutex_num), 1);
+
+
+ /********** load gdev info **********/
+ MESA_load_profile_uint_def(profile, "VXLAN_INFO", "vxlan_gdev_num", &(mrl_instance.mrl_cfg.vxlan_gdev_num), 1);
+
+ temp_mac_str = (char *)calloc(mrl_instance.mrl_cfg.vxlan_gdev_num * MRL_STR_MAC_LEN,sizeof(char));
+ temp_ip_str = (char *)calloc(mrl_instance.mrl_cfg.vxlan_gdev_num * MRL_STR_IP_LEN,sizeof(char));
+ temp_port_str = (char *)calloc(mrl_instance.mrl_cfg.vxlan_gdev_num * MRL_STR_PORT_LEN, sizeof(char));
+ temp_encap_type_str = (char *)calloc(mrl_instance.mrl_cfg.vxlan_gdev_num * MRL_STR_ENCAP_TYPE_LEN,sizeof(char));
+ temp_link_id_str = (char *)calloc(mrl_instance.mrl_cfg.vxlan_gdev_num * MRL_STR_LINK_ID_LEN,sizeof(char));
+ temp_link_dir_str = (char *)calloc(mrl_instance.mrl_cfg.vxlan_gdev_num * MRL_STR_LINK_DIR_LEN, sizeof(char));
+
+ MESA_load_profile_string_def(profile, "VXLAN_INFO", "vxlan_gdev_mac",temp_mac_str, mrl_instance.mrl_cfg.vxlan_gdev_num*MRL_STR_MAC_LEN, "00:00:00:00:00:00");
+ printf("temp mac string is %s\n",temp_mac_str);
+ mrl_instance.mrl_cfg.vxlan_outer_gdev_mac = (char **)malloc(sizeof(char *)*mrl_instance.mrl_cfg.vxlan_gdev_num);
+ for(i=0;i<mrl_instance.mrl_cfg.vxlan_gdev_num;i++)
+ {
+ mrl_instance.mrl_cfg.vxlan_outer_gdev_mac[i] = (char *)malloc(sizeof(char)*MRL_STR_MAC_LEN);
+ }
+ split_num = mrl_split_str(temp_mac_str,",",mrl_instance.mrl_cfg.vxlan_outer_gdev_mac);
+ assert(split_num == mrl_instance.mrl_cfg.vxlan_gdev_num);
+
+ MESA_load_profile_string_def(profile, "VXLAN_INFO", "vxlan_gdev_ip",temp_ip_str, mrl_instance.mrl_cfg.vxlan_gdev_num*MRL_STR_IP_LEN, "0.0.0.0");
+ mrl_instance.mrl_cfg.vxlan_outer_gdev_ip = (char **)malloc(sizeof(char *)*mrl_instance.mrl_cfg.vxlan_gdev_num);
+ for(i=0;i<mrl_instance.mrl_cfg.vxlan_gdev_num;i++)
+ {
+ mrl_instance.mrl_cfg.vxlan_outer_gdev_ip[i] = (char *)malloc(sizeof(char)*MRL_STR_IP_LEN);
+ }
+ split_num = mrl_split_str(temp_ip_str,",",mrl_instance.mrl_cfg.vxlan_outer_gdev_ip);
+ assert(split_num == mrl_instance.mrl_cfg.vxlan_gdev_num);
+
+ MESA_load_profile_string_def(profile, "VXLAN_INFO", "vxlan_gdev_port",temp_port_str, mrl_instance.mrl_cfg.vxlan_gdev_num*MRL_STR_PORT_LEN, "8888");
+ mrl_instance.mrl_cfg.vxlan_outer_gdev_port = (char **)malloc(sizeof(char *)*mrl_instance.mrl_cfg.vxlan_gdev_num);
+ for(i=0;i<mrl_instance.mrl_cfg.vxlan_gdev_num;i++)
+ {
+ mrl_instance.mrl_cfg.vxlan_outer_gdev_port[i] = (char *)malloc(sizeof(char)*MRL_STR_PORT_LEN);
+ }
+ split_num = mrl_split_str(temp_port_str,",",mrl_instance.mrl_cfg.vxlan_outer_gdev_port);
+ assert(split_num == mrl_instance.mrl_cfg.vxlan_gdev_num);
+
+ MESA_load_profile_string_def(profile, "VXLAN_INFO", "vxlan_encap_type",temp_encap_type_str, mrl_instance.mrl_cfg.vxlan_gdev_num*MRL_STR_ENCAP_TYPE_LEN, "0000");
+ mrl_instance.mrl_cfg.vxlan_encap_type = (char **)malloc(sizeof(char *)*mrl_instance.mrl_cfg.vxlan_gdev_num);
+ for(i=0;i<mrl_instance.mrl_cfg.vxlan_gdev_num;i++)
+ {
+ mrl_instance.mrl_cfg.vxlan_encap_type[i] = (char *)malloc(sizeof(char)*MRL_STR_ENCAP_TYPE_LEN);
+ }
+ split_num = mrl_split_str(temp_encap_type_str,",",mrl_instance.mrl_cfg.vxlan_encap_type);
+ assert(split_num == mrl_instance.mrl_cfg.vxlan_gdev_num);
+
+ MESA_load_profile_string_def(profile, "VXLAN_INFO", "vxlan_link_id",temp_link_id_str, mrl_instance.mrl_cfg.vxlan_gdev_num*MRL_STR_LINK_ID_LEN, "0");
+ mrl_instance.mrl_cfg.vxlan_link_id= (char **)malloc(sizeof(char *)*mrl_instance.mrl_cfg.vxlan_gdev_num);
+ for(i=0;i<mrl_instance.mrl_cfg.vxlan_gdev_num;i++)
+ {
+ mrl_instance.mrl_cfg.vxlan_link_id[i] = (char *)malloc(sizeof(char)*MRL_STR_LINK_ID_LEN);
+ }
+ split_num = mrl_split_str(temp_link_id_str,",",mrl_instance.mrl_cfg.vxlan_link_id);
+ assert(split_num == mrl_instance.mrl_cfg.vxlan_gdev_num);
+
+ MESA_load_profile_string_def(profile, "VXLAN_INFO", "vxlan_link_dir",temp_link_dir_str, mrl_instance.mrl_cfg.vxlan_gdev_num*MRL_STR_LINK_DIR_LEN, "0");
+ mrl_instance.mrl_cfg.vxlan_link_dir= (char **)malloc(sizeof(char *)*mrl_instance.mrl_cfg.vxlan_gdev_num);
+ for(i=0;i<mrl_instance.mrl_cfg.vxlan_gdev_num;i++)
+ {
+ mrl_instance.mrl_cfg.vxlan_link_dir[i] = (char *)malloc(sizeof(char)*MRL_STR_LINK_DIR_LEN);
+ }
+ split_num = mrl_split_str(temp_link_dir_str,",",mrl_instance.mrl_cfg.vxlan_link_dir);
+ assert(split_num == mrl_instance.mrl_cfg.vxlan_gdev_num);
+
+ MESA_load_profile_string_def(profile, "VXLAN_INFO", "vxlan_inner_smac",temp_mac_str, mrl_instance.mrl_cfg.vxlan_gdev_num*MRL_STR_MAC_LEN, "00:00:00:00:00:00");
+ mrl_instance.mrl_cfg.vxlan_inner_smac = (char **)malloc(sizeof(char *)*mrl_instance.mrl_cfg.vxlan_gdev_num);
+ for(i=0;i<mrl_instance.mrl_cfg.vxlan_gdev_num;i++)
+ {
+ mrl_instance.mrl_cfg.vxlan_inner_smac[i] = (char *)malloc(sizeof(char)*MRL_STR_MAC_LEN);
+ }
+ split_num = mrl_split_str(temp_mac_str,",",mrl_instance.mrl_cfg.vxlan_inner_smac);
+ assert(split_num == mrl_instance.mrl_cfg.vxlan_gdev_num);
+
+ MESA_load_profile_string_def(profile, "VXLAN_INFO", "vxlan_inner_dmac",temp_mac_str, mrl_instance.mrl_cfg.vxlan_gdev_num*MRL_STR_MAC_LEN, "00:00:00:00:00:00");
+ mrl_instance.mrl_cfg.vxlan_inner_dmac = (char **)malloc(sizeof(char *)*mrl_instance.mrl_cfg.vxlan_gdev_num);
+ for(i=0;i<mrl_instance.mrl_cfg.vxlan_gdev_num;i++)
+ {
+ mrl_instance.mrl_cfg.vxlan_inner_dmac[i] = (char *)malloc(sizeof(char)*MRL_STR_MAC_LEN);
+ }
+ split_num = mrl_split_str(temp_mac_str,",",mrl_instance.mrl_cfg.vxlan_inner_dmac);
+ assert(split_num == mrl_instance.mrl_cfg.vxlan_gdev_num);
+
+ MESA_load_profile_string_def(profile, "VXLAN_INFO", "vxlan_local_mac", mrl_instance.mrl_cfg.vxlan_outer_local_mac, MRL_STR_MAC_LEN, "00:00:00:00:00:00");
+ MESA_load_profile_string_def(profile, "VXLAN_INFO", "vxlan_local_ip", mrl_instance.mrl_cfg.vxlan_outer_local_ip, MRL_STR_IP_LEN, "0.0.0.0");
+ MESA_load_profile_string_def(profile, "VXLAN_INFO", "vxlan_local_port", mrl_instance.mrl_cfg.xvlan_outer_local_port,MRL_STR_PORT_LEN, "8888");
+
+ MESA_load_profile_string_def(profile, "VXLAN_INFO", "vxlan_inner_ip",mrl_instance.mrl_cfg.vxlan_inner_dip, MRL_STR_IP_LEN, "0.0.0.0");
+ MESA_load_profile_string_def(profile, "VXLAN_INFO", "vxlan_inner_sport", mrl_instance.mrl_cfg.vxlan_inner_sport,MRL_STR_PORT_LEN, "8888");
+ MESA_load_profile_string_def(profile, "VXLAN_INFO", "vxlan_inner_dport", mrl_instance.mrl_cfg.vxlan_inner_dport,MRL_STR_PORT_LEN, "8888");
+
+ free(temp_mac_str);
+ free(temp_ip_str);
+ free(temp_port_str);
+ free(temp_encap_type_str);
+ free(temp_link_id_str);
+ free(temp_link_dir_str);
+}
+
+
+extern "C" int mrl_init(void)
+{
+
+ mrl_cfg_init("./conf/mrl.conf");
+
+ mrl_instance.mrl_log_handle = MESA_create_runtime_log_handle(mrl_instance.mrl_cfg.mrl_log_path, mrl_instance.mrl_cfg.mrl_log_level);
+ if(mrl_instance.mrl_log_handle == NULL)
+ {
+ assert(0);
+ }
+ return 1;
+
+ mrl_instance.ht_nominee = mrl_htable_init();
+ mrl_instance.ht_candidate = mrl_htable_init();
+
+
+ mrl_instance.nominee_feather = mrl_Maat_feather_init("mrl_nominee",IR_NOMINEE_IP_TABLE_NAME,NULL,nominee_update_cb,NULL,NULL);
+ mrl_instance.candidate_feather = mrl_Maat_feather_init("mrl_candidate",IR_CANDIDATE_IP_TABLE_NAME,NULL,candidate_update_cb,NULL,NULL);
+
+ mrl_socket_init();
+ //TODO:read from nominee table and candidate table ifno and construct the packet
+ //send to the speficied destination
+
+ //���������߳̽��ղ�����mwg���͵����ݰ�
+ pthread_t pid;
+ if(pthread_create(&pid,NULL,mrl_recv_mgw_action,NULL) <0)
+ {
+ MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"mrl_init","pthread_create func error!");
+ assert(0);
+ }
+ pthread_detach(pid);
+}
+
+int main()
+{
+ mrl_init();
+ return 1;
+}
+
+/*
+extern "C" void mrl_destroy(void)
+{
+ MESA_destroy_runtime_log_handle(instance.logger);
+ Maat_burn_feather(instance.maat_father);
+ free(mrl_instance.mrl_cfg);
+}
+
+extern "C" char mrl_tcpall_entry(struct streaminfo *a_tcp,void **pme, int thread_seq, void *raw_packet)
+{
+ char ret = APP_STATE_GIVEME;
+ switch(a_tcp->pktstate){
+ case OP_STATE_PENDING:
+ if(mrl_pkt_signature_identify(a_tcp))
+ {
+ mrl_send_to_mgw(raw_packet, thread_seq);
+ ret = APP_STATE_GIVEME;//��ָ��faw��drop���ȡ�ĸ�������
+ }
+ else
+ {
+
+ ret = APP_STATE_DROPME|APP_STATE_FAWPKT;//�����ǰIP���Ƽ����е�IP����ô�������ݰ����ø��ң����һ���ת��
+ }
+ break;
+
+ case OP_STATE_DATA:
+ mrl_send_to_mgw(raw_packet, thread_seq);
+ ret = APP_STATE_GIVEME;
+ break;
+
+ case OP_STATE_CLOSE:
+ mrl_send_to_mgw(raw_packet, thread_seq);
+ ret = APP_STATE_DROPME;
+ break;
+
+ default:
+ ret = APP_STATE_DROPME | APP_STATE_FAWPKT;
+ break;
+ }
+ return ret;
+}
+
+
+extern "C" char mrl_udp_entry(struct streaminfo *a_udp, void **pme, int thread_seq, void *raw_packet)
+{
+ char ret = APP_STATE_GIVEME;
+ struct mrl_nominee_item *nominee_item = NULL;
+ switch(a_udp->pktstate){
+ case OP_STATE_PENDING:
+ if(mrl_pkt_signature_identify(a_udp))
+ {
+ mrl_send_to_mgw(raw_packet, thread_seq);
+ ret = APP_STATE_GIVEME;
+ }
+ else
+ {
+ mrl_identify_nominee(a_udp);
+ ret =APP_STATE_DROPME;
+ }
+ break;
+ case OP_STATE_DATA:
+ mrl_send_to_mgw(raw_packet, thread_seq);
+ ret = APP_STATE_GIVEME;
+ break;
+
+ case OP_STATE_CLOSE:
+ mrl_send_to_mgw(raw_packet, thread_seq);
+ ret = APP_STATE_DROPME;
+ break;
+
+ default:
+ ret = APP_STATE_DROPME | APP_STATE_FAWPKT;
+ break;
+ }
+ return ret;
+}
+*/
+
diff --git a/src/mrl_packet.c b/src/mrl_packet.c new file mode 100644 index 0000000..8d1aa2a --- /dev/null +++ b/src/mrl_packet.c @@ -0,0 +1,234 @@ +void mrl_socket_init()
+{
+ //init send fd
+ int i;
+ int thread_num = 0;
+ int len = sizeof(thread_num);
+ sapp_get_platform_opt(SPO_THREAD_COUNT,(void *) &thread_num, &len);
+ mrl_instance.mrl_rcv_fd = (int *) malloc (sizeof(int)*thread_num);
+ for(i = 0; i< thread_num;i++)
+ {
+ mrl_instance.mrl_rcv_fd = socket(AF_INET, SOCK_DGRAM, 0);
+ }
+
+ //init destnation sock addr
+ mrl_instance.mgw_addr.sin_family = AF_INET;
+ inet_pton(AF_INET, mrl_instance.mrl_cfg.mgw_ip, &(mrl_instance.mgw_addr.sin_addr));
+ mrl_instance.mgw_addr.sin_port = htons(mrl_instance.mrl_cfg.mgw_port);
+
+ //init recv fd
+ struct sockaddr_in addr;
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = htonl(INADDR_ANY);
+ addr.sin_port = htons(mrl_instance.mrl_cfg.mrl_port);
+ mrl_instance.mrl_rcv_fd = socket(AF_INET, SOCK_DGRAM, 0);
+ bind(mrl_instance.mrl_rcv_fd , (struct sockaddr *)(&addr), sizeof(addr));
+
+
+}
+
+ssize_t mrl_sock_send(int fd, const char *pkt, ssize_t pkt_len, struct sockaddr_in *dest)
+{
+ ssize_t send_len = 0;
+ send_len = sendto(fd, pkt,pkt_len,0,(struct sockaddr *)dest,sizeof(struct sockaddr_in));
+ return send_len;
+}
+
+ssize_t mrl_sock_recv(int fd, char *recv_buff, size_t buff_len)
+{
+ struct sockaddr_in from;
+ socklen_t from_len = sizeof(from);
+ ssize_t recv_len = 0;
+ recv_len = recvfrom(fd, recv_buff, buff_len, 0, (struct sockaddr *) &from, & from_len);
+ return recv_len ;
+}
+
+
+void mrl_send_to_gdev(int thread_seq, struct mrl_vxlan_info * vxlan_info, const char *payload, size_t payload_len)
+{
+ SAPP_TLV_T option[32];
+ int option_num = 0;
+ int vxlan_link_id = 1;
+
+ char temp_mac[MRL_MAC_LEN];
+ unsigned int temp_ip;
+ short temp_port;
+
+ memset(temp_mac, 0, MRL_MAC_LEN);
+ mrl_mac_pton(vxlan_info->vxlan_outer_local_mac, ':', temp_mac);
+ option[option_num].type = SAPP_SEND_OPT_GDEV_SMAC;
+ option[option_num].length = MRL_MAC_LEN;
+ memcpy(option[option_num].array_value, temp_mac, MRL_MAC_LEN);
+ option_num++;////////
+
+ memset(temp_mac, 0, MRL_MAC_LEN);
+ mrl_mac_pton(vxlan_info->vxlan_outer_gdev_mac, ':', temp_mac);
+ option[option_num].type = SAPP_SEND_OPT_GDEV_DMAC;
+ option[option_num].length = MRL_MAC_LEN;
+ memcpy(option[option_num].array_value, temp_mac, MRL_MAC_LEN);
+ option_num++;///////
+
+ inet_pton(AF_INET, vxlan_info->vxlan_outer_local_ip, &temp_ip);
+ option[option_num].type = SAPP_SEND_OPT_GDEV_SIP;
+ option[option_num].length = 4;
+ option[option_num].int_value = temp_ip;
+ option_num++;///////
+
+
+ inet_pton(AF_INET, vxlan_info->vxlan_outer_gdev_ip, &temp_ip);
+ option[option_num].type = SAPP_SEND_OPT_GDEV_DIP;
+ option[option_num].length = 4;
+ option[option_num].int_value = temp_ip;
+ option_num++;///////
+
+
+ temp_port = atoi(vxlan_info->vxlan_outer_local_port);
+ assert(temp_port >0 && temp_port <= 65535);
+ option[option_num].type = SAPP_SEND_OPT_GDEV_UDP_SPORT;
+ option[option_num].length = 2;
+ option[option_num].short_value = temp_port;
+ option_num++;///////
+
+
+ temp_port = atoi(vxlan_info->vxlan_outer_gdev_port);
+ assert(temp_port >0 && temp_port <= 65535);
+ option[option_num].type = SAPP_SEND_OPT_GDEV_UDP_DPORT;
+ option[option_num].length = 2;
+ option[option_num].short_value = temp_port;
+ option_num++;///////
+
+
+ option[option_num].type = SAPP_SEND_OPT_VXLAN_VPN_ID;
+ option[option_num].length = 4;
+ option[option_num].int_value = 0;
+ option_num++;///////
+
+
+ option[option_num].type = SAPP_SEND_OPT_VXLAN_LINK_ID;
+ option[option_num].length = 4;
+ option[option_num].int_value = vxlan_info.vxlan_link_id;
+ option_num++;///////
+
+
+ option[option_num].type = SAPP_SEND_OPT_VXLAN_LINK_ENCAP_TYPE;
+ option[option_num].length = 1;
+ option[option_num].char_value = vxlan_info.vxlan_encap_type;
+ option_num++;///////
+
+
+ option[option_num].type = SAPP_SEND_OPT_VXLAN_LINK_DIR;
+ option[option_num].length = 1;
+ option[option_num].char_value = vxlan_info.vxlan_link_dir;
+ option_num++;///////
+
+
+ option[option_num].type = SAPP_SEND_OPT_INNER_LINK_ENCAP_TYPE;
+ option[option_num].length = 1;
+ option[option_num].char_value = TUNNAT_TUNNEL_TYPE_ETHER;
+ option_num++;///////
+
+
+ memset(temp_mac, 0, MRL_MAC_LEN);
+ mrl_mac_pton(vxlan_info->vxlan_inner_smac, ':', temp_mac);
+ option[option_num].type = SAPP_SEND_OPT_INNER_SMAC;
+ option[option_num].length = MRL_MAC_LEN;
+ memcpy(option[option_num].array_value, temp_mac, MRL_MAC_LEN);
+ option_num++;///////
+
+
+ memset(temp_mac, 0, MRL_MAC_LEN);
+ mrl_mac_pton(vxlan_info->vxlan_inner_dmac, ':', temp_mac);
+ option[option_num].type = SAPP_SEND_OPT_INNER_DMAC;
+ option[option_num].length = MRL_MAC_LEN;
+ memcpy(option[option_num].array_value, temp_mac, MRL_MAC_LEN);
+ option_num++;///////
+
+
+ MESA_sendpacket_iplayer_options(thread_seq,
+ payload,
+ payload_len,
+ vxlan_info.vxlan_link_dir,
+ option,
+ option_num);
+}
+
+void mrl_get_candidate_vxlan_info(struct streaminfo *mystream, struct mrl_vxlan_info *vxlan)
+{
+ int gdev_ip = 0;
+ get_rawpkt_opt_from_streaminfo(mystream, RAW_PKT_GET_GDEV_IP, &(gdev_ip));
+ inet_ntop(AF_INET, &gdev_ip, vxlan->vxlan_outer_gdev_ip, MRL_STR_IP_LEN);
+ int index = 0;
+ for(; index < mrl_instance.mrl_cfg.gdev_num; index++)
+ {
+ if(memcmp(vxlan->vxlan_outer_gdev_ip,mrl_instance.mrl_cfg.gdev_ip[index],strlen(vxlan->vxlan_outer_gdev_ip)) == 0)
+ {
+ break;
+ }
+ }
+ assert(index < mrl_instance.mrl_cfg.gdev_num);//������ھ�˵�������ݰ���GDEV��������̽���GDEV�У�����
+ memcpy(vxlan->vxlan_inner_smac, mrl_instance.mrl_cfg.vxlan_inner_smacs[index],strlen(mrl_instance.mrl_cfg.vxlan_inner_smacs[index]));
+ memcpy(vxlan->vxlan_inner_dmac, mrl_instance.mrl_cfg.vxlan_inner_dmacs[index], strlen(mrl_instance.mrl_cfg.vxlan_inner_dmacs[index]));
+ vxlan->vxlan_encap_type = mrl_instance.mrl_cfg.vxlan_encap_types[index];
+ vxlan->vxlan_link_id = mrl_instance.mrl_cfg.vxlan_link_ids[index];
+ vxlan->vxlan_link_dir = mrl_instance.mrl_cfg.vxlan_link_dirs[index];
+ vxlan->vxlan_outer_local_port = mrl_instance.mrl_cfg.xvlan_outer_locals_port[index];
+ vxlan->vxlan_outer_gdev_port = mrl_instance.mrl_cfg.vxlan_outer_gdevs_port[index];
+ memcpy(vxlan->vxlan_outer_local_ip, mrl_instance.mrl_cfg.vxlan_outer_locals_ip[index],strlen(mrl_instance.mrl_cfg.vxlan_outer_locals_ip[index]));
+ memcpy(vxlan->vxlan_outer_gdev_ip, mrl_instance.mrl_cfg.vxlan_outer_gdevs_ip[index],strlen(mrl_instance.mrl_cfg.vxlan_outer_gdevs_ip[index]));
+ memcpy(vxlan->vxlan_outer_local_mac, mrl_instance.mrl_cfg.vxlan_outer_locals_mac[index],strlen(mrl_instance.mrl_cfg.vxlan_outer_locals_mac[index]));
+ memcpy(vxlan->vxlan_outer_gdev_mac, mrl_instance.mrl_cfg.vxlan_outer_gdevs_mac[index],strlen(mrl_instance.mrl_cfg.vxlan_outer_gdevs_mac[index]));
+ MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_DEBUG,"mrl_get_candidate_vxlan_info","get vxlan opt from rawpkt:
+ inner_smac:%s, inner_dmac:%s,encap_type:%d,link_id:%d, link_dir:%d,
+ local_port:%u,gdev_port:%u, local_ip:%s,gdev_ip:%s,local_mac:%s, gdev_mac:%s",
+ vxlan->vxlan_inner_smac, vxlan->vxlan_inner_dmac, vxlan->vxlan_encap_type,
+ vxlan->vxlan_link_id, vxlan->vxlan_link_dir,vxlan->vxlan_outer_local_port,
+ vxlan->vxlan_outer_gdev_port,vxlan->vxlan_outer_local_ip,vxlan->vxlan_outer_gdev_ip,
+ vxlan->vxlan_outer_local_mac,vxlan->vxlan_outer_gdev_mac);
+
+}
+
+
+//�ж��Ƿ���IP���ð�
+bool mrl_pkt_signature_identify(struct streaminfo *mystream)
+{
+ UINT16 sport,dport;
+ UINT32 sip,dip;
+ sport = ntohs(mystream->addr.tuple4_v4->source);
+ dport = ntohs(mystream->addr.tuple4_v4->dest);
+ sip = ntohl(mystream->addr.tuple4_v4->saddr);
+ dip = ntohl(mystream->addr.tuple4_v4->daddr);
+ if(((sport ^ sip ^dip)%PORT_MODE) == (dport & PORT_MASK))
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+}
+
+
+void *mrl_recv_mgw_action(void *arg)
+{
+
+ char rcv_buff[MRL_BUFF_LEN];
+ ssize_t recv_len = 0;
+ int thread_seq = 0;
+ int temp_len = sizeof(thread_seq);
+ sapp_get_platform_opt(SPO_REQUEST_SEND_THREAD_ID, &thread_seq, &temp_len);
+ recv_len = mrl_sock_recv(mrl_instance.mgw_rcv_fd,rcv_buff, MRL_BUFF_LEN);
+ struct mrl_vxlan_info *vxlan_info = (struct mrl_vxlan_info *)rcv_buff;
+ size_t vxlan_len = sizeof(struct mrl_vxlan_info);
+ mrl_send_to_gdev(thread_seq,vxlan_info,rcv_buff + vxlan_len, recv_len - vxlan_len);
+}
+
+void mrl_send_to_mgw(void *raw_pkt, int thread_seq)
+{
+ const char *pkt = (const char *)raw_pkt;
+ ssize_t pkt_len = strlen(pkt);
+ ssize_t send_len = 0;
+ send_len = mrl_sock_send(mrl_instance.mrl_snd_fd[thread_seq],pkt,pkt_len,*(mrl_instance.mgw_addr));
+ assert(send_len == pkt_len);
+}
+
+
diff --git a/src/mrl_redis.c b/src/mrl_redis.c new file mode 100644 index 0000000..a0cbb84 --- /dev/null +++ b/src/mrl_redis.c @@ -0,0 +1,247 @@ +#define IR_NOMINEE_IP_TABLE_NAME "IR_NOMINEE_IP"
+#define IR_CANDIDATE_IP_TABLE_NAME "IR_CANDINATE_IP"
+#define REDIS_CANDIATE_KEY_NAME "IR_CANDIDATE_ID"
+struct mrl_nominee_item{
+ int region_id;
+ int group_id;
+ int addr_type;
+ char src_ip[MRL_STR_IP_LEN];
+ char mask_src_ip[MRL_STR_IP_LEN];
+ char dport[MRL_STR_PORT_LEN];
+ char mask_dport[MRL_STR_PORT_LEN];
+ int protocol;
+ int direction;
+ int is_vaild;
+ int action;
+ int service;
+ char date[DATE_LEN];
+ int type;
+ int strategy_group_id;
+};
+
+struct mrl_vxlan_info{
+ char vxlan_outer_local_mac[MRL_STR_MAC_LEN];//���յ������ݰ��ı����豸MAC��ַ
+ char vxlan_outer_gdev_mac[MRL_STR_MAC_LEN];//���յ������ݰ��Ĵ����豸MAC��ַ
+ char vxlan_outer_local_ip[MRL_STR_IP_LEN];
+ char vxlan_outer_gdev_ip[MRL_STR_IP_LEN];//�����豸IP
+ char vxlan_outer_gdev_port[MRL_STR_PORT_LEN];
+ char vxlan_outer_local_port[MRL_STR_PORT_LEN];
+ unsigned char vxlan_encap_type;
+ int vxlan_link_id;
+ unsigned char vxlan_link_dir;
+ char vxlan_inner_smac[MRL_STR_MAC_LEN];
+ char vxlan_inner_dmac[MRL_STR_MAC_LEN];
+};
+
+struct mrl_candidate_item{
+ int candidate_id;
+ int addr_type;
+ char client_ip[MRL_STR_IP_LEN];//�ͻ���IP
+ int type;
+ int location;
+ int group_id;
+ char mrl_ip[MRL_STR_IP_LEN];
+ struct mrl_vxlan_info vxlan_info;
+};
+
+MESA_htable_handle mrl_htable_init()
+{
+ unsigned int opt_int;
+ MESA_htable_handle htable = MESA_htable_born();
+ assert(htable != NULL);
+
+ opt_int = 1;
+ MESA_htable_set_opt(htable, MHO_THREAD_SAFE, &opt_int, sizeof(int));
+
+ opt_int = 1;
+ MESA_htable_set_opt(htable, MHO_MUTEX_NUM, &opt_int, sizeof(int));
+
+ opt_int = 0;
+ MESA_htable_set_opt(htable, MHO_EXPIRE_TIME, &opt_int, sizeof(int));
+
+ opt_int = 0;
+ MESA_htable_set_opt(htable, MHO_AUTO_UPDATE_TIME, &opt_int, sizeof(int));
+
+ opt_int = 0;
+ MESA_htable_set_opt(htable, MHO_SCREEN_PRINT_CTRL, &opt_int, sizeof(int));
+
+ opt_int = mrl_instance.ht_slots;
+ MESA_htable_set_opt(htable, MHO_HASH_SLOT_SIZE, &opt_int, sizeof(int));
+
+ opt_int = mrl_instance.ht_max_sessions + 1;
+ MESA_htable_set_opt(htable, MHO_HASH_MAX_ELEMENT_NUM, &opt_int, sizeof(int));
+
+ opt_int = mrl_instance.ht_mutex_num;
+ MESA_htable_set_opt(htable, MHO_MUTEX_NUM, &opt_int, sizeof(int));
+
+ int ret = MESA_htable_mature(htable);
+ if (ret < 0)
+ {
+ MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"mrl_init_htable","MESA_htable_mature func error!");
+ assert(0);
+ }
+ return htable;
+}
+
+Maat_feather_t Maat_init(const char * instance_name, const char * redis_ip, const short redis_port, const char *stat_path)
+{
+
+ // init Maat
+
+ Maat_feather_t Maat_feather = Maat_feather(mrl_instance.mrl_cfg.Maat_max_threads, mrl_instance.mrl_cfg.Maat_table_path, mrl_instance.mrl_log_handle);
+ if(Maat_feather == NULL)
+ {
+ MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"Maat_init","Maat_feather func error!");
+ assert(0);
+ }
+ wrapped_Maat_set_feather_opt(Maat_feather, MAAT_OPT_INSTANCE_NAME, instance_name, strlen(instance_name)+1);
+ wrapped_Maat_set_feather_opt(Maat_feather, MAAT_OPT_REDIS_IP, redis_ip, strlen(redis_ip)+1);
+ wrapped_Maat_set_feather_opt(Maat_feather, MAAT_OPT_REDIS_PORT, &(redis_port), sizeof(redis_port));
+ wrapped_Maat_set_feather_opt(Maat_feather, MAAT_OPT_STAT_FILE_PATH, stat_path, strlen(stat_path)+1);
+ wrapped_Maat_set_feather_opt(Maat_feather, MAAT_OPT_STAT_ON, NULL, 0);
+ wrapped_Maat_set_feather_opt(Maat_feather, MAAT_OPT_PERF_ON, NULL, 0);
+ int ret = Maat_initiate_feather(Maat_feather);
+ if(unlikely(ret< 0))
+ {
+ MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"Maat_init","Maat_initiate_feather func error!");
+ assert(0);
+ }
+ return Maat_feather;
+}
+
+int Maat_plugin_table(Maat_feather_t feather,const char* table_name, Maat_start_callback_t *start,Maat_update_callback_t *update,Maat_finish_callback_t *finish, void *u_para)
+{
+ int table_id=0,ret=0;
+ table_id=Maat_table_register(feather,table_name);
+ if(unlikely(table_id==-1))
+ {
+ MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"Maat_plugin_table","Maat_table_register func error,feather is %d and table name is %s!",feather,table_name);
+ assert(0);
+ }
+ else
+ {
+ ret=Maat_table_callback_register(feather, table_id, start, update,finish,u_para);
+ if(unlikely(ret<0))
+ {
+ MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"Maat_plugin_table","Maat_table_callback_register func error,ret is %d!",ret);
+ assert(0);
+ }
+ }
+ return ret;
+}
+
+
+Maat_feather_t mrl_Maat_feather_init(const char * instance_name,const char *table_name, Maat_start_callback_t *start,Maat_update_callback_t *update,Maat_finish_callback_t *finish,void *u_para)
+{
+ Maat_feather_t feather;
+ feather = Maat_init(instance_name,mrl_instance.mrl_cfg.Maat_redis_ip,mrl_instance.mrl_cfg.Maat_redis_port,mrl_instance.mrl_cfg.Maat_stat_path);
+ Maat_plugin_table(feather,table_name,start,update,finish,u_para);
+ return feather;
+}
+
+
+void nominee_update_cb(int table_id,const char* table_line,void* u_para)
+{
+
+ struct mrl_nominee_item *nominee_item = (struct mrl_nominee_item *)calloc(1, sizeof(struct mrl_nominee_item));
+ sscanf(table_line,"%d\t%d\t%d\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%s\t%d\t%d",
+ nominee_item->region_id, nominee_item->group_id, nominee_item->addr_type,
+ nominee_item->src_ip, nominee_item->mask_src_ip, nominee_item->dport,
+ nominee_item->mask_dport, nominee_item->protocol, nominee_item->direction,
+ nominee_item->is_vaild, nominee_item->action, nominee_item->service,
+ nominee_item->date, nominee_item->type, nominee_item->strategy_group_id);
+
+ int ret =MESA_htable_add(mrl_instance.ht_nominee, (const unsigned char *)nominee_item->src_ip, sizeof(nominee_item->src_ip), nominee_item);
+ assert(ret >= 0);
+}
+
+
+void candidate_update_cb(int table_id,const char* table_line,void* u_para)
+{
+ struct mrl_candidate_item *candidate_item = (struct mrl_candidate_item *)calloc(1, sizeof(struct mrl_candidate_item));
+ sscanf(table_line,"%d\t%d\t%s\t%d\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t%s\t%s",
+ candidate_item->candidate_id, candidate_item->addr_type, candidate_item->client_ip,
+ candidate_item->type, candidate_item->location, candidate_item->group_id,
+ candidate_item->mrl_ip,candidate_item->vxlan_info.vxlan_outer_local_mac,
+ candidate_item->vxlan_info.vxlan_outer_gdev_mac,candidate_item->vxlan_info.vxlan_outer_local_ip,
+ candidate_item->vxlan_info.vxlan_outer_gdev_ip,candidate_item->vxlan_info.vxlan_outer_local_port,
+ candidate_item->vxlan_info.vxlan_outer_gdev_port,candidate_item->vxlan_info.vxlan_encap_type,
+ candidate_item->vxlan_info.vxlan_link_id,candidate_item->vxlan_info.vxlan_link_dir,
+ candidate_item->vxlan_info.vxlan_inner_smac,candidate_item->vxlan_info.vxlan_inner_dmac);
+
+ int ret = MESA_htable_add(mrl_instance.ht_candidate, (const unsigned char *)candidate_item->client_ip, sizeof(candidate_item->client_ip), candidate_item);
+ assert(ret >= 0);
+}
+
+long ht_search_cb(void *data, const uchar *key, uint size, void *user_arg)
+{
+ struct mrl_nominee_item *nominee_item = (struct mrl_nominee_item *)data;
+ struct streaminfo *mystrem = (struct streaminfo *)user_arg;
+ if(nominee_item != NULL)//�û��������ö˿ڣ�
+ {
+ mrl_create_candidate_item(mystrem,nominee_item);
+ }
+ return 1;
+}
+
+void mrl_identify_nominee(struct streaminfo *mystrem)
+{
+ UINT32 dip;
+ bool ret = false;
+ dip = ntohl(mystrem->addr.tuple4_v4->daddr);
+ MESA_htable_search_cb(mrl_instance.ht_nominee, (const unsigned char *)&(dip), sizeof(dip),ht_search_cb,(void *)mystrem,NULL);
+}
+
+
+void Maat_set_cmd_line(Maat_feather_t feather, struct mrl_candidate_item *candidate_item)
+{
+ struct Maat_line_t line_rule;
+ char table_line[128];
+ int ret=0,i=0;
+ memset(&line_rule,0,sizeof(line_rule));
+ line_rule.label_id=0;
+ line_rule.rule_id=(int)Maat_cmd_incrby(feather,"TEST_PLUG_SEQ", 1);
+ line_rule.table_name=IR_CANDIDATE_IP_TABLE_NAME;
+ snprintf(table_line,sizeof(table_line[i]),"%d\t%d\t%s\t%d\t%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t%s\t%s",
+ candidate_item->candidate_id, candidate_item->addr_type, candidate_item->client_ip,
+ candidate_item->type, candidate_item->location, candidate_item->group_id,
+ candidate_item->mrl_ip,candidate_item->vxlan_info.vxlan_outer_local_mac,
+ candidate_item->vxlan_info.vxlan_outer_gdev_mac,candidate_item->vxlan_info.vxlan_outer_local_ip,
+ candidate_item->vxlan_info.vxlan_outer_gdev_ip,candidate_item->vxlan_info.vxlan_outer_local_port,
+ candidate_item->vxlan_info.vxlan_outer_gdev_port,candidate_item->vxlan_info.vxlan_encap_type,
+ candidate_item->vxlan_info.vxlan_link_id,candidate_item->vxlan_info.vxlan_link_dir,
+ candidate_item->vxlan_info.vxlan_inner_smac,candidate_item->vxlan_info.vxlan_inner_dmac);
+ line_rule.table_line=table_line;
+ line_rule.expire_after=0;
+ ret=Maat_cmd_set_lines(feather, line_rule, 1, MAAT_OP_ADD);
+ assert(ret>0);
+ return;
+}
+
+void mrl_create_candidate_item(struct streaminfo *mystream, struct mrl_nominee_item *nominee_item)
+{
+ int table_id,ret;
+ struct mrl_candidate_item candidate_ip_item;
+ memset(&candidate_ip_item ,0,sizeof(struct mrl_candidate_item));
+ candidate_ip_item.candidate_id = (int)Maat_cmd_incrby(mrl_instance.candiate_feather,REDIS_CANDIATE_KEY_NAME, 1);;
+ candidate_ip_item.addr_type = MRL_IPV4_TYPE ;
+ memcpy(candidate_ip_item.client_ip,nominee_item->src_ip,strlen(nominee_item->src_ip));
+ candidate_ip_item.type = nominee_item->type;
+ //to do how to get ip location
+ candidate_ip_item.location = 0;
+
+ candidate_ip_item.group_id = nominee_item->group_id;
+ memcpy(candidate_ip_item.mrl_ip, mrl_instance.mrl_cfg.local_ip,strlen(mrl_instance.mrl_cfg.local_ip));
+ mrl_get_candidate_vxlan_info(mystream,&(candidate_ip_item.vxlan));
+
+ MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_DEBUG,"mrl_create_candidate_item","get candidate item:
+ candidate_id:%d,addr_type:%d,client_ip:%s,type:%d, location:%d,
+ group_id:%d,mrl_ip:%s",
+ candidate_ip_item.candidate_id, candidate_ip_item.addr_type, candidate_ip_item.client_ip, candidate_ip_item.type,
+ candidate_ip_item.location, candidate_ip_item.group_id, candidate_ip_item.mrl_ip);
+ Maat_set_cmd_line(mrl_instance.candiate_feather,&mrl_candidate_item);
+
+}
+
+
+
|
