Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes for #338 #339

Merged
merged 5 commits into from
Jan 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/apps/ipv6/ipv6.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ local config = require("core.config")
local lib = require("core.lib")
local packet = require("core.packet")
local buffer = require("core.buffer")
local ethernet = require("lib.protocol.ethernet")
local ipv6 = require("lib.protocol.ipv6")
local pcap = require("apps.pcap.pcap")
local Buzz = require("apps.basic.basic_apps").Buzz

Expand Down Expand Up @@ -61,9 +63,15 @@ struct {
SimpleIPv6 = {}

function SimpleIPv6:new (arg)
local conf = config and config.parse_app_arg(arg) or {}
own_mac = conf.own_mac or "\x52\x54\x00\x12\x34\x57"
own_ip = conf.own_ip or "\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"
local conf = arg and config.parse_app_arg(arg) or {}
if type(conf.own_mac) == "string" then
conf.own_mac = ethernet:pton(conf.own_mac)
end
own_mac = conf.own_mac or "52:54:00:12:34:57"
if type(conf.own_ip) == "string" then
conf.own_ip = ipv6:pton(conf.own_ip)
end
own_ip = conf.own_ip or "2::1"
local o = {own_mac = own_mac, own_ip = own_ip}
return setmetatable(o, {__index = SimpleIPv6})
end
Expand Down Expand Up @@ -178,13 +186,13 @@ end

function selftest ()
print("selftest: ipv6")
local own_ip = "\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"
local own_mac = "\x52\x54\x00\x12\x34\x57"
local own_ip = "2::1"
local own_mac = "52:54:00:12:34:57"
local c = config.new()
config.app(c, "source", pcap.PcapReader, "apps/ipv6/selftest.cap.input")
config.app(c, "ipv6", SimpleIPv6,
{ own_ip = "\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",
own_mac = "\x52\x54\x00\x12\x34\x57" })
{ own_ip = "2::1",
own_mac = "52:54:00:12:34:57" })
config.app(c, "sink", pcap.PcapWriter, "apps/ipv6/selftest.cap.output")
config.link(c, "source.output -> ipv6.eth0")
config.link(c, "ipv6.eth0 -> sink.input")
Expand Down
56 changes: 28 additions & 28 deletions src/apps/ipv6/nd_light.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ local ffi = require("ffi")
local C = ffi.C
local app = require("core.app")
local link = require("core.link")
local config = require("core.config")
local packet = require("core.packet")
local datagram = require("lib.protocol.datagram")
local ethernet = require("lib.protocol.ethernet")
Expand All @@ -43,7 +44,7 @@ local tlv = require("lib.protocol.icmp.nd.options.tlv")
local filter = require("lib.pcap.filter")
local timer = require("core.timer")

local nd_light = subClass(nil)
nd_light = subClass(nil)
nd_light._name = "Partial IPv6 neighbor discovery"

-- config:
Expand Down Expand Up @@ -74,27 +75,28 @@ local function check_ip_address(ip, desc)
return ip
end

function nd_light:new (config)
function nd_light:new (arg)
local conf = arg and config.parse_app_arg(arg) or {}
local o = nd_light:superClass().new(self)
config.delay = config.delay or 1000
config.retrans = config.retrans or 10
assert(config.local_mac, "nd_light: missing local MAC address")
if type(config.local_mac) == "string" and string.len(config.local_mac) ~= 6 then
config.local_mac = ethernet:pton(config.local_mac)
conf.delay = conf.delay or 1000
conf.retrans = conf.retrans or 10
assert(conf.local_mac, "nd_light: missing local MAC address")
if type(conf.local_mac) == "string" and string.len(conf.local_mac) ~= 6 then
conf.local_mac = ethernet:pton(conf.local_mac)
else
assert(type(config.local_mac) == "cdata",
assert(type(conf.local_mac) == "cdata",
"nd_light: invalid type for local MAC address, expected cdata, got "
..type(config.local_mac))
..type(conf.local_mac))
end
config.local_ip = check_ip_address(config.local_ip, "local")
config.next_hop = check_ip_address(config.next_hop, "next-hop")
conf.local_ip = check_ip_address(conf.local_ip, "local")
conf.next_hop = check_ip_address(conf.next_hop, "next-hop")

o._config = config
o._config = conf
o._match_ns = function(ns)
return(ns:target_eq(config.local_ip))
return(ns:target_eq(conf.local_ip))
end
o._match_na = function(na)
return(na:target_eq(config.next_hop) and na:solicited())
return(na:target_eq(conf.next_hop) and na:solicited())
end
local errmsg
o._filter, errmsg = filter:new("icmp6 and ( ip6[40] = 135 or ip6[40] = 136 )")
Expand All @@ -105,17 +107,17 @@ function nd_light:new (config)
local dgram = datagram:new()
nh.packet = dgram:packet()
packet.tenure(nh.packet)
local sol_node_mcast = ipv6:solicited_node_mcast(config.next_hop)
local sol_node_mcast = ipv6:solicited_node_mcast(conf.next_hop)
local ipv6 = ipv6:new({ next_header = 58, -- ICMP6
hop_limit = 255,
src = config.local_ip,
src = conf.local_ip,
dst = sol_node_mcast })
local icmp = icmp:new(135, 0)

