1 ------------------------------------------------------------------------
3 --- @brief Ethernet protocol utility.
4 --- Utility functions
for the mac_address and ethernet_header structs
5 --- defined in \ref headers.lua .
\n
7 --- - Ethernet constants
8 --- - Mac address utility
9 --- - Ethernet header utility
10 --- - Definition of ethernet packets
11 ------------------------------------------------------------------------
13 local
ffi = require
"ffi"
14 local
pkt = require
"packet"
19 local ntoh, hton = ntoh, hton
20 local ntoh16, hton16 = ntoh16, hton16
21 local
bor,
band,
bnot, rshift, lshift= bit.bor, bit.band, bit.bnot, bit.rshift, bit.lshift
22 local istype =
ffi.istype
23 local format =
string.format
26 ------------------------------------------------------------------------
27 ---- Ethernet constants
28 ------------------------------------------------------------------------
30 --- Ethernet protocol constants
42 --- Ethernet broadcast address
43 eth.BROADCAST =
"ff:ff:ff:ff:ff:ff"
46 ------------------------------------------------------------------------
48 ------------------------------------------------------------------------
50 --- Module
for mac_address
struct (see \ref headers.lua).
53 local macAddrType =
ffi.typeof(
"struct mac_address")
55 --- Retrieve the MAC address.
56 --- @
return Address in
'struct mac_address' format.
58 local addr = macAddrType()
60 addr.uint8[i] =
self.uint8[i]
65 --- Set the MAC address.
66 --- @param addr Address in
'struct mac_address' format.
69 self.uint8[i] = addr.uint8[i]
73 --- Set the MAC address.
74 --- @param mac Address in
string format.
79 --- Test equality of two MAC addresses.
80 --- @param lhs Address in 'struct mac_address' format.
81 --- @param rhs Address in 'struct mac_address' format.
82 --- @return true if equal, false otherwise.
84 local isMAC = istype(macAddrType, lhs) and istype(macAddrType, rhs)
86 isMAC = isMAC and lhs.uint8[i] == rhs.uint8[i]
91 --- Retrieve the
string representation of a MAC address.
92 --- @return Address in
string format.
94 return ("%02x:%02x:%02x:%02x:%02x:%02x"):format(
95 self.uint8[0], self.uint8[1], self.uint8[2],
96 self.uint8[3], self.uint8[4], self.uint8[5]
101 ----------------------------------------------------------------------------
103 ----------------------------------------------------------------------------
105 --- Module for ethernet_header struct (see \ref headers.lua).
109 --- Set the destination MAC address.
110 --- @param addr Address in
'struct mac_address' format.
115 --- Retrieve the destination MAC address.
116 --- @return Address in 'struct mac_address' format.
118 return self.dst:
get()
121 --- Set the source MAC address.
122 --- @param addr Address in 'struct mac_address' format.
127 --- Retrieve the source MAC address.
128 --- @return Address in 'struct mac_address' format.
130 return self.src:
get()
133 --- Set the destination MAC address.
134 --- @param str Address in
string format.
139 --- Retrieve the destination MAC address.
140 --- @return Address in
string format.
145 --- Set the source MAC address.
146 --- @param str Address in
string format.
151 --- Retrieve the source MAC address.
152 --- @return Address in
string format.
157 --- Set the EtherType.
158 --- @param
int EtherType as 16 bit integer.
161 self.type = hton16(
int)
164 --- Retrieve the EtherType.
165 --- @return EtherType as 16 bit integer.
167 return hton16(self.type)
170 --- Retrieve the ether type.
171 --- @return EtherType as
string.
185 cleartext = "(unknown)"
188 return format("0x%04x %s", type, cleartext)
191 --- Set all members of the ethernet header.
192 --- Per default, all members are
set to default values specified in the respective
set function.
193 --- Optional named arguments can be used to
set a member to a user-provided value. \
n
194 --- Exemplary invocations:
196 ---
fill() --- only default values
197 ---
fill{ ethSrc=
"12:23:34:45:56:67", ethType=0x137 } ---
default value
for ethDst; ethSrc and ethType user-specified
199 --- @param args Table of named arguments. Available arguments: Src, Dst, Type
200 --- @param pre Prefix
for namedArgs. Default
'eth'.
205 local src = pre ..
"Src"
206 local dst = pre ..
"Dst"
207 args[src] = args[src] or
"01:02:03:04:05:06"
208 args[dst] = args[dst] or
"07:08:09:0a:0b:0c"
210 -- addresses can be either a string, a mac_address ctype or a device/queue
object
211 if type(args[src]) ==
"string" then
213 elseif istype(macAddrType, args[src]) then
215 elseif type(args[src]) ==
"table" and args[src].id then
216 self:
setSrcString((args[src].dev or args[src]):getMacString())
218 if type(args[dst]) == "
string" then
220 elseif istype(macAddrType, args[dst]) then
222 elseif type(args[dst]) == "table" and args[dst].
id then
223 self:
setDstString((args[dst].dev or args[dst]):getMacString())
225 self:
setType(args[pre .. "Type"])
228 --- Retrieve the values of all members.
229 --- @param pre Prefix for namedArgs. Default '
eth'.
230 --- @return Table of named arguments. For a list of arguments see "See Also".
238 args[pre .. "Type"] = self:
getType()
243 --- Retrieve the values of all members.
244 --- @return Values in
string format.
249 -- Maps headers to respective types.
250 -- This list should be extended whenever a
new type is added to 'Ethernet constants'.
251 local mapNameType = {
258 --- Resolve which header comes after
this one (in a packet).
259 --- For instance: in tcp/
udp based on the ports.
260 --- This
function must exist and is only used when
get/
dump is executed on
261 --- an unknown (mbuf not yet casted to e.g. tcpv6 packet) packet (mbuf)
262 --- @
return String next header (e.g.
'eth',
'ip4', nil)
265 for name, _type in pairs(mapNameType) do
266 if type == _type then
273 --- Change the default values for namedArguments (for
fill/
get).
274 --- This can be used to for instance calculate a length value based on the total packet length.
276 --- This function must exist and is only used by packet.
fill.
277 --- @param pre The prefix used for the namedArgs, e.g. '
eth'
278 --- @param namedArgs Table of named arguments (see See Also)
279 --- @param nextHeader The header following after this header in a packet
280 --- @param accumulatedLength The so far accumulated length for previous headers in a packet
281 --- @return Table of namedArgs
285 if not namedArgs[pre .. "Type"] then
286 for name, type in pairs(mapNameType) do
287 if nextHeader == name then
288 namedArgs[pre .. "Type"] = type
297 ----------------------------------------------------------------------------------
299 ----------------------------------------------------------------------------------
301 --- Cast the packet to an ethernet packet
307 ----------------------------------------------------------------------------------
309 ----------------------------------------------------------------------------------
function macAddr get()
Retrieve the MAC address.
function macAddr __eq(lhs, rhs)
Test equality of two MAC addresses.
function etherHeader setDst(addr)
Set the destination MAC address.
local ffi
low-level dpdk wrapper
function etherHeader setSrc(addr)
Set the source MAC address.
eth TYPE_ARP
EtherType for Arp.
function etherHeader getSrc(addr)
Retrieve the source MAC address.
function packetCreate(...)
Create struct and functions for a new packet.
function macAddr setString(mac)
Set the MAC address.
pkt getEthernetPacket
Cast the packet to an ethernet packet.
function mod band(mask1, mask2, result)
Bitwise and.
function pkt dump(bytes)
Dumps the packet data cast to the best fitting packet struct.
function etherHeader getTypeString()
Retrieve the ether type.
function macAddr getString()
Retrieve the string representation of a MAC address.
function etherHeader setDstString(str)
Set the destination MAC address.
eth TYPE_PTP
EtherType for Ptp.
function etherHeader setDefaultNamedArgs(pre, namedArgs, nextHeader, accumulatedLength)
Change the default values for namedArguments (for fill/get).
local udp
Udp protocol constants.
function mod bor(mask1, mask2, result)
Bitwise or.
local arp
Arp protocol constants.
function etherHeader fill(args, pre)
Set all members of the ethernet header.
function etherHeader getDstString()
Retrieve the destination MAC address.
local ptp
Ptp protocol constants.
pkt getEthPacket
Cast the packet to an ethernet packet (alias for pkt.getEthernetPacket)
local macAddr
Module for mac_address struct (see headers.lua).
local etherHeader
Module for ethernet_header struct (see headers.lua).
function etherHeader setType(int)
Set the EtherType.
function etherHeader getType()
Retrieve the EtherType.
eth TYPE_IP6
EtherType for IP6.
eth TYPE_IP
EtherType for IP4.
function etherHeader setSrcString(str)
Set the source MAC address.
local eth
Ethernet protocol constants.
local ip6
IP6 protocol constants.
function etherHeader resolveNextHeader()
Resolve which header comes after this one (in a packet).
local pkt
Module for packets (rte_mbuf)
function mod bnot(mask, result)
Bitwise not.
function etherHeader getDst(addr)
Retrieve the destination MAC address.
n
Create a new array of memory buffers (initialized to nil).
function macAddr set(addr)
Set the MAC address.
function parseMacAddress(mac)
Parse a string to a MAC address.
function etherHeader getSrcString()
Retrieve the source MAC address.