bmv2
Designing your own switch target with bmv2
Loading...
Searching...
No Matches
port_monitor.h
Go to the documentation of this file.
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 * Hari Thantry (thantry@barefootnetworks.com)
18 * Antonin Bas (antonin@barefootnetworks.com)
19 *
20 */
21
23
24#ifndef BM_BM_SIM_PORT_MONITOR_H_
25#define BM_BM_SIM_PORT_MONITOR_H_
26
27#include <functional>
28#include <memory>
29
30#include "device_id.h"
31
32namespace bm {
33
34class TransportIface;
35
38 public:
40 using port_t = uint32_t;
41
43 enum class PortStatus { PORT_ADDED, PORT_REMOVED, PORT_UP, PORT_DOWN };
44
46 using PortStatusCb = std::function<void(port_t port_num,
47 const PortStatus status)>;
48
49 using PortStatusFn = std::function<bool(port_t port_num)>;
50
51 // putting it in TransportIface so that it is visible (e.g. for tests)
52 struct msg_hdr_t {
53 char sub_topic[4];
54 s_device_id_t switch_id;
55 unsigned int num_statuses;
56 char _padding[16]; // the header size for notifications is always 32 bytes
57 } __attribute__((packed));
58
59 struct one_status_t {
60 int port;
61 int status;
62 } __attribute__((packed));
63
64 void notify(port_t port_num, const PortStatus evt) {
65 notify_(port_num, evt);
66 }
67
68 void register_cb(const PortStatus evt, const PortStatusCb &cb) {
69 register_cb_(evt, cb);
70 }
71
72 void start(const PortStatusFn &fn) {
73 start_(fn);
74 }
75
76 void stop() {
77 stop_();
78 }
79
80 virtual ~PortMonitorIface() { }
81
82 // all calls are no-op
83 static std::unique_ptr<PortMonitorIface> make_dummy();
84 // a passive monitor does not periodically query the port status
85 static std::unique_ptr<PortMonitorIface> make_passive(
86 device_id_t device_id,
87 std::shared_ptr<TransportIface> notifications_writer = nullptr);
88 // an active monitor periodically queries the port status (using separate
89 // thread)
90 static std::unique_ptr<PortMonitorIface> make_active(
91 device_id_t device_id,
92 std::shared_ptr<TransportIface> notifications_writer = nullptr);
93
94 private:
95 virtual void notify_(port_t port_num, const PortStatus evt) = 0;
96
97 virtual void register_cb_(const PortStatus evt, const PortStatusCb &cb) = 0;
98
99 virtual void start_(const PortStatusFn &fn) = 0;
100
101 virtual void stop_() = 0;
102};
103
104} // namespace bm
105
106#endif // BM_BM_SIM_PORT_MONITOR_H_
Used by DevMgr to monitor the ports attached to the switch.
Definition port_monitor.h:37
PortStatus
Represents the status of a port.
Definition port_monitor.h:43
uint32_t port_t
Representation of a port number.
Definition port_monitor.h:40
std::function< void(port_t port_num, const PortStatus status)> PortStatusCb
Signature of the cb function to call when a port status changes.
Definition port_monitor.h:47