1 ------------------------------------------------------------------------
 
    3 --- @brief (PROTO) utility.
 
    4 --- Utility functions for the PROTO_header structs 
 
    5 --- defined in \ref headers.lua . \
n 
    8 --- - PROTO header utility
 
    9 --- - Definition of PROTO packets
 
   10 ------------------------------------------------------------------------
 
   13 -- Use this file as template when implementing a 
new protocol (to implement all mandatory stuff)
 
   14 -- Replace all occurrences of PROTO with your protocol (e.g. sctp)
 
   15 -- Remove unnecessary comments in this file (comments inbetween [[...]])
 
   16 -- Necessary changes to other files:
 
   17 -- - headers.lua: 
add the header struct (and other structs, e.g. address), named PROTO_header
 
   18 -- - packet.lua: if the header has a length member, adapt 
packetSetLength; 
 
   20 -- - proto/proto.lua: 
add PROTO.lua to the list so it gets loaded
 
   23 local 
pkt = require "packet"
 
   28 ---------------------------------------------------------------------------
 
   30 ---------------------------------------------------------------------------
 
   32 --- PROTO protocol constants
 
   36 ---------------------------------------------------------------------------
 
   38 ---------------------------------------------------------------------------
 
   40 --- Module 
for PROTO_address 
struct (see \ref headers.lua).
 
   41 local PROTOHeader = {}
 
   42 PROTOHeader.__index = PROTOHeader
 
   45 -- 
for set also specify a suitable 
default value
 
   48 --- @param 
int XYZ of the PROTO header as A bit integer.
 
   49 function PROTOHeader:setXYZ(
int)
 
   54 --- @return XYZ as A bit integer.
 
   55 function PROTOHeader:getXYZ()
 
   59 --- Retrieve the XYZ as 
string.
 
   60 --- @return XYZ as 
string.
 
   61 function PROTOHeader:getXYZString()
 
   65 --- Set all members of the PROTO header.
 
   66 --- Per default, all members are 
set to default values specified in the respective 
set function.
 
   67 --- Optional named arguments can be used to 
set a member to a user-provided value.
 
   68 --- @param args Table of named arguments. Available arguments: PROTOXYZ
 
   69 --- @param pre prefix for namedArgs. Default 'PROTO'.
 
   71 --- 
fill() -- only default values
 
   72 --- 
fill{ PROTOXYZ=1 } -- all members are 
set to 
default values with the exception of PROTOXYZ, ...
 
   74 function PROTOHeader:
fill(args, pre)
 
   78     self:setXYZ(args[pre .. 
"PROTOXYZ"])
 
   81 --- Retrieve the values of all members.
 
   82 --- @param pre prefix 
for namedArgs. Default 
'PROTO'.
 
   83 --- @
return Table of named arguments. For a list of arguments see 
"See also".
 
   84 --- @see PROTOHeader:
fill 
   85 function PROTOHeader:
get(pre)
 
   89     args[pre .. 
"PROTOXYZ"] = 
self:getXYZ() 
 
   94 --- Retrieve the values of all members.
 
   95 --- @return Values in 
string format.
 
   97     return "PROTO " .. self:getXYZString()
 
  100 --- Resolve which header comes after this one (in a packet)
 
  101 --- For instance: in tcp/
udp based on the ports
 
  102 --- This function must exist and is only used when get/
dump is executed on 
 
  103 --- an unknown (mbuf not yet casted to e.g. tcpv6 packet) packet (mbuf)
 
  104 --- @return String next header (e.g. '
eth', 'ip4', nil)
 
  109 --- Change the default values for namedArguments (for 
fill/get)
 
  110 --- This can be used to for instance calculate a length value based on the total packet length
 
  112 --- This function must exist and is only used by packet.
fill 
  113 --- @param pre The prefix used for the namedArgs, e.g. 'PROTO'
 
  114 --- @param namedArgs Table of named arguments (see See more)
 
  115 --- @param nextHeader The header following after this header in a packet
 
  116 --- @param accumulatedLength The so far accumulated length for previous headers in a packet
 
  117 --- @return Table of namedArgs
 
  118 --- @see PROTOHeader:
fill 
  123 ----------------------------------------------------------------------------------
 
  125 ----------------------------------------------------------------------------------
 
  127 --[[ define how a packet with this header looks like
 
  128 -- e.g. 'ip4' will 
add a member ip4 of type struct ip4_header to the packet
 
  129 -- e.g. {
'ip4', 
'innerIP'} will 
add a member innerIP of type 
struct ip4_header to the packet
 
  131 --- Cast the packet to a PROTO (IP4) packet 
 
  135 ------------------------------------------------------------------------
 
  137 ------------------------------------------------------------------------
 
  139 ffi.metatype(
"struct PROTO_header", PROTOHeader)
 
local ffi
low-level dpdk wrapper 
 
function checksum(data, len)
Calculate a 16 bit checksum. 
 
function packetCreate(...)
Create struct and functions for a new packet. 
 
function ipsecICV getString(doByteSwap)
Get the IPsec string. 
 
function pkt dump(bytes)
Dumps the packet data cast to the best fitting packet struct. 
 
function ipsecICV set(icv)
Set the IPsec ICV. 
 
local udp
Udp protocol constants. 
 
function ahHeader fill(args, pre)
Set all members of the ah header. 
 
function ahHeader resolveNextHeader()
Resolve which header comes after this one (in a packet) For instance: in tcp/udp based on the ports T...
 
function ip4Addr add(val)
Add a number to an IPv4 address in-place. 
 
local eth
Ethernet protocol constants. 
 
function packetCalculateChecksums(args)
Calculate all checksums manually (not offloading them). 
 
local pkt
Module for packets (rte_mbuf) 
 
n
Create a new array of memory buffers (initialized to nil). 
 
function ahHeader setDefaultNamedArgs(pre, namedArgs, nextHeader, accumulatedLength)
Change the default values for namedArguments (for fill/get) This can be used to for instance calculat...
 
function packetSetLength(args)
Set length for all headers.