PcapPlusPlus  Next
VxlanLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 
6 
9 namespace pcpp
10 {
13 #pragma pack(push, 1)
14  struct vxlan_header
15  {
16 #if (BYTE_ORDER == LITTLE_ENDIAN)
18  uint16_t reserved6_8 : 3;
20  uint16_t vniPresentFlag : 1;
22  uint16_t reserved2_4 : 3;
24  uint16_t gbpFlag : 1;
26  uint16_t reserved14_16 : 3;
28  uint16_t policyAppliedFlag : 1;
30  uint16_t reserved11_12 : 2;
32  uint16_t dontLearnFlag : 1;
34  uint16_t reserved9 : 1;
35 #else
37  uint16_t reserved9 : 1;
39  uint16_t dontLearnFlag : 1;
41  uint16_t reserved11_12 : 2;
43  uint16_t policyAppliedFlag : 1;
45  uint16_t reserved14_16 : 3;
47  uint16_t gbpFlag : 1;
49  uint16_t reserved2_4 : 3;
51  uint16_t vniPresentFlag : 1;
53  uint16_t reserved6_8 : 3;
54 #endif
55 
57  uint16_t groupPolicyID;
58 
60  uint32_t vni : 24;
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 
69  class VxlanLayer : public Layer
70  {
71  public:
77  VxlanLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
78  : Layer(data, dataLen, prevLayer, packet, VXLAN)
79  {}
80 
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 
97  static bool isDataValid(uint8_t const* data, size_t dataLen)
98  {
99  return canReinterpretAs<vxlan_header>(data, dataLen);
100  }
101 
106  {
107  return reinterpret_cast<vxlan_header*>(m_Data);
108  }
109 
111  uint32_t getVNI() const;
112 
115  void setVNI(uint32_t vni);
116 
119  static bool isVxlanPort(uint16_t port)
120  {
121  return port == 4789;
122  }
123 
124  // implement abstract methods
125 
127  void parseNextLayer() override;
128 
130  size_t getHeaderLen() const override
131  {
132  return sizeof(vxlan_header);
133  }
134 
136  void computeCalculateFields() override
137  {}
138 
139  std::string toString() const override;
140 
142  {
143  return OsiModelDataLinkLayer;
144  }
145 
150  {
152  static std::vector<FieldDescriptor> all()
153  {
154  auto result = Layer::SerializedFields::all();
155  std::initializer_list<FieldDescriptor> extra{ ValidVNI, GroupPolicyID, VNI };
156  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
157  return result;
158  }
159 
161  static const FieldDescriptor ValidVNI;
162 
165 
167  static const FieldDescriptor VNI;
168  };
169 
170  protected:
171  void serializeLayer(ObjectScope& serializer) const override;
172  };
173 } // namespace pcpp
Definition: Layer.h:117
Definition: Serializers.h:230
Definition: Packet.h:49
Definition: VxlanLayer.h:70
uint32_t getVNI() const
static bool isDataValid(uint8_t const *data, size_t dataLen)
Definition: VxlanLayer.h:97
void computeCalculateFields() override
Does nothing for this layer.
Definition: VxlanLayer.h:136
std::string toString() const override
vxlan_header * getVxlanHeader() const
Definition: VxlanLayer.h:105
void parseNextLayer() override
Next layer for VXLAN is always Ethernet.
OsiModelLayer getOsiModelLayer() const override
Definition: VxlanLayer.h:141
VxlanLayer(uint32_t vni=0, uint16_t groupPolicyID=0, bool setGbpFlag=false, bool setPolicyAppliedFlag=false, bool setDontLearnFlag=false)
static bool isVxlanPort(uint16_t port)
Definition: VxlanLayer.h:119
void serializeLayer(ObjectScope &serializer) const override
size_t getHeaderLen() const override
Definition: VxlanLayer.h:130
void setVNI(uint32_t vni)
VxlanLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: VxlanLayer.h:77
const ProtocolType VXLAN
VXLAN protocol.
Definition: ProtocolType.h:138
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:420
@ OsiModelDataLinkLayer
Data link layer (layer 2)
Definition: ProtocolType.h:424
Definition: Serializers.h:18
Definition: Layer.h:212
static std::vector< FieldDescriptor > all()
Definition: Layer.h:214
Definition: VxlanLayer.h:150
static const FieldDescriptor ValidVNI
True if the "valid VNI" flag (I bit) is set.
Definition: VxlanLayer.h:161
static const FieldDescriptor GroupPolicyID
Group Policy ID (VXLAN-GBP extension)
Definition: VxlanLayer.h:164
static std::vector< FieldDescriptor > all()
Definition: VxlanLayer.h:152
static const FieldDescriptor VNI
VXLAN Network Identifier (24 bits)
Definition: VxlanLayer.h:167
Definition: VxlanLayer.h:15
uint16_t policyAppliedFlag
Policy applied flag.
Definition: VxlanLayer.h:28
uint16_t gbpFlag
GBP flag.
Definition: VxlanLayer.h:24
uint16_t reserved11_12
Reserved bits.
Definition: VxlanLayer.h:30
uint32_t vni
VXLAN Network ID (VNI)
Definition: VxlanLayer.h:60
uint16_t reserved14_16
Reserved bits.
Definition: VxlanLayer.h:26
uint16_t vniPresentFlag
VNI present flag.
Definition: VxlanLayer.h:20
uint16_t reserved9
Reserved bits.
Definition: VxlanLayer.h:34
uint16_t dontLearnFlag
Don't learn flag.
Definition: VxlanLayer.h:32
uint16_t groupPolicyID
Group Policy ID.
Definition: VxlanLayer.h:57
uint32_t pad
Reserved bits.
Definition: VxlanLayer.h:62
uint16_t reserved6_8
Reserved bits.
Definition: VxlanLayer.h:18
uint16_t reserved2_4
Reserved bits.
Definition: VxlanLayer.h:22