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