Skip to content

Commit 073824b

Browse files
Georgios Ntoutsoscmainas
authored andcommitted
network: Refactor network setup to avoid code duplication
Signed-off-by: Georgios Ntoutsos <[email protected]> Reviewed-by: Charalampos Mainas <[email protected]> Approved-by: Charalampos Mainas <[email protected]> PR: #30
1 parent 05f5650 commit 073824b

File tree

8 files changed

+83
-93
lines changed

8 files changed

+83
-93
lines changed

cmd/urunc/create.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ var createCommand = cli.Command{
8080
if err != nil {
8181
return err
8282
}
83-
8483
return createUnikontainer(context)
8584
}
8685
err := handleNonBimaContainer(context)

cmd/urunc/utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ func handleNonBimaContainer(context *cli.Context) error {
8787
return nil
8888
}
8989
logrus.Info("This is a normal container. Calling runc...")
90+
return runcExec()
91+
}
92+
93+
func runcExec() error {
9094
args := os.Args
9195
binPath, err := exec.LookPath("runc")
9296
if err != nil {
@@ -172,6 +176,7 @@ func handleQueueProxy(context *cli.Context) error {
172176
if err != nil {
173177
return fmt.Errorf("error writing to file: %v", err)
174178
}
179+
return runcExec()
175180
}
176181
return nil
177182
}

internal/constants/network_constants.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package constants
1717
const (
1818
StaticNetworkTapIP = "172.16.1.1"
1919
StaticNetworkUnikernelIP = "172.16.1.2"
20-
DynamicNetworkTapIP = "172.16.X.2"
21-
QueueProxyRedirectIP = "172.16.1.2"
20+
// TODO: Experiment with DynamicNetworkTapIP starting from 172.16.X.1
21+
DynamicNetworkTapIP = "172.16.X.2"
22+
QueueProxyRedirectIP = "172.16.1.2"
2223
)

internal/metrics/metrics.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ func NewZerologMetrics(target string) Writer {
4040
if err != nil {
4141
return nil
4242
}
43-
defer file.Close()
4443
logger := zerolog.New(file).Level(zerolog.InfoLevel).With().Timestamp().Logger()
4544
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixNano
4645
return &zerologMetrics{

pkg/network/network.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"errors"
1919
"fmt"
2020
"net"
21+
"os/user"
22+
"strconv"
2123
"strings"
2224

2325
"github.com/jackpal/gateway"
@@ -72,6 +74,9 @@ func getTapIndex() (int, error) {
7274
tapCount++
7375
}
7476
}
77+
if tapCount > 255 {
78+
return tapCount, fmt.Errorf("TAP interfaces count higher than 255")
79+
}
7580
return tapCount, nil
7681
}
7782

@@ -212,3 +217,60 @@ func addRedirectFilter(source netlink.Link, target netlink.Link) error {
212217
},
213218
})
214219
}
220+
221+
func networkSetup(tapName string, ipAdrress string, redirectLink netlink.Link, addTCRules bool) (netlink.Link, error) {
222+
err := ensureEth0Exists()
223+
// if eth0 does not exist in the namespace, the unikernel was spawned using ctr, so we skip the network setup
224+
if err != nil {
225+
netlog.Info("eth0 interface not found, assuming unikernel was spawned using ctr")
226+
return nil, nil
227+
}
228+
currentUser, err := user.Current()
229+
if err != nil {
230+
return nil, err
231+
}
232+
uid, err := strconv.Atoi(currentUser.Uid)
233+
if err != nil {
234+
return nil, err
235+
}
236+
gid, err := strconv.Atoi(currentUser.Gid)
237+
if err != nil {
238+
return nil, err
239+
}
240+
newTapDevice, err := createTapDevice(tapName, redirectLink.Attrs().MTU, uid, gid)
241+
if err != nil {
242+
return nil, err
243+
}
244+
if addTCRules {
245+
err = addIngressQdisc(newTapDevice)
246+
if err != nil {
247+
return nil, err
248+
}
249+
err = addIngressQdisc(redirectLink)
250+
if err != nil {
251+
return nil, err
252+
}
253+
err = addRedirectFilter(newTapDevice, redirectLink)
254+
if err != nil {
255+
return nil, err
256+
}
257+
err = addRedirectFilter(redirectLink, newTapDevice)
258+
if err != nil {
259+
return nil, err
260+
}
261+
}
262+
ipn, err := netlink.ParseAddr(ipAdrress)
263+
if err != nil {
264+
return nil, err
265+
}
266+
err = netlink.AddrReplace(newTapDevice, ipn)
267+
if err != nil {
268+
return nil, err
269+
}
270+
271+
err = netlink.LinkSetUp(newTapDevice)
272+
if err != nil {
273+
return nil, err
274+
}
275+
return newTapDevice, nil
276+
}

pkg/network/network_dynamic.go

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package network
1616

