vom: acl: Fix l3 acl handle populate
[vpp.git] / src / vpp-api / vom / acl_list.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/acl_list.hpp"
17 #include "vom/acl_list_cmds.hpp"
18 #include "vom/logger.hpp"
19
20 namespace VOM {
21 namespace ACL {
22 template <>
23 void
24 l2_list::event_handler::handle_populate(const client_db::key_t& key)
25 {
26   /* hack to get this function instantiated */
27   m_evh.order();
28
29   /*
30    * dump VPP Bridge domains
31    */
32   std::shared_ptr<list_cmds::l2_dump_cmd> cmd =
33     std::make_shared<list_cmds::l2_dump_cmd>();
34
35   HW::enqueue(cmd);
36   HW::write();
37
38   for (auto& record : *cmd) {
39     auto& payload = record.get_payload();
40
41     const handle_t hdl(payload.acl_index);
42     l2_list acl(hdl, std::string(reinterpret_cast<const char*>(payload.tag)));
43
44     for (unsigned int ii = 0; ii < payload.count; ii++) {
45       const route::prefix_t pfx(payload.r[ii].is_ipv6,
46                                 payload.r[ii].src_ip_addr,
47                                 payload.r[ii].src_ip_prefix_len);
48       l2_rule rule(ii, action_t::from_int(payload.r[ii].is_permit), pfx,
49                    { payload.r[ii].src_mac }, { payload.r[ii].src_mac_mask });
50
51       acl.insert(rule);
52     }
53     VOM_LOG(log_level_t::DEBUG) << "dump: " << acl.to_string();
54
55     /*
56      * Write each of the discovered ACLs into the OM,
57      * but disable the HW Command q whilst we do, so that no
58      * commands are sent to VPP
59      */
60     OM::commit(key, acl);
61   }
62 }
63
64 template <>
65 void
66 l3_list::event_handler::handle_populate(const client_db::key_t& key)
67 {
68   /* hack to get this function instantiated */
69   m_evh.order();
70
71   /*
72    * dump L3 ACLs Bridge domains
73    */
74   std::shared_ptr<list_cmds::l3_dump_cmd> cmd =
75     std::make_shared<list_cmds::l3_dump_cmd>();
76
77   HW::enqueue(cmd);
78   HW::write();
79
80   for (auto& record : *cmd) {
81     auto& payload = record.get_payload();
82
83     const handle_t hdl(payload.acl_index);
84     l3_list acl(hdl, std::string(reinterpret_cast<const char*>(payload.tag)));
85
86     for (unsigned int ii = 0; ii < payload.count; ii++) {
87       const route::prefix_t src(payload.r[ii].is_ipv6,
88                                 payload.r[ii].src_ip_addr,
89                                 payload.r[ii].src_ip_prefix_len);
90       const route::prefix_t dst(payload.r[ii].is_ipv6,
91                                 payload.r[ii].dst_ip_addr,
92                                 payload.r[ii].dst_ip_prefix_len);
93       l3_rule rule(ii, action_t::from_int(payload.r[ii].is_permit), src, dst);
94
95       rule.set_proto(payload.r[ii].proto);
96       rule.set_src_from_port(payload.r[ii].srcport_or_icmptype_first);
97       rule.set_src_to_port(payload.r[ii].srcport_or_icmptype_last);
98       rule.set_dst_from_port(payload.r[ii].dstport_or_icmpcode_first);
99       rule.set_dst_to_port(payload.r[ii].dstport_or_icmpcode_last);
100       rule.set_tcp_flags_mask(payload.r[ii].tcp_flags_mask);
101       rule.set_tcp_flags_value(payload.r[ii].tcp_flags_value);
102
103       acl.insert(rule);
104     }
105     VOM_LOG(log_level_t::DEBUG) << "dump: " << acl.to_string();
106
107     /*
108      * Write each of the discovered ACLs into the OM,
109      * but disable the HW Command q whilst we do, so that no
110      * commands are sent to VPP
111      */
112     OM::commit(key, acl);
113   }
114 }
115
116 template <>
117 void
118 l3_list::update(const l3_list& obj)
119 {
120   /*
121    * always update the instance with the latest rule set
122    */
123   if (!m_hdl || obj.m_rules != m_rules) {
124     HW::enqueue(new list_cmds::l3_update_cmd(m_hdl, m_key, m_rules));
125   }
126   /*
127    * We don't, can't, read the priority from VPP,
128    * so the is equals check above does not include the priorty.
129    * but we save it now.
130    */
131   m_rules = obj.m_rules;
132 }
133 template <>
134 void
135 l2_list::update(const l2_list& obj)
136 {
137   /*
138    * always update the instance with the latest rule set
139    */
140   if (!m_hdl || obj.m_rules != m_rules) {
141     HW::enqueue(new list_cmds::l2_update_cmd(m_hdl, m_key, m_rules));
142   }
143   /*
144    * We don't, can't, read the priority from VPP,
145    * so the is equals check above does not include the priorty.
146    * but we save it now.
147    */
148   m_rules = obj.m_rules;
149 }
150 /**
151  * Sweep/reap the object if still stale
152  */
153 template <>
154 void
155 l3_list::sweep(void)
156 {
157   if (m_hdl) {
158     HW::enqueue(new list_cmds::l3_delete_cmd(m_hdl));
159   }
160   HW::write();
161 }
162 template <>
163 void
164 l2_list::sweep(void)
165 {
166   if (m_hdl) {
167     HW::enqueue(new list_cmds::l2_delete_cmd(m_hdl));
168   }
169   HW::write();
170 }
171
172 /**
173  * Replay the objects state to HW
174  */
175 template <>
176 void
177 l3_list::replay(void)
178 {
179   if (m_hdl) {
180     m_hdl.data().reset();
181     HW::enqueue(new list_cmds::l3_update_cmd(m_hdl, m_key, m_rules));
182   }
183 }
184 template <>
185 void
186 l2_list::replay(void)
187 {
188   if (m_hdl) {
189     m_hdl.data().reset();
190     HW::enqueue(new list_cmds::l2_update_cmd(m_hdl, m_key, m_rules));
191   }
192 }
193
194 }; // namespace ACL
195 }; // namespace VOM
196
197 /*
198  * fd.io coding-style-patch-verification: ON
199  *
200  * Local Variables:
201  * eval: (c-set-style "mozilla")
202  * End:
203  */