MoonGen
 All Files Functions Variables Pages
udp.lua
Go to the documentation of this file.
1 ------------------------------------------------------------------------
2 --- @file udp.lua
3 --- @brief User datagram protocol (UDP) utility.
4 --- Utility functions for udp_header struct
5 --- defined in \ref headers.lua . \n
6 --- Includes:
7 --- - Udp constants
8 --- - Udp header utility
9 --- - Definition of Udp packets
10 ------------------------------------------------------------------------
11 
12 local ffi = require "ffi"
13 local pkt = require "packet"
14 
15 require "utils"
16 require "headers"
17 
18 local ntoh, hton = ntoh, hton
19 local ntoh16, hton16 = ntoh16, hton16
20 local bor, band, bnot, rshift, lshift= bit.bor, bit.band, bit.bnot, bit.rshift, bit.lshift
21 local istype = ffi.istype
22 local format = string.format
23 
24 
25 ---------------------------------------------------------------------------
26 ---- UDP constants
27 ---------------------------------------------------------------------------
28 
29 --- Udp protocol constants
30 local udp = {}
31 
32 -- TODO move well-known ports somewhere else, those are not only Udp
33 
34 --- Well known port for Ptp event message
35 udp.PORT_PTP_EVENTS = 319
36 --- Well known port for Ptp general message
37 udp.PORT_PTP_GENERAL_MESSAGES = 320
38 
39 
40 ---------------------------------------------------------------------------
41 ---- UDP header
42 ---------------------------------------------------------------------------
43 
44 --- Module for udp_header struct (see \ref headers.lua).
45 local udpHeader = {}
46 udpHeader.__index = udpHeader
47 
48 --- Set the source port.
49 --- @param int Source port of the udp header as 16 bit integer.
50 function udpHeader:setSrcPort(int)
51  int = int or 1024
52  self.src = hton16(int)
53 end
54 
55 --- Retrieve the UDP source port.
56 --- @return Port as 16 bit integer.
57 function udpHeader:getSrcPort()
58  return hton16(self.src)
59 end
60 
61 --- Retrieve the UDP source port.
62 --- @return Port as string.
63 function udpHeader:getSrcPortString()
64  return self:getSrcPort()
65 end
66 
67 --- Set the destination port.
68 --- @param int Destination port of the udp header as 16 bit integer.
69 function udpHeader:setDstPort(int)
70  int = int or 1025
71  self.dst = hton16(int)
72 end
73 
74 --- Retrieve the UDP destination port.
75 --- @return Port as 16 bit integer.
76 function udpHeader:getDstPort()
77  return hton16(self.dst)
78 end
79 
80 --- Retrieve the UDP destination port.
81 --- @return Port as string.
82 function udpHeader:getDstPortString()
83  return self:getDstPort()
84 end
85 
86 --- Set the length.
87 --- @param int Length of the udp header plus payload (excluding l2 and l3). 16 bit integer.
88 function udpHeader:setLength(int)
89  int = int or 28 -- with ethernet + IPv4 header -> 64B
90  self.len = hton16(int)
91 end
92 
93 --- Retrieve the length.
94 --- @return Length as 16 bit integer.
95 function udpHeader:getLength()
96  return hton16(self.len)
97 end
98 
99 --- Retrieve the length.
100 --- @return Length as string.
101 function udpHeader:getLengthString()
102  return self:getLength()
103 end
104 
105 --- Set the checksum.
106 --- @param int Checksum of the udp header as 16 bit integer.
107 function udpHeader:setChecksum(int)
108  int = int or 0
109  self.cs = hton16(int)
110 end
111 
112 --- Calculate the checksum.
113 --- @todo FIXME NYI
114 function udpHeader:calculateChecksum(len)
115 end
116 
117 --- Retrieve the checksum.
118 --- @return Checksum as 16 bit integer.
119 function udpHeader:getChecksum()
120  return hton16(self.cs)
121 end
122 
123 --- Retrieve the checksum.
124 --- @return Checksum as string.
125 function udpHeader:getChecksumString()
126  return format("0x%04x", self:getChecksum())
127 end
128 
129 --- Set all members of the udp header.
130 --- Per default, all members are set to default values specified in the respective set function.
131 --- Optional named arguments can be used to set a member to a user-provided value.
132 --- @param args Table of named arguments. Available arguments: udpSrc, udpDst, udpLength, udpChecksum
133 --- @param pre prefix for namedArgs. Default 'udp'.
134 --- @code
135 --- fill() --- only default values
136 --- fill{ udpSrc=44566, ip6Length=101 } --- all members are set to default values with the exception of udpSrc
137 --- @endcode
138 function udpHeader:fill(args, pre)
139  args = args or {}
140  pre = pre or "udp"
142  self:setSrcPort(args[pre .. "Src"])
143  self:setDstPort(args[pre .. "Dst"])
144  self:setLength(args[pre .. "Length"])
145  self:setChecksum(args[pre .. "Checksum"])
146 end
147 
148 --- Retrieve the values of all members.
149 --- @param pre prefix for namedArgs. Default 'udp'.
150 --- @return Table of named arguments. For a list of arguments see "See also".
151 --- @see udpHeader:fill
152 function udpHeader:get(pre)
153  pre = pre or "udp"
154 
155  local args = {}
156  args[pre .. "Src"] = self:getSrcPort()
157  args[pre .. "Dst"] = self:getDstPort()
158  args[pre .. "Length"] = self:getLength()
159  args[pre .. "Checksum"] = self:getChecksum()
160 
161  return args
162 end
163 
164 --- Retrieve the values of all members.
165 --- @return Values in string format.
166 function udpHeader:getString()
167  return "UDP " .. self:getSrcPortString() .. " > " .. self:getDstPortString() .. " len " .. self:getLengthString()
168  .. " cksum " .. self:getChecksumString()
169 end
170 
171 -- Maps headers to respective (well knwon) port.
172 -- This list should be extended whenever a new protocol is added to 'UDP constants'.
173 local mapNamePort = {
174  ptp = { udp.PORT_PTP_EVENTS, udp.PORT_PTP_GENERAL_MESSAGES },
175 }
176 
177 --- Resolve which header comes after this one (in a packet).
178 --- For instance: in tcp/udp based on the ports.
179 --- This function must exist and is only used when get/dump is executed on
180 --- an unknown (mbuf not yet casted to e.g. tcpv6 packet) packet (mbuf)
181 --- @return String next header (e.g. 'udp', 'icmp', nil)
182 function udpHeader:resolveNextHeader()
183  local port = self:getDstPort()
184  for name, _port in pairs(mapNamePort) do
185  if type(_port) == "table" then
186  for _, p in pairs(_port) do
187  if port== p then
188  return name
189  end
190  end
191  elseif port == _port then
192  return name
193  end
194  end
195  return nil
196 end
197 
198 --- Change the default values for namedArguments (for fill/get).
199 --- This can be used to for instance calculate a length value based on the total packet length.
200 --- See proto/ip4.setDefaultNamedArgs as an example.
201 --- This function must exist and is only used by packet.fill.
202 --- @param pre The prefix used for the namedArgs, e.g. 'udp'
203 --- @param namedArgs Table of named arguments (see See Also)
204 --- @param nextHeader The header following after this header in a packet
205 --- @param accumulatedLength The so far accumulated length for previous headers in a packet
206 --- @return Table of namedArgs
207 --- @see ip4Header:fill
208 function udpHeader:setDefaultNamedArgs(pre, namedArgs, nextHeader, accumulatedLength)
209  -- set length
210  if not namedArgs[pre .. "Length"] and namedArgs["pktLength"] then
211  namedArgs[pre .. "Length"] = namedArgs["pktLength"] - accumulatedLength
212  end
213 
214  -- set dst port
215  if not namedArgs[pre .. "Dst"] then
216  for name, _port in pairs(mapNamePort) do
217  if nextHeader == name then
218  namedArgs[pre .. "Dst"] = type(_port) == "table" and _port[1] or _port
219  break
220  end
221  end
222  end
223  return namedArgs
224 end
225 
226 ----------------------------------------------------------------------------------
227 ---- Packets
228 ----------------------------------------------------------------------------------
229 
230 --- Cast the packet to an Udp (IP4) packet
231 pkt.getUdp4Packet = packetCreate("eth", "ip4", "udp")
232 --- Cast the packet to an Udp (IP6) packet
233 pkt.getUdp6Packet = packetCreate("eth", "ip6", "udp")
234 --- Cast the packet to an Udp packet, either using IP4 (nil/true) or IP6 (false), depending on the passed boolean.
235 pkt.getUdpPacket = function(self, ip4)
236  ip4 = ip4 == nil or ip4
237  if ip4 then
238  return pkt.getUdp4Packet(self)
239  else
240  return pkt.getUdp6Packet(self)
241  end
242 end
243 
244 
245 ------------------------------------------------------------------------
246 ---- Metatypes
247 ------------------------------------------------------------------------
248 
249 ffi.metatype("struct udp_header", udpHeader)
250 
251 
252 return udp
function udpHeader getDstPort()
Retrieve the UDP destination port.
pkt getUdpPacket
Cast the packet to an Udp packet, either using IP4 (nil/true) or IP6 (false), depending on the passed...
Definition: udp.lua:143
local ffi
low-level dpdk wrapper
Definition: dpdkc.lua:6
function udpHeader setChecksum(int)
Set the checksum.
pkt getUdp6Packet
Cast the packet to an Udp (IP6) packet.
Definition: udp.lua:141
function checksum(data, len)
Calculate a 16 bit checksum.
function udpHeader getChecksumString()
Retrieve the checksum.
function udpHeader fill(args, pre)
Set all members of the udp header.
function packetCreate(...)
Create struct and functions for a new packet.
function udpHeader getString()
Retrieve the values of all members.
function mod band(mask1, mask2, result)
Bitwise and.
function pkt dump(bytes)
Dumps the packet data cast to the best fitting packet struct.
function udpHeader getLengthString()
Retrieve the length.
function ipsecICV set(icv)
Set the IPsec ICV.
function mod new(n)
function udpHeader getChecksum()
Retrieve the checksum.
function udpHeader getDstPortString()
Retrieve the UDP destination port.
local udp
Udp protocol constants.
Definition: udp.lua:23
function mod bor(mask1, mask2, result)
Bitwise or.
function udpHeader getLength()
Retrieve the length.
local udpHeader
Module for udp_header struct (see headers.lua).
Definition: udp.lua:34
function udpHeader setSrcPort(int)
Set the source port.
function udpHeader setLength(int)
Set the length.
local ptp
Ptp protocol constants.
Definition: ptp.lua:23
function udpHeader setDstPort(int)
Set the destination port.
function udpHeader getSrcPortString()
Retrieve the UDP source port.
local eth
Ethernet protocol constants.
Definition: ethernet.lua:24
local ip6
IP6 protocol constants.
Definition: ip6.lua:25
function udpHeader resolveNextHeader()
Resolve which header comes after this one (in a packet).
pkt getUdp4Packet
Cast the packet to an Udp (IP4) packet.
Definition: udp.lua:139
function udpHeader calculateChecksum(len)
Calculate the checksum.
local pkt
Module for packets (rte_mbuf)
Definition: packet.lua:20
function mod bnot(mask, result)
Bitwise not.
n
Create a new array of memory buffers (initialized to nil).
Definition: memory.lua:76
local ip4Header
Module for ip4_header struct (see headers.lua).
Definition: ip4.lua:88
function udpHeader getSrcPort()
Retrieve the UDP source port.
function udpHeader get(pre)
Retrieve the values of all members.
function udpHeader setDefaultNamedArgs(pre, namedArgs, nextHeader, accumulatedLength)
Change the default values for namedArguments (for fill/get).