LCOV - code coverage report
Current view: top level - bgp/extended-community - load_balance.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 5 5 100.0 %
Date: 2026-11-09 02:10:49 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #ifndef SRC_BGP_EXTENDED_COMMUNITY_LOAD_BALANCE_H_
       7             : #define SRC_BGP_EXTENDED_COMMUNITY_LOAD_BALANCE_H_
       8             : 
       9             : #include <boost/array.hpp>
      10             : #include <boost/system/error_code.hpp>
      11             : 
      12             : #include <endian.h>
      13             : #include <string>
      14             : 
      15             : #include "base/parse_object.h"
      16             : #include "bgp/bgp_path.h"
      17             : #include "bgp/extended-community/types.h"
      18             : #include "bgp/bgp_peer_types.h"
      19             : #include "schema/xmpp_unicast_types.h"
      20             : 
      21             : /*
      22             :  * BGP LoadBalance Opaque Extended Community with SubType 0xAA (TBA)
      23             :  *
      24             :  *  0                   1                   2                   3
      25             :  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      26             :  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      27             :  * | Type  0x03    | Sub-Type 0xAA |s d c p P R R R|R R R R R R R R|
      28             :  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      29             :  * | Reserved      |B R R R R R R R| Reserved      | Reserved      |
      30             :  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      31             :  *
      32             :  * Type: 0x03 Opaque
      33             :  * SubType: 0xAA LoadBalance attribute information (TBA)
      34             :  * s: Use l3_source_address ECMP Load-balancing
      35             :  * d: Use l3_destination_address ECMP Load-balancing
      36             :  * c: Use l4_protocol ECMP Load-balancing
      37             :  * p: Use l4_source_port ECMP Load-balancing
      38             :  * P: Use l4_destination_port ECMP Load-balancing
      39             :  * B: Use source_bias (instead of ECMP load-balancing)
      40             :  * R: Reserved
      41             : */
      42             : class LoadBalance {
      43             : public:
      44             :     static const int kSize = 8;
      45             :     typedef boost::array<uint8_t, kSize> bytes_type;
      46             : 
      47             :     struct LoadBalanceAttribute {
      48             :         static const LoadBalanceAttribute kDefaultLoadBalanceAttribute;
      49             :         union {
      50             :             struct {
      51             : #if BYTE_ORDER == BIG_ENDIAN
      52             :                 // Opaque extended community header
      53             :                 uint8_t type;
      54             :                 uint8_t sub_type;
      55             : 
      56             :                 // ecmp hash fields selection list
      57             :                 uint8_t l3_source_address:1;      // Set by default
      58             :                 uint8_t l3_destination_address:1; // Set by default
      59             :                 uint8_t l4_protocol:1;            // Set by default
      60             :                 uint8_t l4_source_port:1;         // Set by default
      61             :                 uint8_t l4_destination_port:1;    // Set by default
      62             :                 uint8_t reserved1:3;
      63             : 
      64             :                 uint8_t  reserved2; // For future fields such as interface
      65             : 
      66             :                 // Misc bool actions
      67             :                 uint8_t  source_bias:1;
      68             :                 uint8_t  reserved3:7;
      69             : 
      70             :                 uint8_t  reserved4;
      71             :                 uint8_t  reserved5;
      72             :                 uint8_t  reserved6;
      73             : #else
      74             :                 uint8_t  reserved2; // For future fields such as interface
      75             : 
      76             :                 // ecmp hash fields selection list
      77             :                 uint8_t reserved1:3;
      78             :                 uint8_t l4_destination_port:1;    // Set by default
      79             :                 uint8_t l4_source_port:1;         // Set by default
      80             :                 uint8_t l4_protocol:1;            // Set by default
      81             :                 uint8_t l3_destination_address:1; // Set by default
      82             :                 uint8_t l3_source_address:1;      // Set by default
      83             : 
      84             :                 // Opaque extended community header
      85             :                 uint8_t sub_type;
      86             :                 uint8_t type;
      87             : 
      88             :                 uint8_t  reserved6;
      89             :                 uint8_t  reserved5;
      90             :                 uint8_t  reserved4;
      91             : 
      92             :                 // Misc bool actions
      93             :                 uint8_t  reserved3:7;
      94             :                 uint8_t  source_bias:1;
      95             : #endif
      96             :             } __attribute__((packed));
      97             :             struct {
      98             :                 uint32_t value1;
      99             :                 uint32_t value2;
     100             :             } __attribute__((packed));
     101             :         };
     102             :         LoadBalanceAttribute();
     103             :         LoadBalanceAttribute(uint32_t value1, uint32_t value2);
     104             :         void Encode(autogen::LoadBalanceType *lb_type) const;
     105             :         bool operator==(const LoadBalanceAttribute &other) const;
     106             :         bool operator!=(const LoadBalanceAttribute &other) const;
     107             :         const bool IsDefault() const;
     108             :     };
     109             : 
     110             :     LoadBalance();
     111             :     explicit LoadBalance(const bytes_type &data);
     112             :     explicit LoadBalance(const LoadBalanceAttribute &attr);
     113             :     explicit LoadBalance(const autogen::LoadBalanceType &lb_type);
     114             :     explicit LoadBalance(const BgpPath *path);
     115             : 
     116             :     bool operator==(const LoadBalance &other) const;
     117             :     bool operator!=(const LoadBalance &other) const;
     118           6 :     uint8_t Type() const { return data_[0]; }
     119           6 :     uint8_t Subtype() const { return data_[1]; }
     120          75 :     const bytes_type &GetExtCommunity() const { return data_; }
     121         142 :     const uint64_t GetExtCommunityValue() const {
     122         142 :         return get_value(data_.begin(), 8);
     123             :     }
     124             :     const LoadBalanceAttribute ToAttribute() const;
     125             :     void FillAttribute(LoadBalanceAttribute *attr);
     126             :     const bool IsDefault() const;
     127             :     void ShowAttribute(ShowLoadBalance *show_load_balance) const;
     128             :     std::string ToString() const;
     129             :     void SetL3SourceAddress(bool flag);
     130             :     void SetL3DestinationAddress(bool flag);
     131             :     void SetL4Protocol(bool flag);
     132             :     void SetL4SourcePort(bool flag);
     133             :     void SetL4DestinationPort(bool flag);
     134             :     void SetSourceBias(bool flag);
     135             : 
     136             :     static bool IsPresent(const BgpPath *path);
     137             : 
     138             : private:
     139             : 
     140             :     bytes_type data_;
     141             : };
     142             : 
     143             : #endif  // SRC_BGP_EXTENDED_COMMUNITY_LOAD_BALANCE_H_

Generated by: LCOV version 1.14