Coverage Report

Created: 2026-03-31 07:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Packet++/header/VxlanLayer.h
Line
Count
Source
1
#pragma once
2
3
#include "Layer.h"
4
5
/// @file
6
7
/// @namespace pcpp
8
/// @brief The main namespace for the PcapPlusPlus lib
9
namespace pcpp
10
{
11
  /// @struct vxlan_header
12
  /// Represents a VXLAN protocol header
13
#pragma pack(push, 1)
14
  struct vxlan_header
15
  {
16
#if (BYTE_ORDER == LITTLE_ENDIAN)
17
    /// Reserved bits
18
    uint16_t reserved6_8 : 3;
19
    /// VNI present flag
20
    uint16_t vniPresentFlag : 1;
21
    /// Reserved bits
22
    uint16_t reserved2_4 : 3;
23
    /// GBP flag
24
    uint16_t gbpFlag : 1;
25
    /// Reserved bits
26
    uint16_t reserved14_16 : 3;
27
    /// Policy applied flag
28
    uint16_t policyAppliedFlag : 1;
29
    /// Reserved bits
30
    uint16_t reserved11_12 : 2;
31
    /// Don't learn flag
32
    uint16_t dontLearnFlag : 1;
33
    /// Reserved bits
34
    uint16_t reserved9 : 1;
35
#else
36
    /// Reserved bits
37
    uint16_t reserved9 : 1;
38
    /// Don't learn flag
39
    uint16_t dontLearnFlag : 1;
40
    /// Reserved bits
41
    uint16_t reserved11_12 : 2;
42
    /// Policy applied flag
43
    uint16_t policyAppliedFlag : 1;
44
    /// Reserved bits
45
    uint16_t reserved14_16 : 3;
46
    /// GBP flag
47
    uint16_t gbpFlag : 1;
48
    /// Reserved bits
49
    uint16_t reserved2_4 : 3;
50
    /// VNI present flag
51
    uint16_t vniPresentFlag : 1;
52
    /// Reserved bits
53
    uint16_t reserved6_8 : 3;
54
#endif
55
56
    /// Group Policy ID
57
    uint16_t groupPolicyID;
58
59
    /// VXLAN Network ID (VNI)
60
    uint32_t vni : 24;
61
    /// Reserved bits
62
    uint32_t pad : 8;
63
  };
64
#pragma pack(pop)
65
  static_assert(sizeof(vxlan_header) == 8, "vxlan_header size is not 8 bytes");
66
67
  /// @class VxlanLayer
68
  /// Represents a VXLAN (Virtual eXtensible Local Area Network) protocol layer
69
  class VxlanLayer : public Layer
70
  {
71
  public:
72
    /// A constructor that creates the layer from an existing packet raw data
73
    /// @param[in] data A pointer to the raw data
74
    /// @param[in] dataLen Size of the data in bytes
75
    /// @param[in] prevLayer A pointer to the previous layer
76
    /// @param[in] packet A pointer to the Packet instance where layer will be stored in
77
    VxlanLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
78
0
        : Layer(data, dataLen, prevLayer, packet, VXLAN)
79
0
    {}
80
81
    /// A constructor that creates a new VXLAN header and allocates the data. Note: the VNI present flag is set
82
    /// automatically
83
    /// @param[in] vni VNI (VXLAN Network ID) to set. Optional parameter (default is 0)
84
    /// @param[in] groupPolicyID Group Policy ID to set. Optional parameter (default is 0)
85
    /// @param[in] setGbpFlag Set GBP flag. Optional parameter (default is false)
86
    /// @param[in] setPolicyAppliedFlag Set Policy Applied flag. Optional parameter (default is false)
87
    /// @param[in] setDontLearnFlag Set Don't Learn flag. Optional parameter (default is false)
88
    explicit VxlanLayer(uint32_t vni = 0, uint16_t groupPolicyID = 0, bool setGbpFlag = false,
89
                        bool setPolicyAppliedFlag = false, bool setDontLearnFlag = false);
90
91
    ~VxlanLayer() override = default;
92
93
    /// A static method that validates the input data
94
    /// @param[in] data The pointer to the beginning of a byte stream of an Vxlan layer
95
    /// @param[in] dataLen The length of the byte stream
96
    /// @return True if the data is valid and can represent an Vxlan layer
97
    static bool isDataValid(uint8_t const* data, size_t dataLen)
98
0
    {
99
0
      return canReinterpretAs<vxlan_header>(data, dataLen);
100
0
    }
101
102
    /// Get a pointer to the VXLAN header. Notice this points directly to the data, so every change will change the
103
    /// actual packet data
104
    /// @return A pointer to the vxlan_header
105
    vxlan_header* getVxlanHeader() const
106
0
    {
107
0
      return reinterpret_cast<vxlan_header*>(m_Data);
108
0
    }
109
110
    /// @return The VXLAN Network ID (VNI) value
111
    uint32_t getVNI() const;
112
113
    /// Set VXLAN Network ID (VNI) value
114
    /// @param[in] vni VNI value to set
115
    void setVNI(uint32_t vni);
116
117
    /// A static method that checks whether the port is considered as VxLAN
118
    /// @param[in] port The port number to be checked
119
    static bool isVxlanPort(uint16_t port)
120
348k
    {
121
348k
      return port == 4789;
122
348k
    }
123
124
    // implement abstract methods
125
126
    /// Next layer for VXLAN is always Ethernet
127
    void parseNextLayer() override;
128
129
    /// @return Size of vxlan_header
130
    size_t getHeaderLen() const override
131
0
    {
132
0
      return sizeof(vxlan_header);
133
0
    }
134
135
    /// Does nothing for this layer
136
    void computeCalculateFields() override
137
0
    {}
138
139
    std::string toString() const override;
140
141
    OsiModelLayer getOsiModelLayer() const override
142
0
    {
143
0
      return OsiModelDataLinkLayer;
144
0
    }
145
  };
146
}  // namespace pcpp