Skip to content

Commit

Permalink
Merged snabbco#947 (RawSocket/TAP i/o stats counters)
Browse files Browse the repository at this point in the history
Fixed packet documentation merge conflict
  • Loading branch information
Katerina Barone-Adesi committed Jul 4, 2016
2 parents ffaa105 + b418f8f commit b31acfd
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/apps/intel/intel_app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function Intel82599:new (arg)
counter.set(self.stats.counters.type, 0x1000) -- Hardware interface
counter.set(self.stats.counters.dtime, C.get_unix_time())
counter.set(self.stats.counters.mtu, self.dev.mtu)
counter.set(self.stats.counters.speed, 10000000) -- 10 Gbits
counter.set(self.stats.counters.speed, 10000000000) -- 10 Gbits
counter.set(self.stats.counters.status, 2) -- down
if not conf.vmdq and conf.macaddr then
counter.set(self.stats.counters.macaddr,
Expand Down
46 changes: 41 additions & 5 deletions src/apps/socket/raw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ local h = require("syscall.helpers")
local bit = require("bit")
local link = require("core.link")
local packet = require("core.packet")
local counter = require("core.counter")
local ethernet = require("lib.protocol.ethernet")
local ffi = require("ffi")
local C = ffi.C

Expand All @@ -21,6 +23,12 @@ local c, t = S.c, S.types.t

RawSocket = {}

local provided_counters = {
'type', 'dtime',
'rxbytes', 'rxpackets', 'rxmcast', 'rxbcast',
'txbytes', 'txpackets', 'txmcast', 'txbcast'
}

function RawSocket:new (ifname)
assert(ifname)
local index, err = S.util.if_nametoindex(ifname)
Expand All @@ -40,7 +48,16 @@ function RawSocket:new (ifname)
sock:close()
error(err)
end
return setmetatable({sock = sock}, {__index = RawSocket})
local counters = {}
for _, name in ipairs(provided_counters) do
counters[name] = counter.open(name)
end
counter.set(counters.type, 0x1001) -- Virtual interface
counter.set(counters.dtime, C.get_unix_time())
return setmetatable({sock = sock,
rx_p = packet.allocate(),
counters = counters},
{__index = RawSocket})
end

function RawSocket:pull ()
Expand All @@ -61,10 +78,18 @@ function RawSocket:can_receive ()
end

function RawSocket:receive ()
local buffer = ffi.new("uint8_t[?]", C.PACKET_PAYLOAD_SIZE)
local sz, err = S.read(self.sock, buffer, C.PACKET_PAYLOAD_SIZE)
assert(sz, err)
return packet.from_pointer(buffer, sz)
local p = self.rx_p
local sz = assert(S.read(self.sock, p.data, packet.max_payload))
p.length = sz
counter.add(self.counters.rxbytes, sz)
counter.add(self.counters.rxpackets)
if ethernet:is_mcast(p.data) then
counter.add(self.counters.rxmcast)
end
if ethernet:is_bcast(p.data) then
counter.add(self.counters.rxbcast)
end
return packet.clone(p)
end

function RawSocket:push ()
Expand All @@ -73,6 +98,14 @@ function RawSocket:push ()
while not link.empty(l) and self:can_transmit() do
local p = link.receive(l)
self:transmit(p)
counter.add(self.counters.txbytes, p.length)
counter.add(self.counters.txpackets)
if ethernet:is_mcast(p.data) then
counter.add(self.counters.txmcast)
end
if ethernet:is_bcast(p.data) then
counter.add(self.counters.txbcast)
end
packet.free(p)
end
end
Expand All @@ -94,6 +127,9 @@ end

function RawSocket:stop()
self.sock:close()
packet.free(self.rx_p)
-- delete counters
for name, _ in pairs(self.counters) do counter.delete(name) end
end

function selftest ()
Expand Down
36 changes: 34 additions & 2 deletions src/apps/tap/tap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module(..., package.seeall)
local S = require("syscall")
local link = require("core.link")
local packet = require("core.packet")
local counter = require("core.counter")
local ethernet = require("lib.protocol.ethernet")
local ffi = require("ffi")
local C = ffi.C
local const = require("syscall.linux.constants")
Expand All @@ -14,6 +16,12 @@ local t = S.types.t

Tap = { }

local provided_counters = {
'type', 'dtime',
'rxbytes', 'rxpackets', 'rxmcast', 'rxbcast',
'txbytes', 'txpackets', 'txmcast', 'txbcast'
}

function Tap:new (name)
assert(name, "missing tap interface name")

Expand All @@ -27,8 +35,14 @@ function Tap:new (name)
sock:close()
error("Error opening /dev/net/tun: " .. tostring(err))
end

return setmetatable({sock = sock, name = name}, {__index = Tap})
local counters = {}
for _, name in ipairs(provided_counters) do
counters[name] = counter.open(name)
end
counter.set(counters.type, 0x1001) -- Virtual interface
counter.set(counters.dtime, C.get_unix_time())
return setmetatable({sock = sock, name = name, counters = counters},
{__index = Tap})
end

function Tap:pull ()
Expand All @@ -49,6 +63,14 @@ function Tap:pull ()
end
p.length = len
link.transmit(l, p)
counter.add(self.counters.rxbytes, len)
counter.add(self.counters.rxpackets)
if ethernet:is_mcast(p.data) then
counter.add(self.counters.rxmcast)
end
if ethernet:is_bcast(p.data) then
counter.add(self.counters.rxbcast)
end
end
end

Expand All @@ -66,6 +88,14 @@ function Tap:push ()
if len ~= p.length and err.errno == const.E.AGAIN then
return
end
counter.add(self.counters.txbytes, len)
counter.add(self.counters.txpackets)
if ethernet:is_mcast(p.data) then
counter.add(self.counters.txmcast)
end
if ethernet:is_bcast(p.data) then
counter.add(self.counters.txbcast)
end
-- The write completed so dequeue it from the link and free the packet
link.receive(l)
packet.free(p)
Expand All @@ -74,6 +104,8 @@ end

function Tap:stop()
self.sock:close()
-- delete counters
for name, _ in pairs(self.counters) do counter.delete(name) end
end

function selftest()
Expand Down
18 changes: 14 additions & 4 deletions src/apps/vhost/vhost_user.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ VhostUser = {}

local provided_counters = {
'type', 'dtime',
'rxbytes', 'rxpackets', 'rxmcast', 'rxdrop',
'txbytes', 'txpackets', 'txmcast'
'rxbytes', 'rxpackets', 'rxmcast', 'rxbcast', 'rxdrop',
'txbytes', 'txpackets', 'txmcast', 'txbcast'
}

function VhostUser:new (args)
Expand Down Expand Up @@ -107,13 +107,23 @@ end
function VhostUser:tx_callback (p)
counter.add(self.counters.txbytes, packet.length(p))
counter.add(self.counters.txpackets)
counter.add(self.counters.txmcast, ethernet:n_mcast(packet.data(p)))
if ethernet:is_mcast(packet.data(p)) then
counter.add(self.counters.txmcast)
end
if ethernet:is_bcast(packet.data(p)) then
counter.add(self.counters.txbcast)
end
end

function VhostUser:rx_callback (p)
counter.add(self.counters.rxbytes, packet.length(p))
counter.add(self.counters.rxpackets)
counter.add(self.counters.rxmcast, ethernet:n_mcast(packet.data(p)))
if ethernet:is_mcast(packet.data(p)) then
counter.add(self.counters.rxmcast)
end
if ethernet:is_bcast(packet.data(p)) then
counter.add(self.counters.rxbcast)
end
end

function VhostUser:rxdrop_callback (p)
Expand Down
2 changes: 1 addition & 1 deletion src/core/packet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local packet_t = ffi.typeof("struct packet")
local packet_ptr_t = ffi.typeof("struct packet *")
local packet_size = ffi.sizeof(packet_t)
local header_size = 8
local max_payload = tonumber(C.PACKET_PAYLOAD_SIZE)
max_payload = tonumber(C.PACKET_PAYLOAD_SIZE)

-- Freelist containing empty packets ready for use.

Expand Down
5 changes: 2 additions & 3 deletions src/lib/protocol/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,9 @@ Returns the string representation of *mac* address.

Returns a true value if *mac* address denotes a [Multicast address](https://en.wikipedia.org/wiki/Multicast_address#Ethernet).

— Function **ethernet:n_mcast** *mac*
— Function **ethernet:is_bcast** *mac*

Returns 1 if *mac* address denotes a [Multicast address](https://en.wikipedia.org/wiki/Multicast_address#Ethernet)
and 0 otherwise.
Returns a true value if *mac* address denotes a [Broadcast address](https://en.wikipedia.org/wiki/Broadcast_address#Ethernet).

— Function **ethernet:ipv6_mcast** *ip*

Expand Down
13 changes: 7 additions & 6 deletions src/lib/protocol/ethernet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ function ethernet:ipv6_mcast(ip)
return result
end

-- Return 1 if MAC address has its group bit set and 0 otherwise
function ethernet:n_mcast (addr)
return band(addr[0], 0x01)
end

-- Check whether a MAC address has its group bit set
function ethernet:is_mcast (addr)
return ethernet:n_mcast(addr) ~= 0
return band(addr[0], 0x01) ~= 0
end

local bcast_address = ethernet:pton("FF:FF:FF:FF:FF:FF")
-- Check whether a MAC address is the broadcast address
function ethernet:is_bcast (addr)
return C.memcmp(addr, bcast_address, 6) == 0
end

-- Instance methods
Expand Down

0 comments on commit b31acfd

Please sign in to comment.