Skip to content

Commit 4fdcb45

Browse files
committed
Add methods to resolve mac in ipv6 mode
Signed-off-by: Leo Ma <[email protected]>
1 parent dff0563 commit 4fdcb45

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/main/java/com/cisco/trex/stateless/IPv6NeighborDiscoveryService.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,53 @@ public EthernetPacket sendIcmpV6Echo(int portIdx, String dstIp, int icmpId, int
126126

127127
}
128128

129+
public EthernetPacket sendNeighborSolicitation(int portIdx, int timeout, String dstIp) throws ServiceModeRequiredException {
130+
endTs = System.currentTimeMillis() + timeout * 1000;
131+
PortStatus portStatus = tRexClient.getPortStatus(portIdx).get();
132+
133+
srcMac = portStatus.getAttr().getLayerConiguration().getL2Configuration().getSrc();
134+
135+
Packet icmpv6NSPkt = buildICMPV6NSPkt(multicastMacFromIPv6(dstIp).toString(), dstIp, null);
136+
137+
tRexClient.startStreamsIntermediate(portIdx, Arrays.asList(buildStream(icmpv6NSPkt)));
138+
139+
Predicate<EthernetPacket> ipV6NAPktFilter = etherPkt -> {
140+
if (!etherPkt.contains(IcmpV6NeighborAdvertisementPacket.class)) {
141+
return false;
142+
}
143+
IcmpV6NeighborAdvertisementHeader icmpV6NaHdr = etherPkt.get(IcmpV6NeighborAdvertisementPacket.class).getHeader();
144+
145+
String nodeIp = icmpV6NaHdr.getTargetAddress().toString().substring(1);
146+
147+
IpV6Packet.IpV6Header ipV6Header = etherPkt.get(IpV6Packet.class).getHeader();
148+
String dstAddr = ipV6Header.getDstAddr().toString().substring(1);
149+
150+
try {
151+
Inet6Address dstIPv6Addr = (Inet6Address) Inet6Address.getByName(dstAddr);
152+
Inet6Address srcIPv6Addr = (Inet6Address) Inet6Address.getByName(generateIPv6AddrFromMAC(srcMac));
153+
154+
Inet6Address nodeIpv6 = (Inet6Address) Inet6Address.getByName(nodeIp);
155+
Inet6Address targetIpv6inNS = (Inet6Address) Inet6Address.getByName(dstIp);
156+
return icmpV6NaHdr.getSolicitedFlag() && nodeIpv6.equals(targetIpv6inNS) && dstIPv6Addr.equals(srcIPv6Addr);
157+
} catch (UnknownHostException ignored) {}
158+
return false;
159+
};
160+
161+
EthernetPacket na = null;
162+
while (endTs > System.currentTimeMillis() && na == null) {
163+
List<EthernetPacket> pkts = tRexClient.getRxQueue(portIdx, ipV6NAPktFilter);
164+
if (!pkts.isEmpty()) {
165+
na = pkts.get(0);
166+
}
167+
}
168+
tRexClient.removeRxQueue(portIdx);
169+
if (tRexClient.getPortStatus(portIdx).get().getState().equals("TX")) {
170+
tRexClient.stopTraffic(portIdx);
171+
}
172+
tRexClient.removeAllStreams(portIdx);
173+
return na;
174+
}
175+
129176
private Map<String, EthernetPacket> sendNSandIcmpV6Req(int portIdx, int timeDuration, String dstIp) throws ServiceModeRequiredException {
130177
endTs = System.currentTimeMillis() + timeDuration * 1000;
131178
TRexClientResult<PortStatus> portStatusResult = tRexClient.getPortStatus(portIdx);

src/main/java/com/cisco/trex/stateless/TRexClient.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.pcap4j.packet.IcmpV4CommonPacket;
2626
import org.pcap4j.packet.IcmpV4EchoPacket;
2727
import org.pcap4j.packet.IcmpV4EchoReplyPacket;
28+
import org.pcap4j.packet.IcmpV6NeighborAdvertisementPacket;
2829
import org.pcap4j.packet.IllegalRawDataException;
2930
import org.pcap4j.packet.IpV4Packet;
3031
import org.pcap4j.packet.IpV4Rfc791Tos;
@@ -513,6 +514,18 @@ private static Stream build1PktSingleBurstStream(Packet pkt) {
513514
-1);
514515
}
515516

517+
public String resolveIpv6(int portIndex, String dstIp) throws ServiceModeRequiredException {
518+
removeRxQueue(portIndex);
519+
setRxQueue(portIndex, 1000);
520+
521+
EthernetPacket naPacket = new IPv6NeighborDiscoveryService(this).sendNeighborSolicitation(portIndex, 5, dstIp);
522+
if (naPacket != null) {
523+
return naPacket.getHeader().getSrcAddr().toString();
524+
}
525+
526+
return null;
527+
}
528+
516529
public List<EthernetPacket> getRxQueue(int portIndex, Predicate<EthernetPacket> filter) {
517530

518531
Map<String, Object> payload = createPayload(portIndex);

0 commit comments

Comments
 (0)