blob: 8f0fb8058fba213113836aa73e71506a656df505 [file] [log] [blame]
Hariharasubramanian Rcd387972016-01-20 07:19:41 -06001#!/usr/bin/env python
2
3from subprocess import call
4import sys
5import subprocess
6import dbus
7import string
8import os
9import fcntl
10import glib
11import gobject
12import dbus.service
13import dbus.mainloop.glib
14
15DBUS_NAME = 'org.openbmc.NetworkManager'
16OBJ_NAME = '/org/openbmc/NetworkManager/Interface'
17
18network_providers = {
19 'networkd' : {
20 'bus_name' : 'org.freedesktop.network1',
21 'ip_object_name' : '/org/freedesktop/network1/network/default',
22 'hw_object_name' : '/org/freedesktop/network1/link/_31',
23 'interface_name' : 'org.freedesktop.network1.Network',
24 'method' : 'org.freedesktop.network1.Network.SetAddr'
25 },
26 'NetworkManager' : {
27 'bus_name' : 'org.freedesktop.NetworkManager',
28 'ip_object_name' : '/org/freedesktop/NetworkManager',
29 'hw_object_name' : '/org/freedesktop/NetworkManager',
30 'interface_name' : 'org.freedesktop.NetworkManager',
31 'method' : 'org.freedesktop.NetworkManager' # FIXME:
32 },
33}
34
35def getPrefixLen(mask):
36 prefixLen = sum([bin(int(x)).count('1') for x in mask.split('.')])
37 return prefixLen
38
39class IfAddr ():
40 def __init__ (self, family, scope, flags, prefixlen, addr, gw):
41 self.family = family
42 self.scope = scope
43 self.flags = flags
44 self.prefixlen = prefixlen
45 self.addr = addr
46 self.gw = gw
47
48class NetMan (dbus.service.Object):
49 def __init__(self, bus, name):
50 self.bus = bus
51 self.name = name
52 dbus.service.Object.__init__(self,bus,name)
53
54 def setNetworkProvider(self, provider):
55 self.provider = provider
56
Adriana Kobylake150bfd2016-02-01 21:47:34 -060057 def _setAddr (self, op, device, ipaddr, netmask, family, flags, scope, gateway):
Hariharasubramanian Rcd387972016-01-20 07:19:41 -060058 netprov = network_providers [self.provider]
59 bus_name = netprov ['bus_name']
60 obj_path = netprov ['ip_object_name']
61 intf_name = netprov ['interface_name']
62
63 obj = self.bus.get_object(bus_name, obj_path)
64 intf = dbus.Interface(obj, intf_name)
65 if (op == "add"):
Adriana Kobylake150bfd2016-02-01 21:47:34 -060066 return intf.AddAddress (device, ipaddr, netmask, family, flags, scope, gateway)
Hariharasubramanian Rcd387972016-01-20 07:19:41 -060067
68 if (op == "del"):
Adriana Kobylake150bfd2016-02-01 21:47:34 -060069 return intf.DelAddress (device, ipaddr, netmask, family, flags, scope, gateway)
Hariharasubramanian Rcd387972016-01-20 07:19:41 -060070
71 def _getAddr (self, target, device):
72 netprov = network_providers [self.provider]
73 bus_name = netprov ['bus_name']
74
75 if (target == "ip"):
76 intf_name = 'org.freedesktop.network1.Network'
77 obj_path = netprov ['ip_object_name']
78 obj = self.bus.get_object(bus_name, obj_path)
79 intf = dbus.Interface(obj, intf_name)
80 return intf.GetAddress (device)
81
82 if (target == "mac"):
83 intf_name = 'org.freedesktop.network1.Link'
84 obj_path = netprov ['hw_object_name']
85 obj = self.bus.get_object(bus_name, obj_path)
86 intf = dbus.Interface(obj, intf_name)
87 mac = intf.GetAddress (device)
88 print mac
89 return mac
90
91
92
93 @dbus.service.method(DBUS_NAME, "", "")
94 def test(self):
95 print("TEST")
96
97 @dbus.service.method(DBUS_NAME, "ssss", "x")
98 def AddAddress4 (self, device, ipaddr, netmask, gateway):
99 prefixLen = getPrefixLen (netmask)
Chris Austenb13a3cd2016-02-01 18:18:21 -0600100 confFile = "/etc/systemd/network/10-bmc-" + device + ".network"
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600101
102 print("Making .network file...")
103 networkconf = open (confFile, "w+")
104 networkconf.write ('[Match]'+ '\n')
105 networkconf.write ('Name=' + (device) + '\n')
106 networkconf.write ('[Network]' + '\n')
107 networkconf.write ('Address=' + ipaddr + '/' + str(prefixLen) + '\n')
108 networkconf.write ('Gateway=' + gateway + '\n')
Chris Austenb13a3cd2016-02-01 18:18:21 -0600109 networkconf.close()
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600110
111 print("Restarting networkd service...")
Chris Austenb13a3cd2016-02-01 18:18:21 -0600112 call(["ip", "addr", "flush", device])
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600113 return 0
Chris Austenb13a3cd2016-02-01 18:18:21 -0600114 #return self._setAddr ("add", device, ipaddr, netmask, 2, 0, 253, gateway
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600115
116 @dbus.service.method(DBUS_NAME, "ssss", "x")
117 def DelAddress4 (self, device, ipaddr, netmask, gateway):
118 prefixLen = getPrefixLen (netmask)
Chris Austenb13a3cd2016-02-01 18:18:21 -0600119 confFile = "/etc/systemd/network/10-bmc-" + device + ".network"
Hariharasubramanian Rcd387972016-01-20 07:19:41 -0600120 if not (os.path.exists(confFile)):
121 return 1
122
123 self._setAddr ("del", device, ipaddr, netmask, 2, 0, 253, gateway)
124 os.remove (confFile)
125 return 0;
126
127 @dbus.service.method(DBUS_NAME, "s", "a(iyyus)s")
128 def GetAddress4 (self, device):
129 return self._getAddr ("ip", device)
130
131 @dbus.service.method(DBUS_NAME, "s", "s")
132 def GetHwAddress (self, device):
133 return self._getAddr ("mac", device)
134
135def main():
136 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
137 bus = dbus.SystemBus()
138 name = dbus.service.BusName(DBUS_NAME, bus)
139 obj = NetMan (bus, OBJ_NAME)
140 obj.setNetworkProvider ("networkd")
141 mainloop = gobject.MainLoop()
142 print("Started")
143 mainloop.run()
144
145if __name__ == '__main__':
Adriana Kobylake150bfd2016-02-01 21:47:34 -0600146 sys.exit(main())