use of org.onlab.packet.Ethernet in project trellis-control by opennetworkinglab.
the class IcmpHandlerTest method testPing4GatewayPair.
// Ping to a dh gateway
@Test
public void testPing4GatewayPair() {
// Expected behavior
expect(segmentRoutingManager.deviceService.isAvailable(LOCAL_LEAF1)).andReturn(true).times(1);
expect(segmentRoutingManager.deviceService.isAvailable(LOCAL_LEAF2)).andReturn(true).times(1);
replay(segmentRoutingManager.deviceService);
// Process
icmpHandler.processIcmp(ETH_REQ_IPV4_GATEWAY_PAIR, CP2011);
// Verify packet-out
Ethernet ethernet = packetService.getEthernetPacket(ETH_REQ_IPV4_GATEWAY_PAIR.getSourceMAC());
assertNotNull(ethernet);
assertThat(ethernet.getSourceMAC(), is(ETH_REQ_IPV4_GATEWAY_PAIR.getDestinationMAC()));
assertThat(ethernet.getDestinationMAC(), is(ETH_REQ_IPV4_GATEWAY_PAIR.getSourceMAC()));
assertTrue(ethernet.getPayload() instanceof IPv4);
IPv4 ip = (IPv4) ethernet.getPayload();
assertThat(ip.getSourceAddress(), is(DST_IPV4_GATEWAY_PAIR.toInt()));
assertThat(ip.getDestinationAddress(), is(SRC_IPV41.toInt()));
assertTrue(ip.getPayload() instanceof ICMP);
ICMP icmp = (ICMP) ip.getPayload();
assertThat(icmp.getIcmpType(), is(TYPE_ECHO_REPLY));
assertThat(icmp.getIcmpCode(), is(CODE_ECHO_REPLY));
// Verify behavior
verify(segmentRoutingManager.deviceService);
}
use of org.onlab.packet.Ethernet in project trellis-control by opennetworkinglab.
the class IcmpHandlerTest method testPing4MyGateway.
// Ping to our gateway
@Test
public void testPing4MyGateway() {
// Expected behavior
expect(segmentRoutingManager.deviceService.isAvailable(REMOTE_LEAF)).andReturn(true).times(1);
replay(segmentRoutingManager.deviceService);
// Process
icmpHandler.processIcmp(ETH_REQ_IPV4_MY, CP12);
// Verify packet-out
Ethernet ethernet = packetService.getEthernetPacket(ETH_REQ_IPV4_MY.getSourceMAC());
assertNotNull(ethernet);
assertThat(ethernet.getSourceMAC(), is(ETH_REQ_IPV4_MY.getDestinationMAC()));
assertThat(ethernet.getDestinationMAC(), is(ETH_REQ_IPV4_MY.getSourceMAC()));
assertTrue(ethernet.getPayload() instanceof IPv4);
IPv4 ip = (IPv4) ethernet.getPayload();
assertThat(ip.getSourceAddress(), is(DST_IPV4.toInt()));
assertThat(ip.getDestinationAddress(), is(SRC_IPV4_MY.toInt()));
assertTrue(ip.getPayload() instanceof ICMP);
ICMP icmp = (ICMP) ip.getPayload();
assertThat(icmp.getIcmpType(), is(TYPE_ECHO_REPLY));
assertThat(icmp.getIcmpCode(), is(CODE_ECHO_REPLY));
// Verify behavior
verify(segmentRoutingManager.deviceService);
}
use of org.onlab.packet.Ethernet in project onos by opennetworkinglab.
the class HostMonitor method buildArpRequest.
private Ethernet buildArpRequest(IpAddress targetIp, IpAddress sourceIp, MacAddress sourceMac, VlanId vlan) {
ARP arp = new ARP();
arp.setHardwareType(ARP.HW_TYPE_ETHERNET).setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH).setProtocolType(ARP.PROTO_TYPE_IP).setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH).setOpCode(ARP.OP_REQUEST);
arp.setSenderHardwareAddress(sourceMac.toBytes()).setSenderProtocolAddress(sourceIp.toOctets()).setTargetHardwareAddress(ZERO_MAC_ADDRESS).setTargetProtocolAddress(targetIp.toOctets());
Ethernet ethernet = new Ethernet();
ethernet.setEtherType(Ethernet.TYPE_ARP).setDestinationMACAddress(MacAddress.BROADCAST).setSourceMACAddress(sourceMac).setPayload(arp);
if (!vlan.equals(VlanId.NONE)) {
ethernet.setVlanID(vlan.toShort());
}
ethernet.setPad(true);
return ethernet;
}
use of org.onlab.packet.Ethernet in project onos by opennetworkinglab.
the class ReactiveForwarding method installRule.
// Install a rule forwarding the packet to the specified port.
private void installRule(PacketContext context, PortNumber portNumber, ReactiveForwardMetrics macMetrics) {
//
// We don't support (yet) buffer IDs in the Flow Service so
// packet out first.
//
Ethernet inPkt = context.inPacket().parsed();
TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
// If PacketOutOnly or ARP packet than forward directly to output port
if (packetOutOnly || inPkt.getEtherType() == Ethernet.TYPE_ARP) {
packetOut(context, portNumber, macMetrics);
return;
}
//
if (matchDstMacOnly) {
selectorBuilder.matchEthDst(inPkt.getDestinationMAC());
} else {
selectorBuilder.matchInPort(context.inPacket().receivedFrom().port()).matchEthSrc(inPkt.getSourceMAC()).matchEthDst(inPkt.getDestinationMAC());
// If configured Match Vlan ID
if (matchVlanId && inPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) {
selectorBuilder.matchVlanId(VlanId.vlanId(inPkt.getVlanID()));
}
//
if (matchIpv4Address && inPkt.getEtherType() == Ethernet.TYPE_IPV4) {
IPv4 ipv4Packet = (IPv4) inPkt.getPayload();
byte ipv4Protocol = ipv4Packet.getProtocol();
Ip4Prefix matchIp4SrcPrefix = Ip4Prefix.valueOf(ipv4Packet.getSourceAddress(), Ip4Prefix.MAX_MASK_LENGTH);
Ip4Prefix matchIp4DstPrefix = Ip4Prefix.valueOf(ipv4Packet.getDestinationAddress(), Ip4Prefix.MAX_MASK_LENGTH);
selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPSrc(matchIp4SrcPrefix).matchIPDst(matchIp4DstPrefix);
if (matchIpv4Dscp) {
byte dscp = ipv4Packet.getDscp();
byte ecn = ipv4Packet.getEcn();
selectorBuilder.matchIPDscp(dscp).matchIPEcn(ecn);
}
if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_TCP) {
TCP tcpPacket = (TCP) ipv4Packet.getPayload();
selectorBuilder.matchIPProtocol(ipv4Protocol).matchTcpSrc(TpPort.tpPort(tcpPacket.getSourcePort())).matchTcpDst(TpPort.tpPort(tcpPacket.getDestinationPort()));
}
if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_UDP) {
UDP udpPacket = (UDP) ipv4Packet.getPayload();
selectorBuilder.matchIPProtocol(ipv4Protocol).matchUdpSrc(TpPort.tpPort(udpPacket.getSourcePort())).matchUdpDst(TpPort.tpPort(udpPacket.getDestinationPort()));
}
if (matchIcmpFields && ipv4Protocol == IPv4.PROTOCOL_ICMP) {
ICMP icmpPacket = (ICMP) ipv4Packet.getPayload();
selectorBuilder.matchIPProtocol(ipv4Protocol).matchIcmpType(icmpPacket.getIcmpType()).matchIcmpCode(icmpPacket.getIcmpCode());
}
}
//
if (matchIpv6Address && inPkt.getEtherType() == Ethernet.TYPE_IPV6) {
IPv6 ipv6Packet = (IPv6) inPkt.getPayload();
byte ipv6NextHeader = ipv6Packet.getNextHeader();
Ip6Prefix matchIp6SrcPrefix = Ip6Prefix.valueOf(ipv6Packet.getSourceAddress(), Ip6Prefix.MAX_MASK_LENGTH);
Ip6Prefix matchIp6DstPrefix = Ip6Prefix.valueOf(ipv6Packet.getDestinationAddress(), Ip6Prefix.MAX_MASK_LENGTH);
selectorBuilder.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Src(matchIp6SrcPrefix).matchIPv6Dst(matchIp6DstPrefix);
if (matchIpv6FlowLabel) {
selectorBuilder.matchIPv6FlowLabel(ipv6Packet.getFlowLabel());
}
if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_TCP) {
TCP tcpPacket = (TCP) ipv6Packet.getPayload();
selectorBuilder.matchIPProtocol(ipv6NextHeader).matchTcpSrc(TpPort.tpPort(tcpPacket.getSourcePort())).matchTcpDst(TpPort.tpPort(tcpPacket.getDestinationPort()));
}
if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_UDP) {
UDP udpPacket = (UDP) ipv6Packet.getPayload();
selectorBuilder.matchIPProtocol(ipv6NextHeader).matchUdpSrc(TpPort.tpPort(udpPacket.getSourcePort())).matchUdpDst(TpPort.tpPort(udpPacket.getDestinationPort()));
}
if (matchIcmpFields && ipv6NextHeader == IPv6.PROTOCOL_ICMP6) {
ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
selectorBuilder.matchIPProtocol(ipv6NextHeader).matchIcmpv6Type(icmp6Packet.getIcmpType()).matchIcmpv6Code(icmp6Packet.getIcmpCode());
}
}
}
TrafficTreatment treatment;
if (inheritFlowTreatment) {
treatment = context.treatmentBuilder().setOutput(portNumber).build();
} else {
treatment = DefaultTrafficTreatment.builder().setOutput(portNumber).build();
}
ForwardingObjective forwardingObjective = DefaultForwardingObjective.builder().withSelector(selectorBuilder.build()).withTreatment(treatment).withPriority(flowPriority).withFlag(ForwardingObjective.Flag.VERSATILE).fromApp(appId).makeTemporary(flowTimeout).add();
flowObjectiveService.forward(context.inPacket().receivedFrom().deviceId(), forwardingObjective);
forwardPacket(macMetrics);
//
if (packetOutOfppTable) {
packetOut(context, PortNumber.TABLE, macMetrics);
} else {
packetOut(context, portNumber, macMetrics);
}
}
use of org.onlab.packet.Ethernet in project onos by opennetworkinglab.
the class DirectHostManager method handle.
private boolean handle(Ethernet eth) {
checkNotNull(eth);
// skip them.
if (!enabled || (eth.getEtherType() != Ethernet.TYPE_IPV6 && eth.getEtherType() != Ethernet.TYPE_IPV4)) {
return false;
}
// According to the type we set the destIp.
IpAddress dstIp;
if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
IPv4 ip = (IPv4) eth.getPayload();
dstIp = IpAddress.valueOf(ip.getDestinationAddress());
} else {
IPv6 ip = (IPv6) eth.getPayload();
dstIp = IpAddress.valueOf(INET6, ip.getDestinationAddress());
}
// Looking for a candidate output port.
Interface egressInterface = interfaceService.getMatchingInterface(dstIp);
if (egressInterface == null) {
log.info("No egress interface found for {}", dstIp);
return false;
}
// Looking for the destination mac.
Optional<Host> host = hostService.getHostsByIp(dstIp).stream().filter(h -> h.location().equals(egressInterface.connectPoint())).filter(h -> h.vlan().equals(egressInterface.vlan())).findAny();
// and we queue the packets waiting for a destination.
if (host.isPresent()) {
transformAndSend((IP) eth.getPayload(), eth.getEtherType(), egressInterface, host.get().mac());
} else {
hostService.startMonitoringIp(dstIp);
ipPacketCache.asMap().compute(dstIp, (ip, queue) -> {
if (queue == null) {
queue = new ConcurrentLinkedQueue<>();
}
queue.add((IP) eth.getPayload());
return queue;
});
}
return true;
}
Aggregations