FD.io VPP  v19.04.4-rc0-5-ge88582fac
Vector Packet Processing
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 
25  const handle_t& acl,
26  const gbp_contract::gbp_rules_t& gbp_rules,
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
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 #define ARRAY_LEN(x) (sizeof(x) / sizeof(x[0]))
46 
47 rc_t
49 {
50  size_t n_rules = m_gbp_rules.size();
51  uint32_t ii = 0;
52 
53  msg_t req(con.ctx(), n_rules, std::ref(*this));
54 
55  auto& payload = req.get_request().get_payload();
56  payload.is_add = 1;
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();
62 
63  for (auto tt : m_allowed_ethertypes) {
64  payload.contract.allowed_ethertypes[ii] = tt.value();
65  ii++;
66  if (ii == ARRAY_LEN(payload.contract.allowed_ethertypes))
67  break;
68  }
69 
70  ii = 0;
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;
76  else
77  payload.contract.rules[ii].action = GBP_API_RULE_DENY;
78 
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;
83  else
84  payload.contract.rules[ii].nh_set.hash_mode = GBP_API_HASH_MODE_DST_IP;
85 
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();
88 
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();
95  jj++;
96  }
97  ++ii;
98  }
99 
100  VAPI_CALL(req.execute());
101 
102  return (wait());
103 }
104 
105 std::string
107 {
108  std::ostringstream s;
109  s << "gbp-contract-create: " << m_hw_item.to_string()
110  << " sclass:" << m_sclass << " dclass:" << m_dclass << " acl:" << m_acl;
111  s << "[ethertype:";
112  for (auto e : m_allowed_ethertypes)
113  s << " " << e;
114  s << "]";
115 
116  return (s.str());
117 }
118 
122  : rpc_cmd(item)
123  , m_sclass(sclass)
124  , m_dclass(dclass)
125 {
126 }
127 
128 bool
130 {
131  return ((m_sclass == other.m_sclass) && (m_dclass == other.m_dclass));
132 }
133 
134 rc_t
136 {
137  msg_t req(con.ctx(), 0, std::ref(*this));
138 
139  auto& payload = req.get_request().get_payload();
140  payload.is_add = 0;
141  payload.contract.acl_index = ~0;
142  payload.contract.sclass = m_sclass;
143  payload.contract.dclass = m_dclass;
144 
145  VAPI_CALL(req.execute());
146 
147  return (wait());
148 }
149 
150 std::string
152 {
153  std::ostringstream s;
154  s << "gbp-contract-delete: " << m_hw_item.to_string()
155  << " sclass:" << m_sclass << " dclass:" << m_dclass;
156 
157  return (s.str());
158 }
159 
160 bool
161 dump_cmd::operator==(const dump_cmd& other) const
162 {
163  return (true);
164 }
165 
166 rc_t
168 {
169  m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
170 
171  VAPI_CALL(m_dump->execute());
172 
173  wait();
174 
175  return rc_t::OK;
176 }
177 
178 std::string
180 {
181  return ("gbp-contract-dump");
182 }
183 
184 }; // namespace gbp_contract_cmds
185 }; // namespace VOM
186 
187 /*
188  * fd.io coding-style-patch-verification: ON
189  *
190  * Local Variables:
191  * eval: (c-set-style "mozilla")
192  * End:
193  */
uint16_t sclass_t
std::string to_string() const
convert to string format for debug purposes
uint32_t value() const
get the value of the handle
Definition: types.cpp:93
std::string to_string() const
convert to string format for debug purposes
rc_t issue(connection &con)
Issue the command to VPP/HW.
rc_t issue(connection &con)
Issue the command to VPP/HW.
u8 n_rules
Definition: gbp.api:307
bool operator==(const dump_cmd &i) const
Comparison operator - only used for UT.
A cmd class that Dumps all the GBP contracts.
Error codes that VPP will return during a HW write.
Definition: types.hpp:89
create_cmd(HW::item< uint32_t > &item, 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.
vapi_enum_ip_neighbor_flags to_api(const neighbour::flags_t &f)
Definition: api_types.cpp:21
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:161
std::set< ethertype_t > ethertype_set_t
A set of allowed ethertypes.
A representation of the connection to VPP.
Definition: connection.hpp:33
u16 sclass
Definition: gbp.api:118
static const hash_mode_t SYMMETRIC
Flow hash is calculated based on SRC IP, DST IP and Protocol.
Definition: gbp_rule.hpp:124
A base class for all RPC commands to VPP.
Definition: rpc_cmd.hpp:40
#define VAPI_CALL(_stmt)
Convenince wrapper macro for error handling in VAPI sends.
Definition: types.hpp:29
static const action_t REDIRECT
Redirect action.
Definition: gbp_rule.hpp:207
bool operator==(const delete_cmd &i) const
Comparison operator - only used for UT.
rc_t issue(connection &con)
Issue the command to VPP/HW.
A type declaration of an interface handle in VPP.
Definition: types.hpp:233
A command class that creates or updates the GBP contract.
static const rc_t OK
The HW write was successfull.
Definition: types.hpp:109
#define ARRAY_LEN(x)
delete_cmd(HW::item< uint32_t > &item, sclass_t sclass, sclass_t dclass)
Constructor.
std::set< gbp_rule > gbp_rules_t
set of gbp rules
vapi::Connection & ctx()
Retrun the VAPI context the commands will use.
Definition: connection.cpp:49
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
std::set< next_hop_t > next_hops_t
unordered set of next hops
Definition: gbp_rule.hpp:138
static const hash_mode_t SRC_IP
Flow Hash is calculated based on SRC IP in case of load balancing.
Definition: gbp_rule.hpp:111
A cmd class that deletes a GBP contract.
bool operator==(const create_cmd &i) const
Comparison operator - only used for UT.
u16 allowed_ethertypes[16]
Definition: gbp.api:306
u16 dclass
Definition: gbp.api:303
static const action_t PERMIT
Permit action.
Definition: gbp_rule.hpp:197
HW::item< uint32_t > & m_hw_item
A reference to an object&#39;s HW::item that the command will update.
Definition: rpc_cmd.hpp:134
HW::item< uint32_t > & item()
return the HW item the command updates
Definition: rpc_cmd.hpp:66
vapi::Gbp_contract_add_del msg_t
convenient typedef
Definition: rpc_cmd.hpp:46
std::string to_string() const
convert to string format for debug purposes