From 2b7698bc266247bef5b691368992a8293dcc7bf9 Mon Sep 17 00:00:00 2001 From: yinbin Date: Mon, 26 Aug 2019 01:28:51 -0700 Subject: [PATCH] =?UTF-8?q?Add=20aaa-12=20=E6=B7=BB=E5=8A=A0VLAN=E8=BD=AC?= =?UTF-8?q?=E5=8F=91=E6=A8=A1=E5=9D=97=E5=8A=9F=E8=83=BD=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=20SOL=20=E6=B7=BB=E5=8A=A0VLAN=E8=BD=AC=E5=8F=91=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E5=8A=9F=E8=83=BD=E4=BB=A3=E7=A0=81=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=BA=BA=EF=BC=9Ayinbin=20=E6=A3=80=E8=A7=86=E4=BA=BA?= =?UTF-8?q?=EF=BC=9Ayinbin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yinbin --- Common/config_manager.h | 26 +- Platform/build/user.configm.Makefile | 4 +- .../configm/config-server/include/configm.h | 469 ++-- .../config-server/include/vlan_config.h | 192 ++ .../config-server/vlan_config/vlan_config.c | 2332 +++++++++++++++++ 5 files changed, 2782 insertions(+), 241 deletions(-) create mode 100644 Platform/user/configm/config-server/include/vlan_config.h create mode 100644 Platform/user/configm/config-server/vlan_config/vlan_config.c diff --git a/Common/config_manager.h b/Common/config_manager.h index 5de52259f..64a2888eb 100755 --- a/Common/config_manager.h +++ b/Common/config_manager.h @@ -23,14 +23,15 @@ #define LOG_CONFIG_MODULE 0x00000004 +/*vlan config */ +#define VLAN_CONFIG_MODULE 0x00000005 + +/*DHCP CONFIG*/ +#define DHCP_CONFIG_MODULE 0x00000006 + /*nat config */ #define NAT_CONFIG_MODULE 0x00000008 -/*vlan config */ -#define VLAN_CONFIG_MODULE 0x00000005 - -/*DHCP CONFIG*/ -#define DHCP_CONFIG_MODULE 0x00000006 /************************* 模块定义结束 **********************/ /************************ config id定义 **********************/ @@ -55,14 +56,17 @@ #define LOG_CONFIG_REMOTE_LEVEL (uint64)((uint64)LOG_CONFIG_MODULE<<32|5) #define LOG_CONFIG_FILE (uint64)((uint64)LOG_CONFIG_MODULE<<32|6) +#define VLAN_CONFIG (uint64)((uint64)VLAN_CONFIG_MODULE<<32|1) + +#define DHCP_SUBNET_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|1) +#define DHCP_HOST_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|2) +#define DHCP_SHARED_NETWORK_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|3) +#define DHCP_RELAY_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|4) +#define DHCP_CLIENT_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|5) +#define DHCP_DHCPD_LEASE (uint64)((uint64)DHCP_CONFIG_MODULE<<32|6) + #define NAT4_CONFIG (uint64)((uint64)NAT_CONFIG_MODULE<<32|1) -#define DHCP_SUBNET_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|1) -#define DHCP_HOST_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|2) -#define DHCP_SHARED_NETWORK_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|3) -#define DHCP_RELAY_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|4) -#define DHCP_CLIENT_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|5) -#define DHCP_DHCPD_LEASE (uint64)((uint64)DHCP_CONFIG_MODULE<<32|6) /************************ config id定义 end**********************/ diff --git a/Platform/build/user.configm.Makefile b/Platform/build/user.configm.Makefile index 228d62c03..b38b6a0f8 100755 --- a/Platform/build/user.configm.Makefile +++ b/Platform/build/user.configm.Makefile @@ -37,8 +37,8 @@ COMMON_SRCS = configserver.c \ user_manager_config/user_recover_config.c user_manager_config/user_group_config.c user_manager_config/user_account_config.c user_manager_config/usermanager-server/array_index.c \ user_manager_config/usermanager-server/user_group.c user_manager_config/usermanager-server/user_mod.c user_manager_config/usermanager-server/user.c \ log_config/log_config_console.c log_config/log_config_init.c log_config/log_config_cm.c log_config/log_config_monitor.c log_config/log_config_remote.c log_config/log_config_file.c \ - nat_config/natconfig.c - + nat_config/natconfig.c \ + vlan_config/vlan_config.c \ # MRS Board Source Files PLAT_LINUX_SRCS = $(COMMON_SRCS) diff --git a/Platform/user/configm/config-server/include/configm.h b/Platform/user/configm/config-server/include/configm.h index 3662e0878..758911451 100755 --- a/Platform/user/configm/config-server/include/configm.h +++ b/Platform/user/configm/config-server/include/configm.h @@ -1,228 +1,241 @@ -#ifndef CONFIGM_H_ -#define CONFIGM_H_ -#include "config_manager.h" -#include "s2j/s2j.h" -#include "../../../../common/rpc/rpc_common.h" -#include "../../../../../Common/commuapinl.h" -#include "../user_manager_config/user_group_config.h" -#include "../user_manager_config/user_account_config.h" -#include "../user_manager_config/user_recover_config.h" -#include "netconfig.h" -#include "log_config.h" -#include "../web_config/authfree.h" -#include "../web_config/auth_parameters.h" -#include "natconfig.h" - - - -#define CONFIG_INIT_ARRAY \ -{\ - { \ - NETCONFIG_MODULE, \ - net_main \ - }, \ - { \ - LOG_CONFIG_MODULE, \ - log_config_init \ - } \ -} - -/* - 1、配置ID,全局唯一,用于寻找对应的配置业务 - 2、配置源检查,全局唯一,用于寻找对应的配置业务, - 从低位到高位,第一位表示WEB,后续配置扩展 - 3、是否配置恢复 - 4、是否是多实例 - 5、配置校验回调函数 - 6、配置处理接口 - 7、配置获取接口 - 8、配置全部获取接口 -*/ -#define CONFIG_SERVICE_ARRAY \ -{ \ - {\ - IPCONFIG_V4, \ - CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ - FALSE, \ - ip_config_chk, \ - ip_config_proc, \ - ip_config_get, \ - ip_config_get_all \ - },\ - {\ - BR_CONFIG, \ - CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ - FALSE, \ - br_config_chk, \ - br_config_proc, \ - NULL, \ - NULL \ - },\ - {\ - BRIF_CONFIG, \ - CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ - FALSE, \ - br_if_config_chk, \ - br_if_config_proc, \ - br_if_config_get, \ - br_if_config_get_all \ - },\ - {\ - BRFDB_CONFIG, \ - CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ - FALSE, \ - br_fdb_config_chk, \ - NULL, \ - br_fdb_config_get, \ - NULL \ - },\ - {\ - USER_MANAGER_CONFIG_GROUP, \ - CONFIG_FROM_WEB, \ - FALSE, \ - usergroup_config_chk, \ - usergroup_config_proc, \ - usergroup_config_get, \ - usergroup_config_get_all \ - },\ - {\ - USER_MANAGER_CONFIG_RECOVER, \ - CONFIG_FROM_RECOVER1, \ - TRUE, \ - userecover_config_chk, \ - userecover_config_proc, \ - userecover_config_get, \ - userecover_config_get_all \ - },\ - { \ - AUTHFREE_CONFIG, \ - CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ - FALSE, \ - freeauth_config_chk, \ - freeauth_config_proc, \ - NULL, \ - NULL \ - },\ - {\ - FREEPARAMETERS_CONFIG, \ - CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ - FALSE, \ - authpara_config_chk, \ - authpara_config_proc, \ - NULL, \ - NULL \ - },\ - {\ - USER_MANAGER_CONFIG_USER, \ - CONFIG_FROM_WEB, \ - FALSE, \ - user_config_chk, \ - user_config_proc, \ - user_config_get, \ - user_config_get_all \ - },\ - {\ - LOG_CONFIG_CONSOLE, \ - CONFIG_FROM_WEB, \ - FALSE, \ - log_console_config_chk, \ - log_console_config_proc, \ - NULL, \ - NULL \ - },\ - {\ - LOG_CONFIG_MONITOR, \ - CONFIG_FROM_WEB, \ - FALSE, \ - log_monitor_config_chk, \ - log_monitor_config_proc, \ - NULL, \ - NULL \ - },\ - {\ - LOG_CONFIG_REMOTE_ADD_HOST, \ - CONFIG_FROM_WEB, \ - FALSE, \ - log_remote_host_config_chk, \ - log_remote_add_host_config_proc, \ - NULL, \ - NULL \ - },\ - {\ - LOG_CONFIG_REMOTE_DEL_HOST, \ - CONFIG_FROM_WEB, \ - FALSE, \ - log_remote_host_config_chk, \ - log_remote_del_host_config_proc, \ - NULL, \ - NULL \ - },\ - {\ - LOG_CONFIG_REMOTE_LEVEL, \ - CONFIG_FROM_WEB, \ - FALSE, \ - log_remote_level_config_chk, \ - log_remote_level_config_proc, \ - NULL, \ - NULL \ - },\ - {\ - LOG_CONFIG_FILE, \ - CONFIG_FROM_WEB, \ - FALSE, \ - log_file_config_chk, \ - log_file_config_proc, \ - NULL, \ - NULL \ - },\ - {\ - NAT4_CONFIG, \ - CONFIG_FROM_WEB, \ - FALSE, \ - nat_config_chk, \ - nat_config_proc, \ - NULL, \ - nat_config_get_all \ - }\ -} - -typedef ret_code (*cm_config_init)(); - -typedef ret_code (*cm_config_chk)(uint source, uint *config_type, - pointer input, int *input_len, - pointer output, int *output_len); - -typedef ret_code (*cm_config_proc)(uint source, uint config_type, - pointer input, int input_len, - pointer output, int *output_len); - -typedef ret_code (*cm_config_get)(uint source, - pointer input, int input_len, - pointer output, int *output_len); - -typedef ret_code (*cm_config_get_all)(uint source, - pointer output, int *output_len); - -/* 配置注册 */ -struct _config_init { - uint config_mudlue; - cm_config_init init_callback; -}; -typedef struct _config_init config_init_t; - - -/* 配置注册 */ -struct _config_service { - uint64 config_id; /* 配置ID,全局唯一,用于寻找对应的配置业务*/ - uint config_src; /* 配置源检查,全局唯一,用于寻找对应的配置业务,从低位到高位,第一位表示web,后续配置扩展 */ - boolean recovery; /* 配置恢复处理函数,如果为FALSE则不进行配置恢复 */ - cm_config_chk chk_callback; /* 配置校验回调函数 */ - cm_config_proc proc_callback; /* 配置接口 */ - cm_config_get get_callback; /* 获取配置接口 */ - cm_config_get_all getall_callback; /* 获取所有配置接口 */ -}; - -typedef struct _config_service config_service_t; - -#endif /* RPC_COMMON_H_ */ - +#ifndef CONFIGM_H_ +#define CONFIGM_H_ +#include "config_manager.h" +#include "s2j/s2j.h" +#include "../../../../common/rpc/rpc_common.h" +#include "../../../../../Common/commuapinl.h" +#include "../user_manager_config/user_group_config.h" +#include "../user_manager_config/user_account_config.h" +#include "../user_manager_config/user_recover_config.h" +#include "netconfig.h" +#include "log_config.h" +#include "../web_config/authfree.h" +#include "../web_config/auth_parameters.h" +#include "natconfig.h" + + + +#define CONFIG_INIT_ARRAY \ +{\ + { \ + NETCONFIG_MODULE, \ + net_main \ + }, \ + { \ + LOG_CONFIG_MODULE, \ + log_config_init \ + }, \ + { \ + VLAN_CONFIG_MODULE, \ + vlan_config_init \ + } \ +} + +/* + 1、配置ID,全局唯一,用于寻找对应的配置业务 + 2、配置源检查,全局唯一,用于寻找对应的配置业务, + 从低位到高位,第一位表示WEB,后续配置扩展 + 3、是否配置恢复 + 4、是否是多实例 + 5、配置校验回调函数 + 6、配置处理接口 + 7、配置获取接口 + 8、配置全部获取接口 +*/ +#define CONFIG_SERVICE_ARRAY \ +{ \ + {\ + IPCONFIG_V4, \ + CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ + FALSE, \ + ip_config_chk, \ + ip_config_proc, \ + ip_config_get, \ + ip_config_get_all \ + },\ + {\ + BR_CONFIG, \ + CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ + FALSE, \ + br_config_chk, \ + br_config_proc, \ + NULL, \ + NULL \ + },\ + {\ + BRIF_CONFIG, \ + CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ + FALSE, \ + br_if_config_chk, \ + br_if_config_proc, \ + br_if_config_get, \ + br_if_config_get_all \ + },\ + {\ + BRFDB_CONFIG, \ + CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ + FALSE, \ + br_fdb_config_chk, \ + NULL, \ + br_fdb_config_get, \ + NULL \ + },\ + {\ + USER_MANAGER_CONFIG_GROUP, \ + CONFIG_FROM_WEB, \ + FALSE, \ + usergroup_config_chk, \ + usergroup_config_proc, \ + usergroup_config_get, \ + usergroup_config_get_all \ + },\ + {\ + USER_MANAGER_CONFIG_RECOVER, \ + CONFIG_FROM_RECOVER1, \ + TRUE, \ + userecover_config_chk, \ + userecover_config_proc, \ + userecover_config_get, \ + userecover_config_get_all \ + },\ + { \ + AUTHFREE_CONFIG, \ + CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ + FALSE, \ + freeauth_config_chk, \ + freeauth_config_proc, \ + NULL, \ + NULL \ + },\ + {\ + FREEPARAMETERS_CONFIG, \ + CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ + FALSE, \ + authpara_config_chk, \ + authpara_config_proc, \ + NULL, \ + NULL \ + },\ + {\ + USER_MANAGER_CONFIG_USER, \ + CONFIG_FROM_WEB, \ + FALSE, \ + user_config_chk, \ + user_config_proc, \ + user_config_get, \ + user_config_get_all \ + },\ + {\ + LOG_CONFIG_CONSOLE, \ + CONFIG_FROM_WEB, \ + FALSE, \ + log_console_config_chk, \ + log_console_config_proc, \ + NULL, \ + NULL \ + },\ + {\ + LOG_CONFIG_MONITOR, \ + CONFIG_FROM_WEB, \ + FALSE, \ + log_monitor_config_chk, \ + log_monitor_config_proc, \ + NULL, \ + NULL \ + },\ + {\ + LOG_CONFIG_REMOTE_ADD_HOST, \ + CONFIG_FROM_WEB, \ + FALSE, \ + log_remote_host_config_chk, \ + log_remote_add_host_config_proc, \ + NULL, \ + NULL \ + },\ + {\ + LOG_CONFIG_REMOTE_DEL_HOST, \ + CONFIG_FROM_WEB, \ + FALSE, \ + log_remote_host_config_chk, \ + log_remote_del_host_config_proc, \ + NULL, \ + NULL \ + },\ + {\ + LOG_CONFIG_REMOTE_LEVEL, \ + CONFIG_FROM_WEB, \ + FALSE, \ + log_remote_level_config_chk, \ + log_remote_level_config_proc, \ + NULL, \ + NULL \ + },\ + {\ + LOG_CONFIG_FILE, \ + CONFIG_FROM_WEB, \ + FALSE, \ + log_file_config_chk, \ + log_file_config_proc, \ + NULL, \ + NULL \ + },\ + {\ + NAT4_CONFIG, \ + CONFIG_FROM_WEB, \ + FALSE, \ + nat_config_chk, \ + nat_config_proc, \ + NULL, \ + nat_config_get_all \ + },\ + {\ + VLAN_CONFIG, \ + CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ + FALSE, \ + vlan_config_chk, \ + vlan_config_proc, \ + vlan_config_get, \ + vlan_config_get_all \ + }\ +} + +typedef ret_code (*cm_config_init)(); + +typedef ret_code (*cm_config_chk)(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +typedef ret_code (*cm_config_proc)(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + +typedef ret_code (*cm_config_get)(uint source, + pointer input, int input_len, + pointer output, int *output_len); + +typedef ret_code (*cm_config_get_all)(uint source, + pointer output, int *output_len); + +/* 配置注册 */ +struct _config_init { + uint config_mudlue; + cm_config_init init_callback; +}; +typedef struct _config_init config_init_t; + + +/* 配置注册 */ +struct _config_service { + uint64 config_id; /* 配置ID,全局唯一,用于寻找对应的配置业务*/ + uint config_src; /* 配置源检查,全局唯一,用于寻找对应的配置业务,从低位到高位,第一位表示web,后续配置扩展 */ + boolean recovery; /* 配置恢复处理函数,如果为FALSE则不进行配置恢复 */ + cm_config_chk chk_callback; /* 配置校验回调函数 */ + cm_config_proc proc_callback; /* 配置接口 */ + cm_config_get get_callback; /* 获取配置接口 */ + cm_config_get_all getall_callback; /* 获取所有配置接口 */ +}; + +typedef struct _config_service config_service_t; + +#endif /* RPC_COMMON_H_ */ + diff --git a/Platform/user/configm/config-server/include/vlan_config.h b/Platform/user/configm/config-server/include/vlan_config.h new file mode 100644 index 000000000..45f877d1e --- /dev/null +++ b/Platform/user/configm/config-server/include/vlan_config.h @@ -0,0 +1,192 @@ +#ifndef VLAN_CONFIG_H_ +#define VLAN_CONFIG_H_ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "configm.h" +#include "rpc.h" +#include "parsefile.h" +#include "netconfig.h" + +#define MAX_VLAN 4096/* 0ûʶ֡ȼ,4095,4094 */ +#define VID_MIN 2 /* vidΧ2-4094 */ +#define VID_MAX MAX_VLAN - 2 /* vidΧ2-4094 */ +#define MAX_INTERFACES 20 /* ӿĿ */ +#define IF_NAME_LEN 20 /* ӿ󳤶 */ +#define BR_VLAN_NAME_LEN 50 /* br-vlan󳤶 */ +#define SYSCALL_BUF_LEN 100 +#define BITSPERWORD 32 +#define SHIFT 5 +#define MASK 0x1F +#define FILE_BUF_LINE 100 +#define EACH_PORT_MAX_VLAN_NUM 5 +#define ERR_DATA_LEN 500 +#define VLAN_TEST_ADD_PATH "/home/eric/code/test/eric/vlan/conf/vlan_test.conf.add" +#define VLAN_TEST_DEL_PATH "/home/eric/code/test/eric/vlan/conf/vlan_test.conf.del" +#define xfree(X) \ + do{ \ + if(X){ \ + free(X); \ + X = NULL; \ + } \ + }while(0); + +/* ͣADDMODDEL */ +typedef enum{ + OP_NONE, + OP_ADD, + OP_MOD, + OP_DEL, +}operation_type; + +/* ·ͣnoneaccesstrunk */ +typedef enum{ + LINK_TYPE_NONE, + LINK_TYPE_ACCESS, + LINK_TYPE_TRUNK, + LINK_TYPE_HYBIRD, + LINK_TYPE_UNKNOWN, +}interface_link_attr; + +/* ӿģͣ1 Ž; 2 ·Ž; 3 · */ +typedef enum{ + INTERFACE_MODEL_FORWARD, + INTERFACE_MODEL_ROUTE_FORWARD, + INTERFACE_MODEL_ROUTE, +}interface_model; + +/* ӿͣLANWAN */ +typedef enum{ + IF_TYPE_LAN, + IF_TYPE_WAN, +}interface_network_type; + +/* ӿVLANϢ */ +typedef struct interface_vlan_info_t{ + + interface_link_attr attr; + operation_type op_type;//ADDDELMOD + interface_model if_model;//ӿģ + int vidcnt;//vid + int vlan_bitmap[MAX_VLAN/BITSPERWORD]; + char if_name[IF_NAME_LEN]; +}interface_vlan_info; + + +/* vlan1002003004005006007008009001000 */ +#define ADD_100 "/home/eric/code/test/eric/vlan/conf/conf.add.100" +#define ADD_100_200 "/home/eric/code/test/eric/vlan/conf/conf.add.100_200" +#define ADD_100_1000 "/home/eric/code/test/eric/vlan/conf/conf.add.100_1000" +#define DEL_100 "/home/eric/code/test/eric/vlan/conf/conf.add.100" +#define DEL_100_200 "/home/eric/code/test/eric/vlan/conf/conf.add.100_200" +#define DEL_100_1000 "/home/eric/code/test/eric/vlan/conf/conf.add.100_1000" + +/* void */ +void vlan_bitmap_set(int *v, int i); +void vlan_bitmap_clear(int *v, int i); +void interface_vlan_init(void); +void vlan_set_struct_stub(interface_vlan_info *if_vlan_info, operation_type op_type, int *if_vlnum); +void vlan_config_stub(operation_type op_type); +void vlan_config_format_json_stub(int *interface); +void vlan_config_get_all_stub(uint source); +void vlan_config_get_stub(void); +void vlan_config_proc_test(operation_type op_type); +void vlan_config_init(void); +void vlan_config_stub1(void); +void vlan_config_stub1(void); + +/* int */ +int vlan_bitmap_check(int *v, int i); +int if_vlan2subif(char *if_name, int vid, char *subif); +int br_if_num_stub(char *br_name, operation_type op_type); +int br_if_num(char *br_name); +int create_br_stub(char *br_vlname); +int create_br(char *br_vlname); +int br_addif_stub(char *br_name, char *if_name); +int br_addif(char *br_name, char *if_name); +int interface_br_stub(char *if_name, char *br_name); +int interface_br(char *if_name, char *br_name); +int del_br_stub(char *br_name); +int del_br(char *br_name); +int add_bridge_vlan_access(char *if_name, int vid); +int check_config(char *if_name); +int add_interface_vlan_access(char *if_name, int *vlan_bitmap, interface_model if_model); +int add_bridge_vlan_trunk(char *if_name, int *vlan_bitmap); +int add_bridge_vlan_trunk(char *if_name, int *vlan_bitmap); +int del_bridge_vlan_access(char *if_name); +int del_bridge_vlan_access(char *if_name); +int del_bridge_vlan_trunk(char *if_name); +int get_ifnode_from_global(char *if_name); +int find_insert_ifnode_from_global(char *if_name); +int get_old_interface_attr(char *if_name, interface_link_attr *attr); +int if_model_is_change(interface_model old, interface_model new); + +/* typedefine */ +interface_link_attr get_attr_from_ifname(char *if_name); +interface_network_type lan_or_wan_stub(char *if_name); +interface_network_type lan_or_wan(char *if_name); +interface_model get_old_interface_model(char *if_name, interface_model *if_model); +interface_model which_interface_model(char *if_name); + +/* ret_code */ +ret_code add_interface_vlan_trunk(char *if_name, int *vlan_bitmap, interface_model if_model); +ret_code add_interface_vlan(interface_vlan_info *if_vlan_info); +ret_code del_vlan_sub_interface(char *if_name, int *vlan_bitmap); +ret_code add_vlan_sub_interface(char *if_name, int *vlan_bitmap); +ret_code del_interface_vlan_trunk(char *if_name, int *vlan_bitmap, interface_model if_model); +ret_code refresh_if_vlan_info_op_add(interface_vlan_info *if_vlan_info); +ret_code refresh_if_vlan_info_op_del(interface_vlan_info *if_vlan_info); +ret_code vlan_save_conf_file_add(interface_vlan_info *if_vlan_info); +ret_code vlan_save_conf_file_del(interface_vlan_info *if_vlan_info); +ret_code vlan_save_conf_file(interface_vlan_info *if_vlan_info); +ret_code refresh_if_vlan_info(interface_vlan_info *if_vlan_info); +ret_code del_interface_vlan(interface_vlan_info *if_vlan_info); +ret_code vlan_config_json_parse(pointer input, interface_vlan_info *if_vlan_info, int *if_vlnum); +ret_code vlan_config_recovery(void); +ret_code vlan_get_json_parse(int *interface, pointer input); +ret_code vlan_config_format_json(int *interface, pointer output, int *output_len); +ret_code vlan_get_from_conf_file(int *interface); +ret_code vlan_config_get_all(uint source, pointer output, int *output_len); +ret_code vlan_config_json_parse_test(char *path, interface_vlan_info *if_vlan_info, int *if_vlnum); +ret_code vlan_config_json_parse_test1(operation_type op_type, interface_vlan_info *if_vlan_info, int *if_vlnum); +ret_code vlan_config_chk_test(operation_type op_type); +ret_code interface_vlan_check(interface_vlan_info *if_vlan_info); +ret_code if_attr_chk(interface_vlan_info *if_vlan_info); +ret_code op_type_chk(interface_vlan_info *if_vlan_info); +ret_code op_type_add_chk(interface_vlan_info *if_vlan_info); +ret_code op_type_del_chk(interface_vlan_info *if_vlan_info); +ret_code vid_value_chk(int vid); +ret_code vid_num_chk(char *if_name, operation_type op_type, int num); +ret_code if_name_chk(interface_vlan_info *if_vlan_info); +ret_code vlan_config_set_chk(uint source, pointer input); +ret_code vlan_operate_parse(pointer input, int *operate_type); +ret_code vlan_config_get_chk(uint source, pointer input); +ret_code vlan_config_get_all_chk(uint source, pointer input); +ret_code del_compose_if_vlan_info(char *if_name, interface_vlan_info *if_vlan_info); +ret_code interface_vlan_set(interface_vlan_info *if_vlan_info); +ret_code vlan_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + +ret_code vlan_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len); + +ret_code vlan_config_chk(uint source,uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +int del_interface_vlan_cb(BR_EVENT_TYPE event_type, br_event_t event_arg); + + +#endif /* VLAN_CONFIG_H_ */ + diff --git a/Platform/user/configm/config-server/vlan_config/vlan_config.c b/Platform/user/configm/config-server/vlan_config/vlan_config.c new file mode 100644 index 000000000..ca2e8ab2d --- /dev/null +++ b/Platform/user/configm/config-server/vlan_config/vlan_config.c @@ -0,0 +1,2332 @@ +#include "vlan_config.h" + +/* ȫֽӿvlanϢ */ +interface_vlan_info g_if_vlan_info[MAX_INTERFACES] = {0}; + +/* ȫerror dataϢ */ +char g_err_data[ERR_DATA_LEN] = {0}; +/************************************************************ +* :vlan bitmapijλ +* : +* : +* ֵ: +************************************************************/ +void vlan_bitmap_set(int *v, int i) +{ + if(!v || i < 0){ + printf("[vlan]vlan_bitmap_set: input error.\n"); + return; + } + v[i >> SHIFT] |= (1 << (i & MASK)); +} + +/************************************************************ +* :vlan bitmapijλ +* : +* : +* ֵ: +************************************************************/ +void vlan_bitmap_clear(int *v, int i) +{ + if(!v || i < 0){ + printf("[vlan]vlan_bitmap_clear: input error.\n"); + return; + } + v[i >> SHIFT] &= ~(1 << (i & MASK)); +} + +/************************************************************ +* :ѯvlan bitmapijλǷ +* : +* : +* ֵ: +************************************************************/ +int vlan_bitmap_check(int *v, int i) +{ + if(!v || i < 0){ + printf("[vlan]vlan_bitmap_check: input error.\n"); + return -1; + } + return v[i >> SHIFT] & (1 << (i & MASK)); +} + + +/************************************************************ +* :if_nameattrg_if_vlan_info[]б +* if_nameattr +* : +* : +* ֵ: +************************************************************/ +interface_link_attr get_attr_from_ifname(char *if_name) +{ + if(!if_name){ + return LINK_TYPE_UNKNOWN; + } + int ifnode; + ifnode = get_ifnode_from_global(if_name); + if(ifnode == -1){ + printf("[vlan]get_attr_from_ifname: ifnode is -1.\n"); + return LINK_TYPE_UNKNOWN; + } + return g_if_vlan_info[ifnode].attr; +} + +/************************************************************ +* :ӿƺvidӽӿ +* :if_namevid +* :subif +* ֵ: +************************************************************/ +int if_vlan2subif(char *if_name, int vid, char *subif) +{ + if(!if_name || !subif){ + printf("[vlan]if_vlan2subif: input error.\n"); + return -1; + } + sprintf(subif, "%s.%d", if_name, vid); + return 0; +} + +int br_if_num_stub(char *br_name, operation_type op_type) +{ + int ret; + if(op_type == OP_ADD){ + ret = 0; + } + else if(op_type == OP_MOD){ + ret = 1; + } + else if(op_type == OP_DEL){ + ret = 0; + } + return ret; +} +/************************************************************ +* :brϵĽӿĿ +* : +* : +* ֵ: +************************************************************/ +int br_if_num(char *br_name) +{ + return br_if_num_stub(br_name, OP_ADD); +} + +int create_br_stub(char *br_vlname) +{ + char buf[SYSCALL_BUF_LEN] = {0}; + sprintf(buf, "brctl addbr %s", br_vlname); + system(buf); + return 0; +} +/************************************************************ +* :br +* : +* : +* ֵ: +************************************************************/ +int create_br(char *br_vlname) +{ + return create_br_stub(br_vlname); +} + +int br_addif_stub(char *br_name, char *if_name) +{ + char buf[SYSCALL_BUF_LEN] = {0}; + sprintf(buf, "brctl addif %s %s", br_name, if_name); + system(buf); + return 0; +} +/************************************************************ +* :ӿڼbr +* : +* : +* ֵ: +************************************************************/ +int br_addif(char *br_name, char *if_name) +{ + return br_addif_stub(br_name, if_name); +} + +int interface_br_stub(char *if_name, char *br_name) +{ + return 0; +} +/************************************************************ +* :жijӿǷbrϣĸbr +* : +* :br_name +* ֵ: +************************************************************/ +int interface_br(char *if_name, char *br_name) +{ + /* ȴ׮ */ + return interface_br_stub(if_name, br_name); +} + +int del_br_stub(char *br_name) +{ + char buf[SYSCALL_BUF_LEN] = {0}; + sprintf(buf, "brctl delbr %s", br_name); + system(buf); + return 0; +} +/************************************************************ +* :ɾbr +* : +* : +* ֵ: +************************************************************/ +int del_br(char *br_name) +{ + return del_br_stub(br_name); +} + +/************************************************************ +* :жϽӿںģ:·Ž or Ž +* Ŀǰֻ֧·Ž +* : +* : +* ֵ: +************************************************************/ +interface_model which_interface_model(char *if_name) +{ + return INTERFACE_MODEL_ROUTE_FORWARD; +} + + +/************************************************************ +* :ʼinitӦ÷ùinit +* ô׶εٵ +* : +* : +* ֵ: +************************************************************/ +void vlan_config_init(void) +{ + ret_code ret = RET_OK; + int sys_ret = 0; + + system("modprobe 8021q"); + + ret = br_event_register(BR_IF_LEAVE_EVENT_PRE, del_interface_vlan_cb); +#if 0 + /* br0 */ + ret = br_bridge_add("br0", &sys_ret); + if(ret != RET_OK){ + printf("[vlan]vlan_config_init: br0_bridge_add failed(%d)\n", sys_ret); + return; + } + /* ӽӿӽbr0 */ + br_if_bridge_add("br0", "ens38", 1, &sys_ret); + if(sys_ret != 0){ + printf("[vlan]vlan_config_init: br0_if_bridge_add failed(%d)(%s)\n", sys_ret, strerror(sys_ret)); + return; + } + + /* br1 */ + ret = br_bridge_add("br1", &sys_ret); + if(ret != RET_OK){ + printf("[vlan]vlan_config_init: br1_bridge_add failed(%d)\n", sys_ret); + return; + } + /* ӽӿӽbr1 */ + br_if_bridge_add("br1", "ens39", 1, &sys_ret); + if(sys_ret != 0){ + printf("[vlan]vlan_config_init: br1_if_bridge_add failed(%d)(%s)\n", sys_ret, strerror(sys_ret)); + return; + } +#endif + return; +} + +/************************************************************ +* :Žģʽaccess vlan +* Ŀǰֻ֧·Ž,漰Žӵݲʵ +* : +* : +* ֵ: +************************************************************/ +int add_bridge_vlan_access(char *if_name, int vid) +{ + //ϵͳ("bridge vlan add vid %d dev %s pvid untagged", vid, if_name); + //iproute2ӿڣbridge vlan + return 0; +} + + +/************************************************************ +* :У飺֮ǰǷӹýӿ +* : +* : +* ֵ: 0:δù +* 1:ù +************************************************************/ +int check_config(char *if_name) +{ + int i; + + for(i = 0; i < MAX_INTERFACES; i++){ + if(!strcmp(g_if_vlan_info[i].if_name, if_name)){ + printf("[vlan]check_config:already exists, return 1\n"); + return 1; + } + } + return 0; +} + +/************************************************************ +* :access vlan +* Ŀǰֻ֧·Ž,漰Žӵݲʵ +* : +* : +* ֵ: +************************************************************/ +int add_interface_vlan_access(char *if_name, int *vlan_bitmap, interface_model if_model) +{ + int i; + + /* ӿںģ */ + if(INTERFACE_MODEL_FORWARD != if_model){ + printf("accessŽģ,򱨴\n"); + return -1; + } + + for(i = VID_MIN; i <= VID_MAX; i++){ + if(vlan_bitmap_check(vlan_bitmap, i)){ + return add_bridge_vlan_access(if_name, i); + } + } + return -1; +} + +/************************************************************ +* :trunk vlan +* Ŀǰֻ֧·Ž,漰Žӵݲʵ +* vids[i]Ϊ1ʾvlanvidΪi +* : +* : +* ֵ: +************************************************************/ + +int add_bridge_vlan_trunk(char *if_name, int *vlan_bitmap) +{ + int i; + char buf[SYSCALL_BUF_LEN] = {0}; + if(!if_name || !vlan_bitmap){ + return -1; + } + for(i = VID_MIN; i <= VID_MAX; i++){ + if(vlan_bitmap_check(vlan_bitmap, i)){ + sprintf(buf,"bridge vlan add vid %d dev %s", i, if_name);//after change to snprintf + system(buf);//after change to execv + return 0; + } + } + return -1; +} + + +/************************************************************ +* :vlanӽӿ +* vids[i]Ϊ1ʾvlanvidΪi +* vid2ʼ +* : +* : +* ֵ: +************************************************************/ +ret_code add_vlan_sub_interface(char *if_name, int *vlan_bitmap) +{ + if(!if_name || !vlan_bitmap){ + printf("[vlan]add_vlan_sub_interface: input is null.\n"); + return RET_NULLP; + } + printf("[vlan]add_vlan_sub_interface: begin.\n"); + int vid; + char subif[IF_NAME_LEN] = {0}; + char buf[SYSCALL_BUF_LEN] = {0}; + char br_vlname[BR_VLAN_NAME_LEN] = {0}; + char tmp_brname[BR_VLAN_NAME_LEN] = {0}; + int sys_ret = 0, status = 0; + ret_code ret = RET_OK; + for(vid = VID_MIN; vid <= VID_MAX; vid++){ + if(vlan_bitmap_check(vlan_bitmap, vid)){ + + /* vconfigӽӿ */ + sprintf(buf, "vconfig add %s %d", if_name, vid); + printf("[vlan]add_vlan_sub_interface: vconfig buf=%s\n", buf); + status = system(buf); + if(status != 0){ + printf("[vlan]add_vlan_sub_if: subif status error. (%s)\n", strerror(errno)); + return RET_SYSERR; + } + memset(buf, 0, SYSCALL_BUF_LEN); + + /* ӽӿ */ + (void)if_vlan2subif(if_name, vid, subif); + sprintf(buf, "ifconfig %s up", subif); + printf("[vlan]add_vlan_sub_interface: ifconfig sub buf=%s\n", buf); + status = system(buf); + if(status != 0){ + printf("[vlan]add_vlan_sub_if: ifconfig subif status error. (%s)\n", strerror(errno)); + return RET_SYSERR; + } + memset(buf, 0, SYSCALL_BUF_LEN); + + /* WANڲҪ */ + /* زRET_OK,˵WAN,ҪĽvlan,һѭ */ + if(RET_OK != br_if_bridge_get(if_name, tmp_brname)){ + printf("[vlan]add_vlan_sub_interface: port is WAN, don't creat br-vl and addif, continue\n"); + memset(subif, 0, IF_NAME_LEN); + memset(tmp_brname, 0, BR_VLAN_NAME_LEN); + continue; + } + /* br-vl */ + sprintf(br_vlname, "br-vl%d", vid); + printf("[vlan]add_vlan_sub_interface: br_vlname=%s\n", br_vlname); + + /* ѯɵbr-vlǷ,򴴽br-vl */ + if(br_is_exist(br_vlname) == FALSE){ + ret = br_bridge_add(br_vlname, &sys_ret); + if(ret != RET_OK){ + printf("[vlan]add_vlan_sub_interface: br_bridge_add failed(%d)(%s)\n", sys_ret, strerror(sys_ret)); + return ret; + } + } + + /* ӽӿӽbr */ + br_if_bridge_add(br_vlname, subif, 1, &sys_ret); + if(ret != RET_OK){ + printf("[vlan]add_vlan_sub_interface: br_if_bridge_add failed(%d)(%s)\n", sys_ret, strerror(sys_ret)); + return ret; + } + memset(subif, 0, IF_NAME_LEN); + memset(br_vlname, 0, BR_VLAN_NAME_LEN); + } + } + return RET_OK; +} + + +/************************************************************ +* :vlan trunk +* : +* : +* ֵ: +************************************************************/ +ret_code add_interface_vlan_trunk(char *if_name, int *vlan_bitmap, interface_model if_model) +{ + ret_code ret = RET_OK; + /* Ž */ + if(INTERFACE_MODEL_FORWARD == if_model){ + ret = add_bridge_vlan_trunk(if_name, vlan_bitmap); + } + + /* ·Ž,贴ӽӿںbr-vlan */ + else if(INTERFACE_MODEL_ROUTE_FORWARD == if_model){ + ret = add_vlan_sub_interface(if_name, vlan_bitmap); + } + + return ret; +} + + +/************************************************************ +* :ӽӿvlan +* : +* : +* ֵ: +************************************************************/ +ret_code add_interface_vlan(interface_vlan_info *if_vlan_info) +{ + interface_link_attr new_attr = if_vlan_info->attr; + ret_code ret = RET_OK; + /* У */ + //ret = check_config(if_vlan_info->if_name); + //if(0 != ret){ + //return -1; + //} + + /* ӿģ͸ֵ */ + if_vlan_info->if_model = which_interface_model(if_vlan_info->if_name); + + /* ж· */ + if(new_attr == LINK_TYPE_TRUNK){ + ret = add_interface_vlan_trunk(if_vlan_info->if_name, if_vlan_info->vlan_bitmap, if_vlan_info->if_model); + if(ret != RET_OK){ + printf("[vlan]add_interface_vlan: add trunk failed.\n"); + return ret; + } + } + else if(new_attr == LINK_TYPE_ACCESS){ + ret = add_interface_vlan_access(if_vlan_info->if_name, if_vlan_info->vlan_bitmap, if_vlan_info->if_model); + if(ret != RET_OK){ + printf("[vlan]add_interface_vlan: add access failed.\n"); + return ret; + } + } + else{ + printf("[vlan]add_interface_vlan: LINK-TYPE attr(%d) input error.\n", new_attr); + return RET_INPUTERR; + } + + /* ȫֽӿvlanݽṹ */ + ret = refresh_if_vlan_info(if_vlan_info); + return ret; +} + +/************************************************************ +* :ɾbridge vlan +* Ŀǰò,Ŀǰ֧Ž +* : +* : +* ֵ: +************************************************************/ +int del_bridge_vlan_access(char *if_name) +{ + int vid; + char buf[SYSCALL_BUF_LEN] = {0}; + int ifnode; + ifnode = get_ifnode_from_global(if_name); + + /* ڵiٱVLANҵýڵеvlan j,jж + ҵһj,ƴӳһӽӿ,ɾӽӿ*/ + for(vid = VID_MIN; vid <= VID_MAX; vid++){ + if(vlan_bitmap_check(g_if_vlan_info[ifnode].vlan_bitmap, vid)){ + sprintf(buf, "bridge vlan del vid %d dev %s pvid untagged", vid, if_name); + system(buf); + memset(buf, 0, SYSCALL_BUF_LEN); + sprintf(buf, "bridge vlan add vid %d dev %s pvid untagged", 1, if_name); + system(buf); + memset(buf, 0, SYSCALL_BUF_LEN); + } + } + return 0; +} + +/************************************************************ +* :ɾaccess vlan +* Ŀǰò,Ŀǰ֧Ž +* : +* : +* ֵ: +************************************************************/ +int del_interface_vlan_access(char *if_name, interface_model if_model) +{ + int ret; + /* ӿںģ */ + if(INTERFACE_MODEL_FORWARD != if_model){ + printf("accessŽģ,򱨴\n"); + return -1; + } + ret = del_bridge_vlan_access(if_name); + return ret; +} + +/************************************************************ +* :ɾtrunk vlan +* : +* : +* ֵ: +************************************************************/ +int del_bridge_vlan_trunk(char *if_name) +{ + int ifnode, vid; + char buf[SYSCALL_BUF_LEN] = {0}; + + /* ifnameȫֽṹĸڵi,iֻһ */ + for(ifnode = 0; ifnode < MAX_INTERFACES; ifnode++){ + if(!strcmp(g_if_vlan_info[ifnode].if_name, if_name)){ + break; + } + } + /* ûҵ */ + if(ifnode >= MAX_INTERFACES){ + return -1; + } + + /* ڵiٱVLANҵýڵеvlan j,jж */ + for(vid = 1; vid < MAX_VLAN; vid++){ + //if(g_if_vlan_info[i].vid[j]){ + if(vlan_bitmap_check(g_if_vlan_info[ifnode].vlan_bitmap, vid)){ + sprintf(buf, "bridge vlan del vid %d dev %s", vid, if_name); + system(buf); + memset(buf, 0, SYSCALL_BUF_LEN); + } + } + + /* ָvlan 1 pvid untagged */ + sprintf(buf, "bridge vlan add vid %d dev %s pvid untagged", 1, if_name); + system(buf); + return 0; +} + +interface_network_type lan_or_wan_stub(char *if_name) +{ + return IF_TYPE_LAN; +} + +/************************************************************ +* :ļLANWAN +* : +* : +* ֵ: +************************************************************/ + +interface_network_type lan_or_wan(char *if_name) +{ + return lan_or_wan_stub(if_name); +} + +/************************************************************ +* :ɾvlanӽӿ +* ﲻУ,ǰchkУ +* : +* : +* ֵ: +************************************************************/ +ret_code del_vlan_sub_interface(char *if_name, int *vlan_bitmap) +{ + int ifnode, vid; + char br_vlname[BR_VLAN_NAME_LEN] = {0}; + char tmp_brname[BR_VLAN_NAME_LEN] = {0}; + char subif[IF_NAME_LEN]= {0}; + char buf[SYSCALL_BUF_LEN] = {0}; + int sys_ret = 0, status = 0, num = 0; + ret_code ret = RET_OK; + if(!if_name || !vlan_bitmap){ + return -1; + } + printf("[vlan]del_vlan_sub_interface: begin.\n"); + + /* ڵiٱVLANҵýڵеvlan j,jж + ҵһj,ƴӳһӽӿ,ɾӽӿ*/ + for(vid = VID_MIN; vid <= VID_MAX; vid++){ + if(vlan_bitmap_check(vlan_bitmap, vid)){//ͲУ,·ĸvid,ɾĸ + /* ӽӿ */ + (void)if_vlan2subif(if_name, vid, subif); + + /* ѯǷ */ + ret = br_if_bridge_get(if_name, tmp_brname); + + /* ûҵ,WAN,ֻɾVLANӽӿ */ + if(RET_NOTFOUND == ret){ + printf("[vlan]del_vlan_sub_interface: %s is WAN,only del vlansubif\n", if_name); + /* ɾvlanӽӿ */ + sprintf(buf, "vconfig rem %s", subif); + printf("[vlan]del_vlan_sub_interface: buf=%s\n", buf); + status = system(buf); + if(status != 0){ + printf("[vlan]del_vlan_sub_if:status error. (%s)\n", strerror(errno)); + return RET_ERR; + } + memset(buf, 0, SYSCALL_BUF_LEN); + memset(tmp_brname, 0, BR_VLAN_NAME_LEN); + memset(subif, 0, IF_NAME_LEN); + } + /* ҵ,LAN */ + else if(RET_OK == ret){ + sprintf(br_vlname, "br-vl%d", vid); + printf("[vlan]del_vlan_sub_interface: br_vlname=%s\n", br_vlname); + + /* br-vlxx, */ + if(br_is_exist(br_vlname) == FALSE){ + printf("[vlan]del_vlan_sub_interface: %s is not exist, error,return.\n", br_vlname); + return RET_ERR; + } + + /* brctl delifɾbr-vlϵvlanӽӿ */ + printf("[vlan]del_vlan_sub_interface: br-vl%d delif %s\n", vid, subif); + ret = br_if_bridge_del(br_vlname, subif, 1, &sys_ret); + if(ret != RET_OK){ + printf("[vlan]del_vlan_sub_interface: br_if_bridge_del error(%d).\n", sys_ret); + return ret; + } + + /* ɾvlanӽӿ */ + sprintf(buf, "vconfig rem %s", subif); + printf("[vlan]del_vlan_sub_interface: buf=%s\n", buf); + status = system(buf); + if(status != 0){ + printf("[vlan]del_vlan_sub_if:status error. (%s)\n",strerror(errno)); + return RET_ERR; + } + memset(buf, 0, SYSCALL_BUF_LEN); + + + /* br-vlanϵĽӿΪ0,ɾbr-vlan */ + num = br_if_bridge_num(br_vlname); + printf("[vlan]del_vlan_sub_interface: num=%d\n", num); + if(num == 0){ + printf("[vlan]del_vlan_sub_interface: br's port is zero.\n"); + ret = br_bridge_del(br_vlname, &sys_ret); + if(ret != RET_OK){ + printf("[vlan]del_vlan_sub_interface: del br failed(%d)(%s).\n", sys_ret, strerror(sys_ret)); + return ret; + } + } + memset(br_vlname, 0, BR_VLAN_NAME_LEN); + memset(subif, 0, IF_NAME_LEN); + memset(buf, 0, SYSCALL_BUF_LEN); + } + } + } + return RET_OK; +} + +/************************************************************ +* :ɾvlan trunk +* : +* : +* ֵ: +************************************************************/ +ret_code del_interface_vlan_trunk(char *if_name, int *vlan_bitmap, interface_model if_model) +{ + ret_code ret = RET_OK; + /* Ž */ + if(INTERFACE_MODEL_FORWARD == if_model){ + ret = del_bridge_vlan_trunk(if_name); + } + + /* ·Ž,贴ӽӿںbr-vlan */ + else if(INTERFACE_MODEL_ROUTE_FORWARD == if_model){ + ret = del_vlan_sub_interface(if_name, vlan_bitmap); + } + + return ret; +} + + +/************************************************************ +* :ȫif_vlanṹв֮ǰùĽڵ +* սڵ,Ա +* ,һǵǰõĽӿڣ֮ǰѾù,ƥ +* ڴνڵ¾,֮ǰûù,ҵ½ڵ +* : +* : +* ֵ: -1:ûпλ + ifnode:ifnodeλ +************************************************************/ +int find_insert_ifnode_from_global(char *if_name) +{ + int ifnode; + if(!if_name){ + return -1; + } + + /* ҵ֮ǰѾùĽڵλ */ + for(ifnode = 0; ifnode < MAX_INTERFACES; ifnode++){ + if(!strcmp(g_if_vlan_info[ifnode].if_name, if_name)){ + printf("[vlan]find_empty_ifnode_from_global: find exist gnode[%d].%s\n", ifnode, if_name); + return ifnode; + } + } + + /* ҵһյ, */ + for(ifnode = 0; ifnode < MAX_INTERFACES; ifnode++){ + if(!strcmp(g_if_vlan_info[ifnode].if_name, "")){ + printf("[vlan]find_empty_ifnode_from_global: find empty gnode[%d]\n", ifnode); + return ifnode; + } + } + + /* ˵,ûпյĿɲϢĽڵ */ + return -1; +} + +/************************************************************ +* :ADDȫֽṹ +* : +* : +* ֵ: +************************************************************/ +ret_code refresh_if_vlan_info_op_add(interface_vlan_info *if_vlan_info) +{ + int ifnode; + int vid; + if(!if_vlan_info){ + return -1; + } + ifnode = find_insert_ifnode_from_global(if_vlan_info->if_name); + + /* ûҵհ׿ɲϢĽڵ,add */ + if(-1 == ifnode){ + printf("[vlan]refresh_if_vlan_info_op_add: donot find empty node.\n"); + return -1; + } + printf("[vlan]refresh_if_vlan_info_op_add: find node[%d].ifname=%s\n", ifnode, if_vlan_info->if_name); + + /* ifname, attr, op_type, if_model */ + memcpy(g_if_vlan_info[ifnode].if_name, if_vlan_info->if_name, strlen(if_vlan_info->if_name)); + g_if_vlan_info[ifnode].attr = if_vlan_info->attr; + g_if_vlan_info[ifnode].op_type = if_vlan_info->op_type; + g_if_vlan_info[ifnode].if_model = if_vlan_info->if_model; + printf("[vlan]refresh_op_add: node[%d]'s info::\nif_name=%s, attr=%d, op_type=%d, if_model=%d\n", + ifnode, if_vlan_info->if_name, if_vlan_info->attr, + if_vlan_info->op_type, if_vlan_info->if_model); + /* vlanbitmap */ + printf("[vlan]refresh_op_add: vlan_info:\n<"); + for(vid = VID_MIN; vid <= VID_MAX; vid++){ + /* ҵvid,ȫֽṹvlan_bitmapvid bitmap */ + if(vlan_bitmap_check(if_vlan_info->vlan_bitmap, vid)){ + vlan_bitmap_set(g_if_vlan_info[ifnode].vlan_bitmap, vid); + g_if_vlan_info[ifnode].vidcnt++; + printf("%d, ", vid); + } + } + printf(">\n"); + return 0; +} + + +/************************************************************ +* :DELȫֽṹ +* : +* : +* ֵ: +************************************************************/ +ret_code refresh_if_vlan_info_op_del(interface_vlan_info *if_vlan_info) +{ + int ifnode; + int vid = 0; + int vcnt = 0; + if(!if_vlan_info){ + return -1; + } + ifnode = get_ifnode_from_global(if_vlan_info->if_name); + /* ==-1˵ûҵ,Ͳdel */ + if(-1 == ifnode){ + return -1; + } + + /* ûҵӦĽڵ */ + if(ifnode >= MAX_INTERFACES){ + return -1; + } +#if 0 + /* ifnodeڵϢ */ + memset(&g_if_vlan_info[ifnode], 0, sizeof(g_if_vlan_info[ifnode])); + printf("\n\n\n\n\n[vlan]refresh_op_del: g.ifname=%s, g.attr=%d, g.op_type=%d, g.ifmodel=%d\n", + g_if_vlan_info[ifnode].if_name, g_if_vlan_info[ifnode].attr, + g_if_vlan_info[ifnode].op_type, g_if_vlan_info[ifnode].if_model); + printf("[vlan]refresh_op_del: if_vlan_info vid:\n"); + for(vid = 2; vid < MAX_VLAN; vid++){ + if(vlan_bitmap_check(if_vlan_info->vlan_bitmap, vid)){ + printf("%d\n", vid); + } + } + + printf("[vlan]refresh_op_del: g_if_vlan_info vid:\n"); + for(vid = 2; vid < MAX_VLAN; vid++){ + if(vlan_bitmap_check(g_if_vlan_info[ifnode].vlan_bitmap, vid)){ + printf("%d\n", vid); + } + } +#endif + /* ӡ֮ǰȫgṹϢ */ + printf("[vlan]refresh_if_vlan_info_op_del: Before Gloabl node information:::::\n"); + printf("[vlan]refresh_if_vlan_info_op_del: nodeid=%d, if_name=%s, op_type=%d, attr=%d, vid=[" + , ifnode, g_if_vlan_info[ifnode].if_name, g_if_vlan_info[ifnode].op_type + , g_if_vlan_info[ifnode].attr); + + for(vid = VID_MIN; vid <= VID_MAX; vid++){ + if(vlan_bitmap_check(g_if_vlan_info[ifnode].vlan_bitmap, vid)){ + printf("%d, ", vid); + } + } + printf("]\n"); + + /* ӡ·ṹϢ */ + printf("[vlan]refresh_if_vlan_info_op_del: config if_vlan_info node information:::::\n"); + printf("[vlan]refresh_if_vlan_info_op_del: if_name=%s, op_type=%d, attr=%d, vid=[" + , if_vlan_info->if_name, if_vlan_info->op_type + , if_vlan_info->attr); + + for(vid = VID_MIN; vid <= VID_MAX; vid++){ + if(vlan_bitmap_check(if_vlan_info->vlan_bitmap, vid)){ + printf("%d, ", vid); + } + } + printf("]\n"); + + printf("[vlan]refresh_if_vlan_info_op_del: if_vlan_info delete vid:["); + for(vid = VID_MIN; vid <= VID_MAX; vid++){ + /* ȥ·ṹif_vlan_infoȡõvid, + ھɵĽṹȫg_if_vlan_infoЩvid */ + if(vlan_bitmap_check(if_vlan_info->vlan_bitmap, vid)){ + printf("%d, ", vid); + vlan_bitmap_clear(g_if_vlan_info[ifnode].vlan_bitmap, vid); + g_if_vlan_info[ifnode].vidcnt--; + } + } + printf("]\n"); + printf("[vlan]refresh_if_vlan_info_op_del: g_[%d].vidcnt=%d\n", ifnode, g_if_vlan_info[ifnode].vidcnt); + + /* ӿϻvid,򲻴,û,ӿϢ */ + /* vcnt == 0 ˵idnodeѾûvid,ifnodeϢ */ + if(g_if_vlan_info[ifnode].vidcnt == 0){ + printf("[vlan]refresh_if_vlan_info_op_del: clean g_if_vlan_info[%d] info.\n", ifnode); + memset(&g_if_vlan_info[ifnode], 0, sizeof(interface_vlan_info)); + } +#if 0 + printf("[vlan]refresh_if_vlan_info_op_del: g_vlan_info[%d],vid left:[", ifnode); + for(vid = VID_MIN; vid <= VID_MAX; vid++){ + if(vlan_bitmap_check(g_if_vlan_info[ifnode].vlan_bitmap, vid)){ + printf("%d, ", vid); + vcnt++; + } + } + printf("]\n"); + /* vcnt == 0 ˵idnodeѾûvid,ifnodeϢ */ + if(vcnt == 0){ + printf("[vlan]refresh_if_vlan_info_op_del: clean g_if_vlan_info[%d] info.\n", ifnode); + memset(&g_if_vlan_info[ifnode], 0, sizeof(interface_vlan_info)); + } +#endif + return 0; +} + +/************************************************************ +* :ifnameȡȫֽṹڽڵ +* : +* : +* ֵ: -1:ûҵ + ifnode: ifnodeڵ +************************************************************/ +int get_ifnode_from_global(char *if_name) +{ + int ifnode; + if(!if_name){ + return -1; + } + if(!strcmp(if_name, "")){ + printf("[vlan]get_ifnode_from_global: if_name is empty.\n"); + return -1; + } + for(ifnode = 0; ifnode < MAX_INTERFACES; ifnode++){ + if(!strcmp(g_if_vlan_info[ifnode].if_name, if_name)){ + printf("[vlan]get_ifnode_from_global: find gnode[%d], ifname=%s\n", ifnode, if_name); + return ifnode; + } + } + return -1; +} + +/************************************************************ +* :ADDļ +* : +* : +* ֵ: +************************************************************/ +ret_code vlan_save_conf_file_add(interface_vlan_info *if_vlan_info) +{ + int vid; + char subif[IF_NAME_LEN] = {0}; + char *addr_name = "address"; + char addr_buff[IF_BUFF_LEN] = {0}; + if(!if_vlan_info){ + return RET_NULLP; + } + printf("[vlan]save_conf_file_add: begin.\n"); + sprintf(addr_buff, "address %d\n", 0); + for(vid = VID_MIN; vid <= VID_MAX; vid++){ + if(vlan_bitmap_check(if_vlan_info->vlan_bitmap, vid)){ + (void)if_vlan2subif(if_vlan_info->if_name, vid, subif); + ip_conf_file_set(subif, addr_name, addr_buff); + } + } + return RET_OK; +} + +ret_code vlan_conf_file_del(char *if_name, char *address) +{ + if(!if_name || !address){ + return RET_NULLP; + } + char buf[100] = {0}; + int status = 0; + //sprintf(buf, "sed -r -i ':a;N;$!ba;s/auto[ \\t]+%s\\n[ \\t]*iface[ \\t]+%s[ \\t]+inet[ \\t]+static\\n[ \\t]*address[ \\t]+%s\\n//g' %s", if_name, if_name, address, ETC_NETWORK_IFS); + + sprintf(buf, "sed -r -i '/^auto[ \\t]+%s*$/d' %s", if_name, ETC_NETWORK_IFS); + status = system(buf); + if(status != 0){ + printf("[vlan]vlan_conf_file_del1: status error. (%s)\n", strerror(errno)); + return RET_SYSERR; + } + memset(buf, 0, 100); + + sprintf(buf, "sed -r -i '/^iface[ \\t]+%s[ \\t]+inet[ \\t]+static*$/,+1d' %s", if_name, ETC_NETWORK_IFS); + status = system(buf); + if(status != 0){ + printf("[vlan]vlan_conf_file_del2: status error. (%s)\n", strerror(errno)); + return RET_SYSERR; + } + + memset(buf, 0, 100); + + //sprintf(buf, "sed -r -i '/^address[ \\t]+%s*$/d' %s", address, ETC_NETWORK_IFS); + //system(buf); + + return RET_OK; +} +/************************************************************ +* :DELļ +* : +* : +* ֵ: +************************************************************/ +ret_code vlan_save_conf_file_del(interface_vlan_info *if_vlan_info) +{ + //int ifnode; + int vid; + char subif[IF_NAME_LEN] = {0}; + char *del_address = "0"; + ret_code ret = RET_OK; + if(!if_vlan_info){ + return RET_NULLP; + } + //ifnode = get_ifnode_from_global(if_vlan_info->if_name); + for(vid = VID_MIN; vid <= VID_MAX; vid++){ + if(vlan_bitmap_check(if_vlan_info->vlan_bitmap, vid)){ + (void)if_vlan2subif(if_vlan_info->if_name, vid, subif); + printf("[vlan]vlan_save_conf_file_del: vid=%d, subif=%s\n", vid, subif); + ret = vlan_conf_file_del(subif, del_address); + if(ret != RET_OK){ + printf("[vlan]vlan_save_conf_file_del: vlan-conffile del failed(%d).\n", ret); + return ret; + } + } + } + return RET_OK; +} + +/************************************************************ +* :ļ +* : +* : +* ֵ: +************************************************************/ +ret_code vlan_save_conf_file(interface_vlan_info *if_vlan_info) +{ + ret_code ret = RET_OK; + if(!if_vlan_info){ + return RET_NULLP; + } + if(if_vlan_info->op_type == OP_ADD){ + ret = vlan_save_conf_file_add(if_vlan_info); + } + else if(if_vlan_info->op_type == OP_DEL){ + ret = vlan_save_conf_file_del(if_vlan_info); + } + else{ + printf("[vlan]vlan_save_conf_file: OP_TYPE ERROR.return\n"); + ret = RET_UNKNOWN; + } + return ret; +} + +/************************************************************ +* :ȫֽṹ +* : +* : +* ֵ: +************************************************************/ +ret_code refresh_if_vlan_info(interface_vlan_info *if_vlan_info) +{ + ret_code ret = RET_OK; + if(!if_vlan_info){ + return RET_NULLP; + } + + /* ·ŽģͲŻõӽӿ,Ҫļ */ + if(INTERFACE_MODEL_ROUTE_FORWARD == if_vlan_info->if_model){ + ret = vlan_save_conf_file(if_vlan_info); + if(ret != RET_OK){ + printf("[vlan]refresh_if_vlan_info: save conf file failed.return.(%d)\n", ret); + return ret; + } + } + if(OP_ADD == if_vlan_info->op_type){ + ret = refresh_if_vlan_info_op_add(if_vlan_info); + } + else if(OP_DEL == if_vlan_info->op_type){ + ret = refresh_if_vlan_info_op_del(if_vlan_info); + } + else { + printf("[vlan]refresh_if_vlan_info: OP_TYPE ERROR.return\n"); + ret = RET_UNKNOWN; + } + + return ret; +} + +/************************************************************ +* :ɾӿvlan +* ˺϶ָ˿ɾĬɾ˿vlanϢ +* ɾԭӿijvlan,ɾԭӿijvlan +* ͨmodûҪͨdel +* : +* : +* ֵ: +************************************************************/ +ret_code del_interface_vlan(interface_vlan_info *if_vlan_info) +{ + ret_code ret = RET_OK; + if(!if_vlan_info){ + return RET_NULLP; + } + /* У */ + //ret = check_config(if_vlan_info->if_name); + //if(0 == ret){//Ϊ0ʾ֮ǰûãش൱ADD + //return -1; + //} + + /* ӿģ͸ֵ */ + if_vlan_info->if_model = which_interface_model(if_vlan_info->if_name); + + /* ѯǰȫֽṹдportattr */ + interface_link_attr old_attr = get_attr_from_ifname(if_vlan_info->if_name); + + /* trunk */ + if(old_attr == LINK_TYPE_TRUNK){ + ret = del_interface_vlan_trunk(if_vlan_info->if_name, if_vlan_info->vlan_bitmap, if_vlan_info->if_model); + if(ret != RET_OK){ + printf("[vlan]del_interface_vlan: del trunk failed(%d).\n", ret); + return ret; + } + } + else if(old_attr == LINK_TYPE_ACCESS){ + ret = del_interface_vlan_access(if_vlan_info->if_name, if_vlan_info->if_model); + if(ret != RET_OK){ + printf("[vlan]del_interface_vlan: del access failed(%d).\n", ret); + return ret; + } + } + else { + printf("[vlan]del_interface_vlan: LINK-TYPE attr(%d) input error.\n", old_attr); + return RET_UNKNOWN; + } + /* ȫֽӿvlanݽṹ */ + ret = refresh_if_vlan_info(if_vlan_info); + return ret; +} + + +/************************************************************ +* :ȫgloablṹлȡoldĽӿmodel +* : +* : +* ֵ: +************************************************************/ +interface_model get_old_interface_model(char *if_name, interface_model *if_model) +{ + int ifnode; + if(!if_name || !if_model){ + printf("[vlan]get_old_interface_model: input or output para error.\n"); + return -1; + } + ifnode = get_ifnode_from_global(if_name); + if(-1 == ifnode){ + return -1; + } + *if_model = g_if_vlan_info[ifnode].if_model; + return 0; +} + + +/************************************************************ +* :ж¾ɽӿģǷһ +* : +* : +* ֵ: 0: һ£û + -1: һ£ +************************************************************/ +int if_model_is_change(interface_model old, interface_model new) +{ + if(old == new){ + return 0; + } + else{ + return -1; + } +} + +int get_old_interface_attr(char *if_name, interface_link_attr *attr) +{ + int ifnode; + if(!if_name || !attr){ + printf("[vlan]get_old_interface_attr: input or output para error.\n"); + return -1; + } + ifnode = get_ifnode_from_global(if_name); + if(-1 == ifnode){ + return -1; + } + *attr = g_if_vlan_info[ifnode].attr; + return 0; +} + +/************************************************************ +* :ӿvlan +* ݲֱaddmoddel +* addֱӼ +* delֱɾ +* : +* : +* ֵ: +************************************************************/ +ret_code interface_vlan_set(interface_vlan_info *if_vlan_info) +{ + operation_type op_type; + ret_code ret = RET_OK; + op_type = if_vlan_info->op_type; + if(!if_vlan_info){ + return RET_NULLP; + } + /* ADDֻ */ + if(op_type == OP_ADD){ + ret = add_interface_vlan(if_vlan_info); + } + + /* ֻǰ */ + else if(op_type == OP_DEL){ + ret = del_interface_vlan(if_vlan_info); + } + else{ + printf("[vlan]interface_vlan_set: op_type error.\n"); + ret = RET_INPUTERR; + } + return ret; +} + + +/************************************************************ +* : ѭjsonֶεֵӽport_vlan_info[] +* ṹ +* ֻADDDELҪchkУ,getgetallҪ,ֱӷ +* ÿһprocһoperate +* { +* "operate" : "add", +* "conf-ifvlan": [ +* { +* "if-name" : "eth0", +* "vid" : [ +* 100, +* 200, +* ], +* "if-attr" : "trunk" +* }, +* { +* "if-name" : "eth1", +* "vid" : [ +* 300, +* 400, +* ], +* "if-attr" : "trunk" +* }, +* ] +* } +* : +* : if_vlnumif_vlan_info +* ֵ: +************************************************************/ +ret_code vlan_config_json_parse(pointer input, interface_vlan_info *if_vlan_info, int *if_vlnum) +{ + ret_code ret = RET_OK; + int i, if_num = 0, vl_num = 0, j; + cJSON *root = NULL, *operate = NULL, *conf_ifvlan = NULL; + if(!if_vlan_info || !input){ + ret = RET_NULLP; + goto out; + } + if(!(root = cJSON_Parse(input))){ + printf("[vlan-test]vlan_config_json_parse: json parse error, return error.\n"); + ret = RET_INPUTERR; + goto out; + } + if(!(operate = cJSON_GetObjectItem(root, "operate"))){ + printf("[vlan]vlan_config_json_parse: No operate node.\n"); + ret = RET_INPUTERR; + goto out; + } + if(!(conf_ifvlan = cJSON_GetObjectItem(root, "conf-ifvlan"))){ + ret = RET_INPUTERR; + goto out; + } + + /* interface */ + if_num = cJSON_GetArraySize(conf_ifvlan); + if(if_num == 0){ + printf("[vlan]vlan_config_json_parse: json array has no interface.\n"); + ret = RET_INPUTERR; + goto out; + } + cJSON *node; + for(i = 0; i < if_num; i++){ + + /* ÿӿڵop_typeֵ */ + if(NULL == operate->valuestring){ + printf("[vlan]vlan_config_json_parse: operate->valuestring is null.\n"); + ret = RET_INPUTERR; + goto out; + } + if(!strcmp(operate->valuestring, "add")){ + if_vlan_info[i].op_type = OP_ADD; + } + else if(!strcmp(operate->valuestring, "del")){ + if_vlan_info[i].op_type = OP_DEL; + } + else if(!strcmp(operate->valuestring, "mod")){ + if_vlan_info[i].op_type = OP_MOD; + } + + node = cJSON_GetArrayItem(conf_ifvlan, i); + if(!node){ + continue; + } + cJSON *ifName = cJSON_GetObjectItem(node, "if-name"); + if(!ifName){ + continue; + } + if(NULL == ifName->valuestring){ + printf("[vlan]vlan_config_json_parse: ifName->valuestring is null.\n"); + ret = RET_INPUTERR; + goto out; + } + memcpy(if_vlan_info[i].if_name, ifName->valuestring, strlen(ifName->valuestring)); + printf("[vlan]if_vlan_info[%d].if_name=%s\n", i, if_vlan_info[i].if_name); + cJSON *ifAttr = cJSON_GetObjectItem(node, "if-attr"); + if(!ifAttr){ + continue; + } + if(NULL == ifAttr->valuestring){ + printf("[vlan]vlan_config_json_parse: ifAttr->valuestring is null.\n"); + ret = RET_INPUTERR; + goto out; + } + if(!strcmp(ifAttr->valuestring, "trunk")){ + if_vlan_info[i].attr = LINK_TYPE_TRUNK; + } + else if(!strcmp(ifAttr->valuestring, "access")){ + if_vlan_info[i].attr = LINK_TYPE_ACCESS; + } + cJSON *vId = cJSON_GetObjectItem(node, "vid"); + if(!vId){ + continue; + } + + /* ڲvid */ + vl_num = cJSON_GetArraySize(vId); + if(vl_num == 0){ + printf("[vlan]vlan_config_json_parse: json array has no vid.\n"); + ret = RET_INPUTERR; + goto out; + } + /* Уvid,߽, */ + ret = vid_num_chk(if_vlan_info[i].if_name, if_vlan_info[i].op_type, vl_num); + if(ret != RET_OK){ + printf("[vlan]vlan_config_json_parse: vid-num-chk error.\n"); + goto out; + } + cJSON *item = NULL; + for(j = 0; j < vl_num; j++){ + item = cJSON_GetArrayItem(vId, j); + printf("i=%d, vid=%d\n", i, item->valueint); + + /* vidֵΧУ */ + ret = vid_value_chk(item->valueint); + if(ret != RET_OK){ + printf("[vlan]vlan_config_json_parse: vid check failed.\n"); + ret = RET_INPUTERR; + goto out; + } + vlan_bitmap_set(if_vlan_info[i].vlan_bitmap, item->valueint); + } + } + if(if_vlnum){ + *if_vlnum = if_num; + } + + +out: + if(root){ + cJSON_Delete(root); + } + return ret; +} + + +/************************************************************ +* :vlanģù +* : +* : +* ֵ: +************************************************************/ +ret_code vlan_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + operation_type op_type; + int if_vl_num = 0, i; + ret_code ret = RET_OK; + interface_vlan_info if_vlan_info[MAX_INTERFACES] = {0}; + printf("[vlan]===config-proc begin.===\n"); + /* · */ + if(source == CONFIG_FROM_WEB){ + ret = vlan_config_json_parse(input, if_vlan_info, &if_vl_num); + if(ret != RET_OK){ + printf("[vlan]vlan_config_proc: source:%d, json parse failed(%d).\n", source, ret); + return ret; + } + for(i = 0; i < if_vl_num; i++){ + ret = interface_vlan_set(&if_vlan_info[i]); + if(ret != RET_OK){ + printf("[vlan]vlan_config_proc: source:%d, vlan set failed(%d).\n", source, ret); + return ret; + } + } + } + + /* ûָ */ + else if(source == CONFIG_FROM_RECOVER1){ + ret = vlan_config_json_parse(input, if_vlan_info, &if_vl_num); + if(ret != RET_OK){ + printf("[vlan]vlan_config_proc: source:%d, json parse failed(%d).\n", source, ret); + return ret; + } + + for(i = 0; i < if_vl_num; i++){ + ret = interface_vlan_set(&if_vlan_info[i]); + if(ret != RET_OK){ + printf("[vlan]vlan_config_proc: source:%d, vlan set failed(%d).\n", source, ret); + return ret; + } + } + } + + return RET_OK; +} + +/************************************************************ +* :·getjson +* ifnameȥȫֽṹвѯ,ڵ,ڽڵ± +* interfaceṹ,Աʹ +* getʽ +* { +* "get-ifvlan": [ +* { +* "if-name" : "eth0", +* }, +* { +* "if-name" : "eth1", +* }, +* } +* +* ݸʽ +* { +* "if-vlan": [ +* { +* "if-name" : "eth0", +* "vid" : [ +* 100, +* 200, +* ], +* "if-attr" : "trunk" +* }, +* { +* "if-name" : "eth1", +* "vid" : [ +* 300, +* 400, +* ], +* "if-attr" : "trunk" +* }, +* } +* : +* : +* ֵ: +************************************************************/ +ret_code vlan_get_json_parse(int *interface, pointer input) +{ + ret_code ret = RET_OK; + int i, if_num, vl_num, j, ifnode; + cJSON *root = NULL, *operate = NULL, *get_ifvlan = NULL; + if(!interface){ + printf("[vlan]vlan_get_json_parse: interface is null.\n"); + ret = RET_NULLP; + goto out; + } + if(!(root = cJSON_Parse(input))){ + printf("[vlan]vlan_get_json_parse: root failed.\n"); + ret = RET_INPUTERR; + goto out; + } + if(!(operate = cJSON_GetObjectItem(root, "operate"))){ + printf("[vlan]vlan_get_json_parse: No operate node.\n"); + ret = RET_INPUTERR; + goto out; + } + //if(operate->valuestring == NULL || strcmp(operate->valuestring, "get") != 0){ + //printf("[vlan]vlan_get_json_parse: error: operate-val is %s.\n", operate->valuestring); + //ret = RET_INPUTERR; + //goto out; + //} + + if(!(get_ifvlan = cJSON_GetObjectItem(root, "get-ifvlan"))){ + printf("[vlan]vlan_get_json_parse: No get-ifvlan node.\n"); + ret = RET_INPUTERR; + goto out; + } + + /* interface */ + if_num = cJSON_GetArraySize(get_ifvlan); + if(if_num == 0){ + printf("[vlan]vlan_get_json_parse: json array has no interface.\n"); + ret = RET_INPUTERR; + goto out; + } + cJSON *node = NULL; + for(i = 0; i < if_num; i++){ + node = cJSON_GetArrayItem(get_ifvlan, i); + if(!node){//??? + continue; + } + cJSON *ifName = cJSON_GetObjectItem(node, "if-name"); + if(!ifName){ + continue; + } + //if_vlan_info[i].if_name= ifName->valuestring;//memcpy + /* ҵ˶Ӧifnode,Ӧ±1 */ + ifnode = get_ifnode_from_global(ifName->valuestring); + if(ifnode != -1){ + interface[ifnode] = 1; + } + } + +out: + if(root){ + cJSON_Delete(root); + } + return ret; +} + +/************************************************************ +* :interfaceΪ1򽫴±Ϊȫֽṹ +* ±,ȡӦڵװjson +* : +* : +* ֵ: +************************************************************/ +ret_code vlan_config_format_json(int *interface, pointer output, int *output_len) +{ + cJSON *root = NULL, *arr = NULL, *js_list = NULL, *js_vid = NULL; + char *out = NULL; + int i, j, vid; + root = cJSON_CreateObject(); + if(!root){ + printf("[vlan]cJSON_CreateObject1 error.\n"); + return RET_ERR; + } + cJSON_AddItemToObject(root, "if-vlan", arr = cJSON_CreateArray()); + for(i = 0; i < MAX_INTERFACES; i++){ + //ifnode = get_ifnode_from_global(if_vlan_info[i].if_name) + if(!(interface[i])){ + continue; + } + js_list = cJSON_CreateObject(); + if(!js_list){ + printf("[vlan]cJSON_CreateObject2 error.\n"); + cJSON_Delete(root); + return RET_ERR; + } + + cJSON_AddStringToObject(js_list , "if-name", g_if_vlan_info[i].if_name); + cJSON_AddNumberToObject(js_list , "if-attr", g_if_vlan_info[i].attr); + cJSON_AddItemToObject(js_list, "vid", js_vid = cJSON_CreateArray()); + for(vid = 0; vid < MAX_VLAN;vid++){ + if(vlan_bitmap_check(g_if_vlan_info[i].vlan_bitmap, vid)){ + cJSON_AddItemToArray(js_vid, cJSON_CreateNumber(vid)); + } + } + + cJSON_AddItemToArray(arr, js_list); + } + out = cJSON_PrintUnformatted(root); + if(!out){ + printf("[vlan]vlan_config_format_json: cJSON_PrintUnformatted failed.\n"); + cJSON_Delete(root); + return RET_ERR; + } + *output_len = strlen(out) + 1; + memcpy(output, out, *output_len); + xfree(out); + cJSON_Delete(root); + return RET_OK; +} + + +/************************************************************ +* :vlanget +* : +* : +* ֵ: +************************************************************/ +ret_code vlan_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len) +{ + //interface_vlan_info if_vlan_info[MAX_INTERFACES] = {0}; + int if_vlnum = 0; + ret_code ret = RET_OK; + int interface[MAX_INTERFACES] = {0}; + if(!input){ + printf("[vlan]vlan_config_get: input is null.\n"); + return RET_INPUTERR; + } + printf("[vlan]===config-get begin.===\n"); + /* ƥĽڵ±괫interfaceжӦ± */ + ret = vlan_get_json_parse(interface, input); + if(ret != RET_OK){ + printf("[vlan]vlan_config_get: get json parse failed.\n"); + return ret; + } + /* ݶӦinterface±,ȥȡȫg_if_vlan_infoϢ */ + ret = vlan_config_format_json(interface, output, output_len); + if(ret != RET_OK){ + printf("[vlan]vlan_config_get: format json failed.\n"); + return ret; + } + return RET_OK; +} + +/************************************************************ +* :ļжȡӽӿϢ +* "/etc/network/interfaces" +* ֺvlan id +* auto ens38.100 +* | | | +* pauto pifname ppoint +* +* : +* : +* ֵ: +************************************************************/ +ret_code vlan_get_from_conf_file(int *interface) +{ + if(!interface){ + printf("[vlan]vlan_get_from_conf_file: para is null.\n"); + return RET_NULLP; + } + FILE *fp = NULL; + char line[FILE_BUF_LINE] = {0}; + if(!(fp = fopen(ETC_NETWORK_IFS, "r"))){ + printf("[vlan]vlan_get_from_conf_file: file open failed.\n"); + return RET_ERR; + } + //fseek(fp,0,SEEK_END); + //rewind(fp); + int vid; + int node_num = 0; + char *pauto = NULL;//"auto"ָλ + char *point = NULL;//'.'ָλ + char *ifname = NULL;//"ens38"ָλ + char tmp_name[IF_NAME_LEN] = {0};// + int i; + while(fgets(line, FILE_BUF_LINE, fp)){ + + /* ҵ"auto" */ + pauto = strstr(line, "auto"); + if(!pauto){ + continue; + } + /* ҵ'.' */ + point = strchr(pauto, '.'); + if(!point){ + continue; + } + /* autoһַ */ + ifname = pauto + strlen("auto"); + /* ˿ոTAB */ + while(ifname && (*ifname == ' ' || *ifname == '\t')){ + ifname++; + } + /* ҵens38 */ + if(ifname){ + char *p; + printf("[vlan]vlan_get_from_conf_file: ifname="); + for(p = ifname; p < point && p; p++){ + printf("%c", *p); + } + + memcpy(tmp_name, ifname, point - ifname); + /* ѾӽgnodeĽṹеif-nameȽ + ,˵ýڵ㵱ʱֵ,ֻ + ǰнvid*/ + for(i = 0; i < node_num; i++){ + if(!strcmp(g_if_vlan_info[i].if_name, tmp_name)){ + vid = atoi(point+1); + printf("\n[vlan]vlan_get_from_conf_file: find same node, vid = %d\n", vid); + vlan_bitmap_set(g_if_vlan_info[i].vlan_bitmap, vid); + break; + } + } + /* i==node_num˵ǰûҵ,Ǹȫµ */ + if(i == node_num){ + memcpy(g_if_vlan_info[node_num].if_name, ifname, point - ifname); + g_if_vlan_info[node_num].attr = LINK_TYPE_TRUNK; + g_if_vlan_info[node_num].op_type = OP_ADD; + g_if_vlan_info[node_num].if_model = INTERFACE_MODEL_ROUTE_FORWARD; + vid = atoi(point+1); + printf("\n[vlan]vlan_get_from_conf_file: vid = %d\n", vid); + vlan_bitmap_set(g_if_vlan_info[node_num].vlan_bitmap, vid); + interface[node_num] = 1; + node_num++; + } + } + } + + fclose(fp); + return RET_OK; +} + +/************************************************************ +* :vlanget all +* : +* : +* ֵ: +************************************************************/ +ret_code vlan_config_get_all(uint source, pointer output, int *output_len) +{ + int interface[MAX_INTERFACES] = {0}; + int i; + ret_code ret= RET_OK; + printf("[vlan]===config-get-all begin.===\n"); + /* WEB·,g_if_vlan_infoӦnodeݵ + ֻ轫ݵĽڵnodeصinterfaceT*/ + printf("[vlan]vlan_config_get_all: source=%d.\n", source); + if(source == CONFIG_FROM_WEB){ + for(i = 0; i < MAX_INTERFACES; i++){ + //if(get_ifnode_from_global(g_if_vlan_info[i].if_name) != -1) + /* ΪմŸinterface1 */ + if(strcmp(g_if_vlan_info[i].if_name, "")){ + interface[i] = 1; + printf("[vlan]vlan_config_get_all: interface[%d].\n", i); + } + } + } + + /* ûָg_if_vlan_infoǿյ,Ҫȴ + ļݻָڴg_if_vlan_info[] + ȻٽݵĽڵnodeinterface*/ + else if(source == CONFIG_FROM_RECOVER1){ + ret = vlan_get_from_conf_file(interface); + if(ret != RET_OK){ + printf("[vlan]vlan_config_get_all: source %d, get from cfg file failed(%d).\n", source, ret); + return ret; + } + } + ret = vlan_config_format_json(interface, output, output_len); + if(ret != RET_OK){ + printf("[vlan]vlan_config_get_all: source %d, get from cfg file failed(%d).\n", source, ret); + return ret; + } + return RET_OK; +} + +/************************************************************ +* :ṩĽӿ +* װɾvlanϢ +* : +* : +* ֵ: +************************************************************/ +ret_code del_compose_if_vlan_info(char *if_name, interface_vlan_info *if_vlan_info) +{ + int ifnode = -1, vid; + if(!if_name || !if_vlan_info){ + printf("[vlan]del_compose_if_vlan_info: input pointer is null.\n"); + return RET_NULLP; + } + + /* ifnameҵǰifڵ,vlanֵif_vlan_info */ + ifnode = get_ifnode_from_global(if_name); + if(ifnode == -1){ + printf("[vlan]del_compose_if_vlan_info: find empty ifnode.\n"); + return RET_NOTFOUND; + } + if_vlan_info->attr = g_if_vlan_info[ifnode].attr; + if_vlan_info->if_model = g_if_vlan_info[ifnode].if_model; + if_vlan_info->op_type = OP_DEL; + if_vlan_info->vidcnt = g_if_vlan_info[ifnode].vidcnt; + memcpy(if_vlan_info->if_name, g_if_vlan_info[ifnode].if_name, strlen(g_if_vlan_info[ifnode].if_name)); + printf("[vlan]del_compose_if_vlan_info: attr=%d, op_type=%d, ifname=%s\n", + if_vlan_info->attr, if_vlan_info->op_type, if_vlan_info->if_name); + + /* ȡvlanbitmap */ + printf("[vlan]del_compose_if_vlan_info: vlan_info:\n("); + for(vid = VID_MIN; vid <= VID_MAX; vid++){ + /* ҵvid,½ṹvlan_bitmapvid bitmap */ + if(vlan_bitmap_check(g_if_vlan_info[ifnode].vlan_bitmap, vid)){ + vlan_bitmap_set(if_vlan_info->vlan_bitmap, vid); + printf("%d, ", vid); + } + } + printf(")\n"); + return RET_OK; +} +/************************************************************ +* :ṩĽӿ +* ɾָϵvlanĻص +* : +* : +* ֵ: +************************************************************/ +int del_interface_vlan_cb(BR_EVENT_TYPE event_type, br_event_t event_arg) +{ + interface_vlan_info if_vlan_info = {0}; + ret_code ret = RET_OK; + ret = del_compose_if_vlan_info(event_arg.if_name, &if_vlan_info); + if(ret != RET_OK){ + printf("[vlan]del_interface_vlan_cb: compose-if-vlan-info failed(%d).\n", ret); + return ret; + } + ret = del_interface_vlan(&if_vlan_info); + if(ret != RET_OK){ + printf("[vlan]del_interface_vlan_cb: del-if-vlan failed(%d).\n", ret); + return ret; + } + return RET_OK; +} + + +/************************************************************ +* :vlan vidֵУ +* : +* : +* ֵ: +************************************************************/ +ret_code vid_value_chk(int vid) +{ + if(!(vid >= VID_MIN && vid <= VID_MAX)){ + printf("[vlan]vid_value_chk: vid value error,return error\n"); + return RET_INPUTERR; + } + return RET_OK; +} + +/************************************************************ +* :vlanĿУ +* : +* : +* ֵ: +************************************************************/ +ret_code vid_num_chk(char *if_name, operation_type op_type, int num) +{ + int ifnode = -1; + if(!if_name){ + printf("[vlan]vid_num_chk: if_name is null.\n"); + return RET_NULLP; + } + printf("[vlan]vid_num_chk: if_name=%s, op_type=%d, num=%d\n", if_name, op_type, num); + ifnode = get_ifnode_from_global(if_name); + if(ifnode == -1){ + printf("[vlan]vid_num_chk: get ifnode empty, return\n"); + return RET_OK; + } + if(op_type == OP_ADD && + g_if_vlan_info[ifnode].vidcnt + num > EACH_PORT_MAX_VLAN_NUM){ + printf("[vlan]vid_num_chk: ADD operation's vid num(%d+%d=%d) > EACH_PORT_MAX_VLAN_NUM\n", + g_if_vlan_info[ifnode].vidcnt, num, g_if_vlan_info[ifnode].vidcnt + num); + return RET_INPUTERR; + } + + return RET_OK; +} + +/************************************************************ +* :vlanУ +* Ŀǰֻ֧trunk,trunk +* : +* : +* ֵ: +************************************************************/ +ret_code if_attr_chk(interface_vlan_info *if_vlan_info) +{ + if(if_vlan_info->attr != LINK_TYPE_TRUNK){ + printf("[vlan]if_attr_chk: now we only support link-type trunk.\n"); + return RET_INPUTERR; + } + return RET_OK; +} + +/************************************************************ +* :vlanòADDУ +* ·vlan idȫ,򱨴 +* : +* : +* ֵ: +************************************************************/ +ret_code op_type_add_chk(interface_vlan_info *if_vlan_info) +{ + int ifnode, vid; + ifnode = get_ifnode_from_global(if_vlan_info->if_name); + if(ifnode == -1){ + printf("[vlan]op_type_add_chk: This if_name has no node, return OK.\n"); + return RET_OK; + } + + /* ·vidȫg,򱨴 */ + for(vid = VID_MIN; vid <= VID_MAX; vid++){ + if(vlan_bitmap_check(if_vlan_info->vlan_bitmap, vid) + && vlan_bitmap_check(g_if_vlan_info[ifnode].vlan_bitmap, vid)){ + printf("[vlan]op_type_add_chk: Already have vid[%d], now return\n", vid); + return RET_INPUTERR; + } + } + return RET_OK; +} + +/************************************************************ +* :vlanòDELУ +* ·vidȫû,򱨴 +* : +* : +* ֵ: +************************************************************/ +ret_code op_type_del_chk(interface_vlan_info *if_vlan_info) +{ + int ifnode, vid; + ret_code ret = RET_OK; + ifnode = get_ifnode_from_global(if_vlan_info->if_name); + if(ifnode == -1){ + printf("[vlan]op_type_del_chk: get_ifnode failed.\n"); + ret = RET_INPUTERR; + goto out; + } + + /* ·vidȫgû,򱨴 */ + for(vid = VID_MIN; vid <= VID_MAX; vid++){ + if(vlan_bitmap_check(if_vlan_info->vlan_bitmap, vid) + && !vlan_bitmap_check(g_if_vlan_info[ifnode].vlan_bitmap, vid)){ + printf("[vlan]op_type_del_chk: node[%d] do not have vid[%d], now return\n", ifnode, vid); + ret = RET_INPUTERR; + goto out; + } + } +out: + return ret; +} + +/************************************************************ +* :vlanòУ +* ֻ֧ADDDELͱ +* : +* : +* ֵ: +************************************************************/ +ret_code op_type_chk(interface_vlan_info *if_vlan_info) +{ + int ifnode, vid, i; + ret_code ret = RET_OK; + /* ADDУ */ + if(if_vlan_info->op_type == OP_ADD){ + ret = op_type_add_chk(if_vlan_info); + } + /* DELУ */ + else if(if_vlan_info->op_type == OP_DEL){ + ret = op_type_del_chk(if_vlan_info); + } + else { + printf("[vlan]op_type_chk: op_type error.\n"); + ret = RET_INPUTERR; + } + return ret; +} +/************************************************************ +* :ӿУ +* ·ĽӿΪ, +* : +* : +* ֵ: +************************************************************/ +ret_code if_name_chk(interface_vlan_info *if_vlan_info) +{ + if(!(strcmp(if_vlan_info->if_name, ""))){ + printf("[vlan]if_name_chk: if-name is emptyp, return error.\n"); + return RET_ERR; + } + return RET_OK; +} + +/************************************************************ +* :vlanУ +* ӿԡУ;vidjsonʱУ +* : +* : +* ֵ: +************************************************************/ +ret_code interface_vlan_check(interface_vlan_info *if_vlan_info) +{ + ret_code ret = RET_OK; + if(!if_vlan_info){ + return RET_NULLP; + } + printf("[vlan]interface_vlan_check: ifname=%s, op_type=%d, attr=%d\n", + if_vlan_info->if_name, if_vlan_info->op_type, if_vlan_info->attr); + /* if_nameУ */ + ret = if_name_chk(if_vlan_info); + if(ret != RET_OK){ + printf("[vlan-chk]interface_vlan_check: if-name-chk failed.\n"); + goto out; + } + + /* if_attrУ */ + ret = if_attr_chk(if_vlan_info); + if(ret != RET_OK){ + printf("[vlan-chk]interface_vlan_check: if-attr-chk failed.\n"); + goto out; + } + + /* У */ + ret = op_type_chk(if_vlan_info); + if(ret != RET_OK){ + printf("[vlan-chk]interface_vlan_check: op-type-chk failed.\n"); + goto out; + } +out: + return ret; +} + +/************************************************************ +* :vlanòУ +* jsonʱṹif_vlan_info[],ȻУ麯 +* : +* : +* ֵ: +************************************************************/ +ret_code vlan_config_set_chk(uint source, pointer input) +{ + int if_vl_num = 0, i; + ret_code ret = RET_OK; + interface_vlan_info if_vlan_info[MAX_INTERFACES] = {0}; + + ret = vlan_config_json_parse(input, if_vlan_info, &if_vl_num); + if(ret != RET_OK){ + printf("[vlan]vlan_config_set_chk: json-parse failed(%d).\n", ret); + goto out; + } + for(i = 0; i < if_vl_num; i++){ + ret = interface_vlan_check(&if_vlan_info[i]); + if(ret != RET_OK){ + printf("[vlan]vlan_config_set_chk: if-node[%d] vlan chk failed(%d).\n", i, ret); + goto out; + } + } +out: + return ret; + +} + +/************************************************************ +* : +* : +* : +* ֵ: +************************************************************/ +ret_code vlan_operate_parse(pointer input, int *oper_type) +{ + ret_code ret = RET_OK; + int i, if_num, vl_num, j; + cJSON *root = NULL, *operate = NULL, *conf_ifvlan = NULL; + if(!input || !oper_type){ + printf("[vlan]vlan_operate_chk: input or oper_type para is null.\n"); + ret = RET_INPUTERR; + goto out; + } + if(!(root = cJSON_Parse(input))){ + printf("[vlan]vlan_operate_chk: json parse error, return error.\n"); + ret = RET_INPUTERR; + goto out; + } + if(!(operate = cJSON_GetObjectItem(root, "operate"))){ + printf("[vlan]vlan_operate_chk: No operate node.\n"); + ret = RET_INPUTERR; + goto out; + } + + if(operate->valuestring != NULL && !strcmp(operate->valuestring, "add") + || operate->valuestring != NULL && !strcmp(operate->valuestring, "del")){ + printf("[vlan]vlan_operate_chk: operate-val is SET.\n"); + *oper_type = CM_CONFIG_SET; + ret = RET_OK; + goto out; + } + else if(operate->valuestring != NULL && !strcmp(operate->valuestring, "get")){ + printf("[vlan]vlan_operate_chk: operate-val is GET.\n"); + *oper_type = CM_CONFIG_GET; + ret = RET_OK; + goto out; + } + else if(operate->valuestring != NULL && !strcmp(operate->valuestring, "getall")){ + printf("[vlan]vlan_operate_chk: operate-val is GETALL.\n"); + *oper_type = CM_CONFIG_GET_ALL; + ret = RET_OK; + goto out; + } + +out: + if(root){ + cJSON_Delete(root); + } + return ret; +} + +/************************************************************ +* :vlan getУ +* operateͷֱsetgetgetallУ +* ĿǰֻsetУ +* : +* : +* ֵ: +************************************************************/ + +ret_code vlan_config_get_chk(uint source, pointer input) +{ + int ret = RET_OK; + int i; + int interface[MAX_INTERFACES] = {0}; + if(!input){ + printf("[vlan]vlan_config_get_chk: input null.\n"); + return RET_NULLP; + } + ret = vlan_get_json_parse(interface, input); + if(ret != RET_OK){ + printf("[vlan]vlan_config_get_chk: get json parse failed(%d).\n", ret); + return ret; + } + for(i = 0; i < MAX_INTERFACES; i++){ + if(interface[i] == 1){ + return RET_OK; + } + } + return RET_INPUTERR; +} + +/************************************************************ +* :vlan getallУ +* operateͷֱsetgetgetallУ +* ĿǰֻsetУ +* : +* : +* ֵ: +************************************************************/ + +ret_code vlan_config_get_all_chk(uint source, pointer input) +{ + return RET_OK; +} + +/************************************************************ +* :vlanòУ +* operateͷֱsetgetgetallУ +* ĿǰֻsetУ +* : +* : +* ֵ: +************************************************************/ +ret_code vlan_config_chk(uint source,uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len) +{ + printf("[vlan]===config-chk begin.===\n"); + int if_vl_num = 0, i, ret = RET_OK; + interface_vlan_info if_vlan_info[MAX_INTERFACES] = {0}; + int oper_type; + + ret = vlan_operate_parse(input, &oper_type); + if(ret != RET_OK){ + goto out; + } + *config_type = oper_type; + printf("[vlan]vlan_config_chk: operate_type=%d\n", oper_type); + if(oper_type == CM_CONFIG_SET){ + printf("[vlan]vlan_config_chk: SET chk\n"); + ret = vlan_config_set_chk(source, input); + } + else if(oper_type == CM_CONFIG_GET){ + printf("[vlan]vlan_config_chk: GET chk\n"); + ret = vlan_config_get_chk(source, input); + } + else if(oper_type == CM_CONFIG_GET_ALL){ + printf("[vlan]vlan_config_chk: GET-ALL chk\n"); + ret = vlan_config_get_all_chk(source, input); + } + else { + printf("[vlan]vlan_config_chk: oper_type is unknown(%d).\n", oper_type); + ret = RET_UNKNOWN; + } + +out: + printf("[vlan]vlan_config_chk: return ret=%d\n", ret); + return ret; +} + +void vlan_set_struct_stub(interface_vlan_info *if_vlan_info, operation_type op_type, int *if_vlnum) +{ + int vid, i; + *if_vlnum = 0; + int cnt = 0; + if(op_type == OP_ADD){ + /* ens38.100 */ + memcpy(if_vlan_info[cnt].if_name, "ens38", strlen("ens38")); + if_vlan_info[cnt].op_type = op_type; + if_vlan_info[cnt].attr = LINK_TYPE_TRUNK; + if_vlan_info[cnt].if_model = INTERFACE_MODEL_ROUTE_FORWARD; + vid = 100; + vlan_bitmap_set(if_vlan_info[cnt].vlan_bitmap, vid); + cnt++; + + /* ens39.200, ens39.300 */ + memcpy(if_vlan_info[cnt].if_name, "ens39", strlen("ens39")); + if_vlan_info[cnt].op_type = op_type; + if_vlan_info[cnt].attr = LINK_TYPE_TRUNK; + if_vlan_info[cnt].if_model = INTERFACE_MODEL_ROUTE_FORWARD; + vid = 200; + vlan_bitmap_set(if_vlan_info[cnt].vlan_bitmap, vid); + vid = 300; + vlan_bitmap_set(if_vlan_info[cnt].vlan_bitmap, vid); + cnt++; + for(i = 0; i < cnt; i++){ + printf("[vlan-stub]vlan_set_struct_stub: ADD-->if_vlan_info[%d].if_name=%s\n", i, if_vlan_info[i].if_name); + } + } + else if(op_type == OP_DEL){ + /* ɾens39.200 */ + memcpy(if_vlan_info[cnt].if_name, "ens39", strlen("ens39")); + if_vlan_info[cnt].op_type = op_type; + if_vlan_info[cnt].attr = LINK_TYPE_TRUNK; + if_vlan_info[cnt].if_model = INTERFACE_MODEL_ROUTE_FORWARD; + vid = 200; + vlan_bitmap_set(if_vlan_info[cnt].vlan_bitmap, vid); + cnt++; + } + else if(op_type == OP_MOD){ + /* ޸ens39.300-->ens39.400 */ + memcpy(if_vlan_info[cnt].if_name, "ens39", strlen("ens39")); + if_vlan_info[cnt].op_type = op_type; + if_vlan_info[cnt].attr = LINK_TYPE_TRUNK; + if_vlan_info[cnt].if_model = INTERFACE_MODEL_ROUTE_FORWARD; + vid = 400; + vlan_bitmap_set(if_vlan_info[cnt].vlan_bitmap, vid); + cnt++; + for(i = 0; i < cnt; i++){ + printf("[vlan-stub]vlan_set_struct_stub: MOD-->if_vlan_info[%d].if_name=%s\n", i, if_vlan_info[i].if_name); + } + } + *if_vlnum = cnt; + return; +} + +void vlan_config_stub1(void) +{ + int if_vlnum, i; + interface_vlan_info if_vlan_info_add[MAX_INTERFACES] = {0}; + interface_vlan_info if_vlan_info_del[MAX_INTERFACES] = {0}; + interface_vlan_info if_vlan_info_mod[MAX_INTERFACES] = {0}; + + /* ADD */ + vlan_set_struct_stub(if_vlan_info_add, OP_ADD, &if_vlnum); + for(i = 0; i < if_vlnum; i++){ + interface_vlan_set(&if_vlan_info_add[i]); + } + + /* DEL */ + //vlan_set_struct_stub(if_vlan_info_del, OP_DEL, &if_vlnum); + //for(i = 0; i < if_vlnum; i++){ + //interface_vlan_set(&if_vlan_info_del[i]); + //} + + /* MOD */ + vlan_set_struct_stub(if_vlan_info_add, OP_MOD, &if_vlnum); + for(i = 0; i < if_vlnum; i++){ + interface_vlan_set(&if_vlan_info_mod[i]); + } + return; +} + +void vlan_config_stub(operation_type op_type) +{ + int if_vlnum, i; + interface_vlan_info if_vlan_info[MAX_INTERFACES] = {0}; + + vlan_set_struct_stub(if_vlan_info, op_type, &if_vlnum); + for(i = 0; i < if_vlnum; i++){ + interface_vlan_set(&if_vlan_info[i]); + } + + return; +} + +void vlan_config_format_json_stub(int *interface) +{ + int i, j, vid; + for(i = 0; i < MAX_INTERFACES; i++){ + if(!(interface[i])){ + continue; + } + printf("[vlan]vlan_config_format_json_stub: g_if_vlan_info[%d].if_name=%s, attr=%d, if_model=%d, op_type=%d, vlan=\n", + i, g_if_vlan_info[i].if_name, g_if_vlan_info[i].attr, + g_if_vlan_info[i].if_model, g_if_vlan_info[i].op_type); + for(vid = 0; vid < MAX_VLAN;vid++){ + if(vlan_bitmap_check(g_if_vlan_info[i].vlan_bitmap, vid)){ + printf("%d,", vid); + } + } + printf("\n"); + } + return; +} + +void vlan_config_get_stub(void) +{ + int if_vlnum; + int interface[MAX_INTERFACES] = {0}; + interface[1] = 1; + printf("[vlan]vlan_config_get_stub:start.\n"); + /* ݶӦinterface±,ȥȡȫg_if_vlan_infoϢ */ + vlan_config_format_json_stub(interface); + + return; +} + +void vlan_config_get_all_stub(uint source) +{ + int interface[MAX_INTERFACES] = {0}; + int i; + + /* WEB·,g_if_vlan_infoӦnodeݵ + ֻ轫ݵĽڵnodeصinterfaceT*/ + if(source == CONFIG_FROM_WEB){ + printf("[vlan]vlan_config_get_all: CONFIG_FROM_WEB.\n"); + for(i = 0; i < MAX_INTERFACES; i++){ + //if(get_ifnode_from_global(g_if_vlan_info[i].if_name) != -1) + /* ΪմŸinterface1 */ + if(strcmp(g_if_vlan_info[i].if_name, "")){ + interface[i] = 1; + printf("[vlan]get_all_stub: interface[%d].\n", i); + } + } + } + + /* ûָg_if_vlan_infoǿյ,Ҫȴ + ļݻָڴg_if_vlan_info[] + ȻٽݵĽڵnodeinterface*/ + else if(source == CONFIG_FROM_RECOVER1){ + printf("[vlan]vlan_config_get_all: CONFIG_FROM_RECOVER1.\n"); + vlan_get_from_conf_file(interface); + } + vlan_config_format_json_stub(interface); + return; +} +