• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <span>
17 
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 
24 namespace pw::router {
25 
26 // A packet router with a static routing table.
27 //
28 // Thread-safety:
29 //   Internal packet parsing and calls to the provided PacketParser are
30 //   synchronized. Synchronization at the egress level must be implemented by
31 //   derived egresses.
32 //
33 class StaticRouter {
34  public:
35   struct Route {
36     // TODO(frolv): Consider making address size configurable.
37     uint32_t address;
38     Egress& egress;
39   };
40 
StaticRouter(std::span<const Route> routes)41   StaticRouter(std::span<const Route> routes) : routes_(routes) {}
42 
43   StaticRouter(const StaticRouter&) = delete;
44   StaticRouter(StaticRouter&&) = delete;
45   StaticRouter& operator=(const StaticRouter&) = delete;
46   StaticRouter& operator=(StaticRouter&&) = delete;
47 
dropped_packets()48   uint32_t dropped_packets() const {
49     return parser_errors_.value() + route_errors_.value() +
50            egress_errors_.value();
51   }
52 
metrics()53   const metric::Group& metrics() { return metrics_; }
54 
55   // Routes a single packet through the appropriate egress.
56   // Returns one of the following to indicate a router-side error:
57   //
58   //   OK - Packet sent successfully.
59   //   DATA_LOSS - Packet corrupt or incomplete.
60   //   NOT_FOUND - No registered route for the packet.
61   //   UNAVAILABLE - Route egress did not accept packet.
62   //
63   Status RoutePacket(ConstByteSpan packet, PacketParser& parser);
64 
65  private:
66   const std::span<const Route> routes_;
67   PW_METRIC_GROUP(metrics_, "static_router");
68   PW_METRIC(metrics_, parser_errors_, "parser_errors", 0u);
69   PW_METRIC(metrics_, route_errors_, "route_errors", 0u);
70   PW_METRIC(metrics_, egress_errors_, "egress_errors", 0u);
71 };
72 
73 }  // namespace pw::router
74