LCOV - code coverage report
Current view: top level - vnsw/agent/oper - ecmp_load_balance.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 113 185 61.1 %
Date: 2026-11-30 02:00:48 Functions: 31 48 64.6 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      26             : class EcmpLoadBalance {
      27             : public:
      28             :     enum kHashingFields {
      29             :         SOURCE_IP,
      30             :         DESTINATION_IP,
      31             :         IP_PROTOCOL,
      32             :         SOURCE_PORT,
      33             :         DESTINATION_PORT,
      34             :         NUM_HASH_FIELDS
      35             :     };
      36             : 
      37        1744 :     EcmpLoadBalance() {
      38        1744 :         SetAll();
      39        1744 :     }
      40        2097 :     virtual ~EcmpLoadBalance() { }
      41             : 
      42           0 :     const std::string &source_ip_str() const {
      43           0 :         return HashingFieldsStr[(uint8_t)EcmpLoadBalance::SOURCE_IP];
      44             :     }
      45           0 :     const std::string &destination_ip_str() const {
      46           0 :         return HashingFieldsStr[(uint8_t)EcmpLoadBalance::DESTINATION_IP];
      47             :     }
      48           0 :     const std::string &source_port_str() const {
      49           0 :         return HashingFieldsStr[(uint8_t)EcmpLoadBalance::SOURCE_PORT];
      50             :     }
      51           0 :     const std::string &destination_port_str() const {
      52           0 :         return HashingFieldsStr[(uint8_t)EcmpLoadBalance::DESTINATION_PORT];
      53             :     }
      54           0 :     const std::string &ip_protocol_str() const {
      55           0 :         return HashingFieldsStr[(uint8_t)EcmpLoadBalance::IP_PROTOCOL];
      56             :     }
      57             : 
      58           0 :     void GetStringVector (std::vector<std::string> &string_vector) const {
      59           0 :         for (uint8_t field_type = ((uint8_t) EcmpLoadBalance::SOURCE_IP);
      60           0 :              field_type < ((uint8_t) EcmpLoadBalance::NUM_HASH_FIELDS);
      61             :              field_type++) {
      62           0 :             if (hash_fields_to_use_[field_type])
      63           0 :                 string_vector.push_back(HashingFieldsStr[field_type]);
      64             :         }
      65           0 :     }
      66             : 
      67         532 :     bool operator!=(const EcmpLoadBalance &rhs) const {
      68        3042 :         for (uint8_t field_type = ((uint8_t) EcmpLoadBalance::SOURCE_IP);
      69        3042 :              field_type < ((uint8_t) EcmpLoadBalance::NUM_HASH_FIELDS);
      70             :              field_type++) {
      71        2540 :             if (hash_fields_to_use_[field_type] !=
      72        2540 :                 rhs.hash_fields_to_use_[field_type])
      73          30 :                 return true;
      74             :         }
      75         502 :         return false;
      76             :     }
      77             : 
      78          60 :     virtual void Copy(const EcmpLoadBalance &rhs) {
      79          60 :         for (uint8_t field_type = ((uint8_t) EcmpLoadBalance::SOURCE_IP);
      80         360 :              field_type < ((uint8_t) EcmpLoadBalance::NUM_HASH_FIELDS);
      81             :              field_type++) {
      82         300 :             hash_fields_to_use_[field_type] =
      83         300 :                 rhs.hash_fields_to_use_[field_type];
      84             :         }
      85          60 :     }
      86             : 
      87        1744 :     void SetAll() {
      88        1744 :         for (uint8_t field_type = ((uint8_t) EcmpLoadBalance::SOURCE_IP);
      89       10464 :              field_type < ((uint8_t) EcmpLoadBalance::NUM_HASH_FIELDS);
      90             :              field_type++) {
      91        8720 :             hash_fields_to_use_[field_type] = true;
      92             :         }
      93        1744 :     }
      94             : 
      95           8 :     void ResetAll() {
      96           8 :         for (uint8_t field_type = ((uint8_t) EcmpLoadBalance::SOURCE_IP);
      97          48 :              field_type < ((uint8_t) EcmpLoadBalance::NUM_HASH_FIELDS);
      98             :              field_type++) {
      99          40 :             hash_fields_to_use_[field_type] = false;
     100             :         }
     101           8 :     }
     102             : 
     103          36 :     bool AllSet() const {
     104         216 :         for (uint8_t field_type = ((uint8_t) EcmpLoadBalance::SOURCE_IP);
     105         216 :              field_type < ((uint8_t) EcmpLoadBalance::NUM_HASH_FIELDS);
     106             :              field_type++) {
     107         180 :             if (hash_fields_to_use_[field_type] == false)
     108           0 :                 return false;
     109             :         }
     110          36 :         return true;
     111             :     }
     112           5 :     void set_source_ip() {
     113           5 :         hash_fields_to_use_[SOURCE_IP] = true;
     114           5 :     }
     115             :     void reset_source_ip() {
     116             :         hash_fields_to_use_[SOURCE_IP] = false;
     117             :     }
     118           4 :     void set_destination_ip() {
     119           4 :         hash_fields_to_use_[DESTINATION_IP] = true;
     120           4 :     }
     121             :     void reset_destination_ip() {
     122             :         hash_fields_to_use_[DESTINATION_IP] = false;
     123             :     }
     124           2 :     void set_ip_protocol() {
     125           2 :         hash_fields_to_use_[IP_PROTOCOL] = true;
     126           2 :     }
     127             :     void reset_ip_protocol() {
     128             :         hash_fields_to_use_[IP_PROTOCOL] = false;
     129             :     }
     130           1 :     void set_source_port() {
     131           1 :         hash_fields_to_use_[SOURCE_PORT] = true;
     132           1 :     }
     133             :     void reset_source_port() {
     134             :         hash_fields_to_use_[SOURCE_PORT] = false;
     135             :     }
     136           0 :     void set_destination_port() {
     137           0 :         hash_fields_to_use_[DESTINATION_PORT] = true;
     138           0 :     }
     139             :     void reset_destination_port() {
     140             :         hash_fields_to_use_[DESTINATION_PORT] = false;
     141             :     }
     142             : 
     143           0 :     bool is_source_ip_set() const {
     144           0 :         return (hash_fields_to_use_[SOURCE_IP]);
     145             :     }
     146           0 :     bool is_destination_ip_set() const {
     147           0 :         return (hash_fields_to_use_[DESTINATION_IP]);
     148             :     }
     149           0 :     bool is_source_port_set() const {
     150           0 :         return (hash_fields_to_use_[SOURCE_PORT]);
     151             :     }
     152           0 :     bool is_destination_port_set() const {
     153           0 :         return (hash_fields_to_use_[DESTINATION_PORT]);
     154             :     }
     155           0 :     bool is_ip_protocol_set() const {
     156           0 :         return (hash_fields_to_use_[IP_PROTOCOL]);
     157             :     }
     158             : 
     159          88 :     void reset() {
     160          88 :         for (uint8_t field_type = ((uint8_t) EcmpLoadBalance::SOURCE_IP);
     161         528 :              field_type < ((uint8_t) EcmpLoadBalance::NUM_HASH_FIELDS);
     162             :              field_type++) {
     163         440 :             hash_fields_to_use_[field_type] = false;
     164             :         }
     165          88 :     }
     166             : 
     167           0 :     bool UpdateFields
     168             :         (const autogen::EcmpHashingIncludeFields &ecmp_hashing_fields) {
     169           0 :         bool ret = false;
     170             : 
     171           0 :         if (hash_fields_to_use_[SOURCE_IP] != ecmp_hashing_fields.source_ip) {
     172           0 :             hash_fields_to_use_[SOURCE_IP] = ecmp_hashing_fields.source_ip;
     173           0 :             ret = true;
     174             :         }
     175           0 :         if (hash_fields_to_use_[DESTINATION_IP] !=
     176           0 :             ecmp_hashing_fields.destination_ip) {
     177           0 :             hash_fields_to_use_[DESTINATION_IP] =
     178           0 :                 ecmp_hashing_fields.destination_ip;
     179           0 :             ret = true;
     180             :         }
     181           0 :         if (hash_fields_to_use_[SOURCE_PORT] !=
     182           0 :             ecmp_hashing_fields.source_port) {
     183           0 :             hash_fields_to_use_[SOURCE_PORT] = ecmp_hashing_fields.source_port;
     184           0 :             ret = true;
     185             :         }
     186           0 :         if (hash_fields_to_use_[DESTINATION_PORT] !=
     187           0 :             ecmp_hashing_fields.destination_port) {
     188           0 :             hash_fields_to_use_[DESTINATION_PORT] =
     189           0 :                 ecmp_hashing_fields.destination_port;
     190           0 :             ret = true;
     191             :         }
     192           0 :         if (hash_fields_to_use_[IP_PROTOCOL] !=
     193           0 :             ecmp_hashing_fields.ip_protocol) {
     194           0 :             hash_fields_to_use_[IP_PROTOCOL] = ecmp_hashing_fields.ip_protocol;
     195           0 :             ret = true;
     196             :         }
     197           0 :         return ret;
     198             :     }
     199             : 
     200             : private:
     201             :     bool hash_fields_to_use_[NUM_HASH_FIELDS];
     202             : };
     203             : 
     204             : class VmiEcmpLoadBalance : public EcmpLoadBalance {
     205             : public:
     206        1164 :     VmiEcmpLoadBalance() : EcmpLoadBalance(), use_global_vrouter_(true) { }
     207        1164 :     virtual ~VmiEcmpLoadBalance() { }
     208             : 
     209          30 :     bool use_global_vrouter() const {return use_global_vrouter_;}
     210         176 :     void set_use_global_vrouter(bool use_global_vrouter) {
     211         176 :         use_global_vrouter_ = use_global_vrouter;
     212         176 :     }
     213          30 :     virtual void Copy(const VmiEcmpLoadBalance &rhs) {
     214          30 :         use_global_vrouter_ = rhs.use_global_vrouter_;
     215          30 :         EcmpLoadBalance::Copy(rhs);
     216          30 :     }
     217             : 
     218             : private:
     219             :     bool use_global_vrouter_;
     220             : };
     221             : 
     222             : class EcmpField {
     223             : public:
     224        1765 :     EcmpField() {
     225        1765 :         ref_count_= 0;
     226        1765 :     }
     227             : 
     228         515 :     uint32_t RefCount() const {
     229         515 :         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        1765 : inline void intrusive_ptr_add_ref(EcmpField* ptr) {
     238        1765 :     ptr->ref_count_++;
     239        1765 : }
     240             : 
     241        1765 : inline void intrusive_ptr_release(EcmpField* ptr) {
     242        1765 :     uint32_t prev = ptr->ref_count_.fetch_sub(1);
     243        1765 :     if (prev == 1) {
     244        1765 :       delete ptr;
     245             :     }
     246        1765 : }
     247             : 
     248             : class EcmpHashFields {
     249             : public:
     250             :     typedef boost::intrusive_ptr<EcmpField> EcmpFieldPtr;
     251             : 
     252        1570 :     EcmpHashFields() {
     253        1570 :     }
     254             : 
     255         140 :     EcmpHashFields(const uint8_t hash_fields_to_use ) {
     256         140 :        hash_fields_to_use_ = hash_fields_to_use;
     257         140 :     }
     258         217 :     void  operator = (const uint8_t hash_fields_to_use) {
     259         217 :        hash_fields_to_use_ = hash_fields_to_use;
     260         217 :     }
     261             : 
     262         353 :     void AllocateEcmpFields() {
     263         353 :         sip_ = new EcmpField;
     264         353 :         dip_ = new EcmpField;
     265         353 :         proto_ = new EcmpField;
     266         353 :         sport_ = new EcmpField;
     267         353 :         dport_ = new EcmpField;
     268         353 :     }
     269             : 
     270        1614 :     uint8_t HashFieldsToUse() const {
     271        1614 :         return hash_fields_to_use_;
     272             :     }
     273             : 
     274         515 :     void SetHashFieldtoUse(EcmpField *ptr, uint8_t key) {
     275         515 :         if (ptr && ptr->RefCount() == 1) {
     276         515 :             comp_hash_fields_to_use_ |= 1 << key;
     277             :         }
     278         515 :     }
     279             :     // If the field is not set create intrusive pointer
     280             :     // else release the pointer.
     281           0 :     void SetChangeInHashField(bool is_field_set, EcmpFieldPtr& fieldPtr,
     282             :                               EcmpFieldPtr &objFieldPtr) {
     283           0 :         if (!is_field_set) {
     284           0 :             if (!objFieldPtr.get()) {
     285           0 :                 objFieldPtr = fieldPtr;
     286             :             }
     287             :         } else {
     288           0 :             objFieldPtr.reset();
     289             :         }
     290           0 :     }
     291             :     // This function will be called to ge intersection of ecmp fields
     292         103 :     uint8_t  CalculateHashFieldsToUse() {
     293         103 :         comp_hash_fields_to_use_ = 0;
     294         103 :         SetHashFieldtoUse(sip_.get(), EcmpLoadBalance::SOURCE_IP);
     295         103 :         SetHashFieldtoUse(dip_.get(), EcmpLoadBalance::DESTINATION_IP);
     296         103 :         SetHashFieldtoUse(proto_.get(), EcmpLoadBalance::IP_PROTOCOL);
     297         103 :         SetHashFieldtoUse(sport_.get(), EcmpLoadBalance::SOURCE_PORT);
     298         103 :         SetHashFieldtoUse(dport_.get(), EcmpLoadBalance::DESTINATION_PORT);
     299         103 :         return comp_hash_fields_to_use_;
     300             :     }
     301             :     //This function used to calculate the Change in ecmp fields
     302           0 :     void CalculateChangeInEcmpFields(const EcmpLoadBalance &ecmp_load_balance,
     303             :                                      EcmpHashFields& ecmp_hash_fields) {
     304           0 :         SetChangeInHashField(ecmp_load_balance.is_source_ip_set(),
     305           0 :                              ecmp_hash_fields.sip_, sip_);
     306           0 :         SetChangeInHashField(ecmp_load_balance.is_destination_ip_set(),
     307           0 :                              ecmp_hash_fields.dip_, dip_);
     308           0 :         SetChangeInHashField(ecmp_load_balance.is_ip_protocol_set(),
     309           0 :                              ecmp_hash_fields.proto_, proto_);
     310           0 :         SetChangeInHashField(ecmp_load_balance.is_source_port_set(),
     311           0 :                              ecmp_hash_fields.sport_, sport_);
     312           0 :         SetChangeInHashField(ecmp_load_balance.is_destination_port_set(),
     313           0 :                              ecmp_hash_fields.dport_, dport_);
     314           0 :     }
     315             : 
     316         103 :     bool IsFieldsInUseChanged() {
     317         103 :         return hash_fields_to_use_ != CalculateHashFieldsToUse();
     318             :     }
     319             : 
     320          41 :     void SetHashFieldstoUse() {
     321          41 :         hash_fields_to_use_ = comp_hash_fields_to_use_;
     322          41 :     }
     323             : 
     324          11 :     void Reset() {
     325          11 :         sip_ = NULL;
     326          11 :         dip_ = NULL;
     327          11 :         proto_ = NULL;
     328          11 :         sport_ = NULL;
     329          11 :         dport_ = NULL;
     330          11 :     }
     331             : 
     332             : private:
     333             :     // This will have latest computed value
     334             :     uint8_t comp_hash_fields_to_use_;
     335             :     uint8_t hash_fields_to_use_;
     336             :     EcmpFieldPtr sip_;
     337             :     EcmpFieldPtr dip_;
     338             :     EcmpFieldPtr proto_;
     339             :     EcmpFieldPtr sport_;
     340             :     EcmpFieldPtr dport_;
     341             :     DISALLOW_COPY_AND_ASSIGN(EcmpHashFields);
     342             : };
     343             : #endif

Generated by: LCOV version 1.14