MoonGen
 All Files Functions Variables Pages
packet.lua File Reference

Utility functions for packets (rte_mbuf). More...

Go to the source code of this file.

Functions

function pkt getTimestamp ()
 Retrieve the time stamp information. More...
 
function pkt hasTimestamp ()
 Check if the PKT_RX_IEEE1588_TMST flag is set. More...
 
function pkt setVlan (vlan, pcp, cfi)
 Offload VLAN tagging to the NIC for this packet.
 
function pkt getVlan ()
 Get the VLAN associated with a received packet.
 
function pkt setDelay (delay)
 Set the time to wait before the packet is sent for software rate-controlled send methods.
 
function pkt setRate (rate)
 
function pkt setSize (size)
 
function pkt get ()
 Returns the packet data cast to the best fitting packet struct. More...
 
function pkt dump (bytes)
 Dumps the packet data cast to the best fitting packet struct. More...
 
function pkt offloadIPSec (idx, sec_type, esp_mode)
 Use IPsec offloading. More...
 
function pkt setESPTrailerLength (len)
 Set the ESP trailer length. More...
 
function pkt offloadIPChecksum (ipv4, l2_len, l3_len)
 Instruct the NIC to calculate the IP checksum for this packet. More...
 
function pkt offloadUdpChecksum (ipv4, l2_len, l3_len)
 Instruct the NIC to calculate the IP and UDP checksum for this packet. More...
 
function pkt offloadTcpChecksum (ipv4, l2_len, l3_len)
 Instruct the NIC to calculate the IP and TCP checksum for this packet. More...
 
function pkt enableTimestamps ()
 
function packetCreate (...)
 Create struct and functions for a new packet. More...
 
function getHeaderMember (v)
 Get the name of the header and the name of the respective member of a packet. More...
 
function packetGetHeaders (self)
 Get all headers of a packet as list. More...
 
