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