forked from contiv/ofnet
-
Notifications
You must be signed in to change notification settings - Fork 16
/
ofTlvMap.go
168 lines (146 loc) · 4.22 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
158
159
160
161
162
163
164
165
166
167
168
package ofctrl
import (
"fmt"
"time"
"antrea.io/libOpenflow/openflow15"
)
type TLVTableStatus openflow15.TLVTableReply
type TLVStatusManager interface {
TLVMapReplyRcvd(ofSwitch *OFSwitch, status *TLVTableStatus)
}
func (t *TLVTableStatus) GetTLVMap(index uint16) *openflow15.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 *openflow15.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 *openflow15.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 = &openflow15.TLVTableMap{
OptClass: optClass,
OptType: optType,
OptLength: optLength,
Index: tunMetadataIndex,
}
tlvMaps := []*openflow15.TLVTableMap{
{
OptClass: optClass,
OptType: optType,
OptLength: optLength,
Index: tunMetadataIndex,
},
}
tlvMapMod := openflow15.NewTLVTableMod(openflow15.NXTTMC_ADD, tlvMaps)
msg := openflow15.NewTLVTableModMessage(tlvMapMod)
if err := s.Send(msg); err != nil {
return err
}
s.tlvMgr.status.AddTLVMap(tlvMap)
return nil
}
func (s *OFSwitch) DeleteTunnelTLVMap(tlvMaps []*openflow15.TLVTableMap) error {
tlvMapMod := openflow15.NewTLVTableMod(openflow15.NXTTMC_DELETE, tlvMaps)
msg := openflow15.NewTLVTableModMessage(tlvMapMod)
return s.Send(msg)
}
func (s *OFSwitch) ClearTunnelTLVMap(tlvMaps []*openflow15.TLVTableMap) error {
tlvMapMod := openflow15.NewTLVTableMod(openflow15.NXTTMC_CLEAR, tlvMaps)
msg := openflow15.NewTLVTableModMessage(tlvMapMod)
return s.Send(msg)
}
func ResetFieldLength(field *openflow15.MatchField, tlvMapStatus *TLVTableStatus) *openflow15.MatchField {
if tlvMapStatus == nil {
return field
}
if field.Class != openflow15.OXM_CLASS_NXM_1 {
return field
}
if field.Field < openflow15.NXM_NX_TUN_METADATA0 || field.Field > openflow15.NXM_NX_TUN_METADATA7 {
return field
}
index := field.Field - openflow15.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 := openflow15.NewTLVTableRequest()
var err error
err = s.Send(msg)
if err != nil {
return err
}
select {
case <-s.tlvMgr.tlvCh:
return nil
case <-time.After(messageTimeout):
err = fmt.Errorf("tlv-map request is not replied on switch %s in 10s", s.dpid.String())
case <-s.ctx.Done():
err = fmt.Errorf("cancel the wait for tlv-map reply because switch %s is diconnected", s.dpid.String())
}
return err
}
func newTLVMapMgr() *tlvMapMgr {
mgr := new(tlvMapMgr)
mgr.tlvCh = make(chan struct{}, 1)
return mgr
}