Line data Source code
1 : /* 2 : * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include "bgp/bgp_attr_base.h" 7 : 8 : #include <cstdio> 9 : 10 : const size_t BgpProtoPrefix::kLabelSize = 3; 11 : 12 2100992 : BgpProtoPrefix::BgpProtoPrefix() : prefixlen(0), type(0) { 13 2100997 : } 14 : 15 : // 16 : // Extract the label from the BgpProtorefix. 17 : // EVPN extensions for VXLAN use the label to convey a 24-bit VNI. 18 : // 19 135335 : uint32_t BgpProtoPrefix::ReadLabel(size_t label_offset, bool is_vni) const { 20 135335 : assert((label_offset + kLabelSize) <= prefix.size()); 21 135335 : if (is_vni) 22 133 : return get_value(&prefix[label_offset], kLabelSize); 23 135202 : uint32_t label = (prefix[label_offset] << 16 | 24 135202 : prefix[label_offset + 1] << 8 | 25 135202 : prefix[label_offset + 2]) >> 4; 26 135202 : return label; 27 : } 28 : 29 : // 30 : // Write the label to the BgpProtorefix. 31 : // EVPN extensions for VXLAN use the label to convey a 24-bit VNI. 32 : // 33 159720 : void BgpProtoPrefix::WriteLabel(size_t label_offset, uint32_t label, 34 : bool is_vni) { 35 159720 : assert((label_offset + kLabelSize) <= prefix.size()); 36 159724 : if (is_vni) { 37 165 : put_value(&prefix[label_offset], kLabelSize, label); 38 165 : return; 39 : } 40 159559 : uint32_t tmp = (label << 4 | 0x1); 41 638216 : for (size_t idx = 0; idx < kLabelSize; ++idx) { 42 478654 : int offset = (kLabelSize - (idx + 1)) * 8; 43 478654 : prefix[label_offset + idx] = ((tmp >> offset) & 0xff); 44 : } 45 : } 46 : 47 597903 : int BgpAttribute::CompareTo(const BgpAttribute &rhs) const { 48 597903 : KEY_COMPARE(code, rhs.code); 49 597903 : KEY_COMPARE(subcode, rhs.subcode); 50 597903 : KEY_COMPARE(flags & ~ExtendedLength, rhs.flags & ~ExtendedLength); 51 597903 : return 0; 52 : } 53 : 54 751451 : size_t BgpAttribute::EncodeLength() const { 55 751451 : return 0; 56 : } 57 : 58 1992351 : uint8_t BgpAttribute::GetEncodeFlags() const { 59 1992351 : uint8_t value = flags; 60 1992351 : if (EncodeLength() >= sizeof(uint8_t) << 8) { 61 69742 : value |= BgpAttribute::ExtendedLength; 62 : } 63 1992337 : return value; 64 : } 65 : 66 158887 : std::string BgpAttribute::ToString() const { 67 : char repr[80]; 68 158887 : snprintf(repr, sizeof(repr), "<code: %d, flags: %02x>", code, flags); 69 158887 : return std::string(repr); 70 : }