summaryrefslogtreecommitdiffstats
path: root/src/common/Network/Packet/VLANHeader.inl
blob: a1c584c1c9efe867c879c24bd03eb497ab5148ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
Copyright (c) 2015-2015 Cisco Systems, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <common/BigEndianBitManipulation.h> 

inline  void   VLANHeader::setVlanTag(uint16_t data)
{
    myTag = PKT_HTONS(data);
}

inline  uint16_t VLANHeader::getVlanTag()
{
    return(PKT_HTONS(myTag));
}

inline  void  VLANHeader::setTagUserPriorty(uint8_t   argUserPriority)
{
    uint16_t  tempTag = myTag;
    setMaskBit16(tempTag, 0, 2, argUserPriority);
    myTag = tempTag;
}

inline  uint8_t VLANHeader::getTagUserPriorty()
{
    return (uint8_t)(getMaskBit16(myTag, 0, 2));
}


inline  void  VLANHeader::setTagCFI(bool    isSet)
{
    uint16_t  tempTag = myTag;
    setMaskBit16(tempTag, 3, 3, isSet? 1 : 0);
    myTag = tempTag;
}

inline  bool  VLANHeader::getTagCFI()
{
    return (getMaskBit16(myTag, 3, 3) == 1);
}

// This returns host order
inline  uint16_t VLANHeader::getTagID(void)
{
    return  getMaskBit16(myTag, 4, 15);
}

inline  void    VLANHeader::setTagID(uint16_t argNewTag)
{
    uint16_t  tempTag = myTag;
    setMaskBit16(tempTag, 4, 15, argNewTag);
    myTag = tempTag;
}

inline void  VLANHeader::incrementTagID(uint16_t inc_value)
{
    uint16_t  tempTag_Host   = myTag;
    uint16_t  curTagID_Host  = getTagID();
    uint16_t  newTagId_Host  = (0xfff & (curTagID_Host + (0xfff & inc_value))); // addition with 12 LSBits
    setMaskBit16(tempTag_Host, 4, 15, newTagId_Host);
    myTag = tempTag_Host;
}

inline  uint16_t VLANHeader::getNextProtocolNetOrder()
{
    return myNextProtocol;
}

inline  uint16_t VLANHeader::getNextProtocolHostOrder()
{
    return (PKT_HTONS(myNextProtocol));
}

inline  void    VLANHeader::setNextProtocolFromHostOrder(uint16_t  argNextProtocol)
{
    myNextProtocol = PKT_HTONS(argNextProtocol);
}

inline  void    VLANHeader::setNextProtocolFromNetOrder(uint16_t  argNextProtocol)
{
    myNextProtocol = argNextProtocol;
}

inline void     VLANHeader::setFromPkt          (uint8_t* data)
{
    // set the tag from the data
    setVlanTag(*(uint16_t*)data);
    setNextProtocolFromNetOrder(*((uint16_t*)(data + 2))); // next protocol is after the vlan tag
}

inline uint8_t    VLANHeader::reconstructPkt      (uint8_t* destBuff)
{
    *(uint16_t*)destBuff     = getVlanTag();
    *(uint16_t*)(destBuff+2) = getNextProtocolNetOrder();
    return sizeof(VLANHeader);
}