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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
// SPDX-License-Identifier: GPL-2.0
/*
* ioam6.c "ip ioam"
*
* Author: Justin Iurman <justin.iurman@uliege.be>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <inttypes.h>
#include <linux/genetlink.h>
#include <linux/ioam6.h>
#include <linux/ioam6_genl.h>
#include "utils.h"
#include "ip_common.h"
#include "libgenl.h"
#include "json_print.h"
static void usage(void)
{
fprintf(stderr,
"Usage: ip ioam { COMMAND | help }\n"
" ip ioam namespace show\n"
" ip ioam namespace add ID [ data DATA32 ] [ wide DATA64 ]\n"
" ip ioam namespace del ID\n"
" ip ioam schema show\n"
" ip ioam schema add ID DATA\n"
" ip ioam schema del ID\n"
" ip ioam namespace set ID schema { ID | none }\n"
" ip ioam monitor\n");
exit(-1);
}
static struct rtnl_handle grth = { .fd = -1 };
static int genl_family = -1;
#define IOAM6_REQUEST(_req, _bufsiz, _cmd, _flags) \
GENL_REQUEST(_req, _bufsiz, genl_family, 0, \
IOAM6_GENL_VERSION, _cmd, _flags)
static struct {
bool monitor;
unsigned int cmd;
__u32 sc_id;
__u32 ns_data;
__u64 ns_data_wide;
__u16 ns_id;
bool has_ns_data;
bool has_ns_data_wide;
bool sc_none;
__u8 sc_data[IOAM6_MAX_SCHEMA_DATA_LEN];
} opts;
static void print_namespace(struct rtattr *attrs[])
{
print_uint(PRINT_ANY, "namespace", "namespace %u",
rta_getattr_u16(attrs[IOAM6_ATTR_NS_ID]));
if (attrs[IOAM6_ATTR_SC_ID])
print_uint(PRINT_ANY, "schema", " [schema %u]",
rta_getattr_u32(attrs[IOAM6_ATTR_SC_ID]));
if (attrs[IOAM6_ATTR_NS_DATA])
print_hex(PRINT_ANY, "data", ", data %#010x",
rta_getattr_u32(attrs[IOAM6_ATTR_NS_DATA]));
if (attrs[IOAM6_ATTR_NS_DATA_WIDE])
print_0xhex(PRINT_ANY, "wide", ", wide %#018lx",
rta_getattr_u64(attrs[IOAM6_ATTR_NS_DATA_WIDE]));
print_nl();
}
static void print_schema(struct rtattr *attrs[])
{
__u8 data[IOAM6_MAX_SCHEMA_DATA_LEN];
int len, i = 0;
print_uint(PRINT_ANY, "schema", "schema %u",
rta_getattr_u32(attrs[IOAM6_ATTR_SC_ID]));
if (attrs[IOAM6_ATTR_NS_ID])
print_uint(PRINT_ANY, "namespace", " [namespace %u]",
rta_getattr_u16(attrs[IOAM6_ATTR_NS_ID]));
len = RTA_PAYLOAD(attrs[IOAM6_ATTR_SC_DATA]);
memcpy(data, RTA_DATA(attrs[IOAM6_ATTR_SC_DATA]), len);
print_null(PRINT_ANY, "data", ", data:", NULL);
while (i < len) {
print_hhu(PRINT_ANY, "", " %02x", data[i]);
i++;
}
print_nl();
}
static void print_trace(struct rtattr *attrs[])
{
__u8 data[IOAM6_TRACE_DATA_SIZE_MAX];
int len, i = 0;
printf("[TRACE] ");
if (attrs[IOAM6_EVENT_ATTR_TRACE_NAMESPACE])
printf("Namespace=%u ",
rta_getattr_u16(attrs[IOAM6_EVENT_ATTR_TRACE_NAMESPACE]));
if (attrs[IOAM6_EVENT_ATTR_TRACE_NODELEN])
printf("NodeLen=%u ",
rta_getattr_u8(attrs[IOAM6_EVENT_ATTR_TRACE_NODELEN]));
if (attrs[IOAM6_EVENT_ATTR_TRACE_TYPE])
printf("Type=%#08x ",
rta_getattr_u32(attrs[IOAM6_EVENT_ATTR_TRACE_TYPE]));
len = RTA_PAYLOAD(attrs[IOAM6_EVENT_ATTR_TRACE_DATA]);
memcpy(data, RTA_DATA(attrs[IOAM6_EVENT_ATTR_TRACE_DATA]), len);
printf("Data=");
while (i < len) {
printf("%02x", data[i]);
i++;
}
printf("\n");
}
static int process_msg(struct nlmsghdr *n, void *arg)
{
struct rtattr *attrs[IOAM6_ATTR_MAX + 1];
struct genlmsghdr *ghdr;
int len = n->nlmsg_len;
if (n->nlmsg_type != genl_family)
return -1;
len -= NLMSG_LENGTH(GENL_HDRLEN);
if (len < 0)
return -1;
ghdr = NLMSG_DATA(n);
parse_rtattr(attrs, IOAM6_ATTR_MAX, (void *)ghdr + GENL_HDRLEN, len);
open_json_object(NULL);
switch (ghdr->cmd) {
case IOAM6_CMD_DUMP_NAMESPACES:
print_namespace(attrs);
break;
case IOAM6_CMD_DUMP_SCHEMAS:
print_schema(attrs);
break;
}
close_json_object();
return 0;
}
static int ioam6_monitor_msg(struct rtnl_ctrl_data *ctrl, struct nlmsghdr *n,
void *arg)
{
struct rtattr *attrs[IOAM6_EVENT_ATTR_MAX + 1];
const struct genlmsghdr *ghdr = NLMSG_DATA(n);
int len = n->nlmsg_len;
if (n->nlmsg_type != genl_family)
return -1;
len -= NLMSG_LENGTH(GENL_HDRLEN);
if (len < 0)
return -1;
parse_rtattr(attrs, IOAM6_EVENT_ATTR_MAX,
(void *)ghdr + GENL_HDRLEN, len);
switch (ghdr->cmd) {
case IOAM6_EVENT_TRACE:
print_trace(attrs);
break;
}
return 0;
}
static int ioam6_do_cmd(void)
{
IOAM6_REQUEST(req, 1056, opts.cmd, NLM_F_REQUEST);
int dump = 0;
if (genl_init_handle(&grth, IOAM6_GENL_NAME, &genl_family))
exit(1);
if (opts.monitor) {
if (genl_add_mcast_grp(&grth, genl_family,
IOAM6_GENL_EV_GRP_NAME) < 0) {
perror("can't subscribe to ioam6 events");
exit(1);
}
if (rtnl_listen(&grth, ioam6_monitor_msg, stdout) < 0)
exit(1);
return 0;
}
req.n.nlmsg_type = genl_family;
switch (opts.cmd) {
case IOAM6_CMD_ADD_NAMESPACE:
addattr16(&req.n, sizeof(req), IOAM6_ATTR_NS_ID, opts.ns_id);
if (opts.has_ns_data)
addattr32(&req.n, sizeof(req), IOAM6_ATTR_NS_DATA,
opts.ns_data);
if (opts.has_ns_data_wide)
addattr64(&req.n, sizeof(req), IOAM6_ATTR_NS_DATA_WIDE,
opts.ns_data_wide);
break;
case IOAM6_CMD_DEL_NAMESPACE:
addattr16(&req.n, sizeof(req), IOAM6_ATTR_NS_ID, opts.ns_id);
break;
case IOAM6_CMD_DUMP_NAMESPACES:
case IOAM6_CMD_DUMP_SCHEMAS:
dump = 1;
break;
case IOAM6_CMD_ADD_SCHEMA:
addattr32(&req.n, sizeof(req), IOAM6_ATTR_SC_ID, opts.sc_id);
addattr_l(&req.n, sizeof(req), IOAM6_ATTR_SC_DATA, opts.sc_data,
strlen((const char *)opts.sc_data));
break;
case IOAM6_CMD_DEL_SCHEMA:
addattr32(&req.n, sizeof(req), IOAM6_ATTR_SC_ID, opts.sc_id);
break;
case IOAM6_CMD_NS_SET_SCHEMA:
addattr16(&req.n, sizeof(req), IOAM6_ATTR_NS_ID, opts.ns_id);
if (opts.sc_none)
addattr(&req.n, sizeof(req), IOAM6_ATTR_SC_NONE);
else
addattr32(&req.n, sizeof(req), IOAM6_ATTR_SC_ID,
opts.sc_id);
break;
}
if (!dump) {
if (rtnl_talk(&grth, &req.n, NULL) < 0)
return -1;
} else {
req.n.nlmsg_flags |= NLM_F_DUMP;
req.n.nlmsg_seq = grth.dump = ++grth.seq;
if (rtnl_send(&grth, &req, req.n.nlmsg_len) < 0) {
perror("Failed to send dump request");
exit(1);
}
new_json_obj(json);
if (rtnl_dump_filter(&grth, process_msg, stdout) < 0) {
fprintf(stderr, "Dump terminated\n");
exit(1);
}
delete_json_obj();
fflush(stdout);
}
return 0;
}
int do_ioam6(int argc, char **argv)
{
bool maybe_wide = false;
if (argc < 1 || strcmp(*argv, "help") == 0)
usage();
memset(&opts, 0, sizeof(opts));
if (strcmp(*argv, "namespace") == 0) {
NEXT_ARG();
if (strcmp(*argv, "show") == 0) {
opts.cmd = IOAM6_CMD_DUMP_NAMESPACES;
} else if (strcmp(*argv, "add") == 0) {
NEXT_ARG();
if (get_u16(&opts.ns_id, *argv, 0))
invarg("Invalid namespace ID", *argv);
if (NEXT_ARG_OK()) {
NEXT_ARG_FWD();
if (strcmp(*argv, "data") == 0) {
NEXT_ARG();
if (get_u32(&opts.ns_data, *argv, 0))
invarg("Invalid data", *argv);
maybe_wide = true;
opts.has_ns_data = true;
} else if (strcmp(*argv, "wide") == 0) {
NEXT_ARG();
if (get_u64(&opts.ns_data_wide, *argv, 16))
invarg("Invalid wide data", *argv);
opts.has_ns_data_wide = true;
} else {
invarg("Invalid argument", *argv);
}
}
if (NEXT_ARG_OK()) {
NEXT_ARG_FWD();
if (!maybe_wide || strcmp(*argv, "wide") != 0)
invarg("Unexpected argument", *argv);
NEXT_ARG();
if (get_u64(&opts.ns_data_wide, *argv, 16))
invarg("Invalid wide data", *argv);
opts.has_ns_data_wide = true;
}
opts.cmd = IOAM6_CMD_ADD_NAMESPACE;
} else if (strcmp(*argv, "del") == 0) {
NEXT_ARG();
if (get_u16(&opts.ns_id, *argv, 0))
invarg("Invalid namespace ID", *argv);
opts.cmd = IOAM6_CMD_DEL_NAMESPACE;
} else if (strcmp(*argv, "set") == 0) {
NEXT_ARG();
if (get_u16(&opts.ns_id, *argv, 0))
invarg("Invalid namespace ID", *argv);
NEXT_ARG();
if (strcmp(*argv, "schema") != 0)
invarg("Unknown", *argv);
NEXT_ARG();
if (strcmp(*argv, "none") == 0) {
opts.sc_none = true;
} else {
if (get_u32(&opts.sc_id, *argv, 0))
invarg("Invalid schema ID", *argv);
opts.sc_none = false;
}
opts.cmd = IOAM6_CMD_NS_SET_SCHEMA;
} else {
invarg("Unknown", *argv);
}
} else if (strcmp(*argv, "schema") == 0) {
NEXT_ARG();
if (strcmp(*argv, "show") == 0) {
opts.cmd = IOAM6_CMD_DUMP_SCHEMAS;
} else if (strcmp(*argv, "add") == 0) {
NEXT_ARG();
if (get_u32(&opts.sc_id, *argv, 0))
invarg("Invalid schema ID", *argv);
NEXT_ARG();
if (strlen(*argv) > IOAM6_MAX_SCHEMA_DATA_LEN)
invarg("Schema DATA too big", *argv);
memcpy(opts.sc_data, *argv, strlen(*argv));
opts.cmd = IOAM6_CMD_ADD_SCHEMA;
} else if (strcmp(*argv, "del") == 0) {
NEXT_ARG();
if (get_u32(&opts.sc_id, *argv, 0))
invarg("Invalid schema ID", *argv);
opts.cmd = IOAM6_CMD_DEL_SCHEMA;
} else {
invarg("Unknown", *argv);
}
} else if (strcmp(*argv, "monitor") == 0) {
opts.monitor = true;
} else {
invarg("Unknown", *argv);
}
return ioam6_do_cmd();
}
|