Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | from subprocess import call |
| 4 | import sys |
| 5 | import subprocess |
| 6 | import dbus |
| 7 | import string |
| 8 | import os |
| 9 | import fcntl |
| 10 | import glib |
| 11 | import gobject |
| 12 | import dbus.service |
| 13 | import dbus.mainloop.glib |
| 14 | |
| 15 | DBUS_NAME = 'org.openbmc.NetworkManager' |
| 16 | OBJ_NAME = '/org/openbmc/NetworkManager/Interface' |
| 17 | |
| 18 | network_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 | |
| 35 | def getPrefixLen(mask): |
| 36 | prefixLen = sum([bin(int(x)).count('1') for x in mask.split('.')]) |
| 37 | return prefixLen |
| 38 | |
| 39 | class 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 | |
| 48 | class 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 Kobylak | e150bfd | 2016-02-01 21:47:34 -0600 | [diff] [blame^] | 57 | def _setAddr (self, op, device, ipaddr, netmask, family, flags, scope, gateway): |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 58 | 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 Kobylak | e150bfd | 2016-02-01 21:47:34 -0600 | [diff] [blame^] | 66 | return intf.AddAddress (device, ipaddr, netmask, family, flags, scope, gateway) |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 67 | |
| 68 | if (op == "del"): |
Adriana Kobylak | e150bfd | 2016-02-01 21:47:34 -0600 | [diff] [blame^] | 69 | return intf.DelAddress (device, ipaddr, netmask, family, flags, scope, gateway) |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 70 | |
| 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 Austen | b13a3cd | 2016-02-01 18:18:21 -0600 | [diff] [blame] | 100 | confFile = "/etc/systemd/network/10-bmc-" + device + ".network" |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 101 | |
| 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 Austen | b13a3cd | 2016-02-01 18:18:21 -0600 | [diff] [blame] | 109 | networkconf.close() |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 110 | |
| 111 | print("Restarting networkd service...") |
Chris Austen | b13a3cd | 2016-02-01 18:18:21 -0600 | [diff] [blame] | 112 | call(["ip", "addr", "flush", device]) |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 113 | return 0 |
Chris Austen | b13a3cd | 2016-02-01 18:18:21 -0600 | [diff] [blame] | 114 | #return self._setAddr ("add", device, ipaddr, netmask, 2, 0, 253, gateway |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 115 | |
| 116 | @dbus.service.method(DBUS_NAME, "ssss", "x") |
| 117 | def DelAddress4 (self, device, ipaddr, netmask, gateway): |
| 118 | prefixLen = getPrefixLen (netmask) |
Chris Austen | b13a3cd | 2016-02-01 18:18:21 -0600 | [diff] [blame] | 119 | confFile = "/etc/systemd/network/10-bmc-" + device + ".network" |
Hariharasubramanian R | cd38797 | 2016-01-20 07:19:41 -0600 | [diff] [blame] | 120 | 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 | |
| 135 | def 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 | |
| 145 | if __name__ == '__main__': |
Adriana Kobylak | e150bfd | 2016-02-01 21:47:34 -0600 | [diff] [blame^] | 146 | sys.exit(main()) |