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