VOM: GBP show allowed ethertypes in contracts
[vpp.git] / extras / vom / vom / gbp_contract.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/gbp_contract.hpp"
17 #include "vom/api_types.hpp"
18 #include "vom/gbp_contract_cmds.hpp"
19 #include "vom/singular_db_funcs.hpp"
20
21 namespace VOM {
22
23 singular_db<gbp_contract::key_t, gbp_contract> gbp_contract::m_db;
24
25 gbp_contract::event_handler gbp_contract::m_evh;
26
27 gbp_contract::gbp_contract(epg_id_t src_epg_id,
28                            epg_id_t dst_epg_id,
29                            const ACL::l3_list& acl,
30                            const gbp_rules_t& rules,
31                            const ethertype_set_t& allowed_ethertypes)
32   : m_hw(false)
33   , m_src_epg_id(src_epg_id)
34   , m_dst_epg_id(dst_epg_id)
35   , m_acl(acl.singular())
36   , m_gbp_rules(rules)
37   , m_allowed_ethertypes(allowed_ethertypes)
38 {
39 }
40
41 gbp_contract::gbp_contract(const gbp_contract& gbpc)
42   : m_hw(gbpc.m_hw)
43   , m_src_epg_id(gbpc.m_src_epg_id)
44   , m_dst_epg_id(gbpc.m_dst_epg_id)
45   , m_acl(gbpc.m_acl)
46   , m_gbp_rules(gbpc.m_gbp_rules)
47   , m_allowed_ethertypes(gbpc.m_allowed_ethertypes)
48 {
49 }
50
51 gbp_contract::~gbp_contract()
52 {
53   sweep();
54
55   // not in the DB anymore.
56   m_db.release(key(), this);
57 }
58
59 const gbp_contract::key_t
60 gbp_contract::key() const
61 {
62   return (std::make_pair(m_src_epg_id, m_dst_epg_id));
63 }
64
65 bool
66 gbp_contract::operator==(const gbp_contract& gbpc) const
67 {
68   return ((key() == gbpc.key()) && (m_acl->handle() == gbpc.m_acl->handle()));
69 }
70
71 void
72 gbp_contract::sweep()
73 {
74   if (m_hw) {
75     HW::enqueue(
76       new gbp_contract_cmds::delete_cmd(m_hw, m_src_epg_id, m_dst_epg_id));
77   }
78   HW::write();
79 }
80
81 void
82 gbp_contract::replay()
83 {
84   if (m_hw) {
85     HW::enqueue(new gbp_contract_cmds::create_cmd(
86       m_hw, m_src_epg_id, m_dst_epg_id, m_acl->handle(), m_gbp_rules,
87       m_allowed_ethertypes));
88   }
89 }
90
91 std::string
92 gbp_contract::to_string() const
93 {
94   std::ostringstream s;
95   s << "gbp-contract:[{" << m_src_epg_id << ", " << m_dst_epg_id << "}, "
96     << m_acl->to_string();
97   if (m_gbp_rules.size()) {
98     auto it = m_gbp_rules.cbegin();
99     while (it != m_gbp_rules.cend()) {
100       s << it->to_string();
101       ++it;
102     }
103   }
104   s << "[ethertype:";
105   for (auto e : m_allowed_ethertypes)
106     s << " " << e;
107   s << "]]";
108
109   return (s.str());
110 }
111
112 void
113 gbp_contract::update(const gbp_contract& r)
114 {
115   /*
116    * create the table if it is not yet created
117    */
118   if (rc_t::OK != m_hw.rc()) {
119     HW::enqueue(new gbp_contract_cmds::create_cmd(
120       m_hw, m_src_epg_id, m_dst_epg_id, m_acl->handle(), m_gbp_rules,
121       m_allowed_ethertypes));
122   }
123 }
124
125 std::shared_ptr<gbp_contract>
126 gbp_contract::find_or_add(const gbp_contract& temp)
127 {
128   return (m_db.find_or_add(temp.key(), temp));
129 }
130
131 std::shared_ptr<gbp_contract>
132 gbp_contract::find(const key_t& k)
133 {
134   return (m_db.find(k));
135 }
136
137 std::shared_ptr<gbp_contract>
138 gbp_contract::singular() const
139 {
140   return find_or_add(*this);
141 }
142
143 void
144 gbp_contract::dump(std::ostream& os)
145 {
146   db_dump(m_db, os);
147 }
148
149 gbp_contract::event_handler::event_handler()
150 {
151   OM::register_listener(this);
152   inspect::register_handler({ "gbp-contract" }, "GBP Contract", this);
153 }
154
155 void
156 gbp_contract::event_handler::handle_replay()
157 {
158   m_db.replay();
159 }
160
161 void
162 gbp_contract::event_handler::handle_populate(const client_db::key_t& key)
163 {
164   std::shared_ptr<gbp_contract_cmds::dump_cmd> cmd =
165     std::make_shared<gbp_contract_cmds::dump_cmd>();
166
167   HW::enqueue(cmd);
168   HW::write();
169
170   for (auto& record : *cmd) {
171     auto& payload = record.get_payload();
172
173     std::shared_ptr<ACL::l3_list> acl =
174       ACL::l3_list::find(payload.contract.acl_index);
175
176     if (acl) {
177       gbp_contract::gbp_rules_t rules;
178
179       for (uint8_t i = 0; i < payload.contract.n_rules; i++) {
180         const gbp_rule::action_t action =
181           gbp_rule::action_t::from_int(payload.contract.rules[i].action);
182         const gbp_rule::hash_mode_t hm = gbp_rule::hash_mode_t::from_int(
183           payload.contract.rules[i].nh_set.hash_mode);
184         gbp_rule::next_hops_t nhs;
185         for (u8 j = 0; j < payload.contract.rules[i].nh_set.n_nhs; j++) {
186           gbp_rule::next_hop_t nh(
187             from_api(payload.contract.rules[i].nh_set.nhs[j].ip),
188             from_api(payload.contract.rules[i].nh_set.nhs[j].mac),
189             payload.contract.rules[i].nh_set.nhs[j].bd_id,
190             payload.contract.rules[i].nh_set.nhs[j].rd_id);
191           nhs.insert(nh);
192         }
193         gbp_rule::next_hop_set_t next_hop_set(hm, nhs);
194         gbp_rule gr(i, next_hop_set, action);
195         rules.insert(gr);
196       }
197
198       ethertype_set_t allowed_ethertypes;
199       u8 *data, n_et;
200       u16* et;
201
202       data = (((u8*)&payload.contract.n_ether_types) +
203               (sizeof(payload.contract.rules[0]) * payload.contract.n_rules));
204       n_et = *data;
205       et = (u16*)(++data);
206
207       for (uint8_t i = 0; i < n_et; i++) {
208         allowed_ethertypes.insert(ethertype_t::from_numeric_val(et[i]));
209       }
210
211       gbp_contract gbpc(payload.contract.src_epg, payload.contract.dst_epg,
212                         *acl, rules, allowed_ethertypes);
213       OM::commit(key, gbpc);
214
215       VOM_LOG(log_level_t::DEBUG) << "read: " << gbpc.to_string();
216     } else {
217       VOM_LOG(log_level_t::ERROR) << " no ACL:" << payload.contract.acl_index;
218     }
219   }
220 }
221
222 dependency_t
223 gbp_contract::event_handler::order() const
224 {
225   return (dependency_t::ENTRY);
226 }
227
228 void
229 gbp_contract::event_handler::show(std::ostream& os)
230 {
231   db_dump(m_db, os);
232 }
233
234 std::ostream&
235 operator<<(std::ostream& os, const gbp_contract::key_t& key)
236 {
237   os << "{ " << key.first << "," << key.second << "}";
238
239   return (os);
240 }
241
242 } // namespace VOM
243
244 /*
245  * fd.io coding-style-patch-verification: ON
246  *
247  * Local Variables:
248  * eval: (c-set-style "mozilla")
249  * End:
250  */