-- Construct a neighbor solicitation with a source link-layer
-- option.
local ns = ns:new(config.next_hop)
local src_lladdr_tlv = tlv:new(1, config.local_mac):tlv()
local ns = ns:new(conf.next_hop)
local src_lladdr_tlv = tlv:new(1, conf.local_mac):tlv()
local src_lladdr_tlv_len = ffi.sizeof(src_lladdr_tlv)
-- We add both chunks to the payload rather than using push() for
-- the ns header to have everything in a contiguous block for
Expand All @@ -126,7 +128,7 @@ function nd_light:new (config)
dgram:push(icmp)
ipv6:payload_length(icmp:sizeof() + ns:sizeof() + src_lladdr_tlv_len)
dgram:push(ipv6)
dgram:push(ethernet:new({ src = config.local_mac,
dgram:push(ethernet:new({ src = conf.local_mac,
dst = ethernet:ipv6_mcast(sol_node_mcast),
type = 0x86dd }))
dgram:free()
Expand All @@ -135,18 +137,18 @@ function nd_light:new (config)
nh.timer_cb = function (t)
local nh = o._next_hop
print(string.format("Sending neighbor solicitation for next-hop %s",
ipv6:ntop(config.next_hop)))
ipv6:ntop(conf.next_hop)))
link.transmit(o.output.south, nh.packet)
nh.nsent = nh.nsent + 1
if nh.nsent <= o._config.retrans and not o._eth_header then
timer.activate(nh.timer)
end
if nh.nsent > o._config.retrans then
error(string.format("ND for next hop %s has failed",
ipv6:ntop(config.next_hop)))
ipv6:ntop(conf.next_hop)))
end
end
nh.timer = timer.new("ns retransmit", nh.timer_cb, 1e6 * config.delay)
nh.timer = timer.new("ns retransmit", nh.timer_cb, 1e6 * conf.delay)
o._next_hop = nh

