blob: 62535792bd25733d707ae09203e5cc7241bb7512 [file] [log] [blame]
Willy Tua2056e92021-10-10 13:36:16 -07001// 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.
Patrick Venture4d49ae62018-09-17 11:35:32 -070014
15#include "cable.hpp"
16
Patrick Venture0e9aae52020-08-13 13:07:09 -070017#include "commands.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -070018#include "errors.hpp"
19#include "handler.hpp"
Patrick Venture4d49ae62018-09-17 11:35:32 -070020
Patrick Williams444b5ea2023-05-19 13:56:42 -050021#include <ipmid/api-types.hpp>
22
Patrick Venture4d49ae62018-09-17 11:35:32 -070023#include <cstdint>
Patrick Venturece07ee02018-09-19 18:09:32 -070024#include <cstring>
Willy Tub4e37042021-10-12 17:12:30 -070025#include <span>
Patrick Venture4d49ae62018-09-17 11:35:32 -070026#include <string>
Willy Tuff3cd8e2021-09-14 22:49:55 -070027#include <vector>
Patrick Venture4d49ae62018-09-17 11:35:32 -070028
29namespace google
30{
31namespace ipmi
32{
Patrick Venture4d49ae62018-09-17 11:35:32 -070033
34struct CableRequest
35{
Patrick Venture45fad1b2019-03-18 16:52:14 -070036 uint8_t ifNameLength;
Patrick Venture4d49ae62018-09-17 11:35:32 -070037} __attribute__((packed));
38
Willy Tub4e37042021-10-12 17:12:30 -070039Resp cableCheck(std::span<const uint8_t> data, const HandlerInterface* handler)
Patrick Venture4d49ae62018-09-17 11:35:32 -070040{
41 // There is an IPMI LAN channel statistics command which could be used for
42 // this type of check, however, we're not able to wait for the OpenBMC
43 // implementation to stabilize related to the network management.
44 //
45 // There is a link status file, but it is "unknown" to start with...
46 // The path we're checking: /sys/class/net/eth1/statistics/rx_packets
47
Patrick Venture45fad1b2019-03-18 16:52:14 -070048 // This command is expecting: [0x00][len][ifName]
Willy Tuff3cd8e2021-09-14 22:49:55 -070049 // data should have [len][ifName]
50 if (data.size() < sizeof(struct CableRequest))
Patrick Venture4d49ae62018-09-17 11:35:32 -070051 {
Patrick Venturece07ee02018-09-19 18:09:32 -070052 std::fprintf(stderr, "Invalid command length: %u\n",
Willy Tuff3cd8e2021-09-14 22:49:55 -070053 static_cast<uint32_t>(data.size()));
54 return ::ipmi::responseReqDataLenInvalid();
Patrick Venture4d49ae62018-09-17 11:35:32 -070055 }
56
57 const auto request =
Willy Tuff3cd8e2021-09-14 22:49:55 -070058 reinterpret_cast<const struct CableRequest*>(data.data());
Patrick Venture4d49ae62018-09-17 11:35:32 -070059
60 // Sanity check the object contents.
Patrick Venture45fad1b2019-03-18 16:52:14 -070061 if (request->ifNameLength == 0)
Patrick Venture4d49ae62018-09-17 11:35:32 -070062 {
Patrick Venturece07ee02018-09-19 18:09:32 -070063 std::fprintf(stderr, "Invalid string length: %d\n",
Patrick Venture45fad1b2019-03-18 16:52:14 -070064 request->ifNameLength);
Willy Tuff3cd8e2021-09-14 22:49:55 -070065 return ::ipmi::responseReqDataLenInvalid();
Patrick Venture4d49ae62018-09-17 11:35:32 -070066 }
67
68 // Verify the request buffer contains the object and the string.
Willy Tuff3cd8e2021-09-14 22:49:55 -070069 if (data.size() < (sizeof(struct CableRequest) + request->ifNameLength))
Patrick Venture4d49ae62018-09-17 11:35:32 -070070 {
Patrick Venturece07ee02018-09-19 18:09:32 -070071 std::fprintf(stderr, "*dataLen too small: %u\n",
Willy Tuff3cd8e2021-09-14 22:49:55 -070072 static_cast<uint32_t>(data.size()));
73 return ::ipmi::responseReqDataLenInvalid();
Patrick Venture4d49ae62018-09-17 11:35:32 -070074 }
75
76 // Maximum length one can specify, plus null terminator.
77 char nameBuf[256] = {};
Patrick Venture4d49ae62018-09-17 11:35:32 -070078 // Copy the string out of the request buffer.
William A. Kennington III5d789732021-06-24 03:39:31 -070079 std::memcpy(&nameBuf[0], request + 1, request->ifNameLength);
Patrick Venture4d49ae62018-09-17 11:35:32 -070080 std::string name = nameBuf;
Patrick Ventured2037c62019-03-15 10:29:47 -070081 int64_t count;
Patrick Venture4d49ae62018-09-17 11:35:32 -070082
Patrick Venture4d49ae62018-09-17 11:35:32 -070083 try
84 {
Patrick Ventured2037c62019-03-15 10:29:47 -070085 count = handler->getRxPackets(name);
Patrick Venture4d49ae62018-09-17 11:35:32 -070086 }
Patrick Ventured2037c62019-03-15 10:29:47 -070087 catch (const IpmiException& e)
Patrick Venture4d49ae62018-09-17 11:35:32 -070088 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070089 return ::ipmi::response(e.getIpmiError());
Patrick Venture4d49ae62018-09-17 11:35:32 -070090 }
91
Patrick Venture4d49ae62018-09-17 11:35:32 -070092 // If we have received packets then there is a cable present.
Willy Tuff3cd8e2021-09-14 22:49:55 -070093 std::uint8_t value = (count > 0) ? 1 : 0;
Patrick Venture4d49ae62018-09-17 11:35:32 -070094
Willy Tuff3cd8e2021-09-14 22:49:55 -070095 return ::ipmi::responseSuccess(SysOEMCommands::SysCableCheck,
96 std::vector<std::uint8_t>{value});
Patrick Venture4d49ae62018-09-17 11:35:32 -070097}
98
99} // namespace ipmi
100} // namespace google