Skip to content

Commit fe3d5c4

Browse files
committed
Remove unused veth setup code
Networking is setup by plugins for users of runc so it makes sense to get rid of the veth strategy. Signed-off-by: Mrunal Patel <[email protected]>
1 parent 20aff4f commit fe3d5c4

File tree

3 files changed

+0
-194
lines changed

3 files changed

+0
-194
lines changed

libcontainer/network_linux.go

Lines changed: 0 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@ package libcontainer
55
import (
66
"fmt"
77
"io/ioutil"
8-
"net"
98
"path/filepath"
109
"strconv"
1110
"strings"
1211

1312
"github.com/opencontainers/runc/libcontainer/configs"
14-
"github.com/opencontainers/runc/libcontainer/utils"
1513
"github.com/vishvananda/netlink"
1614
)
1715

1816
var strategies = map[string]networkStrategy{
19-
"veth": &veth{},
2017
"loopback": &loopback{},
2118
}
2219

@@ -103,157 +100,3 @@ func (l *loopback) attach(n *configs.Network) (err error) {
103100
func (l *loopback) detach(n *configs.Network) (err error) {
104101
return nil
105102
}
106-
107-
// veth is a network strategy that uses a bridge and creates
108-
// a veth pair, one that is attached to the bridge on the host and the other
109-
// is placed inside the container's namespace
110-
type veth struct {
111-
}
112-
113-
func (v *veth) detach(n *configs.Network) (err error) {
114-
return netlink.LinkSetMaster(&netlink.Device{LinkAttrs: netlink.LinkAttrs{Name: n.HostInterfaceName}}, nil)
115-
}
116-
117-
// attach a container network interface to an external network
118-
func (v *veth) attach(n *configs.Network) (err error) {
119-
brl, err := netlink.LinkByName(n.Bridge)
120-
if err != nil {
121-
return err
122-
}
123-
br, ok := brl.(*netlink.Bridge)
124-
if !ok {
125-
return fmt.Errorf("Wrong device type %T", brl)
126-
}
127-
host, err := netlink.LinkByName(n.HostInterfaceName)
128-
if err != nil {
129-
return err
130-
}
131-
132-
if err := netlink.LinkSetMaster(host, br); err != nil {
133-
return err
134-
}
135-
if err := netlink.LinkSetMTU(host, n.Mtu); err != nil {
136-
return err
137-
}
138-
if n.HairpinMode {
139-
if err := netlink.LinkSetHairpin(host, true); err != nil {
140-
return err
141-
}
142-
}
143-
if err := netlink.LinkSetUp(host); err != nil {
144-
return err
145-
}
146-
147-
return nil
148-
}
149-
150-
func (v *veth) create(n *network, nspid int) (err error) {
151-
tmpName, err := v.generateTempPeerName()
152-
if err != nil {
153-
return err
154-
}
155-
n.TempVethPeerName = tmpName
156-
if n.Bridge == "" {
157-
return fmt.Errorf("bridge is not specified")
158-
}
159-
veth := &netlink.Veth{
160-
LinkAttrs: netlink.LinkAttrs{
161-
Name: n.HostInterfaceName,
162-
TxQLen: n.TxQueueLen,
163-
},
164-
PeerName: n.TempVethPeerName,
165-
}
166-
if err := netlink.LinkAdd(veth); err != nil {
167-
return err
168-
}
169-
defer func() {
170-
if err != nil {
171-
netlink.LinkDel(veth)
172-
}
173-
}()
174-
if err := v.attach(&n.Network); err != nil {
175-
return err
176-
}
177-
child, err := netlink.LinkByName(n.TempVethPeerName)
178-
if err != nil {
179-
return err
180-
}
181-
return netlink.LinkSetNsPid(child, nspid)
182-
}
183-
184-
func (v *veth) generateTempPeerName() (string, error) {
185-
return utils.GenerateRandomName("veth", 7)
186-
}
187-
188-
func (v *veth) initialize(config *network) error {
189-
peer := config.TempVethPeerName
190-
if peer == "" {
191-
return fmt.Errorf("peer is not specified")
192-
}
193-
child, err := netlink.LinkByName(peer)
194-
if err != nil {
195-
return err
196-
}
197-
if err := netlink.LinkSetDown(child); err != nil {
198-
return err
199-
}
200-
if err := netlink.LinkSetName(child, config.Name); err != nil {
201-
return err
202-
}
203-
// get the interface again after we changed the name as the index also changes.
204-
if child, err = netlink.LinkByName(config.Name); err != nil {
205-
return err
206-
}
207-
if config.MacAddress != "" {
208-
mac, err := net.ParseMAC(config.MacAddress)
209-
if err != nil {
210-
return err
211-
}
212-
if err := netlink.LinkSetHardwareAddr(child, mac); err != nil {
213-
return err
214-
}
215-
}
216-
ip, err := netlink.ParseAddr(config.Address)
217-
if err != nil {
218-
return err
219-
}
220-
if err := netlink.AddrAdd(child, ip); err != nil {
221-
return err
222-
}
223-
if config.IPv6Address != "" {
224-
ip6, err := netlink.ParseAddr(config.IPv6Address)
225-
if err != nil {
226-
return err
227-
}
228-
if err := netlink.AddrAdd(child, ip6); err != nil {
229-
return err
230-
}
231-
}
232-
if err := netlink.LinkSetMTU(child, config.Mtu); err != nil {
233-
return err
234-
}
235-
if err := netlink.LinkSetUp(child); err != nil {
236-
return err
237-
}
238-
if config.Gateway != "" {
239-
gw := net.ParseIP(config.Gateway)
240-
if err := netlink.RouteAdd(&netlink.Route{
241-
Scope: netlink.SCOPE_UNIVERSE,
242-
LinkIndex: child.Attrs().Index,
243-
Gw: gw,
244-
}); err != nil {
245-
return err
246-
}
247-
}
248-
if config.IPv6Gateway != "" {
249-
gw := net.ParseIP(config.IPv6Gateway)
250-
if err := netlink.RouteAdd(&netlink.Route{
251-
Scope: netlink.SCOPE_UNIVERSE,
252-
LinkIndex: child.Attrs().Index,
253-
Gw: gw,
254-
}); err != nil {
255-
return err
256-
}
257-
}
258-
return nil
259-
}

libcontainer/utils/utils.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package utils
22

33
import (
4-
"crypto/rand"
5-
"encoding/hex"
64
"encoding/json"
75
"io"
86
"os"
@@ -17,19 +15,6 @@ const (
1715
exitSignalOffset = 128
1816
)
1917

20-
// GenerateRandomName returns a new name joined with a prefix. This size
21-
// specified is used to truncate the randomly generated value
22-
func GenerateRandomName(prefix string, size int) (string, error) {
23-
id := make([]byte, 32)
24-
if _, err := io.ReadFull(rand.Reader, id); err != nil {
25-
return "", err
26-
}
27-
if size > 64 {
28-
size = 64
29-
}
30-
return prefix + hex.EncodeToString(id)[:size], nil
31-
}
32-
3318
// ResolveRootfs ensures that the current working directory is
3419
// not a symlink and returns the absolute path to the rootfs
3520
func ResolveRootfs(uncleanRootfs string) (string, error) {

libcontainer/utils/utils_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,6 @@ import (
1010
"golang.org/x/sys/unix"
1111
)
1212

13-
func TestGenerateName(t *testing.T) {
14-
name, err := GenerateRandomName("veth", 5)
15-
if err != nil {
16-
t.Fatal(err)
17-
}
18-
19-
expected := 5 + len("veth")
20-
if len(name) != expected {
21-
t.Fatalf("expected name to be %d chars but received %d", expected, len(name))
22-
}
23-
24-
name, err = GenerateRandomName("veth", 65)
25-
if err != nil {
26-
t.Fatal(err)
27-
}
28-
29-
expected = 64 + len("veth")
30-
if len(name) != expected {
31-
t.Fatalf("expected name to be %d chars but received %d", expected, len(name))
32-
}
33-
}
34-
3513
var labelTest = []struct {
3614
labels []string
3715
query string

0 commit comments

Comments
 (0)