typedef struct MacRangeT
{
u8b startingCnuMac[6];
u8b endingCnuMac[6];
}__attribute__((packed))MacRangeT;
typedef struct CMC_SetAddOrRemoveCnusReqT
{
u8b groupId;
u8b cnuOption;
u8b numOfEntries;
MacRangeT CnuMac[201];
}__attribute__((packed))CMC_SetAddOrRemoveCnusReqT;
//线程处理函数
void * SetAddCNUsToLdBalGrp_start_run(void * arg)
{
CMC_SetAddOrRemoveCnusReqT *pRevBuf = (CMC_SetAddOrRemoveCnusReqT *)arg;
u16b reqOff = 0;
u8b resultCode = OK;
cout << "SetAddCNUsToLdBalGrp_start_run " << endl;
// Get the load balancing ID from the request buffer.
u8b loadBalancingGroupID = pRevBuf->groupId;
cout << "Load Balancing Group ID = 0x" << hex << (int) loadBalancingGroupID << endl;
// Get the CNU option from the request buffer.
// 1=Add, 2=Delete
u8b cnuOption = pRevBuf->cnuOption;
cout << "CNU option = 0x" << hex << (int) cnuOption << endl;
if (cnuOption > 2)
{
cout << "Invalid CNU option parameter!" << endl;
resultCode = INVALID_PARMS;
}
u8b numOfEntries = pRevBuf->numOfEntries;
cout << "Invalid CNU numOfEntries:" << (int) numOfEntries << endl;
u8b numOfmac = 0;
for( numOfmac=0; numOfmac < numOfEntries; numOfmac++)
{
if (resultCode == OK)
{
u8b startMacAddr[6] = {0};
u8b endMacAddr[6] = {0};
memcpy(startMacAddr,pRevBuf->CnuMac[numOfmac].startingCnuMac,6);
memcpy(endMacAddr,pRevBuf->CnuMac[numOfmac].endingCnuMac,6);
// FIgure out which method is being specified by the user.
cout << "CNU specification method detected = ";
u8b cnuMethod = LB_RANGEMACS;
u8b ouiBytes[3] = {0, 0, 0};
if (memcmp(startMacAddr, endMacAddr, 6) == 0)
{
cnuMethod = LB_SINGLEMAC;
cout << "SingleMac" << endl;
}
else if ((memcmp(startMacAddr, endMacAddr, 3) == 0) &&
(memcmp(startMacAddr+3, ouiBytes, 3) == 0) &&
(memcmp(endMacAddr+3, ouiBytes, 3) == 0))
{
cnuMethod = LB_OUI;
cout << "OUI" << endl;
}
else
{
cout << "RangeOfMacs" << endl;
if (memcmp(startMacAddr, endMacAddr, 6) > 0)
{
cout << "ERROR: Starting MAC address is greater than ending MAC address!" << endl;
cout << "Not adding CNU MAC address range to load balancing group!" << endl;
resultCode = ERROR;
}
}
if (resultCode == OK)
{
switch (cnuMethod)
{
case LB_SINGLEMAC:
{
BcmMacAddress startMac(startMacAddr[0], startMacAddr[1], startMacAddr[2],
startMacAddr[3], startMacAddr[4], startMacAddr[5]);
if (cnuOption == LB_CREATE)
{
resultCode = theLoadBalancer().AddMacAddressToGroup(startMac, loadBalancingGroupID);
}
else if (cnuOption == LB_DESTROY)
{
resultCode = theLoadBalancer().DropMacAddressFromGroup(startMac, loadBalancingGroupID);
}
}
break;
case LB_OUI:
{
BcmMacAddress ouiMac(startMacAddr[0], startMacAddr[1], startMacAddr[2],
startMacAddr[3], startMacAddr[4], startMacAddr[5]);
if (cnuOption == LB_CREATE)
{
resultCode = theLoadBalancer().AddOuiToGroup(ouiMac, loadBalancingGroupID);
}
else if (cnuOption == LB_DESTROY)
{
resultCode = theLoadBalancer().DropOuiFromGroup(ouiMac, loadBalancingGroupID);
}
}
break;
case LB_RANGEMACS:
{
BcmMacAddress startMac(startMacAddr[0], startMacAddr[1], startMacAddr[2],
startMacAddr[3], startMacAddr[4], startMacAddr[5]);
BcmMacAddress endMac(endMacAddr[0], endMacAddr[1], endMacAddr[2],
endMacAddr[3], endMacAddr[4], endMacAddr[5]);
if (cnuOption == LB_CREATE)
{
resultCode = theLoadBalancer().AddMacAddressRangeToGroup(startMac, endMac, loadBalancingGroupID);
}
else if (cnuOption == LB_DESTROY)
{
resultCode = theLoadBalancer().DropMacAddressRangeFromGroup(startMac, endMac, loadBalancingGroupID);
}
}
break;
default:
resultCode = ERROR;
break;
}
}
}
}
if ( numOfEntries > 1)
{
resultCode = theLoadBalancer().UpdateActiveCMByGroup(loadBalancingGroupID);
}
return NULL;
}
//线程启动函数
u8b SetAddCNUsToLdBalGrpThread(CMC_SetAddOrRemoveCnusReqT *pRequestBuf)
{int rc = 0;
struct sched_param priority_holder ;
pthread_attr_t attr;
pthread_t thread_id;
priority_holder.sched_priority = 71; //设置线程的优先级1-99,值越大优先级越高
pthread_attr_init(&attr);
pthread_attr_setschedparam(&attr,&priority_holder);//设置优先级
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);//初始化线程属性
rc = pthread_create(&thread_id,&attr,SetAddCNUsToLdBalGrp_start_run,pRequestBuf);//启动线程SetAddCNUsToLdBalGrp_start_run调用线程处理函数,pRequestBuf传入值
if (rc)
{
cout<< "ERROR; return code is :"<< rc << endl;
return 1;
}
else
{
pthread_detach(thread_id);
}
//pthread_exit(NULL);
return 0;
}