OpenSDN source code
vxlan_templates.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <oper/bgp_as_service.h>
7 
8 template <class ItType>
9 std::vector<IpAddress> VxlanRoutingManager::ItemNexthopsToVector(ItType *item) {
10  std::vector<IpAddress> nh_addr;
11  const uint32_t n_items = item->entry.next_hops.next_hop.size();
12  for (uint32_t i_nh=0; i_nh < n_items; i_nh++) {
13  const IpAddress nh_ip = IpAddress::from_string(
14  item->entry.next_hops.next_hop[i_nh].address);
15  nh_addr.insert(nh_addr.end(), nh_ip);
16  }
17 
18  return nh_addr;
19 }
20 
21 // A nexthop is counted as BGPaaS when it has MPLS label and this
22 // label points to the VmInterface linked with the BGPaaS object
23 static bool IsBgpaasInterfaceNexthop(const Agent* agent, const NextHop* nh) {
24  if (nh->GetType() != NextHop::INTERFACE ||
25  nh->mpls_label() == NULL) {
26  return false;
27  }
28 
29  const InterfaceNH *intf_nh = static_cast<const InterfaceNH *>(nh);
30  const Interface *interface = intf_nh->GetInterface();
31  if (interface->type() != Interface::VM_INTERFACE)
32  return false;
33 
34  const VmInterface *vm_intf =
35  dynamic_cast<const VmInterface*>(interface);
36  const Ip4Address& intf_ip4 = vm_intf->primary_ip_addr();
37  const Ip6Address& intf_ip6 = vm_intf->primary_ip6_addr();
38 
39  if (agent->oper_db()->bgp_as_a_service()->
40  IsBgpService(vm_intf, IpAddress(intf_ip4), IpAddress(intf_ip4))) {
41  return true;
42  }
43  if (agent->oper_db()->bgp_as_a_service()->
44  IsBgpService(vm_intf, IpAddress(intf_ip6), IpAddress(intf_ip6))) {
45  return true;
46  }
47  return false;
48 }
49 
50 // A composite nexthop is counted as BGPaaS when at least one component is
51 // an interface
52 static bool IsBgpaasCompositeNexthop(const Agent* agent, const NextHop* nh) {
53  if (nh->GetType() != NextHop::COMPOSITE)
54  return false;
55  const CompositeNH *composite_nh = dynamic_cast<const CompositeNH*>(nh);
56  uint32_t n_comps = composite_nh->ComponentNHCount();
57  for (uint32_t i=0; i < n_comps; i++) {
58  const NextHop *c_nh = composite_nh->GetNH(i);
59  if (c_nh != nullptr &&
60  IsBgpaasInterfaceNexthop(agent, c_nh) == true) {
61  return true;
62  }
63  }
64  return false;
65 }
66 
67 template<typename NhType>
69  const std::string& vrf_name,
70  const NhType &nh_item,
71  ComponentNHKeyList& comp_nh_list,
72  std::vector<std::string> &peer_sources) {
73 
74  for (const auto &nexthop_addr : peer_sources) {
75  const Agent *agent = Agent::GetInstance();
76  IpAddress nh_ip;
77  uint32_t prefix_len;
78  boost::system::error_code ec;
79 
80  if (is_ipv4_string(nexthop_addr)) {
81  nh_ip = Ip4Address::from_string(ipv4_prefix(nexthop_addr), ec);
82  prefix_len = ipv4_prefix_len(nexthop_addr);
83  } else if (is_ipv6_string(nexthop_addr)) {
84  nh_ip = Ip6Address::from_string(ipv6_prefix(nexthop_addr), ec);
85  prefix_len = ipv6_prefix_len(nexthop_addr);
86  } else {
87  LOG(ERROR, "Error in VxlanRoutingManager"
88  << "::AddBgpaasInterfaceComponentToList"
89  << ", nexthop_addr = " << nexthop_addr
90  << " is not an IPv4 or IPv6 prefix");
91  return;
92  }
93  if (ec) {
94  continue;
95  }
96  const AgentRoute *intf_rt =
97  FindEvpnOrInetRoute(agent, vrf_name, nh_ip, prefix_len, nh_item);
98  if (intf_rt == nullptr) {
99  continue;
100  }
101  const AgentPath *loc_path =
103  if (loc_path == nullptr) {
104  continue;
105  }
106  if (loc_path->nexthop() == nullptr) {
107  continue;
108  }
109  if (IsBgpaasInterfaceNexthop(agent, loc_path->nexthop())) {
110  DBEntryBase::KeyPtr key_ptr =
111  loc_path->nexthop()->GetDBRequestKey();
112  NextHopKey *nh_key =
113  static_cast<NextHopKey *>(key_ptr.release());
114  std::unique_ptr<const NextHopKey> nh_key_ptr(nh_key);
115  ComponentNHKeyPtr component_nh_key(new
117  std::move(nh_key_ptr)));
118  comp_nh_list.push_back(component_nh_key);
119  } else if (IsBgpaasCompositeNexthop(agent, loc_path->nexthop())) {
120  CompositeNH *loc_comp_nh = dynamic_cast<CompositeNH*>
121  (loc_path->nexthop());
122 
123  DBEntryBase::KeyPtr key_ptr =
124  loc_comp_nh->GetDBRequestKey();
125  CompositeNHKey *nh_key =
126  static_cast<CompositeNHKey *>(key_ptr.release());
127  std::unique_ptr<const NextHopKey> nh_key_ptr(nh_key);
128 
129  if (nh_key == nullptr){
130  LOG(ERROR, "Error in VxlanRoutingManager::"
131  << "::AddBgpaasInterfaceComponentToList"
132  << ", null nh key");
133  }
134 
135  const ComponentNHList& component_nh_list =
136  loc_comp_nh->component_nh_list();
137  for (auto &component_nh : component_nh_list) {
138  std::unique_ptr<const NextHopKey> nh_key_ptr;
139  ComponentNHKeyPtr component_nh_key;
140  if (component_nh.get() == nullptr) {
141  // component_nh_key.reset(NULL);
142  } else {
143  DBEntryBase::KeyPtr key =
144  component_nh.get()->nh()->GetDBRequestKey();
145  NextHopKey *nh_key =
146  static_cast<NextHopKey *>(key.release());
147  nh_key_ptr.reset(nh_key);
148  component_nh_key.reset(
150  std::move(nh_key_ptr)));
151  }
152  comp_nh_list.push_back(component_nh_key);
153  }
154  }
155  }
156 }
157 
158 template<typename NhType>
160  const std::string& prefix_str,
161  const std::string& vrf_name,
162  const NhType &nh_item,
163  ComponentNHKeyList& comp_nh_list,
164  std::vector<std::string> &peer_sources) {
165  const Agent *agent = Agent::GetInstance();
166  IpAddress ip_addr;
167  uint32_t prefix_len;
168  boost::system::error_code ec;
169 
170  if (is_ipv4_string(prefix_str)) {
171  ip_addr = Ip4Address::from_string(ipv4_prefix(prefix_str), ec);
172  prefix_len = ipv4_prefix_len(prefix_str);
173  } else if (is_ipv6_string(prefix_str)) {
174  std::string addr_str = ipv6_prefix(prefix_str);
175  prefix_len = ipv6_prefix_len(prefix_str);
176  ip_addr = Ip6Address::from_string(addr_str, ec);
177  } else {
178  LOG(ERROR, "Error in VxlanRoutingManager::AddInterfaceComponentToList"
179  << ", prefix_str = " << prefix_str
180  << " is not an IPv4 or IPv6 prefix");
181  return;
182  }
183 
184  if (ec) {
185  LOG(ERROR, "Possible error in "
186  << "VxlanRoutingManager::AddInterfaceComponentToList"
187  << ", cannot convert prefix_str = " << prefix_str
188  << " to IPv4 or IPv6 address");
189  return;
190  }
191 
192  const AgentRoute *intf_rt =
193  FindEvpnOrInetRoute(agent, vrf_name, ip_addr, prefix_len, nh_item);
194  if (intf_rt == NULL) {
195  AddBgpaasInterfaceComponentToList(vrf_name, nh_item, comp_nh_list,
196  peer_sources);
197  return;
198  }
199 
200  const AgentPath *loc_path =
202  if (loc_path == NULL) {
203  AddBgpaasInterfaceComponentToList(vrf_name, nh_item, comp_nh_list,
204  peer_sources);
205  return;
206  }
207  if (loc_path->nexthop() == NULL) {
208  AddBgpaasInterfaceComponentToList(vrf_name, nh_item, comp_nh_list,
209  peer_sources);
210  return;
211  }
212 
213  // Case 1. NextHop is an interface
214  if (loc_path->nexthop()->GetType() == NextHop::INTERFACE) {
215  DBEntryBase::KeyPtr key_ptr =
216  loc_path->nexthop()->GetDBRequestKey();
217  NextHopKey *nh_key =
218  static_cast<NextHopKey *>(key_ptr.release());
219  std::unique_ptr<const NextHopKey> nh_key_ptr(nh_key);
220  ComponentNHKeyPtr component_nh_key(new ComponentNHKey(MplsTable::kInvalidLabel, // label
221  std::move(nh_key_ptr)));
222  comp_nh_list.push_back(component_nh_key);
223  return;
224  }
225 
226  // Case 2. NextHop is a composite of interfaces
227  // Copy all interfaces from this composite
228  // into the components list
229  if (loc_path->nexthop()->GetType() == NextHop::COMPOSITE) {
230  CompositeNH *loc_comp_nh = dynamic_cast<CompositeNH*>
231  (loc_path->nexthop());
232 
233  DBEntryBase::KeyPtr key_ptr =
234  loc_comp_nh->GetDBRequestKey();
235  CompositeNHKey *nh_key =
236  static_cast<CompositeNHKey *>(key_ptr.release());
237  std::unique_ptr<const NextHopKey> nh_key_ptr(nh_key);
238 
239  if (nh_key == NULL){
240  LOG(ERROR, "Error in VxlanRoutingManager::AddInterfaceComponentToList"
241  << ", null nh key");
242  assert(nh_key != NULL);
243  }
244 
245  // Refresh on path_preference.sequence change
246  const ComponentNHList& component_nh_list =
247  loc_comp_nh->component_nh_list();
248  for (ComponentNHList::const_iterator
249  it_nh = component_nh_list.begin();
250  it_nh != component_nh_list.end(); it_nh++) {
251  // nullptr means deleted component, which
252  // can be reused later
253  std::unique_ptr<const NextHopKey> nh_key_ptr;
254  ComponentNHKeyPtr component_nh_key;
255  if (it_nh->get() == NULL) {
256  // component_nh_key.reset(NULL);
257  } else {
258  DBEntryBase::KeyPtr key =
259  it_nh->get()->nh()->GetDBRequestKey();
260  NextHopKey *nh_key =
261  static_cast<NextHopKey *>(key.release());
262  nh_key_ptr.reset(nh_key);
263  component_nh_key.reset(
264  new ComponentNHKey(MplsTable::kInvalidLabel, std::move(nh_key_ptr)));
265  }
266  comp_nh_list.push_back(component_nh_key);
267  }
268  }
269 } // AddInterfaceComponentToList func
270 
271 //
272 // END-OF-FILE
273 //
boost::asio::ip::address_v6 Ip6Address
Definition: address.h:16
boost::asio::ip::address IpAddress
Definition: address.h:14
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
NextHop * nexthop() const
Definition: agent_path.cc:88
Base class for all Route entries in agent.
Definition: agent_route.h:225
const AgentPath * FindIntfOrCompLocalVmPortPath() const
Finds path to an interface or a composite of interfaces and returns it. The priority is given to comp...
Definition: agent_route.cc:818
Definition: agent.h:361
OperDB * oper_db() const
Definition: agent.cc:1017
static Agent * GetInstance()
Definition: agent.h:439
size_t ComponentNHCount() const
Definition: nexthop.h:1816
const ComponentNHList & component_nh_list() const
Definition: nexthop.h:1870
const NextHop * GetNH(uint32_t idx) const
Definition: nexthop.h:1833
virtual KeyPtr GetDBRequestKey() const
Definition: nexthop.cc:2184
std::unique_ptr< DBRequestKey > KeyPtr
Definition: db_entry.h:25
virtual KeyPtr GetDBRequestKey() const =0
@ VM_INTERFACE
Definition: interface.h:38
static const uint32_t kInvalidLabel
Definition: mpls.h:102
const MplsLabel * mpls_label() const
Definition: nexthop.h:435
@ COMPOSITE
Definition: nexthop.h:355
@ INTERFACE
Definition: nexthop.h:352
Type GetType() const
Definition: nexthop.h:406
BgpAsAService * bgp_as_a_service() const
Definition: operdb_init.h:78
const Ip4Address & primary_ip_addr() const
const Ip6Address & primary_ip6_addr() const
static void AddBgpaasInterfaceComponentToList(const std::string &vrf_name, const NhType &nh_item, ComponentNHKeyList &comp_nh_list, std::vector< std::string > &peer_sources)
static bool is_ipv4_string(const std::string &prefix_str)
Determines whether the address string contains an IPv4 address as substring or not.
static AgentRoute * FindEvpnOrInetRoute(const Agent *agent, const std::string &vrf_name, const IpAddress &ip_addr, uint32_t prefix_len, const autogen::EnetNextHopType &nh_item)
Finds a route with the given prefix address and len in the EVPN table.
static std::string ipv6_prefix(const std::string &prefix_str)
Extracts an IPv6 address string from the prefix string.
static std::vector< IpAddress > ItemNexthopsToVector(ItType *item)
Templates.
static uint32_t ipv4_prefix_len(const std::string &prefix_str)
Extracts length of IPv4 subnet address from the prefix string.
static void AddInterfaceComponentToList(const std::string &prefix_str, const std::string &vrf_name, const NhType &nh_item, ComponentNHKeyList &comp_nh_list, std::vector< std::string > &peer_sources)
Adds an interface or a composite of interfaces nexthops to the list of components NH keys needed for ...
static bool is_ipv6_string(const std::string &prefix_str)
Determines whether the address string contains an IPv6 address as substring or not.
static std::string ipv4_prefix(const std::string &prefix_str)
Extracts an IPv4 address string from the prefix string.
static uint32_t ipv6_prefix_len(const std::string &prefix_str)
Extracts length of IPv6 subnet address from the prefix string.
#define LOG(_Level, _Msg)
Definition: logging.h:35
std::vector< ComponentNHPtr > ComponentNHList
Definition: nexthop.h:1638
boost::shared_ptr< const ComponentNHKey > ComponentNHKeyPtr
Definition: nexthop.h:1640
std::vector< ComponentNHKeyPtr > ComponentNHKeyList
Definition: nexthop.h:1642
static bool IsBgpaasCompositeNexthop(const Agent *agent, const NextHop *nh)
static bool IsBgpaasInterfaceNexthop(const Agent *agent, const NextHop *nh)