| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "format-table.h" |
| 5 |
#include "json-util.h" |
| 6 |
#include "log.h" |
| 7 |
#include "networkctl.h" |
| 8 |
#include "networkctl-dump-util.h" |
| 9 |
#include "networkctl-lldp.h" |
| 10 |
#include "networkctl-util.h" |
| 11 |
#include "stdio-util.h" |
| 12 |
#include "string-util.h" |
| 13 |
#include "strv.h" |
| 14 |
#include "terminal-util.h" |
| 15 |
#include "varlink-util.h" |
| 16 |
|
| 17 |
typedef struct InterfaceInfo { |
| 18 |
int ifindex; |
| 19 |
const char *ifname; |
| 20 |
char **altnames; |
| 21 |
sd_json_variant *v; |
| 22 |
} InterfaceInfo; |
| 23 |
|
| 24 |
static void interface_info_done(InterfaceInfo *p) { |
| 25 |
if (!p) |
| 26 |
return; |
| 27 |
|
| 28 |
strv_free(p->altnames); |
| 29 |
sd_json_variant_unref(p->v); |
| 30 |
} |
| 31 |
|
| 32 |
static const sd_json_dispatch_field interface_info_dispatch_table[] = { |
| 33 |
{ "InterfaceIndex", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_ifindex, offsetof(InterfaceInfo, ifindex), SD_JSON_MANDATORY|SD_JSON_RELAX }, |
| 34 |
{ "InterfaceName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(InterfaceInfo, ifname), SD_JSON_MANDATORY }, |
| 35 |
{ "InterfaceAlternativeNames", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(InterfaceInfo, altnames), 0 }, |
| 36 |
{ "Neighbors", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(InterfaceInfo, v), 0 }, |
| 37 |
{}, |
| 38 |
}; |
| 39 |
|
| 40 |
typedef struct LLDPNeighborInfo { |
| 41 |
const char *chassis_id; |
| 42 |
const char *port_id; |
| 43 |
const char *port_description; |
| 44 |
const char *system_name; |
| 45 |
const char *system_description; |
| 46 |
uint16_t capabilities; |
| 47 |
uint16_t vlan_id; |
| 48 |
} LLDPNeighborInfo; |
| 49 |
|
| 50 |
static const sd_json_dispatch_field lldp_neighbor_dispatch_table[] = { |
| 51 |
{ "ChassisID", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LLDPNeighborInfo, chassis_id), 0 }, |
| 52 |
{ "PortID", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LLDPNeighborInfo, port_id), 0 }, |
| 53 |
{ "PortDescription", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LLDPNeighborInfo, port_description), 0 }, |
| 54 |
{ "SystemName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LLDPNeighborInfo, system_name), 0 }, |
| 55 |
{ "SystemDescription", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LLDPNeighborInfo, system_description), 0 }, |
| 56 |
{ "EnabledCapabilities", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint16, offsetof(LLDPNeighborInfo, capabilities), 0 }, |
| 57 |
{ "VlanID", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint16, offsetof(LLDPNeighborInfo, vlan_id), 0 }, |
| 58 |
{}, |
| 59 |
}; |
| 60 |
|
| 61 |
int dump_lldp_neighbors(sd_varlink *vl, Table *table, int ifindex) { |
| 62 |
_cleanup_strv_free_ char **buf = NULL; |
| 63 |
sd_json_variant *reply; |
| 64 |
int r; |
| 65 |
|
| 66 |
assert(vl); |
| 67 |
assert(table); |
| 68 |
assert(ifindex > 0); |
| 69 |
|
| 70 |
r = varlink_callbo_and_log( |
| 71 |
vl, |
| 72 |
"io.systemd.Network.GetLLDPNeighbors", |
| 73 |
&reply, |
| 74 |
SD_JSON_BUILD_PAIR_INTEGER("InterfaceIndex", ifindex)); |
| 75 |
if (r < 0) |
| 76 |
return r; |
| 77 |
|
| 78 |
sd_json_variant *i; |
| 79 |
JSON_VARIANT_ARRAY_FOREACH(i, sd_json_variant_by_key(reply, "Neighbors")) { |
| 80 |
_cleanup_(interface_info_done) InterfaceInfo info = {}; |
| 81 |
|
| 82 |
r = sd_json_dispatch(i, interface_info_dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &info); |
| 83 |
if (r < 0) |
| 84 |
return r; |
| 85 |
|
| 86 |
if (info.ifindex != ifindex) |
| 87 |
continue; |
| 88 |
|
| 89 |
sd_json_variant *neighbor; |
| 90 |
JSON_VARIANT_ARRAY_FOREACH(neighbor, info.v) { |
| 91 |
LLDPNeighborInfo neighbor_info = {}; |
| 92 |
|
| 93 |
r = sd_json_dispatch(neighbor, lldp_neighbor_dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &neighbor_info); |
| 94 |
if (r < 0) |
| 95 |
return r; |
| 96 |
|
| 97 |
r = strv_extendf(&buf, "%s%s%s%s on port %s%s%s%s", |
| 98 |
strna(neighbor_info.system_name), |
| 99 |
isempty(neighbor_info.system_description) ? "" : " (", |
| 100 |
strempty(neighbor_info.system_description), |
| 101 |
isempty(neighbor_info.system_description) ? "" : ")", |
| 102 |
strna(neighbor_info.port_id), |
| 103 |
isempty(neighbor_info.port_description) ? "" : " (", |
| 104 |
strempty(neighbor_info.port_description), |
| 105 |
isempty(neighbor_info.port_description) ? "" : ")"); |
| 106 |
if (r < 0) |
| 107 |
return log_oom(); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return dump_list(table, "Connected To", buf); |
| 112 |
} |
| 113 |
|
| 114 |
static char* lldp_capabilities_to_string(uint64_t x) { |
| 115 |
static const char characters[] = { |
| 116 |
'o', 'p', 'b', 'w', 'r', 't', 'd', 'a', 'c', 's', 'm', |
| 117 |
}; |
| 118 |
char *ret; |
| 119 |
unsigned i; |
| 120 |
|
| 121 |
ret = new(char, ELEMENTSOF(characters) + 1); |
| 122 |
if (!ret) |
| 123 |
return NULL; |
| 124 |
|
| 125 |
for (i = 0; i < ELEMENTSOF(characters); i++) |
| 126 |
ret[i] = (x & (1U << i)) ? characters[i] : '.'; |
| 127 |
|
| 128 |
ret[i] = 0; |
| 129 |
return ret; |
| 130 |
} |
| 131 |
|
| 132 |
static void lldp_capabilities_legend(uint16_t x) { |
| 133 |
unsigned cols = columns(); |
| 134 |
static const char* const table[] = { |
| 135 |
"o - Other", |
| 136 |
"p - Repeater", |
| 137 |
"b - Bridge", |
| 138 |
"w - WLAN Access Point", |
| 139 |
"r - Router", |
| 140 |
"t - Telephone", |
| 141 |
"d - DOCSIS cable device", |
| 142 |
"a - Station", |
| 143 |
"c - Customer VLAN", |
| 144 |
"s - Service VLAN", |
| 145 |
"m - Two-port MAC Relay (TPMR)", |
| 146 |
}; |
| 147 |
|
| 148 |
if (x == 0) |
| 149 |
return; |
| 150 |
|
| 151 |
printf("\nCapability Flags:\n"); |
| 152 |
for (unsigned w = 0, i = 0; i < ELEMENTSOF(table); i++) |
| 153 |
if (x & (1U << i) || arg_all) { |
| 154 |
bool newline; |
| 155 |
|
| 156 |
newline = w + strlen(table[i]) + (w == 0 ? 0 : 2) > cols; |
| 157 |
if (newline) |
| 158 |
w = 0; |
| 159 |
w += printf("%s%s%s", newline ? "\n" : "", w == 0 ? "" : "; ", table[i]); |
| 160 |
} |
| 161 |
puts(""); |
| 162 |
} |
| 163 |
|
| 164 |
static bool interface_match_pattern(const InterfaceInfo *info, char * const *patterns) { |
| 165 |
assert(info); |
| 166 |
|
| 167 |
if (strv_isempty(patterns)) |
| 168 |
return true; |
| 169 |
|
| 170 |
if (strv_fnmatch(patterns, info->ifname)) |
| 171 |
return true; |
| 172 |
|
| 173 |
char str[DECIMAL_STR_MAX(int)]; |
| 174 |
xsprintf(str, "%i", info->ifindex); |
| 175 |
if (strv_fnmatch(patterns, str)) |
| 176 |
return true; |
| 177 |
|
| 178 |
STRV_FOREACH(a, info->altnames) |
| 179 |
if (strv_fnmatch(patterns, *a)) |
| 180 |
return true; |
| 181 |
|
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
static int dump_lldp_neighbors_json(sd_json_variant *reply, char * const *patterns) { |
| 186 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL, *v = NULL; |
| 187 |
int r; |
| 188 |
|
| 189 |
assert(reply); |
| 190 |
|
| 191 |
if (strv_isempty(patterns)) |
| 192 |
return sd_json_variant_dump(reply, arg_json_format_flags, NULL, NULL); |
| 193 |
|
| 194 |
/* Filter and dump the result. */ |
| 195 |
|
| 196 |
sd_json_variant *i; |
| 197 |
JSON_VARIANT_ARRAY_FOREACH(i, sd_json_variant_by_key(reply, "Neighbors")) { |
| 198 |
_cleanup_(interface_info_done) InterfaceInfo info = {}; |
| 199 |
|
| 200 |
r = sd_json_dispatch(i, interface_info_dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &info); |
| 201 |
if (r < 0) |
| 202 |
return r; |
| 203 |
|
| 204 |
if (!interface_match_pattern(&info, patterns)) |
| 205 |
continue; |
| 206 |
|
| 207 |
r = sd_json_variant_append_array(&array, i); |
| 208 |
if (r < 0) |
| 209 |
return log_error_errno(r, "Failed to append json variant to array: %m"); |
| 210 |
} |
| 211 |
|
| 212 |
r = sd_json_buildo( |
| 213 |
&v, |
| 214 |
SD_JSON_BUILD_PAIR_CONDITION(sd_json_variant_is_blank_array(array), "Neighbors", SD_JSON_BUILD_EMPTY_ARRAY), |
| 215 |
SD_JSON_BUILD_PAIR_CONDITION(!sd_json_variant_is_blank_array(array), "Neighbors", SD_JSON_BUILD_VARIANT(array))); |
| 216 |
if (r < 0) |
| 217 |
return log_error_errno(r, "Failed to build json varinat: %m"); |
| 218 |
|
| 219 |
return sd_json_variant_dump(v, arg_json_format_flags, NULL, NULL); |
| 220 |
} |
| 221 |
|
| 222 |
int verb_link_lldp_status(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 223 |
_cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL; |
| 224 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 225 |
sd_json_variant *reply; |
| 226 |
uint64_t all = 0; |
| 227 |
TableCell *cell; |
| 228 |
size_t m = 0; |
| 229 |
int r; |
| 230 |
|
| 231 |
r = varlink_connect_networkd(&vl); |
| 232 |
if (r < 0) |
| 233 |
return r; |
| 234 |
|
| 235 |
r = varlink_call_and_log(vl, "io.systemd.Network.GetLLDPNeighbors", NULL, &reply); |
| 236 |
if (r < 0) |
| 237 |
return r; |
| 238 |
|
| 239 |
if (sd_json_format_enabled(arg_json_format_flags)) |
| 240 |
return dump_lldp_neighbors_json(reply, strv_skip(argv, 1)); |
| 241 |
|
| 242 |
pager_open(arg_pager_flags); |
| 243 |
|
| 244 |
table = table_new("index", |
| 245 |
"link", |
| 246 |
"system-name", |
| 247 |
"system-description", |
| 248 |
"chassis-id", |
| 249 |
"port-id", |
| 250 |
"port-description", |
| 251 |
"caps"); |
| 252 |
if (!table) |
| 253 |
return log_oom(); |
| 254 |
|
| 255 |
if (arg_full) |
| 256 |
table_set_width(table, 0); |
| 257 |
|
| 258 |
table_set_header(table, arg_legend); |
| 259 |
table_set_ersatz_string(table, TABLE_ERSATZ_DASH); |
| 260 |
table_set_sort(table, (size_t) 0, (size_t) 2); |
| 261 |
table_hide_column_from_display(table, (size_t) 0); |
| 262 |
|
| 263 |
/* Make the capabilities not truncated */ |
| 264 |
assert_se(cell = table_get_cell(table, 0, 7)); |
| 265 |
table_set_minimum_width(table, cell, 11); |
| 266 |
|
| 267 |
sd_json_variant *i; |
| 268 |
JSON_VARIANT_ARRAY_FOREACH(i, sd_json_variant_by_key(reply, "Neighbors")) { |
| 269 |
_cleanup_(interface_info_done) InterfaceInfo info = {}; |
| 270 |
|
| 271 |
r = sd_json_dispatch(i, interface_info_dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &info); |
| 272 |
if (r < 0) |
| 273 |
return r; |
| 274 |
|
| 275 |
if (!interface_match_pattern(&info, strv_skip(argv, 1))) |
| 276 |
continue; |
| 277 |
|
| 278 |
sd_json_variant *neighbor; |
| 279 |
JSON_VARIANT_ARRAY_FOREACH(neighbor, info.v) { |
| 280 |
LLDPNeighborInfo neighbor_info = {}; |
| 281 |
|
| 282 |
r = sd_json_dispatch(neighbor, lldp_neighbor_dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &neighbor_info); |
| 283 |
if (r < 0) |
| 284 |
return r; |
| 285 |
|
| 286 |
all |= neighbor_info.capabilities; |
| 287 |
|
| 288 |
_cleanup_free_ char *cap_str = lldp_capabilities_to_string(neighbor_info.capabilities); |
| 289 |
|
| 290 |
r = table_add_many(table, |
| 291 |
TABLE_INT, info.ifindex, |
| 292 |
TABLE_STRING, info.ifname, |
| 293 |
TABLE_STRING, neighbor_info.system_name, |
| 294 |
TABLE_STRING, neighbor_info.system_description, |
| 295 |
TABLE_STRING, neighbor_info.chassis_id, |
| 296 |
TABLE_STRING, neighbor_info.port_id, |
| 297 |
TABLE_STRING, neighbor_info.port_description, |
| 298 |
TABLE_STRING, cap_str); |
| 299 |
if (r < 0) |
| 300 |
return table_log_add_error(r); |
| 301 |
|
| 302 |
m++; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
r = table_print_or_warn(table); |
| 307 |
if (r < 0) |
| 308 |
return r; |
| 309 |
|
| 310 |
if (arg_legend) { |
| 311 |
lldp_capabilities_legend(all); |
| 312 |
printf("\n%zu neighbor(s) listed.\n", m); |
| 313 |
} |
| 314 |
|
| 315 |
return 0; |
| 316 |
} |