MoonGen
 All Files Functions Variables Pages
newProtocolTemplate.lua
1 ------------------------------------------------------------------------
2 --- @file PROTO.lua
3 --- @brief (PROTO) utility.
4 --- Utility functions for the PROTO_header structs
5 --- defined in \ref headers.lua . \n
6 --- Includes:
7 --- - PROTO constants
8 --- - PROTO header utility
9 --- - Definition of PROTO packets
10 ------------------------------------------------------------------------
11 
12 --[[
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;
19 -- if the packet has a checksum, adapt packetCreate (loop at end of function) and packetCalculateChecksums
20 -- - proto/proto.lua: add PROTO.lua to the list so it gets loaded
21 --]]
22 local ffi = require "ffi"
23 local pkt = require "packet"
24 
25 require "headers"
26 
27 
28 ---------------------------------------------------------------------------
29 ---- PROTO constants
30 ---------------------------------------------------------------------------
31 
32 --- PROTO protocol constants
33 local PROTO = {}
34 
35 
36 ---------------------------------------------------------------------------
37 ---- PROTO header
38 ---------------------------------------------------------------------------
39 
40 --- Module for PROTO_address struct (see \ref headers.lua).
41 local PROTOHeader = {}
42 PROTOHeader.__index = PROTOHeader
43 
44 --[[ for all members of the header: set, get, getString
45 -- for set also specify a suitable default value
46 --]]
47 --- Set the XYZ.
48 --- @param int XYZ of the PROTO header as A bit integer.
49 function PROTOHeader:setXYZ(int)
50  int = int or 0
51 end
52 
53 --- Retrieve the XYZ.
54 --- @return XYZ as A bit integer.
55 function PROTOHeader:getXYZ()
56  return nil
57 end
58 
59 --- Retrieve the XYZ as string.
60 --- @return XYZ as string.
61 function PROTOHeader:getXYZString()
62  return nil
63 end
64 
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'.
70 --- @code
71 --- fill() -- only default values
72 --- fill{ PROTOXYZ=1 } -- all members are set to default values with the exception of PROTOXYZ, ...
73 --- @endcode
74 function PROTOHeader:fill(args, pre)
75  args = args or {}
76  pre = pre or "PROTO"
77 
78  self:setXYZ(args[pre .. "PROTOXYZ"])
79 end
80 
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)
86  pre = pre or "PROTO"
87 
88  local args = {}
89  args[pre .. "PROTOXYZ"] = self:getXYZ()
90 
91  return args
92 end
93 
94 --- Retrieve the values of all members.
95 --- @return Values in string format.
96 function PROTOHeader:getString()
97  return "PROTO " .. self:getXYZString()
98 end
99 
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)
105 function PROTOHeader:resolveNextHeader()
106  return nil
107 end
108 
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
111 --- See proto/ip4.setDefaultNamedArgs as an example
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
119 function PROTOHeader:setDefaultNamedArgs(pre, namedArgs, nextHeader, accumulatedLength)
120  return namedArgs
121 end
122 
123 ----------------------------------------------------------------------------------
124 ---- Packets
125 ----------------------------------------------------------------------------------
126 
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
130 --]]
131 --- Cast the packet to a PROTO (IP4) packet
132 pkt.getPROTOPacket = packetCreate('eth', 'ip4', 'PROTO')
133 
134 
135 ------------------------------------------------------------------------
136 ---- Metatypes
137 ------------------------------------------------------------------------
138 
139 ffi.metatype("struct PROTO_header", PROTOHeader)
140 
141 
142 return PROTO
local ffi
low-level dpdk wrapper
Definition: dpdkc.lua:6
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.
function mod new(n)
local udp
Udp protocol constants.
Definition: udp.lua:23
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.
Definition: ethernet.lua:24
function packetCalculateChecksums(args)
Calculate all checksums manually (not offloading them).
local pkt
Module for packets (rte_mbuf)
Definition: packet.lua:20
n
Create a new array of memory buffers (initialized to nil).
Definition: memory.lua:76
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.