forked from contiv/ofnet
-
Notifications
You must be signed in to change notification settings - Fork 16
/
ofTlvMap.go
157 lines (137 loc) · 3.92 KB
/
ofTlvMap.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package ofctrl
import (
"fmt"
"antrea.io/libOpenflow/openflow13"
)
type TLVTableStatus openflow13.TLVTableReply
type TLVStatusManager interface {
TLVMapReplyRcvd(ofSwitch *OFSwitch, status *TLVTableStatus)
}
func (t *TLVTableStatus) GetTLVMap(index uint16) *openflow13.TLVTableMap {
for _, m := range t.TlvMaps {
if m.Index == index {
return m
}
}
return nil
}
func (t *TLVTableStatus) GetMaxSpace() uint32 {
return t.MaxSpace
}
func (t *TLVTableStatus) GetMaxFields() uint16 {
return t.MaxFields
}
func (t *TLVTableStatus) GetAllocatedResources() (space uint32, indexes []uint16) {
for _, m := range t.TlvMaps {
space += uint32(m.OptLength)
indexes = append(indexes, m.Index)
}
return
}
func (t *TLVTableStatus) String() string {
value := fmt.Sprintf("max option space=%d max field=%d\n", t.MaxSpace, t.MaxFields)
for _, m := range t.TlvMaps {
value = fmt.Sprintf("%s%s", value, t.GetTLVMapString(m))
}
return value
}
func (t *TLVTableStatus) GetTLVMapString(m *openflow13.TLVTableMap) string {
return fmt.Sprintf("TLVMap: class=0x%x,type=0x%x,length=0x%x,match_field=%d", m.OptClass, m.OptType, m.OptLength, m.Index)
}
func (t *TLVTableStatus) AddTLVMap(m *openflow13.TLVTableMap) {
t.TlvMaps = append(t.TlvMaps, m)
}
func (s *OFSwitch) AddTunnelTLVMap(optClass uint16, optType uint8, optLength uint8, tunMetadataIndex uint16) error {
tlvMap := s.tlvMgr.status.GetTLVMap(tunMetadataIndex)
if tlvMap != nil {
if tlvMap.OptClass != optClass {
return fmt.Errorf("another tlv-map is using the same tun_metadata with Class %d", tlvMap.OptClass)
}
if tlvMap.OptType != optType {
return fmt.Errorf("another tlv-map is using the same tun_metadata with Type %d", tlvMap.OptType)
}
if tlvMap.OptLength != optLength {
return fmt.Errorf("another tlv-map is using the same tun_metadata with Length: %d", tlvMap.OptLength)
}
return nil
}
tlvMap = &openflow13.TLVTableMap{
OptClass: optClass,
OptType: optType,
OptLength: optLength,
Index: tunMetadataIndex,
}
tlvMaps := []*openflow13.TLVTableMap{
{
OptClass: optClass,
OptType: optType,
OptLength: optLength,
Index: tunMetadataIndex,
},
}
tlvMapMod := openflow13.NewTLVTableMod(openflow13.NXTTMC_ADD, tlvMaps)
msg := openflow13.NewTLVTableModMessage(tlvMapMod)
if err := s.Send(msg); err != nil {
return err
}
s.tlvMgr.status.AddTLVMap(tlvMap)
return nil
}
func (s *OFSwitch) DeleteTunnelTLVMap(tlvMaps []*openflow13.TLVTableMap) error {
tlvMapMod := openflow13.NewTLVTableMod(openflow13.NXTTMC_DELETE, tlvMaps)
msg := openflow13.NewTLVTableModMessage(tlvMapMod)
return s.Send(msg)
}
func (s *OFSwitch) ClearTunnelTLVMap(tlvMaps []*openflow13.TLVTableMap) error {
tlvMapMod := openflow13.NewTLVTableMod(openflow13.NXTTMC_CLEAR, tlvMaps)
msg := openflow13.NewTLVTableModMessage(tlvMapMod)
return s.Send(msg)
}
func ResetFieldLength(field *openflow13.MatchField, tlvMapStatus *TLVTableStatus) *openflow13.MatchField {
if tlvMapStatus == nil {
return field
}
if field.Class != openflow13.OXM_CLASS_NXM_1 {
return field
}
if field.Field < openflow13.NXM_NX_TUN_METADATA0 || field.Field > openflow13.NXM_NX_TUN_METADATA7 {
return field
}
index := field.Field - openflow13.NXM_NX_TUN_METADATA0
m := tlvMapStatus.GetTLVMap(uint16(index))
if m != nil {
field.Length = m.OptLength
}
return field
}
func (s *OFSwitch) GetTLVMapTableStatus() *TLVTableStatus {
if s.tlvMgr == nil {
return nil
}
return s.tlvMgr.status
}
type tlvMapMgr struct {
status *TLVTableStatus
tlvCh chan struct{}
}
func (m *tlvMapMgr) TLVMapReplyRcvd(ofSwitch *OFSwitch, status *TLVTableStatus) {
m.status = status
m.tlvCh <- struct{}{}
}
func (s *OFSwitch) requestTlvMap() error {
if s.tlvMgr == nil {
return nil
}
msg := openflow13.NewTLVTableRequest()
err := s.Send(msg)
if err != nil {
return err
}
<-s.tlvMgr.tlvCh
return nil
}
func newTLVMapMgr() *tlvMapMgr {
mgr := new(tlvMapMgr)
mgr.tlvCh = make(chan struct{})
return mgr
}