Skip to content

Commit

Permalink
Create lkl config json
Browse files Browse the repository at this point in the history
Both Rumprun and LKL can have json config at run time, but no
compatibility. This commit adds LKL specific config builder.

Signed-off-by: Akira Moroo <retrage01@gmail.com>
  • Loading branch information
retrage committed Apr 19, 2020
1 parent edcd627 commit e73c120
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 7 deletions.
101 changes: 101 additions & 0 deletions llruntimes/nabla/runnc-cont/lkl.go
@@ -0,0 +1,101 @@
// Copyright (c) 2018, IBM
// Author(s): Brandon Lum, Ricardo Koller, Dan Williams
//
// Permission to use, copy, modify, and/or distribute this software for
// any purpose with or without fee is hereby granted, provided that the
// above copyright notice and this permission notice appear in all
// copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

// +build linux

package runnc_cont

import (
"encoding/json"
"fmt"
"net"
"strconv"
"strings"

"github.com/retrage/runnc/nabla-lib/network"
)

type lklArgsNetwork struct {
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Param string `json:"param,omitempty"`
Mtu string `json:"mtu,omitempty"`
Ip string `json:"ip,omitempty"`
Ipv6 string `json:"ipv6,omitempty"`
IfGw string `json:"ifgateway,omitempty"`
IfGw6 string `json:"ifgateway6,omitempty"`
Mac string `json:"mac,omitempty"`
Mask string `json:"masklen,omitempty"`
Mask6 string `json:"masklen6,omitempty"`
Neigh string `json:"neigh,omitempty"`
Qdisc string `json:"qdisc,omitempty"`
Offload string `json:"offload,omitempty"`
}

type lklArgs struct {
If []lklArgsNetwork `json:"interfaces"`
Gw string `json:"gateway,omitempty"`
Gw6 string `json:"gateway6,omitempty"`
Debug string `json:"debug,omitempty"`
Mount string `json:"mount,omitempty"`
Cpu string `json:"singlecpu,omitempty"`
Sysctl string `json:"sysctl,omitempty"`
Cmdline string `json:"boot_cmdline,omitempty"`
Dump string `json:"dump,omitempty"`
Delay string `json:"delay_main,omitempty"`
}

// CreateLklArgs returns the config string for lkl (a json)
func CreateLklArgs(ip net.IP, mask net.IPMask, gw net.IP, mac string) (string, error) {

// XXX: Due to bug in: https://github.com/nabla-containers/runnc/issues/40
// If we detect a /32 mask, we set it to 1 as a "fix", and hope we are in
// the same subnet... (working on a fix for mask:0)
cidr := strconv.Itoa(network.MaskCIDR(mask))
if cidr == "32" {
fmt.Printf("WARNING: Changing CIDR from 32 to 1 due to Issue https://github.com/nabla-containers/runnc/issues/40\n")
cidr = "1"
}

net := lklArgsNetwork{
Type: "rumpfd",
Name: "tap",
Ip: ip.String(),
Mask: cidr,
IfGw: gw.String(),
Mac: mac,
}

la := &lklArgs{
If: []lklArgsNetwork{net},
Debug: "1",
Cpu: "1",
Delay: "50000",
Sysctl: "net.ipv4.tcp_wmem=4096 87380 2147483647",
}

b, err := json.Marshal(la)
if err != nil {
return "", fmt.Errorf("error with lkl config json: %v", err)
}

s := string(b)
s = strings.Replace(s, "\"", "\\\"", -1)
s = strings.Replace(s, " ", "\\ ", -1)

return s, nil
}
24 changes: 17 additions & 7 deletions llruntimes/nabla/runnc-cont/runnc_cont.go
Expand Up @@ -133,7 +133,10 @@ func setupDisk(path string) (string, error) {
}

func (r *RunncCont) Run() error {
var err error
var (
mac string
err error
)

disk, err := setupDisk(r.Disk)
if err != nil {
Expand All @@ -150,14 +153,21 @@ func (r *RunncCont) Run() error {
r.UniKernelBin = unikernel
}

unikernelArgs, err := CreateLklArgs(r.IPAddress, r.IPMask, r.Gateway, mac)
if err != nil {
return fmt.Errorf("could not create the unikernel cmdline: %v\n", err)
}

var args []string
args = []string{r.NablaRunBin,
"--mem=" + strconv.FormatInt(r.Memory, 10),
"--net:tap=" + r.Tap,
"--block:rootfs=" + disk,
r.UniKernelBin}
args = append(args, "__RUMP_FDINFO_NET_tap=4")
args = []string{r.NablaRunBin,
"--mem=" + strconv.FormatInt(r.Memory, 10),
"--net:tap=" + r.Tap,
"--block:rootfs=" + disk,
r.UniKernelBin}
args = append(args, "__RUMP_FDINFO_NET_tap=4")
args = append(args, r.Env...)
args = append(args, "--config")
args = append(args, unikernelArgs)
args = append(args, "--")
args = append(args, r.NablaRunArgs...)

Expand Down

0 comments on commit e73c120

Please sign in to comment.