aboutsummaryrefslogtreecommitdiffstats
path: root/extras/hs-test/netconfig.go
blob: 5c8613c70a8019bf9e3917a9217f69b5060ebd00 (plain)
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package main

import (
	"errors"
	"fmt"
	"os/exec"
	"strings"

	"go.fd.io/govpp/binapi/ethernet_types"
	"go.fd.io/govpp/binapi/interface_types"
	"go.fd.io/govpp/binapi/ip_types"
)

type (
	Cmd                  = exec.Cmd
	MacAddress           = ethernet_types.MacAddress
	AddressWithPrefix    = ip_types.AddressWithPrefix
	IP4AddressWithPrefix = ip_types.IP4AddressWithPrefix
	InterfaceIndex       = interface_types.InterfaceIndex

	NetConfig interface {
		Configure() error
		Unconfigure()
		Name() string
		Type() string
	}

	NetConfigBase struct {
		name     string
		category string // what else to call this when `type` is reserved?
	}

	NetInterface struct {
		NetConfigBase
		addresser        *Addresser
		ip4Address       string // this will have form 10.10.10.1/24
		index            InterfaceIndex
		hwAddress        MacAddress
		networkNamespace string
		networkNumber    int
		peer             *NetInterface
	}

	NetworkNamespace struct {
		NetConfigBase
	}

	NetworkBridge struct {
		NetConfigBase
		networkNamespace string
		interfaces       []string
	}
)

const (
	NetNs  string = "netns"
	Veth   string = "veth"
	Tap    string = "tap"
	Bridge string = "bridge"
)

type InterfaceAdder func(n *NetInterface) *Cmd

var (
	ipCommandMap = map[string]InterfaceAdder{
		Veth: func(n *NetInterface) *Cmd {
			return exec.Command("ip", "link", "add", n.name, "type", "veth", "peer", "name", n.peer.name)
		},
		Tap: func(n *NetInterface) *Cmd {
			return exec.Command("ip", "tuntap", "add", n.name, "mode", "tap")
		},
	}
)

func NewNetworkInterface(cfg NetDevConfig, a *Addresser) (*NetInterface, error) {
	var newInterface *NetInterface = &NetInterface{}
	var err error
	newInterface.addresser = a
	newInterface.name = cfg["name"].(string)
	newInterface.networkNumber = defaultNetworkNumber

	if interfaceType, ok := cfg["type"]; ok {
		newInterface.category = interfaceType.(string)
	}

	if presetHwAddress, ok := cfg["preset-hw-address"]; ok {
		newInterface.hwAddress, err = ethernet_types.ParseMacAddress(presetHwAddress.(string))
		if err != nil {
			return &NetInterface{}, err
		}
	}

	if netns, ok := cfg["netns"]; ok {
		newInterface.networkNamespace = netns.(string)
	}

	if ip, ok := cfg["ip4"]; ok {
		if n, ok := ip.(NetDevConfig)["network"]; ok {
			newInterface.networkNumber = n.(int)
		}
		newInterface.ip4Address, err = newInterface.addresser.NewIp4Address(
			newInterface.networkNumber,
		)
		if err != nil {
			return &NetInterface{}, err
		}
	}

	if _, ok := cfg["peer"]; !ok {
		return newInterface, nil
	}

	peer := cfg["peer"].(NetDevConfig)

	if newInterface.peer, err = NewNetworkInterface(peer, a); err != nil {
		return &NetInterface{}, err
	}

	return newInterface, nil
}

func (n *NetInterface) ConfigureUpState() error {
	err := SetDevUp(n.Name(), "")
	if err != nil {
		return fmt.Errorf("set link up failed: %v", err)
	}
	return nil
}

func (n *NetInterface) ConfigureNetworkNamespace() error {
	if n.networkNamespace != "" {
		err := LinkSetNetns(n.name, n.networkNamespace)
		if err != nil {
			return err
		}
	}
	return nil
}

func (n *NetInterface) ConfigureAddress() error {
	if n.ip4Address != "" {
		if err := AddAddress(
			n.Name(),
			n.ip4Address,
			n.networkNamespace,
		); err != nil {
			return err
		}

	}
	return nil
}

func (n *NetInterface) Configure() error {
	cmd := ipCommandMap[n.Type()](n)
	_, err := cmd.CombinedOutput()
	if err != nil {
		return fmt.Errorf("creating interface '%v' failed: %v", n.Name(), err)
	}

	if err := n.ConfigureUpState(); err != nil {
		return err
	}

	if err := n.ConfigureNetworkNamespace(); err != nil {
		return err
	}

	if err := n.ConfigureAddress(); err != nil {
		return err
	}

	if n.peer != nil && n.peer.name != "" {
		if err := n.Peer().ConfigureUpState(); err != nil {
			return err
		}

		if err := n.Peer().ConfigureNetworkNamespace(); err != nil {
			return err
		}

		if err := n.Peer().ConfigureAddress(); err != nil {
			return err
		}
	}

	return nil
}

func (n *NetInterface) Unconfigure() {
	DelLink(n.name)
}

func (n *NetInterface) Name() string {
	return n.name
}

func (n *NetInterface) Type() string {
	return n.category
}

func (n *NetInterface) SetAddress(address string) {
	n.ip4Address = address
}

func (n *NetInterface) SetIndex(index InterfaceIndex) {
	n.index = index
}

