From 5488c68cb5afb243e5006f000c2f33556cf44031 Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Wed, 8 Jun 2016 17:44:10 +0200 Subject: [PATCH 1/7] lib.protocol.ethernet: add is_bcast and n_bcast. --- src/lib/protocol/README.md | 9 +++++++++ src/lib/protocol/ethernet.lua | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/lib/protocol/README.md b/src/lib/protocol/README.md index 4a13483a4b..a20093cf60 100644 --- a/src/lib/protocol/README.md +++ b/src/lib/protocol/README.md @@ -107,6 +107,15 @@ Returns a true value if *mac* address denotes a [Multicast address](https://en.w Returns 1 if *mac* address denotes a [Multicast address](https://en.wikipedia.org/wiki/Multicast_address#Ethernet) and 0 otherwise. +— Function **ethernet:is_bcast** *mac* + +Returns a true value if *mac* address denotes a [Broadcast address](https://en.wikipedia.org/wiki/Broadcast_address#Ethernet). + +— Function **ethernet:n_bcast** *mac* + +Returns 1 if *mac* address denotes a [Broadcast address](https://en.wikipedia.org/wiki/Broadcast_address#Ethernet) +and 0 otherwise. + — Function **ethernet:ipv6_mcast** *ip* Returns the MAC address for IPv6 multicast *ip* as defined by RFC2464, diff --git a/src/lib/protocol/ethernet.lua b/src/lib/protocol/ethernet.lua index b5abed8d31..2fcf3cf71d 100644 --- a/src/lib/protocol/ethernet.lua +++ b/src/lib/protocol/ethernet.lua @@ -86,6 +86,17 @@ function ethernet:is_mcast (addr) return ethernet:n_mcast(addr) ~= 0 end +-- Return 1 if MAC address is the broadcast address and 0 otherwise +local bcast_address = ethernet:pton("FF:FF:FF:FF:FF:FF") +function ethernet:n_bcast (addr) + return C.memcmp(addr, bcast_address, 6) == 0 +end + +-- Check whether a MAC address is the broadcast address +function ethernet:is_bcast (addr) + return ethernet:n_bcast(addr) ~= 0 +end + -- Instance methods function ethernet:src (a) From 9044c6f0a6f0cae2a2c5c46ff17e5dd1eeb33e94 Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Wed, 8 Jun 2016 17:44:43 +0200 Subject: [PATCH 2/7] apps.vhost.vhost_user: count rxbcast/txbcast. --- src/apps/vhost/vhost_user.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/apps/vhost/vhost_user.lua b/src/apps/vhost/vhost_user.lua index fe2711769e..85fd3198f0 100644 --- a/src/apps/vhost/vhost_user.lua +++ b/src/apps/vhost/vhost_user.lua @@ -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) @@ -108,12 +108,14 @@ 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))) + counter.add(self.counters.txbcast, ethernet:n_bcast(packet.data(p))) 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))) + counter.add(self.counters.rxbcast, ethernet:n_bcast(packet.data(p))) end function VhostUser:rxdrop_callback (p) From 5a0161230e305e37f74149c098b5705b20ea315e Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Wed, 8 Jun 2016 17:45:07 +0200 Subject: [PATCH 3/7] apps.tap.tap: add statistics counters. --- src/apps/tap/tap.lua | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/apps/tap/tap.lua b/src/apps/tap/tap.lua index 497399d11a..fe803a7b70 100644 --- a/src/apps/tap/tap.lua +++ b/src/apps/tap/tap.lua @@ -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") @@ -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") @@ -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 () @@ -49,6 +63,10 @@ function Tap:pull () end p.length = len link.transmit(l, p) + counter.add(self.counters.rxbytes, len) + counter.add(self.counters.rxpackets) + counter.add(self.counters.rxmcast, ethernet:n_mcast(p.data)) + counter.add(self.counters.rxbcast, ethernet:n_bcast(p.data)) end end @@ -66,6 +84,10 @@ 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) + counter.add(self.counters.txmcast, ethernet:n_mcast(p.data)) + counter.add(self.counters.txbcast, ethernet:n_bcast(p.data)) -- The write completed so dequeue it from the link and free the packet link.receive(l) packet.free(p) @@ -74,6 +96,8 @@ end function Tap:stop() self.sock:close() + -- delete counters + for name, _ in pairs(self.counters) do counter.delete(name) end end function selftest() From 6233d1532e00ecd6c399b4db57da96146daa6c3b Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Mon, 13 Jun 2016 18:21:55 +0200 Subject: [PATCH 4/7] core.packet: make max_payload public. --- src/README.md | 4 ++++ src/core/packet.lua | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/README.md b/src/README.md index 92ed940bed..1a5eb2a993 100644 --- a/src/README.md +++ b/src/README.md @@ -316,6 +316,10 @@ not been transmitted or freed. The number of allocatable packets is limited by the size of the underlying "freelist", e.g. a pool of unused packet objects from and to which packets are allocated and freed. +— Variable **packet.max_payload** + +Maximum payload length for packets. Read-only. + — Function **packet.allocate** Returns a new empty packet. An an error is raised if there are no packets diff --git a/src/core/packet.lua b/src/core/packet.lua index 6c4ed5311b..143bfdf920 100644 --- a/src/core/packet.lua +++ b/src/core/packet.lua @@ -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. From 788438e8898156796289a23afc7a597362cea377 Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Wed, 8 Jun 2016 17:45:18 +0200 Subject: [PATCH 5/7] apps.socket.raw: add statistics counters. --- src/apps/socket/raw.lua | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/apps/socket/raw.lua b/src/apps/socket/raw.lua index 24ad578d5d..cd0f9799fa 100644 --- a/src/apps/socket/raw.lua +++ b/src/apps/socket/raw.lua @@ -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 @@ -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) @@ -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 () @@ -61,10 +78,14 @@ 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) + counter.add(self.counters.rxmcast, ethernet:n_mcast(p.data)) + counter.add(self.counters.rxbcast, ethernet:n_bcast(p.data)) + return packet.clone(p) end function RawSocket:push () @@ -73,6 +94,10 @@ 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) + counter.add(self.counters.txmcast, ethernet:n_mcast(p.data)) + counter.add(self.counters.txbcast, ethernet:n_bcast(p.data)) packet.free(p) end end @@ -94,6 +119,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 () From e3fcbbfbe5d3c6a2ec41fb13e8d02cae2b53afc7 Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Mon, 4 Jul 2016 14:38:18 +0200 Subject: [PATCH 6/7] Revert "lib.protocol.ethernet: Add n_mcast, branch-free Multicast predicate." Reason: its actually slower than the initial naive version. This reverts commit c186591e312fb90c4c2d847be01255fb7dba0c8b. # Conflicts: # src/apps/vhost/vhost_user.lua --- src/apps/vhost/vhost_user.lua | 8 ++++++-- src/lib/protocol/README.md | 9 --------- src/lib/protocol/ethernet.lua | 7 +------ 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/apps/vhost/vhost_user.lua b/src/apps/vhost/vhost_user.lua index fe2711769e..0bc822bcae 100644 --- a/src/apps/vhost/vhost_user.lua +++ b/src/apps/vhost/vhost_user.lua @@ -107,13 +107,17 @@ 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 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 end function VhostUser:rxdrop_callback (p) diff --git a/src/lib/protocol/README.md b/src/lib/protocol/README.md index 4a13483a4b..6f239687d7 100644 --- a/src/lib/protocol/README.md +++ b/src/lib/protocol/README.md @@ -98,15 +98,6 @@ Returns the binary representation of MAC address denoted by *string*. Returns the string representation of *mac* address. -— Function **ethernet:is_mcast** *mac* - -Returns a true value if *mac* address denotes a [Multicast address](https://en.wikipedia.org/wiki/Multicast_address#Ethernet). - -— Function **ethernet:n_mcast** *mac* - -Returns 1 if *mac* address denotes a [Multicast address](https://en.wikipedia.org/wiki/Multicast_address#Ethernet) -and 0 otherwise. - — Function **ethernet:ipv6_mcast** *ip* Returns the MAC address for IPv6 multicast *ip* as defined by RFC2464, diff --git a/src/lib/protocol/ethernet.lua b/src/lib/protocol/ethernet.lua index b5abed8d31..0a55c20f1b 100644 --- a/src/lib/protocol/ethernet.lua +++ b/src/lib/protocol/ethernet.lua @@ -76,14 +76,9 @@ 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 -- Instance methods From b418f8f7f6fe1a1ed993186fe98a276b46c729e0 Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Mon, 4 Jul 2016 15:50:29 +0200 Subject: [PATCH 7/7] =?UTF-8?q?intel=5Fapp:=20fix=20wrong=20=E2=80=9Cspeed?= =?UTF-8?q?=E2=80=9D=20counter=20value.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apps/intel/intel_app.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/apps/intel/intel_app.lua b/src/apps/intel/intel_app.lua index 92f97d00fd..c85c2e5530 100644 --- a/src/apps/intel/intel_app.lua +++ b/src/apps/intel/intel_app.lua @@ -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,