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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/*
* module.c - netlink implementation of module commands
*
* Implementation of "ethtool --show-module <dev>" and
* "ethtool --set-module <dev> ..."
*/
#include <errno.h>
#include <ctype.h>
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include "../internal.h"
#include "../common.h"
#include "netlink.h"
#include "parser.h"
/* MODULE_GET */
static const char *module_power_mode_policy_name(u8 val)
{
switch (val) {
case ETHTOOL_MODULE_POWER_MODE_POLICY_HIGH:
return "high";
case ETHTOOL_MODULE_POWER_MODE_POLICY_AUTO:
return "auto";
default:
return "unknown";
}
}
static const char *module_power_mode_name(u8 val)
{
switch (val) {
case ETHTOOL_MODULE_POWER_MODE_LOW:
return "low";
case ETHTOOL_MODULE_POWER_MODE_HIGH:
return "high";
default:
return "unknown";
}
}
int module_reply_cb(const struct nlmsghdr *nlhdr, void *data)
{
const struct nlattr *tb[ETHTOOL_A_MODULE_MAX + 1] = {};
struct nl_context *nlctx = data;
DECLARE_ATTR_TB_INFO(tb);
bool silent;
int err_ret;
int ret;
silent = nlctx->is_dump || nlctx->is_monitor;
err_ret = silent ? MNL_CB_OK : MNL_CB_ERROR;
ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
if (ret < 0)
return err_ret;
nlctx->devname = get_dev_name(tb[ETHTOOL_A_MODULE_HEADER]);
if (!dev_ok(nlctx))
return err_ret;
if (silent)
print_nl();
open_json_object(NULL);
print_string(PRINT_ANY, "ifname", "Module parameters for %s:\n",
nlctx->devname);
if (tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY]) {
u8 val;
val = mnl_attr_get_u8(tb[ETHTOOL_A_MODULE_POWER_MODE_POLICY]);
print_string(PRINT_ANY, "power-mode-policy",
"power-mode-policy: %s\n",
module_power_mode_policy_name(val));
}
if (tb[ETHTOOL_A_MODULE_POWER_MODE]) {
u8 val;
val = mnl_attr_get_u8(tb[ETHTOOL_A_MODULE_POWER_MODE]);
print_string(PRINT_ANY, "power-mode",
"power-mode: %s\n", module_power_mode_name(val));
}
close_json_object();
return MNL_CB_OK;
}
int nl_gmodule(struct cmd_context *ctx)
{
struct nl_context *nlctx = ctx->nlctx;
struct nl_socket *nlsk;
int ret;
if (netlink_cmd_check(ctx, ETHTOOL_MSG_MODULE_GET, true))
return -EOPNOTSUPP;
if (ctx->argc > 0) {
fprintf(stderr, "ethtool: unexpected parameter '%s'\n",
*ctx->argp);
return 1;
}
nlsk = nlctx->ethnl_socket;
ret = nlsock_prep_get_request(nlsk, ETHTOOL_MSG_MODULE_GET,
ETHTOOL_A_MODULE_HEADER, 0);
if (ret < 0)
return ret;
new_json_obj(ctx->json);
ret = nlsock_send_get_request(nlsk, module_reply_cb);
delete_json_obj();
return ret;
}
/* MODULE_SET */
static const struct lookup_entry_u8 power_mode_policy_values[] = {
{ .arg = "high", .val = ETHTOOL_MODULE_POWER_MODE_POLICY_HIGH },
{ .arg = "auto", .val = ETHTOOL_MODULE_POWER_MODE_POLICY_AUTO },
{}
};
static const struct param_parser smodule_params[] = {
{
.arg = "power-mode-policy",
.type = ETHTOOL_A_MODULE_POWER_MODE_POLICY,
.handler = nl_parse_lookup_u8,
.handler_data = power_mode_policy_values,
.min_argc = 1,
},
{}
};
int nl_smodule(struct cmd_context *ctx)
{
struct nl_context *nlctx = ctx->nlctx;
struct nl_msg_buff *msgbuff;
struct nl_socket *nlsk;
int ret;
if (netlink_cmd_check(ctx, ETHTOOL_MSG_MODULE_SET, false))
return -EOPNOTSUPP;
if (!ctx->argc) {
fprintf(stderr, "ethtool (--set-module): parameters missing\n");
return 1;
}
nlctx->cmd = "--set-module";
nlctx->argp = ctx->argp;
nlctx->argc = ctx->argc;
nlctx->devname = ctx->devname;
nlsk = nlctx->ethnl_socket;
msgbuff = &nlsk->msgbuff;
ret = msg_init(nlctx, msgbuff, ETHTOOL_MSG_MODULE_SET,
NLM_F_REQUEST | NLM_F_ACK);
if (ret < 0)
return 2;
if (ethnla_fill_header(msgbuff, ETHTOOL_A_MODULE_HEADER,
ctx->devname, 0))
return -EMSGSIZE;
ret = nl_parser(nlctx, smodule_params, NULL, PARSER_GROUP_NONE, NULL);
if (ret < 0)
return 1;
ret = nlsock_sendmsg(nlsk, NULL);
if (ret < 0)
return 83;
ret = nlsock_process_reply(nlsk, nomsg_reply_cb, nlctx);
if (ret == 0)
return 0;
else
return nlctx->exit_code ?: 83;
}
/* MODULE_FW_FLASH_ACT */
static const struct param_parser flash_module_fw_params[] = {
{
.arg = "file",
.type = ETHTOOL_A_MODULE_FW_FLASH_FILE_NAME,
.handler = nl_parse_string,
.min_argc = 1,
},
{
.arg = "pass",
.type = ETHTOOL_A_MODULE_FW_FLASH_PASSWORD,
.handler = nl_parse_direct_u32,
.min_argc = 1,
},
{}
};
struct module_flash_context {
uint8_t breakout:1,
first:1;
};
static int module_fw_flash_ntf_cb(const struct nlmsghdr *nlhdr, void *data)
{
const struct nlattr *tb[ETHTOOL_A_MODULE_FW_FLASH_MAX + 1] = {};
struct module_flash_context *mfctx;
struct nl_context *nlctx = data;
DECLARE_ATTR_TB_INFO(tb);
u8 status = 0;
int ret;
mfctx = nlctx->cmd_private;
ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
if (ret < 0)
return MNL_CB_OK;
nlctx->devname = get_dev_name(tb[ETHTOOL_A_MODULE_FW_FLASH_HEADER]);
if (!dev_ok(nlctx))
return MNL_CB_OK;
if (tb[ETHTOOL_A_MODULE_FW_FLASH_STATUS])
status = mnl_attr_get_u32(tb[ETHTOOL_A_MODULE_FW_FLASH_STATUS]);
switch (status) {
case ETHTOOL_MODULE_FW_FLASH_STATUS_STARTED:
print_string(PRINT_FP, NULL,
"Transceiver module firmware flashing started for device %s\n",
nlctx->devname);
break;
case ETHTOOL_MODULE_FW_FLASH_STATUS_IN_PROGRESS:
if (mfctx->first) {
print_string(PRINT_FP, NULL,
"Transceiver module firmware flashing in progress for device %s\n",
nlctx->devname);
mfctx->first = 0;
}
break;
case ETHTOOL_MODULE_FW_FLASH_STATUS_COMPLETED:
print_nl();
print_string(PRINT_FP, NULL,
"Transceiver module firmware flashing completed for device %s\n",
nlctx->devname);
mfctx->breakout = 1;
break;
case ETHTOOL_MODULE_FW_FLASH_STATUS_ERROR:
print_nl();
print_string(PRINT_FP, NULL,
"Transceiver module firmware flashing encountered an error for device %s\n",
nlctx->devname);
mfctx->breakout = 1;
break;
default:
break;
}
if (tb[ETHTOOL_A_MODULE_FW_FLASH_STATUS_MSG]) {
const char *status_msg;
status_msg = mnl_attr_get_str(tb[ETHTOOL_A_MODULE_FW_FLASH_STATUS_MSG]);
print_string(PRINT_FP, NULL, "Status message: %s\n", status_msg);
}
if (tb[ETHTOOL_A_MODULE_FW_FLASH_DONE] &&
tb[ETHTOOL_A_MODULE_FW_FLASH_TOTAL]) {
uint64_t done, total;
done = attr_get_uint(tb[ETHTOOL_A_MODULE_FW_FLASH_DONE]);
total = attr_get_uint(tb[ETHTOOL_A_MODULE_FW_FLASH_TOTAL]);
if (total)
print_u64(PRINT_FP, NULL, "Progress: %"PRIu64"%\r",
done * 100 / total);
}
return MNL_CB_OK;
}
static int nl_flash_module_fw_cb(const struct nlmsghdr *nlhdr, void *data)
{
const struct genlmsghdr *ghdr = (const struct genlmsghdr *)(nlhdr + 1);
if (ghdr->cmd != ETHTOOL_MSG_MODULE_FW_FLASH_NTF)
return MNL_CB_OK;
module_fw_flash_ntf_cb(nlhdr, data);
return MNL_CB_STOP;
}
static int nl_flash_module_fw_process_ntf(struct cmd_context *ctx)
{
struct nl_context *nlctx = ctx->nlctx;
struct module_flash_context *mfctx;
struct nl_socket *nlsk;
int ret;
nlsk = nlctx->ethnl_socket;
mfctx = malloc(sizeof(struct module_flash_context));
if (!mfctx)
return -ENOMEM;
mfctx->breakout = 0;
mfctx->first = 1;
nlctx->cmd_private = mfctx;
while (!mfctx->breakout) {
ret = nlsock_process_reply(nlsk, nl_flash_module_fw_cb, nlctx);
if (ret)
goto out;
nlsk->seq++;
}
out:
free(mfctx);
return ret;
}
int nl_flash_module_fw(struct cmd_context *ctx)
{
struct nl_context *nlctx = ctx->nlctx;
struct nl_msg_buff *msgbuff;
struct nl_socket *nlsk;
int ret;
if (netlink_cmd_check(ctx, ETHTOOL_MSG_MODULE_FW_FLASH_ACT, false))
return -EOPNOTSUPP;
if (!ctx->argc) {
fprintf(stderr, "ethtool (--flash-module-firmware): parameters missing\n");
return 1;
}
nlctx->cmd = "--flash-module-firmware";
nlctx->argp = ctx->argp;
nlctx->argc = ctx->argc;
nlctx->devname = ctx->devname;
nlsk = nlctx->ethnl_socket;
msgbuff = &nlsk->msgbuff;
ret = msg_init(nlctx, msgbuff, ETHTOOL_MSG_MODULE_FW_FLASH_ACT,
NLM_F_REQUEST | NLM_F_ACK);
if (ret < 0)
return 2;
if (ethnla_fill_header(msgbuff, ETHTOOL_A_MODULE_FW_FLASH_HEADER,
ctx->devname, 0))
return -EMSGSIZE;
ret = nl_parser(nlctx, flash_module_fw_params, NULL, PARSER_GROUP_NONE,
NULL);
if (ret < 0)
return 1;
ret = nlsock_sendmsg(nlsk, NULL);
if (ret < 0)
fprintf(stderr, "Cannot flash transceiver module firmware\n");
else
ret = nl_flash_module_fw_process_ntf(ctx);
return ret;
}
|