Search in sources :

Example 11 with Ethernet

use of org.opendaylight.genius.mdsalutil.packet.Ethernet 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;
}
Also used : UDP(org.opendaylight.genius.mdsalutil.packet.UDP) IEEE8021Q(org.opendaylight.genius.mdsalutil.packet.IEEE8021Q) IPv4(org.opendaylight.genius.mdsalutil.packet.IPv4) Ethernet(org.opendaylight.genius.mdsalutil.packet.Ethernet) InetAddress(java.net.InetAddress) PacketException(org.opendaylight.openflowplugin.libraries.liblldp.PacketException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 12 with Ethernet

use of org.opendaylight.genius.mdsalutil.packet.Ethernet 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;
}
Also used : Ethernet(org.opendaylight.genius.mdsalutil.packet.Ethernet) PacketException(org.opendaylight.openflowplugin.libraries.liblldp.PacketException)

Example 13 with Ethernet

use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project genius by opendaylight.

the class ArpPacketUtil method getPayload.

static byte[] getPayload(short opCode, byte[] senderMacAddress, byte[] senderIP, byte[] targetMacAddress, byte[] targetIP) throws PacketException {
    ARP arp = createARPPacket(opCode, senderMacAddress, senderIP, targetMacAddress, targetIP);
    Ethernet ethernet = createEthernetPacket(senderMacAddress, targetMacAddress, arp);
    return ethernet.serialize();
}
Also used : Ethernet(org.opendaylight.genius.mdsalutil.packet.Ethernet) ARP(org.opendaylight.genius.mdsalutil.packet.ARP)

Example 14 with Ethernet

use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project genius by opendaylight.

the class AlivenessProtocolHandlerLLDP method makeLLDPPacket.

public Ethernet makeLLDPPacket(String nodeId, long portNum, byte[] srcMac, String sourceInterface) {
    // Create LLDP TTL TLV
    LLDPTLV lldpTlvTTL = buildLLDTLV(LLDPTLV.TLVType.TTL, new byte[] { (byte) 0, (byte) 120 });
    LLDPTLV lldpTlvChassisId = buildLLDTLV(LLDPTLV.TLVType.ChassisID, LLDPTLV.createChassisIDTLVValue(colonize(StringUtils.leftPad(Long.toHexString(MDSALUtil.getDpnIdFromNodeName(nodeId).longValue()), 16, "0"))));
    LLDPTLV lldpTlvSystemName = buildLLDTLV(TLVType.SystemName, LLDPTLV.createSystemNameTLVValue(nodeId));
    LLDPTLV lldpTlvPortId = buildLLDTLV(TLVType.PortID, LLDPTLV.createPortIDTLVValue(Long.toHexString(portNum)));
    String customValue = sourceInterface + "#" + getPacketId();
    LOG.debug("Sending LLDP packet, custome value {}", customValue);
    LLDPTLV lldpTlvCustom = buildLLDTLV(TLVType.Custom, customValue.getBytes(LLDPTLV_CHARSET));
    @SuppressWarnings("AbbreviationAsWordInName") List<LLDPTLV> lstLLDPTLVCustom = new ArrayList<>();
    lstLLDPTLVCustom.add(lldpTlvCustom);
    LLDP lldpDiscoveryPacket = new LLDP();
    lldpDiscoveryPacket.setChassisId(lldpTlvChassisId).setPortId(lldpTlvPortId).setTtl(lldpTlvTTL).setSystemNameId(lldpTlvSystemName).setOptionalTLVList(lstLLDPTLVCustom);
    byte[] destMac = LLDP.LLDP_MULTICAST_MAC;
    Ethernet ethernetPacket = new Ethernet();
    ethernetPacket.setSourceMACAddress(srcMac).setDestinationMACAddress(destMac).setEtherType(EtherTypes.LLDP.shortValue()).setPayload(lldpDiscoveryPacket);
    return ethernetPacket;
}
Also used : Ethernet(org.opendaylight.genius.mdsalutil.packet.Ethernet) ArrayList(java.util.ArrayList) LLDPTLV(org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV) LLDP(org.opendaylight.openflowplugin.libraries.liblldp.LLDP)

Aggregations

Ethernet (org.opendaylight.genius.mdsalutil.packet.Ethernet)14 PacketException (org.opendaylight.openflowplugin.libraries.liblldp.PacketException)10 BigInteger (java.math.BigInteger)5 IPv4 (org.opendaylight.genius.mdsalutil.packet.IPv4)4 ExecutionException (java.util.concurrent.ExecutionException)3 InterfaceInfo (org.opendaylight.genius.interfacemanager.globals.InterfaceInfo)3 UDP (org.opendaylight.genius.mdsalutil.packet.UDP)3 Packet (org.opendaylight.openflowplugin.libraries.liblldp.Packet)3 InetAddress (java.net.InetAddress)2 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 ActionInfo (org.opendaylight.genius.mdsalutil.ActionInfo)2 ARP (org.opendaylight.genius.mdsalutil.packet.ARP)2 DHCP (org.opendaylight.netvirt.dhcpservice.api.DHCP)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Collections (java.util.Collections)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 ReadFailedException (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException)1 ActionPushVlan (org.opendaylight.genius.mdsalutil.actions.ActionPushVlan)1 ActionSetFieldVlanVid (org.opendaylight.genius.mdsalutil.actions.ActionSetFieldVlanVid)1