]> git.ipfire.org Git - thirdparty/lldpd.git/blob - src/client/display.c
lldp: attach remote TTL to port instead of chassis
[thirdparty/lldpd.git] / src / client / display.c
1 /* -*- mode: c; c-file-style: "openbsd" -*- */
2 /*
3 * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <ctype.h>
22 #include <time.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
27 #include <arpa/inet.h>
28 #include <string.h>
29
30 #include "../log.h"
31 #include "client.h"
32
33 static void
34 display_cap(struct writer * w, lldpctl_atom_t *chassis, u_int8_t bit, char *symbol)
35 {
36 if (lldpctl_atom_get_int(chassis, lldpctl_k_chassis_cap_available) & bit) {
37 tag_start(w, "capability", "Capability");
38 tag_attr (w, "type", "", symbol );
39 tag_attr (w, "enabled", "",
40 (lldpctl_atom_get_int(chassis, lldpctl_k_chassis_cap_enabled) & bit)?
41 "on":"off");
42 tag_end (w);
43 }
44 }
45
46 static void
47 display_med_capability(struct writer *w, long int available, int cap,
48 const char *symbol)
49 {
50 if (available & cap) {
51 tag_start(w, "capability", "Capability");
52 tag_attr(w, "type", "", symbol);
53 tag_attr(w, "available", "", "yes");
54 tag_end(w);
55 }
56 }
57
58 static void
59 display_med(struct writer *w, lldpctl_atom_t *port, lldpctl_atom_t *chassis)
60 {
61 lldpctl_atom_t *medpolicies, *medpolicy;
62 lldpctl_atom_t *medlocations, *medlocation;
63 lldpctl_atom_t *caelements, *caelement;
64 lldpctl_atom_t *medpower;
65 long int cap = lldpctl_atom_get_int(chassis, lldpctl_k_chassis_med_cap);
66 const char *type;
67
68 if (lldpctl_atom_get_int(chassis, lldpctl_k_chassis_med_type) <= 0)
69 return;
70
71 tag_start(w, "lldp-med", "LLDP-MED");
72
73 tag_datatag(w, "device-type", "Device Type",
74 lldpctl_atom_get_str(chassis, lldpctl_k_chassis_med_type));
75
76 display_med_capability(w, cap, LLDP_MED_CAP_CAP, "Capabilities");
77 display_med_capability(w, cap, LLDP_MED_CAP_POLICY, "Policy");
78 display_med_capability(w, cap, LLDP_MED_CAP_LOCATION, "Location");
79 display_med_capability(w, cap, LLDP_MED_CAP_MDI_PSE, "MDI/PSE");
80 display_med_capability(w, cap, LLDP_MED_CAP_MDI_PD, "MDI/PD");
81 display_med_capability(w, cap, LLDP_MED_CAP_IV, "Inventory");
82
83 /* LLDP MED policies */
84 medpolicies = lldpctl_atom_get(port, lldpctl_k_port_med_policies);
85 lldpctl_atom_foreach(medpolicies, medpolicy) {
86 if (lldpctl_atom_get_int(medpolicy,
87 lldpctl_k_med_policy_type) <= 0) continue;
88
89 tag_start(w, "policy", "LLDP-MED Network Policy for");
90 tag_attr(w, "apptype", "", lldpctl_atom_get_str(medpolicy, lldpctl_k_med_policy_type));
91 tag_attr(w, "defined", "Defined",
92 (lldpctl_atom_get_int(medpolicy,
93 lldpctl_k_med_policy_unknown) > 0)?"no":"yes");
94
95 if (lldpctl_atom_get_int(medpolicy,
96 lldpctl_k_med_policy_tagged) > 0) {
97 int vid = lldpctl_atom_get_int(medpolicy,
98 lldpctl_k_med_policy_vid);
99 tag_start(w, "vlan", "VLAN");
100 if (vid == 0) {
101 tag_attr(w, "vid", "", "priority");
102 } else if (vid == 4095) {
103 tag_attr(w, "vid", "", "reserved");
104 } else {
105 tag_attr(w, "vid", "",
106 lldpctl_atom_get_str(medpolicy,
107 lldpctl_k_med_policy_vid));
108 }
109 tag_end(w);
110 }
111
112 tag_datatag(w, "priority", "Priority",
113 lldpctl_atom_get_str(medpolicy,
114 lldpctl_k_med_policy_priority));
115 /* Also give a numeric value */
116 int pcp = lldpctl_atom_get_int(medpolicy,
117 lldpctl_k_med_policy_priority);
118 char spcp[2] = { pcp + '0', '\0' };
119 tag_datatag(w, "pcp", "PCP", spcp);
120 tag_datatag(w, "dscp", "DSCP Value",
121 lldpctl_atom_get_str(medpolicy,
122 lldpctl_k_med_policy_dscp));
123
124 tag_end(w);
125 }
126 lldpctl_atom_dec_ref(medpolicies);
127
128 /* LLDP MED locations */
129 medlocations = lldpctl_atom_get(port, lldpctl_k_port_med_locations);
130 lldpctl_atom_foreach(medlocations, medlocation) {
131 int format = lldpctl_atom_get_int(medlocation,
132 lldpctl_k_med_location_format);
133 if (format <= 0) continue;
134 tag_start(w, "location", "LLDP-MED Location Identification");
135 tag_attr(w, "type", "Type",
136 lldpctl_atom_get_str(medlocation,
137 lldpctl_k_med_location_format));
138
139 switch (format) {
140 case LLDP_MED_LOCFORMAT_COORD:
141 tag_attr(w, "geoid", "Geoid",
142 lldpctl_atom_get_str(medlocation,
143 lldpctl_k_med_location_geoid));
144 tag_datatag(w, "lat", "Latitude",
145 lldpctl_atom_get_str(medlocation,
146 lldpctl_k_med_location_latitude));
147 tag_datatag(w, "lon", "Longitude",
148 lldpctl_atom_get_str(medlocation,
149 lldpctl_k_med_location_longitude));
150 tag_start(w, "altitude", "Altitude");
151 tag_attr(w, "unit", "", lldpctl_atom_get_str(medlocation,
152 lldpctl_k_med_location_altitude_unit));
153 tag_data(w, lldpctl_atom_get_str(medlocation,
154 lldpctl_k_med_location_altitude));
155 tag_end(w);
156 break;
157 case LLDP_MED_LOCFORMAT_CIVIC:
158 tag_datatag(w, "country", "Country",
159 lldpctl_atom_get_str(medlocation,
160 lldpctl_k_med_location_country));
161 caelements = lldpctl_atom_get(medlocation,
162 lldpctl_k_med_location_ca_elements);
163 lldpctl_atom_foreach(caelements, caelement) {
164 type = lldpctl_atom_get_str(caelement,
165 lldpctl_k_med_civicaddress_type);
166 tag_datatag(w, totag(type), type,
167 lldpctl_atom_get_str(caelement,
168 lldpctl_k_med_civicaddress_value));
169 }
170 lldpctl_atom_dec_ref(caelements);
171 break;
172 case LLDP_MED_LOCFORMAT_ELIN:
173 tag_datatag(w, "ecs", "ECS ELIN",
174 lldpctl_atom_get_str(medlocation,
175 lldpctl_k_med_location_elin));
176 break;
177 }
178
179 tag_end(w);
180 }
181 lldpctl_atom_dec_ref(medlocations);
182
183 /* LLDP MED power */
184 medpower = lldpctl_atom_get(port, lldpctl_k_port_med_power);
185 if (lldpctl_atom_get_int(medpower, lldpctl_k_med_power_type) > 0) {
186 tag_start(w, "poe", "Extended Power-over-Ethernet");
187
188 tag_datatag(w, "device-type", "Power Type & Source",
189 lldpctl_atom_get_str(medpower, lldpctl_k_med_power_type));
190 tag_datatag(w, "source", "Power Source",
191 lldpctl_atom_get_str(medpower, lldpctl_k_med_power_source));
192 tag_datatag(w, "priority", "Power priority",
193 lldpctl_atom_get_str(medpower, lldpctl_k_med_power_priority));
194 tag_datatag(w, "power", "Power Value",
195 lldpctl_atom_get_str(medpower, lldpctl_k_med_power_val));
196
197 tag_end(w);
198 }
199 lldpctl_atom_dec_ref(medpower);
200
201 /* LLDP MED inventory */
202 do {
203 const char *hw = lldpctl_atom_get_str(chassis,
204 lldpctl_k_chassis_med_inventory_hw);
205 const char *sw = lldpctl_atom_get_str(chassis,
206 lldpctl_k_chassis_med_inventory_sw);
207 const char *fw = lldpctl_atom_get_str(chassis,
208 lldpctl_k_chassis_med_inventory_fw);
209 const char *sn = lldpctl_atom_get_str(chassis,
210 lldpctl_k_chassis_med_inventory_sn);
211 const char *manuf = lldpctl_atom_get_str(chassis,
212 lldpctl_k_chassis_med_inventory_manuf);
213 const char *model = lldpctl_atom_get_str(chassis,
214 lldpctl_k_chassis_med_inventory_model);
215 const char *asset = lldpctl_atom_get_str(chassis,
216 lldpctl_k_chassis_med_inventory_asset);
217 if (!(hw || sw || fw || sn ||
218 manuf || model || asset)) break;
219
220 tag_start(w, "inventory", "Inventory");
221 tag_datatag(w, "hardware", "Hardware Revision", hw);
222 tag_datatag(w, "software", "Software Revision", sw);
223 tag_datatag(w, "firmware", "Firmware Revision", fw);
224 tag_datatag(w, "serial", "Serial Number", sn);
225 tag_datatag(w, "manufacturer", "Manufacturer", manuf);
226 tag_datatag(w, "model", "Model", model);
227 tag_datatag(w, "asset", "Asset ID", asset);
228 tag_end(w);
229 } while(0);
230
231 tag_end(w);
232 }
233
234 static void
235 display_chassis(struct writer* w, lldpctl_atom_t* chassis, int details)
236 {
237 lldpctl_atom_t *mgmts, *mgmt;
238
239 tag_start(w, "chassis", "Chassis");
240 tag_start(w, "id", "ChassisID");
241 tag_attr (w, "type", "",
242 lldpctl_atom_get_str(chassis,
243 lldpctl_k_chassis_id_subtype));
244 tag_data(w, lldpctl_atom_get_str(chassis,
245 lldpctl_k_chassis_id));
246 tag_end(w);
247 tag_datatag(w, "name", "SysName",
248 lldpctl_atom_get_str(chassis, lldpctl_k_chassis_name));
249 if (details == DISPLAY_BRIEF) {
250 tag_end(w);
251 return;
252 }
253 tag_datatag(w, "descr", "SysDescr",
254 lldpctl_atom_get_str(chassis, lldpctl_k_chassis_descr));
255 if (details && lldpctl_atom_get_int(chassis, lldpctl_k_chassis_ttl) > 0)
256 tag_datatag(w, "ttl", "TTL",
257 lldpctl_atom_get_str(chassis, lldpctl_k_chassis_ttl));
258
259 /* Management addresses */
260 mgmts = lldpctl_atom_get(chassis, lldpctl_k_chassis_mgmt);
261 lldpctl_atom_foreach(mgmts, mgmt) {
262 tag_datatag(w, "mgmt-ip", "MgmtIP",
263 lldpctl_atom_get_str(mgmt, lldpctl_k_mgmt_ip));
264 }
265 lldpctl_atom_dec_ref(mgmts);
266
267 /* Capabilities */
268 display_cap(w, chassis, LLDP_CAP_OTHER, "Other");
269 display_cap(w, chassis, LLDP_CAP_REPEATER, "Repeater");
270 display_cap(w, chassis, LLDP_CAP_BRIDGE, "Bridge");
271 display_cap(w, chassis, LLDP_CAP_ROUTER, "Router");
272 display_cap(w, chassis, LLDP_CAP_WLAN, "Wlan");
273 display_cap(w, chassis, LLDP_CAP_TELEPHONE, "Tel");
274 display_cap(w, chassis, LLDP_CAP_DOCSIS, "Docsis");
275 display_cap(w, chassis, LLDP_CAP_STATION, "Station");
276
277 tag_end(w);
278 }
279
280 static void
281 display_custom_tlvs(struct writer* w, lldpctl_atom_t* neighbor)
282 {
283 lldpctl_atom_t *custom_list, *custom;
284 int have_custom_tlvs = 0;
285 size_t i, len, slen;
286 const uint8_t *oui, *oui_info;
287 char buf[1600]; /* should be enough for printing */
288
289 custom_list = lldpctl_atom_get(neighbor, lldpctl_k_custom_tlvs);
290 lldpctl_atom_foreach(custom_list, custom) {
291 /* This tag gets added only once, if there are any custom TLVs */
292 if (!have_custom_tlvs) {
293 tag_start(w, "unknown-tlvs", "Unknown TLVs");
294 have_custom_tlvs++;
295 }
296 len = 0;
297 oui = lldpctl_atom_get_buffer(custom, lldpctl_k_custom_tlv_oui, &len);
298 len = 0;
299 oui_info = lldpctl_atom_get_buffer(custom, lldpctl_k_custom_tlv_oui_info_string, &len);
300 if (!oui)
301 continue;
302 tag_start(w, "unknown-tlv", "TLV");
303
304 /* Add OUI as attribute */
305 snprintf(buf, sizeof(buf), "%02X,%02X,%02X", oui[0], oui[1], oui[2]);
306 tag_attr(w, "oui", "OUI", buf);
307 snprintf(buf, sizeof(buf), "%d",
308 (int)lldpctl_atom_get_int(custom, lldpctl_k_custom_tlv_oui_subtype));
309 tag_attr(w, "subtype", "SubType", buf);
310 snprintf(buf, sizeof(buf), "%d", (int)len);
311 tag_attr(w, "len", "Len", buf);
312 if (len > 0) {
313 for (slen=0, i=0; i < len; ++i)
314 slen += snprintf(buf + slen, sizeof(buf) > slen ? sizeof(buf) - slen : 0,
315 "%02X%s", oui_info[i], ((i < len - 1) ? "," : ""));
316 tag_data(w, buf);
317 }
318 tag_end(w);
319 }
320 lldpctl_atom_dec_ref(custom_list);
321
322 if (have_custom_tlvs)
323 tag_end(w);
324 }
325
326
327 static void
328 display_autoneg(struct writer * w, int advertised, int bithd, int bitfd, char *desc)
329 {
330 if (!((advertised & bithd) ||
331 (advertised & bitfd)))
332 return;
333
334 tag_start(w, "advertised", "Adv");
335 tag_attr(w, "type", "", desc);
336 if (bitfd != bithd) {
337 tag_attr(w, "hd", "HD", (advertised & bithd)?"yes":"no");
338 tag_attr(w, "fd", "FD", (advertised & bitfd)?"yes":"no");
339 }
340 tag_end (w);
341 }
342
343 static void
344 display_port(struct writer *w, lldpctl_atom_t *port, int details)
345 {
346 tag_start(w, "port", "Port");
347 tag_start(w, "id", "PortID");
348 tag_attr (w, "type", "",
349 lldpctl_atom_get_str(port, lldpctl_k_port_id_subtype));
350 tag_data(w, lldpctl_atom_get_str(port, lldpctl_k_port_id));
351 tag_end(w);
352
353 tag_datatag(w, "descr", "PortDescr",
354 lldpctl_atom_get_str(port, lldpctl_k_port_descr));
355 if (details)
356 tag_datatag(w, "ttl", "TTL",
357 lldpctl_atom_get_str(port, lldpctl_k_port_ttl));
358
359 /* Dot3 */
360 if (details == DISPLAY_DETAILS) {
361 tag_datatag(w, "mfs", "MFS",
362 lldpctl_atom_get_str(port, lldpctl_k_port_dot3_mfs));
363 tag_datatag(w, "aggregation", "Port is aggregated. PortAggregID",
364 lldpctl_atom_get_str(port, lldpctl_k_port_dot3_aggregid));
365
366 long int autoneg_support, autoneg_enabled, autoneg_advertised, mautype;
367 autoneg_support = lldpctl_atom_get_int(port,
368 lldpctl_k_port_dot3_autoneg_support);
369 autoneg_enabled = lldpctl_atom_get_int(port,
370 lldpctl_k_port_dot3_autoneg_enabled);
371 autoneg_advertised = lldpctl_atom_get_int(port,
372 lldpctl_k_port_dot3_autoneg_advertised);
373 mautype = lldpctl_atom_get_int(port, lldpctl_k_port_dot3_mautype);
374 if (autoneg_support > 0 || autoneg_enabled > 0 || mautype > 0) {
375 tag_start(w, "auto-negotiation", "PMD autoneg");
376 tag_attr (w, "supported", "supported",
377 (autoneg_support > 0)?"yes":"no");
378 tag_attr (w, "enabled", "enabled",
379 (autoneg_enabled > 0)?"yes":"no");
380
381 if (autoneg_enabled > 0) {
382 if (autoneg_advertised < 0)
383 autoneg_advertised = 0;
384 display_autoneg(w, autoneg_advertised,
385 LLDP_DOT3_LINK_AUTONEG_10BASE_T,
386 LLDP_DOT3_LINK_AUTONEG_10BASET_FD,
387 "10Base-T");
388 display_autoneg(w, autoneg_advertised,
389 LLDP_DOT3_LINK_AUTONEG_100BASE_TX,
390 LLDP_DOT3_LINK_AUTONEG_100BASE_TXFD,
391 "100Base-TX");
392 display_autoneg(w, autoneg_advertised,
393 LLDP_DOT3_LINK_AUTONEG_100BASE_T2,
394 LLDP_DOT3_LINK_AUTONEG_100BASE_T2FD,
395 "100Base-T2");
396 display_autoneg(w, autoneg_advertised,
397 LLDP_DOT3_LINK_AUTONEG_100BASE_T4,
398 LLDP_DOT3_LINK_AUTONEG_100BASE_T4,
399 "100Base-T4");
400 display_autoneg(w, autoneg_advertised,
401 LLDP_DOT3_LINK_AUTONEG_1000BASE_X,
402 LLDP_DOT3_LINK_AUTONEG_1000BASE_XFD,
403 "1000Base-X");
404 display_autoneg(w, autoneg_advertised,
405 LLDP_DOT3_LINK_AUTONEG_1000BASE_T,
406 LLDP_DOT3_LINK_AUTONEG_1000BASE_TFD,
407 "1000Base-T");
408 }
409 tag_datatag(w, "current", "MAU oper type",
410 lldpctl_atom_get_str(port, lldpctl_k_port_dot3_mautype));
411 tag_end(w);
412 }
413
414 lldpctl_atom_t *dot3_power = lldpctl_atom_get(port,
415 lldpctl_k_port_dot3_power);
416 int devicetype = lldpctl_atom_get_int(dot3_power,
417 lldpctl_k_dot3_power_devicetype);
418 if (devicetype > 0) {
419 tag_start(w, "power", "MDI Power");
420 tag_attr(w, "supported", "supported",
421 (lldpctl_atom_get_int(dot3_power,
422 lldpctl_k_dot3_power_supported) > 0)?"yes":"no");
423 tag_attr(w, "enabled", "enabled",
424 (lldpctl_atom_get_int(dot3_power,
425 lldpctl_k_dot3_power_enabled) > 0)?"yes":"no");
426 tag_attr(w, "paircontrol", "pair control",
427 (lldpctl_atom_get_int(dot3_power,
428 lldpctl_k_dot3_power_paircontrol) > 0)?"yes":"no");
429 tag_start(w, "device-type", "Device type");
430 tag_data(w, lldpctl_atom_get_str(dot3_power,
431 lldpctl_k_dot3_power_devicetype));;
432 tag_end(w);
433 tag_start(w, "pairs", "Power pairs");
434 tag_data(w, lldpctl_atom_get_str(dot3_power,
435 lldpctl_k_dot3_power_pairs));
436 tag_end(w);
437 tag_start(w, "class", "Class");
438 tag_data(w, lldpctl_atom_get_str(dot3_power,
439 lldpctl_k_dot3_power_class));
440 tag_end(w);
441
442 /* 802.3at */
443 if (lldpctl_atom_get_int(dot3_power,
444 lldpctl_k_dot3_power_type) > LLDP_DOT3_POWER_8023AT_OFF) {
445 tag_start(w, "power-type", "Power type");
446 tag_data(w, lldpctl_atom_get_str(dot3_power,
447 lldpctl_k_dot3_power_type));
448 tag_end(w);
449
450 tag_start(w, "source", "Power Source");
451 tag_data(w, lldpctl_atom_get_str(dot3_power,
452 lldpctl_k_dot3_power_source));
453 tag_end(w);
454
455 tag_start(w, "priority", "Power Priority");
456 tag_data(w, lldpctl_atom_get_str(dot3_power,
457 lldpctl_k_dot3_power_priority));
458 tag_end(w);
459
460 tag_start(w, "requested", "PD requested power Value");
461 tag_data(w, lldpctl_atom_get_str(dot3_power,
462 lldpctl_k_dot3_power_requested));
463 tag_end(w);
464
465 tag_start(w, "allocated", "PSE allocated power Value");
466 tag_data(w, lldpctl_atom_get_str(dot3_power,
467 lldpctl_k_dot3_power_allocated));
468 tag_end(w);
469 }
470
471 tag_end(w);
472 }
473 lldpctl_atom_dec_ref(dot3_power);
474 }
475
476 tag_end(w);
477 }
478
479 static void
480 display_vlans(struct writer *w, lldpctl_atom_t *port)
481 {
482 lldpctl_atom_t *vlans, *vlan;
483 int foundpvid = 0;
484 int pvid, vid;
485
486 pvid = lldpctl_atom_get_int(port,
487 lldpctl_k_port_vlan_pvid);
488
489 vlans = lldpctl_atom_get(port, lldpctl_k_port_vlans);
490 lldpctl_atom_foreach(vlans, vlan) {
491 vid = lldpctl_atom_get_int(vlan,
492 lldpctl_k_vlan_id);
493 if (pvid == vid)
494 foundpvid = 1;
495
496 tag_start(w, "vlan", "VLAN");
497 tag_attr(w, "vlan-id", "",
498 lldpctl_atom_get_str(vlan, lldpctl_k_vlan_id));
499 if (pvid == vid)
500 tag_attr(w, "pvid", "pvid", "yes");
501 tag_data(w, lldpctl_atom_get_str(vlan,
502 lldpctl_k_vlan_name));
503 tag_end(w);
504 }
505 lldpctl_atom_dec_ref(vlans);
506
507 if (!foundpvid && pvid > 0) {
508 tag_start(w, "vlan", "VLAN");
509 tag_attr(w, "vlan-id", "",
510 lldpctl_atom_get_str(port,
511 lldpctl_k_port_vlan_pvid));
512 tag_attr(w, "pvid", "pvid", "yes");
513 tag_end(w);
514 }
515 }
516
517 static void
518 display_ppvids(struct writer *w, lldpctl_atom_t *port)
519 {
520 lldpctl_atom_t *ppvids, *ppvid;
521 ppvids = lldpctl_atom_get(port, lldpctl_k_port_ppvids);
522 lldpctl_atom_foreach(ppvids, ppvid) {
523 int status = lldpctl_atom_get_int(ppvid,
524 lldpctl_k_ppvid_status);
525 tag_start(w, "ppvid", "PPVID");
526 if (lldpctl_atom_get_int(ppvid,
527 lldpctl_k_ppvid_id) > 0)
528 tag_attr(w, "value", "",
529 lldpctl_atom_get_str(ppvid,
530 lldpctl_k_ppvid_id));
531 tag_attr(w, "supported", "supported",
532 (status & LLDP_PPVID_CAP_SUPPORTED)?"yes":"no");
533 tag_attr(w, "enabled", "enabled",
534 (status & LLDP_PPVID_CAP_ENABLED)?"yes":"no");
535 tag_end(w);
536 }
537 lldpctl_atom_dec_ref(ppvids);
538 }
539
540 static void
541 display_pids(struct writer *w, lldpctl_atom_t *port)
542 {
543 lldpctl_atom_t *pids, *pid;
544 pids = lldpctl_atom_get(port, lldpctl_k_port_pis);
545 lldpctl_atom_foreach(pids, pid) {
546 const char *pi = lldpctl_atom_get_str(pid, lldpctl_k_pi_id);
547 if (pi && strlen(pi) > 0)
548 tag_datatag(w, "pi", "PI", pi);
549 }
550 lldpctl_atom_dec_ref(pids);
551 }
552
553 static const char*
554 display_age(time_t lastchange)
555 {
556 static char sage[30];
557 int age = (int)(time(NULL) - lastchange);
558 if (snprintf(sage, sizeof(sage),
559 "%d day%s, %02d:%02d:%02d",
560 age / (60*60*24),
561 (age / (60*60*24) > 1)?"s":"",
562 (age / (60*60)) % 24,
563 (age / 60) % 60,
564 age % 60) >= sizeof(sage))
565 return "too much";
566 else
567 return sage;
568 }
569
570 void
571 display_local_chassis(lldpctl_conn_t *conn, struct writer *w,
572 struct cmd_env *env, int details)
573 {
574 tag_start(w, "local-chassis", "Local chassis");
575
576 lldpctl_atom_t *chassis = lldpctl_get_local_chassis(conn);
577 display_chassis(w, chassis, details);
578 if (details == DISPLAY_DETAILS) {
579 display_med(w, NULL, chassis);
580 }
581 lldpctl_atom_dec_ref(chassis);
582
583 tag_end(w);
584 }
585
586 void
587 display_interface(lldpctl_conn_t *conn, struct writer *w, int hidden,
588 lldpctl_atom_t *iface, lldpctl_atom_t *neighbor, int details, int protocol)
589 {
590 if (!hidden &&
591 lldpctl_atom_get_int(neighbor, lldpctl_k_port_hidden))
592 return;
593
594 /* user might have specified protocol to filter on display */
595 if ((protocol != LLDPD_MODE_MAX) &&
596 (protocol != lldpctl_atom_get_int(neighbor, lldpctl_k_port_protocol)))
597 return;
598
599 lldpctl_atom_t *chassis = lldpctl_atom_get(neighbor, lldpctl_k_port_chassis);
600
601 tag_start(w, "interface", "Interface");
602 tag_attr(w, "name", "",
603 lldpctl_atom_get_str(iface, lldpctl_k_interface_name));
604 tag_attr(w, "via" , "via",
605 lldpctl_atom_get_str(neighbor, lldpctl_k_port_protocol));
606 if (details > DISPLAY_BRIEF) {
607 tag_attr(w, "rid" , "RID",
608 lldpctl_atom_get_str(chassis, lldpctl_k_chassis_index));
609 tag_attr(w, "age" , "Time",
610 display_age(lldpctl_atom_get_int(neighbor, lldpctl_k_port_age)));
611 }
612
613 display_chassis(w, chassis, details);
614 display_port(w, neighbor, details);
615 if (details == DISPLAY_DETAILS) {
616 display_vlans(w, neighbor);
617 display_ppvids(w, neighbor);
618 display_pids(w, neighbor);
619 display_med(w, neighbor, chassis);
620 }
621
622 lldpctl_atom_dec_ref(chassis);
623
624 display_custom_tlvs(w, neighbor);
625
626 tag_end(w);
627 }
628
629 /**
630 * Display information about interfaces.
631 *
632 * @param conn Connection to lldpd.
633 * @param w Writer.
634 * @param hidden Whatever to show hidden ports.
635 * @param env Environment from which we may find the list of ports.
636 * @param details Level of details we need (DISPLAY_*).
637 */
638 void
639 display_interfaces(lldpctl_conn_t *conn, struct writer *w,
640 struct cmd_env *env,
641 int hidden, int details)
642 {
643 lldpctl_atom_t *iface;
644 int protocol = LLDPD_MODE_MAX;
645 const char *proto_str;
646
647 /* user might have specified protocol to filter display results */
648 proto_str = cmdenv_get(env, "protocol");
649
650 if (proto_str) {
651 log_debug("display", "filter protocol: %s ", proto_str);
652
653 protocol = 0;
654 for (lldpctl_map_t *protocol_map =
655 lldpctl_key_get_map(lldpctl_k_port_protocol);
656 protocol_map->string;
657 protocol_map++) {
658 if (!strcasecmp(proto_str, protocol_map->string)) {
659 protocol = protocol_map->value;
660 break;
661 }
662 }
663 }
664
665 tag_start(w, "lldp", "LLDP neighbors");
666 while ((iface = cmd_iterate_on_interfaces(conn, env))) {
667 lldpctl_atom_t *port;
668 lldpctl_atom_t *neighbors;
669 lldpctl_atom_t *neighbor;
670 port = lldpctl_get_port(iface);
671 neighbors = lldpctl_atom_get(port, lldpctl_k_port_neighbors);
672 lldpctl_atom_foreach(neighbors, neighbor) {
673 display_interface(conn, w, hidden, iface, neighbor, details, protocol);
674 }
675 lldpctl_atom_dec_ref(neighbors);
676 lldpctl_atom_dec_ref(port);
677 }
678 tag_end(w);
679 }
680
681 void
682 display_stat(struct writer *w, const char *tag, const char *descr,
683 long unsigned int cnt)
684 {
685 char buf[20] = {};
686
687 tag_start(w, tag, descr);
688 snprintf(buf, sizeof(buf), "%lu", cnt);
689 tag_attr(w, tag, "", buf);
690 tag_end(w);
691 }
692
693 void
694 display_interface_stats(lldpctl_conn_t *conn, struct writer *w,
695 lldpctl_atom_t *port)
696 {
697 tag_start(w, "interface", "Interface");
698 tag_attr(w, "name", "",
699 lldpctl_atom_get_str(port, lldpctl_k_port_name));
700
701 display_stat(w, "tx", "Transmitted",
702 lldpctl_atom_get_int(port, lldpctl_k_tx_cnt));
703 display_stat(w, "rx", "Received",
704 lldpctl_atom_get_int(port, lldpctl_k_rx_cnt));
705
706 display_stat(w, "rx_discarded_cnt", "Discarded",
707 lldpctl_atom_get_int(port,
708 lldpctl_k_rx_discarded_cnt));
709
710 display_stat(w, "rx_unrecognized_cnt", "Unrecognized",
711 lldpctl_atom_get_int(port,
712 lldpctl_k_rx_unrecognized_cnt));
713
714 display_stat(w, "ageout_cnt", "Ageout",
715 lldpctl_atom_get_int(port,
716 lldpctl_k_ageout_cnt));
717
718 display_stat(w, "insert_cnt", "Inserted",
719 lldpctl_atom_get_int(port,
720 lldpctl_k_insert_cnt));
721
722 display_stat(w, "delete_cnt", "Deleted",
723 lldpctl_atom_get_int(port,
724 lldpctl_k_delete_cnt));
725
726 tag_end(w);
727 }
728
729 /**
730 * Display interface stats
731 *
732 * @param conn Connection to lldpd.
733 * @param w Writer.
734 * @param env Environment from which we may find the list of ports.
735 */
736 void
737 display_interfaces_stats(lldpctl_conn_t *conn, struct writer *w,
738 struct cmd_env *env)
739 {
740 lldpctl_atom_t *iface;
741 int summary = 0;
742 u_int64_t h_tx_cnt = 0;
743 u_int64_t h_rx_cnt = 0;
744 u_int64_t h_rx_discarded_cnt = 0;
745 u_int64_t h_rx_unrecognized_cnt = 0;
746 u_int64_t h_ageout_cnt = 0;
747 u_int64_t h_insert_cnt = 0;
748 u_int64_t h_delete_cnt = 0;
749
750 if (cmdenv_get(env, "summary"))
751 summary = 1;
752
753 tag_start(w, "lldp", (summary ? "LLDP Global statistics" :
754 "LLDP statistics"));
755 while ((iface = cmd_iterate_on_interfaces(conn, env))) {
756 lldpctl_atom_t *port;
757 port = lldpctl_get_port(iface);
758 if (!summary)
759 display_interface_stats(conn, w, port);
760 else {
761 h_tx_cnt += lldpctl_atom_get_int(port,
762 lldpctl_k_tx_cnt);
763 h_rx_cnt += lldpctl_atom_get_int(port,
764 lldpctl_k_rx_cnt);
765 h_rx_discarded_cnt += lldpctl_atom_get_int(port,
766 lldpctl_k_rx_discarded_cnt);
767 h_rx_unrecognized_cnt += lldpctl_atom_get_int(port,
768 lldpctl_k_rx_unrecognized_cnt);
769 h_ageout_cnt += lldpctl_atom_get_int(port,
770 lldpctl_k_ageout_cnt);
771 h_insert_cnt += lldpctl_atom_get_int(port,
772 lldpctl_k_insert_cnt);
773 h_delete_cnt += lldpctl_atom_get_int(port,
774 lldpctl_k_delete_cnt);
775 }
776 lldpctl_atom_dec_ref(port);
777 }
778
779 if (summary) {
780 tag_start(w, "summary", "Summary of stats");
781 display_stat(w, "tx", "Transmitted", h_tx_cnt);
782 display_stat(w, "rx", "Received", h_rx_cnt);
783 display_stat(w, "rx_discarded_cnt", "Discarded",
784 h_rx_discarded_cnt);
785
786 display_stat(w, "rx_unrecognized_cnt", "Unrecognized",
787 h_rx_unrecognized_cnt);
788
789 display_stat(w, "ageout_cnt", "Ageout", h_ageout_cnt);
790
791 display_stat(w, "insert_cnt", "Inserted", h_insert_cnt);
792
793 display_stat(w, "delete_cnt", "Deleted", h_delete_cnt);
794 tag_end(w);
795 }
796 tag_end(w);
797 }
798
799 static const char *
800 N(const char *str) {
801 if (str == NULL || strlen(str) == 0) return "(none)";
802 return str;
803 }
804
805 void
806 display_configuration(lldpctl_conn_t *conn, struct writer *w)
807 {
808 lldpctl_atom_t *configuration;
809
810 configuration = lldpctl_get_configuration(conn);
811 if (!configuration) {
812 log_warnx("lldpctl", "not able to get configuration. %s",
813 lldpctl_last_strerror(conn));
814 return;
815 }
816
817 tag_start(w, "configuration", "Global configuration");
818 tag_start(w, "config", "Configuration");
819
820 tag_datatag(w, "tx-delay", "Transmit delay",
821 lldpctl_atom_get_str(configuration, lldpctl_k_config_tx_interval));
822 tag_datatag(w, "tx-hold", "Transmit hold",
823 lldpctl_atom_get_str(configuration, lldpctl_k_config_tx_hold));
824 tag_datatag(w, "rx-only", "Receive mode",
825 lldpctl_atom_get_int(configuration, lldpctl_k_config_receiveonly)?
826 "yes":"no");
827 tag_datatag(w, "mgmt-pattern", "Pattern for management addresses",
828 N(lldpctl_atom_get_str(configuration, lldpctl_k_config_mgmt_pattern)));
829 tag_datatag(w, "iface-pattern", "Interface pattern",
830 N(lldpctl_atom_get_str(configuration, lldpctl_k_config_iface_pattern)));
831 tag_datatag(w, "cid-pattern", "Interface pattern for chassis ID",
832 N(lldpctl_atom_get_str(configuration, lldpctl_k_config_cid_pattern)));
833 tag_datatag(w, "description", "Override description with",
834 N(lldpctl_atom_get_str(configuration, lldpctl_k_config_description)));
835 tag_datatag(w, "platform", "Override platform with",
836 N(lldpctl_atom_get_str(configuration, lldpctl_k_config_platform)));
837 tag_datatag(w, "hostname", "Override system name with",
838 N(lldpctl_atom_get_str(configuration, lldpctl_k_config_hostname)));
839 tag_datatag(w, "advertise-version", "Advertise version",
840 lldpctl_atom_get_int(configuration, lldpctl_k_config_advertise_version)?
841 "yes":"no");
842 tag_datatag(w, "ifdescr-update", "Update interface descriptions",
843 lldpctl_atom_get_int(configuration, lldpctl_k_config_ifdescr_update)?
844 "yes":"no");
845 tag_datatag(w, "iface-promisc", "Promiscuous mode on managed interfaces",
846 lldpctl_atom_get_int(configuration, lldpctl_k_config_iface_promisc)?
847 "yes":"no");
848 tag_datatag(w, "lldpmed-no-inventory", "Disable LLDP-MED inventory",
849 (lldpctl_atom_get_int(configuration, lldpctl_k_config_lldpmed_noinventory) == 0)?
850 "no":"yes");
851 tag_datatag(w, "lldpmed-faststart", "LLDP-MED fast start mechanism",
852 (lldpctl_atom_get_int(configuration, lldpctl_k_config_fast_start_enabled) == 0)?
853 "no":"yes");
854 tag_datatag(w, "lldpmed-faststart-interval", "LLDP-MED fast start interval",
855 N(lldpctl_atom_get_str(configuration, lldpctl_k_config_fast_start_interval)));
856 tag_datatag(w, "bond-slave-src-mac-type",
857 "Source MAC for LLDP frames on bond slaves",
858 lldpctl_atom_get_str(configuration,
859 lldpctl_k_config_bond_slave_src_mac_type));
860 tag_datatag(w, "lldp-portid-type",
861 "Port ID TLV subtype for LLDP frames",
862 lldpctl_atom_get_str(configuration,
863 lldpctl_k_config_lldp_portid_type));
864 tag_datatag(w, "lldp-agent-type",
865 "Agent type",
866 lldpctl_atom_get_str(configuration,
867 lldpctl_k_config_lldp_agent_type));
868
869 tag_end(w);
870 tag_end(w);
871
872 lldpctl_atom_dec_ref(configuration);
873 }