OpenSDN source code
bgp_attr_base.cc
Go to the documentation of this file.
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 BgpProtoPrefix::BgpProtoPrefix() : prefixlen(0), type(0) {
13 }
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 uint32_t BgpProtoPrefix::ReadLabel(size_t label_offset, bool is_vni) const {
20  assert((label_offset + kLabelSize) <= prefix.size());
21  if (is_vni)
22  return get_value(&prefix[label_offset], kLabelSize);
23  uint32_t label = (prefix[label_offset] << 16 |
24  prefix[label_offset + 1] << 8 |
25  prefix[label_offset + 2]) >> 4;
26  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 void BgpProtoPrefix::WriteLabel(size_t label_offset, uint32_t label,
34  bool is_vni) {
35  assert((label_offset + kLabelSize) <= prefix.size());
36  if (is_vni) {
37  put_value(&prefix[label_offset], kLabelSize, label);
38  return;
39  }
40  uint32_t tmp = (label << 4 | 0x1);
41  for (size_t idx = 0; idx < kLabelSize; ++idx) {
42  int offset = (kLabelSize - (idx + 1)) * 8;
43  prefix[label_offset + idx] = ((tmp >> offset) & 0xff);
44  }
45 }
46 
47 int BgpAttribute::CompareTo(const BgpAttribute &rhs) const {
48  KEY_COMPARE(code, rhs.code);
51  return 0;
52 }
53 
55  return 0;
56 }
57 
59  uint8_t value = flags;
60  if (EncodeLength() >= sizeof(uint8_t) << 8) {
62  }
63  return value;
64 }
65 
66 std::string BgpAttribute::ToString() const {
67  char repr[80];
68  snprintf(repr, sizeof(repr), "<code: %d, flags: %02x>", code, flags);
69  return std::string(repr);
70 }
uint8_t subcode
Definition: bgp_attr_base.h:71
uint8_t flags
Definition: bgp_attr_base.h:72
virtual int CompareTo(const BgpAttribute &rhs) const
uint8_t GetEncodeFlags() const
uint8_t code
Definition: bgp_attr_base.h:70
virtual size_t EncodeLength() const
virtual std::string ToString() const
uint8_t type
Definition: load_balance.h:2
static void put_value(uint8_t *data, int size, uint64_t value)
Definition: parse_object.h:56
static uint64_t get_value(const uint8_t *data, int size)
Definition: parse_object.h:40
void WriteLabel(size_t label_offset, uint32_t label, bool is_vni=false)
uint32_t ReadLabel(size_t label_offset, bool is_vni=false) const
static const size_t kLabelSize
std::vector< uint8_t > prefix
#define KEY_COMPARE(x, y)
Definition: util.h:71