1 ------------------------------------------------------------------------
3 --- @brief Precision time protocol (PTP) utility.
4 --- Utility functions for the ptp_header struct
5 --- defined in \ref headers.lua . \
n
8 --- - PTP header utility
9 --- - Definition of PTP packets
10 ------------------------------------------------------------------------
13 local
pkt = require "packet"
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
25 ---------------------------------------------------------------------------
27 ---------------------------------------------------------------------------
29 --- Ptp protocol constants
34 -- Message type: delay req
35 ptp.TYPE_DELAY_REQ = 1
36 -- Message type: follow up
37 ptp.TYPE_FOLLOW_UP = 8
38 -- Message type: delay resp
39 ptp.TYPE_DELAY_RESP = 9
41 -- Message control: sync
43 -- Message control: delay req
44 ptp.CONTROL_DELAY_REQ = 1
45 -- Message control: follow up
46 ptp.CONTROL_FOLLOW_UP = 2
47 -- Message control: delay resp
48 ptp.CONTROL_DELAY_RESP = 3
51 ---------------------------------------------------------------------------
53 ---------------------------------------------------------------------------
55 --- Module
for ptp_header
struct (see \ref headers.lua).
59 --- Set the message type.
60 --- @param mt Message type as 8 bit integer.
62 mt = mt or
ptp.TYPE_SYNC
66 --- Retrieve the message type.
67 --- @return Message type as 8 bit integer.
69 return self.messageType
72 --- Retrieve the message type.
73 --- @return Message type in
string format.
78 if type ==
ptp.TYPE_SYNC then
80 elseif type ==
ptp.TYPE_DELAY_REQ then
81 cleartext = "(delay req)"
82 elseif type ==
ptp.TYPE_FOLLOW_UP then
83 cleartext = "(follow up)"
84 elseif type ==
ptp.TYPE_DELAY_RESP then
85 cleartext = "(delay resp)"
87 cleartext = "(unknown)"
90 return format("0x%02x %s", type, cleartext)
94 --- @param v Version as 8 bit integer.
96 v = v or 0x02 -- version 2
100 --- Retrieve the version.
101 --- @return Version as 8 bit integer.
103 return self.versionPTP
106 --- Retrieve the version.
107 --- @return Version in
string format.
113 --- @param l Length as 16 bit integer.
115 l = l or 34 + 10 + 0 -- header, body, suffix
119 --- Retrieve the length.
120 --- @return Length as 16 bit integer.
122 return hton16(self.len)
125 --- Retrieve the length.
126 --- @return Length in
string format.
132 --- @param d Domain as 8 bit integer.
134 d = d or 0 -- default domain
138 --- Retrieve the domain.
139 --- @return Domain as 8 bit integer.
144 --- Retrieve the domain.
145 --- @return Domain in
string format.
150 --- Set the reserved field.
151 --- @param uint8 Reserved as 8 bit integer.
154 self.reserved = uint8
157 --- Retrieve the reserved field.
158 --- @return Reserved field as 8 bit integer.
163 --- Retrieve the reserved field.
164 --- @return Reserved field in
string format.
170 --- @param f Flags as 16 bit integer.
172 f = f or 0 -- no flags
173 self.flags = hton16(f)
176 --- Retrieve the flags.
177 --- @return Flags as 16 bit integer.
179 return hton16(self.flags)
182 --- Retrieve the flags.
183 --- @return Flags in
string format.
185 return format("0x%04x", self:
getFlags())
188 --- Set the correction field.
189 --- @param c Correction field as table of two 32 bit integers { high, low }.
190 --- @todo find something better
for this, 64 bit seems to be trouble
for lua?
191 --- c = { high32bit, low32bit }
193 c = c or { high=0, low=0 } -- correction offset 0
194 self.correction[0] = hton(c.low)
195 self.correction[1] = hton(c.high)
198 --- Retrieve the correction field.
199 --- @return Correction field as table two 32 bit integers { high, low }.
201 return { high = hton(
self.correction[1]), low = hton(
self.correction[0]) }
204 --- Retrieve the correction field.
205 --- @
return Correction field in
string format.
208 return format("0x%08x%08x", t.high, t.low)
211 --- Set the reserved2 field.
212 --- @param uint32 Reserved2 as 32 bit integer.
215 self.reserved2 = hton(uint32)
218 --- Retrieve the reserved2 field.
219 --- @return Reserved2 field as 32 bit integer.
221 return hton(self.reserved2)
224 --- Retrieve the reserved2 field.
225 --- @return Reserved2 field in
string format.
231 --- @param
int Oui as 24 bit integer.
236 self.oui[0] = rshift(
band(
int, 0xFF0000), 16)
237 self.oui[1] = rshift(
band(
int, 0x00FF00), 8)
238 self.oui[2] =
band(
int, 0x0000FF)
241 --- Retrieve the oui.
242 --- @return Oui as 24 bit integer.
244 return
bor(lshift(self.oui[0], 16),
bor(lshift(self.oui[1], 8), self.oui[2]))
247 --- Retrieve the oui.
248 --- @return Oui in
string format.
250 return format("0x%06x", self:
getOui())
254 --- @param
int Uuis as table of two integers { high, low }, high as 8 bit integer and low as 32 bit integer.
255 --- @todo same problem as with correction field
256 --- c = { high8bit, low32bit }
258 int =
int or { high=0, low=0 }
260 -- X X X 1 5 4 3 2 -> 1 2 3 4 5
261 self.uuid[0] =
int.high
262 self.uuid[1] = rshift(
band(
int.low, 0xFF000000), 24)
263 self.uuid[2] = rshift(
band(
int.low, 0x00FF0000), 16)
264 self.uuid[3] = rshift(
band(
int.low, 0x0000FF00), 8)
265 self.uuid[4] =
band(
int.low, 0x000000FF)
268 --- Retrieve the Uuid.
269 --- @return Uuid as table of two integers { high, low }, high is an 8 bit integer, low 32 bit integer.
272 t.high =
self.uuid[0]
273 t.low =
bor(lshift(
self.uuid[1], 24),
bor(lshift(
self.uuid[2], 16),
bor(lshift(
self.uuid[3], 8),
self.uuid[4])))
277 --- Retrieve the Uuid.
278 --- @return Uuid in
string format.
281 return format("0x%02x%08x", t.high, t.low)
284 --- Set the node port.
285 --- @param p Node port as 16 bit integer.
288 self.ptpNodePort = hton16(p)
291 --- Retrieve the node port.
292 --- @return Node port as 16 bit integer.
294 return hton16(self.ptpNodePort)
297 --- Retrieve the node port.
298 --- @return Node port in
string format.
303 --- Set the sequence ID.
304 --- @param s Sequence ID as 16 bit integer.
307 self.sequenceId = hton16(s)
310 --- Retrieve the sequence ID.
311 --- @return Sequence ID as 16 bit integer.
313 return hton16(self.sequenceId)
316 --- Retrieve the sequence ID.
317 --- @return Sequence ID in
string format.
322 --- Set the control field.
323 --- @param c Control field as 8 bit integer.
325 c = c or
ptp.CONTROL_SYNC
329 --- Retrieve the control field.
330 --- @return Control field as 8 bit integer.
335 --- Retrieve the control field.
336 --- @return Control field in
string format.
341 if type ==
ptp.CONTROL_SYNC then
343 elseif type ==
ptp.CONTROL_DELAY_REQ then
344 cleartext = "(delay req)"
345 elseif type ==
ptp.CONTROL_FOLLOW_UP then
346 cleartext = "(follow up)"
347 elseif type ==
ptp.CONTROL_DELAY_RESP then
348 cleartext = "(delay resp)"
350 cleartext = "(unknown)"
353 return format("0x%02x %s", type, cleartext)
356 --- Set the log message interval.
357 --- @param l Log message interval as 8 bit integer.
359 l = l or 0x7F -- default value
360 self.logMessageInterval = l
363 --- Retrieve the log message interval.
364 --- @return Log message interval as 8 bit integer.
366 return self.logMessageInterval
369 --- Retrieve the log message interval.
370 --- @return Log message interval in
string format.
375 --- Set all members of the
ip header.
376 --- Per default, all members are
set to default values specified in the respective
set function.
377 --- Optional named arguments can be used to
set a member to a user-provided value.
378 --- @param args Table of named arguments. Available arguments: MessageType, Version, Length, Domain, Reserved, Flags, Correction, Reserved2, Oui, Uuid, NodePort, SequenceID, Control, LogMessageInterval
379 --- @param pre prefix for namedArgs. Default '
ptp'.
381 ---
fill() --- only default values
382 ---
fill{ ptpLenght=123, ipTTL=100 } --- all members are
set to
default values with the exception of ptpLength
396 self:
setOui(args[pre ..
"Oui"])
397 self:
setUuid(args[pre ..
"Uuid"])
404 --- Retrieve the values of all members.
405 --- @param pre prefix
for namedArgs. Default
'ptp'.
406 --- @
return Table of named arguments. For a list of arguments see
"See also".
417 args[pre .. "Flags"] = self:
getFlags()
420 args[pre .. "Oui"] = self:
getOui()
421 args[pre .. "Uuid"] = self:
getUuid()
430 --- Retrieve the values of all members.
431 --- @return Values in
string format.
449 --- Resolve which header comes after this one (in a packet).
450 --- For instance: in tcp/
udp based on the ports.
451 --- This function must exist and is only used when
get/
dump is executed on
452 --- an unknown (mbuf not yet casted to e.g. tcpv6 packet) packet (mbuf)
453 --- @return String next header (e.g. '
udp', '
icmp', nil)
458 --- Change the default values for namedArguments (for
fill/
get).
459 --- This can be used to for instance calculate a length value based on the total packet length.
461 --- This function must exist and is only used by packet.
fill.
462 --- @param pre The prefix used for the namedArgs, e.g. '
ptp'
463 --- @param namedArgs Table of named arguments (see See Also)
464 --- @param nextHeader The header following after this header in a packet
465 --- @param accumulatedLength The so far accumulated length for previous headers in a packet
466 --- @return Table of namedArgs
470 if not namedArgs[pre .. "Length"] and namedArgs["pktLength"] then
471 namedArgs[pre .. "Length"] = namedArgs["pktLength"] - accumulatedLength
477 ---------------------------------------------------------------------------------
479 ---------------------------------------------------------------------------------
481 --- Cast the packet to a layer 2 Ptp packet
483 --- Cast the packet to a Ptp over Udp (IP4) packet
487 ------------------------------------------------------------------------
489 ------------------------------------------------------------------------
function ptpHeader getSequenceID()
Retrieve the sequence ID.
function ptpHeader getSequenceIDString()
Retrieve the sequence ID.
function ptpHeader setDefaultNamedArgs(pre, namedArgs, nextHeader, accumulatedLength)
Change the default values for namedArguments (for fill/get).
local ffi
low-level dpdk wrapper
pkt getPtpPacket
Cast the packet to a layer 2 Ptp packet.
function ptpHeader getReserved2()
Retrieve the reserved2 field.
function packetCreate(...)
Create struct and functions for a new packet.
function ptpHeader getLogMessageIntervalString()
Retrieve the log message interval.
function ptpHeader get(pre)
Retrieve the values of all members.
function ptpHeader setNodePort(p)
Set the node port.
function ptpHeader setLogMessageInterval(l)
Set the log message interval.
function mod band(mask1, mask2, result)
Bitwise and.
function ptpHeader setMessageType(mt)
Set the message type.
function ptpHeader setCorrection(c)
Set the correction field.
function pkt dump(bytes)
Dumps the packet data cast to the best fitting packet struct.
function ptpHeader getDomain()
Retrieve the domain.
function ptpHeader getUuid()
Retrieve the Uuid.
function ptpHeader getReservedString()
Retrieve the reserved field.
function ipsecICV set(icv)
Set the IPsec ICV.
function ptpHeader getMessageType()
Retrieve the message type.
function ptpHeader getFlags()
Retrieve the flags.
function ptpHeader getString()
Retrieve the values of all members.
function ptpHeader fill(args, pre)
Set all members of the ip header.
function ptpHeader getLength()
Retrieve the length.
local udp
Udp protocol constants.
function ptpHeader getVersion()
Retrieve the version.
function ptpHeader setUuid(int)
Set the uuid.
function ptpHeader getUuidString()
Retrieve the Uuid.
function ptpHeader getCorrection()
Retrieve the correction field.
function mod bor(mask1, mask2, result)
Bitwise or.
function ptpHeader getReserved2String()
Retrieve the reserved2 field.
function ptpHeader setVersion(v)
Set the version.
function ptpHeader getOuiString()
Retrieve the oui.
function ptpHeader getReserved()
Retrieve the reserved field.
function ptpHeader setReserved(uint8)
Set the reserved field.
function ptpHeader getControl()
Retrieve the control field.
local ptp
Ptp protocol constants.
function ptpHeader setReserved2(uint32)
Set the reserved2 field.
pkt getUdpPtpPacket
Cast the packet to a Ptp over Udp (IP4) packet.
function ptpHeader getOui()
Retrieve the oui.
function ptpHeader setOui(int)
Set the oui.
function ptpHeader getCorrectionString()
Retrieve the correction field.
function ptpHeader setDomain(d)
Set the domain.
function ptpHeader getFlagsString()
Retrieve the flags.
local eth
Ethernet protocol constants.
function ptpHeader setLength(l)
Set the length.
local ip
IP4 protocol constants.
function ptpHeader getNodePort()
Retrieve the node port.
local pkt
Module for packets (rte_mbuf)
function mod bnot(mask, result)
Bitwise not.
function ptpHeader getControlString()
Retrieve the control field.
n
Create a new array of memory buffers (initialized to nil).
function ptpHeader setControl(c)
Set the control field.
local ptpHeader
Module for ptp_header struct (see headers.lua).
function ptpHeader getDomainString()
Retrieve the domain.
function ptpHeader getNodePortString()
Retrieve the node port.
function ptpHeader setSequenceID(s)
Set the sequence ID.
function ptpHeader getVersionString()
Retrieve the version.
function ptpHeader getMessageTypeString()
Retrieve the message type.
local icmp
Icmp4 protocol constants.
function ptpHeader resolveNextHeader()
Resolve which header comes after this one (in a packet).
function ptpHeader getLogMessageInterval()
Retrieve the log message interval.
function ptpHeader setFlags(f)
Set the flags.
function ptpHeader getLengthString()
Retrieve the length.