1717
import (
1818
"fmt"
19-
"os/user"
2019
"strconv"
2120
"strings"
2221

@@ -37,72 +36,27 @@ type DynamicNetwork struct {
3736
// for multiple unikernels in the same pod/network namespace
3837
// See: https://github.com/nubificus/urunc/issues/13
3938
func (n DynamicNetwork) NetworkSetup() (*UnikernelNetworkInfo, error) {
40-
err := ensureEth0Exists()
41-
// if eth0 does not exist in the namespace, the unikernel was spawned using ctr, so we skip the network setup
42-
if err != nil {
43-
netlog.Info("eth0 interface not found, assuming unikernel was spawned using ctr")
44-
return nil, nil
45-
}
46-
redirectLink, err := netlink.LinkByName(DefaultInterface)
47-
if err != nil {
48-
netlog.Errorf("failed to find %s interface", DefaultInterface)
49-
return nil, err
50-
}
51-
ifInfo, err := getInterfaceInfo(DefaultInterface)
52-
if err != nil {
53-
return nil, err
54-
}
55-
currentUser, err := user.Current()
56-
if err != nil {
57-
return nil, err
58-
}
59-
uid, err := strconv.Atoi(currentUser.Uid)
60-
if err != nil {
61-
return nil, err
62-
}
63-
gid, err := strconv.Atoi(currentUser.Gid)
64-
if err != nil {
65-
return nil, err
66-
}
6739
tapIndex, err := getTapIndex()
6840
if err != nil {
6941
return nil, err
7042
}
71-
newTapName := strings.ReplaceAll(DefaultTap, "X", strconv.Itoa(tapIndex))
72-
newTapDevice, err := createTapDevice(newTapName, redirectLink.Attrs().MTU, uid, gid)
43+
redirectLink, err := netlink.LinkByName(DefaultInterface)
7344
if err != nil {
45+
netlog.Errorf("failed to find %s interface", DefaultInterface)
7446
return nil, err
7547
}
48+
newTapName := strings.ReplaceAll(DefaultTap, "X", strconv.Itoa(tapIndex))
49+
addTCRules := false
7650
if tapIndex == 0 {
77-
err = addIngressQdisc(newTapDevice)
78-
if err != nil {
79-
return nil, err
80-
}
81-
err = addIngressQdisc(redirectLink)
82-
if err != nil {
83-
return nil, err
84-
}
85-
err = addRedirectFilter(newTapDevice, redirectLink)
86-
if err != nil {
87-
return nil, err
88-
}
89-
err = addRedirectFilter(redirectLink, newTapDevice)
90-
if err != nil {
91-
return nil, err
92-
}
51+
addTCRules = true
9352
}
9453
ipTemplate := fmt.Sprintf("%s/24", constants.DynamicNetworkTapIP)
9554
newIPAddr := strings.ReplaceAll(ipTemplate, "X", strconv.Itoa(tapIndex+1))
96-
ipn, err := netlink.ParseAddr(newIPAddr)
97-
if err != nil {
98-
return nil, err
99-
}
100-
err = netlink.AddrReplace(newTapDevice, ipn)
55+
newTapDevice, err := networkSetup(newTapName, newIPAddr, redirectLink, addTCRules)
10156
if err != nil {
10257
return nil, err
10358
}
104-
105-
err = netlink.LinkSetUp(newTapDevice)
59+
ifInfo, err := getInterfaceInfo(DefaultInterface)
10660
if err != nil {
10761
return nil, err
10862
}

pkg/network/network_static.go

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ package network
1616

1717
import (
1818
"fmt"
19-
"os/user"
20-
"strconv"
2119
"strings"
2220

2321
"github.com/nubificus/urunc/internal/constants"
@@ -30,42 +28,14 @@ type StaticNetwork struct {
3028
}
3129

3230
func (n StaticNetwork) NetworkSetup() (*UnikernelNetworkInfo, error) {
33-
err := ensureEth0Exists()
34-
if err != nil {
35-
netlog.Error("failed to find eth0 interface in current netns")
36-
return nil, err
37-
}
38-
redirectLink, err := netlink.LinkByName(DefaultInterface)
39-
if err != nil {
40-
netlog.Errorf("failed to find %s interface", DefaultInterface)
41-
return nil, err
42-
}
43-
currentUser, err := user.Current()
44-
if err != nil {
45-
return nil, err
46-
}
47-
uid, err := strconv.Atoi(currentUser.Uid)
48-
if err != nil {
49-
return nil, err
50-
}
51-
gid, err := strconv.Atoi(currentUser.Gid)
52-
if err != nil {
53-
return nil, err
54-
}
5531
newTapName := strings.ReplaceAll(DefaultTap, "X", "0")
56-
newTapDevice, err := createTapDevice(newTapName, redirectLink.Attrs().MTU, uid, gid)
57-
if err != nil {
58-
return nil, err
59-
}
60-
ipn, err := netlink.ParseAddr(StaticIPAddr)
61-
if err != nil {
62-
return nil, err
63-
}
64-
err = netlink.AddrReplace(newTapDevice, ipn)
32+
addTCRules := false
33+
redirectLink, err := netlink.LinkByName(DefaultInterface)
6534
if err != nil {
35+
netlog.Errorf("failed to find %s interface", DefaultInterface)
6636
return nil, err
6737
}
68-
err = netlink.LinkSetUp(newTapDevice)
38+
newTapDevice, err := networkSetup(newTapName, StaticIPAddr, redirectLink, addTCRules)
6939
if err != nil {
7040
return nil, err
7141
}
@@ -75,7 +45,7 @@ func (n StaticNetwork) NetworkSetup() (*UnikernelNetworkInfo, error) {
7545
IP: constants.StaticNetworkUnikernelIP,
7646
DefaultGateway: constants.StaticNetworkTapIP,
7747
Mask: "255.255.255.0",
78-
Interface: "eth0", // or tap0_urunc?
48+
Interface: DefaultInterface, // or tap0_urunc?
7949
MAC: redirectLink.Attrs().HardwareAddr.String(),
8050
},
8151
}, nil

pkg/unikontainers/unikontainers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (u *Unikontainer) Exec() error {
169169
}
170170
networkInfo, err := netManager.NetworkSetup()
171171
if err != nil {
172-
return err
172+
Log.Errorf("Failed to setup network :%v. Possibly due to ctr", err)
173173
}
174174
metrics.Capture(u.State.ID, "TS17")
175175

0 commit comments

Comments
 (0)