-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathveth.go
More file actions
394 lines (358 loc) · 10.8 KB
/
Copy pathveth.go
File metadata and controls
394 lines (358 loc) · 10.8 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
Copyright 2019 Yoichi Hariguchi
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 iproute
import (
"fmt"
"github.com/vishvananda/netlink"
"net"
)
type Veth struct {
Link netlink.Link
Peer netlink.Link
}
// VethGetLinkByName returns a pointer to netlink.Veth whose name is `name'
// in: name Name of veth interface
// return: 1. Pointer to netlink.Veth associated with `name'
// undetermined otherwise.
// 2. nil if there is a veth interface whose name is `name'
// non-nil otherwise
func VethGetLinkByName(name string) (*netlink.Veth, error) {
if l, err := netlink.LinkByName(name); err == nil {
switch l := l.(type) {
case *netlink.Veth:
return l, nil
default:
return nil, fmt.Errorf("VethGetLinkByName(%s): not veth", name)
}
} else {
return nil, fmt.Errorf("VethGetLinkByName(%s): %v", name, err)
}
}
// VethGetPeerLinkByName returns a pointer to netlink.Veth that is
// the peer of veth interface whose name is `name'
// in: name Name of veth interface
// return: 1. Pointer to netlink.Veth associated with the peer of `name'
// undetermined otherwise.
// 2. nil if there is the peer of veth interface whose name is `name'
// non-nil otherwise
func VethGetPeerLinkByName(name string) (*netlink.Veth, error) {
if l, err := netlink.LinkByName(name); err == nil {
switch l := l.(type) {
case *netlink.Veth:
if idx, err := netlink.VethPeerIndex(l); err == nil {
if l, err := netlink.LinkByIndex(idx); err == nil {
return l.(*netlink.Veth), nil
} else {
return nil, fmt.Errorf("LinkByIndex(%s): %v", name, err)
}
} else {
return nil, fmt.Errorf("VethPeerIndex(%s): %v", name, err)
}
default:
return nil,
fmt.Errorf("VethGetPeerLinkByName(): %s is not veth", name)
}
} else {
return nil, fmt.Errorf("VethGetPeerLinkByName(%s): %v", name, err)
}
}
// VethGetByName returns a pointer to Veth whose name is `name'
// in: name Name of veth interface
// return: 1. Pointer to Veth associated with `name'
// undetermined otherwise. `Veth.Peer' is nil
// in the case the peer belongs to a different network namespace
// 2. nil if there is a veth interface whose name is `name'
// non-nil otherwise
func VethGetByName(name string) (*Veth, error) {
var veth Veth
if l, err := VethGetLinkByName(name); err == nil {
veth.Link = l
} else {
return nil, fmt.Errorf("VethGetLinkByName(%s) %v", name, err)
}
if l, err := VethGetPeerLinkByName(name); err == nil {
veth.Peer = l
return &veth, nil
} else if IsNotFound(err) {
//
// the peer belongs to a different namespace
//
return &veth, nil
} else {
return nil, fmt.Errorf("VethGetPeerLinkByName(%s): %v", name, err)
}
}
// VethAdd adds a veth pair. The values of all fields except
// interface names are default
// in: name Name of veth
// peer Name of veth peer
// return 1. Pointer to Veth if success
// nil otherwise
// 2. nil if success
// non-nil otherwise
func VethAdd(name, peer string, up bool) (*Veth, error) {
var (
veth Veth
msg string
)
l := &netlink.Veth{
LinkAttrs: netlink.LinkAttrs{
Name: name,
TxQLen: DefaultTxQlen,
MTU: DefaultMTU,
NumTxQueues: DefaultTxQueues,
NumRxQueues: DefaultRxQueues,
},
PeerName: peer,
}
err := netlink.LinkAdd(l)
if err != nil {
return nil, err
}
if l, err := VethGetLinkByName(name); err == nil {
veth.Link = l
} else {
return nil, fmt.Errorf("VethGetLinkByName(%s): %v", name, err)
}
if l, err := VethGetPeerLinkByName(name); err == nil {
veth.Peer = l
} else {
return nil, fmt.Errorf("VethGetPeerLinkByName(%s): %v", name, err)
}
if up {
if err := netlink.LinkSetUp(veth.Link); err != nil {
msg = fmt.Sprintf("LinkSetUp(%s): %v", veth.Name(), err)
}
if err := netlink.LinkSetUp(veth.Peer); err != nil {
if msg != "" {
msg += ", "
}
msg = fmt.Sprintf("LinkSetUp(%s): %v", veth.PeerName(), err)
}
}
if msg == "" {
return &veth, nil
}
return &veth, fmt.Errorf(msg)
}
// VethDelete deletes the specified veth pair
// in: name Name of veth interface
// return: nil if success
// non-nil otherwise
func VethDelete(name string) error {
return LinkDel(name)
}
// VethIfExists returns true if veth `name' exists.
// It returns false otherwise.
// in: name Name of veth interface
// return: 1. true if veth `name' exists
// false otherwise
// 2. nil if success
// non-nil otherwise
func VethIfExists(name string) (bool, error) {
return ifExists(name, &netlink.Veth{})
}
// VethPeerIndex returns positive ifindex if veth `name' exists.
// It returns -1 otherwise.
// in: name Name of veth interface
// return: 1. Positive ifindex if veth `name' exists
// -1 otherwise
// 2. nil if success
// non-nil otherwise
func VethPeerIndex(name string) (int, error) {
veth, err := VethGetByName(name)
if err != nil {
return -1, err
}
return veth.PeerIndex()
}
// VethPeerName returns the peer name of veth `name' if exists.
// It returns empty string otherwise.
// in: name Name of veth interface
// return: 1. The name of peer interface of veth `name' if exists
// Empty string otherwise
// 2. nil if success
// non-nil otherwise
func VethPeerName(name string) (string, error) {
veth, err := VethGetByName(name)
if err != nil {
return "", err
}
return veth.PeerName(), nil
}
// SetNS bind veth `v' to network namespace `nsName'
// in: nsName Name of the network namespace to bind `v'
// return nil if success
// non-nil otherwise
func (v *Veth) SetNS(nsName string, up bool) error {
return IfSetNS(v.Name(), nsName)
}
// SetNSbyPid bind veth `v' to network namespace whose process ID is `pid'
// in: nsName Name of the network namespace to bind `v'
// return nil if success
// non-nil otherwise
func (v *Veth) SetNSbyPid(pid int) error {
return IfSetNSbyPid(v.Name(), pid)
}
// UnsetNS unbinds veth `v' from network namespace whose
// process ID is `pid'
// in: nsName Name of the network namespace to unbind `v'
// return nil if success
// non-nil otherwise
func (v *Veth) UnsetNS(nsName string) error {
return IfUnsetNS(v.Name(), nsName)
}
// IpAddrAdd adds an IP prefix to either this or peer interface.
// in: intf Add `addr' to this interface if true
// Add `addr' to the peer interface if false
// addr IP prefix (IPv4 or IPv6)
// up Bring up the interface if true
// Do nothing otherwise
// return: nil if success
// non-nil otherwise
func (v *Veth) IpAddrAdd(intf bool, addr *net.IPNet, up bool) error {
var l netlink.Link
if intf == Self {
l = v.Link
} else if v.Peer != nil {
l = v.Peer
} else {
fs := "IpAddrAdd(%s): peer belongs to a different namespace"
return fmt.Errorf(fs, v.Name())
}
if err := netlink.AddrAdd(l, &netlink.Addr{IPNet: addr}); err != nil {
return err
}
if up {
return netlink.LinkSetUp(l)
}
return nil
}
// IpAddrReplace replaces (or adds unless present) an IP prefix to
// either this or peer interface.
// in: intf Add `addr' to this interface if true
// Add `addr' to the peer interface if false
// addr IP prefix (IPv4 or IPv6)
// up Bring up the interface if true
// Do nothing otherwise
// return: nil if success
// non-nil otherwise
func (v *Veth) IpAddrReplace(intf bool, addr *net.IPNet, up bool) error {
var l netlink.Link
if intf == Self {
l = v.Link
} else if v.Peer != nil {
l = v.Peer
} else {
fs := "IpAddrReplace(%s): peer belongs to a different namespace"
return fmt.Errorf(fs, v.Name())
}
if err := netlink.AddrReplace(l, &netlink.Addr{IPNet: addr}); err != nil {
return err
}
if up {
return netlink.LinkSetUp(l)
}
return nil
}
// IpAddrAdd deletes the IP prefix from either this or peer interface.
// in: intf Delete `addr' from this interface if true
// Delete `addr' from the peer interface if false
// addr IP prefix (IPv4 or IPv6)
// return: nil if success
// non-nil otherwise
func (v *Veth) IpAddrDelete(intf bool, addr *net.IPNet) error {
var l netlink.Link
if intf == Self {
l = v.Link
} else if v.Peer != nil {
l = v.Peer
} else {
fs := "IpAddrDelete(%s): peer belongs to a different namespace"
return fmt.Errorf(fs, v.Name())
}
return netlink.AddrDel(l, &netlink.Addr{IPNet: addr})
}
// Index returns ifindex of this veth interface
func (v *Veth) Index() int {
return v.Link.Attrs().Index
}
// Name() returns the name of this veth interface
func (v *Veth) Name() string {
return v.Link.Attrs().Name
}
// PeerIndex returns ifindex of the peer of this veth interface
// return: 1. Peer's ifindex if success
// 2. nil if success
// non-nil otherwise
func (v *Veth) PeerIndex() (int, error) {
return netlink.VethPeerIndex(v.Link.(*netlink.Veth))
}
// Name() returns the name of the peer of this veth interface
func (v *Veth) PeerName() string {
if v.Peer == nil {
return ""
}
return v.Peer.Attrs().Name
}
// TxQLEN returns transmit queue length of this veth interface
func (v *Veth) TxQlen() int {
return v.Link.Attrs().TxQLen
}
// PeerTxQLEN returns transmit queue length of the peer of
// this veth interface if it is visible. Returns -1 otherwise.
func (v *Veth) PeerTxQlen() int {
if v.Peer == nil {
return -1
}
return v.Peer.Attrs().TxQLen
}
// MTU returns maximum transfer unit of this veth interface
func (v *Veth) MTU() int {
return v.Link.Attrs().MTU
}
// PeerMTU returns maximum transfer unit of the peer of
// this veth interface if it is visible. Returns -1 otherwise.
func (v *Veth) PeerMTU() int {
if v.Peer == nil {
return -1
}
return v.Peer.Attrs().MTU
}
// NtxQs returns theh number of transmit queues of this veth interface
func (v *Veth) NtxQs() int {
return v.Link.Attrs().NumTxQueues
}
// PeerNtxQs returns the number of transmit queues of the peer of
// this veth interface if it is visible. Returns -1 otherwise.
func (v *Veth) PeerNtxQs() int {
if v.Peer == nil {
return -1
}
return v.Peer.Attrs().NumTxQueues
}
// NrxQs returns theh number of receive queues of this veth interface
func (v *Veth) NrxQs() int {
if v.Peer == nil {
return -1
}
return v.Link.Attrs().NumRxQueues
}
// PeerNrxQs returns the number of receive queues of the peer of
// this veth interface if it is visible. Returns -1 otherwise.
func (v *Veth) PeerNrxQs() int {
if v.Peer == nil {
return -1
}
return v.Peer.Attrs().NumRxQueues
}