xref: /openbmc/google-ipmi-sys/eth.cpp (revision 11a5bb6bec507a21f7f0228f00cf0405b4d2ea5e)
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "eth.hpp"
16 
17 #include "commands.hpp"
18 #include "handler.hpp"
19 
20 #include <ipmid/api-types.hpp>
21 #include <stdplus/print.hpp>
22 
23 #include <cstdint>
24 #include <cstring>
25 #include <span>
26 #include <string>
27 #include <tuple>
28 #include <vector>
29 
30 namespace google
31 {
32 namespace ipmi
33 {
34 
35 // TODO(venture): The ipmid.h has this macro, which is a header we
36 // can't normally access.
37 #ifndef MAX_IPMI_BUFFER
38 #define MAX_IPMI_BUFFER 64
39 #endif
40 
getEthDevice(std::span<const uint8_t> data,const HandlerInterface * handler)41 Resp getEthDevice(std::span<const uint8_t> data,
42                   const HandlerInterface* handler)
43 {
44     std::tuple<std::uint8_t, std::string> details =
45         handler->getEthDetails(std::string(data.begin(), data.end()));
46 
47     std::string device = std::get<1>(details);
48     if (device.length() == 0)
49     {
50         stdplus::print(stderr, "Invalid eth string\n");
51         return ::ipmi::responseReqDataLenInvalid();
52     }
53 
54     if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER)
55     {
56         stdplus::print(stderr, "Response would overflow response buffer\n");
57         return ::ipmi::responseRetBytesUnavailable();
58     }
59 
60     std::vector<std::uint8_t> reply;
61     reply.reserve(device.length() + sizeof(struct EthDeviceReply));
62     reply.emplace_back(std::get<0>(details));                /* channel */
63     reply.emplace_back(device.length());                     /* ifNameLength */
64     reply.insert(reply.end(), device.begin(), device.end()); /* name */
65 
66     return ::ipmi::responseSuccess(SysOEMCommands::SysGetEthDevice, reply);
67 }
68 
69 } // namespace ipmi
70 } // namespace google
71