在Linux中,什么度量有没有度量的路由?

在Linux中,什么度量有没有度量的路由?

如果你(在 Linux 中)有这两条路线:

default via 192.168.1.1 dev enp58s0f1
default via 192.168.16.1 dev wlp59s0  proto static  metric 600

我希望使用第一个,但事实并非如此:而是使用第二个。

如果我把它改成这样:

default via 192.168.1.1 dev enp58s0f1  proto static  metric 100 
default via 192.168.16.1 dev wlp59s0  proto static  metric 600

然后它就会按预期工作。似乎“无度量”比任何数字都更糟糕(更高),而不是度量 0。

这是怎么回事?它是特定于 Linux 的还是网络标准?

提前致谢。

答案1

您确定您的第一个观察结果吗?ip route show那么显示什么route -n?如果proto static在第一种情况下添加,结果会改变吗?

我发现至少有两个资源明确指出这0是 Linux 中的默认值:

肯定需要 Linux 内核黑客来解决这个问题。

此外,无论选择什么默认值,显然都是特定于操作系统的。本文 (https://support.microsoft.com/en-us/help/299540/an-explanation-of-the-automatic-metric-feature-for-ipv4-routes)例如,显示 Windows 根据链路带宽选择默认指标。

答案2

由于这些路由位于不同的子网上,因此这里涉及的不仅仅是度量标准。例如,如果原始流量位于 192.168.1.1 子网上,路由表中有一条匹配的非默认路由,那么该路由将通过以下方式匹配最长前缀匹配在考虑指标之前。

假设非默认路由不匹配,则没有度量应该被内核解释为度量为 0,因此是最高优先级的路由。尽管这是一个简单化的视图,因为某些路由守护程序稍后会将默认度量转换为另一个值,例如 1024。我希望这就是您和您的未命名发行版所发生的情况。

如果ip route根本没有显示任何指标,您可以使用route -nnet-tools 包或cat /proc/net/route.但是,此输出不一定与路由守护程序遇到 0 度量值时在内部使用的输出相匹配。

此外,如何创建路线也很重要。ip route使用netlink API,同时route使用ioctl。关于如何在两种方法之间创建默认指标的代码会产生不同的指标值。例如:创建 IPv6 默认路由 viaip route将导致 RHEL 7 上的度量值为 1024,而创建相同的路由 viaroute将导致度量值为 1。

红帽:

  • 如果没有任何内容作为路由度量传递给路由命令,则命令本身将使用值 1。
  • 如果没有任何内容作为路由度量传递给 ip 命令,则根本不会创建该属性,并且内核将其理解为 0,稍后将其转换为默认值 1024。

答案3

如果未另行设置,网络管理器默认按设备类型设置指标。请参阅 man 5 nm 设置。这是摘录,您可以搜索“metric”一词。另请注意,它们根据 ip v4 与 v6 的不同而有所不同。不确定这是否对您有帮助。

IPv4 设置
IPv4 设置。

   ┌───────────────────┬───────────────────┬───────────────┬────────────────────────┐

   │Key Name           │ Value Type        │ Default Value │ Value Description      │  
   ├───────────────────┼───────────────────┼───────────────┼────────────────────────┤

   ...  
   ├───────────────────┼───────────────────┼───────────────┼────────────────────────┤

   │route-data         │ array of vardict  │               │ Array of IPv4 routes.  │  
   │                   │                   │               │ Each route dictionary  │  
   │                   │                   │               │ contains at least      │  
   │                   │                   │               │ 'dest' and 'prefix'    │  
   │                   │                   │               │ entries, containing    │  
   │                   │                   │               │ the destination IP     │  
   │                   │                   │               │ address as a string,   │  
   │                   │                   │               │ and the prefix length  │  
   │                   │                   │               │ as a uint32. Most      │  
   │                   │                   │               │ routes will also have  │  
   │                   │                   │               │ a 'gateway' entry,     │  
   │                   │                   │               │ containing the gateway │  
   │                   │                   │               │ IP address as a        │  
   │                   │                   │               │ string. If the route   │  
   │                   │                   │               │ has a 'metric' entry   │  
   │                   │                   │               │ (containing a uint32), │  
   │                   │                   │               │ that will be used as   │  
   │                   │                   │               │ the metric for the     │  
   │                   │                   │               │ route (otherwise NM    │  
   │                   │                   │               │ will pick a default    │  
   │                   │                   │               │ value appropriate to   │  
   │                   │                   │               │ the device).           │  
   │                   │                   │               │ Additional attributes  │  
   │                   │                   │               │ may also exist on some │  
   │                   │                   │               │ routes.                │  
   ├───────────────────┼───────────────────┼───────────────┼────────────────────────┤

   │route-metric       │ int64             │ -1            │ The default metric for │  
   │                   │                   │               │ routes that don't      │  
   │                   │                   │               │ explicitly specify a   │  
   │                   │                   │               │ metric. The default    │  
   │                   │                   │               │ value -1 means that    │  
   │                   │                   │               │ the metric is chosen   │  
   │                   │                   │               │ automatically based on │  
   │                   │                   │               │ the device type. The   │  
   │                   │                   │               │ metric applies to      │  
   │                   │                   │               │ dynamic routes, manual │  
   │                   │                   │               │ (static) routes that   │  
   │                   │                   │               │ don't have an explicit │  
   │                   │                   │               │ metric setting,        │  
   │                   │                   │               │ address prefix routes, │  
   │                   │                   │               │ and the default route. │  
   │                   │                   │               │ Note that for IPv6,    │  
   │                   │                   │               │ the kernel accepts     │  
   │                   │                   │               │ zero (0) but coerces   │  
   │                   │                   │               │ it to 1024 (user       │  
   │                   │                   │               │ default). Hence,       │  
   │                   │                   │               │ setting this property  │  
   │                   │                   │               │ to zero effectively    │  
   │                   │                   │               │ mean setting it to     │  
   │                   │                   │               │ 1024. For IPv4, zero   │  
   │                   │                   │               │ is a regular value for │  
   │                   │                   │               │ the metric.            │  
   ├───────────────────┼───────────────────┼───────────────┼────────────────────────┤

   │route-table        │ uint32            │ 0             │ Enable policy routing  │  
   │                   │                   │               │ (source routing) and   │  
   │                   │                   │               │ set the routing table  │  
   │                   │                   │               │ used when adding       │  
   │                   │                   │               │ routes. This affects   │  
   │                   │                   │               │ all routes, including  │  
   │                   │                   │               │ device-routes, IPv4LL, │  
   │                   │                   │               │ DHCP, SLAAC,           │  
   │                   │                   │               │ default-routes and     │  
   │                   │                   │               │ static routes. But     │  
   │                   │                   │               │ note that static       │  
   │                   │                   │               │ routes can             │  
   │                   │                   │               │ individually overwrite │  
   │                   │                   │               │ the setting by         │  
   │                   │                   │               │ explicitly specifying  │  
   │                   │                   │               │ a non-zero routing     │  
   │                   │                   │               │ table. If the table    │  
   │                   │                   │               │ setting is left at     │  
   │                   │                   │               │ zero, it is eligible   │  
   │                   │                   │               │ to be overwritten via  │  
   │                   │                   │               │ global configuration.  │  
   │                   │                   │               │ If the property is     │  
   │                   │                   │               │ zero even after        │  
   │                   │                   │               │ applying the global    │  
   │                   │                   │               │ configuration value,   │  
   │                   │                   │               │ policy routing is      │  
   │                   │                   │               │ disabled for the       │  
   │                   │                   │               │ address family of this │  
   │                   │                   │               │ connection. Policy     │  
   │                   │                   │               │ routing disabled means │  
   │                   │                   │               │ that NetworkManager    │  
   │                   │                   │               │ will add all routes to │  
   │                   │                   │               │ the main table (except │  
   │                   │                   │               │ static routes that     │  
   │                   │                   │               │ explicitly configure a │  
   │                   │                   │               │ different table).      │  
   │                   │                   │               │ Additionally,          │  
   │                   │                   │               │ NetworkManager will    │  
   │                   │                   │               │ not delete any         │  
   │                   │                   │               │ extraneous routes from │  
   │                   │                   │               │ tables except the main │  
   │                   │                   │               │ table. This is to      │  
   │                   │                   │               │ preserve backward      │  
   │                   │                   │               │ compatibility for      │  
   │                   │                   │               │ users who manage       │  
   │                   │                   │               │ routing tables outside │  
   │                   │                   │               │ of NetworkManager.     │  
   ├───────────────────┼───────────────────┼───────────────┼────────────────────────┤

   │routes             │ array of array of │ []            │ Deprecated in favor of │  
   │                   │ uint32            │               │ the 'route-data'       │  
   │                   │                   │               │ property, but this can │  
   │                   │                   │               │ be used for            │  
   │                   │                   │               │ backward-compatibility │  
   │                   │                   │               │ with older daemons.    │  
   │                   │                   │               │ Note that if you send  │  
   │                   │                   │               │ this property the      │  
   │                   │                   │               │ daemon will ignore     │  
   │                   │                   │               │ 'route-data'.  Array   │  
   │                   │                   │               │ of IPv4 route          │  
   │                   │                   │               │ structures.  Each IPv4 │  
   │                   │                   │               │ route structure is     │  
   │                   │                   │               │ composed of 4 32-bit   │  
   │                   │                   │               │ values; the first      │  
   │                   │                   │               │ being the destination  │  
   │                   │                   │               │ IPv4 network or        │  
   │                   │                   │               │ address (network byte  │  
   │                   │                   │               │ order), the second the │  
   │                   │                   │               │ destination network or │  
   │                   │                   │               │ address prefix (1 -    │  
   │                   │                   │               │ 32), the third being   │  
   │                   │                   │               │ the next-hop (network  │  
   │                   │                   │               │ byte order) if any,    │  
   │                   │                   │               │ and the fourth being   │  
   │                   │                   │               │ the route metric. If   │  
   │                   │                   │               │ the metric is 0, NM    │  
   │                   │                   │               │ will choose an         │  
   │                   │                   │               │ appropriate default    │  
   │                   │                   │               │ metric for the device. │  
   │                   │                   │               │ (There is no way to    │  
   │                   │                   │               │ explicitly specify an  │  
   │                   │                   │               │ actual metric of 0     │  
   │                   │                   │               │ with this property.)   │  
   └───────────────────┴───────────────────┴───────────────┴────────────────────────┘

  ipv6 setting  
   IPv6 Settings.  

   ┌───────────────────┬───────────────────────────┬───────────────┬────────────────────────────────────────────────────┐

   │Key Name           │ Value Type                │ Default Value │ Value Description                                  │  
   ├───────────────────┼───────────────────────────┼───────────────┼────────────────────────────────────────────────────┤

   ...  
   ├───────────────────┼───────────────────────────┼───────────────┼────────────────────────────────────────────────────┤

   │route-data         │ array of vardict          │               │ Array of IPv6 routes. Each route dictionary        │  
   │                   │                           │               │ contains at least 'dest' and 'prefix' entries,     │  
   │                   │                           │               │ containing the destination IP address as a string, │  
   │                   │                           │               │ and the prefix length as a uint32. Most routes     │  
   │                   │                           │               │ will also have a 'next-hop' entry, containing the  │  
   │                   │                           │               │ next hop IP address as a string. If the route has  │  
   │                   │                           │               │ a 'metric' entry (containing a uint32), that will  │  
   │                   │                           │               │ be used as the metric for the route (otherwise NM  │  
   │                   │                           │               │ will pick a default value appropriate to the       │  
   │                   │                           │               │ device). Additional attributes may also exist on   │  
   │                   │                           │               │ some routes.                                       │  
   ├───────────────────┼───────────────────────────┼───────────────┼────────────────────────────────────────────────────┤

   │route-metric       │ int64                     │ -1            │ The default metric for routes that don't           │  
   │                   │                           │               │ explicitly specify a metric. The default value -1  │  
   │                   │                           │               │ means that the metric is chosen automatically      │  
   │                   │                           │               │ based on the device type. The metric applies to    │  
   │                   │                           │               │ dynamic routes, manual (static) routes that don't  │  
   │                   │                           │               │ have an explicit metric setting, address prefix    │  
   │                   │                           │               │ routes, and the default route. Note that for IPv6, │  
   │                   │                           │               │ the kernel accepts zero (0) but coerces it to 1024 │  
   │                   │                           │               │ (user default). Hence, setting this property to    │  
   │                   │                           │               │ zero effectively mean setting it to 1024. For      │  
   │                   │                           │               │ IPv4, zero is a regular value for the metric.      │  
   ├───────────────────┼───────────────────────────┼───────────────┼────────────────────────────────────────────────────┤

   │route-table        │ uint32                    │ 0             │ Enable policy routing (source routing) and set the │  
   │                   │                           │               │ routing table used when adding routes. This        │  
   │                   │                           │               │ affects all routes, including device-routes,       │  
   │                   │                           │               │ IPv4LL, DHCP, SLAAC, default-routes and static     │  
   │                   │                           │               │ routes. But note that static routes can            │  
   │                   │                           │               │ individually overwrite the setting by explicitly   │  
   │                   │                           │               │ specifying a non-zero routing table. If the table  │  
   │                   │                           │               │ setting is left at zero, it is eligible to be      │  
   │                   │                           │               │ overwritten via global configuration. If the       │  
   │                   │                           │               │ property is zero even after applying the global    │  
   │                   │                           │               │ configuration value, policy routing is disabled    │  
   │                   │                           │               │ for the address family of this connection. Policy  │  
   │                   │                           │               │ routing disabled means that NetworkManager will    │  
   │                   │                           │               │ add all routes to the main table (except static    │  
   │                   │                           │               │ routes that explicitly configure a different       │  
   │                   │                           │               │ table). Additionally, NetworkManager will not      │  
   │                   │                           │               │ delete any extraneous routes from tables except    │  
   │                   │                           │               │ the main table. This is to preserve backward       │  
   │                   │                           │               │ compatibility for users who manage routing tables  │  
   │                   │                           │               │ outside of NetworkManager.                         │  
   ├───────────────────┼───────────────────────────┼───────────────┼────────────────────────────────────────────────────┤

   │routes             │ array of legacy IPv6      │ []            │ Deprecated in favor of the 'route-data' property,  │  
   │                   │ route struct (a(ayuayu))  │               │ but this can be used for backward-compatibility    │  
   │                   │                           │               │ with older daemons. Note that if you send this     │  
   │                   │                           │               │ property the daemon will ignore 'route-data'.      │  
   │                   │                           │               │ Array of IPv6 route structures.  Each IPv6 route   │  
   │                   │                           │               │ structure is composed of an IPv6 address, a prefix │  
   │                   │                           │               │ length (1 - 128), an IPv6 next hop address (which  │  
   │                   │                           │               │ may be zeroed out if there is no next hop), and a  │  
   │                   │                           │               │ metric. If the metric is 0, NM will choose an      │  
   │                   │                           │               │ appropriate default metric for the device.         │  
   ├───────────────────┼───────────────────────────┼───────────────┼────────────────────────────────────────────────────┤

   │token              │ string                    │               │ Configure the token for                            │  
   │                   │                           │               │ draft-chown-6man-tokenised-ipv6-identifiers-02     │  
   │                   │                           │               │ IPv6 tokenized interface identifiers. Useful with  │  
   │                   │                           │               │ eui64 addr-gen-mode.                               │  
   └───────────────────┴───────────────────────────┴───────────────┴────────────────────────────────────────────────────┘  

相关内容