-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetns.go
140 lines (116 loc) · 3.79 KB
/
netns.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
/*
* vc5/xvs load balancer. Copyright (C) 2021-present David Coles
*
* 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
* (at your option) 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 xvs
import (
"fmt"
"net"
"os/exec"
"time"
"github.com/davidcoles/xvs/xdp"
)
type ip4 [4]byte
type mac [6]byte
func (i *ip4) String() string { return b4s(*i) }
func (m *mac) String() string { return b6s(*m) }
type netns struct {
a, b nic
ns string
}
func (n *netns) namespace() string { return n.ns }
func (n *netns) addr() [4]byte { return n.b.ip4 }
func (n *netns) mac() [6]byte { return n.b.mac }
func (n *netns) xthis() [6]byte { return n.a.mac }
func (n *netns) id() uint32 { return uint32(n.a.idx) }
func (n *netns) nat(i uint16) ip4 { return ip4{n.a.ip4[0], n.a.ip4[1], byte(i >> 8), byte(i & 0xff)} }
func nat(x *xdp.XDP, fwd string, pass string) (*netns, error) {
namespace := "vc5"
var n netns
n.ns = namespace
n.a.nic = namespace
n.b.nic = namespace + "ns"
var ip ip4 = ip4{10, 255, 255, 254}
n.a.ip4 = [4]byte{ip[0], ip[1], 255, 253}
n.b.ip4 = [4]byte{ip[0], ip[1], 255, 254}
if err := create_pair(n.a.nic, n.b.nic); err != nil {
return nil, err
}
time.Sleep(time.Second * 1) // TODO race condition with assigned MACs
if iface, err := net.InterfaceByName(n.a.nic); err != nil {
return nil, err
} else {
copy(n.a.mac[:], iface.HardwareAddr[:])
n.a.idx = iface.Index
}
if iface, err := net.InterfaceByName(n.b.nic); err != nil {
return nil, err
} else {
copy(n.b.mac[:], iface.HardwareAddr[:])
n.b.idx = iface.Index
}
if err := x.LoadBpfSection(fwd, false, uint32(n.a.idx)); err != nil {
return nil, err
}
// this seems to be needed to make native mode hardware work
if err := x.LoadBpfSection(pass, true, uint32(n.b.idx)); err != nil {
return nil, err
}
if err := config_pair(n.ns, n.a, n.b); err != nil {
return nil, err
}
return &n, nil
}
func (n *netns) clean() {
script := `
ip link del ` + n.a.nic + ` >/dev/null 2>&1 || true
ip netns del ` + n.ns + ` >/dev/null 2>&1 || true
`
exec.Command("/bin/sh", "-e", "-c", script).Output()
}
func create_pair(if1, if2 string) error {
script := `
ip link del ` + if1 + ` >/dev/null 2>&1 || true
ip link add ` + if1 + ` type veth peer name ` + if2 + `
`
if _, err := exec.Command("/bin/sh", "-e", "-c", script).Output(); err != nil {
return fmt.Errorf("Error creating netns pair: %s", err.Error())
}
return nil
}
// can set mac: ip l set vc5 addr 26:7c:d6:2c:d9:32
func config_pair(ns string, a, b nic) error {
ip1 := a.ip4.String()
ip2 := b.ip4.String()
cb := a.ip4
cb[2] = 0
cb[3] = 0
cbs := cb.String()
script := `
ip netns del ` + ns + ` >/dev/null 2>&1 || true
ip l set ` + a.nic + ` up
ip a add ` + ip1 + `/30 dev ` + a.nic + `
ip netns add ` + ns + `
ip link set ` + b.nic + ` netns ` + ns + `
ip netns exec vc5 /bin/sh -c "ip l set ` + b.nic + ` up && ip a add ` + ip2 + `/30 dev ` + b.nic + ` && ip r replace default via ` + ip1 +
` && ip netns exec ` + ns + ` ethtool -K ` + b.nic + ` tx off"
ip r replace ` + cbs + `/16 via ` + ip2 + `
`
if _, err := exec.Command("/bin/sh", "-e", "-c", script).Output(); err != nil {
return fmt.Errorf("Error seting up netns: %s", err.Error())
}
return nil
}