xref: /openbmc/google-ipmi-sys/eth.cpp (revision 444b5ea4847ea7c014114094d4b63672122f9786)
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 
22 #include <cstdint>
23 #include <cstring>
24 #include <span>
25 #include <string>
26 #include <tuple>
27 #include <vector>
28 
29 namespace google
30 {
31 namespace ipmi
32 {
33 
34 // TOOD(venture): The ipmid.h has this macro, which is a header we
35 // can't normally access.
36 #ifndef MAX_IPMI_BUFFER
37 #define MAX_IPMI_BUFFER 64
38 #endif
39 
40 Resp getEthDevice(std::span<const uint8_t> data,
41                   const HandlerInterface* handler)
42 {
43     std::tuple<std::uint8_t, std::string> details =
44         handler->getEthDetails(std::string(data.begin(), data.end()));
45 
46     std::string device = std::get<1>(details);
47     if (device.length() == 0)
48     {
49         std::fprintf(stderr, "Invalid eth string\n");
50         return ::ipmi::responseReqDataLenInvalid();
51     }
52 
53     if ((sizeof(struct EthDeviceReply) + device.length()) > MAX_IPMI_BUFFER)
54     {
55         std::fprintf(stderr, "Response would overflow response buffer\n");
56         return ::ipmi::responseRetBytesUnavailable();
57     }
58 
59     std::vector<std::uint8_t> reply;
60     reply.reserve(device.length() + sizeof(struct EthDeviceReply));
61     reply.emplace_back(std::get<0>(details));                /* channel */
62     reply.emplace_back(device.length());                     /* ifNameLength */
63     reply.insert(reply.end(), device.begin(), device.end()); /* name */
64 
65     return ::ipmi::responseSuccess(SysOEMCommands::SysGetEthDevice, reply);
66 }
67 
68 } // namespace ipmi
69 } // namespace google
70