OpenSDN source code
ecmp_load_balance.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #ifndef vnsw_agent_ecmp_load_balance_hpp
7 #define vnsw_agent_ecmp_load_balance_hpp
8 
9 #include <atomic>
10 #include <boost/intrusive_ptr.hpp>
11 #include <vnc_cfg_types.h>
12 
13 namespace autogen {
14  struct EcmpHashingIncludeFields;
15 }
16 
17 static const std::string HashingFieldsStr[] = {
18  "l3-source-address",
19  "l3-destination-address",
20  "l4-protocol",
21  "l4-source-port",
22  "l4-destination-port"
23 };
24 static const std::string LoadBalanceDecision = "field-hash";
25 
27 public:
35  };
36 
38  SetAll();
39  }
40  virtual ~EcmpLoadBalance() { }
41 
42  const std::string &source_ip_str() const {
44  }
45  const std::string &destination_ip_str() const {
47  }
48  const std::string &source_port_str() const {
50  }
51  const std::string &destination_port_str() const {
53  }
54  const std::string &ip_protocol_str() const {
56  }
57 
58  void GetStringVector (std::vector<std::string> &string_vector) const {
59  for (uint8_t field_type = ((uint8_t) EcmpLoadBalance::SOURCE_IP);
60  field_type < ((uint8_t) EcmpLoadBalance::NUM_HASH_FIELDS);
61  field_type++) {
62  if (hash_fields_to_use_[field_type])
63  string_vector.push_back(HashingFieldsStr[field_type]);
64  }
65  }
66 
67  bool operator!=(const EcmpLoadBalance &rhs) const {
68  for (uint8_t field_type = ((uint8_t) EcmpLoadBalance::SOURCE_IP);
69  field_type < ((uint8_t) EcmpLoadBalance::NUM_HASH_FIELDS);
70  field_type++) {
71  if (hash_fields_to_use_[field_type] !=
72  rhs.hash_fields_to_use_[field_type])
73  return true;
74  }
75  return false;
76  }
77 
78  virtual void Copy(const EcmpLoadBalance &rhs) {
79  for (uint8_t field_type = ((uint8_t) EcmpLoadBalance::SOURCE_IP);
80  field_type < ((uint8_t) EcmpLoadBalance::NUM_HASH_FIELDS);
81  field_type++) {
82  hash_fields_to_use_[field_type] =
83  rhs.hash_fields_to_use_[field_type];
84  }
85  }
86 
87  void SetAll() {
88  for (uint8_t field_type = ((uint8_t) EcmpLoadBalance::SOURCE_IP);
89  field_type < ((uint8_t) EcmpLoadBalance::NUM_HASH_FIELDS);
90  field_type++) {
91  hash_fields_to_use_[field_type] = true;
92  }
93  }
94 
95  void ResetAll() {
96  for (uint8_t field_type = ((uint8_t) EcmpLoadBalance::SOURCE_IP);
97  field_type < ((uint8_t) EcmpLoadBalance::NUM_HASH_FIELDS);
98  field_type++) {
99  hash_fields_to_use_[field_type] = false;
100  }
101  }
102 
103  bool AllSet() const {
104  for (uint8_t field_type = ((uint8_t) EcmpLoadBalance::SOURCE_IP);
105  field_type < ((uint8_t) EcmpLoadBalance::NUM_HASH_FIELDS);
106  field_type++) {
107  if (hash_fields_to_use_[field_type] == false)
108  return false;
109  }
110  return true;
111  }
112  void set_source_ip() {
114  }
117  }
120  }
123  }
126  }
129  }
132  }
135  }
138  }
141  }
142 
143  bool is_source_ip_set() const {
144  return (hash_fields_to_use_[SOURCE_IP]);
145  }
146  bool is_destination_ip_set() const {
148  }
149  bool is_source_port_set() const {
151  }
152  bool is_destination_port_set() const {
154  }
155  bool is_ip_protocol_set() const {
157  }
158 
159  void reset() {
160  for (uint8_t field_type = ((uint8_t) EcmpLoadBalance::SOURCE_IP);
161  field_type < ((uint8_t) EcmpLoadBalance::NUM_HASH_FIELDS);
162  field_type++) {
163  hash_fields_to_use_[field_type] = false;
164  }
165  }
166 
168  (const autogen::EcmpHashingIncludeFields &ecmp_hashing_fields) {
169  bool ret = false;
170 
171  if (hash_fields_to_use_[SOURCE_IP] != ecmp_hashing_fields.source_ip) {
172  hash_fields_to_use_[SOURCE_IP] = ecmp_hashing_fields.source_ip;
173  ret = true;
174  }
176  ecmp_hashing_fields.destination_ip) {
178  ecmp_hashing_fields.destination_ip;
179  ret = true;
180  }
182  ecmp_hashing_fields.source_port) {
183  hash_fields_to_use_[SOURCE_PORT] = ecmp_hashing_fields.source_port;
184  ret = true;
185  }
187  ecmp_hashing_fields.destination_port) {
189  ecmp_hashing_fields.destination_port;
190  ret = true;
191  }
193  ecmp_hashing_fields.ip_protocol) {
194  hash_fields_to_use_[IP_PROTOCOL] = ecmp_hashing_fields.ip_protocol;
195  ret = true;
196  }
197  return ret;
198  }
199 
200 private:
202 };
203 
205 public:
207  virtual ~VmiEcmpLoadBalance() { }
208 
212  }
213  virtual void Copy(const VmiEcmpLoadBalance &rhs) {
216  }
217 
218 private:
220 };
221 
222 class EcmpField {
223 public:
225  ref_count_= 0;
226  }
227 
228  uint32_t RefCount() const {
229  return ref_count_;
230  }
231 private:
232  friend void intrusive_ptr_add_ref(EcmpField* ptr);
233  friend void intrusive_ptr_release(EcmpField* ptr);
234  mutable std::atomic<uint32_t> ref_count_;
235 };
236 
237 inline void intrusive_ptr_add_ref(EcmpField* ptr) {
238  ptr->ref_count_++;
239 }
240 
241 inline void intrusive_ptr_release(EcmpField* ptr) {
242  uint32_t prev = ptr->ref_count_.fetch_sub(1);
243  if (prev == 1) {
244  delete ptr;
245  }
246 }
247 
249 public:
250  typedef boost::intrusive_ptr<EcmpField> EcmpFieldPtr;
251 
253  }
254 
255  EcmpHashFields(const uint8_t hash_fields_to_use ) {
256  hash_fields_to_use_ = hash_fields_to_use;
257  }
258  void operator = (const uint8_t hash_fields_to_use) {
259  hash_fields_to_use_ = hash_fields_to_use;
260  }
261 
263  sip_ = new EcmpField;
264  dip_ = new EcmpField;
265  proto_ = new EcmpField;
266  sport_ = new EcmpField;
267  dport_ = new EcmpField;
268  }
269 
270  uint8_t HashFieldsToUse() const {
271  return hash_fields_to_use_;
272  }
273 
274  void SetHashFieldtoUse(EcmpField *ptr, uint8_t key) {
275  if (ptr && ptr->RefCount() == 1) {
276  comp_hash_fields_to_use_ |= 1 << key;
277  }
278  }
279  // If the field is not set create intrusive pointer
280  // else release the pointer.
281  void SetChangeInHashField(bool is_field_set, EcmpFieldPtr& fieldPtr,
282  EcmpFieldPtr &objFieldPtr) {
283  if (!is_field_set) {
284  if (!objFieldPtr.get()) {
285  objFieldPtr = fieldPtr;
286  }
287  } else {
288  objFieldPtr.reset();
289  }
290  }
291  // This function will be called to ge intersection of ecmp fields
300  }
301  //This function used to calculate the Change in ecmp fields
302  void CalculateChangeInEcmpFields(const EcmpLoadBalance &ecmp_load_balance,
303  EcmpHashFields& ecmp_hash_fields) {
304  SetChangeInHashField(ecmp_load_balance.is_source_ip_set(),
305  ecmp_hash_fields.sip_, sip_);
306  SetChangeInHashField(ecmp_load_balance.is_destination_ip_set(),
307  ecmp_hash_fields.dip_, dip_);
308  SetChangeInHashField(ecmp_load_balance.is_ip_protocol_set(),
309  ecmp_hash_fields.proto_, proto_);
310  SetChangeInHashField(ecmp_load_balance.is_source_port_set(),
311  ecmp_hash_fields.sport_, sport_);
312  SetChangeInHashField(ecmp_load_balance.is_destination_port_set(),
313  ecmp_hash_fields.dport_, dport_);
314  }
315 
318  }
319 
322  }
323 
324  void Reset() {
325  sip_ = NULL;
326  dip_ = NULL;
327  proto_ = NULL;
328  sport_ = NULL;
329  dport_ = NULL;
330  }
331 
332 private:
333  // This will have latest computed value
342 };
343 #endif
uint32_t RefCount() const
friend void intrusive_ptr_add_ref(EcmpField *ptr)
friend void intrusive_ptr_release(EcmpField *ptr)
std::atomic< uint32_t > ref_count_
void CalculateChangeInEcmpFields(const EcmpLoadBalance &ecmp_load_balance, EcmpHashFields &ecmp_hash_fields)
uint8_t comp_hash_fields_to_use_
EcmpFieldPtr sip_
void SetChangeInHashField(bool is_field_set, EcmpFieldPtr &fieldPtr, EcmpFieldPtr &objFieldPtr)
boost::intrusive_ptr< EcmpField > EcmpFieldPtr
uint8_t HashFieldsToUse() const
DISALLOW_COPY_AND_ASSIGN(EcmpHashFields)
EcmpHashFields(const uint8_t hash_fields_to_use)
uint8_t CalculateHashFieldsToUse()
void SetHashFieldtoUse(EcmpField *ptr, uint8_t key)
EcmpFieldPtr sport_
EcmpFieldPtr dip_
void operator=(const uint8_t hash_fields_to_use)
uint8_t hash_fields_to_use_
EcmpFieldPtr dport_
EcmpFieldPtr proto_
bool UpdateFields(const autogen::EcmpHashingIncludeFields &ecmp_hashing_fields)
virtual ~EcmpLoadBalance()
bool is_ip_protocol_set() const
virtual void Copy(const EcmpLoadBalance &rhs)
bool is_destination_port_set() const
bool AllSet() const
const std::string & destination_port_str() const
const std::string & source_port_str() const
const std::string & ip_protocol_str() const
bool is_destination_ip_set() const
const std::string & source_ip_str() const
bool hash_fields_to_use_[NUM_HASH_FIELDS]
bool is_source_ip_set() const
bool operator!=(const EcmpLoadBalance &rhs) const
const std::string & destination_ip_str() const
bool is_source_port_set() const
void GetStringVector(std::vector< std::string > &string_vector) const
bool use_global_vrouter() const
void set_use_global_vrouter(bool use_global_vrouter)
virtual void Copy(const VmiEcmpLoadBalance &rhs)
static const std::string LoadBalanceDecision
static const std::string HashingFieldsStr[]
void intrusive_ptr_add_ref(EcmpField *ptr)
void intrusive_ptr_release(EcmpField *ptr)