OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bgp_attr_base.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include "bgp/bgp_attr_base.h"
6 
7 #include <cstdio>
8 
9 const size_t BgpProtoPrefix::kLabelSize = 3;
10 
11 BgpProtoPrefix::BgpProtoPrefix() : prefixlen(0), type(0) {
12 }
13 
14 //
15 // Extract the label from the BgpProtorefix.
16 // EVPN extensions for VXLAN use the label to convey a 24-bit VNI.
17 //
18 uint32_t BgpProtoPrefix::ReadLabel(size_t label_offset, bool is_vni) const {
19  assert((label_offset + kLabelSize) <= prefix.size());
20  if (is_vni)
21  return get_value(&prefix[label_offset], kLabelSize);
22  uint32_t label = (prefix[label_offset] << 16 |
23  prefix[label_offset + 1] << 8 |
24  prefix[label_offset + 2]) >> 4;
25  return label;
26 }
27 
28 //
29 // Write the label to the BgpProtorefix.
30 // EVPN extensions for VXLAN use the label to convey a 24-bit VNI.
31 //
32 void BgpProtoPrefix::WriteLabel(size_t label_offset, uint32_t label,
33  bool is_vni) {
34  assert((label_offset + kLabelSize) <= prefix.size());
35  if (is_vni) {
36  put_value(&prefix[label_offset], kLabelSize, label);
37  return;
38  }
39  uint32_t tmp = (label << 4 | 0x1);
40  for (size_t idx = 0; idx < kLabelSize; ++idx) {
41  int offset = (kLabelSize - (idx + 1)) * 8;
42  prefix[label_offset + idx] = ((tmp >> offset) & 0xff);
43  }
44 }
45 
46 int BgpAttribute::CompareTo(const BgpAttribute &rhs) const {
47  KEY_COMPARE(code, rhs.code);
50  return 0;
51 }
52 
54  return 0;
55 }
56 
58  uint8_t value = flags;
59  if (EncodeLength() >= sizeof(uint8_t) << 8) {
61  }
62  return value;
63 }
64 
65 std::string BgpAttribute::ToString() const {
66  char repr[80];
67  snprintf(repr, sizeof(repr), "<code: %d, flags: %02x>", code, flags);
68  return std::string(repr);
69 }
virtual std::string ToString() const
void WriteLabel(size_t label_offset, uint32_t label, bool is_vni=false)
#define KEY_COMPARE(x, y)
Definition: util.h:70
uint8_t flags
Definition: bgp_attr_base.h:70
uint32_t ReadLabel(size_t label_offset, bool is_vni=false) const
uint8_t GetEncodeFlags() const
virtual size_t EncodeLength() const
static uint64_t get_value(const uint8_t *data, int size)
Definition: parse_object.h:39
uint8_t type
Definition: load_balance.h:109
virtual int CompareTo(const BgpAttribute &rhs) const
static const size_t kLabelSize
Definition: bgp_attr_base.h:98
std::vector< uint8_t > prefix
uint8_t subcode
Definition: bgp_attr_base.h:69
uint8_t code
Definition: bgp_attr_base.h:68
static void put_value(uint8_t *data, int size, uint64_t value)
Definition: parse_object.h:55