@@ -1836,6 +1836,52 @@ TEST(NetlinkRouteTest, LookupAllAddrOrder) {
18361836 }
18371837}
18381838
1839+ TEST (NetlinkRouteTest, LinIndexOrder) {
1840+ // This test verifies that links are sorted by index in ascending order.
1841+ SKIP_IF (!ASSERT_NO_ERRNO_AND_VALUE (HaveCapability (CAP_NET_ADMIN)));
1842+ SKIP_IF (IsRunningWithHostinet ());
1843+
1844+ const FileDescriptor curr_nsfd =
1845+ ASSERT_NO_ERRNO_AND_VALUE (Open (" /proc/thread-self/ns/net" , O_RDONLY));
1846+ Cleanup restore_netns = Cleanup ([&] {
1847+ ASSERT_THAT (setns (curr_nsfd.get (), CLONE_NEWNET),
1848+ SyscallSucceedsWithValue (0 ));
1849+ });
1850+ ASSERT_THAT (unshare (CLONE_NEWNET), SyscallSucceedsWithValue (0 ));
1851+
1852+ // Add some interfaces
1853+ FileDescriptor fd =
1854+ ASSERT_NO_ERRNO_AND_VALUE (NetlinkBoundSocket (NETLINK_ROUTE));
1855+ VethRequest req3 = GetVethRequest (kSeq , " veth3a" , " veth3b" );
1856+ ASSERT_NO_ERRNO (
1857+ NetlinkRequestAckOrError (fd, kSeq , &req3, req3.hdr .nlmsg_len ));
1858+ VethRequest req1 = GetVethRequest (kSeq , " veth1a" , " veth1b" );
1859+ ASSERT_NO_ERRNO (
1860+ NetlinkRequestAckOrError (fd, kSeq , &req1, req1.hdr .nlmsg_len ));
1861+ VethRequest req2 = GetVethRequest (kSeq , " veth2a" , " veth2b" );
1862+ ASSERT_NO_ERRNO (
1863+ NetlinkRequestAckOrError (fd, kSeq , &req2, req2.hdr .nlmsg_len ));
1864+
1865+ // Dump links and verify they come back in ascending order by index.
1866+ std::vector<int > found_indexes;
1867+ ASSERT_NO_ERRNO (DumpLinks (fd, kSeq , [&](const struct nlmsghdr * hdr) {
1868+ if (hdr->nlmsg_type != RTM_NEWLINK) {
1869+ return ;
1870+ }
1871+ if (hdr->nlmsg_len < NLMSG_SPACE (sizeof (struct ifinfomsg ))) {
1872+ return ;
1873+ }
1874+ const struct ifinfomsg * msg =
1875+ reinterpret_cast <const struct ifinfomsg *>(NLMSG_DATA (hdr));
1876+ found_indexes.push_back (msg->ifi_index );
1877+ }));
1878+
1879+ ASSERT_GE (found_indexes.size (), 6 )
1880+ << " Expected at least 3 veth pairs to be found" ;
1881+ ASSERT_TRUE (std::is_sorted (found_indexes.begin (), found_indexes.end ()))
1882+ << " Link indexes are not in ascending order" ;
1883+ }
1884+
18391885struct NetNSRequest {
18401886 struct nlmsghdr hdr;
18411887 struct ifinfomsg ifm;
0 commit comments