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