#include <stdlib.h>
#include <unistd.h>
#include "rpcclt_openapi.h"
#include "proc_util.h"
#define TCAM_MAX_STR_LENGTH 256
{
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];
static void openapiReceivePackets(uint32_t agentNum, uint32_t packetCount,
char *fileName)
{
uint32_t i, pktCount, offset;
uint32_t bytesRcvd;
int sockfd = -1;
char agentName[] = "Test Agent";
FILE *fp = NULL;
fp = fopen(fileName, "w");
if (fp == NULL)
{
printf("Could not open file for writing...\n");
return;
}
agentNameBuff.
pstart = agentName;
agentNameBuff.
size =
sizeof(agentName);
{
printf("Failed to register as rx agent\n");
return;
}
if (l7proc_extagentpkt_rxsockcreate(agentNum, &sockfd) != 0)
{
fclose(fp);
return;
}
pktCount = 0;
while (pktCount < packetCount)
{
memset(buffer, 0, sizeof(buffer));
bytesRcvd = recvfrom(sockfd, buffer, sizeof(buffer), 0, 0, 0);
if (bytesRcvd <= 0)
{
continue;
}
fprintf(fp, "Packet Received from ifNum %d, pktLen %d\n",
pktDesc->
rxIfNum, pktDesc->
pktLength);
fprintf(fp,"Packet content: \n");
offset = pktDesc->
descLen;
for (i = 0; i < pktDesc->pktLength; i++)
{
fprintf(fp,"%2.2x ", buffer[offset + i]);
}
fprintf(fp, "\n\n");
pktCount++;
fflush(fp);
}
l7proc_extagentpkt_rxsockclose(agentNum, sockfd);
fclose(fp);
exit(0);
return;
}
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 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);
}
uint32_t agentCookie)
{
policyInfo->ruleNum = agentCookie;
}
char *fileName)
{
open_tcamApiPolicy_t policyInfo;
uint32_t intfNum;
uint32_t len;
do
{
openBuildHeader(&policyInfo);
openAddClassifiers(&policyInfo);
openAddPolicyAction(clientHandle, &policyInfo, agentCookie);
{
printf("Error: Policy could not be created Reason %d", errorCode);
break;
}
else
{
printf("Policy succesfully added,handle is %d \n", policyHandle);
}
{
printf("Error: Create policy first \n");
}
else
{
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);
break;
}
else
{
{
printf("Error: Policy could not be applied to intf(%d) %d", intfNum, errorCode);
break;
}
}
openapiReceivePackets(agentCookie, packetCount, fileName);
}
}
while (0);
return 0;
}
int
main (int argc, char **argv)
{
char switch_os_revision_string[100];
uint32_t packetCount;
uint32_t agentCookie;
char *fileName;
if (argc != 4)
{
printf ("Usage: %s <agentNum> <packetCount> <fileName>\n", argv[0]);
exit (1);
}
agentCookie = atoi(argv[1]);
packetCount = atoi(argv[2]);
fileName = argv[3];
printf("cookie %d count %d and name %s \n", agentCookie, packetCount, fileName);
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 ("Network OS Version retrieve error\n");
}
printf ("\n");
return openPacketRx(&clientHandle, agentCookie, packetCount, fileName);
}