Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 1 | #include "ncsi_util.hpp" |
| 2 | |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 3 | #include <linux/ncsi.h> |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 4 | #include <netlink/genl/ctrl.h> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 5 | #include <netlink/genl/genl.h> |
| 6 | #include <netlink/netlink.h> |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 7 | |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/lg2.hpp> |
William A. Kennington III | cbe5073 | 2023-07-25 13:52:18 -0700 | [diff] [blame^] | 9 | #include <stdplus/numeric/str.hpp> |
| 10 | #include <stdplus/str/buf.hpp> |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 11 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 12 | #include <iomanip> |
Manojkiran Eda | e5a867a | 2020-06-01 18:08:30 +0530 | [diff] [blame] | 13 | #include <iostream> |
Jiaqing Zhao | 7c44a78 | 2022-04-10 15:30:04 +0800 | [diff] [blame] | 14 | #include <vector> |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 15 | |
| 16 | namespace phosphor |
| 17 | { |
| 18 | namespace network |
| 19 | { |
| 20 | namespace ncsi |
| 21 | { |
| 22 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 23 | using CallBack = int (*)(struct nl_msg* msg, void* arg); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 24 | |
William A. Kennington III | cbe5073 | 2023-07-25 13:52:18 -0700 | [diff] [blame^] | 25 | static stdplus::StrBuf toHexStr(std::span<const uint8_t> c) noexcept |
| 26 | { |
| 27 | stdplus::StrBuf ret; |
| 28 | if (c.empty()) |
| 29 | { |
| 30 | return ret; |
| 31 | } |
| 32 | stdplus::IntToStr<16, uint8_t> its; |
| 33 | auto oit = ret.append(c.size() * 3 - 1); |
| 34 | auto cit = c.begin(); |
| 35 | oit = its(oit, *cit++, 2); |
| 36 | for (; cit != c.end(); ++cit) |
| 37 | { |
| 38 | *oit++ = ' '; |
| 39 | oit = its(oit, *cit, 2); |
| 40 | } |
| 41 | return ret; |
| 42 | } |
| 43 | |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 44 | namespace internal |
| 45 | { |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 46 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 47 | struct NCSIPacketHeader |
| 48 | { |
| 49 | uint8_t MCID; |
| 50 | uint8_t revision; |
| 51 | uint8_t reserved; |
| 52 | uint8_t id; |
| 53 | uint8_t type; |
| 54 | uint8_t channel; |
| 55 | uint16_t length; |
| 56 | uint32_t rsvd[2]; |
| 57 | }; |
| 58 | |
| 59 | class Command |
| 60 | { |
| 61 | public: |
| 62 | Command() = delete; |
| 63 | ~Command() = default; |
| 64 | Command(const Command&) = delete; |
| 65 | Command& operator=(const Command&) = delete; |
| 66 | Command(Command&&) = default; |
| 67 | Command& operator=(Command&&) = default; |
| 68 | Command( |
| 69 | int c, int nc = DEFAULT_VALUE, |
| 70 | std::span<const unsigned char> p = std::span<const unsigned char>()) : |
| 71 | cmd(c), |
| 72 | ncsi_cmd(nc), payload(p) |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 73 | {} |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 74 | |
| 75 | int cmd; |
| 76 | int ncsi_cmd; |
| 77 | std::span<const unsigned char> payload; |
| 78 | }; |
| 79 | |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 80 | using nlMsgPtr = std::unique_ptr<nl_msg, decltype(&::nlmsg_free)>; |
| 81 | using nlSocketPtr = std::unique_ptr<nl_sock, decltype(&::nl_socket_free)>; |
| 82 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 83 | CallBack infoCallBack = [](struct nl_msg* msg, void* arg) { |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 84 | using namespace phosphor::network::ncsi; |
| 85 | auto nlh = nlmsg_hdr(msg); |
| 86 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 87 | struct nlattr* tb[NCSI_ATTR_MAX + 1] = {nullptr}; |
| 88 | struct nla_policy ncsiPolicy[NCSI_ATTR_MAX + 1] = { |
William A. Kennington III | 05368f1 | 2021-05-13 18:40:47 -0700 | [diff] [blame] | 89 | {NLA_UNSPEC, 0, 0}, {NLA_U32, 0, 0}, {NLA_NESTED, 0, 0}, |
| 90 | {NLA_U32, 0, 0}, {NLA_U32, 0, 0}, |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 91 | }; |
| 92 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 93 | struct nlattr* packagetb[NCSI_PKG_ATTR_MAX + 1] = {nullptr}; |
| 94 | struct nla_policy packagePolicy[NCSI_PKG_ATTR_MAX + 1] = { |
William A. Kennington III | 05368f1 | 2021-05-13 18:40:47 -0700 | [diff] [blame] | 95 | {NLA_UNSPEC, 0, 0}, {NLA_NESTED, 0, 0}, {NLA_U32, 0, 0}, |
| 96 | {NLA_FLAG, 0, 0}, {NLA_NESTED, 0, 0}, |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 97 | }; |
| 98 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 99 | struct nlattr* channeltb[NCSI_CHANNEL_ATTR_MAX + 1] = {nullptr}; |
| 100 | struct nla_policy channelPolicy[NCSI_CHANNEL_ATTR_MAX + 1] = { |
William A. Kennington III | 05368f1 | 2021-05-13 18:40:47 -0700 | [diff] [blame] | 101 | {NLA_UNSPEC, 0, 0}, {NLA_NESTED, 0, 0}, {NLA_U32, 0, 0}, |
| 102 | {NLA_FLAG, 0, 0}, {NLA_NESTED, 0, 0}, {NLA_UNSPEC, 0, 0}, |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 103 | }; |
| 104 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 105 | *(int*)arg = 0; |
| 106 | |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 107 | auto ret = genlmsg_parse(nlh, 0, tb, NCSI_ATTR_MAX, ncsiPolicy); |
| 108 | if (!tb[NCSI_ATTR_PACKAGE_LIST]) |
| 109 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 110 | lg2::error("No Packages"); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | auto attrTgt = static_cast<nlattr*>(nla_data(tb[NCSI_ATTR_PACKAGE_LIST])); |
| 115 | if (!attrTgt) |
| 116 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 117 | lg2::error("Package list attribute is null"); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | auto rem = nla_len(tb[NCSI_ATTR_PACKAGE_LIST]); |
| 122 | nla_for_each_nested(attrTgt, tb[NCSI_ATTR_PACKAGE_LIST], rem) |
| 123 | { |
| 124 | ret = nla_parse_nested(packagetb, NCSI_PKG_ATTR_MAX, attrTgt, |
| 125 | packagePolicy); |
| 126 | if (ret < 0) |
| 127 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 128 | lg2::error("Failed to parse package nested"); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | if (packagetb[NCSI_PKG_ATTR_ID]) |
| 133 | { |
| 134 | auto attrID = nla_get_u32(packagetb[NCSI_PKG_ATTR_ID]); |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 135 | lg2::debug("Package has id : {ATTR_ID}", "ATTR_ID", lg2::hex, |
| 136 | attrID); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 137 | } |
| 138 | else |
| 139 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 140 | lg2::debug("Package with no id"); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if (packagetb[NCSI_PKG_ATTR_FORCED]) |
| 144 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 145 | lg2::debug("This package is forced"); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | auto channelListTarget = static_cast<nlattr*>( |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 149 | nla_data(packagetb[NCSI_PKG_ATTR_CHANNEL_LIST])); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 150 | |
| 151 | auto channelrem = nla_len(packagetb[NCSI_PKG_ATTR_CHANNEL_LIST]); |
| 152 | nla_for_each_nested(channelListTarget, |
| 153 | packagetb[NCSI_PKG_ATTR_CHANNEL_LIST], channelrem) |
| 154 | { |
| 155 | ret = nla_parse_nested(channeltb, NCSI_CHANNEL_ATTR_MAX, |
| 156 | channelListTarget, channelPolicy); |
| 157 | if (ret < 0) |
| 158 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 159 | lg2::error("Failed to parse channel nested"); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | if (channeltb[NCSI_CHANNEL_ATTR_ID]) |
| 164 | { |
| 165 | auto channel = nla_get_u32(channeltb[NCSI_CHANNEL_ATTR_ID]); |
| 166 | if (channeltb[NCSI_CHANNEL_ATTR_ACTIVE]) |
| 167 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 168 | lg2::debug("Channel Active : {CHANNEL}", "CHANNEL", |
| 169 | lg2::hex, channel); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 170 | } |
| 171 | else |
| 172 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 173 | lg2::debug("Channel Not Active : {CHANNEL}", "CHANNEL", |
| 174 | lg2::hex, channel); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | if (channeltb[NCSI_CHANNEL_ATTR_FORCED]) |
| 178 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 179 | lg2::debug("Channel is forced"); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 180 | } |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 181 | } |
| 182 | else |
| 183 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 184 | lg2::debug("Channel with no ID"); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 185 | } |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 186 | |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 187 | if (channeltb[NCSI_CHANNEL_ATTR_VERSION_MAJOR]) |
| 188 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 189 | auto major = |
| 190 | nla_get_u32(channeltb[NCSI_CHANNEL_ATTR_VERSION_MAJOR]); |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 191 | lg2::debug("Channel Major Version : {CHANNEL_MAJOR_VERSION}", |
| 192 | "CHANNEL_MAJOR_VERSION", lg2::hex, major); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 193 | } |
| 194 | if (channeltb[NCSI_CHANNEL_ATTR_VERSION_MINOR]) |
| 195 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 196 | auto minor = |
| 197 | nla_get_u32(channeltb[NCSI_CHANNEL_ATTR_VERSION_MINOR]); |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 198 | lg2::debug("Channel Minor Version : {CHANNEL_MINOR_VERSION}", |
| 199 | "CHANNEL_MINOR_VERSION", lg2::hex, minor); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 200 | } |
| 201 | if (channeltb[NCSI_CHANNEL_ATTR_VERSION_STR]) |
| 202 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 203 | auto str = |
| 204 | nla_get_string(channeltb[NCSI_CHANNEL_ATTR_VERSION_STR]); |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 205 | lg2::debug("Channel Version Str : {CHANNEL_VERSION_STR}", |
| 206 | "CHANNEL_VERSION_STR", str); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 207 | } |
| 208 | if (channeltb[NCSI_CHANNEL_ATTR_LINK_STATE]) |
| 209 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 210 | auto link = |
| 211 | nla_get_u32(channeltb[NCSI_CHANNEL_ATTR_LINK_STATE]); |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 212 | lg2::debug("Channel Link State : {LINK_STATE}", "LINK_STATE", |
| 213 | lg2::hex, link); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 214 | } |
| 215 | if (channeltb[NCSI_CHANNEL_ATTR_VLAN_LIST]) |
| 216 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 217 | lg2::debug("Active Vlan ids"); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 218 | auto vids = channeltb[NCSI_CHANNEL_ATTR_VLAN_LIST]; |
| 219 | auto vid = static_cast<nlattr*>(nla_data(vids)); |
| 220 | auto len = nla_len(vids); |
| 221 | while (nla_ok(vid, len)) |
| 222 | { |
| 223 | auto id = nla_get_u16(vid); |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 224 | lg2::debug("VID : {VLAN_ID}", "VLAN_ID", id); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 225 | vid = nla_next(vid, &len); |
| 226 | } |
| 227 | } |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 228 | } |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 229 | } |
| 230 | return (int)NL_SKIP; |
| 231 | }; |
| 232 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 233 | CallBack sendCallBack = [](struct nl_msg* msg, void* arg) { |
| 234 | using namespace phosphor::network::ncsi; |
| 235 | auto nlh = nlmsg_hdr(msg); |
| 236 | struct nlattr* tb[NCSI_ATTR_MAX + 1] = {nullptr}; |
| 237 | static struct nla_policy ncsiPolicy[NCSI_ATTR_MAX + 1] = { |
| 238 | {NLA_UNSPEC, 0, 0}, {NLA_U32, 0, 0}, {NLA_NESTED, 0, 0}, |
| 239 | {NLA_U32, 0, 0}, {NLA_U32, 0, 0}, {NLA_BINARY, 0, 0}, |
| 240 | {NLA_FLAG, 0, 0}, {NLA_U32, 0, 0}, {NLA_U32, 0, 0}, |
| 241 | }; |
| 242 | |
| 243 | *(int*)arg = 0; |
| 244 | |
| 245 | auto ret = genlmsg_parse(nlh, 0, tb, NCSI_ATTR_MAX, ncsiPolicy); |
| 246 | if (ret) |
| 247 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 248 | lg2::error("Failed to parse package"); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 249 | return ret; |
| 250 | } |
| 251 | |
Jian Zhang | 442d9e5 | 2022-10-20 22:11:14 +0800 | [diff] [blame] | 252 | if (tb[NCSI_ATTR_DATA] == nullptr) |
| 253 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 254 | lg2::error("Response: No data"); |
Jian Zhang | 442d9e5 | 2022-10-20 22:11:14 +0800 | [diff] [blame] | 255 | return -1; |
| 256 | } |
| 257 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 258 | auto data_len = nla_len(tb[NCSI_ATTR_DATA]) - sizeof(NCSIPacketHeader); |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 259 | unsigned char* data = (unsigned char*)nla_data(tb[NCSI_ATTR_DATA]) + |
| 260 | sizeof(NCSIPacketHeader); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 261 | |
| 262 | // Dump the response to stdout. Enhancement: option to save response data |
William A. Kennington III | cbe5073 | 2023-07-25 13:52:18 -0700 | [diff] [blame^] | 263 | auto str = toHexStr(std::span<const unsigned char>(data, data_len)); |
| 264 | lg2::debug("Response {DATA_LEN} bytes: {DATA}", "DATA_LEN", data_len, |
| 265 | "DATA", str); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 266 | |
| 267 | return 0; |
| 268 | }; |
| 269 | |
| 270 | int applyCmd(int ifindex, const Command& cmd, int package = DEFAULT_VALUE, |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 271 | int channel = DEFAULT_VALUE, int flags = NONE, |
| 272 | CallBack function = nullptr) |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 273 | { |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 274 | int cb_ret = 0; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 275 | nlSocketPtr socket(nl_socket_alloc(), &::nl_socket_free); |
Johnathan Mantey | d49c5c6 | 2021-06-23 09:14:42 -0700 | [diff] [blame] | 276 | if (socket == nullptr) |
| 277 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 278 | lg2::error("Unable to allocate memory for the socket"); |
Johnathan Mantey | d49c5c6 | 2021-06-23 09:14:42 -0700 | [diff] [blame] | 279 | return -ENOMEM; |
| 280 | } |
| 281 | |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 282 | auto ret = genl_connect(socket.get()); |
| 283 | if (ret < 0) |
| 284 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 285 | lg2::error("Failed to open the socket , RC : {RC}", "RC", ret); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | auto driverID = genl_ctrl_resolve(socket.get(), "NCSI"); |
| 290 | if (driverID < 0) |
| 291 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 292 | lg2::error("Failed to resolve, RC : {RC}", "RC", ret); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 293 | return driverID; |
| 294 | } |
| 295 | |
| 296 | nlMsgPtr msg(nlmsg_alloc(), &::nlmsg_free); |
Johnathan Mantey | d49c5c6 | 2021-06-23 09:14:42 -0700 | [diff] [blame] | 297 | if (msg == nullptr) |
| 298 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 299 | lg2::error("Unable to allocate memory for the message"); |
Johnathan Mantey | d49c5c6 | 2021-06-23 09:14:42 -0700 | [diff] [blame] | 300 | return -ENOMEM; |
| 301 | } |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 302 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 303 | auto msgHdr = genlmsg_put(msg.get(), 0, 0, driverID, 0, flags, cmd.cmd, 0); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 304 | if (!msgHdr) |
| 305 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 306 | lg2::error("Unable to add the netlink headers , COMMAND : {COMMAND}", |
| 307 | "COMMAND", cmd.cmd); |
Johnathan Mantey | d49c5c6 | 2021-06-23 09:14:42 -0700 | [diff] [blame] | 308 | return -ENOMEM; |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | if (package != DEFAULT_VALUE) |
| 312 | { |
| 313 | ret = nla_put_u32(msg.get(), ncsi_nl_attrs::NCSI_ATTR_PACKAGE_ID, |
| 314 | package); |
| 315 | if (ret < 0) |
| 316 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 317 | lg2::error("Failed to set the attribute , RC : {RC} PACKAGE " |
| 318 | "{PACKAGE}", |
| 319 | "RC", ret, "PACKAGE", lg2::hex, package); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 320 | return ret; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if (channel != DEFAULT_VALUE) |
| 325 | { |
| 326 | ret = nla_put_u32(msg.get(), ncsi_nl_attrs::NCSI_ATTR_CHANNEL_ID, |
| 327 | channel); |
| 328 | if (ret < 0) |
| 329 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 330 | lg2::error("Failed to set the attribute , RC : {RC} CHANNEL : " |
| 331 | "{CHANNEL}", |
| 332 | "RC", ret, "CHANNEL", lg2::hex, channel); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 333 | return ret; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | ret = nla_put_u32(msg.get(), ncsi_nl_attrs::NCSI_ATTR_IFINDEX, ifindex); |
| 338 | if (ret < 0) |
| 339 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 340 | lg2::error("Failed to set the attribute , RC : {RC} INTERFACE : " |
| 341 | "{INTERFACE}", |
| 342 | "RC", ret, "INTERFACE", lg2::hex, ifindex); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 343 | return ret; |
| 344 | } |
| 345 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 346 | if (cmd.ncsi_cmd != DEFAULT_VALUE) |
| 347 | { |
| 348 | std::vector<unsigned char> pl(sizeof(NCSIPacketHeader) + |
| 349 | cmd.payload.size()); |
| 350 | NCSIPacketHeader* hdr = (NCSIPacketHeader*)pl.data(); |
| 351 | |
| 352 | std::copy(cmd.payload.begin(), cmd.payload.end(), |
| 353 | pl.begin() + sizeof(NCSIPacketHeader)); |
| 354 | |
| 355 | hdr->type = cmd.ncsi_cmd; |
| 356 | hdr->length = htons(cmd.payload.size()); |
| 357 | |
| 358 | ret = nla_put(msg.get(), ncsi_nl_attrs::NCSI_ATTR_DATA, pl.size(), |
| 359 | pl.data()); |
| 360 | if (ret < 0) |
| 361 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 362 | lg2::error("Failed to set the data attribute, RC : {RC}", "RC", |
| 363 | ret); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 364 | return ret; |
| 365 | } |
| 366 | |
| 367 | nl_socket_disable_seq_check(socket.get()); |
| 368 | } |
| 369 | |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 370 | if (function) |
| 371 | { |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 372 | cb_ret = 1; |
| 373 | |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 374 | // Add a callback function to the socket |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 375 | nl_socket_modify_cb(socket.get(), NL_CB_VALID, NL_CB_CUSTOM, function, |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 376 | &cb_ret); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 377 | } |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 378 | |
| 379 | ret = nl_send_auto(socket.get(), msg.get()); |
| 380 | if (ret < 0) |
| 381 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 382 | lg2::error("Failed to send the message , RC : {RC}", "RC", ret); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 383 | return ret; |
| 384 | } |
| 385 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 386 | do |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 387 | { |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 388 | ret = nl_recvmsgs_default(socket.get()); |
| 389 | if (ret < 0) |
| 390 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 391 | lg2::error("Failed to receive the message , RC : {RC}", "RC", ret); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 392 | break; |
| 393 | } |
| 394 | } while (cb_ret); |
| 395 | |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 396 | return ret; |
| 397 | } |
| 398 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 399 | } // namespace internal |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 400 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 401 | int sendOemCommand(int ifindex, int package, int channel, |
| 402 | std::span<const unsigned char> payload) |
| 403 | { |
| 404 | constexpr auto cmd = 0x50; |
| 405 | |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 406 | lg2::debug("Send OEM Command, CHANNEL : {CHANNEL} , PACKAGE : {PACKAGE}, " |
| 407 | "INTERFACE_INDEX: {INTERFACE_INDEX}", |
| 408 | "CHANNEL", lg2::hex, channel, "PACKAGE", lg2::hex, package, |
| 409 | "INTERFACE_INDEX", lg2::hex, ifindex); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 410 | if (!payload.empty()) |
| 411 | { |
William A. Kennington III | cbe5073 | 2023-07-25 13:52:18 -0700 | [diff] [blame^] | 412 | lg2::debug("Payload: {PAYLOAD}", "PAYLOAD", toHexStr(payload)); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | return internal::applyCmd( |
| 416 | ifindex, |
| 417 | internal::Command(ncsi_nl_commands::NCSI_CMD_SEND_CMD, cmd, payload), |
| 418 | package, channel, NONE, internal::sendCallBack); |
| 419 | } |
| 420 | |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 421 | int setChannel(int ifindex, int package, int channel) |
| 422 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 423 | lg2::debug( |
| 424 | "Set CHANNEL : {CHANNEL} , PACKAGE : {PACKAGE}, INTERFACE_INDEX: " |
| 425 | "{INTERFACE_INDEX}", |
| 426 | "CHANNEL", lg2::hex, channel, "PACKAGE", lg2::hex, package, |
| 427 | "INTERFACE_INDEX", lg2::hex, ifindex); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 428 | return internal::applyCmd( |
| 429 | ifindex, internal::Command(ncsi_nl_commands::NCSI_CMD_SET_INTERFACE), |
| 430 | package, channel); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | int clearInterface(int ifindex) |
| 434 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 435 | lg2::debug("ClearInterface , INTERFACE_INDEX : {INTERFACE_INDEX}", |
| 436 | "INTERFACE_INDEX", lg2::hex, ifindex); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 437 | return internal::applyCmd( |
| 438 | ifindex, internal::Command(ncsi_nl_commands::NCSI_CMD_CLEAR_INTERFACE)); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 439 | } |
| 440 | |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 441 | int getInfo(int ifindex, int package) |
| 442 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 443 | lg2::debug( |
| 444 | "Get Info , PACKAGE : {PACKAGE}, INTERFACE_INDEX: {INTERFACE_INDEX}", |
| 445 | "PACKAGE", lg2::hex, package, "INTERFACE_INDEX", lg2::hex, ifindex); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 446 | if (package == DEFAULT_VALUE) |
| 447 | { |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 448 | return internal::applyCmd( |
| 449 | ifindex, internal::Command(ncsi_nl_commands::NCSI_CMD_PKG_INFO), |
| 450 | package, DEFAULT_VALUE, NLM_F_DUMP, internal::infoCallBack); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 451 | } |
| 452 | else |
| 453 | { |
| 454 | return internal::applyCmd(ifindex, ncsi_nl_commands::NCSI_CMD_PKG_INFO, |
| 455 | package, DEFAULT_VALUE, NONE, |
| 456 | internal::infoCallBack); |
| 457 | } |
| 458 | } |
| 459 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 460 | } // namespace ncsi |
| 461 | } // namespace network |
| 462 | } // namespace phosphor |