SatCat5
eth_plugin.h
Go to the documentation of this file.
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
30 
31 
32 #pragma once
33 
34 #include <satcat5/eth_arp.h>
35 #include <satcat5/eth_header.h>
36 #include <satcat5/eth_switch.h>
37 #include <satcat5/ip_core.h>
38 #include <satcat5/list.h>
39 #include <satcat5/tcp_core.h>
40 #include <satcat5/types.h>
41 #include <satcat5/udp_core.h>
42 
43 namespace satcat5 {
44  namespace eth {
48  struct PluginPacket {
49  public:
52 
56 
66 
76 
78  u16 hlen;
79 
81  u16 flags;
82 
85  constexpr PluginPacket()
86  : pkt(nullptr), hdr{}, arp(), ip{}, tcp{}
87  , udp(udp::HEADER_EMPTY), dst_mask(0), hlen(0), flags(0) {}
88  PluginPacket(const PluginPacket& t) = delete;
89  PluginPacket& operator=(const PluginPacket& t) = delete;
90 
94  bool read_from(satcat5::io::MultiPacket* packet);
95 
98  void write_to(satcat5::io::Writeable* wr) const;
99 
105  inline void adjust()
106  { flags |= FLAG_HEADER_CHANGE; }
107 
114  inline void divert()
115  { flags |= FLAG_DIVERT; }
116 
118  inline void drop(u8 reason)
119  { dst_mask = 0; flags = u16(reason); }
120 
125  inline bool is_adjusted() const
126  { return !!(flags & FLAG_HEADER_CHANGE); }
127  inline bool is_diverted() const
128  { return !!(flags & FLAG_DIVERT); }
129  inline bool is_ip() const
130  { return hdr.type == satcat5::eth::ETYPE_IPV4; }
131  inline bool is_arp() const
132  { return hdr.type == satcat5::eth::ETYPE_ARP; }
133  inline bool is_tcp() const
134  { return is_ip() && ip.proto() == satcat5::ip::PROTO_TCP; }
135  inline bool is_udp() const
136  { return is_ip() && ip.proto() == satcat5::ip::PROTO_UDP; }
137  inline unsigned length() const
138  { return pkt->m_length; }
140  { return idx2mask(src_port()); }
141  inline unsigned src_port() const
142  { return unsigned(pkt->m_user[0]); }
144  { return satcat5::eth::VtagPolicy{pkt->m_user[1]}; }
145  inline u8 reason() const
146  { return u8(flags & 0xFF); }
148 
149  protected:
151  static const u16 FLAG_DIVERT = (1u << 8);
153  static const u16 FLAG_HEADER_CHANGE = (1u << 9);
154 
157  };
158 
166  class PluginCore {
167  public:
174  virtual void query(PluginPacket& pkt) = 0;
175 
176  protected:
179  explicit PluginCore(satcat5::eth::SwitchCore* sw);
180  ~PluginCore() SATCAT5_OPTIONAL_DTOR;
181 
183  satcat5::eth::SwitchCore* const m_switch;
184 
185  private:
186  // Linked list for chaining multiple plugins.
187  friend satcat5::util::ListCore;
188  satcat5::eth::PluginCore* m_next;
189  };
190 
198  class PluginPort {
199  public:
207  virtual void ingress(PluginPacket& pkt) {} // GCOVR_EXCL_LINE
208 
215  virtual void egress(PluginPacket& pkt) {} // GCOVR_EXCL_LINE
216 
217  protected:
220  explicit PluginPort(satcat5::eth::SwitchPort* port);
221  ~PluginPort() SATCAT5_OPTIONAL_DTOR;
222 
224  satcat5::eth::SwitchPort* const m_port;
225 
226  private:
227  // Linked list for chaining multiple plugins.
228  friend satcat5::util::ListCore;
229  satcat5::eth::PluginPort* m_next;
230  };
231  }
232 }
Ethernet switch plugin API.
Definition: eth_plugin.h:166
PluginCore(satcat5::eth::SwitchCore *sw)
Associate this plugin object with the designated switch.
Definition: eth_plugin.cc:50
virtual void query(PluginPacket &pkt)=0
Packet-received callback.
satcat5::eth::SwitchCore *const m_switch
Pointer to the associated SwitchCore object.
Definition: eth_plugin.h:183
Ethernet port plugin API.
Definition: eth_plugin.h:198
virtual void egress(PluginPacket &pkt)
Packet-transmit callback.
Definition: eth_plugin.h:215
virtual void ingress(PluginPacket &pkt)
Packet-received callback.
Definition: eth_plugin.h:207
A shared-memory Ethernet switch based on the MultiBuffer class.
Definition: eth_switch.h:93
Generic packetized I/O interface for use with SwitchCore.
Definition: eth_switch.h:245
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
Protocol handler for the Address Resolution Protocol (ARP)
Type definitions for Ethernet frames and protocol handlers.
Software-defined Ethernet switch.
#define SATCAT5_PMASK_TYPE
Set the integer type used to identify source and destination ports.
Definition: eth_switch.h:41
constexpr SATCAT5_PMASK_TYPE idx2mask(unsigned idx)
Macro converting port-index to a bit-mask.
Definition: eth_switch.h:53
Templated functions for manipulating singly-linked lists.
Address Resolution Protocol header.
Definition: eth_arp.h:23
An Ethernet frame header.
Definition: eth_header.h:167
satcat5::eth::MacType type
Ethernet header fields.
Definition: eth_header.h:172
Ephemeral data structure provided to plugin callbacks.
Definition: eth_plugin.h:48
satcat5::io::MultiPacket * pkt
Complete packet contents.
Definition: eth_plugin.h:51
bool is_udp() const
Accessors and shortcuts for packet metadata.
Definition: eth_plugin.h:135
satcat5::ip::Header ip
Copy of additional header fields, if present.
Definition: eth_plugin.h:62
bool is_ip() const
Accessors and shortcuts for packet metadata.
Definition: eth_plugin.h:129
void drop(u8 reason)
Drop this frame, indicating why.
Definition: eth_plugin.h:118
u8 reason() const
Accessors and shortcuts for packet metadata.
Definition: eth_plugin.h:145
satcat5::tcp::Header tcp
Copy of additional header fields, if present.
Definition: eth_plugin.h:63
constexpr PluginPacket()
Default constructor only, with no copy-constructor.
Definition: eth_plugin.h:85
unsigned src_port() const
Accessors and shortcuts for packet metadata.
Definition: eth_plugin.h:141
u16 hlen
Original header length.
Definition: eth_plugin.h:78
static const u16 FLAG_DIVERT
Packet diverted from normal processing.
Definition: eth_plugin.h:151
bool is_arp() const
Accessors and shortcuts for packet metadata.
Definition: eth_plugin.h:131
u16 flags
Additional status flags indicating packet status.
Definition: eth_plugin.h:81
bool is_adjusted() const
Accessors and shortcuts for packet metadata.
Definition: eth_plugin.h:125
unsigned length() const
Accessors and shortcuts for packet metadata.
Definition: eth_plugin.h:137
bool read_internal(satcat5::io::Readable *rd)
Internal use only.
SATCAT5_PMASK_TYPE dst_mask
Destination mask for which port(s) receive this packet.
Definition: eth_plugin.h:75
bool is_diverted() const
Accessors and shortcuts for packet metadata.
Definition: eth_plugin.h:127
SATCAT5_PMASK_TYPE src_mask() const
Accessors and shortcuts for packet metadata.
Definition: eth_plugin.h:139
satcat5::eth::VtagPolicy port_vcfg() const
Accessors and shortcuts for packet metadata.
Definition: eth_plugin.h:143
satcat5::eth::ArpHeader arp
Copy of additional header fields, if present.
Definition: eth_plugin.h:61
void divert()
Divert this frame for deferred processing.
Definition: eth_plugin.h:114
satcat5::eth::Header hdr
Copy of Ethernet header fields.
Definition: eth_plugin.h:55
bool is_tcp() const
Accessors and shortcuts for packet metadata.
Definition: eth_plugin.h:133
void write_to(satcat5::io::Writeable *wr) const
Copy packet headers to the specified destination.
Definition: eth_plugin.cc:42
bool read_from(satcat5::io::MultiPacket *packet)
Read metadata from a packet object.
Definition: eth_plugin.cc:20
static const u16 FLAG_HEADER_CHANGE
Header contents changed.
Definition: eth_plugin.h:153
void adjust()
Notify parent that header contents have changed.
Definition: eth_plugin.h:105
satcat5::udp::Header udp
Copy of additional header fields, if present.
Definition: eth_plugin.h:64
Data structure for configuring VLAN tagging policy of each port.
Definition: switch_cfg.h:68
A packet is a linked-list of memory blocks, plus metadata.
Definition: multi_buffer.h:91
unsigned m_length
Packet length in bytes.
Definition: multi_buffer.h:102
u32 m_user[SATCAT5_MBUFF_USER]
Packet metadata.
Definition: multi_buffer.h:107
Structure for holding an IPv4 Header, including options.
Definition: ip_core.h:180
constexpr u8 proto() const
< Inner protocol (UDP/TCP/etc.)
Definition: ip_core.h:204
TCP header contents.
Definition: tcp_core.h:34
UDP header contents.
Definition: udp_core.h:48
Type definitions for manipulating TCP segments.
Basic type aliases and prototypes used throughout SatCat5.