API: Change ip4_address and ip6_address to use type alias.
[vpp.git] / extras / vom / vom / gbp_contract_cmds.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_cmds.hpp"
17 #include "vom/api_types.hpp"
18
19 namespace VOM {
20 namespace gbp_contract_cmds {
21
22 create_cmd::create_cmd(HW::item<bool>& item,
23                        epg_id_t src_epg_id,
24                        epg_id_t dst_epg_id,
25                        const handle_t& acl,
26                        const gbp_contract::gbp_rules_t& gbp_rules)
27   : rpc_cmd(item)
28   , m_src_epg_id(src_epg_id)
29   , m_dst_epg_id(dst_epg_id)
30   , m_acl(acl)
31   , m_gbp_rules(gbp_rules)
32 {
33 }
34
35 bool
36 create_cmd::operator==(const create_cmd& other) const
37 {
38   return ((m_acl == other.m_acl) && (m_src_epg_id == other.m_src_epg_id) &&
39           (m_dst_epg_id == other.m_dst_epg_id) &&
40           (m_gbp_rules == other.m_gbp_rules));
41 }
42
43 rc_t
44 create_cmd::issue(connection& con)
45 {
46   u8 size = m_gbp_rules.empty() ? 1 : m_gbp_rules.size();
47   msg_t req(con.ctx(), size, std::ref(*this));
48
49   auto& payload = req.get_request().get_payload();
50   payload.is_add = 1;
51   payload.contract.acl_index = m_acl.value();
52   payload.contract.src_epg = m_src_epg_id;
53   payload.contract.dst_epg = m_dst_epg_id;
54   if (size > 1) {
55     u32 ii = 0;
56     auto it = m_gbp_rules.cbegin();
57     payload.contract.n_rules = m_gbp_rules.size();
58     while (it != m_gbp_rules.cend()) {
59       if (it->action() == gbp_rule::action_t::REDIRECT)
60         payload.contract.rules[ii].action = GBP_API_RULE_REDIRECT;
61       else if (it->action() == gbp_rule::action_t::PERMIT)
62         payload.contract.rules[ii].action = GBP_API_RULE_PERMIT;
63       else
64         payload.contract.rules[ii].action = GBP_API_RULE_DENY;
65
66       if (it->nhs().getHashMode() == gbp_rule::hash_mode_t::SYMMETRIC)
67         payload.contract.rules[ii].nh_set.hash_mode =
68           GBP_API_HASH_MODE_SYMMETRIC;
69       else if (it->nhs().getHashMode() == gbp_rule::hash_mode_t::SRC_IP)
70         payload.contract.rules[ii].nh_set.hash_mode = GBP_API_HASH_MODE_SRC_IP;
71       else
72         payload.contract.rules[ii].nh_set.hash_mode = GBP_API_HASH_MODE_DST_IP;
73
74       const gbp_rule::next_hops_t& next_hops = it->nhs().getNextHops();
75       u8 jj = 0, nh_size = (next_hops.size() > 8) ? 8 : next_hops.size();
76       auto nh_it = next_hops.cbegin();
77
78       payload.contract.rules[ii].nh_set.n_nhs = nh_size;
79       while (jj < nh_size) {
80         to_api(nh_it->getIp(), payload.contract.rules[ii].nh_set.nhs[jj].ip);
81         payload.contract.rules[ii].nh_set.nhs[jj].mac = to_api(nh_it->getMac());
82         payload.contract.rules[ii].nh_set.nhs[jj].bd_id = nh_it->getBdId();
83         payload.contract.rules[ii].nh_set.nhs[jj].rd_id = nh_it->getRdId();
84         ++nh_it;
85         ++jj;
86       }
87
88       ++it;
89       ++ii;
90     }
91   }
92   VAPI_CALL(req.execute());
93
94   return (wait());
95 }
96
97 std::string
98 create_cmd::to_string() const
99 {
100   std::ostringstream s;
101   s << "gbp-contract-create: " << m_hw_item.to_string()
102     << " src-epg-id:" << m_src_epg_id << " dst-epg-id:" << m_dst_epg_id
103     << " acl:" << m_acl;
104
105   return (s.str());
106 }
107
108 delete_cmd::delete_cmd(HW::item<bool>& item,
109                        epg_id_t src_epg_id,
110                        epg_id_t dst_epg_id)
111   : rpc_cmd(item)
112   , m_src_epg_id(src_epg_id)
113   , m_dst_epg_id(dst_epg_id)
114 {
115 }
116
117 bool
118 delete_cmd::operator==(const delete_cmd& other) const
119 {
120   return ((m_src_epg_id == other.m_src_epg_id) &&
121           (m_dst_epg_id == other.m_dst_epg_id));
122 }
123
124 rc_t
125 delete_cmd::issue(connection& con)
126 {
127   msg_t req(con.ctx(), 1, std::ref(*this));
128
129   auto& payload = req.get_request().get_payload();
130   payload.is_add = 0;
131   payload.contract.acl_index = ~0;
132   payload.contract.src_epg = m_src_epg_id;
133   payload.contract.dst_epg = m_dst_epg_id;
134
135   VAPI_CALL(req.execute());
136
137   return (wait());
138 }
139
140 std::string
141 delete_cmd::to_string() const
142 {
143   std::ostringstream s;
144   s << "gbp-contract-delete: " << m_hw_item.to_string()
145     << " src-epg-id:" << m_src_epg_id << " dst-epg-id:" << m_dst_epg_id;
146
147   return (s.str());
148 }
149
150 bool
151 dump_cmd::operator==(const dump_cmd& other) const
152 {
153   return (true);
154 }
155
156 rc_t
157 dump_cmd::issue(connection& con)
158 {
159   m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
160
161   VAPI_CALL(m_dump->execute());
162
163   wait();
164
165   return rc_t::OK;
166 }
167
168 std::string
169 dump_cmd::to_string() const
170 {
171   return ("gbp-contract-dump");
172 }
173
174 }; // namespace gbp_contract_cmds
175 }; // namespace VOM
176
177 /*
178  * fd.io coding-style-patch-verification: ON
179  *
180  * Local Variables:
181  * eval: (c-set-style "mozilla")
182  * End:
183  */