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 365 : PluginVersionManager::PluginVersionManager(PackageEnvInfo& envInfo, PackageHashStore& hashStore, PackageContext& ctx)
17 365 : : envInfo_(envInfo), hashStore_(hashStore), ctx_(ctx)
18 365 : {}
19 :
20 17 : bool PluginVersionManager::IsCompatPluginPackage(const PackConfDetail& detail) const
21 : {
22 17 : return detail.decDstDir == DeviceInstallPath::COMPAT_PLUGIN_PATH;
23 : }
24 :
25 16 : PluginUpdateStrategy PluginVersionManager::GetPluginUpdateStrategy()
26 : {
27 16 : if (hasComputedPluginStrategy_) {
28 10 : return pluginUpdateStrategy_;
29 : }
30 :
31 6 : int64_t flag = 0;
32 : const auto drvRet =
33 6 : halGetDeviceInfo(envInfo_.GetLogicDeviceId(), MODULE_TYPE_SYSTEM, INFO_TYPE_SWPLUGIN_UPGRADE_POLICY, &flag);
34 6 : if (drvRet != DRV_ERROR_NONE) {
35 2 : TSD_RUN_WARN(
36 : "[TsdClient][deviceId=%u] halGetDeviceInfo(INFO_TYPE_SWPLUGIN_UPGRADE_POLICY) failed, retCode[%d],"
37 : " fallback to PLUGIN_NOT_FORCE_UPDATE",
38 : envInfo_.GetLogicDeviceId(), drvRet);
39 2 : return PluginUpdateStrategy::PLUGIN_NOT_FORCE_UPDATE;
40 : }
41 4 : if (flag < static_cast<int64_t>(PluginUpdateStrategy::PLUGIN_NOT_FORCE_UPDATE) ||
42 4 : flag > static_cast<int64_t>(PluginUpdateStrategy::PLUGIN_NO_UPDATE)) {
43 1 : TSD_RUN_WARN(
44 : "[TsdClient][deviceId=%u] invalid plugin update strategy from DRV: %lld,"
45 : " fallback to PLUGIN_NOT_FORCE_UPDATE",
46 : envInfo_.GetLogicDeviceId(), flag);
47 1 : return PluginUpdateStrategy::PLUGIN_NOT_FORCE_UPDATE;
48 : }
49 3 : pluginUpdateStrategy_ = static_cast<PluginUpdateStrategy>(flag);
50 3 : hasComputedPluginStrategy_ = true;
51 3 : TSD_RUN_INFO("[TsdClient][deviceId=%u] plugin update strategy from DRV = %lld", envInfo_.GetLogicDeviceId(), flag);
52 3 : return pluginUpdateStrategy_;
53 : }
54 :
55 10 : bool PluginVersionManager::ShouldLoadCompatPluginPkg(const std::string& pkgPureName)
56 : {
57 10 : const PluginUpdateStrategy strategy = GetPluginUpdateStrategy();
58 10 : if (strategy == PluginUpdateStrategy::PLUGIN_NO_UPDATE) {
59 1 : TSD_RUN_INFO("plugin update strategy is PLUGIN_NO_UPDATE, skip pkg:%s", pkgPureName.c_str());
60 1 : return false;
61 : }
62 9 : if (strategy == PluginUpdateStrategy::PLUGIN_FORCE_UPDATE) {
63 2 : TSD_RUN_INFO(
64 : "plugin update strategy is PLUGIN_FORCE_UPDATE, fallback to checkcode compare pkg:%s", pkgPureName.c_str());
65 2 : return !hashStore_.IsCommonSinkHostAndDevicePkgSame(pkgPureName);
66 : }
67 7 : return CompareHostDeviceCompatPluginVersion(pkgPureName);
68 : }
69 :
70 7 : bool PluginVersionManager::CompareHostDeviceCompatPluginVersion(const std::string& pkgPureName)
71 : {
72 7 : const PluginPkgVersion hostInfo = PackageProcessConfig::GetInstance()->GetHostPluginVersion(pkgPureName);
73 7 : const auto itDev = devicePluginVersions_.find(pkgPureName);
74 7 : const PluginPkgVersion deviceInfo = (itDev != devicePluginVersions_.end()) ? itDev->second : PluginPkgVersion{};
75 7 : if (deviceInfo.version.empty()) {
76 2 : TSD_RUN_INFO("device plugin pkg:%s version unavailable, fallback to checkcode compare", pkgPureName.c_str());
77 2 : return !hashStore_.IsCommonSinkHostAndDevicePkgSame(pkgPureName);
78 : }
79 5 : if (hostInfo.Empty()) {
80 1 : TSD_RUN_WARN(
81 : "[TsdClient][deviceId=%u] host plugin pkg:%s version info unavailable, skip", envInfo_.GetLogicDeviceId(),
82 : pkgPureName.c_str());
83 1 : return false;
84 : }
85 4 : const int32_t cmp = PluginPkgVersionUtil::Compare(hostInfo, deviceInfo);
86 4 : if (cmp > 0) {
87 2 : TSD_RUN_INFO(
88 : "host plugin pkg:%s newer than device, load. host[%s/%s] device[%s/%s]", pkgPureName.c_str(),
89 : hostInfo.version.c_str(), hostInfo.timestamp.c_str(), deviceInfo.version.c_str(),
90 : deviceInfo.timestamp.c_str());
91 2 : return true;
92 : }
93 2 : const char* const cmpStr = (cmp < 0) ? "older than" : "equals";
94 2 : TSD_RUN_INFO(
95 : "host plugin pkg:%s %s device, skip. host[%s/%s] device[%s/%s]", pkgPureName.c_str(), cmpStr,
96 : hostInfo.version.c_str(), hostInfo.timestamp.c_str(), deviceInfo.version.c_str(), deviceInfo.timestamp.c_str());
97 2 : return false;
98 7 : }
99 :
100 2 : void PluginVersionManager::HandleDevicePluginVersionRsp(const HDCMessage& msg)
101 : {
102 2 : devicePluginVersions_.clear();
103 6 : for (int32_t i = 0; i < msg.device_plugin_versions_size(); ++i) {
104 4 : const auto& info = msg.device_plugin_versions(i);
105 4 : PluginPkgVersion ver;
106 4 : ver.version = info.version();
107 4 : ver.timestamp = info.timestamp();
108 4 : devicePluginVersions_[info.package_name()] = ver;
109 4 : TSD_RUN_INFO(
110 : "device plugin pkg:%s version:%s timestamp:%s", info.package_name().c_str(), ver.version.c_str(),
111 : ver.timestamp.c_str());
112 4 : }
113 2 : ctx_.pkgRspCode = ((msg.tsd_rsp_code() == 0U) ? ResponseCode::SUCCESS : ResponseCode::FAIL);
114 2 : TSD_RUN_INFO("device plugin info rsp, pkgCount:%d", msg.device_plugin_versions_size());
115 2 : }
116 :
117 : } // namespace tsd
|