Skip to content

Commit 9b025c3

Browse files
committed
support new feature about pcap file parse and fix a bug
1 parent 796e781 commit 9b025c3

File tree

9 files changed

+745
-38
lines changed

9 files changed

+745
-38
lines changed

src/main/java/com/cisco/trex/stateful/AstfAssociation.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,16 @@ public JsonArray toJson() {
4444
return jsonArray;
4545
}
4646

47+
/**
48+
* get Port
49+
*
50+
* @return port
51+
*/
52+
public int getPort() {
53+
if (astfAssociationRuleList.size() != 1) {
54+
throw new IllegalStateException(String.format("rule list size should be 1, but it's %s now", astfAssociationRuleList.size()));
55+
}
56+
return astfAssociationRuleList.get(0).getPort();
57+
}
58+
4759
}

src/main/java/com/cisco/trex/stateful/AstfAssociationRule.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
* Astf Association Rule
88
*/
99
public class AstfAssociationRule {
10-
private String ipStart;
11-
private String ipEnd;
1210
private JsonObject fields = new JsonObject();
1311
private int port;
1412

@@ -21,8 +19,6 @@ public class AstfAssociationRule {
2119
*/
2220
public AstfAssociationRule(String ipStart, String ipEnd, int port) {
2321
this.port = port;
24-
this.ipStart = ipStart;
25-
this.ipEnd = ipEnd;
2622
fields.addProperty("port", port);
2723
if (!StringUtils.isEmpty(ipStart)) {
2824
fields.addProperty("ip_start", ipStart);

src/main/java/com/cisco/trex/stateful/AstfCmd.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
public abstract class AstfCmd {
99

1010
protected JsonObject fields;
11-
protected boolean stream = false;
12-
protected boolean buffer = false;
11+
protected Boolean stream = null;
12+
protected Boolean buffer = null;
1313

1414
/**
1515
* construct
@@ -30,7 +30,7 @@ public AstfCmd() {
3030
*
3131
* @return true if it's stream
3232
*/
33-
public boolean isStream() {
33+
public Boolean isStream() {
3434
return stream;
3535
}
3636

@@ -48,7 +48,7 @@ public JsonObject toJson() {
4848
*
4949
* @return true if it's buffer
5050
*/
51-
public boolean isBuffer() {
51+
public Boolean isBuffer() {
5252
return buffer;
5353
}
5454

src/main/java/com/cisco/trex/stateful/AstfCmdTxPkt.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public AstfCmdTxPkt(byte[] asciiBuf) {
2222
fields.addProperty("name", NAME);
2323
fields.addProperty("buf_index", -1);
2424
this.bufLen = asciiBuf.length;
25-
buffer = true;
2625
stream = false;
26+
buffer = true;
2727
}
2828

2929
/**
@@ -58,11 +58,6 @@ public String getName() {
5858
return NAME;
5959
}
6060

61-
@Override
62-
public boolean isBuffer() {
63-
return true;
64-
}
65-
6661
/**
6762
* get buf length
6863
*

src/main/java/com/cisco/trex/stateful/AstfProfile.java

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
* ASTF profile
1616
*/
1717
public class AstfProfile {
18-
protected static final Logger LOGGER = LoggerFactory.getLogger(AstfProfile.class);
18+
private static final Logger LOGGER = LoggerFactory.getLogger(AstfProfile.class);
19+
20+
private static final String L7_PRECENT = "l7_percent";
21+
private static final String CPS = "cps";
1922

2023
private AstfIpGen astfIpGen;
2124
private AstfGlobalInfo astfClientGlobalInfo;
@@ -53,26 +56,90 @@ public AstfProfile(AstfIpGen defaultIpGen, AstfGlobalInfo astfClientGlobalInfo,
5356
this.astfCapInfoList = astfCapInfoList;
5457

5558
/**
56-
* for pcap file scenario
57-
* TODO: need to be implemented in the future
59+
* for pcap file parse scenario
5860
*/
5961
if (astfCapInfoList != null && astfCapInfoList.size() != 0) {
60-
6162
String mode = null;
62-
Map<String, Object> allCapInfo = new HashMap();
63+
List<Map<String, Object>> allCapInfo = new ArrayList();
6364
List<Integer> dPorts = new ArrayList();
6465
int totalPayload = 0;
6566
for (AstfCapInfo capInfo : astfCapInfoList) {
6667
String capFile = capInfo.getFilePath();
6768
AstfIpGen ipGen = capInfo.getAstfIpGen() != null ? capInfo.getAstfIpGen() : defaultIpGen;
6869
AstfGlobalInfoPerTemplate globC = capInfo.getClientGlobInfo();
6970
AstfGlobalInfoPerTemplate globS = capInfo.getServerGlobInfo();
70-
AstfProgram progC = new AstfProgram(new File(capFile), AstfProgram.SideType.Client, null, true);
71-
AstfProgram progS = new AstfProgram(new File(capFile), AstfProgram.SideType.Server, null, true);
71+
AstfProgram progC = new AstfProgram(capFile, AstfProgram.SideType.Client);
72+
AstfProgram progS = new AstfProgram(capFile, AstfProgram.SideType.Server);
7273
progC.updateKeepAlive(progS);
73-
//TODO: analysis pcap file data.
74+
75+
AstfTcpInfo tcpC = new AstfTcpInfo(capFile);
76+
int tcpCPort = tcpC.getPort();
77+
float cps = capInfo.getCps();
78+
float l7Percent = capInfo.getL7Percent();
79+
if (mode == null) {
80+
if (l7Percent > 0) {
81+
mode = L7_PRECENT;
82+
} else {
83+
mode = CPS;
84+
}
85+
} else {
86+
if (mode.equals(L7_PRECENT) && l7Percent == 0) {
87+
throw new IllegalStateException("If one cap specifies l7_percent, then all should specify it");
88+
}
89+
if (mode.equals(CPS) && l7Percent > 0) {
90+
throw new IllegalStateException("Can't mix specifications of cps and l7_percent in same cap list");
91+
}
92+
}
93+
totalPayload += progC.getPayloadLen();
94+
95+
int dPort;
96+
AstfAssociation myAssoc;
97+
if (capInfo.getAssoc() == null) {
98+
dPort = tcpCPort;
99+
myAssoc = new AstfAssociation(new AstfAssociationRule(dPort));
100+
} else {
101+
dPort = capInfo.getAssoc().getPort();
102+
myAssoc = capInfo.getAssoc();
103+
throw new IllegalStateException(String.format("More than one cap use dest port %s. This is currently not supported.", dPort));
104+
}
105+
dPorts.add(dPort);
106+
107+
/**
108+
* add param to cap info map
109+
*/
110+
HashMap<String, Object> map = new HashMap();
111+
map.put("ip_gen", ipGen);
112+
map.put("prog_c", progC);
113+
map.put("prog_s", progS);
114+
map.put("glob_c", globC);
115+
map.put("glob_s", globS);
116+
map.put("cps", cps);
117+
map.put("d_port", dPort);
118+
map.put("my_assoc", myAssoc);
119+
map.put("limit", capInfo.getLimit());
120+
allCapInfo.add(map);
121+
}
122+
123+
//calculate cps from l7 percent
124+
if (mode.equals(L7_PRECENT)) {
125+
float percentSum = 0;
126+
for (Map<String, Object> map : allCapInfo) {
127+
float newCps = ((AstfProgram) map.get("prog_c")).getPayloadLen() * 100.0f / totalPayload;
128+
map.put("cps", newCps);
129+
percentSum += newCps;
130+
}
131+
if (percentSum != 100) {
132+
throw new IllegalStateException("l7_percent values must sum up to 100");
133+
}
74134
}
75135

136+
for (Map<String, Object> map : allCapInfo) {
137+
AstfTcpClientTemplate tempC = new AstfTcpClientTemplate((AstfProgram) map.get("prog_c"), (AstfIpGen) map.get("ip_gen"), null,
138+
(int) map.get("d_port"), (float) map.get("cps"), (AstfGlobalInfoPerTemplate) map.get("glob_c"), (int) map.get("limit"));
139+
AstfTcpServerTemplate tempS = new AstfTcpServerTemplate((AstfProgram) map.get("prog_s"), (AstfAssociation) map.get("my_assoc"), (AstfGlobalInfoPerTemplate) map.get("glob_s"));
140+
AstfTemplate template = new AstfTemplate(tempC, tempS);
141+
astfTemplateList.add(template);
142+
}
76143
}
77144
}
78145

0 commit comments

Comments
 (0)