use of org.onlab.packet.IPv4.PROTOCOL_UDP 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.IPv4.PROTOCOL_UDP in project onos by opennetworkinglab.
the class OpenstackRoutingSnatHandler method packetOut.
private void packetOut(Ethernet ethPacketIn, DeviceId srcDevice, int patPort, IpAddress externalIp, ExternalPeerRouter externalPeerRouter) {
IPv4 iPacket = (IPv4) ethPacketIn.getPayload();
switch(iPacket.getProtocol()) {
case IPv4.PROTOCOL_TCP:
iPacket.setPayload(buildPacketOutTcp(iPacket, patPort));
break;
case IPv4.PROTOCOL_UDP:
iPacket.setPayload(buildPacketOutUdp(iPacket, patPort));
break;
default:
log.trace("Temporally, this method can process UDP and TCP protocol.");
return;
}
iPacket.setSourceAddress(externalIp.toString());
iPacket.resetChecksum();
iPacket.setParent(ethPacketIn);
ethPacketIn.setSourceMACAddress(DEFAULT_GATEWAY_MAC);
ethPacketIn.setDestinationMACAddress(externalPeerRouter.macAddress());
ethPacketIn.setPayload(iPacket);
if (!externalPeerRouter.vlanId().equals(VlanId.NONE)) {
ethPacketIn.setVlanID(externalPeerRouter.vlanId().toShort());
}
ethPacketIn.resetChecksum();
OpenstackNode srcNode = osNodeService.node(srcDevice);
if (srcNode == null) {
final String error = String.format("Cannot find openstack node for %s", srcDevice);
throw new IllegalStateException(error);
}
TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
packetService.emit(new DefaultOutboundPacket(srcDevice, tBuilder.setOutput(srcNode.uplinkPortNum()).build(), ByteBuffer.wrap(ethPacketIn.serialize())));
}
use of org.onlab.packet.IPv4.PROTOCOL_UDP in project onos by opennetworkinglab.
the class OpenstackRoutingSnatHandler method setStatelessSnatUpstreamRules.
private void setStatelessSnatUpstreamRules(String segmentId, Type networkType, IpAddress externalIp, ExternalPeerRouter externalPeerRouter, TpPort patPort, InboundPacket packetIn) {
IPv4 iPacket = (IPv4) packetIn.parsed().getPayload();
TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).matchIPProtocol(iPacket.getProtocol()).matchIPSrc(IpPrefix.valueOf(iPacket.getSourceAddress(), VM_PREFIX)).matchIPDst(IpPrefix.valueOf(iPacket.getDestinationAddress(), VM_PREFIX));
TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
switch(networkType) {
case VXLAN:
case GRE:
case GENEVE:
sBuilder.matchTunnelId(Long.parseLong(segmentId));
break;
case VLAN:
sBuilder.matchVlanId(VlanId.vlanId(segmentId));
tBuilder.popVlan();
break;
default:
final String error = String.format("%s %s", ERR_UNSUPPORTED_NET_TYPE, networkType.toString());
throw new IllegalStateException(error);
}
switch(iPacket.getProtocol()) {
case IPv4.PROTOCOL_TCP:
TCP tcpPacket = (TCP) iPacket.getPayload();
sBuilder.matchTcpSrc(TpPort.tpPort(tcpPacket.getSourcePort())).matchTcpDst(TpPort.tpPort(tcpPacket.getDestinationPort()));
tBuilder.setTcpSrc(patPort).setEthDst(externalPeerRouter.macAddress());
break;
case IPv4.PROTOCOL_UDP:
UDP udpPacket = (UDP) iPacket.getPayload();
sBuilder.matchUdpSrc(TpPort.tpPort(udpPacket.getSourcePort())).matchUdpDst(TpPort.tpPort(udpPacket.getDestinationPort()));
tBuilder.setUdpSrc(patPort).setEthDst(externalPeerRouter.macAddress());
break;
default:
log.debug("Unsupported IPv4 protocol {}");
break;
}
if (!externalPeerRouter.vlanId().equals(VlanId.NONE)) {
tBuilder.pushVlan().setVlanId(externalPeerRouter.vlanId());
}
tBuilder.setIpSrc(externalIp);
osNodeService.completeNodes(GATEWAY).forEach(gNode -> {
TrafficTreatment.Builder tmpBuilder = DefaultTrafficTreatment.builder(tBuilder.build());
tmpBuilder.setOutput(gNode.uplinkPortNum());
osFlowRuleService.setRule(appId, gNode.intgBridge(), sBuilder.build(), tmpBuilder.build(), PRIORITY_SNAT_RULE, GW_COMMON_TABLE, true);
});
}
use of org.onlab.packet.IPv4.PROTOCOL_UDP in project onos by opennetworkinglab.
the class DefaultLispEncapsulatedControlTest method setup.
@Before
public void setup() {
// Creates ecm1
EcmBuilder builder1 = new DefaultEcmBuilder();
IP innerIp1 = new IPv4().setSourceAddress(ECM1_SRC_IP).setDestinationAddress(ECM1_DST_IP).setProtocol(IPv4.PROTOCOL_UDP).setVersion((byte) (4 & 0xf));
UDP innerUdp1 = new UDP().setSourcePort(1).setDestinationPort(2);
RegisterBuilder msgBuilder = new DefaultRegisterBuilder();
List<LispMapRecord> records1 = ImmutableList.of(getMapRecord(), getMapRecord());
LispMapRegister innerMsg1 = msgBuilder.withIsProxyMapReply(true).withIsWantMapNotify(false).withKeyId((short) 1).withAuthKey(AUTH_KEY).withNonce(1L).withMapRecords(records1).build();
ecm1 = builder1.isSecurity(false).innerIpHeader(innerIp1).innerUdpHeader(innerUdp1).innerLispMessage(innerMsg1).build();
// Creates sameAsEcm1
EcmBuilder builder2 = new DefaultEcmBuilder();
IP innerIp2 = new IPv4().setSourceAddress(ECM1_SRC_IP).setDestinationAddress(ECM1_DST_IP).setProtocol(IPv4.PROTOCOL_UDP);
UDP innerUdp2 = new UDP().setSourcePort(1).setDestinationPort(2);
RegisterBuilder msgBuilder2 = new DefaultRegisterBuilder();
List<LispMapRecord> records2 = ImmutableList.of(getMapRecord(), getMapRecord());
LispMapRegister innerMsg2 = msgBuilder2.withIsProxyMapReply(true).withIsWantMapNotify(false).withKeyId((short) 1).withAuthKey(AUTH_KEY).withNonce(1L).withMapRecords(records2).build();
sameAsEcm1 = builder2.isSecurity(false).innerIpHeader(innerIp2).innerUdpHeader(innerUdp2).innerLispMessage(innerMsg2).build();
// Creates ecm2
EcmBuilder builder3 = new DefaultEcmBuilder();
IP innerIp3 = new IPv4().setSourceAddress(ECM2_SRC_IP).setDestinationAddress(ECM2_DST_IP).setProtocol(IPv4.PROTOCOL_UDP);
UDP innerUdp3 = new UDP().setSourcePort(10).setDestinationPort(20);
RegisterBuilder msgBuilder3 = new DefaultRegisterBuilder();
List<LispMapRecord> records3 = ImmutableList.of(getMapRecord(), getMapRecord());
LispMapRegister innerMsg3 = msgBuilder3.withIsProxyMapReply(true).withIsWantMapNotify(false).withKeyId((short) 2).withAuthKey(AUTH_KEY).withNonce(1L).withMapRecords(records3).build();
ecm2 = builder3.isSecurity(false).innerIpHeader(innerIp3).innerUdpHeader(innerUdp3).innerLispMessage(innerMsg3).build();
}
use of org.onlab.packet.IPv4.PROTOCOL_UDP in project onos by opennetworkinglab.
the class OpenstackRoutingSnatHandler method setStatelessSnatDownstreamRules.
private void setStatelessSnatDownstreamRules(InstancePort srcInstPort, String segmentId, Type networkType, IpAddress externalIp, ExternalPeerRouter externalPeerRouter, TpPort patPort, InboundPacket packetIn) {
IPv4 iPacket = (IPv4) packetIn.parsed().getPayload();
IpAddress internalIp = IpAddress.valueOf(iPacket.getSourceAddress());
TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).matchIPProtocol(iPacket.getProtocol()).matchIPDst(IpPrefix.valueOf(externalIp.getIp4Address(), VM_PREFIX)).matchIPSrc(IpPrefix.valueOf(iPacket.getDestinationAddress(), VM_PREFIX));
TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder().setEthDst(packetIn.parsed().getSourceMAC()).setIpDst(internalIp);
if (!externalPeerRouter.vlanId().equals(VlanId.NONE)) {
sBuilder.matchVlanId(externalPeerRouter.vlanId());
tBuilder.popVlan();
}
switch(networkType) {
case VXLAN:
case GRE:
case GENEVE:
tBuilder.setTunnelId(Long.parseLong(segmentId));
break;
case VLAN:
tBuilder.pushVlan().setVlanId(VlanId.vlanId(segmentId));
break;
default:
final String error = String.format("%s %s", ERR_UNSUPPORTED_NET_TYPE, networkType.toString());
throw new IllegalStateException(error);
}
switch(iPacket.getProtocol()) {
case IPv4.PROTOCOL_TCP:
TCP tcpPacket = (TCP) iPacket.getPayload();
sBuilder.matchTcpSrc(TpPort.tpPort(tcpPacket.getDestinationPort())).matchTcpDst(patPort);
tBuilder.setTcpDst(TpPort.tpPort(tcpPacket.getSourcePort()));
break;
case IPv4.PROTOCOL_UDP:
UDP udpPacket = (UDP) iPacket.getPayload();
sBuilder.matchUdpSrc(TpPort.tpPort(udpPacket.getDestinationPort())).matchUdpDst(patPort);
tBuilder.setUdpDst(TpPort.tpPort(udpPacket.getSourcePort()));
break;
default:
break;
}
OpenstackNode srcNode = osNodeService.node(srcInstPort.deviceId());
osNodeService.completeNodes(GATEWAY).forEach(gNode -> {
TrafficTreatment treatment = getDownstreamTreatment(networkType, tBuilder, gNode, srcNode);
osFlowRuleService.setRule(appId, gNode.intgBridge(), sBuilder.build(), treatment, PRIORITY_SNAT_RULE, GW_COMMON_TABLE, true);
});
}
Aggregations