config.go 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package occlum

import (
	"strconv"
	"strings"

	"github.com/sirupsen/logrus"
)

const (
11 12 13 14 15 16 17 18 19 20 21 22
	UserSpaceSize           = "OCCLUM_USER_SPACE_SIZE"
	KernelSpaceHeapSize     = "OCCLUM_KERNEL_SPACE_HEAP_SIZE"
	KernelSpaceStackSize    = "OCCLUM_KERNEL_SPACE_STACK_SIZE"
	MaxNumOfThreads         = "OCCLUM_MAX_NUM_OF_THREADS"
	ProcessDefaultStackSize = "OCCLUM_PROCESS_DEFAULT_STACK_SIZE"
	ProcessDefaultHeapSize  = "OCCLUM_PROCESS_DEFAULT_HEAP_SIZE"
	ProcessDefaultMmapSize  = "OCCLUM_PROCESS_DEFAULT_MMAP_SIZE"
	ProductId               = "OCCLUM_PRODUCT_ID"
	VersionNumber           = "OCCLUM_VERSION_NUMBER"
	Debuggable              = "OCCLUM_DEBUGGABLE"
	DefalutEnv              = "OCCLUM_DEFAULT_ENV"
	UntrustedEnv            = "OCCLUM_UNTRUSTED_ENV"
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
)

type OcclumConfig struct {
	ResourceLimits ResourceLimits `json:"resource_limits"`
	Process        Process        `json:"process"`
	EntryPoints    []string       `json:"entry_points"`
	Env            Env            `json:"env"`
	Metadata       Metadata       `json:"metadata"`
	Mount          []Mount        `json:"mount"`
}

type ResourceLimits struct {
	UserSpaceSize        string `json:"user_space_size"`
	KernelSpaceHeapSize  string `json:"kernel_space_heap_size"`
	KernelSpaceStackSize string `json:"kernel_space_stack_size"`
	MaxNumOfThreads      int64  `json:"max_num_of_threads"`
}

type Process struct {
	DefaultStackSize string `json:"default_stack_size"`
	DefaultHeapSize  string `json:"default_heap_size"`
	DefaultMmapSize  string `json:"default_mmap_size"`
}

type Env struct {
	Default   []string `json:"default"`
	Untrusted []string `json:"untrusted"`
}

type Metadata struct {
	ProductId     int64 `json:"product_id"`
	VersionNumber int64 `json:"version_number"`
	Debuggable    bool  `json:"debuggable"`
}

type Mount struct {
	Target  string                 `json:"target"`
	Type    string                 `json:"type"`
	Source  string                 `json:"source,omitempty"`
	Options map[string]interface{} `json:"options,omitempty"`
}

func (c *OcclumConfig) ApplyEnvs(envs []string) {
	for _, env := range envs {
		items := strings.SplitN(env, "=", 2)
		if len(items) != 2 {
			continue
		}
		k := items[0]
		v := items[1]
		switch k {
74
		case UserSpaceSize:
75 76
			c.ResourceLimits.UserSpaceSize = v
			break
77
		case KernelSpaceHeapSize:
78 79
			c.ResourceLimits.KernelSpaceHeapSize = v
			break
80
		case KernelSpaceStackSize:
81 82
			c.ResourceLimits.KernelSpaceStackSize = v
			break
83
		case MaxNumOfThreads:
84 85 86 87 88 89
			i, err := strconv.ParseInt(v, 10, 64)
			if err != nil {
				logrus.Error("ApplyEnvs: parse environment variable %s failed. error: %++v", k, err)
			}
			c.ResourceLimits.MaxNumOfThreads = i
			break
90
		case ProcessDefaultStackSize:
91 92
			c.Process.DefaultStackSize = v
			break
93
		case ProcessDefaultHeapSize:
94 95
			c.Process.DefaultHeapSize = v
			break
96
		case ProcessDefaultMmapSize:
97 98
			c.Process.DefaultMmapSize = v
			break
99
		case ProductId:
100 101 102 103 104 105
			i, err := strconv.ParseInt(v, 10, 64)
			if err != nil {
				logrus.Error("ApplyEnvs: parse environment variable %s failed. error: %++v", k, err)
			}
			c.Metadata.ProductId = i
			break
106
		case VersionNumber:
107 108 109 110 111 112
			i, err := strconv.ParseInt(v, 10, 64)
			if err != nil {
				logrus.Error("ApplyEnvs: parse environment variable %s failed. error: %++v", k, err)
			}
			c.Metadata.VersionNumber = i
			break
113
		case Debuggable:
114 115 116 117 118 119
			i, err := strconv.ParseBool(v)
			if err != nil {
				logrus.Error("ApplyEnvs: parse environment variable %s failed. error: %++v", k, err)
			}
			c.Metadata.Debuggable = i
			break
120 121 122 123 124 125 126 127 128 129
		case DefalutEnv:
			if len(v) > 0 {
				c.Env.Default = strings.Split(v, ",")
			}
			break
		case UntrustedEnv:
			if len(v) > 0 {
				c.Env.Untrusted = strings.Split(v, ",")
			}
			break
130 131 132 133
		}
	}
}

134 135 136 137
func (c *OcclumConfig) ApplyEntrypoints(entrypoints []string) {
	c.EntryPoints = entrypoints
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
func GetDefaultOcclumConfig() *OcclumConfig {
	return &OcclumConfig{
		ResourceLimits: ResourceLimits{
			UserSpaceSize:        "256MB",
			KernelSpaceHeapSize:  "32MB",
			KernelSpaceStackSize: "1MB",
			MaxNumOfThreads:      32},
		Process: Process{
			DefaultStackSize: "4MB",
			DefaultHeapSize:  "32MB",
			DefaultMmapSize:  "80MB",
		},
		EntryPoints: []string{"/bin"},
		Env: Env{
			Default:   []string{"OCCLUM=yes"},
			Untrusted: []string{"EXAMPLE"},
		},
		Metadata: Metadata{
			ProductId:     0,
			VersionNumber: 0,
			Debuggable:    true,
		},
		Mount: []Mount{
			{
				Target:  "/",
				Type:    "sefs",
				Source:  "./image",
				Options: map[string]interface{}{"integrity_only": true},
			},
			{
				Target: "/root",
				Type:   "sefs",
			},
			{
				Target: "/host",
				Type:   "hostfs",
				Source: ".",
			},
			{
				Target: "/tmp",
				Type:   "ramfs",
			},
		},
	}
}