4041e31535288edb7a9dbd16becd4e5cb06c4366
[vpp.git] / src / vpp-api / vom / bridge_domain_entry.cpp
1 /*
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 #include "vom/bridge_domain_entry.hpp"
17
18 namespace VOM {
19 singular_db<bridge_domain_entry::key_t, bridge_domain_entry>
20   bridge_domain_entry::m_db;
21
22 bridge_domain_entry::event_handler bridge_domain_entry::m_evh;
23
24 bridge_domain_entry::bridge_domain_entry(const bridge_domain& bd,
25                                          const mac_address_t& mac,
26                                          const interface& tx_itf)
27   : m_hw(false)
28   , m_mac(mac)
29   , m_bd(bd.singular())
30   , m_tx_itf(tx_itf.singular())
31 {
32 }
33
34 bridge_domain_entry::bridge_domain_entry(const mac_address_t& mac,
35                                          const interface& tx_itf)
36   : m_hw(false)
37   , m_mac(mac)
38   , m_bd(nullptr)
39   , m_tx_itf(tx_itf.singular())
40 {
41   /*
42  * the route goes in the default table
43  */
44   bridge_domain bd(bridge_domain::DEFAULT_TABLE);
45
46   m_bd = bd.singular();
47 }
48
49 bridge_domain_entry::bridge_domain_entry(const bridge_domain_entry& bde)
50   : m_hw(bde.m_hw)
51   , m_mac(bde.m_mac)
52   , m_bd(bde.m_bd)
53   , m_tx_itf(bde.m_tx_itf)
54 {
55 }
56
57 bridge_domain_entry::~bridge_domain_entry()
58 {
59   sweep();
60
61   // not in the DB anymore.
62   m_db.release(std::make_pair(m_bd->id(), m_mac), this);
63 }
64
65 void
66 bridge_domain_entry::sweep()
67 {
68   if (m_hw) {
69     HW::enqueue(new delete_cmd(m_hw, m_mac, m_bd->id()));
70   }
71   HW::write();
72 }
73
74 void
75 bridge_domain_entry::replay()
76 {
77   if (m_hw) {
78     HW::enqueue(new create_cmd(m_hw, m_mac, m_bd->id(), m_tx_itf->handle()));
79   }
80 }
81 std::string
82 bridge_domain_entry::to_string() const
83 {
84   std::ostringstream s;
85   s << "bridge-domain-entry:[" << m_bd->to_string() << ", " << m_mac.to_string()
86     << ", tx:" << m_tx_itf->name() << "]";
87
88   return (s.str());
89 }
90
91 void
92 bridge_domain_entry::update(const bridge_domain_entry& r)
93 {
94   /*
95  * create the table if it is not yet created
96  */
97   if (rc_t::OK != m_hw.rc()) {
98     HW::enqueue(new create_cmd(m_hw, m_mac, m_bd->id(), m_tx_itf->handle()));
99   }
100 }
101
102 std::shared_ptr<bridge_domain_entry>
103 bridge_domain_entry::find_or_add(const bridge_domain_entry& temp)
104 {
105   return (m_db.find_or_add(std::make_pair(temp.m_bd->id(), temp.m_mac), temp));
106 }
107
108 std::shared_ptr<bridge_domain_entry>
109 bridge_domain_entry::singular() const
110 {
111   return find_or_add(*this);
112 }
113
114 void
115 bridge_domain_entry::dump(std::ostream& os)
116 {
117   m_db.dump(os);
118 }
119
120 bridge_domain_entry::event_handler::event_handler()
121 {
122   OM::register_listener(this);
123   inspect::register_handler({ "bd-entry" },
124                             "bridge domain entry configurations", this);
125 }
126
127 void
128 bridge_domain_entry::event_handler::handle_replay()
129 {
130   m_db.replay();
131 }
132
133 void
134 bridge_domain_entry::event_handler::handle_populate(const client_db::key_t& key)
135 {
136   std::shared_ptr<bridge_domain_entry::dump_cmd> cmd(
137     new bridge_domain_entry::dump_cmd());
138
139   HW::enqueue(cmd);
140   HW::write();
141
142   for (auto& record : *cmd) {
143     auto& payload = record.get_payload();
144
145     std::shared_ptr<interface> itf = interface::find(payload.sw_if_index);
146     std::shared_ptr<bridge_domain> bd = bridge_domain::find(payload.bd_id);
147     mac_address_t mac(payload.mac);
148     bridge_domain_entry bd_e(*bd, mac, *itf);
149
150     VOM_LOG(log_level_t::DEBUG) << "bd-entry-dump: " << bd->to_string()
151                                 << mac.to_string() << itf->to_string();
152
153     /*
154  * Write each of the discovered interfaces into the OM,
155  * but disable the HW Command q whilst we do, so that no
156  * commands are sent to VPP
157  */
158     OM::commit(key, bd_e);
159   }
160 }
161
162 dependency_t
163 bridge_domain_entry::event_handler::order() const
164 {
165   return (dependency_t::ENTRY);
166 }
167
168 void
169 bridge_domain_entry::event_handler::show(std::ostream& os)
170 {
171   m_db.dump(os);
172 }
173
174 std::ostream&
175 operator<<(std::ostream& os, const bridge_domain_entry::key_t& key)
176 {
177   os << "[" << key.first << ", " << key.second.to_string() << "]";
178
179   return (os);
180 }
181 }
182
183 /*
184  * fd.io coding-style-patch-verification: ON
185  *
186  * Local Variables:
187  * eval: (c-set-style "mozilla")
188  * End:
189  */