-
Notifications
You must be signed in to change notification settings - Fork 16
/
info.c
199 lines (173 loc) · 4.96 KB
/
info.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* SMC Tools - Shared Memory Communication Tools
*
* Copyright IBM Corp. 2021
*
* Userspace program for SMC Information display
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "smctools_common.h"
#include "util.h"
#include "libnetlink.h"
#include "info.h"
#include "dev.h"
static int show_cmd = 0;
static int ism_count, rocev1_count, rocev2_count, rocev3_count;
static struct nla_policy
smc_gen_info_policy[SMC_NLA_SYS_MAX + 1] = {
[SMC_NLA_SYS_UNSPEC] = { .type = NLA_UNSPEC },
[SMC_NLA_SYS_VER] = { .type = NLA_U8 },
[SMC_NLA_SYS_REL] = { .type = NLA_U8 },
[SMC_NLA_SYS_IS_ISM_V2] = { .type = NLA_U8 },
[SMC_NLA_SYS_LOCAL_HOST]= { .type = NLA_NUL_STRING },
[SMC_NLA_SYS_SEID] = { .type = NLA_NUL_STRING },
[SMC_NLA_SYS_IS_SMCR_V2]= { .type = NLA_U8 },
};
static void usage(void)
{
fprintf(stderr,
#if defined(SMCD)
"Usage: smcd info [show]\n"
#elif defined(SMCR)
"Usage: smcr info [show]\n"
#else
"Usage: smc info [show]\n"
#endif
);
exit(-1);
}
static int handle_gen_info_reply(struct nl_msg *msg, void *arg)
{
struct nlattr *info_attrs[SMC_NLA_SYS_MAX + 1];
struct nlattr *attrs[SMC_GEN_MAX + 1];
struct nlmsghdr *hdr = nlmsg_hdr(msg);
int rc = NL_OK;
char tmp[80];
int smc_version = 1;
if (!show_cmd)
return rc;
if (genlmsg_parse(hdr, 0, attrs, SMC_GEN_MAX,
(struct nla_policy *)smc_gen_net_policy) < 0) {
fprintf(stderr, "Error: Invalid data returned: smc_gen_net_policy\n");
nl_msg_dump(msg, stderr);
return NL_STOP;
}
if (!attrs[SMC_GEN_SYS_INFO])
return rc;
if (nla_parse_nested(info_attrs, SMC_NLA_DEV_MAX,
attrs[SMC_GEN_SYS_INFO],
smc_gen_info_policy)) {
fprintf(stderr, "Error: Failed to parse nested attributes: smc_gen_info_policy\n");
return NL_STOP;
}
printf("Kernel Capabilities\n");
/* Version */
tmp[0] = '\0';
if (info_attrs[SMC_NLA_SYS_VER] && info_attrs[SMC_NLA_SYS_REL]) {
sprintf(tmp, "%d.%d", nla_get_u8(info_attrs[SMC_NLA_SYS_VER]), nla_get_u8(info_attrs[SMC_NLA_SYS_REL]));
smc_version = nla_get_u8(info_attrs[SMC_NLA_SYS_VER]);
}
printf("SMC Version: %s\n", (tmp[0] != '\0' ? tmp : "n/a"));
/* Hostname */
tmp[0] = '\0';
if (info_attrs[SMC_NLA_SYS_LOCAL_HOST]) {
sprintf(tmp, "%s", nla_get_string(info_attrs[SMC_NLA_SYS_LOCAL_HOST]));
}
printf("SMC Hostname: %s\n", (tmp[0] != '\0' ? tmp : "n/a"));
/* SMC-D */
sprintf(tmp, "%s", "v1");
if (smc_version >= 2) {
strcat(tmp, " v2");
}
printf("SMC-D Features: %s\n", tmp);
/* SMC-R */
sprintf(tmp, "%s", "v1");
if (info_attrs[SMC_NLA_SYS_IS_SMCR_V2] && nla_get_u8(info_attrs[SMC_NLA_SYS_IS_SMCR_V2])) {
strcat(tmp, " v2");
}
printf("SMC-R Features: %s\n", tmp);
printf("\n");
printf("Hardware Capabilities\n");
/* SEID */
tmp[0] = '\0';
if (info_attrs[SMC_NLA_SYS_SEID]) {
sprintf(tmp, "%s", nla_get_string(info_attrs[SMC_NLA_SYS_SEID]));
}
printf("SEID: %s\n", (tmp[0] != '\0' ? tmp : "n/a"));
/* ISM hardware */
tmp[0] = '\0';
if (ism_count) {
/* Kernel found any ISM device */
sprintf(tmp, "%s", "v1"); /* dev found, v1 is possible */
if (info_attrs[SMC_NLA_SYS_IS_ISM_V2]) {
if (nla_get_u8(info_attrs[SMC_NLA_SYS_IS_ISM_V2]))
strcat(tmp, " v2");
}
}
printf("ISM: %s\n", (tmp[0] != '\0' ? tmp : "n/a"));
/* RoCE hardware */
tmp[0] = '\0';
if (rocev1_count || rocev2_count || rocev3_count) {
/* Kernel found any RoCE device */
strcpy(tmp, "");
if (rocev1_count || rocev2_count || rocev3_count)
strcat(tmp, "v1 ");
if (rocev2_count || rocev3_count)
strcat(tmp, "v2");
}
printf("RoCE: %s\n", (tmp[0] != '\0' ? tmp : "n/a"));
return rc;
}
static void handle_cmd_params(int argc, char **argv)
{
if (argc == 0) {
show_cmd = 1; /* no object given, so use the default "show" */
return;
}
while (1) {
if (contains(argv[0], "help") == 0) {
usage();
} else if (contains(argv[0], "show") == 0) {
show_cmd = 1;
break;
} else {
usage();
}
if (!NEXT_ARG_OK())
break;
NEXT_ARG();
}
/* Too many parameters or wrong sequence of parameters */
if (NEXT_ARG_OK())
usage();
}
int invoke_info(int argc, char **argv, int detail_level)
{
int rc = EXIT_SUCCESS;
handle_cmd_params(argc, argv);
if (show_cmd) {
if (dev_count_ism_devices(&ism_count)) {
fprintf(stderr, "Error: Failed to retrieve ISM device count\n");
return EXIT_FAILURE;
}
if (dev_count_roce_devices(&rocev1_count, &rocev2_count, &rocev3_count)) {
fprintf(stderr, "Error: Failed to retrieve RoCE device count\n");
return EXIT_FAILURE;
}
rc = gen_nl_handle_dump(SMC_NETLINK_GET_SYS_INFO, handle_gen_info_reply, NULL);
} else {
printf("Error: Unknown command\n"); /* we should never come here ... */
return EXIT_FAILURE;
}
return rc;
}