1 #include "hyp_network_manager.hpp"
2 
3 #include <phosphor-logging/elog-errors.hpp>
4 #include <phosphor-logging/elog.hpp>
5 #include <phosphor-logging/log.hpp>
6 #include <sdbusplus/bus.hpp>
7 #include <sdbusplus/server/object.hpp>
8 #include <xyz/openbmc_project/Common/error.hpp>
9 
10 using sdbusplus::exception::SdBusError;
11 
12 namespace phosphor
13 {
14 namespace network
15 {
16 using namespace phosphor::logging;
17 using InternalFailure =
18     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
19 
20 const std::string intType = "Integer";
21 const std::string strType = "String";
22 const std::string enumType = "Enumeration";
23 
24 using ObjectTree =
25     std::map<std::string, std::map<std::string, std::vector<std::string>>>;
26 
27 auto HypNetworkMgr::getDBusProp(const std::string& objectName,
28                                 const std::string& interface,
29                                 const std::string& kw)
30 {
31     auto bus = sdbusplus::bus::new_default();
32     auto properties = bus.new_method_call(
33         "xyz.openbmc_project.BIOSConfigManager", objectName.c_str(),
34         "org.freedesktop.DBus.Properties", "Get");
35     properties.append(interface);
36     properties.append(kw);
37     auto result = bus.call(properties);
38 
39     if (result.is_method_error())
40     {
41         throw std::runtime_error("Get api failed");
42     }
43     return result;
44 }
45 
46 void HypNetworkMgr::setBIOSTableAttr(
47     std::string attrName, std::variant<std::string, int64_t> attrValue,
48     std::string attrType)
49 {
50     auto findAttr = biosTableAttrs.find(attrName);
51     if (findAttr != biosTableAttrs.end())
52     {
53         if (attrType == intType)
54         {
55             int64_t value = std::get<int64_t>(attrValue);
56             if (value != std::get<int64_t>(findAttr->second))
57             {
58                 biosTableAttrs.erase(findAttr);
59                 biosTableAttrs.emplace(attrName, value);
60             }
61         }
62         else if (attrType == strType)
63         {
64             std::string value = std::get<std::string>(attrValue);
65             if (value != std::get<std::string>(findAttr->second))
66             {
67                 biosTableAttrs.erase(findAttr);
68                 biosTableAttrs.emplace(attrName, value);
69             }
70         }
71     }
72     else
73     {
74         log<level::INFO>(
75             "setBIOSTableAttr: Attribute is not found in biosTableAttrs"),
76             entry("attrName : ", attrName.c_str());
77     }
78 }
79 
80 void HypNetworkMgr::setDefaultBIOSTableAttrsOnIntf(const std::string& intf)
81 {
82     biosTableAttrs.emplace("vmi_" + intf + "_ipv4_ipaddr", "0.0.0.0");
83     biosTableAttrs.emplace("vmi_" + intf + "_ipv4_gateway", "0.0.0.0");
84     biosTableAttrs.emplace("vmi_" + intf + "_ipv4_prefix_length", 0);
85     biosTableAttrs.emplace("vmi_" + intf + "_ipv4_method", "IPv4Static");
86 }
87 
88 void HypNetworkMgr::setDefaultHostnameInBIOSTableAttrs()
89 {
90     biosTableAttrs.emplace("vmi_hostname", "");
91 }
92 
93 void HypNetworkMgr::setBIOSTableAttrs()
94 {
95     try
96     {
97         constexpr auto biosMgrIntf = "xyz.openbmc_project.BIOSConfig.Manager";
98         constexpr auto biosMgrObj = "/xyz/openbmc_project/bios_config";
99 
100         constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper";
101         constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper";
102         constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
103 
104         std::vector<std::string> interfaces;
105         interfaces.emplace_back(biosMgrIntf);
106         auto depth = 0;
107 
108         auto mapperCall =
109             bus.new_method_call(mapperBus, mapperObj, mapperIntf, "GetSubTree");
110 
111         mapperCall.append(biosMgrObj, depth, interfaces);
112 
113         auto mapperReply = bus.call(mapperCall);
114         if (mapperReply.is_method_error())
115         {
116             log<level::ERR>("Error in mapper call");
117             elog<InternalFailure>();
118         }
119 
120         ObjectTree objectTree;
121         mapperReply.read(objectTree);
122 
123         if (objectTree.empty())
124         {
125             log<level::ERR>("No Object has implemented the interface",
126                             entry("INTERFACE=%s", biosMgrIntf));
127             elog<InternalFailure>();
128         }
129 
130         std::string objPath;
131 
132         if (1 == objectTree.size())
133         {
134             objPath = objectTree.begin()->first;
135         }
136         else
137         {
138             // If there are more than 2 objects, object path must contain the
139             // interface name
140             for (auto const& object : objectTree)
141             {
142                 log<level::INFO>("interface", entry("INT=%s", biosMgrIntf));
143                 log<level::INFO>("object",
144                                  entry("OBJ=%s", object.first.c_str()));
145 
146                 if (std::string::npos != object.first.find(biosMgrIntf))
147                 {
148                     objPath = object.first;
149                     break;
150                 }
151             }
152 
153             if (objPath.empty())
154             {
155                 log<level::ERR>("Can't find the object for the interface",
156                                 entry("intfName=%s", biosMgrIntf));
157                 elog<InternalFailure>();
158             }
159         }
160 
161         std::variant<BiosBaseTableType> response;
162         getDBusProp(objPath, biosMgrIntf, "BaseBIOSTable").read(response);
163 
164         const BiosBaseTableType* baseBiosTable =
165             std::get_if<BiosBaseTableType>(&response);
166 
167         if (baseBiosTable == nullptr)
168         {
169             log<level::ERR>("BaseBiosTable is empty. No attributes found!");
170             return;
171         }
172 
173         for (const BiosBaseTableItemType& item : *baseBiosTable)
174         {
175             if (item.first.rfind("vmi", 0) == 0) // starts with the prefix
176             {
177                 const std::string& itemType =
178                     std::get<biosBaseAttrType>(item.second);
179 
180                 if (itemType.compare(itemType.size() - intType.size(),
181                                      intType.size(), intType) == 0)
182                 {
183                     const int64_t* currValue = std::get_if<int64_t>(
184                         &std::get<biosBaseCurrValue>(item.second));
185                     if (currValue != nullptr)
186                     {
187                         biosTableAttrs.emplace(item.first, *currValue);
188                     }
189                 }
190                 else if ((itemType.compare(itemType.size() - strType.size(),
191                                            strType.size(), strType) == 0) ||
192                          (itemType.compare(itemType.size() - enumType.size(),
193                                            enumType.size(), enumType) == 0))
194                 {
195                     const std::string* currValue = std::get_if<std::string>(
196                         &std::get<biosBaseCurrValue>(item.second));
197                     if (currValue != nullptr)
198                     {
199                         biosTableAttrs.emplace(item.first, *currValue);
200                     }
201                 }
202                 else
203                 {
204                     log<level::ERR>("Unsupported datatype: The attribute is of "
205                                     "unknown type");
206                 }
207             }
208         }
209     }
210     catch (const SdBusError& e)
211     {
212         log<level::ERR>("Error in making dbus call");
213         throw std::runtime_error("DBus call failed");
214     }
215 }
216 
217 void HypNetworkMgr::createIfObjects()
218 {
219     setBIOSTableAttrs();
220 
221     if ((getBIOSTableAttrs()).size() == 0)
222     {
223         setDefaultHostnameInBIOSTableAttrs();
224     }
225 
226     // The hypervisor can support maximum of
227     // 2 ethernet interfaces. Both eth0/1 objects are
228     // created during init time to support the static
229     // network configurations on the both.
230     // create eth0 and eth1 objects
231     log<level::INFO>("Creating eth0 and eth1 objects");
232     interfaces.emplace("eth0",
233                        std::make_unique<HypEthInterface>(
234                            bus, (objectPath + "/eth0").c_str(), "eth0", *this));
235     interfaces.emplace("eth1",
236                        std::make_unique<HypEthInterface>(
237                            bus, (objectPath + "/eth1").c_str(), "eth1", *this));
238 }
239 
240 void HypNetworkMgr::createSysConfObj()
241 {
242     systemConf.reset(nullptr);
243     this->systemConf =
244         std::make_unique<HypSysConfig>(bus, objectPath + "/config", *this);
245 }
246 
247 } // namespace network
248 } // namespace phosphor
249