blob: 0177e56ea2b0b760961e94f0853686af778e053a [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
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#ifndef __VOM_L3_BINDING_H__
17#define __VOM_L3_BINDING_H__
18
Neale Ranns812ed392017-10-16 04:20:13 -070019#include "vom/hw.hpp"
20#include "vom/inspect.hpp"
21#include "vom/interface.hpp"
22#include "vom/object_base.hpp"
23#include "vom/om.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070024#include "vom/singular_db.hpp"
25
Neale Ranns812ed392017-10-16 04:20:13 -070026namespace VOM {
27/**
28 * A representation of L3 configuration on an interface
29 */
30class l3_binding : public object_base
31{
32public:
33 /**
Neale Rannsfd920602017-11-23 12:15:00 -080034 * The key type for l3_bindings
35 */
36 typedef std::pair<interface::key_t, route::prefix_t> key_t;
37
38 /**
Neale Ranns812ed392017-10-16 04:20:13 -070039 * Construct a new object matching the desried state
40 */
41 l3_binding(const interface& itf, const route::prefix_t& pfx);
42
43 /**
44 * Copy Constructor
45 */
46 l3_binding(const l3_binding& o);
47
48 /**
49 * Destructor
50 */
51 ~l3_binding();
52
53 /**
Neale Rannsfd920602017-11-23 12:15:00 -080054 * Comparison operator
Neale Ranns352ea0c2017-11-14 11:04:28 -080055 */
Neale Rannsfd920602017-11-23 12:15:00 -080056 bool operator==(const l3_binding& l) const;
57
58 /**
59 * Get the object's key
60 */
61 const key_t key() const;
Neale Ranns352ea0c2017-11-14 11:04:28 -080062
63 /**
64 * The iterator type
65 */
Neale Rannsfd920602017-11-23 12:15:00 -080066 typedef singular_db<key_t, l3_binding>::const_iterator const_iterator_t;
Neale Ranns352ea0c2017-11-14 11:04:28 -080067
68 static const_iterator_t cbegin();
69 static const_iterator_t cend();
70
71 /**
Neale Ranns812ed392017-10-16 04:20:13 -070072 * Return the 'singular instance' of the L3-Config that matches this
73 * object
74 */
75 std::shared_ptr<l3_binding> singular() const;
76
77 /**
78 * convert to string format for debug purposes
79 */
80 std::string to_string() const;
81
82 /**
Neale Ranns352ea0c2017-11-14 11:04:28 -080083 * Return the prefix associated with this L3 binding
Neale Ranns812ed392017-10-16 04:20:13 -070084 */
85 const route::prefix_t& prefix() const;
86
87 /**
Neale Ranns352ea0c2017-11-14 11:04:28 -080088 * Return the interface associated with this L3 binding
89 */
90 const interface& itf() const;
91
92 /**
Neale Ranns812ed392017-10-16 04:20:13 -070093 * Dump all l3_bindings into the stream provided
94 */
95 static void dump(std::ostream& os);
96
97 /**
Neale Rannsfd920602017-11-23 12:15:00 -080098 * Find all bindings in the DB for the interface passed
Neale Ranns812ed392017-10-16 04:20:13 -070099 */
100 static std::deque<std::shared_ptr<l3_binding>> find(const interface& i);
101
Neale Rannsfd920602017-11-23 12:15:00 -0800102 /**
103 * Find a binding from its key
104 */
105 static std::shared_ptr<l3_binding> find(const key_t& k);
106
Neale Ranns812ed392017-10-16 04:20:13 -0700107private:
108 /**
109 * Class definition for listeners to OM events
110 */
111 class event_handler : public OM::listener, public inspect::command_handler
112 {
113 public:
114 event_handler();
115 virtual ~event_handler() = default;
116
117 /**
118 * Handle a populate event
119 */
120 void handle_populate(const client_db::key_t& key);
121
122 /**
123 * Handle a replay event
124 */
125 void handle_replay();
126
127 /**
128 * Show the object in the Singular DB
129 */
130 void show(std::ostream& os);
131
132 /**
133 * Get the sortable Id of the listener
134 */
135 dependency_t order() const;
136 };
137
138 /**
139 * event_handler to register with OM
140 */
141 static event_handler m_evh;
142
143 /**
144 * Enquue commonds to the VPP command Q for the update
145 */
146 void update(const l3_binding& obj);
147
148 /**
149 * Find or add the singular instance in the DB
150 */
151 static std::shared_ptr<l3_binding> find_or_add(const l3_binding& temp);
152
153 /*
154 * It's the VPPHW class that updates the objects in HW
155 */
156 friend class OM;
157
158 /**
159 e* It's the singular_db class that calls replay()
160 */
Neale Rannsfd920602017-11-23 12:15:00 -0800161 friend class singular_db<key_t, l3_binding>;
Neale Ranns812ed392017-10-16 04:20:13 -0700162
163 /**
164 * Sweep/reap the object if still stale
165 */
166 void sweep(void);
167
168 /**
169 * replay the object to create it in hardware
170 */
171 void replay(void);
172
Neale Ranns352ea0c2017-11-14 11:04:28 -0800173 friend class interface;
174
Neale Ranns812ed392017-10-16 04:20:13 -0700175 /**
176 * A reference counting pointer the interface that this L3 layer
177 * represents. By holding the reference here, we can guarantee that
178 * this object will outlive the interface
179 */
180 const std::shared_ptr<interface> m_itf;
181
182 /**
183 * The prefix for this L3 configuration
184 */
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800185 const route::prefix_t m_pfx;
Neale Ranns812ed392017-10-16 04:20:13 -0700186
187 /**
188 * HW configuration for the binding. The bool representing the
189 * do/don't bind.
190 */
191 HW::item<bool> m_binding;
192
193 /**
194 * A map of all L3 configs keyed against a combination of the interface
195 * and subnet's keys.
196 */
Neale Rannsfd920602017-11-23 12:15:00 -0800197 static singular_db<key_t, l3_binding> m_db;
Neale Ranns812ed392017-10-16 04:20:13 -0700198};
199
200/**
201 * Ostream output for the key
202 */
Neale Rannsfd920602017-11-23 12:15:00 -0800203std::ostream& operator<<(std::ostream& os, const l3_binding::key_t& key);
Neale Ranns812ed392017-10-16 04:20:13 -0700204};
205
206/*
207 * fd.io coding-style-patch-verification: ON
208 *
209 * Local Variables:
210 * eval: (c-set-style "mozilla")
211 * End:
212 */
213
214#endif