Skip to contentSkip to conversation

net: Add "info neighbors" command

PATCH In review Doug Evans 4 participants 6 messages Sep 2, 2021 – Sep 16, 2021 Networking

lore
Share & tools
lore

Doug Evans

author
#0 lore raw

PATCH 1/1 net: Add "info neighbors" command

This command dumps the ARP and NDP tables maintained within slirp. One use-case for it is showing the guest's IPv6 address(es).

---
 hmp-commands-info.hx               | 15 +++++++
 include/net/slirp.h                |  1 +
 net/slirp.c                        | 15 +++++++
 tests/acceptance/info_neighbors.py | 69 ++++++++++++++++++++++++++++++
 4 files changed, 100 insertions(+)
 create mode 100644 tests/acceptance/info_neighbors.py
hmp-commands-info.hx +15 -0
index 27206ac049..386f09f163 100644
@@ -512,6 +512,21 @@ SRST
     Show user network stack connection states.
 ERST
 
+#if defined(CONFIG_SLIRP)
+    {
+        .name       = "neighbors",
+        .args_type  = "",
+        .params     = "",
+        .help       = "show the ARP and NDP tables",
+        .cmd        = hmp_info_neighbors,
+    },
+#endif
+
+SRST
+  ``info neighbors``
+    Show the ARP and NDP tables.
+ERST
+
     {
         .name       = "migrate",
         .args_type  = "",
include/net/slirp.h +1 -0
index bad3e1e241..b9ccfda1e7 100644
@@ -31,6 +31,7 @@ void hmp_hostfwd_add(Monitor *mon, const QDict *qdict);
 void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict);
 
 void hmp_info_usernet(Monitor *mon, const QDict *qdict);
+void hmp_info_neighbors(Monitor *mon, const QDict *qdict);
 
 #endif
 
net/slirp.c +15 -0
index ad3a838e0b..29a4cd3fe1 100644
@@ -1028,6 +1028,21 @@ void hmp_info_usernet(Monitor *mon, const QDict *qdict)
     }
 }
 
+void hmp_info_neighbors(Monitor *mon, const QDict *qdict)
+{
+    SlirpState *s;
+
+    QTAILQ_FOREACH(s, &slirp_stacks, entry) {
+        int id;
+        bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0;
+        char *info = slirp_neighbor_info(s->slirp);
+        monitor_printf(mon, "Hub %d (%s):\n%s",
+                       got_hub_id ? id : -1,
+                       s->nc.name, info);
+        g_free(info);
+    }
+}
+
 static void
 net_init_slirp_configs(const StringList *fwd, int flags)
 {
tests/acceptance/info_neighbors.py new file +69 -0

69 lines not on this page · raw message

Signature · 1 line
2.33.0.153.gba50c8fa24-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Doug Evans

This command dumps the ARP and NDP tables maintained within slirp. One use-case for it is showing the guest's IPv6 address(es).

Quoted 154 lines
---
 hmp-commands-info.hx               | 15 +++++++
 include/net/slirp.h                |  1 +
 net/slirp.c                        | 15 +++++++
 tests/acceptance/info_neighbors.py | 69 ++++++++++++++++++++++++++++++
 4 files changed, 100 insertions(+)
 create mode 100644 tests/acceptance/info_neighbors.py
hmp-commands-info.hx +15 -0
index 27206ac049..386f09f163 100644
@@ -512,6 +512,21 @@ SRST
     Show user network stack connection states.
 ERST
 
+#if defined(CONFIG_SLIRP)
+    {
+        .name       = "neighbors",
+        .args_type  = "",
+        .params     = "",
+        .help       = "show the ARP and NDP tables",
+        .cmd        = hmp_info_neighbors,
+    },
+#endif
+
+SRST
+  ``info neighbors``
+    Show the ARP and NDP tables.
+ERST
+
     {
         .name       = "migrate",
         .args_type  = "",
include/net/slirp.h +1 -0
index bad3e1e241..b9ccfda1e7 100644
@@ -31,6 +31,7 @@ void hmp_hostfwd_add(Monitor *mon, const QDict *qdict);
 void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict);
 
 void hmp_info_usernet(Monitor *mon, const QDict *qdict);
+void hmp_info_neighbors(Monitor *mon, const QDict *qdict);
 
 #endif
 
net/slirp.c +15 -0
index ad3a838e0b..29a4cd3fe1 100644
@@ -1028,6 +1028,21 @@ void hmp_info_usernet(Monitor *mon, const QDict
 *qdict)
     }
 }
 
