use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project netvirt by opendaylight.
the class DhcpPktHandler method onPacketReceived.
// TODO: Handle this in a separate thread
@Override
public void onPacketReceived(PacketReceived packet) {
if (!config.isControllerDhcpEnabled()) {
return;
}
Class<? extends PacketInReason> pktInReason = packet.getPacketInReason();
short tableId = packet.getTableId().getValue();
if ((tableId == NwConstants.DHCP_TABLE || tableId == NwConstants.DHCP_TABLE_EXTERNAL_TUNNEL) && isPktInReasonSendtoCtrl(pktInReason)) {
byte[] inPayload = packet.getPayload();
Ethernet ethPkt = new Ethernet();
try {
ethPkt.deserialize(inPayload, 0, inPayload.length * NetUtils.NUM_BITS_IN_A_BYTE);
} catch (PacketException e) {
LOG.warn("Failed to decode DHCP Packet.", e);
LOG.trace("Received packet {}", packet);
return;
}
DHCP pktIn;
pktIn = getDhcpPktIn(ethPkt);
if (pktIn != null) {
LOG.trace("DHCPPkt received: {}", pktIn);
LOG.trace("Received Packet: {}", packet);
BigInteger metadata = packet.getMatch().getMetadata().getMetadata();
long portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
String macAddress = DHCPUtils.byteArrayToString(ethPkt.getSourceMACAddress());
BigInteger tunnelId = packet.getMatch().getTunnel() == null ? null : packet.getMatch().getTunnel().getTunnelId();
String interfaceName = getInterfaceNameFromTag(portTag);
InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfoFromOperationalDataStore(interfaceName);
if (interfaceInfo == null) {
LOG.error("Failed to get interface info for interface name {}", interfaceName);
return;
}
Port port;
if (tunnelId != null) {
port = dhcpExternalTunnelManager.readVniMacToPortCache(tunnelId, macAddress);
} else {
port = getNeutronPort(interfaceName);
}
Subnet subnet = getNeutronSubnet(port);
String serverMacAddress = interfaceInfo.getMacAddress();
String serverIp = null;
if (subnet != null) {
java.util.Optional<SubnetToDhcpPort> dhcpPortData = DhcpServiceUtils.getSubnetDhcpPortData(broker, subnet.getUuid().getValue());
/* If enable_dhcp_service flag was enabled and an ODL network DHCP Port data was made available use
* the ports Fixed IP as server IP for DHCP communication.
*/
if (dhcpPortData.isPresent()) {
serverIp = dhcpPortData.get().getPortFixedip();
serverMacAddress = dhcpPortData.get().getPortMacaddress();
} else {
// DHCP Neutron Port not found for this network
LOG.error("Neutron DHCP port is not available for the Subnet {} and port {}.", subnet.getUuid(), port.getUuid());
return;
}
}
DHCP replyPkt = handleDhcpPacket(pktIn, interfaceName, macAddress, port, subnet, serverIp);
if (replyPkt == null) {
LOG.warn("Unable to construct reply packet for interface name {}", interfaceName);
return;
}
byte[] pktOut = getDhcpPacketOut(replyPkt, ethPkt, serverMacAddress);
sendPacketOut(pktOut, interfaceInfo.getDpId(), interfaceName, tunnelId);
}
}
}
use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project genius by opendaylight.
the class AlivenessMonitor method onPacketReceived.
@Override
public void onPacketReceived(PacketReceived packetReceived) {
Class<? extends PacketInReason> pktInReason = packetReceived.getPacketInReason();
if (LOG.isTraceEnabled()) {
LOG.trace("Packet Received {}", packetReceived);
}
if (pktInReason == SendToController.class) {
Packet packetInFormatted;
byte[] data = packetReceived.getPayload();
Ethernet res = new Ethernet();
try {
packetInFormatted = res.deserialize(data, 0, data.length * NetUtils.NUM_BITS_IN_A_BYTE);
} catch (PacketException e) {
LOG.warn("Failed to decode packet: ", e);
return;
}
if (packetInFormatted == null) {
LOG.warn("Failed to deserialize Received Packet from table {}", packetReceived.getTableId().getValue());
return;
}
Packet objPayload = packetInFormatted.getPayload();
if (objPayload == null) {
LOG.trace("Unsupported packet type. Ignoring the packet...");
return;
}
if (LOG.isTraceEnabled()) {
LOG.trace("onPacketReceived packet: {}, packet class: {}", packetReceived, objPayload.getClass());
}
AlivenessProtocolHandler<Packet> livenessProtocolHandler = alivenessProtocolHandlerRegistry.getOpt(Packet.class);
if (livenessProtocolHandler == null) {
return;
}
String monitorKey = livenessProtocolHandler.handlePacketIn(packetInFormatted.getPayload(), packetReceived);
if (monitorKey != null) {
processReceivedMonitorKey(monitorKey);
} else {
LOG.debug("No monitorkey associated with received packet");
}
}
}
use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project genius by opendaylight.
the class AlivenessProtocolHandlerLLDP method startMonitoringTask.
@Override
public void startMonitoringTask(MonitoringInfo monitorInfo) {
String sourceInterface;
EndpointType source = monitorInfo.getSource().getEndpointType();
if (source instanceof Interface) {
Interface intf = (Interface) source;
sourceInterface = intf.getInterfaceName();
} else {
LOG.warn("Invalid source endpoint. Could not retrieve source interface to send LLDP Packet");
return;
}
// Get Mac Address for the source interface
org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState;
try {
interfaceState = getInterfaceFromOperDS(sourceInterface);
} catch (ReadFailedException e) {
LOG.error("getInterfaceFromOperDS failed for sourceInterface: {}", sourceInterface, e);
return;
}
Optional<byte[]> optSourceMac = getMacAddress(interfaceState);
if (!optSourceMac.isPresent()) {
LOG.error("Could not read mac address for the source interface {} from the Inventory. " + "LLDP packet cannot be send.", sourceInterface);
return;
}
byte[] sourceMac = optSourceMac.get();
String lowerLayerIf = interfaceState.getLowerLayerIf().get(0);
NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
long nodeId = Long.parseLong(getDpnFromNodeConnectorId(nodeConnectorId));
long portNum = Long.parseLong(getPortNoFromNodeConnectorId(nodeConnectorId));
Ethernet ethenetLLDPPacket = makeLLDPPacket(Long.toString(nodeId), portNum, sourceMac, sourceInterface);
try {
List<ActionInfo> actions = getInterfaceActions(interfaceState, portNum);
if (actions.isEmpty()) {
LOG.error("No interface actions to send packet out over interface {}", sourceInterface);
return;
}
TransmitPacketInput transmitPacketInput = MDSALUtil.getPacketOut(actions, ethenetLLDPPacket.serialize(), nodeId, MDSALUtil.getNodeConnRef(BigInteger.valueOf(nodeId), "0xfffffffd"));
addErrorLogging(packetProcessingService.transmitPacket(transmitPacketInput), LOG, "transmitPacket() failed: {}", transmitPacketInput);
} catch (InterruptedException | ExecutionException | PacketException e) {
LOG.error("Error while sending LLDP Packet", e);
}
}
use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project genius by opendaylight.
the class ArpPacketUtil method createEthernetPacket.
private static Ethernet createEthernetPacket(byte[] sourceMAC, byte[] targetMAC, ARP arp) throws PacketException {
Ethernet ethernet = new Ethernet();
ethernet.setSourceMACAddress(sourceMAC);
ethernet.setDestinationMACAddress(targetMAC);
ethernet.setEtherType(EtherTypes.ARP.shortValue());
ethernet.setPayload(arp);
return ethernet;
}
use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project genius by opendaylight.
the class ArpUtilImpl method onPacketReceived.
@Override
public void onPacketReceived(PacketReceived packetReceived) {
Class<? extends PacketInReason> pktInReason = packetReceived.getPacketInReason();
LOG.trace("Packet Received {}", packetReceived);
if (pktInReason == SendToController.class) {
try {
int tableId = packetReceived.getTableId().getValue();
byte[] data = packetReceived.getPayload();
Ethernet ethernet = new Ethernet();
ethernet.deserialize(data, 0, data.length * NetUtils.NUM_BITS_IN_A_BYTE);
if (ethernet.getEtherType() != ArpConstants.ETH_TYPE_ARP) {
return;
}
Packet pkt = ethernet.getPayload();
ARP arp = (ARP) pkt;
InetAddress srcInetAddr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
InetAddress dstInetAddr = InetAddress.getByAddress(arp.getTargetProtocolAddress());
byte[] srcMac = ethernet.getSourceMACAddress();
byte[] dstMac = ethernet.getDestinationMACAddress();
Metadata metadata = packetReceived.getMatch().getMetadata();
String interfaceName = getInterfaceName(metadata);
checkAndFireMacChangedNotification(interfaceName, srcInetAddr, srcMac);
macsDB.put(interfaceName + "-" + srcInetAddr.getHostAddress(), NWUtil.toStringMacAddress(srcMac));
if (arp.getOpCode() == ArpConstants.ARP_REQUEST_OP) {
fireArpReqRecvdNotification(interfaceName, srcInetAddr, srcMac, dstInetAddr, tableId, metadata.getMetadata());
} else {
fireArpRespRecvdNotification(interfaceName, srcInetAddr, srcMac, tableId, metadata.getMetadata(), dstInetAddr, dstMac);
}
if (macAddrs.get(srcInetAddr.getHostAddress()) != null) {
threadPool.execute(new MacResponderTask(arp));
}
} catch (PacketException | UnknownHostException | InterruptedException | ExecutionException e) {
LOG.trace("Failed to decode packet", e);
}
}
}
Aggregations