use of org.opendaylight.openflowplugin.libraries.liblldp.PacketException in project genius by opendaylight.
the class ArpUtilImpl method sendArpRequest.
@Override
public Future<RpcResult<Void>> sendArpRequest(SendArpRequestInput arpReqInput) {
LOG.trace("rpc sendArpRequest invoked for ip {}", arpReqInput.getIpaddress());
BigInteger dpnId;
byte[] payload;
String interfaceName = null;
byte[] srcIpBytes;
byte[] dstIpBytes;
byte[] srcMac;
RpcResultBuilder<Void> failureBuilder = RpcResultBuilder.failed();
RpcResultBuilder<Void> successBuilder = RpcResultBuilder.success();
try {
dstIpBytes = getIpAddressBytes(arpReqInput.getIpaddress());
} catch (UnknownHostException e) {
LOG.error("Cannot get IP address", e);
failureBuilder.withError(ErrorType.APPLICATION, ArpConstants.UNKNOWN_IP_ADDRESS_SUPPLIED);
return Futures.immediateFuture(failureBuilder.build());
}
int localErrorCount = 0;
for (InterfaceAddress interfaceAddress : arpReqInput.getInterfaceAddress()) {
try {
interfaceName = interfaceAddress.getInterface();
srcIpBytes = getIpAddressBytes(interfaceAddress.getIpAddress());
GetPortFromInterfaceOutput portResult = getPortFromInterface(interfaceName);
checkNotNull(portResult);
dpnId = portResult.getDpid();
Long portid = portResult.getPortno();
checkArgument(null != dpnId && !BigInteger.ZERO.equals(dpnId), ArpConstants.DPN_NOT_FOUND_ERROR, interfaceName);
NodeConnectorRef ref = MDSALUtil.getNodeConnRef(dpnId, portid.toString());
checkNotNull(ref, ArpConstants.NODE_CONNECTOR_NOT_FOUND_ERROR, interfaceName);
LOG.trace("sendArpRequest received dpnId {} out interface {}", dpnId, interfaceName);
if (interfaceAddress.getMacaddress() == null) {
srcMac = MDSALUtil.getMacAddressForNodeConnector(dataBroker, (InstanceIdentifier<NodeConnector>) ref.getValue());
} else {
String macAddr = interfaceAddress.getMacaddress().getValue();
srcMac = HexEncode.bytesFromHexString(macAddr);
}
checkNotNull(srcMac, ArpConstants.FAILED_TO_GET_SRC_MAC_FOR_INTERFACE, interfaceName, ref.getValue());
checkNotNull(srcIpBytes, ArpConstants.FAILED_TO_GET_SRC_IP_FOR_INTERFACE, interfaceName);
payload = ArpPacketUtil.getPayload(ArpConstants.ARP_REQUEST_OP, srcMac, srcIpBytes, ArpPacketUtil.ETHERNET_BROADCAST_DESTINATION, dstIpBytes);
List<Action> actions = getEgressAction(interfaceName);
sendPacketOutWithActions(dpnId, payload, ref, actions);
LOG.trace("sent arp request for {}", arpReqInput.getIpaddress());
} catch (UnknownHostException | PacketException | InterruptedException | ExecutionException | ReadFailedException e) {
LOG.trace("failed to send arp req for {} on interface {}", arpReqInput.getIpaddress(), interfaceName);
failureBuilder.withError(ErrorType.APPLICATION, ArpConstants.FAILED_TO_SEND_ARP_REQ_FOR_INTERFACE + interfaceName, e);
successBuilder.withError(ErrorType.APPLICATION, ArpConstants.FAILED_TO_SEND_ARP_REQ_FOR_INTERFACE + interfaceName, e);
localErrorCount++;
}
}
if (localErrorCount == arpReqInput.getInterfaceAddress().size()) {
// All the requests failed
return Futures.immediateFuture(failureBuilder.build());
}
return Futures.immediateFuture(successBuilder.build());
}
use of org.opendaylight.openflowplugin.libraries.liblldp.PacketException in project netvirt by opendaylight.
the class DhcpPktHandler method getDhcpPacketOut.
// "Consider returning a zero length array rather than null" - the eventual user of the returned byte[] likely
// expects null and it's unclear what the behavior would be if empty array was returned.
@SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
protected byte[] getDhcpPacketOut(DHCP reply, Ethernet etherPkt, String phyAddrees) {
if (reply == null) {
/*
* DECLINE or RELEASE don't result in reply packet
*/
return null;
}
LOG.trace("Sending DHCP Pkt {}", reply);
InetAddress serverIp = reply.getOptionInetAddr(DHCPConstants.OPT_SERVER_IDENTIFIER);
// create UDP pkt
UDP udpPkt = new UDP();
byte[] rawPkt;
try {
rawPkt = reply.serialize();
} catch (PacketException e) {
LOG.warn("Failed to serialize packet", e);
return null;
}
udpPkt.setRawPayload(rawPkt);
udpPkt.setDestinationPort(DhcpMConstants.DHCP_CLIENT_PORT);
udpPkt.setSourcePort(DhcpMConstants.DHCP_SERVER_PORT);
udpPkt.setLength((short) (rawPkt.length + 8));
// Create IP Pkt
try {
rawPkt = udpPkt.serialize();
} catch (PacketException e) {
LOG.warn("Failed to serialize packet", e);
return null;
}
short checkSum = 0;
boolean computeUdpChecksum = true;
if (computeUdpChecksum) {
checkSum = computeChecksum(rawPkt, serverIp.getAddress(), NetUtils.intToByteArray4(DhcpMConstants.BCAST_IP));
}
udpPkt.setChecksum(checkSum);
IPv4 ip4Reply = new IPv4();
ip4Reply.setPayload(udpPkt);
ip4Reply.setProtocol(IPProtocols.UDP.byteValue());
ip4Reply.setSourceAddress(serverIp);
ip4Reply.setDestinationAddress(DhcpMConstants.BCAST_IP);
ip4Reply.setTotalLength((short) (rawPkt.length + 20));
ip4Reply.setTtl((byte) 32);
// create Ethernet Frame
Ethernet ether = new Ethernet();
if (etherPkt.getEtherType() == (short) NwConstants.ETHTYPE_802_1Q) {
IEEE8021Q vlanPacket = (IEEE8021Q) etherPkt.getPayload();
IEEE8021Q vlanTagged = new IEEE8021Q();
vlanTagged.setCFI(vlanPacket.getCfi());
vlanTagged.setPriority(vlanPacket.getPriority());
vlanTagged.setVlanId(vlanPacket.getVlanId());
vlanTagged.setPayload(ip4Reply);
vlanTagged.setEtherType(EtherTypes.IPv4.shortValue());
ether.setPayload(vlanTagged);
ether.setEtherType((short) NwConstants.ETHTYPE_802_1Q);
} else {
ether.setEtherType(EtherTypes.IPv4.shortValue());
ether.setPayload(ip4Reply);
}
ether.setSourceMACAddress(getServerMacAddress(phyAddrees));
ether.setDestinationMACAddress(etherPkt.getSourceMACAddress());
try {
rawPkt = ether.serialize();
} catch (PacketException e) {
LOG.warn("Failed to serialize ethernet reply", e);
return null;
}
return rawPkt;
}
use of org.opendaylight.openflowplugin.libraries.liblldp.PacketException in project netvirt by opendaylight.
the class ArpUtils method createEthernetPacket.
private static byte[] createEthernetPacket(byte[] sourceMAC, byte[] targetMAC, byte[] arp) {
Ethernet ethernet = new Ethernet();
byte[] rawEthPkt = null;
try {
ethernet.setSourceMACAddress(sourceMAC);
ethernet.setDestinationMACAddress(targetMAC);
ethernet.setEtherType(EtherTypes.ARP.shortValue());
ethernet.setRawPayload(arp);
rawEthPkt = ethernet.serialize();
} catch (PacketException ex) {
LOG.error("VPNUtil: Serialized Ethernet packet with sourceMacAddress {} targetMacAddress {} exception ", sourceMAC, targetMAC, ex);
}
return rawEthPkt;
}
use of org.opendaylight.openflowplugin.libraries.liblldp.PacketException in project netvirt by opendaylight.
the class ArpUtils method createARPPacket.
private static byte[] createARPPacket(short opCode, byte[] senderMacAddress, byte[] senderIP, byte[] targetMacAddress, byte[] targetIP) {
ARP arp = new ARP();
byte[] rawArpPkt = null;
try {
arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
arp.setProtocolType(EtherTypes.IPv4.shortValue());
arp.setHardwareAddressLength((byte) 6);
arp.setProtocolAddressLength((byte) 4);
arp.setOpCode(opCode);
arp.setSenderHardwareAddress(senderMacAddress);
arp.setSenderProtocolAddress(senderIP);
arp.setTargetHardwareAddress(targetMacAddress);
arp.setTargetProtocolAddress(targetIP);
rawArpPkt = arp.serialize();
} catch (PacketException ex) {
LOG.error("VPNUtil: Serialized ARP packet with senderIp {} targetIP {} exception ", senderIP, targetIP, ex);
}
return rawArpPkt;
}
use of org.opendaylight.openflowplugin.libraries.liblldp.PacketException in project netvirt by opendaylight.
the class DHCP method deserialize.
// public void setPadding(byte[] pad) {
// this.pad = pad;
// }
/**
* This method deserializes the data bits obtained from the wire into the
* respective header and payload which are of type Packet.
*
* @param data byte[] data from wire to deserialize
* @param bitOffset int bit position where packet header starts in data
* array
* @param size int size of packet in bits
* @return Packet
* @throws PacketException the packet deserialization failed
*
* <p>Note: Copied from org.opendaylight.controller.sal.packet.Packet</p>
*/
@Override
// We can’t do much about PacketException (yet; see https://git.opendaylight.org/gerrit/65837)
@SuppressWarnings("checkstyle:AvoidHidingCauseException")
public Packet deserialize(byte[] data, int bitOffset, int size) throws PacketException {
// Deserialize the header fields one by one
int startOffset = 0;
int numBits = 0;
for (Entry<String, Pair<Integer, Integer>> pairs : hdrFieldCoordMap.entrySet()) {
String hdrField = pairs.getKey();
startOffset = bitOffset + this.getfieldOffset(hdrField);
if (hdrField.equals(OPTIONS)) {
numBits = (size - DHCP_NOOPT_HDR_SIZE) * 8;
} else {
numBits = this.getfieldnumBits(hdrField);
}
byte[] hdrFieldBytes = null;
try {
hdrFieldBytes = BitBufferHelper.getBits(data, startOffset, numBits);
} catch (BufferException e) {
throw new PacketException(e.getMessage());
}
/*
* Store the raw read value, checks the payload type and set the
* payloadClass accordingly
*/
this.setHeaderField(hdrField, hdrFieldBytes);
if (LOG.isTraceEnabled()) {
LOG.trace("{}: {}: {} (offset {} bitsize {})", this.getClass().getSimpleName(), hdrField, HexEncode.bytesToHexString(hdrFieldBytes), startOffset, numBits);
}
}
// Deserialize the payload now
int payloadStart = startOffset + numBits;
int payloadSize = data.length * NetUtils.NUM_BITS_IN_A_BYTE - payloadStart;
if (payloadClass != null) {
try {
payload = payloadClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException("Error parsing payload for Ethernet packet", e);
}
payload.deserialize(data, payloadStart, payloadSize);
payload.setParent(this);
} else {
/*
* The payload class was not set, it means no class for parsing
* this payload is present. Let's store the raw payload if any.
*/
int start = payloadStart / NetUtils.NUM_BITS_IN_A_BYTE;
int stop = start + payloadSize / NetUtils.NUM_BITS_IN_A_BYTE;
rawPayload = Arrays.copyOfRange(data, start, stop);
}
// Take care of computation that can be done only after deserialization
postDeserializeCustomOperation(data, payloadStart - getHeaderSize());
return this;
}
Aggregations