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 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 9 | #include <iomanip> |
Manojkiran Eda | e5a867a | 2020-06-01 18:08:30 +0530 | [diff] [blame] | 10 | #include <iostream> |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 11 | #include <phosphor-logging/lg2.hpp> |
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 | |
| 23 | namespace internal |
| 24 | { |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 25 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 26 | struct NCSIPacketHeader |
| 27 | { |
| 28 | uint8_t MCID; |
| 29 | uint8_t revision; |
| 30 | uint8_t reserved; |
| 31 | uint8_t id; |
| 32 | uint8_t type; |
| 33 | uint8_t channel; |
| 34 | uint16_t length; |
| 35 | uint32_t rsvd[2]; |
| 36 | }; |
| 37 | |
| 38 | class Command |
| 39 | { |
| 40 | public: |
| 41 | Command() = delete; |
| 42 | ~Command() = default; |
| 43 | Command(const Command&) = delete; |
| 44 | Command& operator=(const Command&) = delete; |
| 45 | Command(Command&&) = default; |
| 46 | Command& operator=(Command&&) = default; |
| 47 | Command( |
| 48 | int c, int nc = DEFAULT_VALUE, |
| 49 | std::span<const unsigned char> p = std::span<const unsigned char>()) : |
| 50 | cmd(c), |
| 51 | ncsi_cmd(nc), payload(p) |
| 52 | { |
| 53 | } |
| 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 | { |
Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 190 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 191 | auto link = |
| 192 | nla_get_u32(channeltb[NCSI_CHANNEL_ATTR_LINK_STATE]); |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 193 | lg2::debug("Channel Link State : {LINK_STATE}", "LINK_STATE", |
| 194 | lg2::hex, link); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 195 | } |
| 196 | if (channeltb[NCSI_CHANNEL_ATTR_VLAN_LIST]) |
| 197 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 198 | lg2::debug("Active Vlan ids"); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 199 | auto vids = channeltb[NCSI_CHANNEL_ATTR_VLAN_LIST]; |
| 200 | auto vid = static_cast<nlattr*>(nla_data(vids)); |
| 201 | auto len = nla_len(vids); |
| 202 | while (nla_ok(vid, len)) |
| 203 | { |
| 204 | auto id = nla_get_u16(vid); |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 205 | lg2::debug("VID : {VLAN_ID}", "VLAN_ID", id); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 206 | vid = nla_next(vid, &len); |
| 207 | } |
| 208 | } |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 209 | } |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 210 | } |
| 211 | return (int)NL_SKIP; |
| 212 | }; |
| 213 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 214 | CallBack sendCallBack = [](struct nl_msg* msg, void* arg) { |
| 215 | using namespace phosphor::network::ncsi; |
| 216 | auto nlh = nlmsg_hdr(msg); |
| 217 | struct nlattr* tb[NCSI_ATTR_MAX + 1] = {nullptr}; |
| 218 | static struct nla_policy ncsiPolicy[NCSI_ATTR_MAX + 1] = { |
| 219 | {NLA_UNSPEC, 0, 0}, {NLA_U32, 0, 0}, {NLA_NESTED, 0, 0}, |
| 220 | {NLA_U32, 0, 0}, {NLA_U32, 0, 0}, {NLA_BINARY, 0, 0}, |
| 221 | {NLA_FLAG, 0, 0}, {NLA_U32, 0, 0}, {NLA_U32, 0, 0}, |
| 222 | }; |
| 223 | |
| 224 | *(int*)arg = 0; |
| 225 | |
| 226 | auto ret = genlmsg_parse(nlh, 0, tb, NCSI_ATTR_MAX, ncsiPolicy); |
| 227 | if (ret) |
| 228 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 229 | lg2::error("Failed to parse package"); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 230 | return ret; |
| 231 | } |
| 232 | |
Jian Zhang | 442d9e5 | 2022-10-20 22:11:14 +0800 | [diff] [blame] | 233 | if (tb[NCSI_ATTR_DATA] == nullptr) |
| 234 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 235 | lg2::error("Response: No data"); |
Jian Zhang | 442d9e5 | 2022-10-20 22:11:14 +0800 | [diff] [blame] | 236 | return -1; |
| 237 | } |
| 238 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 239 | auto data_len = nla_len(tb[NCSI_ATTR_DATA]) - sizeof(NCSIPacketHeader); |
| 240 | unsigned char* data = |
| 241 | (unsigned char*)nla_data(tb[NCSI_ATTR_DATA]) + sizeof(NCSIPacketHeader); |
| 242 | auto s = std::span<const unsigned char>(data, data_len); |
| 243 | |
| 244 | // Dump the response to stdout. Enhancement: option to save response data |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 245 | lg2::debug("Response : {DATA_LEN} bytes, {DATA}", "DATA_LEN", data_len, |
| 246 | "DATA", |
| 247 | fmt::format("{:02x}", fmt::join(s.begin(), s.end(), " "))); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 248 | |
| 249 | return 0; |
| 250 | }; |
| 251 | |
| 252 | int applyCmd(int ifindex, const Command& cmd, int package = DEFAULT_VALUE, |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 253 | int channel = DEFAULT_VALUE, int flags = NONE, |
| 254 | CallBack function = nullptr) |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 255 | { |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 256 | int cb_ret = 0; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 257 | nlSocketPtr socket(nl_socket_alloc(), &::nl_socket_free); |
Johnathan Mantey | d49c5c6 | 2021-06-23 09:14:42 -0700 | [diff] [blame] | 258 | if (socket == nullptr) |
| 259 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 260 | lg2::error("Unable to allocate memory for the socket"); |
Johnathan Mantey | d49c5c6 | 2021-06-23 09:14:42 -0700 | [diff] [blame] | 261 | return -ENOMEM; |
| 262 | } |
| 263 | |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 264 | auto ret = genl_connect(socket.get()); |
| 265 | if (ret < 0) |
| 266 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 267 | lg2::error("Failed to open the socket , RC : {RC}", "RC", ret); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | auto driverID = genl_ctrl_resolve(socket.get(), "NCSI"); |
| 272 | if (driverID < 0) |
| 273 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 274 | lg2::error("Failed to resolve, RC : {RC}", "RC", ret); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 275 | return driverID; |
| 276 | } |
| 277 | |
| 278 | nlMsgPtr msg(nlmsg_alloc(), &::nlmsg_free); |
Johnathan Mantey | d49c5c6 | 2021-06-23 09:14:42 -0700 | [diff] [blame] | 279 | if (msg == nullptr) |
| 280 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 281 | lg2::error("Unable to allocate memory for the message"); |
Johnathan Mantey | d49c5c6 | 2021-06-23 09:14:42 -0700 | [diff] [blame] | 282 | return -ENOMEM; |
| 283 | } |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 284 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 285 | 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] | 286 | if (!msgHdr) |
| 287 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 288 | lg2::error("Unable to add the netlink headers , COMMAND : {COMMAND}", |
| 289 | "COMMAND", cmd.cmd); |
Johnathan Mantey | d49c5c6 | 2021-06-23 09:14:42 -0700 | [diff] [blame] | 290 | return -ENOMEM; |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | if (package != DEFAULT_VALUE) |
| 294 | { |
| 295 | ret = nla_put_u32(msg.get(), ncsi_nl_attrs::NCSI_ATTR_PACKAGE_ID, |
| 296 | package); |
| 297 | if (ret < 0) |
| 298 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 299 | lg2::error("Failed to set the attribute , RC : {RC} PACKAGE " |
| 300 | "{PACKAGE}", |
| 301 | "RC", ret, "PACKAGE", lg2::hex, package); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 302 | return ret; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (channel != DEFAULT_VALUE) |
| 307 | { |
| 308 | ret = nla_put_u32(msg.get(), ncsi_nl_attrs::NCSI_ATTR_CHANNEL_ID, |
| 309 | channel); |
| 310 | if (ret < 0) |
| 311 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 312 | lg2::error("Failed to set the attribute , RC : {RC} CHANNEL : " |
| 313 | "{CHANNEL}", |
| 314 | "RC", ret, "CHANNEL", lg2::hex, channel); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 315 | return ret; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | ret = nla_put_u32(msg.get(), ncsi_nl_attrs::NCSI_ATTR_IFINDEX, ifindex); |
| 320 | if (ret < 0) |
| 321 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 322 | lg2::error("Failed to set the attribute , RC : {RC} INTERFACE : " |
| 323 | "{INTERFACE}", |
| 324 | "RC", ret, "INTERFACE", lg2::hex, ifindex); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 325 | return ret; |
| 326 | } |
| 327 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 328 | if (cmd.ncsi_cmd != DEFAULT_VALUE) |
| 329 | { |
| 330 | std::vector<unsigned char> pl(sizeof(NCSIPacketHeader) + |
| 331 | cmd.payload.size()); |
| 332 | NCSIPacketHeader* hdr = (NCSIPacketHeader*)pl.data(); |
| 333 | |
| 334 | std::copy(cmd.payload.begin(), cmd.payload.end(), |
| 335 | pl.begin() + sizeof(NCSIPacketHeader)); |
| 336 | |
| 337 | hdr->type = cmd.ncsi_cmd; |
| 338 | hdr->length = htons(cmd.payload.size()); |
| 339 | |
| 340 | ret = nla_put(msg.get(), ncsi_nl_attrs::NCSI_ATTR_DATA, pl.size(), |
| 341 | pl.data()); |
| 342 | if (ret < 0) |
| 343 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 344 | lg2::error("Failed to set the data attribute, RC : {RC}", "RC", |
| 345 | ret); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | nl_socket_disable_seq_check(socket.get()); |
| 350 | } |
| 351 | |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 352 | if (function) |
| 353 | { |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 354 | cb_ret = 1; |
| 355 | |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 356 | // Add a callback function to the socket |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 357 | 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] | 358 | &cb_ret); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 359 | } |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 360 | |
| 361 | ret = nl_send_auto(socket.get(), msg.get()); |
| 362 | if (ret < 0) |
| 363 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 364 | lg2::error("Failed to send the message , RC : {RC}", "RC", ret); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 365 | return ret; |
| 366 | } |
| 367 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 368 | do |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 369 | { |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 370 | ret = nl_recvmsgs_default(socket.get()); |
| 371 | if (ret < 0) |
| 372 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 373 | lg2::error("Failed to receive the message , RC : {RC}", "RC", ret); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 374 | break; |
| 375 | } |
| 376 | } while (cb_ret); |
| 377 | |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 378 | return ret; |
| 379 | } |
| 380 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 381 | } // namespace internal |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 382 | |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 383 | int sendOemCommand(int ifindex, int package, int channel, |
| 384 | std::span<const unsigned char> payload) |
| 385 | { |
| 386 | constexpr auto cmd = 0x50; |
| 387 | |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 388 | lg2::debug("Send OEM Command, CHANNEL : {CHANNEL} , PACKAGE : {PACKAGE}, " |
| 389 | "INTERFACE_INDEX: {INTERFACE_INDEX}", |
| 390 | "CHANNEL", lg2::hex, channel, "PACKAGE", lg2::hex, package, |
| 391 | "INTERFACE_INDEX", lg2::hex, ifindex); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 392 | if (!payload.empty()) |
| 393 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 394 | std::string payloadStr; |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 395 | for (auto& i : payload) |
| 396 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 397 | payloadStr += fmt::format(" {:02x}", (int)i); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 398 | } |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 399 | lg2::debug("Payload :{PAYLOAD}", "PAYLOAD", payloadStr); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | return internal::applyCmd( |
| 403 | ifindex, |
| 404 | internal::Command(ncsi_nl_commands::NCSI_CMD_SEND_CMD, cmd, payload), |
| 405 | package, channel, NONE, internal::sendCallBack); |
| 406 | } |
| 407 | |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 408 | int setChannel(int ifindex, int package, int channel) |
| 409 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 410 | lg2::debug( |
| 411 | "Set CHANNEL : {CHANNEL} , PACKAGE : {PACKAGE}, INTERFACE_INDEX: " |
| 412 | "{INTERFACE_INDEX}", |
| 413 | "CHANNEL", lg2::hex, channel, "PACKAGE", lg2::hex, package, |
| 414 | "INTERFACE_INDEX", lg2::hex, ifindex); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 415 | return internal::applyCmd( |
| 416 | ifindex, internal::Command(ncsi_nl_commands::NCSI_CMD_SET_INTERFACE), |
| 417 | package, channel); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | int clearInterface(int ifindex) |
| 421 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 422 | lg2::debug("ClearInterface , INTERFACE_INDEX : {INTERFACE_INDEX}", |
| 423 | "INTERFACE_INDEX", lg2::hex, ifindex); |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 424 | return internal::applyCmd( |
| 425 | ifindex, internal::Command(ncsi_nl_commands::NCSI_CMD_CLEAR_INTERFACE)); |
Ratan Gupta | bbe4579 | 2018-03-23 00:22:55 +0530 | [diff] [blame] | 426 | } |
| 427 | |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 428 | int getInfo(int ifindex, int package) |
| 429 | { |
Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 430 | lg2::debug( |
| 431 | "Get Info , PACKAGE : {PACKAGE}, INTERFACE_INDEX: {INTERFACE_INDEX}", |
| 432 | "PACKAGE", lg2::hex, package, "INTERFACE_INDEX", lg2::hex, ifindex); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 433 | if (package == DEFAULT_VALUE) |
| 434 | { |
Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 435 | return internal::applyCmd( |
| 436 | ifindex, internal::Command(ncsi_nl_commands::NCSI_CMD_PKG_INFO), |
| 437 | package, DEFAULT_VALUE, NLM_F_DUMP, internal::infoCallBack); |
Ratan Gupta | aac603e | 2018-03-23 00:25:54 +0530 | [diff] [blame] | 438 | } |
| 439 | else |
| 440 | { |
| 441 | return internal::applyCmd(ifindex, ncsi_nl_commands::NCSI_CMD_PKG_INFO, |
| 442 | package, DEFAULT_VALUE, NONE, |
| 443 | internal::infoCallBack); |
| 444 | } |
| 445 | } |
| 446 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 447 | } // namespace ncsi |
| 448 | } // namespace network |
| 449 | } // namespace phosphor |