SatCat5
eth_sw_log.h
Go to the documentation of this file.
1 // Copyright 2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
60 
61 #pragma once
62 
63 #include <satcat5/cfgbus_core.h>
64 #include <satcat5/eth_address.h>
65 #include <satcat5/eth_switch.h>
66 #include <satcat5/io_writeable.h>
67 #include <satcat5/pkt_buffer.h>
68 #include <satcat5/polling.h>
69 #include <satcat5/udp_core.h>
70 
71 namespace satcat5 {
72  namespace eth {
75  // Define "reason codes" used when dropping a packet.
76  // These match the codes defined in "eth_frame_common.vhd".
77  static constexpr u8
78  REASON_KEEP = 0x00,
79  DROP_OVERFLOW = 0x01,
80  DROP_BADFCS = 0x02,
81  DROP_BADFRM = 0x03,
82  DROP_MCTRL = 0x04,
83  DROP_VLAN = 0x05,
84  DROP_VRATE = 0x06,
85  DROP_PTPERR = 0x07,
86  DROP_NO_ROUTE = 0x08,
87  DROP_DISABLED = 0x09,
88  DROP_UNKNOWN = 0xFF;
89 
90  // Other constants:
91  static constexpr u8
92  SRC_MASK = 0x1F,
93  TYPE_MASK = 0xE0,
94  TYPE_KEEP = (0 << 5),
95  TYPE_DROP = (1 << 5),
96  TYPE_SKIP = (2 << 5);
97  static constexpr u32
98  TIME_MASK = 0xFFFFFF;
99  static constexpr unsigned
100  LEN_BYTES = 24;
101 
102  // Raw packet fields
103  u32 tstamp;
104  u8 type_src;
106  u32 meta;
107 
108  // Explicitly declare default constructor and assignment methods.
109  SwitchLogMessage() = default;
110  SwitchLogMessage(const SwitchLogMessage& t) = default;
111  SwitchLogMessage& operator=(const SwitchLogMessage& t) = default;
112 
115  u8 reason() const;
116 
118  const char* reason_str() const;
119 
123  { return SATCAT5_PMASK_TYPE(meta); }
124 
125  // Other accessors
126  u16 count_drop() const;
127  u16 count_keep() const;
128  inline u8 srcport() const
129  { return type_src & SRC_MASK; }
130  inline u8 type() const
131  { return type_src & TYPE_MASK; }
132 
134  void init_keep(const satcat5::eth::Header& hdr, u8 src, u32 dst);
135 
137  void init_drop(const satcat5::eth::Header& hdr, u8 src, u8 why);
138 
140  void init_skip(u16 drop, u16 keep);
141 
143  void log_to(satcat5::log::LogBuffer& wr) const;
145  void write_to(satcat5::io::Writeable* wr) const;
149  };
150 
153  public:
160 
161  protected:
162  // Event handlers.
163  void timer_event() override;
164 
165  // Member variables.
166  satcat5::eth::SwitchLogHandler* const m_dst;
169  };
170 
174  public:
176  struct TrafficStats {
180  u32 errct_ovr;
181  u32 errct_pkt;
183  };
184 
187  TrafficStats get_port(unsigned idx);
188 
190  void log_packet(const satcat5::eth::SwitchLogMessage& msg) override;
191 
192  protected:
194  SwitchLogStats(TrafficStats* buff, unsigned size);
195 
196  // Working buffers are provided by the parent class.
198  const unsigned m_size;
199  };
200 
202  template <unsigned SIZE = satcat5::eth::PMASK_SIZE>
204  public:
205  SwitchLogStatsStatic() : SwitchLogStats(m_stats_buffer, SIZE) {}
206 
207  protected:
208  satcat5::eth::SwitchLogStats::TrafficStats m_stats_buffer[SIZE];
209  };
210 
216  , protected satcat5::poll::Timer {
217  public:
220  : m_dst(dst), m_skip_drop(0), m_skip_keep(0) {}
221 
223  void log_packet(const satcat5::eth::SwitchLogMessage& msg) override;
224 
225  protected:
226  // Event handlers.
227  void timer_event() override;
228 
229  // Member variables.
230  satcat5::io::Writeable* const m_dst;
231  u16 m_skip_drop;
232  u16 m_skip_keep;
233  };
234 
238  template <unsigned NPKT>
240  public:
243  : ReadableRedirect(&m_buffer), m_writer(&m_buffer) {}
244 
246  inline void clear()
247  { m_buffer.clear(); }
248 
251  { return &m_writer; }
252 
253  protected:
254  static constexpr unsigned NBYTES = NPKT * SwitchLogMessage::LEN_BYTES;
257  };
258 
265  : protected satcat5::io::EventListener
266  , protected satcat5::net::Protocol
267  , protected satcat5::poll::Timer {
268  public:
274  void set_polling_interval(unsigned msec);
275 
276  protected:
279  explicit SwitchLogReader(satcat5::io::Readable* src);
283  ~SwitchLogReader() SATCAT5_OPTIONAL_DTOR;
284 
287  virtual void log_event(const satcat5::eth::SwitchLogMessage& msg) = 0;
288 
289  // Event handlers.
290  void data_rcvd(satcat5::io::Readable* src) override;
291  void data_unlink(satcat5::io::Readable* src) override;
292  void frame_rcvd(satcat5::io::LimitedRead& src) override;
293  void timer_event() override;
294 
295  // Member variables.
296  satcat5::io::Readable* m_src;
297  satcat5::net::Dispatch* const m_iface;
298  };
299 
304  public:
306  explicit SwitchLogFormatter(
308  const char* lbl = "PktLog");
309 
311  explicit SwitchLogFormatter(
312  satcat5::eth::Dispatch* iface,
313  satcat5::eth::MacType etype = satcat5::eth::ETYPE_SWITCH_LOG,
314  const char* lbl = "PktLog");
315 
317  explicit SwitchLogFormatter(
318  satcat5::udp::Dispatch* iface,
319  satcat5::udp::Port port = satcat5::udp::PORT_SWITCH_LOG,
320  const char* lbl = "PktLog");
321 
323  void log_event(const satcat5::eth::SwitchLogMessage& msg) override;
324 
325  protected:
326  const char* const m_label;
327  };
328 
343  public:
349  satcat5::udp::Dispatch* iface);
350 
356  satcat5::udp::Dispatch* iface);
357 
359  void connect(
360  const satcat5::eth::MacAddr& addr,
361  const satcat5::eth::MacType& type = satcat5::eth::ETYPE_SWITCH_LOG,
362  const satcat5::eth::VlanTag& vtag = satcat5::eth::VTAG_NONE);
363 
365  void connect(
366  const satcat5::udp::Addr& addr,
367  const satcat5::udp::Port& port = satcat5::udp::PORT_SWITCH_LOG,
368  const satcat5::eth::VlanTag& vtag = satcat5::eth::VTAG_NONE);
369 
371  inline void close() {
372  m_eth.close();
373  m_udp.close();
374  }
375 
377  inline bool ready() const
378  { return m_eth.ready() || m_udp.ready(); }
379 
381  void log_event(const satcat5::eth::SwitchLogMessage& msg) override;
382 
383  protected:
385  satcat5::io::Writeable* const m_dst_raw;
386  satcat5::eth::Address m_eth;
387  satcat5::udp::Address m_udp;
388  bool m_first;
389  };
390  }
391 }
ConfigBus core definitions.
Pointer-like wrapper for one or more ConfigBus registers.
Definition: cfgbus_core.h:76
Send packets to a specific Ethernet address.
Definition: eth_address.h:17
bool ready() const override
Is this address object ready for use? Child MUST override this method.
Definition: eth_address.cc:43
void close() override
Close any open connections and revert to idle.
Definition: eth_address.cc:37
Implemention of "net::Dispatch" for Ethernet frames.
Definition: eth_dispatch.h:21
Combine a SwitchLogWriter with a built-in output buffer.
Definition: eth_sw_log.h:239
void clear()
Flush contents of the internal buffer.
Definition: eth_sw_log.h:246
SwitchLogBuffer()
Create and link the writer and buffer objects.
Definition: eth_sw_log.h:242
SwitchLogHandler * writer()
Return the inner object for passing to SwitchCore::add_log().
Definition: eth_sw_log.h:250
Read binary packet logs to produce human-readable log messages.
Definition: eth_sw_log.h:303
SwitchLogFormatter(satcat5::io::Readable *src, const char *lbl="PktLog")
Bind this object to a stream of packet-logging data.
Definition: eth_sw_log.cc:296
void log_event(const satcat5::eth::SwitchLogMessage &msg) override
Implement the SwitchLogReader callback.
Definition: eth_sw_log.cc:316
Define the API for packet-logging callbacks from eth::SwitchCore.
Definition: eth_switch.h:64
Poll a hardware switch or router for log data.
Definition: eth_sw_log.h:152
void timer_event() override
Child class MUST override this method.
Definition: eth_sw_log.cc:143
SwitchLogHardware(satcat5::eth::SwitchLogHandler *dst, satcat5::cfg::Register src)
Link this object to a log-writer and a data source.
Definition: eth_sw_log.cc:134
Read packet-logs from an input stream or network interface.
Definition: eth_sw_log.h:267
virtual void log_event(const satcat5::eth::SwitchLogMessage &msg)=0
The child class MUST override this method.
void set_polling_interval(unsigned msec)
Set polling interval, in milliseconds.
Definition: eth_sw_log.cc:267
void frame_rcvd(satcat5::io::LimitedRead &src) override
Dispatch calls frame_rcvd(...) for each incoming frame with with a matching net::Type value.
Definition: eth_sw_log.cc:288
void data_unlink(satcat5::io::Readable *src) override
Unlink this EventListener from the designated source, because the designated Readable object is being...
Definition: eth_sw_log.cc:286
void data_rcvd(satcat5::io::Readable *src) override
The data_rcvd() callback is polled whenever data is available.
Definition: eth_sw_log.cc:278
void timer_event() override
Child class MUST override this method.
Definition: eth_sw_log.cc:292
SwitchLogReader(satcat5::io::Readable *src)
Constructor for buffered mode.
Definition: eth_sw_log.cc:244
Record packet statistics based on switch log events.
Definition: eth_sw_log.h:173
TrafficStats get_port(unsigned idx)
Read most recent statistics for the Nth port.
Definition: eth_sw_log.cc:211
const unsigned m_size
Buffer size.
Definition: eth_sw_log.h:198
TrafficStats *const m_stats
Working counters.
Definition: eth_sw_log.h:197
SwitchLogStats(TrafficStats *buff, unsigned size)
Constructor accepts a pointer to the working buffer.
Definition: eth_sw_log.cc:203
void log_packet(const satcat5::eth::SwitchLogMessage &msg) override
Process each packet event.
Definition: eth_sw_log.cc:218
Static allocation wrapper for eth::SwitchLogStats.
Definition: eth_sw_log.h:203
Forward binary packet logs to a network interface.
Definition: eth_sw_log.h:342
void close()
Close the connection and stop log forwarding.
Definition: eth_sw_log.h:371
SwitchLogToPort(satcat5::io::Readable *src, satcat5::eth::SwitchPort *dst, satcat5::udp::Dispatch *iface)
Bind this object to a software egress port.
Definition: eth_sw_log.cc:320
bool ready() const
Is this connection currently active?
Definition: eth_sw_log.h:377
void log_event(const satcat5::eth::SwitchLogMessage &msg) override
Implement the SwitchLogReader callback.
Definition: eth_sw_log.cc:366
void connect(const satcat5::eth::MacAddr &addr, const satcat5::eth::MacType &type=satcat5::eth::ETYPE_SWITCH_LOG, const satcat5::eth::VlanTag &vtag=satcat5::eth::VTAG_NONE)
Connect to a designated raw-Ethernet address.
Definition: eth_sw_log.cc:348
Record rate-limited packet-logs for a switch or router.
Definition: eth_sw_log.h:216
void timer_event() override
Child class MUST override this method.
Definition: eth_sw_log.cc:186
SwitchLogWriter(satcat5::io::Writeable *dst)
Link this object to a Writeable destination.
Definition: eth_sw_log.h:219
void log_packet(const satcat5::eth::SwitchLogMessage &msg) override
Process each packet event.
Definition: eth_sw_log.cc:164
Generic packetized I/O interface for use with SwitchCore.
Definition: eth_switch.h:245
Event-handler interface for newly received data.
Definition: io_readable.h:43
Limited read of next N bytes.
Definition: io_readable.h:255
MultiWriter adapter for bypass mode.
Definition: multi_buffer.h:481
void clear()
Reset buffer contents.
Definition: pkt_buffer.cc:22
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Wrapper class for forwarding reads to another object.
Definition: io_readable.h:299
constexpr ReadableRedirect(satcat5::io::Readable *src)
Constructor and destructor should only be called by the child.
Definition: io_readable.h:310
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Internal buffer used by the Log class.
Definition: log.h:134
The "Dispatch" is a generic interface that knows how to read a designated protocol layer and sort inc...
Definition: net_dispatch.h:36
A net::Protocol is the counterpart to net::Dispatch that handles a particular data stream,...
Definition: net_protocol.h:28
Timer objects are polled after a fixed delay or at a regular interval.
Definition: polling.h:213
Implementation of "net::Address" for UDP Dispatch.
Definition: udp_core.h:72
bool ready() const override
Is this address object ready for use? Child MUST override this method.
Definition: udp_core.h:103
void close() override
Close any open connections and revert to idle.
Definition: udp_core.h:102
Dispatcher sorts incoming UDP messages by port index.
Definition: udp_dispatch.h:20
Software-defined Ethernet switch.
#define SATCAT5_PMASK_TYPE
Set the integer type used to identify source and destination ports.
Definition: eth_switch.h:41
"Writeable" I/O interface core definitions
Core event-processing loop for SatCat5 software.
An Ethernet frame header.
Definition: eth_header.h:167
An Ethernet MAC address (with serializable interface).
Definition: eth_header.h:29
EtherType field (uint16) is used a protocol-ID [1536..65535].
Definition: eth_header.h:96
A single 24-byte packet-log message.
Definition: eth_sw_log.h:74
void init_keep(const satcat5::eth::Header &hdr, u8 src, u32 dst)
Initialize a KEEP message.
Definition: eth_sw_log.cc:73
static constexpr u8 DROP_UNKNOWN
Other unspecified error.
Definition: eth_sw_log.h:88
satcat5::eth::Header hdr
Ethernet packet header.
Definition: eth_sw_log.h:105
static constexpr u8 DROP_VLAN
Virtual-LAN policy.
Definition: eth_sw_log.h:83
static constexpr u8 DROP_MCTRL
Link-local control packet.
Definition: eth_sw_log.h:82
bool read_from(satcat5::io::Readable *rd)
Read descriptor from the designated stream.
Definition: eth_sw_log.cc:122
static constexpr unsigned LEN_BYTES
Message length, in bytes.
Definition: eth_sw_log.h:100
static constexpr u8 DROP_BADFRM
Frame length, source MAC, etc.
Definition: eth_sw_log.h:81
void init_drop(const satcat5::eth::Header &hdr, u8 src, u8 why)
Initialize a DROP message.
Definition: eth_sw_log.cc:80
u16 count_keep() const
Count delivered packets.
Definition: eth_sw_log.cc:67
static constexpr u8 DROP_PTPERR
PTP error (no timestamp)
Definition: eth_sw_log.h:85
static constexpr u8 DROP_BADFCS
Invalid frame check sequence.
Definition: eth_sw_log.h:80
static constexpr u8 TYPE_SKIP
Message type: SKIP (summary)
Definition: eth_sw_log.h:96
u16 count_drop() const
Count dropped packets.
Definition: eth_sw_log.cc:61
static constexpr u8 DROP_DISABLED
Ingress or egress port disabled.
Definition: eth_sw_log.h:87
u8 type_src
Type and source port.
Definition: eth_sw_log.h:104
void init_skip(u16 drop, u16 keep)
Initialize a SKIP message.
Definition: eth_sw_log.cc:87
const char * reason_str() const
Human-readable reason for a dropped packet, if applicable.
Definition: eth_sw_log.cc:45
static constexpr u8 DROP_NO_ROUTE
No destination or null route.
Definition: eth_sw_log.h:86
static constexpr u8 REASON_KEEP
Packet accepted / not dropped.
Definition: eth_sw_log.h:78
static constexpr u8 DROP_VRATE
Virtual-LAN rate limits.
Definition: eth_sw_log.h:84
static constexpr u8 TYPE_MASK
Mask for message type.
Definition: eth_sw_log.h:93
u8 reason() const
Coded reason for a dropped packet, if applicable.
Definition: eth_sw_log.cc:37
u32 meta
Additional metadata.
Definition: eth_sw_log.h:106
void log_to(satcat5::log::LogBuffer &wr) const
Format this field as a human-readable string.
Definition: eth_sw_log.cc:94
u8 type() const
< Message type.
Definition: eth_sw_log.h:130
static constexpr u8 TYPE_DROP
Message type: DROP (dropped)
Definition: eth_sw_log.h:95
static constexpr u32 TIME_MASK
Timestamp wraparound at 2^24.
Definition: eth_sw_log.h:98
static constexpr u8 DROP_OVERFLOW
FIFO overflow (Rx or Tx)
Definition: eth_sw_log.h:79
u32 tstamp
Timestamp in microseconds.
Definition: eth_sw_log.h:103
static constexpr u8 TYPE_KEEP
Message type: KEEP (delivered)
Definition: eth_sw_log.h:94
static constexpr u8 SRC_MASK
Mask for source index.
Definition: eth_sw_log.h:92
void write_to(satcat5::io::Writeable *wr) const
Write descriptor to the designated stream.
Definition: eth_sw_log.cc:112
SATCAT5_PMASK_TYPE dstmask() const
Destination mask (KEEP messages only).
Definition: eth_sw_log.h:122
u8 srcport() const
< Source port index.
Definition: eth_sw_log.h:128
Data structure for reporting per-port traffic statistics.
Definition: eth_sw_log.h:176
u32 bcast_frames
Broadcast frames received from device.
Definition: eth_sw_log.h:177
u32 errct_total
Total packet errors, all types.
Definition: eth_sw_log.h:182
u32 errct_ovr
Frames dropped due to FIFO overflow.
Definition: eth_sw_log.h:180
u32 sent_frames
Total frames sent from switch to device.
Definition: eth_sw_log.h:179
u32 rcvd_frames
Total frames received from device.
Definition: eth_sw_log.h:178
u32 errct_pkt
Invalid packets (bad checksum, etc.)
Definition: eth_sw_log.h:181
Header contents for an 802.1Q Virtual-LAN tag.
Definition: eth_header.h:124
IPv4 address is a 32-bit unsigned integer.
Definition: ip_core.h:15
UDP and TCP ports are both 16-bit unsigned integers.
Definition: ip_core.h:119
Multipurpose filter for matching fields in network packets.
Definition: net_type.h:38