Skip to content

Commit 6c3c23d

Browse files
ananosurunc-bot[bot]
authored andcommitted
chore(logs): Improve network error logging and propagation
There are cases where the environment that we run urunc is not properly setup. Thus, have more intuitive error messages can help us identify what the actual error is. In this case, these error messages helped us identify which kernel config options were missing from the host system to add TUN/TAP devices properly and to install the TC filters. PR: #265 Signed-off-by: Anastassios Nanos <ananos@nubificus.co.uk> Reviewed-by: Charalampos Mainas <cmainas@nubificus.co.uk> Approved-by: Charalampos Mainas <cmainas@nubificus.co.uk>
1 parent f98d8b3 commit 6c3c23d

2 files changed

Lines changed: 60 additions & 28 deletions

File tree

‎pkg/network/network.go‎

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -217,49 +217,72 @@ func addRedirectFilter(source netlink.Link, target netlink.Link) error {
217217
}
218218

219219
func networkSetup(tapName string, ipAddress string, redirectLink netlink.Link, addTCRules bool, uid uint32, gid uint32) (netlink.Link, error) {
220+
netlog.Debugf("starting for tapName=%s ipAddress=%s redirectLink=%s addTCRules=%v",
221+
tapName, ipAddress, redirectLink.Attrs().Name, addTCRules)
222+
223+
// Sanity check: ensure eth0 exists in this namespace
220224
err := ensureEth0Exists()
221-
// if eth0 does not exist in the namespace, the unikernel was spawned using ctr, so we skip the network setup
222225
if err != nil {
223-
netlog.Warn("eth0 interface not found, assuming unikernel was spawned using ctr")
226+
netlog.Warnf("eth0 interface not found in namespace (unikernel may have been spawned using ctr): %v", err)
224227
return nil, nil
225228
}
229+
230+
// Create TAP
231+
netlog.Debugf("creating tap device %s (mtu=%d)", tapName, redirectLink.Attrs().MTU)
226232
newTapDevice, err := createTapDevice(tapName, redirectLink.Attrs().MTU, uid, gid)
227233
if err != nil {
228-
return nil, err
234+
return nil, fmt.Errorf("createTapDevice(%s) failed: %w", tapName, err)
235+
}
236+
netlog.Debugf("created tap device %s (index=%d)", newTapDevice.Attrs().Name, newTapDevice.Attrs().Index)
237+
238+
// Bring TAP up before qdisc
239+
if err = netlink.LinkSetUp(newTapDevice); err != nil {
240+
return nil, fmt.Errorf("LinkSetUp(%s) failed: %w", newTapDevice.Attrs().Name, err)
229241
}
242+
netlog.Debugf("TAP %s is UP", newTapDevice.Attrs().Name)
243+
244+
// Bring redirectLink (eth0) up before using it in filters
245+
if err = netlink.LinkSetUp(redirectLink); err != nil {
246+
return nil, fmt.Errorf("LinkSetUp(%s) failed: %w", redirectLink.Attrs().Name, err)
247+
}
248+
netlog.Debugf("redirectLink %s is UP", redirectLink.Attrs().Name)
249+
250+
// Add qdisc + redirect filters
230251
if addTCRules {
231-
err = addIngressQdisc(newTapDevice)
232-
if err != nil {
233-
return nil, err
252+
netlog.Debug("adding tc ingress qdisc + redirect filters")
253+
254+
if err = addIngressQdisc(newTapDevice); err != nil {
255+
return nil, fmt.Errorf("addIngressQdisc(tap=%s) failed: %w",
256+
newTapDevice.Attrs().Name, err)
234257
}
235-
err = addIngressQdisc(redirectLink)
236-
if err != nil {
237-
return nil, err
258+
if err = addIngressQdisc(redirectLink); err != nil {
259+
return nil, fmt.Errorf("addIngressQdisc(redirect=%s) failed: %w",
260+
redirectLink.Attrs().Name, err)
238261
}
239-
err = addRedirectFilter(newTapDevice, redirectLink)
240-
if err != nil {
241-
return nil, err
262+
if err = addRedirectFilter(newTapDevice, redirectLink); err != nil {
263+
return nil, fmt.Errorf("addRedirectFilter(%s->%s) failed: %w",
264+
newTapDevice.Attrs().Name, redirectLink.Attrs().Name, err)
242265
}
243-
err = addRedirectFilter(redirectLink, newTapDevice)
244-
if err != nil {
245-
return nil, err
266+
if err = addRedirectFilter(redirectLink, newTapDevice); err != nil {
267+
return nil, fmt.Errorf("addRedirectFilter(%s->%s) failed: %w",
268+
redirectLink.Attrs().Name, newTapDevice.Attrs().Name, err)
246269
}
247270
}
271+
272+
// Optionally assign IP to TAP
248273
if ipAddress != "" {
274+
netlog.Debugf("assigning IP %s to %s", ipAddress, newTapDevice.Attrs().Name)
249275
ipn, err := netlink.ParseAddr(ipAddress)
250276
if err != nil {
251-
return nil, err
277+
return nil, fmt.Errorf("ParseAddr(%s) failed: %w", ipAddress, err)
252278
}
253-
err = netlink.AddrReplace(newTapDevice, ipn)
254-
if err != nil {
255-
return nil, err
279+
if err = netlink.AddrReplace(newTapDevice, ipn); err != nil {
280+
return nil, fmt.Errorf("AddrReplace(%s, %s) failed: %w",
281+
newTapDevice.Attrs().Name, ipAddress, err)
256282
}
257283
}
258284

259-
err = netlink.LinkSetUp(newTapDevice)
260-
if err != nil {
261-
return nil, err
262-
}
285+
netlog.Debugf("completed successfully (tap=%s, index=%d)", newTapDevice.Attrs().Name, newTapDevice.Attrs().Index)
263286
return newTapDevice, nil
264287
}
265288

‎pkg/network/network_dynamic.go‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,34 @@ type DynamicNetwork struct {
3737
func (n DynamicNetwork) NetworkSetup(uid uint32, gid uint32) (*UnikernelNetworkInfo, error) {
3838
tapIndex, err := getTapIndex()
3939
if err != nil {
40-
return nil, err
40+
return nil, fmt.Errorf("getTapIndex failed: %w", err)
4141
}
4242
if tapIndex > 0 {
4343
return nil, fmt.Errorf("unsupported operation: can't spawn multiple unikernels in the same network namespace")
4444
}
45+
46+
netlog.Debugf("trying LinkByName(%s)", DefaultInterface)
4547
redirectLink, err := netlink.LinkByName(DefaultInterface)
4648
if err != nil {
47-
netlog.Errorf("failed to find %s interface", DefaultInterface)
48-
return nil, err
49+
return nil, fmt.Errorf("failed to find interface %s: %w", DefaultInterface, err)
4950
}
51+
netlog.Debugf("found interface %s (index=%d)", redirectLink.Attrs().Name, redirectLink.Attrs().Index)
52+
5053
newTapName := strings.ReplaceAll(DefaultTap, "X", strconv.Itoa(tapIndex))
54+
netlog.Debugf("creating tap device %s", newTapName)
55+
5156
newTapDevice, err := networkSetup(newTapName, "", redirectLink, true, uid, gid)
5257
if err != nil {
53-
return nil, err
58+
return nil, fmt.Errorf("networkSetup(%s) failed: %w", newTapName, err)
5459
}
60+
netlog.Debugf("tap device created: %s", newTapDevice.Attrs().Name)
61+
62+
netlog.Debugf("fetching info for %s", DefaultInterface)
5563
ifInfo, err := getInterfaceInfo(DefaultInterface)
5664
if err != nil {
57-
return nil, err
65+
return nil, fmt.Errorf("getInterfaceInfo(%s) failed: %w", DefaultInterface, err)
5866
}
67+
5968
return &UnikernelNetworkInfo{
6069
TapDevice: newTapDevice.Attrs().Name,
6170
EthDevice: ifInfo,

0 commit comments

Comments
 (0)