Skip to content

Commit

Permalink
lib.protocol.ethernet: Add n_mcast, branch-free Multicast predicate.
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeneia committed May 13, 2016
1 parent aca8064 commit c186591
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
16 changes: 6 additions & 10 deletions src/apps/vhost/vhost_user.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions src/lib/protocol/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion src/lib/protocol/ethernet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c186591

Please sign in to comment.