Skip to content

Commit b990b4e

Browse files
committed
Handle IPv6
Display IPv6 PIF's fields when `primary_address_type` is `IPv6` Reconfigure management interface with appropriate method Support network reset in the IPv6 case Signed-off-by: BenjiReis <[email protected]>
1 parent 0ef1e7e commit b990b4e

7 files changed

+97
-45
lines changed

XSConsoleData.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,22 @@ def ReconfigureManagement(self, inPIF, inMode, inIP, inNetmask, inGateway, in
929929
Auth.Inst().AssertAuthenticated()
930930
try:
931931
self.RequireSession()
932-
self.session.xenapi.PIF.reconfigure_ip(inPIF['opaqueref'], inMode, inIP, inNetmask, inGateway, FirstValue(inDNS, ''))
932+
if inPIF['primary_address_type'].lower() == 'ipv4':
933+
self.session.xenapi.PIF.reconfigure_ip(inPIF['opaqueref'], inMode, inIP, inNetmask, inGateway, FirstValue(inDNS, ''))
934+
if inPIF['ipv6_configuration_mode'].lower() == 'static':
935+
# Update IPv6 DNS as well
936+
self.session.xenapi.PIF.reconfigure_ipv6(
937+
inPIF['opaqueref'], inPIF['ipv6_configuration_mode'], ','.join(inPIF['IPv6']), inPIF['ipv6_gateway'], FirstValue(inDNS, '')
938+
)
939+
else:
940+
inIPv6 = '' if inIP == '0.0.0.0' else inIP + '/' + inNetmask
941+
inGateway = '' if inGateway == '0.0.0.0' else inGateway
942+
self.session.xenapi.PIF.reconfigure_ipv6(inPIF['opaqueref'], inMode, inIPv6, inGateway, FirstValue(inDNS, ''))
943+
if inPIF['ip_configuration_mode'].lower() == 'static':
944+
# Update IPv4 DNS as well
945+
self.session.xenapi.PIF.reconfigure_ip(
946+
inPIF['opaqueref'], inPIF['ip_configuration_mode'], inPIF['IP'], inPIF['netmask'], inPIF['gateway'], FirstValue(inDNS, '')
947+
)
933948
self.session.xenapi.host.management_reconfigure(inPIF['opaqueref'])
934949
status, output = getstatusoutput('%s host-signal-networking-change' % (Config.Inst().XECLIPath()))
935950
if status != 0:
@@ -949,6 +964,7 @@ def DisableManagement(self):
949964
# Disable the PIF that the management interface was using
950965
for pif in self.derived.managementpifs([]):
951966
self.session.xenapi.PIF.reconfigure_ip(pif['opaqueref'], 'None','' ,'' ,'' ,'')
967+
self.session.xenapi.PIF.reconfigure_ipv6(pif['opaqueref'], 'None','' ,'' ,'')
952968
finally:
953969
# Network reconfigured so this link is potentially no longer valid
954970
self.session = Auth.Inst().CloseSession(self.session)
@@ -983,7 +999,12 @@ def ManagementNetmask(self, inDefault = None):
983999
retVal = inDefault
9841000

9851001
for pif in self.derived.managementpifs([]):
986-
retVal = pif['netmask']
1002+
ipv6 = pif['primary_address_type'].lower() == 'ipv6'
1003+
try:
1004+
# IPv6 are stored as an array of `<ipv6>/<prefix>`
1005+
retVal = pif['IPv6'][0].split('/')[1] if ipv6 else pif['netmask']
1006+
except IndexError:
1007+
return ''
9871008
if retVal:
9881009
break
9891010

@@ -993,7 +1014,8 @@ def ManagementGateway(self, inDefault = None):
9931014
retVal = inDefault
9941015

9951016
for pif in self.derived.managementpifs([]):
996-
retVal = pif['gateway']
1017+
ipv6 = pif['primary_address_type'].lower() == 'ipv6'
1018+
retVal = pif['ipv6_gateway'] if ipv6 else pif['gateway']
9971019
if retVal:
9981020
break
9991021

XSConsoleUtils.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# with this program; if not, write to the Free Software Foundation, Inc.,
1414
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1515

16-
import re, signal, string, subprocess, time, types
16+
import re, signal, socket, string, subprocess, time, types
1717
from pprint import pprint
1818

1919
from XSConsoleBases import *
@@ -189,28 +189,29 @@ def DateTimeToSecs(cls, inDateTime):
189189
return retVal
190190

191191
class IPUtils:
192+
@classmethod
193+
def ValidateIPFamily(cls, text, family):
194+
try:
195+
socket.inet_pton(family, text)
196+
return True
197+
except socket.error:
198+
return False
199+
200+
@classmethod
201+
def ValidateIPv4(cls, text):
202+
return cls.ValidateIPFamily(text, socket.AF_INET)
203+
204+
@classmethod
205+
def ValidateIPv6(cls, text):
206+
return cls.ValidateIPFamily(text, socket.AF_INET6)
207+
192208
@classmethod
193209
def ValidateIP(cls, text):
194-
rc = re.match("^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$", text)
195-
if not rc: return False
196-
ints = list(map(int, rc.groups()))
197-
largest = 0
198-
for i in ints:
199-
if i > 255: return False
200-
largest = max(largest, i)
201-
if largest == 0: return False
202-
return True
210+
return cls.ValidateIPv4(text) or cls.ValidateIPv6(text)
203211

204212
@classmethod
205213
def ValidateNetmask(cls, text):
206-
rc = re.match("^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$", text)
207-
if not rc:
208-
return False
209-
ints = list(map(int, rc.groups()))
210-
for i in ints:
211-
if i > 255:
212-
return False
213-
return True
214+
return cls.ValidateIPv4(text) or (int(text) > 4 and int(text) < 128)
214215

215216
@classmethod
216217
def AssertValidNetmask(cls, inIP):
@@ -329,4 +330,3 @@ def SRSizeString(cls, inBytes):
329330
@classmethod
330331
def DiskSizeString(cls, inBytes):
331332
return cls.BinarySizeString(inBytes)+' ('+cls.DecimalSizeString(inBytes)+')'
332-

plugins-base/XSFeatureDNS.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ def StatusUpdateHandler(cls, inPane):
179179
inPane.AddWrappedTextField(str(dns))
180180
inPane.NewLine()
181181
for pif in data.derived.managementpifs([]):
182-
if pif['ip_configuration_mode'].lower().startswith('static'):
182+
ipv6 = pif['primary_address_type'].lower() == 'ipv6'
183+
configuration_mode = pif['ipv6_configuration_mode'] if ipv6 else pif['ip_configuration_mode']
184+
if configuration_mode.lower().startswith('static'):
183185
inPane.AddKeyHelpField( { Lang("Enter") : Lang("Update DNS Servers") })
184186
break
185187
inPane.AddKeyHelpField( {
@@ -203,7 +205,9 @@ def Register(self):
203205
def ActivateHandler(cls):
204206
data = Data.Inst()
205207
for pif in data.derived.managementpifs([]):
206-
if pif['ip_configuration_mode'].lower().startswith('static'):
208+
ipv6 = pif['primary_address_type'].lower() == 'ipv6'
209+
configuration_mode = pif['ipv6_configuration_mode'] if ipv6 else pif['ip_configuration_mode']
210+
if configuration_mode.lower().startswith('static'):
207211
DialogueUtils.AuthenticatedOnly(lambda: Layout.Inst().PushDialogue(DNSDialogue()))
208212
return
209213

plugins-base/XSFeatureInterface.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,14 @@ def __init__(self):
5353

5454
self.nicMenu = Menu(self, None, "Configure Management Interface", choiceDefs)
5555

56-
self.modeMenu = Menu(self, None, Lang("Select IP Address Configuration Mode"), [
57-
ChoiceDef(Lang("DHCP"), lambda: self.HandleModeChoice('DHCP2') ),
58-
ChoiceDef(Lang("DHCP with Manually Assigned Hostname"), lambda: self.HandleModeChoice('DHCPMANUAL') ),
59-
ChoiceDef(Lang("Static"), lambda: self.HandleModeChoice('STATIC') )
60-
])
56+
mode_choicedefs = []
57+
if(currentPIF['primary_address_type'].lower() == 'ipv6'):
58+
mode_choicedefs.append(ChoiceDef(Lang("Autoconf"), lambda : self.HandleModeChoice("AUTOCONF") ))
59+
mode_choicedefs.append(ChoiceDef(Lang("DHCP"), lambda: self.HandleModeChoice('DHCP2') ))
60+
mode_choicedefs.append(ChoiceDef(Lang("DHCP with Manually Assigned Hostname"),
61+
lambda: self.HandleModeChoice('DHCPMANUAL') ))
62+
mode_choicedefs.append(ChoiceDef(Lang("Static"), lambda: self.HandleModeChoice('STATIC') ))
63+
self.modeMenu = Menu(self, None, Lang("Select IP Address Configuration Mode"), mode_choicedefs)
6164

6265
self.postDHCPMenu = Menu(self, None, Lang("Accept or Edit"), [
6366
ChoiceDef(Lang("Continue With DHCP Enabled"), lambda: self.HandlePostDHCPChoice('CONTINUE') ),
@@ -83,11 +86,17 @@ def __init__(self):
8386
self.hostname = data.host.hostname('')
8487

8588
if currentPIF is not None:
86-
if 'ip_configuration_mode' in currentPIF: self.mode = currentPIF['ip_configuration_mode']
89+
ipv6 = currentPIF['primary_address_type'].lower() == 'ipv6'
90+
configuration_mode_key = 'ipv6_configuration_mode' if ipv6 else 'ip_configuration_mode'
91+
if configuration_mode_key in currentPIF:
92+
self.mode = currentPIF[configuration_mode_key]
8793
if self.mode.lower().startswith('static'):
88-
if 'IP' in currentPIF: self.IP = currentPIF['IP']
89-
if 'netmask' in currentPIF: self.netmask = currentPIF['netmask']
90-
if 'gateway' in currentPIF: self.gateway = currentPIF['gateway']
94+
if 'IP' in currentPIF:
95+
self.IP = currentPIF['IPv6'][0].split('/')[0] if ipv6 else currentPIF['IP']
96+
if 'netmask' in currentPIF:
97+
self.netmask = currentPIF['IPv6'][0].split('/')[1] if ipv6 else currentPIF['netmask']
98+
if 'gateway' in currentPIF:
99+
self.gateway = currentPIF['ipv6_gateway'] if ipv6 else currentPIF['gateway']
91100

92101
# Make the menu current choices point to our best guess of current choices
93102
if self.nic is not None:
@@ -169,8 +178,10 @@ def UpdateFieldsPRECOMMIT(self):
169178
pane.AddStatusField(Lang("Netmask", 16), self.netmask)
170179
pane.AddStatusField(Lang("Gateway", 16), self.gateway)
171180

172-
if self.mode != 'Static' and self.hostname == '':
181+
if self.mode == 'DHCP' and self.hostname == '':
173182
pane.AddStatusField(Lang("Hostname", 16), Lang("Assigned by DHCP"))
183+
elif self.mode == 'Autoconf' and self.hostname == '':
184+
pane.AddStatusField(Lang("Hostname", 16), Lang("Automatically assigned"))
174185
else:
175186
pane.AddStatusField(Lang("Hostname", 16), self.hostname)
176187

@@ -376,6 +387,9 @@ def HandleModeChoice(self, inChoice):
376387
self.hostname = Data.Inst().host.hostname('')
377388
self.mode = 'Static'
378389
self.ChangeState('STATICIP')
390+
elif inChoice == 'AUTOCONF':
391+
self.mode = 'Autoconf'
392+
self.ChangeState('PRECOMMIT')
379393

380394
def HandlePostDHCPChoice(self, inChoice):
381395
if inChoice == 'CONTINUE':
@@ -459,11 +473,13 @@ def StatusUpdateHandler(cls, inPane):
459473
inPane.AddWrappedTextField(Lang("<No interface configured>"))
460474
else:
461475
for pif in data.derived.managementpifs([]):
476+
ipv6 = pif['primary_address_type'].lower() == 'ipv6'
477+
configuration_mode = pif['ipv6_configuration_mode'] if ipv6 else pif['ip_configuration_mode']
462478
inPane.AddStatusField(Lang('Device', 16), pif['device'])
463479
if int(pif['VLAN']) >= 0:
464480
inPane.AddStatusField(Lang('VLAN', 16), pif['VLAN'])
465481
inPane.AddStatusField(Lang('MAC Address', 16), pif['MAC'])
466-
inPane.AddStatusField(Lang('DHCP/Static IP', 16), pif['ip_configuration_mode'])
482+
inPane.AddStatusField(Lang('DHCP/Static IP', 16), configuration_mode)
467483

468484
inPane.AddStatusField(Lang('IP address', 16), data.ManagementIP(''))
469485
inPane.AddStatusField(Lang('Netmask', 16), data.ManagementNetmask(''))

plugins-base/XSFeatureNetworkReset.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,18 +365,23 @@ def Commit(self):
365365
inventory['CURRENT_INTERFACES'] = ''
366366
write_inventory(inventory)
367367

368+
ipv6 = self.IP.find(':') > -1
369+
368370
# Rewrite firstboot management.conf file, which will be picked it by xcp-networkd on restart (if used)
369371
f = open(management_conf, 'w')
370372
try:
371373
f.write("LABEL='" + self.device + "'\n")
372-
f.write("MODE='" + self.mode + "'\n")
374+
f.write(("MODEV6" if ipv6 else "MODE") + "='" + self.mode + "'\n")
373375
if self.vlan != '':
374376
f.write("VLAN='" + self.vlan + "'\n")
375377
if self.mode == 'static':
376-
f.write("IP='" + self.IP + "'\n")
377-
f.write("NETMASK='" + self.netmask + "'\n")
378+
if ipv6:
379+
f.write("IPv6='" + self.IP + "/" + self.netmask + "'\n")
380+
else:
381+
f.write("IP='" + self.IP + "'\n")
382+
f.write("NETMASK='" + self.netmask + "'\n")
378383
if self.gateway != '':
379-
f.write("GATEWAY='" + self.gateway + "'\n")
384+
f.write(("IPv6_GATEWAY" if ipv6 else "GATEWAY") + "='" + self.gateway + "'\n")
380385
if self.dns != '':
381386
f.write("DNS='" + self.dns + "'\n")
382387
finally:
@@ -386,14 +391,17 @@ def Commit(self):
386391
f = open(network_reset, 'w')
387392
try:
388393
f.write('DEVICE=' + self.device + '\n')
389-
f.write('MODE=' + self.mode + '\n')
394+
f.write(('MODE_V6' if ipv6 else 'MODE') + '=' + self.mode + '\n')
390395
if self.vlan != '':
391396
f.write('VLAN=' + self.vlan + '\n')
392397
if self.mode == 'static':
393-
f.write('IP=' + self.IP + '\n')
394-
f.write('NETMASK=' + self.netmask + '\n')
398+
if ipv6:
399+
f.write('IPV6=' + self.IP + '/' + self.netmask + '\n')
400+
else:
401+
f.write('IP=' + self.IP + '\n')
402+
f.write('NETMASK=' + self.netmask + '\n')
395403
if self.gateway != '':
396-
f.write('GATEWAY=' + self.gateway + '\n')
404+
f.write(('GATEWAY_V6' if ipv6 else 'GATEWAY') + '=' + self.gateway + '\n')
397405
if self.dns != '':
398406
f.write('DNS=' + self.dns + '\n')
399407
finally:

plugins-base/XSMenuLayout.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ def UpdateFieldsNETWORK(self, inPane):
6868
ntpState = 'Disabled'
6969

7070
for pif in data.derived.managementpifs([]):
71+
ipv6 = pif['primary_address_type'].lower() == 'ipv6'
72+
configuration_mode = pif['ipv6_configuration_mode'] if ipv6 else pif['ip_configuration_mode']
7173
inPane.AddStatusField(Lang('Device', 16), pif['device'])
7274
if int(pif['VLAN']) >= 0:
7375
inPane.AddStatusField(Lang('VLAN', 16), pif['VLAN'])
7476
inPane.AddStatusField(Lang('MAC Address', 16), pif['MAC'])
75-
inPane.AddStatusField(Lang('DHCP/Static IP', 16), pif['ip_configuration_mode'])
77+
inPane.AddStatusField(Lang('DHCP/Static IP', 16), configuration_mode)
7678

7779
inPane.AddStatusField(Lang('IP address', 16), data.ManagementIP(''))
7880
inPane.AddStatusField(Lang('Netmask', 16), data.ManagementNetmask(''))

tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def test_min(self):
1212
self.assertTrue(IPUtils.ValidateIP('0.0.0.1'))
1313

1414
def test_beyond_min(self):
15-
self.assertFalse(IPUtils.ValidateIP('0.0.0.0'))
15+
self.assertTrue(IPUtils.ValidateIP('0.0.0.0'))
1616

1717
def test_max(self):
1818
self.assertTrue(IPUtils.ValidateIP('255.255.255.255'))

0 commit comments

Comments
 (0)