Line data Source code
1 : /**
2 : * Copyright (c) 2025 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 : #include "trace_server_mgr.h"
11 : #include "trace_system_api.h"
12 : #include "adiag_print.h"
13 : #include "trace_session_mgr.h"
14 : #include "trace_msg.h"
15 :
16 : #define MSG_STATUS_LONG_LINK 12
17 : #define MSG_STATUS_SHORT_LINK 13
18 :
19 : #define RECV_BUFF_SIZE (512 *1024) // 512K
20 : #define MAX_RECV_TIMEOUT 3000 // timeout 3s
21 : typedef struct {
22 : unsigned short headInfo; // head magic data, judge to little
23 : unsigned char headVer; // head version
24 : unsigned char order; // packet order (reserved)
25 : unsigned short reqType; // request type of proto
26 : unsigned short devId; // request device Id
27 : unsigned int totalLen; // whole message length, only all data[0] length
28 : unsigned int sliceLen; // one slice length, only data[0] length
29 : unsigned int offset; // offset
30 : unsigned short msgType; // message type
31 : unsigned short status; // message status data
32 : unsigned char data[0]; // message data
33 : } TraceDataMsg;
34 :
35 15 : int32_t TraceDeviceInit(void)
36 : {
37 15 : return TRACE_SUCCESS;
38 : }
39 :
40 5 : STATIC TraStatus TraceHandleHelloMsg(const void *handle, const TraceHelloMsg *msg)
41 : {
42 5 : if ((msg->magic != TRACE_HEAD_MAGIC) || (msg->version != TRACE_HEAD_VERSION)) {
43 1 : ADIAG_ERR("msg head check failed, msg->magic=%u, msg->version=%u.", msg->magic, msg->version);
44 1 : return TRACE_FAILURE;
45 : }
46 :
47 : // get pid from handle
48 4 : int32_t pid = 0;
49 4 : int32_t rt = TraceAdxGetAttrByCommHandle(handle, HDC_SESSION_ATTR_PEER_CREATE_PID, &pid);
50 4 : if (rt != TRACE_SUCCESS) {
51 1 : ADIAG_ERR("get pid failed, ret = %d.", rt);
52 1 : return TRACE_FAILURE;
53 : }
54 3 : ADIAG_INF("hello msg pid = %d.", pid);
55 3 : TraceHelloMsg *sendMsg = NULL;
56 3 : sendMsg = (TraceHelloMsg *)AdiagMalloc(sizeof(TraceHelloMsg));
57 3 : if (sendMsg == NULL) {
58 1 : ADIAG_ERR("malloc failed.");
59 1 : return TRACE_FAILURE;
60 : }
61 2 : sendMsg->msgType = TRACE_HELLO_MSG;
62 2 : sendMsg->magic = TRACE_HEAD_MAGIC;
63 2 : sendMsg->version = TRACE_HEAD_VERSION;
64 : // short link need to send end msg
65 2 : TraStatus ret = TraceAdxSendMsg(handle, (const char *)sendMsg, (uint32_t)sizeof(TraceHelloMsg));
66 2 : TraStatus retEnd = TraceAdxSendMsg(handle, HDC_END_MSG, strlen(HDC_END_MSG));
67 2 : ADIAG_SAFE_FREE(sendMsg);
68 2 : if ((ret != TRACE_SUCCESS) || (retEnd != TRACE_SUCCESS)) {
69 1 : ADIAG_ERR("send hello msg failed.");
70 1 : return TRACE_FAILURE;
71 : }
72 1 : ADIAG_INF("send hello msg successfully.");
73 1 : return TRACE_SUCCESS;
74 : }
75 :
76 3 : STATIC TraStatus TraceHandleStartMsg(const void *handle, const TraceStartMsg *msg)
77 : {
78 : // get devId from handle
79 3 : int32_t devId = 0;
80 3 : TraStatus ret = TraceAdxGetAttrByCommHandle(handle, HDC_SESSION_ATTR_DEV_ID, &devId);
81 3 : if (ret != TRACE_SUCCESS) {
82 1 : ADIAG_ERR("get device id failed, ret = %d.", ret);
83 1 : return TRACE_FAILURE;
84 : }
85 :
86 : // get pid from handle
87 2 : int32_t pid = 0;
88 2 : ret = TraceAdxGetAttrByCommHandle(handle, HDC_SESSION_ATTR_PEER_CREATE_PID, &pid);
89 2 : if (ret != TRACE_SUCCESS) {
90 1 : ADIAG_ERR("get pid failed, ret = %d.", ret);
91 1 : return TRACE_FAILURE;
92 : }
93 1 : ret = TraceServerInsertSessionNode(handle, pid, devId, msg->timeout);
94 1 : if (ret != TRACE_SUCCESS) {
95 0 : return TRACE_FAILURE;
96 : }
97 1 : ADIAG_INF("trace server insert session node successfully, pid = %d, devId = %d, msgtype = %d, timeout = %dms.",
98 : pid, devId, (int32_t)msg->msgType, msg->timeout);
99 1 : return TRACE_SUCCESS;
100 : }
101 :
102 6 : STATIC TraStatus TraceHandleLinkMsg(const void *handle)
103 : {
104 6 : uint32_t dataMaxLen = RECV_BUFF_SIZE;
105 6 : char *data = (char *)AdiagMalloc(dataMaxLen);
106 6 : if (data == NULL) {
107 2 : ADIAG_ERR("malloc failed, strerr = %s.", strerror(AdiagGetErrorCode()));
108 2 : TraceAdxDestroyCommHandle(handle);
109 2 : return TRACE_FAILURE;
110 : }
111 4 : TraStatus ret = TraceAdxRecvMsg(handle, &data, &dataMaxLen, MAX_RECV_TIMEOUT);
112 4 : if (ret != TRACE_SUCCESS) {
113 1 : ADIAG_ERR("adx receive message failed, ret = %d", ret);
114 1 : TraceAdxDestroyCommHandle(handle);
115 1 : ADIAG_SAFE_FREE(data);
116 1 : return TRACE_FAILURE;
117 : }
118 :
119 3 : ret = TraceHandleStartMsg(handle, (const TraceStartMsg *)data);
120 3 : ADIAG_SAFE_FREE(data);
121 3 : if (ret != TRACE_SUCCESS) {
122 2 : TraceAdxDestroyCommHandle(handle);
123 2 : return TRACE_FAILURE;
124 : }
125 1 : return TRACE_SUCCESS;
126 : }
127 :
128 4 : STATIC TraStatus TraceHandleEndMsg(const void *handle, const TraceEndMsg *msg)
129 : {
130 : (void)msg;
131 : // get devId from handle
132 4 : int32_t devId = 0;
133 4 : TraStatus ret = TraceAdxGetAttrByCommHandle(handle, HDC_SESSION_ATTR_DEV_ID, &devId);
134 4 : if (ret != TRACE_SUCCESS) {
135 1 : ADIAG_ERR("get device id failed, ret = %d.", ret);
136 1 : return TRACE_FAILURE;
137 : }
138 :
139 : // get pid from handle
140 3 : int32_t pid = 0;
141 3 : ret = TraceAdxGetAttrByCommHandle(handle, HDC_SESSION_ATTR_PEER_CREATE_PID, &pid);
142 3 : if (ret != TRACE_SUCCESS) {
143 1 : ADIAG_ERR("get pid failed, ret = %d.", ret);
144 1 : return TRACE_FAILURE;
145 : }
146 2 : ret = TraceServerDeleteSessionNode(handle, pid, devId);
147 2 : if (ret != TRACE_SUCCESS) {
148 1 : ADIAG_ERR("delete session node failed, ret = %d.", ret);
149 1 : return TRACE_FAILURE;
150 : }
151 :
152 1 : ADIAG_INF("delete session node successfully, pid = %d.", pid);
153 1 : return TRACE_SUCCESS;
154 : }
155 :
156 16 : STATIC TraStatus TraceHandleCmd(const void *handle, const TraceDataMsg *msg, uint32_t len)
157 : {
158 16 : if (msg->status == MSG_STATUS_LONG_LINK) {
159 6 : ADIAG_INF("trace long link.");
160 6 : return TraceHandleLinkMsg(handle);
161 : } else {
162 10 : TraStatus ret = TRACE_FAILURE;
163 10 : if ((msg->data[0] == TRACE_HELLO_MSG) && (len >= sizeof(TraceHelloMsg))) {
164 5 : ADIAG_INF("trace hello msg.");
165 5 : ret = TraceHandleHelloMsg(handle, (const TraceHelloMsg *)msg->data);
166 5 : } else if ((msg->data[0] == TRACE_END_MSG) && (len >= sizeof(TraceEndMsg))) {
167 4 : ADIAG_INF("trace end msg.");
168 4 : ret = TraceHandleEndMsg(handle, (const TraceEndMsg *)msg->data);
169 : } else {
170 1 : ADIAG_INF("invalid trace cmd, msgType = %c.", msg->data[0]);
171 1 : ret = TRACE_FAILURE;
172 : }
173 10 : TraceAdxDestroyCommHandle(handle);
174 10 : return ret;
175 : }
176 : }
177 :
178 19 : int32_t TraceDeviceProcess(AdxCommConHandle handle, const void *value, uint32_t len)
179 : {
180 19 : if (TraceAdxIsCommHandleValid(handle) != TRACE_SUCCESS) {
181 1 : ADIAG_ERR("handle is invalid.");
182 1 : return TRACE_FAILURE;
183 : }
184 18 : if ((value == NULL) || (len < sizeof(TraceDataMsg))) {
185 2 : TraceAdxDestroyCommHandle(handle);
186 2 : ADIAG_ERR("value is invalid, len = %u bytes.", len);
187 2 : return TRACE_FAILURE;
188 : }
189 16 : TraStatus ret = TraceHandleCmd(handle, (const TraceDataMsg *)value, len);
190 16 : if (ret != TRACE_SUCCESS) {
191 13 : return TRACE_FAILURE;
192 : }
193 3 : return TRACE_SUCCESS;
194 : }
195 :
196 15 : int32_t TraceDeviceExit(void)
197 : {
198 15 : return TRACE_SUCCESS;
199 : }
|