1 // Copyright 2021 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
18 #include "pw_bytes/span.h"
19 #include "pw_metric/metric.h"
20 #include "pw_router/egress.h"
21 #include "pw_router/packet_parser.h"
22 #include "pw_status/status.h"
23 #include "pw_sync/mutex.h"
25 namespace pw::router {
27 // A packet router with a static routing table.
30 // Internal packet parsing and calls to the provided PacketParser are
31 // synchronized. Synchronization at the egress level must be implemented by
37 // TODO(frolv): Consider making address size configurable.
42 StaticRouter(PacketParser& parser, std::span<const Route> routes)
43 : parser_(parser), routes_(routes) {}
45 StaticRouter(const StaticRouter&) = delete;
46 StaticRouter(StaticRouter&&) = delete;
47 StaticRouter& operator=(const StaticRouter&) = delete;
48 StaticRouter& operator=(StaticRouter&&) = delete;
50 uint32_t dropped_packets() const {
51 return parser_errors_.value() + route_errors_.value() +
52 egress_errors_.value();
55 const metric::Group& metrics() { return metrics_; }
57 // Routes a single packet through the appropriate egress.
58 // Returns one of the following to indicate a router-side error:
60 // OK - Packet sent successfully.
61 // DATA_LOSS - Packet corrupt or incomplete.
62 // NOT_FOUND - No registered route for the packet.
63 // UNAVAILABLE - Route egress did not accept packet.
65 Status RoutePacket(ConstByteSpan packet);
68 PacketParser& parser_;
69 std::span<const Route> routes_;
71 PW_METRIC_GROUP(metrics_, "static_router");
72 PW_METRIC(metrics_, parser_errors_, "parser_errors", 0u);
73 PW_METRIC(metrics_, route_errors_, "route_errors", 0u);
74 PW_METRIC(metrics_, egress_errors_, "egress_errors", 0u);
77 } // namespace pw::router