예제 #1
0
func TestSetNetworkDataOVS(t *testing.T) {
	hnic1 := &host.NIC{
		Name:    "br0-ovs",
		Driver:  "openvswitch",
		Vendor:  "",
		Model:   "",
		PCIAddr: "",
		Desc:    "test1_ovs",
		Type:    host.NicTypeOVS,
	}
	gnic1 := guest.NewNIC()
	gnic1.Network = "net1"
	gnic1.PCIAddr.Domain = "0000"
	gnic1.PCIAddr.Slot = "5"
	gnic1.PCIAddr.Bus = "00"
	gnic1.PCIAddr.Function = "0"
	gnic1.HostNIC = hnic1

	list1 := guest.NewNICList()
	list1.Add(gnic1)

	mode1 := new(xmlinput.Mode)
	mode1.Type = xmlinput.ConTypeOVS
	mode1.VnicDriver = "virtio"

	net1 := new(xmlinput.Network)
	net1.Modes = []*xmlinput.Mode{mode1}
	net1.Name = "net1"

	hnic2 := &host.NIC{
		Name:    "br1-ovs",
		Driver:  "openvswitch",
		Vendor:  "",
		Model:   "",
		PCIAddr: "",
		Desc:    "test1_ovs",
		Type:    host.NicTypeOVS,
	}
	gnic2 := guest.NewNIC()
	gnic2.Network = "net2"
	gnic2.PCIAddr.Domain = "0000"
	gnic2.PCIAddr.Slot = "6"
	gnic2.PCIAddr.Bus = "00"
	gnic2.PCIAddr.Function = "0"
	gnic2.HostNIC = hnic2

	list2 := guest.NewNICList()
	list2.Add(gnic2)

	mode2 := new(xmlinput.Mode)
	mode2.Type = xmlinput.ConTypeOVS
	mode2.VnicDriver = "virtio"

	net2 := new(xmlinput.Network)
	net2.Modes = []*xmlinput.Mode{mode2}
	net2.Name = "net2"

	lists := []guest.NICList{list1, list2}
	nets := []*xmlinput.Network{net1, net2}

	c := guest.NewConfig()
	c.NICLists = lists
	c.Networks = nets
	d := new(meta)
	str, err := d.SetNetworkData(c, "")
	if err != nil {
		t.Fatal(err)
	}

	fmt.Printf("Template: %s\n\n", TmpltBridgedOVS)
	fmt.Printf("Expected: %s\n", expectedSetNetworkDataBridgedOVS)
	fmt.Printf("Generated:%s\n", str)
}
예제 #2
0
func TestSetNetworkDataDirect(t *testing.T) {
	hnic1 := &host.NIC{
		Name:    "eth0",
		Driver:  "tg3",
		Vendor:  "Broadcom",
		Model:   "",
		PCIAddr: "pci@0000:09:00.0",
		Desc:    "test1_direct",
		Type:    host.NicTypePhys,
	}

	gnic1 := guest.NewNIC()
	gnic1.Network = "net1"
	gnic1.PCIAddr.Domain = "0000"
	gnic1.PCIAddr.Slot = "5"
	gnic1.PCIAddr.Bus = "00"
	gnic1.PCIAddr.Function = "0"
	gnic1.HostNIC = hnic1

	list1 := guest.NewNICList()
	list1.Add(gnic1)

	mode1 := new(xmlinput.Mode)
	mode1.Type = xmlinput.ConTypeDirect
	mode1.VnicDriver = "virtio"

	net1 := new(xmlinput.Network)
	net1.Modes = []*xmlinput.Mode{mode1}
	net1.Name = "net1"

	hnic2 := &host.NIC{
		Name:    "eth1",
		Driver:  "tg3",
		Vendor:  "Broadcom",
		Model:   "",
		PCIAddr: "pci@0000:10:00.0",
		Desc:    "test1_direct",
		Type:    host.NicTypePhys,
	}

	gnic2 := guest.NewNIC()
	gnic2.Network = "net1"
	gnic2.PCIAddr.Domain = "0000"
	gnic2.PCIAddr.Slot = "6"
	gnic2.PCIAddr.Bus = "00"
	gnic2.PCIAddr.Function = "0"
	gnic2.HostNIC = hnic2

	list2 := guest.NewNICList()
	list2.Add(gnic2)

	mode2 := new(xmlinput.Mode)
	mode2.Type = xmlinput.ConTypeDirect
	mode2.VnicDriver = "virtio"

	net2 := new(xmlinput.Network)
	net2.Modes = []*xmlinput.Mode{mode2}
	net2.Name = "net2"

	lists := []guest.NICList{list1, list2}
	nets := []*xmlinput.Network{net1, net2}

	c := guest.NewConfig()
	c.NICLists = lists
	c.Networks = nets
	d := new(meta)
	str, err := d.SetNetworkData(c, "")
	if err != nil {
		t.Fatal(err)
	}

	fmt.Printf("Template: %s\n\n", TmpltDirect)
	fmt.Printf("Expected: %s\n", expectedSetNetworkDataDirect)
	fmt.Printf("Generated:%s\n", str)
}
예제 #3
0
파일: ui.go 프로젝트: weldpua2008/deployer
func uiNicSelectMenu(ui *gui.DialogUi, data *xmlinput.XMLInputData, guestPortCounter *int,
	guestPciSlotCounter *int, hnics host.NICList, net *xmlinput.Network, index int) (guest.NICList, error) {
	list := make([]string, 0)
	keeper := make(map[string]*host.NIC)
	indexStrToInt := make(map[string][]int)
	indexInt := 1
	for _, hnic := range hnics {
		indexStr := strconv.Itoa(indexInt)
		list = append(list, indexStr, fmt.Sprintf("%-22s%-15s%-68s%-10s", hnic.Name, hnic.PCIAddr, hnic.Desc, uiNUMAIntToString(hnic.NUMANode)))
		keeper[indexStr] = hnic
		if indexInt == 1 {
			// index 0 - element index in the list
			// index 1 - element counter
			// index 2 - PCI slot number represented as integer
			indexStrToInt[indexStr] = []int{1, 0, 0}
		} else {
			indexStrToInt[indexStr] = []int{indexInt*2 - 1, 0, 0}
		}
		indexInt++
	}

	listLength := len(list)
	gnics := guest.NewNICList()
	var disjuncNicVendor string
	var disjuncNicModel string

	for {
		if index > 0 {
			ui.HelpButton(true)
			ui.SetHelpLabel("Back")
		}
		width := uiHeaderSelectNics(ui)
		ui.SetSize(listLength+8, width+5)
		ui.SetTitle(fmt.Sprintf("Select interface for network \"%s\"", net.Name))
		nicNumStr, err := ui.Menu(listLength+5, list[0:]...)
		if err != nil {
			if err.Error() == gui.DialogNext {
				if len(gnics) == 0 && net.Optional == false {
					continue
				}
				break
			}
			*guestPciSlotCounter -= len(gnics)
			*guestPortCounter -= len(gnics)
			return nil, err
		}

		hnic := keeper[nicNumStr]
		// verify that we need to proceed with "disjunction"
		if net.NicsDisjunction && (hnic.Type == host.NicTypePhys || hnic.Type == host.NicTypePhysVF) &&
			(hnic.Vendor != disjuncNicVendor && hnic.Model != disjuncNicModel) {
			// back to menu in case "disjunction" entries already exist
			if host_hwfilter.NicDisjunctionFound(hnic, data.HostNics.Allowed) && disjuncNicVendor != "" {
				msg := fmt.Sprintf("'%s' cannot be selected alongside '%s %s'", hnic.Desc, disjuncNicVendor, disjuncNicModel)
				ui.Output(gui.Warning, msg, "Press <OK> to return to menu.")
				continue
			}
			// set the new entries
			disjuncNicVendor = hnic.Vendor
			disjuncNicModel = hnic.Model
		}

		delNicCounter := indexStrToInt[nicNumStr][1]
		// counter for the NIC found,we should remove the NIC object and its references
		if delNicCounter > 0 {
			// find the guest NIC object in the guest NICs list
			if _, index, err := gnics.NicByHostNicObj(hnic); err == nil {
				// if found :
				// - remove the object
				// - decrement guestPortCounter
				// - decriment guestPciSlotCounter
				gnics.Remove(index)
				if *guestPortCounter > 0 {
					*guestPortCounter--
				}
				if *guestPciSlotCounter > data.GuestNic.PCI.FirstSlot {
					*guestPciSlotCounter--
				}
				// update the list with the new entry containing deselected NIC
				list[indexStrToInt[nicNumStr][0]] = fmt.Sprintf("%-22s%-15s%-68s%-10s", hnic.Name, hnic.PCIAddr, hnic.Desc, uiNUMAIntToString(hnic.NUMANode))
				// - reset element counter
				// - reset PCI slot number
				indexStrToInt[nicNumStr][1] = 0
				indexStrToInt[nicNumStr][2] = 0
			}
			// iterate over the map and update entries
			for nicIndex, data := range indexStrToInt {
				nicCounter := data[1]
				if nicCounter > delNicCounter {
					tmpNic := keeper[nicIndex]
					nicCounter--
					list[data[0]] = fmt.Sprintf("%-22s%-15s%-68s%-9s%d", tmpNic.Name, tmpNic.PCIAddr,
						tmpNic.Desc, uiNUMAIntToString(tmpNic.NUMANode), nicCounter)
					indexStrToInt[nicIndex][1] = nicCounter
					indexStrToInt[nicIndex][2]--
					if gnic, _, err := gnics.NicByHostNicObj(tmpNic); err == nil {
						gnic.PCIAddr.Slot = utils.IntToHexString(indexStrToInt[nicIndex][2])
					}
				}
			}
			// clear "disjunction" entries in case gnics slice is empty
			if gnics.Length() == 0 {
				disjuncNicVendor = ""
				disjuncNicModel = ""
			}
			continue
		}
		// create new guest NIC, set his  number and counter
		indexStrToInt[nicNumStr][1] = *guestPortCounter
		indexStrToInt[nicNumStr][2] = *guestPciSlotCounter
		list[indexStrToInt[nicNumStr][0]] = fmt.Sprintf("%-22s%-15s%-68s%-9s%d", hnic.Name, hnic.PCIAddr,
			hnic.Desc, uiNUMAIntToString(hnic.NUMANode), *guestPortCounter)
		gnic := guest.NewNIC()
		gnic.Network = net.Name
		gnic.PCIAddr.Domain = data.PCI.Domain
		gnic.PCIAddr.Bus = data.PCI.Bus
		gnic.PCIAddr.Slot = utils.IntToHexString(*guestPciSlotCounter)
		gnic.PCIAddr.Function = data.PCI.Function
		gnic.HostNIC = hnic
		gnics.Add(gnic)
		*guestPciSlotCounter++
		*guestPortCounter++
	}
	return gnics, nil
}