function packetGetHeader (self, h)
 Get the specified header of a packet (e.g. More...
 
function packetDump (self, bytes)
 Print a hex dump of a packet. More...
 
function packetFill (self, namedArgs)
 Set all members of all headers. More...
 
function packetGet (self)
 Retrieve the values of all members as list of named arguments. More...
 
function packetResolveLastHeader (self)
 Try to find out what the next header in the payload of this packet is. More...
 
function packetSetLength (args)
 Set length for all headers. More...
 
function packetCalculateChecksums (args)
 Calculate all checksums manually (not offloading them). More...
 
function packetMakeStruct (...)
 Creates a packet struct (cdata) consisting of different headers. More...
 

Variables

local pkt = {}
 Module for packets (rte_mbuf)
 
pkt packetStructs = {}
 Table that contains the names and args of all created packet structs.
 

Detailed Description

Utility functions for packets (rte_mbuf).

Includes:

  • General functions (timestamping, rate control, ...)
  • Offloading
  • Create packet types

Definition in file packet.lua.

Function Documentation

function pkt dump ( bytes  )

Dumps the packet data cast to the best fitting packet struct.

Parameters
bytesnumber of bytes to dump, optional
Examples:
l3-tcp-syn-flood.lua.
function pkt enableTimestamps ( )
Todo:
TODO docu
function pkt get ( )

Returns the packet data cast to the best fitting packet struct.

Starting with ethernet header.

Returns
packet data as cdata of best fitting packet
function getHeaderMember ( )

Get the name of the header and the name of the respective member of a packet.

Parameters
vEither the name of the header (then the member has the same name), or a table { header, member }
Returns
Name of the header
Name of the member
function pkt getTimestamp ( )

Retrieve the time stamp information.

Returns
The timestamp or nil if the packet was not time stamped.
function pkt hasTimestamp ( )

Check if the PKT_RX_IEEE1588_TMST flag is set.

Turns out that this flag is pretty pointless, it does not indicate if the packet was actually timestamped, just that it came from a queue/filter with timestamping enabled. You probably want to use device:hasTimestamp() and check the sequence number.

function pkt offloadIPChecksum ( ipv4  ,
l2_len  ,
l3_len   
)

Instruct the NIC to calculate the IP checksum for this packet.

Parameters
ipv4Boolean to decide whether the packet uses IPv4 (set to nil/true) or IPv6 (set to anything else). In case it is an IPv6 packet, do nothing (the header has no checksum).
l2_lenLength of the layer 2 header in bytes (default 14 bytes for ethernet).
l3_lenLength of the layer 3 header in bytes (default 20 bytes for IPv4).
function pkt offloadIPSec ( idx  ,
sec_type  ,
esp_mode   
)

Use IPsec offloading.

Parameters
idxSA_IDX to use
sec_typeIPSec type to use ("esp"/"ah")
esp_modeESP mode to use encrypt(1) or authenticate(0)
function pkt offloadTcpChecksum ( ipv4  ,
l2_len  ,
l3_len   
)

Instruct the NIC to calculate the IP and TCP checksum for this packet.

Parameters
ipv4Boolean to decide whether the packet uses IPv4 (set to nil/true) or IPv6 (set to anything else).
l2_lenLength of the layer 2 header in bytes (default 14 bytes for ethernet).
l3_lenLength of the layer 3 header in bytes (default 20 bytes for IPv4, 40 bytes for IPv6).
function pkt offloadUdpChecksum ( ipv4  ,
l2_len  ,
l3_len   
)

Instruct the NIC to calculate the IP and UDP checksum for this packet.

Parameters
ipv4Boolean to decide whether the packet uses IPv4 (set to nil/true) or IPv6 (set to anything else).
l2_lenLength of the layer 2 header in bytes (default 14 bytes for ethernet).
l3_lenLength of the layer 3 header in bytes (default 20 bytes for IPv4, 40 bytes for IPv6).
function packetCalculateChecksums ( args  )

Calculate all checksums manually (not offloading them).

There also exist functions to calculate the checksum of only one header. Naming convention: pkt:calculate<member>Checksum() (for all existing packets member = {Ip, Tcp, Udp, Icmp})

Note
Calculating checksums manually is extremely slow compared to offloading this task to the NIC (~65% performance loss at the moment)
Todo:
Manual calculation of udp and tcp checksums NYI
function packetCreate (   ...)

Create struct and functions for a new packet.

For implemented headers (see proto/) these packets are defined in the section 'Packet struct' of each protocol file

Parameters
argslist of keywords (see makeStruct)
Returns
returns the constructor/cast function for this packet
See Also
packetMakeStruct
function packetDump ( self  ,
bytes   
)

Print a hex dump of a packet.

Parameters
selfthe packet
bytesNumber of bytes to dump. If no size is specified the payload is truncated.
function packetFill ( self  ,
namedArgs   
)

Set all members of all headers.

Per default, all members are set to default values specified in the respective set function. Optional named arguments can be used to set a member to a user-provided value. The argument 'pktLength' can be used to automatically calculate and set the length member of headers (e.g. ip header).

fill() --- only default values
fill{ ethSrc="12:23:34:45:56:67", ipTTL=100 } --- all members are set to default values with the exception of ethSrc and ipTTL
fill{ pktLength=64 } --- only default values, length members of the headers are adjusted
Parameters
selfThe packet
argsTable of named arguments. For a list of available arguments see "See also"
Note
This function is slow. If you want to modify members of a header during a time critical section of your script use the respective setters.
function packetGet ( self  )

Retrieve the values of all members as list of named arguments.

Parameters
selfThe packet
Returns
Table of named arguments. For a list of arguments see "See also".
See Also
packetFill
function packetGetHeader ( self  ,
 
)

Get the specified header of a packet (e.g.

self.eth).

Parameters
selfthe packet (cdata)
hheader to be returned
Returns
The member of the packet
function packetGetHeaders ( self  )

Get all headers of a packet as list.

Parameters
selfThe packet
Returns
Table of members of the packet
function packetMakeStruct (   ...)

Creates a packet struct (cdata) consisting of different headers.

Simply list the headers in the order you want them to be in a packet. If you want the member to be named differently, use the following syntax: normal: <header> ; different membername: { <header>, <member> }. Supported keywords: eth, arp, ptp, ip, ip6, udp, tcp, icmp

makeStruct('eth', { 'ip4', 'ip' }, 'udp') --- creates an UDP packet struct
--- the ip4 member of the packet is named 'ip'

The name of the created (internal) struct looks as follows: struct HEADER1_MEMBER1__HEADER2_MEMBER2 ... Only the "</strong>" (double underscore) has the special meaning of "a new header starts with name everything until "_" (single underscore)>, followed by the member name <everything after "_" until next header starts (indicated by next __)>"

Parameters
argslist of keywords/tables of keyword-member pairs
Returns
name name of the struct
ctype ctype of the struct

function packetResolveLastHeader ( self  )

Try to find out what the next header in the payload of this packet is.

This function is only used for buf:get/buf:dump

Parameters
selfThe packet
function packetSetLength ( args  )

Set length for all headers.

Necessary when sending variable sized packets.

Parameters
selfThe packet
lengthLength of the packet. Value for respective length member of headers get calculated using this value.
function pkt setESPTrailerLength ( len  )

Set the ESP trailer length.

Parameters
lenESP Trailer length in bytes
function pkt setRate ( rate  )
Todo:
TODO docu
function pkt setSize ( size  )
Todo:
TODO does