Line data Source code
1 : /**
2 : * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "plugin_version_manager.h"
12 : #include "tsd_log.h"
13 :
14 : namespace tsd {
15 :
16 263 : PluginVersionManager::PluginVersionManager(
17 263 : PackageEnvInfo& envInfo, PackageHashStore& hashStore, ResponseCode& pkgRspCode)
18 263 : : envInfo_(envInfo), hashStore_(hashStore), pkgRspCode_(pkgRspCode)
19 263 : {}
20 :
21 17 : bool PluginVersionManager::IsCompatPluginPackage(const PackConfDetail& detail) const
22 : {
23 17 : return detail.decDstDir == DeviceInstallPath::COMPAT_PLUGIN_PATH;
24 : }
25 :
26 16 : PluginUpdateStrategy PluginVersionManager::GetPluginUpdateStrategy()
27 : {
28 16 : if (hasComputedPluginStrategy_) {
29 10 : return pluginUpdateStrategy_;
30 : }
31 :
32 6 : int64_t flag = 0;
33 : auto drvRet =
34 6 : halGetDeviceInfo(envInfo_.GetLogicDeviceId(), MODULE_TYPE_SYSTEM, INFO_TYPE_SWPLUGIN_UPGRADE_POLICY, &flag);
35 6 : if (drvRet != DRV_ERROR_NONE) {
36 2 : TSD_RUN_WARN(
37 : "[TsdClient][deviceId=%u] halGetDeviceInfo(INFO_TYPE_SWPLUGIN_UPGRADE_POLICY) failed, retCode[%d],"
38 : " fallback to PLUGIN_NOT_FORCE_UPDATE",
39 : envInfo_.GetLogicDeviceId(), drvRet);
40 2 : return PluginUpdateStrategy::PLUGIN_NOT_FORCE_UPDATE;
41 : }
42 4 : if (flag < static_cast<int64_t>(PluginUpdateStrategy::PLUGIN_NOT_FORCE_UPDATE) ||
43 4 : flag > static_cast<int64_t>(PluginUpdateStrategy::PLUGIN_NO_UPDATE)) {
44 1 : TSD_RUN_WARN(
45 : "[TsdClient][deviceId=%u] invalid plugin update strategy from DRV: %lld,"
46 : " fallback to PLUGIN_NOT_FORCE_UPDATE",
47 : envInfo_.GetLogicDeviceId(), flag);
48 1 : return PluginUpdateStrategy::PLUGIN_NOT_FORCE_UPDATE;
49 : }
50 3 : pluginUpdateStrategy_ = static_cast<PluginUpdateStrategy>(flag);
51 3 : hasComputedPluginStrategy_ = true;
52 3 : TSD_RUN_INFO("[TsdClient][deviceId=%u] plugin update strategy from DRV = %lld", envInfo_.GetLogicDeviceId(), flag);
53 3 : return pluginUpdateStrategy_;
54 : }
55 :
56 10 : bool PluginVersionManager::ShouldLoadCompatPluginPkg(const std::string& pkgPureName)
57 : {
58 10 : const PluginUpdateStrategy strategy = GetPluginUpdateStrategy();
59 10 : if (strategy == PluginUpdateStrategy::PLUGIN_NO_UPDATE) {
60 1 : TSD_RUN_INFO("plugin update strategy is PLUGIN_NO_UPDATE, skip pkg:%s", pkgPureName.c_str());
61 1 : return false;
62 : }
63 9 : if (strategy == PluginUpdateStrategy::PLUGIN_FORCE_UPDATE) {
64 2 : TSD_RUN_INFO(
65 : "plugin update strategy is PLUGIN_FORCE_UPDATE, fallback to checkcode compare pkg:%s", pkgPureName.c_str());
66 2 : return !hashStore_.IsCommonSinkHostAndDevicePkgSame(pkgPureName);
67 : }
68 7 : return CompareHostDeviceCompatPluginVersion(pkgPureName);
69 : }
70 :
71 7 : bool PluginVersionManager::CompareHostDeviceCompatPluginVersion(const std::string& pkgPureName)
72 : {
73 7 : const PluginPkgVersion hostInfo = PackageProcessConfig::GetInstance()->GetHostPluginVersion(pkgPureName);
74 7 : const auto itDev = devicePluginVersions_.find(pkgPureName);
75 7 : const PluginPkgVersion deviceInfo = (itDev != devicePluginVersions_.end()) ? itDev->second : PluginPkgVersion{};
76 7 : if (deviceInfo.version.empty()) {
77 2 : TSD_RUN_INFO("device plugin pkg:%s version unavailable, fallback to checkcode compare", pkgPureName.c_str());
78 2 : return !hashStore_.IsCommonSinkHostAndDevicePkgSame(pkgPureName);
79 : }
80 5 : if (hostInfo.Empty()) {
81 1 : TSD_RUN_WARN(
82 : "[TsdClient][deviceId=%u] host plugin pkg:%s version info unavailable, skip", envInfo_.GetLogicDeviceId(),
83 : pkgPureName.c_str());
84 1 : return false;
85 : }
86 4 : const int32_t cmp = PluginPkgVersionUtil::Compare(hostInfo, deviceInfo);
87 4 : if (cmp > 0) {
88 2 : TSD_RUN_INFO(
89 : "host plugin pkg:%s newer than device, load. host[%s/%s] device[%s/%s]", pkgPureName.c_str(),
90 : hostInfo.version.c_str(), hostInfo.timestamp.c_str(), deviceInfo.version.c_str(),
91 : deviceInfo.timestamp.c_str());
92 2 : return true;
93 : }
94 2 : TSD_RUN_INFO(
95 : "host plugin pkg:%s %s device, skip. host[%s/%s] device[%s/%s]", pkgPureName.c_str(),
96 : (cmp < 0 ? "older than" : "equals"), hostInfo.version.c_str(), hostInfo.timestamp.c_str(),
97 : deviceInfo.version.c_str(), deviceInfo.timestamp.c_str());
98 2 : return false;
99 7 : }
100 :
101 0 : void PluginVersionManager::HandleDevicePluginVersionRsp(const HDCMessage& msg)
102 : {
103 0 : devicePluginVersions_.clear();
104 0 : for (int32_t i = 0; i < msg.device_plugin_versions_size(); ++i) {
105 0 : const auto& info = msg.device_plugin_versions(i);
106 0 : PluginPkgVersion ver;
107 0 : ver.version = info.version();
108 0 : ver.timestamp = info.timestamp();
109 0 : devicePluginVersions_[info.package_name()] = ver;
110 0 : TSD_RUN_INFO(
111 : "device plugin pkg:%s version:%s timestamp:%s", info.package_name().c_str(), ver.version.c_str(),
112 : ver.timestamp.c_str());
113 0 : }
114 0 : pkgRspCode_ = ((msg.tsd_rsp_code() == 0U) ? ResponseCode::SUCCESS : ResponseCode::FAIL);
115 0 : TSD_RUN_INFO("device plugin info rsp, pkgCount:%d", msg.device_plugin_versions_size());
116 0 : }
117 :
118 : } // namespace tsd
|