1 /* 2 * Copyright (c) 2025-2026 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_NETWORKSLICE_KERNEL_PROXY_H 17 #define OHOS_NETWORKSLICE_KERNEL_PROXY_H 18 19 #include <cstdint> 20 #include <shared_mutex> 21 #include <unordered_map> 22 #include <set> 23 #include <vector> 24 #include <linux/netlink.h> 25 #include "networkslice_service_base.h" 26 #include "singleton.h" 27 #include "ffrt_inner.h" 28 namespace OHOS { 29 namespace NetManagerStandard { 30 namespace { 31 constexpr int32_t NETLINK_SOCKET_DEFAULT = -1; 32 } 33 34 enum KernelReqMsgType { 35 KERNEL_MSG_APP_QOE_SYNC_CMD = 0, 36 KERNEL_MSG_UPDATE_APP_INFO_CMD = 1, 37 KERNEL_MSG_TCP_PKT_COLLEC_CMD = 6, 38 KERNEL_MSG_ICMP_PING_DETECT_CMD = 11, 39 KERNEL_MSG_WIFI_PARA_COLLECT_START = 20, 40 KERNEL_MSG_WIFI_PARA_COLLECT_STOP = 21, 41 KERNEL_MSG_WIFI_PARA_COLLECT_UPDATE = 27, 42 KERNEL_MSG_TRAFFIC_DATA_NETWORK_CHANGE = 34, 43 KERNEL_MSG_TRAFFIC_UIDS = 35, 44 KERNEL_MSG_FG_CHANGE_UID = 44, 45 KERNEL_MSG_SOCKET_CLOSE_DETECT_UID_ADD = 46, 46 KERNEL_MSG_SOCKET_CLOSE_DETECT_UID_DEL = 47, 47 KERNEL_MSG_APP_ACCELERATOR_CMD = 51, 48 49 // QOE 模块使用 100 ~200 50 K_MSG_QOE_STREAM_CFG = 100, 51 K_MSG_QOE_STREAM_HOOK_ADD = 101, 52 K_MSG_QOE_STREAM_HOOK_DEL = 102 53 }; 54 55 struct KernelIpRptEnableMsg { 56 int16_t type; // Event enumeration values 57 int16_t len; // The length behind this field, the limit lower 2048 58 int16_t isEnable; 59 }; 60 61 struct KernelBindMsg { 62 int16_t type; // Event enumeration values 63 int16_t len; // The length behind this field, the limit lower 2048 64 unsigned char buf[0]; 65 }; 66 67 enum KernelRsqMsgType { 68 KERNEL_RSP_APP_QOE = 0, 69 KERNEL_RSP_FG_UID_DROP = 1, 70 KERNEL_RSP_FG_UID_RECOVERY = 2, 71 KERNEL_RSP_TCP_PKT_CONUT = 3, 72 KERNEL_RSP_SLICE_IP_PARA = 4, 73 KERNEL_RSP_ICMP_PING_REPORT = 5, 74 KERNEL_RSP_PACKET_DELAY = 6, 75 KERNEL_RSP_FASTGRAB_CHR = 7, 76 KERNEL_RSP_STEADY_SPEED_STATS = 8, 77 KERNEL_RSP_WIFI_PARA = 9, 78 KERNEL_RSP_SPEED_TEST_CHR = 10, 79 KERNEL_RSP_STREAM_DETECTION, 80 KERNEL_RSP_TRAFFIC_STATS_INFO = 14, 81 KERNEL_RSP_IPV6_SYNC_ABNORMAL_REPORT_UID = 16, 82 KERNEL_RSP_SOCKET_CLOSE_CHR_MSG_ID = 17, 83 KERNEL_RSP_TCP_RESET_CHR_MSG_ID = 18, 84 KERNEL_RSP_APP_PROXY_RESULT = 19, 85 KERNEL_RSP_TOP_APP_ABN_STAT_REPORT = 20, 86 KERNEL_RSP_NBMSG_RPT_BUTT 87 }; 88 89 struct NetlinkInfo { 90 struct nlmsghdr hdr; 91 unsigned char data[0]; 92 }; 93 94 /* Each module sends the message request is defined as: */ 95 struct KernelMsg { 96 int16_t type; // Event enumeration values 97 int16_t len; // The length behind this field, the limit lower 2048 98 char buf[0]; 99 }; 100 101 struct KernelMsgNS { 102 char buf[0]; 103 }; 104 105 struct KernelCmdMsg { 106 int16_t cmd; 107 int16_t cnt; 108 int32_t interval; 109 }; 110 111 class NetworkSliceKernelProxy { 112 DECLARE_DELAYED_SINGLETON(NetworkSliceKernelProxy); 113 public: 114 void RegistHandler(NetworkSliceSubModule moduleId, NetworkSliceServiceBase* handler, 115 const std::vector<int16_t>& msgTypeVec); 116 void UnRegistHandler(NetworkSliceSubModule moduleId); 117 int32_t StartNetlink(); 118 void StartRecvThread(); 119 void StopRecvThread(); 120 int32_t SendDataToKernel(KernelMsg &msgData); 121 private: 122 int32_t NetlinkInit(); 123 int32_t SendMsgToKernel(int32_t type, nlmsghdr *nlmsg, size_t nlmsgLen); 124 void RecvThread(); 125 static void RecvKernelData(void *data, uint32_t event); 126 bool IsValidDataLen(int32_t dataLen); 127 void DispatchKernelMsg(void *msg, int32_t dataLen); 128 129 int32_t netlinkSocket_ { NETLINK_SOCKET_DEFAULT }; 130 std::unordered_map<NetworkSliceSubModule, NetworkSliceServiceBase*> moduleIdHandlerMap_ {}; 131 std::unordered_map<int16_t, std::set<NetworkSliceSubModule>> msgTypeModuleIdsMap_ {}; 132 std::unordered_map<NetworkSliceSubModule, std::vector<int16_t>> moduleIdMsgTypesMap_ {}; 133 std::shared_timed_mutex mutex_ {}; 134 ffrt_qos_t taskQos_ = 0; 135 }; 136 } // namespace NetManagerStandard 137 } // namespace OHOS 138 #endif 139