xref: /aosp_15_r20/external/pigweed/pw_router/public/pw_router/static_router.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #include "pw_bytes/span.h"
17 #include "pw_metric/metric.h"
18 #include "pw_router/egress.h"
19 #include "pw_router/packet_parser.h"
20 #include "pw_span/span.h"
21 #include "pw_status/status.h"
22 
23 namespace pw::router {
24 
25 // A packet router with a static routing table.
26 //
27 // Thread-safety:
28 //   Internal packet parsing and calls to the provided PacketParser are
29 //   synchronized. Synchronization at the egress level must be implemented by
30 //   derived egresses.
31 //
32 class StaticRouter {
33  public:
34   struct Route {
35     // TODO(frolv): Consider making address size configurable.
36     uint32_t address;
37     Egress& egress;
38   };
39 
StaticRouter(span<const Route> routes)40   StaticRouter(span<const Route> routes) : routes_(routes) {}
41 
42   StaticRouter(const StaticRouter&) = delete;
43   StaticRouter(StaticRouter&&) = delete;
44   StaticRouter& operator=(const StaticRouter&) = delete;
45   StaticRouter& operator=(StaticRouter&&) = delete;
46 
dropped_packets()47   uint32_t dropped_packets() const {
48     return parser_errors_.value() + route_errors_.value() +
49            egress_errors_.value();
50   }
51 
metrics()52   const metric::Group& metrics() { return metrics_; }
53 
54   // Routes a single packet through the appropriate egress.
55   // Returns one of the following to indicate a router-side error:
56   //
57   //   OK - Packet sent successfully.
58   //   DATA_LOSS - Packet corrupt or incomplete.
59   //   NOT_FOUND - No registered route for the packet.
60   //   UNAVAILABLE - Route egress did not accept packet.
61   //
62   Status RoutePacket(ConstByteSpan packet, PacketParser& parser);
63 
64  private:
65   const span<const Route> routes_;
66   PW_METRIC_GROUP(metrics_, "static_router");
67   PW_METRIC(metrics_, parser_errors_, "parser_errors", 0u);
68   PW_METRIC(metrics_, route_errors_, "route_errors", 0u);
69   PW_METRIC(metrics_, egress_errors_, "egress_errors", 0u);
70 };
71 
72 }  // namespace pw::router
73