|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include "nl_api.h" |
| 9 | + |
| 10 | +namespace L0 { |
| 11 | + |
| 12 | +static constexpr std::string_view libgenlFile = "libnl-genl-3.so.200"; |
| 13 | +static constexpr std::string_view genlConnectRoutine = "genl_connect"; |
| 14 | +static constexpr std::string_view genlCtrlResolveRoutine = "genl_ctrl_resolve"; |
| 15 | +static constexpr std::string_view genlHandleMsgRoutine = "genl_handle_msg"; |
| 16 | +static constexpr std::string_view genlmsgPutRoutine = "genlmsg_put"; |
| 17 | +static constexpr std::string_view genlOpsResolveRoutine = "genl_ops_resolve"; |
| 18 | +static constexpr std::string_view genlRegisterFamilyRoutine = "genl_register_family"; |
| 19 | +static constexpr std::string_view genlUnregisterFamilyRoutine = "genl_unregister_family"; |
| 20 | +static constexpr std::string_view nlRecvmsgsDefaultRoutine = "nl_recvmsgs_default"; |
| 21 | +static constexpr std::string_view nlSendAutoRoutine = "nl_send_auto"; |
| 22 | +static constexpr std::string_view nlSocketAllocRoutine = "nl_socket_alloc"; |
| 23 | +static constexpr std::string_view nlSocketDisableSeqCheckRoutine = "nl_socket_disable_seq_check"; |
| 24 | +static constexpr std::string_view nlSocketFreeRoutine = "nl_socket_free"; |
| 25 | +static constexpr std::string_view nlSocketModifyCbRoutine = "nl_socket_modify_cb"; |
| 26 | +static constexpr std::string_view nlaDataRoutine = "nla_data"; |
| 27 | +static constexpr std::string_view nlaGetU32Routine = "nla_get_u32"; |
| 28 | +static constexpr std::string_view nlaGetU64Routine = "nla_get_u64"; |
| 29 | +static constexpr std::string_view nlaGetU8Routine = "nla_get_u8"; |
| 30 | +static constexpr std::string_view nlaIsNestedRoutine = "nla_is_nested"; |
| 31 | +static constexpr std::string_view nlaLenRoutine = "nla_len"; |
| 32 | +static constexpr std::string_view nlaNextRoutine = "nla_next"; |
| 33 | +static constexpr std::string_view nlaOkRoutine = "nla_ok"; |
| 34 | +static constexpr std::string_view nlaPutU16Routine = "nla_put_u16"; |
| 35 | +static constexpr std::string_view nlaPutU32Routine = "nla_put_u32"; |
| 36 | +static constexpr std::string_view nlaPutU64Routine = "nla_put_u64"; |
| 37 | +static constexpr std::string_view nlaPutU8Routine = "nla_put_u8"; |
| 38 | +static constexpr std::string_view nlaTypeRoutine = "nla_type"; |
| 39 | +static constexpr std::string_view nlmsgAllocRoutine = "nlmsg_alloc"; |
| 40 | +static constexpr std::string_view nlmsgAttrdataRoutine = "nlmsg_attrdata"; |
| 41 | +static constexpr std::string_view nlmsgAttrlenRoutine = "nlmsg_attrlen"; |
| 42 | +static constexpr std::string_view nlmsgFreeRoutine = "nlmsg_free"; |
| 43 | +static constexpr std::string_view nlmsgHdrRoutine = "nlmsg_hdr"; |
| 44 | + |
| 45 | +template <class T> |
| 46 | +bool NlApi::getSymbolAddr(const std::string_view &name, T &sym) { |
| 47 | + sym = reinterpret_cast<T>(genlLibraryHandle->getProcAddress(std::string(name))); |
| 48 | + return nullptr != sym; |
| 49 | +} |
| 50 | + |
| 51 | +bool NlApi::loadEntryPoints() { |
| 52 | + if (!isAvailable()) |
| 53 | + return false; |
| 54 | + |
| 55 | + bool ok = true; |
| 56 | + ok = getSymbolAddr(genlConnectRoutine, genlConnectEntry); |
| 57 | + ok = ok && getSymbolAddr(genlCtrlResolveRoutine, genlCtrlResolveEntry); |
| 58 | + ok = ok && getSymbolAddr(genlHandleMsgRoutine, genlHandleMsgEntry); |
| 59 | + ok = ok && getSymbolAddr(genlmsgPutRoutine, genlmsgPutEntry); |
| 60 | + ok = ok && getSymbolAddr(genlOpsResolveRoutine, genlOpsResolveEntry); |
| 61 | + ok = ok && getSymbolAddr(genlRegisterFamilyRoutine, genlRegisterFamilyEntry); |
| 62 | + ok = ok && getSymbolAddr(genlUnregisterFamilyRoutine, genlUnregisterFamilyEntry); |
| 63 | + ok = ok && getSymbolAddr(nlRecvmsgsDefaultRoutine, nlRecvmsgsDefaultEntry); |
| 64 | + ok = ok && getSymbolAddr(nlSendAutoRoutine, nlSendAutoEntry); |
| 65 | + ok = ok && getSymbolAddr(nlSocketAllocRoutine, nlSocketAllocEntry); |
| 66 | + ok = ok && getSymbolAddr(nlSocketDisableSeqCheckRoutine, nlSocketDisableSeqCheckEntry); |
| 67 | + ok = ok && getSymbolAddr(nlSocketFreeRoutine, nlSocketFreeEntry); |
| 68 | + ok = ok && getSymbolAddr(nlSocketModifyCbRoutine, nlSocketModifyCbEntry); |
| 69 | + ok = ok && getSymbolAddr(nlaDataRoutine, nlaDataEntry); |
| 70 | + ok = ok && getSymbolAddr(nlaGetU32Routine, nlaGetU32Entry); |
| 71 | + ok = ok && getSymbolAddr(nlaGetU64Routine, nlaGetU64Entry); |
| 72 | + ok = ok && getSymbolAddr(nlaGetU8Routine, nlaGetU8Entry); |
| 73 | + ok = ok && getSymbolAddr(nlaIsNestedRoutine, nlaIsNestedEntry); |
| 74 | + ok = ok && getSymbolAddr(nlaLenRoutine, nlaLenEntry); |
| 75 | + ok = ok && getSymbolAddr(nlaNextRoutine, nlaNextEntry); |
| 76 | + ok = ok && getSymbolAddr(nlaOkRoutine, nlaOkEntry); |
| 77 | + ok = ok && getSymbolAddr(nlaPutU16Routine, nlaPutU16Entry); |
| 78 | + ok = ok && getSymbolAddr(nlaPutU32Routine, nlaPutU32Entry); |
| 79 | + ok = ok && getSymbolAddr(nlaPutU64Routine, nlaPutU64Entry); |
| 80 | + ok = ok && getSymbolAddr(nlaPutU8Routine, nlaPutU8Entry); |
| 81 | + ok = ok && getSymbolAddr(nlaTypeRoutine, nlaTypeEntry); |
| 82 | + ok = ok && getSymbolAddr(nlmsgAllocRoutine, nlmsgAllocEntry); |
| 83 | + ok = ok && getSymbolAddr(nlmsgAttrdataRoutine, nlmsgAttrdataEntry); |
| 84 | + ok = ok && getSymbolAddr(nlmsgAttrlenRoutine, nlmsgAttrlenEntry); |
| 85 | + ok = ok && getSymbolAddr(nlmsgFreeRoutine, nlmsgFreeEntry); |
| 86 | + ok = ok && getSymbolAddr(nlmsgHdrRoutine, nlmsgHdrEntry); |
| 87 | + |
| 88 | + return ok; |
| 89 | +} |
| 90 | + |
| 91 | +int NlApi::genlConnect(struct nl_sock *sock) { |
| 92 | + UNRECOVERABLE_IF(nullptr == genlConnectEntry); |
| 93 | + return (*genlConnectEntry)(sock); |
| 94 | +} |
| 95 | + |
| 96 | +int NlApi::genlCtrlResolve(struct nl_sock *sock, const char *name) { |
| 97 | + UNRECOVERABLE_IF(nullptr == genlCtrlResolveEntry); |
| 98 | + return (*genlCtrlResolveEntry)(sock, name); |
| 99 | +} |
| 100 | + |
| 101 | +int NlApi::genlHandleMsg(struct nl_msg *msg, void *arg) { |
| 102 | + UNRECOVERABLE_IF(nullptr == genlHandleMsgEntry); |
| 103 | + return (*genlHandleMsgEntry)(msg, arg); |
| 104 | +} |
| 105 | + |
| 106 | +void *NlApi::genlmsgPut(struct nl_msg *msg, uint32_t port, uint32_t seq, int family, int hdrlen, int flags, uint8_t cmd, uint8_t version) { |
| 107 | + UNRECOVERABLE_IF(nullptr == genlmsgPutEntry); |
| 108 | + return (*genlmsgPutEntry)(msg, port, seq, family, hdrlen, flags, cmd, version); |
| 109 | +} |
| 110 | + |
| 111 | +int NlApi::genlOpsResolve(struct nl_sock *sock, struct genl_ops *ops) { |
| 112 | + UNRECOVERABLE_IF(nullptr == genlOpsResolveEntry); |
| 113 | + return (*genlOpsResolveEntry)(sock, ops); |
| 114 | +} |
| 115 | + |
| 116 | +int NlApi::genlRegisterFamily(struct genl_ops *ops) { |
| 117 | + UNRECOVERABLE_IF(nullptr == genlRegisterFamilyEntry); |
| 118 | + return (*genlRegisterFamilyEntry)(ops); |
| 119 | +} |
| 120 | + |
| 121 | +int NlApi::genlUnregisterFamily(struct genl_ops *ops) { |
| 122 | + UNRECOVERABLE_IF(nullptr == genlUnregisterFamilyEntry); |
| 123 | + return (*genlUnregisterFamilyEntry)(ops); |
| 124 | +} |
| 125 | + |
| 126 | +int NlApi::nlRecvmsgsDefault(struct nl_sock *sock) { |
| 127 | + UNRECOVERABLE_IF(nullptr == nlRecvmsgsDefaultEntry); |
| 128 | + return (*nlRecvmsgsDefaultEntry)(sock); |
| 129 | +} |
| 130 | + |
| 131 | +int NlApi::nlSendAuto(struct nl_sock *sock, struct nl_msg *msg) { |
| 132 | + UNRECOVERABLE_IF(nullptr == nlSendAutoEntry); |
| 133 | + return (*nlSendAutoEntry)(sock, msg); |
| 134 | +} |
| 135 | + |
| 136 | +struct nl_sock *NlApi::nlSocketAlloc() { |
| 137 | + UNRECOVERABLE_IF(nullptr == nlSocketAllocEntry); |
| 138 | + return (*nlSocketAllocEntry)(); |
| 139 | +} |
| 140 | + |
| 141 | +void NlApi::nlSocketDisableSeqCheck(struct nl_sock *sock) { |
| 142 | + UNRECOVERABLE_IF(nullptr == nlSocketDisableSeqCheckEntry); |
| 143 | + (*nlSocketDisableSeqCheckEntry)(sock); |
| 144 | + return; |
| 145 | +} |
| 146 | + |
| 147 | +void NlApi::nlSocketFree(struct nl_sock *sock) { |
| 148 | + UNRECOVERABLE_IF(nullptr == nlSocketFreeEntry); |
| 149 | + (*nlSocketFreeEntry)(sock); |
| 150 | + return; |
| 151 | +} |
| 152 | + |
| 153 | +int NlApi::nlSocketModifyCb(struct nl_sock *sock, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t cb, void *arg) { |
| 154 | + UNRECOVERABLE_IF(nullptr == nlSocketModifyCbEntry); |
| 155 | + return (*nlSocketModifyCbEntry)(sock, type, kind, cb, arg); |
| 156 | +} |
| 157 | + |
| 158 | +void *NlApi::nlaData(const struct nlattr *attr) { |
| 159 | + UNRECOVERABLE_IF(nullptr == nlaDataEntry); |
| 160 | + return (*nlaDataEntry)(attr); |
| 161 | +} |
| 162 | + |
| 163 | +uint32_t NlApi::nlaGetU32(const struct nlattr *attr) { |
| 164 | + UNRECOVERABLE_IF(nullptr == nlaGetU32Entry); |
| 165 | + return (*nlaGetU32Entry)(attr); |
| 166 | +} |
| 167 | + |
| 168 | +uint64_t NlApi::nlaGetU64(const struct nlattr *attr) { |
| 169 | + UNRECOVERABLE_IF(nullptr == nlaGetU64Entry); |
| 170 | + return (*nlaGetU64Entry)(attr); |
| 171 | +} |
| 172 | + |
| 173 | +uint8_t NlApi::nlaGetU8(const struct nlattr *attr) { |
| 174 | + UNRECOVERABLE_IF(nullptr == nlaGetU8Entry); |
| 175 | + return (*nlaGetU8Entry)(attr); |
| 176 | +} |
| 177 | + |
| 178 | +int NlApi::nlaIsNested(const struct nlattr *attr) { |
| 179 | + UNRECOVERABLE_IF(nullptr == nlaIsNestedEntry); |
| 180 | + return (*nlaIsNestedEntry)(attr); |
| 181 | +} |
| 182 | + |
| 183 | +int NlApi::nlaLen(const struct nlattr *attr) { |
| 184 | + UNRECOVERABLE_IF(nullptr == nlaLenEntry); |
| 185 | + return (*nlaLenEntry)(attr); |
| 186 | +} |
| 187 | + |
| 188 | +struct nlattr *NlApi::nlaNext(const struct nlattr *attr, int *remaining) { |
| 189 | + UNRECOVERABLE_IF(nullptr == nlaNextEntry); |
| 190 | + return (*nlaNextEntry)(attr, remaining); |
| 191 | +} |
| 192 | + |
| 193 | +int NlApi::nlaOk(const struct nlattr *attr, int remaining) { |
| 194 | + UNRECOVERABLE_IF(nullptr == nlaOkEntry); |
| 195 | + return (*nlaOkEntry)(attr, remaining); |
| 196 | +} |
| 197 | + |
| 198 | +int NlApi::nlaPutU16(struct nl_msg *msg, int id, uint16_t data) { |
| 199 | + UNRECOVERABLE_IF(nullptr == nlaPutU16Entry); |
| 200 | + return (*nlaPutU16Entry)(msg, id, data); |
| 201 | +} |
| 202 | + |
| 203 | +int NlApi::nlaPutU32(struct nl_msg *msg, int id, uint32_t data) { |
| 204 | + UNRECOVERABLE_IF(nullptr == nlaPutU32Entry); |
| 205 | + return (*nlaPutU32Entry)(msg, id, data); |
| 206 | +} |
| 207 | + |
| 208 | +int NlApi::nlaPutU64(struct nl_msg *msg, int id, uint64_t data) { |
| 209 | + UNRECOVERABLE_IF(nullptr == nlaPutU64Entry); |
| 210 | + return (*nlaPutU64Entry)(msg, id, data); |
| 211 | +} |
| 212 | + |
| 213 | +int NlApi::nlaPutU8(struct nl_msg *msg, int id, uint8_t data) { |
| 214 | + UNRECOVERABLE_IF(nullptr == nlaPutU8Entry); |
| 215 | + return (*nlaPutU8Entry)(msg, id, data); |
| 216 | +} |
| 217 | + |
| 218 | +int NlApi::nlaType(const struct nlattr *attr) { |
| 219 | + UNRECOVERABLE_IF(nullptr == nlaTypeEntry); |
| 220 | + return (*nlaTypeEntry)(attr); |
| 221 | +} |
| 222 | + |
| 223 | +struct nl_msg *NlApi::nlmsgAlloc() { |
| 224 | + UNRECOVERABLE_IF(nullptr == nlmsgAllocEntry); |
| 225 | + return (*nlmsgAllocEntry)(); |
| 226 | +} |
| 227 | + |
| 228 | +struct nlattr *NlApi::nlmsgAttrdata(const struct nlmsghdr *hdr, int attr) { |
| 229 | + UNRECOVERABLE_IF(nullptr == nlmsgAttrdataEntry); |
| 230 | + return (*nlmsgAttrdataEntry)(hdr, attr); |
| 231 | +} |
| 232 | + |
| 233 | +int NlApi::nlmsgAttrlen(const struct nlmsghdr *hdr, int attr) { |
| 234 | + UNRECOVERABLE_IF(nullptr == nlmsgAttrlenEntry); |
| 235 | + return (*nlmsgAttrlenEntry)(hdr, attr); |
| 236 | +} |
| 237 | + |
| 238 | +void NlApi::nlmsgFree(struct nl_msg *msg) { |
| 239 | + UNRECOVERABLE_IF(nullptr == nlmsgFreeEntry); |
| 240 | + (*nlmsgFreeEntry)(msg); |
| 241 | + return; |
| 242 | +} |
| 243 | + |
| 244 | +struct nlmsghdr *NlApi::nlmsgHdr(struct nl_msg *msg) { |
| 245 | + UNRECOVERABLE_IF(nullptr == nlmsgHdrEntry); |
| 246 | + return (*nlmsgHdrEntry)(msg); |
| 247 | +} |
| 248 | + |
| 249 | +NlApi::NlApi() { |
| 250 | + genlLibraryHandle.reset(NEO::OsLibrary::load(std::string(libgenlFile))); |
| 251 | +} |
| 252 | + |
| 253 | +} // namespace L0 |
0 commit comments