From 64cbd6197d6c71e223ac399c252be508b63ade73 Mon Sep 17 00:00:00 2001 From: ycl <931415151@qq.com> Date: Tue, 10 Sep 2024 16:21:17 +0800 Subject: [PATCH 01/14] add infinibandlayer rxe for pcapp --- Packet++/CMakeLists.txt | 1 + Packet++/header/InfiniBandLayer.h | 101 +++++++++ Packet++/header/ProtocolType.h | 2 + Packet++/src/InfiniBandLayer.cpp | 40 ++++ Packet++/src/UdpLayer.cpp | 3 + Tests/Packet++Test/CMakeLists.txt | 1 + Tests/Packet++Test/TestDefinition.h | 3 + Tests/Packet++Test/Tests/InfiniBandTests.cpp | 208 +++++++++++++++++++ Tests/Packet++Test/main.cpp | 2 + 9 files changed, 361 insertions(+) create mode 100644 Packet++/header/InfiniBandLayer.h create mode 100644 Packet++/src/InfiniBandLayer.cpp create mode 100644 Tests/Packet++Test/Tests/InfiniBandTests.cpp diff --git a/Packet++/CMakeLists.txt b/Packet++/CMakeLists.txt index cc7a8268d2..379a54d43b 100644 --- a/Packet++/CMakeLists.txt +++ b/Packet++/CMakeLists.txt @@ -60,6 +60,7 @@ add_library( src/TLVData.cpp src/TpktLayer.cpp src/UdpLayer.cpp + src/InfiniBandLayer.cpp src/VlanLayer.cpp src/VrrpLayer.cpp src/VxlanLayer.cpp diff --git a/Packet++/header/InfiniBandLayer.h b/Packet++/header/InfiniBandLayer.h new file mode 100644 index 0000000000..9d0eb809c0 --- /dev/null +++ b/Packet++/header/InfiniBandLayer.h @@ -0,0 +1,101 @@ +#pragma once + +#include "Layer.h" + +/// @file + +/** + * \namespace pcpp + * \brief The main namespace for the PcapPlusPlus lib + */ +namespace pcpp +{ + /* + * IBA header types and methods + * + * Some of these are for reference and completeness only since + * rxe does not currently support RD transport + * most of this could be moved into IB core. ib_pack.h has + * part of this but is incomplete + * + * Header specific routines to insert/extract values to/from headers + * the routines that are named __hhh_(set_)fff() take a pointer to a + * hhh header and get(set) the fff field. The routines named + * hhh_(set_)fff take a packet info struct and find the + * header and field based on the opcode in the packet. + * Conversion to/from network byte order from cpu order is also done. + */ + + #define RXE_ICRC_SIZE (4) + #define RXE_MAX_HDR_LENGTH (80) + + /****************************************************************************** + * Base Transport Header + ******************************************************************************/ +#pragma pack(push, 1) + struct rxe_bth + { + uint8_t opcode; + uint8_t flags; + uint16_t pkey; + uint32_t qpn; + uint32_t apsn; + }; +#pragma pack(pop) + + #define BTH_TVER (0) + #define BTH_DEF_PKEY (0xffff) + + #define BTH_SE_MASK (0x80) + #define BTH_MIG_MASK (0x40) + #define BTH_PAD_MASK (0x30) + #define BTH_TVER_MASK (0x0f) + #define BTH_FECN_MASK (0x80000000) + #define BTH_BECN_MASK (0x40000000) + #define BTH_RESV6A_MASK (0x3f000000) + #define BTH_QPN_MASK (0x00ffffff) + #define BTH_ACK_MASK (0x80000000) + #define BTH_RESV7_MASK (0x7f000000) + #define BTH_PSN_MASK (0x00ffffff) + + class InfiniBandLayer : public Layer + { + public: + + InfiniBandLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) + : Layer(data, dataLen, prevLayer, packet, IB) + {} + + rxe_bth* getBthHeader() const + { + return (rxe_bth*)m_Data; + } + + uint8_t getOpcode() const; + + void parseNextLayer(); + + size_t getHeaderLen() const + { + return sizeof(rxe_bth); + } + + /** + * Calculate @ref udphdr#headerChecksum field + */ + void computeCalculateFields(); + + std::string toString() const; + + OsiModelLayer getOsiModelLayer() const + { + return OsiModelTransportLayer; + } + + static inline bool isInfiniBandPort(uint16_t port) + { + return (port == 4791); + } + }; + +} // namespace pcpp diff --git a/Packet++/header/ProtocolType.h b/Packet++/header/ProtocolType.h index ada6950c5e..15332cc71e 100644 --- a/Packet++/header/ProtocolType.h +++ b/Packet++/header/ProtocolType.h @@ -347,6 +347,8 @@ namespace pcpp */ const ProtocolType LDAP = 55; + const ProtocolType IB = 56; + /** * An enum representing OSI model layers */ diff --git a/Packet++/src/InfiniBandLayer.cpp b/Packet++/src/InfiniBandLayer.cpp new file mode 100644 index 0000000000..626e28ea58 --- /dev/null +++ b/Packet++/src/InfiniBandLayer.cpp @@ -0,0 +1,40 @@ +#include "InfiniBandLayer.h" +#include "PayloadLayer.h" +#include "PacketUtils.h" +#include "Logger.h" +#include +#include "EndianPortable.h" + +namespace pcpp +{ + + void InfiniBandLayer::parseNextLayer() + { + if (m_DataLen <= sizeof(rxe_bth)) + return; + + uint8_t* bthData = m_Data + sizeof(rxe_bth); + size_t bthDataLen = m_DataLen - sizeof(rxe_bth); + + m_NextLayer = new PayloadLayer(bthData, bthDataLen, this, m_Packet); + } + + void InfiniBandLayer::computeCalculateFields() + { + + } + + std::string InfiniBandLayer::toString() const + { + std::ostringstream opCodeStream; + opCodeStream << getOpcode(); + + return "InfiniBand Layer, Opcode: " + opCodeStream.str(); + } + + + uint8_t InfiniBandLayer::getOpcode() const + { + return getBthHeader()->opcode; + } +} \ No newline at end of file diff --git a/Packet++/src/UdpLayer.cpp b/Packet++/src/UdpLayer.cpp index 2e17b9cddc..8bd3745d89 100644 --- a/Packet++/src/UdpLayer.cpp +++ b/Packet++/src/UdpLayer.cpp @@ -5,6 +5,7 @@ #include "PayloadLayer.h" #include "IPv4Layer.h" #include "IPv6Layer.h" +#include "InfiniBandLayer.h" #include "DnsLayer.h" #include "DhcpLayer.h" #include "DhcpV6Layer.h" @@ -134,6 +135,8 @@ namespace pcpp m_NextLayer = SomeIpLayer::parseSomeIpLayer(udpData, udpDataLen, this, m_Packet); else if ((WakeOnLanLayer::isWakeOnLanPort(portDst) && WakeOnLanLayer::isDataValid(udpData, udpDataLen))) m_NextLayer = new WakeOnLanLayer(udpData, udpDataLen, this, m_Packet); + else if (InfiniBandLayer::isInfiniBandPort(portDst)) + m_NextLayer = new InfiniBandLayer(udpData, udpDataLen, this, m_Packet); else m_NextLayer = new PayloadLayer(udpData, udpDataLen, this, m_Packet); } diff --git a/Tests/Packet++Test/CMakeLists.txt b/Tests/Packet++Test/CMakeLists.txt index 0e6bbc75bc..6b3c60821d 100644 --- a/Tests/Packet++Test/CMakeLists.txt +++ b/Tests/Packet++Test/CMakeLists.txt @@ -39,6 +39,7 @@ add_executable( Tests/TcpTests.cpp Tests/TelnetTests.cpp Tests/TpktTests.cpp + Tests/InfiniBandTests.cpp Tests/VlanMplsTests.cpp Tests/VrrpTest.cpp Tests/WakeOnLanTests.cpp diff --git a/Tests/Packet++Test/TestDefinition.h b/Tests/Packet++Test/TestDefinition.h index b628c34f56..f2e4048304 100644 --- a/Tests/Packet++Test/TestDefinition.h +++ b/Tests/Packet++Test/TestDefinition.h @@ -262,3 +262,6 @@ PTF_TEST_CASE(Asn1EncodingTest); // Implemented in LdapTests.cpp PTF_TEST_CASE(LdapParsingTest); PTF_TEST_CASE(LdapCreationTest); + +// Implemented in InfiniBandTests.cpp +PTF_TEST_CASE(InfiniBandPacketParsing); \ No newline at end of file diff --git a/Tests/Packet++Test/Tests/InfiniBandTests.cpp b/Tests/Packet++Test/Tests/InfiniBandTests.cpp new file mode 100644 index 0000000000..5520cb1f51 --- /dev/null +++ b/Tests/Packet++Test/Tests/InfiniBandTests.cpp @@ -0,0 +1,208 @@ +#include "../TestDefinition.h" +#include "../Utils/TestUtils.h" +#include +#include "EndianPortable.h" +#include "Logger.h" +#include "MacAddress.h" +#include "Packet.h" +#include "EthLayer.h" +#include "VlanLayer.h" +#include "IPv4Layer.h" +#include "IPv6Layer.h" +#include "UdpLayer.h" +#include "InfiniBandLayer.h" +#include "PayloadLayer.h" +#include "SystemUtils.h" + +std::string getProtocolTypeAsString(pcpp::ProtocolType protocolType) +{ + switch (protocolType) + { + case pcpp::Ethernet: + return "Ethernet"; + case pcpp::VLAN: + return "Vlan"; + case pcpp::IPv4: + return "IPv4"; + case pcpp::UDP: + return "UDP"; + case pcpp::TCP: + return "TCP"; + case pcpp::IB: + return "IB"; + case pcpp::HTTPRequest: + case pcpp::HTTPResponse: + return "HTTP"; + case pcpp::GenericPayload: + return "Payload"; + default: + return "Unknown"; + } +} + +PTF_TEST_CASE(InfiniBandPacketParsing) +{ + timeval time; + gettimeofday(&time, nullptr); + + /* + uint8_t buffer1[] = { + 0x30, 0x46, 0x9a, 0x23, 0xfb, 0xfa, 0x6c, 0xf0, + 0x49, 0xb2, 0xde, 0x6e, 0x08, 0x00, 0x45, 0x00, + 0x00, 0x3c, 0x1a, 0x57, 0x00, 0x00, 0x80, 0x01, + 0x14, 0x65, 0x0a, 0x00, 0x00, 0x04, 0x01, 0x01, + 0x01, 0x01, 0x08, 0x00, 0x4d, 0x5a, 0x00, 0x01, + 0x00, 0x01, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, + 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, + 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, + }; + */ + + + // RRoce + uint8_t buffer1[] = { + 0x00, 0x0c, 0x29, 0xae, 0x1c, 0xa4, 0x00, 0x0c, + 0x29, 0x89, 0xa2, 0xe5, 0x08, 0x00, 0x45, 0x00, + 0x00, 0x3c, 0xbe, 0x10, 0x40, 0x00, 0x40, 0x11, + 0x8a, 0x4b, 0xc0, 0xa8, 0x38, 0x81, 0xc0, 0xa8, + 0x38, 0x83, 0xdf, 0x94, 0x12, 0xb7, 0x00, 0x28, + 0x00, 0x00, 0x0c, 0x00, 0xff, 0xff, 0x00, 0x00, + 0x00, 0x11, 0x80, 0x54, 0xcb, 0x63, 0x00, 0x00, + 0x7f, 0xee, 0x26, 0x0f, 0xb0, 0x00, 0x00, 0x00, + 0x02, 0xb8, 0x00, 0x01, 0x00, 0x00, 0x08, 0xc6, + 0x15, 0x4a, + }; + + /* + // eth + vlan + ipv4 + ICMP + uint8_t buffer1[] = { + 0x00, 0x1b, 0xd4, 0x1b, 0xa4, 0xd8, 0x00, 0x13, + 0xc3, 0xdf, 0xae, 0x18, 0x81, 0x00, 0x00, 0x76, + 0x81, 0x00, 0x00, 0x0a, 0x08, 0x00, 0x45, 0x00, + 0x00, 0x64, 0x00, 0x0f, 0x00, 0x00, 0xff, 0x01, + 0x92, 0x9b, 0x0a, 0x76, 0x0a, 0x01, 0x0a, 0x76, + 0x0a, 0x02, 0x08, 0x00, 0xce, 0xb7, 0x00, 0x03, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, + 0xaf, 0x70, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd + }; + */ + + int bufferLength1 = sizeof(buffer1); + + uint8_t* result = new uint8_t[bufferLength1]; + memcpy(result, buffer1, bufferLength1); + + pcpp::RawPacket rawPacket1(static_cast(result), bufferLength1, time, true); + + // parse the raw packet into a parsed packet + pcpp::Packet parsedPacket(&rawPacket1); + + // first let's go over the layers one by one and find out its type, its total length, its header length and its payload length + for (auto* curLayer = parsedPacket.getFirstLayer(); curLayer != nullptr; curLayer = curLayer->getNextLayer()) + { + std::cout + << "Layer type: " << getProtocolTypeAsString(curLayer->getProtocol()) << "; " // get layer type + << "Total data: " << curLayer->getDataLen() << " [bytes]; " // get total length of the layer + << "Layer data: " << curLayer->getHeaderLen() << " [bytes]; " // get the header length of the layer + << "Layer payload: " << curLayer->getLayerPayloadSize() << " [bytes]" // get the payload length of the layer (equals total length minus header length) + << std::endl; + + switch (curLayer->getProtocol()) + { + case pcpp::Ethernet: + { + // now let's get the Ethernet layer + auto* ethernetLayer = parsedPacket.getLayerOfType(); + if (ethernetLayer == nullptr) + { + std::cerr << "Something went wrong, couldn't find Ethernet layer" << std::endl; + } + + // print the source and dest MAC addresses and the Ether type + std::cout + << "Source MAC address: " << ethernetLayer->getSourceMac() << std::endl + << "Destination MAC address: " << ethernetLayer->getDestMac() << std::endl + << "Ether type = 0x" << std::hex << pcpp::netToHost16(ethernetLayer->getEthHeader()->etherType) << std::endl; + break; + } + case pcpp::VLAN: + { + // now let's get the Vlan layer + auto* vlanLayer = parsedPacket.getLayerOfType(); + if (vlanLayer == nullptr) + { + std::cerr << "Something went wrong, couldn't find Ethernet layer" << std::endl; + } + + std::cout + << vlanLayer->toString() << std::endl + << "vlan type = 0x" << std::hex << pcpp::netToHost16(vlanLayer->getVlanHeader()->etherType) << std::endl; + break; + } + case pcpp::IPv4: + { + // let's get the IPv4 layer + auto* ipLayer = parsedPacket.getLayerOfType(); + if (ipLayer == nullptr) + { + std::cerr << "Something went wrong, couldn't find IPv4 layer" << std::endl; + } + + // print source and dest IP addresses, IP ID and TTL + std::cout + << "Source IP address: " << ipLayer->getSrcIPAddress() << std::endl + << "Destination IP address: " << ipLayer->getDstIPAddress() << std::endl + << "IP ID: 0x" << std::hex << pcpp::netToHost16(ipLayer->getIPv4Header()->ipId) << std::endl + << "TTL: " << std::dec << (int)ipLayer->getIPv4Header()->timeToLive << std::endl; + break; + } + case pcpp::UDP: + { + // let's get the UDP layer + auto* udpLayer = parsedPacket.getLayerOfType(); + if (udpLayer == nullptr) + { + std::cerr << "Something went wrong, couldn't find UDP layer" << std::endl; + } + // print source and dest port + std::cout + << "Source port: " << udpLayer->getSrcPort() << std::endl + << "Destination port: " << udpLayer->getDstPort() << std::endl; + break; + } + case pcpp::IB: + { + // let's get the IB layer + auto* ibLayer = parsedPacket.getLayerOfType(); + if (ibLayer == nullptr) + { + std::cerr << "Something went wrong, couldn't find IB layer" << std::endl; + } + // print opcode + std::cout + << "Opcode: " << std::dec << (int)ibLayer->getOpcode() << std::endl; + break; + } + case pcpp::GenericPayload: + { + break; + } + default: + { + std::cerr << "Something went wrong, couldn't find this layer" << std::endl; + break; + } + } + } + +} // InfiniBandPacketParsing \ No newline at end of file diff --git a/Tests/Packet++Test/main.cpp b/Tests/Packet++Test/main.cpp index d235f69f41..668ad63f7f 100644 --- a/Tests/Packet++Test/main.cpp +++ b/Tests/Packet++Test/main.cpp @@ -333,5 +333,7 @@ int main(int argc, char* argv[]) PTF_RUN_TEST(LdapParsingTest, "ldap"); PTF_RUN_TEST(LdapCreationTest, "ldap"); + PTF_RUN_TEST(InfiniBandPacketParsing, "ib"); + PTF_END_RUNNING_TESTS; } From 9f6016fbafb9b25374b45c64c2cfc21f0d942002 Mon Sep 17 00:00:00 2001 From: ycl <931415151@qq.com> Date: Wed, 11 Sep 2024 01:10:06 +0800 Subject: [PATCH 02/14] add bth definiction --- Packet++/header/InfiniBandLayer.h | 28 +++ Packet++/src/InfiniBandLayer.cpp | 169 +++++++++++++++++++ Tests/Packet++Test/Tests/InfiniBandTests.cpp | 13 +- 3 files changed, 209 insertions(+), 1 deletion(-) diff --git a/Packet++/header/InfiniBandLayer.h b/Packet++/header/InfiniBandLayer.h index 9d0eb809c0..f81bce13ef 100644 --- a/Packet++/header/InfiniBandLayer.h +++ b/Packet++/header/InfiniBandLayer.h @@ -66,12 +66,40 @@ namespace pcpp : Layer(data, dataLen, prevLayer, packet, IB) {} + InfiniBandLayer( uint8_t opcode, int se, + int mig, int pad, uint16_t pkey, uint32_t qpn, int ack_req, + uint32_t psn); + rxe_bth* getBthHeader() const { return (rxe_bth*)m_Data; } uint8_t getOpcode() const; + void setOpcode(uint8_t opcode) const; + uint8_t getSe() const; + void setSe(int se) const; + uint8_t getMig() const; + void setMig(uint8_t mig) const; + uint8_t getPad() const; + void setPad(uint8_t pad) const; + uint8_t getTver() const; + void setTver(uint8_t tver) const; + uint16_t getPkey() const; + void setPkey(uint16_t pkey) const; + uint32_t getQpn() const; + void setQpn(uint32_t qpn) const; + int getFecn() const; + void setfecn(int fecn) const; + int getBecn() const; + void setbecn(int becn) const; + uint8_t getResv6a() const; + void setResv6a() const; + int getAck() const; + void setAck(int ack) const; + void setResv7() const; + uint32_t getPsn() const; + void setPsn(uint32_t psn) const; void parseNextLayer(); diff --git a/Packet++/src/InfiniBandLayer.cpp b/Packet++/src/InfiniBandLayer.cpp index 626e28ea58..2a6bd4321e 100644 --- a/Packet++/src/InfiniBandLayer.cpp +++ b/Packet++/src/InfiniBandLayer.cpp @@ -8,6 +8,31 @@ namespace pcpp { + InfiniBandLayer::InfiniBandLayer( uint8_t opcode, int se, + int mig, int pad, uint16_t pkey, uint32_t qpn, int ack_req, + uint32_t psn) + { + const size_t headerLen = sizeof(rxe_bth); + m_DataLen = headerLen; + m_Data = new uint8_t[headerLen]; + memset(m_Data, 0, headerLen); + rxe_bth* bthHdr = (rxe_bth*)m_Data; + + bthHdr->opcode = opcode; + bthHdr->flags = (pad << 4) & BTH_PAD_MASK; + if (se) + bthHdr->flags |= BTH_SE_MASK; + if (mig) + bthHdr->flags |= BTH_MIG_MASK; + bthHdr->pkey = htobe16(pkey); + bthHdr->qpn = htobe32(qpn & BTH_QPN_MASK); + psn &= BTH_PSN_MASK; + if (ack_req) + psn |= BTH_ACK_MASK; + bthHdr->apsn = htobe32(psn); + m_Protocol = IB; + } + void InfiniBandLayer::parseNextLayer() { if (m_DataLen <= sizeof(rxe_bth)) @@ -37,4 +62,148 @@ namespace pcpp { return getBthHeader()->opcode; } + + void InfiniBandLayer::setOpcode(uint8_t opcode) const + { + getBthHeader()->opcode = opcode; + } + + uint8_t InfiniBandLayer::getSe() const + { + return 0 != (BTH_SE_MASK & getBthHeader()->flags); + } + + void InfiniBandLayer::setSe(int se) const + { + if (se) + getBthHeader()->flags |= BTH_SE_MASK; + else + getBthHeader()->flags &= ~BTH_SE_MASK; + } + + uint8_t InfiniBandLayer::getMig() const + { + return 0 != (BTH_MIG_MASK & getBthHeader()->flags); + } + + void InfiniBandLayer::setMig(uint8_t mig) const + { + if (mig) + getBthHeader()->flags |= BTH_MIG_MASK; + else + getBthHeader()->flags &= ~BTH_MIG_MASK; + } + + uint8_t InfiniBandLayer::getPad() const + { + return (BTH_PAD_MASK & getBthHeader()->flags) >> 4; + } + + void InfiniBandLayer::setPad(uint8_t pad) const + { + getBthHeader()->flags = (BTH_PAD_MASK & (pad << 4)) | + (~BTH_PAD_MASK & getBthHeader()->flags); + } + + uint8_t InfiniBandLayer::getTver() const + { + return BTH_TVER_MASK & getBthHeader()->flags; + } + + void InfiniBandLayer::setTver(uint8_t tver) const + { + getBthHeader()->flags = (BTH_TVER_MASK & tver) | + (~BTH_TVER_MASK & getBthHeader()->flags); + } + + uint16_t InfiniBandLayer::getPkey() const + { + return be16toh(getBthHeader()->pkey); + } + + void InfiniBandLayer::setPkey(uint16_t pkey) const + { + getBthHeader()->pkey = htobe16(pkey); + } + + uint32_t InfiniBandLayer::getQpn() const + { + return BTH_QPN_MASK & be32toh(getBthHeader()->qpn); + } + + void InfiniBandLayer::setQpn(uint32_t qpn) const + { + + uint32_t resvqpn = be32toh(getBthHeader()->qpn); + + getBthHeader()->qpn = htobe32((BTH_QPN_MASK & qpn) | + (~BTH_QPN_MASK & resvqpn)); + } + + int InfiniBandLayer::getFecn() const + { + return 0 != (htobe32(BTH_FECN_MASK) & getBthHeader()->qpn); + } + + void InfiniBandLayer::setfecn(int fecn) const + { + if (fecn) + getBthHeader()->qpn |= htobe32(BTH_FECN_MASK); + else + getBthHeader()->qpn &= ~htobe32(BTH_FECN_MASK); + } + + int InfiniBandLayer::getBecn() const + { + return 0 != (htobe32(BTH_BECN_MASK) & getBthHeader()->qpn); + } + + void InfiniBandLayer::setbecn(int becn) const + { + if (becn) + getBthHeader()->qpn |= htobe32(BTH_BECN_MASK); + else + getBthHeader()->qpn &= ~htobe32(BTH_BECN_MASK); + } + + uint8_t InfiniBandLayer::getResv6a() const + { + return (BTH_RESV6A_MASK & be32toh(getBthHeader()->qpn)) >> 24; + } + + void InfiniBandLayer::setResv6a() const + { + getBthHeader()->qpn = htobe32(~BTH_RESV6A_MASK); + } + + int InfiniBandLayer::getAck() const + { + return 0 != (htobe32(BTH_ACK_MASK) & getBthHeader()->apsn); + } + + void InfiniBandLayer::setAck(int ack) const + { + if (ack) + getBthHeader()->apsn |= htobe32(BTH_ACK_MASK); + else + getBthHeader()->apsn &= ~htobe32(BTH_ACK_MASK); + } + + void InfiniBandLayer::setResv7() const + { + getBthHeader()->apsn &= ~htobe32(BTH_RESV7_MASK); + } + + uint32_t InfiniBandLayer::getPsn() const + { + return BTH_PSN_MASK & be32toh(getBthHeader()->apsn); + } + + void InfiniBandLayer::setPsn(uint32_t psn) const + { + uint32_t apsn = be32toh(getBthHeader()->apsn); + + getBthHeader()->apsn = htobe32((BTH_PSN_MASK & psn) | + (~BTH_PSN_MASK & apsn)); + } } \ No newline at end of file diff --git a/Tests/Packet++Test/Tests/InfiniBandTests.cpp b/Tests/Packet++Test/Tests/InfiniBandTests.cpp index 5520cb1f51..c45dc26b5b 100644 --- a/Tests/Packet++Test/Tests/InfiniBandTests.cpp +++ b/Tests/Packet++Test/Tests/InfiniBandTests.cpp @@ -190,7 +190,18 @@ PTF_TEST_CASE(InfiniBandPacketParsing) } // print opcode std::cout - << "Opcode: " << std::dec << (int)ibLayer->getOpcode() << std::endl; + << "Opcode: " << std::dec << (int)ibLayer->getOpcode() << std::endl + << "Se: " << (int)ibLayer->getSe() << std::endl + << "Mig: " << (int)ibLayer->getMig() << std::endl + << "Pad: " << (int)ibLayer->getPad() << std::endl + << "Tver: " << (int)ibLayer->getTver() << std::endl + << "Pkey: " << (int)ibLayer->getPkey() << std::endl + << "Qpn: " << (int)ibLayer->getQpn() << std::endl + << "Fecn: " << (int)ibLayer->getFecn() << std::endl + << "Becn: " << (int)ibLayer->getBecn() << std::endl + << "Resv6a: " << (int)ibLayer->getResv6a() << std::endl + << "Ack: " << (int)ibLayer->getAck() << std::endl + << "Psn: " << (int)ibLayer->getPsn() << std::endl; break; } case pcpp::GenericPayload: From 3a7dfafb02e172382065dcfd0d029be96a7a58f7 Mon Sep 17 00:00:00 2001 From: ycl <931415151@qq.com> Date: Fri, 4 Oct 2024 15:49:00 +0800 Subject: [PATCH 03/14] fix C-cast and add override to code --- Packet++/header/InfiniBandLayer.h | 12 ++++++------ Packet++/src/InfiniBandLayer.cpp | 2 +- Tests/Packet++Test/TestDefinition.h | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Packet++/header/InfiniBandLayer.h b/Packet++/header/InfiniBandLayer.h index f81bce13ef..239974a625 100644 --- a/Packet++/header/InfiniBandLayer.h +++ b/Packet++/header/InfiniBandLayer.h @@ -72,7 +72,7 @@ namespace pcpp rxe_bth* getBthHeader() const { - return (rxe_bth*)m_Data; + return reinterpret_cast(m_Data); } uint8_t getOpcode() const; @@ -101,9 +101,9 @@ namespace pcpp uint32_t getPsn() const; void setPsn(uint32_t psn) const; - void parseNextLayer(); + void parseNextLayer() override; - size_t getHeaderLen() const + size_t getHeaderLen() const override { return sizeof(rxe_bth); } @@ -111,11 +111,11 @@ namespace pcpp /** * Calculate @ref udphdr#headerChecksum field */ - void computeCalculateFields(); + void computeCalculateFields() override; - std::string toString() const; + std::string toString() const override; - OsiModelLayer getOsiModelLayer() const + OsiModelLayer getOsiModelLayer() const override { return OsiModelTransportLayer; } diff --git a/Packet++/src/InfiniBandLayer.cpp b/Packet++/src/InfiniBandLayer.cpp index 2a6bd4321e..f0941ec91d 100644 --- a/Packet++/src/InfiniBandLayer.cpp +++ b/Packet++/src/InfiniBandLayer.cpp @@ -16,7 +16,7 @@ namespace pcpp m_DataLen = headerLen; m_Data = new uint8_t[headerLen]; memset(m_Data, 0, headerLen); - rxe_bth* bthHdr = (rxe_bth*)m_Data; + rxe_bth* bthHdr = reinterpret_cast(m_Data); bthHdr->opcode = opcode; bthHdr->flags = (pad << 4) & BTH_PAD_MASK; diff --git a/Tests/Packet++Test/TestDefinition.h b/Tests/Packet++Test/TestDefinition.h index 81a7e4b1b8..b89365b47d 100644 --- a/Tests/Packet++Test/TestDefinition.h +++ b/Tests/Packet++Test/TestDefinition.h @@ -274,4 +274,4 @@ PTF_TEST_CASE(WireGuardCreationTest); PTF_TEST_CASE(WireGuardEditTest); // Implemented in InfiniBandTests.cpp -PTF_TEST_CASE(InfiniBandPacketParsing); \ No newline at end of file +PTF_TEST_CASE(InfiniBandPacketParsing); From 05333c886ed9d76143d797845f9e7e059c62b87c Mon Sep 17 00:00:00 2001 From: ycl <931415151@qq.com> Date: Fri, 4 Oct 2024 22:07:06 +0800 Subject: [PATCH 04/14] fix pre-commit errors --- Packet++/header/InfiniBandLayer.h | 194 +++++----- Packet++/header/ProtocolType.h | 5 +- Packet++/src/InfiniBandLayer.cpp | 365 +++++++++---------- Tests/Packet++Test/TestDefinition.h | 1 - Tests/Packet++Test/Tests/InfiniBandTests.cpp | 169 ++++----- Tests/Packet++Test/main.cpp | 1 - 6 files changed, 366 insertions(+), 369 deletions(-) diff --git a/Packet++/header/InfiniBandLayer.h b/Packet++/header/InfiniBandLayer.h index 239974a625..f5a175acf5 100644 --- a/Packet++/header/InfiniBandLayer.h +++ b/Packet++/header/InfiniBandLayer.h @@ -10,105 +10,105 @@ */ namespace pcpp { - /* - * IBA header types and methods - * - * Some of these are for reference and completeness only since - * rxe does not currently support RD transport - * most of this could be moved into IB core. ib_pack.h has - * part of this but is incomplete - * - * Header specific routines to insert/extract values to/from headers - * the routines that are named __hhh_(set_)fff() take a pointer to a - * hhh header and get(set) the fff field. The routines named - * hhh_(set_)fff take a packet info struct and find the - * header and field based on the opcode in the packet. - * Conversion to/from network byte order from cpu order is also done. - */ - - #define RXE_ICRC_SIZE (4) - #define RXE_MAX_HDR_LENGTH (80) - - /****************************************************************************** - * Base Transport Header - ******************************************************************************/ + /* + * IBA header types and methods + * + * Some of these are for reference and completeness only since + * rxe does not currently support RD transport + * most of this could be moved into IB core. ib_pack.h has + * part of this but is incomplete + * + * Header specific routines to insert/extract values to/from headers + * the routines that are named __hhh_(set_)fff() take a pointer to a + * hhh header and get(set) the fff field. The routines named + * hhh_(set_)fff take a packet info struct and find the + * header and field based on the opcode in the packet. + * Conversion to/from network byte order from cpu order is also done. + */ + + #define RXE_ICRC_SIZE (4) + #define RXE_MAX_HDR_LENGTH (80) + + /****************************************************************************** + * Base Transport Header + ******************************************************************************/ #pragma pack(push, 1) - struct rxe_bth - { - uint8_t opcode; - uint8_t flags; - uint16_t pkey; - uint32_t qpn; - uint32_t apsn; - }; + struct rxe_bth + { + uint8_t opcode; + uint8_t flags; + uint16_t pkey; + uint32_t qpn; + uint32_t apsn; + }; #pragma pack(pop) - #define BTH_TVER (0) - #define BTH_DEF_PKEY (0xffff) - - #define BTH_SE_MASK (0x80) - #define BTH_MIG_MASK (0x40) - #define BTH_PAD_MASK (0x30) - #define BTH_TVER_MASK (0x0f) - #define BTH_FECN_MASK (0x80000000) - #define BTH_BECN_MASK (0x40000000) - #define BTH_RESV6A_MASK (0x3f000000) - #define BTH_QPN_MASK (0x00ffffff) - #define BTH_ACK_MASK (0x80000000) - #define BTH_RESV7_MASK (0x7f000000) - #define BTH_PSN_MASK (0x00ffffff) - - class InfiniBandLayer : public Layer + #define BTH_TVER (0) + #define BTH_DEF_PKEY (0xffff) + + #define BTH_SE_MASK (0x80) + #define BTH_MIG_MASK (0x40) + #define BTH_PAD_MASK (0x30) + #define BTH_TVER_MASK (0x0f) + #define BTH_FECN_MASK (0x80000000) + #define BTH_BECN_MASK (0x40000000) + #define BTH_RESV6A_MASK (0x3f000000) + #define BTH_QPN_MASK (0x00ffffff) + #define BTH_ACK_MASK (0x80000000) + #define BTH_RESV7_MASK (0x7f000000) + #define BTH_PSN_MASK (0x00ffffff) + + class InfiniBandLayer : public Layer { - public: + public: - InfiniBandLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) - : Layer(data, dataLen, prevLayer, packet, IB) + InfiniBandLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) + : Layer(data, dataLen, prevLayer, packet, IB) {} - InfiniBandLayer( uint8_t opcode, int se, - int mig, int pad, uint16_t pkey, uint32_t qpn, int ack_req, - uint32_t psn); - - rxe_bth* getBthHeader() const - { - return reinterpret_cast(m_Data); - } - - uint8_t getOpcode() const; - void setOpcode(uint8_t opcode) const; - uint8_t getSe() const; - void setSe(int se) const; - uint8_t getMig() const; - void setMig(uint8_t mig) const; - uint8_t getPad() const; - void setPad(uint8_t pad) const; - uint8_t getTver() const; - void setTver(uint8_t tver) const; - uint16_t getPkey() const; - void setPkey(uint16_t pkey) const; - uint32_t getQpn() const; - void setQpn(uint32_t qpn) const; - int getFecn() const; - void setfecn(int fecn) const; - int getBecn() const; - void setbecn(int becn) const; - uint8_t getResv6a() const; - void setResv6a() const; - int getAck() const; - void setAck(int ack) const; - void setResv7() const; - uint32_t getPsn() const; - void setPsn(uint32_t psn) const; - - void parseNextLayer() override; - - size_t getHeaderLen() const override - { - return sizeof(rxe_bth); - } - - /** + InfiniBandLayer( uint8_t opcode, int se, + int mig, int pad, uint16_t pkey, uint32_t qpn, int ack_req, + uint32_t psn); + + rxe_bth* getBthHeader() const + { + return reinterpret_cast(m_Data); + } + + uint8_t getOpcode() const; + void setOpcode(uint8_t opcode) const; + uint8_t getSe() const; + void setSe(int se) const; + uint8_t getMig() const; + void setMig(uint8_t mig) const; + uint8_t getPad() const; + void setPad(uint8_t pad) const; + uint8_t getTver() const; + void setTver(uint8_t tver) const; + uint16_t getPkey() const; + void setPkey(uint16_t pkey) const; + uint32_t getQpn() const; + void setQpn(uint32_t qpn) const; + int getFecn() const; + void setfecn(int fecn) const; + int getBecn() const; + void setbecn(int becn) const; + uint8_t getResv6a() const; + void setResv6a() const; + int getAck() const; + void setAck(int ack) const; + void setResv7() const; + uint32_t getPsn() const; + void setPsn(uint32_t psn) const; + + void parseNextLayer() override; + + size_t getHeaderLen() const override + { + return sizeof(rxe_bth); + } + + /** * Calculate @ref udphdr#headerChecksum field */ void computeCalculateFields() override; @@ -120,10 +120,10 @@ namespace pcpp return OsiModelTransportLayer; } - static inline bool isInfiniBandPort(uint16_t port) - { - return (port == 4791); - } - }; + static inline bool isInfiniBandPort(uint16_t port) + { + return (port == 4791); + } + }; } // namespace pcpp diff --git a/Packet++/header/ProtocolType.h b/Packet++/header/ProtocolType.h index 53ace8ddcb..6c4e085910 100644 --- a/Packet++/header/ProtocolType.h +++ b/Packet++/header/ProtocolType.h @@ -347,16 +347,15 @@ namespace pcpp */ const ProtocolType LDAP = 55; - /* * WireGuard protocol */ const ProtocolType WireGuard = 56; - /* + /* * infiniband protocol */ - const ProtocolType IB = 56; + const ProtocolType IB = 57; /** * An enum representing OSI model layers diff --git a/Packet++/src/InfiniBandLayer.cpp b/Packet++/src/InfiniBandLayer.cpp index f0941ec91d..fdb95a7217 100644 --- a/Packet++/src/InfiniBandLayer.cpp +++ b/Packet++/src/InfiniBandLayer.cpp @@ -8,202 +8,201 @@ namespace pcpp { - InfiniBandLayer::InfiniBandLayer( uint8_t opcode, int se, - int mig, int pad, uint16_t pkey, uint32_t qpn, int ack_req, - uint32_t psn) - { - const size_t headerLen = sizeof(rxe_bth); + InfiniBandLayer::InfiniBandLayer( uint8_t opcode, int se, int mig, int pad, uint16_t pkey, uint32_t qpn, int ack_req, + uint32_t psn) + { + const size_t headerLen = sizeof(rxe_bth); m_DataLen = headerLen; m_Data = new uint8_t[headerLen]; memset(m_Data, 0, headerLen); rxe_bth* bthHdr = reinterpret_cast(m_Data); - bthHdr->opcode = opcode; - bthHdr->flags = (pad << 4) & BTH_PAD_MASK; - if (se) - bthHdr->flags |= BTH_SE_MASK; - if (mig) - bthHdr->flags |= BTH_MIG_MASK; - bthHdr->pkey = htobe16(pkey); - bthHdr->qpn = htobe32(qpn & BTH_QPN_MASK); - psn &= BTH_PSN_MASK; - if (ack_req) - psn |= BTH_ACK_MASK; - bthHdr->apsn = htobe32(psn); - m_Protocol = IB; - } - - void InfiniBandLayer::parseNextLayer() - { - if (m_DataLen <= sizeof(rxe_bth)) + bthHdr->opcode = opcode; + bthHdr->flags = (pad << 4) & BTH_PAD_MASK; + if (se) + bthHdr->flags |= BTH_SE_MASK; + if (mig) + bthHdr->flags |= BTH_MIG_MASK; + bthHdr->pkey = htobe16(pkey); + bthHdr->qpn = htobe32(qpn & BTH_QPN_MASK); + psn &= BTH_PSN_MASK; + if (ack_req) + psn |= BTH_ACK_MASK; + bthHdr->apsn = htobe32(psn); + m_Protocol = IB; + } + + void InfiniBandLayer::parseNextLayer() + { + if (m_DataLen <= sizeof(rxe_bth)) return; uint8_t* bthData = m_Data + sizeof(rxe_bth); size_t bthDataLen = m_DataLen - sizeof(rxe_bth); - m_NextLayer = new PayloadLayer(bthData, bthDataLen, this, m_Packet); - } + m_NextLayer = new PayloadLayer(bthData, bthDataLen, this, m_Packet); + } - void InfiniBandLayer::computeCalculateFields() + void InfiniBandLayer::computeCalculateFields() { - + } std::string InfiniBandLayer::toString() const { - std::ostringstream opCodeStream; - opCodeStream << getOpcode(); - - return "InfiniBand Layer, Opcode: " + opCodeStream.str(); - } - - - uint8_t InfiniBandLayer::getOpcode() const - { - return getBthHeader()->opcode; - } - - void InfiniBandLayer::setOpcode(uint8_t opcode) const - { - getBthHeader()->opcode = opcode; - } - - uint8_t InfiniBandLayer::getSe() const - { - return 0 != (BTH_SE_MASK & getBthHeader()->flags); - } - - void InfiniBandLayer::setSe(int se) const - { - if (se) - getBthHeader()->flags |= BTH_SE_MASK; - else - getBthHeader()->flags &= ~BTH_SE_MASK; - } - - uint8_t InfiniBandLayer::getMig() const - { - return 0 != (BTH_MIG_MASK & getBthHeader()->flags); - } - - void InfiniBandLayer::setMig(uint8_t mig) const - { - if (mig) - getBthHeader()->flags |= BTH_MIG_MASK; - else - getBthHeader()->flags &= ~BTH_MIG_MASK; - } - - uint8_t InfiniBandLayer::getPad() const - { - return (BTH_PAD_MASK & getBthHeader()->flags) >> 4; - } - - void InfiniBandLayer::setPad(uint8_t pad) const - { - getBthHeader()->flags = (BTH_PAD_MASK & (pad << 4)) | - (~BTH_PAD_MASK & getBthHeader()->flags); - } - - uint8_t InfiniBandLayer::getTver() const - { - return BTH_TVER_MASK & getBthHeader()->flags; - } - - void InfiniBandLayer::setTver(uint8_t tver) const - { - getBthHeader()->flags = (BTH_TVER_MASK & tver) | - (~BTH_TVER_MASK & getBthHeader()->flags); - } - - uint16_t InfiniBandLayer::getPkey() const - { - return be16toh(getBthHeader()->pkey); - } - - void InfiniBandLayer::setPkey(uint16_t pkey) const - { - getBthHeader()->pkey = htobe16(pkey); - } - - uint32_t InfiniBandLayer::getQpn() const - { - return BTH_QPN_MASK & be32toh(getBthHeader()->qpn); - } - - void InfiniBandLayer::setQpn(uint32_t qpn) const - { - - uint32_t resvqpn = be32toh(getBthHeader()->qpn); - - getBthHeader()->qpn = htobe32((BTH_QPN_MASK & qpn) | - (~BTH_QPN_MASK & resvqpn)); - } - - int InfiniBandLayer::getFecn() const - { - return 0 != (htobe32(BTH_FECN_MASK) & getBthHeader()->qpn); - } - - void InfiniBandLayer::setfecn(int fecn) const - { - if (fecn) - getBthHeader()->qpn |= htobe32(BTH_FECN_MASK); - else - getBthHeader()->qpn &= ~htobe32(BTH_FECN_MASK); - } - - int InfiniBandLayer::getBecn() const - { - return 0 != (htobe32(BTH_BECN_MASK) & getBthHeader()->qpn); - } - - void InfiniBandLayer::setbecn(int becn) const - { - if (becn) - getBthHeader()->qpn |= htobe32(BTH_BECN_MASK); - else - getBthHeader()->qpn &= ~htobe32(BTH_BECN_MASK); - } - - uint8_t InfiniBandLayer::getResv6a() const - { - return (BTH_RESV6A_MASK & be32toh(getBthHeader()->qpn)) >> 24; - } - - void InfiniBandLayer::setResv6a() const - { - getBthHeader()->qpn = htobe32(~BTH_RESV6A_MASK); - } - - int InfiniBandLayer::getAck() const - { - return 0 != (htobe32(BTH_ACK_MASK) & getBthHeader()->apsn); - } - - void InfiniBandLayer::setAck(int ack) const - { - if (ack) - getBthHeader()->apsn |= htobe32(BTH_ACK_MASK); - else - getBthHeader()->apsn &= ~htobe32(BTH_ACK_MASK); - } - - void InfiniBandLayer::setResv7() const - { - getBthHeader()->apsn &= ~htobe32(BTH_RESV7_MASK); - } - - uint32_t InfiniBandLayer::getPsn() const - { - return BTH_PSN_MASK & be32toh(getBthHeader()->apsn); - } - - void InfiniBandLayer::setPsn(uint32_t psn) const - { - uint32_t apsn = be32toh(getBthHeader()->apsn); - - getBthHeader()->apsn = htobe32((BTH_PSN_MASK & psn) | - (~BTH_PSN_MASK & apsn)); - } -} \ No newline at end of file + std::ostringstream opCodeStream; + opCodeStream << getOpcode(); + + return "InfiniBand Layer, Opcode: " + opCodeStream.str(); + } + + + uint8_t InfiniBandLayer::getOpcode() const + { + return getBthHeader()->opcode; + } + + void InfiniBandLayer::setOpcode(uint8_t opcode) const + { + getBthHeader()->opcode = opcode; + } + + uint8_t InfiniBandLayer::getSe() const + { + return 0 != (BTH_SE_MASK & getBthHeader()->flags); + } + + void InfiniBandLayer::setSe(int se) const + { + if (se) + getBthHeader()->flags |= BTH_SE_MASK; + else + getBthHeader()->flags &= ~BTH_SE_MASK; + } + + uint8_t InfiniBandLayer::getMig() const + { + return 0 != (BTH_MIG_MASK & getBthHeader()->flags); + } + + void InfiniBandLayer::setMig(uint8_t mig) const + { + if (mig) + getBthHeader()->flags |= BTH_MIG_MASK; + else + getBthHeader()->flags &= ~BTH_MIG_MASK; + } + + uint8_t InfiniBandLayer::getPad() const + { + return (BTH_PAD_MASK & getBthHeader()->flags) >> 4; + } + + void InfiniBandLayer::setPad(uint8_t pad) const + { + getBthHeader()->flags = (BTH_PAD_MASK & (pad << 4)) | + (~BTH_PAD_MASK & getBthHeader()->flags); + } + + uint8_t InfiniBandLayer::getTver() const + { + return BTH_TVER_MASK & getBthHeader()->flags; + } + + void InfiniBandLayer::setTver(uint8_t tver) const + { + getBthHeader()->flags = (BTH_TVER_MASK & tver) | + (~BTH_TVER_MASK & getBthHeader()->flags); + } + + uint16_t InfiniBandLayer::getPkey() const + { + return be16toh(getBthHeader()->pkey); + } + + void InfiniBandLayer::setPkey(uint16_t pkey) const + { + getBthHeader()->pkey = htobe16(pkey); + } + + uint32_t InfiniBandLayer::getQpn() const + { + return BTH_QPN_MASK & be32toh(getBthHeader()->qpn); + } + + void InfiniBandLayer::setQpn(uint32_t qpn) const + { + + uint32_t resvqpn = be32toh(getBthHeader()->qpn); + + getBthHeader()->qpn = htobe32((BTH_QPN_MASK & qpn) | + (~BTH_QPN_MASK & resvqpn)); + } + + int InfiniBandLayer::getFecn() const + { + return 0 != (htobe32(BTH_FECN_MASK) & getBthHeader()->qpn); + } + + void InfiniBandLayer::setfecn(int fecn) const + { + if (fecn) + getBthHeader()->qpn |= htobe32(BTH_FECN_MASK); + else + getBthHeader()->qpn &= ~htobe32(BTH_FECN_MASK); + } + + int InfiniBandLayer::getBecn() const + { + return 0 != (htobe32(BTH_BECN_MASK) & getBthHeader()->qpn); + } + + void InfiniBandLayer::setbecn(int becn) const + { + if (becn) + getBthHeader()->qpn |= htobe32(BTH_BECN_MASK); + else + getBthHeader()->qpn &= ~htobe32(BTH_BECN_MASK); + } + + uint8_t InfiniBandLayer::getResv6a() const + { + return (BTH_RESV6A_MASK & be32toh(getBthHeader()->qpn)) >> 24; + } + + void InfiniBandLayer::setResv6a() const + { + getBthHeader()->qpn = htobe32(~BTH_RESV6A_MASK); + } + + int InfiniBandLayer::getAck() const + { + return 0 != (htobe32(BTH_ACK_MASK) & getBthHeader()->apsn); + } + + void InfiniBandLayer::setAck(int ack) const + { + if (ack) + getBthHeader()->apsn |= htobe32(BTH_ACK_MASK); + else + getBthHeader()->apsn &= ~htobe32(BTH_ACK_MASK); + } + + void InfiniBandLayer::setResv7() const + { + getBthHeader()->apsn &= ~htobe32(BTH_RESV7_MASK); + } + + uint32_t InfiniBandLayer::getPsn() const + { + return BTH_PSN_MASK & be32toh(getBthHeader()->apsn); + } + + void InfiniBandLayer::setPsn(uint32_t psn) const + { + uint32_t apsn = be32toh(getBthHeader()->apsn); + + getBthHeader()->apsn = htobe32((BTH_PSN_MASK & psn) | + (~BTH_PSN_MASK & apsn)); + } +} // namespace pcpp \ No newline at end of file diff --git a/Tests/Packet++Test/TestDefinition.h b/Tests/Packet++Test/TestDefinition.h index b89365b47d..8baadc6294 100644 --- a/Tests/Packet++Test/TestDefinition.h +++ b/Tests/Packet++Test/TestDefinition.h @@ -264,7 +264,6 @@ PTF_TEST_CASE(Asn1EncodingTest); PTF_TEST_CASE(LdapParsingTest); PTF_TEST_CASE(LdapCreationTest); - // Implemented in WireGuardTests.cpp PTF_TEST_CASE(WireGuardHandshakeInitParsingTest); PTF_TEST_CASE(WireGuardHandshakeRespParsingTest); diff --git a/Tests/Packet++Test/Tests/InfiniBandTests.cpp b/Tests/Packet++Test/Tests/InfiniBandTests.cpp index c45dc26b5b..3edbca18a1 100644 --- a/Tests/Packet++Test/Tests/InfiniBandTests.cpp +++ b/Tests/Packet++Test/Tests/InfiniBandTests.cpp @@ -107,7 +107,8 @@ PTF_TEST_CASE(InfiniBandPacketParsing) // parse the raw packet into a parsed packet pcpp::Packet parsedPacket(&rawPacket1); - // first let's go over the layers one by one and find out its type, its total length, its header length and its payload length + // first let's go over the layers one by one and find out its type, its total length, + // its header length and its payload length for (auto* curLayer = parsedPacket.getFirstLayer(); curLayer != nullptr; curLayer = curLayer->getNextLayer()) { std::cout @@ -119,100 +120,100 @@ PTF_TEST_CASE(InfiniBandPacketParsing) switch (curLayer->getProtocol()) { - case pcpp::Ethernet: - { - // now let's get the Ethernet layer - auto* ethernetLayer = parsedPacket.getLayerOfType(); - if (ethernetLayer == nullptr) - { - std::cerr << "Something went wrong, couldn't find Ethernet layer" << std::endl; - } - - // print the source and dest MAC addresses and the Ether type - std::cout - << "Source MAC address: " << ethernetLayer->getSourceMac() << std::endl - << "Destination MAC address: " << ethernetLayer->getDestMac() << std::endl - << "Ether type = 0x" << std::hex << pcpp::netToHost16(ethernetLayer->getEthHeader()->etherType) << std::endl; - break; - } - case pcpp::VLAN: + case pcpp::Ethernet: + { + // now let's get the Ethernet layer + auto* ethernetLayer = parsedPacket.getLayerOfType(); + if (ethernetLayer == nullptr) { - // now let's get the Vlan layer - auto* vlanLayer = parsedPacket.getLayerOfType(); - if (vlanLayer == nullptr) - { - std::cerr << "Something went wrong, couldn't find Ethernet layer" << std::endl; - } - - std::cout - << vlanLayer->toString() << std::endl - << "vlan type = 0x" << std::hex << pcpp::netToHost16(vlanLayer->getVlanHeader()->etherType) << std::endl; - break; + std::cerr << "Something went wrong, couldn't find Ethernet layer" << std::endl; } - case pcpp::IPv4: - { - // let's get the IPv4 layer - auto* ipLayer = parsedPacket.getLayerOfType(); - if (ipLayer == nullptr) - { - std::cerr << "Something went wrong, couldn't find IPv4 layer" << std::endl; - } - // print source and dest IP addresses, IP ID and TTL - std::cout - << "Source IP address: " << ipLayer->getSrcIPAddress() << std::endl - << "Destination IP address: " << ipLayer->getDstIPAddress() << std::endl - << "IP ID: 0x" << std::hex << pcpp::netToHost16(ipLayer->getIPv4Header()->ipId) << std::endl - << "TTL: " << std::dec << (int)ipLayer->getIPv4Header()->timeToLive << std::endl; - break; - } - case pcpp::UDP: + // print the source and dest MAC addresses and the Ether type + std::cout + << "Source MAC address: " << ethernetLayer->getSourceMac() << std::endl + << "Destination MAC address: " << ethernetLayer->getDestMac() << std::endl + << "Ether type = 0x" << std::hex << pcpp::netToHost16(ethernetLayer->getEthHeader()->etherType) << std::endl; + break; + } + case pcpp::VLAN: + { + // now let's get the Vlan layer + auto* vlanLayer = parsedPacket.getLayerOfType(); + if (vlanLayer == nullptr) { - // let's get the UDP layer - auto* udpLayer = parsedPacket.getLayerOfType(); - if (udpLayer == nullptr) - { - std::cerr << "Something went wrong, couldn't find UDP layer" << std::endl; - } - // print source and dest port - std::cout - << "Source port: " << udpLayer->getSrcPort() << std::endl - << "Destination port: " << udpLayer->getDstPort() << std::endl; - break; + std::cerr << "Something went wrong, couldn't find Ethernet layer" << std::endl; } - case pcpp::IB: + + std::cout + << vlanLayer->toString() << std::endl + << "vlan type = 0x" << std::hex << pcpp::netToHost16(vlanLayer->getVlanHeader()->etherType) << std::endl; + break; + } + case pcpp::IPv4: + { + // let's get the IPv4 layer + auto* ipLayer = parsedPacket.getLayerOfType(); + if (ipLayer == nullptr) { - // let's get the IB layer - auto* ibLayer = parsedPacket.getLayerOfType(); - if (ibLayer == nullptr) - { - std::cerr << "Something went wrong, couldn't find IB layer" << std::endl; - } - // print opcode - std::cout - << "Opcode: " << std::dec << (int)ibLayer->getOpcode() << std::endl - << "Se: " << (int)ibLayer->getSe() << std::endl - << "Mig: " << (int)ibLayer->getMig() << std::endl - << "Pad: " << (int)ibLayer->getPad() << std::endl - << "Tver: " << (int)ibLayer->getTver() << std::endl - << "Pkey: " << (int)ibLayer->getPkey() << std::endl - << "Qpn: " << (int)ibLayer->getQpn() << std::endl - << "Fecn: " << (int)ibLayer->getFecn() << std::endl - << "Becn: " << (int)ibLayer->getBecn() << std::endl - << "Resv6a: " << (int)ibLayer->getResv6a() << std::endl - << "Ack: " << (int)ibLayer->getAck() << std::endl - << "Psn: " << (int)ibLayer->getPsn() << std::endl; - break; + std::cerr << "Something went wrong, couldn't find IPv4 layer" << std::endl; } - case pcpp::GenericPayload: + + // print source and dest IP addresses, IP ID and TTL + std::cout + << "Source IP address: " << ipLayer->getSrcIPAddress() << std::endl + << "Destination IP address: " << ipLayer->getDstIPAddress() << std::endl + << "IP ID: 0x" << std::hex << pcpp::netToHost16(ipLayer->getIPv4Header()->ipId) << std::endl + << "TTL: " << std::dec << (int)ipLayer->getIPv4Header()->timeToLive << std::endl; + break; + } + case pcpp::UDP: + { + // let's get the UDP layer + auto* udpLayer = parsedPacket.getLayerOfType(); + if (udpLayer == nullptr) { - break; + std::cerr << "Something went wrong, couldn't find UDP layer" << std::endl; } - default: + // print source and dest port + std::cout + << "Source port: " << udpLayer->getSrcPort() << std::endl + << "Destination port: " << udpLayer->getDstPort() << std::endl; + break; + } + case pcpp::IB: + { + // let's get the IB layer + auto* ibLayer = parsedPacket.getLayerOfType(); + if (ibLayer == nullptr) { - std::cerr << "Something went wrong, couldn't find this layer" << std::endl; - break; + std::cerr << "Something went wrong, couldn't find IB layer" << std::endl; } + // print opcode + std::cout + << "Opcode: " << std::dec << (int)ibLayer->getOpcode() << std::endl + << "Se: " << (int)ibLayer->getSe() << std::endl + << "Mig: " << (int)ibLayer->getMig() << std::endl + << "Pad: " << (int)ibLayer->getPad() << std::endl + << "Tver: " << (int)ibLayer->getTver() << std::endl + << "Pkey: " << (int)ibLayer->getPkey() << std::endl + << "Qpn: " << (int)ibLayer->getQpn() << std::endl + << "Fecn: " << (int)ibLayer->getFecn() << std::endl + << "Becn: " << (int)ibLayer->getBecn() << std::endl + << "Resv6a: " << (int)ibLayer->getResv6a() << std::endl + << "Ack: " << (int)ibLayer->getAck() << std::endl + << "Psn: " << (int)ibLayer->getPsn() << std::endl; + break; + } + case pcpp::GenericPayload: + { + break; + } + default: + { + std::cerr << "Something went wrong, couldn't find this layer" << std::endl; + break; + } } } diff --git a/Tests/Packet++Test/main.cpp b/Tests/Packet++Test/main.cpp index ac4b81024f..d5f58dd632 100644 --- a/Tests/Packet++Test/main.cpp +++ b/Tests/Packet++Test/main.cpp @@ -334,7 +334,6 @@ int main(int argc, char* argv[]) PTF_RUN_TEST(LdapParsingTest, "ldap"); PTF_RUN_TEST(LdapCreationTest, "ldap"); - PTF_RUN_TEST(WireGuardHandshakeInitParsingTest, "wg"); PTF_RUN_TEST(WireGuardHandshakeRespParsingTest, "wg"); PTF_RUN_TEST(WireGuardCookieReplyParsingTest, "wg"); From 008eab77d9521e8e9378301fdf7a27bfb8916f78 Mon Sep 17 00:00:00 2001 From: ycl <931415151@qq.com> Date: Sat, 5 Oct 2024 10:12:55 +0800 Subject: [PATCH 05/14] fix macro and coding style --- Packet++/header/InfiniBandLayer.h | 48 ++++++++++---------- Packet++/header/ProtocolType.h | 2 +- Packet++/src/InfiniBandLayer.cpp | 2 +- Tests/Packet++Test/Tests/InfiniBandTests.cpp | 10 ++-- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/Packet++/header/InfiniBandLayer.h b/Packet++/header/InfiniBandLayer.h index f5a175acf5..98c7414ff6 100644 --- a/Packet++/header/InfiniBandLayer.h +++ b/Packet++/header/InfiniBandLayer.h @@ -15,7 +15,7 @@ namespace pcpp * * Some of these are for reference and completeness only since * rxe does not currently support RD transport - * most of this could be moved into IB core. ib_pack.h has + * most of this could be moved into Infiniband core. ib_pack.h has * part of this but is incomplete * * Header specific routines to insert/extract values to/from headers @@ -26,13 +26,10 @@ namespace pcpp * Conversion to/from network byte order from cpu order is also done. */ - #define RXE_ICRC_SIZE (4) - #define RXE_MAX_HDR_LENGTH (80) +#define RXE_ICRC_SIZE 4 +#define RXE_MAX_HDR_LENGTH 80 - /****************************************************************************** - * Base Transport Header - ******************************************************************************/ -#pragma pack(push, 1) + /* Base Transport Header */ struct rxe_bth { uint8_t opcode; @@ -41,29 +38,32 @@ namespace pcpp uint32_t qpn; uint32_t apsn; }; -#pragma pack(pop) - - #define BTH_TVER (0) - #define BTH_DEF_PKEY (0xffff) - - #define BTH_SE_MASK (0x80) - #define BTH_MIG_MASK (0x40) - #define BTH_PAD_MASK (0x30) - #define BTH_TVER_MASK (0x0f) - #define BTH_FECN_MASK (0x80000000) - #define BTH_BECN_MASK (0x40000000) - #define BTH_RESV6A_MASK (0x3f000000) - #define BTH_QPN_MASK (0x00ffffff) - #define BTH_ACK_MASK (0x80000000) - #define BTH_RESV7_MASK (0x7f000000) - #define BTH_PSN_MASK (0x00ffffff) +#define BTH_TVER 0x0 +#define BTH_DEF_PKEY 0xffff + +#define BTH_SE_MASK 0x80 +#define BTH_MIG_MASK 0x40 +#define BTH_PAD_MASK 0x30 +#define BTH_TVER_MASK 0x0f +#define BTH_FECN_MASK 0x80000000 +#define BTH_BECN_MASK 0x40000000 +#define BTH_RESV6A_MASK 0x3f000000 +#define BTH_QPN_MASK 0x00ffffff +#define BTH_ACK_MASK 0x80000000 +#define BTH_RESV7_MASK 0x7f000000 +#define BTH_PSN_MASK 0x00ffffff + + /** + * @class InfiniBandLayer + * Represents an InfiniBand protocol layer + */ class InfiniBandLayer : public Layer { public: InfiniBandLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) - : Layer(data, dataLen, prevLayer, packet, IB) + : Layer(data, dataLen, prevLayer, packet, Infiniband) {} InfiniBandLayer( uint8_t opcode, int se, diff --git a/Packet++/header/ProtocolType.h b/Packet++/header/ProtocolType.h index 6c4e085910..391569d471 100644 --- a/Packet++/header/ProtocolType.h +++ b/Packet++/header/ProtocolType.h @@ -355,7 +355,7 @@ namespace pcpp /* * infiniband protocol */ - const ProtocolType IB = 57; + const ProtocolType Infiniband = 57; /** * An enum representing OSI model layers diff --git a/Packet++/src/InfiniBandLayer.cpp b/Packet++/src/InfiniBandLayer.cpp index fdb95a7217..77e9dd02fd 100644 --- a/Packet++/src/InfiniBandLayer.cpp +++ b/Packet++/src/InfiniBandLayer.cpp @@ -29,7 +29,7 @@ namespace pcpp if (ack_req) psn |= BTH_ACK_MASK; bthHdr->apsn = htobe32(psn); - m_Protocol = IB; + m_Protocol = Infiniband; } void InfiniBandLayer::parseNextLayer() diff --git a/Tests/Packet++Test/Tests/InfiniBandTests.cpp b/Tests/Packet++Test/Tests/InfiniBandTests.cpp index 3edbca18a1..bf4d365d97 100644 --- a/Tests/Packet++Test/Tests/InfiniBandTests.cpp +++ b/Tests/Packet++Test/Tests/InfiniBandTests.cpp @@ -28,8 +28,8 @@ std::string getProtocolTypeAsString(pcpp::ProtocolType protocolType) return "UDP"; case pcpp::TCP: return "TCP"; - case pcpp::IB: - return "IB"; + case pcpp::Infiniband: + return "Infiniband"; case pcpp::HTTPRequest: case pcpp::HTTPResponse: return "HTTP"; @@ -181,13 +181,13 @@ PTF_TEST_CASE(InfiniBandPacketParsing) << "Destination port: " << udpLayer->getDstPort() << std::endl; break; } - case pcpp::IB: + case pcpp::Infiniband: { - // let's get the IB layer + // let's get the Infiniband layer auto* ibLayer = parsedPacket.getLayerOfType(); if (ibLayer == nullptr) { - std::cerr << "Something went wrong, couldn't find IB layer" << std::endl; + std::cerr << "Something went wrong, couldn't find Infiniband layer" << std::endl; } // print opcode std::cout From 86d3a580fe82e114560c9429a895f5bf1e5739fa Mon Sep 17 00:00:00 2001 From: ycl <931415151@qq.com> Date: Sat, 5 Oct 2024 14:12:55 +0800 Subject: [PATCH 06/14] IB toString use more efficient style --- Packet++/src/InfiniBandLayer.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Packet++/src/InfiniBandLayer.cpp b/Packet++/src/InfiniBandLayer.cpp index 77e9dd02fd..f5f69d69ed 100644 --- a/Packet++/src/InfiniBandLayer.cpp +++ b/Packet++/src/InfiniBandLayer.cpp @@ -50,10 +50,9 @@ namespace pcpp std::string InfiniBandLayer::toString() const { - std::ostringstream opCodeStream; - opCodeStream << getOpcode(); - - return "InfiniBand Layer, Opcode: " + opCodeStream.str(); + std::ostringstream ss; + ss << "InfiniBand Layer, Opcode: " << getOpcode(); + return ss.str(); } From 5cbc3f95d6937ecec8743319263a9d005ad5d280 Mon Sep 17 00:00:00 2001 From: ycl <931415151@qq.com> Date: Sat, 5 Oct 2024 21:44:15 +0800 Subject: [PATCH 07/14] IB coding style fix --- Packet++/header/InfiniBandLayer.h | 79 +++--- Packet++/header/ProtocolType.h | 2 +- Packet++/src/InfiniBandLayer.cpp | 24 +- Tests/Packet++Test/Tests/InfiniBandTests.cpp | 256 ++++++++++--------- 4 files changed, 179 insertions(+), 182 deletions(-) diff --git a/Packet++/header/InfiniBandLayer.h b/Packet++/header/InfiniBandLayer.h index 98c7414ff6..24bb3b3c4f 100644 --- a/Packet++/header/InfiniBandLayer.h +++ b/Packet++/header/InfiniBandLayer.h @@ -11,48 +11,48 @@ namespace pcpp { /* - * IBA header types and methods - * - * Some of these are for reference and completeness only since - * rxe does not currently support RD transport - * most of this could be moved into Infiniband core. ib_pack.h has - * part of this but is incomplete - * - * Header specific routines to insert/extract values to/from headers - * the routines that are named __hhh_(set_)fff() take a pointer to a - * hhh header and get(set) the fff field. The routines named - * hhh_(set_)fff take a packet info struct and find the - * header and field based on the opcode in the packet. - * Conversion to/from network byte order from cpu order is also done. - */ - -#define RXE_ICRC_SIZE 4 -#define RXE_MAX_HDR_LENGTH 80 + * IBA header types and methods + * + * Some of these are for reference and completeness only since + * rxe does not currently support RD transport + * most of this could be moved into Infiniband core. ib_pack.h has + * part of this but is incomplete + * + * Header specific routines to insert/extract values to/from headers + * the routines that are named __hhh_(set_)fff() take a pointer to a + * hhh header and get(set) the fff field. The routines named + * hhh_(set_)fff take a packet info struct and find the + * header and field based on the opcode in the packet. + * Conversion to/from network byte order from cpu order is also done. + */ + +#define RXE_ICRC_SIZE 4 +#define RXE_MAX_HDR_LENGTH 80 /* Base Transport Header */ struct rxe_bth { - uint8_t opcode; - uint8_t flags; - uint16_t pkey; - uint32_t qpn; - uint32_t apsn; + uint8_t opcode; + uint8_t flags; + uint16_t pkey; + uint32_t qpn; + uint32_t apsn; }; -#define BTH_TVER 0x0 -#define BTH_DEF_PKEY 0xffff - -#define BTH_SE_MASK 0x80 -#define BTH_MIG_MASK 0x40 -#define BTH_PAD_MASK 0x30 -#define BTH_TVER_MASK 0x0f -#define BTH_FECN_MASK 0x80000000 -#define BTH_BECN_MASK 0x40000000 -#define BTH_RESV6A_MASK 0x3f000000 -#define BTH_QPN_MASK 0x00ffffff -#define BTH_ACK_MASK 0x80000000 -#define BTH_RESV7_MASK 0x7f000000 -#define BTH_PSN_MASK 0x00ffffff +#define BTH_TVER 0x0 +#define BTH_DEF_PKEY 0xffff + +#define BTH_SE_MASK 0x80 +#define BTH_MIG_MASK 0x40 +#define BTH_PAD_MASK 0x30 +#define BTH_TVER_MASK 0x0f +#define BTH_FECN_MASK 0x80000000 +#define BTH_BECN_MASK 0x40000000 +#define BTH_RESV6A_MASK 0x3f000000 +#define BTH_QPN_MASK 0x00ffffff +#define BTH_ACK_MASK 0x80000000 +#define BTH_RESV7_MASK 0x7f000000 +#define BTH_PSN_MASK 0x00ffffff /** * @class InfiniBandLayer @@ -63,12 +63,11 @@ namespace pcpp public: InfiniBandLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) - : Layer(data, dataLen, prevLayer, packet, Infiniband) + : Layer(data, dataLen, prevLayer, packet, Infiniband) {} - InfiniBandLayer( uint8_t opcode, int se, - int mig, int pad, uint16_t pkey, uint32_t qpn, int ack_req, - uint32_t psn); + InfiniBandLayer(uint8_t opcode, int se, int mig, int pad, uint16_t pkey, uint32_t qpn, int ack_req, + uint32_t psn); rxe_bth* getBthHeader() const { diff --git a/Packet++/header/ProtocolType.h b/Packet++/header/ProtocolType.h index 391569d471..9adfc733c3 100644 --- a/Packet++/header/ProtocolType.h +++ b/Packet++/header/ProtocolType.h @@ -351,7 +351,7 @@ namespace pcpp * WireGuard protocol */ const ProtocolType WireGuard = 56; - + /* * infiniband protocol */ diff --git a/Packet++/src/InfiniBandLayer.cpp b/Packet++/src/InfiniBandLayer.cpp index f5f69d69ed..6ee3c0894d 100644 --- a/Packet++/src/InfiniBandLayer.cpp +++ b/Packet++/src/InfiniBandLayer.cpp @@ -8,8 +8,8 @@ namespace pcpp { - InfiniBandLayer::InfiniBandLayer( uint8_t opcode, int se, int mig, int pad, uint16_t pkey, uint32_t qpn, int ack_req, - uint32_t psn) + InfiniBandLayer::InfiniBandLayer(uint8_t opcode, int se, int mig, int pad, uint16_t pkey, uint32_t qpn, int ack_req, + uint32_t psn) { const size_t headerLen = sizeof(rxe_bth); m_DataLen = headerLen; @@ -44,14 +44,12 @@ namespace pcpp } void InfiniBandLayer::computeCalculateFields() - { - - } + {} std::string InfiniBandLayer::toString() const { std::ostringstream ss; - ss << "InfiniBand Layer, Opcode: " << getOpcode(); + ss << "InfiniBand Layer, Opcode: " << getOpcode(); return ss.str(); } @@ -99,8 +97,7 @@ namespace pcpp void InfiniBandLayer::setPad(uint8_t pad) const { - getBthHeader()->flags = (BTH_PAD_MASK & (pad << 4)) | - (~BTH_PAD_MASK & getBthHeader()->flags); + getBthHeader()->flags = (BTH_PAD_MASK & (pad << 4)) | (~BTH_PAD_MASK & getBthHeader()->flags); } uint8_t InfiniBandLayer::getTver() const @@ -110,8 +107,7 @@ namespace pcpp void InfiniBandLayer::setTver(uint8_t tver) const { - getBthHeader()->flags = (BTH_TVER_MASK & tver) | - (~BTH_TVER_MASK & getBthHeader()->flags); + getBthHeader()->flags = (BTH_TVER_MASK & tver) | (~BTH_TVER_MASK & getBthHeader()->flags); } uint16_t InfiniBandLayer::getPkey() const @@ -134,8 +130,7 @@ namespace pcpp uint32_t resvqpn = be32toh(getBthHeader()->qpn); - getBthHeader()->qpn = htobe32((BTH_QPN_MASK & qpn) | - (~BTH_QPN_MASK & resvqpn)); + getBthHeader()->qpn = htobe32((BTH_QPN_MASK & qpn) | (~BTH_QPN_MASK & resvqpn)); } int InfiniBandLayer::getFecn() const @@ -201,7 +196,6 @@ namespace pcpp { uint32_t apsn = be32toh(getBthHeader()->apsn); - getBthHeader()->apsn = htobe32((BTH_PSN_MASK & psn) | - (~BTH_PSN_MASK & apsn)); + getBthHeader()->apsn = htobe32((BTH_PSN_MASK & psn) | (~BTH_PSN_MASK & apsn)); } -} // namespace pcpp \ No newline at end of file +} // namespace pcpp \ No newline at end of file diff --git a/Tests/Packet++Test/Tests/InfiniBandTests.cpp b/Tests/Packet++Test/Tests/InfiniBandTests.cpp index bf4d365d97..b0c4845c75 100644 --- a/Tests/Packet++Test/Tests/InfiniBandTests.cpp +++ b/Tests/Packet++Test/Tests/InfiniBandTests.cpp @@ -47,53 +47,48 @@ PTF_TEST_CASE(InfiniBandPacketParsing) /* uint8_t buffer1[] = { - 0x30, 0x46, 0x9a, 0x23, 0xfb, 0xfa, 0x6c, 0xf0, - 0x49, 0xb2, 0xde, 0x6e, 0x08, 0x00, 0x45, 0x00, - 0x00, 0x3c, 0x1a, 0x57, 0x00, 0x00, 0x80, 0x01, - 0x14, 0x65, 0x0a, 0x00, 0x00, 0x04, 0x01, 0x01, - 0x01, 0x01, 0x08, 0x00, 0x4d, 0x5a, 0x00, 0x01, - 0x00, 0x01, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, - 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, - 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, - 0x68, 0x69, + 0x30, 0x46, 0x9a, 0x23, 0xfb, 0xfa, 0x6c, 0xf0, + 0x49, 0xb2, 0xde, 0x6e, 0x08, 0x00, 0x45, 0x00, + 0x00, 0x3c, 0x1a, 0x57, 0x00, 0x00, 0x80, 0x01, + 0x14, 0x65, 0x0a, 0x00, 0x00, 0x04, 0x01, 0x01, + 0x01, 0x01, 0x08, 0x00, 0x4d, 0x5a, 0x00, 0x01, + 0x00, 0x01, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, + 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, + 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, }; */ // RRoce uint8_t buffer1[] = { - 0x00, 0x0c, 0x29, 0xae, 0x1c, 0xa4, 0x00, 0x0c, - 0x29, 0x89, 0xa2, 0xe5, 0x08, 0x00, 0x45, 0x00, - 0x00, 0x3c, 0xbe, 0x10, 0x40, 0x00, 0x40, 0x11, - 0x8a, 0x4b, 0xc0, 0xa8, 0x38, 0x81, 0xc0, 0xa8, - 0x38, 0x83, 0xdf, 0x94, 0x12, 0xb7, 0x00, 0x28, - 0x00, 0x00, 0x0c, 0x00, 0xff, 0xff, 0x00, 0x00, - 0x00, 0x11, 0x80, 0x54, 0xcb, 0x63, 0x00, 0x00, - 0x7f, 0xee, 0x26, 0x0f, 0xb0, 0x00, 0x00, 0x00, - 0x02, 0xb8, 0x00, 0x01, 0x00, 0x00, 0x08, 0xc6, - 0x15, 0x4a, + 0x00, 0x0c, 0x29, 0xae, 0x1c, 0xa4, 0x00, 0x0c, 0x29, 0x89, 0xa2, 0xe5, 0x08, 0x00, 0x45, + 0x00, 0x00, 0x3c, 0xbe, 0x10, 0x40, 0x00, 0x40, 0x11, 0x8a, 0x4b, 0xc0, 0xa8, 0x38, 0x81, + 0xc0, 0xa8, 0x38, 0x83, 0xdf, 0x94, 0x12, 0xb7, 0x00, 0x28, 0x00, 0x00, 0x0c, 0x00, 0xff, + 0xff, 0x00, 0x00, 0x00, 0x11, 0x80, 0x54, 0xcb, 0x63, 0x00, 0x00, 0x7f, 0xee, 0x26, 0x0f, + 0xb0, 0x00, 0x00, 0x00, 0x02, 0xb8, 0x00, 0x01, 0x00, 0x00, 0x08, 0xc6, 0x15, 0x4a, }; /* // eth + vlan + ipv4 + ICMP uint8_t buffer1[] = { - 0x00, 0x1b, 0xd4, 0x1b, 0xa4, 0xd8, 0x00, 0x13, - 0xc3, 0xdf, 0xae, 0x18, 0x81, 0x00, 0x00, 0x76, - 0x81, 0x00, 0x00, 0x0a, 0x08, 0x00, 0x45, 0x00, - 0x00, 0x64, 0x00, 0x0f, 0x00, 0x00, 0xff, 0x01, - 0x92, 0x9b, 0x0a, 0x76, 0x0a, 0x01, 0x0a, 0x76, - 0x0a, 0x02, 0x08, 0x00, 0xce, 0xb7, 0x00, 0x03, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, - 0xaf, 0x70, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd + 0x00, 0x1b, 0xd4, 0x1b, 0xa4, 0xd8, 0x00, 0x13, + 0xc3, 0xdf, 0xae, 0x18, 0x81, 0x00, 0x00, 0x76, + 0x81, 0x00, 0x00, 0x0a, 0x08, 0x00, 0x45, 0x00, + 0x00, 0x64, 0x00, 0x0f, 0x00, 0x00, 0xff, 0x01, + 0x92, 0x9b, 0x0a, 0x76, 0x0a, 0x01, 0x0a, 0x76, + 0x0a, 0x02, 0x08, 0x00, 0xce, 0xb7, 0x00, 0x03, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, + 0xaf, 0x70, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, + 0xab, 0xcd }; */ @@ -107,114 +102,123 @@ PTF_TEST_CASE(InfiniBandPacketParsing) // parse the raw packet into a parsed packet pcpp::Packet parsedPacket(&rawPacket1); - // first let's go over the layers one by one and find out its type, its total length, + // first let's go over the layers one by one and find out its type, its total length, // its header length and its payload length for (auto* curLayer = parsedPacket.getFirstLayer(); curLayer != nullptr; curLayer = curLayer->getNextLayer()) { - std::cout - << "Layer type: " << getProtocolTypeAsString(curLayer->getProtocol()) << "; " // get layer type - << "Total data: " << curLayer->getDataLen() << " [bytes]; " // get total length of the layer - << "Layer data: " << curLayer->getHeaderLen() << " [bytes]; " // get the header length of the layer - << "Layer payload: " << curLayer->getLayerPayloadSize() << " [bytes]" // get the payload length of the layer (equals total length minus header length) - << std::endl; + std::cout << "Layer type: " << getProtocolTypeAsString(curLayer->getProtocol()) << "; " // get layer type + << "Total data: " << curLayer->getDataLen() << " [bytes]; " // get total length of the layer + << "Layer data: " << curLayer->getHeaderLen() << " [bytes]; " // get the header length of the layer + << "Layer payload: " << curLayer->getLayerPayloadSize() + << " [bytes]" // get the payload length of the layer (equals total length minus header length) + << std::endl; - switch (curLayer->getProtocol()) - { - case pcpp::Ethernet: + switch (curLayer->getProtocol()) + { + case pcpp::Ethernet: + { + // now let's get the Ethernet layer + auto* ethernetLayer = parsedPacket.getLayerOfType(); + if (ethernetLayer == nullptr) + { + std::cerr << "Something went wrong, couldn't find Ethernet layer" << std::endl; + } + else { - // now let's get the Ethernet layer - auto* ethernetLayer = parsedPacket.getLayerOfType(); - if (ethernetLayer == nullptr) - { - std::cerr << "Something went wrong, couldn't find Ethernet layer" << std::endl; - } - // print the source and dest MAC addresses and the Ether type - std::cout - << "Source MAC address: " << ethernetLayer->getSourceMac() << std::endl - << "Destination MAC address: " << ethernetLayer->getDestMac() << std::endl - << "Ether type = 0x" << std::hex << pcpp::netToHost16(ethernetLayer->getEthHeader()->etherType) << std::endl; - break; + std::cout << "Source MAC address: " << ethernetLayer->getSourceMac() << std::endl + << "Destination MAC address: " << ethernetLayer->getDestMac() << std::endl + << "Ether type = 0x" << std::hex << pcpp::netToHost16(ethernetLayer->getEthHeader()->etherType) + << std::endl; } - case pcpp::VLAN: + break; + } + case pcpp::VLAN: + { + // now let's get the Vlan layer + auto* vlanLayer = parsedPacket.getLayerOfType(); + if (vlanLayer == nullptr) { - // now let's get the Vlan layer - auto* vlanLayer = parsedPacket.getLayerOfType(); - if (vlanLayer == nullptr) - { - std::cerr << "Something went wrong, couldn't find Ethernet layer" << std::endl; - } - - std::cout - << vlanLayer->toString() << std::endl - << "vlan type = 0x" << std::hex << pcpp::netToHost16(vlanLayer->getVlanHeader()->etherType) << std::endl; - break; + std::cerr << "Something went wrong, couldn't find Ethernet layer" << std::endl; } - case pcpp::IPv4: + else { - // let's get the IPv4 layer - auto* ipLayer = parsedPacket.getLayerOfType(); - if (ipLayer == nullptr) - { - std::cerr << "Something went wrong, couldn't find IPv4 layer" << std::endl; - } - - // print source and dest IP addresses, IP ID and TTL - std::cout - << "Source IP address: " << ipLayer->getSrcIPAddress() << std::endl - << "Destination IP address: " << ipLayer->getDstIPAddress() << std::endl - << "IP ID: 0x" << std::hex << pcpp::netToHost16(ipLayer->getIPv4Header()->ipId) << std::endl - << "TTL: " << std::dec << (int)ipLayer->getIPv4Header()->timeToLive << std::endl; - break; - } - case pcpp::UDP: + std::cout << vlanLayer->toString() << std::endl + << "vlan type = 0x" << std::hex << pcpp::netToHost16(vlanLayer->getVlanHeader()->etherType) + << std::endl; + } + break; + } + case pcpp::IPv4: + { + // let's get the IPv4 layer + auto* ipLayer = parsedPacket.getLayerOfType(); + if (ipLayer == nullptr) { - // let's get the UDP layer - auto* udpLayer = parsedPacket.getLayerOfType(); - if (udpLayer == nullptr) - { - std::cerr << "Something went wrong, couldn't find UDP layer" << std::endl; - } - // print source and dest port - std::cout - << "Source port: " << udpLayer->getSrcPort() << std::endl - << "Destination port: " << udpLayer->getDstPort() << std::endl; - break; + std::cerr << "Something went wrong, couldn't find IPv4 layer" << std::endl; } - case pcpp::Infiniband: + else { - // let's get the Infiniband layer - auto* ibLayer = parsedPacket.getLayerOfType(); - if (ibLayer == nullptr) - { - std::cerr << "Something went wrong, couldn't find Infiniband layer" << std::endl; - } - // print opcode - std::cout - << "Opcode: " << std::dec << (int)ibLayer->getOpcode() << std::endl - << "Se: " << (int)ibLayer->getSe() << std::endl - << "Mig: " << (int)ibLayer->getMig() << std::endl - << "Pad: " << (int)ibLayer->getPad() << std::endl - << "Tver: " << (int)ibLayer->getTver() << std::endl - << "Pkey: " << (int)ibLayer->getPkey() << std::endl - << "Qpn: " << (int)ibLayer->getQpn() << std::endl - << "Fecn: " << (int)ibLayer->getFecn() << std::endl - << "Becn: " << (int)ibLayer->getBecn() << std::endl - << "Resv6a: " << (int)ibLayer->getResv6a() << std::endl - << "Ack: " << (int)ibLayer->getAck() << std::endl - << "Psn: " << (int)ibLayer->getPsn() << std::endl; - break; + // print source and dest IP addresses, IP ID and TTL + std::cout << "Source IP address: " << ipLayer->getSrcIPAddress() << std::endl + << "Destination IP address: " << ipLayer->getDstIPAddress() << std::endl + << "IP ID: 0x" << std::hex << pcpp::netToHost16(ipLayer->getIPv4Header()->ipId) << std::endl + << "TTL: " << std::dec << (int)ipLayer->getIPv4Header()->timeToLive << std::endl; + } + break; + } + case pcpp::UDP: + { + // let's get the UDP layer + auto* udpLayer = parsedPacket.getLayerOfType(); + if (udpLayer == nullptr) + { + std::cerr << "Something went wrong, couldn't find UDP layer" << std::endl; } - case pcpp::GenericPayload: + else { - break; + // print source and dest port + std::cout << "Source port: " << udpLayer->getSrcPort() << std::endl + << "Destination port: " << udpLayer->getDstPort() << std::endl; } - default: + break; + } + case pcpp::Infiniband: + { + // let's get the Infiniband layer + auto* ibLayer = parsedPacket.getLayerOfType(); + if (ibLayer == nullptr) { - std::cerr << "Something went wrong, couldn't find this layer" << std::endl; - break; + std::cerr << "Something went wrong, couldn't find Infiniband layer" << std::endl; } + else + { + // print opcode + std::cout << "Opcode: " << std::dec << (int)ibLayer->getOpcode() << std::endl + << "Se: " << (int)ibLayer->getSe() << std::endl + << "Mig: " << (int)ibLayer->getMig() << std::endl + << "Pad: " << (int)ibLayer->getPad() << std::endl + << "Tver: " << (int)ibLayer->getTver() << std::endl + << "Pkey: " << (int)ibLayer->getPkey() << std::endl + << "Qpn: " << (int)ibLayer->getQpn() << std::endl + << "Fecn: " << (int)ibLayer->getFecn() << std::endl + << "Becn: " << (int)ibLayer->getBecn() << std::endl + << "Resv6a: " << (int)ibLayer->getResv6a() << std::endl + << "Ack: " << (int)ibLayer->getAck() << std::endl + << "Psn: " << (int)ibLayer->getPsn() << std::endl; } + break; + } + case pcpp::GenericPayload: + { + break; + } + default: + { + std::cerr << "Something went wrong, couldn't find this layer" << std::endl; + break; + } + } } -} // InfiniBandPacketParsing \ No newline at end of file +} // InfiniBandPacketParsing \ No newline at end of file From b6f3c0407ef4526fc06b5f3033af8f2e7e20c765 Mon Sep 17 00:00:00 2001 From: ycl <931415151@qq.com> Date: Sun, 13 Oct 2024 15:15:12 +0800 Subject: [PATCH 08/14] add documentation of IB --- Packet++/CMakeLists.txt | 2 +- Packet++/header/InfiniBandLayer.h | 163 +++++++++++++++++- Packet++/header/ProtocolType.h | 2 +- Packet++/src/InfiniBandLayer.cpp | 1 - .../PacketExamples/InfinibandPacket.dat | 1 + Tests/Packet++Test/Tests/InfiniBandTests.cpp | 144 +++------------- 6 files changed, 185 insertions(+), 128 deletions(-) create mode 100644 Tests/Packet++Test/PacketExamples/InfinibandPacket.dat diff --git a/Packet++/CMakeLists.txt b/Packet++/CMakeLists.txt index 975622fe90..0c613f0999 100644 --- a/Packet++/CMakeLists.txt +++ b/Packet++/CMakeLists.txt @@ -19,6 +19,7 @@ add_library( src/IcmpLayer.cpp src/IcmpV6Layer.cpp src/IgmpLayer.cpp + src/InfiniBandLayer.cpp src/IPReassembly.cpp src/IPSecLayer.cpp src/IPv4Layer.cpp @@ -60,7 +61,6 @@ add_library( src/TLVData.cpp src/TpktLayer.cpp src/UdpLayer.cpp - src/InfiniBandLayer.cpp src/VlanLayer.cpp src/VrrpLayer.cpp src/VxlanLayer.cpp diff --git a/Packet++/header/InfiniBandLayer.h b/Packet++/header/InfiniBandLayer.h index 24bb3b3c4f..248274d330 100644 --- a/Packet++/header/InfiniBandLayer.h +++ b/Packet++/header/InfiniBandLayer.h @@ -29,7 +29,11 @@ namespace pcpp #define RXE_ICRC_SIZE 4 #define RXE_MAX_HDR_LENGTH 80 - /* Base Transport Header */ + /** + * @struct bth + * Represents an Base Transport Header + */ +#pragma pack(push, 1) struct rxe_bth { uint8_t opcode; @@ -38,6 +42,7 @@ namespace pcpp uint32_t qpn; uint32_t apsn; }; +#pragma pack(pop) #define BTH_TVER 0x0 #define BTH_DEF_PKEY 0xffff @@ -61,47 +66,196 @@ namespace pcpp class InfiniBandLayer : public Layer { public: - + /** + * A constructor that creates the layer from an existing packet raw data + * @param[in] data A pointer to the raw data (will be casted to bth_header) + * @param[in] dataLen Size of the data in bytes + * @param[in] prevLayer A pointer to the previous layer + * @param[in] packet A pointer to the Packet instance where layer will be stored in + */ InfiniBandLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : Layer(data, dataLen, prevLayer, packet, Infiniband) {} + /** + * A constructor that creates a new rxe_bth header and allocates the data + * @param[in] opcode The operation code + * @param[in] se The solicited event + * @param[in] mig The migration state + * @param[in] pad The pad count + * @param[in] pkey The parition key + * @param[in] qpn The destination queue pair (QP) number + * @param[in] ack_req The acknowledgment request + * @param[in] psn The packet sequence number + */ InfiniBandLayer(uint8_t opcode, int se, int mig, int pad, uint16_t pkey, uint32_t qpn, int ack_req, uint32_t psn); + /** + * Get a pointer to the BTH header. Notice this points directly to the data, so every change will change + * the actual packet data + * @return A pointer to the bth_header + */ rxe_bth* getBthHeader() const { return reinterpret_cast(m_Data); } + /** + * @return The operation code which defines the interpretation of the remaining header and payload bytes + */ uint8_t getOpcode() const; + + /** + * Set operation code + * @param[in] opcode The opcode to set + */ void setOpcode(uint8_t opcode) const; + + /** + * @return solicited event that the responder shall invoke the CQ event handler + */ uint8_t getSe() const; + + /** + * Set solicited evnet + * @param[in] se The solicited evnet to set + */ void setSe(int se) const; + + /** + * @return migreq which used to communicate migration state + */ uint8_t getMig() const; + + /** + * Set migreq + * @param[in] mig The migration state to set. If set to one, indicates the connection or EE context has been migrated; + * if set to zero, it means there is no change in the current migration state. + */ void setMig(uint8_t mig) const; + + /** + * @return PadCount which Packet payloads are sent as a multiple of 4-byte quantities. + * Pad count indicates the number of pad bytes - 0 to 3 - that are appended to the packetpayload. + * Pads are used to “stretch” the payload (payloads may be zero or more bytes in length) to be a multiple of 4 bytes + */ uint8_t getPad() const; + + /** + * Set PadCount + * @param[in] pad The PadCount to set + */ void setPad(uint8_t pad) const; + + /** + * @return Transport Header Version that specifies the version of the IBA Transport used for this packet + */ uint8_t getTver() const; + + /** + * Set Transport Header Version + * @param[in] tvr The transport header version to set + */ void setTver(uint8_t tver) const; + + /** + * @return parition key identifing the partition + * that the destination QP (RC, UC, UD, XRC) or EE Context (RD) is a member. + */ uint16_t getPkey() const; + + /** + * Set parition key + * @param[in] pkey The parition key to set + */ void setPkey(uint16_t pkey) const; + + /** + * @return destination queue pair (QP) identifier + */ uint32_t getQpn() const; + + /** + * Set Queue Pair Number + * @param[in] qpn The queue pair number to set + */ void setQpn(uint32_t qpn) const; + + /** + * @return FECN + * F (FECN): 0 indicates that a FECN indication was not received. + * 1 indicates that the packet went through a point of congestion + */ int getFecn() const; + + /** + * Set Fecn + * @param[in] fecn The FECN to set + */ void setfecn(int fecn) const; + + /** + * @return BECN + * B (BECN): 0 the packet did not go through a point of congestion or went + * through a point of congestion but was not marked. 1 indicates that the + * packet indicated by this header was subject to forward congestion. The B + * bit is set in an ACK or CN BTH + */ int getBecn() const; + + /** + * Set BECN + * @param[in] becn The BECN to set + */ void setbecn(int becn) const; + + /** + * @return Reserved (variant) - 6 bits. Transmitted as 0, ignored on receive. + */ uint8_t getResv6a() const; + + /** + * Set Reserved 6 bits + */ void setResv6a() const; + + /** + * @return ackreq that requests responder to schedule an acknowledgment on the associated QP. + */ int getAck() const; + + /** + * Set acknowledgment for requests + * @param[in] ack The acknowledgment to set + */ void setAck(int ack) const; + + /** + * Transmitted as 0, ignored on receive. + */ void setResv7() const; + + /** + * @return packet sequence number that is used to identify the position of a packet + * within a sequence of packets. + */ uint32_t getPsn() const; + + /** + * Set packet sequence number + * @param[in] psn The packet sequence number to set + */ void setPsn(uint32_t psn) const; + /** + * Currently identifies the following next layers sets to PayloadLayer + */ void parseNextLayer() override; + /** + * @return Size of rxe_bth header + */ size_t getHeaderLen() const override { return sizeof(rxe_bth); @@ -119,6 +273,11 @@ namespace pcpp return OsiModelTransportLayer; } + /** + * A static method that check whether is inifiniband RoCE port + * @param[in] port The port from UDP destionation port + * @return True if the port is inifiniband RoCE and can represent an rxe packet + */ static inline bool isInfiniBandPort(uint16_t port) { return (port == 4791); diff --git a/Packet++/header/ProtocolType.h b/Packet++/header/ProtocolType.h index 9adfc733c3..04b99821d7 100644 --- a/Packet++/header/ProtocolType.h +++ b/Packet++/header/ProtocolType.h @@ -353,7 +353,7 @@ namespace pcpp const ProtocolType WireGuard = 56; /* - * infiniband protocol + * Infiniband protocol */ const ProtocolType Infiniband = 57; diff --git a/Packet++/src/InfiniBandLayer.cpp b/Packet++/src/InfiniBandLayer.cpp index 6ee3c0894d..41e74cb8a1 100644 --- a/Packet++/src/InfiniBandLayer.cpp +++ b/Packet++/src/InfiniBandLayer.cpp @@ -52,7 +52,6 @@ namespace pcpp ss << "InfiniBand Layer, Opcode: " << getOpcode(); return ss.str(); } - uint8_t InfiniBandLayer::getOpcode() const { diff --git a/Tests/Packet++Test/PacketExamples/InfinibandPacket.dat b/Tests/Packet++Test/PacketExamples/InfinibandPacket.dat new file mode 100644 index 0000000000..7c58b77e10 --- /dev/null +++ b/Tests/Packet++Test/PacketExamples/InfinibandPacket.dat @@ -0,0 +1 @@ +000c29ae1ca4000c2989a2e508004500003cbe10400040118a4bc0a83881c0a83883df9412b7002800000c00ffff000000118054cb6300007fee260fb000000002b80001000008c6154a \ No newline at end of file diff --git a/Tests/Packet++Test/Tests/InfiniBandTests.cpp b/Tests/Packet++Test/Tests/InfiniBandTests.cpp index b0c4845c75..237f830432 100644 --- a/Tests/Packet++Test/Tests/InfiniBandTests.cpp +++ b/Tests/Packet++Test/Tests/InfiniBandTests.cpp @@ -45,59 +45,9 @@ PTF_TEST_CASE(InfiniBandPacketParsing) timeval time; gettimeofday(&time, nullptr); - /* - uint8_t buffer1[] = { - 0x30, 0x46, 0x9a, 0x23, 0xfb, 0xfa, 0x6c, 0xf0, - 0x49, 0xb2, 0xde, 0x6e, 0x08, 0x00, 0x45, 0x00, - 0x00, 0x3c, 0x1a, 0x57, 0x00, 0x00, 0x80, 0x01, - 0x14, 0x65, 0x0a, 0x00, 0x00, 0x04, 0x01, 0x01, - 0x01, 0x01, 0x08, 0x00, 0x4d, 0x5a, 0x00, 0x01, - 0x00, 0x01, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, - 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, - 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, - 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, - 0x68, 0x69, - }; - */ + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/InfinibandPacket.dat"); - - // RRoce - uint8_t buffer1[] = { - 0x00, 0x0c, 0x29, 0xae, 0x1c, 0xa4, 0x00, 0x0c, 0x29, 0x89, 0xa2, 0xe5, 0x08, 0x00, 0x45, - 0x00, 0x00, 0x3c, 0xbe, 0x10, 0x40, 0x00, 0x40, 0x11, 0x8a, 0x4b, 0xc0, 0xa8, 0x38, 0x81, - 0xc0, 0xa8, 0x38, 0x83, 0xdf, 0x94, 0x12, 0xb7, 0x00, 0x28, 0x00, 0x00, 0x0c, 0x00, 0xff, - 0xff, 0x00, 0x00, 0x00, 0x11, 0x80, 0x54, 0xcb, 0x63, 0x00, 0x00, 0x7f, 0xee, 0x26, 0x0f, - 0xb0, 0x00, 0x00, 0x00, 0x02, 0xb8, 0x00, 0x01, 0x00, 0x00, 0x08, 0xc6, 0x15, 0x4a, - }; - - /* - // eth + vlan + ipv4 + ICMP - uint8_t buffer1[] = { - 0x00, 0x1b, 0xd4, 0x1b, 0xa4, 0xd8, 0x00, 0x13, - 0xc3, 0xdf, 0xae, 0x18, 0x81, 0x00, 0x00, 0x76, - 0x81, 0x00, 0x00, 0x0a, 0x08, 0x00, 0x45, 0x00, - 0x00, 0x64, 0x00, 0x0f, 0x00, 0x00, 0xff, 0x01, - 0x92, 0x9b, 0x0a, 0x76, 0x0a, 0x01, 0x0a, 0x76, - 0x0a, 0x02, 0x08, 0x00, 0xce, 0xb7, 0x00, 0x03, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, - 0xaf, 0x70, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, 0xab, 0xcd, - 0xab, 0xcd - }; - */ - - int bufferLength1 = sizeof(buffer1); - - uint8_t* result = new uint8_t[bufferLength1]; - memcpy(result, buffer1, bufferLength1); - - pcpp::RawPacket rawPacket1(static_cast(result), bufferLength1, time, true); + pcpp::Packet ip4Packet(&rawPacket1); // parse the raw packet into a parsed packet pcpp::Packet parsedPacket(&rawPacket1); @@ -106,107 +56,55 @@ PTF_TEST_CASE(InfiniBandPacketParsing) // its header length and its payload length for (auto* curLayer = parsedPacket.getFirstLayer(); curLayer != nullptr; curLayer = curLayer->getNextLayer()) { - std::cout << "Layer type: " << getProtocolTypeAsString(curLayer->getProtocol()) << "; " // get layer type - << "Total data: " << curLayer->getDataLen() << " [bytes]; " // get total length of the layer - << "Layer data: " << curLayer->getHeaderLen() << " [bytes]; " // get the header length of the layer - << "Layer payload: " << curLayer->getLayerPayloadSize() - << " [bytes]" // get the payload length of the layer (equals total length minus header length) - << std::endl; - switch (curLayer->getProtocol()) { case pcpp::Ethernet: { // now let's get the Ethernet layer auto* ethernetLayer = parsedPacket.getLayerOfType(); - if (ethernetLayer == nullptr) - { - std::cerr << "Something went wrong, couldn't find Ethernet layer" << std::endl; - } - else - { - // print the source and dest MAC addresses and the Ether type - std::cout << "Source MAC address: " << ethernetLayer->getSourceMac() << std::endl - << "Destination MAC address: " << ethernetLayer->getDestMac() << std::endl - << "Ether type = 0x" << std::hex << pcpp::netToHost16(ethernetLayer->getEthHeader()->etherType) - << std::endl; - } + PTF_ASSERT_NOT_NULL(ethernetLayer); break; } case pcpp::VLAN: { // now let's get the Vlan layer auto* vlanLayer = parsedPacket.getLayerOfType(); - if (vlanLayer == nullptr) - { - std::cerr << "Something went wrong, couldn't find Ethernet layer" << std::endl; - } - else - { - std::cout << vlanLayer->toString() << std::endl - << "vlan type = 0x" << std::hex << pcpp::netToHost16(vlanLayer->getVlanHeader()->etherType) - << std::endl; - } + PTF_ASSERT_NOT_NULL(vlanLayer); break; } case pcpp::IPv4: { // let's get the IPv4 layer auto* ipLayer = parsedPacket.getLayerOfType(); - if (ipLayer == nullptr) - { - std::cerr << "Something went wrong, couldn't find IPv4 layer" << std::endl; - } - else - { - // print source and dest IP addresses, IP ID and TTL - std::cout << "Source IP address: " << ipLayer->getSrcIPAddress() << std::endl - << "Destination IP address: " << ipLayer->getDstIPAddress() << std::endl - << "IP ID: 0x" << std::hex << pcpp::netToHost16(ipLayer->getIPv4Header()->ipId) << std::endl - << "TTL: " << std::dec << (int)ipLayer->getIPv4Header()->timeToLive << std::endl; - } + PTF_ASSERT_NOT_NULL(ipLayer); break; } case pcpp::UDP: { // let's get the UDP layer auto* udpLayer = parsedPacket.getLayerOfType(); - if (udpLayer == nullptr) - { - std::cerr << "Something went wrong, couldn't find UDP layer" << std::endl; - } - else - { - // print source and dest port - std::cout << "Source port: " << udpLayer->getSrcPort() << std::endl - << "Destination port: " << udpLayer->getDstPort() << std::endl; - } + PTF_ASSERT_NOT_NULL(udpLayer); + PTF_ASSERT_EQUAL(udpLayer->getSrcPort(), 57236); + PTF_ASSERT_EQUAL(udpLayer->getDstPort(), 4791); break; } case pcpp::Infiniband: { // let's get the Infiniband layer auto* ibLayer = parsedPacket.getLayerOfType(); - if (ibLayer == nullptr) - { - std::cerr << "Something went wrong, couldn't find Infiniband layer" << std::endl; - } - else - { - // print opcode - std::cout << "Opcode: " << std::dec << (int)ibLayer->getOpcode() << std::endl - << "Se: " << (int)ibLayer->getSe() << std::endl - << "Mig: " << (int)ibLayer->getMig() << std::endl - << "Pad: " << (int)ibLayer->getPad() << std::endl - << "Tver: " << (int)ibLayer->getTver() << std::endl - << "Pkey: " << (int)ibLayer->getPkey() << std::endl - << "Qpn: " << (int)ibLayer->getQpn() << std::endl - << "Fecn: " << (int)ibLayer->getFecn() << std::endl - << "Becn: " << (int)ibLayer->getBecn() << std::endl - << "Resv6a: " << (int)ibLayer->getResv6a() << std::endl - << "Ack: " << (int)ibLayer->getAck() << std::endl - << "Psn: " << (int)ibLayer->getPsn() << std::endl; - } + PTF_ASSERT_NOT_NULL(ibLayer); + PTF_ASSERT_EQUAL(ibLayer->getOpcode(), 12); + PTF_ASSERT_EQUAL(ibLayer->getSe(), 0); + PTF_ASSERT_EQUAL(ibLayer->getMig(), 0); + PTF_ASSERT_EQUAL(ibLayer->getPad(), 0); + PTF_ASSERT_EQUAL(ibLayer->getTver(), 0); + PTF_ASSERT_EQUAL(ibLayer->getPkey(), 65535); + PTF_ASSERT_EQUAL(ibLayer->getQpn(), 17); + PTF_ASSERT_EQUAL(ibLayer->getFecn(), 0); + PTF_ASSERT_EQUAL(ibLayer->getBecn(), 0); + PTF_ASSERT_EQUAL(ibLayer->getResv6a(), 0); + PTF_ASSERT_EQUAL(ibLayer->getAck(), 1); + PTF_ASSERT_EQUAL(ibLayer->getPsn(), 5557091); break; } case pcpp::GenericPayload: From b7a2a74aa3a1499b5c8f4962ce6b1505169450be Mon Sep 17 00:00:00 2001 From: ycl <931415151@qq.com> Date: Sun, 13 Oct 2024 22:01:29 +0800 Subject: [PATCH 09/14] add IB creation test case --- Tests/Packet++Test/TestDefinition.h | 3 +- Tests/Packet++Test/Tests/InfiniBandTests.cpp | 29 ++++++++++++++++++-- Tests/Packet++Test/main.cpp | 3 +- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Tests/Packet++Test/TestDefinition.h b/Tests/Packet++Test/TestDefinition.h index 8baadc6294..3accf3a023 100644 --- a/Tests/Packet++Test/TestDefinition.h +++ b/Tests/Packet++Test/TestDefinition.h @@ -273,4 +273,5 @@ PTF_TEST_CASE(WireGuardCreationTest); PTF_TEST_CASE(WireGuardEditTest); // Implemented in InfiniBandTests.cpp -PTF_TEST_CASE(InfiniBandPacketParsing); +PTF_TEST_CASE(InfiniBandParsingTest); +PTF_TEST_CASE(InfiniBandCreationTest); diff --git a/Tests/Packet++Test/Tests/InfiniBandTests.cpp b/Tests/Packet++Test/Tests/InfiniBandTests.cpp index 237f830432..dbbcd7bc27 100644 --- a/Tests/Packet++Test/Tests/InfiniBandTests.cpp +++ b/Tests/Packet++Test/Tests/InfiniBandTests.cpp @@ -40,7 +40,7 @@ std::string getProtocolTypeAsString(pcpp::ProtocolType protocolType) } } -PTF_TEST_CASE(InfiniBandPacketParsing) +PTF_TEST_CASE(InfiniBandParsingTest) { timeval time; gettimeofday(&time, nullptr); @@ -113,10 +113,33 @@ PTF_TEST_CASE(InfiniBandPacketParsing) } default: { - std::cerr << "Something went wrong, couldn't find this layer" << std::endl; break; } } } +} // InfiniBandParsingTest -} // InfiniBandPacketParsing \ No newline at end of file +PTF_TEST_CASE(InfiniBandCreationTest) +{ + pcpp::EthLayer ethLayer1(pcpp::MacAddress("02:7d:fa:01:17:40"), pcpp::MacAddress("02:7d:fa:00:10:01"), + PCPP_ETHERTYPE_IP); + pcpp::IPv4Layer ipLayer1(pcpp::IPv4Address("192.168.0.1"), pcpp::IPv4Address("192.168.0.2")); + pcpp::UdpLayer udpLayer1(30502, 4791); + pcpp::InfiniBandLayer infinibandLayer1(12, 0, 0, 0, 65535, 17, 1, 5557091); + + pcpp::Packet infinibandPacket1(100); + PTF_ASSERT_TRUE(infinibandPacket1.addLayer(ðLayer1)); + PTF_ASSERT_TRUE(infinibandPacket1.addLayer(&ipLayer1)); + PTF_ASSERT_TRUE(infinibandPacket1.addLayer(&udpLayer1)); + PTF_ASSERT_TRUE(infinibandPacket1.addLayer(&infinibandLayer1)); + + PTF_ASSERT_EQUAL(infinibandPacket1.getLayerOfType()->getDataLen(), 20); + PTF_ASSERT_NOT_NULL(infinibandPacket1.getLayerOfType()); + PTF_ASSERT_EQUAL(udpLayer1.getDstPort(), 4791); + PTF_ASSERT_EQUAL(infinibandLayer1.getOpcode(), 12); + PTF_ASSERT_EQUAL(infinibandLayer1.getPkey(), 65535); + PTF_ASSERT_EQUAL(infinibandLayer1.getQpn(), 17); + PTF_ASSERT_EQUAL(infinibandLayer1.getAck(), 1); + PTF_ASSERT_EQUAL(infinibandLayer1.getPsn(), 5557091); + +} // InfiniBandCreationTest \ No newline at end of file diff --git a/Tests/Packet++Test/main.cpp b/Tests/Packet++Test/main.cpp index d5f58dd632..3e08d7d22e 100644 --- a/Tests/Packet++Test/main.cpp +++ b/Tests/Packet++Test/main.cpp @@ -341,7 +341,8 @@ int main(int argc, char* argv[]) PTF_RUN_TEST(WireGuardCreationTest, "wg"); PTF_RUN_TEST(WireGuardEditTest, "wg"); - PTF_RUN_TEST(InfiniBandPacketParsing, "ib"); + PTF_RUN_TEST(InfiniBandParsingTest, "ib"); + PTF_RUN_TEST(InfiniBandCreationTest, "ib"); PTF_END_RUNNING_TESTS; } From 9579f68f639315c33203c54d7fc25ff0a6d5b783 Mon Sep 17 00:00:00 2001 From: ycl <931415151@qq.com> Date: Mon, 14 Oct 2024 22:28:38 +0800 Subject: [PATCH 10/14] fix IB code format --- Packet++/header/InfiniBandLayer.h | 41 ++++++++++---------- Packet++/src/InfiniBandLayer.cpp | 4 +- Tests/Packet++Test/Tests/InfiniBandTests.cpp | 2 +- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Packet++/header/InfiniBandLayer.h b/Packet++/header/InfiniBandLayer.h index 248274d330..00817af5e9 100644 --- a/Packet++/header/InfiniBandLayer.h +++ b/Packet++/header/InfiniBandLayer.h @@ -83,7 +83,7 @@ namespace pcpp * @param[in] se The solicited event * @param[in] mig The migration state * @param[in] pad The pad count - * @param[in] pkey The parition key + * @param[in] pkey The partition key * @param[in] qpn The destination queue pair (QP) number * @param[in] ack_req The acknowledgment request * @param[in] psn The packet sequence number @@ -101,9 +101,9 @@ namespace pcpp return reinterpret_cast(m_Data); } - /** - * @return The operation code which defines the interpretation of the remaining header and payload bytes - */ + /** + * @return The operation code which defines the interpretation of the remaining header and payload bytes + */ uint8_t getOpcode() const; /** @@ -118,8 +118,8 @@ namespace pcpp uint8_t getSe() const; /** - * Set solicited evnet - * @param[in] se The solicited evnet to set + * Set solicited event + * @param[in] se The solicited event to set */ void setSe(int se) const; @@ -130,21 +130,22 @@ namespace pcpp /** * Set migreq - * @param[in] mig The migration state to set. If set to one, indicates the connection or EE context has been migrated; - * if set to zero, it means there is no change in the current migration state. + * @param[in] mig The migration state to set. If set to one, indicates the connection or EE context has been + * migrated; if set to zero, it means there is no change in the current migration state. */ void setMig(uint8_t mig) const; /** - * @return PadCount which Packet payloads are sent as a multiple of 4-byte quantities. + * @return PadCount which Packet payloads are sent as a multiple of 4-byte quantities. * Pad count indicates the number of pad bytes - 0 to 3 - that are appended to the packetpayload. - * Pads are used to “stretch” the payload (payloads may be zero or more bytes in length) to be a multiple of 4 bytes + * Pads are used to “stretch” the payload (payloads may be zero or more bytes in length) to be a multiple of 4 + * bytes */ uint8_t getPad() const; /** * Set PadCount - * @param[in] pad The PadCount to set + * @param[in] pad The PadCount to set */ void setPad(uint8_t pad) const; @@ -160,14 +161,14 @@ namespace pcpp void setTver(uint8_t tver) const; /** - * @return parition key identifing the partition + * @return partition key identifying the partition * that the destination QP (RC, UC, UD, XRC) or EE Context (RD) is a member. */ uint16_t getPkey() const; /** - * Set parition key - * @param[in] pkey The parition key to set + * Set partition key + * @param[in] pkey The partition key to set */ void setPkey(uint16_t pkey) const; @@ -184,7 +185,7 @@ namespace pcpp /** * @return FECN - * F (FECN): 0 indicates that a FECN indication was not received. + * F (FECN): 0 indicates that a FECN indication was not received. * 1 indicates that the packet went through a point of congestion */ int getFecn() const; @@ -211,7 +212,7 @@ namespace pcpp void setbecn(int becn) const; /** - * @return Reserved (variant) - 6 bits. Transmitted as 0, ignored on receive. + * @return Reserved (variant) - 6 bits. Transmitted as 0, ignored on receive. */ uint8_t getResv6a() const; @@ -232,13 +233,13 @@ namespace pcpp void setAck(int ack) const; /** - * Transmitted as 0, ignored on receive. + * Transmitted as 0, ignored on receive. */ void setResv7() const; /** - * @return packet sequence number that is used to identify the position of a packet - * within a sequence of packets. + * @return packet sequence number that is used to identify the position of a packet + * within a sequence of packets. */ uint32_t getPsn() const; @@ -275,7 +276,7 @@ namespace pcpp /** * A static method that check whether is inifiniband RoCE port - * @param[in] port The port from UDP destionation port + * @param[in] port The port from UDP destination port * @return True if the port is inifiniband RoCE and can represent an rxe packet */ static inline bool isInfiniBandPort(uint16_t port) diff --git a/Packet++/src/InfiniBandLayer.cpp b/Packet++/src/InfiniBandLayer.cpp index 41e74cb8a1..3378e535b8 100644 --- a/Packet++/src/InfiniBandLayer.cpp +++ b/Packet++/src/InfiniBandLayer.cpp @@ -52,7 +52,7 @@ namespace pcpp ss << "InfiniBand Layer, Opcode: " << getOpcode(); return ss.str(); } - + uint8_t InfiniBandLayer::getOpcode() const { return getBthHeader()->opcode; @@ -197,4 +197,4 @@ namespace pcpp getBthHeader()->apsn = htobe32((BTH_PSN_MASK & psn) | (~BTH_PSN_MASK & apsn)); } -} // namespace pcpp \ No newline at end of file +} // namespace pcpp diff --git a/Tests/Packet++Test/Tests/InfiniBandTests.cpp b/Tests/Packet++Test/Tests/InfiniBandTests.cpp index dbbcd7bc27..4b0524393f 100644 --- a/Tests/Packet++Test/Tests/InfiniBandTests.cpp +++ b/Tests/Packet++Test/Tests/InfiniBandTests.cpp @@ -142,4 +142,4 @@ PTF_TEST_CASE(InfiniBandCreationTest) PTF_ASSERT_EQUAL(infinibandLayer1.getAck(), 1); PTF_ASSERT_EQUAL(infinibandLayer1.getPsn(), 5557091); -} // InfiniBandCreationTest \ No newline at end of file +} // InfiniBandCreationTest From 342367e863c9690e465070f7e145e715a2728faf Mon Sep 17 00:00:00 2001 From: ycl <931415151@qq.com> Date: Tue, 15 Oct 2024 22:46:43 +0800 Subject: [PATCH 11/14] doxygen fix --- Packet++/header/InfiniBandLayer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Packet++/header/InfiniBandLayer.h b/Packet++/header/InfiniBandLayer.h index 00817af5e9..4e1c7c36b0 100644 --- a/Packet++/header/InfiniBandLayer.h +++ b/Packet++/header/InfiniBandLayer.h @@ -10,7 +10,7 @@ */ namespace pcpp { - /* + /** * IBA header types and methods * * Some of these are for reference and completeness only since From 09a9e92acb41e013087c234c71308672385431a3 Mon Sep 17 00:00:00 2001 From: ycl <931415151@qq.com> Date: Sat, 19 Oct 2024 22:48:38 +0800 Subject: [PATCH 12/14] IB code style fix --- 3rdParty/OUIDataset/PCPP_OUIDataset.json | 2 +- Packet++/CMakeLists.txt | 1 + Packet++/header/InfiniBandLayer.h | 67 +++++--------------- Packet++/header/ProtocolType.h | 4 +- Packet++/src/InfiniBandLayer.cpp | 19 +++++- Tests/Packet++Test/CMakeLists.txt | 2 +- Tests/Packet++Test/Tests/InfiniBandTests.cpp | 8 +-- 7 files changed, 44 insertions(+), 59 deletions(-) diff --git a/3rdParty/OUIDataset/PCPP_OUIDataset.json b/3rdParty/OUIDataset/PCPP_OUIDataset.json index ca6fd632ac..daca89e9ef 100644 --- a/3rdParty/OUIDataset/PCPP_OUIDataset.json +++ b/3rdParty/OUIDataset/PCPP_OUIDataset.json @@ -75756,7 +75756,7 @@ "vendor": "Lear" }, "8933597": { - "vendor": "Infiniband Trade Association" + "vendor": "InfiniBand Trade Association" }, "8933622": { "vendor": "Shenzhen Jingxun Software Telecommunication Technology Co.,Ltd" diff --git a/Packet++/CMakeLists.txt b/Packet++/CMakeLists.txt index 0c613f0999..b12596c39b 100644 --- a/Packet++/CMakeLists.txt +++ b/Packet++/CMakeLists.txt @@ -89,6 +89,7 @@ set(public_headers header/IcmpLayer.h header/IcmpV6Layer.h header/IgmpLayer.h + header/InfiniBandLayer.h header/IPLayer.h header/IPReassembly.h header/IPSecLayer.h diff --git a/Packet++/header/InfiniBandLayer.h b/Packet++/header/InfiniBandLayer.h index 4e1c7c36b0..51c789bc48 100644 --- a/Packet++/header/InfiniBandLayer.h +++ b/Packet++/header/InfiniBandLayer.h @@ -10,61 +10,28 @@ */ namespace pcpp { - /** - * IBA header types and methods - * - * Some of these are for reference and completeness only since - * rxe does not currently support RD transport - * most of this could be moved into Infiniband core. ib_pack.h has - * part of this but is incomplete - * - * Header specific routines to insert/extract values to/from headers - * the routines that are named __hhh_(set_)fff() take a pointer to a - * hhh header and get(set) the fff field. The routines named - * hhh_(set_)fff take a packet info struct and find the - * header and field based on the opcode in the packet. - * Conversion to/from network byte order from cpu order is also done. - */ - -#define RXE_ICRC_SIZE 4 -#define RXE_MAX_HDR_LENGTH 80 - - /** - * @struct bth - * Represents an Base Transport Header - */ -#pragma pack(push, 1) - struct rxe_bth - { - uint8_t opcode; - uint8_t flags; - uint16_t pkey; - uint32_t qpn; - uint32_t apsn; - }; -#pragma pack(pop) - -#define BTH_TVER 0x0 -#define BTH_DEF_PKEY 0xffff - -#define BTH_SE_MASK 0x80 -#define BTH_MIG_MASK 0x40 -#define BTH_PAD_MASK 0x30 -#define BTH_TVER_MASK 0x0f -#define BTH_FECN_MASK 0x80000000 -#define BTH_BECN_MASK 0x40000000 -#define BTH_RESV6A_MASK 0x3f000000 -#define BTH_QPN_MASK 0x00ffffff -#define BTH_ACK_MASK 0x80000000 -#define BTH_RESV7_MASK 0x7f000000 -#define BTH_PSN_MASK 0x00ffffff - /** * @class InfiniBandLayer * Represents an InfiniBand protocol layer */ class InfiniBandLayer : public Layer { + private: + /** + * @struct bth + * Represents an Base Transport Header + */ +#pragma pack(push, 1) + struct rxe_bth + { + uint8_t opcode; + uint8_t flags; + uint16_t pkey; + uint32_t qpn; + uint32_t apsn; + }; +#pragma pack(pop) + public: /** * A constructor that creates the layer from an existing packet raw data @@ -74,7 +41,7 @@ namespace pcpp * @param[in] packet A pointer to the Packet instance where layer will be stored in */ InfiniBandLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) - : Layer(data, dataLen, prevLayer, packet, Infiniband) + : Layer(data, dataLen, prevLayer, packet, InfiniBand) {} /** diff --git a/Packet++/header/ProtocolType.h b/Packet++/header/ProtocolType.h index 04b99821d7..1ba8ef314a 100644 --- a/Packet++/header/ProtocolType.h +++ b/Packet++/header/ProtocolType.h @@ -353,9 +353,9 @@ namespace pcpp const ProtocolType WireGuard = 56; /* - * Infiniband protocol + * InfiniBand protocol */ - const ProtocolType Infiniband = 57; + const ProtocolType InfiniBand = 57; /** * An enum representing OSI model layers diff --git a/Packet++/src/InfiniBandLayer.cpp b/Packet++/src/InfiniBandLayer.cpp index 3378e535b8..8b7230fc34 100644 --- a/Packet++/src/InfiniBandLayer.cpp +++ b/Packet++/src/InfiniBandLayer.cpp @@ -7,6 +7,23 @@ namespace pcpp { +#define RXE_ICRC_SIZE 4 +#define RXE_MAX_HDR_LENGTH 80 + +#define BTH_TVER 0x0 +#define BTH_DEF_PKEY 0xffff + +#define BTH_SE_MASK 0x80 +#define BTH_MIG_MASK 0x40 +#define BTH_PAD_MASK 0x30 +#define BTH_TVER_MASK 0x0f +#define BTH_FECN_MASK 0x80000000 +#define BTH_BECN_MASK 0x40000000 +#define BTH_RESV6A_MASK 0x3f000000 +#define BTH_QPN_MASK 0x00ffffff +#define BTH_ACK_MASK 0x80000000 +#define BTH_RESV7_MASK 0x7f000000 +#define BTH_PSN_MASK 0x00ffffff InfiniBandLayer::InfiniBandLayer(uint8_t opcode, int se, int mig, int pad, uint16_t pkey, uint32_t qpn, int ack_req, uint32_t psn) @@ -29,7 +46,7 @@ namespace pcpp if (ack_req) psn |= BTH_ACK_MASK; bthHdr->apsn = htobe32(psn); - m_Protocol = Infiniband; + m_Protocol = InfiniBand; } void InfiniBandLayer::parseNextLayer() diff --git a/Tests/Packet++Test/CMakeLists.txt b/Tests/Packet++Test/CMakeLists.txt index c54f410ea6..514b0cbb10 100644 --- a/Tests/Packet++Test/CMakeLists.txt +++ b/Tests/Packet++Test/CMakeLists.txt @@ -15,6 +15,7 @@ add_executable( Tests/IcmpTests.cpp Tests/IcmpV6Tests.cpp Tests/IgmpTests.cpp + Tests/InfiniBandTests.cpp Tests/IPSecTests.cpp Tests/IPv4Tests.cpp Tests/IPv6Tests.cpp @@ -39,7 +40,6 @@ add_executable( Tests/TcpTests.cpp Tests/TelnetTests.cpp Tests/TpktTests.cpp - Tests/InfiniBandTests.cpp Tests/VlanMplsTests.cpp Tests/VrrpTest.cpp Tests/WakeOnLanTests.cpp diff --git a/Tests/Packet++Test/Tests/InfiniBandTests.cpp b/Tests/Packet++Test/Tests/InfiniBandTests.cpp index 4b0524393f..9fc476aaed 100644 --- a/Tests/Packet++Test/Tests/InfiniBandTests.cpp +++ b/Tests/Packet++Test/Tests/InfiniBandTests.cpp @@ -28,8 +28,8 @@ std::string getProtocolTypeAsString(pcpp::ProtocolType protocolType) return "UDP"; case pcpp::TCP: return "TCP"; - case pcpp::Infiniband: - return "Infiniband"; + case pcpp::InfiniBand: + return "InfiniBand"; case pcpp::HTTPRequest: case pcpp::HTTPResponse: return "HTTP"; @@ -88,9 +88,9 @@ PTF_TEST_CASE(InfiniBandParsingTest) PTF_ASSERT_EQUAL(udpLayer->getDstPort(), 4791); break; } - case pcpp::Infiniband: + case pcpp::InfiniBand: { - // let's get the Infiniband layer + // let's get the InfiniBand layer auto* ibLayer = parsedPacket.getLayerOfType(); PTF_ASSERT_NOT_NULL(ibLayer); PTF_ASSERT_EQUAL(ibLayer->getOpcode(), 12); From 8a05842e837ce188cca82ea70894df8f13797ef1 Mon Sep 17 00:00:00 2001 From: ycl <931415151@qq.com> Date: Sat, 26 Oct 2024 22:33:02 +0800 Subject: [PATCH 13/14] IB add pcap --- Packet++/header/InfiniBandLayer.h | 54 ++++++++------- Packet++/src/InfiniBandLayer.cpp | 65 +++++++++++-------- Packet++/src/UdpLayer.cpp | 3 +- ...inibandPacket.dat => InfiniBandPacket.dat} | 0 Tests/Packet++Test/Tests/InfiniBandTests.cpp | 26 ++++---- 5 files changed, 83 insertions(+), 65 deletions(-) rename Tests/Packet++Test/PacketExamples/{InfinibandPacket.dat => InfiniBandPacket.dat} (100%) diff --git a/Packet++/header/InfiniBandLayer.h b/Packet++/header/InfiniBandLayer.h index 51c789bc48..9e0dc416ae 100644 --- a/Packet++/header/InfiniBandLayer.h +++ b/Packet++/header/InfiniBandLayer.h @@ -55,8 +55,8 @@ namespace pcpp * @param[in] ack_req The acknowledgment request * @param[in] psn The packet sequence number */ - InfiniBandLayer(uint8_t opcode, int se, int mig, int pad, uint16_t pkey, uint32_t qpn, int ack_req, - uint32_t psn); + InfiniBandLayer(uint8_t opcode, int soliciteEvent, int migrationState, int padCount, uint16_t partitionKey, + uint32_t queuePairNumber, int ackReq, uint32_t packetSequenceNumber); /** * Get a pointer to the BTH header. Notice this points directly to the data, so every change will change @@ -82,25 +82,25 @@ namespace pcpp /** * @return solicited event that the responder shall invoke the CQ event handler */ - uint8_t getSe() const; + uint8_t getSoliciteEvent() const; /** * Set solicited event * @param[in] se The solicited event to set */ - void setSe(int se) const; + void setSolicitedEvent(int se) const; /** * @return migreq which used to communicate migration state */ - uint8_t getMig() const; + uint8_t getMigrationState() const; /** * Set migreq * @param[in] mig The migration state to set. If set to one, indicates the connection or EE context has been * migrated; if set to zero, it means there is no change in the current migration state. */ - void setMig(uint8_t mig) const; + void setMigrationState(uint8_t mig) const; /** * @return PadCount which Packet payloads are sent as a multiple of 4-byte quantities. @@ -108,69 +108,69 @@ namespace pcpp * Pads are used to “stretch” the payload (payloads may be zero or more bytes in length) to be a multiple of 4 * bytes */ - uint8_t getPad() const; + uint8_t getPadCount() const; /** * Set PadCount * @param[in] pad The PadCount to set */ - void setPad(uint8_t pad) const; + void setPadCount(uint8_t pad) const; /** * @return Transport Header Version that specifies the version of the IBA Transport used for this packet */ - uint8_t getTver() const; + uint8_t getTransportHeaderVersion() const; /** * Set Transport Header Version * @param[in] tvr The transport header version to set */ - void setTver(uint8_t tver) const; + void setTransportHeaderVersion(uint8_t tver) const; /** * @return partition key identifying the partition * that the destination QP (RC, UC, UD, XRC) or EE Context (RD) is a member. */ - uint16_t getPkey() const; + uint16_t getPartitionKey() const; /** * Set partition key * @param[in] pkey The partition key to set */ - void setPkey(uint16_t pkey) const; + void setPartitionKey(uint16_t pkey) const; /** * @return destination queue pair (QP) identifier */ - uint32_t getQpn() const; + uint32_t getQueuePairNumber() const; /** * Set Queue Pair Number * @param[in] qpn The queue pair number to set */ - void setQpn(uint32_t qpn) const; + void setQueuePairNumber(uint32_t qpn) const; /** * @return FECN - * F (FECN): 0 indicates that a FECN indication was not received. - * 1 indicates that the packet went through a point of congestion + * F (FECN): false indicates that a FECN indication was not received. + * true indicates that the packet went through a point of congestion */ - int getFecn() const; + bool getFecn() const; /** * Set Fecn * @param[in] fecn The FECN to set */ - void setfecn(int fecn) const; + void setFecn(int fecn) const; /** * @return BECN - * B (BECN): 0 the packet did not go through a point of congestion or went - * through a point of congestion but was not marked. 1 indicates that the + * B (BECN): false the packet did not go through a point of congestion or went + * through a point of congestion but was not marked. true indicates that the * packet indicated by this header was subject to forward congestion. The B * bit is set in an ACK or CN BTH */ - int getBecn() const; + bool getBecn() const; /** * Set BECN @@ -208,13 +208,13 @@ namespace pcpp * @return packet sequence number that is used to identify the position of a packet * within a sequence of packets. */ - uint32_t getPsn() const; + uint32_t getPacketSequenceNumber() const; /** * Set packet sequence number * @param[in] psn The packet sequence number to set */ - void setPsn(uint32_t psn) const; + void setPacketSequenceNumber(uint32_t psn) const; /** * Currently identifies the following next layers sets to PayloadLayer @@ -250,6 +250,14 @@ namespace pcpp { return (port == 4791); } + + /** + * The static method makes validation of UDP data + * @param[in] udpData The pointer to the UDP payload data. It points to the first byte of rxe_bth header. + * @param[in] udpDataLen The payload data size + * @return True if the data is valid and can represent the rxe_bth packet + */ + static bool isDataValid(const uint8_t* udpData, size_t udpDataLen); }; } // namespace pcpp diff --git a/Packet++/src/InfiniBandLayer.cpp b/Packet++/src/InfiniBandLayer.cpp index 8b7230fc34..dfd4c04623 100644 --- a/Packet++/src/InfiniBandLayer.cpp +++ b/Packet++/src/InfiniBandLayer.cpp @@ -25,8 +25,8 @@ namespace pcpp #define BTH_RESV7_MASK 0x7f000000 #define BTH_PSN_MASK 0x00ffffff - InfiniBandLayer::InfiniBandLayer(uint8_t opcode, int se, int mig, int pad, uint16_t pkey, uint32_t qpn, int ack_req, - uint32_t psn) + InfiniBandLayer::InfiniBandLayer(uint8_t opcode, int soliciteEvent, int migrationState, int padCount, uint16_t partitionKey, + uint32_t queuePairNumber, int ackReq, uint32_t packetSequenceNumber) { const size_t headerLen = sizeof(rxe_bth); m_DataLen = headerLen; @@ -35,17 +35,17 @@ namespace pcpp rxe_bth* bthHdr = reinterpret_cast(m_Data); bthHdr->opcode = opcode; - bthHdr->flags = (pad << 4) & BTH_PAD_MASK; - if (se) + bthHdr->flags = (padCount << 4) & BTH_PAD_MASK; + if (soliciteEvent) bthHdr->flags |= BTH_SE_MASK; - if (mig) + if (migrationState) bthHdr->flags |= BTH_MIG_MASK; - bthHdr->pkey = htobe16(pkey); - bthHdr->qpn = htobe32(qpn & BTH_QPN_MASK); - psn &= BTH_PSN_MASK; - if (ack_req) - psn |= BTH_ACK_MASK; - bthHdr->apsn = htobe32(psn); + bthHdr->pkey = htobe16(partitionKey); + bthHdr->qpn = htobe32(queuePairNumber & BTH_QPN_MASK); + packetSequenceNumber &= BTH_PSN_MASK; + if (ackReq) + packetSequenceNumber |= BTH_ACK_MASK; + bthHdr->apsn = htobe32(packetSequenceNumber); m_Protocol = InfiniBand; } @@ -80,12 +80,12 @@ namespace pcpp getBthHeader()->opcode = opcode; } - uint8_t InfiniBandLayer::getSe() const + uint8_t InfiniBandLayer::getSoliciteEvent() const { return 0 != (BTH_SE_MASK & getBthHeader()->flags); } - void InfiniBandLayer::setSe(int se) const + void InfiniBandLayer::setSolicitedEvent(int se) const { if (se) getBthHeader()->flags |= BTH_SE_MASK; @@ -93,12 +93,12 @@ namespace pcpp getBthHeader()->flags &= ~BTH_SE_MASK; } - uint8_t InfiniBandLayer::getMig() const + uint8_t InfiniBandLayer::getMigrationState() const { return 0 != (BTH_MIG_MASK & getBthHeader()->flags); } - void InfiniBandLayer::setMig(uint8_t mig) const + void InfiniBandLayer::setMigrationState(uint8_t mig) const { if (mig) getBthHeader()->flags |= BTH_MIG_MASK; @@ -106,42 +106,42 @@ namespace pcpp getBthHeader()->flags &= ~BTH_MIG_MASK; } - uint8_t InfiniBandLayer::getPad() const + uint8_t InfiniBandLayer::getPadCount() const { return (BTH_PAD_MASK & getBthHeader()->flags) >> 4; } - void InfiniBandLayer::setPad(uint8_t pad) const + void InfiniBandLayer::setPadCount(uint8_t pad) const { getBthHeader()->flags = (BTH_PAD_MASK & (pad << 4)) | (~BTH_PAD_MASK & getBthHeader()->flags); } - uint8_t InfiniBandLayer::getTver() const + uint8_t InfiniBandLayer::getTransportHeaderVersion() const { return BTH_TVER_MASK & getBthHeader()->flags; } - void InfiniBandLayer::setTver(uint8_t tver) const + void InfiniBandLayer::setTransportHeaderVersion(uint8_t tver) const { getBthHeader()->flags = (BTH_TVER_MASK & tver) | (~BTH_TVER_MASK & getBthHeader()->flags); } - uint16_t InfiniBandLayer::getPkey() const + uint16_t InfiniBandLayer::getPartitionKey() const { return be16toh(getBthHeader()->pkey); } - void InfiniBandLayer::setPkey(uint16_t pkey) const + void InfiniBandLayer::setPartitionKey(uint16_t pkey) const { getBthHeader()->pkey = htobe16(pkey); } - uint32_t InfiniBandLayer::getQpn() const + uint32_t InfiniBandLayer::getQueuePairNumber() const { return BTH_QPN_MASK & be32toh(getBthHeader()->qpn); } - void InfiniBandLayer::setQpn(uint32_t qpn) const + void InfiniBandLayer::setQueuePairNumber(uint32_t qpn) const { uint32_t resvqpn = be32toh(getBthHeader()->qpn); @@ -149,12 +149,12 @@ namespace pcpp getBthHeader()->qpn = htobe32((BTH_QPN_MASK & qpn) | (~BTH_QPN_MASK & resvqpn)); } - int InfiniBandLayer::getFecn() const + bool InfiniBandLayer::getFecn() const { return 0 != (htobe32(BTH_FECN_MASK) & getBthHeader()->qpn); } - void InfiniBandLayer::setfecn(int fecn) const + void InfiniBandLayer::setFecn(int fecn) const { if (fecn) getBthHeader()->qpn |= htobe32(BTH_FECN_MASK); @@ -162,7 +162,7 @@ namespace pcpp getBthHeader()->qpn &= ~htobe32(BTH_FECN_MASK); } - int InfiniBandLayer::getBecn() const + bool InfiniBandLayer::getBecn() const { return 0 != (htobe32(BTH_BECN_MASK) & getBthHeader()->qpn); } @@ -203,15 +203,24 @@ namespace pcpp getBthHeader()->apsn &= ~htobe32(BTH_RESV7_MASK); } - uint32_t InfiniBandLayer::getPsn() const + uint32_t InfiniBandLayer::getPacketSequenceNumber() const { return BTH_PSN_MASK & be32toh(getBthHeader()->apsn); } - void InfiniBandLayer::setPsn(uint32_t psn) const + void InfiniBandLayer::setPacketSequenceNumber(uint32_t psn) const { uint32_t apsn = be32toh(getBthHeader()->apsn); getBthHeader()->apsn = htobe32((BTH_PSN_MASK & psn) | (~BTH_PSN_MASK & apsn)); } + + bool InfiniBandLayer::isDataValid(const uint8_t* udpData, size_t udpDataLen) + { + if (udpData != nullptr && udpDataLen >= sizeof(rxe_bth)) + { + return true; + } + return false; + } } // namespace pcpp diff --git a/Packet++/src/UdpLayer.cpp b/Packet++/src/UdpLayer.cpp index 57b635ff35..617dfa046b 100644 --- a/Packet++/src/UdpLayer.cpp +++ b/Packet++/src/UdpLayer.cpp @@ -142,7 +142,8 @@ namespace pcpp if (!m_NextLayer) m_NextLayer = new PayloadLayer(udpData, udpDataLen, this, m_Packet); } - else if (InfiniBandLayer::isInfiniBandPort(portDst)) + else if (InfiniBandLayer::isInfiniBandPort(portDst) && + InfiniBandLayer::isDataValid(udpData, udpDataLen)) m_NextLayer = new InfiniBandLayer(udpData, udpDataLen, this, m_Packet); else m_NextLayer = new PayloadLayer(udpData, udpDataLen, this, m_Packet); diff --git a/Tests/Packet++Test/PacketExamples/InfinibandPacket.dat b/Tests/Packet++Test/PacketExamples/InfiniBandPacket.dat similarity index 100% rename from Tests/Packet++Test/PacketExamples/InfinibandPacket.dat rename to Tests/Packet++Test/PacketExamples/InfiniBandPacket.dat diff --git a/Tests/Packet++Test/Tests/InfiniBandTests.cpp b/Tests/Packet++Test/Tests/InfiniBandTests.cpp index 9fc476aaed..e615e46ac4 100644 --- a/Tests/Packet++Test/Tests/InfiniBandTests.cpp +++ b/Tests/Packet++Test/Tests/InfiniBandTests.cpp @@ -45,7 +45,7 @@ PTF_TEST_CASE(InfiniBandParsingTest) timeval time; gettimeofday(&time, nullptr); - READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/InfinibandPacket.dat"); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/InfiniBandPacket.dat"); pcpp::Packet ip4Packet(&rawPacket1); @@ -94,17 +94,17 @@ PTF_TEST_CASE(InfiniBandParsingTest) auto* ibLayer = parsedPacket.getLayerOfType(); PTF_ASSERT_NOT_NULL(ibLayer); PTF_ASSERT_EQUAL(ibLayer->getOpcode(), 12); - PTF_ASSERT_EQUAL(ibLayer->getSe(), 0); - PTF_ASSERT_EQUAL(ibLayer->getMig(), 0); - PTF_ASSERT_EQUAL(ibLayer->getPad(), 0); - PTF_ASSERT_EQUAL(ibLayer->getTver(), 0); - PTF_ASSERT_EQUAL(ibLayer->getPkey(), 65535); - PTF_ASSERT_EQUAL(ibLayer->getQpn(), 17); - PTF_ASSERT_EQUAL(ibLayer->getFecn(), 0); - PTF_ASSERT_EQUAL(ibLayer->getBecn(), 0); + PTF_ASSERT_EQUAL(ibLayer->getSoliciteEvent(), 0); + PTF_ASSERT_EQUAL(ibLayer->getMigrationState(), 0); + PTF_ASSERT_EQUAL(ibLayer->getPadCount(), 0); + PTF_ASSERT_EQUAL(ibLayer->getTransportHeaderVersion(), 0); + PTF_ASSERT_EQUAL(ibLayer->getPartitionKey(), 65535); + PTF_ASSERT_EQUAL(ibLayer->getQueuePairNumber(), 17); + PTF_ASSERT_EQUAL(ibLayer->getFecn(), false); + PTF_ASSERT_EQUAL(ibLayer->getBecn(), false); PTF_ASSERT_EQUAL(ibLayer->getResv6a(), 0); PTF_ASSERT_EQUAL(ibLayer->getAck(), 1); - PTF_ASSERT_EQUAL(ibLayer->getPsn(), 5557091); + PTF_ASSERT_EQUAL(ibLayer->getPacketSequenceNumber(), 5557091); break; } case pcpp::GenericPayload: @@ -137,9 +137,9 @@ PTF_TEST_CASE(InfiniBandCreationTest) PTF_ASSERT_NOT_NULL(infinibandPacket1.getLayerOfType()); PTF_ASSERT_EQUAL(udpLayer1.getDstPort(), 4791); PTF_ASSERT_EQUAL(infinibandLayer1.getOpcode(), 12); - PTF_ASSERT_EQUAL(infinibandLayer1.getPkey(), 65535); - PTF_ASSERT_EQUAL(infinibandLayer1.getQpn(), 17); + PTF_ASSERT_EQUAL(infinibandLayer1.getPartitionKey(), 65535); + PTF_ASSERT_EQUAL(infinibandLayer1.getQueuePairNumber(), 17); PTF_ASSERT_EQUAL(infinibandLayer1.getAck(), 1); - PTF_ASSERT_EQUAL(infinibandLayer1.getPsn(), 5557091); + PTF_ASSERT_EQUAL(infinibandLayer1.getPacketSequenceNumber(), 5557091); } // InfiniBandCreationTest From e2d8f042ef44cb8af197b7cd718ae24a8e00a6fd Mon Sep 17 00:00:00 2001 From: ycl <931415151@qq.com> Date: Sun, 3 Nov 2024 21:59:04 +0800 Subject: [PATCH 14/14] fix IB argument type --- 3rdParty/OUIDataset/PCPP_OUIDataset.json | 2 +- Packet++/header/InfiniBandLayer.h | 64 +++++++++----------- Packet++/src/InfiniBandLayer.cpp | 28 +++------ Tests/Packet++Test/Tests/InfiniBandTests.cpp | 5 +- 4 files changed, 38 insertions(+), 61 deletions(-) diff --git a/3rdParty/OUIDataset/PCPP_OUIDataset.json b/3rdParty/OUIDataset/PCPP_OUIDataset.json index daca89e9ef..ca6fd632ac 100644 --- a/3rdParty/OUIDataset/PCPP_OUIDataset.json +++ b/3rdParty/OUIDataset/PCPP_OUIDataset.json @@ -75756,7 +75756,7 @@ "vendor": "Lear" }, "8933597": { - "vendor": "InfiniBand Trade Association" + "vendor": "Infiniband Trade Association" }, "8933622": { "vendor": "Shenzhen Jingxun Software Telecommunication Technology Co.,Ltd" diff --git a/Packet++/header/InfiniBandLayer.h b/Packet++/header/InfiniBandLayer.h index 9e0dc416ae..376b7c3d96 100644 --- a/Packet++/header/InfiniBandLayer.h +++ b/Packet++/header/InfiniBandLayer.h @@ -47,27 +47,17 @@ namespace pcpp /** * A constructor that creates a new rxe_bth header and allocates the data * @param[in] opcode The operation code - * @param[in] se The solicited event - * @param[in] mig The migration state - * @param[in] pad The pad count - * @param[in] pkey The partition key - * @param[in] qpn The destination queue pair (QP) number - * @param[in] ack_req The acknowledgment request - * @param[in] psn The packet sequence number + * @param[in] soliciteEvent The solicited event + * @param[in] migrationState The migration state + * @param[in] padCount The pad count + * @param[in] partitionKey The partition key + * @param[in] queuePairNumber The destination queue pair (QP) number + * @param[in] ackReq The acknowledgment request + * @param[in] packetSequenceNumber The packet sequence number */ InfiniBandLayer(uint8_t opcode, int soliciteEvent, int migrationState, int padCount, uint16_t partitionKey, uint32_t queuePairNumber, int ackReq, uint32_t packetSequenceNumber); - /** - * Get a pointer to the BTH header. Notice this points directly to the data, so every change will change - * the actual packet data - * @return A pointer to the bth_header - */ - rxe_bth* getBthHeader() const - { - return reinterpret_cast(m_Data); - } - /** * @return The operation code which defines the interpretation of the remaining header and payload bytes */ @@ -82,25 +72,25 @@ namespace pcpp /** * @return solicited event that the responder shall invoke the CQ event handler */ - uint8_t getSoliciteEvent() const; + bool getSoliciteEvent() const; /** * Set solicited event * @param[in] se The solicited event to set */ - void setSolicitedEvent(int se) const; + void setSolicitedEvent(bool se) const; /** * @return migreq which used to communicate migration state */ - uint8_t getMigrationState() const; + bool getMigrationState() const; /** * Set migreq * @param[in] mig The migration state to set. If set to one, indicates the connection or EE context has been * migrated; if set to zero, it means there is no change in the current migration state. */ - void setMigrationState(uint8_t mig) const; + void setMigrationState(bool mig) const; /** * @return PadCount which Packet payloads are sent as a multiple of 4-byte quantities. @@ -161,7 +151,7 @@ namespace pcpp * Set Fecn * @param[in] fecn The FECN to set */ - void setFecn(int fecn) const; + void setFecn(bool fecn) const; /** * @return BECN @@ -176,12 +166,7 @@ namespace pcpp * Set BECN * @param[in] becn The BECN to set */ - void setbecn(int becn) const; - - /** - * @return Reserved (variant) - 6 bits. Transmitted as 0, ignored on receive. - */ - uint8_t getResv6a() const; + void setBecn(bool becn) const; /** * Set Reserved 6 bits @@ -199,11 +184,6 @@ namespace pcpp */ void setAck(int ack) const; - /** - * Transmitted as 0, ignored on receive. - */ - void setResv7() const; - /** * @return packet sequence number that is used to identify the position of a packet * within a sequence of packets. @@ -217,7 +197,7 @@ namespace pcpp void setPacketSequenceNumber(uint32_t psn) const; /** - * Currently identifies the following next layers sets to PayloadLayer + * Identify the next layer as PayloadLayer */ void parseNextLayer() override; @@ -230,9 +210,10 @@ namespace pcpp } /** - * Calculate @ref udphdr#headerChecksum field + * Does nothing for this layer */ - void computeCalculateFields() override; + void computeCalculateFields() override + {} std::string toString() const override; @@ -258,6 +239,17 @@ namespace pcpp * @return True if the data is valid and can represent the rxe_bth packet */ static bool isDataValid(const uint8_t* udpData, size_t udpDataLen); + + private: + /** + * Get a pointer to the BTH header. Notice this points directly to the data, so every change will change + * the actual packet data + * @return A pointer to the bth_header + */ + rxe_bth* getBthHeader() const + { + return reinterpret_cast(m_Data); + } }; } // namespace pcpp diff --git a/Packet++/src/InfiniBandLayer.cpp b/Packet++/src/InfiniBandLayer.cpp index dfd4c04623..f527aa2d34 100644 --- a/Packet++/src/InfiniBandLayer.cpp +++ b/Packet++/src/InfiniBandLayer.cpp @@ -60,13 +60,10 @@ namespace pcpp m_NextLayer = new PayloadLayer(bthData, bthDataLen, this, m_Packet); } - void InfiniBandLayer::computeCalculateFields() - {} - std::string InfiniBandLayer::toString() const { std::ostringstream ss; - ss << "InfiniBand Layer, Opcode: " << getOpcode(); + ss << "InfiniBand Layer, Opcode: " << static_cast(getOpcode()); return ss.str(); } @@ -80,12 +77,12 @@ namespace pcpp getBthHeader()->opcode = opcode; } - uint8_t InfiniBandLayer::getSoliciteEvent() const + bool InfiniBandLayer::getSoliciteEvent() const { return 0 != (BTH_SE_MASK & getBthHeader()->flags); } - void InfiniBandLayer::setSolicitedEvent(int se) const + void InfiniBandLayer::setSolicitedEvent(bool se) const { if (se) getBthHeader()->flags |= BTH_SE_MASK; @@ -93,12 +90,12 @@ namespace pcpp getBthHeader()->flags &= ~BTH_SE_MASK; } - uint8_t InfiniBandLayer::getMigrationState() const + bool InfiniBandLayer::getMigrationState() const { return 0 != (BTH_MIG_MASK & getBthHeader()->flags); } - void InfiniBandLayer::setMigrationState(uint8_t mig) const + void InfiniBandLayer::setMigrationState(bool mig) const { if (mig) getBthHeader()->flags |= BTH_MIG_MASK; @@ -143,7 +140,6 @@ namespace pcpp void InfiniBandLayer::setQueuePairNumber(uint32_t qpn) const { - uint32_t resvqpn = be32toh(getBthHeader()->qpn); getBthHeader()->qpn = htobe32((BTH_QPN_MASK & qpn) | (~BTH_QPN_MASK & resvqpn)); @@ -154,7 +150,7 @@ namespace pcpp return 0 != (htobe32(BTH_FECN_MASK) & getBthHeader()->qpn); } - void InfiniBandLayer::setFecn(int fecn) const + void InfiniBandLayer::setFecn(bool fecn) const { if (fecn) getBthHeader()->qpn |= htobe32(BTH_FECN_MASK); @@ -167,7 +163,7 @@ namespace pcpp return 0 != (htobe32(BTH_BECN_MASK) & getBthHeader()->qpn); } - void InfiniBandLayer::setbecn(int becn) const + void InfiniBandLayer::setBecn(bool becn) const { if (becn) getBthHeader()->qpn |= htobe32(BTH_BECN_MASK); @@ -175,11 +171,6 @@ namespace pcpp getBthHeader()->qpn &= ~htobe32(BTH_BECN_MASK); } - uint8_t InfiniBandLayer::getResv6a() const - { - return (BTH_RESV6A_MASK & be32toh(getBthHeader()->qpn)) >> 24; - } - void InfiniBandLayer::setResv6a() const { getBthHeader()->qpn = htobe32(~BTH_RESV6A_MASK); @@ -198,11 +189,6 @@ namespace pcpp getBthHeader()->apsn &= ~htobe32(BTH_ACK_MASK); } - void InfiniBandLayer::setResv7() const - { - getBthHeader()->apsn &= ~htobe32(BTH_RESV7_MASK); - } - uint32_t InfiniBandLayer::getPacketSequenceNumber() const { return BTH_PSN_MASK & be32toh(getBthHeader()->apsn); diff --git a/Tests/Packet++Test/Tests/InfiniBandTests.cpp b/Tests/Packet++Test/Tests/InfiniBandTests.cpp index e615e46ac4..9e2506e55a 100644 --- a/Tests/Packet++Test/Tests/InfiniBandTests.cpp +++ b/Tests/Packet++Test/Tests/InfiniBandTests.cpp @@ -94,15 +94,14 @@ PTF_TEST_CASE(InfiniBandParsingTest) auto* ibLayer = parsedPacket.getLayerOfType(); PTF_ASSERT_NOT_NULL(ibLayer); PTF_ASSERT_EQUAL(ibLayer->getOpcode(), 12); - PTF_ASSERT_EQUAL(ibLayer->getSoliciteEvent(), 0); - PTF_ASSERT_EQUAL(ibLayer->getMigrationState(), 0); + PTF_ASSERT_EQUAL(ibLayer->getSoliciteEvent(), false); + PTF_ASSERT_EQUAL(ibLayer->getMigrationState(), false); PTF_ASSERT_EQUAL(ibLayer->getPadCount(), 0); PTF_ASSERT_EQUAL(ibLayer->getTransportHeaderVersion(), 0); PTF_ASSERT_EQUAL(ibLayer->getPartitionKey(), 65535); PTF_ASSERT_EQUAL(ibLayer->getQueuePairNumber(), 17); PTF_ASSERT_EQUAL(ibLayer->getFecn(), false); PTF_ASSERT_EQUAL(ibLayer->getBecn(), false); - PTF_ASSERT_EQUAL(ibLayer->getResv6a(), 0); PTF_ASSERT_EQUAL(ibLayer->getAck(), 1); PTF_ASSERT_EQUAL(ibLayer->getPacketSequenceNumber(), 5557091); break;