From c186591e312fb90c4c2d847be01255fb7dba0c8b Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Mon, 25 Apr 2016 16:25:06 +0200 Subject: [PATCH] lib.protocol.ethernet: Add n_mcast, branch-free Multicast predicate. --- src/apps/vhost/vhost_user.lua | 16 ++++++---------- src/lib/protocol/README.md | 9 +++++++++ src/lib/protocol/ethernet.lua | 7 ++++++- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/apps/vhost/vhost_user.lua b/src/apps/vhost/vhost_user.lua index 96e82780eb..16936709bf 100644 --- a/src/apps/vhost/vhost_user.lua +++ b/src/apps/vhost/vhost_user.lua @@ -102,20 +102,16 @@ end function VhostUser:tx_callback (p) counter.add(self.counters['out-octets'], packet.length(p)) - if ethernet:is_mcast(packet.data(p)) then - counter.add(self.counters['out-multicast']) - else - counter.add(self.counters['out-unicast']) - end + local mcast = ethernet:n_mcast(packet.data(p)) + counter.add(self.counters['out-multicast'], mcast) + counter.add(self.counters['out-unicast'], 1 - mcast) end function VhostUser:rx_callback (p) counter.add(self.counters['in-octets'], packet.length(p)) - if ethernet:is_mcast(packet.data(p)) then - counter.add(self.counters['in-multicast']) - else - counter.add(self.counters['in-unicast']) - end + local mcast = ethernet:n_mcast(packet.data(p)) + counter.add(self.counters['in-multicast'], mcast) + counter.add(self.counters['in-unicast'], 1 - mcast) end function VhostUser:rxdrop_callback (p) diff --git a/src/lib/protocol/README.md b/src/lib/protocol/README.md index 6f239687d7..4a13483a4b 100644 --- a/src/lib/protocol/README.md +++ b/src/lib/protocol/README.md @@ -98,6 +98,15 @@ 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 0a55c20f1b..b5abed8d31 100644 --- a/src/lib/protocol/ethernet.lua +++ b/src/lib/protocol/ethernet.lua @@ -76,9 +76,14 @@ 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 band(addr[0], 0x01) ~= 0 + return ethernet:n_mcast(addr) ~= 0 end -- Instance methods