From: Sam Tannous Date: Tue, 8 Jul 2014 19:15:50 +0000 (-0400) Subject: lldpcli: add display filter to show nbrs running specific protocols X-Git-Tag: 0.7.10~17 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=494264f0f831d82332d444faa501de26443476e6;p=thirdparty%2Flldpd.git lldpcli: add display filter to show nbrs running specific protocols LLDPD allows a peer to run multiple protocols (LLDP, CDPv1, CDPv2). lldpcli can be used to show the primary protocol for a nbr or all the protocols using the "hidden" cmdline arg This allows lldpcli to filter the nbr display based on the protocol that is running on it. Signed-off-by: Kanna Rajagopal Signed-off-by: Sam Tannous --- diff --git a/src/client/client.h b/src/client/client.h index 600acc64..4a97fd4e 100644 --- a/src/client/client.h +++ b/src/client/client.h @@ -97,6 +97,7 @@ int cmd_store_something_env_value_and_pop2(const char *, struct cmd_env *, lldpctl_atom_t* cmd_iterate_on_interfaces(struct lldpctl_conn_t *, struct cmd_env *); void cmd_restrict_ports(struct cmd_node *); +void cmd_restrict_protocol(struct cmd_node *); /* misc.c */ int contains(const char *, const char *); @@ -109,7 +110,7 @@ char* totag(const char *); void display_interfaces(lldpctl_conn_t *, struct writer *, struct cmd_env *, int, int); void display_interface(lldpctl_conn_t *, struct writer *, int, - lldpctl_atom_t *, lldpctl_atom_t *, int); + lldpctl_atom_t *, lldpctl_atom_t *, int, int); void display_configuration(lldpctl_conn_t *, struct writer *); void display_interfaces_stats(lldpctl_conn_t *, struct writer *, struct cmd_env *); diff --git a/src/client/commands.c b/src/client/commands.c index d16259ce..e18c8f13 100644 --- a/src/client/commands.c +++ b/src/client/commands.c @@ -676,3 +676,20 @@ cmd_restrict_ports(struct cmd_node *root) "Restrict configuration to the specified ports (comma-separated list)", NULL, cmd_store_env_value_and_pop2, "ports"); } + +/** + * Restrict the command to specific protocol + */ +void +cmd_restrict_protocol(struct cmd_node *root) +{ + /* Restrict to some ports. */ + commands_new( + commands_new(root, + "protocol", + "Restrict to specific protocol", + cmd_check_no_env, NULL, "protocol"), + NULL, + "Restrict display to the specified protocol", + NULL, cmd_store_env_value_and_pop2, "protocol"); +} diff --git a/src/client/display.c b/src/client/display.c index 85defbb0..9d0cc77d 100644 --- a/src/client/display.c +++ b/src/client/display.c @@ -504,12 +504,17 @@ display_age(time_t lastchange) void display_interface(lldpctl_conn_t *conn, struct writer *w, int hidden, - lldpctl_atom_t *iface, lldpctl_atom_t *neighbor, int details) + lldpctl_atom_t *iface, lldpctl_atom_t *neighbor, int details, int protocol) { if (!hidden && lldpctl_atom_get_int(neighbor, lldpctl_k_port_hidden)) return; + /* user might have specified protocol to filter on display */ + if ((protocol != LLDPD_MODE_MAX) && + (protocol != lldpctl_atom_get_int(neighbor, lldpctl_k_port_protocol))) + return; + tag_start(w, "interface", "Interface"); tag_attr(w, "name", "", lldpctl_atom_get_str(iface, lldpctl_k_interface_name)); @@ -549,6 +554,26 @@ display_interfaces(lldpctl_conn_t *conn, struct writer *w, int hidden, int details) { lldpctl_atom_t *iface; + int protocol = LLDPD_MODE_MAX; + char *proto_str; + + /* user might have specified protocol to filter display results */ + proto_str = cmdenv_get(env, "protocol"); + + if (proto_str) { + log_debug("display", "filter protocol: %s ", proto_str); + + if (!strcmp(proto_str, "cdpv1")) { + protocol = LLDPD_MODE_CDPV1; + } else if (!strcmp(proto_str, "cdpv2")) { + protocol = LLDPD_MODE_CDPV2; + } else if (!strcmp(proto_str, "lldp")) { + protocol = LLDPD_MODE_LLDP; + } else { + /* unsupported - dont show anything */ + protocol = 0; + } + } tag_start(w, "lldp", "LLDP neighbors"); while ((iface = cmd_iterate_on_interfaces(conn, env))) { @@ -558,7 +583,7 @@ display_interfaces(lldpctl_conn_t *conn, struct writer *w, port = lldpctl_get_port(iface); neighbors = lldpctl_atom_get(port, lldpctl_k_port_neighbors); lldpctl_atom_foreach(neighbors, neighbor) { - display_interface(conn, w, hidden, iface, neighbor, details); + display_interface(conn, w, hidden, iface, neighbor, details, protocol); } lldpctl_atom_dec_ref(neighbors); lldpctl_atom_dec_ref(port); diff --git a/src/client/show.c b/src/client/show.c index 420b88f2..1959dd77 100644 --- a/src/client/show.c +++ b/src/client/show.c @@ -108,10 +108,30 @@ watchcb(lldpctl_conn_t *conn, struct cmd_env *env = wa->env; struct writer *w = wa->w; const char *interfaces = cmdenv_get(env, "ports"); + char *proto_str; + int protocol = LLDPD_MODE_MAX; + if (interfaces && !contains(interfaces, lldpctl_atom_get_str(interface, lldpctl_k_interface_name))) return; + /* user might have specified protocol to filter display results */ + proto_str = cmdenv_get(env, "protocol"); + + if (proto_str) { + log_debug("display", "filter protocol: %s ", proto_str); + + if (!strcmp(proto_str, "cdpv1")) + protocol = LLDPD_MODE_CDPV1; + else if (!strcmp(proto_str, "cdpv2")) + protocol = LLDPD_MODE_CDPV2; + else if (!strcmp(proto_str, "lldp")) + protocol = LLDPD_MODE_LLDP; + else + /* unsupported - dont show anything */ + protocol = 0; + } + switch (type) { case lldpctl_c_deleted: tag_start(w, "lldp-deleted", "LLDP neighbor deleted"); @@ -127,7 +147,7 @@ watchcb(lldpctl_conn_t *conn, display_interface(conn, w, 1, interface, neighbor, cmdenv_get(env, "summary")?DISPLAY_BRIEF: cmdenv_get(env, "detailed")?DISPLAY_DETAILS: - DISPLAY_NORMAL); + DISPLAY_NORMAL, protocol); tag_end(w); } @@ -185,6 +205,9 @@ register_common_commands(struct cmd_node *root) /* Some specific port */ cmd_restrict_ports(root); + + /* Specific protocol */ + cmd_restrict_protocol(root); } /**