4

I have a question related to the veth pair that is used in Linux system. I want to know which veth pairs are running in the current host, which I mean querying the pairs by using one Linux command or finding the related configuration in some files.

I know that to construct veth pair, you can simply by using

ip link add name1 type veth name2

But I haven't found an command or file that could be used to query the current running veth pairs.

If you know a way that could find the current running veth pairs, could you please tell me? This would help me a lot.

8 Answers 8

6

You can get peer ifindex with the following ethtool command.

# ethtool -S veth1
NIC statistics:
     peer_ifindex: 7

ifindex is shown with:

# ip link

Ref: http://www.spinics.net/lists/netdev/msg102062.html

1
  • 3
    For me it shows the peer ifindex right in "ip link" eg veth173321d@if82 so 82 is the linked address if I type "ethtool -S veth173321d". Mar 19, 2018 at 22:40
2

I have an older linux kernel without ip netns so I cobbled this together to get the needed info. Assuming you can ssh into your LXC containers, this might be of use to you. It's a quick hack 8)

MY_VMS="10.0.1.1" # etc... change as needed
for A_VM in $MY_VMS
do
  if [ ! -f $A_VM.list ]
  then
    ssh $A_VM ip link list > $A_VM.list
  fi

  for i in `ifconfig -a | grep "Link encap" | sed 's/ .*//g'`
  do
    PEER_IFINDEX=`ethtool -S $i 2>/dev/null | grep peer_ifindex | sed 's/ *peer_ifindex: *//g' `
    if [ "$PEER_IFINDEX" = "" ]
    then
      continue
    fi

    PEER_IF=`grep "^$PEER_IFINDEX:" $A_VM.list  | awk '{print $2}' | sed 's/:.*//g'`
    if [ "$PEER_IF" = "" ]
    then
      continue
    fi
    printf "%-10s is paired with %-10s on %-20s\n" $i $PEER_IF $A_VM
  done
done
1

With this simple script you can find the pairs (for OpenStack):

#!/bin/bash
for i in `ifconfig -a |grep qv |awk -F: '{print $1}'` ;
   do
      echo "---------------"
      echo $i
      echo "Our ID: " `ip link show dev $i | grep $i | awk -F: '{print $1}'`
      echo "Peer ID: " `ethtool -S $i |  grep -i peer_ifindex | awk -F: '{print $2}'`
      echo "---------------"
done

In OpenStack veth pairs - veth interface names starts with qv*.

1

I have just written a command to show the peer veth interface:

https://github.com/hariguchi/veth-peer

Example

$ sudo ip link add foo-bar type veth peer name bar-foo
[sudo] password for XXX:
$ veth-peer foor-bar
bar-foo
$ veth-peer xxx
xxx: Link not found
$ veth-peer lo
lo is not veth.
$ sudo ip link del foo-bar
           $ veth-peer bar-foo
           bar-foo: Link not found
$
1

to get all veth interfaces just use

# ip -c link show type veth

17: veth1@if16: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether 8e:61:92:d9:03:15 brd ff:ff:ff:ff:ff:ff link-netnsid 0
25: veth3@if24: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether 2a:a1:13:8c:9b:d2 brd ff:ff:ff:ff:ff:ff link-netnsid 0
27: veth5@if26: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether 3a:b6:fb:5e:5b:ce brd ff:ff:ff:ff:ff:ff link-netnsid 0
28: veth8@if29: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether de:30:40:26:04:20 brd ff:ff:ff:ff:ff:ff link-netnsid 1
30: veth11@veth10: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether de:bb:24:8c:40:00 brd ff:ff:ff:ff:ff:ff
31: veth10@veth11: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether 96:07:3e:b2:ac:1c brd ff:ff:ff:ff:ff:ff
0

At least on Ubuntu 20, to list interfaces, including veth pairs, just do:

ip link

This is assuming the veth pair was created with something like:

ip link add veth0 type veth peer name veth1

If you created namespaces and assigned the VETH devices to them, like in this example:

# ip netns add net1
# ip netns add net2
# ip link add veth1 netns net1 type veth peer name veth2 netns net2

then you'd view them like so:

sudo ip netns exec net1 ip link

In other words ip netns exec $namespace $command, where command is the same ip link above.

Bonus tip: to delete a veth pair, simply delete one of the interfaces:

sudo ip link delete veth1

And they will both be gone.

0

I have iproute2 version 5.5.0-1

On this "ip link" shows the peer name after "@" for example

ip link | grep veth 

78: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 
79: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
1
  • kubernetes based veths are not shown by your command.... so the type has to be there like ip -c link show type veth Mar 24 at 12:42
0

https://github.com/t1anz0ng/iftree

I made a tool for visualizing bridged veth pais,

output be like: Tree table

1
  • awesome -- this tool made me to understand the port binding in Canal ( calico + flannel ) in RKE2 kubernetes. awesome Mar 24 at 12:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .