-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathclient.go
More file actions
275 lines (236 loc) · 6.82 KB
/
client.go
File metadata and controls
275 lines (236 loc) · 6.82 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2017 DigitalOcean.
//
// 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.
package ovsnl
import (
"fmt"
"os"
"strings"
"unsafe"
"github.com/digitalocean/go-openvswitch/ovsnl/internal/ovsh"
"github.com/mdlayher/genetlink"
"github.com/mdlayher/netlink"
"github.com/mdlayher/netlink/nlenc"
)
// Sizes of various structures, used in unsafe casts.
const (
sizeofHeader = int(unsafe.Sizeof(ovsh.Header{}))
sizeofDPStats = int(unsafe.Sizeof(ovsh.DPStats{}))
sizeofDPMegaflowStats = int(unsafe.Sizeof(ovsh.DPMegaflowStats{}))
sizeofVportStats = int(unsafe.Sizeof(ovsh.VportStats{}))
)
// A Client is a Linux Open vSwitch generic netlink client.
type Client struct {
// Datapath provides access to DatapathService methods.
Datapath *DatapathService
// Vport provides access to VportService methods.
Vport *VportService
c *genetlink.Conn
}
// New creates a new Linux Open vSwitch generic netlink client.
//
// If no OvS generic netlink families are available on this system, an
// error will be returned which can be checked using os.IsNotExist.
func New() (*Client, error) {
c, err := genetlink.Dial(nil)
if err != nil {
return nil, err
}
return newClient(c)
}
// newClient is the internal Client constructor, used in tests.
func newClient(c *genetlink.Conn) (*Client, error) {
// Must ensure that the generic netlink connection is closed on any errors
// that occur before it is returned to the caller.
families, err := c.ListFamilies()
if err != nil {
_ = c.Close()
return nil, err
}
client := &Client{c: c}
if err := client.init(families); err != nil {
_ = c.Close()
return nil, err
}
return client, nil
}
// Close closes the Client's generic netlink connection.
func (c *Client) Close() error {
return c.c.Close()
}
// init initializes the generic netlink family service of Client.
func (c *Client) init(families []genetlink.Family) error {
var gotf int
for _, f := range families {
// Ignore any families without the OVS prefix.
if !strings.HasPrefix(f.Name, "ovs_") {
continue
}
// Ignore any families that might be unknown.
if err := c.initFamily(f); err != nil {
continue
}
gotf++
}
// No known families; return error for os.IsNotExist check.
if gotf == 0 {
return os.ErrNotExist
}
return nil
}
// initFamily initializes a single generic netlink family service.
func (c *Client) initFamily(f genetlink.Family) error {
switch f.Name {
case ovsh.DatapathFamily:
c.Datapath = &DatapathService{
f: f,
c: c,
}
return nil
case ovsh.VportFamily:
c.Vport = &VportService{
c: c,
f: f,
}
return nil
default:
// Unknown OVS netlink family, nothing we can do.
return fmt.Errorf("unknown OVS generic netlink family: %q", f.Name)
}
}
// headerBytes converts an ovsh.Header into a byte slice.
func headerBytes(h ovsh.Header) []byte {
b := *(*[sizeofHeader]byte)(unsafe.Pointer(&h))
return b[:]
}
// parseHeader converts a byte slice into ovsh.Header.
func parseHeader(b []byte) (ovsh.Header, error) {
// Verify that the byte slice is long enough before doing unsafe casts.
if l := len(b); l < sizeofHeader {
return ovsh.Header{}, fmt.Errorf("not enough data for OVS message header: %d bytes", l)
}
h := *(*ovsh.Header)(unsafe.Pointer(&b[:sizeofHeader][0]))
return h, nil
}
// NlMsgBuilder to build genetlink message
type NlMsgBuilder struct {
msg *genetlink.Message
}
// NewNlMsgBuilder construct a netlink message builder with genetlink.Message
func NewNlMsgBuilder() *NlMsgBuilder {
return &NlMsgBuilder{msg: &genetlink.Message{}}
}
// PutGenlMsgHdr set msg header with genetlink.Header
func (nlmsg *NlMsgBuilder) PutGenlMsgHdr(command, version uint8) {
nlmsg.msg.Header = genetlink.Header{
Command: command,
Version: version,
}
}
// PutOvsHeader set ovs header with ovsh.Header
func (nlmsg *NlMsgBuilder) PutOvsHeader(dpID int32) {
nlmsg.msg.Data = headerBytes(ovsh.Header{Ifindex: dpID})
}
// PutStringAttr put attribute with string value
func (nlmsg *NlMsgBuilder) PutStringAttr(typ uint16, value string) error {
attrs := []netlink.Attribute{
{
Type: typ,
Data: nlenc.Bytes(value),
},
}
attr, err := netlink.MarshalAttributes(attrs)
if err != nil {
return fmt.Errorf("marshal string attributes failed:%s", err)
}
nlmsg.msg.Data = append(nlmsg.msg.Data[:], attr...)
return nil
}
// PutUint8Attr put attribute with uint8 value
func (nlmsg *NlMsgBuilder) PutUint8Attr(typ uint16, value uint8) error {
attrs := []netlink.Attribute{
{
Type: typ,
Data: nlenc.Uint8Bytes(value),
},
}
attr, err := netlink.MarshalAttributes(attrs)
if err != nil {
return fmt.Errorf("marshal uint8 attributes failed:%s", err)
}
nlmsg.msg.Data = append(nlmsg.msg.Data[:], attr...)
return nil
}
// PutUint16Attr put attribute with uint16 value
func (nlmsg *NlMsgBuilder) PutUint16Attr(typ uint16, value uint16) error {
attrs := []netlink.Attribute{
{
Type: typ,
Data: nlenc.Uint16Bytes(value),
},
}
attr, err := netlink.MarshalAttributes(attrs)
if err != nil {
return fmt.Errorf("marshal uint16 attributes failed:%s", err)
}
nlmsg.msg.Data = append(nlmsg.msg.Data[:], attr...)
return nil
}
// PutUint32Attr put attribute with uint32 value
func (nlmsg *NlMsgBuilder) PutUint32Attr(typ uint16, value uint32) error {
attrs := []netlink.Attribute{
{
Type: typ,
Data: nlenc.Uint32Bytes(value),
},
}
attr, err := netlink.MarshalAttributes(attrs)
if err != nil {
return fmt.Errorf("marshal uint32 attributes failed:%s", err)
}
nlmsg.msg.Data = append(nlmsg.msg.Data[:], attr...)
return nil
}
// PutSliceAttr put attribute with slice byte value
func (nlmsg *NlMsgBuilder) PutSliceAttr(typ uint16, value []byte) error {
attrs := []netlink.Attribute{
{
Type: typ,
Data: value,
},
}
attr, err := netlink.MarshalAttributes(attrs)
if err != nil {
return fmt.Errorf("marshal slice byte attributes failed:%s", err)
}
nlmsg.msg.Data = append(nlmsg.msg.Data[:], attr...)
return nil
}
// PutEmptyAttr put attribute with empty value
func (nlmsg *NlMsgBuilder) PutEmptyAttr(typ uint16) error {
attrs := []netlink.Attribute{
{
Type: typ,
},
}
attr, err := netlink.MarshalAttributes(attrs)
if err != nil {
return fmt.Errorf("marshal empty attributes failed:%s", err)
}
nlmsg.msg.Data = append(nlmsg.msg.Data[:], attr...)
return nil
}
// Message generic netlink message
func (nlmsg *NlMsgBuilder) Message() genetlink.Message {
return *nlmsg.msg
}