Handle RTAMetrics properly

This commit is contained in:
Brian Candler
2011-05-03 19:03:35 +01:00
parent 340e5e85f3
commit 834f7a83d6
2 changed files with 22 additions and 16 deletions

View File

@@ -16,10 +16,10 @@ pp rt.links["eth0"]
puts "\nAddresses on interface eth0:" puts "\nAddresses on interface eth0:"
pp rt.addrs.list(:index=>"eth0").to_a pp rt.addrs.list(:index=>"eth0").to_a
puts "\nAll v4 routes in main routing table:" puts "\nAll routes in main routing table:"
pp rt.routes.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN).to_a pp rt.routes.list(:table=>Netlink::RT_TABLE_MAIN).to_a
puts "\nDefault route is probably:" puts "\nV4 default route is probably:"
pp rt.routes.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN). pp rt.routes.list(:family=>Socket::AF_INET, :table=>Netlink::RT_TABLE_MAIN).
min_by { |route| route.dst_len } min_by { |route| route.dst_len }

View File

@@ -25,19 +25,8 @@ module Netlink
rtattr :gateway, RTA_GATEWAY, :l3addr rtattr :gateway, RTA_GATEWAY, :l3addr
rtattr :priority, RTA_PRIORITY, :uint32 rtattr :priority, RTA_PRIORITY, :uint32
rtattr :prefsrc, RTA_PREFSRC, :l3addr rtattr :prefsrc, RTA_PREFSRC, :l3addr
# Route metrics are themselves packed using the rtattr format. rtattr :metrics, RTA_METRICS,
# In the kernel, the dst.metrics structure is an array of u32. :unpack => lambda { |str,obj| RTAMetrics.parse(str) }
METRIC_PACK = "SSL".freeze #:nodoc:
METRIC_SIZE = [0,0,0].pack(METRIC_PACK).bytesize #:nodoc:
rtattr :metrics, RTA_METRICS, # {RTAX_* => Integer}
:pack => lambda { |metrics,obj|
metrics.map { |code,val| [METRIC_SIZE,code,val].pack(METRIC_PACK) }.join
},
:unpack => lambda { |str,obj|
res = {}
RtattrMessage.unpack_rtattr(str) { |code,val| res[code] = val.unpack("L").first }
res
}
rtattr :multipath, RTA_MULTIPATH rtattr :multipath, RTA_MULTIPATH
rtattr :flow, RTA_FLOW rtattr :flow, RTA_FLOW
rtattr :cacheinfo, RTA_CACHEINFO, rtattr :cacheinfo, RTA_CACHEINFO,
@@ -46,6 +35,23 @@ module Netlink
rtattr :table2, RTA_TABLE, :uint32 # NOTE: table in two places! rtattr :table2, RTA_TABLE, :uint32 # NOTE: table in two places!
end end
class RTAMetrics < RtattrMessage
rtattr :lock, RTAX_LOCK, :uint32
rtattr :mtu, RTAX_MTU, :uint32
rtattr :window, RTAX_WINDOW, :uint32
rtattr :rtt, RTAX_RTT, :uint32
rtattr :rttvar, RTAX_RTTVAR, :uint32
rtattr :ssthresh, RTAX_SSTHRESH, :uint32
rtattr :cwnd, RTAX_CWND, :uint32
rtattr :advmss, RTAX_ADVMSS, :uint32
rtattr :reordering, RTAX_REORDERING, :uint32
rtattr :hoplimit, RTAX_HOPLIMIT, :uint32
rtattr :initcwnd, RTAX_INITCWND, :uint32
rtattr :features, RTAX_FEATURES, :uint32
rtattr :rto_min, RTAX_RTO_MIN, :uint32
rtattr :initrwnd, RTAX_INITRWND, :uint32
end
module Route module Route
# This class manipulates the kernel routing table # This class manipulates the kernel routing table
class RouteHandler < Handler class RouteHandler < Handler