Reorganise under Linux::
This commit is contained in:
126
lib/linux/netlink/route/addr_handler.rb
Normal file
126
lib/linux/netlink/route/addr_handler.rb
Normal file
@@ -0,0 +1,126 @@
|
||||
require 'linux/netlink/route'
|
||||
require 'linux/netlink/route/handler'
|
||||
|
||||
module Linux
|
||||
module Netlink
|
||||
# struct ifa_cacheinfo
|
||||
IFACacheInfo = Struct.new :prefered, :valid, :cstamp, :tstamp
|
||||
|
||||
# struct ifaddrmsg
|
||||
class IFAddr < RtattrMessage
|
||||
code RTM_NEWADDR, RTM_DELADDR, RTM_GETADDR
|
||||
|
||||
field :family, :uchar # Socket::AF_*
|
||||
field :prefixlen, :uchar
|
||||
field :flags, :uchar # IFA_F_*
|
||||
field :scope, :uchar # RT_SCOPE_*
|
||||
field :index, :int
|
||||
rtattr :address, IFA_ADDRESS, :l3addr
|
||||
rtattr :local, IFA_LOCAL, :l3addr
|
||||
rtattr :label, IFA_LABEL, :cstring
|
||||
rtattr :broadcast, IFA_BROADCAST, :l3addr
|
||||
rtattr :anycast, IFA_ANYCAST, :l3addr
|
||||
rtattr :cacheinfo, IFA_CACHEINFO,
|
||||
:pack => lambda { |val,obj| val.to_a.pack("L*") },
|
||||
:unpack => lambda { |str,obj| IFACacheInfo.new(*(str.unpack("L*"))) }
|
||||
rtattr :multicast, IFA_MULTICAST, :l3addr
|
||||
end
|
||||
|
||||
module Route
|
||||
# This class provides an API for manipulating iaddresses.
|
||||
class AddrHandler < Handler
|
||||
def clear_cache
|
||||
@addr = nil
|
||||
end
|
||||
|
||||
# Download a list of link addresses. Either returns an array of
|
||||
# Netlink::IFAddr objects, or yields them to the supplied block.
|
||||
# You will need to use the 'index' to cross reference to the interface.
|
||||
#
|
||||
# A hash of kernel options may be supplied, but likely only :family
|
||||
# is honoured.
|
||||
#
|
||||
# res = nl.read_addr(:family => Socket::AF_INET)
|
||||
# p res
|
||||
# [#<Linux::Netlink::IFAddr {:family=>2, :prefixlen=>8, :flags=>128, :scope=>254,
|
||||
# :index=>1, :address=>#<IPAddr: IPv4:127.0.0.1/255.255.255.255>,
|
||||
# :local=>#<IPAddr: IPv4:127.0.0.1/255.255.255.255>, :label=>"lo"}>, ...]
|
||||
def read_addr(opt=nil, &blk)
|
||||
@rtsocket.send_request RTM_GETADDR, IFAddr.new(opt),
|
||||
NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST
|
||||
@rtsocket.receive_until_done(RTM_NEWADDR, &blk)
|
||||
end
|
||||
|
||||
class Filter < BaseFilter #:nodoc:
|
||||
filter(:family) { |o,v| o.family == v }
|
||||
filter(:scope) { |o,v| o.scope == scope }
|
||||
filter(:flags) { |o,v| (o.flags & v) == v }
|
||||
filter(:noflags) { |o,v| (o.flags & v) == 0 }
|
||||
filter(:index) { |o,v| o.index == v }
|
||||
end
|
||||
|
||||
# Iterate over all addresses, or addressees matching the given
|
||||
# criteria. Returns an Enumerator if no block given.
|
||||
#
|
||||
# The full address list is read once and memoized, so
|
||||
# it is efficient to call this method multiple times.
|
||||
#
|
||||
# nl.addr.list { |x| p x }
|
||||
# addrs_eth0 = nl.addr.list(:index=>"eth0").to_a
|
||||
# addrs_eth0_v4 = nl.addr.list(:index=>"eth0", :family=>Socket::AF_INET).to_a
|
||||
def list(filter=nil, &blk)
|
||||
@addr ||= read_addr
|
||||
filter[:index] = index(filter[:index]) if filter && filter.has_key?(:index)
|
||||
filter_list(@addr, filter, &blk)
|
||||
end
|
||||
alias :each :list
|
||||
|
||||
# Return addresses grouped by interface name. e.g.
|
||||
# group_by_interface(:family => Socket::AF_INET).to_a
|
||||
# #=> {"eth0"=>[addr, addr,...], "lo"=>[addr, addr,...]
|
||||
#
|
||||
# The hash has an empty array as its default, so it's safe to do
|
||||
# group_by_interface(...)["eth0"].each { |a| ... }
|
||||
# even if eth0 has no addresses matching the given filter.
|
||||
def group_by_interface(*filter)
|
||||
res = list(*filter).group_by { |a| ifname(a.index) }
|
||||
res.default = EMPTY_ARRAY
|
||||
res
|
||||
end
|
||||
|
||||
# Add an IP address to an interface
|
||||
#
|
||||
# require 'netlink/route'
|
||||
# ip = Linux::Netlink::Route::Socket.new
|
||||
# ip.addr.add(:index=>"eth0", :local=>"1.2.3.4", :prefixlen=>24)
|
||||
def add(opt)
|
||||
ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, opt)
|
||||
end
|
||||
|
||||
def change(opt)
|
||||
ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, opt)
|
||||
end
|
||||
|
||||
def replace(opt)
|
||||
ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, opt)
|
||||
end
|
||||
|
||||
# Delete an IP address from an interface. Pass in either a hash of
|
||||
# parameters, or an existing IFAddr object.
|
||||
def delete(opt)
|
||||
ipaddr_modify(RTM_DELADDR, 0, opt)
|
||||
end
|
||||
|
||||
def ipaddr_modify(code, flags, msg) #:nodoc:
|
||||
msg = IFAddr.new(msg)
|
||||
msg.index = index(msg.index) unless msg.index.is_a?(Integer)
|
||||
msg.address ||= msg.local
|
||||
# Note: IPAddr doesn't support addresses off the subnet base,
|
||||
# so there's no point trying to set msg.prefixlen from the IPAddr mask
|
||||
@rtsocket.cmd code, msg, flags|NLM_F_REQUEST
|
||||
clear_cache
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end # module Linux
|
48
lib/linux/netlink/route/handler.rb
Normal file
48
lib/linux/netlink/route/handler.rb
Normal file
@@ -0,0 +1,48 @@
|
||||
module Linux
|
||||
module Netlink
|
||||
module Route
|
||||
# The base class containing shared methods between all the
|
||||
# NETLINK_ROUTE handler classes.
|
||||
class BaseFilter #:nodoc:
|
||||
def self.filter name, &blk
|
||||
define_method "#{name}=" do |matchval|
|
||||
@conds << [blk, matchval]
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(h)
|
||||
@conds = []
|
||||
h.each { |k,v| send "#{k}=", v }
|
||||
end
|
||||
|
||||
def match(obj)
|
||||
!@conds.find { |blk,matchval| !blk[obj,matchval] }
|
||||
end
|
||||
end
|
||||
|
||||
# Code which is common to all the NETLINK_ROUTE handlers
|
||||
class Handler
|
||||
def initialize(rtsocket = Route::Socket.new)
|
||||
@rtsocket = rtsocket
|
||||
clear_cache
|
||||
end
|
||||
|
||||
def index(v)
|
||||
@rtsocket.index(v)
|
||||
end
|
||||
|
||||
def ifname(v)
|
||||
@rtsocket.ifname(v)
|
||||
end
|
||||
|
||||
# Generic listing and filtering
|
||||
def filter_list(data, filter, &blk)
|
||||
return data.each(&blk) unless filter
|
||||
return to_enum(:list, filter) unless block_given?
|
||||
fm = self.class::Filter.new(filter)
|
||||
data.each { |o| yield o if fm.match(o) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end # module Linux
|
247
lib/linux/netlink/route/link_handler.rb
Normal file
247
lib/linux/netlink/route/link_handler.rb
Normal file
@@ -0,0 +1,247 @@
|
||||
require 'linux/netlink/route'
|
||||
require 'linux/netlink/route/handler'
|
||||
|
||||
module Linux
|
||||
module Netlink
|
||||
# struct rtnl_link_stats / rtnl_link_stats64
|
||||
LinkStats = Struct.new :rx_packets, :tx_packets,
|
||||
:rx_bytes, :tx_bytes,
|
||||
:rx_errors, :tx_errors,
|
||||
:rx_dropped, :tx_dropped,
|
||||
:multicast, :collisions,
|
||||
:rx_length_errors, :rx_over_errors,
|
||||
:rx_crc_errors, :rx_frame_errors,
|
||||
:rx_fifo_errors, :rx_missed_errors,
|
||||
:tx_aborted_errorsr, :tx_carrier_errors,
|
||||
:tx_fifo_errors, :tx_heartbeat_errors,
|
||||
:tx_window_errors,
|
||||
:rx_compressed, :tx_compressed
|
||||
|
||||
# struct ifmap
|
||||
IFMap = Struct.new :mem_start, :mem_end, :base_addr, :irq, :dma, :port
|
||||
|
||||
# struct ifinfomsg
|
||||
class IFInfo < RtattrMessage
|
||||
code RTM_NEWLINK, RTM_DELLINK, RTM_GETLINK
|
||||
|
||||
field :family, :uchar
|
||||
field :type, :ushort # ARPHRD_*
|
||||
field :index, :int
|
||||
field :flags, :uint # IFF_*
|
||||
field :change, :uint, :default=>0xffffffff # flags to change
|
||||
rtattr :address, IFLA_ADDRESS, :l2addr
|
||||
rtattr :broadcast, IFLA_BROADCAST, :l2addr
|
||||
rtattr :ifname, IFLA_IFNAME, :cstring
|
||||
rtattr :mtu, IFLA_MTU, :uint32
|
||||
rtattr :link, IFLA_LINK, :int32
|
||||
rtattr :qdisc, IFLA_QDISC, :cstring
|
||||
rtattr :stats32, IFLA_STATS,
|
||||
:pack => lambda { |val,obj| val.to_a.pack("L23") },
|
||||
:unpack => lambda { |str,obj| LinkStats.new(*(str.unpack("L23"))) }
|
||||
rtattr :cost, IFLA_COST
|
||||
rtattr :master, IFLA_MASTER, :uint32
|
||||
rtattr :wireless, IFLA_WIRELESS
|
||||
rtattr :protinfo, IFLA_PROTINFO, :uchar
|
||||
rtattr :txqlen, IFLA_TXQLEN, :uint32
|
||||
IFMAP_PACK = "QQQSCC".freeze #:nodoc:
|
||||
rtattr :map, IFLA_MAP,
|
||||
:pack => lambda { |val,obj| val.to_a.pack(IFMAP_PACK) },
|
||||
:unpack => lambda { |str,obj| IFMap.new(*(str.unpack(IFMAP_PACK))) }
|
||||
rtattr :weight, IFLA_WEIGHT, :uint32
|
||||
rtattr :operstate, IFLA_OPERSTATE, :uchar
|
||||
rtattr :linkmode, IFLA_LINKMODE, :uchar
|
||||
rtattr :linkinfo, IFLA_LINKINFO # nested
|
||||
rtattr :net_ns_pid, IFLA_NET_NS_PID, :uint32
|
||||
rtattr :ifalias, IFLA_IFALIAS, :cstring
|
||||
rtattr :num_vf, IFLA_NUM_VF, :uint32
|
||||
rtattr :vfinfo_list, IFLA_VFINFO_LIST
|
||||
rtattr :stats64, IFLA_STATS64,
|
||||
:pack => lambda { |val,obj| val.to_a.pack("Q23") },
|
||||
:unpack => lambda { |str,obj| LinkStats.new(*(str.unpack("Q23"))) }
|
||||
rtattr :vf_ports, IFLA_VF_PORTS
|
||||
rtattr :port_self, IFLA_PORT_SELF
|
||||
|
||||
# Return the best stats available (64bit or 32bit)
|
||||
def stats
|
||||
stats64 || stats32
|
||||
end
|
||||
|
||||
# Link kind for special links, e.g. "vlan" or "gre"
|
||||
def kind
|
||||
linkinfo && linkinfo.kind
|
||||
end
|
||||
|
||||
# Set link kind, creating a linkinfo member if necessary. e.g.
|
||||
# i = IFAddr.new
|
||||
# i.kind = "vlan"
|
||||
# i.linkinfo.data = VlanInfo.new(...)
|
||||
def kind=(str)
|
||||
self.linkinfo ||= LinkInfo.new
|
||||
linkinfo.kind = str
|
||||
end
|
||||
|
||||
def kind?(str)
|
||||
kind == str
|
||||
end
|
||||
|
||||
def after_parse #:nodoc:
|
||||
self.linkinfo = LinkInfo.parse(linkinfo) if linkinfo
|
||||
end
|
||||
end
|
||||
|
||||
class LinkInfo < RtattrMessage
|
||||
rtattr :kind, IFLA_INFO_KIND, :cstring
|
||||
rtattr :data, IFLA_INFO_DATA # rtattr packed, see below
|
||||
rtattr :xstats, :IFLA_INFO_XSTATS # don't know
|
||||
|
||||
def after_parse #:nodoc:
|
||||
case kind
|
||||
when "vlan"
|
||||
self.data = VlanInfo.parse(data)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class VlanFlags < CStruct
|
||||
field :flags, :uint32
|
||||
field :mask, :uint32, :default => 0xffffffff
|
||||
end
|
||||
|
||||
# VLAN information is packed in rtattr format (there is no corresponding 'struct')
|
||||
class VlanInfo < RtattrMessage
|
||||
rtattr :id, IFLA_VLAN_ID, :ushort
|
||||
rtattr :flags, IFLA_VLAN_FLAGS,
|
||||
:unpack => lambda { |str,obj| VlanFlags.parse(str) }
|
||||
rtattr :egress_qos, IFLA_VLAN_EGRESS_QOS
|
||||
rtattr :ingress_qos, IFLA_VLAN_INGRESS_QOS
|
||||
end
|
||||
|
||||
module Route
|
||||
# This class provides an API for manipulating interfaces and addresses.
|
||||
# Since we frequently need to map ifname to ifindex, or vice versa,
|
||||
# we keep a memoized list of interfaces. If the interface list changes,
|
||||
# you should create a new instance of this object.
|
||||
class LinkHandler < Handler
|
||||
def clear_cache
|
||||
@link = nil
|
||||
@linkmap = nil
|
||||
end
|
||||
|
||||
# Download a list of links (interfaces). Either returns an array of
|
||||
# Netlink::IFInfo objects, or yields them to the supplied block.
|
||||
#
|
||||
# res = ip.link.read_link
|
||||
# p res
|
||||
# [#<Linux::Netlink::IFInfo {:family=>0, :type=>772, :index=>1,
|
||||
# :flags=>65609, :change=>0, :ifname=>"lo", :txqlen=>0, :operstate=>0,
|
||||
# :linkmode=>0, :mtu=>16436, :qdisc=>"noqueue", :map=>"...",
|
||||
# :address=>"\x00\x00\x00\x00\x00\x00", :broadcast=>"\x00\x00\x00\x00\x00\x00",
|
||||
# :stats32=>#<struct Linux::Netlink::LinkStats rx_packets=22, ...>,
|
||||
# :stats64=>#<struct Linux::Netlink::LinkStats rx_packets=22, ...>}>, ...]
|
||||
def read_link(opt=nil, &blk)
|
||||
@rtsocket.send_request RTM_GETLINK, IFInfo.new(opt),
|
||||
NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST
|
||||
@rtsocket.receive_until_done(RTM_NEWLINK, &blk)
|
||||
end
|
||||
|
||||
class Filter < BaseFilter #:nodoc:
|
||||
filter(:type) { |o,v| o.type == v }
|
||||
filter(:kind) { |o,v| o.kind?(v) }
|
||||
filter(:flags) { |o,v| (o.flags & v) == v }
|
||||
filter(:noflags) { |o,v| (o.flags & v) == 0 }
|
||||
filter(:link) { |o,v| o.link == v }
|
||||
end
|
||||
|
||||
# Iterate over all interfaces, or interfaces matching the given
|
||||
# criteria. Returns an Enumerator if no block given.
|
||||
#
|
||||
# The full interface list is read once and memoized, so
|
||||
# it is efficient to call this method multiple times.
|
||||
#
|
||||
# ip.link.list { |x| p x }
|
||||
# ethers = ip.link.list(:type => Linux::ARPHRD_ETHER).to_a
|
||||
# vlans = ip.link.list(:kind => "vlan").to_a
|
||||
# ip.link.list(:flags => Linux::IFF_RUNNING)
|
||||
# ip.link.list(:noflags => Linux::IFF_POINTOPOINT)
|
||||
# ip.link.list(:link => "lo") # vlan etc attached to this interface
|
||||
def list(filter=nil, &blk)
|
||||
@link ||= read_link
|
||||
filter[:link] = index(filter[:link]) if filter && filter.has_key?(:link)
|
||||
filter_list(@link, filter, &blk)
|
||||
end
|
||||
alias :each :list
|
||||
|
||||
# Return a memoized Hash of interfaces, keyed by both index and name
|
||||
def linkmap
|
||||
@linkmap ||= (
|
||||
h = {}
|
||||
list { |link| h[link.index] = h[link.ifname] = link }
|
||||
h
|
||||
)
|
||||
end
|
||||
|
||||
# Return details of one interface, given its name or index.
|
||||
# Raises exception if unknown value.
|
||||
def [](key)
|
||||
linkmap.fetch(key)
|
||||
end
|
||||
|
||||
# Add an interface (raw). e.g.
|
||||
#
|
||||
# require 'linux/netlink/route'
|
||||
# ip = Linux::Netlink::Route::Socket.new
|
||||
# ip.link.add(
|
||||
# :link=>"lo",
|
||||
# :linkinfo=>Linux::Netlink::LinkInfo.new(
|
||||
# :kind=>"vlan",
|
||||
# :data=>Linux::Netlink::VlanInfo.new(
|
||||
# :id=>1234,
|
||||
# :flags => Linux::Netlink::VlanFlags.new(
|
||||
# :flags=>Linux::VLAN_FLAG_LOOSE_BINDING,
|
||||
# :mask=>0xffffffff
|
||||
# ))))
|
||||
|
||||
def add(opt)
|
||||
iplink_modify(RTM_NEWLINK, NLM_F_CREATE|NLM_F_EXCL, opt)
|
||||
end
|
||||
|
||||
def change(opt)
|
||||
iplink_modify(RTM_NEWLINK, NLM_F_REPLACE, opt)
|
||||
end
|
||||
|
||||
def replace(opt)
|
||||
iplink_modify(RTM_NEWLINK, NLM_F_CREATE|NLM_F_REPLACE, opt)
|
||||
end
|
||||
|
||||
# Delete an existing link. Pass in ifname or index, or options
|
||||
# hash {:index=>n}
|
||||
def delete(opt)
|
||||
case opt
|
||||
when Integer
|
||||
opt = {:index=>opt}
|
||||
when String
|
||||
opt = {:index=>index(opt)}
|
||||
end
|
||||
iplink_modify(RTM_DELLINK, 0, opt)
|
||||
end
|
||||
|
||||
def iplink_modify(code, flags, msg) #:nodoc:
|
||||
msg = IFInfo.new(msg)
|
||||
|
||||
if (flags & NLM_F_CREATE) != 0
|
||||
raise "Missing :linkinfo" unless msg.linkinfo
|
||||
raise "Missing :kind" unless msg.linkinfo.kind
|
||||
else
|
||||
raise "Missing :index" if msg.index.nil? || msg.index == 0
|
||||
end
|
||||
|
||||
msg.index = index(msg.index) if msg.index.is_a?(String)
|
||||
msg.link = index(msg.link) if msg.link.is_a?(String)
|
||||
|
||||
@rtsocket.cmd code, msg, flags|NLM_F_REQUEST
|
||||
clear_cache
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end # module Linux
|
199
lib/linux/netlink/route/route_handler.rb
Normal file
199
lib/linux/netlink/route/route_handler.rb
Normal file
@@ -0,0 +1,199 @@
|
||||
require 'linux/netlink/route'
|
||||
require 'linux/netlink/route/handler'
|
||||
|
||||
module Linux
|
||||
module Netlink
|
||||
# struct rta_cacheinfo
|
||||
RTACacheInfo = Struct.new :clntref, :lastuse, :expires, :error, :used, :id, :ts, :tsage
|
||||
|
||||
# struct rtmsg
|
||||
class RT < RtattrMessage
|
||||
code RTM_NEWROUTE, RTM_DELROUTE, RTM_GETROUTE
|
||||
|
||||
field :family, :uchar # Socket::AF_*
|
||||
field :dst_len, :uchar
|
||||
field :src_len, :uchar
|
||||
field :tos, :uchar
|
||||
field :table, :uchar # table id or RT_TABLE_*
|
||||
field :protocol, :uchar # RTPROT_*
|
||||
field :scope, :uchar # RT_SCOPE_*
|
||||
field :type, :uchar # RTN_*
|
||||
field :flags, :uint # RTM_F_*
|
||||
rtattr :dst, RTA_DST, :l3addr
|
||||
rtattr :src, RTA_SRC, :l3addr
|
||||
rtattr :iif, RTA_IIF, :uint32
|
||||
rtattr :oif, RTA_OIF, :uint32
|
||||
rtattr :gateway, RTA_GATEWAY, :l3addr
|
||||
rtattr :priority, RTA_PRIORITY, :uint32
|
||||
rtattr :prefsrc, RTA_PREFSRC, :l3addr
|
||||
rtattr :metrics, RTA_METRICS,
|
||||
:unpack => lambda { |str,obj| RTAMetrics.parse(str) }
|
||||
rtattr :multipath, RTA_MULTIPATH
|
||||
rtattr :flow, RTA_FLOW
|
||||
rtattr :cacheinfo, RTA_CACHEINFO,
|
||||
:pack => lambda { |val,obj| val.to_a.pack("L*") },
|
||||
:unpack => lambda { |str,obj| RTACacheInfo.new(*(str.unpack("L*"))) }
|
||||
rtattr :table2, RTA_TABLE, :uint32 # NOTE: table in two places!
|
||||
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
|
||||
# This class manipulates the kernel routing table
|
||||
class RouteHandler < Handler
|
||||
def clear_cache
|
||||
@route = nil
|
||||
end
|
||||
|
||||
# Send message to download the kernel routing table. Either returns an
|
||||
# array of Netlink::RT objects, or yields them to the supplied block.
|
||||
#
|
||||
# A hash of kernel options may be supplied, but you might also have
|
||||
# to perform your own filtering. e.g.
|
||||
# read_route(:family=>Socket::AF_INET) # works
|
||||
# read_route(:protocol=>Linux::RTPROT_STATIC) # ignored
|
||||
#
|
||||
# res = ip.route.read_route(:family => Socket::AF_INET)
|
||||
# p res
|
||||
# [#<Linux::Netlink::RT {:family=>2, :dst_len=>32, :src_len=>0, :tos=>0,
|
||||
# :table=>255, :protocol=>2, :scope=>253, :type=>3, :flags=>0, :table2=>255,
|
||||
# :dst=>#<IPAddr: IPv4:127.255.255.255/255.255.255.255>,
|
||||
# :prefsrc=>#<IPAddr: IPv4:127.0.0.1/255.255.255.255>, :oif=>1}>, ...]
|
||||
#
|
||||
# Note that not all attributes will always be present. In particular,
|
||||
# a defaultroute (dst_len=0) misses out the dst address completely:
|
||||
#
|
||||
# [#<Linux::Netlink::RT {:family=>2, :dst_len=>0, :src_len=>0, :tos=>0,
|
||||
# :table=>254, :protocol=>4, :scope=>0, :type=>1, :flags=>0, :table2=>254,
|
||||
# :gateway=>#<IPAddr: IPv4:10.69.255.253/255.255.255.255>, :oif=>2}>, ...]
|
||||
def read_route(opt=nil, &blk)
|
||||
@rtsocket.send_request RTM_GETROUTE, RT.new(opt),
|
||||
NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST
|
||||
@rtsocket.receive_until_done(RTM_NEWROUTE, &blk)
|
||||
end
|
||||
|
||||
class Filter < BaseFilter #:nodoc:
|
||||
filter(:family) { |o,v| o.family == v }
|
||||
filter(:table) { |o,v| o.table == v }
|
||||
filter(:protocol) { |o,v| o.protocol == v }
|
||||
filter(:type) { |o,v| o.type == v }
|
||||
filter(:scope) { |o,v| o.scope == v }
|
||||
filter(:flags) { |o,v| (o.flags & v) == v }
|
||||
filter(:noflags) { |o,v| (o.flags & v) == 0 }
|
||||
filter(:oif) { |o,v| o.oif == v }
|
||||
filter(:iif) { |o,v| o.iif == v }
|
||||
end
|
||||
|
||||
# Return the memoized route table, filtered according to
|
||||
# the optional criteria. Examples:
|
||||
# :family => Socket::AF_INET
|
||||
# :table => Linux::RT_TABLE_DEFAULT
|
||||
# :protocol => Linux::RTPROT_STATIC
|
||||
# :type => Linux::RTN_UNICAST
|
||||
# :scope => Linux::RT_SCOPE_HOST
|
||||
# :flags => Linux::RTM_F_NOTIFY
|
||||
# :noflags => Linux::RTM_F_CLONED
|
||||
# :oif => "eth0"
|
||||
# :iif => "eth1"
|
||||
def list(filter=nil, &blk)
|
||||
@route ||= read_route
|
||||
filter[:oif] = index(filter[:oif]) if filter && filter.has_key?(:oif)
|
||||
filter[:iif] = index(filter[:iif]) if filter && filter.has_key?(:iif)
|
||||
filter_list(@route, filter, &blk)
|
||||
end
|
||||
alias :each :list
|
||||
|
||||
def add(opt)
|
||||
iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_EXCL, opt)
|
||||
end
|
||||
|
||||
def change(opt)
|
||||
iproute_modify(RTM_NEWROUTE, NLM_F_REPLACE, opt)
|
||||
end
|
||||
|
||||
def replace(opt)
|
||||
iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_REPLACE, opt)
|
||||
end
|
||||
|
||||
def prepend(opt)
|
||||
iproute_modify(RTM_NEWROUTE, NLM_F_CREATE, opt)
|
||||
end
|
||||
|
||||
def append(opt)
|
||||
iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_APPEND, opt)
|
||||
end
|
||||
|
||||
def test(opt)
|
||||
iproute_modify(RTM_NEWROUTE, NLM_F_EXCL, opt)
|
||||
end
|
||||
|
||||
def delete(opt)
|
||||
iproute_modify(RTM_DELROUTE, 0, opt)
|
||||
end
|
||||
|
||||
# Get route matching given criteria
|
||||
def get(msg)
|
||||
msg = RT.new(msg)
|
||||
raise "Missing :dst" unless msg.dst
|
||||
msg.iif = index(msg.iif) if msg.iif.is_a?(String)
|
||||
msg.oif = index(msg.oif) if msg.oif.is_a?(String)
|
||||
@rtsocket.cmd RTM_GETROUTE, msg, NLM_F_REQUEST, RTM_NEWROUTE
|
||||
end
|
||||
|
||||
def iproute_modify(code, flags, msg) #:nodoc:
|
||||
msg = RT.new(msg)
|
||||
|
||||
if code != RTM_DELROUTE
|
||||
msg.protocol ||= RTPROT_BOOT
|
||||
msg.type ||= RTN_UNICAST
|
||||
end
|
||||
# There is scary code in ip/iproute.c for setting defaults
|
||||
unless msg.table
|
||||
msg.table = case msg.type
|
||||
when RTN_LOCAL, RTN_BROADCAST, RTN_NAT, RTN_ANYCAST
|
||||
RT_TABLE_LOCAL
|
||||
else
|
||||
RT_TABLE_MAIN
|
||||
end
|
||||
end
|
||||
unless msg.scope
|
||||
msg.scope = (code != RTM_DELROUTE) ? RT_SCOPE_UNIVERSE : RT_SCOPE_NOWHERE
|
||||
case msg.type
|
||||
when RTN_LOCAL, RTN_NAT
|
||||
msg.scope = RT_SCOPE_HOST
|
||||
when RTN_BROADCAST, RTN_MULTICAST, RTN_ANYCAST
|
||||
msg.scope RT_SCOPE_LINK
|
||||
when RTN_UNICAST, RTN_UNSPEC
|
||||
if code == RTM_DELROUTE
|
||||
msg.scope = RT_SCOPE_NOWHERE
|
||||
elsif !msg.gateway && !msg.multipath
|
||||
msg.scope = RT_SCOPE_LINK
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
msg.iif = index(msg.iif) if msg.iif.is_a?(String)
|
||||
msg.oif = index(msg.oif) if msg.oif.is_a?(String)
|
||||
|
||||
@rtsocket.cmd code, msg, flags|NLM_F_REQUEST
|
||||
clear_cache
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end # module Linux
|
67
lib/linux/netlink/route/vlan_handler.rb
Normal file
67
lib/linux/netlink/route/vlan_handler.rb
Normal file
@@ -0,0 +1,67 @@
|
||||
require 'linux/netlink/route'
|
||||
require 'linux/netlink/route/handler'
|
||||
|
||||
module Linux
|
||||
module Netlink
|
||||
module Route
|
||||
class VlanHandler < Handler
|
||||
def clear_cache
|
||||
# No cache
|
||||
end
|
||||
|
||||
def list(filter={}, &blk)
|
||||
@rtsocket.link.list(filter.merge(:kind=>"vlan"))
|
||||
end
|
||||
alias :each :list
|
||||
|
||||
# Higher-level API to manipulate VLAN interface.
|
||||
# nl.vlans.add(
|
||||
# :link=>"lo",
|
||||
# :vlan_id=>1234,
|
||||
# :vlan_flags=>Linux::VLAN_FLAG_LOOSE_BINDING,
|
||||
# :vlan_mask=>0xffffffff
|
||||
# )
|
||||
def add(opt)
|
||||
@rtsocket.link.add(vlan_options(opt))
|
||||
end
|
||||
|
||||
def change(opt)
|
||||
@rtsocket.link.change(vlan_options(opt))
|
||||
end
|
||||
|
||||
def replace(opt)
|
||||
@rtsocket.link.replace(vlan_options(opt))
|
||||
end
|
||||
|
||||
# Delete vlan given :link and :vlan_id. If you want to delete
|
||||
# by :index then call link.delete instead.
|
||||
def delete(opt)
|
||||
raise "Missing vlan_id" unless opt[:vlan_id]
|
||||
raise "Missing link" unless opt[:link]
|
||||
link = list(:link=>opt[:link]).find { |l|
|
||||
l.linkinfo.data &&
|
||||
l.linkinfo.data.id == opt[:vlan_id]
|
||||
}
|
||||
raise Errno::ENODEV unless link
|
||||
@rtsocket.link.delete(link.index)
|
||||
end
|
||||
|
||||
def vlan_options(orig) #:nodoc:
|
||||
opt = orig.dup
|
||||
opt[:link] = index(opt.fetch(:link))
|
||||
li = opt[:linkinfo] ||= LinkInfo.new
|
||||
li.kind = "vlan"
|
||||
li.data ||= VlanInfo.new
|
||||
li.data.id = opt.delete(:vlan_id) if opt.has_key?(:vlan_id)
|
||||
if opt.has_key?(:vlan_flags)
|
||||
li.data.flags ||= VlanFlags.new(:flags => opt.delete(:vlan_flags))
|
||||
li.data.flags.mask = opt.delete(:vlan_mask) if opt.has_key?(:vlan_mask)
|
||||
end
|
||||
li.data.egress_qos = opt.delete(:egress_qos) if opt.has_key?(:egress_qos)
|
||||
li.data.ingress_qos = opt.delete(:ingress_qos) if opt.has_key?(:ingress_qos)
|
||||
opt
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end # module Linux
|
Reference in New Issue
Block a user