Skip to content

Commit

Permalink
Merge pull request #228 from cihangir/domain_number
Browse files Browse the repository at this point in the history
ptp/ptp4u: add default DomainNumber support #219
  • Loading branch information
vvfedorenko committed Nov 11, 2022
2 parents 969d480 + 42ef5ec commit 4b3e8f0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
5 changes: 5 additions & 0 deletions cmd/ptp4u/main.go
Expand Up @@ -52,6 +52,7 @@ func main() {
flag.IntVar(&c.QueueSize, "queue", 0, "Size of the queue to send out packets")
flag.IntVar(&c.RecvWorkers, "recvworkers", 10, "Set the number of receive workers")
flag.IntVar(&c.SendWorkers, "workers", 100, "Set the number of send workers")
flag.UintVar(&c.DomainNumber, "domainnumber", 0, "Set the PTP domain by its number. Valid values are [0-255]")
flag.StringVar(&c.ConfigFile, "config", "", "Path to a config with dynamic settings")
flag.StringVar(&c.DebugAddr, "pprofaddr", "", "host:port for the pprof to bind")
flag.StringVar(&c.Interface, "iface", "eth0", "Set the interface")
Expand Down Expand Up @@ -86,6 +87,10 @@ func main() {
log.Fatalf("Unsupported DSCP value %v", c.DSCP)
}

if c.DomainNumber > 255 {
log.Fatalf("Unsupported DomainNumber value %v", c.DomainNumber)
}

switch c.TimestampType {
case timestamp.SWTIMESTAMP:
log.Warning("Software timestamps greatly reduce the precision")
Expand Down
1 change: 1 addition & 0 deletions ptp/ptp4u/server/config.go
Expand Up @@ -48,6 +48,7 @@ type StaticConfig struct {
IP net.IP
LogLevel string
MonitoringPort int
DomainNumber uint
PidFile string
QueueSize int
RecvWorkers int
Expand Down
8 changes: 4 additions & 4 deletions ptp/ptp4u/server/subscription.go
Expand Up @@ -202,7 +202,7 @@ func (sc *SubscriptionClient) initSync() {
SdoIDAndMsgType: ptp.NewSdoIDAndMsgType(ptp.MessageSync, 0),
Version: ptp.Version,
MessageLength: uint16(binary.Size(ptp.SyncDelayReq{})),
DomainNumber: 0,
DomainNumber: uint8(sc.serverConfig.DomainNumber),
FlagField: ptp.FlagUnicast | ptp.FlagTwoStep,
SequenceID: 0,
SourcePortIdentity: ptp.PortIdentity{
Expand Down Expand Up @@ -231,7 +231,7 @@ func (sc *SubscriptionClient) initFollowup() {
SdoIDAndMsgType: ptp.NewSdoIDAndMsgType(ptp.MessageFollowUp, 0),
Version: ptp.Version,
MessageLength: uint16(binary.Size(ptp.FollowUp{})),
DomainNumber: 0,
DomainNumber: uint8(sc.serverConfig.DomainNumber),
FlagField: ptp.FlagUnicast,
SequenceID: 0,
SourcePortIdentity: ptp.PortIdentity{
Expand Down Expand Up @@ -266,7 +266,7 @@ func (sc *SubscriptionClient) initAnnounce() {
SdoIDAndMsgType: ptp.NewSdoIDAndMsgType(ptp.MessageAnnounce, 0),
Version: ptp.Version,
MessageLength: uint16(binary.Size(ptp.Header{}) + binary.Size(ptp.AnnounceBody{})),
DomainNumber: 0,
DomainNumber: uint8(sc.serverConfig.DomainNumber),
FlagField: ptp.FlagUnicast | ptp.FlagPTPTimescale,
SequenceID: 0,
SourcePortIdentity: ptp.PortIdentity{
Expand Down Expand Up @@ -314,7 +314,7 @@ func (sc *SubscriptionClient) initDelayResp() {
SdoIDAndMsgType: ptp.NewSdoIDAndMsgType(ptp.MessageDelayResp, 0),
Version: ptp.Version,
MessageLength: uint16(binary.Size(ptp.DelayResp{})),
DomainNumber: 0,
DomainNumber: uint8(sc.serverConfig.DomainNumber),
FlagField: ptp.FlagUnicast,
SequenceID: 0,
SourcePortIdentity: ptp.PortIdentity{
Expand Down
42 changes: 38 additions & 4 deletions ptp/ptp4u/server/subscription_test.go
Expand Up @@ -151,9 +151,15 @@ func TestSubscriptionflags(t *testing.T) {

func TestSyncPacket(t *testing.T) {
sequenceID := uint16(42)
domainNumber := uint8(13)

w := &sendWorker{}
c := &Config{clockIdentity: ptp.ClockIdentity(1234)}
c := &Config{
clockIdentity: ptp.ClockIdentity(1234),
StaticConfig: StaticConfig{
DomainNumber: uint(domainNumber),
},
}
sa := timestamp.IPToSockaddr(net.ParseIP("127.0.0.1"), 123)
sc := NewSubscriptionClient(w.queue, w.signalingQueue, sa, sa, ptp.MessageAnnounce, c, time.Second, time.Time{})
sc.sequenceID = sequenceID
Expand All @@ -163,15 +169,23 @@ func TestSyncPacket(t *testing.T) {
sc.UpdateSync()
require.Equal(t, uint16(44), sc.Sync().Header.MessageLength) // check packet length
require.Equal(t, sequenceID+1, sc.Sync().Header.SequenceID)
require.Equal(t, domainNumber, sc.Sync().Header.DomainNumber)
}

func TestFollowupPacket(t *testing.T) {
sequenceID := uint16(42)
now := time.Now()
interval := 3 * time.Second
domainNumber := uint8(13)

w := &sendWorker{}
c := &Config{clockIdentity: ptp.ClockIdentity(1234)}

c := &Config{
clockIdentity: ptp.ClockIdentity(1234),
StaticConfig: StaticConfig{
DomainNumber: uint(domainNumber),
},
}
sa := timestamp.IPToSockaddr(net.ParseIP("127.0.0.1"), 123)
sc := NewSubscriptionClient(w.queue, w.signalingQueue, sa, sa, ptp.MessageAnnounce, c, time.Second, time.Time{})
sc.sequenceID = sequenceID
Expand All @@ -187,6 +201,7 @@ func TestFollowupPacket(t *testing.T) {
require.Equal(t, sequenceID+1, sc.Followup().Header.SequenceID)
require.Equal(t, i, sc.Followup().Header.LogMessageInterval)
require.Equal(t, now.Unix(), sc.Followup().FollowUpBody.PreciseOriginTimestamp.Time().Unix())
require.Equal(t, domainNumber, sc.Followup().Header.DomainNumber)
}

func TestAnnouncePacket(t *testing.T) {
Expand All @@ -195,9 +210,20 @@ func TestAnnouncePacket(t *testing.T) {
interval := 3 * time.Second
clockClass := ptp.ClockClass7
clockAccuracy := ptp.ClockAccuracyMicrosecond1
domainNumber := uint8(13)

w := &sendWorker{}
c := &Config{clockIdentity: ptp.ClockIdentity(1234), DynamicConfig: DynamicConfig{ClockClass: clockClass, ClockAccuracy: clockAccuracy, UTCOffset: UTCOffset}}
c := &Config{
clockIdentity: ptp.ClockIdentity(1234),
DynamicConfig: DynamicConfig{
ClockClass: clockClass,
ClockAccuracy: clockAccuracy,
UTCOffset: UTCOffset,
},
StaticConfig: StaticConfig{
DomainNumber: uint(domainNumber),
},
}
sa := timestamp.IPToSockaddr(net.ParseIP("127.0.0.1"), 123)
sc := NewSubscriptionClient(w.queue, w.signalingQueue, sa, sa, ptp.MessageAnnounce, c, time.Second, time.Time{})
sc.sequenceID = sequenceID
Expand All @@ -221,14 +247,21 @@ func TestAnnouncePacket(t *testing.T) {
require.Equal(t, ptp.ClockClass7, sc.Announce().AnnounceBody.GrandmasterClockQuality.ClockClass)
require.Equal(t, ptp.ClockAccuracyMicrosecond1, sc.Announce().AnnounceBody.GrandmasterClockQuality.ClockAccuracy)
require.Equal(t, int16(UTCOffset.Seconds()), sc.Announce().AnnounceBody.CurrentUTCOffset)
require.Equal(t, domainNumber, sc.Announce().Header.DomainNumber)
}

func TestDelayRespPacket(t *testing.T) {
sequenceID := uint16(42)
now := time.Now()
domainNumber := uint8(13)

w := &sendWorker{}
c := &Config{clockIdentity: ptp.ClockIdentity(1234)}
c := &Config{
clockIdentity: ptp.ClockIdentity(1234),
StaticConfig: StaticConfig{
DomainNumber: uint(domainNumber),
},
}
sa := timestamp.IPToSockaddr(net.ParseIP("127.0.0.1"), 123)
sc := NewSubscriptionClient(w.queue, w.signalingQueue, sa, sa, ptp.MessageAnnounce, c, time.Second, time.Time{})

Expand All @@ -250,6 +283,7 @@ func TestDelayRespPacket(t *testing.T) {
require.Equal(t, sp, sc.DelayResp().Header.SourcePortIdentity)
require.Equal(t, now.Unix(), sc.DelayResp().DelayRespBody.ReceiveTimestamp.Time().Unix())
require.Equal(t, ptp.FlagUnicast, sc.DelayResp().Header.FlagField)
require.Equal(t, domainNumber, sc.DelayResp().Header.DomainNumber)
}

func TestSignalingGrantPacket(t *testing.T) {
Expand Down

0 comments on commit 4b3e8f0

Please sign in to comment.