FD.io VPP  v19.01.3-6-g70449b9b9
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 
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,
28  : rpc_cmd(item)
29  , m_src_epg_id(src_epg_id)
30  , m_dst_epg_id(dst_epg_id)
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_src_epg_id == other.m_src_epg_id) &&
41  (m_dst_epg_id == other.m_dst_epg_id) &&
42  (m_gbp_rules == other.m_gbp_rules) &&
43  (m_allowed_ethertypes == other.m_allowed_ethertypes));
44 }
45 
46 rc_t
48 {
49  size_t n_rules = m_gbp_rules.size();
50  size_t n_et_rules = 0;
51 
52  msg_t req(con.ctx(), n_rules, n_et_rules, std::ref(*this));
53 
54  auto& payload = req.get_request().get_payload();
55  payload.is_add = 1;
56  payload.contract.acl_index = m_acl.value();
57  payload.contract.src_epg = m_src_epg_id;
58  payload.contract.dst_epg = m_dst_epg_id;
59 
60  uint32_t ii = 0;
61  payload.contract.n_rules = n_rules;
62 
63  for (auto rule : m_gbp_rules) {
64  if (rule.action() == gbp_rule::action_t::REDIRECT)
65  payload.contract.rules[ii].action = GBP_API_RULE_REDIRECT;
66  else if (rule.action() == gbp_rule::action_t::PERMIT)
67  payload.contract.rules[ii].action = GBP_API_RULE_PERMIT;
68  else
69  payload.contract.rules[ii].action = GBP_API_RULE_DENY;
70 
71  if (rule.nhs().hash_mode() == gbp_rule::hash_mode_t::SYMMETRIC)
72  payload.contract.rules[ii].nh_set.hash_mode = GBP_API_HASH_MODE_SYMMETRIC;
73  else if (rule.nhs().hash_mode() == gbp_rule::hash_mode_t::SRC_IP)
74  payload.contract.rules[ii].nh_set.hash_mode = GBP_API_HASH_MODE_SRC_IP;
75  else
76  payload.contract.rules[ii].nh_set.hash_mode = GBP_API_HASH_MODE_DST_IP;
77 
78  const gbp_rule::next_hops_t& next_hops = rule.nhs().next_hops();
79  uint8_t jj = 0, nh_size = (next_hops.size() > 8) ? 8 : next_hops.size();
80 
81  payload.contract.rules[ii].nh_set.n_nhs = nh_size;
82  for (auto nh : next_hops) {
83  to_api(nh.getIp(), payload.contract.rules[ii].nh_set.nhs[jj].ip);
84  to_api(nh.getMac(), payload.contract.rules[ii].nh_set.nhs[jj].mac);
85  payload.contract.rules[ii].nh_set.nhs[jj].bd_id = nh.getBdId();
86  payload.contract.rules[ii].nh_set.nhs[jj].rd_id = nh.getRdId();
87  jj++;
88  }
89  ++ii;
90  }
91 
92  u8* data;
93  u16* et;
94 
95  data = (((u8*)&payload.contract.n_ether_types) +
96  (sizeof(payload.contract.rules[0]) * payload.contract.n_rules));
97  *data = m_allowed_ethertypes.size();
98  et = (u16*)(++data);
99  ii = 0;
100  for (auto tt : m_allowed_ethertypes) {
101  et[ii] = tt.value();
102  ii++;
103  }
104 
105  VAPI_CALL(req.execute());
106 
107  return (wait());
108 }
109 
110 std::string
112 {
113  std::ostringstream s;
114  s << "gbp-contract-create: " << m_hw_item.to_string()
115  << " src-epg-id:" << m_src_epg_id << " dst-epg-id:" << m_dst_epg_id
116  << " acl:" << m_acl;
117 
118  return (s.str());
119 }
120 
122  epg_id_t src_epg_id,
123  epg_id_t dst_epg_id)
124  : rpc_cmd(item)
125  , m_src_epg_id(src_epg_id)
126  , m_dst_epg_id(dst_epg_id)
127 {
128 }
129 
130 bool
132 {
133  return ((m_src_epg_id == other.m_src_epg_id) &&
134  (m_dst_epg_id == other.m_dst_epg_id));
135 }
136 
137 rc_t
139 {
140  msg_t req(con.ctx(), 0, 0, std::ref(*this));
141 
142  auto& payload = req.get_request().get_payload();
143  payload.is_add = 0;
144  payload.contract.acl_index = ~0;
145  payload.contract.src_epg = m_src_epg_id;
146  payload.contract.dst_epg = m_dst_epg_id;
147 
148  VAPI_CALL(req.execute());
149 
150  return (wait());
151 }
152 
153 std::string
155 {
156  std::ostringstream s;
157  s << "gbp-contract-delete: " << m_hw_item.to_string()
158  << " src-epg-id:" << m_src_epg_id << " dst-epg-id:" << m_dst_epg_id;
159 
160  return (s.str());
161 }
162 
163 bool
164 dump_cmd::operator==(const dump_cmd& other) const
165 {
166  return (true);
167 }
168 
169 rc_t
171 {
172  m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
173 
174  VAPI_CALL(m_dump->execute());
175 
176  wait();
177 
178  return rc_t::OK;
179 }
180 
181 std::string
183 {
184  return ("gbp-contract-dump");
185 }
186 
187 }; // namespace gbp_contract_cmds
188 }; // namespace VOM
189 
190 /*
191  * fd.io coding-style-patch-verification: ON
192  *
193  * Local Variables:
194  * eval: (c-set-style "mozilla")
195  * End:
196  */
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
uint32_t epg_id_t
EPG IDs are 32 bit integers.
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:296
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
unsigned char u8
Definition: types.h:56
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.
u16 allowed_ethertypes[n_ether_types]
Definition: gbp.api:299
A representation of the connection to VPP.
Definition: connection.hpp:33
void to_api(const ip_address_t &a, vapi_type_address &v)
Definition: api_types.cpp:21
unsigned short u16
Definition: types.h:57
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
delete_cmd(HW::item< bool > &item, epg_id_t src_epg_id, epg_id_t dst_epg_id)
Constructor.
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
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.
create_cmd(HW::item< bool > &item, epg_id_t src_epg_id, epg_id_t dst_epg_id, const handle_t &acl, const gbp_contract::gbp_rules_t &gbp_rules, const gbp_contract::ethertype_set_t &allowed_ethertypes)
Constructor.
static const action_t PERMIT
Permit action.
Definition: gbp_rule.hpp:197
HW::item< bool > & m_hw_item
A reference to an object&#39;s HW::item that the command will update.
Definition: rpc_cmd.hpp:134
HW::item< bool > & 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