Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 1 | #pragma once |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 2 | #include "types.hpp" |
| 3 | |
Vernon Mauery | 5401250 | 2018-11-07 10:17:31 -0800 | [diff] [blame] | 4 | #include <optional> |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 5 | #include <sdbusplus/server.hpp> |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 6 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 7 | namespace ipmi |
| 8 | { |
| 9 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 10 | constexpr auto MAPPER_BUS_NAME = "xyz.openbmc_project.ObjectMapper"; |
| 11 | constexpr auto MAPPER_OBJ = "/xyz/openbmc_project/object_mapper"; |
| 12 | constexpr auto MAPPER_INTF = "xyz.openbmc_project.ObjectMapper"; |
| 13 | |
| 14 | constexpr auto ROOT = "/"; |
| 15 | constexpr auto HOST_MATCH = "host0"; |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 16 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 17 | constexpr auto PROP_INTF = "org.freedesktop.DBus.Properties"; |
| 18 | constexpr auto DELETE_INTERFACE = "xyz.openbmc_project.Object.Delete"; |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 19 | |
| 20 | constexpr auto METHOD_GET = "Get"; |
| 21 | constexpr auto METHOD_GET_ALL = "GetAll"; |
| 22 | constexpr auto METHOD_SET = "Set"; |
| 23 | |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 24 | /** @class ServiceCache |
| 25 | * @brief Caches lookups of service names from the object mapper. |
| 26 | * @details Most ipmi commands need to talk to other dbus daemons to perform |
| 27 | * their intended actions on the BMC. This usually means they will |
| 28 | * first look up the service name providing the interface they |
| 29 | * require. This class reduces the number of such calls by caching |
| 30 | * the lookup for a specific service. |
| 31 | */ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 32 | class ServiceCache |
| 33 | { |
| 34 | public: |
| 35 | /** @brief Creates a new service cache for the given interface |
| 36 | * and path. |
| 37 | * |
| 38 | * @param[in] intf - The interface used for each lookup |
| 39 | * @param[in] path - The path used for each lookup |
| 40 | */ |
| 41 | ServiceCache(const std::string& intf, const std::string& path); |
| 42 | ServiceCache(std::string&& intf, std::string&& path); |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 43 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 44 | /** @brief Gets the service name from the cache or does in a |
| 45 | * lookup when invalid. |
| 46 | * |
| 47 | * @param[in] bus - The bus associated with and used for looking |
| 48 | * up the service. |
| 49 | */ |
| 50 | const std::string& getService(sdbusplus::bus::bus& bus); |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 51 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 52 | /** @brief Invalidates the current service name */ |
| 53 | void invalidate(); |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 54 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 55 | /** @brief A wrapper around sdbusplus bus.new_method_call |
| 56 | * |
| 57 | * @param[in] bus - The bus used for calling the method |
| 58 | * @param[in] intf - The interface containing the method |
| 59 | * @param[in] method - The method name |
| 60 | * @return The message containing the method call. |
| 61 | */ |
| 62 | sdbusplus::message::message newMethodCall(sdbusplus::bus::bus& bus, |
| 63 | const char* intf, |
| 64 | const char* method); |
William A. Kennington III | 82c173a | 2018-05-11 16:10:12 -0700 | [diff] [blame] | 65 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 66 | /** @brief Check to see if the current cache is valid |
| 67 | * |
| 68 | * @param[in] bus - The bus used for the service lookup |
| 69 | * @return True if the cache is valid false otherwise. |
| 70 | */ |
| 71 | bool isValid(sdbusplus::bus::bus& bus) const; |
| 72 | |
| 73 | private: |
| 74 | /** @brief DBUS interface provided by the service */ |
| 75 | const std::string intf; |
| 76 | /** @brief DBUS path provided by the service */ |
| 77 | const std::string path; |
| 78 | /** @brief The name of the service if valid */ |
Vernon Mauery | 5401250 | 2018-11-07 10:17:31 -0800 | [diff] [blame] | 79 | std::optional<std::string> cachedService; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 80 | /** @brief The name of the bus used in the service lookup */ |
Vernon Mauery | 5401250 | 2018-11-07 10:17:31 -0800 | [diff] [blame] | 81 | std::optional<std::string> cachedBusName; |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 84 | /** |
| 85 | * @brief Get the DBUS Service name for the input dbus path |
| 86 | * |
| 87 | * @param[in] bus - DBUS Bus Object |
| 88 | * @param[in] intf - DBUS Interface |
| 89 | * @param[in] path - DBUS Object Path |
| 90 | * |
| 91 | */ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 92 | std::string getService(sdbusplus::bus::bus& bus, const std::string& intf, |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 93 | const std::string& path); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 94 | |
| 95 | /** @brief Gets the dbus object info implementing the given interface |
| 96 | * from the given subtree. |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 97 | * @param[in] bus - DBUS Bus Object. |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 98 | * @param[in] interface - Dbus interface. |
| 99 | * @param[in] subtreePath - subtree from where the search should start. |
| 100 | * @param[in] match - identifier for object. |
| 101 | * @return On success returns the object having objectpath and servicename. |
| 102 | */ |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 103 | DbusObjectInfo getDbusObject(sdbusplus::bus::bus& bus, |
| 104 | const std::string& interface, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 105 | const std::string& subtreePath = ROOT, |
| 106 | const std::string& match = {}); |
| 107 | |
Ratan Gupta | dd64620 | 2017-11-21 17:46:59 +0530 | [diff] [blame] | 108 | /** @brief Get the ipObject of first dbus IP object of Non-LinkLocalIPAddress |
Gunnar Mills | 8991dd6 | 2017-10-25 17:11:29 -0500 | [diff] [blame] | 109 | * type from the given subtree, if not available gets IP object of |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 110 | * LinkLocalIPAddress type. |
| 111 | * @param[in] bus - DBUS Bus Object. |
| 112 | * @param[in] interface - Dbus interface. |
| 113 | * @param[in] subtreePath - subtree from where the search should start. |
| 114 | * @param[in] match - identifier for object. |
Ratan Gupta | dd64620 | 2017-11-21 17:46:59 +0530 | [diff] [blame] | 115 | * @return On success returns the object having objectpath and servicename. |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 116 | */ |
Ratan Gupta | dd64620 | 2017-11-21 17:46:59 +0530 | [diff] [blame] | 117 | DbusObjectInfo getIPObject(sdbusplus::bus::bus& bus, |
| 118 | const std::string& interface, |
| 119 | const std::string& subtreePath, |
| 120 | const std::string& match); |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 121 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 122 | /** @brief Gets the value associated with the given object |
| 123 | * and the interface. |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 124 | * @param[in] bus - DBUS Bus Object. |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 125 | * @param[in] service - Dbus service name. |
| 126 | * @param[in] objPath - Dbus object path. |
| 127 | * @param[in] interface - Dbus interface. |
| 128 | * @param[in] property - name of the property. |
| 129 | * @return On success returns the value of the property. |
| 130 | */ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 131 | Value getDbusProperty(sdbusplus::bus::bus& bus, const std::string& service, |
| 132 | const std::string& objPath, const std::string& interface, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 133 | const std::string& property); |
| 134 | |
| 135 | /** @brief Gets all the properties associated with the given object |
| 136 | * and the interface. |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 137 | * @param[in] bus - DBUS Bus Object. |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 138 | * @param[in] service - Dbus service name. |
| 139 | * @param[in] objPath - Dbus object path. |
| 140 | * @param[in] interface - Dbus interface. |
| 141 | * @return On success returns the map of name value pair. |
| 142 | */ |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 143 | PropertyMap getAllDbusProperties(sdbusplus::bus::bus& bus, |
| 144 | const std::string& service, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 145 | const std::string& objPath, |
| 146 | const std::string& interface); |
| 147 | |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 148 | /** @brief Gets all managed objects associated with the given object |
| 149 | * path and service. |
| 150 | * @param[in] bus - D-Bus Bus Object. |
| 151 | * @param[in] service - D-Bus service name. |
| 152 | * @param[in] objPath - D-Bus object path. |
| 153 | * @return On success returns the map of name value pair. |
| 154 | */ |
| 155 | ObjectValueTree getManagedObjects(sdbusplus::bus::bus& bus, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 156 | const std::string& service, |
| 157 | const std::string& objPath); |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 158 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 159 | /** @brief Sets the property value of the given object. |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 160 | * @param[in] bus - DBUS Bus Object. |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 161 | * @param[in] service - Dbus service name. |
| 162 | * @param[in] objPath - Dbus object path. |
| 163 | * @param[in] interface - Dbus interface. |
| 164 | * @param[in] property - name of the property. |
| 165 | * @param[in] value - value which needs to be set. |
| 166 | */ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 167 | void setDbusProperty(sdbusplus::bus::bus& bus, const std::string& service, |
| 168 | const std::string& objPath, const std::string& interface, |
| 169 | const std::string& property, const Value& value); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 170 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 171 | /** @brief Gets all the dbus objects from the given service root |
| 172 | * which matches the object identifier. |
| 173 | * @param[in] bus - DBUS Bus Object. |
| 174 | * @param[in] serviceRoot - Service root path. |
| 175 | * @param[in] interface - Dbus interface. |
| 176 | * @param[in] match - Identifier for a path. |
| 177 | * @returns map of object path and service info. |
| 178 | */ |
| 179 | ObjectTree getAllDbusObjects(sdbusplus::bus::bus& bus, |
| 180 | const std::string& serviceRoot, |
| 181 | const std::string& interface, |
Vernon Mauery | 6f2c8cd | 2018-07-28 06:40:39 -0700 | [diff] [blame] | 182 | const std::string& match = {}); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 183 | |
| 184 | /** @brief Deletes all the dbus objects from the given service root |
| 185 | which matches the object identifier. |
| 186 | * @param[in] bus - DBUS Bus Object. |
| 187 | * @param[in] serviceRoot - Service root path. |
| 188 | * @param[in] interface - Dbus interface. |
| 189 | * @param[in] match - Identifier for object. |
| 190 | */ |
| 191 | void deleteAllDbusObjects(sdbusplus::bus::bus& bus, |
| 192 | const std::string& serviceRoot, |
| 193 | const std::string& interface, |
| 194 | const std::string& match = {}); |
| 195 | |
Ratan Gupta | cc6cdbf | 2017-09-01 23:06:25 +0530 | [diff] [blame] | 196 | /** @brief Gets the ancestor objects of the given object |
| 197 | which implements the given interface. |
| 198 | * @param[in] bus - Dbus bus object. |
| 199 | * @param[in] path - Child Dbus object path. |
| 200 | * @param[in] interfaces - Dbus interface list. |
| 201 | * @return map of object path and service info. |
| 202 | */ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 203 | ObjectTree getAllAncestors(sdbusplus::bus::bus& bus, const std::string& path, |
Ratan Gupta | cc6cdbf | 2017-09-01 23:06:25 +0530 | [diff] [blame] | 204 | InterfaceList&& interfaces); |
| 205 | |
James Feist | 1e12112 | 2018-07-31 11:44:09 -0700 | [diff] [blame] | 206 | /** @struct VariantToDoubleVisitor |
| 207 | * @brief Visitor to convert variants to doubles |
| 208 | * @details Performs a static cast on the underlying type |
| 209 | */ |
| 210 | struct VariantToDoubleVisitor |
| 211 | { |
| 212 | template <typename T> |
| 213 | std::enable_if_t<std::is_arithmetic<T>::value, double> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 214 | operator()(const T& t) const |
James Feist | 1e12112 | 2018-07-31 11:44:09 -0700 | [diff] [blame] | 215 | { |
| 216 | return static_cast<double>(t); |
| 217 | } |
| 218 | |
| 219 | template <typename T> |
| 220 | std::enable_if_t<!std::is_arithmetic<T>::value, double> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 221 | operator()(const T& t) const |
James Feist | 1e12112 | 2018-07-31 11:44:09 -0700 | [diff] [blame] | 222 | { |
| 223 | throw std::invalid_argument("Cannot translate type to double"); |
| 224 | } |
| 225 | }; |
| 226 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 227 | namespace method_no_args |
| 228 | { |
| 229 | |
| 230 | /** @brief Calls the Dbus method which waits for response. |
| 231 | * @param[in] bus - DBUS Bus Object. |
| 232 | * @param[in] service - Dbus service name. |
| 233 | * @param[in] objPath - Dbus object path. |
| 234 | * @param[in] interface - Dbus interface. |
| 235 | * @param[in] method - Dbus method. |
| 236 | */ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 237 | void callDbusMethod(sdbusplus::bus::bus& bus, const std::string& service, |
| 238 | const std::string& objPath, const std::string& interface, |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 239 | const std::string& method); |
| 240 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 241 | } // namespace method_no_args |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 242 | |
| 243 | namespace network |
| 244 | { |
| 245 | |
| 246 | constexpr auto ROOT = "/xyz/openbmc_project/network"; |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 247 | constexpr auto SERVICE = "xyz.openbmc_project.Network"; |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 248 | constexpr auto IP_TYPE = "ipv4"; |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 249 | constexpr auto IPV4_PREFIX = "169.254"; |
| 250 | constexpr auto IPV6_PREFIX = "fe80"; |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 251 | constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP"; |
| 252 | constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress"; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 253 | constexpr auto SYSTEMCONFIG_INTERFACE = |
| 254 | "xyz.openbmc_project.Network.SystemConfiguration"; |
| 255 | constexpr auto ETHERNET_INTERFACE = |
| 256 | "xyz.openbmc_project.Network.EthernetInterface"; |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 257 | constexpr auto IP_CREATE_INTERFACE = "xyz.openbmc_project.Network.IP.Create"; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 258 | constexpr auto VLAN_CREATE_INTERFACE = |
| 259 | "xyz.openbmc_project.Network.VLAN.Create"; |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 260 | constexpr auto VLAN_INTERFACE = "xyz.openbmc_project.Network.VLAN"; |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 261 | |
| 262 | /* @brief converts the given subnet into prefix notation. |
| 263 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 264 | * @param[in] mask - Subnet Mask. |
| 265 | * @returns prefix. |
| 266 | */ |
| 267 | uint8_t toPrefix(int addressFamily, const std::string& subnetMask); |
| 268 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 269 | /** @brief Sets the ip on the system. |
| 270 | * @param[in] bus - DBUS Bus Object. |
| 271 | * @param[in] service - Dbus service name. |
| 272 | * @param[in] objPath - Dbus object path. |
| 273 | * @param[in] protocolType - Protocol type |
| 274 | * @param[in] ipaddress - IPaddress. |
| 275 | * @param[in] prefix - Prefix length. |
| 276 | */ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 277 | void createIP(sdbusplus::bus::bus& bus, const std::string& service, |
| 278 | const std::string& objPath, const std::string& protocolType, |
| 279 | const std::string& ipaddress, uint8_t prefix); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 280 | |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 281 | /** @brief Creates the VLAN on the given interface. |
| 282 | * @param[in] bus - DBUS Bus Object. |
| 283 | * @param[in] service - Dbus service name. |
| 284 | * @param[in] objPath - Dbus object path. |
| 285 | * @param[in] interface - EthernetInterface. |
| 286 | * @param[in] vlanID - Vlan ID. |
| 287 | */ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 288 | void createVLAN(sdbusplus::bus::bus& bus, const std::string& service, |
| 289 | const std::string& objPath, const std::string& interface, |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 290 | uint32_t vlanID); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 291 | |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 292 | /** @brief Gets the vlan id from the given object path. |
| 293 | * @param[in] path - Dbus object path. |
| 294 | */ |
| 295 | uint32_t getVLAN(const std::string& path); |
| 296 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 297 | } // namespace network |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 298 | } // namespace ipmi |