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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "vom/gbp_contract_cmds.hpp"
17 #include "vom/api_types.hpp"
20 namespace gbp_contract_cmds {
22 create_cmd::create_cmd(HW::item<uint32_t>& item,
26 const gbp_contract::gbp_rules_t& gbp_rules,
27 const gbp_contract::ethertype_set_t& allowed_ethertypes)
32 , m_gbp_rules(gbp_rules)
33 , m_allowed_ethertypes(allowed_ethertypes)
38 create_cmd::operator==(const create_cmd& other) const
40 return ((m_acl == other.m_acl) && (m_sclass == other.m_sclass) &&
41 (m_dclass == other.m_dclass) && (m_gbp_rules == other.m_gbp_rules) &&
42 (m_allowed_ethertypes == other.m_allowed_ethertypes));
45 #define ARRAY_LEN(x) (sizeof(x) / sizeof(x[0]))
48 create_cmd::issue(connection& con)
50 size_t n_rules = m_gbp_rules.size();
53 msg_t req(con.ctx(), n_rules, std::ref(*this));
55 auto& payload = req.get_request().get_payload();
57 payload.contract.acl_index = m_acl.value();
58 payload.contract.sclass = m_sclass;
59 payload.contract.dclass = m_dclass;
60 payload.contract.n_rules = n_rules;
61 payload.contract.n_ether_types = m_allowed_ethertypes.size();
63 for (auto tt : m_allowed_ethertypes) {
64 payload.contract.allowed_ethertypes[ii] = tt.value();
66 if (ii == ARRAY_LEN(payload.contract.allowed_ethertypes))
71 for (auto rule : m_gbp_rules) {
72 if (rule.action() == gbp_rule::action_t::REDIRECT)
73 payload.contract.rules[ii].action = GBP_API_RULE_REDIRECT;
74 else if (rule.action() == gbp_rule::action_t::PERMIT)
75 payload.contract.rules[ii].action = GBP_API_RULE_PERMIT;
77 payload.contract.rules[ii].action = GBP_API_RULE_DENY;
79 if (rule.nhs().hash_mode() == gbp_rule::hash_mode_t::SYMMETRIC)
80 payload.contract.rules[ii].nh_set.hash_mode = GBP_API_HASH_MODE_SYMMETRIC;
81 else if (rule.nhs().hash_mode() == gbp_rule::hash_mode_t::SRC_IP)
82 payload.contract.rules[ii].nh_set.hash_mode = GBP_API_HASH_MODE_SRC_IP;
84 payload.contract.rules[ii].nh_set.hash_mode = GBP_API_HASH_MODE_DST_IP;
86 const gbp_rule::next_hops_t& next_hops = rule.nhs().next_hops();
87 uint8_t jj = 0, nh_size = (next_hops.size() > 8) ? 8 : next_hops.size();
89 payload.contract.rules[ii].nh_set.n_nhs = nh_size;
90 for (auto nh : next_hops) {
91 to_api(nh.getIp(), payload.contract.rules[ii].nh_set.nhs[jj].ip);
92 to_api(nh.getMac(), payload.contract.rules[ii].nh_set.nhs[jj].mac);
93 payload.contract.rules[ii].nh_set.nhs[jj].bd_id = nh.getBdId();
94 payload.contract.rules[ii].nh_set.nhs[jj].rd_id = nh.getRdId();
100 VAPI_CALL(req.execute());
106 create_cmd::to_string() const
108 std::ostringstream s;
109 s << "gbp-contract-create: " << m_hw_item.to_string()
110 << " sclass:" << m_sclass << " dclass:" << m_dclass << " acl:" << m_acl;
112 for (auto e : m_allowed_ethertypes)
119 delete_cmd::delete_cmd(HW::item<uint32_t>& item,
129 delete_cmd::operator==(const delete_cmd& other) const
131 return ((m_sclass == other.m_sclass) && (m_dclass == other.m_dclass));
135 delete_cmd::issue(connection& con)
137 msg_t req(con.ctx(), 0, std::ref(*this));
139 auto& payload = req.get_request().get_payload();
141 payload.contract.acl_index = ~0;
142 payload.contract.sclass = m_sclass;
143 payload.contract.dclass = m_dclass;
145 VAPI_CALL(req.execute());
151 delete_cmd::to_string() const
153 std::ostringstream s;
154 s << "gbp-contract-delete: " << m_hw_item.to_string()
155 << " sclass:" << m_sclass << " dclass:" << m_dclass;
161 dump_cmd::operator==(const dump_cmd& other) const
167 dump_cmd::issue(connection& con)
169 m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
171 VAPI_CALL(m_dump->execute());
179 dump_cmd::to_string() const
181 return ("gbp-contract-dump");
184 }; // namespace gbp_contract_cmds
188 * fd.io coding-style-patch-verification: ON
191 * eval: (c-set-style "mozilla")