+void hmp_info_neighbors(Monitor *mon, const QDict *qdict)
+{
+    SlirpState *s;
+
+    QTAILQ_FOREACH(s, &slirp_stacks, entry) {
+        int id;
+        bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0;
+        char *info = slirp_neighbor_info(s->slirp);
+        monitor_printf(mon, "Hub %d (%s):\n%s",
+                       got_hub_id ? id : -1,
+                       s->nc.name, info);
+        g_free(info);
+    }
+}
+
 static void
 net_init_slirp_configs(const StringList *fwd, int flags)

{

tests/acceptance/info_neighbors.py new file +69 -1
new file mode 100644
index 0000000000..ff79ec3ff3
@@ -0,0 +1,69 @@
+# Test for the hmp command "info neighbors"
+#
+# Copyright 2021 Google LLC
+#
+# This work is licensed under the terms of the GNU GPL, version 2 or
+# later.  See the COPYING file in the top-level directory.
+
+import re
+
+from avocado_qemu import LinuxTest
+from avocado_qemu import Test
+
+VNET_HUB_HEADER = 'Hub -1 (vnet):'
+NEIGHBOR_HEADER_REGEX = '^ *Table *MacAddr *IP Address$'
+
+def trim(text):
+    return " ".join(text.split())
+
+def hmc(test, cmd):
+    return test.vm.command('human-monitor-command', command_line=cmd)
+
+def get_neighbors(test):
+    output = hmc(test, 'info neighbors').splitlines()
+    if len(output) < 2:
+        test.fail("Insufficient output from 'info neighbors'")
+    test.assertEquals(output[0], VNET_HUB_HEADER)
+    test.assertTrue(re.fullmatch(NEIGHBOR_HEADER_REGEX, output[1]))
+    return output[2:]
+
+class InfoNeighborsNone(Test):
+
+    def test_no_neighbors(self):
+        self.vm.add_args('-nodefaults',
+                         '-netdev', 'user,id=vnet',
+                         '-device', 'virtio-net,netdev=vnet')
+        self.vm.launch()
+        neighbors = get_neighbors(self)
+        self.assertEquals(len(neighbors), 0)
+
+class InfoNeighbors(LinuxTest):
+
+    def test_neighbors(self):
+        """
+        :avocado: tags=arch:x86_64
+        :avocado: tags=machine:pc
+        :avocado: tags=accel:kvm
+        """
+        self.require_accelerator('kvm')
+        self.vm.add_args("-accel", "kvm")
+        self.vm.add_args('-nographic',
+                         '-m', '1024')
+        self.launch_and_wait()
+
+        # Ensure there's some packets to the guest and back.
+        self.ssh_command('pwd')
+
+        # We should now be aware of the guest as a neighbor.
+        expected_ipv4_neighbor = 'ARP 52:54:00:12:34:56 10.0.2.15'
+        # The default ipv6 net is fec0. Both fe80 and fec0 can appear.
+        expected_ipv6_neighbors = [
+            'NDP 52:54:00:12:34:56 fe80::5054:ff:fe12:3456',
+            'NDP 52:54:00:12:34:56 fec0::5054:ff:fe12:3456'
+        ]
+        neighbors = get_neighbors(self)
+        self.assertTrue(len(neighbors) >= 2 and len(neighbors) <= 3)
+        # IPv4 is output first.
+        self.assertEquals(trim(neighbors[0]), expected_ipv4_neighbor)
+        for neighbor in neighbors[1:]:
+            self.assertTrue(trim(neighbor) in expected_ipv6_neighbors)
--

2.33.0.153.gba50c8fa24-goog

#2 lore raw
In reply to Doug Evans
Quoted 11 lines · Doug Evans

This command dumps the ARP and NDP tables maintained within slirp. One use-case for it is showing the guest's IPv6 address(es).

---
 hmp-commands-info.hx               | 15 +++++++
 include/net/slirp.h                |  1 +
 net/slirp.c                        | 15 +++++++
 tests/acceptance/info_neighbors.py | 69 ++++++++++++++++++++++++++++++
 4 files changed, 100 insertions(+)
 create mode 100644 tests/acceptance/info_neighbors.py

Standard request for new HMP commands without corresponding QMP
commands: please state in the commit message why the QMP command is not worthwhile.

HMP commands without a QMP equivalent are okay if their functionality makes no sense in QMP, or is of use only for human users.

Example for "makes no sense in QMP": setting the current CPU, because a QMP monitor doesn't have a current CPU.

Examples for "is of use only for human users": HMP command "help", the integrated pocket calculator.

Debugging commands are kind of borderline. Debugging is commonly a human activity, where HMP is just fine. However, humans create tools to assist with their activities, and then QMP is useful. While I wouldn't encourage HMP-only for the debugging use case, I wouldn't veto it.

author
#3 lore raw
Quoted 24 lines · Markus Armbruster
Quoted 11 lines · Doug Evans

This command dumps the ARP and NDP tables maintained within slirp. One use-case for it is showing the guest's IPv6 address(es).

---
 hmp-commands-info.hx               | 15 +++++++
 include/net/slirp.h                |  1 +
 net/slirp.c                        | 15 +++++++
 tests/acceptance/info_neighbors.py | 69 ++++++++++++++++++++++++++++++
 4 files changed, 100 insertions(+)
 create mode 100644 tests/acceptance/info_neighbors.py

Standard request for new HMP commands without corresponding QMP
commands: please state in the commit message why the QMP command is not worthwhile.

HMP commands without a QMP equivalent are okay if their functionality makes no sense in QMP, or is of use only for human users.

Example for "makes no sense in QMP": setting the current CPU, because a QMP monitor doesn't have a current CPU.

Quoted 8 lines

Examples for "is of use only for human users": HMP command "help", the integrated pocket calculator.

Debugging commands are kind of borderline. Debugging is commonly a human activity, where HMP is just fine. However, humans create tools to assist with their activities, and then QMP is useful. While I wouldn't encourage HMP-only for the debugging use case, I wouldn't veto it.

Mostly I was following what I saw for "info usernet".
I don't see a difference between "info neighbors" and "info usernet" so I went with that.
Both draw their data from libslirp.

I'm happy to add QMP support if necessary.
Note that there is code that parses "info usernet" output, e.g., get_info_usernet_hostfwd_port for python.

Presumably we don't want to print text in slirp only to parse it in qemu, right?
That'll change the qemu/slirp interface.
OTOH, to what extent does libslirp want to export a more formal API for this, vs just text?

#4 lore raw
Quoted 36 lines · Doug Evans
Quoted 24 lines · Markus Armbruster
Quoted 11 lines · Doug Evans

This command dumps the ARP and NDP tables maintained within slirp. One use-case for it is showing the guest's IPv6 address(es).

---
 hmp-commands-info.hx               | 15 +++++++
 include/net/slirp.h                |  1 +
 net/slirp.c                        | 15 +++++++
 tests/acceptance/info_neighbors.py | 69 ++++++++++++++++++++++++++++++
 4 files changed, 100 insertions(+)
 create mode 100644 tests/acceptance/info_neighbors.py

Standard request for new HMP commands without corresponding QMP
commands: please state in the commit message why the QMP command is not worthwhile.

HMP commands without a QMP equivalent are okay if their functionality makes no sense in QMP, or is of use only for human users.

Example for "makes no sense in QMP": setting the current CPU, because a QMP monitor doesn't have a current CPU.

Quoted 8 lines

Examples for "is of use only for human users": HMP command "help", the integrated pocket calculator.

Debugging commands are kind of borderline. Debugging is commonly a human activity, where HMP is just fine. However, humans create tools to assist with their activities, and then QMP is useful. While I wouldn't encourage HMP-only for the debugging use case, I wouldn't veto it.

Quoted 4 lines

Mostly I was following what I saw for "info usernet".
I don't see a difference between "info neighbors" and "info usernet" so I went with that.
Both draw their data from libslirp.

I see.

Quoted 3 lines

I'm happy to add QMP support if necessary.
Note that there is code that parses "info usernet" output, e.g., get_info_usernet_hostfwd_port for python.

Demonstrates "is of use only for human users" is wrong.

Quoted 2 lines

Presumably we don't want to print text in slirp only to parse it in qemu, right?

Yes, we'd prefer not to parse.

As long as libslirp can only give us text, we need to parse it somewhere.

We can parse it right in QEMU, or punt the job to whatever uses QEMU. The latter can get away with parsing just the part they need. But we may end up with multiple parsers.

Quoted 3 lines

That'll change the qemu/slirp interface.
OTOH, to what extent does libslirp want to export a more formal API for this, vs just text?

This is a question for Samuel or Marc-André.

If the answer is "no", then HMP only (so we don't have to parse in QEMU) is fair, I think. The commit message should explain this.

#5 lore raw

Hi

Quoted 66 lines · Markus Armbruster
Quoted 1 line · Doug Evans

On Fri, Sep 3, 2021 at 6:08 AM Markus Armbruster <[email protected]>

Quoted 6 lines · wrote:
Quoted 6 lines
Quoted 3 lines · Doug Evans

This command dumps the ARP and NDP tables maintained within slirp. One use-case for it is showing the guest's IPv6 address(es).

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
---
 hmp-commands-info.hx               | 15 +++++++
 include/net/slirp.h                |  1 +
 net/slirp.c                        | 15 +++++++
 tests/acceptance/info_neighbors.py | 69

++++++++++++++++++++++++++++++

Quoted 23 lines
Quoted 13 lines
Quoted 2 lines
 4 files changed, 100 insertions(+)
 create mode 100644 tests/acceptance/info_neighbors.py

Standard request for new HMP commands without corresponding QMP
commands: please state in the commit message why the QMP command is not worthwhile.

HMP commands without a QMP equivalent are okay if their functionality makes no sense in QMP, or is of use only for human users.

Example for "makes no sense in QMP": setting the current CPU, because a QMP monitor doesn't have a current CPU.

Quoted 8 lines

Examples for "is of use only for human users": HMP command "help", the integrated pocket calculator.

Debugging commands are kind of borderline. Debugging is commonly a human activity, where HMP is just fine. However, humans create tools to assist with their activities, and then QMP is useful. While I wouldn't encourage HMP-only for the debugging use case, I wouldn't veto it.

Quoted 4 lines

Mostly I was following what I saw for "info usernet".
I don't see a difference between "info neighbors" and "info usernet" so I went with that.
Both draw their data from libslirp.

I see.

Quoted 3 lines

I'm happy to add QMP support if necessary.
Note that there is code that parses "info usernet" output, e.g., get_info_usernet_hostfwd_port for python.

Demonstrates "is of use only for human users" is wrong.

Quoted 2 lines

Presumably we don't want to print text in slirp only to parse it in qemu, right?

Yes, we'd prefer not to parse.

As long as libslirp can only give us text, we need to parse it somewhere.

We can parse it right in QEMU, or punt the job to whatever uses QEMU. The latter can get away with parsing just the part they need. But we may end up with multiple parsers.

Quoted 10 lines
Quoted 3 lines

That'll change the qemu/slirp interface.
OTOH, to what extent does libslirp want to export a more formal API for this, vs just text?

This is a question for Samuel or Marc-André.

If the answer is "no", then HMP only (so we don't have to parse in QEMU) is fair, I think. The commit message should explain this.

Until now, libslirp (& QEMU HMP) provided inner state details as textual form. But since there is a need for a programming-friendly API, it could be added to libslirp and exported as QAPI/QMP by QEMU. Let's try to design the API in a way that can be easily extended (via getters and/or keys etc). Alternatively, libslirp could dump its state as JSON, but this wouldn't fit so nicely with QMP machinery/introspection etc.