GBP: use sclass in the DP for policy
[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                        sclass_t sclass,
24                        sclass_t dclass,
25                        const handle_t& acl,
26                        const gbp_contract::gbp_rules_t& gbp_rules,
27                        const gbp_contract::ethertype_set_t& allowed_ethertypes)
28   : rpc_cmd(item)
29   , m_sclass(sclass)
30   , m_dclass(dclass)
31   , m_acl(acl)
32   , m_gbp_rules(gbp_rules)
33   , m_allowed_ethertypes(allowed_ethertypes)
34 {
35 }
36
37 bool
38 create_cmd::operator==(const create_cmd& other) const
39 {
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));
43 }
44
45 rc_t
46 create_cmd::issue(connection& con)
47 {
48   size_t n_rules = m_gbp_rules.size();
49   size_t n_et_rules = 0;
50
51   msg_t req(con.ctx(), n_rules, n_et_rules, std::ref(*this));
52
53   auto& payload = req.get_request().get_payload();
54   payload.is_add = 1;
55   payload.contract.acl_index = m_acl.value();
56   payload.contract.sclass = m_sclass;
57   payload.contract.dclass = m_dclass;
58
59   uint32_t ii = 0;
60   payload.contract.n_rules = n_rules;
61
62   for (auto rule : m_gbp_rules) {
63     if (rule.action() == gbp_rule::action_t::REDIRECT)
64       payload.contract.rules[ii].action = GBP_API_RULE_REDIRECT;
65     else if (rule.action() == gbp_rule::action_t::PERMIT)
66       payload.contract.rules[ii].action = GBP_API_RULE_PERMIT;
67     else
68       payload.contract.rules[ii].action = GBP_API_RULE_DENY;
69
70     if (rule.nhs().hash_mode() == gbp_rule::hash_mode_t::SYMMETRIC)
71       payload.contract.rules[ii].nh_set.hash_mode = GBP_API_HASH_MODE_SYMMETRIC;
72     else if (rule.nhs().hash_mode() == gbp_rule::hash_mode_t::SRC_IP)
73       payload.contract.rules[ii].nh_set.hash_mode = GBP_API_HASH_MODE_SRC_IP;
74     else
75       payload.contract.rules[ii].nh_set.hash_mode = GBP_API_HASH_MODE_DST_IP;
76
77     const gbp_rule::next_hops_t& next_hops = rule.nhs().next_hops();
78     uint8_t jj = 0, nh_size = (next_hops.size() > 8) ? 8 : next_hops.size();
79
80     payload.contract.rules[ii].nh_set.n_nhs = nh_size;
81     for (auto nh : next_hops) {
82       to_api(nh.getIp(), payload.contract.rules[ii].nh_set.nhs[jj].ip);
83       to_api(nh.getMac(), payload.contract.rules[ii].nh_set.nhs[jj].mac);
84       payload.contract.rules[ii].nh_set.nhs[jj].bd_id = nh.getBdId();
85       payload.contract.rules[ii].nh_set.nhs[jj].rd_id = nh.getRdId();
86       jj++;
87     }
88     ++ii;
89   }
90
91   u8* data;
92   u16* et;
93
94   data = (((u8*)&payload.contract.n_ether_types) +
95           (sizeof(payload.contract.rules[0]) * payload.contract.n_rules));
96   *data = m_allowed_ethertypes.size();
97   et = (u16*)(++data);
98   ii = 0;
99   for (auto tt : m_allowed_ethertypes) {
100     et[ii] = tt.value();
101     ii++;
102   }
103
104   VAPI_CALL(req.execute());
105
106   return (wait());
107 }
108
109 std::string
110 create_cmd::to_string() const
111 {
112   std::ostringstream s;
113   s << "gbp-contract-create: " << m_hw_item.to_string()
114     << " sclass:" << m_sclass << " dclass:" << m_dclass << " acl:" << m_acl;
115   s << "[ethertype:";
116   for (auto e : m_allowed_ethertypes)
117     s << " " << e;
118   s << "]";
119
120   return (s.str());
121 }
122
123 delete_cmd::delete_cmd(HW::item<bool>& item, sclass_t sclass, sclass_t dclass)
124   : rpc_cmd(item)
125   , m_sclass(sclass)
126   , m_dclass(dclass)
127 {
128 }
129
130 bool
131 delete_cmd::operator==(const delete_cmd& other) const
132 {
133   return ((m_sclass == other.m_sclass) && (m_dclass == other.m_dclass));
134 }
135
136 rc_t
137 delete_cmd::issue(connection& con)
138 {
139   msg_t req(con.ctx(), 0, 0, std::ref(*this));
140
141   auto& payload = req.get_request().get_payload();
142   payload.is_add = 0;
143   payload.contract.acl_index = ~0;
144   payload.contract.sclass = m_sclass;
145   payload.contract.dclass = m_dclass;
146
147   VAPI_CALL(req.execute());
148
149   return (wait());
150 }
151
152 std::string
153 delete_cmd::to_string() const
154 {
155   std::ostringstream s;
156   s << "gbp-contract-delete: " << m_hw_item.to_string()
157     << " src-epg-id:" << m_sclass << " dst-epg-id:" << m_dclass;
158
159   return (s.str());
160 }
161
162 bool
163 dump_cmd::operator==(const dump_cmd& other) const
164 {
165   return (true);
166 }
167
168 rc_t
169 dump_cmd::issue(connection& con)
170 {
171   m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
172
173   VAPI_CALL(m_dump->execute());
174
175   wait();
176
177   return rc_t::OK;
178 }
179
180 std::string
181 dump_cmd::to_string() const
182 {
183   return ("gbp-contract-dump");
184 }
185
186 }; // namespace gbp_contract_cmds
187 }; // namespace VOM
188
189 /*
190  * fd.io coding-style-patch-verification: ON
191  *
192  * Local Variables:
193  * eval: (c-set-style "mozilla")
194  * End:
195  */