github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/stage1/init/kvm/network.go (about) 1 // Copyright 2015 The rkt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package kvm 16 17 import ( 18 "fmt" 19 "net" 20 21 "github.com/coreos/rkt/networking" 22 ) 23 24 // GetNetworkDescriptions explicitly convert slice of activeNets to slice of netDescribers 25 // which is slice required by GetKVMNetArgs 26 func GetNetworkDescriptions(n *networking.Networking) []netDescriber { 27 var nds []netDescriber 28 for _, an := range n.GetActiveNetworks() { 29 nds = append(nds, an) 30 } 31 return nds 32 } 33 34 // netDescriber is something that describes network configuration 35 type netDescriber interface { 36 HostIP() net.IP 37 GuestIP() net.IP 38 Mask() net.IP 39 IfName() string 40 IPMasq() bool 41 } 42 43 // GetKVMNetArgs returns additional arguments that need to be passed to kernel 44 // and lkvm tool to configure networks properly. 45 // Logic is based on Network configuration extracted from Networking struct 46 // and essentially from activeNets that expose netDescriber behavior 47 func GetKVMNetArgs(nds []netDescriber) ([]string, []string, error) { 48 49 var lkvmArgs []string 50 var kernelParams []string 51 52 for i, nd := range nds { 53 // https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt 54 // ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>:<dns0-ip>:<dns1-ip> 55 var gw string 56 if nd.IPMasq() { 57 gw = nd.HostIP().String() 58 } 59 kernelParam := fmt.Sprintf("ip=%s::%s:%s::%s:::", nd.GuestIP(), gw, nd.Mask(), fmt.Sprintf(networking.IfNamePattern, i)) 60 kernelParams = append(kernelParams, kernelParam) 61 62 lkvmArgs = append(lkvmArgs, "--network") 63 lkvmArg := fmt.Sprintf("mode=tap,tapif=%s,host_ip=%s,guest_ip=%s", nd.IfName(), nd.HostIP(), nd.GuestIP()) 64 lkvmArgs = append(lkvmArgs, lkvmArg) 65 } 66 67 return lkvmArgs, kernelParams, nil 68 }