#include <stdlib.h>
#include <unistd.h>
#include "rpcclt_openapi.h"
#include "proc_util.h"
#define TCAM_MAX_STR_LENGTH 256
void printMenu()
{
printf("\nEnter 1 Initialize/reset Policy \n"
" 2 to Add classifiers to Policy \n"
" 3 to Add actions to Policy\n"
" 4 to create the Policy\n"
" 5 to add intfs \n"
" 6 to delete intfs \n"
" 7 to destroy the Policy \n"
" 8 to Get \n"
" 9 to Delete all existing TCAM policies\n"
" 10 to Add policy on all interfaces\n"
" 11 to Get Policy Stats\n"
" 12 to Clear Policy Stats\n"
" 13 Initialize/reset version 2 Policy \n"
" 14 to Add version 2 classifiers to Policy \n"
" 15 to Add actions to Policy\n"
" 16 to create version 2 Policy\n"
" 17 to quit \n");
}
{
struct sockaddr_in sa;
if (af == AF_INET)
{
if (inet_pton(af,strIpAddr,&(sa.sin_addr)) > 0)
{
addr->
addr.
ipv4 = ntohl(sa.sin_addr.s_addr);
}
}
else if (af == AF_INET6)
{
if (inet_pton(AF_INET6, strIpAddr, (
void*)&(addr->addr.
ipv6)) > 0)
{
}
}
}
static open_error_t getMacAddrFromInput (
char * input_mac,
unsigned char * mac)
{
int i;
unsigned char c;
unsigned char temp_mac [12];
for ( i = 0; i < 12; i++ )
{
c = input_mac [i];
if ( (c >= '0') && (c <= '9') )
temp_mac[i] = c - '0';
else if ( (c >= 'a') && (c <= 'f') )
temp_mac[i] = c - 'a' + 10;
else if ( (c >= 'A') && (c <= 'F') )
temp_mac[i] = c - 'A' + 10;
else return(1);
};
for ( i = 0; i < 12; i+= 2 )
{
mac[i/2] = (temp_mac[i] * 16) + temp_mac[i+1];
};
return(0);
};
char intfString[100];
char redirIntfString[100];
static void openBuildHeader(open_tcamApiPolicy_t *policyInfo)
{
uint32_t prio;
memset(policyInfo,0, sizeof(*policyInfo));
policyInfo->policyHeader.headerLen = sizeof(policyInfo->policyHeader);
printf("Enter policy priority \n");
scanf("%d", &prio);
policyInfo->policyPrio = prio;
printf("Select policy type 1 for openflow 2 for Gen (IPv6) 3 for Egress \n");
scanf("%d", &policyType);
switch (policyType)
{
case 1:
break;
case 2:
break;
case 3:
break;
}
}
static void openBuildHeaderV2(open_tcamApiPolicy_v2_t *policyInfo)
{
uint32_t prio;
uint32_t policyType = 0;
memset(policyInfo,0, sizeof(*policyInfo));
policyInfo->policyHeader.headerLen = sizeof(policyInfo->policyHeader);
printf("Enter policy priority \n");
scanf("%d", &prio);
policyInfo->policyPrio = prio;
printf("Select policy type 1 for openflow, 2 for Gen (IPv6), 3 for Egress, 4 for Port \n");
scanf("%d", &policyType);
switch (policyType)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
}
static void openAddClassifiers(open_tcamApiPolicy_t *policyInfo)
{
char mac[TCAM_MAX_STR_LENGTH];
uint32_t response;
uint32_t value;
char strSubnetMask[TCAM_MAX_STR_LENGTH];
char strIpaddr[TCAM_MAX_STR_LENGTH];
printf("This selection will add multiple classifiers to the policy\n");
do
{
response = 0;
printf("Enter classification type \n"
"1 to match dest MAC\n"
"2 to match Src MAC\n"
"3 to match VLAN\n"
"4 to match VLAN Priority\n"
"5 to match ether type\n"
"6 to match dest IP address\n"
"7 to match src IP address \n"
"8 to match dest IPv6 address\n"
"9 to match src IPv6 address\n"
"10 quit \n");
scanf("%d", &response);
switch (response)
{
case 1:
memset(mac, 0, sizeof(mac));
printf("Enter Mac address\n");
scanf("%s", mac);
if (getMacAddrFromInput(mac, policyInfo->matchDstMac))
printf("Error: Invalid input\n");
break;
case 2:
memset(mac, 0, sizeof(mac));
printf("Enter Mac address\n");
scanf("%s", mac);
if (getMacAddrFromInput(mac, policyInfo->matchSrcMac))
printf("Error: Invalid input\n");
break;
case 3:
printf("Enter VLAN id \n");
scanf("%d",&value);
policyInfo->matchVlanVal = value;
break;
case 4:
printf("Enter VLAN priority \n");
scanf("%d",&value);
policyInfo->matchVlanPrio = value;
break;
case 5:
printf("Enter ether type in hex\n");
scanf("%x",&value);
policyInfo->matchEthType = value;
break;
case 6:
printf("Enter Dest IP address \n");
scanf("%s",strIpaddr);
if (getIpAddrFromStr(AF_INET, strIpaddr, &addr) ==
OPEN_E_NONE)
{
policyInfo->matchDstIp = addr.addr.ipv4;
printf("Enter Mask \n");
scanf("%s",strSubnetMask);
if (getIpAddrFromStr(AF_INET, strSubnetMask, &addr) ==
OPEN_E_NONE)
{
policyInfo->matchDstIpMask = addr.addr.ipv4;
break;
}
}
printf("Error: Incorrect Input\n");
break;
case 7:
printf("Enter Src IP address \n");
scanf("%s",strIpaddr);
if (getIpAddrFromStr(AF_INET, strIpaddr, &addr) ==
OPEN_E_NONE)
{
policyInfo->matchSrcIp = addr.addr.ipv4;
printf("Enter Mask \n");
scanf("%s",strSubnetMask);
if (getIpAddrFromStr(AF_INET, strSubnetMask, &addr) ==
OPEN_E_NONE)
{
policyInfo->matchSrcIpMask = addr.addr.ipv4;
break;
}
}
printf("Error: Incorrect Input\n");
break;
case 8:
printf("Enter Dest IPv6 address \n");
scanf("%s",strIpaddr);
if (getIpAddrFromStr(AF_INET6, strIpaddr, &addr) ==
OPEN_E_NONE)
{
memcpy(&policyInfo->matchDstIpv6Addr, &addr.addr.ipv6,
sizeof(policyInfo->matchDstIpv6Addr));
printf("Enter Mask \n");
scanf("%s",strSubnetMask);
if (getIpAddrFromStr(AF_INET6, strSubnetMask, &addr) ==
OPEN_E_NONE)
{
memcpy(&policyInfo->matchDstIpv6Mask, &addr.addr.ipv6,
sizeof(policyInfo->matchDstIpv6Mask));
break;
}
}
printf("Error: Incorrect Input\n");
break;
case 9:
printf("Enter Src IPv6 address \n");
scanf("%s",strIpaddr);
if (getIpAddrFromStr(AF_INET6, strIpaddr, &addr) ==
OPEN_E_NONE)
{
memcpy(&policyInfo->matchSrcIpv6Addr, &addr.addr.ipv6,
sizeof(policyInfo->matchSrcIpv6Addr));
printf("Enter Mask \n");
scanf("%s",strSubnetMask);
if (getIpAddrFromStr(AF_INET6, strSubnetMask, &addr) ==
OPEN_E_NONE)
{
memcpy(&policyInfo->matchSrcIpv6Mask, &addr.addr.ipv6,
sizeof(policyInfo->matchSrcIpv6Mask));
break;
}
}
printf("Error: Incorrect Input\n");
break;
case 10:
default:
return;
}
} while (response != 10);
}
static void openAddClassifiersV2(open_tcamApiPolicy_v2_t *policyInfo)
{
char mac[TCAM_MAX_STR_LENGTH];
uint32_t response;
uint32_t value;
uint32_t mask;
char strSubnetMask[TCAM_MAX_STR_LENGTH];
char strIpaddr[TCAM_MAX_STR_LENGTH];
printf("This selection will add multiple classifiers to the policy\n");
do
{
response = 0;
printf("Enter classification type \n"
"1 to match dest MAC\n"
"2 to match Src MAC\n"
"3 to match VLAN\n"
"4 to match VLAN Priority\n"
"5 to match ether type\n"
"6 to match dest IP address\n"
"7 to match src IP address \n"
"8 to match dest IPv6 address\n"
"9 to match src IPv6 address\n"
"10 to match CPU visibility \n"
"11 quit \n");
scanf("%d", &response);
switch (response)
{
case 1:
memset(mac, 0, sizeof(mac));
printf("Enter Mac address\n");
scanf("%s", mac);
if (getMacAddrFromInput(mac, policyInfo->matchDstMac))
printf("Error: Invalid input\n");
break;
case 2:
memset(mac, 0, sizeof(mac));
printf("Enter Mac address\n");
scanf("%s", mac);
if (getMacAddrFromInput(mac, policyInfo->matchSrcMac))
printf("Error: Invalid input\n");
break;
case 3:
printf("Enter VLAN id \n");
scanf("%d",&value);
policyInfo->matchVlanVal = value;
break;
case 4:
printf("Enter VLAN priority \n");
scanf("%d",&value);
policyInfo->matchVlanPrio = value;
break;
case 5:
printf("Enter ether type in hex\n");
scanf("%x",&value);
policyInfo->matchEthType = value;
break;
case 6:
printf("Enter Dest IP address \n");
scanf("%s",strIpaddr);
if (getIpAddrFromStr(AF_INET, strIpaddr, &addr) ==
OPEN_E_NONE)
{
policyInfo->matchDstIp = addr.addr.ipv4;
printf("Enter Mask \n");
scanf("%s",strSubnetMask);
if (getIpAddrFromStr(AF_INET, strSubnetMask, &addr) ==
OPEN_E_NONE)
{
policyInfo->matchDstIpMask = addr.addr.ipv4;
break;
}
}
printf("Error: Incorrect Input\n");
break;
case 7:
printf("Enter Src IP address \n");
scanf("%s",strIpaddr);
if (getIpAddrFromStr(AF_INET, strIpaddr, &addr) ==
OPEN_E_NONE)
{
policyInfo->matchSrcIp = addr.addr.ipv4;
printf("Enter Mask \n");
scanf("%s",strSubnetMask);
if (getIpAddrFromStr(AF_INET, strSubnetMask, &addr) ==
OPEN_E_NONE)
{
policyInfo->matchSrcIpMask = addr.addr.ipv4;
break;
}
}
printf("Error: Incorrect Input\n");
break;
case 8:
printf("Enter Dest IPv6 address \n");
scanf("%s",strIpaddr);
if (getIpAddrFromStr(AF_INET6, strIpaddr, &addr) ==
OPEN_E_NONE)
{
memcpy(&policyInfo->matchDstIpv6Addr, &addr.addr.ipv6,
sizeof(policyInfo->matchDstIpv6Addr));
printf("Enter Mask \n");
scanf("%s",strSubnetMask);
if (getIpAddrFromStr(AF_INET6, strSubnetMask, &addr) ==
OPEN_E_NONE)
{
memcpy(&policyInfo->matchDstIpv6Mask, &addr.addr.ipv6,
sizeof(policyInfo->matchDstIpv6Mask));
break;
}
}
printf("Error: Incorrect Input\n");
break;
case 9:
printf("Enter Src IPv6 address \n");
scanf("%s",strIpaddr);
if (getIpAddrFromStr(AF_INET6, strIpaddr, &addr) ==
OPEN_E_NONE)
{
memcpy(&policyInfo->matchSrcIpv6Addr, &addr.addr.ipv6,
sizeof(policyInfo->matchSrcIpv6Addr));
printf("Enter Mask \n");
scanf("%s",strSubnetMask);
if (getIpAddrFromStr(AF_INET6, strSubnetMask, &addr) ==
OPEN_E_NONE)
{
memcpy(&policyInfo->matchSrcIpv6Mask, &addr.addr.ipv6,
sizeof(policyInfo->matchSrcIpv6Mask));
break;
}
}
printf("Error: Incorrect Input\n");
break;
case 10:
printf("Enter CPU visibility qualifier value \n");
scanf("%d",&value);
if ((value != 0) &&
(value != 1))
{
printf("Error: Incorrect Input, please enter either 0 or 1\n");
break;
}
policyInfo->matchCpuVisibility = value;
printf("Enter CPU visibility qualifier mask \n");
scanf("%d",&mask);
if ((mask != 0) &&
(mask != 0xf))
{
printf("Error: Incorrect Input, please enter either 0 or 15\n");
break;
}
policyInfo->matchCpuVisibilityMask = mask;
break;
case 11:
default:
return;
}
} while (response != 11);
}
{
char mac[TCAM_MAX_STR_LENGTH];
uint32_t response;
uint32_t value;
uint32_t len;
printf("This selection will add multiple actions to the policy\n");
do
{
response = 0;
printf("Enter Action \n"
"1 to add/change outer VLAN\n"
"2 to set VLAN priority\n"
"3 to set new src mac\n"
"4 to set new dst mac\n"
"5 to redirect the traffic to another port\n"
"6 to change ether type\n"
"7 to send packets to CPU\n"
"8 to drop the packets\n"
"10 quit \n");
scanf("%d", &response);
switch (response)
{
case 1:
printf("Enter VLAN ID \n");
scanf("%d",&value);
policyInfo->actionVal.vlanId = value;
break;
case 2:
printf("Enter VLAN priority\n");
scanf("%d",&value);
policyInfo->actionVal.vlanPrio = value;
break;
case 3:
memset(mac, 0, sizeof(mac));
printf("Enter Mac address\n");
scanf("%s", mac);
if (getMacAddrFromInput(mac, policyInfo->actionVal.srcAddr))
printf("Invalid input\n");
break;
case 4:
memset(mac, 0, sizeof(mac));
printf("Enter Mac address\n");
scanf("%s", mac);
if (getMacAddrFromInput(mac, policyInfo->actionVal.dstMac))
printf("Error: Invalid input\n");
break;
case 5:
printf("Enter intf string\n");
scanf("%s", redirIntfString);
len = strlen(redirIntfString);
buffDesc.
size = len < (
sizeof(redirIntfString) -1) ? len : (
sizeof(redirIntfString) -1);
buffDesc.
pstart = redirIntfString;
{
printf("Error: Incorrect interface name %s \n", intfString);
}
break;
case 6:
printf("Enter ether type in hex\n");
scanf("%x",&value);
policyInfo->actionVal.etherType = value;
break;
case 7:
printf("Enter rule number\n");
scanf("%d",&value);
policyInfo->ruleNum = value;
break;
case 8:
case 10:
default:
return;
}
} while (response != 10);
}
static void openAddPolicyActionV2(
openapiClientHandle_t *clientHandle, open_tcamApiPolicy_v2_t *policyInfo)
{
char mac[TCAM_MAX_STR_LENGTH];
uint32_t response;
uint32_t value;
uint32_t len;
printf("This selection will add multiple actions to the policy\n");
do
{
response = 0;
printf("Enter Action \n"
"1 to add/change outer VLAN\n"
"2 to set VLAN priority\n"
"3 to set new src mac\n"
"4 to set new dst mac\n"
"5 to redirect the traffic to another port\n"
"6 to change ether type\n"
"7 to send packets to CPU\n"
"8 to drop the packets\n"
"10 quit \n");
scanf("%d", &response);
switch (response)
{
case 1:
printf("Enter VLAN ID \n");
scanf("%d",&value);
policyInfo->actionVal.vlanId = value;
break;
case 2:
printf("Enter VLAN priority\n");
scanf("%d",&value);
policyInfo->actionVal.vlanPrio = value;
break;
case 3:
memset(mac, 0, sizeof(mac));
printf("Enter Mac address\n");
scanf("%s", mac);
if (getMacAddrFromInput(mac, policyInfo->actionVal.srcAddr))
printf("Invalid input\n");
break;
case 4:
memset(mac, 0, sizeof(mac));
printf("Enter Mac address\n");
scanf("%s", mac);
if (getMacAddrFromInput(mac, policyInfo->actionVal.dstMac))
printf("Error: Invalid input\n");
break;
case 5:
printf("Enter intf string\n");
scanf("%s", redirIntfString);
len = strlen(redirIntfString);
buffDesc.size = len < (sizeof(redirIntfString) -1) ? len : (sizeof(redirIntfString) -1);
buffDesc.pstart = redirIntfString;
{
printf("Error: Incorrect interface name %s \n", intfString);
}
break;
case 6:
printf("Enter ether type in hex\n");
scanf("%x",&value);
policyInfo->actionVal.etherType = value;
break;
case 7:
printf("Enter rule number\n");
scanf("%d",&value);
policyInfo->ruleNum = value;
break;
case 8:
case 10:
default:
return;
}
} while (response != 10);
}
{
open_tcamApiPolicy_t policyInfo;
open_tcamApiPolicy_t getPolicyInfo;
uint32_t action = 0, intfNum;
uint32_t len;
uint64_t inProf = 0, outProf = 0;
open_tcamApiPolicy_v2_t policyInfoV2;
do
{
printMenu();
action = 0;
scanf("%d",&action);
switch (action)
{
case 1:
openBuildHeader(&policyInfo);
case 2:
openAddClassifiers(&policyInfo);
case 3:
openAddPolicyAction(clientHandle, &policyInfo);
break;
case 4:
{
{
printf("Error: Policy could not be created Reason %d", errorCode);
}
else
{
printf("Policy succesfully added,handle is %d \n", policyHandle);
}
}
break;
case 5:
case 6:
{
printf("Enter policy Handle\n");
scanf("%d", &policyHandle);
}
printf("Enter intf string\n");
scanf("%s", intfString);
len = strlen(intfString);
buffDesc.size = len < (sizeof(intfString) -1) ? len : (sizeof(intfString) -1);
buffDesc.pstart = intfString;
{
printf("Error: Incorrect interface name %s \n", intfString);
}
else
{
if (action == 22)
{
}
else
{
}
{
printf("Error: Policy could not be applied to intf(%d) %d", intfNum, errorCode);
}
}
break;
case 7:
{
printf("Enter policy Handle\n");
scanf("%d", &policyHandle);
}
{
}
break;
case 8:
{
printf("Enter policy Handle\n");
scanf("%d", &policyHandle);
}
{
printf("Error: Create policy first \n");
}
else
{
printf("Error: Policy does not exist\n");
}
break;
case 9:
break;
case 10:
{
{
printf("Error: Policy could not be applied on all interfaces, error %d\n", errorCode);
return errorCode;
}
else
{
printf("Policy is applied on all interfaces\n");
}
}
break;
case 11:
{
{
printf("Error: Policy could not be applied on all interfaces, error %d\n", errorCode);
return errorCode;
}
else
{
printf("Policy is applied on all interfaces\n");
}
{
printf("Error: Unable to get Policy stats, error %d\n", errorCode);
return errorCode;
}
else
{
printf("Policy stats retrieved successfully\n");
}
}
break;
case 12:
{
{
printf("Error: Policy could not be applied on all interfaces, error %d\n", errorCode);
return errorCode;
}
else
{
printf("Policy is applied on all interfaces\n");
}
printf("Start transmitting traffic and wait for 10 seconds ...");
sleep(10);
{
printf("Error: Unable to clear Policy stats, error %d\n", errorCode);
return errorCode;
}
else
{
printf("Policy stats cleared successfully\n");
}
}
break;
case 13:
openBuildHeaderV2(&policyInfoV2);
case 14:
openAddClassifiersV2(&policyInfoV2);
case 15:
openAddPolicyActionV2(clientHandle, &policyInfoV2);
break;
case 16:
{
{
printf("Error: Policy could not be created Reason %d", errorCode);
}
else
{
printf("Policy succesfully added,handle is %d \n", policyHandle);
}
}
break;
case 17:
break;
default:
return 0;
}
}
while (action != 17);
return 0;
}
int
main (int argc, char **argv)
{
char switch_os_revision_string[100];
l7proc_crashlog_register ();
if ((result =
{
printf ("\nFailed to initialize RPC to OpEN. Exiting (result = %d)\n", result);
exit (2);
}
{
sleep (1);
}
L7PROC_LOGF (L7PROC_LOG_SEVERITY_INFO, 0, "Starting TCAM API example application");
printf ("\n");
switch_os_revision.pstart = switch_os_revision_string;
switch_os_revision.size = sizeof (switch_os_revision_string);
{
printf ("Network OS version = %s\n", switch_os_revision_string);
}
else
{
printf ("Network OS version retrieve error\n");
}
{
}
else
{
printf ("TCAM API version retrieve error\n");
}
printf ("\n");
return openTcamMainMenu(&clientHandle);
}