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