-- Prepare packet for solicited neighbor advertisement
Expand All @@ -158,12 +160,12 @@ function nd_light:new (config)
-- the incoming solicitation
ipv6 = ipv6:new({ next_header = 58, -- ICMP6
hop_limit = 255,
src = config.local_ip })
src = conf.local_ip })
icmp = icmp:new(136, 0)
-- Construct a neighbor solicitation with a target link-layer
-- option.
local na = na:new(config.local_ip, nil, 1, nil)
local tgt_lladdr_tlv = tlv:new(2, config.local_mac):tlv()
local na = na:new(conf.local_ip, nil, 1, nil)
local tgt_lladdr_tlv = tlv:new(2, conf.local_mac):tlv()
local tgt_lladdr_tlv_len = ffi.sizeof(tgt_lladdr_tlv)
dgram:payload(na:header(), na:sizeof())
local mem, length = dgram:payload(tgt_lladdr_tlv, tgt_lladdr_tlv_len)
Expand All @@ -172,7 +174,7 @@ function nd_light:new (config)
ipv6:payload_length(icmp:sizeof() + na:sizeof() + tgt_lladdr_tlv_len)
dgram:push(ipv6, true)
-- Leave dst address unspecified.
local eth = ethernet:new({ src = config.local_mac,
local eth = ethernet:new({ src = conf.local_mac,
type = 0x86dd })
dgram:push(eth, true)
-- These headers are relocated to the packet buffer in order to be
Expand Down Expand Up @@ -299,5 +301,3 @@ function nd_light:push ()
end
end
end

return nd_light
2 changes: 0 additions & 2 deletions src/apps/ipv6/ns_responder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,3 @@ function ns_responder:push()
end
end
end

return ns_responder
2 changes: 0 additions & 2 deletions src/apps/socket/raw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,3 @@ function RawSocket.selftest ()
-- pings to 127.0.0.1 and ::1 into lo and capture/compare
-- the responses with a pre-recorded pcap.
end

return RawSocket
2 changes: 0 additions & 2 deletions src/apps/vhost/raw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ end
function RawVhost.selftest ()
print("lib.vhost.raw selftest not implemented")
end

return RawVhost
36 changes: 23 additions & 13 deletions src/apps/vpn/vpws.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local C = ffi.C
local lib = require("core.lib")
local app = require("core.app")
local link = require("core.link")
local config = require("core.config")
local datagram = require("lib.protocol.datagram")
local ethernet = require("lib.protocol.ethernet")
local ipv6 = require("lib.protocol.ipv6")
Expand All @@ -23,26 +24,39 @@ local pcap = require("apps.pcap.pcap")
local vpws = subClass(nil)
local in_to_out = { customer = 'uplink', uplink = 'customer' }

function vpws:new(config)
function vpws:new(arg)
local conf = arg and config.parse_app_arg(arg) or {}
-- Parse MAC and IP addresses if necessary.
for _, key in ipairs({'local_mac', 'remote_mac'}) do
if type(conf[key]) == "string" then
conf[key] = ethernet:pton(conf[key])
end
end
for _, key in ipairs({'local_vpn_ip', 'remote_vpn_ip'}) do
if type(conf[key]) == "string" then
conf[key] = ipv6:pton(conf[key])
end
end

local o = vpws:superClass().new(self)
o._config = config
o._name = config.name
o._config = conf
o._name = conf.name
o._encap = {
ipv6 = ipv6:new({ next_header = 47, hop_limit = 64, src = config.local_vpn_ip,
dst = config.remote_vpn_ip}),
gre = gre:new({ protocol = 0x6558, checksum = config.checksum, key = config.label })
ipv6 = ipv6:new({ next_header = 47, hop_limit = 64, src = conf.local_vpn_ip,
dst = conf.remote_vpn_ip}),
gre = gre:new({ protocol = 0x6558, checksum = conf.checksum, key = conf.label })
}
-- Use a dummy value for the destination MAC address in case it is
-- omitted from the configuration. In this case, the app needs to
-- be connected to something that performs address resolution and
-- overwrites the Ethernet header (e.g. the nd_light app)
-- accordinly.
o._encap.ether = ethernet:new({ src = config.local_mac,
dst = config.remote_mac or ethernet:pton('00:00:00:00:00:00'),
o._encap.ether = ethernet:new({ src = conf.local_mac,
dst = conf.remote_mac or ethernet:pton('00:00:00:00:00:00'),
type = 0x86dd })
-- Pre-computed size of combined Ethernet and IPv6 header
o._eth_ipv6_size = ethernet:sizeof() + ipv6:sizeof()
local program = "ip6 and dst host "..ipv6:ntop(config.local_vpn_ip) .." and ip6 proto 47"
local program = "ip6 and dst host "..ipv6:ntop(conf.local_vpn_ip) .." and ip6 proto 47"
local filter, errmsg = filter:new(program)
assert(filter, errmsg and ffi.string(errmsg))
o._filter = filter
Expand Down Expand Up @@ -151,7 +165,3 @@ function selftest()
os.exit(1)
end
end

vpws.selftest = selftest

return vpws
2 changes: 1 addition & 1 deletion src/lib/nfv/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local Intel82599 = require("apps.intel.intel_app").Intel82599
local VhostUser = require("apps.vhost.vhost_user").VhostUser
local PacketFilter = require("apps.packet_filter.packet_filter").PacketFilter
local RateLimiter = require("apps.rate_limiter.rate_limiter").RateLimiter
local nd_light = require("apps.ipv6.nd_light")
local nd_light = require("apps.ipv6.nd_light").nd_light
local L2TPv3 = require("apps.keyed_ipv6_tunnel.tunnel").SimpleKeyedTunnel
local ffi = require("ffi")
local C = ffi.C
Expand Down
15 changes: 9 additions & 6 deletions src/lib/protocol/datagram.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ function datagram:parse_match (class, check)
local class = class or parse.ulp
local iovec = self._packet[0].iovecs[parse.iovec]

if not parse.ulp or (class and class ~= parse.ulp) then
if not class then
return nil
end
local proto = parse.ulp:new_from_mem(iovec.buffer.pointer + iovec.offset
+ parse.offset, iovec.length - parse.offset)
local proto = class:new_from_mem(iovec.buffer.pointer + iovec.offset
+ parse.offset, iovec.length - parse.offset)
if proto == nil or (check and not check(proto)) then
if proto then proto:free() end
return nil
Expand Down Expand Up @@ -302,16 +302,19 @@ function datagram:payload (mem, size)
local parse = self._parse
local iovec = self._packet[0].iovecs[parse.iovec]
local payload = iovec.buffer.pointer + iovec.offset + parse.offset
local multi_buffer_p = parse.iovec ~= self._packet[0].niovecs - 1
if mem ~= nil then
assert(size <= iovec.buffer.size - (iovec.offset + iovec.length),
"not enough space in buffer to add payload of size "..size)
"not enough space in buffer to add payload")
assert(not multi_buffer_p,
"can not add payload to multi-buffer packet")
ffi.copy(iovec.buffer.pointer + iovec.offset + iovec.length,
mem, size)
mem, size)
iovec.length = iovec.length + size
self._packet[0].length = self._packet[0].length + size
end
local p_size = iovec.length - parse.offset
return payload, p_size, (parse.iovec == self._packet[0].niovecs - 1)
return payload, p_size, (multi_buffer_p and parse.iovec + 1) or nil
end

return datagram
4 changes: 2 additions & 2 deletions src/lib/protocol/udp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ udp._ulp = { method = nil }

function udp:new (config)
local o = udp:superClass().new(self)
o:src_por(tconfig.src_port)
o:src_port(config.src_port)
o:dst_port(config.dst_port)
o:length(0)
o:length(8)
o:header().checksum = 0
return o
end
Expand Down