15

So what I'm looking for is an interface index (or name) but the real one. if_nametoindex(3) and ioctl(2) don't seem to help me here. Now the only way I found how to do this is via /sys. Let me demonstrate on example what I need:

# cat /sys/class/net/bond0.1007/ifindex 
26
# cat /sys/class/net/bond0.1007/iflink
23                                  <-- I need either this or a reference to bond0
# cat /sys/class/net/bond0/ifindex 
23

Is there a way to do it with a function call or the only way is sysfs?

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
UVV
  • 2,928
  • 3
  • 23
  • 36
  • Look at what `ip link show` does (probably using the netlink api) – Stéphane Chazelas Aug 14 '14 at 14:01
  • See also `/proc/net/vlan/config` or `/proc/net/vlan/bond0.1007` if it's a vlan interface. – Stéphane Chazelas Aug 14 '14 at 14:02
  • @StéphaneChazelas Thanks. I decided to keep using `/sys`. `/proc` or `/sys` are more or less the same in that sense. I just wanted to check if there is a syscall to get this kind of info, it turns out there is not. – UVV Aug 15 '14 at 10:39
  • 1
    the syscalls would be the sendmsg/recvmsg using the netlink socket API. – Stéphane Chazelas Aug 15 '14 at 10:52
  • 1
    rtnetlink documentation: http://man7.org/linux/man-pages/man7/rtnetlink.7.html - issue `RTM_GETLINK` message and look for `IFLA_LINK` attribute in reply. – ecatmur Mar 23 '15 at 19:50
  • If you want to do using function call then check this link - https://stackoverflow.com/questions/14264371/how-to-get-nic-details-from-a-c-program – dexterous Aug 23 '19 at 03:48

1 Answers1

4

The command ip link show is probably what you want. It will display the index in front of the interface information. Example below from a system with bonded NICs.

# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 9214 qdisc mq master bond0 portid 000f53000000 state UP mode DEFAULT qlen 1000
    link/ether 00:0f:53:00:00:00 brd ff:ff:ff:ff:ff:ff
3: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 9214 qdisc mq master bond0 portid 000f53000001 state UP mode DEFAULT qlen 1000
    link/ether 00:0f:53:00:00:00 brd ff:ff:ff:ff:ff:ff
4: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 9214 qdisc noqueue state UP mode DEFAULT qlen 1000
    link/ether 00:0f:53:00:00:00 brd ff:ff:ff:ff:ff:ff
Boscoe
  • 236
  • 1
  • 5