/
config.go
139 lines (118 loc) · 3.21 KB
/
config.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
/*
* Cherry - An OpenFlow Controller
*
* Copyright (C) 2015 Samjung Data Service, Inc. All rights reserved.
* Kitae Kim <superkkt@sds.co.kr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package of13
import (
"encoding/binary"
"fmt"
"github.com/superkkt/cherry/openflow"
)
type Config struct {
err error
flags uint16
missSendLength uint16
}
func (r *Config) Flags() openflow.ConfigFlag {
switch r.flags {
case OFPC_FRAG_NORMAL:
return openflow.FragNormal
case OFPC_FRAG_DROP:
return openflow.FragDrop
case OFPC_FRAG_REASM:
return openflow.FragReasm
case OFPC_FRAG_MASK:
return openflow.FragMask
default:
panic(fmt.Sprintf("unexpected config flag: %v", r.flags))
}
}
func (r *Config) SetFlags(flags openflow.ConfigFlag) {
switch flags {
case openflow.FragNormal:
r.flags = OFPC_FRAG_NORMAL
case openflow.FragDrop:
r.flags = OFPC_FRAG_DROP
case openflow.FragReasm:
r.flags = OFPC_FRAG_REASM
case openflow.FragMask:
r.flags = OFPC_FRAG_MASK
default:
r.err = fmt.Errorf("SetFlags: unexpected config flag: %v", flags)
}
}
func (r *Config) MissSendLength() uint16 {
return r.missSendLength
}
func (r *Config) SetMissSendLength(length uint16) {
r.missSendLength = length
}
func (r *Config) Error() error {
return r.err
}
type SetConfig struct {
openflow.Message
Config
}
func NewSetConfig(xid uint32) openflow.SetConfig {
return &SetConfig{
Message: openflow.NewMessage(openflow.OF13_VERSION, OFPT_SET_CONFIG, xid),
Config: Config{
flags: OFPC_FRAG_NORMAL,
missSendLength: 0xFFFF,
},
}
}
func (r *SetConfig) MarshalBinary() ([]byte, error) {
if err := r.Error(); err != nil {
return nil, err
}
v := make([]byte, 4)
binary.BigEndian.PutUint16(v[0:2], r.flags)
binary.BigEndian.PutUint16(v[2:4], r.missSendLength)
r.SetPayload(v)
return r.Message.MarshalBinary()
}
type GetConfigRequest struct {
openflow.Message
}
func NewGetConfigRequest(xid uint32) openflow.GetConfigRequest {
return &GetConfigRequest{
Message: openflow.NewMessage(openflow.OF13_VERSION, OFPT_GET_CONFIG_REQUEST, xid),
}
}
func (r *GetConfigRequest) MarshalBinary() ([]byte, error) {
return r.Message.MarshalBinary()
}
type GetConfigReply struct {
openflow.Message
Config
}
func (r *GetConfigReply) UnmarshalBinary(data []byte) error {
if err := r.Message.UnmarshalBinary(data); err != nil {
return err
}
payload := r.Payload()
if payload == nil || len(payload) < 4 {
return openflow.ErrInvalidPacketLength
}
r.flags = binary.BigEndian.Uint16(payload[0:2])
r.missSendLength = binary.BigEndian.Uint16(payload[2:4])
return nil
}