diff mbox series

[ovs-dev] ovs-tcpdump: Fallback to read /proc/net/dev on Linux

Message ID cd8facbfcabf14083a0761549f0adff72c8d591a.1578934023.git.tredaelli@redhat.com
State Accepted
Commit 02707cd45c5fdc2e871b1893b84cb08feb33ee1a
Headers show
Series [ovs-dev] ovs-tcpdump: Fallback to read /proc/net/dev on Linux | expand

Commit Message

Timothy Redaelli Jan. 13, 2020, 4:47 p.m. UTC
Currently, ovs-tcpdump uses python3-netifaces in order to get the list of
the network interfaces, but python3-netifaces may not be installed nor
available on some distributions (for example on RHEL7).

This commit adds, only for Linux, an alternative way (that is only used
when netifaces is not available) to read the list of the network interfaces
by reading "/proc/net/dev".

Signed-off-by: Timothy Redaelli <tredaelli@redhat.com>
---
 utilities/ovs-tcpdump.in | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

Comments

Ben Pfaff Jan. 13, 2020, 5:42 p.m. UTC | #1
On Mon, Jan 13, 2020 at 05:47:03PM +0100, Timothy Redaelli wrote:
> Currently, ovs-tcpdump uses python3-netifaces in order to get the list of
> the network interfaces, but python3-netifaces may not be installed nor
> available on some distributions (for example on RHEL7).
> 
> This commit adds, only for Linux, an alternative way (that is only used
> when netifaces is not available) to read the list of the network interfaces
> by reading "/proc/net/dev".
> 
> Signed-off-by: Timothy Redaelli <tredaelli@redhat.com>

Good work.  I applied this to master.
diff mbox series

Patch

diff --git a/utilities/ovs-tcpdump.in b/utilities/ovs-tcpdump.in
index 0fa5dc418..5ec02383c 100755
--- a/utilities/ovs-tcpdump.in
+++ b/utilities/ovs-tcpdump.in
@@ -24,7 +24,21 @@  import subprocess
 import sys
 import time
 
-import netifaces
+try:
+    from netifaces import interfaces
+except ImportError:
+    if sys.platform in ['linux', 'linux2']:
+        def interfaces():
+            devices = []
+            with open("/proc/net/dev", "r") as f_netdev:
+                for line in f_netdev:
+                    if ":" not in line:
+                        continue
+                    devices.append(line.split(":")[0].strip())
+            return devices
+    else:
+        print("ERROR: Please install netifaces Python library.")
+        sys.exit(1)
 
 try:
     from ovs.db import idl
@@ -438,11 +452,11 @@  def main():
             mirror_interface = _make_mirror_name[sys.platform](interface)
 
     if sys.platform in _make_taps and \
-       mirror_interface not in netifaces.interfaces():
+       mirror_interface not in interfaces():
         _make_taps[sys.platform](mirror_interface,
                                  ovsdb.interface_mtu(interface))
 
-    if mirror_interface not in netifaces.interfaces():
+    if mirror_interface not in interfaces():
         print("ERROR: Please create an interface called `%s`" %
               mirror_interface)
         print("See your OS guide for how to do this.")