SatCat5
eth_plugin.cc
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 
6 #include <satcat5/eth_plugin.h>
7 #include <satcat5/eth_switch.h>
8 #include <satcat5/log.h>
9 #include <satcat5/utils.h>
10 
19 
20 bool PluginPacket::read_from(MultiPacket* packet) {
21  // Set basic parameters.
22  pkt = packet;
24  flags = 0;
25  hlen = 0;
26 
27  // Create a new Reader object, so we can peek at frame-headers.
28  // TODO: ARP parser only accepts Ethernet/IPv4 ARP.
29  // Are we likely to encounter anything else in the wild?
30  MultiPacket::Reader rd(packet);
31  if (!hdr.read_from(&rd)) return false;
32  if (is_arp() && !arp.read_from(&rd)) return false;
33  if (is_ip() && !ip.read_from(&rd)) return false;
34  if (is_tcp() && !tcp.read_from(&rd)) return false;
35  if (is_udp() && !udp.read_from(&rd)) return false;
36 
37  // Calculate header length from number of bytes consumed.
38  hlen = u16(packet->m_length - rd.get_read_ready());
39  return true;
40 }
41 
43  hdr.write_to(wr);
44  if (is_arp()) arp.write_to(wr);
45  if (is_ip()) ip.write_to(wr);
46  if (is_tcp()) tcp.write_to(wr);
47  if (is_udp()) udp.write_to(wr);
48 }
49 
51  : m_switch(sw), m_next(0)
52 {
53  if (m_switch) m_switch->plugin_add(this);
54 }
55 
56 #if SATCAT5_ALLOW_DELETION
57 PluginCore::~PluginCore() {
58  if (m_switch) m_switch->plugin_remove(this);
59 }
60 #endif
61 
63  : m_port(port), m_next(0)
64 {
65  if (m_port) m_port->plugin_add(this);
66 }
67 
68 #if SATCAT5_ALLOW_DELETION
69 PluginPort::~PluginPort() {
70  if (m_port) m_port->plugin_remove(this);
71 }
72 #endif
73 
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
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
PluginPort(satcat5::eth::SwitchPort *port)
Associate this plugin object with the designated port.
Definition: eth_plugin.cc:62
satcat5::eth::SwitchPort *const m_port
Pointer to the associated SwitchPort object.
Definition: eth_plugin.h:224
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
void plugin_remove(satcat5::eth::PluginPort *plugin)
Plugin management.
Definition: eth_switch.cc:297
void plugin_add(satcat5::eth::PluginPort *plugin)
Plugin management.
Definition: eth_switch.cc:295
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
Plugins for the software-defined Ethernet switch and IPv4 router.
Software-defined Ethernet switch.
constexpr SATCAT5_PMASK_TYPE PMASK_ALL(-1)
Global port-mask indicating every port (i.e., broadcast).
Diagnostic logging to UART and/or Ethernet ports.
void write_to(satcat5::io::Writeable *wr) const
Write header contents to the specified destination.
Definition: eth_arp.cc:63
bool read_from(satcat5::io::Readable *rd)
Attempt to read and validate the header.
Definition: eth_arp.cc:30
void write_to(satcat5::io::Writeable *wr) const
Write Ethernet header to the designated stream.
Definition: eth_header.cc:117
bool read_from(satcat5::io::Readable *rd)
Read Ethernet header from the designated stream.
Definition: eth_header.cc:128
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
satcat5::tcp::Header tcp
Copy of additional header fields, if present.
Definition: eth_plugin.h:63
u16 hlen
Original header length.
Definition: eth_plugin.h:78
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
SATCAT5_PMASK_TYPE dst_mask
Destination mask for which port(s) receive this packet.
Definition: eth_plugin.h:75
satcat5::eth::ArpHeader arp
Copy of additional header fields, if present.
Definition: eth_plugin.h:61
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
satcat5::udp::Header udp
Copy of additional header fields, if present.
Definition: eth_plugin.h:64
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
void write_to(satcat5::io::Writeable *wr) const
Write IPv4 header to the designated stream.
Definition: ip_core.cc:82
bool read_from(satcat5::io::Readable *rd)
Read an entire IPv4 header from the designated stream.
Definition: ip_core.cc:127
void write_to(satcat5::io::Writeable *wr) const
Write TCP header to the designated stream.
Definition: tcp_core.cc:24
bool read_from(satcat5::io::Readable *rd)
Read TCP header from the designated stream.
Definition: tcp_core.cc:42
void write_to(satcat5::io::Writeable *wr) const
Write UDP header to the designated stream.
Definition: udp_core.cc:83
bool read_from(satcat5::io::Readable *rd)
Read UDP header from the designated stream.
Definition: udp_core.cc:91
Miscellaneous mathematical utility functions.