Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/ncsi.c
2 views
1
/* SPDX-License-Identifier: BSD-3-Clause */
2
/*
3
* NC-SI (Network Controller Sideband Interface) "echo" model
4
*
5
* Copyright (C) 2016-2018 IBM Corp.
6
*
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
9
* are met:
10
*
11
* 1. Redistributions of source code must retain the above
12
* copyright notice, this list of conditions and the following
13
* disclaimer.
14
*
15
* 2. Redistributions in binary form must reproduce the above
16
* copyright notice, this list of conditions and the following
17
* disclaimer in the documentation and/or other materials provided
18
* with the distribution.
19
*
20
* 3. Neither the name of the copyright holder nor the names of its
21
* contributors may be used to endorse or promote products derived
22
* from this software without specific prior written permission.
23
*
24
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
35
* OF THE POSSIBILITY OF SUCH DAMAGE.
36
*/
37
#include "slirp.h"
38
39
#include "ncsi-pkt.h"
40
41
static uint32_t ncsi_calculate_checksum(uint8_t *data, int len)
42
{
43
uint32_t checksum = 0;
44
int i;
45
46
/*
47
* 32-bit unsigned sum of the NC-SI packet header and NC-SI packet
48
* payload interpreted as a series of 16-bit unsigned integer values.
49
*/
50
for (i = 0; i < len; i += 2) {
51
checksum += (((uint16_t) data[i]) << 8) + data[i+1];
52
}
53
54
checksum = (~checksum + 1);
55
return checksum;
56
}
57
58
/* Response handler for Mellanox command Get Mac Address */
59
static int ncsi_rsp_handler_oem_mlx_gma(Slirp *slirp,
60
const struct ncsi_pkt_hdr *nh,
61
struct ncsi_rsp_pkt_hdr *rnh)
62
{
63
uint8_t oob_eth_addr_allocated = 0;
64
struct ncsi_rsp_oem_pkt *rsp;
65
int i;
66
67
rsp = (struct ncsi_rsp_oem_pkt *)rnh;
68
69
/* Set the payload length */
70
rsp->rsp.common.length = htons(MLX_GMA_PAYLOAD_LEN);
71
72
for (i = 0; i < ETH_ALEN; i++) {
73
if (slirp->oob_eth_addr[i] != 0x00) {
74
oob_eth_addr_allocated = 1;
75
break;
76
}
77
}
78
rsp->data[MLX_GMA_STATUS_OFFSET] = oob_eth_addr_allocated;
79
80
/* Set the allocated management address */
81
memcpy(&rsp->data[MLX_MAC_ADDR_OFFSET], slirp->oob_eth_addr, ETH_ALEN);
82
83
return 0;
84
}
85
86
/* Response handler for Mellanox card */
87
static int ncsi_rsp_handler_oem_mlx(Slirp *slirp, const struct ncsi_pkt_hdr *nh,
88
struct ncsi_rsp_pkt_hdr *rnh)
89
{
90
const struct ncsi_cmd_oem_pkt *cmd;
91
const struct ncsi_rsp_oem_mlx_pkt *cmd_mlx;
92
struct ncsi_rsp_oem_pkt *rsp;
93
struct ncsi_rsp_oem_mlx_pkt *rsp_mlx;
94
95
/* Get the command header */
96
cmd = (const struct ncsi_cmd_oem_pkt *)nh;
97
cmd_mlx = (const struct ncsi_rsp_oem_mlx_pkt *)cmd->data;
98
99
/* Get the response header */
100
rsp = (struct ncsi_rsp_oem_pkt *)rnh;
101
rsp_mlx = (struct ncsi_rsp_oem_mlx_pkt *)rsp->data;
102
103
/* Ensure the OEM response header matches the command's */
104
rsp_mlx->cmd_rev = cmd_mlx->cmd_rev;
105
rsp_mlx->cmd = cmd_mlx->cmd;
106
rsp_mlx->param = cmd_mlx->param;
107
rsp_mlx->optional = cmd_mlx->optional;
108
109
if (cmd_mlx->cmd == NCSI_OEM_MLX_CMD_GMA &&
110
cmd_mlx->param == NCSI_OEM_MLX_CMD_GMA_PARAM)
111
return ncsi_rsp_handler_oem_mlx_gma(slirp, nh, rnh);
112
113
rsp->rsp.common.length = htons(8);
114
rsp->rsp.code = htons(NCSI_PKT_RSP_C_UNSUPPORTED);
115
rsp->rsp.reason = htons(NCSI_PKT_RSP_R_UNKNOWN);
116
return -ENOENT;
117
}
118
119
static const struct ncsi_rsp_oem_handler {
120
unsigned int mfr_id;
121
int (*handler)(Slirp *slirp, const struct ncsi_pkt_hdr *nh,
122
struct ncsi_rsp_pkt_hdr *rnh);
123
} ncsi_rsp_oem_handlers[] = {
124
{ NCSI_OEM_MFR_MLX_ID, ncsi_rsp_handler_oem_mlx },
125
{ NCSI_OEM_MFR_BCM_ID, NULL },
126
{ NCSI_OEM_MFR_INTEL_ID, NULL },
127
};
128
129
/* Response handler for OEM command */
130
static int ncsi_rsp_handler_oem(Slirp *slirp, const struct ncsi_pkt_hdr *nh,
131
struct ncsi_rsp_pkt_hdr *rnh)
132
{
133
const struct ncsi_rsp_oem_handler *nrh = NULL;
134
const struct ncsi_cmd_oem_pkt *cmd = (const struct ncsi_cmd_oem_pkt *)nh;
135
struct ncsi_rsp_oem_pkt *rsp = (struct ncsi_rsp_oem_pkt *)rnh;
136
uint32_t mfr_id = ntohl(cmd->mfr_id);
137
int i;
138
139
rsp->mfr_id = cmd->mfr_id;
140
141
if (mfr_id != slirp->mfr_id) {
142
goto error;
143
}
144
145
/* Check for manufacturer id and Find the handler */
146
for (i = 0; i < G_N_ELEMENTS(ncsi_rsp_oem_handlers); i++) {
147
if (ncsi_rsp_oem_handlers[i].mfr_id == mfr_id) {
148
if (ncsi_rsp_oem_handlers[i].handler)
149
nrh = &ncsi_rsp_oem_handlers[i];
150
else
151
nrh = NULL;
152
153
break;
154
}
155
}
156
157
if (!nrh) {
158
goto error;
159
}
160
161
/* Process the packet */
162
return nrh->handler(slirp, nh, rnh);
163
164
error:
165
rsp->rsp.common.length = htons(8);
166
rsp->rsp.code = htons(NCSI_PKT_RSP_C_UNSUPPORTED);
167
rsp->rsp.reason = htons(NCSI_PKT_RSP_R_UNKNOWN);
168
return -ENOENT;
169
}
170
171
172
/* Get Version ID */
173
static int ncsi_rsp_handler_gvi(Slirp *slirp, const struct ncsi_pkt_hdr *nh,
174
struct ncsi_rsp_pkt_hdr *rnh)
175
{
176
struct ncsi_rsp_gvi_pkt *rsp = (struct ncsi_rsp_gvi_pkt *)rnh;
177
178
rsp->ncsi_version = htonl(0xF1F0F000);
179
rsp->mf_id = htonl(slirp->mfr_id);
180
181
return 0;
182
}
183
184
/* Get Capabilities */
185
static int ncsi_rsp_handler_gc(Slirp *slirp, const struct ncsi_pkt_hdr *nh,
186
struct ncsi_rsp_pkt_hdr *rnh)
187
{
188
struct ncsi_rsp_gc_pkt *rsp = (struct ncsi_rsp_gc_pkt *)rnh;
189
190
rsp->cap = htonl(~0);
191
rsp->bc_cap = htonl(~0);
192
rsp->mc_cap = htonl(~0);
193
rsp->buf_cap = htonl(~0);
194
rsp->aen_cap = htonl(~0);
195
rsp->vlan_mode = 0xff;
196
rsp->uc_cnt = 2;
197
return 0;
198
}
199
200
/* Get Link status */
201
static int ncsi_rsp_handler_gls(Slirp *slirp, const struct ncsi_pkt_hdr *nh,
202
struct ncsi_rsp_pkt_hdr *rnh)
203
{
204
struct ncsi_rsp_gls_pkt *rsp = (struct ncsi_rsp_gls_pkt *)rnh;
205
206
rsp->status = htonl(0x1);
207
return 0;
208
}
209
210
/* Get Parameters */
211
static int ncsi_rsp_handler_gp(Slirp *slirp, const struct ncsi_pkt_hdr *nh,
212
struct ncsi_rsp_pkt_hdr *rnh)
213
{
214
struct ncsi_rsp_gp_pkt *rsp = (struct ncsi_rsp_gp_pkt *)rnh;
215
216
/* no MAC address filters or VLAN filters on the channel */
217
rsp->mac_cnt = 0;
218
rsp->mac_enable = 0;
219
rsp->vlan_cnt = 0;
220
rsp->vlan_enable = 0;
221
222
return 0;
223
}
224
225
static const struct ncsi_rsp_handler {
226
unsigned char type;
227
int payload;
228
int (*handler)(Slirp *slirp, const struct ncsi_pkt_hdr *nh,
229
struct ncsi_rsp_pkt_hdr *rnh);
230
} ncsi_rsp_handlers[] = { { NCSI_PKT_RSP_CIS, 4, NULL },
231
{ NCSI_PKT_RSP_SP, 4, NULL },
232
{ NCSI_PKT_RSP_DP, 4, NULL },
233
{ NCSI_PKT_RSP_EC, 4, NULL },
234
{ NCSI_PKT_RSP_DC, 4, NULL },
235
{ NCSI_PKT_RSP_RC, 4, NULL },
236
{ NCSI_PKT_RSP_ECNT, 4, NULL },
237
{ NCSI_PKT_RSP_DCNT, 4, NULL },
238
{ NCSI_PKT_RSP_AE, 4, NULL },
239
{ NCSI_PKT_RSP_SL, 4, NULL },
240
{ NCSI_PKT_RSP_GLS, 16, ncsi_rsp_handler_gls },
241
{ NCSI_PKT_RSP_SVF, 4, NULL },
242
{ NCSI_PKT_RSP_EV, 4, NULL },
243
{ NCSI_PKT_RSP_DV, 4, NULL },
244
{ NCSI_PKT_RSP_SMA, 4, NULL },
245
{ NCSI_PKT_RSP_EBF, 4, NULL },
246
{ NCSI_PKT_RSP_DBF, 4, NULL },
247
{ NCSI_PKT_RSP_EGMF, 4, NULL },
248
{ NCSI_PKT_RSP_DGMF, 4, NULL },
249
{ NCSI_PKT_RSP_SNFC, 4, NULL },
250
{ NCSI_PKT_RSP_GVI, 40, ncsi_rsp_handler_gvi },
251
{ NCSI_PKT_RSP_GC, 32, ncsi_rsp_handler_gc },
252
{ NCSI_PKT_RSP_GP, 40, ncsi_rsp_handler_gp },
253
{ NCSI_PKT_RSP_GCPS, 172, NULL },
254
{ NCSI_PKT_RSP_GNS, 172, NULL },
255
{ NCSI_PKT_RSP_GNPTS, 172, NULL },
256
{ NCSI_PKT_RSP_GPS, 8, NULL },
257
{ NCSI_PKT_RSP_OEM, 0, ncsi_rsp_handler_oem },
258
{ NCSI_PKT_RSP_PLDM, 0, NULL },
259
{ NCSI_PKT_RSP_GPUUID, 20, NULL } };
260
261
/*
262
* packet format : ncsi header + payload + checksum
263
*/
264
#define NCSI_MAX_PAYLOAD 172
265
#define NCSI_MAX_LEN (sizeof(struct ncsi_pkt_hdr) + NCSI_MAX_PAYLOAD + 4)
266
267
void ncsi_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
268
{
269
const struct ncsi_pkt_hdr *nh =
270
(const struct ncsi_pkt_hdr *)(pkt + ETH_HLEN);
271
uint8_t ncsi_reply[2 + ETH_HLEN + NCSI_MAX_LEN];
272
struct ethhdr *reh = (struct ethhdr *)(ncsi_reply + 2);
273
struct ncsi_rsp_pkt_hdr *rnh =
274
(struct ncsi_rsp_pkt_hdr *)(ncsi_reply + 2 + ETH_HLEN);
275
const struct ncsi_rsp_handler *handler = NULL;
276
int i;
277
int ncsi_rsp_len = sizeof(*nh);
278
uint32_t checksum;
279
uint32_t *pchecksum;
280
281
if (pkt_len < ETH_HLEN + sizeof(struct ncsi_pkt_hdr)) {
282
return; /* packet too short */
283
}
284
285
memset(ncsi_reply, 0, sizeof(ncsi_reply));
286
287
memset(reh->h_dest, 0xff, ETH_ALEN);
288
memset(reh->h_source, 0xff, ETH_ALEN);
289
reh->h_proto = htons(ETH_P_NCSI);
290
291
for (i = 0; i < G_N_ELEMENTS(ncsi_rsp_handlers); i++) {
292
if (ncsi_rsp_handlers[i].type == nh->type + 0x80) {
293
handler = &ncsi_rsp_handlers[i];
294
break;
295
}
296
}
297
298
rnh->common.mc_id = nh->mc_id;
299
rnh->common.revision = NCSI_PKT_REVISION;
300
rnh->common.id = nh->id;
301
rnh->common.type = nh->type + 0x80;
302
rnh->common.channel = nh->channel;
303
304
if (handler) {
305
rnh->common.length = htons(handler->payload);
306
rnh->code = htons(NCSI_PKT_RSP_C_COMPLETED);
307
rnh->reason = htons(NCSI_PKT_RSP_R_NO_ERROR);
308
309
if (handler->handler) {
310
handler->handler(slirp, nh, rnh);
311
}
312
ncsi_rsp_len += ntohs(rnh->common.length);
313
} else {
314
rnh->common.length = 0;
315
rnh->code = htons(NCSI_PKT_RSP_C_UNAVAILABLE);
316
rnh->reason = htons(NCSI_PKT_RSP_R_UNKNOWN);
317
}
318
319
/* Add the optional checksum at the end of the frame. */
320
checksum = ncsi_calculate_checksum((uint8_t *)rnh, ncsi_rsp_len);
321
pchecksum = (uint32_t *)((char *)rnh + ncsi_rsp_len);
322
*pchecksum = htonl(checksum);
323
ncsi_rsp_len += 4;
324
325
slirp_send_packet_all(slirp, ncsi_reply + 2, ETH_HLEN + ncsi_rsp_len);
326
}
327
328