1 /*
2  * Copyright 2021, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include <trusty/uuid.h>
22 
23 /**
24  * DOC: Metrics
25  *
26  * Metrics interface provides a way for Android to get Trusty metrics data.
27  *
28  * Currently, only "push" model is supported. Clients are expected to connect to
29  * metrics service, listen for events, e.g. app crash events, and respond to
30  * every event with a &struct metrics_req.
31  *
32  * Communication is driven by metrics service, i.e. requests/responses are all
33  * sent from/to metrics service.
34  *
35  * Note that the type of the event is not known to the client ahead of time.
36  *
37  * This interface used to be exposed on the METRICS_PORT for Android to listen
38  * to the Trusty metrics event.
39  * However the METRICS_PORT is deprecated and replaced by ISTATS_SETTER_PORT
40  * from `trusty/user/base/interface/stats_setter` allowing asynchronous callback
41  * to a Normal-World IStats.aidl service (also see IStatsSetter.aidl under
42  * `system/core/trusty/stats/aidl/android/trusty/stats/setter`).
43  *
44  * This Metrics interface still is used on the METRICS_CONSUMER_PORT, allowing
45  * the Trusty kernel errors to be reported to the metrics user-space service,
46  * then relayed via the IStats callback.
47  */
48 
49 #define METRICS_PORT "com.android.trusty.metrics"
50 
51 /**
52  * enum metrics_cmd - command identifiers for metrics interface
53  * @METRICS_CMD_RESP_BIT:             message is a response
54  * @METRICS_CMD_REQ_SHIFT:            number of bits used by @METRICS_CMD_RESP_BIT
55  * @METRICS_CMD_REPORT_EVENT_DROP:    report gaps in the event stream
56  * @METRICS_CMD_REPORT_CRASH:         report an app crash event
57  * @METRICS_CMD_REPORT_EXIT:          report an app exit
58  * @METRICS_CMD_REPORT_STORAGE_ERROR: report trusty storage error
59  */
60 enum metrics_cmd {
61     METRICS_CMD_RESP_BIT = 1,
62     METRICS_CMD_REQ_SHIFT = 1,
63 
64     METRICS_CMD_REPORT_EVENT_DROP = (1 << METRICS_CMD_REQ_SHIFT),
65     METRICS_CMD_REPORT_CRASH = (2 << METRICS_CMD_REQ_SHIFT),
66     METRICS_CMD_REPORT_EXIT = (3 << METRICS_CMD_REQ_SHIFT),
67     METRICS_CMD_REPORT_STORAGE_ERROR = (4 << METRICS_CMD_REQ_SHIFT),
68 };
69 
70 /**
71  * enum metrics_error - metrics error codes
72  * @METRICS_NO_ERROR:        no error
73  * @METRICS_ERR_UNKNOWN_CMD: unknown or not implemented command
74  */
75 enum metrics_error {
76     METRICS_NO_ERROR = 0,
77     METRICS_ERR_UNKNOWN_CMD = 1,
78 };
79 
80 /**
81  * struct metrics_req - common structure for metrics requests
82  * @cmd:      command identifier - one of &enum metrics_cmd
83  * @reserved: must be 0
84  */
85 struct metrics_req {
86     uint32_t cmd;
87     uint32_t reserved;
88 } __attribute__((__packed__));
89 
90 /**
91  * struct metrics_resp - common structure for metrics responses
92  * @cmd: command identifier - %METRICS_CMD_RESP_BIT or'ed with a cmd in
93  *                            one of &enum metrics_cmd
94  * @status: response status, one of &enum metrics_error
95  */
96 struct metrics_resp {
97     uint32_t cmd;
98     uint32_t status;
99 } __attribute__((__packed__));
100 
101 /**
102  * struct metrics_report_exit_req - arguments of %METRICS_CMD_REPORT_EXIT
103  *                                   requests
104  * @app_id: app_id in the form UUID in ascii format
105  *          "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
106  * @exit_code: architecture-specific exit code
107  */
108 struct metrics_report_exit_req {
109     char app_id[UUID_STR_SIZE];
110     uint32_t exit_code;
111 } __attribute__((__packed__));
112 
113 /**
114  * struct metrics_report_crash_req - arguments of %METRICS_CMD_REPORT_CRASH
115  *                                   requests
116  * @app_id: app_id in the form UUID in ascii format
117  *          "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
118  * @crash_reason: architecture-specific code representing the reason for the
119  *                crash
120  */
121 struct metrics_report_crash_req {
122     char app_id[UUID_STR_SIZE];
123     uint32_t crash_reason;
124 } __attribute__((__packed__));
125 
126 #define METRICS_MAX_APP_ID_LEN 256
127 
128 #define METRICS_MAX_MSG_SIZE 1024
129