bmv2
Designing your own switch target with bmv2
Loading...
Searching...
No Matches
debugger.h
1/* Copyright 2013-present Barefoot Networks, Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://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,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/*
17 * Antonin Bas (antonin@barefootnetworks.com)
18 *
19 */
20
21#ifndef BM_BM_SIM_DEBUGGER_H_
22#define BM_BM_SIM_DEBUGGER_H_
23
24#include <bm/config.h>
25
26#include <cstdint>
27#include <limits>
28#include <memory>
29#include <string>
30
31namespace bm {
32
33using device_id_t = uint64_t;
34using s_device_id_t = device_id_t;
35
36/* This whole code if for a proof of concept and is temporary */
37
38// forward declaration
39class DebuggerIface;
40
41// TODO(antonin)
42// temprorary: experimenting with debugger
43#define DBG_CTR_PARSER (1 << 24)
44#define DBG_CTR_PARSE_STATE (2 << 24)
45#define DBG_CTR_CONTROL (3 << 24)
46#define DBG_CTR_TABLE (4 << 24)
47#define DBG_CTR_CONDITION (5 << 24)
48#define DBG_CTR_ACTION (6 << 24)
49#define DBG_CTR_DEPARSER (7 << 24)
50
51#define DBG_CTR_EXIT(x) (x | (0x80 << 24))
52
53class Debugger {
54 public:
55 static constexpr uint64_t FIELD_COND =
56 std::numeric_limits<uint64_t>::max() - 1;
57
58 static constexpr uint64_t FIELD_ACTION =
59 std::numeric_limits<uint64_t>::max() - 2;
60
61 struct PacketId {
62 uint64_t packet_id;
63 uint64_t copy_id;
64
65 bool operator==(const PacketId& other) const {
66 return packet_id == other.packet_id && copy_id == other.copy_id;
67 }
68
69 bool operator!=(const PacketId& other) const {
70 return !(*this == other);
71 }
72
73 static PacketId make(uint64_t packet_id, uint64_t copy_id) {
74 return {packet_id, copy_id};
75 }
76 };
77
78 static void init_debugger(const std::string &addr, device_id_t device_id);
79
80 static DebuggerIface *get() {
81 return debugger;
82 }
83
84 // returns an empty string if debugger wasn't initialized
85 static std::string get_addr();
86
87 static DebuggerIface *debugger;
88
89 static constexpr PacketId dummy_PacketId{0u, 0u};
90
91 private:
92 static bool is_init;
93};
94
95class DebuggerIface {
96 public:
97 using PacketId = Debugger::PacketId;
98
99 void notify_update(const PacketId &packet_id,
100 uint64_t id, const char *bytes, int nbits) {
101 notify_update_(packet_id, id, bytes, nbits);
102 }
103
104 void notify_update(const PacketId &packet_id,
105 uint64_t id, uint32_t v) {
106 notify_update_(packet_id, id, v);
107 }
108
109 void notify_ctr(const PacketId &packet_id, uint32_t ctr) {
110 notify_ctr_(packet_id, ctr);
111 }
112
113 void packet_in(const PacketId &packet_id, int port) {
114 packet_in_(packet_id, port);
115 }
116
117 void packet_out(const PacketId &packet_id, int port) {
118 packet_out_(packet_id, port);
119 }
120
121 void config_change() {
122 config_change_();
123 }
124
125 std::string get_addr() const {
126 return get_addr_();
127 }
128
129 protected:
130 ~DebuggerIface() { }
131
132 private:
133 virtual void notify_update_(const PacketId &packet_id, uint64_t id,
134 const char *bytes, int nbits) = 0;
135
136 virtual void notify_update_(const PacketId &packet_id, uint64_t id,
137 uint32_t v) = 0;
138
139 virtual void notify_ctr_(const PacketId &packet_id, uint32_t ctr) = 0;
140
141 virtual void packet_in_(const PacketId &packet_id, int port) = 0;
142
143 virtual void packet_out_(const PacketId &packet_id, int port) = 0;
144
145 virtual void config_change_() = 0;
146
147 virtual std::string get_addr_() const = 0;
148};
149
150#ifdef BM_DEBUG_ON
151#define DEBUGGER_NOTIFY_UPDATE(packet_id, id, bytes, nbits) \
152 Debugger::get()->notify_update(packet_id, id, bytes, nbits);
153#define DEBUGGER_NOTIFY_UPDATE_V(packet_id, id, v) \
154 Debugger::get()->notify_update(packet_id, id, v);
155#define DEBUGGER_NOTIFY_CTR(packet_id, ctr) \
156 Debugger::get()->notify_ctr(packet_id, ctr);
157#define DEBUGGER_PACKET_IN(packet_id, port) \
158 Debugger::get()->packet_in(packet_id, port);
159#define DEBUGGER_PACKET_OUT(packet_id, port) \
160 Debugger::get()->packet_out(packet_id, port);
161#else
162#define DEBUGGER_NOTIFY_UPDATE(packet_id, id, bytes, nbits)
163#define DEBUGGER_NOTIFY_UPDATE_V(packet_id, id, v)
164#define DEBUGGER_NOTIFY_CTR(packet_id, ctr)
165#define DEBUGGER_PACKET_IN(packet_id, port)
166#define DEBUGGER_PACKET_OUT(packet_id, port)
167#endif
168
169} // namespace bm
170
171#endif // BM_BM_SIM_DEBUGGER_H_