static int res_no_args_parse_cb(const struct nlmsghdr *nlh, void *data) { struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; struct rd *rd = data; const char *name; uint32_t idx; mnl_attr_parse(nlh, 0, rd_attr_cb, tb); if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] || !tb[RDMA_NLDEV_ATTR_RES_SUMMARY]) return MNL_CB_ERROR; idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); if (rd->json_output) { jsonw_uint_field(rd->jw, "ifindex", idx); jsonw_string_field(rd->jw, "ifname", name); } else { pr_out("%u: %s: ", idx, name); } res_print_summary(rd, tb); if (!rd->json_output) pr_out("\n"); return MNL_CB_OK; }
static void print_cm_id_state(struct rd *rd, uint8_t state) { if (rd->json_output) { jsonw_string_field(rd->jw, "state", cm_id_state_to_str(state)); return; } pr_out("state %s ", cm_id_state_to_str(state)); }
static void print_ps(struct rd *rd, uint32_t ps) { if (rd->json_output) { jsonw_string_field(rd->jw, "ps", cm_id_ps_to_str(ps)); return; } pr_out("ps %s ", cm_id_ps_to_str(ps)); }
static void print_qp_type(struct rd *rd, uint32_t val) { if (rd->json_output) jsonw_string_field(rd->jw, "qp-type", qp_types_to_str(val)); else pr_out("qp-type %s ", qp_types_to_str(val)); }
static void print_poll_ctx(struct rd *rd, uint8_t poll_ctx) { if (rd->json_output) { jsonw_string_field(rd->jw, "poll-ctx", poll_ctx_to_str(poll_ctx)); return; } pr_out("poll-ctx %s ", poll_ctx_to_str(poll_ctx)); }
static void print_dev(struct rd *rd, uint32_t idx, const char *name) { if (rd->json_output) { jsonw_uint_field(rd->jw, "ifindex", idx); jsonw_string_field(rd->jw, "ifname", name); } else { pr_out("dev %s ", name); } }
static void print_ipaddr(struct rd *rd, const char *key, char *addrstr, uint16_t port) { if (rd->json_output) { int name_size = INET6_ADDRSTRLEN+strlen(":65535"); char json_name[name_size]; snprintf(json_name, name_size, "%s:%u", addrstr, port); jsonw_string_field(rd->jw, key, json_name); return; } pr_out("%s %s:%u ", key, addrstr, port); }
static void print_pathmig(struct rd *rd, uint32_t val, struct nlattr **nla_line) { if (!nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]) return; if (rd->json_output) jsonw_string_field(rd->jw, "path-mig-state", path_mig_to_str(val)); else pr_out("path-mig-state %s ", path_mig_to_str(val)); }
static int show_bpf_prog(int id, const char *attach_type_str, const char *attach_flags_str, int level) { struct bpf_prog_info info = {}; __u32 info_len = sizeof(info); int prog_fd; prog_fd = bpf_prog_get_fd_by_id(id); if (prog_fd < 0) return -1; if (bpf_obj_get_info_by_fd(prog_fd, &info, &info_len)) { close(prog_fd); return -1; } if (json_output) { jsonw_start_object(json_wtr); jsonw_uint_field(json_wtr, "id", info.id); jsonw_string_field(json_wtr, "attach_type", attach_type_str); jsonw_string_field(json_wtr, "attach_flags", attach_flags_str); jsonw_string_field(json_wtr, "name", info.name); jsonw_end_object(json_wtr); } else { printf("%s%-8u %-15s %-15s %-15s\n", level ? " " : "", info.id, attach_type_str, attach_flags_str, info.name); } close(prog_fd); return 0; }
int main(int argc, char **argv) { json_writer_t *wr = jsonw_new(stdout); jsonw_start_object(wr); jsonw_pretty(wr, true); jsonw_name(wr, "Vyatta"); jsonw_start_object(wr); jsonw_string_field(wr, "url", "http://vyatta.com"); jsonw_uint_field(wr, "downloads", 2000000ul); jsonw_float_field(wr, "stock", 8.16); jsonw_name(wr, "ARGV"); jsonw_start_array(wr); while (--argc) jsonw_string(wr, *++argv); jsonw_end_array(wr); jsonw_name(wr, "empty"); jsonw_start_array(wr); jsonw_end_array(wr); jsonw_name(wr, "NIL"); jsonw_start_object(wr); jsonw_end_object(wr); jsonw_null_field(wr, "my_null"); jsonw_name(wr, "special chars"); jsonw_start_array(wr); jsonw_string_field(wr, "slash", "/"); jsonw_string_field(wr, "newline", "\n"); jsonw_string_field(wr, "tab", "\t"); jsonw_string_field(wr, "ff", "\f"); jsonw_string_field(wr, "quote", "\""); jsonw_string_field(wr, "tick", "\'"); jsonw_string_field(wr, "backslash", "\\"); jsonw_end_array(wr); jsonw_end_object(wr); jsonw_end_object(wr); jsonw_destroy(&wr); return 0; }
void print_color_string(enum output_type type, enum color_attr color, const char *key, const char *fmt, const char *value) { if (_IS_JSON_CONTEXT(type)) { if (key && !value) jsonw_name(_jw, key); else if (!key && value) jsonw_string(_jw, value); else jsonw_string_field(_jw, key, value); } else if (_IS_FP_CONTEXT(type)) { color_fprintf(stdout, color, fmt, value); } }
static void print_link(struct rd *rd, uint32_t idx, const char *name, uint32_t port, struct nlattr **nla_line) { if (rd->json_output) { jsonw_uint_field(rd->jw, "ifindex", idx); if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]) jsonw_uint_field(rd->jw, "port", port); jsonw_string_field(rd->jw, "ifname", name); } else { if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]) pr_out("link %s/%u ", name, port); else pr_out("link %s/- ", name); } }
void print_color_hex(enum output_type type, enum color_attr color, const char *key, const char *fmt, unsigned int hex) { if (_IS_JSON_CONTEXT(type)) { SPRINT_BUF(b1); snprintf(b1, sizeof(b1), "%x", hex); if (key) jsonw_string_field(_jw, key, b1); else jsonw_string(_jw, b1); } else if (_IS_FP_CONTEXT(type)) { color_fprintf(stdout, color, fmt, hex); } }
static void print_comm(struct rd *rd, const char *str, struct nlattr **nla_line) { char tmp[18]; if (rd->json_output) { /* Don't beatify output in JSON format */ jsonw_string_field(rd->jw, "comm", str); return; } if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) snprintf(tmp, sizeof(tmp), "%s", str); else snprintf(tmp, sizeof(tmp), "[%s]", str); pr_out("comm %s ", tmp); }
static int do_show_tree_fn(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftw) { enum bpf_attach_type type; bool skip = true; int cgroup_fd; if (typeflag != FTW_D) return 0; cgroup_fd = open(fpath, O_RDONLY); if (cgroup_fd < 0) { p_err("can't open cgroup %s: %s", fpath, strerror(errno)); return SHOW_TREE_FN_ERR; } for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) { int count = count_attached_bpf_progs(cgroup_fd, type); if (count < 0 && errno != EINVAL) { p_err("can't query bpf programs attached to %s: %s", fpath, strerror(errno)); close(cgroup_fd); return SHOW_TREE_FN_ERR; } if (count > 0) { skip = false; break; } } if (skip) { close(cgroup_fd); return 0; } if (json_output) { jsonw_start_object(json_wtr); jsonw_string_field(json_wtr, "cgroup", fpath); jsonw_name(json_wtr, "programs"); jsonw_start_array(json_wtr); } else { printf("%s\n", fpath); } for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) show_attached_bpf_progs(cgroup_fd, type, ftw->level); if (errno == EINVAL) /* Last attach type does not support query. * Do not report an error for this, especially because batch * mode would stop processing commands. */ errno = 0; if (json_output) { jsonw_end_array(json_wtr); jsonw_end_object(json_wtr); } close(cgroup_fd); return 0; }
static int dump_btf_type(const struct btf *btf, __u32 id, const struct btf_type *t) { json_writer_t *w = json_wtr; int kind, safe_kind; kind = BTF_INFO_KIND(t->info); safe_kind = kind <= BTF_KIND_MAX ? kind : BTF_KIND_UNKN; if (json_output) { jsonw_start_object(w); jsonw_uint_field(w, "id", id); jsonw_string_field(w, "kind", btf_kind_str[safe_kind]); jsonw_string_field(w, "name", btf_str(btf, t->name_off)); } else { printf("[%u] %s '%s'", id, btf_kind_str[safe_kind], btf_str(btf, t->name_off)); } switch (BTF_INFO_KIND(t->info)) { case BTF_KIND_INT: { __u32 v = *(__u32 *)(t + 1); const char *enc; enc = btf_int_enc_str(BTF_INT_ENCODING(v)); if (json_output) { jsonw_uint_field(w, "size", t->size); jsonw_uint_field(w, "bits_offset", BTF_INT_OFFSET(v)); jsonw_uint_field(w, "nr_bits", BTF_INT_BITS(v)); jsonw_string_field(w, "encoding", enc); } else { printf(" size=%u bits_offset=%u nr_bits=%u encoding=%s", t->size, BTF_INT_OFFSET(v), BTF_INT_BITS(v), enc); } break; } case BTF_KIND_PTR: case BTF_KIND_CONST: case BTF_KIND_VOLATILE: case BTF_KIND_RESTRICT: case BTF_KIND_TYPEDEF: if (json_output) jsonw_uint_field(w, "type_id", t->type); else printf(" type_id=%u", t->type); break; case BTF_KIND_ARRAY: { const struct btf_array *arr = (const void *)(t + 1); if (json_output) { jsonw_uint_field(w, "type_id", arr->type); jsonw_uint_field(w, "index_type_id", arr->index_type); jsonw_uint_field(w, "nr_elems", arr->nelems); } else { printf(" type_id=%u index_type_id=%u nr_elems=%u", arr->type, arr->index_type, arr->nelems); } break; } case BTF_KIND_STRUCT: case BTF_KIND_UNION: { const struct btf_member *m = (const void *)(t + 1); __u16 vlen = BTF_INFO_VLEN(t->info); int i; if (json_output) { jsonw_uint_field(w, "size", t->size); jsonw_uint_field(w, "vlen", vlen); jsonw_name(w, "members"); jsonw_start_array(w); } else { printf(" size=%u vlen=%u", t->size, vlen); } for (i = 0; i < vlen; i++, m++) { const char *name = btf_str(btf, m->name_off); __u32 bit_off, bit_sz; if (BTF_INFO_KFLAG(t->info)) { bit_off = BTF_MEMBER_BIT_OFFSET(m->offset); bit_sz = BTF_MEMBER_BITFIELD_SIZE(m->offset); } else { bit_off = m->offset; bit_sz = 0; } if (json_output) { jsonw_start_object(w); jsonw_string_field(w, "name", name); jsonw_uint_field(w, "type_id", m->type); jsonw_uint_field(w, "bits_offset", bit_off); if (bit_sz) { jsonw_uint_field(w, "bitfield_size", bit_sz); } jsonw_end_object(w); } else { printf("\n\t'%s' type_id=%u bits_offset=%u", name, m->type, bit_off); if (bit_sz) printf(" bitfield_size=%u", bit_sz); } } if (json_output) jsonw_end_array(w); break; } case BTF_KIND_ENUM: { const struct btf_enum *v = (const void *)(t + 1); __u16 vlen = BTF_INFO_VLEN(t->info); int i; if (json_output) { jsonw_uint_field(w, "size", t->size); jsonw_uint_field(w, "vlen", vlen); jsonw_name(w, "values"); jsonw_start_array(w); } else { printf(" size=%u vlen=%u", t->size, vlen); } for (i = 0; i < vlen; i++, v++) { const char *name = btf_str(btf, v->name_off); if (json_output) { jsonw_start_object(w); jsonw_string_field(w, "name", name); jsonw_uint_field(w, "val", v->val); jsonw_end_object(w); } else { printf("\n\t'%s' val=%u", name, v->val); } } if (json_output) jsonw_end_array(w); break; } case BTF_KIND_FWD: { const char *fwd_kind = BTF_INFO_KFLAG(t->info) ? "union" : "struct"; if (json_output) jsonw_string_field(w, "fwd_kind", fwd_kind); else printf(" fwd_kind=%s", fwd_kind); break; } case BTF_KIND_FUNC: if (json_output) jsonw_uint_field(w, "type_id", t->type); else printf(" type_id=%u", t->type); break; case BTF_KIND_FUNC_PROTO: { const struct btf_param *p = (const void *)(t + 1); __u16 vlen = BTF_INFO_VLEN(t->info); int i; if (json_output) { jsonw_uint_field(w, "ret_type_id", t->type); jsonw_uint_field(w, "vlen", vlen); jsonw_name(w, "params"); jsonw_start_array(w); } else { printf(" ret_type_id=%u vlen=%u", t->type, vlen); } for (i = 0; i < vlen; i++, p++) { const char *name = btf_str(btf, p->name_off); if (json_output) { jsonw_start_object(w); jsonw_string_field(w, "name", name); jsonw_uint_field(w, "type_id", p->type); jsonw_end_object(w); } else { printf("\n\t'%s' type_id=%u", name, p->type); } } if (json_output) jsonw_end_array(w); break; } case BTF_KIND_VAR: { const struct btf_var *v = (const void *)(t + 1); const char *linkage; linkage = btf_var_linkage_str(v->linkage); if (json_output) { jsonw_uint_field(w, "type_id", t->type); jsonw_string_field(w, "linkage", linkage); } else { printf(" type_id=%u, linkage=%s", t->type, linkage); } break; } case BTF_KIND_DATASEC: { const struct btf_var_secinfo *v = (const void *)(t+1); __u16 vlen = BTF_INFO_VLEN(t->info); int i; if (json_output) { jsonw_uint_field(w, "size", t->size); jsonw_uint_field(w, "vlen", vlen); jsonw_name(w, "vars"); jsonw_start_array(w); } else { printf(" size=%u vlen=%u", t->size, vlen); } for (i = 0; i < vlen; i++, v++) { if (json_output) { jsonw_start_object(w); jsonw_uint_field(w, "type_id", v->type); jsonw_uint_field(w, "offset", v->offset); jsonw_uint_field(w, "size", v->size); jsonw_end_object(w); } else { printf("\n\ttype_id=%u offset=%u size=%u", v->type, v->offset, v->size); } } if (json_output) jsonw_end_array(w); break; } default: break; } if (json_output) jsonw_end_object(json_wtr); else printf("\n"); return 0; }