2014-05-23 00:42:09 +04:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-18 23:26:27 +04:00
|
|
|
"net"
|
2014-08-25 23:35:40 +04:00
|
|
|
"sort"
|
2014-05-23 00:42:09 +04:00
|
|
|
"strconv"
|
2014-07-01 00:37:09 +04:00
|
|
|
"strings"
|
2014-05-23 00:42:09 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type InterfaceGenerator interface {
|
|
|
|
Name() string
|
2014-06-13 03:13:19 +04:00
|
|
|
Filename() string
|
2014-05-23 00:42:09 +04:00
|
|
|
Netdev() string
|
|
|
|
Link() string
|
|
|
|
Network() string
|
2014-07-01 00:37:09 +04:00
|
|
|
Type() string
|
|
|
|
ModprobeParams() string
|
2014-05-23 00:42:09 +04:00
|
|
|
}
|
|
|
|
|
2014-06-13 03:13:19 +04:00
|
|
|
type networkInterface interface {
|
|
|
|
InterfaceGenerator
|
|
|
|
Children() []networkInterface
|
|
|
|
setConfigDepth(int)
|
|
|
|
}
|
|
|
|
|
2014-05-23 00:42:09 +04:00
|
|
|
type logicalInterface struct {
|
2014-06-13 03:13:19 +04:00
|
|
|
name string
|
2014-08-18 23:26:27 +04:00
|
|
|
hwaddr net.HardwareAddr
|
2014-06-13 03:13:19 +04:00
|
|
|
config configMethod
|
|
|
|
children []networkInterface
|
|
|
|
configDepth int
|
2014-05-23 00:42:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *logicalInterface) Network() string {
|
2014-08-18 23:26:27 +04:00
|
|
|
config := fmt.Sprintln("[Match]")
|
|
|
|
if i.name != "" {
|
|
|
|
config += fmt.Sprintf("Name=%s\n", i.name)
|
|
|
|
}
|
|
|
|
if i.hwaddr != nil {
|
|
|
|
config += fmt.Sprintf("MACAddress=%s\n", i.hwaddr)
|
|
|
|
}
|
|
|
|
config += "\n[Network]\n"
|
2014-05-23 00:42:09 +04:00
|
|
|
|
|
|
|
for _, child := range i.children {
|
|
|
|
switch iface := child.(type) {
|
|
|
|
case *vlanInterface:
|
|
|
|
config += fmt.Sprintf("VLAN=%s\n", iface.name)
|
|
|
|
case *bondInterface:
|
|
|
|
config += fmt.Sprintf("Bond=%s\n", iface.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch conf := i.config.(type) {
|
|
|
|
case configMethodStatic:
|
|
|
|
for _, nameserver := range conf.nameservers {
|
|
|
|
config += fmt.Sprintf("DNS=%s\n", nameserver)
|
|
|
|
}
|
2014-08-18 23:26:27 +04:00
|
|
|
for _, addr := range conf.addresses {
|
|
|
|
config += fmt.Sprintf("\n[Address]\nAddress=%s\n", addr.String())
|
2014-05-23 00:42:09 +04:00
|
|
|
}
|
|
|
|
for _, route := range conf.routes {
|
|
|
|
config += fmt.Sprintf("\n[Route]\nDestination=%s\nGateway=%s\n", route.destination.String(), route.gateway)
|
|
|
|
}
|
|
|
|
case configMethodDHCP:
|
|
|
|
config += "DHCP=true\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2014-06-12 10:21:35 +04:00
|
|
|
func (i *logicalInterface) Link() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-06-13 03:13:19 +04:00
|
|
|
func (i *logicalInterface) Filename() string {
|
|
|
|
return fmt.Sprintf("%02x-%s", i.configDepth, i.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *logicalInterface) Children() []networkInterface {
|
|
|
|
return i.children
|
|
|
|
}
|
|
|
|
|
2014-07-01 00:37:09 +04:00
|
|
|
func (i *logicalInterface) ModprobeParams() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-06-13 03:13:19 +04:00
|
|
|
func (i *logicalInterface) setConfigDepth(depth int) {
|
|
|
|
i.configDepth = depth
|
|
|
|
}
|
|
|
|
|
2014-05-23 00:42:09 +04:00
|
|
|
type physicalInterface struct {
|
|
|
|
logicalInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *physicalInterface) Name() string {
|
|
|
|
return p.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *physicalInterface) Netdev() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-07-01 00:37:09 +04:00
|
|
|
func (p *physicalInterface) Type() string {
|
|
|
|
return "physical"
|
|
|
|
}
|
|
|
|
|
2014-05-23 00:42:09 +04:00
|
|
|
type bondInterface struct {
|
|
|
|
logicalInterface
|
2014-07-01 00:37:09 +04:00
|
|
|
slaves []string
|
|
|
|
options map[string]string
|
2014-05-23 00:42:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bondInterface) Name() string {
|
|
|
|
return b.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bondInterface) Netdev() string {
|
|
|
|
return fmt.Sprintf("[NetDev]\nKind=bond\nName=%s\n", b.name)
|
|
|
|
}
|
|
|
|
|
2014-07-01 00:37:09 +04:00
|
|
|
func (b *bondInterface) Type() string {
|
|
|
|
return "bond"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bondInterface) ModprobeParams() string {
|
|
|
|
params := ""
|
2014-08-25 23:01:27 +04:00
|
|
|
for _, name := range sortedKeys(b.options) {
|
|
|
|
params += fmt.Sprintf("%s=%s ", name, b.options[name])
|
2014-07-01 00:37:09 +04:00
|
|
|
}
|
|
|
|
params = strings.TrimSuffix(params, " ")
|
|
|
|
return params
|
|
|
|
}
|
|
|
|
|
2014-05-23 00:42:09 +04:00
|
|
|
type vlanInterface struct {
|
|
|
|
logicalInterface
|
|
|
|
id int
|
|
|
|
rawDevice string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vlanInterface) Name() string {
|
|
|
|
return v.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *vlanInterface) Netdev() string {
|
2014-06-12 10:21:35 +04:00
|
|
|
config := fmt.Sprintf("[NetDev]\nKind=vlan\nName=%s\n", v.name)
|
|
|
|
switch c := v.config.(type) {
|
|
|
|
case configMethodStatic:
|
|
|
|
if c.hwaddress != nil {
|
|
|
|
config += fmt.Sprintf("MACAddress=%s\n", c.hwaddress)
|
|
|
|
}
|
|
|
|
case configMethodDHCP:
|
|
|
|
if c.hwaddress != nil {
|
|
|
|
config += fmt.Sprintf("MACAddress=%s\n", c.hwaddress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config += fmt.Sprintf("\n[VLAN]\nId=%d\n", v.id)
|
|
|
|
return config
|
2014-05-23 00:42:09 +04:00
|
|
|
}
|
|
|
|
|
2014-07-01 00:37:09 +04:00
|
|
|
func (v *vlanInterface) Type() string {
|
|
|
|
return "vlan"
|
|
|
|
}
|
|
|
|
|
2014-05-23 00:42:09 +04:00
|
|
|
func buildInterfaces(stanzas []*stanzaInterface) []InterfaceGenerator {
|
2014-06-13 03:13:19 +04:00
|
|
|
interfaceMap := createInterfaces(stanzas)
|
|
|
|
linkAncestors(interfaceMap)
|
|
|
|
markConfigDepths(interfaceMap)
|
2014-06-12 09:07:33 +04:00
|
|
|
|
2014-06-13 03:13:19 +04:00
|
|
|
interfaces := make([]InterfaceGenerator, 0, len(interfaceMap))
|
2014-08-25 23:01:27 +04:00
|
|
|
for _, name := range sortedInterfaces(interfaceMap) {
|
|
|
|
interfaces = append(interfaces, interfaceMap[name])
|
2014-06-13 03:13:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return interfaces
|
|
|
|
}
|
|
|
|
|
|
|
|
func createInterfaces(stanzas []*stanzaInterface) map[string]networkInterface {
|
|
|
|
interfaceMap := make(map[string]networkInterface)
|
2014-05-23 00:42:09 +04:00
|
|
|
for _, iface := range stanzas {
|
|
|
|
switch iface.kind {
|
|
|
|
case interfaceBond:
|
2014-07-01 00:37:09 +04:00
|
|
|
bondOptions := make(map[string]string)
|
|
|
|
for _, k := range []string{"mode", "miimon", "lacp-rate"} {
|
|
|
|
if v, ok := iface.options["bond-"+k]; ok && len(v) > 0 {
|
|
|
|
bondOptions[k] = v[0]
|
|
|
|
}
|
|
|
|
}
|
2014-06-12 09:07:33 +04:00
|
|
|
interfaceMap[iface.name] = &bondInterface{
|
|
|
|
logicalInterface{
|
|
|
|
name: iface.name,
|
|
|
|
config: iface.configMethod,
|
2014-06-13 03:13:19 +04:00
|
|
|
children: []networkInterface{},
|
2014-06-12 09:07:33 +04:00
|
|
|
},
|
2014-07-01 00:37:09 +04:00
|
|
|
iface.options["bond-slaves"],
|
|
|
|
bondOptions,
|
2014-06-12 09:07:33 +04:00
|
|
|
}
|
2014-07-01 00:37:09 +04:00
|
|
|
for _, slave := range iface.options["bond-slaves"] {
|
2014-06-12 09:07:33 +04:00
|
|
|
if _, ok := interfaceMap[slave]; !ok {
|
|
|
|
interfaceMap[slave] = &physicalInterface{
|
|
|
|
logicalInterface{
|
|
|
|
name: slave,
|
|
|
|
config: configMethodManual{},
|
2014-06-13 03:13:19 +04:00
|
|
|
children: []networkInterface{},
|
2014-06-12 09:07:33 +04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-23 00:42:09 +04:00
|
|
|
|
2014-06-12 09:07:33 +04:00
|
|
|
case interfacePhysical:
|
|
|
|
if _, ok := iface.configMethod.(configMethodLoopback); ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
interfaceMap[iface.name] = &physicalInterface{
|
|
|
|
logicalInterface{
|
|
|
|
name: iface.name,
|
|
|
|
config: iface.configMethod,
|
2014-06-13 03:13:19 +04:00
|
|
|
children: []networkInterface{},
|
2014-06-12 09:07:33 +04:00
|
|
|
},
|
|
|
|
}
|
2014-05-23 00:42:09 +04:00
|
|
|
|
2014-06-12 09:07:33 +04:00
|
|
|
case interfaceVLAN:
|
|
|
|
var rawDevice string
|
|
|
|
id, _ := strconv.Atoi(iface.options["id"][0])
|
|
|
|
if device := iface.options["raw_device"]; len(device) == 1 {
|
|
|
|
rawDevice = device[0]
|
|
|
|
if _, ok := interfaceMap[rawDevice]; !ok {
|
|
|
|
interfaceMap[rawDevice] = &physicalInterface{
|
|
|
|
logicalInterface{
|
|
|
|
name: rawDevice,
|
|
|
|
config: configMethodManual{},
|
2014-06-13 03:13:19 +04:00
|
|
|
children: []networkInterface{},
|
2014-06-12 09:07:33 +04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
interfaceMap[iface.name] = &vlanInterface{
|
|
|
|
logicalInterface{
|
|
|
|
name: iface.name,
|
|
|
|
config: iface.configMethod,
|
2014-06-13 03:13:19 +04:00
|
|
|
children: []networkInterface{},
|
2014-06-12 09:07:33 +04:00
|
|
|
},
|
|
|
|
id,
|
|
|
|
rawDevice,
|
|
|
|
}
|
2014-05-23 00:42:09 +04:00
|
|
|
}
|
|
|
|
}
|
2014-06-13 03:13:19 +04:00
|
|
|
return interfaceMap
|
|
|
|
}
|
2014-05-23 00:42:09 +04:00
|
|
|
|
2014-06-13 03:13:19 +04:00
|
|
|
func linkAncestors(interfaceMap map[string]networkInterface) {
|
2014-08-25 23:01:27 +04:00
|
|
|
for _, name := range sortedInterfaces(interfaceMap) {
|
|
|
|
iface := interfaceMap[name]
|
2014-06-12 09:07:33 +04:00
|
|
|
switch i := iface.(type) {
|
|
|
|
case *vlanInterface:
|
|
|
|
if parent, ok := interfaceMap[i.rawDevice]; ok {
|
|
|
|
switch p := parent.(type) {
|
|
|
|
case *physicalInterface:
|
|
|
|
p.children = append(p.children, iface)
|
|
|
|
case *bondInterface:
|
|
|
|
p.children = append(p.children, iface)
|
|
|
|
}
|
2014-05-23 00:42:09 +04:00
|
|
|
}
|
2014-06-12 09:07:33 +04:00
|
|
|
case *bondInterface:
|
|
|
|
for _, slave := range i.slaves {
|
|
|
|
if parent, ok := interfaceMap[slave]; ok {
|
|
|
|
switch p := parent.(type) {
|
|
|
|
case *physicalInterface:
|
|
|
|
p.children = append(p.children, iface)
|
|
|
|
case *bondInterface:
|
|
|
|
p.children = append(p.children, iface)
|
|
|
|
}
|
|
|
|
}
|
2014-05-23 00:42:09 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-13 03:13:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func markConfigDepths(interfaceMap map[string]networkInterface) {
|
|
|
|
rootInterfaceMap := make(map[string]networkInterface)
|
|
|
|
for k, v := range interfaceMap {
|
|
|
|
rootInterfaceMap[k] = v
|
|
|
|
}
|
2014-05-23 00:42:09 +04:00
|
|
|
|
2014-06-12 09:07:33 +04:00
|
|
|
for _, iface := range interfaceMap {
|
2014-06-13 03:13:19 +04:00
|
|
|
for _, child := range iface.Children() {
|
|
|
|
delete(rootInterfaceMap, child.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, iface := range rootInterfaceMap {
|
2014-07-18 07:47:37 +04:00
|
|
|
setDepth(iface)
|
2014-05-23 00:42:09 +04:00
|
|
|
}
|
2014-06-13 03:13:19 +04:00
|
|
|
}
|
2014-05-23 00:42:09 +04:00
|
|
|
|
2014-07-18 07:47:37 +04:00
|
|
|
func setDepth(iface networkInterface) int {
|
|
|
|
maxDepth := 0
|
2014-06-13 03:13:19 +04:00
|
|
|
for _, child := range iface.Children() {
|
2014-07-18 07:47:37 +04:00
|
|
|
if depth := setDepth(child); depth > maxDepth {
|
|
|
|
maxDepth = depth
|
|
|
|
}
|
2014-06-13 03:13:19 +04:00
|
|
|
}
|
2014-07-18 07:47:37 +04:00
|
|
|
iface.setConfigDepth(maxDepth)
|
|
|
|
return (maxDepth + 1)
|
2014-05-23 00:42:09 +04:00
|
|
|
}
|
2014-08-25 23:01:27 +04:00
|
|
|
|
|
|
|
func sortedKeys(m map[string]string) (keys []string) {
|
|
|
|
for key := range m {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortedInterfaces(m map[string]networkInterface) (keys []string) {
|
|
|
|
for key := range m {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
return
|
|
|
|
}
|