func (n *NetInterface) Index() InterfaceIndex {
	return n.index
}

func (n *NetInterface) AddressWithPrefix() AddressWithPrefix {
	address, _ := ip_types.ParseAddressWithPrefix(n.ip4Address)
	return address
}

func (n *NetInterface) IP4AddressWithPrefix() IP4AddressWithPrefix {
	ip4Prefix, _ := ip_types.ParseIP4Prefix(n.ip4Address)
	ip4AddressWithPrefix := ip_types.IP4AddressWithPrefix(ip4Prefix)
	return ip4AddressWithPrefix
}

func (n *NetInterface) IP4AddressString() string {
	return strings.Split(n.ip4Address, "/")[0]
}

func (n *NetInterface) HwAddress() MacAddress {
	return n.hwAddress
}

func (n *NetInterface) Peer() *NetInterface {
	return n.peer
}

func (b *NetConfigBase) Name() string {
	return b.name
}

func (b *NetConfigBase) Type() string {
	return b.category
}

func NewNetNamespace(cfg NetDevConfig) (NetworkNamespace, error) {
	var networkNamespace NetworkNamespace
	networkNamespace.name = cfg["name"].(string)
	networkNamespace.category = NetNs
	return networkNamespace, nil
}

func (ns *NetworkNamespace) Configure() error {
	return addDelNetns(ns.name, true)
}

func (ns *NetworkNamespace) Unconfigure() {
	addDelNetns(ns.name, false)
}

func NewBridge(cfg NetDevConfig) (NetworkBridge, error) {
	var bridge NetworkBridge
	bridge.name = cfg["name"].(string)
	bridge.category = Bridge
	for _, v := range cfg["interfaces"].([]interface{}) {
		bridge.interfaces = append(bridge.interfaces, v.(string))
	}

	bridge.networkNamespace = ""
	if netns, ok := cfg["netns"]; ok {
		bridge.networkNamespace = netns.(string)
	}
	return bridge, nil
}

func (b *NetworkBridge) Configure() error {
	return AddBridge(b.name, b.interfaces, b.networkNamespace)
}

func (b *NetworkBridge) Unconfigure() {
	DelBridge(b.name, b.networkNamespace)
}

func DelBridge(brName, ns string) error {
	err := SetDevDown(brName, ns)
	if err != nil {
		return err
	}

	err = addDelBridge(brName, ns, false)
	if err != nil {
		return err
	}

	return nil
}

func SetDevUp(dev, ns string) error {
	return setDevUpDown(dev, ns, true)
}

func SetDevDown(dev, ns string) error {
	return setDevUpDown(dev, ns, false)
}

func DelLink(ifName string) {
	cmd := exec.Command("ip", "link", "del", ifName)
	cmd.Run()
}

func setDevUpDown(dev, ns string, isUp bool) error {
	var op string
	if isUp {
		op = "up"
	} else {
		op = "down"
	}
	c := []string{"ip", "link", "set", "dev", dev, op}
	cmd := appendNetns(c, ns)
	err := cmd.Run()
	if err != nil {
		s := fmt.Sprintf("error bringing %s device %s!", dev, op)
		return errors.New(s)
	}
	return nil
}

func addDelNetns(name string, isAdd bool) error {
	var op string
	if isAdd {
		op = "add"
	} else {
		op = "del"
	}
	cmd := exec.Command("ip", "netns", op, name)
	_, err := cmd.CombinedOutput()
	if err != nil {
		return errors.New("add/del netns failed")
	}
	return nil
}

func AddNetns(nsName string) error {
	return addDelNetns(nsName, true)
}

func DelNetns(nsName string) error {
	return addDelNetns(nsName, false)
}

func LinkSetNetns(ifName, ns string) error {
	cmd := exec.Command("ip", "link", "set", "dev", ifName, "up", "netns", ns)
	err := cmd.Run()
	if err != nil {
		return fmt.Errorf("error setting device '%s' to netns '%s: %v", ifName, ns, err)
	}
	return nil
}

func NewCommand(s []string, ns string) *exec.Cmd {
	return appendNetns(s, ns)
}

func appendNetns(s []string, ns string) *exec.Cmd {
	var cmd *exec.Cmd
	if ns == "" {
		// use default namespace
		cmd = exec.Command(s[0], s[1:]...)
	} else {
		var args = []string{"netns", "exec", ns}
		args = append(args, s[:]...)
		cmd = exec.Command("ip", args...)
	}
	return cmd
}

func addDelBridge(brName, ns string, isAdd bool) error {
	var op string
	if isAdd {
		op = "addbr"
	} else {
		op = "delbr"
	}
	var c = []string{"brctl", op, brName}
	cmd := appendNetns(c, ns)
	err := cmd.Run()
	if err != nil {
		s := fmt.Sprintf("%s %s failed!", op, brName)
		return errors.New(s)
	}
	return nil
}

func AddBridge(brName string, ifs []string, ns string) error {
	err := addDelBridge(brName, ns, true)
	if err != nil {
		return err
	}

	for _, v := range ifs {
		c := []string{"brctl", "addif", brName, v}
		cmd := appendNetns(c, ns)
		err = cmd.Run()
		if err != nil {
			s := fmt.Sprintf("error adding %s to bridge %s: %v", v, brName, err)
			return errors.New(s)
		}
	}
	err = SetDevUp(brName, ns)
	if err != nil {
		return err
	}
	return nil
}