]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
lldpcli: add display filter to show nbrs running specific protocols
authorSam Tannous <stannous@cumulusnetworks.com>
Tue, 8 Jul 2014 19:15:50 +0000 (15:15 -0400)
committerVincent Bernat <vincent@bernat.im>
Tue, 8 Jul 2014 20:43:05 +0000 (22:43 +0200)
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 <kanna@cumulusnetworks.com>
Signed-off-by: Sam Tannous <stannous@cumulusnetworks.com>
src/client/client.h
src/client/commands.c
src/client/display.c
src/client/show.c

index 600acc64fc1bd391905dca4682584b04627bebe8..4a97fd4e7a2e657560a7ef469ecca79efd14bef9 100644 (file)
@@ -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 *);
index d16259ce24b7b5ea06bdfcdb04f82c4d52ddb8db..e18c8f13d4c4218da27365f7f85ee8f95535cd31 100644 (file)
@@ -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");
+}
index 85defbb0bb09e080d73896595df00a4ee29382c7..9d0cc77d5430f8613ac4036108a7c760d31c3ee2 100644 (file)
@@ -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);
index 420b88f2be5932b5a6211dae0ed89ba41f94a528..1959dd77c1114f9ccd08f5af85f5141f01a58868 100644 (file)
@@ -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);
 }
 
 /**