SatCat5
eth_sw_cache.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_sw_cache.h>
7 #include <satcat5/eth_sw_log.h>
8 #include <satcat5/utils.h>
9 
16 
17 SwitchCacheInner::SwitchCacheInner(SwitchCore* sw, CacheEntry* array, unsigned size)
18  : PluginCore(sw)
19  , m_learn(true)
20  , m_miss_mask(PMASK_ALL)
21  , m_array(array)
22  , m_size(size)
23  , m_cache(array, size)
24 {
25  // Nothing else to initialize.
26 }
27 
29  // Preemptively reject any packet with an invalid source.
30  if (pkt.hdr.src == satcat5::eth::MACADDR_NONE) return 0;
31  if (pkt.hdr.src.is_multicast()) return 0;
32 
33  // Check special-case destination addresses.
34  if (pkt.hdr.dst == satcat5::eth::MACADDR_NONE) return 0;
35  if (pkt.hdr.dst.is_swcontrol()) return 0;
36  if (pkt.hdr.dst.is_multicast()) return PMASK_ALL;
37 
38  // Otherwise, attempt MAC-address lookup.
39  CacheEntry* dst = m_cache.find(pkt.hdr.dst.to_u64());
40  return dst ? (1u << dst->m_port) : m_miss_mask;
41 }
42 
44  // Update our cached entry for this source address?
45  if (m_learn && pkt.hdr.src.is_unicast()) {
46  CacheEntry* src = m_cache.query(pkt.hdr.src.to_u64());
47  src->m_port = pkt.src_port();
48  }
49 
50  // Update the destination mask and proceed with delivery.
51  pkt.dst_mask &= destination_mask(pkt);
53 }
54 
55 void SwitchCacheInner::set_miss_bcast(unsigned port_idx, bool enable) {
56  SATCAT5_PMASK_TYPE mask = idx2mask(port_idx);
57  satcat5::util::set_mask_if(m_miss_mask, mask, enable);
58 }
59 
60 bool SwitchCacheInner::mactbl_read(unsigned tbl_idx, unsigned& port_idx, MacAddr& mac_addr) {
61  if (tbl_idx >= m_size) return false;
62  port_idx = m_array[tbl_idx].m_port;
63  mac_addr = MacAddr::from_u64(m_array[tbl_idx].m_key);
64  return true;
65 }
66 
67 bool SwitchCacheInner::mactbl_write(unsigned port_idx, const MacAddr& mac_addr) {
68  // Sanity check: Do not allow user to write reserved MAC addresses.
69  if (mac_addr == satcat5::eth::MACADDR_NONE) return false;
70  if (mac_addr == satcat5::eth::MACADDR_BROADCAST) return false;
71 
72  // Otherwise, add the requested address to the cache.
73  CacheEntry* tmp = m_cache.query(mac_addr.to_u64());
74  tmp->m_port = port_idx;
75  return true;
76 }
Ethernet switch plugin API.
Definition: eth_plugin.h:166
MAC-address cache plugin for the software-defined Ethernet switch.
Definition: eth_sw_cache.h:25
void set_miss_bcast(unsigned port_idx, bool enable)
Enable or disable "miss-as-broadcast" flag for a specific port.
Definition: eth_sw_cache.cc:55
bool mactbl_read(unsigned tbl_idx, unsigned &port_idx, satcat5::eth::MacAddr &mac_addr)
Read or manipulate the contents of the MAC-address table.
Definition: eth_sw_cache.cc:60
SATCAT5_PMASK_TYPE destination_mask(const PluginPacket &pkt)
Destination MAC-address lookup.
Definition: eth_sw_cache.cc:28
bool mactbl_write(unsigned port_idx, const satcat5::eth::MacAddr &mac_addr)
Read or manipulate the contents of the MAC-address table.
Definition: eth_sw_cache.cc:67
void query(satcat5::eth::PluginPacket &pkt) override
Implement the required SwitchPlugin API.
Definition: eth_sw_cache.cc:43
A shared-memory Ethernet switch based on the MultiBuffer class.
Definition: eth_switch.h:93
Diagnostic logging system for the 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
constexpr SATCAT5_PMASK_TYPE PMASK_ALL(-1)
Global port-mask indicating every port (i.e., broadcast).
An Ethernet frame header.
Definition: eth_header.h:167
satcat5::eth::MacAddr src
Ethernet header fields.
Definition: eth_header.h:171
satcat5::eth::MacAddr dst
Ethernet header fields.
Definition: eth_header.h:170
An Ethernet MAC address (with serializable interface).
Definition: eth_header.h:29
static constexpr satcat5::eth::MacAddr from_u64(u64 x)
Convert from u64 to MacAddr.
Definition: eth_header.h:38
bool is_swcontrol() const
Link-local control (01:80:C2:00:00:*)
Definition: eth_header.cc:63
bool is_multicast() const
Broadcast, L2 multicast, or L3 multicast.
Definition: eth_header.cc:57
bool is_unicast() const
Any normal single-destination address.
Definition: eth_header.cc:74
constexpr u64 to_u64() const
Convert from MacAddr to u64.
Definition: eth_header.h:44
Ephemeral data structure provided to plugin callbacks.
Definition: eth_plugin.h:48
void drop(u8 reason)
Drop this frame, indicating why.
Definition: eth_plugin.h:118
unsigned src_port() const
Accessors and shortcuts for packet metadata.
Definition: eth_plugin.h:141
SATCAT5_PMASK_TYPE dst_mask
Destination mask for which port(s) receive this packet.
Definition: eth_plugin.h:75
satcat5::eth::Header hdr
Copy of Ethernet header fields.
Definition: eth_plugin.h:55
Data structure for the internal cache.
Definition: eth_sw_cache.h:71
unsigned m_port
Associated port index.
Definition: eth_sw_cache.h:78
A single 24-byte packet-log message.
Definition: eth_sw_log.h:74
static constexpr u8 DROP_NO_ROUTE
No destination or null route.
Definition: eth_sw_log.h:86
Miscellaneous mathematical utility functions.