use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project netvirt by opendaylight.
the class ElanPacketInHandler method onPacketReceived.
@Override
public void onPacketReceived(PacketReceived notification) {
Class<? extends PacketInReason> pktInReason = notification.getPacketInReason();
short tableId = notification.getTableId().getValue();
if (pktInReason == NoMatch.class && tableId == NwConstants.ELAN_SMAC_TABLE) {
ElanManagerCounters.unknown_smac_pktin_rcv.inc();
try {
byte[] data = notification.getPayload();
Ethernet res = new Ethernet();
res.deserialize(data, 0, data.length * NetUtils.NUM_BITS_IN_A_BYTE);
byte[] srcMac = res.getSourceMACAddress();
final String macAddress = NWUtil.toStringMacAddress(srcMac);
final BigInteger metadata = notification.getMatch().getMetadata().getMetadata();
final long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
long portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
Optional<IfIndexInterface> interfaceInfoOp = elanUtils.getInterfaceInfoByInterfaceTag(portTag);
if (!interfaceInfoOp.isPresent()) {
LOG.warn("There is no interface for given portTag {}", portTag);
return;
}
String interfaceName = interfaceInfoOp.get().getInterfaceName();
LOG.debug("Received a packet with srcMac: {} ElanTag: {} PortTag: {} InterfaceName: {}", macAddress, elanTag, portTag, interfaceName);
ElanTagName elanTagName = elanUtils.getElanInfoByElanTag(elanTag);
if (elanTagName == null) {
LOG.warn("not able to find elanTagName in elan-tag-name-map for elan tag {}", elanTag);
return;
}
ElanInterfaceMac elanInterfaceMac = elanUtils.getElanInterfaceMacByInterfaceName(interfaceName);
if (elanInterfaceMac == null) {
LOG.info("There is no ElanInterfaceForwardingEntryDS created for interface :{}", interfaceName);
return;
}
String elanName = elanTagName.getName();
PhysAddress physAddress = new PhysAddress(macAddress);
MacEntry oldMacEntry = elanUtils.getMacEntryForElanInstance(elanName, physAddress).orNull();
boolean isVlanOrFlatProviderIface = interfaceManager.isExternalInterface(interfaceName);
Optional<IpAddress> srcIpAddress = elanUtils.getSourceIpAddress(res);
MacEntry newMacEntry = null;
BigInteger timeStamp = new BigInteger(String.valueOf(System.currentTimeMillis()));
if (!srcIpAddress.isPresent()) {
newMacEntry = new MacEntryBuilder().setInterface(interfaceName).setMacAddress(physAddress).setKey(new MacEntryKey(physAddress)).setControllerLearnedForwardingEntryTimestamp(timeStamp).setIsStaticAddress(false).build();
} else {
newMacEntry = new MacEntryBuilder().setInterface(interfaceName).setMacAddress(physAddress).setIpPrefix(srcIpAddress.get()).setKey(new MacEntryKey(physAddress)).setControllerLearnedForwardingEntryTimestamp(timeStamp).setIsStaticAddress(false).build();
}
if (srcIpAddress.isPresent()) {
String prefix = srcIpAddress.get().getIpv4Address().getValue();
InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
ElanInstance elanInstance = elanInstanceCache.get(elanName).orNull();
evpnUtils.advertisePrefix(elanInstance, macAddress, prefix, interfaceName, interfaceInfo.getDpId());
}
enqueueJobForMacSpecificTasks(macAddress, elanTag, interfaceName, elanName, physAddress, oldMacEntry, newMacEntry, isVlanOrFlatProviderIface);
ElanInstance elanInstance = elanInstanceCache.get(elanName).orNull();
InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
if (interfaceInfo == null) {
LOG.trace("Interface:{} is not present under Config DS", interfaceName);
return;
}
enqueueJobForDPNSpecificTasks(macAddress, elanTag, interfaceName, physAddress, elanInstance, interfaceInfo, oldMacEntry, newMacEntry, isVlanOrFlatProviderIface);
} catch (PacketException e) {
LOG.error("Failed to decode packet: {}", notification, e);
}
}
}
use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project netvirt by opendaylight.
the class SubnetRoutePacketInHandler method onPacketReceived.
@Override
public void onPacketReceived(PacketReceived notification) {
short tableId = notification.getTableId().getValue();
LOG.trace("{} onPacketReceived: Packet punted from table {}", LOGGING_PREFIX, tableId);
byte[] data = notification.getPayload();
if (notification.getMatch() == null || notification.getMatch().getMetadata() == null) {
LOG.error("{} onPacketReceived: Received from table {} where the match or metadata are null", LOGGING_PREFIX, tableId);
return;
}
BigInteger metadata = notification.getMatch().getMetadata().getMetadata();
Ethernet res = new Ethernet();
if (tableId == NwConstants.L3_SUBNET_ROUTE_TABLE) {
LOG.trace("{} onPacketReceived: Some packet received as {}", LOGGING_PREFIX, notification);
try {
res.deserialize(data, 0, data.length * NetUtils.NUM_BITS_IN_A_BYTE);
} catch (PacketException e) {
LOG.error("{} onPacketReceived: Failed to decode Packet ", LOGGING_PREFIX, e);
VpnManagerCounters.subnet_route_packet_failed.inc();
return;
}
try {
Packet pkt = res.getPayload();
if (pkt instanceof IPv4) {
IPv4 ipv4 = (IPv4) pkt;
byte[] srcIp = Ints.toByteArray(ipv4.getSourceAddress());
byte[] dstIp = Ints.toByteArray(ipv4.getDestinationAddress());
String dstIpStr = NWUtil.toStringIpAddress(dstIp);
String srcIpStr = NWUtil.toStringIpAddress(srcIp);
// It is an ARP request on a configured VPN. So we must
// attempt to respond.
long vpnId = MetaDataUtil.getVpnIdFromMetadata(metadata);
LOG.info("{} onPacketReceived: Processing IPv4 Packet received with Source IP {} and Target IP {}" + " and vpnId {}", LOGGING_PREFIX, srcIpStr, dstIpStr, vpnId);
Optional<VpnIds> vpnIdsOptional = VpnUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, VpnUtil.getVpnIdToVpnInstanceIdentifier(vpnId));
if (!vpnIdsOptional.isPresent()) {
// Donot trigger subnetroute logic for packets from
// unknown VPNs
VpnManagerCounters.subnet_route_packet_ignored.inc();
LOG.info("{} onPacketReceived: Ignoring IPv4 packet with destination Ip {} and source Ip {}" + " as it came on unknown VPN with ID {}", LOGGING_PREFIX, dstIpStr, srcIpStr, vpnId);
return;
}
String vpnIdVpnInstanceName = vpnIdsOptional.get().getVpnInstanceName();
if (VpnUtil.getNeutronPortFromVpnPortFixedIp(dataBroker, vpnIdVpnInstanceName, dstIpStr) != null) {
VpnManagerCounters.subnet_route_packet_ignored.inc();
LOG.info("{} onPacketReceived: IPv4 Packet received with Target IP {} source IP {} vpnId {} " + "is a valid Neutron port,ignoring subnet route processing", LOGGING_PREFIX, dstIpStr, srcIp, vpnId);
return;
}
if (VpnUtil.getLearntVpnVipToPort(dataBroker, vpnIdVpnInstanceName, dstIpStr) != null) {
VpnManagerCounters.subnet_route_packet_ignored.inc();
LOG.info("{} onPacketReceived: IPv4 Packet received with Target IP {} source Ip {} vpnId {}" + " is an already discovered IPAddress, ignoring subnet route processing", LOGGING_PREFIX, dstIpStr, srcIp, vpnId);
return;
}
long elanTag = MetaDataUtil.getElanTagFromMetadata(metadata);
if (elanTag == 0L) {
VpnManagerCounters.subnet_route_packet_failed.inc();
LOG.error("{} onPacketReceived: elanTag value from metadata found to be 0, for IPv4 " + " Packet received with Target IP {} src Ip {} vpnId {}", LOGGING_PREFIX, dstIpStr, srcIp, vpnId);
return;
}
if (!vpnIdsOptional.get().isExternalVpn()) {
handleInternalVpnSubnetRoutePacket(metadata, dstIp, srcIpStr, dstIpStr, ipv4.getDestinationAddress(), vpnIdVpnInstanceName, elanTag);
return;
}
byte[] srcMac = res.getSourceMACAddress();
handleBgpVpnSubnetRoute(ipv4, srcMac, dstIp, dstIpStr, srcIpStr, elanTag);
}
} catch (UnknownHostException | InterruptedException | ExecutionException ex) {
// Failed to handle packet
VpnManagerCounters.subnet_route_packet_failed.inc();
LOG.error("{} onPacketReceived: Failed to handle subnetroute packet.", LOGGING_PREFIX, ex);
}
return;
}
// All Arp responses learning for invisble IPs is handled by
// ArpNotificationHandler
}
use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project netvirt by opendaylight.
the class NaptEventHandler method prepareAndSendPacketOut.
private void prepareAndSendPacketOut(NAPTEntryEvent naptEntryEvent, Long routerId) {
// Send Packetout - tcp or udp packets which got punted to controller.
BigInteger metadata = naptEntryEvent.getPacketReceived().getMatch().getMetadata().getMetadata();
byte[] inPayload = naptEntryEvent.getPacketReceived().getPayload();
Ethernet ethPkt = new Ethernet();
if (inPayload != null) {
try {
ethPkt.deserialize(inPayload, 0, inPayload.length * NetUtils.NUM_BITS_IN_A_BYTE);
} catch (PacketException e) {
LOG.error("prepareAndSendPacketOut : Failed to decode Packet", e);
return;
}
}
long portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
LOG.debug("prepareAndSendPacketOut : portTag from incoming packet is {}", portTag);
String interfaceName = getInterfaceNameFromTag(portTag);
LOG.debug("prepareAndSendPacketOut : interfaceName fetched from portTag is {}", interfaceName);
org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface = null;
int vlanId = 0;
iface = interfaceManager.getInterfaceInfoFromConfigDataStore(interfaceName);
if (iface == null) {
LOG.error("prepareAndSendPacketOut : Unable to read interface {} from config DataStore", interfaceName);
return;
}
List<ActionInfo> actionInfos = new ArrayList<>();
IfL2vlan ifL2vlan = iface.getAugmentation(IfL2vlan.class);
if (ifL2vlan != null && ifL2vlan.getVlanId() != null) {
vlanId = ifL2vlan.getVlanId().getValue() == null ? 0 : ifL2vlan.getVlanId().getValue();
}
InterfaceInfo infInfo = interfaceManager.getInterfaceInfoFromOperationalDataStore(interfaceName);
if (infInfo == null) {
LOG.error("prepareAndSendPacketOut : error in getting interfaceInfo from Operation DS");
return;
}
byte[] pktOut = buildNaptPacketOut(ethPkt);
if (ethPkt.getEtherType() != (short) NwConstants.ETHTYPE_802_1Q) {
// VLAN Access port
LOG.debug("prepareAndSendPacketOut : vlanId is {}", vlanId);
if (vlanId != 0) {
// Push vlan
actionInfos.add(new ActionPushVlan(0));
actionInfos.add(new ActionSetFieldVlanVid(1, vlanId));
} else {
LOG.debug("prepareAndSendPacketOut : No vlanId {}, may be untagged", vlanId);
}
} else {
// VLAN Trunk Port
LOG.debug("prepareAndSendPacketOut : This is VLAN Trunk port case - need not do VLAN tagging again");
}
if (pktOut != null) {
String routerName = NatUtil.getRouterName(dataBroker, routerId);
long tunId = NatUtil.getTunnelIdForNonNaptToNaptFlow(dataBroker, elanManager, idManager, routerId, routerName);
sendNaptPacketOut(pktOut, infInfo, actionInfos, tunId);
} else {
LOG.warn("prepareAndSendPacketOut : Unable to send Packet Out");
}
}
use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project netvirt by opendaylight.
the class NaptPacketInHandler method onPacketReceived.
@Override
// TODO Clean up the exception handling
@SuppressWarnings("checkstyle:IllegalCatch")
public void onPacketReceived(PacketReceived packetReceived) {
String internalIPAddress = "";
int portNumber = 0;
long routerId = 0L;
NAPTEntryEvent.Operation operation = NAPTEntryEvent.Operation.ADD;
NAPTEntryEvent.Protocol protocol;
Short tableId = packetReceived.getTableId().getValue();
LOG.trace("onPacketReceived : packet: {}, tableId {}", packetReceived, tableId);
if (tableId == NwConstants.OUTBOUND_NAPT_TABLE) {
LOG.debug("onPacketReceived : NAPTPacketInHandler Packet for Outbound NAPT Table");
byte[] inPayload = packetReceived.getPayload();
Ethernet ethPkt = new Ethernet();
if (inPayload != null) {
try {
ethPkt.deserialize(inPayload, 0, inPayload.length * NetUtils.NUM_BITS_IN_A_BYTE);
} catch (Exception e) {
LOG.warn("onPacketReceived: Failed to decode Packet", e);
return;
}
if (ethPkt.getPayload() instanceof IPv4) {
IPv4 ipPkt = (IPv4) ethPkt.getPayload();
byte[] ipSrc = Ints.toByteArray(ipPkt.getSourceAddress());
internalIPAddress = NWUtil.toStringIpAddress(ipSrc);
LOG.trace("onPacketReceived : Retrieved internalIPAddress {}", internalIPAddress);
if (ipPkt.getPayload() instanceof TCP) {
TCP tcpPkt = (TCP) ipPkt.getPayload();
portNumber = tcpPkt.getSourcePort();
if (portNumber < 0) {
portNumber = 32767 + portNumber + 32767 + 2;
LOG.trace("onPacketReceived : Retrieved and extracted TCP portNumber {}", portNumber);
}
protocol = NAPTEntryEvent.Protocol.TCP;
LOG.trace("onPacketReceived : Retrieved TCP portNumber {}", portNumber);
} else if (ipPkt.getPayload() instanceof UDP) {
UDP udpPkt = (UDP) ipPkt.getPayload();
portNumber = udpPkt.getSourcePort();
if (portNumber < 0) {
portNumber = 32767 + portNumber + 32767 + 2;
LOG.trace("onPacketReceived : Retrieved and extracted UDP portNumber {}", portNumber);
}
protocol = NAPTEntryEvent.Protocol.UDP;
LOG.trace("onPacketReceived : Retrieved UDP portNumber {}", portNumber);
} else {
LOG.error("onPacketReceived : Incoming Packet is neither TCP or UDP packet");
return;
}
} else {
LOG.error("onPacketReceived : Incoming Packet is not IPv4 packet");
return;
}
if (internalIPAddress != null) {
BigInteger metadata = packetReceived.getMatch().getMetadata().getMetadata();
routerId = MetaDataUtil.getNatRouterIdFromMetadata(metadata);
if (routerId <= 0) {
LOG.error("onPacketReceived : Router ID is invalid");
return;
}
String sourceIPPortKey = routerId + NatConstants.COLON_SEPARATOR + internalIPAddress + NatConstants.COLON_SEPARATOR + portNumber;
NatPacketProcessingState state = incomingPacketMap.get(sourceIPPortKey);
if (state == null) {
state = new NatPacketProcessingState(System.currentTimeMillis(), -1);
incomingPacketMap.put(sourceIPPortKey, state);
LOG.trace("onPacketReceived : Processing new SNAT({}) Packet", sourceIPPortKey);
// send to Event Queue
NAPTEntryEvent naptEntryEvent = new NAPTEntryEvent(internalIPAddress, portNumber, routerId, operation, protocol, packetReceived, false, state);
LOG.info("onPacketReceived : First Packet IN Queue Size : {}", ((ThreadPoolExecutor) firstPacketExecutorService).getQueue().size());
firstPacketExecutorService.execute(() -> naptEventHandler.handleEvent(naptEntryEvent));
} else {
LOG.trace("onPacketReceived : SNAT({}) Packet already processed.", sourceIPPortKey);
NAPTEntryEvent naptEntryEvent = new NAPTEntryEvent(internalIPAddress, portNumber, routerId, operation, protocol, packetReceived, true, state);
LOG.debug("onPacketReceived : Retry Packet IN Queue Size : {}", ((ThreadPoolExecutor) retryPacketExecutorService).getQueue().size());
long firstPacketInTime = state.getFirstPacketInTime();
retryPacketExecutorService.execute(() -> {
if (System.currentTimeMillis() - firstPacketInTime > 4000) {
LOG.error("onPacketReceived : Flow not installed even after 4sec." + "Dropping SNAT ({}) Packet", sourceIPPortKey);
removeIncomingPacketMap(sourceIPPortKey);
return;
}
naptEventHandler.handleEvent(naptEntryEvent);
});
}
} else {
LOG.error("onPacketReceived : Retrived internalIPAddress is NULL");
}
}
} else {
LOG.trace("onPacketReceived : Packet is not from the Outbound NAPT table");
}
}
use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project netvirt by opendaylight.
the class DhcpPktHandler method getDhcpPktIn.
private DHCP getDhcpPktIn(Ethernet actualEthernetPacket) {
Ethernet ethPkt = actualEthernetPacket;
if (ethPkt.getEtherType() == (short) NwConstants.ETHTYPE_802_1Q) {
ethPkt = (Ethernet) ethPkt.getPayload();
}
// Currently only IPv4 is supported
if (ethPkt.getPayload() instanceof IPv4) {
IPv4 ipPkt = (IPv4) ethPkt.getPayload();
if (ipPkt.getPayload() instanceof UDP) {
UDP udpPkt = (UDP) ipPkt.getPayload();
if (udpPkt.getSourcePort() == DhcpMConstants.DHCP_CLIENT_PORT && udpPkt.getDestinationPort() == DhcpMConstants.DHCP_SERVER_PORT) {
LOG.trace("Matched DHCP_CLIENT_PORT and DHCP_SERVER_PORT");
byte[] rawDhcpPayload = udpPkt.getRawPayload();
DHCP reply = new DHCP();
try {
reply.deserialize(rawDhcpPayload, 0, rawDhcpPayload.length);
} catch (PacketException e) {
LOG.warn("Failed to deserialize DHCP pkt");
LOG.trace("Reason for failure", e);
return null;
}
return reply;
}
}
}
return null;
}
Aggregations