Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 615fe66

Browse files
authored
Merge pull request #602 from allencloud/refactor-code-in-xen
refactor codes in xen to be more readable
2 parents 6a1c8bf + a750ddd commit 615fe66

File tree

1 file changed

+49
-50
lines changed

1 file changed

+49
-50
lines changed

hypervisor/xen/xen.go

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,25 @@ func InitDriver() *XenDriver {
6363
if res != 0 {
6464
glog.Info("failed to initialize xen context")
6565
return nil
66-
} else if ctx.Version < REQUIRED_VERSION {
66+
}
67+
if ctx.Version < REQUIRED_VERSION {
6768
glog.Infof("Xen version is not new enough (%d), need 4.5 or higher", ctx.Version)
6869
return nil
69-
} else {
70-
glog.V(1).Info("Xen capabilities: ", ctx.Capabilities)
71-
hvm := false
72-
caps := strings.Split(ctx.Capabilities, " ")
73-
for _, cap := range caps {
74-
if strings.HasPrefix(cap, "hvm-") {
75-
hvm = true
76-
break
77-
}
78-
}
79-
if !hvm {
80-
glog.Infof("Xen installation does not support HVM, current capabilities: %s", ctx.Capabilities)
81-
return nil
70+
}
71+
72+
glog.V(1).Info("Xen capabilities: ", ctx.Capabilities)
73+
hvm := false
74+
caps := strings.Split(ctx.Capabilities, " ")
75+
for _, cap := range caps {
76+
if strings.HasPrefix(cap, "hvm-") {
77+
hvm = true
78+
break
8279
}
8380
}
81+
if !hvm {
82+
glog.Infof("Xen installation does not support HVM, current capabilities: %s", ctx.Capabilities)
83+
return nil
84+
}
8485

8586
sigchan := make(chan os.Signal, 1)
8687
go func() {
@@ -130,19 +131,18 @@ func (xd *XenDriver) LoadContext(persisted map[string]interface{}) (hypervisor.D
130131
d, ok := persisted["domid"]
131132
if !ok {
132133
return nil, errors.New("cannot read the dom id info from persist info")
133-
} else {
134-
switch d.(type) {
135-
case float64:
136-
domid = (int)(d.(float64))
137-
if domid <= 0 {
138-
return nil, fmt.Errorf("loaded wrong domid %d", domid)
139-
}
140-
if HyperxlDomainCheck(xd.Ctx, (uint32)(domid)) != 0 {
141-
return nil, fmt.Errorf("cannot load domain %d, not exist", domid)
142-
}
143-
default:
144-
return nil, errors.New("wrong domid type in persist info")
134+
}
135+
switch d.(type) {
136+
case float64:
137+
domid = (int)(d.(float64))
138+
if domid <= 0 {
139+
return nil, fmt.Errorf("loaded wrong domid %d", domid)
140+
}
141+
if HyperxlDomainCheck(xd.Ctx, (uint32)(domid)) != 0 {
142+
return nil, fmt.Errorf("cannot load domain %d, not exist", domid)
145143
}
144+
default:
145+
return nil, errors.New("wrong domid type in persist info")
146146
}
147147

148148
return &XenContext{driver: xd, domId: domid}, nil
@@ -244,38 +244,37 @@ func (xc *XenContext) AddNic(ctx *hypervisor.VmContext, host *hypervisor.HostNic
244244

245245
glog.V(1).Infof("allocate nic %s for dom %d", host.Mac, xc.domId)
246246
hw, err := net.ParseMAC(host.Mac)
247-
if err == nil {
248-
dev := host.Device
249-
glog.V(1).Infof("add network for %d - ip: %s, br: %s, gw: %s, dev: %s, hw: %s", xc.domId, guest.Ipaddr,
250-
host.Bridge, host.Bridge, dev, hw.String())
251-
252-
res := HyperxlNicAdd(xc.driver.Ctx, (uint32)(xc.domId), guest.Ipaddr[0], host.Bridge, host.Bridge, dev, []byte(hw))
253-
if res == 0 {
254-
255-
glog.V(1).Infof("nic %s insert succeeded", guest.Device)
256-
257-
err = network.UpAndAddToBridge(dev, "", "")
258-
if err != nil {
259-
glog.Error("fail to add vif to bridge: ", err.Error())
260-
ctx.Hub <- &hypervisor.DeviceFailed{
261-
Session: callback,
262-
}
263-
HyperxlNicRemove(xc.driver.Ctx, (uint32)(xc.domId), host.Mac)
264-
return
265-
}
266-
267-
result <- callback
268-
return
247+
if err != nil {
248+
glog.Errorf("failed to parse MAC %s: %v", host.Mac, err)
249+
result <- &hypervisor.DeviceFailed{
250+
Session: callback,
269251
}
252+
return
253+
}
254+
dev := host.Device
255+
glog.V(1).Infof("add network for %d - ip: %s, br: %s, gw: %s, dev: %s, hw: %s", xc.domId, guest.Ipaddr,
256+
host.Bridge, host.Bridge, dev, hw.String())
257+
258+
res := HyperxlNicAdd(xc.driver.Ctx, (uint32)(xc.domId), guest.Ipaddr[0], host.Bridge, host.Bridge, dev, []byte(hw))
259+
if res != 0 {
270260
glog.V(1).Infof("nic %s insert succeeded [faked] ", guest.Device)
271261
result <- callback
272262
return
273263
}
274264

275-
glog.Errorf("nic %s insert failed", guest.Device)
276-
result <- &hypervisor.DeviceFailed{
265+
glog.V(1).Infof("nic %s insert succeeded", guest.Device)
266+
267+
if err := network.UpAndAddToBridge(dev, "", ""); err == nil {
268+
result <- callback
269+
return
270+
}
271+
glog.Error("fail to add vif to bridge: ", err.Error())
272+
ctx.Hub <- &hypervisor.DeviceFailed{
277273
Session: callback,
278274
}
275+
276+
HyperxlNicRemove(xc.driver.Ctx, (uint32)(xc.domId), host.Mac)
277+
return
279278
}()
280279
}
281280

0 commit comments

Comments
 (0)