SatCat5
router2_table.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/router2_table.h>
7 
11 
12 // Register map defined in "router2_common.vhd"
13 static constexpr unsigned REG_CTRL = 509;
14 static constexpr unsigned REG_DATA = 508;
15 
16 // Bit masks for the control register.
17 static constexpr u32 MASK_BUSY = (1u << 31);
18 static constexpr u32 MASK_SIZE = 0xFFFF;
19 static constexpr u32 OPCODE_WRITE = (1u << 28);
20 static constexpr u32 OPCODE_DROUTE = (2u << 28);
21 static constexpr u32 OPCODE_CLEAR = (3u << 28);
22 
23 Table::Table(ConfigBus* cfg, unsigned devaddr)
24  : m_cfg(cfg->get_register(devaddr))
25 {
26  m_cfg[REG_CTRL] = OPCODE_CLEAR;
27 }
28 
29 unsigned Table::table_size() {
30  return unsigned(m_cfg[REG_CTRL] & MASK_SIZE);
31 }
32 
33 bool Table::route_wrdef(const Route& route) {
34  // Attempt write to the software table, mirror if successful.
36  && route_load(OPCODE_DROUTE, route);
37 }
38 
39 bool Table::route_write(unsigned idx, const Route& route) {
40  // Attempt write to the software table, mirror if successful.
41  return satcat5::ip::Table::route_write(idx, route)
42  && route_load(OPCODE_WRITE + idx, route);
43 
44 }
45 
46 bool Table::route_load(u32 opcode, const Route& route) {
47  // Extract parameters of interest...
48  u64 dmac = route.dstmac.to_u64();
49  u32 pfix = route.subnet.prefix();
50  u32 port = route.port;
51 
52  // Wait until hardware is idle/ready.
53  // (This should only take a few microseconds...)
54  while (m_cfg[REG_CTRL] & MASK_BUSY) {}
55 
56  // Copy the new entry to the hardware table.
57  m_cfg[REG_DATA] = u32((pfix << 24) | (port << 16) | (dmac >> 32));
58  m_cfg[REG_DATA] = u32(dmac & 0xFFFFFFFFu);
59  m_cfg[REG_DATA] = u32(route.subnet.addr.value);
60  m_cfg[REG_CTRL] = opcode;
61  return true;
62 }
Generic ConfigBus API.
Definition: cfgbus_core.h:124
virtual bool route_wrdef(const satcat5::ip::Route &route)
Internal methods used to access "m_route_default".
Definition: ip_table.cc:229
virtual bool route_write(unsigned idx, const satcat5::ip::Route &route)
Internal methods used to access "m_route_table".
Definition: ip_table.cc:234
IPv4 forwarding table with hardware mirroring.
Definition: router2_table.h:22
unsigned table_size()
Read the size of the hardware table.
bool route_wrdef(const satcat5::ip::Route &route) override
Internal methods used to access "m_route_default".
bool route_write(unsigned idx, const satcat5::ip::Route &route) override
Internal methods used to access "m_route_table".
constexpr u64 to_u64() const
Convert from MacAddr to u64.
Definition: eth_header.h:44
u32 value
Raw access to the underlying representation.
Definition: ip_core.h:17
A single entry in the static routing table.
Definition: ip_table.h:57
satcat5::eth::MacAddr dstmac
Next-hop MAC address.
Definition: ip_table.h:61
satcat5::ip::Subnet subnet
Subnet address + mask.
Definition: ip_table.h:59
u8 port
Next-hop port number.
Definition: ip_table.h:62
unsigned prefix() const
Get the CIDR prefix length for this subnet.
Definition: ip_core.h:111
satcat5::ip::Addr addr
Base address.
Definition: ip_core.h:89