代码拉取完成,页面将自动刷新
#include"basetype.h"
#include"common.h"
#include"switch_data.h"
#include"addr_lib.h"
#include"timer_data.h"
#include"debug_lib.h"
#define MAC_NODE_AGING 48
SWITCH_DEVICE_S g_stSwitchDeviceList;
UINT32 g_uiTimeId;
VOID* CheckTimeOut(UINT32 auiPara[4])
{
SWITCH_DEVICE_S* pstSwitch = g_stSwitchDeviceList.pstNextDevice;
SWITCH_MACNODE_S* pstMacNode;
MAC_TABLE_S* pstMacTable;
UINT16 usVlanid;
UINT16 usVlanCount = DEVICE_MAX_VLAN;
UINT32 NowTime;
UINT32 i, j;
for (;; ) // 遍历交换机设备
{
if (NULL == pstSwitch)
{
return;
}
for (i = 0; i < DEVICE_MAX_VLAN; i++)//遍历vlan地址表
{
pstMacTable = &pstSwitch->astMacTable[i];
for (j = 0; j < pstMacTable->usMacCount; j++) //遍历mac表
{
pstMacNode = &pstMacTable->astMacNode[j];
NowTime = TimerData_GetTimeNow();
if (NowTime > pstMacNode->uiAgingTime + MAC_NODE_AGING)// 把该表项删掉
{
usVlanid = pstMacTable[j].usVlanId;
MacData_Delete(pstSwitch->uiDeviceId, pstMacNode->aucMacAddr, pstMacNode->usSwitchPortNum, usVlanid);
}
}
}
pstSwitch = pstSwitch->pstNextDevice;
}
}
INT32 MacData_Init(VOID)
{
UINT32 auiPara[4];
UINT32 NowTime;
memset(&g_stSwitchDeviceList, 0, sizeof(SWITCH_DEVICE_S));
g_uiTimeId = TimerData_Add(2, TIMER_FLAG_ROUTINE, auiPara, CheckTimeOut);
return ERROR_SUCCESS;
}
/*
description: 添加一个交换机设备
input: UINT32 uiDeviceId, 添加的交换机id
output: 添加成功/失败
return: 错误码
tip: IN是空宏,表示该参数是输入。
author:
*/
INT32 MacData_AddDevice(IN UINT32 uiDeviceId)
{
SWITCH_DEVICE_S* pstSwitch;
UINT32 i;
pstSwitch = malloc(sizeof(SWITCH_DEVICE_S));
if (NULL == pstSwitch)
{
return ERROR_NO_MEMORY;
}
memset(pstSwitch, 0, sizeof(SWITCH_DEVICE_S));
pstSwitch->uiDeviceId = uiDeviceId;
pstSwitch->astMacTable[1].usVlanId = 1;
pstSwitch->astMacTable[1].usFlag = 1;
for (i = 0; i < DEVICE_MAX_VLAN_IF; i++)
{
pstSwitch->astIfCfg[i].usPortMode = IF_TYPE_ACCESS;
pstSwitch->astIfCfg[i].usVlanCount = 1;
pstSwitch->astIfCfg[i].astPermitVlanId[0] = 1;
}
pstSwitch->pstNextDevice = g_stSwitchDeviceList.pstNextDevice;
g_stSwitchDeviceList.pstNextDevice = pstSwitch;
return;
}
/*
*
description: 删除交换机设备
input: UINT32 uiDeviceId 删除的交换机id
output: 添加成功/失败
return: 错误码
tip: IN是空宏,表示该参数是输入。
author:
*/
INT32 MacData_DeleteDevice(IN UINT32 uiDeviceId)
{
SWITCH_DEVICE_S* pstSwitchPre = &g_stSwitchDeviceList;
SWITCH_DEVICE_S* pstSwitch = g_stSwitchDeviceList.pstNextDevice;
for (;;)
{
if (NULL == pstSwitch)
{
return;
}
if (uiDeviceId != pstSwitch->uiDeviceId)
{
pstSwitchPre = pstSwitch;
pstSwitch = pstSwitch->pstNextDevice;
}
else
{
/* uiDeviceId == pstSwitch->uiDeviceId */
pstSwitchPre->pstNextDevice = pstSwitch->pstNextDevice;
free(pstSwitch);
return;
}
}
}
/*
description: 根据设备ID查找交换机设备数据结构
input: UINT32 uiDeviceId 交换机设备id
output:
return: 成功,返回找到的交换机数据结构指针 /失败,返回NULL
tip: IN是空宏,表示该参数是输入。
author: 杨洋
*/
SWITCH_DEVICE_S* MacData_FindDevice(IN UINT32 uiDeviceId)
{
SWITCH_DEVICE_S* pstSwitch = g_stSwitchDeviceList.pstNextDevice;
for (;;)
{
if (NULL == pstSwitch)
{
return NULL;
}
if (uiDeviceId == pstSwitch->uiDeviceId)
{
return pstSwitch;
}
pstSwitch = pstSwitch->pstNextDevice;
}
}
INT32 Vlan_Add(IN UINT16 usDeviceId, IN UINT16 usVlanid)
{
SWITCH_DEVICE_S* pstSwitch;
MAC_TABLE_S* pstMacTable;
pstSwitch = MacData_FindDevice(usDeviceId);
if (NULL == pstSwitch)
{
return ERROR_NOT_FOUND;
}
if (usVlanid >= DEVICE_MAX_VLAN)
{
return ERROR_INDEX_EXCEED;
}
pstMacTable = &pstSwitch->astMacTable[usVlanid];
if (1 == pstMacTable->usFlag)
{
return ERROR_ALREADY_EXSISIT;
}
pstMacTable->usFlag = 1;
pstMacTable->usVlanId = usVlanid;
//DebugLib_Printf(MODULE_MACTABLE, "vlan类型:%d\n", IF_TYPE_ACCESS);
return ERROR_SUCCESS;
}
INT32 Vlan_Delete(IN UINT16 usDeviceId, IN UINT16 usVlanid)
{
SWITCH_DEVICE_S* pstSwitch = NULL;
MAC_TABLE_S* pstMacTable;
pstSwitch = MacData_FindDevice(usDeviceId);
if (NULL == pstSwitch)
{
return ERROR_NOT_FOUND;
}
if (usVlanid >= DEVICE_MAX_VLAN)
{
return ERROR_INDEX_EXCEED;
}
pstMacTable = &pstSwitch->astMacTable[usVlanid];
if (0 == pstMacTable->usFlag)
{
return ERROR_NOT_FOUND;
}
pstMacTable->usFlag = 0;
return ERROR_SUCCESS;
}
/*
description: 增加一条mac地址表项
input: UINT32 uiDeviceId 交换机设备id
UINT8* aucMacAddr mac地址
UINT16 usFlag mac地址类型MAC_NODE_XXX
UINT16 usSwitchPortNum, 增加到交换机的哪个接口
output:
return: 成功/失败(错误码)
tip: IN是空宏,表示该参数是输入。
author: 杨洋
*/
INT32 MacData_Add(IN UINT16 usDeviceId, IN UINT8* pucMacAddr, IN UINT32 uiAgingTime,
IN UINT16 usSwitchPortNum, IN UINT16 usVlanid)
{
UINT32 i;
SWITCH_DEVICE_S* pstSwitch;
SWITCH_MACNODE_S* pstMacNode;
MAC_TABLE_S* pstMacTable;
UINT32 uiNowTime;
pstSwitch = MacData_FindDevice(usDeviceId);
if (NULL == pstSwitch)
{
return ERROR_NOT_FOUND;
}
if (usSwitchPortNum >= DEVICE_MAX_VLAN_IF)
{
return ERROR_INDEX_EXCEED;
}
if (usVlanid >= DEVICE_MAX_VLAN)
{
return ERROR_INDEX_EXCEED;
}
pstMacTable = &pstSwitch->astMacTable[usVlanid];
if (1 != usVlanid && 0 == pstMacTable->usFlag)
{
return ERROR_SWITCH_VLAN_NOT_EXSIT;
}
//查找mac地址是否已经添加过
for (i = 0; i < pstMacTable->usMacCount; i++)
{
pstMacNode = &pstMacTable->astMacNode[i];
if (0 == memcmp(pstMacNode->aucMacAddr, pucMacAddr, MAC_BYTE))
{
//要添加的mac地址已经存在,更新端口编号
pstMacNode->usSwitchPortNum = usSwitchPortNum;
uiNowTime = TimerData_GetTimeNow();
if (MAC_NODE_STATIC == uiAgingTime)//静态表项
{
pstMacNode->uiAgingTime = uiAgingTime;
}
else//动态表项
{
pstMacNode->uiAgingTime = uiNowTime;
}
return ERROR_ALREADY_EXSISIT;
}
}
pstMacNode = &pstMacTable->astMacNode[pstMacTable->usMacCount];
//构造添加的新节点
memcpy(&pstMacNode->aucMacAddr, pucMacAddr, MAC_BYTE);
pstMacNode->usSwitchPortNum = usSwitchPortNum;
uiNowTime = TimerData_GetTimeNow();
if (MAC_NODE_STATIC == uiAgingTime) //静态表项
{
pstMacNode->uiAgingTime = uiAgingTime;
}
else//动态表项
{
pstMacNode->uiAgingTime = uiNowTime;
}
//DebugLib_Printf(MODULE_MACTABLE, "mac Type:%d\n", pstMacNode->uiAgingTime);
pstMacTable->usMacCount++;
return ERROR_SUCCESS;
}
/*
description: 删除一条mac地址表项
input: UINT32 uiDeviceID 交换机设备id
UINT8* aucMacAddr, 目的mac地址
IN UINT16 usFlag, mac地址类型
UINT16 usSwitchPortNum, 删除的交换机接口号
output: 成功/失败
return: 错误码
tip: IN是空宏,表示该参数是输入。
author:
*/
INT32 MacData_Delete(IN UINT32 uiDeviceId, IN UINT8* aucMacAddr, IN UINT16 usSwitchPortNum, IN UINT16 usVlanid)
{
SWITCH_DEVICE_S* pstSwitch;
SWITCH_MACNODE_S* pstMacNode;
MAC_TABLE_S* pstMacTable;
UINT32 i;
pstSwitch = MacData_FindDevice(uiDeviceId);
if (NULL == pstSwitch)
{
return ERROR_NOT_FOUND;
}
if (usSwitchPortNum >= DEVICE_MAX_VLAN_IF)
{
return ERROR_INDEX_EXCEED;
}
if (usVlanid >= DEVICE_MAX_VLAN)
{
return ERROR_INDEX_EXCEED;
}
pstMacTable = &pstSwitch->astMacTable[usVlanid];
//判断flag是不是1
if (1 != usVlanid && 0 == pstMacTable->usFlag)
{
return ERROR_SWITCH_VLAN_NOT_EXSIT;
}
//查找删除的mac地址是否存在
for (i = 0; i < pstMacTable->usMacCount; i++)
{
pstMacNode = &pstMacTable->astMacNode[i];
if (0 == memcmp(pstMacNode->aucMacAddr, aucMacAddr, MAC_BYTE))
{
//将count - 1位置上的结点拷贝到该位置上
memcpy(pstMacNode, &pstMacTable->astMacNode[pstMacTable->usMacCount - 1], sizeof(SWITCH_MACNODE_S));
ASSERT(pstMacTable->usMacCount > 0);
pstMacTable->usMacCount--;
return ERROR_SUCCESS;
}
}
return ERROR_NOT_EXIST;
}
/*
description: 将交换机的数据提封装成字符串提供给process层使用
input: UINT32 uiDeviceId, 交换机的id
INT8* pcString, 打印的字符串信息
UINT32 uiStringLength, 缓冲区的最大长度
output: 成功/失败
return: 错误码
tip: IN表示该参数是输入,OUT表示该参数是输出。
author: 杨洋
*/
INT32 MacData_Print(IN SWITCH_DEVICE_S* pstSwitchDevice, IN UINT16 usVlanid)
{
SWITCH_MACNODE_S* pstMacNode;
UINT32 uiTimeNow;
UINT8 aucMacAddrString[MAC_STRING_MAXLEN] = { 0 };
UINT8 aucMacAging[UINT_STRING_MAXLEN] = { 0 };
UINT32 i;
if (0 == pstSwitchDevice->astMacTable[usVlanid].usFlag)
{
return ERROR_INVALID_INPUT;
}
if (usVlanid >= DEVICE_MAX_VLAN)
{
return ERROR_INDEX_EXCEED;
}
printf("MAC Address\t\t Port\t\tType\t\tAge\n");
for (i = 0; i < pstSwitchDevice->astMacTable[usVlanid].usMacCount; i++)
{
pstMacNode = &pstSwitchDevice->astMacTable[usVlanid].astMacNode[i];
AddrLib_HextoMacString(pstMacNode->aucMacAddr, aucMacAddrString);
if (MAC_NODE_STATIC != pstMacNode->uiAgingTime)
{
uiTimeNow = TimerData_GetTimeNow();
sprintf(aucMacAging, "%d", MAC_NODE_AGING + pstMacNode->uiAgingTime - uiTimeNow);
printf("%s\t\t%d\t\t%s\t\t%s\n", aucMacAddrString, pstMacNode->usSwitchPortNum,
"dynamic", aucMacAging);
}
else
{
printf("%s\t\t%d\t\t%s\t\t%s\n", aucMacAddrString, pstMacNode->usSwitchPortNum,
"static", "forever");
}
}
return ERROR_SUCCESS;
}
/*
description: 允许指定的 VLAN 通过当前类型端口
input: UINT32 uiDeviceId 交换机设备id
UINT16 usVlanid Vlan号
UINT16 usSwitchPortNum 接口号
UINT16 usInterfaceType 接口类型
output: 成功/失败
return: (错误码)
tip: IN是空宏,表示该参数是输入。
author: 刘星雨
*/
INT32 Port_AddPermitVlan(IN UINT16 usDeviceId, IN UINT16 usVlanid, IN UINT16 usSwitchPortNum, IN UINT16 usPortMode)
{
SWITCH_DEVICE_S* pstSwitch;
SWITCH_IFCFG_S* pstIfCfg;
MAC_TABLE_S* pstMacTable;
UINT32 i;
pstSwitch = MacData_FindDevice(usDeviceId);
if (NULL == pstSwitch)
{
return ERROR_NOT_FOUND;
}
if (usSwitchPortNum >= DEVICE_MAX_VLAN_IF)
{
return ERROR_INDEX_EXCEED;
}
if (usVlanid >= DEVICE_MAX_VLAN)
{
return ERROR_INDEX_EXCEED;
}
pstMacTable = &pstSwitch->astMacTable[usVlanid];
//todo 检查vlan是否已经启用
if (0 == pstMacTable->usFlag)
{
return ERROR_SWITCH_VLAN_NOT_EXSIT;
}
pstIfCfg = &pstSwitch->astIfCfg[usSwitchPortNum];
if (usPortMode != pstIfCfg->usPortMode)
{
return ERROR_INVALID_INPUT;
}
for (i = 0; i < pstIfCfg->usVlanCount; i++)
{
if (usVlanid == pstIfCfg->astPermitVlanId)
{
return ERROR_ALREADY_EXSISIT;
}
}
if (IF_TYPE_ACCESS == pstIfCfg->usPortMode)
{
pstIfCfg->astPermitVlanId[0] = usVlanid;
}
else
{
pstIfCfg->astPermitVlanId[pstIfCfg->usVlanCount] = usVlanid;
pstIfCfg->usVlanCount++;
}
return ERROR_SUCCESS;
}
/*
description: 禁止指定的 VLAN 通过当前类型端口
input: UINT32 uiDeviceId 交换机设备id
UINT16 usVlanid Vlan号
UINT16 usSwitchPortNum 接口号
UINT16 usInterfaceType 接口类型
output: 成功/失败
return: (错误码)
tip: IN是空宏,表示该参数是输入。
author: 刘星雨
*/
INT32 Port_DeletePermitVlan(IN UINT16 usDeviceId, IN UINT16 usVlanid, IN UINT16 usSwitchPortNum, IN UINT16 usPortMode)
{
SWITCH_DEVICE_S* pstSwitch;
SWITCH_IFCFG_S* pstIfCfg;
MAC_TABLE_S* pstMacTable;
UINT16 usVlanIndex = DEVICE_MAX_VLAN;
pstSwitch = MacData_FindDevice(usDeviceId);
if (NULL == pstSwitch)
{
return ERROR_NOT_FOUND;
}
if (usSwitchPortNum >= DEVICE_MAX_VLAN_IF)
{
return ERROR_INDEX_EXCEED;
}
if (usVlanid >= DEVICE_MAX_VLAN)
{
return ERROR_INDEX_EXCEED;
}
pstMacTable = &pstSwitch->astMacTable[usVlanid];
if (0 == pstMacTable->usFlag)
{
return ERROR_NOT_EXIST;
}
pstIfCfg = &pstSwitch->astIfCfg[usSwitchPortNum];
for (int i = 0; i < pstIfCfg->usVlanCount; i++)
{
if (usVlanid == pstIfCfg->astPermitVlanId[i])
{
pstIfCfg->astPermitVlanId[i] = pstIfCfg->astPermitVlanId[pstIfCfg->usVlanCount - 1];
pstIfCfg->usVlanCount--;
return ERROR_SUCCESS;
}
}
return ERROR_FAIL;
}
/*
description: 配置端口类型
input: UINT32 uiDeviceId 交换机设备id
UINT16 usSwitchPortNum 接口号
UINT16 usInterfaceType 增加的接口类型
output: 成功/失败
return: (错误码)
tip: IN是空宏,表示该参数是输入。
author: 刘星雨
*/
//set
INT32 Port_SetMode(IN UINT16 usDeviceId, IN UINT16 usPortMode, IN UINT16 usSwitchPortNum, IN UINT16 usVlanid)
{
SWITCH_DEVICE_S* pstSwitch;
SWITCH_IFCFG_S* pstIfCfg;
UINT32 i;
pstSwitch = MacData_FindDevice(usDeviceId);
if (NULL == pstSwitch)
{
return ERROR_NOT_FOUND;
}
if (usSwitchPortNum >= DEVICE_MAX_VLAN_IF)
{
return ERROR_INDEX_EXCEED;
}
if (usVlanid >= DEVICE_MAX_VLAN)
{
return ERROR_INDEX_EXCEED;
}
pstIfCfg = &pstSwitch->astIfCfg[usSwitchPortNum];
//DebugLib_Printf(MODULE_MACTABLE, "设备类型:%d\t 传参类型:%d\n", pstIfCfg->usInterfaceType, usInterfaceType);
//do 的逻辑
if (usPortMode != IF_TYPE_NULL)
{
//IF_TYPE_ACCESS 改为IF_TYPE_TRUNK 直接改
if (IF_TYPE_ACCESS == pstIfCfg->usPortMode && usPortMode == IF_TYPE_TRUNK)
{
pstIfCfg->usPortMode = usPortMode;
}
else
{
//配置成跟原来一样的接口类型
if (pstIfCfg->usPortMode == usPortMode)
{
//IF_TYPE_ACCESS 改为 IF_TYPE_ACCESS,有两种情况
if (IF_TYPE_ACCESS == usPortMode)
{
//1。初始化的接口类型默认为IF_TYPE_ACCESS,第一次配置时需要更改允许的vlan
if (1 == pstIfCfg->astPermitVlanId[0])
{
pstIfCfg->astPermitVlanId[0] = usVlanid;
}
//2。配置其它的,需要先undo之后才能配置
else
{
return ERROR_ALREADY_EXSIT;
}
}
else
{
return ERROR_ALREADY_EXSIT;
}
}
else
{
//IF_TYPE_TRUNK改为IF_TYPE_ACCESS,不允许
return ERROR_INVALID_INPUT;
}
}
}
//undo
else
{
for (i = 0; i < pstIfCfg->usVlanCount; i++)
{
Port_DeletePermitVlan(usDeviceId, usVlanid, usSwitchPortNum, IF_TYPE_ACCESS);
}
pstIfCfg->usVlanCount = 1;
pstIfCfg->astPermitVlanId[0] = 1;
}
return ERROR_SUCCESS;
}
SWITCH_MACNODE_S* MacData_BestMatch(UINT8* aucMacaddr, UINT16 uiDeviceId, IN UINT16 usVlanid, IN UINT16 usifIndex)
{
SWITCH_DEVICE_S* pstSwitch = MacData_FindDevice(uiDeviceId);
SWITCH_MACNODE_S* pstMacNodes = NULL;
SWITCH_MACNODE_S* pstMacNode;
MAC_TABLE_S* pstMacTable;
UINT32 i;
UINT16 usMacCount;
if (NULL == pstSwitch)
{
return ERROR_NOT_FOUND;
}
if (usVlanid >= DEVICE_MAX_VLAN)
{
return ERROR_INDEX_EXCEED;
}
pstMacTable = &pstSwitch->astMacTable[usVlanid];
usMacCount = pstMacTable->usMacCount;
pstMacNodes = &pstMacTable->astMacNode;
for (i = 0; i < usMacCount; i++)
{
pstMacNode = &pstMacNodes[i];
if (0 == memcmp(aucMacaddr, pstMacNode->aucMacAddr, MAC_BYTE))
{
return pstMacNode;
}
}
return NULL;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。