FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
gbp_contract_cmds.cpp
Go to the documentation of this file.
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 
17 #include "vom/api_types.hpp"
18 
19 namespace VOM {
20 namespace gbp_contract_cmds {
21 
23  scope_t scope,
26  const handle_t& acl,
27  const gbp_contract::gbp_rules_t& gbp_rules,
29  : rpc_cmd(item)
30  , m_scope(scope)
31  , m_sclass(sclass)
32  , m_dclass(dclass)
33  , m_acl(acl)
34  , m_gbp_rules(gbp_rules)
35  , m_allowed_ethertypes(allowed_ethertypes)
36 {
37 }
38 
39 bool
41 {
42  return ((m_acl == other.m_acl) && (m_sclass == other.m_sclass) &&
43  (m_scope == other.m_scope) && (m_dclass == other.m_dclass) &&
44  (m_gbp_rules == other.m_gbp_rules) &&
45  (m_allowed_ethertypes == other.m_allowed_ethertypes));
46 }
47 
48 #define ARRAY_LEN(x) (sizeof(x) / sizeof(x[0]))
49 
50 rc_t
52 {
53  size_t n_rules = m_gbp_rules.size();
54  uint32_t ii = 0;
55 
56  msg_t req(con.ctx(), n_rules, std::ref(*this));
57 
58  auto& payload = req.get_request().get_payload();
59  payload.is_add = 1;
60  payload.contract.acl_index = m_acl.value();
61  payload.contract.scope = m_scope;
62  payload.contract.sclass = m_sclass;
63  payload.contract.dclass = m_dclass;
64  payload.contract.n_rules = n_rules;
65  payload.contract.n_ether_types = m_allowed_ethertypes.size();
66 
67  for (auto tt : m_allowed_ethertypes) {
68  payload.contract.allowed_ethertypes[ii] = tt.value();
69  ii++;
70  if (ii == ARRAY_LEN(payload.contract.allowed_ethertypes))
71  break;
72  }
73 
74  ii = 0;
75  for (auto rule : m_gbp_rules) {
76  if (rule.action() == gbp_rule::action_t::REDIRECT)
77  payload.contract.rules[ii].action = GBP_API_RULE_REDIRECT;
78  else if (rule.action() == gbp_rule::action_t::PERMIT)
79  payload.contract.rules[ii].action = GBP_API_RULE_PERMIT;
80  else
81  payload.contract.rules[ii].action = GBP_API_RULE_DENY;
82 
83  if (rule.nhs().hash_mode() == gbp_rule::hash_mode_t::SYMMETRIC)
84  payload.contract.rules[ii].nh_set.hash_mode = GBP_API_HASH_MODE_SYMMETRIC;
85  else if (rule.nhs().hash_mode() == gbp_rule::hash_mode_t::SRC_IP)
86  payload.contract.rules[ii].nh_set.hash_mode = GBP_API_HASH_MODE_SRC_IP;
87  else
88  payload.contract.rules[ii].nh_set.hash_mode = GBP_API_HASH_MODE_DST_IP;
89 
90  const gbp_rule::next_hops_t& next_hops = rule.nhs().next_hops();
91  uint8_t jj = 0, nh_size = (next_hops.size() > 8) ? 8 : next_hops.size();
92 
93  payload.contract.rules[ii].nh_set.n_nhs = nh_size;
94  for (auto nh : next_hops) {
95  to_api(nh.getIp(), payload.contract.rules[ii].nh_set.nhs[jj].ip);
96  to_api(nh.getMac(), payload.contract.rules[ii].nh_set.nhs[jj].mac);
97  payload.contract.rules[ii].nh_set.nhs[jj].bd_id = nh.getBdId();
98  payload.contract.rules[ii].nh_set.nhs[jj].rd_id = nh.getRdId();
99  jj++;
100  }
101  ++ii;
102  }
103 
104  VAPI_CALL(req.execute());
105 
106  return (wait());
107 }
108 
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 
124  scope_t scope,
127  : rpc_cmd(item)
128  , m_scope(scope)
129  , m_sclass(sclass)
130  , m_dclass(dclass)
131 {
132 }
133 
134 bool
136 {
137  return ((m_sclass == other.m_sclass) && (m_dclass == other.m_dclass));
138 }
139 
140 rc_t
142 {
143  msg_t req(con.ctx(), 0, std::ref(*this));
144 
145  auto& payload = req.get_request().get_payload();
146  payload.is_add = 0;
147  payload.contract.acl_index = ~0;
148  payload.contract.scope = m_scope;
149  payload.contract.sclass = m_sclass;
150  payload.contract.dclass = m_dclass;
151 
152  VAPI_CALL(req.execute());
153 
154  return (wait());
155 }
156 
159 {
160  std::ostringstream s;
161  s << "gbp-contract-delete: " << m_hw_item.to_string() << " scope: " << m_scope
162  << " sclass:" << m_sclass << " dclass:" << m_dclass;
163 
164  return (s.str());
165 }
166 
167 bool
168 dump_cmd::operator==(const dump_cmd& other) const
169 {
170  return (true);
171 }
172 
173 rc_t
175 {
176  m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
177 
178  VAPI_CALL(m_dump->execute());
179 
180  wait();
181 
182  return rc_t::OK;
183 }
184 
187 {
188  return ("gbp-contract-dump");
189 }
190 
191 }; // namespace gbp_contract_cmds
192 }; // namespace VOM
193 
194 /*
195  * fd.io coding-style-patch-verification: OFF
196  *
197  * Local Variables:
198  * eval: (c-set-style "mozilla")
199  * End:
200  */
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: gbp_contract_cmds.cpp:48
VOM::HW::item< uint32_t >
GBP_API_RULE_REDIRECT
@ GBP_API_RULE_REDIRECT
Definition: gbp.api:320
VOM::dump_cmd< vapi::Gbp_contract_dump >::msg_t
vapi::Gbp_contract_dump msg_t
Definition: dump_cmd.hpp:46
GBP_API_HASH_MODE_DST_IP
@ GBP_API_HASH_MODE_DST_IP
Definition: gbp.api:305
VOM::gbp_contract_cmds::create_cmd::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: gbp_contract_cmds.cpp:110
VOM::gbp_contract_cmds::delete_cmd::delete_cmd
delete_cmd(HW::item< uint32_t > &item, scope_t scope, sclass_t sclass, sclass_t dclass)
Constructor.
Definition: gbp_contract_cmds.cpp:123
VOM::rpc_cmd< HW::item< uint32_t >, vapi::Gbp_contract_add_del >::wait
rc_t wait()
Wait on the commands promise.
Definition: rpc_cmd.hpp:82
GBP_API_HASH_MODE_SRC_IP
@ GBP_API_HASH_MODE_SRC_IP
Definition: gbp.api:304
VOM
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
VOM::scope_t
uint16_t scope_t
Definition: gbp_types.hpp:28
VOM::rc_t::OK
const static rc_t OK
The HW write was successfull.
Definition: types.hpp:109
VOM::gbp_rule::next_hops_t
std::set< next_hop_t > next_hops_t
unordered set of next hops
Definition: gbp_rule.hpp:138
VOM::handle_t::value
uint32_t value() const
get the value of the handle
Definition: types.cpp:93
VOM::rpc_cmd
A base class for all RPC commands to VPP.
Definition: rpc_cmd.hpp:40
VOM::gbp_contract_cmds::create_cmd::operator==
bool operator==(const create_cmd &i) const
Comparison operator - only used for UT.
Definition: gbp_contract_cmds.cpp:40
VOM::gbp_contract_cmds::create_cmd
A command class that creates or updates the GBP contract.
Definition: gbp_contract_cmds.hpp:30
con
Connection con
Definition: vapi_cpp_test.cpp:56
VOM::gbp_contract_cmds::dump_cmd::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: gbp_contract_cmds.cpp:186
VOM::sclass_t
uint16_t sclass_t
Definition: gbp_types.hpp:27
allowed_ethertypes
u16 allowed_ethertypes[16]
Definition: gbp.api:336
VOM::gbp_contract_cmds::delete_cmd::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: gbp_contract_cmds.cpp:158
VOM::rpc_cmd< HW::item< uint32_t >, vapi::Gbp_contract_add_del >::m_hw_item
HW::item< uint32_t > & m_hw_item
A reference to an object's HW::item that the command will update.
Definition: rpc_cmd.hpp:134
VOM::dump_cmd< vapi::Gbp_contract_dump >::m_dump
std::unique_ptr< vapi::Gbp_contract_dump > m_dump
The VAPI event registration.
Definition: dump_cmd.hpp:143
VOM::gbp_rule::action_t::PERMIT
const static action_t PERMIT
Permit action.
Definition: gbp_rule.hpp:197
VOM::gbp_rule::action_t::REDIRECT
const static action_t REDIRECT
Redirect action.
Definition: gbp_rule.hpp:207
VOM::gbp_contract::ethertype_set_t
std::set< ethertype_t > ethertype_set_t
A set of allowed ethertypes.
Definition: gbp_contract.hpp:47
VOM::gbp_rule::hash_mode_t::SYMMETRIC
const static hash_mode_t SYMMETRIC
Flow hash is calculated based on SRC IP, DST IP and Protocol.
Definition: gbp_rule.hpp:124
GBP_API_HASH_MODE_SYMMETRIC
@ GBP_API_HASH_MODE_SYMMETRIC
Definition: gbp.api:306
VAPI_CALL
#define VAPI_CALL(_stmt)
Convenince wrapper macro for error handling in VAPI sends.
Definition: types.hpp:29
VOM::gbp_contract_cmds::delete_cmd
A cmd class that deletes a GBP contract.
Definition: gbp_contract_cmds.hpp:72
VOM::gbp_contract_cmds::create_cmd::create_cmd
create_cmd(HW::item< uint32_t > &item, scope_t scope, sclass_t sclass, sclass_t dclass, const handle_t &acl, const gbp_contract::gbp_rules_t &gbp_rules, const gbp_contract::ethertype_set_t &allowed_ethertypes)
Constructor.
Definition: gbp_contract_cmds.cpp:22
VOM::connection
A representation of the connection to VPP.
Definition: connection.hpp:33
VOM::dump_cmd< vapi::Gbp_contract_dump >::wait
rc_t wait()
Wait for the issue of the command to complete.
Definition: dump_cmd.hpp:93
VOM::gbp_contract_cmds::create_cmd::issue
rc_t issue(connection &con)
Issue the command to VPP/HW.
Definition: gbp_contract_cmds.cpp:51
scope
vl_api_gbp_scope_t scope
Definition: gbp.api:78
VOM::handle_t
A type declaration of an interface handle in VPP.
Definition: types.hpp:233
nh
vl_api_fib_path_nh_t nh
Definition: fib_types.api:126
sclass
u16 sclass
Definition: gbp.api:131
VOM::HW::item::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:161
GBP_API_RULE_PERMIT
@ GBP_API_RULE_PERMIT
Definition: gbp.api:318
VOM::to_api
vapi_enum_ip_neighbor_flags to_api(const neighbour::flags_t &f)
Definition: api_types.cpp:21
VOM::rpc_cmd< HW::item< uint32_t >, vapi::Gbp_contract_add_del >::msg_t
vapi::Gbp_contract_add_del msg_t
convenient typedef
Definition: rpc_cmd.hpp:46
VOM::gbp_contract_cmds::dump_cmd
A cmd class that Dumps all the GBP contracts.
Definition: gbp_contract_cmds.hpp:108
gbp_contract_cmds.hpp
dclass
u16 dclass
Definition: gbp.api:333
VOM::gbp_contract_cmds::delete_cmd::operator==
bool operator==(const delete_cmd &i) const
Comparison operator - only used for UT.
Definition: gbp_contract_cmds.cpp:135
VOM::gbp_contract::gbp_rules_t
std::set< gbp_rule > gbp_rules_t
set of gbp rules
Definition: gbp_contract.hpp:37
item
cJSON * item
Definition: cJSON.h:222
GBP_API_RULE_DENY
@ GBP_API_RULE_DENY
Definition: gbp.api:319
n_rules
u8 n_rules
Definition: gbp.api:337
VOM::rc_t
Error codes that VPP will return during a HW write.
Definition: types.hpp:89
VOM::gbp_contract_cmds::dump_cmd::operator==
bool operator==(const dump_cmd &i) const
Comparison operator - only used for UT.
Definition: gbp_contract_cmds.cpp:168
VOM::gbp_rule::hash_mode_t::SRC_IP
const static hash_mode_t SRC_IP
Flow Hash is calculated based on SRC IP in case of load balancing.
Definition: gbp_rule.hpp:111
VOM::gbp_contract_cmds::dump_cmd::issue
rc_t issue(connection &con)
Issue the command to VPP/HW.
Definition: gbp_contract_cmds.cpp:174
api_types.hpp
VOM::gbp_contract_cmds::delete_cmd::issue
rc_t issue(connection &con)
Issue the command to VPP/HW.
Definition: gbp_contract_cmds.cpp:141
string
const char *const string
Definition: cJSON.h:172