Detecting that a network interface is “physical” or “virtual” using only RTNETLINK on Linux?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I would like to detect whether a network interface is "physical" or "virtual", where "physical" means that the interface has hardware attached, even if that "hardware" is virtualized by a hypervisor. "Virtual" then means that it's one of the Linux kernel elements, such as bridges, veths, macvlans, et cetera.



I'm aware that by using /sys/class/net/[nif]/... I can follow the device link and thus differentiate between (virtual) physical and "real" (sic!) virtual network interfaces, based on the device path containing the "virtual" sub directory or not. However, when dealing with network namespaces, this requires a properly remounted sysfs in order to be able to see the correct /sys/class/net/... entries. So a simpler method relying solely on RTNETLINK data might be more suitable.



When using RTNETLINK to discover the available network interfaces is there some property/attribute which allows me to differentiate without the need to check /sys/class/net/[nif]?







share|improve this question

























    up vote
    2
    down vote

    favorite












    I would like to detect whether a network interface is "physical" or "virtual", where "physical" means that the interface has hardware attached, even if that "hardware" is virtualized by a hypervisor. "Virtual" then means that it's one of the Linux kernel elements, such as bridges, veths, macvlans, et cetera.



    I'm aware that by using /sys/class/net/[nif]/... I can follow the device link and thus differentiate between (virtual) physical and "real" (sic!) virtual network interfaces, based on the device path containing the "virtual" sub directory or not. However, when dealing with network namespaces, this requires a properly remounted sysfs in order to be able to see the correct /sys/class/net/... entries. So a simpler method relying solely on RTNETLINK data might be more suitable.



    When using RTNETLINK to discover the available network interfaces is there some property/attribute which allows me to differentiate without the need to check /sys/class/net/[nif]?







    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I would like to detect whether a network interface is "physical" or "virtual", where "physical" means that the interface has hardware attached, even if that "hardware" is virtualized by a hypervisor. "Virtual" then means that it's one of the Linux kernel elements, such as bridges, veths, macvlans, et cetera.



      I'm aware that by using /sys/class/net/[nif]/... I can follow the device link and thus differentiate between (virtual) physical and "real" (sic!) virtual network interfaces, based on the device path containing the "virtual" sub directory or not. However, when dealing with network namespaces, this requires a properly remounted sysfs in order to be able to see the correct /sys/class/net/... entries. So a simpler method relying solely on RTNETLINK data might be more suitable.



      When using RTNETLINK to discover the available network interfaces is there some property/attribute which allows me to differentiate without the need to check /sys/class/net/[nif]?







      share|improve this question













      I would like to detect whether a network interface is "physical" or "virtual", where "physical" means that the interface has hardware attached, even if that "hardware" is virtualized by a hypervisor. "Virtual" then means that it's one of the Linux kernel elements, such as bridges, veths, macvlans, et cetera.



      I'm aware that by using /sys/class/net/[nif]/... I can follow the device link and thus differentiate between (virtual) physical and "real" (sic!) virtual network interfaces, based on the device path containing the "virtual" sub directory or not. However, when dealing with network namespaces, this requires a properly remounted sysfs in order to be able to see the correct /sys/class/net/... entries. So a simpler method relying solely on RTNETLINK data might be more suitable.



      When using RTNETLINK to discover the available network interfaces is there some property/attribute which allows me to differentiate without the need to check /sys/class/net/[nif]?









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jul 20 at 17:27
























      asked Jun 27 at 5:47









      TheDiveO

      22310




      22310




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          The only way seems to look at the IFLA_INFO_KIND attribute returned from a RTM_GETLINK. This is an optional attribute that's missing for physical devices, and also for the lo loopback device.



          When it's present, it contains a string such as veth, bridge, et cetera. Comparing this to a list of well-known virtual link types (see ip link help) then identifies virtual network interfaces, leaving all other network interfaces in the "physical network interface" ballpark.



          Not perfect, but works without having to consult /sys/class/net/[IFNAME], which isn't always be correctly present and needs sysfs (re)mounting, see also Identify if a network interface is a veth using /sys/class/net?






          share|improve this answer























            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452144%2fdetecting-that-a-network-interface-is-physical-or-virtual-using-only-rtnetli%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            The only way seems to look at the IFLA_INFO_KIND attribute returned from a RTM_GETLINK. This is an optional attribute that's missing for physical devices, and also for the lo loopback device.



            When it's present, it contains a string such as veth, bridge, et cetera. Comparing this to a list of well-known virtual link types (see ip link help) then identifies virtual network interfaces, leaving all other network interfaces in the "physical network interface" ballpark.



            Not perfect, but works without having to consult /sys/class/net/[IFNAME], which isn't always be correctly present and needs sysfs (re)mounting, see also Identify if a network interface is a veth using /sys/class/net?






            share|improve this answer



























              up vote
              0
              down vote



              accepted










              The only way seems to look at the IFLA_INFO_KIND attribute returned from a RTM_GETLINK. This is an optional attribute that's missing for physical devices, and also for the lo loopback device.



              When it's present, it contains a string such as veth, bridge, et cetera. Comparing this to a list of well-known virtual link types (see ip link help) then identifies virtual network interfaces, leaving all other network interfaces in the "physical network interface" ballpark.



              Not perfect, but works without having to consult /sys/class/net/[IFNAME], which isn't always be correctly present and needs sysfs (re)mounting, see also Identify if a network interface is a veth using /sys/class/net?






              share|improve this answer

























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                The only way seems to look at the IFLA_INFO_KIND attribute returned from a RTM_GETLINK. This is an optional attribute that's missing for physical devices, and also for the lo loopback device.



                When it's present, it contains a string such as veth, bridge, et cetera. Comparing this to a list of well-known virtual link types (see ip link help) then identifies virtual network interfaces, leaving all other network interfaces in the "physical network interface" ballpark.



                Not perfect, but works without having to consult /sys/class/net/[IFNAME], which isn't always be correctly present and needs sysfs (re)mounting, see also Identify if a network interface is a veth using /sys/class/net?






                share|improve this answer















                The only way seems to look at the IFLA_INFO_KIND attribute returned from a RTM_GETLINK. This is an optional attribute that's missing for physical devices, and also for the lo loopback device.



                When it's present, it contains a string such as veth, bridge, et cetera. Comparing this to a list of well-known virtual link types (see ip link help) then identifies virtual network interfaces, leaving all other network interfaces in the "physical network interface" ballpark.



                Not perfect, but works without having to consult /sys/class/net/[IFNAME], which isn't always be correctly present and needs sysfs (re)mounting, see also Identify if a network interface is a veth using /sys/class/net?







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Jul 20 at 17:31


























                answered Jun 27 at 18:55









                TheDiveO

                22310




                22310






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452144%2fdetecting-that-a-network-interface-is-physical-or-virtual-using-only-rtnetli%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?